From e68cfb8dc7761e912abf27755ec2208c2531b845 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Wed, 31 Mar 2021 17:42:59 +1100 Subject: [PATCH] fix!: multiformats@7, latest codecs, improve types & type exports --- lib/decoder.js | 2 -- lib/encoder.js | 2 -- package.json | 39 +++++++++++++++++++++--------------- test/common.js | 44 +++++++++++++++++++++++++++-------------- test/node-test-large.js | 13 +++++------- tsconfig.json | 13 ++++++------ 6 files changed, 63 insertions(+), 50 deletions(-) diff --git a/lib/decoder.js b/lib/decoder.js index c475101..a8a4050 100644 --- a/lib/decoder.js +++ b/lib/decoder.js @@ -1,8 +1,6 @@ -// @ts-ignore import varint from 'varint' import { CID } from 'multiformats/cid' import * as Digest from 'multiformats/hashes/digest' -// @ts-ignore import { decode as decodeDagCbor } from '@ipld/dag-cbor' /** diff --git a/lib/encoder.js b/lib/encoder.js index 3c723bd..35061ca 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -1,6 +1,4 @@ -// @ts-ignore import varint from 'varint' -// @ts-ignore import { encode as dagCborEncode } from '@ipld/dag-cbor' /** diff --git a/package.json b/package.json index 0b90004..700c184 100644 --- a/package.json +++ b/package.json @@ -4,16 +4,17 @@ "description": "Content Addressable aRchive format reader and writer", "main": "car.js", "type": "module", + "types": "./types/car.d.ts", "directories": { "test": "test" }, "scripts": { "build": "npm run build:js && npm run build:types", - "build:js": "npm_config_yes=true ipjs build --tests --main && npm run build:copy", + "build:js": "ipjs build --tests --main && npm run build:copy", "build:copy": "cp -a tsconfig.json *.js *.ts lib/ test/ dist/", "build:types": "npm run build:copy && cd dist && tsc --build", "prepublishOnly": "npm run build", - "publish": "npm_config_yes=true npx ipjs@latest publish", + "publish": "ipjs publish", "lint": "standard", "test:cjs": "rm -rf dist && npm run build && cp test/go.car dist/cjs/node-test/ && mocha dist/cjs/node-test/test-*.js && mocha dist/cjs/node-test/node-test-*.js && npm run test:cjs:browser", "test:node": "hundreds mocha test/test-*.js test/node-test-*.js", @@ -38,25 +39,28 @@ }, "homepage": "https://github.com/ipld/js-car#readme", "devDependencies": { - "@ipld/dag-pb": "^0.0.1", - "@types/mocha": "^8.0.4", - "@types/node": "^14.14.9", - "@typescript-eslint/eslint-plugin": "^4.8.1", - "@typescript-eslint/parser": "^4.8.1", - "chai": "^4.2.0", + "@ipld/dag-pb": "^1.0.0", + "@types/chai": "^4.2.16", + "@types/chai-as-promised": "^7.1.3", + "@types/mocha": "^8.2.2", + "@types/node": "^14.14.41", + "@typescript-eslint/eslint-plugin": "^4.22.0", + "@typescript-eslint/parser": "^4.22.0", + "chai": "^4.3.4", "chai-as-promised": "^7.1.1", - "garbage": "^0.0.0", - "hundreds": "0.0.9", - "ipjs": "^3.3.0", + "hundreds": "^0.0.9", + "ipjs": "^5.0.0", + "ipld-garbage": "^3.0.2", "jsdoc4readme": "^1.4.0", - "mocha": "^8.2.0", + "mocha": "^8.3.2", "polendina": "^1.1.0", "standard": "^16.0.3", - "typescript": "^4.2.3" + "typescript": "^4.2.4" }, "dependencies": { - "@ipld/dag-cbor": "^2.0.3", - "multiformats": "git://github.com/multiformats/js-multiformats.git#fix/type-mapping", + "@ipld/dag-cbor": "^5.0.0", + "@types/varint": "^6.0.0", + "multiformats": "^7.0.0", "varint": "^6.0.0" }, "exports": { @@ -85,7 +89,10 @@ "typesVersions": { "*": { "*": [ - "dist/types/*" + "types/*" + ], + "types/*": [ + "types/*" ] } } diff --git a/test/common.js b/test/common.js index 742deda..22a514e 100644 --- a/test/common.js +++ b/test/common.js @@ -1,32 +1,45 @@ import { bytes, CID } from 'multiformats' import { sha256 } from 'multiformats/hashes/sha2' import * as raw from 'multiformats/codecs/raw' -// @ts-ignore import * as dagCbor from '@ipld/dag-cbor' -// @ts-ignore import * as dagPb from '@ipld/dag-pb' -// @ts-ignore import chai from 'chai' -// @ts-ignore import chaiAsPromised from 'chai-as-promised' /** * @typedef {import('../api').Block} Block + * @typedef {import('@ipld/dag-pb').PBNode} PBNode */ +/** + * @extends {Block} + */ +class TestBlock { + /** + * @param {Uint8Array} bytes + * @param {CID} cid + * @param {any} object + */ + constructor (bytes, cid, object) { + this.bytes = bytes + this.cid = cid + this.object = object + } +} + chai.use(chaiAsPromised) const { assert } = chai -/** @type {Block[]} */ +/** @type {TestBlock[]} */ let rawBlocks -/** @type {Block[]} */ +/** @type {TestBlock[]} */ const pbBlocks = [] -/** @type {Block[]} */ +/** @type {TestBlock[]} */ const cborBlocks = [] -/** @type {[string, Block[]][]} */ +/** @type {[string, TestBlock[]][]} */ let allBlocks -/** @type {Block[]} */ +/** @type {TestBlock[]} */ let allBlocksFlattened const rndCid = CID.parse('bafyreihyrpefhacm6kkp4ql6j6udakdit7g3dmkzfriqfykhjw6cad5lrm') @@ -35,13 +48,13 @@ const rndCid = CID.parse('bafyreihyrpefhacm6kkp4ql6j6udakdit7g3dmkzfriqfykhjw6ca * @param {any} object * @param {{code: number, encode: (obj: any) => Uint8Array}} codec * @param {number} version - * @returns {Promise} + * @returns {Promise} */ async function toBlock (object, codec, version = 1) { const bytes = codec.encode(object) const hash = await sha256.digest(bytes) const cid = CID.create(version, codec.code, hash) - return { bytes, cid, object } + return new TestBlock(bytes, cid, object) } async function makeData () { @@ -52,14 +65,15 @@ async function makeData () { /** * @param {string} name - * @param {Block} block + * @param {TestBlock} block */ const toPbLink = (name, block) => { let size = block.bytes.length if (block.cid.code === 0x70) { // special cumulative size handling for linking to dag-pb blocks - // @ts-ignore - size = block.object.Links.reduce((p, c) => p + c.Tsize, size) + /** @type {PBNode} */ + const node = block.object + size = node.Links.reduce((p, c) => p + (c.Tsize || 0), size) } return { Name: name, @@ -82,7 +96,7 @@ async function makeData () { } allBlocks = [['raw', rawBlocks.slice(0, 3)], ['pb', pbBlocks], ['cbor', cborBlocks]] - allBlocksFlattened = allBlocks.reduce((/** @type {Block[]} */ p, c) => p.concat(c[1]), []) + allBlocksFlattened = allBlocks.reduce((/** @type {TestBlock[]} */ p, c) => p.concat(c[1]), /** @type {TestBlock[]} */ []) } return { diff --git a/test/node-test-large.js b/test/node-test-large.js index c1f5448..32132de 100644 --- a/test/node-test-large.js +++ b/test/node-test-large.js @@ -4,11 +4,8 @@ import fs from 'fs' import { Readable } from 'stream' -// @ts-ignore -import garbage from 'garbage' -// @ts-ignore +import { garbage } from 'ipld-garbage' import varint from 'varint' -// @ts-ignore import * as dagCbor from '@ipld/dag-cbor' import { sha256 } from 'multiformats/hashes/sha2' import { CID } from 'multiformats/cid' @@ -30,13 +27,13 @@ describe('Large CAR', () => { Readable.from(out).pipe(fs.createWriteStream('./test.car')) // offset starts at header length - let offset = dagCbor.encode({ version: 1, roots: [] }, 'dag-cbor').length - offset += varint.encode(offset.length).length + let offset = dagCbor.encode({ version: 1, roots: [] }).length + offset += varint.encode(offset).length for (let i = 0; i < 500; i++) { - const obj = garbage.object(1000) + const obj = garbage(1000) objects.push(obj) - const bytes = dagCbor.encode(obj, 'dag-cbor') + const bytes = dagCbor.encode(obj) const hash = await sha256.digest(bytes) const cid = CID.create(1, dagCbor.code, hash) cids.push(cid.toString()) diff --git a/tsconfig.json b/tsconfig.json index f33d6aa..71181a6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,11 +21,12 @@ "moduleResolution": "node", "declaration": true, "declarationMap": true, - "emitDeclarationOnly": true, "outDir": "types", "skipLibCheck": true, "stripInternal": true, "resolveJsonModule": true, + "emitDeclarationOnly": true, + "baseUrl": ".", "paths": { "@ipld/car": [ "car.js", "car-browser.js", "lib/" ], "@ipld/car/writer": [ "./lib/writer.js" ], @@ -33,14 +34,12 @@ "@ipld/car/indexed-reader": [ "./lib/indexed-reader.js" ], "@ipld/car/iterator": [ "./lib/iterator.js" ], "@ipld/car/indexer": [ "./lib/indexer.js" ] - }, - "baseUrl": "." + } }, - "include": [ "lib", "." ], "exclude": [ - "node_modules/", - "esm/", - "cjs/", + "node_modules", + "esm", + "cjs", "example*.js" ], "compileOnSave": false