This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[WIP] feat: load IPLD formats lazily from IPFS #1830
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,99 @@ | ||
'use strict' | ||
|
||
const IPLD_FORMATS_CID = 'QmP7qfTriY43hb2fUppkd5NvCSV5GxMAHM6cYHMq9uFTeX' | ||
|
||
module.exports = self => { | ||
const options = self._options.ipld || {} | ||
const jsLoader = createIpfsJsLoader(self, `/ipfs/${options.formatsCid || IPLD_FORMATS_CID}`) | ||
|
||
// All known (non-default) IPLD formats | ||
const IpldFormatLoaders = { | ||
'bitcoin-block': jsLoader( | ||
'/ipld-bitcoin@0.1.9/index.min.js', | ||
() => window.IpldBitcoin | ||
), | ||
'eth-account-snapshot': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethAccountSnapshot | ||
), | ||
'eth-block': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethBlock | ||
), | ||
'eth-block-list': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethBlockList | ||
), | ||
'eth-state-trie': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethStateTrie | ||
), | ||
'eth-storage-trie': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethStorageTrie | ||
), | ||
'eth-tx': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethTx | ||
), | ||
'eth-tx-trie': jsLoader( | ||
'/ipld-ethereum@2.0.3/index.min.js', | ||
() => window.IpldEthereum.ethTxTrie | ||
), | ||
'git-raw': jsLoader( | ||
'/ipld-git@0.2.3/index.min.js', | ||
() => window.IpldGit | ||
), | ||
'zcash-block': jsLoader( | ||
'/ipld-zcash@0.1.6/index.min.js', | ||
() => window.IpldZcash | ||
) | ||
} | ||
|
||
return (codec, callback) => { | ||
if (IpldFormatLoaders[codec]) return IpldFormatLoaders[codec](callback) | ||
callback(new Error(`Missing IPLD format "${codec}"`)) | ||
} | ||
} | ||
|
||
// Create a module loader for the passed root path e.g. /ipfs/QmHash | ||
function createIpfsJsLoader (ipfs, rootPath) { | ||
const Modules = {} | ||
|
||
// Create a loader for the given path that will extract a JS object from | ||
// the exports of the loaded module using getExport | ||
return (path, getExport) => { | ||
return callback => { | ||
if (Modules[path]) { | ||
switch (Modules[path].state) { | ||
case 'loading': | ||
return Modules[path].callbacks.push({ getExport, callback }) | ||
case 'loaded': | ||
return callback(null, getExport(Modules[path].exports)) | ||
case 'error': | ||
return callback(Modules[path].error) | ||
} | ||
return callback(new Error('unknown format load state')) | ||
} | ||
|
||
Modules[path] = { state: 'loading', callbacks: [{ getExport, callback }] } | ||
|
||
ipfs.cat(`${rootPath}${path}`, (err, data) => { | ||
if (err) { | ||
Object.assign(Modules[path], { state: 'error', error: err }) | ||
Modules[path].callbacks.forEach(({ callback }) => callback(err)) | ||
Modules[path].callbacks = [] | ||
return | ||
} | ||
|
||
const exports = (new Function(data.toString()))() // eslint-disable-line no-new-func | ||
Object.assign(Modules[path], { state: 'loaded', exports }) | ||
|
||
Modules[path].callbacks.forEach(({ getExport, callback }) => { | ||
callback(null, getExport(exports)) | ||
}) | ||
Modules[path].callbacks = [] | ||
}) | ||
} | ||
} | ||
} |
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,42 @@ | ||
'use strict' | ||
|
||
// All known (non-default) IPLD formats | ||
const IpldFormats = { | ||
get 'bitcoin-block' () { | ||
return require('ipld-bitcoin') | ||
}, | ||
get 'eth-account-snapshot' () { | ||
return require('ipld-ethereum').ethAccountSnapshot | ||
}, | ||
get 'eth-block' () { | ||
return require('ipld-ethereum').ethBlock | ||
}, | ||
get 'eth-block-list' () { | ||
return require('ipld-ethereum').ethBlockList | ||
}, | ||
get 'eth-state-trie' () { | ||
return require('ipld-ethereum').ethStateTrie | ||
}, | ||
get 'eth-storage-trie' () { | ||
return require('ipld-ethereum').ethStorageTrie | ||
}, | ||
get 'eth-tx' () { | ||
return require('ipld-ethereum').ethTx | ||
}, | ||
get 'eth-tx-trie' () { | ||
return require('ipld-ethereum').ethTxTrie | ||
}, | ||
get 'git-raw' () { | ||
return require('ipld-git') | ||
}, | ||
get 'zcash-block' () { | ||
return require('ipld-zcash') | ||
} | ||
} | ||
|
||
module.exports = () => { | ||
return (codec, callback) => { | ||
if (IpldFormats[codec]) return callback(null, IpldFormats[codec]) | ||
callback(new Error(`Missing IPLD format "${codec}"`)) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.