Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 739d75a

Browse files
refi93pabigot
authored andcommitted
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 pabigot#24. Signed-off-by: Peter A. Bigot <[email protected]>
1 parent 79fbb0c commit 739d75a

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [1.2.1] - 2021-04-29
4+
5+
* Improve [browser compatibility][pr#24] by using `Buffer.isBuffer` instead of
6+
`instanceof` to confirm Buffer types.
7+
38
## [1.2.0] - 2018-03-14
49

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

132137
* Initial release.
133138

139+
[1.2.1]: https://github.com/pabigot/buffer-layout/compare/v1.2.0...v1.2.1
134140
[1.2.0]: https://github.com/pabigot/buffer-layout/compare/v1.1.0...v1.2.0
135141
[1.1.0]: https://github.com/pabigot/buffer-layout/compare/v1.0.0...v1.1.0
136142
[1.0.0]: https://github.com/pabigot/buffer-layout/compare/v0.13.0...v1.0.0
@@ -193,6 +199,7 @@
193199
[issue#19]: https://github.com/pabigot/buffer-layout/issues/19
194200
[issue#20]: https://github.com/pabigot/buffer-layout/issues/20
195201
[issue#21]: https://github.com/pabigot/buffer-layout/issues/21
202+
[pr#24]: https://github.com/pabigot/buffer-layout/pull/24
196203
[ci:travis]: https://travis-ci.org/pabigot/buffer-layout
197204
[ci:coveralls]: https://coveralls.io/github/pabigot/buffer-layout
198205
[node:issue#3992]: https://github.com/nodejs/node/issues/3992

lib/Layout.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ class Union extends Layout {
17651765
*/
17661766
getVariant(vb, offset) {
17671767
let variant = vb;
1768-
if (vb instanceof Buffer) {
1768+
if (Buffer.isBuffer(vb)) {
17691769
if (undefined === offset) {
17701770
offset = 0;
17711771
}
@@ -2325,7 +2325,7 @@ class Blob extends Layout {
23252325
if (this.length instanceof ExternalLayout) {
23262326
span = src.length;
23272327
}
2328-
if (!((src instanceof Buffer)
2328+
if (!(Buffer.isBuffer(src)
23292329
&& (span === src.length))) {
23302330
throw new TypeError(nameWithProperty('Blob.encode', this)
23312331
+ ' requires (length ' + span + ') Buffer as src');
@@ -2361,7 +2361,7 @@ class CString extends Layout {
23612361

23622362
/** @override */
23632363
getSpan(b, offset) {
2364-
if (!(b instanceof Buffer)) {
2364+
if (!Buffer.isBuffer(b)) {
23652365
throw new TypeError('b must be a Buffer');
23662366
}
23672367
if (undefined === offset) {
@@ -2452,7 +2452,7 @@ class UTF8 extends Layout {
24522452

24532453
/** @override */
24542454
getSpan(b, offset) {
2455-
if (!(b instanceof Buffer)) {
2455+
if (!Buffer.isBuffer(b)) {
24562456
throw new TypeError('b must be a Buffer');
24572457
}
24582458
if (undefined === offset) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "buffer-layout",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Translation between JavaScript values and Buffers",
55
"keywords": [
66
"Buffer",

0 commit comments

Comments
 (0)