From f7bdf4d8fefb7ecd611ad7ec01a711e789c9e906 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Wed, 18 Nov 2020 15:40:49 +0000 Subject: [PATCH 01/10] feat: add ts types --- .github/workflows/main.yml | 2 + README.md | 78 ++++---------------------------------- package.json | 14 +++++-- src/base.js | 25 ++++++------ src/constants.js | 19 +++++++--- src/index.js | 22 +++++------ src/rfc4648.js | 9 +++-- src/types.ts | 62 ++++++++++++++++++++++++++++++ src/util.js | 6 +-- test/multibase.spec.js | 10 +++++ test/spec-test1.spec.js | 5 +++ test/spec-test2.spec.js | 5 +++ test/spec-test3.spec.js | 5 +++ test/spec-test4.spec.js | 4 ++ test/spec-test5.spec.js | 4 ++ tsconfig.json | 9 +++++ 16 files changed, 169 insertions(+), 110 deletions(-) create mode 100644 src/types.ts create mode 100644 tsconfig.json diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8266f90..36d92ba 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,6 +14,8 @@ jobs: - uses: actions/checkout@v2 - run: yarn - run: yarn lint + - run: yarn build + - run: yarn aegir ts -p check - run: yarn aegir dep-check -- -i aegir - uses: ipfs/aegir/actions/bundle-size@master name: size diff --git a/README.md b/README.md index 098fa05..8ebf0bb 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,27 @@ -js-multibase +js-multibase ============ [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai) [![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs) -[![Dependency Status](https://david-dm.org/multiformats/js-multibase.svg?style=flat-square)](https://david-dm.org/multiformats/js-multibase) [![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multibase.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multibase) -[![Travis CI](https://flat.badgen.net/travis/multiformats/js-multibase)](https://travis-ci.com/multiformats/js-multibase) +[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/multiformats/js-multibase/ci?label=ci&style=flat-square)](https://github.com/multiformats/js-multibase/actions?query=branch%3Amaster+workflow%3Aci+) > JavaScript implementation of the [multibase](https://github.com/multiformats/multibase) specification -## Lead Maintainer +## Lead Maintainer [Hugo Dias](https://github.com/hugomrdias) -## Table of Contents +## Table of Contents - [Install](#install) - [NPM](#npm) - - [In the Browser through ` From da6788a5de807108178d2105827f6accf28fb8c2 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 19 Nov 2020 11:35:16 +0000 Subject: [PATCH 05/10] fix: feedback --- .github/workflows/main.yml | 2 +- package.json | 7 +------ src/base.js | 10 +++++----- src/constants.js | 14 +++++++------- src/index.js | 20 ++++++++++---------- src/types.ts | 6 +++--- test/multibase.spec.js | 6 +++--- test/spec-test1.spec.js | 4 ++-- test/spec-test2.spec.js | 4 ++-- test/spec-test3.spec.js | 4 ++-- test/spec-test4.spec.js | 4 ++-- test/spec-test5.spec.js | 4 ++-- 12 files changed, 40 insertions(+), 45 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 36d92ba..b94c65e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: - run: yarn - run: yarn lint - run: yarn build - - run: yarn aegir ts -p check + - uses: gozala/typescript-error-reporter-action@v1.0.4 - run: yarn aegir dep-check -- -i aegir - uses: ipfs/aegir/actions/bundle-size@master name: size diff --git a/package.json b/package.json index 6282b8c..582f8ca 100644 --- a/package.json +++ b/package.json @@ -48,13 +48,8 @@ "eslintConfig": { "extends": "ipfs" }, - "types": "./dist/src/index.d.ts", "typesVersions": { - "*": { - "src/*": [ - "dist/src/*" - ] - } + "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } }, "contributors": [ "David Dias ", diff --git a/src/base.js b/src/base.js index b32f7ef..635d4ca 100644 --- a/src/base.js +++ b/src/base.js @@ -3,17 +3,17 @@ const { encodeText } = require('./util') /** @typedef {import('./types').CodecFactory} CodecFactory */ -/** @typedef {import("./types").BaseNames} BaseNames */ -/** @typedef {import("./types").BaseCodes} BaseCodes */ +/** @typedef {import("./types").BaseName} BaseName */ +/** @typedef {import("./types").BaseCode} BaseCode */ /** - * Class + * Class to encode/decode in the supported Bases * */ class Base { /** - * @param {BaseNames} name - * @param {BaseCodes} code + * @param {BaseName} name + * @param {BaseCode} code * @param {CodecFactory} factory * @param {string} alphabet */ diff --git a/src/constants.js b/src/constants.js index 0246588..979a58d 100644 --- a/src/constants.js +++ b/src/constants.js @@ -7,8 +7,8 @@ const { decodeText, encodeText } = require('./util') /** @typedef {import('./types').CodecFactory} CodecFactory */ /** @typedef {import('./types').Codec} Codec */ -/** @typedef {import('./types').BaseNames} BaseNames */ -/** @typedef {import('./types').BaseCodes} BaseCodes */ +/** @typedef {import('./types').BaseName} BaseName */ +/** @typedef {import('./types').BaseCode} BaseCode */ /** @type {CodecFactory} */ const identity = () => { @@ -22,7 +22,7 @@ const identity = () => { * * name, code, implementation, alphabet * - * @type {Array<[BaseNames, BaseCodes, CodecFactory, string]>} + * @type {Array<[BaseName, BaseCode, CodecFactory, string]>} */ const constants = [ ['identity', '\x00', identity, ''], @@ -50,17 +50,17 @@ const constants = [ ['base64urlpad', 'U', rfc4648(6), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_='] ] -/** @type {Record} */ +/** @type {Record} */ const names = constants.reduce((prev, tupple) => { prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3]) return prev -}, /** @type {Record} */({})) +}, /** @type {Record} */({})) -/** @type {Record} */ +/** @type {Record} */ const codes = constants.reduce((prev, tupple) => { prev[tupple[1]] = names[tupple[0]] return prev -}, /** @type {Record} */({})) +}, /** @type {Record} */({})) module.exports = { names, diff --git a/src/index.js b/src/index.js index 8008ee5..310e597 100644 --- a/src/index.js +++ b/src/index.js @@ -8,13 +8,13 @@ const constants = require('./constants') const { encodeText, decodeText, concat } = require('./util') /** @typedef {import('./base')} Base */ -/** @typedef {import("./types").BaseNamesCodes} BaseNamesCodes */ -/** @typedef {import("./types").BaseCodes} BaseCodes */ +/** @typedef {import("./types").BaseNameOrCode} BaseNameOrCode */ +/** @typedef {import("./types").BaseCode} BaseCode */ /** * Create a new Uint8Array with the multibase varint+code. * - * @param {BaseNamesCodes} nameOrCode - The multibase name or code number. + * @param {BaseNameOrCode} nameOrCode - The multibase name or code number. * @param {Uint8Array} buf - The data to be prefixed with multibase. * @returns {Uint8Array} * @throws {Error} Will throw if the encoding is not supported @@ -32,7 +32,7 @@ function multibase (nameOrCode, buf) { /** * Encode data with the specified base and add the multibase prefix. * - * @param {BaseNamesCodes} nameOrCode - The multibase name or code number. + * @param {BaseNameOrCode} nameOrCode - The multibase name or code number. * @param {Uint8Array} buf - The data to be encoded. * @returns {Uint8Array} * @throws {Error} Will throw if the encoding is not supported @@ -64,7 +64,7 @@ function decode (data) { if (['f', 'F', 'v', 'V', 't', 'T', 'b', 'B', 'c', 'C', 'h', 'k', 'K'].includes(prefix)) { data = data.toLowerCase() } - const enc = encoding(/** @type {BaseCodes} */(data[0])) + const enc = encoding(/** @type {BaseCode} */(data[0])) return enc.decode(data.substring(1)) } @@ -72,7 +72,7 @@ function decode (data) { * Is the given data multibase encoded? * * @param {Uint8Array|string} data - * @returns {boolean | string} + * @returns {false | string} */ function isEncoded (data) { if (data instanceof Uint8Array) { @@ -85,7 +85,7 @@ function isEncoded (data) { } try { - const enc = encoding(/** @type {BaseCodes} */(data[0])) + const enc = encoding(/** @type {BaseCode} */(data[0])) return enc.name } catch (err) { return false @@ -95,7 +95,7 @@ function isEncoded (data) { /** * Validate encoded data * - * @param {BaseNamesCodes} name + * @param {BaseNameOrCode} name * @param {Uint8Array} buf * @returns {void} * @throws {Error} Will throw if the encoding is not supported @@ -108,7 +108,7 @@ function validEncode (name, buf) { /** * Get the encoding by name or code * - * @param {BaseNamesCodes} nameOrCode + * @param {BaseNameOrCode} nameOrCode * @returns {Base} * @throws {Error} Will throw if the encoding is not supported */ @@ -134,7 +134,7 @@ function encodingFromData (data) { data = decodeText(data) } - return encoding(/** @type {BaseCodes} */(data[0])) + return encoding(/** @type {BaseCode} */(data[0])) } exports = module.exports = multibase diff --git a/src/types.ts b/src/types.ts index 702482c..84438f8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,7 @@ /** * - Codes of the supported encodings */ -export type BaseCodes = +export type BaseCode = | '\x00' | '0' | '7' @@ -29,7 +29,7 @@ export type BaseCodes = /** * - Names of the supported encodings */ -export type BaseNames = +export type BaseName = | 'identity' | 'base2' | 'base8' @@ -54,7 +54,7 @@ export type BaseNames = | 'base64url' | 'base64urlpad'; -export type BaseNamesCodes = BaseCodes | BaseNames; +export type BaseNameOrCode = BaseCode | BaseName; export type Codec = { encode: (buffer: Uint8Array) => string; decode: (hash: string) => Uint8Array; diff --git a/test/multibase.spec.js b/test/multibase.spec.js index 066bcf3..9d80cd9 100644 --- a/test/multibase.spec.js +++ b/test/multibase.spec.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ 'use strict' -// @ts-ignore const { expect } = require('aegir/utils/chai') const { encodeText, decodeText } = require('../src/util') const multibase = require('../src') @@ -10,9 +9,10 @@ const constants = require('../src/constants.js') const unsupportedBases = [] /** - * @typedef {import('../src/types').BaseNames} BaseNames + * @typedef {import('../src/types').BaseName} BaseName */ -/** @type {Array<[BaseNames, string, string]>} */ + +/** @type {Array<[BaseName, string, string]>} */ const supportedBases = [ ['base16', decodeText(Uint8Array.from([0x01])), 'f01'], diff --git a/test/spec-test1.spec.js b/test/spec-test1.spec.js index 9b24a8b..156849f 100644 --- a/test/spec-test1.spec.js +++ b/test/spec-test1.spec.js @@ -8,9 +8,9 @@ const constants = require('../src/constants.js') const input = 'Decentralize everything!!' /** - * @typedef {import('../src/types').BaseNames} BaseNames + * @typedef {import('../src/types').BaseName} BaseName */ -/** @type {Array<[BaseNames, string]>} */ +/** @type {Array<[BaseName, string]>} */ const encoded = [ ['identity', '\x00Decentralize everything!!'], ['base2', '001000100011001010110001101100101011011100111010001110010011000010110110001101001011110100110010100100000011001010111011001100101011100100111100101110100011010000110100101101110011001110010000100100001'], diff --git a/test/spec-test2.spec.js b/test/spec-test2.spec.js index 220ed45..88b60dd 100644 --- a/test/spec-test2.spec.js +++ b/test/spec-test2.spec.js @@ -8,9 +8,9 @@ const constants = require('../src/constants.js') const input = 'yes mani !' /** - * @typedef {import('../src/types').BaseNames} BaseNames + * @typedef {import('../src/types').BaseName} BaseName */ -/** @type {Array<[BaseNames, string]>} */ +/** @type {Array<[BaseName, string]>} */ const encoded = [ ['identity', '\x00yes mani !'], ['base2', '001111001011001010111001100100000011011010110000101101110011010010010000000100001'], diff --git a/test/spec-test3.spec.js b/test/spec-test3.spec.js index ad4bd6c..35f6513 100644 --- a/test/spec-test3.spec.js +++ b/test/spec-test3.spec.js @@ -8,9 +8,9 @@ const constants = require('../src/constants.js') const input = 'hello world' /** - * @typedef {import('../src/types').BaseNames} BaseNames + * @typedef {import('../src/types').BaseName} BaseName */ -/** @type {Array<[BaseNames, string]>} */ +/** @type {Array<[BaseName, string]>} */ const encoded = [ ['identity', '\x00hello world'], ['base2', '00110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'], diff --git a/test/spec-test4.spec.js b/test/spec-test4.spec.js index 7c96068..3dbfa4b 100644 --- a/test/spec-test4.spec.js +++ b/test/spec-test4.spec.js @@ -7,9 +7,9 @@ const multibase = require('../src') const constants = require('../src/constants.js') const input = '\x00yes mani !' /** - * @typedef {import('../src/types').BaseNames} BaseNames + * @typedef {import('../src/types').BaseName} BaseName */ -/** @type {Array<[BaseNames, string]>} */ +/** @type {Array<[BaseName, string]>} */ const encoded = [ ['identity', '\x00\x00yes mani !'], ['base2', '00000000001111001011001010111001100100000011011010110000101101110011010010010000000100001'], diff --git a/test/spec-test5.spec.js b/test/spec-test5.spec.js index 166e425..97f654f 100644 --- a/test/spec-test5.spec.js +++ b/test/spec-test5.spec.js @@ -7,9 +7,9 @@ const multibase = require('../src') const constants = require('../src/constants.js') const input = '\x00\x00yes mani !' /** - * @typedef {import('../src/types').BaseNames} BaseNames + * @typedef {import('../src/types').BaseName} BaseName */ -/** @type {Array<[BaseNames, string]>} */ +/** @type {Array<[BaseName, string]>} */ const encoded = [ ['identity', '\x00\x00\x00yes mani !'], ['base2', '0000000000000000001111001011001010111001100100000011011010110000101101110011010010010000000100001'], From 55a9896b394b7fe0e42e4b3975b54967f745de5a Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 19 Nov 2020 11:40:51 +0000 Subject: [PATCH 06/10] fix: update aegir --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 582f8ca..257c501 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "web-encoding": "^1.0.4" }, "devDependencies": { - "aegir": "github:ipfs/aegir#feat/ts-cmd", + "aegir": "github:ipfs/aegir#fix/ts-docs-update", "benchmark": "^2.1.4" }, "engines": { From 67eb62cc9c3c89b41260747500edb1363098f740 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 19 Nov 2020 15:00:53 +0000 Subject: [PATCH 07/10] fix: add types --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 257c501..e5fa0e7 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "eslintConfig": { "extends": "ipfs" }, + "types": "dist/src/index.d.ts", "typesVersions": { "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } }, From 9d407d2265be7048edfa598c93dc1829eb4afe45 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 19 Nov 2020 15:05:56 +0000 Subject: [PATCH 08/10] fix: type versions --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e5fa0e7..35ebd45 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ }, "types": "dist/src/index.d.ts", "typesVersions": { - "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } + "*": { "src/*": ["dist/src/*"] } }, "contributors": [ "David Dias ", From 5c7b497af3ef080cf19d712f136a478fa5647363 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 19 Nov 2020 16:23:20 +0000 Subject: [PATCH 09/10] fix: typeversions --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 35ebd45..e5fa0e7 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ }, "types": "dist/src/index.d.ts", "typesVersions": { - "*": { "src/*": ["dist/src/*"] } + "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } }, "contributors": [ "David Dias ", From 280b133dc893ed8b41c983d8d91e371630d7aca4 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 20 Nov 2020 11:59:15 +0000 Subject: [PATCH 10/10] fix: aegir --- package.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e5fa0e7..bb8e0ed 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "web-encoding": "^1.0.4" }, "devDependencies": { - "aegir": "github:ipfs/aegir#fix/ts-docs-update", + "aegir": "^29.0.1", "benchmark": "^2.1.4" }, "engines": { @@ -50,7 +50,12 @@ }, "types": "dist/src/index.d.ts", "typesVersions": { - "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } + "*": { + "src/*": [ + "dist/src/*", + "dist/src/*/index" + ] + } }, "contributors": [ "David Dias ",