-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
9afb0f9
commit b7c24a1
Showing
3 changed files
with
47 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,55 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const expect = require('chai').expect | ||
const { expect } = require('aegir/utils/chai') | ||
const { Buffer } = require('buffer') | ||
const uint8ArrayFromString = require('uint8arrays/from-string') | ||
const vd = require('../src') | ||
|
||
describe('varint-decoder', () => { | ||
it('decode 1 varint', () => { | ||
const buf = Buffer.from('05', 'hex') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(5) | ||
}) | ||
const types = [{ | ||
name: 'Buffer', | ||
fromHexString: (hex) => Buffer.from(hex, 'hex') | ||
}, { | ||
name: 'Uint8Array', | ||
fromHexString: (hex) => uint8ArrayFromString(hex, 'base16') | ||
}] | ||
|
||
it('decode 2 varints', () => { | ||
const buf = Buffer.from('000a', 'hex') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(0) | ||
expect(arr[1]).to.equal(10) | ||
}) | ||
types.forEach(({ name, fromHexString }) => { | ||
describe(`varint-decoder (${name})`, () => { | ||
it('decode 1 varint', () => { | ||
const buf = fromHexString('05') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(5) | ||
}) | ||
|
||
it('decode 3 varints', () => { | ||
const buf = Buffer.from('0b0c03', 'hex') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(11) | ||
expect(arr[1]).to.equal(12) | ||
expect(arr[2]).to.equal(3) | ||
}) | ||
it('decode 2 varints', () => { | ||
const buf = fromHexString('000a') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(0) | ||
expect(arr[1]).to.equal(10) | ||
}) | ||
|
||
it('decode 1 long varint', () => { | ||
const buf = Buffer.from('c801', 'hex') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(200) | ||
}) | ||
it('decode 3 varints', () => { | ||
const buf = fromHexString('0b0c03') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(11) | ||
expect(arr[1]).to.equal(12) | ||
expect(arr[2]).to.equal(3) | ||
}) | ||
|
||
it('decode 1 long varint', () => { | ||
const buf = fromHexString('c801') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(200) | ||
}) | ||
|
||
it('decode a mix of long and short', () => { | ||
const buf = Buffer.from('96130208b90a', 'hex') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(2454) | ||
expect(arr[1]).to.equal(2) | ||
expect(arr[2]).to.equal(8) | ||
expect(arr[3]).to.equal(1337) | ||
it('decode a mix of long and short', () => { | ||
const buf = fromHexString('96130208b90a') | ||
const arr = vd(buf) | ||
expect(arr[0]).to.equal(2454) | ||
expect(arr[1]).to.equal(2) | ||
expect(arr[2]).to.equal(8) | ||
expect(arr[3]).to.equal(1337) | ||
}) | ||
}) | ||
}) |