Files: 54f45b9e3a2114fd3c234a8de046349c9a3dab1c / src / Util.js
1031 bytesRaw
1 | import { default as pull } from 'pull-stream' |
2 | import fetch from 'isomorphic-fetch' |
3 | |
4 | export default class Util { |
5 | |
6 | constructor(sbot) { |
7 | this.sbot = sbot |
8 | } |
9 | |
10 | getBlob(blobId) { |
11 | function Utf8ArrayToStr(array) { |
12 | return new Promise((resolve, reject) => { |
13 | var bb = new Blob([array]); |
14 | var f = new FileReader(); |
15 | f.onload = function(e) { |
16 | resolve(e.target.result); |
17 | }; |
18 | f.readAsText(bb); |
19 | }) |
20 | } |
21 | |
22 | |
23 | |
24 | return new Promise((resolve, reject) => { |
25 | this.sbot.blobs.want(blobId).then(() => { |
26 | pull( |
27 | this.sbot.blobs.get(blobId), |
28 | pull.collect((err, values) => { |
29 | if (err) reject(err) |
30 | Promise.all(values.map(v => Utf8ArrayToStr(v))).then(s => s.join('')).then(resolve) |
31 | }) |
32 | ) |
33 | }) |
34 | }) |
35 | } |
36 | |
37 | dereferenceUriOrSigil(uriOrSigil) { |
38 | if (uriOrSigil.startsWith('&')) { |
39 | return this.getBlob(uriOrSigil) |
40 | } else { |
41 | return fetch(uriOrSigil).then(response => response.text()) |
42 | } |
43 | } |
44 | } |
Built with git-ssb-web