From a8be7a46608a99afc3c90b88d54113e2d5982ef4 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Mon, 10 Mar 2025 18:17:24 +0530 Subject: [PATCH] fix(napi/parser): disable raw transfer on unsupported platforms --- napi/parser/index.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/napi/parser/index.js b/napi/parser/index.js index 3935a5a1c80d8..81e9e006aea8f 100644 --- a/napi/parser/index.js +++ b/napi/parser/index.js @@ -69,7 +69,10 @@ let buffer, encoder; function parseSyncRaw(filename, sourceText, options) { if (!rawTransferSupported()) { - throw new Error('`experimentalRawTransfer` option is not supported on 32-bit or big-endian systems'); + throw new Error( + '`experimentalRawTransfer` option is not supported on 32-bit or big-endian systems, ' + + 'versions of NodeJS prior to v22.0.0, versions of Deno prior to v2.0.0, and other runtimes', + ); } // Delete `experimentalRawTransfer` option @@ -146,10 +149,45 @@ function createBuffer() { let rawTransferIsSupported = null; // Returns `true` if `experimentalRawTransfer` is option is supported. -// Raw transfer is only available on 64-bit little-endian systems. +// +// Raw transfer is only supported on 64-bit little-endian systems, +// and NodeJS >= v22.0.0 or Deno >= v2.0.0. +// +// Versions of NodeJS prior to v22.0.0 do not support creating an `ArrayBuffer` larger than 4 GiB. +// Bun (as at v1.2.4) also does not support creating an `ArrayBuffer` larger than 4 GiB. +// Support on Deno v1 is unknown and it's EOL, so treating Deno before v2.0.0 as unsupported. function rawTransferSupported() { - if (rawTransferIsSupported === null) rawTransferIsSupported = bindings.rawTransferSupported(); + if (rawTransferIsSupported === null) { + rawTransferIsSupported = rawTransferRuntimeSupported() && bindings.rawTransferSupported(); + } return rawTransferIsSupported; } module.exports.rawTransferSupported = rawTransferSupported; + +// Checks copied from: +// https://github.com/unjs/std-env/blob/ab15595debec9e9115a9c1d31bc7597a8e71dbfd/src/runtimes.ts +// MIT license: https://github.com/unjs/std-env/blob/ab15595debec9e9115a9c1d31bc7597a8e71dbfd/LICENCE +function rawTransferRuntimeSupported() { + let global; + try { + global = globalThis; + } catch (e) { + return false; + } + + const isBun = !!global.Bun || !!global.process?.versions?.bun; + if (isBun) return false; + + const isDeno = !!global.Deno; + if (isDeno) { + const match = Deno.version?.deno?.match(/^(\d+)\./); + return !!match && match[1] * 1 >= 2; + } + + const isNode = global.process?.release?.name === 'node'; + if (!isNode) return false; + + const match = process.version?.match(/^v(\d+)\./); + return !!match && match[1] * 1 >= 22; +}