Files
node/test/parallel/test-blob-detached-buffer.js
Filip Skokan aead708af4 buffer: align Blob and File with Web IDL
Convert constructor arguments in Web IDL order before processing blob
parts, including inherited dictionary members and File.lastModified's
long long conversion.

Read buffer slots and copy bytes without invoking user getters, slice
methods, or species constructors. Treat detached parts as empty and
track Blob brands without invoking user getters or accepting proxies.

Keep rejecting bare SharedArrayBuffers and shared-memory views for
browser compatibility pending resolution of the union conversion rules.

Refs: https://github.com/whatwg/webidl/issues/1491
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/66776
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-11-05 22:26:21 +00:00

47 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('node:assert');
const { Blob, File } = require('node:buffer');
(async () => {
for (const create of [
(parts, options) => new Blob(parts, options),
(parts, options) => new File(parts, 'test', options),
]) {
const buffer = new ArrayBuffer(16);
const parts = [
buffer,
new Uint8Array(buffer),
new Uint8Array(buffer, 4, 8),
new Uint16Array(buffer, 4, 4),
new DataView(buffer),
new DataView(buffer, 4, 8),
Buffer.from(buffer, 4, 8),
];
buffer.transfer();
for (const part of parts) {
const empty = create([part]);
assert.strictEqual(empty.size, 0);
assert.deepStrictEqual(await empty.arrayBuffer(), new ArrayBuffer(0));
const mixed = create(['hello', part, 'world']);
assert.strictEqual(mixed.size, 10);
assert.strictEqual(await mixed.text(), 'helloworld');
}
// Options conversion can detach buffers after blob parts conversion.
const anotherBuffer = new ArrayBuffer(16);
const view = new DataView(anotherBuffer, 4, 8);
const blob = create([anotherBuffer, view, 'hello'], {
get endings() {
anotherBuffer.transfer();
return 'transparent';
},
});
assert.strictEqual(blob.size, 5);
assert.strictEqual(await blob.text(), 'hello');
}
})().then(common.mustCall());