This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This backwards compatible PR allows missing IPLD formats to be loaded dynamically. This follows on from the discussion here #164 (comment) A new constructor option `loadFormat` allows users to asynchronously load an IPLD format. The IPLD instance will call this function automatically when it is requested to resolve a format that it doesn't currently understand. This can re-enable lazy loading for IPLD modules in IPFS and also means that people who just want to add _more_ resolvers can do so without having to depend directly on the defaults and pass them in as well. License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
Showing
5 changed files
with
179 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
const BlockService = require('ipfs-block-service') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
|
||
const IPLDResolver = require('../src') | ||
|
||
module.exports = (repo) => { | ||
describe('IPLD format support', () => { | ||
let data, cid | ||
|
||
before((done) => { | ||
const bs = new BlockService(repo) | ||
const resolver = new IPLDResolver({ blockService: bs }) | ||
|
||
data = { now: Date.now() } | ||
|
||
dagCBOR.util.cid(data, (err, c) => { | ||
expect(err).to.not.exist() | ||
cid = c | ||
resolver.put(data, { cid }, done) | ||
}) | ||
}) | ||
|
||
describe('Dynamic format loading', () => { | ||
it('should fail to dynamically load format', (done) => { | ||
const bs = new BlockService(repo) | ||
const resolver = new IPLDResolver({ | ||
blockService: bs, | ||
formats: [] | ||
}) | ||
|
||
resolver.get(cid, '/', (err) => { | ||
expect(err).to.exist() | ||
expect(err.message).to.equal('No resolver found for codec "dag-cbor"') | ||
done() | ||
}) | ||
}) | ||
|
||
it('should fail to dynamically load format via loadFormat option', (done) => { | ||
const errMsg = 'BOOM' + Date.now() | ||
const bs = new BlockService(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))) | ||
} | ||
}) | ||
|
||
resolver.get(cid, '/', (err) => { | ||
expect(err).to.exist() | ||
expect(err.message).to.equal(errMsg) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should dynamically load missing format', (done) => { | ||
const bs = new BlockService(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)) | ||
} | ||
}) | ||
|
||
resolver.get(cid, '/', (err, result) => { | ||
expect(err).to.not.exist() | ||
expect(result.value).to.eql(data) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters