Skip to content

Commit e40bb03

Browse files
committed
updated URI schemes
1 parent 3fa2ba8 commit e40bb03

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,4 @@ document: ipfs://QmPm5sCx6HLSmdJHFrozmsVNxC6mrE3VMHH7XuTQmtqUuA
134134

135135
## Support
136136

137-
This loader supports JSON-encoded contexts and documents on IPFS under the `dweb:/ipfs/` and `ipfs://` URI schemes (the loader will atempt to parse those files into JSON and will throw an error if that fails), as well as the `dag-cbor`, `dag-json`, `dab-pb`, and `raw` IPLD formats.
138-
139-
`dag-cbor` and `dag-json` both deserialize directly to JSON objects, which are used directly. This loader will attempt to parse `dab-pb` and `raw` blocks as JSON and will throw an error if unsuccessful.
137+
This loader supports JSON-encoded contexts and documents on IPFS under the `dweb:/ipfs/` and `ipfs://` URI schemes (the loader will atempt to parse those files as JSON and will throw an error if that fails), as well as the `dag-cbor` and `dag-json` IPLD formats, which deserialize directly to JSON.

index.js

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,18 @@
11
const CID = require("cids")
22

3-
function parseJSON(bytes, callback) {
4-
const string = bytes.toString("utf8")
5-
let value = null,
6-
error = null
7-
try {
8-
value = { document: JSON.parse(string) }
9-
} catch (e) {
10-
error = e
11-
} finally {
12-
callback(error, value)
13-
}
14-
}
15-
16-
const ipldLoaders = {
17-
raw(value, callback) {
18-
parseJSON(value, callback)
19-
},
20-
"dag-pb"(value, callback) {
21-
parseJSON(value.data, callback)
22-
},
23-
"dag-cbor"(value, callback) {
24-
callback(null, { document: value })
25-
},
26-
"dag-json"(value, callback) {
27-
callback(null, { document: value })
28-
},
29-
}
30-
313
function ipldLoader(ipfs, path, callback) {
324
let cid
335
try {
346
cid = new CID(path)
357
} catch (e) {
368
callback(e)
379
}
38-
if (ipldLoaders.hasOwnProperty(cid.codec)) {
10+
if (cid.codec === "dag-cbor" || cid.codec === "dag-json") {
3911
ipfs.dag.get(path, (err, { value }) => {
4012
if (err) {
4113
callback(err)
4214
} else {
43-
ipldLoaders[cid.codec](value, callback)
15+
callback(null, { document: value })
4416
}
4517
})
4618
} else {
@@ -53,7 +25,16 @@ function ipfsLoader(ipfs, path, callback) {
5325
if (err) {
5426
callback(err)
5527
} else {
56-
parseJSON(bytes, callback)
28+
const string = bytes.toString("utf8")
29+
let value = null,
30+
error = null
31+
try {
32+
value = { document: JSON.parse(string) }
33+
} catch (e) {
34+
error = e
35+
} finally {
36+
callback(error, value)
37+
}
5738
}
5839
})
5940
}

0 commit comments

Comments
 (0)