|
| 1 | +'use strict' |
| 2 | +/* eslint-env browser, webextensions */ |
| 3 | +const pull = require('pull-stream/pull') |
| 4 | +const drain = require('pull-stream/sinks/drain') |
| 5 | +const toStream = require('it-to-stream') |
| 6 | +const tar = require('tar-stream') |
| 7 | +const CID = require('cids') |
| 8 | +const { webuiCid } = require('./state') |
| 9 | + |
| 10 | +const debug = require('debug') |
| 11 | +const log = debug('ipfs-companion:precache') |
| 12 | +log.error = debug('ipfs-companion:precache:error') |
| 13 | + |
| 14 | +const PRECACHE_ARCHIVES = [ |
| 15 | + { tarPath: '/dist/precache/webui.tar', cid: webuiCid } |
| 16 | +] |
| 17 | + |
| 18 | +/** |
| 19 | + * Adds important assets such as Web UI to the local js-ipfs-repo. |
| 20 | + * This ensures they load instantly, even in offline environments. |
| 21 | + */ |
| 22 | +module.exports = async (ipfs) => { |
| 23 | + for (const { cid, tarPath } of PRECACHE_ARCHIVES) { |
| 24 | + if (!await inRepo(ipfs, cid)) { |
| 25 | + await importTar(ipfs, tarPath, cid) |
| 26 | + } else { |
| 27 | + log(`${cid} already in local repo, skipping import`) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +async function inRepo (ipfs, cid) { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + let local = false |
| 35 | + pull( |
| 36 | + ipfs.refs.localPullStream(), |
| 37 | + drain(block => { |
| 38 | + if (block.ref === cid) { |
| 39 | + local = true |
| 40 | + return false // abort stream |
| 41 | + } |
| 42 | + }, () => resolve(local)) |
| 43 | + ) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +async function importTar (ipfs, tarPath, expectedCid) { |
| 48 | + const stream = toStream.readable(streamTar(tarPath)) |
| 49 | + // TODO: HTTP 404 means precache is disabled in the current runtime |
| 50 | + // (eg. in Firefox, due to https://github.com/ipfs-shipyard/ipfs-webui/issues/959) |
| 51 | + const untarAndAdd = tar.extract() |
| 52 | + |
| 53 | + const files = [] |
| 54 | + |
| 55 | + untarAndAdd.on('entry', (header, stream, next) => { |
| 56 | + // header is the tar header |
| 57 | + // stream is the content body (might be an empty stream) |
| 58 | + // call next when you are done with this entry |
| 59 | + |
| 60 | + if (header.type !== 'file') { |
| 61 | + // skip non-files |
| 62 | + stream.on('end', next) |
| 63 | + stream.resume() // drain stream |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + files.push(new Promise((resolve, reject) => { |
| 68 | + let chunks = [] |
| 69 | + stream.on('data', data => chunks.push(data)) |
| 70 | + stream.on('end', () => { |
| 71 | + resolve({ path: header.name, content: Buffer.concat(chunks) }) |
| 72 | + chunks = null |
| 73 | + next() |
| 74 | + }) |
| 75 | + })) |
| 76 | + }) |
| 77 | + |
| 78 | + untarAndAdd.on('finish', async () => { |
| 79 | + const { version } = new CID(expectedCid) |
| 80 | + const opts = { cidVersion: version, pin: false, preload: false } |
| 81 | + const results = await ipfs.add(await Promise.all(files), opts) |
| 82 | + const root = results.find(e => e.hash === expectedCid) |
| 83 | + if (root) { |
| 84 | + log(`${tarPath} successfully precached`, root) |
| 85 | + } else { |
| 86 | + log.error('imported CID does not match expected one (requires new release with updated package.json)') |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + log(`importing ${tarPath} to js-ipfs-repo`) |
| 91 | + stream.pipe(untarAndAdd) |
| 92 | +} |
| 93 | + |
| 94 | +async function * streamTar (repoPath) { |
| 95 | + const response = await fetch(repoPath) |
| 96 | + const reader = response.body.getReader() |
| 97 | + try { |
| 98 | + while (true) { |
| 99 | + const { done, value } = await reader.read() |
| 100 | + if (done) return |
| 101 | + yield value |
| 102 | + } |
| 103 | + } finally { |
| 104 | + // Firefox only? https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/releaseLock |
| 105 | + if (typeof reader.releaseLock === 'function') reader.releaseLock() |
| 106 | + } |
| 107 | +} |
0 commit comments