From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sat, 25 Oct 2025 11:06:10 +0200 Subject: lib: check SharedArrayBuffer existence in fast-utf8-stream After https://github.com/nodejs/node/pull/58897 calling Object.entries on fs will lazy-load fast-utf8-stream, which uses SharedArrayBuffer without checking for its existence first. It won't exist in the renderer process and will throw 'SharedArrayBuffer is not a constructor'. Refactor to check for SharedArrayBuffer first. This should be upstreamed to Node.js diff --git a/lib/internal/streams/fast-utf8-stream.js b/lib/internal/streams/fast-utf8-stream.js index 3bfcc494d17b5cc4c9f494fab4aff4f0d68d2287..241a6b05e84f8e79b8c9b93abfda5562c2e35e9e 100644 --- a/lib/internal/streams/fast-utf8-stream.js +++ b/lib/internal/streams/fast-utf8-stream.js @@ -49,7 +49,8 @@ const { const BUSY_WRITE_TIMEOUT = 100; const kEmptyBuffer = Buffer.allocUnsafe(0); -const kNil = new Int32Array(new SharedArrayBuffer(4)); +const haveSAB = typeof SharedArrayBuffer !== 'undefined'; +const kNil = haveSAB ? new Int32Array(new SharedArrayBuffer(4)) : null; function sleep(ms) { // Also filters out NaN, non-number types, including empty strings, but allows bigints @@ -59,10 +60,14 @@ function sleep(ms) { throw new ERR_INVALID_ARG_TYPE('ms', ['number', 'bigint'], ms); } throw new ERR_INVALID_ARG_VALUE.RangeError('ms', ms, - 'must be a number greater than 0 and less than Infinity'); + 'must be a number greater than 0 and less than Infinity'); + } + if (haveSAB) { + AtomicsWait(kNil, 0, 0, Number(ms)); + } else { + const { sleep: _sleep } = internalBinding('util'); + _sleep(ms); } - - AtomicsWait(kNil, 0, 0, Number(ms)); } // 16 KB. Don't write more than docker buffer size.