From 1f2d48cd975b719f0248e471f3b503cb01398dfb Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Wed, 14 Aug 2024 09:42:31 -0700 Subject: fix(node/fs): node:fs.read and write should accept typed arrays other than Uint8Array (#25030) Part of #25028. Our underlying read/write operations in `io` assume the buffer is a Uint8Array, but we were passing in other typed arrays (in the case above it was `Int8Array`). --- ext/node/polyfills/internal/fs/utils.mjs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'ext/node/polyfills/internal/fs/utils.mjs') diff --git a/ext/node/polyfills/internal/fs/utils.mjs b/ext/node/polyfills/internal/fs/utils.mjs index 9f3322bae..53184b9c3 100644 --- a/ext/node/polyfills/internal/fs/utils.mjs +++ b/ext/node/polyfills/internal/fs/utils.mjs @@ -986,6 +986,16 @@ export const validatePosition = hideStackFrames((position) => { } }); +/** @type {(buffer: ArrayBufferView) => Uint8Array} */ +export const arrayBufferViewToUint8Array = hideStackFrames( + (buffer) => { + if (!(buffer instanceof Uint8Array)) { + return new Uint8Array(buffer.buffer); + } + return buffer; + }, +); + export const realpathCacheKey = Symbol("realpathCacheKey"); export const constants = { kIoMaxLength, -- cgit v1.2.3