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

fix: fix types and remove typesVersions #139

Merged
merged 4 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
"leadMaintainer": "Volker Mische <volker.mische@gmail.com>",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": {
"src/*": [
"dist/src/*",
"dist/src/*/index"
]
}
},
"scripts": {
"lint": "aegir lint",
"test": "aegir test",
Expand Down Expand Up @@ -50,12 +42,12 @@
"dependencies": {
"multibase": "^3.0.1",
"multicodec": "^2.1.0",
"multihashes": "^3.0.1",
"uint8arrays": "^1.1.0"
"multihashes": "^3.1.0",
"uint8arrays": "^2.0.5"
},
"devDependencies": {
"@sinonjs/samsam": "^5.3.0",
"aegir": "^29.0.1",
"aegir": "^30.3.0",
"multihashing-async": "^2.0.1"
},
"engines": {
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const uint8ArrayConcat = require('uint8arrays/concat')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayEquals = require('uint8arrays/equals')

const codecInts = Object.keys(codecs).reduce((p, name) => {
const codecInts = /** @type {CodecName[]} */(Object.keys(codecs)).reduce((p, name) => {
p[codecs[name]] = name
return p
}, {})
}, /** @type {Record<CodecNumber, CodecName>} */({}))

const symbol = Symbol.for('@ipld/js-cid/CID')

Expand Down Expand Up @@ -103,6 +103,7 @@ class CID {
this.codec = cid.codec
this.multihash = cid.multihash
// Default guard for when a CID < 0.7 is passed with no multibaseName
// @ts-ignore
this.multibaseName = cid.multibaseName || (cid.version === 0 ? 'base58btc' : 'base32')
return
}
Expand Down Expand Up @@ -154,6 +155,7 @@ class CID {
this.version = version

if (typeof codec === 'number') {
// @ts-ignore
codec = codecInts[codec]
}

Expand Down
9 changes: 3 additions & 6 deletions test/cid-util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
'use strict'

const { expect } = require('aegir/utils/chai')
// @ts-ignore
const multihashing = require('multihashing-async')
const CID = require('../src')
const CIDUtil = require('../src/cid-util')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('CIDUtil', () => {
let hash

before(async () => {
hash = await multihashing(uint8ArrayFromString('abc'), 'sha2-256')
})
describe('CIDUtil', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe async for describe is supported. It'll work if this can be done within a tick (or so? there's some amount of time that mocha uses to collect all of the tests after running describe()s), but if there's more than a tiny delay then the tests will get entirely dropped from the suite. Doing the setup in an async before() is the safest bet.

running with 8.2.1, this runs a single test:

describe('foo', async () => {
  await new Promise((resolve) => setImmediate(resolve))
  it('bar', () => {})
})

but this doesn't run any:

describe('foo', async () => {
  await new Promise((resolve) => setTimeout(resolve, 0))
  it('bar', () => {})
})

did you also mean to add the @type to this one like in index.spec.js?

const hash = await multihashing(uint8ArrayFromString('abc'), 'sha2-256')

describe('checkCIDComponents()', () => {
describe('returns undefined on valid CID', () => {
Expand Down
1 change: 1 addition & 0 deletions test/helpers/gen-cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const multibase = require('multibase')
const codecs = require('../../src').codecs
// @ts-ignore
const multihashing = require('multihashing-async')
const utf8ArrayFromString = require('uint8arrays/from-string')

Expand Down
12 changes: 6 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

const { expect } = require('aegir/utils/chai')
const multihash = require('multihashes')
// @ts-ignore
const multihashing = require('multihashing-async')
const multicodec = require('multicodec')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
const CID = require('../src')
const { deepEqual } = require('@sinonjs/samsam')

describe('CID', () => {
let hash

before(async () => {
hash = await multihashing(uint8ArrayFromString('abc'), 'sha2-256')
})
describe('CID', async () => {
/** @type {Uint8Array} */
const hash = await multihashing(uint8ArrayFromString('abc'), 'sha2-256')

describe('v0', () => {
it('handles B58Str multihash', () => {
Expand Down Expand Up @@ -346,10 +344,12 @@ describe('CID', () => {
}))

invalid.forEach((i) => it(`new CID(0, 'dag-pb', ${i instanceof Uint8Array ? 'Uint8Array' : 'String'}<${i.toString()}>)`, () => {
// @ts-expect-error - string should throw
expect(() => new CID(0, 'dag-pb', i)).to.throw()
}))

invalid.forEach((i) => it(`new CID(1, 'dag-pb', ${i instanceof Uint8Array ? 'Uint8Array' : 'String'}<${i.toString()}>)`, () => {
// @ts-expect-error - string should throw
expect(() => new CID(1, 'dag-pb', i)).to.throw()
}))

Expand Down
13 changes: 12 additions & 1 deletion test/profiling/cidperf-x.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
'use strict'

// @ts-ignore
const multihashing = require('multihashing-async')
// [1] Original/existing implementation.
// const CID = require('cids')
Expand All @@ -9,6 +10,9 @@ const CID = require('../../src')
const uint8ArrayFromString = require('uint8arrays/from-string')

// Used to delay the testing for a few seconds.
/**
* @param {number} ms
*/
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
Expand All @@ -17,6 +21,9 @@ function sleep (ms) {
// The purpose of this class is
// to simply test the CID ctor (and, primarily, CID.isCID() method).
class CIDPerfX {
/**
* @param {any} mh
*/
constructor (mh) {
this.version = 1
this.codec = 'dag-pb'
Expand All @@ -25,6 +32,10 @@ class CIDPerfX {

// i: Running-counter.
// print: If true, it'll print/dump the CID data.
/**
* @param {string | number} i
* @param {boolean | undefined} [print]
*/
run (i, print) {
// @ts-ignore
const cid = new CID(this.version, this.codec, this.mh)
Expand Down Expand Up @@ -53,7 +64,7 @@ sleep(1000).then(async () => {
const cidPerf = new CIDPerfX(mh);

[...Array(reps).keys()].map(i => {
cidPerf.run(i)
return cidPerf.run(i)
})
console.timeEnd('run')
})