Files
scriptc/tests/corpus/6206-buffer-is-buffer-views.ts
T
Malte UblandClaude Opus 5.5 9e63a937db Test the Buffer brand at run time and alias the buffer module's Buffer
`Buffer.isBuffer` over a Uint8Array-typed value (safe-buffer's
`Buffer.from(...)` result, etag's input) refused at compile time because
the bytes IR shares storage between Buffer and Uint8Array. The view keeps
its Buffer brand at run time, so the test now reads it.

`require('buffer').Buffer`, `const { Buffer } = require('buffer')` and
`buffer.Buffer` through a namespace binding alias the global Buffer (the
module member is the same constructor), so their statics lower like the
global's. A file-scope `var` of the namespace form aliases too when nothing
reassigns it and the require-binding early-reference analysis finds no
earlier reader. `Buffer.from` over a JS-inferred `string | Uint8Array |
undefined` value takes the checked runtime form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 15:55:18 -07:00

17 lines
927 B
TypeScript

// Buffer.isBuffer over Uint8Array-typed values reads the view's Buffer
// brand: Buffer factories, Buffer subarrays and slices are Buffers, plain
// typed arrays and copies into them are not.
function check(view: Uint8Array): boolean {
return Buffer.isBuffer(view);
}
const plain = new Uint8Array([1, 2, 3]);
const buffer = Buffer.from(plain);
console.log(check(plain), check(buffer), check(Buffer.alloc(2)), check(Buffer.from("hey").subarray(1)));
console.log(check(plain.subarray(1)), check(new Uint8Array(buffer)), check(buffer.slice(1)), check(Buffer.concat([plain])));
const views: Uint8Array[] = [plain, buffer, new TextEncoder().encode("x"), Buffer.allocUnsafe(1)];
console.log(views.map((v) => Buffer.isBuffer(v)).join(","));
function words(view: Uint32Array): boolean {
return Buffer.isBuffer(view);
}
console.log(words(new Uint32Array(2)), Buffer.from(plain).toString("hex"), Buffer.from(views[2]!).toString());