Skip to content

Commit

Permalink
fix: deprecation warning for new Buffer (#1905)
Browse files Browse the repository at this point in the history
Co-authored-by: Filip Mösner <[email protected]>
  • Loading branch information
panther7 and Filip Mösner authored Aug 21, 2023
1 parent eaf9f0a commit e93286e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,14 @@ Reader.prototype.bytes = function read_bytes() {
this.pos += length;
if (Array.isArray(this.buf)) // plain array
return this.buf.slice(start, end);
return start === end // fix for IE 10/Win8 and others' subarray returning array of size 1
? new this.buf.constructor(0)
: this._slice.call(this.buf, start, end);

if (start === end) { // fix for IE 10/Win8 and others' subarray returning array of size 1
var nativeBuffer = util.Buffer;
return nativeBuffer
? nativeBuffer.alloc(0)
: new this.buf.constructor(0);
}
return this._slice.call(this.buf, start, end);
};

/**
Expand Down

1 comment on commit e93286e

@anneb
Copy link

@anneb anneb commented on e93286e Jun 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some cases this commit still produces a deprecation warning.

I believe the fallback call should be util.newBuffer(0), not new this.buf.constructor(0)

In my case, the deprecation warning happens because I have built the library for ESM using rollup. This commit uses util.Buffer which in turn uses util.inquire("buffer").Buffer. util.inquire uses a hidden require to test if module 'buffer' is available. However, the require method is not available under ESM, so under ESM util.inquire always fails, even for Node environments that have a native Buffer available.

Please sign in to comment.