Skip to content

Commit

Permalink
fix!: multiformats@7, latest codecs, improve types & type exports
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Apr 26, 2021
1 parent 6a0807e commit 554b0f2
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Install dependencies
run: npm install
- name: Typecheck
uses: gozala/typescript-error-reporter-action@v1.0.2
uses: gozala/typescript-error-reporter-action@v1.0.8
4 changes: 2 additions & 2 deletions api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'

/* Generic types for interfacing with block storage */

Expand Down Expand Up @@ -49,4 +49,4 @@ export interface CarCIDIterator extends CIDIterator, RootsReader {}
export interface CarReader extends BlockReader, RootsReader {}
export interface CarIndexer extends AsyncIterable<BlockIndex>, RootsReader {}
export interface CarWriter extends BlockWriter {}
*/
*/
2 changes: 1 addition & 1 deletion lib/coding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'
import { Block, BlockIndex } from '../api'

export interface CarEncoder {
Expand Down
4 changes: 1 addition & 3 deletions lib/decoder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-ignore
import varint from 'varint'
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'
import * as Digest from 'multiformats/hashes/digest'
// @ts-ignore
import { decode as decodeDagCbor } from '@ipld/dag-cbor'

/**
Expand Down
2 changes: 0 additions & 2 deletions lib/encoder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @ts-ignore
import varint from 'varint'
// @ts-ignore
import { encode as dagCborEncode } from '@ipld/dag-cbor'

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/indexed-reader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'
import { Readable } from 'stream'
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'
import CarIndexer from './indexer.js'
import NodeCarReader from './reader.js'

Expand Down
2 changes: 1 addition & 1 deletion lib/writer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'
import { createEncoder } from './encoder.js'
import iteratorChannel from './iterator-channel.js'

Expand Down
42 changes: 25 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +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",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist/types",
"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",
Expand All @@ -37,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.1.2"
"typescript": "^4.2.4"
},
"dependencies": {
"@ipld/dag-cbor": "^2.0.3",
"multiformats": "^4.6.1",
"@ipld/dag-cbor": "^5.0.0",
"@types/varint": "^6.0.0",
"multiformats": "^7.0.0",
"varint": "^6.0.0"
},
"exports": {
Expand Down Expand Up @@ -84,7 +89,10 @@
"typesVersions": {
"*": {
"*": [
"dist/types/*"
"types/*"
],
"types/*": [
"types/*"
]
}
}
Expand Down
46 changes: 30 additions & 16 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import { bytes, CID } from 'multiformats'
import { sha256 } from 'multiformats/hashes/sha2'
import raw from 'multiformats/codecs/raw'
// @ts-ignore
import * as raw from 'multiformats/codecs/raw'
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')
Expand All @@ -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<Block & { object: any }>}
* @returns {Promise<TestBlock & { object: any }>}
*/
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 () {
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
15 changes: 6 additions & 9 deletions test/node-test-large.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

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'
import { CID } from 'multiformats/cid'
import { CarWriter, CarIndexer, CarReader, CarIndexedReader } from '@ipld/car'
import { assert } from './common.js'

Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion test/verify-store-reader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bytes } from 'multiformats'
import raw from 'multiformats/codecs/raw'
import * as raw from 'multiformats/codecs/raw'
import { toBlock, assert, makeData } from './common.js'

/**
Expand Down
13 changes: 7 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"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" ],
"@ipld/car/reader": [ "./lib/reader.js" ],
"@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/",
"./dist/",
"node_modules",
"esm",
"cjs",
"example*.js"
],
"compileOnSave": false
Expand Down

0 comments on commit 554b0f2

Please sign in to comment.