Skip to content

Commit

Permalink
allow SharedArrayBuffer as well as ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
SRHerzog committed Mar 13, 2023
1 parent 7f63710 commit fdad5c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const {
} = constants;

const pathModule = require('path');
const { isArrayBufferView, isArrayBuffer } = require('internal/util/types');
const { isArrayBufferView, isArrayBuffer, isSharedArrayBuffer } = require('internal/util/types');

// We need to get the statValues from the binding at the callsite since
// it's re-initialized after deserialization.
Expand Down Expand Up @@ -821,7 +821,7 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
fd = getValidatedFd(fd);

let offset = offsetOrOptions;
const bufferToWrite = isArrayBuffer(buffer) ? new Uint8Array(buffer) : buffer;
const bufferToWrite = isArrayBuffer(buffer) || isSharedArrayBuffer(buffer) ? new Uint8Array(buffer) : buffer;
if (isArrayBufferView(bufferToWrite)) {
callback = maybeCallback(callback || position || length || offset);

Expand Down Expand Up @@ -891,7 +891,7 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
let result;

let offset = offsetOrOptions;
if (isArrayBuffer(buffer)) {
if (isArrayBuffer(buffer) || isSharedArrayBuffer(buffer)) {
buffer = new Uint8Array(buffer);
}
if (isArrayBufferView(buffer)) {
Expand Down Expand Up @@ -2184,7 +2184,7 @@ function writeFile(path, data, options, callback) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

if (isArrayBuffer(data)) {
if (isArrayBuffer(data) || isSharedArrayBuffer(data)) {
data = new Uint8Array(data);
}

Expand Down Expand Up @@ -2228,7 +2228,7 @@ function writeFile(path, data, options, callback) {
function writeFileSync(path, data, options) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });

if (isArrayBuffer(data)) {
if (isArrayBuffer(data) || isSharedArrayBuffer(data)) {
data = new Uint8Array(data);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const {
AbortError,
aggregateTwoErrors,
} = require('internal/errors');
const { isArrayBuffer, isArrayBufferView } = require('internal/util/types');
const { isArrayBuffer, isArrayBufferView, isSharedArrayBuffer } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');
const {
constants: {
Expand Down Expand Up @@ -410,7 +410,7 @@ async function writeFileHandle(filehandle, data, signal, encoding) {
}
return;
}
if (isArrayBuffer(data)) {
if (isArrayBuffer(data) || isSharedArrayBuffer(data)) {
data = new Uint8Array(data);
} else {
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
Expand Down Expand Up @@ -619,7 +619,7 @@ async function write(handle, buffer, offsetOrOptions, length, position) {
return { __proto__: null, bytesWritten: 0, buffer };

let offset = offsetOrOptions;
const bufferView = isArrayBuffer(buffer) ? new Uint8Array(buffer) : buffer;
const bufferView = isArrayBuffer(buffer) || isSharedArrayBuffer(buffer) ? new Uint8Array(buffer) : buffer;
if (isArrayBufferView(bufferView)) {
if (typeof offset === 'object') {
({
Expand Down Expand Up @@ -890,7 +890,7 @@ async function writeFile(path, data, options) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

if (!isArrayBufferView(data) && !isArrayBuffer(data) && !isCustomIterable(data)) {
if (!isArrayBufferView(data) && !isArrayBuffer(data) && !isSharedArrayBuffer(data) && !isCustomIterable(data)) {
validateStringAfterArrayBufferView(data, 'data');
data = Buffer.from(data, options.encoding || 'utf8');
}
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayIsArray,
ArrayPrototypePush,
BigInt,
Date,
DateNow,
Expand Down Expand Up @@ -42,6 +43,7 @@ const {
isArrayBufferView,
isBigInt64Array,
isDate,
isSharedArrayBuffer,
isUint8Array,
} = require('internal/util/types');
const {
Expand Down Expand Up @@ -737,9 +739,9 @@ const validateAndNormalizeBufferArray = hideStackFrames((buffers, propName = 'bu
for (let i = 0; i < buffers.length; i++) {
const buffer = buffers[i];
if (isArrayBufferView(buffers[i])) {
output.push(buffer);
} else if (isArrayBuffer(buffer)) {
output.push(new Uint8Array(buffers[i]));
ArrayPrototypePush(output, buffer);
} else if (isArrayBuffer(buffer) || isSharedArrayBuffer(buffer)) {
ArrayPrototypePush(output, new Uint8Array(buffers[i]));
} else {
throw new ERR_INVALID_ARG_TYPE(propName, ['ArrayBufferView[]', 'ArrayBuffer[]'], buffers);
}
Expand Down

0 comments on commit fdad5c2

Please sign in to comment.