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

Commit

Permalink
fix: update dep and fix types (#111)
Browse files Browse the repository at this point in the history
- improve types 
- update deps
  • Loading branch information
hugomrdias committed Jan 13, 2021
1 parent 5a5ee8e commit 666c4ad
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 18 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dist"
],
"scripts": {
"prepare": "aegir build",
"prepare": "aegir build --no-bundle",
"lint": "aegir lint",
"test:browser": "aegir test --target browser",
"test:node": "aegir test --target node",
Expand All @@ -41,12 +41,12 @@
"repository": "github:multiformats/js-multihash",
"dependencies": {
"multibase": "^3.1.0",
"uint8arrays": "^1.0.0",
"uint8arrays": "^2.0.5",
"varint": "^6.0.0"
},
"devDependencies": {
"aegir": "^29.0.1",
"ipfs-utils": "^5.0.0"
"aegir": "^30.3.0",
"ipfs-utils": "^5.0.1"
},
"eslintConfig": {
"extends": "ipfs"
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

/**
* @type { Object<HashName,HashCode> }
* @type { Record<HashName,HashCode> }
*/
const names = Object.freeze({
'identity': 0x00,
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const codes = /** @type {import('./types').CodeNameMap} */({})

// eslint-disable-next-line guard-for-in
for (const key in names) {
codes[names[key]] = key
const name = /** @type {HashName} */(key)
codes[names[name]] = name
}

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ function decode (bytes) {
throw new Error('multihash too short. must be > 2 bytes.')
}

const code = varint.decode(bytes)
const code = /** @type {HashCode} */(varint.decode(bytes))
if (!isValidCode(code)) {
throw new Error(`multihash unknown function code: 0x${code.toString(16)}`)
}
Expand Down Expand Up @@ -166,6 +167,7 @@ function coerceCode (name) {
throw new Error(`Hash function code should be a number. Got: ${code}`)
}

// @ts-ignore
if (codes[code] === undefined && !isAppCode(code)) {
throw new Error(`Unrecognized function code: ${code}`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HashCode, HashName } from './constants.js'
import type { HashCode, HashName } from './constants.js'

export type CodeNameMap = Record<HashCode, HashName>
export type NameCodeMap = Record<HashName, HashCode>
33 changes: 26 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayEquals = require('uint8arrays/equals')

/**
* @typedef {import('../src/constants.js').HashName} HashName
* @typedef {import('../src/constants.js').HashCode} HashCode
*/

/**
* @param {string | number} code
* @param {number} size
* @param {string} hex
*/
function sample (code, size, hex) {
/**
* @param {number | string} i
*/
const toHex = (i) => {
if (typeof i === 'string') {
return i
Expand All @@ -23,6 +36,10 @@ function sample (code, size, hex) {
return uint8ArrayFromString(`${toHex(code)}${toHex(size)}${hex}`, 'base16')
}

/**
* @param {string} description
* @param {(test: { encodeText: (text: string) => Uint8Array, encodeHex: (text: string) => Uint8Array }) => void} test
*/
const they = (description, test) => {
it(description, () => test({
encodeText: (text) => uint8ArrayFromString(text),
Expand Down Expand Up @@ -195,7 +212,7 @@ describe('multihash', () => {
it('invalid', () => {
invalidCases.forEach((test) => {
expect(
() => mh.validate(sample(test.encoding.varint || test.code, test.size, test.hex))
() => mh.validate(sample(test.code, test.size, test.hex))
).to.throw()
})

Expand Down Expand Up @@ -257,7 +274,7 @@ describe('multihash', () => {
false
)

for (var m = 0x10; m <= 0xff; m++) {
for (let m = 0x10; m <= 0xff; m++) {
expect(
mh.isAppCode(m)
).to.equal(
Expand All @@ -269,17 +286,18 @@ describe('multihash', () => {

describe('coerceCode', () => {
it('valid', () => {
/** @type {Partial<Record<import('../src/constants').HashName, import('../src/constants').HashCode>> } */
const names = {
sha1: 0x11,
'sha2-256': 0x12,
'sha2-512': 0x13,
'sha3-512': 0x14
}

Object.keys(names).forEach((name) => {
/** @type {keyof typeof names} */
let name
// eslint-disable-next-line guard-for-in
for (name in names) {
expect(
mh.coerceCode(/** @type {import('../src/constants.js').HashName} */(name))
mh.coerceCode(name)
).to.be.eql(
names[name]
)
Expand All @@ -289,7 +307,7 @@ describe('multihash', () => {
).to.be.eql(
names[name]
)
})
}
})

they('invalid', ({ encodeText }) => {
Expand All @@ -309,6 +327,7 @@ describe('multihash', () => {
})

expect(
// @ts-expect-error
() => mh.coerceCode(encodeText('hello'))
).to.throw(
/should be a number/
Expand Down
9 changes: 7 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"baseUrl": "./",
"paths": {
"*": ["./types/*"]
}
},
"include": [
"test", // remove this line if you don't want to type-check tests
"src"
"src",
"types"
]
}
39 changes: 39 additions & 0 deletions types/varint/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const encode: {
/**
* Encodes `num` into `buffer` starting at `offset`. returns `buffer`, with the encoded varint written into it.
* `varint.encode.bytes` will now be set to the number of bytes modified.
*/
(num: number, buffer: Uint8Array, offset?: number): Uint8Array

/**
* Encodes `num` into `array` starting at `offset`. returns `array`, with the encoded varint written into it.
* If `array` is not provided, it will default to a new array.
* `varint.encode.bytes` will now be set to the number of bytes modified.
*/
(num: number, array?: number[], offset?: number): number[]

/**
* Similar to `decode.bytes` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array).
* You can access this via `varint.encode.bytes` which holds the number of bytes written in the last encode.
*/
bytes: number
}

export const decode: {
/**
* Decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer.
* Throws a `RangeError` when `data` does not represent a valid encoding.
*/
(buf: Uint8Array | Buffer | number[], offset?: number): number

/**
* If you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`.
* This is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
*/
bytes: number
}

/**
* returns the number of bytes this number will be encoded as, up to a maximum of 8.
*/
export function encodingLength (num: number): number
2 changes: 1 addition & 1 deletion update-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const run = async () => {
*/
/**
* @type { Object<HashName,HashCode> }
* @type { Record<HashName,HashCode> }
*/
const names = Object.freeze({
${processed}
Expand Down

0 comments on commit 666c4ad

Please sign in to comment.