Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

feat: accept Uint8Arrays input in place of Buffers #88

Merged
merged 8 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 42 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/* eslint-disable guard-for-in */
/**
* Multihash implementation in JavaScript.
Expand All @@ -10,6 +11,9 @@ const { Buffer } = require('buffer')
const multibase = require('multibase')
const varint = require('varint')
const { names } = require('./constants')
const textDecoder = typeof TextDecoder !== 'undefined'
? new TextDecoder()
: new (require('util').TextDecoder)()

const codes = {}

Expand All @@ -22,15 +26,19 @@ exports.codes = Object.freeze(codes)
/**
* Convert the given multihash to a hex encoded string.
*
* @param {Buffer} hash
* @param {Uint8Array} hash
Gozala marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string}
*/
exports.toHexString = function toHexString (hash) {
if (!Buffer.isBuffer(hash)) {
throw new Error('must be passed a buffer')
if (!(hash instanceof Uint8Array)) {
throw new Error('must be passed an Uint8Array')
Gozala marked this conversation as resolved.
Show resolved Hide resolved
}

return hash.toString('hex')
const buffer = Buffer.isBuffer(hash)
? hash
: Buffer.from(hash.buffer, hash.byteOffset, hash.byteLength)

return buffer.toString('hex')
}

/**
Expand All @@ -46,42 +54,44 @@ exports.fromHexString = function fromHexString (hash) {
/**
* Convert the given multihash to a base58 encoded string.
*
* @param {Buffer} hash
* @param {Uint8Array} hash
* @returns {string}
*/
exports.toB58String = function toB58String (hash) {
if (!Buffer.isBuffer(hash)) {
throw new Error('must be passed a buffer')
if (!(hash instanceof Uint8Array)) {
throw new Error('must be passed an Uint8Array')
}

return multibase.encode('base58btc', hash).toString().slice(1)
return textDecoder.decode(multibase.encode('base58btc', hash)).slice(1)
}

/**
* Convert the given base58 encoded string to a multihash.
*
* @param {string|Buffer} hash
* @param {string|Uint8Array} hash
* @returns {Buffer}
*/
exports.fromB58String = function fromB58String (hash) {
let encoded = hash
if (Buffer.isBuffer(hash)) {
encoded = hash.toString()
}
const encoded = hash instanceof Uint8Array
? textDecoder.decode(hash)
: hash

return multibase.decode('z' + encoded)
}

/**
* Decode a hash from the given multihash.
*
* @param {Buffer} buf
* @param {Uint8Array} bytes
* @returns {{code: number, name: string, length: number, digest: Buffer}} result
*/
exports.decode = function decode (buf) {
if (!(Buffer.isBuffer(buf))) {
throw new Error('multihash must be a Buffer')
exports.decode = function decode (bytes) {
if (!(bytes instanceof Uint8Array)) {
throw new Error('multihash must be an Uint8Array')
}
let buf = Buffer.isBuffer(bytes)
? bytes
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)

if (buf.length < 2) {
throw new Error('multihash too short. must be > 2 bytes.')
Expand Down Expand Up @@ -116,7 +126,7 @@ exports.decode = function decode (buf) {
*
* > **Note:** the length is derived from the length of the digest itself.
*
* @param {Buffer} digest
* @param {Uint8Array} digest
* @param {string|number} code
* @param {number} [length]
* @returns {Buffer}
Expand All @@ -129,8 +139,8 @@ exports.encode = function encode (digest, code, length) {
// ensure it's a hashfunction code.
const hashfn = exports.coerceCode(code)

if (!(Buffer.isBuffer(digest))) {
throw new Error('digest should be a Buffer')
if (!(digest instanceof Uint8Array)) {
throw new Error('digest should be an Uint8Array')
}

if (length == null) {
Expand All @@ -141,11 +151,13 @@ exports.encode = function encode (digest, code, length) {
throw new Error('digest length should be equal to specified length.')
}

return Buffer.concat([
Buffer.from(varint.encode(hashfn)),
Buffer.from(varint.encode(length)),
digest
])
const hash = varint.encode(hashfn)
const len = varint.encode(length)
const buffer = Buffer.alloc(hash.length + len.length + digest.length)
buffer.set(hash, 0)
buffer.set(len, hash.length)
buffer.set(digest, hash.length + len.length)
return buffer
}

/**
Expand Down Expand Up @@ -206,8 +218,8 @@ exports.isValidCode = function validCode (code) {
/**
* Check if the given buffer is a valid multihash. Throws an error if it is not valid.
*
* @param {Buffer} multihash
* @returns {undefined}
* @param {Uint8Array} multihash
* @returns {void}
* @throws {Error}
*/
function validate (multihash) {
Expand All @@ -218,12 +230,12 @@ exports.validate = validate
/**
* Returns a prefix from a valid multihash. Throws an error if it is not valid.
*
* @param {Buffer} multihash
* @returns {undefined}
* @param {Uint8Array} multihash
* @returns {Buffer}
* @throws {Error}
*/
exports.prefix = function prefix (multihash) {
validate(multihash)

return multihash.slice(0, 2)
return Buffer.from(multihash.buffer, multihash.byteOffset, 2)
}
13 changes: 13 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'
Gozala marked this conversation as resolved.
Show resolved Hide resolved

const textDecoder = typeof TextDecoder !== 'undefined'
? new TextDecoder()
: new (require('util').TextDecoder)()

/**
* @param {Uint8Arary} bytes
* @returns {string}
*/
const decodeText = (bytes) => textDecoder.decode(bytes)

module.exports = { decodeText }
42 changes: 26 additions & 16 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const mh = require('../src')
const constants = require('../src/constants')
const validCases = require('./fixtures/valid')
const invalidCases = require('./fixtures/invalid')
const textEncoder = typeof TextEncoder !== 'undefined'
Gozala marked this conversation as resolved.
Show resolved Hide resolved
? new TextEncoder()
: new (require('util').TextEncoder)()

function sample (code, size, hex) {
const toHex = (i) => {
Expand All @@ -24,12 +27,19 @@ function sample (code, size, hex) {
return Buffer.from(`${toHex(code)}${toHex(size)}${hex}`, 'hex')
}

const encodeHex = string => {
const { buffer, byteOffset, byteLength } = Buffer.from(string, 'hex')
return new Uint8Array(buffer, byteOffset, byteLength)
}

const encodeText = string => textEncoder.encode(string)

describe('multihash', () => {
describe('toHexString', () => {
it('valid', () => {
validCases.forEach((test) => {
const code = test.encoding.code
const buf = mh.encode(Buffer.from(test.hex, 'hex'), code)
const buf = mh.encode(encodeHex(test.hex), code)
expect(
mh.toHexString(buf)
).to.be.eql(
Expand All @@ -42,7 +52,7 @@ describe('multihash', () => {
expect(
() => mh.toHexString('hello world')
).to.throw(
/must be passed a buffer/
/must be passed an Uint8Array/
)
})
})
Expand All @@ -51,7 +61,7 @@ describe('multihash', () => {
it('valid', () => {
validCases.forEach((test) => {
const code = test.encoding.code
const buf = mh.encode(Buffer.from(test.hex, 'hex'), code)
const buf = mh.encode(encodeHex(test.hex), code)
expect(
mh.fromHexString(buf.toString('hex')).toString('hex')
).to.be.eql(
Expand All @@ -65,7 +75,7 @@ describe('multihash', () => {
it('valid', () => {
validCases.forEach((test) => {
const code = test.encoding.code
const buf = mh.encode(Buffer.from(test.hex, 'hex'), code)
const buf = mh.encode(encodeHex(test.hex, 'hex'), code)
expect(
mh.toB58String(buf)
).to.be.eql(
Expand All @@ -78,15 +88,15 @@ describe('multihash', () => {
expect(
() => mh.toB58String('hello world')
).to.throw(
/must be passed a buffer/
/must be passed an Uint8Array/
Gozala marked this conversation as resolved.
Show resolved Hide resolved
)
})
})

describe('fromB58String', () => {
it('valid', () => {
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
const expected = Buffer.from('122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461', 'hex')
const expected = encodeHex('122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461')

expect(
mh.fromB58String(src)
Expand All @@ -95,7 +105,7 @@ describe('multihash', () => {
)

expect(
mh.fromB58String(Buffer.from(src))
mh.fromB58String(encodeText(src))
).to.be.eql(
expected
)
Expand Down Expand Up @@ -125,7 +135,7 @@ describe('multihash', () => {
expect(
() => mh.decode('hello')
).to.throw(
/multihash must be a Buffer/
/multihash must be an Uint8Array/
)
})
})
Expand All @@ -137,8 +147,8 @@ describe('multihash', () => {
const name = test.encoding.name
const buf = sample(test.encoding.varint || code, test.size, test.hex)
const results = [
mh.encode(Buffer.from(test.hex, 'hex'), code),
mh.encode(Buffer.from(test.hex, 'hex'), name)
mh.encode(encodeHex(test.hex), code),
mh.encode(encodeHex(test.hex), name)
]

results.forEach((res) => {
Expand All @@ -161,11 +171,11 @@ describe('multihash', () => {
expect(
() => mh.encode('hello', 0x11)
).to.throw(
/digest should be a Buffer/
/digest should be an Uint8Array/
)

expect(
() => mh.encode(Buffer.from('hello'), 0x11, 2)
() => mh.encode(encodeText('hello'), 0x11, 2)
).to.throw(
/length should be equal/
)
Expand All @@ -188,7 +198,7 @@ describe('multihash', () => {
).to.throw()
})

const longBuffer = Buffer.alloc(150, 'a')
const longBuffer = Uint8Array.from(Buffer.alloc(150, 'a'))
expect(
() => mh.validate(longBuffer)
).to.throw()
Expand Down Expand Up @@ -293,7 +303,7 @@ describe('multihash', () => {
})

expect(
() => mh.coerceCode(Buffer.from('hello'))
() => mh.coerceCode(encodeText('hello'))
).to.throw(
/should be a number/
)
Expand All @@ -307,13 +317,13 @@ describe('multihash', () => {
})

it('prefix', () => {
const multihash = mh.encode(Buffer.from('hey'), 0x11, 3)
const multihash = mh.encode(encodeText('hey'), 0x11, 3)
const prefix = mh.prefix(multihash)
expect(prefix.toString('hex')).to.eql('1103')
})

it('prefix throws on invalid multihash', () => {
const multihash = Buffer.from('definitely not valid')
const multihash = encodeText('definitely not valid')

expect(() => mh.prefix(multihash)).to.throw()
})
Expand Down