Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type mapping #72

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ package-lock.json
node_modules
.DS_Store
yarn.lock
types
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Library provides implementations for most basics and many others can be found in
## Interfaces

```js
import CID from 'multiformats/cid'
import json from 'multiformats/codecs/json'
import { CID } from 'multiformats/cid'
import * as json from 'multiformats/codecs/json'
import { sha256 } from 'multiformats/hashes/sha2'

const bytes = json.encode({ hello: 'world' })
Expand Down
34 changes: 19 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
"version": "0.0.0-dev",
"description": "Interface for multihash, multicodec, multibase and CID",
"main": "./src/index.js",
"types": "./types/index.d.ts",
"type": "module",
"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": "npm_config_yes=true ipjs build --tests --main",
"build:types": "cp tsconfig.json dist && cp -a src vendor dist/ && cd dist && tsc --build",
"build:vendor": "npm run build:vendor:varint && npm run build:vendor:base-x",
"build:vendor:varint": "npx brrp -x varint > vendor/varint.js",
"build:vendor:base-x": "npx brrp -x @multiformats/base-x > vendor/base-x.js",
"publish": "npm_config_yes=true ipjs publish",
"lint": "standard",
"check": "tsc --noEmit --noErrorTruncation",
"check": "tsc --build --noErrorTruncation",
"test:cjs": "npm run build:js && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
"test:node": "hundreds mocha test/test-*.js",
"test:cjs:browser": "polendina --page --worker --serviceworker --cleanup dist/cjs/browser-test/test-*.js",
"test:ts": "npm run build:types && npm run test --prefix test/ts-use",
"test": "npm run lint && npm run test:node && npm run test:cjs && npm run test:ts",
"test:node-v12": "mocha test/test-*.js && npm run test:cjs",
"coverage": "c8 --reporter=html mocha test/test-*.js && npm_config_yes=true npx st -d coverage -p 8080"
"coverage": "c8 --reporter=html mocha test/test-*.js && npm_config_yes=true npx st -d coverage -p 8080",
"prepare": "tsc --build"
},
"c8": {
"exclude": [
Expand Down Expand Up @@ -84,16 +86,16 @@
}
},
"devDependencies": {
"@types/node": "14.14.3",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"c8": "^7.3.5",
"@types/node": "^14.14.37",
"@typescript-eslint/eslint-plugin": "^4.20.0",
"@typescript-eslint/parser": "^4.20.0",
"c8": "^7.6.0",
"hundreds": "0.0.9",
"ipjs": "^3.4.4",
"mocha": "^8.2.0",
"ipjs": "^5.0.0",
"mocha": "^8.3.2",
"polendina": "^1.1.0",
"standard": "^15.0.0",
"typescript": "^4.0.3"
"standard": "^16.0.3",
"typescript": "^4.2.3"
},
"standard": {
"ignore": [
Expand All @@ -102,9 +104,8 @@
]
},
"dependencies": {
"buffer": "^5.6.1",
"cids": "^1.0.2",
"lodash.transform": "^4.6.0"
"buffer": "^6.0.3",
"cids": "^1.1.6"
},
"directories": {
"test": "test"
Expand All @@ -121,6 +122,9 @@
"*": {
"*": [
"types/*"
],
"types/*": [
"types/*"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bases/base32.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { withAlphabet } from './base.js'
* @param {input} alphabet
*/
function decode (input, alphabet) {
input = input.replace(new RegExp('=', 'g'), '')
input = input.replace(/=/g, '')
const length = input.length

let bits = 0
Expand Down
4 changes: 2 additions & 2 deletions src/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as base32 from './bases/base32.js'
import * as base58 from './bases/base58.js'
import * as sha2 from './hashes/sha2.js'

import raw from './codecs/raw.js'
import json from './codecs/json.js'
import * as raw from './codecs/raw.js'
import * as json from './codecs/json.js'

import { CID, hasher, digest, varint, bytes } from './index.js'

Expand Down
3 changes: 2 additions & 1 deletion src/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const equals = (aa, bb) => {
}

/**
* @param {ArrayBufferView|ArrayBuffer} o
* @param {ArrayBufferView|ArrayBuffer|Uint8Array} o
* @returns {Uint8Array}
*/
const coerce = o => {
if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') return o
Expand Down
2 changes: 1 addition & 1 deletion src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { coerce } from './bytes.js'
* @typedef {import('./bases/interface').MultibaseDecoder<Prefix>} MultibaseDecoder
*/

export default class CID {
export class CID {
/**
* @param {0|1} version
* @param {number} code
Expand Down
51 changes: 7 additions & 44 deletions src/codecs/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
* @param {Code} options.code
* @param {(data:T) => Uint8Array} options.encode
* @param {(bytes:Uint8Array) => T} options.decode
* @returns {import('./interface'). BlockCodec<Code, T>}
*/
export const codec = ({ name, code, decode, encode }) =>
new Codec(name, code, encode, decode)
export const codec = ({ name, code, decode, encode }) => {
const decoder = new Decoder(name, code, decode)
const encoder = new Encoder(name, code, encode)

return { name, code, decode, encode, decoder, encoder }
}

/**
* @template {number} Code
Expand Down Expand Up @@ -64,45 +69,3 @@ export class Decoder {
this.decode = decode
}
}

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec
*/

/**
* @class
* @template {string} Name
* @template {number} Code
* @template T
* @implements {BlockCodec<Code, T>}
*/
export class Codec {
/**
* @param {Name} name
* @param {Code} code
* @param {(data:T) => Uint8Array} encode
* @param {(bytes:Uint8Array) => T} decode
*/
constructor (name, code, encode, decode) {
this.name = name
this.code = code
this.encode = encode
this.decode = decode
}

get decoder () {
const { name, code, decode } = this
const decoder = new Decoder(name, code, decode)
Object.defineProperty(this, 'decoder', { value: decoder })
return decoder
}

get encoder () {
const { name, code, encode } = this
const encoder = new Encoder(name, code, encode)
Object.defineProperty(this, 'encoder', { value: encoder })
return encoder
}
}
5 changes: 4 additions & 1 deletion src/codecs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export interface BlockDecoder<Code extends number, T> {
* separate those capabilties as sender requires encoder and receiver
* requires decoder.
*/
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> { }
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> {
encoder: BlockEncoder<Code, T>,
decoder: BlockDecoder<Code, T>
}


// This just a hack to retain type information abouth the data that
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { codec } from './codec.js'

export default codec({
export const { name, code, decode, encode, decoder, encoder } = codec({
name: 'json',
code: 0x0200,
encode: json => new TextEncoder().encode(JSON.stringify(json)),
Expand Down
12 changes: 3 additions & 9 deletions src/codecs/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
import { coerce } from '../bytes.js'
import { codec } from './codec.js'

/**
* @param {Uint8Array} bytes
* @returns {Uint8Array}
*/
const raw = (bytes) => coerce(bytes)

export default codec({
export const { name, code, decode, encode, decoder, encoder } = codec({
name: 'raw',
code: 85,
decode: raw,
encode: raw
decode: coerce,
encode: coerce
})
2 changes: 1 addition & 1 deletion src/hashes/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { from } from './hasher.js'
import { coerce } from '../bytes.js'

export default from({
export const identity = from({
name: 'identity',
code: 0x0,
encode: (input) => coerce(input)
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CID from './cid.js'
import { CID } from './cid.js'
import * as varint from './varint.js'
import * as bytes from './bytes.js'
import * as hasher from './hashes/hasher.js'
Expand Down
5 changes: 2 additions & 3 deletions src/legacy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OldCID from 'cids'
import * as bytes from './bytes.js'
import { Buffer } from 'buffer'
import CID from './cid.js'
import { CID } from './cid.js'

/**
* @template {number} Code
Expand All @@ -11,7 +11,7 @@ import CID from './cid.js'
* @param {Object<string, MultihashHasher>} options.hashes
*/

const legacy = (codec, { hashes }) => {
export const legacy = (codec, { hashes }) => {
/**
* @param {*} obj
*/
Expand Down Expand Up @@ -139,7 +139,6 @@ const legacy = (codec, { hashes }) => {
return { defaultHashAlg, codec: codec.code, util, resolver }
}

export default legacy
/**
* @typedef {import('./hashes/interface').MultihashHasher} MultihashHasher
*/
Expand Down
2 changes: 1 addition & 1 deletion test/test-block.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* globals describe, it */
import codec from 'multiformats/codecs/json'
import * as codec from 'multiformats/codecs/json'
import { sha256 as hasher } from 'multiformats/hashes/sha2'
import * as main from 'multiformats/block'
import { CID, bytes } from 'multiformats'
Expand Down
8 changes: 4 additions & 4 deletions test/test-legacy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* globals before, describe, it */
import { Buffer } from 'buffer'
import assert from 'assert'
import legacy from 'multiformats/legacy'
import rawCodec from 'multiformats/codecs/raw'
import jsonCodec from 'multiformats/codecs/json'
import { legacy } from 'multiformats/legacy'
import * as rawCodec from 'multiformats/codecs/raw'
import * as jsonCodec from 'multiformats/codecs/json'
import { sha256, sha512 } from 'multiformats/hashes/sha2'
import { codec } from 'multiformats/codecs/codec'
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'

const same = assert.deepStrictEqual
const test = it
Expand Down
4 changes: 2 additions & 2 deletions test/test-multicodec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals describe, it */
import * as bytes from '../src/bytes.js'
import assert from 'assert'
import raw from 'multiformats/codecs/raw'
import json from 'multiformats/codecs/json'
import * as raw from 'multiformats/codecs/raw'
import * as json from 'multiformats/codecs/json'
import { codec } from 'multiformats/codecs/codec'
const same = assert.deepStrictEqual
const test = it
Expand Down
2 changes: 1 addition & 1 deletion test/test-multihash.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import valid from './fixtures/valid-multihash.js'
import invalid from './fixtures/invalid-multihash.js'
import crypto from 'crypto'
import { sha256, sha512, __browser } from 'multiformats/hashes/sha2'
import identity from 'multiformats/hashes/identity'
import { identity } from 'multiformats/hashes/identity'
import { decode as decodeDigest, create as createDigest } from 'multiformats/hashes/digest'
const test = it
const encode = name => data => coerce(crypto.createHash(name).update(data).digest())
Expand Down
2 changes: 1 addition & 1 deletion test/ts-use/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Block from 'multiformats/block'
import { sha256 } from 'multiformats/hashes/sha2'
import json from 'multiformats/codecs/json'
import * as json from 'multiformats/codecs/json'

const main = async () => {
const block = await Block.encode({
Expand Down
4 changes: 3 additions & 1 deletion test/ts-use/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"compilerOptions": {
"strict": true,
"moduleResolution": "node",
"noImplicitAny": true,
"skipLibCheck": true
"skipLibCheck": true,
"incremental": true
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"emitDeclarationOnly": true,
"outDir": "types",
"skipLibCheck": true,
"stripInternal": true,
"resolveJsonModule": true,
Expand Down