Skip to content

Commit b7c24a1

Browse files
achingbraindaviddias
authored andcommitted
fix: support uint8arrays as well as buffers
Relaxes the input check to accept Uint8Arrays instead of node Buffers as this module doesn't do anything Buffer-specific. Repeats the tests for both Buffers and Uint8Arrays. Removes deps that aren't used after the above refactor.
1 parent 9afb0f9 commit b7c24a1

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
"npm": ">=3.0.0"
2121
},
2222
"dependencies": {
23-
"is-buffer": "^2.0.4",
2423
"varint": "^5.0.0"
2524
},
2625
"devDependencies": {
27-
"aegir": "^21.9.0",
26+
"aegir": "^25.0.0",
2827
"buffer": "^5.6.0",
29-
"chai": "^4.2.0"
28+
"uint8arrays": "^1.0.0"
3029
},
3130
"repository": {
3231
"type": "git",

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict'
22

33
const varint = require('varint')
4-
const isBuffer = require('is-buffer')
54

65
module.exports = (buf) => {
7-
if (!isBuffer(buf)) {
8-
throw new Error('arg needs to be a buffer')
6+
if (!(buf instanceof Uint8Array)) {
7+
throw new Error('arg needs to be a Uint8Array')
98
}
109

1110
const result = []

test/varint-decoder.spec.js

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

4-
const expect = require('chai').expect
4+
const { expect } = require('aegir/utils/chai')
55
const { Buffer } = require('buffer')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
67
const vd = require('../src')
78

8-
describe('varint-decoder', () => {
9-
it('decode 1 varint', () => {
10-
const buf = Buffer.from('05', 'hex')
11-
const arr = vd(buf)
12-
expect(arr[0]).to.equal(5)
13-
})
9+
const types = [{
10+
name: 'Buffer',
11+
fromHexString: (hex) => Buffer.from(hex, 'hex')
12+
}, {
13+
name: 'Uint8Array',
14+
fromHexString: (hex) => uint8ArrayFromString(hex, 'base16')
15+
}]
1416

15-
it('decode 2 varints', () => {
16-
const buf = Buffer.from('000a', 'hex')
17-
const arr = vd(buf)
18-
expect(arr[0]).to.equal(0)
19-
expect(arr[1]).to.equal(10)
20-
})
17+
types.forEach(({ name, fromHexString }) => {
18+
describe(`varint-decoder (${name})`, () => {
19+
it('decode 1 varint', () => {
20+
const buf = fromHexString('05')
21+
const arr = vd(buf)
22+
expect(arr[0]).to.equal(5)
23+
})
2124

22-
it('decode 3 varints', () => {
23-
const buf = Buffer.from('0b0c03', 'hex')
24-
const arr = vd(buf)
25-
expect(arr[0]).to.equal(11)
26-
expect(arr[1]).to.equal(12)
27-
expect(arr[2]).to.equal(3)
28-
})
25+
it('decode 2 varints', () => {
26+
const buf = fromHexString('000a')
27+
const arr = vd(buf)
28+
expect(arr[0]).to.equal(0)
29+
expect(arr[1]).to.equal(10)
30+
})
2931

30-
it('decode 1 long varint', () => {
31-
const buf = Buffer.from('c801', 'hex')
32-
const arr = vd(buf)
33-
expect(arr[0]).to.equal(200)
34-
})
32+
it('decode 3 varints', () => {
33+
const buf = fromHexString('0b0c03')
34+
const arr = vd(buf)
35+
expect(arr[0]).to.equal(11)
36+
expect(arr[1]).to.equal(12)
37+
expect(arr[2]).to.equal(3)
38+
})
39+
40+
it('decode 1 long varint', () => {
41+
const buf = fromHexString('c801')
42+
const arr = vd(buf)
43+
expect(arr[0]).to.equal(200)
44+
})
3545

36-
it('decode a mix of long and short', () => {
37-
const buf = Buffer.from('96130208b90a', 'hex')
38-
const arr = vd(buf)
39-
expect(arr[0]).to.equal(2454)
40-
expect(arr[1]).to.equal(2)
41-
expect(arr[2]).to.equal(8)
42-
expect(arr[3]).to.equal(1337)
46+
it('decode a mix of long and short', () => {
47+
const buf = fromHexString('96130208b90a')
48+
const arr = vd(buf)
49+
expect(arr[0]).to.equal(2454)
50+
expect(arr[1]).to.equal(2)
51+
expect(arr[2]).to.equal(8)
52+
expect(arr[3]).to.equal(1337)
53+
})
4354
})
4455
})

0 commit comments

Comments
 (0)