Skip to content

Commit

Permalink
passim: replace instanceof with Buffer.isBuffer
Browse files Browse the repository at this point in the history
instanceOf check may fail in browser environment where multiple
versions of the buffer package may be bundled together, hence
resulting in different instances of Buffer being present in the
code. A more resilient way of checking whether the parameter supplied
is a buffer is the Buffer.isBuffer() method which I'm migrating to in
this PR.

From #24.

Signed-off-by: Peter A. Bigot <[email protected]>
  • Loading branch information
refi93 authored and pabigot committed Apr 29, 2021
1 parent 79fbb0c commit 739d75a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [1.2.1] - 2021-04-29

* Improve [browser compatibility][pr#24] by using `Buffer.isBuffer` instead of
`instanceof` to confirm Buffer types.

## [1.2.0] - 2018-03-14

* **API** Add [UTF8][doc:UTF8] to encode UTF strings in a possibly
Expand Down Expand Up @@ -131,6 +136,7 @@

* Initial release.

[1.2.1]: https://github.com/pabigot/buffer-layout/compare/v1.2.0...v1.2.1
[1.2.0]: https://github.com/pabigot/buffer-layout/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/pabigot/buffer-layout/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/pabigot/buffer-layout/compare/v0.13.0...v1.0.0
Expand Down Expand Up @@ -193,6 +199,7 @@
[issue#19]: https://github.com/pabigot/buffer-layout/issues/19
[issue#20]: https://github.com/pabigot/buffer-layout/issues/20
[issue#21]: https://github.com/pabigot/buffer-layout/issues/21
[pr#24]: https://github.com/pabigot/buffer-layout/pull/24
[ci:travis]: https://travis-ci.org/pabigot/buffer-layout
[ci:coveralls]: https://coveralls.io/github/pabigot/buffer-layout
[node:issue#3992]: https://github.com/nodejs/node/issues/3992
Expand Down
8 changes: 4 additions & 4 deletions lib/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ class Union extends Layout {
*/
getVariant(vb, offset) {
let variant = vb;
if (vb instanceof Buffer) {
if (Buffer.isBuffer(vb)) {
if (undefined === offset) {
offset = 0;
}
Expand Down Expand Up @@ -2325,7 +2325,7 @@ class Blob extends Layout {
if (this.length instanceof ExternalLayout) {
span = src.length;
}
if (!((src instanceof Buffer)
if (!(Buffer.isBuffer(src)
&& (span === src.length))) {
throw new TypeError(nameWithProperty('Blob.encode', this)
+ ' requires (length ' + span + ') Buffer as src');
Expand Down Expand Up @@ -2361,7 +2361,7 @@ class CString extends Layout {

/** @override */
getSpan(b, offset) {
if (!(b instanceof Buffer)) {
if (!Buffer.isBuffer(b)) {
throw new TypeError('b must be a Buffer');
}
if (undefined === offset) {
Expand Down Expand Up @@ -2452,7 +2452,7 @@ class UTF8 extends Layout {

/** @override */
getSpan(b, offset) {
if (!(b instanceof Buffer)) {
if (!Buffer.isBuffer(b)) {
throw new TypeError('b must be a Buffer');
}
if (undefined === offset) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "buffer-layout",
"version": "1.2.0",
"version": "1.2.1",
"description": "Translation between JavaScript values and Buffers",
"keywords": [
"Buffer",
Expand Down

0 comments on commit 739d75a

Please sign in to comment.