-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UintArray): Adds support for Uint8Arrays
All API calls can now handle taking in a Uint8Array instead of a buffer. Consumers will still need to globally provide a polyfill for Buffer, as it is still used internally.
- Loading branch information
1 parent
7f8d1b6
commit 2a54053
Showing
5 changed files
with
152 additions
and
35 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer. | ||
* | ||
* @param {Buffer|Uint8Array} potentialBuffer The potential buffer | ||
* @returns {Buffer} the input if potentialBuffer is a buffer, or a buffer that | ||
* wraps a passed in Uint8Array | ||
* @throws {TypeError} If anything other than a Buffer or Uint8Array is passed in | ||
*/ | ||
module.exports = function ensureBuffer(potentialBuffer) { | ||
if (potentialBuffer instanceof Buffer) { | ||
return potentialBuffer; | ||
} | ||
if (potentialBuffer instanceof Uint8Array) { | ||
return new Buffer(potentialBuffer.buffer); | ||
} | ||
|
||
throw new TypeError('Must use either Buffer or Uint8Array'); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const ensureBuffer = require('../../lib/bson/ensure_buffer'); | ||
const expect = require('chai').expect; | ||
|
||
describe('ensureBuffer tests', function() { | ||
it('should be a function', function() { | ||
expect(ensureBuffer).to.be.a('function'); | ||
}); | ||
|
||
it('should return the exact same buffer if a buffer is passed in', function() { | ||
const bufferIn = new Buffer(10); | ||
let bufferOut; | ||
|
||
expect(function() { | ||
bufferOut = ensureBuffer(bufferIn); | ||
}).to.not.throw(Error); | ||
|
||
expect(bufferOut).to.equal(bufferIn); | ||
}); | ||
|
||
it('should wrap a UInt8Array with a buffer', function() { | ||
const arrayIn = Uint8Array.from([1, 2, 3]); | ||
let bufferOut; | ||
|
||
expect(function() { | ||
bufferOut = ensureBuffer(arrayIn); | ||
}).to.not.throw(Error); | ||
|
||
expect(bufferOut).to.be.an.instanceOf(Buffer); | ||
expect(bufferOut.buffer).to.equal(arrayIn.buffer); | ||
}); | ||
|
||
[0, 12, -1, '', 'foo', null, undefined, ['list'], {}, /x/].forEach(function(item) { | ||
it(`should throw if input is ${typeof item}: ${item}`, function() { | ||
expect(function() { | ||
ensureBuffer(item); | ||
}).to.throw(TypeError); | ||
}); | ||
}); | ||
|
||
[ | ||
/* eslint-disable */ | ||
Int8Array, | ||
Uint8ClampedArray, | ||
Int16Array, | ||
Uint16Array, | ||
Int32Array, | ||
Uint32Array, | ||
Float32Array, | ||
Float64Array | ||
/* eslint-enable */ | ||
].forEach(function(TypedArray) { | ||
it(`should throw if input is typed array ${TypedArray.name}`, function() { | ||
const typedArray = new TypedArray(); | ||
expect(function() { | ||
ensureBuffer(typedArray); | ||
}).to.throw(TypeError); | ||
}); | ||
}); | ||
}); |