-
Notifications
You must be signed in to change notification settings - Fork 327
feat: precached webui works in offline mode #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4140232
feat: add precache dir to chromium bundle
lidel f2759d4
feat(precache): import TAR archives on boot
lidel 073f058
fix: precache webui 2.5.7
lidel f5cc59d
Merge branch 'master' into feat/offline-webui
lidel 417e4e8
refactor: reuse webui's CID from config
lidel 625a826
fix(ci): run regular build on osx and windows
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict' | ||
/* eslint-env browser, webextensions */ | ||
const pull = require('pull-stream/pull') | ||
const drain = require('pull-stream/sinks/drain') | ||
const toStream = require('it-to-stream') | ||
const tar = require('tar-stream') | ||
const CID = require('cids') | ||
const { webuiCid } = require('./state') | ||
|
||
const debug = require('debug') | ||
const log = debug('ipfs-companion:precache') | ||
log.error = debug('ipfs-companion:precache:error') | ||
|
||
const PRECACHE_ARCHIVES = [ | ||
{ tarPath: '/dist/precache/webui.tar', cid: webuiCid } | ||
] | ||
|
||
/** | ||
* Adds important assets such as Web UI to the local js-ipfs-repo. | ||
* This ensures they load instantly, even in offline environments. | ||
*/ | ||
module.exports = async (ipfs) => { | ||
for (const { cid, tarPath } of PRECACHE_ARCHIVES) { | ||
if (!await inRepo(ipfs, cid)) { | ||
await importTar(ipfs, tarPath, cid) | ||
} else { | ||
log(`${cid} already in local repo, skipping import`) | ||
} | ||
} | ||
} | ||
|
||
async function inRepo (ipfs, cid) { | ||
return new Promise((resolve, reject) => { | ||
let local = false | ||
pull( | ||
ipfs.refs.localPullStream(), | ||
drain(block => { | ||
if (block.ref === cid) { | ||
local = true | ||
return false // abort stream | ||
} | ||
}, () => resolve(local)) | ||
) | ||
}) | ||
} | ||
|
||
async function importTar (ipfs, tarPath, expectedCid) { | ||
const stream = toStream.readable(streamTar(tarPath)) | ||
// TODO: HTTP 404 means precache is disabled in the current runtime | ||
// (eg. in Firefox, due to https://github.com/ipfs-shipyard/ipfs-webui/issues/959) | ||
const untarAndAdd = tar.extract() | ||
|
||
const files = [] | ||
|
||
untarAndAdd.on('entry', (header, stream, next) => { | ||
// header is the tar header | ||
// stream is the content body (might be an empty stream) | ||
// call next when you are done with this entry | ||
|
||
if (header.type !== 'file') { | ||
// skip non-files | ||
stream.on('end', next) | ||
stream.resume() // drain stream | ||
return | ||
} | ||
|
||
files.push(new Promise((resolve, reject) => { | ||
let chunks = [] | ||
stream.on('data', data => chunks.push(data)) | ||
stream.on('end', () => { | ||
resolve({ path: header.name, content: Buffer.concat(chunks) }) | ||
chunks = null | ||
next() | ||
}) | ||
})) | ||
}) | ||
|
||
untarAndAdd.on('finish', async () => { | ||
const { version } = new CID(expectedCid) | ||
const opts = { cidVersion: version, pin: false, preload: false } | ||
const results = await ipfs.add(await Promise.all(files), opts) | ||
const root = results.find(e => e.hash === expectedCid) | ||
if (root) { | ||
log(`${tarPath} successfully precached`, root) | ||
} else { | ||
log.error('imported CID does not match expected one (requires new release with updated package.json)') | ||
} | ||
}) | ||
|
||
log(`importing ${tarPath} to js-ipfs-repo`) | ||
stream.pipe(untarAndAdd) | ||
} | ||
|
||
async function * streamTar (repoPath) { | ||
const response = await fetch(repoPath) | ||
const reader = response.body.getReader() | ||
try { | ||
while (true) { | ||
const { done, value } = await reader.read() | ||
if (done) return | ||
yield value | ||
} | ||
} finally { | ||
// Firefox only? https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/releaseLock | ||
if (typeof reader.releaseLock === 'function') reader.releaseLock() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this was just a dev alias for
ws-star.discovery.libp2p.io