From 30186c49653f7782b616774399b02a3b85d021a0 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 7 Aug 2020 14:35:31 +0100 Subject: [PATCH 1/2] fix: replace node buffers with uint8arrays All uses of node Buffers have been replaced with Uint8Arrays. Also redundant modules have been removed from package.json BREAKING CHANGES: - Where node Buffers were returned, now Uint8Arrays are - The `.buffer` property has been renamed `.bytes` similar to cid@1.0.0 --- .aegir.js | 7 +++ README.md | 34 ++++++----- examples/try.js | 4 +- intro.md | 8 +-- package.json | 15 ++--- src/codec.js | 71 +++++++++++------------ src/convert.js | 102 +++++++++++++++++---------------- src/index.d.ts | 14 ++--- src/index.js | 42 +++++++------- src/ip.js | 19 ++++--- test/codec.spec.js | 24 ++++---- test/convert.spec.js | 24 ++++---- test/index.spec.js | 124 ++++++++++++++++++++--------------------- test/protocols.spec.js | 5 +- 14 files changed, 247 insertions(+), 246 deletions(-) create mode 100644 .aegir.js diff --git a/.aegir.js b/.aegir.js new file mode 100644 index 00000000..07d86700 --- /dev/null +++ b/.aegir.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = { + bundlesize: { + maxSize: '14kB' + } +} \ No newline at end of file diff --git a/README.md b/README.md index bb125bef..0772a0ae 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,20 @@ js-multiaddr ## 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: ` ``` -**NOTE**: You will need access to the Node.js `Buffer` API. If you are running -in the browser, you can access it with `multiaddr.Buffer` or you can install -[feross/buffer](https://github.com/feross/buffer). - ## Usage ```js @@ -93,8 +91,8 @@ $ node > const addr = multiaddr("/ip4/127.0.0.1/udp/1234") -> addr.buffer - +> addr.bytes + > addr.toString() '/ip4/127.0.0.1/udp/1234' diff --git a/examples/try.js b/examples/try.js index 7cfd10a9..6762c3cf 100644 --- a/examples/try.js +++ b/examples/try.js @@ -5,9 +5,9 @@ var log = console.log var addr = multiaddr('/ip4/127.0.0.1/udp/1234') log(addr) -log(addr.buffer) +log(addr.bytes) log(addr.toString()) -log(multiaddr(addr.buffer)) +log(multiaddr(addr.bytes)) log(addr.protoCodes()) log(addr.protoNames()) diff --git a/intro.md b/intro.md index 3dd9c11f..eb6503ec 100644 --- a/intro.md +++ b/intro.md @@ -2,14 +2,14 @@ JavaScript implementation of [Multiaddr](https://github.com/multiformats/multiad ## What is multiaddr? -Multiaddr is a standard way to represent addresses that: +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: +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) @@ -22,8 +22,8 @@ var Multiaddr = require('multiaddr') var home = new Multiaddr('/ip4/127.0.0.1/tcp/80') // -home.buffer -// +home.bytes +// home.toString() // '/ip4/127.0.0.1/tcp/80' diff --git a/package.json b/package.json index 817a9652..2b2b738f 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "release-minor": "aegir release --type minor", "release-major": "aegir release --type major", "docs": "aegir docs", - "size": "bundlesize -f dist/index.min.js -s 14kB" + "size": "aegir build -b" }, "files": [ "src", @@ -32,22 +32,19 @@ "bugs": "https://github.com/multiformats/js-multiaddr/issues", "homepage": "https://github.com/multiformats/js-multiaddr", "dependencies": { - "buffer": "^5.5.0", - "cids": "~0.8.0", + "cids": "^1.0.0", "class-is": "^1.1.0", "is-ip": "^3.1.0", - "multibase": "^0.7.0", + "multibase": "^3.0.0", + "uint8arrays": "^1.1.0", "varint": "^5.0.0" }, "devDependencies": { "@types/chai": "^4.2.8", "@types/dirty-chai": "^2.0.2", - "@types/mocha": "^7.0.1", + "@types/mocha": "^8.0.1", "@types/node": "^14.0.11", - "aegir": "^22.0.0", - "bundlesize": "~0.18.0", - "chai": "^4.2.0", - "dirty-chai": "^2.0.1", + "aegir": "^25.0.0", "typescript": "^3.9.5" }, "contributors": [ diff --git a/src/codec.js b/src/codec.js index 8e8319c9..09bbfa0c 100644 --- a/src/codec.js +++ b/src/codec.js @@ -1,9 +1,10 @@ 'use strict' -const { Buffer } = require('buffer') const convert = require('./convert') const protocols = require('./protocols-table') const varint = require('varint') +const uint8ArrayConcat = require('uint8arrays/concat') +const uint8ArrayToString = require('uint8arrays/to-string') // export codec module.exports = { @@ -13,16 +14,16 @@ module.exports = { tuplesToStringTuples, stringTuplesToTuples, - bufferToTuples, - tuplesToBuffer, + bytesToTuples, + tuplesToBytes, - bufferToString, - stringToBuffer, + bytesToString, + stringToBytes, fromString, - fromBuffer, - validateBuffer, - isValidBuffer, + fromBytes, + validateBytes, + isValidBytes, cleanPath, ParseError, @@ -85,7 +86,7 @@ function stringTuplesToString (tuples) { return cleanPath(parts.join('/')) } -// [[str name, str addr]... ] -> [[int code, Buffer]... ] +// [[str name, str addr]... ] -> [[int code, Uint8Array]... ] function stringTuplesToTuples (tuples) { return tuples.map(tup => { if (!Array.isArray(tup)) { @@ -93,13 +94,13 @@ function stringTuplesToTuples (tuples) { } const proto = protoFromTuple(tup) if (tup.length > 1) { - return [proto.code, convert.toBuffer(proto.code, tup[1])] + return [proto.code, convert.toBytes(proto.code, tup[1])] } return [proto.code] }) } -// [[int code, Buffer]... ] -> [[str name, str addr]... ] +// [[int code, Uint8Array]... ] -> [[str name, str addr]... ] function tuplesToStringTuples (tuples) { return tuples.map(tup => { const proto = protoFromTuple(tup) @@ -110,14 +111,14 @@ function tuplesToStringTuples (tuples) { }) } -// [[int code, Buffer ]... ] -> Buffer -function tuplesToBuffer (tuples) { - return fromBuffer(Buffer.concat(tuples.map(tup => { +// [[int code, Uint8Array ]... ] -> Uint8Array +function tuplesToBytes (tuples) { + return fromBytes(uint8ArrayConcat(tuples.map(tup => { const proto = protoFromTuple(tup) - let buf = Buffer.from(varint.encode(proto.code)) + let buf = Uint8Array.from(varint.encode(proto.code)) if (tup.length > 1) { - buf = Buffer.concat([buf, tup[1]]) // add address buffer + buf = uint8ArrayConcat([buf, tup[1]]) // add address buffer } return buf @@ -135,8 +136,8 @@ function sizeForAddr (p, addr) { } } -// Buffer -> [[int code, Buffer ]... ] -function bufferToTuples (buf) { +// Uint8Array -> [[int code, Uint8Array ]... ] +function bytesToTuples (buf) { const tuples = [] let i = 0 while (i < buf.length) { @@ -158,7 +159,7 @@ function bufferToTuples (buf) { i += (size + n) if (i > buf.length) { // did not end _exactly_ at buffer.length - throw ParseError('Invalid address buffer: ' + buf.toString('hex')) + throw ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(buf, 'base16')) } // ok, tuple seems good. @@ -168,44 +169,44 @@ function bufferToTuples (buf) { return tuples } -// Buffer -> String -function bufferToString (buf) { - const a = bufferToTuples(buf) +// Uint8Array -> String +function bytesToString (buf) { + const a = bytesToTuples(buf) const b = tuplesToStringTuples(a) return stringTuplesToString(b) } -// String -> Buffer -function stringToBuffer (str) { +// String -> Uint8Array +function stringToBytes (str) { str = cleanPath(str) const a = stringToStringTuples(str) const b = stringTuplesToTuples(a) - return tuplesToBuffer(b) + return tuplesToBytes(b) } -// String -> Buffer +// String -> Uint8Array function fromString (str) { - return stringToBuffer(str) + return stringToBytes(str) } -// Buffer -> Buffer -function fromBuffer (buf) { - const err = validateBuffer(buf) +// Uint8Array -> Uint8Array +function fromBytes (buf) { + const err = validateBytes(buf) if (err) throw err - return Buffer.from(buf) // copy + return Uint8Array.from(buf) // copy } -function validateBuffer (buf) { +function validateBytes (buf) { try { - bufferToTuples(buf) // try to parse. will throw if breaks + bytesToTuples(buf) // try to parse. will throw if breaks } catch (err) { return err } } -function isValidBuffer (buf) { - return validateBuffer(buf) === undefined +function isValidBytes (buf) { + return validateBytes(buf) === undefined } function cleanPath (str) { diff --git a/src/convert.js b/src/convert.js index cb3bcd9f..7258b50c 100644 --- a/src/convert.js +++ b/src/convert.js @@ -1,20 +1,22 @@ 'use strict' -const { Buffer } = require('buffer') const ip = require('./ip') const protocols = require('./protocols-table') const CID = require('cids') const multibase = require('multibase') const varint = require('varint') +const uint8ArrayToString = require('uint8arrays/to-string') +const uint8ArrayFromString = require('uint8arrays/from-string') +const uint8ArrayConcat = require('uint8arrays/concat') module.exports = Convert // converts (serializes) addresses function Convert (proto, a) { - if (a instanceof Buffer) { + if (a instanceof Uint8Array) { return Convert.toString(proto, a) } else { - return Convert.toBuffer(proto, a) + return Convert.toBytes(proto, a) } } @@ -23,13 +25,13 @@ Convert.toString = function convertToString (proto, buf) { switch (proto.code) { case 4: // ipv4 case 41: // ipv6 - return buf2ip(buf) + return bytes2ip(buf) case 6: // tcp case 273: // udp case 33: // dccp case 132: // sctp - return buf2port(buf) + return bytes2port(buf) case 53: // dns case 54: // dns4 @@ -37,32 +39,32 @@ Convert.toString = function convertToString (proto, buf) { case 56: // dnsaddr case 400: // unix case 777: // memory - return buf2str(buf) + return bytes2str(buf) case 421: // ipfs - return buf2mh(buf) + return bytes2mh(buf) case 444: // onion - return buf2onion(buf) + return bytes2onion(buf) case 445: // onion3 - return buf2onion(buf) + return bytes2onion(buf) default: - return buf.toString('hex') // no clue. convert to hex + return uint8ArrayToString(buf, 'base16') // no clue. convert to hex } } -Convert.toBuffer = function convertToBuffer (proto, str) { +Convert.toBytes = function convertToBytes (proto, str) { proto = protocols(proto) switch (proto.code) { case 4: // ipv4 - return ip2buf(str) + return ip2bytes(str) case 41: // ipv6 - return ip2buf(str) + return ip2bytes(str) case 6: // tcp case 273: // udp case 33: // dccp case 132: // sctp - return port2buf(parseInt(str, 10)) + return port2bytes(parseInt(str, 10)) case 53: // dns case 54: // dns4 @@ -70,27 +72,27 @@ Convert.toBuffer = function convertToBuffer (proto, str) { case 56: // dnsaddr case 400: // unix case 777: // memory - return str2buf(str) + return str2bytes(str) case 421: // ipfs - return mh2buf(str) + return mh2bytes(str) case 444: // onion - return onion2buf(str) + return onion2bytes(str) case 445: // onion3 - return onion32buf(str) + return onion32bytes(str) default: - return Buffer.from(str, 'hex') // no clue. convert from hex + return uint8ArrayFromString(str, 'base16') // no clue. convert from hex } } -function ip2buf (ipString) { +function ip2bytes (ipString) { if (!ip.isIP(ipString)) { throw new Error('invalid ip address') } - return ip.toBuffer(ipString) + return ip.toBytes(ipString) } -function buf2ip (ipBuff) { +function bytes2ip (ipBuff) { const ipString = ip.toString(ipBuff) if (!ipString || !ip.isIP(ipString)) { throw new Error('invalid ip address') @@ -98,23 +100,26 @@ function buf2ip (ipBuff) { return ipString } -function port2buf (port) { - const buf = Buffer.alloc(2) - buf.writeUInt16BE(port, 0) - return buf +function port2bytes (port) { + const buf = new ArrayBuffer(2) + const view = new DataView(buf) + view.setUint16(0, port) + + return new Uint8Array(buf) } -function buf2port (buf) { - return buf.readUInt16BE(0) +function bytes2port (buf) { + const view = new DataView(buf.buffer) + return view.getUint16(0) } -function str2buf (str) { - const buf = Buffer.from(str) - const size = Buffer.from(varint.encode(buf.length)) - return Buffer.concat([size, buf]) +function str2bytes (str) { + const buf = uint8ArrayFromString(str) + const size = Uint8Array.from(varint.encode(buf.length)) + return uint8ArrayConcat([size, buf], size.length + buf.length) } -function buf2str (buf) { +function bytes2str (buf) { const size = varint.decode(buf) buf = buf.slice(varint.decode.bytes) @@ -122,27 +127,28 @@ function buf2str (buf) { throw new Error('inconsistent lengths') } - return buf.toString() + return uint8ArrayToString(buf) } -function mh2buf (hash) { +function mh2bytes (hash) { // the address is a varint prefixed multihash string representation const mh = new CID(hash).multihash - const size = Buffer.from(varint.encode(mh.length)) - return Buffer.concat([size, mh]) + const size = Uint8Array.from(varint.encode(mh.length)) + return uint8ArrayConcat([size, mh], size.length + mh.length) } -function buf2mh (buf) { +function bytes2mh (buf) { const size = varint.decode(buf) const address = buf.slice(varint.decode.bytes) if (address.length !== size) { throw new Error('inconsistent lengths') } - return multibase.encode('base58btc', address).toString().slice(1) + + return uint8ArrayToString(address, 'base58btc') } -function onion2buf (str) { +function onion2bytes (str) { const addr = str.split(':') if (addr.length !== 2) { throw new Error('failed to parse onion addr: ' + addr + ' does not contain a port number') @@ -159,11 +165,11 @@ function onion2buf (str) { if (port < 1 || port > 65536) { throw new Error('Port number is not in range(1, 65536)') } - const portBuf = port2buf(port) - return Buffer.concat([buf, portBuf]) + const portBuf = port2bytes(port) + return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length) } -function onion32buf (str) { +function onion32bytes (str) { const addr = str.split(':') if (addr.length !== 2) { throw new Error('failed to parse onion addr: ' + addr + ' does not contain a port number') @@ -179,14 +185,14 @@ function onion32buf (str) { if (port < 1 || port > 65536) { throw new Error('Port number is not in range(1, 65536)') } - const portBuf = port2buf(port) - return Buffer.concat([buf, portBuf]) + const portBuf = port2bytes(port) + return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length) } -function buf2onion (buf) { +function bytes2onion (buf) { const addrBytes = buf.slice(0, buf.length - 2) const portBytes = buf.slice(buf.length - 2) - const addr = multibase.encode('base32', addrBytes).toString().slice(1) - const port = buf2port(portBytes) + const addr = uint8ArrayToString(addrBytes, 'base32') + const port = bytes2port(portBytes) return addr + ':' + port } diff --git a/src/index.d.ts b/src/index.d.ts index f33073cf..430a538f 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -38,19 +38,19 @@ declare type NodeAddress = { port: string; }; -declare type MultiaddrInput = string | Buffer | Multiaddr | null; +declare type MultiaddrInput = string | Uint8Array | Multiaddr | null; declare class Multiaddr { /** * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from - * a Buffer, String or another Multiaddr instance + * a Uint8Array, String or another Multiaddr instance * public key. - * @param addr - If String or Buffer, needs to adhere + * @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); - buffer: Buffer; + bytes: Uint8Array; /** * Returns Multiaddr as a String @@ -95,7 +95,7 @@ declare class Multiaddr { /** * Returns a tuple of parts */ - tuples(): [number, Buffer][]; + tuples(): [number, Uint8Array][]; /** * Returns a tuple of string/number parts @@ -187,9 +187,9 @@ declare namespace Multiaddr { /** * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from - * a Buffer, String or another Multiaddr instance + * a Uint8Array, String or another Multiaddr instance * public key. - * @param addr - If String or Buffer, needs to adhere + * @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; diff --git a/src/index.js b/src/index.js index 023faefd..39391c70 100644 --- a/src/index.js +++ b/src/index.js @@ -1,20 +1,20 @@ 'use strict' const codec = require('./codec') -const { Buffer } = require('buffer') const protocols = require('./protocols-table') const varint = require('varint') -const multibase = require('multibase') const CID = require('cids') const withIs = require('class-is') const inspect = Symbol.for('nodejs.util.inspect.custom') +const uint8ArrayToString = require('uint8arrays/to-string') +const uint8ArrayEquals = require('uint8arrays/equals') /** * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from - * a Buffer, String or another Multiaddr instance + * a Uint8Array, String or another Multiaddr instance * public key. * @class Multiaddr - * @param {(String|Buffer|Multiaddr)} addr - If String or Buffer, needs to adhere + * @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') @@ -30,18 +30,18 @@ const Multiaddr = withIs.proto(function (addr) { addr = '' } - if (addr instanceof Buffer) { + if (addr instanceof Uint8Array) { /** - * @type {Buffer} - The raw bytes representing this multiaddress + * @type {Uint8Array} - The raw bytes representing this multiaddress */ - this.buffer = codec.fromBuffer(addr) + 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.buffer = codec.fromString(addr) - } else if (addr.buffer && addr.protos && addr.protoCodes) { // Multiaddr - this.buffer = codec.fromBuffer(addr.buffer) // validate + copy buffer + 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') } @@ -56,7 +56,7 @@ const Multiaddr = withIs.proto(function (addr) { * // '/ip4/127.0.0.1/tcp/4001' */ Multiaddr.prototype.toString = function toString () { - return codec.bufferToString(this.buffer) + return codec.bytesToString(this.bytes) } /** @@ -99,8 +99,8 @@ Multiaddr.prototype.toOptions = function toOptions () { */ Multiaddr.prototype[inspect] = function inspectCustom () { return '' + uint8ArrayToString(this.bytes, 'base16') + ' - ' + + codec.bytesToString(this.bytes) + '>' } /** @@ -115,8 +115,8 @@ Multiaddr.prototype[inspect] = function inspectCustom () { */ Multiaddr.prototype.inspect = function inspect () { return '' + uint8ArrayToString(this.bytes, 'base16') + ' - ' + + codec.bytesToString(this.bytes) + '>' } /** @@ -149,7 +149,7 @@ Multiaddr.prototype.protos = function protos () { */ Multiaddr.prototype.protoCodes = function protoCodes () { const codes = [] - const buf = this.buffer + const buf = this.bytes let i = 0 while (i < buf.length) { const code = varint.decode(buf, i) @@ -189,7 +189,7 @@ Multiaddr.prototype.protoNames = function protoNames () { * // [ [ 4, ], [ 6, ] ] */ Multiaddr.prototype.tuples = function tuples () { - return codec.bufferToTuples(this.buffer) + return codec.bytesToTuples(this.bytes) } /** @@ -203,7 +203,7 @@ Multiaddr.prototype.tuples = function tuples () { * // [ [ 4, '127.0.0.1' ], [ 6, 4001 ] ] */ Multiaddr.prototype.stringTuples = function stringTuples () { - const t = codec.bufferToTuples(this.buffer) + const t = codec.bytesToTuples(this.bytes) return codec.tuplesToStringTuples(t) } @@ -280,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.tuplesToBuffer(tuples.slice(0, i))) + return Multiaddr(codec.tuplesToBytes(tuples.slice(0, i))) } } return this @@ -309,7 +309,7 @@ Multiaddr.prototype.getPeerId = function getPeerId () { // Get the last id b58str = tuples.pop()[1] // Get multihash, unwrap from CID if needed - b58str = multibase.encode('base58btc', new CID(b58str).multihash).toString().slice(1) + b58str = uint8ArrayToString(new CID(b58str).multihash, 'base58btc') } catch (e) { b58str = null } @@ -363,7 +363,7 @@ Multiaddr.prototype.getPath = function getPath () { * // false */ Multiaddr.prototype.equals = function equals (addr) { - return this.buffer.equals(addr.buffer) + return uint8ArrayEquals(this.bytes, addr.bytes) } /** diff --git a/src/ip.js b/src/ip.js index e5f40b48..c1ffb273 100644 --- a/src/ip.js +++ b/src/ip.js @@ -1,20 +1,20 @@ 'use strict' -const { Buffer } = require('buffer') const isIp = require('is-ip') +const uint8ArrayToString = require('uint8arrays/to-string') const isIP = isIp const isV4 = isIp.v4 const isV6 = isIp.v6 // Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7 -const toBuffer = function (ip, buff, offset) { +const toBytes = function (ip, buff, offset) { offset = ~~offset var result if (isV4(ip)) { - result = buff || Buffer.alloc(offset + 4) + result = buff || new Uint8Array(offset + 4) ip.split(/\./g).map(function (byte) { result[offset++] = parseInt(byte, 10) & 0xff }) @@ -27,12 +27,12 @@ const toBuffer = function (ip, buff, offset) { var v4Buffer if (isv4) { - v4Buffer = toBuffer(sections[i]) - sections[i] = v4Buffer.slice(0, 2).toString('hex') + v4Buffer = toBytes(sections[i]) + sections[i] = uint8ArrayToString(v4Buffer.slice(0, 2), 'base16') } if (v4Buffer && ++i < 8) { - sections.splice(i, 0, v4Buffer.slice(2, 4).toString('hex')) + sections.splice(i, 0, uint8ArrayToString(v4Buffer.slice(2, 4), 'base16')) } } @@ -49,7 +49,7 @@ const toBuffer = function (ip, buff, offset) { sections.splice.apply(sections, argv) } - result = buff || Buffer.alloc(offset + 16) + result = buff || new Uint8Array(offset + 16) for (i = 0; i < sections.length; i++) { var word = parseInt(sections[i], 16) result[offset++] = (word >> 8) & 0xff @@ -71,6 +71,7 @@ const toString = function (buff, offset, length) { var result = [] var string + const view = new DataView(buff.buffer) if (length === 4) { // IPv4 for (let i = 0; i < length; i++) { @@ -80,7 +81,7 @@ const toString = function (buff, offset, length) { } else if (length === 16) { // IPv6 for (let i = 0; i < length; i += 2) { - result.push(buff.readUInt16BE(offset + i).toString(16)) + result.push(view.getUint16(offset + i).toString(16)) } string = result.join(':') string = string.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3') @@ -94,6 +95,6 @@ module.exports = { isIP, isV4, isV6, - toBuffer, + toBytes, toString } diff --git a/test/codec.spec.js b/test/codec.spec.js index b3eeab2b..9d8974fd 100644 --- a/test/codec.spec.js +++ b/test/codec.spec.js @@ -3,10 +3,8 @@ const codec = require('../src/codec') const varint = require('varint') -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) +const { expect } = require('aegir/utils/chai') +const uint8ArrayFromString = require('uint8arrays/from-string') describe('codec', () => { describe('.stringToStringTuples', () => { @@ -24,7 +22,7 @@ describe('codec', () => { expect( codec.stringTuplesToTuples([['ip4', '0.0.0.0'], 'utp']) ).to.eql( - [[4, Buffer.from([0, 0, 0, 0])], [302]] + [[4, Uint8Array.from([0, 0, 0, 0])], [302]] ) }) }) @@ -39,34 +37,34 @@ describe('codec', () => { }) }) - describe('.bufferToTuples', () => { + describe('.bytesToTuples', () => { it('throws on invalid address', () => { expect( - () => codec.bufferToTuples(codec.tuplesToBuffer([[4, Buffer.from('192')]])) + () => codec.bytesToTuples(codec.tuplesToBytes([[4, uint8ArrayFromString('192')]])) ).to.throw( - /Invalid address buffer/ + /Invalid address/ ) }) }) - describe('.fromBuffer', () => { + describe('.fromBytes', () => { it('throws on invalid buffer', () => { expect( - () => codec.fromBuffer(Buffer.from('hello/world')) + () => codec.fromBytes(uint8ArrayFromString('hello/world')) ).to.throw() }) }) - describe('.isValidBuffer', () => { + describe('.isValidBytes', () => { it('returns true for valid buffers', () => { expect( - codec.isValidBuffer(Buffer.from(varint.encode(302))) + codec.isValidBytes(Uint8Array.from(varint.encode(302))) ).to.equal(true) }) it('returns false for invalid buffers', () => { expect( - codec.isValidBuffer(Buffer.from(varint.encode(1234))) + codec.isValidBytes(Uint8Array.from(varint.encode(1234))) ).to.equal(false) }) }) diff --git a/test/convert.spec.js b/test/convert.spec.js index e53c0a0f..3c86f968 100644 --- a/test/convert.spec.js +++ b/test/convert.spec.js @@ -2,15 +2,13 @@ 'use strict' const convert = require('../src/convert') -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) +const { expect } = require('aegir/utils/chai') +const uint8ArrayFromString = require('uint8arrays/from-string') describe('convert', () => { it('handles ip4 buffers', () => { expect( - convert('ip4', Buffer.from('c0a80001', 'hex')) + convert('ip4', uint8ArrayFromString('c0a80001', 'base16')) ).to.eql( '192.168.0.1' ) @@ -18,7 +16,7 @@ describe('convert', () => { it('handles ip6 buffers', () => { expect( - convert('ip6', Buffer.from('abcd0000000100020003000400050006', 'hex')) + convert('ip6', uint8ArrayFromString('abcd0000000100020003000400050006', 'base16')) ).to.eql( 'abcd:0:1:2:3:4:5:6' ) @@ -28,7 +26,7 @@ describe('convert', () => { expect( convert('ip6', 'ABCD::1:2:3:4:5:6') ).to.eql( - Buffer.from('ABCD0000000100020003000400050006', 'hex') + uint8ArrayFromString('ABCD0000000100020003000400050006', 'base16upper') ) }) @@ -36,7 +34,7 @@ describe('convert', () => { expect( convert('ip4', '192.168.0.1') ).to.eql( - Buffer.from('c0a80001', 'hex') + uint8ArrayFromString('c0a80001', 'base16') ) }) @@ -56,19 +54,19 @@ describe('convert', () => { ) }) - describe('.toBuffer', () => { + describe('.toBytes', () => { it('defaults to hex conversion', () => { expect( - convert.toBuffer('ws', 'c0a80001') + convert.toBytes('ws', 'c0a80001') ).to.eql( - Buffer.from([192, 168, 0, 1]) + Uint8Array.from([192, 168, 0, 1]) ) }) }) describe('.toString', () => { it('throws on inconsistent ipfs links', () => { - const valid = Buffer.from('03221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a68911c0b', 'hex') + const valid = uint8ArrayFromString('03221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a68911c0b', 'base16') expect( () => convert.toString('ipfs', valid.slice(0, valid.length - 8)) ).to.throw( @@ -78,7 +76,7 @@ describe('convert', () => { it('defaults to hex conversion', () => { expect( - convert.toString('ws', Buffer.from([192, 168, 0, 1])) + convert.toString('ws', Uint8Array.from([192, 168, 0, 1])) ).to.eql( 'c0a80001' ) diff --git a/test/index.spec.js b/test/index.spec.js index e684e8de..c154eb4a 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -3,10 +3,8 @@ 'use strict' const multiaddr = require('../src') -const chai = require('chai') -const dirtyChai = require('dirty-chai') -chai.use(dirtyChai) -const expect = chai.expect +const { expect } = require('aegir/utils/chai') +const uint8ArrayFromString = require('uint8arrays/from-string') describe('construction', () => { let udpAddr @@ -22,23 +20,23 @@ describe('construction', () => { }) it('reconstruct with buffer', () => { - expect(multiaddr(udpAddr.buffer).buffer === udpAddr.buffer).to.equal(false) - expect(multiaddr(udpAddr.buffer).buffer).to.deep.equal(udpAddr.buffer) + expect(multiaddr(udpAddr.bytes).bytes === udpAddr.bytes).to.equal(false) + expect(multiaddr(udpAddr.bytes).bytes).to.deep.equal(udpAddr.bytes) }) it('reconstruct with string', () => { - expect(multiaddr(udpAddr.toString()).buffer === udpAddr.buffer).to.equal(false) - expect(multiaddr(udpAddr.toString()).buffer).to.deep.equal(udpAddr.buffer) + expect(multiaddr(udpAddr.toString()).bytes === udpAddr.bytes).to.equal(false) + expect(multiaddr(udpAddr.toString()).bytes).to.deep.equal(udpAddr.bytes) }) it('reconstruct with object', () => { - expect(multiaddr(udpAddr).buffer === udpAddr.buffer).to.equal(false) - expect(multiaddr(udpAddr).buffer).to.deep.equal(udpAddr.buffer) + expect(multiaddr(udpAddr).bytes === udpAddr.bytes).to.equal(false) + expect(multiaddr(udpAddr).bytes).to.deep.equal(udpAddr.bytes) }) it('reconstruct with JSON', () => { - expect(multiaddr(JSON.parse(JSON.stringify(udpAddr))).buffer === udpAddr.buffer).to.equal(false) - expect(multiaddr(JSON.parse(JSON.stringify(udpAddr))).buffer).to.deep.equal(udpAddr.buffer) + expect(multiaddr(JSON.parse(JSON.stringify(udpAddr))).bytes === udpAddr.bytes).to.equal(false) + expect(multiaddr(JSON.parse(JSON.stringify(udpAddr))).bytes).to.deep.equal(udpAddr.bytes) }) it('empty construct still works', () => { @@ -88,18 +86,18 @@ describe('requiring varint', () => { }) it('reconstruct with buffer', () => { - expect(multiaddr(uTPAddr.buffer).buffer === uTPAddr.buffer).to.equal(false) - expect(multiaddr(uTPAddr.buffer).buffer).to.deep.equal(uTPAddr.buffer) + expect(multiaddr(uTPAddr.bytes).bytes === uTPAddr.bytes).to.equal(false) + expect(multiaddr(uTPAddr.bytes).bytes).to.deep.equal(uTPAddr.bytes) }) it('reconstruct with string', () => { - expect(multiaddr(uTPAddr.toString()).buffer === uTPAddr.buffer).to.equal(false) - expect(multiaddr(uTPAddr.toString()).buffer).to.deep.equal(uTPAddr.buffer) + expect(multiaddr(uTPAddr.toString()).bytes === uTPAddr.bytes).to.equal(false) + expect(multiaddr(uTPAddr.toString()).bytes).to.deep.equal(uTPAddr.bytes) }) it('reconstruct with object', () => { - expect(multiaddr(uTPAddr).buffer === uTPAddr.buffer).to.equal(false) - expect(multiaddr(uTPAddr).buffer).to.deep.equal(uTPAddr.buffer) + expect(multiaddr(uTPAddr).bytes === uTPAddr.bytes).to.equal(false) + expect(multiaddr(uTPAddr).bytes).to.deep.equal(uTPAddr.bytes) }) it('empty construct still works', () => { @@ -110,21 +108,21 @@ describe('requiring varint', () => { describe('manipulation', () => { it('basic', () => { const udpAddrStr = '/ip4/127.0.0.1/udp/1234' - const udpAddrBuf = Buffer.from('047f000001910204d2', 'hex') + const udpAddrBuf = uint8ArrayFromString('047f000001910204d2', 'base16') const udpAddr = multiaddr(udpAddrStr) expect(udpAddr.toString()).to.equal(udpAddrStr) - expect(udpAddr.buffer).to.deep.equal(udpAddrBuf) + expect(udpAddr.bytes).to.deep.equal(udpAddrBuf) expect(udpAddr.protoCodes()).to.deep.equal([4, 273]) expect(udpAddr.protoNames()).to.deep.equal(['ip4', 'udp']) 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 udpAddrBuf2 = udpAddr.encapsulate('/udp/5678') - expect(udpAddrBuf2.toString()).to.equal('/ip4/127.0.0.1/udp/1234/udp/5678') - expect(udpAddrBuf2.decapsulate('/udp').toString()).to.equal('/ip4/127.0.0.1/udp/1234') - expect(udpAddrBuf2.decapsulate('/ip4').toString()).to.equal('/') + const udpAddrbytes2 = udpAddr.encapsulate('/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(multiaddr('/').encapsulate(udpAddr).toString()).to.equal(udpAddr.toString()) expect(multiaddr('/').decapsulate('/').toString()).to.equal('/') @@ -184,63 +182,63 @@ describe('variants', () => { it('ip4', () => { const str = '/ip4/127.0.0.1' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + tcp', () => { const str = '/ip4/127.0.0.1/tcp/5000' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + tcp', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/5000' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + udp', () => { const str = '/ip4/127.0.0.1/udp/5000' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + udp', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/5000' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + p2p', () => { const str = '/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + ipfs', () => { const str = '/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str.replace('/ipfs/', '/p2p/')) }) it('ip6 + p2p', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) @@ -253,14 +251,14 @@ describe('variants', () => { it('ip4 + udp + utp', () => { const str = '/ip4/127.0.0.1/udp/5000/utp' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + udp + utp', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/5000/utp' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.protoNames()) expect(addr.toString()).to.equal(str) }) @@ -268,119 +266,119 @@ describe('variants', () => { it('ip4 + tcp + http', () => { const str = '/ip4/127.0.0.1/tcp/8000/http' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + tcp + unix', () => { const str = '/ip4/127.0.0.1/tcp/80/unix/a/b/c/d/e/f' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + tcp + http', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/http' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + tcp + unix', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/unix/a/b/c/d/e/f' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + tcp + https', () => { const str = '/ip4/127.0.0.1/tcp/8000/https' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + tcp + https', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/https' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip4 + tcp + websockets', () => { const str = '/ip4/127.0.0.1/tcp/8000/ws' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + tcp + websockets', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + tcp + websockets + ipfs', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str.replace('/ipfs/', '/p2p/')) }) it('ip6 + tcp + websockets + p2p', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('ip6 + udp + quic + ipfs', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/4001/quic/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str.replace('/ipfs/', '/p2p/')) }) it('ip6 + udp + quic + p2p', () => { const str = '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/4001/quic/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('unix', () => { const str = '/unix/a/b/c/d/e' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('p2p', () => { const str = '/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('p2p', () => { const str = '/p2p/bafzbeidt255unskpefjmqb2rc27vjuyxopkxgaylxij6pw35hhys4vnyp4' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal('/p2p/QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t') }) it('ipfs', () => { const str = '/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str.replace('/ipfs/', '/p2p/')) }) it('onion', () => { const str = '/onion/timaq4ygg2iegci7:1234' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) @@ -402,7 +400,7 @@ describe('variants', () => { it('onion3', () => { const str = '/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) @@ -424,56 +422,56 @@ describe('variants', () => { it('p2p-circuit', () => { const str = '/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('p2p-circuit p2p', () => { const str = '/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/p2p-circuit' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('p2p-circuit ipfs', () => { const str = '/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/p2p-circuit' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str.replace('/ipfs/', '/p2p/')) }) it('p2p-webrtc-star', () => { const str = '/ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('p2p-webrtc-star ipfs', () => { const str = '/ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str.replace('/ipfs/', '/p2p/')) }) it('p2p-webrtc-direct', () => { const str = '/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('p2p-websocket-star', () => { const str = '/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) it('memory + p2p', () => { const str = '/memory/test/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str) - expect(addr).to.have.property('buffer') + expect(addr).to.have.property('bytes') expect(addr.toString()).to.equal(str) }) }) @@ -611,7 +609,7 @@ describe('helpers', () => { it('returns the tuples', () => { expect(multiaddr('/ip4/0.0.0.0/utp').tuples()) .to.eql([ - [4, Buffer.from([0, 0, 0, 0])], + [4, Uint8Array.from([0, 0, 0, 0])], [302] ]) }) @@ -863,7 +861,7 @@ describe('helpers', () => { expect(multiaddr.isMultiaddr('/')).to.be.eql(false) expect(multiaddr.isMultiaddr(123)).to.be.eql(false) - expect(multiaddr.isMultiaddr(Buffer.from('/hello'))).to.be.eql(false) + expect(multiaddr.isMultiaddr(uint8ArrayFromString('/hello'))).to.be.eql(false) }) }) diff --git a/test/protocols.spec.js b/test/protocols.spec.js index 586190b8..b5b53016 100644 --- a/test/protocols.spec.js +++ b/test/protocols.spec.js @@ -2,10 +2,7 @@ 'use strict' const protocols = require('../src/protocols-table') -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) +const { expect } = require('aegir/utils/chai') describe('protocols', () => { describe('throws on non existent protocol', () => { From ebcb3433689cd5b8ca02e48115103e62abdbf2b5 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 7 Aug 2020 15:40:46 +0100 Subject: [PATCH 2/2] chore: downgrade aegir --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2b2b738f..371dc7e3 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@types/dirty-chai": "^2.0.2", "@types/mocha": "^8.0.1", "@types/node": "^14.0.11", - "aegir": "^25.0.0", + "aegir": "^22.0.0", "typescript": "^3.9.5" }, "contributors": [