This repository has been 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
/
ipld-formats-browser.js
99 lines (89 loc) · 3.07 KB
/
ipld-formats-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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(
'/[email protected]/index.min.js',
() => window.IpldBitcoin
),
'eth-account-snapshot': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethAccountSnapshot
),
'eth-block': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethBlock
),
'eth-block-list': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethBlockList
),
'eth-state-trie': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethStateTrie
),
'eth-storage-trie': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethStorageTrie
),
'eth-tx': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethTx
),
'eth-tx-trie': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldEthereum.ethTxTrie
),
'git-raw': jsLoader(
'/[email protected]/index.min.js',
() => window.IpldGit
),
'zcash-block': jsLoader(
'/[email protected]/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 = []
})
}
}
}