Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions napi/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Loading