From 678341b8178dbf0596e7bb5bf87eeea425fff133 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 13:54:15 +0000 Subject: [PATCH 01/19] feat: use github ci --- .github/ISSUE_TEMPLATE/bug_report.md | 38 +++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 +++++++ .github/workflows/main.yml | 67 +++++++++++++++++++++++ .travis.yml | 44 --------------- 4 files changed, 125 insertions(+), 44 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..dd84ea78 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..bbcbbe7d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..b94c65e7 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,67 @@ +name: ci +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: yarn lint + - run: yarn build + - 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 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + test-node: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + node: [12, 14] + fail-fast: true + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node }} + - run: yarn + - run: npx nyc --reporter=lcov npm run test:node -- --bail + - uses: codecov/codecov-action@v1 + test-chrome: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: yarn aegir test -t browser -t webworker + test-firefox: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: yarn aegir test -t browser -t webworker -- --browsers FirefoxHeadless + test-electron-main: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: npx xvfb-maybe yarn aegir test -t electron-main --bail + test-electron-renderer: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: npx xvfb-maybe yarn aegir test -t electron-renderer --bail \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 81758d88..00000000 --- a/.travis.yml +++ /dev/null @@ -1,44 +0,0 @@ -language: node_js -cache: npm -stages: - - check - - test - - cov - -node_js: - - 'lts/*' - - 'stable' - -os: - - linux - - osx - -script: npx nyc -s npm run test:node -- --bail -after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov - -jobs: - include: - - os: windows - cache: false - - - stage: check - script: - - npx aegir dep-check - - npm run lint - - npm run test:types - - - stage: test - name: chrome - addons: - chrome: stable - script: npx aegir test -t browser -t webworker - - - stage: test - name: firefox - addons: - firefox: latest - script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless - -notifications: - email: false - From 8bf9caeb82916dbd33fc1e1dc6a671cddd53dc54 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 13:54:33 +0000 Subject: [PATCH 02/19] fix: remove old docs stuff --- documentation.yml | 3 -- intro.md | 84 ----------------------------------------------- 2 files changed, 87 deletions(-) delete mode 100644 documentation.yml delete mode 100644 intro.md diff --git a/documentation.yml b/documentation.yml deleted file mode 100644 index 63ff7196..00000000 --- a/documentation.yml +++ /dev/null @@ -1,3 +0,0 @@ -toc: - - name: Introduction - file: intro.md diff --git a/intro.md b/intro.md deleted file mode 100644 index eb6503ec..00000000 --- a/intro.md +++ /dev/null @@ -1,84 +0,0 @@ -JavaScript implementation of [Multiaddr](https://github.com/multiformats/multiaddr). - -## What is multiaddr? - -Multiaddr is a standard way to represent addresses that: -- Support any standard network protocols. -- Self-describe (include protocols). -- Have a binary packed format. -- Have a nice string representation. -- Encapsulate well. - -You can read more about what Multiaddr is in the language-independent Github repository: -https://github.com/multiformats/multiaddr - -Multiaddr is a part of a group of values called [Multiformats](https://github.com/multiformats/multiformats) - -## Example - -```js -var Multiaddr = require('multiaddr') - -var home = new Multiaddr('/ip4/127.0.0.1/tcp/80') -// - -home.bytes -// - -home.toString() -// '/ip4/127.0.0.1/tcp/80' - -home.protos() -// [ { code: 4, size: 32, name: 'ip4' }, -// { code: 6, size: 16, name: 'tcp' } ] - -home.nodeAddress() -// { family: 'IPv4', address: '127.0.0.1', port: '80' } - -var proxy = new Multiaddr('/ip4/192.168.2.1/tcp/3128') -// - -var full = proxy.encapsulate(home) -// - -full.toString() -// '/ip4/192.168.2.1/tcp/3128/ip4/127.0.0.1/tcp/80' -``` - -## Installation - -### npm - -```sh -> npm install multiaddr -``` - -## Setup - -### Node.js - -```js -var Multiaddr = require('multiaddr') -``` - -### Browser: Browserify, Webpack, other bundlers - -The code published to npm that gets loaded on require is in fact a ES5 -transpiled version with the right shims added. This means that you can require -it and use with your favourite bundler without having to adjust asset management -process. - -```js -var Multiaddr = require('multiaddr') -``` - -### Browser: ` - - -``` From 708292caf20a8086ee127e498e0fac33dcc49544 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 13:58:28 +0000 Subject: [PATCH 03/19] fix: fix resolver exports --- src/resolvers/dns.js | 2 +- src/resolvers/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resolvers/dns.js b/src/resolvers/dns.js index e85a5bd2..75206c00 100644 --- a/src/resolvers/dns.js +++ b/src/resolvers/dns.js @@ -3,7 +3,7 @@ let dns try { - dns = require('dns').promises + dns = require('dns').Resolver if (!dns) { throw new Error('no dns available') } diff --git a/src/resolvers/index.js b/src/resolvers/index.js index d14c5c99..3e065486 100644 --- a/src/resolvers/index.js +++ b/src/resolvers/index.js @@ -12,7 +12,7 @@ const { code: dnsaddrCode } = protocols('dnsaddr') * @returns {Promise>} */ async function dnsaddrResolver (addr) { - const { Resolver } = require('./dns') + const Resolver = require('./dns') const resolver = new Resolver() const peerId = addr.getPeerId() From 4cec873b78225ef016da0094db55e41c2ab37a68 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 13:59:32 +0000 Subject: [PATCH 04/19] fix: add some types to codec.js --- src/codec.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/codec.js b/src/codec.js index 09bbfa0c..75a245d0 100644 --- a/src/codec.js +++ b/src/codec.js @@ -100,11 +100,18 @@ function stringTuplesToTuples (tuples) { }) } -// [[int code, Uint8Array]... ] -> [[str name, str addr]... ] +/** + * Convert tuples to string tuples + * + * [[int code, Uint8Array]... ] -> [[int code, str addr]... ] + * + * @param {Array<[number, Uint8Array?]>} tuples + * @returns {Array<[number, string?]>} + */ function tuplesToStringTuples (tuples) { return tuples.map(tup => { const proto = protoFromTuple(tup) - if (tup.length > 1) { + if (tup.length > 1 && tup[1]) { return [proto.code, convert.toString(proto.code, tup[1])] } return [proto.code] @@ -137,7 +144,14 @@ function sizeForAddr (p, addr) { } // Uint8Array -> [[int code, Uint8Array ]... ] + +/** + * + * @param {Uint8Array} buf + * @returns {Array<[number, Uint8Array?]>} + */ function bytesToTuples (buf) { + /** @type {Array<[number, Uint8Array?]>} */ const tuples = [] let i = 0 while (i < buf.length) { From 449065d98b110691d403f4ed884037da820de94f Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 14:01:27 +0000 Subject: [PATCH 05/19] fix: add some types to convert BREAKING CHANGE: Convert.toString now always a string --- src/convert.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/convert.js b/src/convert.js index 7258b50c..7af26a6f 100644 --- a/src/convert.js +++ b/src/convert.js @@ -20,9 +20,16 @@ function Convert (proto, a) { } } -Convert.toString = function convertToString (proto, buf) { - proto = protocols(proto) - switch (proto.code) { +/** + * Convert [code,Uint8Array] to string + * + * @param {number|string} proto + * @param {Uint8Array} buf + * @returns {string} + */ +Convert.toString = function (proto, buf) { + const protocol = protocols(proto) + switch (protocol.code) { case 4: // ipv4 case 41: // ipv6 return bytes2ip(buf) @@ -31,7 +38,7 @@ Convert.toString = function convertToString (proto, buf) { case 273: // udp case 33: // dccp case 132: // sctp - return bytes2port(buf) + return bytes2port(buf).toString() case 53: // dns case 54: // dns4 @@ -137,6 +144,12 @@ function mh2bytes (hash) { return uint8ArrayConcat([size, mh], size.length + mh.length) } +/** + * Converts bytes to bas58btc string + * + * @param {Uint8Array} buf + * @returns {string} bas58btc string + */ function bytes2mh (buf) { const size = varint.decode(buf) const address = buf.slice(varint.decode.bytes) From 7251ac96dc33282bec9f6f85b5bc4ebbaecac629 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 14:01:49 +0000 Subject: [PATCH 06/19] fix: add some types to protocols --- src/protocols-table.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/protocols-table.js b/src/protocols-table.js index 4578e436..eeb81c5c 100644 --- a/src/protocols-table.js +++ b/src/protocols-table.js @@ -1,5 +1,11 @@ 'use strict' +/** + * Protocols + * + * @param {number | string | string} proto + * @returns {import("./types").Protocol} + */ function Protocols (proto) { if (typeof (proto) === 'number') { if (Protocols.codes[proto]) { @@ -7,7 +13,7 @@ function Protocols (proto) { } throw new Error('no protocol with code: ' + proto) - } else if (typeof (proto) === 'string' || proto instanceof String) { + } else if (typeof (proto) === 'string') { if (Protocols.names[proto]) { return Protocols.names[proto] } @@ -22,6 +28,7 @@ const V = -1 Protocols.lengthPrefixedVarSize = V Protocols.V = V +/** @type {Array<[number, number, string, (string|boolean)?, string?]>} */ Protocols.table = [ [4, 32, 'ip4'], [6, 16, 'tcp'], @@ -59,7 +66,9 @@ Protocols.table = [ [777, V, 'memory'] ] +/** @type {Record} */ Protocols.names = {} +/** @type {Record} */ Protocols.codes = {} // populate tables @@ -71,6 +80,17 @@ Protocols.table.map(row => { Protocols.object = p +/** + * + * Create a protocol + * + * @param {number} code + * @param {number} size + * @param {string} name + * @param {any} [resolvable] + * @param {any} [path] + * @returns {import("./types").Protocol} + */ function p (code, size, name, resolvable, path) { return { code, From 9a853c2ef92cf3379aa982eea5ec01ebad146dc3 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 14:05:02 +0000 Subject: [PATCH 07/19] fix: remove class-is and fix types in index.js --- src/index.js | 87 ++++++++++++++++++++++++++++------------------------ src/types.ts | 7 +++++ 2 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 src/types.ts diff --git a/src/index.js b/src/index.js index fc464bd6..46de9541 100644 --- a/src/index.js +++ b/src/index.js @@ -4,30 +4,31 @@ const codec = require('./codec') const protocols = require('./protocols-table') const varint = require('varint') const CID = require('cids') -const withIs = require('class-is') const errCode = require('err-code') const inspect = Symbol.for('nodejs.util.inspect.custom') const uint8ArrayToString = require('uint8arrays/to-string') const uint8ArrayEquals = require('uint8arrays/equals') const resolvers = new Map() +const symbol = Symbol.for('@multiformats/js-multiaddr/multiaddr') /** * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from * a Uint8Array, String or another Multiaddr instance * public key. * - * @class Multiaddr + * @constructor Multiaddr * @param {(string | Uint8Array | Multiaddr)} addr - If String or Uint8Array, needs to adhere * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) * @example * Multiaddr('/ip4/127.0.0.1/tcp/4001') * // */ -const Multiaddr = withIs.proto(function (addr) { +function Multiaddr (addr) { if (!(this instanceof Multiaddr)) { return new Multiaddr(addr) } + Object.defineProperty(this, symbol, { value: true }) // default if (addr == null) { @@ -49,7 +50,7 @@ const Multiaddr = withIs.proto(function (addr) { } else { throw new Error('addr must be a string, Buffer, or another Multiaddr') } -}, { className: 'Multiaddr', symbolName: '@multiformats/js-multiaddr/multiaddr' }) +} /** * Returns Multiaddr as a String @@ -59,7 +60,7 @@ const Multiaddr = withIs.proto(function (addr) { * Multiaddr('/ip4/127.0.0.1/tcp/4001').toString() * // '/ip4/127.0.0.1/tcp/4001' */ -Multiaddr.prototype.toString = function toString () { +Multiaddr.prototype.toString = function () { return codec.bytesToString(this.bytes) } @@ -123,22 +124,13 @@ Multiaddr.prototype.inspect = function inspect () { codec.bytesToString(this.bytes) + '>' } -/** - * @typedef {object} protocol - * @property {number} code - * @property {number} size - * @property {string} name - * @property {boolean} [resolvable] - * @property {boolean} [path] - */ - /** * Returns the protocols the Multiaddr is defined with, as an array of objects, in * left-to-right order. Each object contains the protocol code, protocol name, * and the size of its address space in bits. * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) * - * @returns {protocol[]} protocols - All the protocols the address is composed of + * @returns {import('./types').Protocol[]} protocols - All the protocols the address is composed of * @example * Multiaddr('/ip4/127.0.0.1/tcp/4001').protos() * // [ { code: 4, size: 32, name: 'ip4' }, @@ -191,7 +183,7 @@ Multiaddr.prototype.protoNames = function protoNames () { /** * Returns a tuple of parts * - * @returns {[number, Uint8Array][]} tuples + * @returns {[number, (Uint8Array | undefined)?][]} tuples * @example * Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples() * // [ [ 4, ], [ 6, ] ] @@ -205,7 +197,7 @@ Multiaddr.prototype.tuples = function tuples () { * - tuples[][0] = code of protocol * - tuples[][1] = contents of address * - * @returns {[number, string|number][]} tuples + * @returns {[number, string?][]} tuples * @example * Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples() * // [ [ 4, '127.0.0.1' ], [ 6, 4001 ] ] @@ -234,8 +226,8 @@ Multiaddr.prototype.stringTuples = function stringTuples () { * // '/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001' */ Multiaddr.prototype.encapsulate = function encapsulate (addr) { - addr = Multiaddr(addr) - return Multiaddr(this.toString() + addr.toString()) + addr = new Multiaddr(addr) + return new Multiaddr(this.toString() + addr.toString()) } /** @@ -257,13 +249,13 @@ Multiaddr.prototype.encapsulate = function encapsulate (addr) { * // '/ip4/8.8.8.8/tcp/1080' */ Multiaddr.prototype.decapsulate = function decapsulate (addr) { - addr = addr.toString() + const addrString = addr.toString() const s = this.toString() - const i = s.lastIndexOf(addr) + const i = s.lastIndexOf(addrString) if (i < 0) { - throw new Error('Address ' + this + ' does not contain subaddress: ' + addr) + throw new Error('Address ' + this + ' does not contain subaddress: ' + addrString) } - return Multiaddr(s.slice(0, i)) + return new Multiaddr(s.slice(0, i)) } /** @@ -288,7 +280,7 @@ Multiaddr.prototype.decapsulateCode = function decapsulateCode (code) { const tuples = this.tuples() for (let i = tuples.length - 1; i >= 0; i--) { if (tuples[i][0] === code) { - return Multiaddr(codec.tuplesToBytes(tuples.slice(0, i))) + return new Multiaddr(codec.tuplesToBytes(tuples.slice(0, i))) } } return this @@ -306,7 +298,6 @@ Multiaddr.prototype.decapsulateCode = function decapsulateCode (code) { * const peerId = mh1.getPeerId() */ Multiaddr.prototype.getPeerId = function getPeerId () { - let b58str = null try { const tuples = this.stringTuples().filter((tuple) => { if (tuple[0] === protocols.names.ipfs.code) { @@ -314,15 +305,17 @@ Multiaddr.prototype.getPeerId = function getPeerId () { } }) - // Get the last id - b58str = tuples.pop()[1] - // Get multihash, unwrap from CID if needed - b58str = uint8ArrayToString(new CID(b58str).multihash, 'base58btc') + // Get the last ipfs tuple ['ipfs', 'peerid string'] + const tuple = tuples.pop() + if (tuple && tuple[1]) { + // Get multihash, unwrap from CID if needed + return uint8ArrayToString(new CID(tuple[1]).multihash, 'base58btc') + } else { + return null + } } catch (e) { - b58str = null + return null } - - return b58str } /** @@ -345,6 +338,10 @@ Multiaddr.prototype.getPath = function getPath () { return true } })[0][1] + + if (!path) { + path = null + } } catch (e) { path = null } @@ -356,7 +353,7 @@ Multiaddr.prototype.getPath = function getPath () { * Checks if two Multiaddrs are the same * * @param {Multiaddr} addr - * @returns {Bool} + * @returns {boolean} * @example * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') * // @@ -402,7 +399,7 @@ Multiaddr.prototype.resolve = async function resolve () { } const addresses = await resolver(this) - return addresses.map(a => Multiaddr(a)) + return addresses.map(a => new Multiaddr(a)) } /** @@ -412,11 +409,11 @@ Multiaddr.prototype.resolve = async function resolve () { * * Has to be a ThinWaist Address, otherwise throws error * - * @returns {{family: string, address: string, port: number}} + * @returns {{family: number, address: string, port: number}} * @throws {Error} Throws error if Multiaddr is not a Thin Waist address * @example * Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress() - * // {family: 'IPv4', address: '127.0.0.1', port: '4001'} + * // {family: 4, address: '127.0.0.1', port: '4001'} */ Multiaddr.prototype.nodeAddress = function nodeAddress () { const codes = this.protoCodes() @@ -464,7 +461,7 @@ Multiaddr.fromNodeAddress = function fromNodeAddress (addr, transport) { default: throw Error(`Invalid addr family. Got '${addr.family}' instead of 'IPv4' or 'IPv6'`) } - return Multiaddr('/' + [ip, addr.address, transport, addr.port].join('/')) + return new Multiaddr('/' + [ip, addr.address, transport, addr.port].join('/')) } // TODO find a better example, not sure about it's good enough @@ -524,7 +521,7 @@ Multiaddr.protocols = protocols * Returns if something is a Multiaddr that is a name * * @param {Multiaddr} addr - * @returns {Bool} isName + * @returns {boolean} isName */ Multiaddr.isName = function isName (addr) { if (!Multiaddr.isMultiaddr(addr)) { @@ -540,7 +537,7 @@ Multiaddr.isName = function isName (addr) { * * @async * @param {Multiaddr} addr - * @returns {Multiaddr[]} + * @returns {Promise} */ Multiaddr.resolve = function resolve (addr) { if (!Multiaddr.isMultiaddr(addr) || !Multiaddr.isName(addr)) { @@ -555,5 +552,15 @@ Multiaddr.resolve = function resolve (addr) { return Promise.reject(new Error('not implemented yet')) } +/** + * Check if object is a CID instance + * + * @param {any} value + * @returns {boolean} + */ +Multiaddr.isMultiaddr = (value) => { + return value instanceof Multiaddr || Boolean(value && value[symbol]) +} + Multiaddr.resolvers = resolvers -exports = module.exports = Multiaddr +module.exports = Multiaddr diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..9ae0bef5 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,7 @@ +export type Protocol = { + code: number; + size: number; + name: string; + resolvable?: boolean | undefined; + path?: boolean | undefined; +} From a09e829e85a53e9516f8b465e64550d0f5c768e6 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 17:44:36 +0000 Subject: [PATCH 08/19] fix: transform multiaddr into a class BREAKING CHANGE: index.js default export no longer is both callable and constructor. encapsulate and decapsulate only accept Multiaddr instances as input. --- src/index.d.ts | 204 --------- src/index.js | 981 +++++++++++++++++++++-------------------- src/protocols-table.js | 9 +- src/resolvers/index.js | 5 +- test/index.spec.js | 19 +- test/protocols.spec.js | 1 + test/resolvers.spec.js | 2 +- test/types.spec.ts | 22 - 8 files changed, 513 insertions(+), 730 deletions(-) delete mode 100644 src/index.d.ts delete mode 100644 test/types.spec.ts diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index 0fca50e3..00000000 --- a/src/index.d.ts +++ /dev/null @@ -1,204 +0,0 @@ -declare type NetOptions = { - family: "ipv4" | "ipv6"; - host: string; - transport: "tcp" | "udp"; - port: number; -}; - -declare type Protocol = { - code: number; - size: number; - name: string; - resolvable: boolean | undefined; - path: boolean | undefined; -}; - -declare interface Protocols { - table: { - [index: number]: Protocol; - }; - names: { - [index: string]: Protocol; - }; - codes: { - [index: number]: Protocol; - }; - object( - code: number, - size: number, - name: string, - resolvable?: any, - path?: any - ): Protocol; -} - -declare type NodeAddress = { - family: "IPv4" | "IPv6"; - address: string; - port: string; -}; - -declare type MultiaddrInput = string | Uint8Array | Multiaddr | null; - -declare class Multiaddr { - /** - * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from - * a Uint8Array, String or another Multiaddr instance - * public key. - * @param addr - If String or Uint8Array, needs to adhere - * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) - */ - constructor(addr?: MultiaddrInput); - - bytes: Uint8Array; - - /** - * Returns Multiaddr as a String - */ - toString(): string; - - /** - * Returns Multiaddr as a JSON encoded object - */ - toJSON(): string; - - /** - * Returns Multiaddr as a convinient options object to be used with net.createConnection - */ - toOptions(): NetOptions; - - /** - * Returns Multiaddr as a human-readable string - */ - inspect(): string; - - /** - * Returns the protocols the Multiaddr is defined with, as an array of objects, in - * left-to-right order. Each object contains the protocol code, protocol name, - * and the size of its address space in bits. - * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) - */ - protos(): Protocol[]; - - /** - * Returns the codes of the protocols in left-to-right order. - * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) - */ - protoCodes(): number[]; - - /** - * Returns the names of the protocols in left-to-right order. - * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) - */ - protoNames(): string[]; - - /** - * Returns a tuple of parts - */ - tuples(): [number, Uint8Array][]; - - /** - * Returns a tuple of string/number parts - */ - stringTuples(): [number, string | number][]; - - /** - * Encapsulates a Multiaddr in another Multiaddr - */ - encapsulate(addr: MultiaddrInput): Multiaddr; - - /** - * Decapsulates a Multiaddr from another Multiaddr - */ - decapsulate(addr: MultiaddrInput): Multiaddr; - - /** - * A more reliable version of `decapsulate` if you are targeting a - * specific code, such as 421 (the `p2p` protocol code). The last index of the code - * will be removed from the `Multiaddr`, and a new instance will be returned. - * If the code is not present, the original `Multiaddr` is returned. - */ - decapsulateCode(code: number): Multiaddr; - - /** - * Extract the peerId if the multiaddr contains one - */ - getPeerId(): string; - - /** - * Extract the path if the multiaddr contains one - */ - getPath(): string | null; - - /** - * Checks if two Multiaddrs are the same - */ - equals(addr: Multiaddr): boolean; - - /** - * Gets a Multiaddrs node-friendly address object. Note that protocol information - * is left out: in Node (and most network systems) the protocol is unknowable - * given only the address. - * - * Has to be a ThinWaist Address, otherwise throws error - */ - nodeAddress(): NodeAddress; - - /** - * Returns if a Multiaddr is a Thin Waist address or not. - * - * Thin Waist is if a Multiaddr adheres to the standard combination of: - * - * `{IPv4, IPv6}/{TCP, UDP}` - */ - isThinWaistAddress(addr?: Multiaddr): boolean; - - /** - * Resolve multiaddr if containing resolvable hostname. - */ - resolve(): Promise> -} - -declare namespace Multiaddr { - const resolvers: Map < string, (addr: Multiaddr) => Promise < Array < string >>> - - /** - * Creates a Multiaddr from a node-friendly address object - */ - function fromNodeAddress(addr: NodeAddress, transport: string): Multiaddr; - - /** - * Object containing table, names and codes of all supported protocols. - * To get the protocol values from a Multiaddr, you can use - * [`.protos()`](#multiaddrprotos), - * [`.protoCodes()`](#multiaddrprotocodes) or - * [`.protoNames()`](#multiaddrprotonames) - */ - const protocols: Protocols; - - /** - * Returns if something is a Multiaddr - */ - function isMultiaddr(addr: unknown): addr is Multiaddr; - - /** - * Returns if something is a Multiaddr that is a name - */ - function isName(addr: Multiaddr): boolean; - - /** - * Returns an array of multiaddrs, by resolving the multiaddr that is a name - */ - function resolve(addr: Multiaddr): Promise; -} - -/** - * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from - * a Uint8Array, String or another Multiaddr instance - * public key. - * @param addr - If String or Uint8Array, needs to adhere - * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) - */ -declare function Multiaddr(input?: MultiaddrInput): Multiaddr; - -export = Multiaddr; diff --git a/src/index.js b/src/index.js index 46de9541..df7d1121 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,6 @@ const protocols = require('./protocols-table') const varint = require('varint') const CID = require('cids') const errCode = require('err-code') -const inspect = Symbol.for('nodejs.util.inspect.custom') const uint8ArrayToString = require('uint8arrays/to-string') const uint8ArrayEquals = require('uint8arrays/equals') @@ -16,551 +15,561 @@ const symbol = Symbol.for('@multiformats/js-multiaddr/multiaddr') * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from * a Uint8Array, String or another Multiaddr instance * public key. - * - * @constructor Multiaddr - * @param {(string | Uint8Array | Multiaddr)} addr - If String or Uint8Array, needs to adhere - * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001') - * // */ -function Multiaddr (addr) { - if (!(this instanceof Multiaddr)) { - return new Multiaddr(addr) - } - Object.defineProperty(this, symbol, { value: true }) +class Multiaddr { + /** + * @param {string | Uint8Array | Multiaddr | null} [addr] - If String or Uint8Array, needs to adhere + * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001') + * // + */ + constructor (addr) { + Object.defineProperty(this, symbol, { value: true }) - // default - if (addr == null) { - addr = '' - } + // default + if (addr == null) { + addr = '' + } - if (addr instanceof Uint8Array) { - /** - * @type {Uint8Array} - The raw bytes representing this multiaddress - */ - this.bytes = codec.fromBytes(addr) - } else if (typeof addr === 'string' || addr instanceof String) { - if (addr.length > 0 && addr.charAt(0) !== '/') { - throw new Error(`multiaddr "${addr}" must start with a "/"`) + if (addr instanceof Uint8Array) { + /** + * @type {Uint8Array} - The raw bytes representing this multiaddress + */ + this.bytes = codec.fromBytes(addr) + } else if (typeof addr === 'string' || addr instanceof String) { + if (addr.length > 0 && addr.charAt(0) !== '/') { + throw new Error(`multiaddr "${addr}" must start with a "/"`) + } + this.bytes = codec.fromString(addr) + } else if (addr.bytes && addr.protos && addr.protoCodes) { // Multiaddr + this.bytes = codec.fromBytes(addr.bytes) // validate + copy buffer + } else { + throw new Error('addr must be a string, Buffer, or another Multiaddr') } - this.bytes = codec.fromString(addr) - } else if (addr.bytes && addr.protos && addr.protoCodes) { // Multiaddr - this.bytes = codec.fromBytes(addr.bytes) // validate + copy buffer - } else { - throw new Error('addr must be a string, Buffer, or another Multiaddr') } -} -/** - * Returns Multiaddr as a String - * - * @returns {string} - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').toString() - * // '/ip4/127.0.0.1/tcp/4001' - */ -Multiaddr.prototype.toString = function () { - return codec.bytesToString(this.bytes) -} - -/** - * Returns Multiaddr as a JSON encoded object - * - * @returns {string} - * @example - * JSON.stringify(Multiaddr('/ip4/127.0.0.1/tcp/4001')) - * // '/ip4/127.0.0.1/tcp/4001' - */ -Multiaddr.prototype.toJSON = Multiaddr.prototype.toString + /** + * Returns Multiaddr as a String + * + * @returns {string} + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001').toString() + * // '/ip4/127.0.0.1/tcp/4001' + */ + toString () { + return codec.bytesToString(this.bytes) + } -/** - * Returns Multiaddr as a convinient options object to be used with net.createConnection - * - * @returns {{family: string, host: string, transport: string, port: number}} - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions() - * // { family: 'ipv4', host: '127.0.0.1', transport: 'tcp', port: 4001 } - */ -Multiaddr.prototype.toOptions = function toOptions () { - const opts = {} - const parsed = this.toString().split('/') - opts.family = parsed[1] === 'ip4' ? 'ipv4' : 'ipv6' - opts.host = parsed[2] - opts.transport = parsed[3] - opts.port = parseInt(parsed[4]) - return opts -} + /** + * Returns Multiaddr as a JSON encoded object + * + * @returns {string} + * @example + * JSON.stringify(Multiaddr('/ip4/127.0.0.1/tcp/4001')) + * // '/ip4/127.0.0.1/tcp/4001' + */ + toJSON () { + return this.toString() + } -/** - * Returns Multiaddr as a human-readable string. - * For post Node.js v10.0.0. - * https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect - * - * @returns {string} - * @example - * console.log(Multiaddr('/ip4/127.0.0.1/tcp/4001')) - * // '' - */ -Multiaddr.prototype[inspect] = function inspectCustom () { - return '' -} + /** + * Returns Multiaddr as a convinient options object to be used with net.createConnection + * + * @returns {{family: string, host: string, transport: string, port: number}} + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions() + * // { family: 'ipv4', host: '127.0.0.1', transport: 'tcp', port: 4001 } + */ + toOptions () { + const opts = {} + const parsed = this.toString().split('/') + opts.family = parsed[1] === 'ip4' ? 'ipv4' : 'ipv6' + opts.host = parsed[2] + opts.transport = parsed[3] + opts.port = parseInt(parsed[4]) + return opts + } -/** - * Returns Multiaddr as a human-readable string. - * Fallback for pre Node.js v10.0.0. - * https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect - * - * @returns {string} - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').inspect() - * // '' - */ -Multiaddr.prototype.inspect = function inspect () { - return '' -} + /** + * Returns Multiaddr as a human-readable string. + * Fallback for pre Node.js v10.0.0. + * https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect + * + * @returns {string} + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001').inspect() + * // '' + */ + [Symbol.for('nodejs.util.inspect.custom')] () { + return '' + } -/** - * Returns the protocols the Multiaddr is defined with, as an array of objects, in - * left-to-right order. Each object contains the protocol code, protocol name, - * and the size of its address space in bits. - * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) - * - * @returns {import('./types').Protocol[]} protocols - All the protocols the address is composed of - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').protos() - * // [ { code: 4, size: 32, name: 'ip4' }, - * // { code: 6, size: 16, name: 'tcp' } ] - */ -Multiaddr.prototype.protos = function protos () { - return this.protoCodes().map(code => Object.assign({}, protocols(code))) -} + inspect () { + return '' + } -/** - * Returns the codes of the protocols in left-to-right order. - * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) - * - * @returns {Array} protocol codes - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes() - * // [ 4, 6 ] - */ -Multiaddr.prototype.protoCodes = function protoCodes () { - const codes = [] - const buf = this.bytes - let i = 0 - while (i < buf.length) { - const code = varint.decode(buf, i) - const n = varint.decode.bytes - - const p = protocols(code) - const size = codec.sizeForAddr(p, buf.slice(i + n)) - - i += (size + n) - codes.push(code) + /** + * Returns the protocols the Multiaddr is defined with, as an array of objects, in + * left-to-right order. Each object contains the protocol code, protocol name, + * and the size of its address space in bits. + * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) + * + * @returns {import('./types').Protocol[]} protocols - All the protocols the address is composed of + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001').protos() + * // [ { code: 4, size: 32, name: 'ip4' }, + * // { code: 6, size: 16, name: 'tcp' } ] + */ + protos () { + return this.protoCodes().map(code => Object.assign({}, protocols(code))) } - return codes -} + /** + * Returns the codes of the protocols in left-to-right order. + * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) + * + * @returns {Array} protocol codes + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes() + * // [ 4, 6 ] + */ + protoCodes () { + const codes = [] + const buf = this.bytes + let i = 0 + while (i < buf.length) { + const code = varint.decode(buf, i) + const n = varint.decode.bytes + + const p = protocols(code) + const size = codec.sizeForAddr(p, buf.slice(i + n)) + + i += (size + n) + codes.push(code) + } -/** - * Returns the names of the protocols in left-to-right order. - * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) - * - * @returns {Array.} protocol names - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames() - * // [ 'ip4', 'tcp' ] - */ -Multiaddr.prototype.protoNames = function protoNames () { - return this.protos().map(proto => proto.name) -} + return codes + } -/** - * Returns a tuple of parts - * - * @returns {[number, (Uint8Array | undefined)?][]} tuples - * @example - * Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples() - * // [ [ 4, ], [ 6, ] ] - */ -Multiaddr.prototype.tuples = function tuples () { - return codec.bytesToTuples(this.bytes) -} + /** + * Returns the names of the protocols in left-to-right order. + * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) + * + * @returns {Array.} protocol names + * @example + * Multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames() + * // [ 'ip4', 'tcp' ] + */ + protoNames () { + return this.protos().map(proto => proto.name) + } -/** - * Returns a tuple of string/number parts - * - tuples[][0] = code of protocol - * - tuples[][1] = contents of address - * - * @returns {[number, string?][]} tuples - * @example - * Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples() - * // [ [ 4, '127.0.0.1' ], [ 6, 4001 ] ] - */ -Multiaddr.prototype.stringTuples = function stringTuples () { - const t = codec.bytesToTuples(this.bytes) - return codec.tuplesToStringTuples(t) -} + /** + * Returns a tuple of parts + * + * @returns {[number, (Uint8Array | undefined)?][]} tuples + * @example + * Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples() + * // [ [ 4, ], [ 6, ] ] + */ + tuples () { + return codec.bytesToTuples(this.bytes) + } -/** - * Encapsulates a Multiaddr in another Multiaddr - * - * @param {Multiaddr} addr - Multiaddr to add into this Multiaddr - * @returns {Multiaddr} - * @example - * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') - * // - * - * const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001') - * // - * - * const mh3 = mh1.encapsulate(mh2) - * // - * - * mh3.toString() - * // '/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001' - */ -Multiaddr.prototype.encapsulate = function encapsulate (addr) { - addr = new Multiaddr(addr) - return new Multiaddr(this.toString() + addr.toString()) -} + /** + * Returns a tuple of string/number parts + * - tuples[][0] = code of protocol + * - tuples[][1] = contents of address + * + * @returns {[number, string?][]} tuples + * @example + * Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples() + * // [ [ 4, '127.0.0.1' ], [ 6, 4001 ] ] + */ + stringTuples () { + const t = codec.bytesToTuples(this.bytes) + return codec.tuplesToStringTuples(t) + } -/** - * Decapsulates a Multiaddr from another Multiaddr - * - * @param {Multiaddr} addr - Multiaddr to remove from this Multiaddr - * @returns {Multiaddr} - * @example - * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') - * // - * - * const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001') - * // - * - * const mh3 = mh1.encapsulate(mh2) - * // - * - * mh3.decapsulate(mh2).toString() - * // '/ip4/8.8.8.8/tcp/1080' - */ -Multiaddr.prototype.decapsulate = function decapsulate (addr) { - const addrString = addr.toString() - const s = this.toString() - const i = s.lastIndexOf(addrString) - if (i < 0) { - throw new Error('Address ' + this + ' does not contain subaddress: ' + addrString) + /** + * Encapsulates a Multiaddr in another Multiaddr + * + * @param {Multiaddr} addr - Multiaddr to add into this Multiaddr + * @returns {Multiaddr} + * @example + * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') + * // + * + * const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001') + * // + * + * const mh3 = mh1.encapsulate(mh2) + * // + * + * mh3.toString() + * // '/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001' + */ + encapsulate (addr) { + addr = new Multiaddr(addr) + return new Multiaddr(this.toString() + addr.toString()) } - return new Multiaddr(s.slice(0, i)) -} -/** - * A more reliable version of `decapsulate` if you are targeting a - * specific code, such as 421 (the `p2p` protocol code). The last index of the code - * will be removed from the `Multiaddr`, and a new instance will be returned. - * If the code is not present, the original `Multiaddr` is returned. - * - * @param {number} code - The code of the protocol to decapsulate from this Multiaddr - * @returns {Multiaddr} - * @example - * const addr = Multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC') - * // - * - * addr.decapsulateCode(421).toString() - * // '/ip4/0.0.0.0/tcp/8080' - * - * Multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString() - * // '/ip4/127.0.0.1/tcp/8080' - */ -Multiaddr.prototype.decapsulateCode = function decapsulateCode (code) { - const tuples = this.tuples() - for (let i = tuples.length - 1; i >= 0; i--) { - if (tuples[i][0] === code) { - return new Multiaddr(codec.tuplesToBytes(tuples.slice(0, i))) + /** + * Decapsulates a Multiaddr from another Multiaddr + * + * @param {Multiaddr} addr - Multiaddr to remove from this Multiaddr + * @returns {Multiaddr} + * @example + * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') + * // + * + * const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001') + * // + * + * const mh3 = mh1.encapsulate(mh2) + * // + * + * mh3.decapsulate(mh2).toString() + * // '/ip4/8.8.8.8/tcp/1080' + */ + decapsulate (addr) { + const addrString = addr.toString() + const s = this.toString() + const i = s.lastIndexOf(addrString) + if (i < 0) { + throw new Error('Address ' + this + ' does not contain subaddress: ' + addrString) } + return new Multiaddr(s.slice(0, i)) } - return this -} -/** - * Extract the peerId if the multiaddr contains one - * - * @returns {string | null} peerId - The id of the peer or null if invalid or missing from the ma - * @example - * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string') - * // - * - * // should return QmValidBase58string or null if the id is missing or invalid - * const peerId = mh1.getPeerId() - */ -Multiaddr.prototype.getPeerId = function getPeerId () { - try { - const tuples = this.stringTuples().filter((tuple) => { - if (tuple[0] === protocols.names.ipfs.code) { - return true + /** + * A more reliable version of `decapsulate` if you are targeting a + * specific code, such as 421 (the `p2p` protocol code). The last index of the code + * will be removed from the `Multiaddr`, and a new instance will be returned. + * If the code is not present, the original `Multiaddr` is returned. + * + * @param {number} code - The code of the protocol to decapsulate from this Multiaddr + * @returns {Multiaddr} + * @example + * const addr = Multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC') + * // + * + * addr.decapsulateCode(421).toString() + * // '/ip4/0.0.0.0/tcp/8080' + * + * Multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString() + * // '/ip4/127.0.0.1/tcp/8080' + */ + decapsulateCode (code) { + const tuples = this.tuples() + for (let i = tuples.length - 1; i >= 0; i--) { + if (tuples[i][0] === code) { + return new Multiaddr(codec.tuplesToBytes(tuples.slice(0, i))) } - }) + } + return this + } - // Get the last ipfs tuple ['ipfs', 'peerid string'] - const tuple = tuples.pop() - if (tuple && tuple[1]) { - // Get multihash, unwrap from CID if needed - return uint8ArrayToString(new CID(tuple[1]).multihash, 'base58btc') - } else { + /** + * Extract the peerId if the multiaddr contains one + * + * @returns {string | null} peerId - The id of the peer or null if invalid or missing from the ma + * @example + * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string') + * // + * + * // should return QmValidBase58string or null if the id is missing or invalid + * const peerId = mh1.getPeerId() + */ + getPeerId () { + try { + const tuples = this.stringTuples().filter((tuple) => { + if (tuple[0] === protocols.names.ipfs.code) { + return true + } + }) + + // Get the last ipfs tuple ['ipfs', 'peerid string'] + const tuple = tuples.pop() + if (tuple && tuple[1]) { + // Get multihash, unwrap from CID if needed + return uint8ArrayToString(new CID(tuple[1]).multihash, 'base58btc') + } else { + return null + } + } catch (e) { return null } - } catch (e) { - return null } -} -/** - * Extract the path if the multiaddr contains one - * - * @returns {string | null} path - The path of the multiaddr, or null if no path protocol is present - * @example - * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock') - * // - * - * // should return utf8 string or null if the id is missing or invalid - * const path = mh1.getPath() - */ -Multiaddr.prototype.getPath = function getPath () { - let path = null - try { - path = this.stringTuples().filter((tuple) => { - const proto = protocols(tuple[0]) - if (proto.path) { - return true + /** + * Extract the path if the multiaddr contains one + * + * @returns {string | null} path - The path of the multiaddr, or null if no path protocol is present + * @example + * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock') + * // + * + * // should return utf8 string or null if the id is missing or invalid + * const path = mh1.getPath() + */ + getPath () { + let path = null + try { + path = this.stringTuples().filter((tuple) => { + const proto = protocols(tuple[0]) + if (proto.path) { + return true + } + })[0][1] + + if (!path) { + path = null } - })[0][1] - - if (!path) { + } catch (e) { path = null } - } catch (e) { - path = null + + return path } - return path -} + /** + * Checks if two Multiaddrs are the same + * + * @param {Multiaddr} addr + * @returns {boolean} + * @example + * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') + * // + * + * const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001') + * // + * + * mh1.equals(mh1) + * // true + * + * mh1.equals(mh2) + * // false + */ + equals (addr) { + return uint8ArrayEquals(this.bytes, addr.bytes) + } -/** - * Checks if two Multiaddrs are the same - * - * @param {Multiaddr} addr - * @returns {boolean} - * @example - * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080') - * // - * - * const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/4001') - * // - * - * mh1.equals(mh1) - * // true - * - * mh1.equals(mh2) - * // false - */ -Multiaddr.prototype.equals = function equals (addr) { - return uint8ArrayEquals(this.bytes, addr.bytes) -} + /** + * Resolve multiaddr if containing resolvable hostname. + * + * @returns {Promise>} + * @example + * Multiaddr.resolvers.set('dnsaddr', resolverFunction) + * const mh1 = Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb') + * const resolvedMultiaddrs = await mh1.resolve() + * // [ + * // , + * // , + * // + * // ] + */ + async resolve () { + const resolvableProto = this.protos().find((p) => p.resolvable) -/** - * Resolve multiaddr if containing resolvable hostname. - * - * @returns {Promise>} - * @example - * Multiaddr.resolvers.set('dnsaddr', resolverFunction) - * const mh1 = Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb') - * const resolvedMultiaddrs = await mh1.resolve() - * // [ - * // , - * // , - * // - * // ] - */ -Multiaddr.prototype.resolve = async function resolve () { - const resolvableProto = this.protos().find((p) => p.resolvable) + // Multiaddr is not resolvable? + if (!resolvableProto) { + return [this] + } - // Multiaddr is not resolvable? - if (!resolvableProto) { - return [this] - } + const resolver = resolvers.get(resolvableProto.name) + if (!resolver) { + throw errCode(new Error(`no available resolver for ${resolvableProto.name}`), 'ERR_NO_AVAILABLE_RESOLVER') + } - const resolver = resolvers.get(resolvableProto.name) - if (!resolver) { - throw errCode(new Error(`no available resolver for ${resolvableProto.name}`), 'ERR_NO_AVAILABLE_RESOLVER') + const addresses = await resolver(this) + return addresses.map(a => new Multiaddr(a)) } - const addresses = await resolver(this) - return addresses.map(a => new Multiaddr(a)) -} - -/** - * Gets a Multiaddrs node-friendly address object. Note that protocol information - * is left out: in Node (and most network systems) the protocol is unknowable - * given only the address. - * - * Has to be a ThinWaist Address, otherwise throws error - * - * @returns {{family: number, address: string, port: number}} - * @throws {Error} Throws error if Multiaddr is not a Thin Waist address - * @example - * Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress() - * // {family: 4, address: '127.0.0.1', port: '4001'} - */ -Multiaddr.prototype.nodeAddress = function nodeAddress () { - const codes = this.protoCodes() - const names = this.protoNames() - const parts = this.toString().split('/').slice(1) - - if (parts.length < 4) { - throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6}/{address}/{tcp, udp}/{port}".') - } else if (codes[0] !== 4 && codes[0] !== 41 && codes[0] !== 54 && codes[0] !== 55) { - throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns4, dns6}".`) - } else if (parts[2] !== 'tcp' && parts[2] !== 'udp') { - throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`) - } + /** + * Gets a Multiaddrs node-friendly address object. Note that protocol information + * is left out: in Node (and most network systems) the protocol is unknowable + * given only the address. + * + * Has to be a ThinWaist Address, otherwise throws error + * + * @returns {{family: number, address: string, port: number}} + * @throws {Error} Throws error if Multiaddr is not a Thin Waist address + * @example + * + * ```js + * Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress() + * // {family: 4, address: '127.0.0.1', port: '4001'} + * ``` + * + */ + nodeAddress () { + const codes = this.protoCodes() + const names = this.protoNames() + const parts = this.toString().split('/').slice(1) + + if (parts.length < 4) { + throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6}/{address}/{tcp, udp}/{port}".') + } else if (codes[0] !== 4 && codes[0] !== 41 && codes[0] !== 54 && codes[0] !== 55) { + throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns4, dns6}".`) + } else if (parts[2] !== 'tcp' && parts[2] !== 'udp') { + throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`) + } - return { - family: (codes[0] === 41 || codes[0] === 55) ? 6 : 4, - address: parts[1], // ip addr - port: parseInt(parts[3]) // tcp or udp port + return { + family: (codes[0] === 41 || codes[0] === 55) ? 6 : 4, + address: parts[1], + port: parseInt(parts[3]) // tcp or udp port + } } -} -/** - * Creates a Multiaddr from a node-friendly address object - * - * @param {{family: string, address: string, port: number}} addr - * @param {string} transport - * @returns {Multiaddr} multiaddr - * @throws {Error} Throws error if addr is not truthy - * @throws {Error} Throws error if transport is not truthy - * @example - * Multiaddr.fromNodeAddress({address: '127.0.0.1', port: '4001'}, 'tcp') - * // - */ -Multiaddr.fromNodeAddress = function fromNodeAddress (addr, transport) { - if (!addr) throw new Error('requires node address object') - if (!transport) throw new Error('requires transport protocol') - let ip - switch (addr.family) { - case 'IPv4': - ip = 'ip4' - break - case 'IPv6': - ip = 'ip6' - break - default: - throw Error(`Invalid addr family. Got '${addr.family}' instead of 'IPv4' or 'IPv6'`) - } - return new Multiaddr('/' + [ip, addr.address, transport, addr.port].join('/')) -} + // TODO find a better example, not sure about it's good enough + /** + * Returns if a Multiaddr is a Thin Waist address or not. + * + * Thin Waist is if a Multiaddr adheres to the standard combination of: + * + * `{IPv4, IPv6}/{TCP, UDP}` + * + * @param {Multiaddr} [addr] - Defaults to using `this` instance + * @returns {boolean} isThinWaistAddress + * @example + * const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/4001') + * // + * const mh2 = Multiaddr('/ip4/192.168.2.1/tcp/5001') + * // + * const mh3 = mh1.encapsulate(mh2) + * // + * mh1.isThinWaistAddress() + * // true + * mh2.isThinWaistAddress() + * // true + * mh3.isThinWaistAddress() + * // false + */ + isThinWaistAddress (addr) { + const protos = (addr || this).protos() -// TODO find a better example, not sure about it's good enough -/** - * Returns if a Multiaddr is a Thin Waist address or not. - * - * Thin Waist is if a Multiaddr adheres to the standard combination of: - * - * `{IPv4, IPv6}/{TCP, UDP}` - * - * @param {Multiaddr} [addr] - Defaults to using `this` instance - * @returns {boolean} isThinWaistAddress - * @example - * const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/4001') - * // - * const mh2 = Multiaddr('/ip4/192.168.2.1/tcp/5001') - * // - * const mh3 = mh1.encapsulate(mh2) - * // - * mh1.isThinWaistAddress() - * // true - * mh2.isThinWaistAddress() - * // true - * mh3.isThinWaistAddress() - * // false - */ -Multiaddr.prototype.isThinWaistAddress = function isThinWaistAddress (addr) { - const protos = (addr || this).protos() + if (protos.length !== 2) { + return false + } - if (protos.length !== 2) { - return false + if (protos[0].code !== 4 && protos[0].code !== 41) { + return false + } + if (protos[1].code !== 6 && protos[1].code !== 273) { + return false + } + return true } - if (protos[0].code !== 4 && protos[0].code !== 41) { - return false - } - if (protos[1].code !== 6 && protos[1].code !== 273) { - return false + /** + * Creates a Multiaddr from a node-friendly address object + * + * @param {{family: string, address: string, port: string}} addr + * @param {string} transport + * @returns {Multiaddr} multiaddr + * @throws {Error} Throws error if addr or transport are not truthy + * @example + * + * ```js + * Multiaddr.fromNodeAddress({address: '127.0.0.1', port: '4001'}, 'tcp') + * // + * ``` + */ + static fromNodeAddress (addr, transport) { + if (!addr) { throw new Error('requires node address object') } + if (!transport) { throw new Error('requires transport protocol') } + let ip + switch (addr.family) { + case 'IPv4': + ip = 'ip4' + break + case 'IPv6': + ip = 'ip6' + break + default: + throw Error(`Invalid addr family. Got '${addr.family}' instead of 'IPv4' or 'IPv6'`) + } + return new Multiaddr('/' + [ip, addr.address, transport, addr.port].join('/')) } - return true -} -/** - * Object containing table, names and codes of all supported protocols. - * To get the protocol values from a Multiaddr, you can use - * [`.protos()`](#multiaddrprotos), - * [`.protoCodes()`](#multiaddrprotocodes) or - * [`.protoNames()`](#multiaddrprotonames) - * - * @instance - * @returns {{table: Array, names: Object, codes: Object}} - * - */ -Multiaddr.protocols = protocols + /** + * Returns if something is a Multiaddr that is a name + * + * @param {Multiaddr} addr + * @returns {boolean} isName + */ + static isName (addr) { + if (!Multiaddr.isMultiaddr(addr)) { + return false + } -/** - * Returns if something is a Multiaddr that is a name - * - * @param {Multiaddr} addr - * @returns {boolean} isName - */ -Multiaddr.isName = function isName (addr) { - if (!Multiaddr.isMultiaddr(addr)) { - return false + // if a part of the multiaddr is resolvable, then return true + return addr.protos().some((proto) => proto.resolvable) } - // if a part of the multiaddr is resolvable, then return true - return addr.protos().some((proto) => proto.resolvable) -} + /** + * Returns an array of multiaddrs, by resolving the multiaddr that is a name + * + * @async + * @param {Multiaddr} addr + * @returns {Promise} + */ + static resolve (addr) { + if (!Multiaddr.isMultiaddr(addr) || !Multiaddr.isName(addr)) { + return Promise.reject(Error('not a valid name')) + } -/** - * Returns an array of multiaddrs, by resolving the multiaddr that is a name - * - * @async - * @param {Multiaddr} addr - * @returns {Promise} - */ -Multiaddr.resolve = function resolve (addr) { - if (!Multiaddr.isMultiaddr(addr) || !Multiaddr.isName(addr)) { - return Promise.reject(Error('not a valid name')) + /* + * Needs more consideration from spec design: + * - what to return + * - how to achieve it in the browser? + */ + return Promise.reject(new Error('not implemented yet')) } - /* - * Needs more consideration from spec design: - * - what to return - * - how to achieve it in the browser? + /** + * Check if object is a CID instance + * + * @param {any} value + * @returns {boolean} */ - return Promise.reject(new Error('not implemented yet')) + static isMultiaddr (value) { + return value instanceof Multiaddr || Boolean(value && value[symbol]) + } } +Multiaddr.protocols = protocols + +Multiaddr.resolvers = resolvers + /** - * Check if object is a CID instance + * Callable Multiaddr factory function * - * @param {any} value - * @returns {boolean} + * @param {string | Uint8Array | Multiaddr | null} [addr] - If String or Uint8Array, needs to adhere + * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) + * @example + * ```js + * Multiaddr('/ip4/127.0.0.1/tcp/4001') + * // + * ``` */ -Multiaddr.isMultiaddr = (value) => { - return value instanceof Multiaddr || Boolean(value && value[symbol]) +const MultiaddrFactory = (addr) => { + return new Multiaddr(addr) } -Multiaddr.resolvers = resolvers -module.exports = Multiaddr +MultiaddrFactory.protocols = protocols +MultiaddrFactory.resolvers = resolvers +MultiaddrFactory.Multiaddr = Multiaddr +MultiaddrFactory.isMultiaddr = Multiaddr.isMultiaddr +MultiaddrFactory.resolve = Multiaddr.resolve +MultiaddrFactory.isName = Multiaddr.isName +MultiaddrFactory.fromNodeAddress = Multiaddr.fromNodeAddress + +module.exports = MultiaddrFactory diff --git a/src/protocols-table.js b/src/protocols-table.js index eeb81c5c..a00ee166 100644 --- a/src/protocols-table.js +++ b/src/protocols-table.js @@ -1,10 +1,11 @@ 'use strict' +/** @typedef {import("./types").Protocol} Protocol */ /** * Protocols * * @param {number | string | string} proto - * @returns {import("./types").Protocol} + * @returns {Protocol} */ function Protocols (proto) { if (typeof (proto) === 'number') { @@ -66,9 +67,9 @@ Protocols.table = [ [777, V, 'memory'] ] -/** @type {Record} */ +/** @type {Record} */ Protocols.names = {} -/** @type {Record} */ +/** @type {Record} */ Protocols.codes = {} // populate tables @@ -89,7 +90,7 @@ Protocols.object = p * @param {string} name * @param {any} [resolvable] * @param {any} [path] - * @returns {import("./types").Protocol} + * @returns {Protocol} */ function p (code, size, name, resolvable, path) { return { diff --git a/src/resolvers/index.js b/src/resolvers/index.js index 3e065486..5c224b73 100644 --- a/src/resolvers/index.js +++ b/src/resolvers/index.js @@ -1,14 +1,14 @@ 'use strict' -const Multiaddr = require('..') // eslint-disable-line no-unused-vars const protocols = require('../protocols-table') const { code: dnsaddrCode } = protocols('dnsaddr') +// TODO `addr` type needs https://github.com/microsoft/TypeScript/issues/41672 /** * Resolver for dnsaddr addresses. * - * @param {Multiaddr} addr + * @param {any} addr * @returns {Promise>} */ async function dnsaddrResolver (addr) { @@ -19,7 +19,6 @@ async function dnsaddrResolver (addr) { const [, hostname] = addr.stringTuples().find(([proto]) => proto === dnsaddrCode) || [] const records = await resolver.resolveTxt(`_dnsaddr.${hostname}`) - // @ts-ignore let addresses = records.flat().map((a) => a.split('=')[1]) if (peerId) { diff --git a/test/index.spec.js b/test/index.spec.js index c154eb4a..d65814f0 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -11,7 +11,7 @@ describe('construction', () => { it('create multiaddr', () => { udpAddr = multiaddr('/ip4/127.0.0.1/udp/1234') - expect(udpAddr instanceof multiaddr).to.equal(true) + expect(udpAddr instanceof multiaddr.Multiaddr).to.equal(true) }) it('clone multiaddr', () => { @@ -77,7 +77,7 @@ describe('requiring varint', () => { it('create multiaddr', () => { uTPAddr = multiaddr('/ip4/127.0.0.1/udp/1234/utp') - expect(uTPAddr instanceof multiaddr).to.equal(true) + expect(uTPAddr instanceof multiaddr.Multiaddr).to.equal(true) }) it('clone multiaddr', () => { @@ -119,13 +119,13 @@ describe('manipulation', () => { expect(udpAddr.protos()).to.deep.equal([multiaddr.protocols.codes[4], multiaddr.protocols.codes[273]]) expect(udpAddr.protos()[0] === multiaddr.protocols.codes[4]).to.equal(false) - const udpAddrbytes2 = udpAddr.encapsulate('/udp/5678') + const udpAddrbytes2 = udpAddr.encapsulate(multiaddr('/udp/5678')) expect(udpAddrbytes2.toString()).to.equal('/ip4/127.0.0.1/udp/1234/udp/5678') - expect(udpAddrbytes2.decapsulate('/udp').toString()).to.equal('/ip4/127.0.0.1/udp/1234') - expect(udpAddrbytes2.decapsulate('/ip4').toString()).to.equal('/') - expect(function () { udpAddr.decapsulate('/').toString() }).to.throw() + expect(udpAddrbytes2.decapsulate(multiaddr('/udp/5678')).toString()).to.equal('/ip4/127.0.0.1/udp/1234') + expect(udpAddrbytes2.decapsulate(multiaddr('/ip4/127.0.0.1')).toString()).to.equal('/') + expect(function () { udpAddr.decapsulate(multiaddr('/')).toString() }).to.throw() expect(multiaddr('/').encapsulate(udpAddr).toString()).to.equal(udpAddr.toString()) - expect(multiaddr('/').decapsulate('/').toString()).to.equal('/') + expect(multiaddr('/').decapsulate(multiaddr('/')).toString()).to.equal('/') }) it('ipfs', () => { @@ -628,7 +628,7 @@ describe('helpers', () => { describe('.decapsulate', () => { it('throws on address with no matching subaddress', () => { expect( - () => multiaddr('/ip4/127.0.0.1').decapsulate('/ip4/198.168.0.0') + () => multiaddr('/ip4/127.0.0.1').decapsulate(multiaddr('/ip4/198.168.0.0')) ).to.throw( /does not contain subaddress/ ) @@ -638,7 +638,7 @@ describe('helpers', () => { describe('.decapsulateCode', () => { it('removes the last occurrence of the code from the multiaddr', () => { const relayTCP = multiaddr('/ip4/0.0.0.0/tcp/8080') - const relay = relayTCP.encapsulate('/p2p/QmZR5a9AAXGqQF2ADqoDdGS8zvqv8n3Pag6TDDnTNMcFW6/p2p-circuit') + const relay = relayTCP.encapsulate(multiaddr('/p2p/QmZR5a9AAXGqQF2ADqoDdGS8zvqv8n3Pag6TDDnTNMcFW6/p2p-circuit')) const target = multiaddr('/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC') const original = relay.encapsulate(target) expect(original.decapsulateCode(421)).to.eql(relay) @@ -906,7 +906,6 @@ describe('helpers', () => { const addr = multiaddr(str) return multiaddr.resolve(addr) - // @ts-ignore .then(expect.fail, (err) => { expect(err).to.exist() expect(err.message).to.eql('not a valid name') diff --git a/test/protocols.spec.js b/test/protocols.spec.js index b5b53016..a6e12a01 100644 --- a/test/protocols.spec.js +++ b/test/protocols.spec.js @@ -24,6 +24,7 @@ describe('protocols', () => { it('else', () => { expect( + // @ts-expect-error () => protocols({ hi: 34 }) ).to.throw( /invalid protocol id type/ diff --git a/test/resolvers.spec.js b/test/resolvers.spec.js index 747c80de..1967979b 100644 --- a/test/resolvers.spec.js +++ b/test/resolvers.spec.js @@ -6,7 +6,7 @@ const sinon = require('sinon') const multiaddr = require('../src') const resolvers = require('../src/resolvers') -const { Resolver } = require('../src/resolvers/dns') +const Resolver = require('../src/resolvers/dns') const dnsaddrStub1 = [ ['dnsaddr=/dnsaddr/ams-1.bootstrap.libp2p.io/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd'], diff --git a/test/types.spec.ts b/test/types.spec.ts deleted file mode 100644 index ba9129c9..00000000 --- a/test/types.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import Multiaddr from '../src' - -const testStr = '/ip4/127.0.0.1' - -export const maFromFunctionConstructor: Multiaddr = Multiaddr(testStr) - -export const maFromClassConstructor: Multiaddr = new Multiaddr(testStr) - -export const maFromMa: Multiaddr = Multiaddr(new Multiaddr(testStr)) - -export const maFromConstructorFunction: Multiaddr = Multiaddr.fromNodeAddress( - { - family: 'IPv4', - address: '127.0.0.1', - port: '12345' - }, - 'udp' -) - -export function doSthWithMa (ma: Multiaddr): void { - ma.toOptions() -} From 04e0b48f596cf10f422df8715289c5b17e647368 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 17:45:12 +0000 Subject: [PATCH 09/19] fix: update package.json and tsconfig --- package.json | 28 +++++++++++++++++----------- tsconfig.json | 24 ++++++++---------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 637dae53..053eb059 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,21 @@ "description": "multiaddr implementation (binary + string representation of network addresses)", "leadMaintainer": "Jacob Heun ", "main": "src/index.js", - "types": "src/index.d.ts", + "types": "dist/src/index.d.ts", + "typesVersions": { + "*": { + "src/*": [ + "dist/src/*", + "dist/src/*/index" + ] + } + }, "scripts": { "lint": "aegir lint", - "test": "npm run test:node && npm run test:browser", - "test:node": "aegir test --ts -t node", + "test": "aegir test", + "test:node": "aegir test -t node", "test:browser": "aegir test -t browser", - "test:types": "npx tsc", + "test:types": "aegir ts -p check", "build": "aegir build", "release": "aegir release", "release-minor": "aegir release --type minor", @@ -42,16 +50,14 @@ "is-ip": "^3.1.0", "multibase": "^3.0.0", "uint8arrays": "^1.1.0", - "varint": "^5.0.0" + "varint": "^6.0.0" }, "devDependencies": { - "@types/chai": "^4.2.8", - "@types/dirty-chai": "^2.0.2", - "@types/mocha": "^8.0.1", - "@types/node": "^14.0.11", "aegir": "^29.0.1", - "sinon": "^9.2.0", - "typescript": "^3.9.5" + "sinon": "^9.2.0" + }, + "eslintConfig": { + "extends": "ipfs" }, "contributors": [ "David Dias ", diff --git a/tsconfig.json b/tsconfig.json index 60e2c5b1..e605b61a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,18 +1,10 @@ { - "compilerOptions": { - "module": "commonjs", - "lib": ["es6"], - "target": "ES5", - "noImplicitAny": false, - "noImplicitThis": true, - "strictFunctionTypes": true, - "strictNullChecks": true, - "esModuleInterop": true, - "resolveJsonModule": true, - "allowJs": true, - "checkJs": true, - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "include": ["src/index.d.ts", "test/**/*.spec.js", "test/**/*.spec.ts"] + "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "test", // remove this line if you don't want to type-check tests + "src" + ] } From 980973deba0198b7f6ba56b2e106c20c96b661fe Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 24 Nov 2020 17:45:57 +0000 Subject: [PATCH 10/19] chore: update readme to look like the other multiformats repos --- README.md | 62 ++++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index cf037f0e..1324e831 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,30 @@ -js-multiaddr +js-multiaddr ============ -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) -[![](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-multiaddr.svg?style=flat-square)](https://david-dm.org/multiformats/js-multiaddr) -[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) -[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) -[![](https://img.shields.io/travis/multiformats/js-multiaddr.svg?style=flat-square)](https://travis-ci.com/multiformats/js-multiaddr) +[![pl](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai) +[![project](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats) +[![irc](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs) [![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiaddr.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiaddr) +[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/multiformats/js-multiaddr/ci?label=ci&style=flat-square)](https://github.com/multiformats/js-multiaddr/actions?query=branch%3Amaster+workflow%3Aci+) > JavaScript implementation of [multiaddr](https://github.com/multiformats/multiaddr). -## Lead Maintainer +## Lead Maintainer [Jacob Heun](https://github.com/jacobheun) -## Table of Contents - -- [js-multiaddr](#js-multiaddr) - - [Lead Maintainer](#lead-maintainer) - - [Table of Contents](#table-of-contents) - - [Background](#background) - - [What is multiaddr?](#what-is-multiaddr) - - [Install](#install) - - [Setup](#setup) - - [Node.js](#nodejs) - - [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers) - - [Browser: `