From 688a071c2ba70b2d6b7f5ae2fdd80d3c0a6b20b3 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 31 Jul 2020 15:08:29 +0100 Subject: [PATCH] fix: replace node buffers with uint8arrays All useages of node `Buffer`s have been replaced with `Uint8Array`s. BREAKING CHANGES: - Where node `Buffer`s were returned, they are now `Uint8Array`s --- README.md | 8 +++++--- example.js | 2 +- package.json | 4 +--- src/index.js | 24 ++++++++++++------------ src/util.js | 30 ++++++++++++++++-------------- src/varint-table.js | 2 +- test/multicodec.spec.js | 22 ++++++++++------------ 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 4ddf066..7345ef4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# js-multicodec +# js-multicodec [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai) [![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats) @@ -9,14 +9,16 @@ > JavaScript implementation of the multicodec specification -## Lead Maintainer +## Lead Maintainer [Henrique Dias](http://github.com/hacdias) -## Table of Contents +## Table of Contents - [Install](#install) - [Usage](#usage) + - [Example](#example) + - [API](#api) - [Updating the lookup table](#updating-the-lookup-table) - [Contribute](#contribute) - [License](#license) diff --git a/example.js b/example.js index 3c0b39b..7413d49 100644 --- a/example.js +++ b/example.js @@ -2,7 +2,7 @@ const multicodec = require('multicodec') -const prefixedProtobuf = multicodec.addPrefix('protobuf', Buffer.from('some protobuf code')) +const prefixedProtobuf = multicodec.addPrefix('protobuf', new TextEncoder().encode('some protobuf code')) // eslint-disable-next-line no-console console.log(prefixedProtobuf) diff --git a/package.json b/package.json index 72c2ec7..71a41dd 100644 --- a/package.json +++ b/package.json @@ -41,14 +41,12 @@ }, "homepage": "https://github.com/multiformats/js-multicodec#readme", "dependencies": { - "buffer": "^5.6.0", + "uint8arrays": "0.0.2", "varint": "^5.0.0" }, "devDependencies": { "aegir": "^23.0.0", "bent": "^7.3.4", - "chai": "^4.2.0", - "dirty-chai": "^2.0.1", "pre-push": "~0.1.1" }, "contributors": [ diff --git a/src/index.js b/src/index.js index 8854a71..c060c12 100644 --- a/src/index.js +++ b/src/index.js @@ -11,11 +11,11 @@ */ 'use strict' -const { Buffer } = require('buffer') const varint = require('varint') const intTable = require('./int-table') const codecNameToCodeVarint = require('./varint-table') const util = require('./util') +const uint8ArrayConcat = require('uint8arrays/concat') exports = module.exports @@ -23,14 +23,14 @@ exports = module.exports * Prefix a buffer with a multicodec-packed. * * @param {string|number} multicodecStrOrCode - * @param {Buffer} data - * @returns {Buffer} + * @param {Uint8Array} data + * @returns {Uint8Array} */ exports.addPrefix = (multicodecStrOrCode, data) => { let prefix - if (Buffer.isBuffer(multicodecStrOrCode)) { - prefix = util.varintBufferEncode(multicodecStrOrCode) + if (multicodecStrOrCode instanceof Uint8Array) { + prefix = util.varintUint8ArrayEncode(multicodecStrOrCode) } else { if (codecNameToCodeVarint[multicodecStrOrCode]) { prefix = codecNameToCodeVarint[multicodecStrOrCode] @@ -38,14 +38,14 @@ exports.addPrefix = (multicodecStrOrCode, data) => { throw new Error('multicodec not recognized') } } - return Buffer.concat([prefix, data]) + return uint8ArrayConcat([prefix, data], prefix.length + data.length) } /** * Decapsulate the multicodec-packed prefix from the data. * - * @param {Buffer} data - * @returns {Buffer} + * @param {Uint8Array} data + * @returns {Uint8Array} */ exports.rmPrefix = (data) => { varint.decode(data) @@ -54,7 +54,7 @@ exports.rmPrefix = (data) => { /** * Get the codec of the prefixed data. - * @param {Buffer} prefixedData + * @param {Uint8Array} prefixedData * @returns {string} */ exports.getCodec = (prefixedData) => { @@ -85,12 +85,12 @@ exports.getNumber = (name) => { if (code === undefined) { throw new Error('Codec `' + name + '` not found') } - return util.varintBufferDecode(code)[0] + return util.varintUint8ArrayDecode(code)[0] } /** * Get the code of the prefixed data. - * @param {Buffer} prefixedData + * @param {Uint8Array} prefixedData * @returns {number} */ exports.getCode = (prefixedData) => { @@ -100,7 +100,7 @@ exports.getCode = (prefixedData) => { /** * Get the code as varint of a codec name. * @param {string} codecName - * @returns {Buffer} + * @returns {Uint8Array} */ exports.getCodeVarint = (codecName) => { const code = codecNameToCodeVarint[codecName] diff --git a/src/util.js b/src/util.js index 8ed4fb5..bde8c0d 100644 --- a/src/util.js +++ b/src/util.js @@ -1,35 +1,37 @@ 'use strict' + const varint = require('varint') -const { Buffer } = require('buffer') +const uint8ArrayToString = require('uint8arrays/to-string') +const uint8ArrayFromString = require('uint8arrays/from-string') module.exports = { - numberToBuffer, - bufferToNumber, - varintBufferEncode, - varintBufferDecode, + numberToUint8Array, + uint8ArrayToNumber, + varintUint8ArrayEncode, + varintUint8ArrayDecode, varintEncode } -function bufferToNumber (buf) { - return parseInt(buf.toString('hex'), 16) +function uint8ArrayToNumber (buf) { + return parseInt(uint8ArrayToString(buf, 'base16'), 16) } -function numberToBuffer (num) { +function numberToUint8Array (num) { let hexString = num.toString(16) if (hexString.length % 2 === 1) { hexString = '0' + hexString } - return Buffer.from(hexString, 'hex') + return uint8ArrayFromString(hexString, 'base16') } -function varintBufferEncode (input) { - return Buffer.from(varint.encode(bufferToNumber(input))) +function varintUint8ArrayEncode (input) { + return Uint8Array.from(varint.encode(uint8ArrayToNumber(input))) } -function varintBufferDecode (input) { - return numberToBuffer(varint.decode(input)) +function varintUint8ArrayDecode (input) { + return numberToUint8Array(varint.decode(input)) } function varintEncode (num) { - return Buffer.from(varint.encode(num)) + return Uint8Array.from(varint.encode(num)) } diff --git a/src/varint-table.js b/src/varint-table.js index 21af1b8..30b647e 100644 --- a/src/varint-table.js +++ b/src/varint-table.js @@ -3,7 +3,7 @@ const baseTable = require('./base-table.json') const varintEncode = require('./util').varintEncode -// map for codecName -> codeVarintBuffer +// map for codecName -> codeVarintUint8Array const varintTable = {} for (const encodingName in baseTable) { diff --git a/test/multicodec.spec.js b/test/multicodec.spec.js index 439794e..b45635b 100644 --- a/test/multicodec.spec.js +++ b/test/multicodec.spec.js @@ -1,29 +1,27 @@ /* eslint-env mocha */ 'use strict' -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) +const { expect } = require('aegir/utils/chai') const multicodec = require('../src') +const uint8ArrayFromString = require('uint8arrays/from-string') describe('multicodec', () => { it('add prefix through multicodec (string)', () => { - const buf = Buffer.from('hey') + const buf = uint8ArrayFromString('hey') const prefixedBuf = multicodec.addPrefix('protobuf', buf) expect(multicodec.getCodec(prefixedBuf)).to.equal('protobuf') expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf)) }) it('add prefix through code (code)', () => { - const buf = Buffer.from('hey') - const prefixedBuf = multicodec.addPrefix(Buffer.from('70', 'hex'), buf) + const buf = uint8ArrayFromString('hey') + const prefixedBuf = multicodec.addPrefix(uint8ArrayFromString('70', 'base16'), buf) expect(multicodec.getCodec(prefixedBuf)).to.equal('dag-pb') expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf)) }) it('add multibyte varint prefix (eth-block) through multicodec (string)', () => { - const buf = Buffer.from('hey') + const buf = uint8ArrayFromString('hey') const prefixedBuf = multicodec.addPrefix('eth-block', buf) expect(multicodec.getCodec(prefixedBuf)).to.equal('eth-block') expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf)) @@ -31,11 +29,11 @@ describe('multicodec', () => { it('returns code via codec name', () => { const code = multicodec.getCodeVarint('keccak-256') - expect(code).to.eql(Buffer.from('1b', 'hex')) + expect(code).to.eql(uint8ArrayFromString('1b', 'base16')) }) it('returns code from prefixed data', () => { - const buf = Buffer.from('hey') + const buf = uint8ArrayFromString('hey') const prefixedBuf = multicodec.addPrefix('dag-cbor', buf) const code = multicodec.getCode(prefixedBuf) expect(code).to.eql(multicodec.DAG_CBOR) @@ -92,9 +90,9 @@ describe('multicodec', () => { }) it('throws error on unknown codec name when getting the codec', () => { - const code = Buffer.from('ffee', 'hex') + const code = uint8ArrayFromString('ffee', 'base16') - const buf = Buffer.from('hey') + const buf = uint8ArrayFromString('hey') const prefixedBuf = multicodec.addPrefix(code, buf) expect(() => { multicodec.getCodec(prefixedBuf)