Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
refactor: make _getFormat() async/await
Browse files Browse the repository at this point in the history
BREAKING CHANGE: your custom format loading function needs
to be an async now.

So the signature for `options.loadFormat` is no longer:

   function (codec, callback)

but

  async functiont (codec)
  • Loading branch information
vmx committed Mar 21, 2019
1 parent 162473b commit 996e9dc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 54 deletions.
113 changes: 65 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ class IPLDResolver {
}
}

this.support.load = options.loadFormat || ((codec, callback) => {
callback(new Error(`No resolver found for codec "${codec}"`))
})
if (options.loadFormat === undefined) {
this.support.load = async (codec) => {
throw new Error(`No resolver found for codec "${codec}"`)
}
} else {
this.support.load = options.loadFormat
}

this.support.rm = (multicodec) => {
if (this.resolvers[multicodec]) {
Expand Down Expand Up @@ -83,46 +87,47 @@ class IPLDResolver {
return Promise.resolve({ done: true })
}

return new Promise((resolve, reject) => {
this._getFormat(cid.codec, (err, format) => {
return new Promise(async (resolve, reject) => {
let format
try {
format = await this._getFormat(cid.codec)
} catch (err) {
return reject(err)
}

// get block
// use local resolver
// update path value
this.bs.get(cid, (err, block) => {
if (err) {
return reject(err)
}

// get block
// use local resolver
// update path value
this.bs.get(cid, (err, block) => {
format.resolver.resolve(block.data, path, (err, result) => {
if (err) {
return reject(err)
}

format.resolver.resolve(block.data, path, (err, result) => {
if (err) {
return reject(err)
}
// Prepare for the next iteration if there is a `remainderPath`
path = result.remainderPath
let value = result.value
// NOTE vmx 2018-11-29: Not all IPLD Formats return links as
// CIDs yet. Hence try to convert old style links to CIDs
if (Object.keys(value).length === 1 && '/' in value) {
value = new CID(value['/'])
}
if (CID.isCID(value)) {
cid = value
} else {
cid = null
}

// Prepare for the next iteration if there is a `remainderPath`
path = result.remainderPath
let value = result.value
// NOTE vmx 2018-11-29: Not all IPLD Formats return links as
// CIDs yet. Hence try to convert old style links to CIDs
if (Object.keys(value).length === 1 && '/' in value) {
value = new CID(value['/'])
}
if (CID.isCID(value)) {
cid = value
} else {
cid = null
return resolve({
done: false,
value: {
remainderPath: path,
value
}

return resolve({
done: false,
value: {
remainderPath: path,
value
}
})
})
})
})
Expand All @@ -148,9 +153,12 @@ class IPLDResolver {
return callback(err)
}
map(blocks, (block, mapCallback) => {
this._getFormat(block.cid.codec, (err, format) => {
if (err) return mapCallback(err)
// TODO vmx 2018-12-07: Make this one async/await once
// `util.serialize()` is a Promise
this._getFormat(block.cid.codec).then((format) => {
format.util.deserialize(block.data, mapCallback)
}).catch((err) => {
mapCallback(err)
})
},
callback)
Expand All @@ -174,9 +182,9 @@ class IPLDResolver {
return this._put(options.cid, node, callback)
}

this._getFormat(options.format, (err, format) => {
if (err) return callback(err)

// TODO vmx 2018-12-07: Make this async/await once `put()` returns a
// Promise
this._getFormat(options.format).then((format) => {
format.util.cid(node, options, (err, cid) => {
if (err) {
return callback(err)
Expand All @@ -188,6 +196,8 @@ class IPLDResolver {

this._put(cid, node, callback)
})
}).catch((err) => {
callback(err)
})
}

Expand All @@ -205,7 +215,9 @@ class IPLDResolver {
p = pullDeferSource()

waterfall([
(cb) => this._getFormat(cid.codec, cb),
async () => {
return this._getFormat(cid.codec)
},
(format, cb) => this.bs.get(cid, (err, block) => {
if (err) return cb(err)
cb(null, format, block)
Expand Down Expand Up @@ -239,7 +251,9 @@ class IPLDResolver {
const cid = el.cid

waterfall([
(cb) => this._getFormat(cid.codec, cb),
async () => {
return this._getFormat(cid.codec)
},
(format, cb) => this.bs.get(cid, (err, block) => {
if (err) return cb(err)
cb(null, format, block)
Expand Down Expand Up @@ -310,24 +324,27 @@ class IPLDResolver {
/* */
/* internals */
/* */
_getFormat (codec, callback) {
async _getFormat (codec) {
if (this.resolvers[codec]) {
return callback(null, this.resolvers[codec])
return this.resolvers[codec]
}

// If not supported, attempt to dynamically load this format
this.support.load(codec, (err, format) => {
if (err) return callback(err)
this.resolvers[codec] = format
callback(null, format)
})
const format = await this.support.load(codec)
this.resolvers[codec] = format
return format
}

_put (cid, node, callback) {
callback = callback || noop

waterfall([
(cb) => this._getFormat(cid.codec, cb),
(cb) => {
this._getFormat(cid.codec).then(
(format) => cb(null, format),
(error) => cb(error)
)
},
(format, cb) => format.util.serialize(node, cb),
(buf, cb) => this.bs.put(new Block(buf, cid), cb)
], (err) => {
Expand Down
12 changes: 6 additions & 6 deletions test/format-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ module.exports = (repo) => {
const resolver = new IPLDResolver({
blockService: bs,
formats: [],
loadFormat (codec, callback) {
if (codec !== 'dag-cbor') return callback(new Error('unexpected codec'))
setTimeout(() => callback(new Error(errMsg)))
async loadFormat (codec) {
if (codec !== 'dag-cbor') throw new Error('unexpected codec')
throw new Error(errMsg)
}
})

Expand All @@ -63,9 +63,9 @@ module.exports = (repo) => {
const resolver = new IPLDResolver({
blockService: bs,
formats: [],
loadFormat (codec, callback) {
if (codec !== 'dag-cbor') return callback(new Error('unexpected codec'))
setTimeout(() => callback(null, dagCBOR))
async loadFormat (codec) {
if (codec !== 'dag-cbor') throw new Error('unexpected codec')
return dagCBOR
}
})

Expand Down

0 comments on commit 996e9dc

Please sign in to comment.