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

Commit

Permalink
fix: fix types and remove typesVersions (#139)
Browse files Browse the repository at this point in the history
This PR updates aegir and fixes errors with the new ts config and removes typesVersions.

`typesVersions` workaround makes TS rewrite imports to `cids` to `cids/src` and because this package doesn't really needs this a workaround its just removed.

This should fix libp2p/js-libp2p#839 (comment)
  • Loading branch information
hugomrdias authored Jan 12, 2021
1 parent 4a5904b commit 83f3848
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
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
2 changes: 2 additions & 0 deletions test/cid-util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +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', () => {
/** @type {Uint8Array} */
let hash

before(async () => {
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
6 changes: 4 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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')
Expand All @@ -12,12 +13,11 @@ const CID = require('../src')
const { deepEqual } = require('@sinonjs/samsam')

describe('CID', () => {
/** @type {Uint8Array} */
let hash

before(async () => {
hash = await multihashing(uint8ArrayFromString('abc'), 'sha2-256')
})

describe('v0', () => {
it('handles B58Str multihash', () => {
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
Expand Down Expand Up @@ -346,10 +346,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')
})

0 comments on commit 83f3848

Please sign in to comment.