Skip to content

Commit c401e90

Browse files
committed
Deprecated callback interface, now uses async/await API"
1 parent 1f54329 commit c401e90

File tree

5 files changed

+529
-494
lines changed

5 files changed

+529
-494
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ JSON-LD document loader for IPFS and IPLD
1111
}
1212
```
1313

14-
<<< [Golang implementation](https://gist.github.com/joeltg/0cdbe1e058197b0058e9f8ea8dbbd7e9) >>>
15-
1614
## Motivation
1715

1816
Suppose you have some JSON-LD, like this example taken from the [W3C Security Vocabulary](https://web-payments.org/vocabs/security#Digest):
@@ -102,7 +100,7 @@ const ipfs = new IPFS({})
102100
ipfs.on("ready", () => {
103101
const doc = "dweb:/ipfs/QmXjY3nz81qG99vbMF4Tb2NeSFmUdWBUG7ecYVtvnGxrXt"
104102
const documentLoader = createDocumentLoader(ipfs)
105-
jsonld.expand(doc, { documentLoader }, (err, expanded) => {
103+
jsonld.expand(doc, { documentLoader }).then(expanded => {
106104
console.log(err, JSON.stringify(expanded))
107105
process.exit()
108106
})

index.js

+11-38
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
11
const CID = require("cids")
22

3-
function ipldLoader(ipfs, path, callback) {
4-
let cid
5-
try {
6-
cid = new CID(path)
7-
} catch (e) {
8-
callback(e)
9-
}
10-
if (cid.codec === "dag-cbor" || cid.codec === "dag-json") {
11-
ipfs.dag.get(path, (err, { value }) => {
12-
if (err) {
13-
callback(err)
14-
} else {
15-
callback(null, { document: value })
16-
}
17-
})
3+
async function ipldLoader(ipfs, path) {
4+
const cid = new CID(path)
5+
if (cid.codec === "dag-cbor") {
6+
return ipfs.dag.get(path).then(({ value }) => ({ document: value }))
187
} else {
19-
callback(new Error("Unsupported IPLD codecc"))
8+
throw new Error("Unsupported IPLD codec")
209
}
2110
}
2211

23-
function ipfsLoader(ipfs, path, callback) {
24-
ipfs.cat(path, (err, bytes) => {
25-
if (err) {
26-
callback(err)
27-
} else {
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-
}
38-
}
39-
})
12+
function ipfsLoader(ipfs, path) {
13+
return ipfs.cat(path).then(bytes => ({ document: JSON.parse(bytes) }))
4014
}
4115

4216
const documentLoaders = {
@@ -47,13 +21,12 @@ const documentLoaders = {
4721
}
4822

4923
const prefixes = Object.keys(documentLoaders)
50-
const getDocumentLoader = ipfs => (url, callback) => {
24+
25+
module.exports = ipfs => async (url, options) => {
5126
const prefix = prefixes.find(prefix => url.indexOf(prefix) === 0)
5227
if (prefix) {
53-
documentLoaders[prefix](ipfs, url.slice(prefix.length), callback)
28+
return documentLoaders[prefix](ipfs, url.slice(prefix.length))
5429
} else {
55-
callback(new Error("Could not load document", url))
30+
throw new Error("Could not load document", url)
5631
}
5732
}
58-
59-
module.exports = getDocumentLoader

0 commit comments

Comments
 (0)