-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use native Buffer whenever available
- Loading branch information
Showing
4 changed files
with
95 additions
and
2 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,37 @@ | ||
const buffer = require('buffer/'); | ||
|
||
// Use the native Buffer when available. | ||
// This is done to get better performance when operating on large buffers | ||
// For example, when converting a large buffer to a string, the native Buffer | ||
// is much faster than the isomorphic buffer and supports toString() operation on | ||
// buffers of size up to 512MB , while the isomorphic buffer errors out after 100MB. | ||
if (typeof globalThis.Buffer === 'function') { | ||
const isomorphicBuffer = buffer.Buffer; | ||
const nativeBuffer = globalThis.Buffer; | ||
|
||
// For node v20 and above `new Buffer()` or `Buffer()` syntax | ||
// does not work. Patching it here for backward compatibility | ||
buffer.Buffer = function () { | ||
return new isomorphicBuffer(...arguments); | ||
}; | ||
|
||
// Ensure the exposed Buffer has same interface as the isomorphicBuffer Buffer | ||
for (const key in isomorphicBuffer) { | ||
buffer.Buffer[key] = nativeBuffer[key]; | ||
} | ||
|
||
// Ensure `instanceof` works for buffers created using the `new Buffer()` or `Buffer()` syntax | ||
Object.defineProperty(buffer.Buffer, Symbol.hasInstance, { | ||
value: function (instance) { | ||
return instance instanceof nativeBuffer || instance instanceof isomorphicBuffer; | ||
} | ||
}); | ||
|
||
// Extend `isBuffer` for buffers created using the `new Buffer()` or `Buffer()` syntax | ||
buffer.Buffer.isBuffer = function (obj) { | ||
return nativeBuffer.isBuffer(obj) || isomorphicBuffer.isBuffer(obj); | ||
}; | ||
} | ||
|
||
module.exports = buffer; | ||
|
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