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

Commit 7eecbf2

Browse files
committed
Return Uint8Arrays instead of node Buffers.
BREAKING CHANGE: - node `Buffer`s are not returned any more, only `Uint8Array`s
1 parent 47dfd56 commit 7eecbf2

11 files changed

+92
-82
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
},
3636
"dependencies": {
3737
"base-x": "^3.0.8",
38-
"buffer": "^5.5.0",
3938
"web-encoding": "^1.0.2"
4039
},
4140
"devDependencies": {

src/base.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
'use strict'
3-
const { Buffer } = require('buffer')
3+
4+
const { encodeText } = require('./util')
45

56
/**
67
* @typedef {Object} Codec
@@ -20,7 +21,7 @@ class Base {
2021
constructor (name, code, implementation, alphabet) {
2122
this.name = name
2223
this.code = code
23-
this.codeBuf = Buffer.from(this.code)
24+
this.codeBuf = encodeText(this.code)
2425
this.alphabet = alphabet
2526
this.engine = implementation(alphabet)
2627
}

src/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,56 @@
66
*/
77
'use strict'
88

9-
const { Buffer } = require('buffer')
109
const constants = require('./constants')
11-
const { decodeText, asBuffer } = require('./util')
10+
const { encodeText, decodeText, concat } = require('./util')
1211

1312
/** @typedef {import("./base")} Base */
1413

1514
/**
16-
* Create a new buffer with the multibase varint+code.
15+
* Create a new Uint8Array with the multibase varint+code.
1716
*
1817
* @param {string|number} nameOrCode - The multibase name or code number.
1918
* @param {Uint8Array} buf - The data to be prefixed with multibase.
20-
* @returns {Buffer}
19+
* @returns {Uint8Array}
2120
* @throws {Error} Will throw if the encoding is not supported
2221
*/
2322
function multibase (nameOrCode, buf) {
2423
if (!buf) {
25-
throw new Error('requires an encoded buffer')
24+
throw new Error('requires an encoded Uint8Array')
2625
}
2726
const { name, codeBuf } = encoding(nameOrCode)
2827
validEncode(name, buf)
2928

30-
const buffer = Buffer.alloc(codeBuf.length + buf.length)
31-
buffer.set(codeBuf, 0)
32-
buffer.set(buf, codeBuf.length)
33-
34-
return buffer
29+
return concat([codeBuf, buf], codeBuf.length + buf.length)
3530
}
3631

3732
/**
3833
* Encode data with the specified base and add the multibase prefix.
3934
*
4035
* @param {string|number} nameOrCode - The multibase name or code number.
4136
* @param {Uint8Array} buf - The data to be encoded.
42-
* @returns {Buffer}
37+
* @returns {Uint8Array}
4338
* @throws {Error} Will throw if the encoding is not supported
4439
*
4540
*/
4641
function encode (nameOrCode, buf) {
4742
const enc = encoding(nameOrCode)
43+
const data = encodeText(enc.encode(buf))
4844

49-
return Buffer.concat([enc.codeBuf, Buffer.from(enc.encode(buf))])
45+
return concat([enc.codeBuf, data], enc.codeBuf.length + data.length)
5046
}
5147

5248
/**
5349
* Takes a Uint8Array or string encoded with multibase header, decodes it and
5450
* returns the decoded buffer
5551
*
5652
* @param {Uint8Array|string} data
57-
* @returns {Buffer}
53+
* @returns {Uint8Array}
5854
* @throws {Error} Will throw if the encoding is not supported
5955
*
6056
*/
6157
function decode (data) {
62-
if (ArrayBuffer.isView(data)) {
58+
if (data instanceof Uint8Array) {
6359
data = decodeText(data)
6460
}
6561
const prefix = data[0]
@@ -69,7 +65,7 @@ function decode (data) {
6965
data = data.toLowerCase()
7066
}
7167
const enc = encoding(data[0])
72-
return asBuffer(enc.decode(data.substring(1)))
68+
return enc.decode(data.substring(1))
7369
}
7470

7571
/**

src/util.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @ts-check
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const { TextEncoder, TextDecoder } = require('web-encoding')
65

76
const textDecoder = new TextDecoder()
@@ -19,10 +18,26 @@ const textEncoder = new TextEncoder()
1918
const encodeText = (text) => textEncoder.encode(text)
2019

2120
/**
22-
* @param {ArrayBufferView} bytes
23-
* @returns {Buffer}
21+
* Returns a new Uint8Array created by concatenating the passed Arrays
22+
*
23+
* @param {Array<ArrayLike<Number>>} arrs
24+
* @param {Number} [length]
25+
* @returns {Uint8Array}
2426
*/
25-
const asBuffer = ({ buffer, byteLength, byteOffset }) =>
26-
Buffer.from(buffer, byteOffset, byteLength)
27+
function concat (arrs, length) {
28+
if (!length) {
29+
length = arrs.reduce((acc, curr) => acc + curr.length, 0)
30+
}
31+
32+
const output = new Uint8Array(length)
33+
let offset = 0
34+
35+
for (const arr of arrs) {
36+
output.set(arr, offset)
37+
offset += arr.length
38+
}
39+
40+
return output
41+
}
2742

28-
module.exports = { decodeText, encodeText, asBuffer }
43+
module.exports = { decodeText, encodeText, concat }

test/multibase.spec.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
'use strict'
33

44
const { expect } = require('aegir/utils/chai')
5-
const { Buffer } = require('buffer')
6-
const { encodeText } = require('../src/util')
5+
const { encodeText, decodeText } = require('../src/util')
76
const multibase = require('../src')
87
const constants = require('../src/constants.js')
98

109
const unsupportedBases = []
1110

1211
const supportedBases = [
1312

14-
['base16', Buffer.from([0x01]).toString(), 'f01'],
15-
['base16', Buffer.from([15]).toString(), 'f0f'],
13+
['base16', decodeText(Uint8Array.from([0x01])), 'f01'],
14+
['base16', decodeText(Uint8Array.from([15])), 'f0f'],
1615
['base16', 'f', 'f66'],
1716
['base16', 'fo', 'f666f'],
1817
['base16', 'foo', 'f666f6f'],
@@ -85,7 +84,6 @@ const supportedBases = [
8584
]
8685

8786
const they = (label, def) => {
88-
it(`${label} (Buffer)`, def.bind(null, Buffer.from))
8987
it(`${label} (Uint8Array)`, def.bind(null, encodeText))
9088
}
9189

@@ -120,27 +118,27 @@ describe('multibase', () => {
120118
const output = elements[2]
121119
const base = constants.names[name]
122120
describe(name, () => {
123-
they('adds multibase code to valid encoded buffer, by name', (encode) => {
121+
they('adds multibase code to valid encoded Uint8Array, by name', (encode) => {
124122
if (typeof input === 'string') {
125123
const buf = encode(input)
126124
const encodedBuf = encode(base.encode(buf))
127125
const multibasedBuf = multibase(base.name, encodedBuf)
128-
expect(multibasedBuf.toString()).to.equal(output)
126+
expect(decodeText(multibasedBuf)).to.equal(output)
129127
} else {
130128
const encodedBuf = encode(base.encode(input))
131129
const multibasedBuf = multibase(base.name, encodedBuf)
132-
expect(multibasedBuf.toString()).to.equal(output)
130+
expect(decodeText(multibasedBuf)).to.equal(output)
133131
}
134132
})
135133

136-
they('adds multibase code to valid encoded buffer, by code', (encode) => {
134+
they('adds multibase code to valid encoded Uint8Array, by code', (encode) => {
137135
const buf = encode(input)
138136
const encodedBuf = encode(base.encode(buf))
139137
const multibasedBuf = multibase(base.code, encodedBuf)
140-
expect(multibasedBuf.toString()).to.equal(output)
138+
expect(decodeText(multibasedBuf)).to.equal(output)
141139
})
142140

143-
they('fails to add multibase code to invalid encoded buffer', (encode) => {
141+
they('fails to add multibase code to invalid encoded Uint8Array', (encode) => {
144142
const nonEncodedBuf = encode('^!@$%!#$%@#y')
145143
expect(() => {
146144
multibase(base.name, nonEncodedBuf)
@@ -152,7 +150,7 @@ describe('multibase', () => {
152150
expect(name).to.equal(base.name)
153151
})
154152

155-
they('isEncoded buffer', (encode) => {
153+
they('isEncoded Uint8Array', (encode) => {
156154
const multibasedStr = encode(output)
157155
const name = multibase.isEncoded(multibasedStr)
158156
expect(name).to.equal(base.name)
@@ -167,10 +165,10 @@ describe('multibase.encode ', () => {
167165
const input = elements[1]
168166
const output = elements[2]
169167
describe(name, () => {
170-
they('encodes a buffer', (encode) => {
168+
they('encodes a Uint8Array', (encode) => {
171169
const buf = encode(input)
172170
const multibasedBuf = multibase.encode(name, buf)
173-
expect(multibasedBuf.toString()).to.equal(output)
171+
expect(decodeText(multibasedBuf)).to.equal(output)
174172
})
175173
})
176174
}
@@ -180,7 +178,7 @@ describe('multibase.encode ', () => {
180178
const decoded = multibase.decode(encodedStr)
181179

182180
const encoded = multibase.encode('c', decoded)
183-
expect(encodedStr).to.be.eq(encoded.toString())
181+
expect(encodedStr).to.be.eq(decodeText(encoded))
184182
})
185183
})
186184

@@ -193,13 +191,13 @@ describe('multibase.decode', () => {
193191
it('decodes a string', () => {
194192
const multibasedStr = output
195193
const buf = multibase.decode(multibasedStr)
196-
expect(buf).to.eql(Buffer.from(input))
194+
expect(buf).to.eql(encodeText(input))
197195
})
198196

199-
they('decodes a buffer', (encode) => {
197+
they('decodes a Uint8Array', (encode) => {
200198
const multibasedBuf = encode(output)
201199
const buf = multibase.decode(multibasedBuf)
202-
expect(buf).to.eql(Buffer.from(input))
200+
expect(buf).to.eql(encodeText(input))
203201
})
204202
})
205203
}
@@ -241,7 +239,7 @@ describe('multibase.codes', () => {
241239
})
242240

243241
describe('multibase.isEncoded', () => {
244-
it('should not throw for non string/buffer input', () => {
242+
it('should not throw for non String/Uint8Array input', () => {
245243
const invalidInputs = [
246244
null,
247245
undefined,

test/spec-test1.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
4+
const { decodeText, encodeText } = require('../src/util')
55
const { expect } = require('aegir/utils/chai')
66
const multibase = require('../src')
77
const constants = require('../src/constants.js')
@@ -37,33 +37,33 @@ describe('spec test1', () => {
3737
const base = constants.names[name]
3838

3939
describe(name, () => {
40-
it('should encode buffer by base name', () => {
41-
const out = multibase.encode(name, Buffer.from(input))
42-
expect(out.toString()).to.equal(output)
40+
it('should encode Uint8Array by base name', () => {
41+
const out = multibase.encode(name, encodeText(input))
42+
expect(decodeText(out)).to.equal(output)
4343
})
4444

45-
it('should encode buffer by base code', () => {
46-
const out = multibase.encode(base.code, Buffer.from(input))
47-
expect(out.toString()).to.equal(output)
45+
it('should encode Uint8Array by base code', () => {
46+
const out = multibase.encode(base.code, encodeText(input))
47+
expect(decodeText(out)).to.equal(output)
4848
})
4949

5050
it('should decode string', () => {
5151
const out = multibase.decode(output)
52-
expect(out.toString()).to.equal(input)
52+
expect(decodeText(out)).to.equal(input)
5353
})
5454

55-
it('should prefix encoded buffer', () => {
55+
it('should prefix encoded Uint8Array', () => {
5656
const base = constants.names[name]
57-
const data = base.encode(Buffer.from(input))
57+
const data = base.encode(encodeText(input))
5858

59-
expect(multibase(name, Buffer.from(data)).toString()).to.equal(output)
59+
expect(decodeText(multibase(name, encodeText(data)))).to.equal(output)
6060
})
6161

6262
it('should fail decode with invalid char', function () {
6363
if (name === 'identity') {
6464
return this.skip()
6565
}
66-
const nonEncodedBuf = Buffer.from(base.code + '^!@$%!#$%@#y')
66+
const nonEncodedBuf = encodeText(base.code + '^!@$%!#$%@#y')
6767
expect(() => {
6868
multibase.decode(nonEncodedBuf)
6969
}).to.throw(Error, 'invalid character \'^\' in \'^!@$%!#$%@#y\'')

test/spec-test2.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
4+
const { decodeText, encodeText } = require('../src/util')
55
const { expect } = require('aegir/utils/chai')
66
const multibase = require('../src')
77
const constants = require('../src/constants.js')
@@ -38,18 +38,18 @@ describe('spec test2', () => {
3838

3939
describe(name, () => {
4040
it('should encode buffer by base name', () => {
41-
const out = multibase.encode(name, Buffer.from(input))
42-
expect(out.toString()).to.equal(output)
41+
const out = multibase.encode(name, encodeText(input))
42+
expect(decodeText(out)).to.equal(output)
4343
})
4444

4545
it('should encode buffer by base code', () => {
46-
const out = multibase.encode(base.code, Buffer.from(input))
47-
expect(out.toString()).to.equal(output)
46+
const out = multibase.encode(base.code, encodeText(input))
47+
expect(decodeText(out)).to.equal(output)
4848
})
4949

5050
it('should decode string', () => {
5151
const out = multibase.decode(output)
52-
expect(out.toString()).to.equal(input)
52+
expect(decodeText(out)).to.equal(input)
5353
})
5454
})
5555
}

test/spec-test3.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
4+
const { decodeText, encodeText } = require('../src/util')
55
const { expect } = require('aegir/utils/chai')
66
const multibase = require('../src')
77
const constants = require('../src/constants.js')
@@ -38,18 +38,18 @@ describe('spec test3', () => {
3838

3939
describe(name, () => {
4040
it('should encode buffer by base name', () => {
41-
const out = multibase.encode(name, Buffer.from(input))
42-
expect(out.toString()).to.equal(output)
41+
const out = multibase.encode(name, encodeText(input))
42+
expect(decodeText(out)).to.equal(output)
4343
})
4444

4545
it('should encode buffer by base code', () => {
46-
const out = multibase.encode(base.code, Buffer.from(input))
47-
expect(out.toString()).to.equal(output)
46+
const out = multibase.encode(base.code, encodeText(input))
47+
expect(decodeText(out)).to.equal(output)
4848
})
4949

5050
it('should decode string', () => {
5151
const out = multibase.decode(output)
52-
expect(out.toString()).to.equal(input)
52+
expect(decodeText(out)).to.equal(input)
5353
})
5454
})
5555
}

0 commit comments

Comments
 (0)