-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (27 loc) · 812 Bytes
/
index.js
File metadata and controls
32 lines (27 loc) · 812 Bytes
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
const CID = require("cids")
async function ipldLoader(ipfs, path) {
const cid = new CID(path)
if (cid.codec === "dag-cbor") {
return ipfs.dag.get(path).then(({ value }) => ({ document: value }))
} else {
throw new Error("Unsupported IPLD codec")
}
}
function ipfsLoader(ipfs, path) {
return ipfs.cat(path).then(bytes => ({ document: JSON.parse(bytes) }))
}
const documentLoaders = {
"ipld://": ipldLoader,
"dweb:/ipld/": ipldLoader,
"ipfs://": ipfsLoader,
"dweb:/ipfs/": ipfsLoader,
}
const prefixes = Object.keys(documentLoaders)
module.exports = ipfs => async (url, options) => {
const prefix = prefixes.find(prefix => url.indexOf(prefix) === 0)
if (prefix) {
return documentLoaders[prefix](ipfs, url.slice(prefix.length))
} else {
throw new Error("Could not load document", url)
}
}