Description
- Version: v12.4.0 (official binary)
- Platform: macOS 10.14.5 (Darwin Kernel Version 18.6.0)
The documentation says Buffer
has an incompatibility with Uint8rray
and I believe it is considered to a bug, or at least tends to cause bugs.
https://nodejs.org/api/buffer.html
Buffer instances are also Uint8Array instances. However, there are subtle incompatibilities with TypedArray. For example, while ArrayBuffer#slice() creates a copy of the slice, the implementation of Buffer#slice() creates a view over the existing Buffer without copying, making Buffer#slice() far more efficient.
This incompatibility is problematic when a code expects Uint8Array as a parameter and it distinguishes the semantics of Uint8Array.prototype.slice()
and Uint8Array.prototype.subarray()
.
Unfortunately, TypeScript can't help this problem because function f(b: Uint8Array)
can take Buffer
without notice:
// typtescript
const a: Uint8Array = Buffer.from([]); // OK because Buffer extends Uint8Array
const b: Int8Array = Buffer.from([]); // NG, of course
I think this incompatibility should be fixed in the long-term perspective, so firstly I propose to mark slice()
as deprecated.
What do you think of it?