mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
Treat detached ArrayBuffers and Buffer or TypedArray views backed by them as zero-length inputs in buffer.isUtf8() and buffer.isAscii(). Both functions now return true for these inputs, consistent with other empty inputs. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/64504 Fixes: https://github.com/nodejs/node/issues/64503 Reviewed-By: James M Snell <jasnell@gmail.com>
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
'use strict';
|
|
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const { isAscii, Buffer } = require('buffer');
|
|
const { TextEncoder } = require('util');
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
assert.strictEqual(isAscii(encoder.encode('hello')), true);
|
|
assert.strictEqual(isAscii(encoder.encode('ğ')), false);
|
|
assert.strictEqual(isAscii(Buffer.from([])), true);
|
|
|
|
[
|
|
undefined,
|
|
'', 'hello',
|
|
false, true,
|
|
0, 1,
|
|
0n, 1n,
|
|
Symbol(),
|
|
() => {},
|
|
{}, [], null,
|
|
].forEach((input) => {
|
|
assert.throws(
|
|
() => { isAscii(input); },
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
},
|
|
);
|
|
});
|
|
|
|
{
|
|
// Detached array buffers and views are treated as empty.
|
|
const arrayBuffer = new ArrayBuffer(1);
|
|
const typedArray = new Uint8Array(arrayBuffer);
|
|
typedArray[0] = 0xff;
|
|
const inputs = [
|
|
arrayBuffer,
|
|
typedArray,
|
|
Buffer.from(arrayBuffer),
|
|
];
|
|
for (const input of inputs) {
|
|
assert.strictEqual(isAscii(input), false);
|
|
}
|
|
structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
|
|
for (const input of inputs) {
|
|
assert.strictEqual(isAscii(input), true);
|
|
}
|
|
}
|