Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const {
reuseOrCreateUnrefTimeout,
getTimerDuration,
} = require('internal/timers');
const { isUint8Array } = require('internal/util/types');
const {
isArrayBuffer,
isUint8Array,
} = require('internal/util/types');
const { clearTimeout } = require('timers');
const { validateFunction } = require('internal/validators');

Expand Down Expand Up @@ -192,9 +195,11 @@ function onStreamRead(arrayBuffer) {
// If the buffer that will be reused for the next read was detached during
// the callback (e.g. by transferring a view into a byte `ReadableStream`),
// it can no longer be safely written to. Stop reading instead of handing
// the stale address back to the native read path.
// the stale address back to the native read path. A `SharedArrayBuffer`
// cannot be detached, and the getter throws on one.
const reusableBuf = stream[kBuffer];
if (isUint8Array(reusableBuf) &&
isArrayBuffer(reusableBuf.buffer) &&
ArrayBufferPrototypeGetDetached(reusableBuf.buffer)) {
result = false;
}
Expand Down
48 changes: 48 additions & 0 deletions test/parallel/test-net-onread-buffer-shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

// An `onread` buffer backed by a `SharedArrayBuffer` can never be detached.
// Reading into one must not throw from the detached-buffer check.

const message = Buffer.from('hello world');

function test(buffer, check) {
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, common.mustCall(function() {
const incoming = [];
net.connect({
port: this.address().port,
onread: {
buffer,
callback: common.mustCallAtLeast((nread, buf) => {
check(buf);
assert.ok(buf.buffer instanceof SharedArrayBuffer);
incoming.push(Buffer.from(buf.subarray(0, nread)));
}),
},
}).on('data', common.mustNotCall())
.on('error', common.mustNotCall())
.on('end', common.mustCall(() => {
assert.deepStrictEqual(Buffer.concat(incoming), message);
}));
}));
}

// Static buffer.
{
const sockBuf = new Uint8Array(new SharedArrayBuffer(4));
test(sockBuf, (buf) => assert.strictEqual(buf, sockBuf));
}

// Buffer returned by a factory function.
{
let last;
test(common.mustCallAtLeast(() => {
last = new Uint8Array(new SharedArrayBuffer(4));
return last;
}), (buf) => assert.strictEqual(buf, last));
}
58 changes: 58 additions & 0 deletions test/parallel/test-tls-onread-buffer-shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const tls = require('tls');
const fixtures = require('../common/fixtures');

// An `onread` buffer backed by a `SharedArrayBuffer` can never be detached.
// Reading into one must not throw from the detached-buffer check.

const options = {
key: fixtures.readKey('agent2-key.pem'),
cert: fixtures.readKey('agent2-cert.pem'),
};

const message = Buffer.from('hello world');

function test(buffer, check) {
tls.createServer(options, common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, common.mustCall(function() {
const incoming = [];
tls.connect({
port: this.address().port,
rejectUnauthorized: false,
onread: {
buffer,
callback: common.mustCallAtLeast((nread, buf) => {
check(buf);
assert.ok(buf.buffer instanceof SharedArrayBuffer);
incoming.push(Buffer.from(buf.subarray(0, nread)));
}),
},
}).on('data', common.mustNotCall())
.on('error', common.mustNotCall())
.on('end', common.mustCall(() => {
assert.deepStrictEqual(Buffer.concat(incoming), message);
}));
}));
}

// Static buffer.
{
const sockBuf = new Uint8Array(new SharedArrayBuffer(4));
test(sockBuf, (buf) => assert.strictEqual(buf, sockBuf));
}

// Buffer returned by a factory function.
{
let last;
test(common.mustCallAtLeast(() => {
last = new Uint8Array(new SharedArrayBuffer(4));
return last;
}), (buf) => assert.strictEqual(buf, last));
}
Loading