From 739d75ab5a3d3ca56eb8eeed4d18bb2e77b5c48d Mon Sep 17 00:00:00 2001 From: Rafael Korbas Date: Wed, 28 Apr 2021 13:17:41 +0200 Subject: [PATCH] passim: replace instanceof with Buffer.isBuffer 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 --- CHANGELOG.md | 7 +++++++ lib/Layout.js | 8 ++++---- package.json | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b469ac5..ce58aac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 @@ -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 diff --git a/lib/Layout.js b/lib/Layout.js index b6cf096..3ed160c 100644 --- a/lib/Layout.js +++ b/lib/Layout.js @@ -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; } @@ -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'); @@ -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) { @@ -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) { diff --git a/package.json b/package.json index 00ba755..0f4e830 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "buffer-layout", - "version": "1.2.0", + "version": "1.2.1", "description": "Translation between JavaScript values and Buffers", "keywords": [ "Buffer",