multiformats
Advanced tools
Comparing version 9.5.4 to 9.5.5
'use strict'; | ||
function testThrow(fn, message) { | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const testThrowSync = (fn, message) => { | ||
try { | ||
@@ -12,4 +14,15 @@ fn(); | ||
throw new Error('Test failed to throw'); | ||
} | ||
}; | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
module.exports = testThrow; | ||
exports.testThrowAsync = testThrowAsync; | ||
exports.testThrowSync = testThrowSync; |
@@ -8,2 +8,3 @@ 'use strict'; | ||
var chai = require('chai'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
var cid = require('../src/cid.js'); | ||
@@ -140,37 +141,18 @@ var bytes = require('../src/bytes.js'); | ||
it('constructor missing args', () => { | ||
let threw = true; | ||
try { | ||
threw = new block.Block({}); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== 'Missing required argument') | ||
throw e; | ||
} | ||
chai.assert.deepStrictEqual(threw, true); | ||
testThrow.testThrowSync(() => new block.Block({}), 'Missing required argument'); | ||
}); | ||
const errTest = async (method, arg, message) => { | ||
let threw = true; | ||
try { | ||
await method(arg); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
} | ||
chai.assert.deepStrictEqual(threw, true); | ||
}; | ||
it('encode', async () => { | ||
await errTest(block.encode, {}, 'Missing required argument "value"'); | ||
await errTest(block.encode, { value: true }, 'Missing required argument: codec or hasher'); | ||
await testThrow.testThrowAsync(() => block.encode({}), 'Missing required argument "value"'); | ||
await testThrow.testThrowAsync(() => block.encode({ value: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('decode', async () => { | ||
await errTest(block.decode, {}, 'Missing required argument "bytes"'); | ||
await errTest(block.decode, { bytes: true }, 'Missing required argument: codec or hasher'); | ||
await testThrow.testThrowAsync(() => block.decode({}), 'Missing required argument "bytes"'); | ||
await testThrow.testThrowAsync(() => block.decode({ bytes: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('createUnsafe', async () => { | ||
await errTest(block.createUnsafe, {}, 'Missing required argument, must either provide "value" or "codec"'); | ||
await testThrow.testThrowAsync(() => block.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"'); | ||
}); | ||
it('create', async () => { | ||
await errTest(block.create, {}, 'Missing required argument "bytes"'); | ||
await errTest(block.create, { bytes: true }, 'Missing required argument "hasher"'); | ||
await testThrow.testThrowAsync(() => block.create({}), 'Missing required argument "bytes"'); | ||
await testThrow.testThrowAsync(() => block.create({ bytes: true }), 'Missing required argument "hasher"'); | ||
const block$1 = await block.encode({ | ||
@@ -189,3 +171,3 @@ value: fixture, | ||
}); | ||
await errTest(block.create, { | ||
await testThrow.testThrowAsync(() => block.create({ | ||
bytes: block$1.bytes, | ||
@@ -195,3 +177,3 @@ cid: block2.cid, | ||
hasher: sha2Browser.sha256 | ||
}, 'CID hash does not match bytes'); | ||
}), 'CID hash does not match bytes'); | ||
}); | ||
@@ -204,5 +186,5 @@ it('get', async () => { | ||
}); | ||
await errTest(path => block$1.get(path), '/asd/fs/dfasd/f', 'Object has no property at ["asd"]'); | ||
await testThrow.testThrowAsync(() => block$1.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]'); | ||
}); | ||
}); | ||
}); |
@@ -8,4 +8,4 @@ 'use strict'; | ||
it('isBinary', () => { | ||
chai.assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer()), true); | ||
chai.assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer())), true); | ||
chai.assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer(0)), true); | ||
chai.assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer(0))), true); | ||
}); | ||
@@ -12,0 +12,0 @@ it('coerce', () => { |
@@ -65,3 +65,3 @@ 'use strict'; | ||
const msg = 'Non-base58btc character'; | ||
await testThrow(() => cid.CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
await testThrow.testThrowSync(() => cid.CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
}); | ||
@@ -71,3 +71,3 @@ it('throws on trying to create a CIDv0 with a codec other than dag-pb', async () => { | ||
const msg = 'Version 0 CID must use dag-pb (code: 112) block encoding'; | ||
await testThrow(() => cid.CID.create(0, 113, hash), msg); | ||
await testThrow.testThrowSync(() => cid.CID.create(0, 113, hash), msg); | ||
}); | ||
@@ -78,3 +78,3 @@ it('throws on trying to base encode CIDv0 in other base than base58btc', async () => { | ||
const msg = 'Cannot string encode V0 in base32 encoding'; | ||
await testThrow(() => cid$1.toString(base32.base32), msg); | ||
await testThrow.testThrowSync(() => cid$1.toString(base32.base32), msg); | ||
}); | ||
@@ -145,3 +145,3 @@ it('.bytes', async () => { | ||
chai.assert.deepStrictEqual(cid$1.version, 1); | ||
chai.assert.ok(bytes.equals(cid$1.multihash, hash)); | ||
equalDigest(cid$1.multihash, hash); | ||
}); | ||
@@ -297,3 +297,3 @@ it('can roundtrip through cid.toString()', async () => { | ||
const cid$1 = cid.CID.create(1, 113, hash); | ||
await testThrow(() => cid$1.toV0(), 'Cannot convert a non dag-pb CID to CIDv0'); | ||
await testThrow.testThrowSync(() => cid$1.toV0(), 'Cannot convert a non dag-pb CID to CIDv0'); | ||
}); | ||
@@ -303,3 +303,3 @@ it('should not convert v1 to v0 if not sha2-256 multihash', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.toV0(), 'Cannot convert non sha2-256 multihash CID to CIDv0'); | ||
await testThrow.testThrowSync(() => cid$1.toV0(), 'Cannot convert non sha2-256 multihash CID to CIDv0'); | ||
}); | ||
@@ -384,3 +384,3 @@ it('should return assert.deepStrictEqual instance when converting v1 to v1', async () => { | ||
chai.assert.strictEqual(cid1.version, version); | ||
chai.assert.ok(bytes.equals(cid1.multihash, hash)); | ||
chai.assert.ok(bytes.equals(cid1.multihash.bytes, hash.bytes)); | ||
const cid2 = cid.CID.asCID({ | ||
@@ -402,3 +402,3 @@ version, | ||
chai.assert.strictEqual(cid3.version, version); | ||
chai.assert.ok(bytes.equals(cid3.multihash, hash)); | ||
chai.assert.ok(bytes.equals(cid3.multihash.bytes, hash.bytes)); | ||
const cid4 = cid.CID.asCID(cid3); | ||
@@ -409,11 +409,10 @@ chai.assert.strictEqual(cid3, cid4); | ||
chai.assert.strictEqual(cid5.version, 1); | ||
chai.assert.ok(bytes.equals(cid5.multihash, hash)); | ||
chai.assert.ok(bytes.equals(cid5.multihash.bytes, hash.bytes)); | ||
chai.assert.strictEqual(cid5.code, 85); | ||
}); | ||
const digestsame = (x, y) => { | ||
chai.assert.deepStrictEqual(x.digest, y.digest); | ||
chai.assert.deepStrictEqual(x.hash, y.hash); | ||
chai.assert.deepStrictEqual(x.bytes, y.bytes); | ||
if (x.multihash) { | ||
digestsame(x.multihash, y.multihash); | ||
equalDigest(x.multihash, y.multihash); | ||
} | ||
@@ -434,2 +433,7 @@ const empty = { | ||
}; | ||
const equalDigest = (x, y) => { | ||
chai.assert.deepStrictEqual(x.digest, y.digest); | ||
chai.assert.deepStrictEqual(x.code, y.code); | ||
chai.assert.deepStrictEqual(x.digest, y.digest); | ||
}; | ||
describe('CID.parse', async () => { | ||
@@ -458,3 +462,3 @@ it('parse 32 encoded CIDv1', async () => { | ||
const msg = 'To parse non base32 or base58btc encoded CID multibase decoder must be provided'; | ||
await testThrow(() => cid.CID.parse(cid$1.toString(base64.base64)), msg); | ||
await testThrow.testThrowSync(() => cid.CID.parse(cid$1.toString(base64.base64)), msg); | ||
}); | ||
@@ -498,3 +502,3 @@ it('parses base64 encoded CIDv1 if base64 is provided', async () => { | ||
chai.assert.deepStrictEqual(cid$1.version, 1); | ||
chai.assert.ok(bytes.equals(cid$1.multihash, hash)); | ||
equalDigest(cid$1.multihash, hash); | ||
chai.assert.deepStrictEqual(cid$1.code, 85); | ||
@@ -512,4 +516,4 @@ }); | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.codec, '"codec" property is deprecated, use integer "code" property instead'); | ||
await testThrow(() => cid.CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported'); | ||
await testThrow.testThrowSync(() => cid$1.codec, '"codec" property is deprecated, use integer "code" property instead'); | ||
await testThrow.testThrowSync(() => cid.CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported'); | ||
}); | ||
@@ -519,3 +523,3 @@ it('multibaseName', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.multibaseName, '"multibaseName" property is deprecated'); | ||
await testThrow.testThrowSync(() => cid$1.multibaseName, '"multibaseName" property is deprecated'); | ||
}); | ||
@@ -525,3 +529,3 @@ it('prefix', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.prefix, '"prefix" property is deprecated'); | ||
await testThrow.testThrowSync(() => cid$1.prefix, '"prefix" property is deprecated'); | ||
}); | ||
@@ -531,3 +535,3 @@ it('toBaseEncodedString()', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.toBaseEncodedString(), 'Deprecated, use .toString()'); | ||
await testThrow.testThrowSync(() => cid$1.toBaseEncodedString(), 'Deprecated, use .toString()'); | ||
}); | ||
@@ -537,3 +541,3 @@ }); | ||
const encoded = varint.encodeTo(2, new Uint8Array(32)); | ||
await testThrow(() => cid.CID.decode(encoded), 'Invalid CID version 2'); | ||
await testThrow.testThrowSync(() => cid.CID.decode(encoded), 'Invalid CID version 2'); | ||
}); | ||
@@ -543,4 +547,4 @@ it('buffer', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.buffer, 'Deprecated .buffer property, use .bytes to get Uint8Array instead'); | ||
await testThrow.testThrowSync(() => cid$1.buffer, 'Deprecated .buffer property, use .bytes to get Uint8Array instead'); | ||
}); | ||
}); |
@@ -573,5 +573,5 @@ 'use strict'; | ||
console.info('expect', `Non-${ base.name } character`); | ||
testThrow(() => base.decode(base.prefix + '^!@$%!#$%@#y'), `Non-${ base.name } character`); | ||
testThrow.testThrowSync(() => base.decode(base.prefix + '^!@$%!#$%@#y'), `Non-${ base.name } character`); | ||
}); | ||
} | ||
}); |
@@ -49,3 +49,3 @@ 'use strict'; | ||
const msg = `Non-${ base.name } character`; | ||
testThrow(() => base.decode(str), msg); | ||
testThrow.testThrowAsync(() => base.decode(str), msg); | ||
}); | ||
@@ -56,9 +56,9 @@ }); | ||
const msg = 'Unknown type, must be binary type'; | ||
testThrow(() => base32.encode('asdf'), msg); | ||
testThrow(() => base32.encoder.encode('asdf'), msg); | ||
testThrow.testThrowAsync(() => base32.encode('asdf'), msg); | ||
testThrow.testThrowAsync(() => base32.encoder.encode('asdf'), msg); | ||
}); | ||
it('decode int failure', () => { | ||
const msg = 'Can only multibase decode strings'; | ||
testThrow(() => base32.decode(1), msg); | ||
testThrow(() => base32.decoder.decode(1), msg); | ||
testThrow.testThrowAsync(() => base32.decode(1), msg); | ||
testThrow.testThrowAsync(() => base32.decoder.decode(1), msg); | ||
}); | ||
@@ -118,3 +118,3 @@ const buff = bytes.fromString('test'); | ||
const msg = `Unable to decode multibase string "${ b64 }", base32 decoder only supports inputs prefixed with ${ base32.prefix }`; | ||
testThrow(() => base32.decode(b64), msg); | ||
testThrow.testThrowAsync(() => base32.decode(b64), msg); | ||
}); | ||
@@ -129,6 +129,6 @@ it('decoder composition', () => { | ||
const msg = `Unable to decode multibase string "${ b64 }", only inputs prefixed with ${ base32.prefix },${ base58btc.prefix } are supported`; | ||
testThrow(() => base.decode(b64), msg); | ||
testThrow.testThrowAsync(() => base.decode(b64), msg); | ||
const baseExt = base.or(base64); | ||
chai.assert.deepStrictEqual(baseExt.decode(b64), bytes.fromString('test')); | ||
testThrow(() => base.decode(b64), msg); | ||
testThrow.testThrowAsync(() => base.decode(b64), msg); | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)); | ||
@@ -144,4 +144,12 @@ chai.assert.deepStrictEqual(baseExt2.decode(b64), bytes.fromString('test')); | ||
])); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
testThrow.testThrowAsync(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
}); | ||
it('infers prefix and name corretly', () => { | ||
const name = base32.name; | ||
const name2 = base32.name; | ||
const prefix = base32.prefix; | ||
chai.assert.equal(prefix, 'b'); | ||
chai.assert.equal(name, 'base32'); | ||
chai.assert.equal(name2, name); | ||
}); | ||
}); |
@@ -13,3 +13,3 @@ 'use strict'; | ||
chai.assert.deepStrictEqual(buff, bytes.fromString('test')); | ||
chai.assert.deepStrictEqual(raw.decode(buff, 'raw'), bytes.fromString('test')); | ||
chai.assert.deepStrictEqual(raw.decode(buff), bytes.fromString('test')); | ||
}); | ||
@@ -22,4 +22,4 @@ it('encode/decode json', () => { | ||
it('raw cannot encode string', async () => { | ||
await testThrow(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
await testThrow.testThrowAsync(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
}); | ||
}); |
@@ -12,2 +12,3 @@ 'use strict'; | ||
var digest = require('../src/hashes/digest.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
@@ -23,12 +24,2 @@ const sample = (code, size, hex) => { | ||
}; | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multihash', () => { | ||
@@ -93,3 +84,3 @@ const empty = new Uint8Array(0); | ||
const buff = bytes.fromHex(test.hex); | ||
await testThrowAsync(() => digest.decode(buff), test.message); | ||
await testThrow.testThrowAsync(() => digest.decode(buff), test.message); | ||
} | ||
@@ -99,4 +90,4 @@ }); | ||
it('throw on hashing non-buffer', async () => { | ||
await testThrowAsync(() => sha2Browser.sha256.digest('asdf'), 'Unknown type, must be binary type'); | ||
await testThrow.testThrowAsync(() => sha2Browser.sha256.digest('asdf'), 'Unknown type, must be binary type'); | ||
}); | ||
}); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var chai = require('chai'); | ||
var bytes = require('../src/bytes.js'); | ||
@@ -35,3 +36,3 @@ function _interopNamespace(e) { | ||
const linksE = []; | ||
const valueE = createNode(Uint8Array.from('string E qacdswa'), linksE); | ||
const valueE = createNode(bytes.fromString('string E qacdswa'), linksE); | ||
const blockE = await block.encode({ | ||
@@ -44,3 +45,3 @@ value: valueE, | ||
const linksD = []; | ||
const valueD = createNode(Uint8Array.from('string D zasa'), linksD); | ||
const valueD = createNode(bytes.fromString('string D zasa'), linksD); | ||
const blockD = await block.encode({ | ||
@@ -56,3 +57,3 @@ value: valueD, | ||
]; | ||
const valueC = createNode(Uint8Array.from('string C zxc'), linksC); | ||
const valueC = createNode(bytes.fromString('string C zxc'), linksC); | ||
const blockC = await block.encode({ | ||
@@ -68,3 +69,3 @@ value: valueC, | ||
]; | ||
const valueB = createNode(Uint8Array.from('string B lpokjiasd'), linksB); | ||
const valueB = createNode(bytes.fromString('string B lpokjiasd'), linksB); | ||
const blockB = await block.encode({ | ||
@@ -80,3 +81,3 @@ value: valueB, | ||
]; | ||
const valueA = createNode(Uint8Array.from('string A qwertcfdgshaa'), linksA); | ||
const valueA = createNode(bytes.fromString('string A qwertcfdgshaa'), linksA); | ||
const blockA = await block.encode({ | ||
@@ -169,3 +170,3 @@ value: valueA, | ||
const links = []; | ||
const value = createNode(Uint8Array.from('test'), links); | ||
const value = createNode(bytes.fromString('test'), links); | ||
const block$1 = await block.encode({ | ||
@@ -172,0 +173,0 @@ value: value, |
'use strict'; | ||
function testThrow(fn, message) { | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const testThrowSync = (fn, message) => { | ||
try { | ||
@@ -12,4 +14,15 @@ fn(); | ||
throw new Error('Test failed to throw'); | ||
} | ||
}; | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
module.exports = testThrow; | ||
exports.testThrowAsync = testThrowAsync; | ||
exports.testThrowSync = testThrowSync; |
@@ -8,2 +8,3 @@ 'use strict'; | ||
var chai = require('chai'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
var cid = require('../src/cid.js'); | ||
@@ -140,37 +141,18 @@ var bytes = require('../src/bytes.js'); | ||
it('constructor missing args', () => { | ||
let threw = true; | ||
try { | ||
threw = new block.Block({}); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== 'Missing required argument') | ||
throw e; | ||
} | ||
chai.assert.deepStrictEqual(threw, true); | ||
testThrow.testThrowSync(() => new block.Block({}), 'Missing required argument'); | ||
}); | ||
const errTest = async (method, arg, message) => { | ||
let threw = true; | ||
try { | ||
await method(arg); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
} | ||
chai.assert.deepStrictEqual(threw, true); | ||
}; | ||
it('encode', async () => { | ||
await errTest(block.encode, {}, 'Missing required argument "value"'); | ||
await errTest(block.encode, { value: true }, 'Missing required argument: codec or hasher'); | ||
await testThrow.testThrowAsync(() => block.encode({}), 'Missing required argument "value"'); | ||
await testThrow.testThrowAsync(() => block.encode({ value: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('decode', async () => { | ||
await errTest(block.decode, {}, 'Missing required argument "bytes"'); | ||
await errTest(block.decode, { bytes: true }, 'Missing required argument: codec or hasher'); | ||
await testThrow.testThrowAsync(() => block.decode({}), 'Missing required argument "bytes"'); | ||
await testThrow.testThrowAsync(() => block.decode({ bytes: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('createUnsafe', async () => { | ||
await errTest(block.createUnsafe, {}, 'Missing required argument, must either provide "value" or "codec"'); | ||
await testThrow.testThrowAsync(() => block.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"'); | ||
}); | ||
it('create', async () => { | ||
await errTest(block.create, {}, 'Missing required argument "bytes"'); | ||
await errTest(block.create, { bytes: true }, 'Missing required argument "hasher"'); | ||
await testThrow.testThrowAsync(() => block.create({}), 'Missing required argument "bytes"'); | ||
await testThrow.testThrowAsync(() => block.create({ bytes: true }), 'Missing required argument "hasher"'); | ||
const block$1 = await block.encode({ | ||
@@ -189,3 +171,3 @@ value: fixture, | ||
}); | ||
await errTest(block.create, { | ||
await testThrow.testThrowAsync(() => block.create({ | ||
bytes: block$1.bytes, | ||
@@ -195,3 +177,3 @@ cid: block2.cid, | ||
hasher: sha2.sha256 | ||
}, 'CID hash does not match bytes'); | ||
}), 'CID hash does not match bytes'); | ||
}); | ||
@@ -204,5 +186,5 @@ it('get', async () => { | ||
}); | ||
await errTest(path => block$1.get(path), '/asd/fs/dfasd/f', 'Object has no property at ["asd"]'); | ||
await testThrow.testThrowAsync(() => block$1.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]'); | ||
}); | ||
}); | ||
}); |
@@ -8,4 +8,4 @@ 'use strict'; | ||
it('isBinary', () => { | ||
chai.assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer()), true); | ||
chai.assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer())), true); | ||
chai.assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer(0)), true); | ||
chai.assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer(0))), true); | ||
}); | ||
@@ -12,0 +12,0 @@ it('coerce', () => { |
@@ -65,3 +65,3 @@ 'use strict'; | ||
const msg = 'Non-base58btc character'; | ||
await testThrow(() => cid.CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
await testThrow.testThrowSync(() => cid.CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
}); | ||
@@ -71,3 +71,3 @@ it('throws on trying to create a CIDv0 with a codec other than dag-pb', async () => { | ||
const msg = 'Version 0 CID must use dag-pb (code: 112) block encoding'; | ||
await testThrow(() => cid.CID.create(0, 113, hash), msg); | ||
await testThrow.testThrowSync(() => cid.CID.create(0, 113, hash), msg); | ||
}); | ||
@@ -78,3 +78,3 @@ it('throws on trying to base encode CIDv0 in other base than base58btc', async () => { | ||
const msg = 'Cannot string encode V0 in base32 encoding'; | ||
await testThrow(() => cid$1.toString(base32.base32), msg); | ||
await testThrow.testThrowSync(() => cid$1.toString(base32.base32), msg); | ||
}); | ||
@@ -145,3 +145,3 @@ it('.bytes', async () => { | ||
chai.assert.deepStrictEqual(cid$1.version, 1); | ||
chai.assert.ok(bytes.equals(cid$1.multihash, hash)); | ||
equalDigest(cid$1.multihash, hash); | ||
}); | ||
@@ -297,3 +297,3 @@ it('can roundtrip through cid.toString()', async () => { | ||
const cid$1 = cid.CID.create(1, 113, hash); | ||
await testThrow(() => cid$1.toV0(), 'Cannot convert a non dag-pb CID to CIDv0'); | ||
await testThrow.testThrowSync(() => cid$1.toV0(), 'Cannot convert a non dag-pb CID to CIDv0'); | ||
}); | ||
@@ -303,3 +303,3 @@ it('should not convert v1 to v0 if not sha2-256 multihash', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.toV0(), 'Cannot convert non sha2-256 multihash CID to CIDv0'); | ||
await testThrow.testThrowSync(() => cid$1.toV0(), 'Cannot convert non sha2-256 multihash CID to CIDv0'); | ||
}); | ||
@@ -384,3 +384,3 @@ it('should return assert.deepStrictEqual instance when converting v1 to v1', async () => { | ||
chai.assert.strictEqual(cid1.version, version); | ||
chai.assert.ok(bytes.equals(cid1.multihash, hash)); | ||
chai.assert.ok(bytes.equals(cid1.multihash.bytes, hash.bytes)); | ||
const cid2 = cid.CID.asCID({ | ||
@@ -402,3 +402,3 @@ version, | ||
chai.assert.strictEqual(cid3.version, version); | ||
chai.assert.ok(bytes.equals(cid3.multihash, hash)); | ||
chai.assert.ok(bytes.equals(cid3.multihash.bytes, hash.bytes)); | ||
const cid4 = cid.CID.asCID(cid3); | ||
@@ -409,11 +409,10 @@ chai.assert.strictEqual(cid3, cid4); | ||
chai.assert.strictEqual(cid5.version, 1); | ||
chai.assert.ok(bytes.equals(cid5.multihash, hash)); | ||
chai.assert.ok(bytes.equals(cid5.multihash.bytes, hash.bytes)); | ||
chai.assert.strictEqual(cid5.code, 85); | ||
}); | ||
const digestsame = (x, y) => { | ||
chai.assert.deepStrictEqual(x.digest, y.digest); | ||
chai.assert.deepStrictEqual(x.hash, y.hash); | ||
chai.assert.deepStrictEqual(x.bytes, y.bytes); | ||
if (x.multihash) { | ||
digestsame(x.multihash, y.multihash); | ||
equalDigest(x.multihash, y.multihash); | ||
} | ||
@@ -434,2 +433,7 @@ const empty = { | ||
}; | ||
const equalDigest = (x, y) => { | ||
chai.assert.deepStrictEqual(x.digest, y.digest); | ||
chai.assert.deepStrictEqual(x.code, y.code); | ||
chai.assert.deepStrictEqual(x.digest, y.digest); | ||
}; | ||
describe('CID.parse', async () => { | ||
@@ -458,3 +462,3 @@ it('parse 32 encoded CIDv1', async () => { | ||
const msg = 'To parse non base32 or base58btc encoded CID multibase decoder must be provided'; | ||
await testThrow(() => cid.CID.parse(cid$1.toString(base64.base64)), msg); | ||
await testThrow.testThrowSync(() => cid.CID.parse(cid$1.toString(base64.base64)), msg); | ||
}); | ||
@@ -498,3 +502,3 @@ it('parses base64 encoded CIDv1 if base64 is provided', async () => { | ||
chai.assert.deepStrictEqual(cid$1.version, 1); | ||
chai.assert.ok(bytes.equals(cid$1.multihash, hash)); | ||
equalDigest(cid$1.multihash, hash); | ||
chai.assert.deepStrictEqual(cid$1.code, 85); | ||
@@ -512,4 +516,4 @@ }); | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.codec, '"codec" property is deprecated, use integer "code" property instead'); | ||
await testThrow(() => cid.CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported'); | ||
await testThrow.testThrowSync(() => cid$1.codec, '"codec" property is deprecated, use integer "code" property instead'); | ||
await testThrow.testThrowSync(() => cid.CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported'); | ||
}); | ||
@@ -519,3 +523,3 @@ it('multibaseName', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.multibaseName, '"multibaseName" property is deprecated'); | ||
await testThrow.testThrowSync(() => cid$1.multibaseName, '"multibaseName" property is deprecated'); | ||
}); | ||
@@ -525,3 +529,3 @@ it('prefix', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.prefix, '"prefix" property is deprecated'); | ||
await testThrow.testThrowSync(() => cid$1.prefix, '"prefix" property is deprecated'); | ||
}); | ||
@@ -531,3 +535,3 @@ it('toBaseEncodedString()', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.toBaseEncodedString(), 'Deprecated, use .toString()'); | ||
await testThrow.testThrowSync(() => cid$1.toBaseEncodedString(), 'Deprecated, use .toString()'); | ||
}); | ||
@@ -537,3 +541,3 @@ }); | ||
const encoded = varint.encodeTo(2, new Uint8Array(32)); | ||
await testThrow(() => cid.CID.decode(encoded), 'Invalid CID version 2'); | ||
await testThrow.testThrowSync(() => cid.CID.decode(encoded), 'Invalid CID version 2'); | ||
}); | ||
@@ -543,4 +547,4 @@ it('buffer', async () => { | ||
const cid$1 = cid.CID.create(1, 112, hash); | ||
await testThrow(() => cid$1.buffer, 'Deprecated .buffer property, use .bytes to get Uint8Array instead'); | ||
await testThrow.testThrowSync(() => cid$1.buffer, 'Deprecated .buffer property, use .bytes to get Uint8Array instead'); | ||
}); | ||
}); |
@@ -573,5 +573,5 @@ 'use strict'; | ||
console.info('expect', `Non-${ base.name } character`); | ||
testThrow(() => base.decode(base.prefix + '^!@$%!#$%@#y'), `Non-${ base.name } character`); | ||
testThrow.testThrowSync(() => base.decode(base.prefix + '^!@$%!#$%@#y'), `Non-${ base.name } character`); | ||
}); | ||
} | ||
}); |
@@ -49,3 +49,3 @@ 'use strict'; | ||
const msg = `Non-${ base.name } character`; | ||
testThrow(() => base.decode(str), msg); | ||
testThrow.testThrowAsync(() => base.decode(str), msg); | ||
}); | ||
@@ -56,9 +56,9 @@ }); | ||
const msg = 'Unknown type, must be binary type'; | ||
testThrow(() => base32.encode('asdf'), msg); | ||
testThrow(() => base32.encoder.encode('asdf'), msg); | ||
testThrow.testThrowAsync(() => base32.encode('asdf'), msg); | ||
testThrow.testThrowAsync(() => base32.encoder.encode('asdf'), msg); | ||
}); | ||
it('decode int failure', () => { | ||
const msg = 'Can only multibase decode strings'; | ||
testThrow(() => base32.decode(1), msg); | ||
testThrow(() => base32.decoder.decode(1), msg); | ||
testThrow.testThrowAsync(() => base32.decode(1), msg); | ||
testThrow.testThrowAsync(() => base32.decoder.decode(1), msg); | ||
}); | ||
@@ -118,3 +118,3 @@ const buff = bytes.fromString('test'); | ||
const msg = `Unable to decode multibase string "${ b64 }", base32 decoder only supports inputs prefixed with ${ base32.prefix }`; | ||
testThrow(() => base32.decode(b64), msg); | ||
testThrow.testThrowAsync(() => base32.decode(b64), msg); | ||
}); | ||
@@ -129,6 +129,6 @@ it('decoder composition', () => { | ||
const msg = `Unable to decode multibase string "${ b64 }", only inputs prefixed with ${ base32.prefix },${ base58btc.prefix } are supported`; | ||
testThrow(() => base.decode(b64), msg); | ||
testThrow.testThrowAsync(() => base.decode(b64), msg); | ||
const baseExt = base.or(base64); | ||
chai.assert.deepStrictEqual(baseExt.decode(b64), bytes.fromString('test')); | ||
testThrow(() => base.decode(b64), msg); | ||
testThrow.testThrowAsync(() => base.decode(b64), msg); | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)); | ||
@@ -144,4 +144,12 @@ chai.assert.deepStrictEqual(baseExt2.decode(b64), bytes.fromString('test')); | ||
])); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
testThrow.testThrowAsync(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
}); | ||
it('infers prefix and name corretly', () => { | ||
const name = base32.name; | ||
const name2 = base32.name; | ||
const prefix = base32.prefix; | ||
chai.assert.equal(prefix, 'b'); | ||
chai.assert.equal(name, 'base32'); | ||
chai.assert.equal(name2, name); | ||
}); | ||
}); |
@@ -13,3 +13,3 @@ 'use strict'; | ||
chai.assert.deepStrictEqual(buff, bytes.fromString('test')); | ||
chai.assert.deepStrictEqual(raw.decode(buff, 'raw'), bytes.fromString('test')); | ||
chai.assert.deepStrictEqual(raw.decode(buff), bytes.fromString('test')); | ||
}); | ||
@@ -22,4 +22,4 @@ it('encode/decode json', () => { | ||
it('raw cannot encode string', async () => { | ||
await testThrow(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
await testThrow.testThrowAsync(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
}); | ||
}); |
@@ -12,2 +12,3 @@ 'use strict'; | ||
var digest = require('../src/hashes/digest.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
@@ -23,12 +24,2 @@ const sample = (code, size, hex) => { | ||
}; | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multihash', () => { | ||
@@ -93,3 +84,3 @@ const empty = new Uint8Array(0); | ||
const buff = bytes.fromHex(test.hex); | ||
await testThrowAsync(() => digest.decode(buff), test.message); | ||
await testThrow.testThrowAsync(() => digest.decode(buff), test.message); | ||
} | ||
@@ -99,4 +90,4 @@ }); | ||
it('throw on hashing non-buffer', async () => { | ||
await testThrowAsync(() => sha2.sha256.digest('asdf'), 'Unknown type, must be binary type'); | ||
await testThrow.testThrowAsync(() => sha2.sha256.digest('asdf'), 'Unknown type, must be binary type'); | ||
}); | ||
}); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var chai = require('chai'); | ||
var bytes = require('../src/bytes.js'); | ||
@@ -35,3 +36,3 @@ function _interopNamespace(e) { | ||
const linksE = []; | ||
const valueE = createNode(Uint8Array.from('string E qacdswa'), linksE); | ||
const valueE = createNode(bytes.fromString('string E qacdswa'), linksE); | ||
const blockE = await block.encode({ | ||
@@ -44,3 +45,3 @@ value: valueE, | ||
const linksD = []; | ||
const valueD = createNode(Uint8Array.from('string D zasa'), linksD); | ||
const valueD = createNode(bytes.fromString('string D zasa'), linksD); | ||
const blockD = await block.encode({ | ||
@@ -56,3 +57,3 @@ value: valueD, | ||
]; | ||
const valueC = createNode(Uint8Array.from('string C zxc'), linksC); | ||
const valueC = createNode(bytes.fromString('string C zxc'), linksC); | ||
const blockC = await block.encode({ | ||
@@ -68,3 +69,3 @@ value: valueC, | ||
]; | ||
const valueB = createNode(Uint8Array.from('string B lpokjiasd'), linksB); | ||
const valueB = createNode(bytes.fromString('string B lpokjiasd'), linksB); | ||
const blockB = await block.encode({ | ||
@@ -80,3 +81,3 @@ value: valueB, | ||
]; | ||
const valueA = createNode(Uint8Array.from('string A qwertcfdgshaa'), linksA); | ||
const valueA = createNode(bytes.fromString('string A qwertcfdgshaa'), linksA); | ||
const blockA = await block.encode({ | ||
@@ -169,3 +170,3 @@ value: valueA, | ||
const links = []; | ||
const value = createNode(Uint8Array.from('test'), links); | ||
const value = createNode(bytes.fromString('test'), links); | ||
const block$1 = await block.encode({ | ||
@@ -172,0 +173,0 @@ value: value, |
@@ -1,2 +0,2 @@ | ||
export default function testThrow(fn, message) { | ||
export const testThrowSync = (fn, message) => { | ||
try { | ||
@@ -10,2 +10,12 @@ fn(); | ||
throw new Error('Test failed to throw'); | ||
} | ||
}; | ||
export const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; |
@@ -9,2 +9,6 @@ import * as codec from '../src/codecs/json.js'; | ||
import { assert } from 'chai'; | ||
import { | ||
testThrowAsync, | ||
testThrowSync | ||
} from './fixtures/test-throw.js'; | ||
const fixture = { hello: 'world' }; | ||
@@ -138,37 +142,18 @@ const link = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae'); | ||
it('constructor missing args', () => { | ||
let threw = true; | ||
try { | ||
threw = new main.Block({}); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== 'Missing required argument') | ||
throw e; | ||
} | ||
assert.deepStrictEqual(threw, true); | ||
testThrowSync(() => new main.Block({}), 'Missing required argument'); | ||
}); | ||
const errTest = async (method, arg, message) => { | ||
let threw = true; | ||
try { | ||
await method(arg); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
} | ||
assert.deepStrictEqual(threw, true); | ||
}; | ||
it('encode', async () => { | ||
await errTest(main.encode, {}, 'Missing required argument "value"'); | ||
await errTest(main.encode, { value: true }, 'Missing required argument: codec or hasher'); | ||
await testThrowAsync(() => main.encode({}), 'Missing required argument "value"'); | ||
await testThrowAsync(() => main.encode({ value: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('decode', async () => { | ||
await errTest(main.decode, {}, 'Missing required argument "bytes"'); | ||
await errTest(main.decode, { bytes: true }, 'Missing required argument: codec or hasher'); | ||
await testThrowAsync(() => main.decode({}), 'Missing required argument "bytes"'); | ||
await testThrowAsync(() => main.decode({ bytes: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('createUnsafe', async () => { | ||
await errTest(main.createUnsafe, {}, 'Missing required argument, must either provide "value" or "codec"'); | ||
await testThrowAsync(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"'); | ||
}); | ||
it('create', async () => { | ||
await errTest(main.create, {}, 'Missing required argument "bytes"'); | ||
await errTest(main.create, { bytes: true }, 'Missing required argument "hasher"'); | ||
await testThrowAsync(() => main.create({}), 'Missing required argument "bytes"'); | ||
await testThrowAsync(() => main.create({ bytes: true }), 'Missing required argument "hasher"'); | ||
const block = await main.encode({ | ||
@@ -187,3 +172,3 @@ value: fixture, | ||
}); | ||
await errTest(main.create, { | ||
await testThrowAsync(() => main.create({ | ||
bytes: block.bytes, | ||
@@ -193,3 +178,3 @@ cid: block2.cid, | ||
hasher | ||
}, 'CID hash does not match bytes'); | ||
}), 'CID hash does not match bytes'); | ||
}); | ||
@@ -202,5 +187,5 @@ it('get', async () => { | ||
}); | ||
await errTest(path => block.get(path), '/asd/fs/dfasd/f', 'Object has no property at ["asd"]'); | ||
await testThrowAsync(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]'); | ||
}); | ||
}); | ||
}); |
@@ -5,4 +5,4 @@ import * as bytes from '../src/bytes.js'; | ||
it('isBinary', () => { | ||
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer()), true); | ||
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer())), true); | ||
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer(0)), true); | ||
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer(0))), true); | ||
}); | ||
@@ -9,0 +9,0 @@ it('coerce', () => { |
@@ -20,3 +20,3 @@ import OLDCID from 'cids'; | ||
import invalidMultihash from './fixtures/invalid-multihash.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowSync as testThrow } from './fixtures/test-throw.js'; | ||
const textEncoder = new TextEncoder(); | ||
@@ -144,3 +144,3 @@ const testThrowAny = async fn => { | ||
assert.deepStrictEqual(cid.version, 1); | ||
assert.ok(equals(cid.multihash, hash)); | ||
equalDigest(cid.multihash, hash); | ||
}); | ||
@@ -381,3 +381,3 @@ it('can roundtrip through cid.toString()', async () => { | ||
assert.strictEqual(cid1.version, version); | ||
assert.ok(equals(cid1.multihash, hash)); | ||
assert.ok(equals(cid1.multihash.bytes, hash.bytes)); | ||
const cid2 = CID.asCID({ | ||
@@ -399,3 +399,3 @@ version, | ||
assert.strictEqual(cid3.version, version); | ||
assert.ok(equals(cid3.multihash, hash)); | ||
assert.ok(equals(cid3.multihash.bytes, hash.bytes)); | ||
const cid4 = CID.asCID(cid3); | ||
@@ -406,11 +406,10 @@ assert.strictEqual(cid3, cid4); | ||
assert.strictEqual(cid5.version, 1); | ||
assert.ok(equals(cid5.multihash, hash)); | ||
assert.ok(equals(cid5.multihash.bytes, hash.bytes)); | ||
assert.strictEqual(cid5.code, 85); | ||
}); | ||
const digestsame = (x, y) => { | ||
assert.deepStrictEqual(x.digest, y.digest); | ||
assert.deepStrictEqual(x.hash, y.hash); | ||
assert.deepStrictEqual(x.bytes, y.bytes); | ||
if (x.multihash) { | ||
digestsame(x.multihash, y.multihash); | ||
equalDigest(x.multihash, y.multihash); | ||
} | ||
@@ -431,2 +430,7 @@ const empty = { | ||
}; | ||
const equalDigest = (x, y) => { | ||
assert.deepStrictEqual(x.digest, y.digest); | ||
assert.deepStrictEqual(x.code, y.code); | ||
assert.deepStrictEqual(x.digest, y.digest); | ||
}; | ||
describe('CID.parse', async () => { | ||
@@ -494,3 +498,3 @@ it('parse 32 encoded CIDv1', async () => { | ||
assert.deepStrictEqual(cid.version, 1); | ||
assert.ok(equals(cid.multihash, hash)); | ||
equalDigest(cid.multihash, hash); | ||
assert.deepStrictEqual(cid.code, 85); | ||
@@ -497,0 +501,0 @@ }); |
@@ -5,3 +5,3 @@ 'use strict'; | ||
import { assert } from 'chai'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowSync as testThrow } from './fixtures/test-throw.js'; | ||
const encoded = [ | ||
@@ -8,0 +8,0 @@ { |
@@ -11,3 +11,3 @@ import * as bytes from '../src/bytes.js'; | ||
import * as b64 from '../src/bases/base64.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowAsync as testThrow } from './fixtures/test-throw.js'; | ||
const {base16, base32, base58btc, base64} = { | ||
@@ -140,2 +140,10 @@ ...b16, | ||
}); | ||
it('infers prefix and name corretly', () => { | ||
const name = base32.name; | ||
const name2 = base32.name; | ||
const prefix = base32.prefix; | ||
assert.equal(prefix, 'b'); | ||
assert.equal(name, 'base32'); | ||
assert.equal(name2, name); | ||
}); | ||
}); |
@@ -5,3 +5,3 @@ import * as bytes from '../src/bytes.js'; | ||
import * as json from '../src/codecs/json.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowAsync } from './fixtures/test-throw.js'; | ||
describe('multicodec', () => { | ||
@@ -11,3 +11,3 @@ it('encode/decode raw', () => { | ||
assert.deepStrictEqual(buff, bytes.fromString('test')); | ||
assert.deepStrictEqual(raw.decode(buff, 'raw'), bytes.fromString('test')); | ||
assert.deepStrictEqual(raw.decode(buff), bytes.fromString('test')); | ||
}); | ||
@@ -20,4 +20,4 @@ it('encode/decode json', () => { | ||
it('raw cannot encode string', async () => { | ||
await testThrow(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
await testThrowAsync(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
}); | ||
}); |
@@ -19,2 +19,3 @@ import { | ||
} from '../src/hashes/digest.js'; | ||
import { testThrowAsync } from './fixtures/test-throw.js'; | ||
const sample = (code, size, hex) => { | ||
@@ -29,12 +30,2 @@ const toHex = i => { | ||
}; | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multihash', () => { | ||
@@ -41,0 +32,0 @@ const empty = new Uint8Array(0); |
@@ -7,2 +7,3 @@ import * as codec from '../src/codecs/json.js'; | ||
import { assert } from 'chai'; | ||
import { fromString } from '../src/bytes.js'; | ||
const {createLink, createNode} = dagPB; | ||
@@ -12,3 +13,3 @@ describe('traversal', () => { | ||
const linksE = []; | ||
const valueE = createNode(Uint8Array.from('string E qacdswa'), linksE); | ||
const valueE = createNode(fromString('string E qacdswa'), linksE); | ||
const blockE = await main.encode({ | ||
@@ -21,3 +22,3 @@ value: valueE, | ||
const linksD = []; | ||
const valueD = createNode(Uint8Array.from('string D zasa'), linksD); | ||
const valueD = createNode(fromString('string D zasa'), linksD); | ||
const blockD = await main.encode({ | ||
@@ -33,3 +34,3 @@ value: valueD, | ||
]; | ||
const valueC = createNode(Uint8Array.from('string C zxc'), linksC); | ||
const valueC = createNode(fromString('string C zxc'), linksC); | ||
const blockC = await main.encode({ | ||
@@ -45,3 +46,3 @@ value: valueC, | ||
]; | ||
const valueB = createNode(Uint8Array.from('string B lpokjiasd'), linksB); | ||
const valueB = createNode(fromString('string B lpokjiasd'), linksB); | ||
const blockB = await main.encode({ | ||
@@ -57,3 +58,3 @@ value: valueB, | ||
]; | ||
const valueA = createNode(Uint8Array.from('string A qwertcfdgshaa'), linksA); | ||
const valueA = createNode(fromString('string A qwertcfdgshaa'), linksA); | ||
const blockA = await main.encode({ | ||
@@ -146,3 +147,3 @@ value: valueA, | ||
const links = []; | ||
const value = createNode(Uint8Array.from('test'), links); | ||
const value = createNode(fromString('test'), links); | ||
const block = await main.encode({ | ||
@@ -149,0 +150,0 @@ value: value, |
@@ -1,2 +0,2 @@ | ||
export default function testThrow(fn, message) { | ||
export const testThrowSync = (fn, message) => { | ||
try { | ||
@@ -10,2 +10,12 @@ fn(); | ||
throw new Error('Test failed to throw'); | ||
} | ||
}; | ||
export const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; |
@@ -9,2 +9,6 @@ import * as codec from '../src/codecs/json.js'; | ||
import { assert } from 'chai'; | ||
import { | ||
testThrowAsync, | ||
testThrowSync | ||
} from './fixtures/test-throw.js'; | ||
const fixture = { hello: 'world' }; | ||
@@ -138,37 +142,18 @@ const link = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae'); | ||
it('constructor missing args', () => { | ||
let threw = true; | ||
try { | ||
threw = new main.Block({}); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== 'Missing required argument') | ||
throw e; | ||
} | ||
assert.deepStrictEqual(threw, true); | ||
testThrowSync(() => new main.Block({}), 'Missing required argument'); | ||
}); | ||
const errTest = async (method, arg, message) => { | ||
let threw = true; | ||
try { | ||
await method(arg); | ||
threw = false; | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
} | ||
assert.deepStrictEqual(threw, true); | ||
}; | ||
it('encode', async () => { | ||
await errTest(main.encode, {}, 'Missing required argument "value"'); | ||
await errTest(main.encode, { value: true }, 'Missing required argument: codec or hasher'); | ||
await testThrowAsync(() => main.encode({}), 'Missing required argument "value"'); | ||
await testThrowAsync(() => main.encode({ value: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('decode', async () => { | ||
await errTest(main.decode, {}, 'Missing required argument "bytes"'); | ||
await errTest(main.decode, { bytes: true }, 'Missing required argument: codec or hasher'); | ||
await testThrowAsync(() => main.decode({}), 'Missing required argument "bytes"'); | ||
await testThrowAsync(() => main.decode({ bytes: true }), 'Missing required argument: codec or hasher'); | ||
}); | ||
it('createUnsafe', async () => { | ||
await errTest(main.createUnsafe, {}, 'Missing required argument, must either provide "value" or "codec"'); | ||
await testThrowAsync(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"'); | ||
}); | ||
it('create', async () => { | ||
await errTest(main.create, {}, 'Missing required argument "bytes"'); | ||
await errTest(main.create, { bytes: true }, 'Missing required argument "hasher"'); | ||
await testThrowAsync(() => main.create({}), 'Missing required argument "bytes"'); | ||
await testThrowAsync(() => main.create({ bytes: true }), 'Missing required argument "hasher"'); | ||
const block = await main.encode({ | ||
@@ -187,3 +172,3 @@ value: fixture, | ||
}); | ||
await errTest(main.create, { | ||
await testThrowAsync(() => main.create({ | ||
bytes: block.bytes, | ||
@@ -193,3 +178,3 @@ cid: block2.cid, | ||
hasher | ||
}, 'CID hash does not match bytes'); | ||
}), 'CID hash does not match bytes'); | ||
}); | ||
@@ -202,5 +187,5 @@ it('get', async () => { | ||
}); | ||
await errTest(path => block.get(path), '/asd/fs/dfasd/f', 'Object has no property at ["asd"]'); | ||
await testThrowAsync(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]'); | ||
}); | ||
}); | ||
}); |
@@ -5,4 +5,4 @@ import * as bytes from '../src/bytes.js'; | ||
it('isBinary', () => { | ||
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer()), true); | ||
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer())), true); | ||
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer(0)), true); | ||
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer(0))), true); | ||
}); | ||
@@ -9,0 +9,0 @@ it('coerce', () => { |
@@ -20,3 +20,3 @@ import OLDCID from 'cids'; | ||
import invalidMultihash from './fixtures/invalid-multihash.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowSync as testThrow } from './fixtures/test-throw.js'; | ||
const textEncoder = new TextEncoder(); | ||
@@ -144,3 +144,3 @@ const testThrowAny = async fn => { | ||
assert.deepStrictEqual(cid.version, 1); | ||
assert.ok(equals(cid.multihash, hash)); | ||
equalDigest(cid.multihash, hash); | ||
}); | ||
@@ -381,3 +381,3 @@ it('can roundtrip through cid.toString()', async () => { | ||
assert.strictEqual(cid1.version, version); | ||
assert.ok(equals(cid1.multihash, hash)); | ||
assert.ok(equals(cid1.multihash.bytes, hash.bytes)); | ||
const cid2 = CID.asCID({ | ||
@@ -399,3 +399,3 @@ version, | ||
assert.strictEqual(cid3.version, version); | ||
assert.ok(equals(cid3.multihash, hash)); | ||
assert.ok(equals(cid3.multihash.bytes, hash.bytes)); | ||
const cid4 = CID.asCID(cid3); | ||
@@ -406,11 +406,10 @@ assert.strictEqual(cid3, cid4); | ||
assert.strictEqual(cid5.version, 1); | ||
assert.ok(equals(cid5.multihash, hash)); | ||
assert.ok(equals(cid5.multihash.bytes, hash.bytes)); | ||
assert.strictEqual(cid5.code, 85); | ||
}); | ||
const digestsame = (x, y) => { | ||
assert.deepStrictEqual(x.digest, y.digest); | ||
assert.deepStrictEqual(x.hash, y.hash); | ||
assert.deepStrictEqual(x.bytes, y.bytes); | ||
if (x.multihash) { | ||
digestsame(x.multihash, y.multihash); | ||
equalDigest(x.multihash, y.multihash); | ||
} | ||
@@ -431,2 +430,7 @@ const empty = { | ||
}; | ||
const equalDigest = (x, y) => { | ||
assert.deepStrictEqual(x.digest, y.digest); | ||
assert.deepStrictEqual(x.code, y.code); | ||
assert.deepStrictEqual(x.digest, y.digest); | ||
}; | ||
describe('CID.parse', async () => { | ||
@@ -494,3 +498,3 @@ it('parse 32 encoded CIDv1', async () => { | ||
assert.deepStrictEqual(cid.version, 1); | ||
assert.ok(equals(cid.multihash, hash)); | ||
equalDigest(cid.multihash, hash); | ||
assert.deepStrictEqual(cid.code, 85); | ||
@@ -497,0 +501,0 @@ }); |
@@ -5,3 +5,3 @@ 'use strict'; | ||
import { assert } from 'chai'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowSync as testThrow } from './fixtures/test-throw.js'; | ||
const encoded = [ | ||
@@ -8,0 +8,0 @@ { |
@@ -11,3 +11,3 @@ import * as bytes from '../src/bytes.js'; | ||
import * as b64 from '../src/bases/base64.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowAsync as testThrow } from './fixtures/test-throw.js'; | ||
const {base16, base32, base58btc, base64} = { | ||
@@ -140,2 +140,10 @@ ...b16, | ||
}); | ||
it('infers prefix and name corretly', () => { | ||
const name = base32.name; | ||
const name2 = base32.name; | ||
const prefix = base32.prefix; | ||
assert.equal(prefix, 'b'); | ||
assert.equal(name, 'base32'); | ||
assert.equal(name2, name); | ||
}); | ||
}); |
@@ -5,3 +5,3 @@ import * as bytes from '../src/bytes.js'; | ||
import * as json from '../src/codecs/json.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
import { testThrowAsync } from './fixtures/test-throw.js'; | ||
describe('multicodec', () => { | ||
@@ -11,3 +11,3 @@ it('encode/decode raw', () => { | ||
assert.deepStrictEqual(buff, bytes.fromString('test')); | ||
assert.deepStrictEqual(raw.decode(buff, 'raw'), bytes.fromString('test')); | ||
assert.deepStrictEqual(raw.decode(buff), bytes.fromString('test')); | ||
}); | ||
@@ -20,4 +20,4 @@ it('encode/decode json', () => { | ||
it('raw cannot encode string', async () => { | ||
await testThrow(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
await testThrowAsync(() => raw.encode('asdf'), 'Unknown type, must be binary type'); | ||
}); | ||
}); |
@@ -19,2 +19,3 @@ import { | ||
} from '../src/hashes/digest.js'; | ||
import { testThrowAsync } from './fixtures/test-throw.js'; | ||
const sample = (code, size, hex) => { | ||
@@ -29,12 +30,2 @@ const toHex = i => { | ||
}; | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multihash', () => { | ||
@@ -41,0 +32,0 @@ const empty = new Uint8Array(0); |
@@ -7,2 +7,3 @@ import * as codec from '../src/codecs/json.js'; | ||
import { assert } from 'chai'; | ||
import { fromString } from '../src/bytes.js'; | ||
const {createLink, createNode} = dagPB; | ||
@@ -12,3 +13,3 @@ describe('traversal', () => { | ||
const linksE = []; | ||
const valueE = createNode(Uint8Array.from('string E qacdswa'), linksE); | ||
const valueE = createNode(fromString('string E qacdswa'), linksE); | ||
const blockE = await main.encode({ | ||
@@ -21,3 +22,3 @@ value: valueE, | ||
const linksD = []; | ||
const valueD = createNode(Uint8Array.from('string D zasa'), linksD); | ||
const valueD = createNode(fromString('string D zasa'), linksD); | ||
const blockD = await main.encode({ | ||
@@ -33,3 +34,3 @@ value: valueD, | ||
]; | ||
const valueC = createNode(Uint8Array.from('string C zxc'), linksC); | ||
const valueC = createNode(fromString('string C zxc'), linksC); | ||
const blockC = await main.encode({ | ||
@@ -45,3 +46,3 @@ value: valueC, | ||
]; | ||
const valueB = createNode(Uint8Array.from('string B lpokjiasd'), linksB); | ||
const valueB = createNode(fromString('string B lpokjiasd'), linksB); | ||
const blockB = await main.encode({ | ||
@@ -57,3 +58,3 @@ value: valueB, | ||
]; | ||
const valueA = createNode(Uint8Array.from('string A qwertcfdgshaa'), linksA); | ||
const valueA = createNode(fromString('string A qwertcfdgshaa'), linksA); | ||
const blockA = await main.encode({ | ||
@@ -146,3 +147,3 @@ value: valueA, | ||
const links = []; | ||
const value = createNode(Uint8Array.from('test'), links); | ||
const value = createNode(fromString('test'), links); | ||
const block = await main.encode({ | ||
@@ -149,0 +150,0 @@ value: value, |
{ | ||
"name": "multiformats", | ||
"version": "9.5.4", | ||
"version": "9.5.5", | ||
"description": "Interface for multihash, multicodec, multibase and CID", | ||
@@ -144,3 +144,5 @@ "main": "./cjs/src/index.js", | ||
"@stablelib/sha512": "^1.0.1", | ||
"@types/node": "^16.11.12", | ||
"@types/chai": "^4.3.0", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^17.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.6.0", | ||
@@ -156,3 +158,3 @@ "@typescript-eslint/parser": "^5.6.0", | ||
"standard": "^16.0.4", | ||
"typescript": "^4.5.2" | ||
"typescript": "^4.5.4" | ||
}, | ||
@@ -159,0 +161,0 @@ "standard": { |
@@ -356,5 +356,7 @@ import basex from '../../vendor/base-x.js' | ||
* | ||
* @template {string} Base | ||
* @template {string} Prefix | ||
* @param {Object} options | ||
* @param {string} options.name | ||
* @param {string} options.prefix | ||
* @param {Base} options.name | ||
* @param {Prefix} options.prefix | ||
* @param {string} options.alphabet | ||
@@ -361,0 +363,0 @@ * @param {number} options.bitsPerChar |
@@ -1,7 +0,10 @@ | ||
export default function testThrow (fn, message) { | ||
/** | ||
* @param {Function} fn | ||
* @param {string} message | ||
*/ | ||
export const testThrowSync = (fn, message) => { | ||
try { | ||
fn() | ||
} catch (e) { | ||
if (e.message !== message) throw e | ||
if (/** @type {Error} */(e).message !== message) throw e | ||
return | ||
@@ -12,1 +15,16 @@ } | ||
} | ||
/** | ||
* @param {Function} fn | ||
* @param {string} message | ||
*/ | ||
export const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn() | ||
} catch (e) { | ||
if (/** @type {Error} */(e).message !== message) throw e | ||
return | ||
} | ||
/* c8 ignore next */ | ||
throw new Error('Test failed to throw') | ||
} |
@@ -7,2 +7,3 @@ /* globals describe, it */ | ||
import { assert } from 'chai' | ||
import { testThrowAsync, testThrowSync } from './fixtures/test-throw.js' | ||
@@ -31,3 +32,11 @@ const fixture = { hello: 'world' } | ||
describe('reader', () => { | ||
const value = { link, nope: 'skip', arr: [link], obj: { arr: [{ obj: {} }] }, bytes: Uint8Array.from('1234') } | ||
const value = { | ||
link, | ||
nope: 'skip', | ||
arr: [link], | ||
obj: { arr: [{ obj: {} }] }, | ||
// @ts-expect-error - 'string' is not assignable to parameter of type 'ArrayLike<number>' | ||
bytes: Uint8Array.from('1234') | ||
} | ||
// @ts-expect-error - 'boolean' is not assignable to type 'CID' | ||
const block = main.createUnsafe({ value, codec, hasher, cid: true, bytes: true }) | ||
@@ -55,2 +64,3 @@ | ||
ret = block.get('nope') | ||
// @ts-expect-error - 'string' is not expected | ||
assert.deepStrictEqual(ret, { value: 'skip' }) | ||
@@ -60,3 +70,11 @@ }) | ||
it('null links/tree', () => { | ||
const block = main.createUnsafe({ value: null, codec, hasher, bytes: true, cid: true }) | ||
const block = main.createUnsafe({ | ||
value: null, | ||
codec, | ||
hasher, | ||
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>' | ||
bytes: true, | ||
// @ts-expect-error - 'boolean' is not assignable to type 'CID' | ||
cid: true | ||
}) | ||
// eslint-disable-next-line | ||
@@ -75,3 +93,10 @@ for (const x of block.tree()) { | ||
const sink = { one: { two: { arr: [true, false, null], three: 3, buff, link } } } | ||
const block = main.createUnsafe({ value: sink, codec, bytes: true, cid: true }) | ||
const block = main.createUnsafe({ | ||
value: sink, | ||
codec, | ||
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>' | ||
bytes: true, | ||
// @ts-expect-error - 'boolean' is not assignable to type 'CID' | ||
cid: true | ||
}) | ||
assert.deepStrictEqual(sink, block.value) | ||
@@ -82,43 +107,36 @@ }) | ||
it('constructor missing args', () => { | ||
let threw = true | ||
try { | ||
threw = new main.Block({}) | ||
threw = false | ||
} catch (e) { | ||
if (e.message !== 'Missing required argument') throw e | ||
} | ||
assert.deepStrictEqual(threw, true) | ||
testThrowSync( | ||
// @ts-expect-error - missing properties | ||
() => new main.Block({}), | ||
'Missing required argument' | ||
) | ||
}) | ||
const errTest = async (method, arg, message) => { | ||
let threw = true | ||
try { | ||
await method(arg) | ||
threw = false | ||
} catch (e) { | ||
if (e.message !== message) throw e | ||
} | ||
assert.deepStrictEqual(threw, true) | ||
} | ||
it('encode', async () => { | ||
await errTest(main.encode, {}, 'Missing required argument "value"') | ||
await errTest(main.encode, { value: true }, 'Missing required argument: codec or hasher') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.encode({}), 'Missing required argument "value"') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.encode({ value: true }), 'Missing required argument: codec or hasher') | ||
}) | ||
it('decode', async () => { | ||
await errTest(main.decode, {}, 'Missing required argument "bytes"') | ||
await errTest(main.decode, { bytes: true }, 'Missing required argument: codec or hasher') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.decode({}), 'Missing required argument "bytes"') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.decode({ bytes: true }), 'Missing required argument: codec or hasher') | ||
}) | ||
it('createUnsafe', async () => { | ||
await errTest(main.createUnsafe, {}, 'Missing required argument, must either provide "value" or "codec"') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"') | ||
}) | ||
it('create', async () => { | ||
await errTest(main.create, {}, 'Missing required argument "bytes"') | ||
await errTest(main.create, { bytes: true }, 'Missing required argument "hasher"') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.create({}), 'Missing required argument "bytes"') | ||
// @ts-expect-error | ||
await testThrowAsync(() => main.create({ bytes: true }), 'Missing required argument "hasher"') | ||
const block = await main.encode({ value: fixture, codec, hasher }) | ||
const block2 = await main.encode({ value: { ...fixture, test: 'blah' }, codec, hasher }) | ||
await errTest(main.create, { bytes: block.bytes, cid: block2.cid, codec, hasher }, 'CID hash does not match bytes') | ||
await testThrowAsync(() => main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes') | ||
}) | ||
@@ -128,5 +146,5 @@ | ||
const block = await main.encode({ value: fixture, codec, hasher }) | ||
await errTest(path => block.get(path), '/asd/fs/dfasd/f', 'Object has no property at ["asd"]') | ||
await testThrowAsync(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]') | ||
}) | ||
}) | ||
}) |
@@ -7,4 +7,4 @@ /* globals describe, it */ | ||
it('isBinary', () => { | ||
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer()), true) | ||
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer())), true) | ||
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer(0)), true) | ||
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer(0))), true) | ||
}) | ||
@@ -11,0 +11,0 @@ |
@@ -12,6 +12,9 @@ /* globals describe, it */ | ||
import invalidMultihash from './fixtures/invalid-multihash.js' | ||
import testThrow from './fixtures/test-throw.js' | ||
import { testThrowSync as testThrow } from './fixtures/test-throw.js' | ||
const textEncoder = new TextEncoder() | ||
/** | ||
* @param {Function} fn | ||
*/ | ||
const testThrowAny = async fn => { | ||
@@ -103,3 +106,3 @@ try { | ||
const newCid = CID.asCID(oldCid) | ||
assert.deepStrictEqual(newCid.toString(), cidStr) | ||
assert.deepStrictEqual(/** @type {CID} */(newCid).toString(), cidStr) | ||
}) | ||
@@ -161,3 +164,3 @@ | ||
assert.deepStrictEqual(cid.version, 1) | ||
assert.ok(equals(cid.multihash, hash)) | ||
equalDigest(cid.multihash, hash) | ||
}) | ||
@@ -209,3 +212,3 @@ | ||
const newCid = CID.asCID(oldCid) | ||
assert.deepStrictEqual(newCid.toString(), cidStr) | ||
assert.deepStrictEqual(/** @type {CID} */(newCid).toString(), cidStr) | ||
}) | ||
@@ -261,2 +264,3 @@ }) | ||
const ch1 = CID.parse(h1) | ||
// @ts-expect-error - '_baseCache' is private | ||
ch1._baseCache.set('herp', 'derp') | ||
@@ -299,2 +303,3 @@ assert.deepStrictEqual(ch1, CID.parse(h1)) | ||
const name = `CID.create(${version}, ${code}, ${mh})` | ||
// @ts-expect-error - version issn't always 0|1 | ||
it(name, async () => await testThrowAny(() => CID.create(version, code, hash))) | ||
@@ -371,7 +376,12 @@ } | ||
const cid = CID.create(1, 112, hash) | ||
// @ts-expect-error - _baseCache is private | ||
assert.deepStrictEqual(cid._baseCache.size, 0) | ||
assert.deepStrictEqual(cid.toString(base64), 'mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt') | ||
// @ts-expect-error - _baseCache is private | ||
assert.deepStrictEqual(cid._baseCache.get(base64.prefix), 'mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt') | ||
// @ts-expect-error - _baseCache is private | ||
assert.deepStrictEqual(cid._baseCache.has(base32.prefix), false) | ||
@@ -382,2 +392,3 @@ | ||
// @ts-expect-error - _baseCache is private | ||
assert.deepStrictEqual(cid._baseCache.get(base32.prefix), base32String) | ||
@@ -390,2 +401,3 @@ assert.deepStrictEqual(cid.toString(base64), 'mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt') | ||
const cid = CID.parse(base32String) | ||
// @ts-expect-error - _baseCache is private | ||
assert.deepStrictEqual(cid._baseCache.get(base32.prefix), base32String) | ||
@@ -413,2 +425,7 @@ }) | ||
class IncompatibleCID { | ||
/** | ||
* @param {number} version | ||
* @param {number} code | ||
* @param {import('multiformats/hashes/interface').MultihashDigest} multihash | ||
*/ | ||
constructor (version, code, multihash) { | ||
@@ -432,9 +449,10 @@ this.version = version | ||
assert.strictEqual(incompatibleCID.toString(), '[object Object]') | ||
// @ts-expect-error - no such method | ||
assert.strictEqual(typeof incompatibleCID.toV0, 'undefined') | ||
const cid1 = CID.asCID(incompatibleCID) | ||
const cid1 = /** @type {CID} */(CID.asCID(incompatibleCID)) | ||
assert.ok(cid1 instanceof CID) | ||
assert.strictEqual(cid1.code, code) | ||
assert.strictEqual(cid1.version, version) | ||
assert.ok(equals(cid1.multihash, hash)) | ||
assert.ok(equals(cid1.multihash.bytes, hash.bytes)) | ||
@@ -445,8 +463,9 @@ const cid2 = CID.asCID({ version, code, hash }) | ||
const duckCID = { version, code, multihash: hash } | ||
// @ts-expect-error - no such property | ||
duckCID.asCID = duckCID | ||
const cid3 = CID.asCID(duckCID) | ||
const cid3 = /** @type {CID} */ (CID.asCID(duckCID)) | ||
assert.ok(cid3 instanceof CID) | ||
assert.strictEqual(cid3.code, code) | ||
assert.strictEqual(cid3.version, version) | ||
assert.ok(equals(cid3.multihash, hash)) | ||
assert.ok(equals(cid3.multihash.bytes, hash.bytes)) | ||
@@ -456,15 +475,19 @@ const cid4 = CID.asCID(cid3) | ||
const cid5 = CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) | ||
const cid5 = /** @type {CID} */(CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes)))) | ||
assert.ok(cid5 instanceof CID) | ||
assert.strictEqual(cid5.version, 1) | ||
assert.ok(equals(cid5.multihash, hash)) | ||
assert.ok(equals(cid5.multihash.bytes, hash.bytes)) | ||
assert.strictEqual(cid5.code, 85) | ||
}) | ||
/** | ||
* @param {CID} x | ||
* @param {CID} y | ||
*/ | ||
const digestsame = (x, y) => { | ||
assert.deepStrictEqual(x.digest, y.digest) | ||
// @ts-ignore - not sure what this supposed to be | ||
assert.deepStrictEqual(x.hash, y.hash) | ||
assert.deepStrictEqual(x.bytes, y.bytes) | ||
if (x.multihash) { | ||
digestsame(x.multihash, y.multihash) | ||
equalDigest(x.multihash, y.multihash) | ||
} | ||
@@ -475,2 +498,13 @@ const empty = { hash: null, bytes: null, digest: null, multihash: null } | ||
/** | ||
* @typedef {import('multiformats/hashes/interface').MultihashDigest} MultihashDigest | ||
* @param {MultihashDigest} x | ||
* @param {MultihashDigest} y | ||
*/ | ||
const equalDigest = (x, y) => { | ||
assert.deepStrictEqual(x.digest, y.digest) | ||
assert.deepStrictEqual(x.code, y.code) | ||
assert.deepStrictEqual(x.digest, y.digest) | ||
} | ||
describe('CID.parse', async () => { | ||
@@ -489,3 +523,3 @@ it('parse 32 encoded CIDv1', async () => { | ||
const parsed = CID.parse(cid.toString(base58btc)) | ||
const parsed = /** @type {CID} */(CID.parse(cid.toString(base58btc))) | ||
digestsame(cid, parsed) | ||
@@ -550,6 +584,6 @@ }) | ||
const hash = await sha256.digest(textEncoder.encode('abc')) | ||
const cid = CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) | ||
const cid = /** @type {CID} */ (CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes)))) | ||
assert.deepStrictEqual(cid.version, 1) | ||
assert.ok(equals(cid.multihash, hash)) | ||
equalDigest(cid.multihash, hash) | ||
assert.deepStrictEqual(cid.code, 85) | ||
@@ -561,3 +595,5 @@ }) | ||
const cid = CID.create(1, 112, hash) | ||
// @ts-expect-error - no such method is known | ||
assert.deepStrictEqual(typeof cid[Symbol.for('nodejs.util.inspect.custom')], 'function') | ||
// @ts-expect-error - no such method is known | ||
assert.deepStrictEqual(cid[Symbol.for('nodejs.util.inspect.custom')](), 'CID(bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu)') | ||
@@ -571,2 +607,3 @@ }) | ||
await testThrow(() => cid.codec, '"codec" property is deprecated, use integer "code" property instead') | ||
// @ts-expect-error - 'string' is not assignable to parameter of type 'number' | ||
await testThrow(() => CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported') | ||
@@ -590,2 +627,3 @@ }) | ||
const cid = CID.create(1, 112, hash) | ||
// @ts-expect-error - deprecated | ||
await testThrow(() => cid.toBaseEncodedString(), 'Deprecated, use .toString()') | ||
@@ -592,0 +630,0 @@ }) |
@@ -7,3 +7,3 @@ /* eslint-env mocha */ | ||
import { assert } from 'chai' | ||
import testThrow from './fixtures/test-throw.js' | ||
import { testThrowSync as testThrow } from './fixtures/test-throw.js' | ||
@@ -175,3 +175,3 @@ const encoded = [ | ||
for (const [name, output] of tests) { | ||
const base = bases[name] | ||
const base = bases[/** @type {keyof bases} */(name)] | ||
@@ -178,0 +178,0 @@ describe(name, () => { |
@@ -12,3 +12,3 @@ /* globals describe, it */ | ||
import * as b64 from 'multiformats/bases/base64' | ||
import testThrow from './fixtures/test-throw.js' | ||
import { testThrowAsync as testThrow } from './fixtures/test-throw.js' | ||
@@ -51,3 +51,5 @@ const { base16, base32, base58btc, base64 } = { ...b16, ...b32, ...b58, ...b64 } | ||
const msg = 'Unknown type, must be binary type' | ||
// @ts-expect-error - expects bytes | ||
testThrow(() => base32.encode('asdf'), msg) | ||
// @ts-expect-error - expects bytes | ||
testThrow(() => base32.encoder.encode('asdf'), msg) | ||
@@ -58,3 +60,5 @@ }) | ||
const msg = 'Can only multibase decode strings' | ||
// @ts-expect-error - 'number' is not assignable to parameter of type 'string' | ||
testThrow(() => base32.decode(1), msg) | ||
// @ts-expect-error - 'number' is not assignable to parameter of type 'string' | ||
testThrow(() => base32.decoder.decode(1), msg) | ||
@@ -66,2 +70,5 @@ }) | ||
/** | ||
* @param {typeof b2|b8|b10|b16|b32|b36|b58|b64} bases | ||
*/ | ||
const baseTest = bases => { | ||
@@ -160,2 +167,17 @@ for (const base of Object.values(bases)) { | ||
}) | ||
it('infers prefix and name corretly', () => { | ||
/** @type {'base32'} */ | ||
const name = base32.name | ||
/** @type {'base16'} */ | ||
// @ts-expect-error - TS catches mismatch | ||
const name2 = base32.name | ||
/** @type {'b'} */ | ||
const prefix = base32.prefix | ||
assert.equal(prefix, 'b') | ||
assert.equal(name, 'base32') | ||
assert.equal(name2, name) | ||
}) | ||
}) |
@@ -6,3 +6,3 @@ /* globals describe, it */ | ||
import * as json from 'multiformats/codecs/json' | ||
import testThrow from './fixtures/test-throw.js' | ||
import { testThrowAsync } from './fixtures/test-throw.js' | ||
@@ -13,3 +13,3 @@ describe('multicodec', () => { | ||
assert.deepStrictEqual(buff, bytes.fromString('test')) | ||
assert.deepStrictEqual(raw.decode(buff, 'raw'), bytes.fromString('test')) | ||
assert.deepStrictEqual(raw.decode(buff), bytes.fromString('test')) | ||
}) | ||
@@ -24,4 +24,5 @@ | ||
it('raw cannot encode string', async () => { | ||
await testThrow(() => raw.encode('asdf'), 'Unknown type, must be binary type') | ||
// @ts-expect-error - 'string' is not assignable to parameter of type 'Uint8Array' | ||
await testThrowAsync(() => raw.encode('asdf'), 'Unknown type, must be binary type') | ||
}) | ||
}) |
@@ -11,4 +11,13 @@ /* globals describe, it */ | ||
import { decode as decodeDigest, create as createDigest } from 'multiformats/hashes/digest' | ||
import { testThrowAsync } from './fixtures/test-throw.js' | ||
/** | ||
* @param {number|string} code | ||
* @param {number} size | ||
* @param {string} hex | ||
*/ | ||
const sample = (code, size, hex) => { | ||
/** | ||
* @param {number|string} i | ||
*/ | ||
const toHex = (i) => { | ||
@@ -22,13 +31,2 @@ if (typeof i === 'string') return i | ||
const testThrowAsync = async (fn, message) => { | ||
try { | ||
await fn() | ||
} catch (e) { | ||
if (e.message !== message) throw e | ||
return | ||
} | ||
/* c8 ignore next */ | ||
throw new Error('Test failed to throw') | ||
} | ||
describe('multihash', () => { | ||
@@ -109,4 +107,5 @@ const empty = new Uint8Array(0) | ||
it('throw on hashing non-buffer', async () => { | ||
// @ts-expect-error - string is incompatible arg | ||
await testThrowAsync(() => sha256.digest('asdf'), 'Unknown type, must be binary type') | ||
}) | ||
}) |
@@ -8,2 +8,3 @@ /* globals describe, it */ | ||
import { assert } from 'chai' | ||
import { fromString } from '../src/bytes.js' | ||
@@ -20,9 +21,9 @@ const { createLink, createNode } = dagPB | ||
// D D D E | ||
const linksE = [] | ||
const valueE = createNode(Uint8Array.from('string E qacdswa'), linksE) | ||
const linksE = /** @type {[]} */([]) | ||
const valueE = createNode(fromString('string E qacdswa'), linksE) | ||
const blockE = await main.encode({ value: valueE, codec, hasher }) | ||
const cidE = blockE.cid | ||
const linksD = [] | ||
const valueD = createNode(Uint8Array.from('string D zasa'), linksD) | ||
const linksD = /** @type {[]} */([]) | ||
const valueD = createNode(fromString('string D zasa'), linksD) | ||
const blockD = await main.encode({ value: valueD, codec, hasher }) | ||
@@ -32,3 +33,3 @@ const cidD = blockD.cid | ||
const linksC = [createLink('link1', 100, cidD), createLink('link2', 100, cidE)] | ||
const valueC = createNode(Uint8Array.from('string C zxc'), linksC) | ||
const valueC = createNode(fromString('string C zxc'), linksC) | ||
const blockC = await main.encode({ value: valueC, codec, hasher }) | ||
@@ -38,3 +39,3 @@ const cidC = blockC.cid | ||
const linksB = [createLink('link1', 100, cidD), createLink('link2', 100, cidD)] | ||
const valueB = createNode(Uint8Array.from('string B lpokjiasd'), linksB) | ||
const valueB = createNode(fromString('string B lpokjiasd'), linksB) | ||
const blockB = await main.encode({ value: valueB, codec, hasher }) | ||
@@ -44,6 +45,9 @@ const cidB = blockB.cid | ||
const linksA = [createLink('link1', 100, cidB), createLink('link2', 100, cidC)] | ||
const valueA = createNode(Uint8Array.from('string A qwertcfdgshaa'), linksA) | ||
const valueA = createNode(fromString('string A qwertcfdgshaa'), linksA) | ||
const blockA = await main.encode({ value: valueA, codec, hasher }) | ||
const cidA = blockA.cid | ||
/** | ||
* @param {import('multiformats').CID} cid | ||
*/ | ||
const load = async (cid) => { | ||
@@ -68,6 +72,14 @@ if (cid.equals(cidE)) { | ||
const loadWrapper = (load, arr = []) => (cid) => { | ||
arr.push(cid.toString()) | ||
return load(cid) | ||
} | ||
/** | ||
* @param {typeof load} load | ||
* @param {string[]} arr | ||
*/ | ||
const loadWrapper = (load, arr = []) => | ||
/** | ||
* @param {import('multiformats').CID} cid | ||
*/ | ||
(cid) => { | ||
arr.push(cid.toString()) | ||
return load(cid) | ||
} | ||
@@ -81,2 +93,3 @@ it('block with no links', async () => { | ||
const expectedCallArray = [cidD.toString()] | ||
/** @type {string[]} */ | ||
const callArray = [] | ||
@@ -100,2 +113,3 @@ | ||
const expectedCallArray = [cidC.toString(), cidD.toString(), cidE.toString()] | ||
/** @type {string[]} */ | ||
const callArray = [] | ||
@@ -119,2 +133,3 @@ | ||
const expectedCallArray = [cidB.toString(), cidD.toString()] | ||
/** @type {string[]} */ | ||
const callArray = [] | ||
@@ -146,2 +161,3 @@ | ||
] | ||
/** @type {string[]} */ | ||
const callArray = [] | ||
@@ -157,7 +173,9 @@ | ||
it('null return', async () => { | ||
/** @type {[]} */ | ||
const links = [] | ||
const value = createNode(Uint8Array.from('test'), links) | ||
const value = createNode(fromString('test'), links) | ||
const block = await main.encode({ value: value, codec, hasher }) | ||
const cid = block.cid | ||
const expectedCallArray = [cid.toString()] | ||
/** @type {string[]} */ | ||
const callArray = [] | ||
@@ -164,0 +182,0 @@ |
@@ -19,3 +19,3 @@ { | ||
"esModuleInterop": true, | ||
"target": "ES2018", | ||
"target": "ES2020", | ||
"moduleResolution": "node", | ||
@@ -30,5 +30,71 @@ "declaration": true, | ||
"baseUrl": ".", | ||
// unfortunately we have to manually path map each file so that | ||
// ts can resolve imports correctly (which workarounds resolution | ||
// occuring from node_modules/multiformats which is installed by dev | ||
// dependency) | ||
// Once https://www.typescriptlang.org/docs/handbook/esm-node.html | ||
// is stabilized we will no longer need this hack. | ||
"paths": { | ||
"multiformats": [ | ||
"src" | ||
"src/index.js" | ||
], | ||
"multiformats/cid": [ | ||
"./src/cid.js" | ||
], | ||
"multiformats/basics": [ | ||
"./src/basics.js" | ||
], | ||
"multiformats/block": [ | ||
"./src/block.js" | ||
], | ||
"multiformats/traversal": [ | ||
"./src/traversal.js" | ||
], | ||
"multiformats/bases/identity": [ | ||
"./src/bases/identity.js" | ||
], | ||
"multiformats/bases/base2": [ | ||
"./src/bases/base2.js" | ||
], | ||
"multiformats/bases/base8": [ | ||
"./src/bases/base8.js" | ||
], | ||
"multiformats/bases/base10": [ | ||
"./src/bases/base10.js" | ||
], | ||
"multiformats/bases/base16": [ | ||
"./src/bases/base16.js" | ||
], | ||
"multiformats/bases/base32": [ | ||
"./src/bases/base32.js" | ||
], | ||
"multiformats/bases/base36": [ | ||
"./src/bases/base36.js" | ||
], | ||
"multiformats/bases/base58": [ | ||
"./src/bases/base58.js" | ||
], | ||
"multiformats/bases/base64": [ | ||
"./src/bases/base64.js" | ||
], | ||
"multiformats/hashes/hasher": [ | ||
"./src/hashes/hasher.js" | ||
], | ||
"multiformats/hashes/digest": [ | ||
"./src/hashes/digest.js" | ||
], | ||
"multiformats/hashes/sha2": [ | ||
"./src/hashes/sha2-browser.js" | ||
], | ||
"multiformats/hashes/identity": [ | ||
"./src/hashes/identity.js" | ||
], | ||
"multiformats/codecs/json": [ | ||
"./src/codecs/json.js" | ||
], | ||
"multiformats/codecs/raw": [ | ||
"src/codecs/raw.js" | ||
], | ||
"multiformats/*": [ | ||
"src/*" | ||
] | ||
@@ -38,9 +104,11 @@ } | ||
"include": [ | ||
"src" | ||
"src", | ||
"test" | ||
], | ||
"exclude": [ | ||
"vendor", | ||
"node_modules" | ||
"node_modules", | ||
"node_modules/multiformats" | ||
], | ||
"compileOnSave": false | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
483106
240
14510
17