2017-10-12 05:17:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-30 15:55:29 +00:00
|
|
|
const {
|
|
|
|
Symbol,
|
|
|
|
} = primordials;
|
|
|
|
|
2019-04-05 21:56:00 +00:00
|
|
|
const { setImmediate } = require('timers');
|
2019-03-10 14:37:51 +00:00
|
|
|
const assert = require('internal/assert');
|
2017-10-12 05:17:48 +00:00
|
|
|
const { Socket } = require('net');
|
2018-09-04 14:13:25 +00:00
|
|
|
const { JSStream } = internalBinding('js_stream');
|
2018-08-06 21:40:30 +00:00
|
|
|
const uv = internalBinding('uv');
|
2020-03-14 11:55:44 +00:00
|
|
|
let debug = require('internal/util/debuglog').debuglog(
|
|
|
|
'stream_socket',
|
|
|
|
(fn) => {
|
|
|
|
debug = fn;
|
2023-02-14 17:45:16 +00:00
|
|
|
},
|
2020-03-14 11:55:44 +00:00
|
|
|
);
|
2018-07-27 12:35:39 +00:00
|
|
|
const { owner_symbol } = require('internal/async_hooks').symbols;
|
2018-03-04 21:16:24 +00:00
|
|
|
const { ERR_STREAM_WRAP } = require('internal/errors').codes;
|
2024-04-08 09:53:18 +00:00
|
|
|
const { kBoundSession } = require('internal/stream_base_commons');
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
const kCurrentWriteRequest = Symbol('kCurrentWriteRequest');
|
|
|
|
const kCurrentShutdownRequest = Symbol('kCurrentShutdownRequest');
|
2018-11-10 20:09:42 +00:00
|
|
|
const kPendingShutdownRequest = Symbol('kPendingShutdownRequest');
|
2023-08-24 15:05:02 +00:00
|
|
|
const kPendingClose = Symbol('kPendingClose');
|
2017-12-29 15:17:12 +00:00
|
|
|
|
2021-11-13 07:21:42 +00:00
|
|
|
function isClosing() { return this[owner_symbol].isClosing(); }
|
2019-11-28 06:56:21 +00:00
|
|
|
|
2021-11-13 07:21:42 +00:00
|
|
|
function onreadstart() { return this[owner_symbol].readStart(); }
|
2019-11-28 06:56:21 +00:00
|
|
|
|
2021-11-13 07:21:42 +00:00
|
|
|
function onreadstop() { return this[owner_symbol].readStop(); }
|
2019-11-28 06:56:21 +00:00
|
|
|
|
2021-11-13 07:21:42 +00:00
|
|
|
function onshutdown(req) { return this[owner_symbol].doShutdown(req); }
|
2019-11-28 06:56:21 +00:00
|
|
|
|
2021-11-13 07:21:42 +00:00
|
|
|
function onwrite(req, bufs) { return this[owner_symbol].doWrite(req, bufs); }
|
2017-12-29 15:17:12 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
/* This class serves as a wrapper for when the C++ side of Node wants access
|
|
|
|
* to a standard JS stream. For example, TLS or HTTP do not operate on network
|
|
|
|
* resources conceptually, although that is the common case and what we are
|
|
|
|
* optimizing for; in theory, they are completely composable and can work with
|
|
|
|
* any stream resource they see.
|
|
|
|
*
|
|
|
|
* For the common case, i.e. a TLS socket wrapping around a net.Socket, we
|
|
|
|
* can skip going through the JS layer and let TLS access the raw C++ handle
|
|
|
|
* of a net.Socket. The flipside of this is that, to maintain composability,
|
|
|
|
* we need a way to create "fake" net.Socket instances that call back into a
|
2018-12-19 22:14:57 +00:00
|
|
|
* "real" JavaScript stream. JSStreamSocket is exactly this.
|
2017-10-12 05:57:45 +00:00
|
|
|
*/
|
2018-12-19 22:14:57 +00:00
|
|
|
class JSStreamSocket extends Socket {
|
2017-10-12 05:57:45 +00:00
|
|
|
constructor(stream) {
|
|
|
|
const handle = new JSStream();
|
|
|
|
handle.close = (cb) => {
|
|
|
|
debug('close');
|
|
|
|
this.doClose(cb);
|
|
|
|
};
|
2017-12-29 15:17:12 +00:00
|
|
|
// Inside of the following functions, `this` refers to the handle
|
2018-12-19 22:14:57 +00:00
|
|
|
// and `this[owner_symbol]` refers to this JSStreamSocket instance.
|
2017-12-29 15:17:12 +00:00
|
|
|
handle.isClosing = isClosing;
|
|
|
|
handle.onreadstart = onreadstart;
|
|
|
|
handle.onreadstop = onreadstop;
|
|
|
|
handle.onshutdown = onshutdown;
|
|
|
|
handle.onwrite = onwrite;
|
2017-10-12 05:57:45 +00:00
|
|
|
|
|
|
|
stream.pause();
|
|
|
|
stream.on('error', (err) => this.emit('error', err));
|
|
|
|
const ondata = (chunk) => {
|
|
|
|
if (typeof chunk === 'string' ||
|
2019-05-19 11:54:07 +00:00
|
|
|
stream.readableObjectMode === true) {
|
2017-10-12 05:57:45 +00:00
|
|
|
// Make sure that no further `data` events will happen.
|
|
|
|
stream.pause();
|
|
|
|
stream.removeListener('data', ondata);
|
|
|
|
|
2018-03-04 21:16:24 +00:00
|
|
|
this.emit('error', new ERR_STREAM_WRAP());
|
2017-10-12 05:17:48 +00:00
|
|
|
return;
|
2017-10-12 05:57:45 +00:00
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
debug('data', chunk.length);
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.readBuffer(chunk);
|
|
|
|
};
|
|
|
|
stream.on('data', ondata);
|
|
|
|
stream.once('end', () => {
|
|
|
|
debug('end');
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.emitEOF();
|
2017-10-12 05:17:48 +00:00
|
|
|
});
|
2018-10-14 12:13:03 +00:00
|
|
|
// Some `Stream` don't pass `hasError` parameters when closed.
|
|
|
|
stream.once('close', () => {
|
|
|
|
// Errors emitted from `stream` have also been emitted to this instance
|
|
|
|
// so that we don't pass errors to `destroy()` again.
|
|
|
|
this.destroy();
|
|
|
|
});
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
super({ handle, manualStart: true });
|
|
|
|
this.stream = stream;
|
2017-12-29 15:17:12 +00:00
|
|
|
this[kCurrentWriteRequest] = null;
|
|
|
|
this[kCurrentShutdownRequest] = null;
|
2018-11-10 20:09:42 +00:00
|
|
|
this[kPendingShutdownRequest] = null;
|
2023-08-24 15:05:02 +00:00
|
|
|
this[kPendingClose] = false;
|
2018-04-12 09:15:31 +00:00
|
|
|
this.readable = stream.readable;
|
|
|
|
this.writable = stream.writable;
|
2017-12-29 15:17:12 +00:00
|
|
|
|
|
|
|
// Start reading.
|
2017-10-12 05:57:45 +00:00
|
|
|
this.read(0);
|
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2018-12-19 22:14:57 +00:00
|
|
|
// Allow legacy requires in the test suite to keep working:
|
|
|
|
// const { StreamWrap } = require('internal/js_stream_socket')
|
2017-10-12 05:57:45 +00:00
|
|
|
static get StreamWrap() {
|
2018-12-19 22:14:57 +00:00
|
|
|
return JSStreamSocket;
|
2017-10-12 05:57:45 +00:00
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
isClosing() {
|
|
|
|
return !this.readable || !this.writable;
|
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
readStart() {
|
|
|
|
this.stream.resume();
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
readStop() {
|
|
|
|
this.stream.pause();
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
doShutdown(req) {
|
2017-12-29 15:17:12 +00:00
|
|
|
// TODO(addaleax): It might be nice if we could get into a state where
|
|
|
|
// DoShutdown() is not called on streams while a write is still pending.
|
|
|
|
//
|
|
|
|
// Currently, the only part of the code base where that happens is the
|
|
|
|
// TLS implementation, which calls both DoWrite() and DoShutdown() on the
|
|
|
|
// underlying network stream inside of its own DoShutdown() method.
|
|
|
|
// Working around that on the native side is not quite trivial (yet?),
|
|
|
|
// so for now that is supported here.
|
|
|
|
|
2018-11-10 20:09:42 +00:00
|
|
|
if (this[kCurrentWriteRequest] !== null) {
|
|
|
|
this[kPendingShutdownRequest] = req;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-24 15:05:02 +00:00
|
|
|
|
2019-03-10 14:37:51 +00:00
|
|
|
assert(this[kCurrentWriteRequest] === null);
|
|
|
|
assert(this[kCurrentShutdownRequest] === null);
|
2018-10-07 13:18:15 +00:00
|
|
|
this[kCurrentShutdownRequest] = req;
|
2017-12-29 15:17:12 +00:00
|
|
|
|
2023-08-24 15:05:02 +00:00
|
|
|
if (this[kPendingClose]) {
|
|
|
|
// If doClose is pending, the stream & this._handle are gone. We can't do
|
|
|
|
// anything. doClose will call finishShutdown with ECANCELED for us shortly.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
const handle = this._handle;
|
2023-08-25 13:16:35 +00:00
|
|
|
assert(handle !== null);
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2022-09-29 18:55:17 +00:00
|
|
|
process.nextTick(() => {
|
2018-02-08 03:59:10 +00:00
|
|
|
// Ensure that write is dispatched asynchronously.
|
|
|
|
this.stream.end(() => {
|
2017-12-29 15:17:12 +00:00
|
|
|
this.finishShutdown(handle, 0);
|
2017-10-12 05:57:45 +00:00
|
|
|
});
|
2017-10-12 05:17:48 +00:00
|
|
|
});
|
2017-10-12 05:57:45 +00:00
|
|
|
return 0;
|
2017-10-12 05:17:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
// handle === this._handle except when called from doClose().
|
|
|
|
finishShutdown(handle, errCode) {
|
|
|
|
// The shutdown request might already have been cancelled.
|
|
|
|
if (this[kCurrentShutdownRequest] === null)
|
|
|
|
return;
|
|
|
|
const req = this[kCurrentShutdownRequest];
|
|
|
|
this[kCurrentShutdownRequest] = null;
|
|
|
|
handle.finishShutdown(req, errCode);
|
|
|
|
}
|
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
doWrite(req, bufs) {
|
2019-03-10 14:37:51 +00:00
|
|
|
assert(this[kCurrentWriteRequest] === null);
|
|
|
|
assert(this[kCurrentShutdownRequest] === null);
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2023-08-24 15:05:02 +00:00
|
|
|
if (this[kPendingClose]) {
|
|
|
|
// If doClose is pending, the stream & this._handle are gone. We can't do
|
|
|
|
// anything. doClose will call finishWrite with ECANCELED for us shortly.
|
|
|
|
this[kCurrentWriteRequest] = req; // Store req, for doClose to cancel
|
|
|
|
return 0;
|
2023-10-20 13:28:18 +00:00
|
|
|
} else if (this._handle === null) {
|
|
|
|
// If this._handle is already null, there is nothing left to do with a
|
|
|
|
// pending write request, so we discard it.
|
|
|
|
return 0;
|
2023-08-24 15:05:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
const handle = this._handle;
|
2023-08-25 13:16:35 +00:00
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
const self = this;
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
let pending = bufs.length;
|
2017-10-12 05:57:45 +00:00
|
|
|
|
|
|
|
this.stream.cork();
|
2019-11-12 15:13:48 +00:00
|
|
|
// Use `var` over `let` for performance optimization.
|
2022-04-02 04:18:40 +00:00
|
|
|
// eslint-disable-next-line no-var
|
2017-12-29 15:17:12 +00:00
|
|
|
for (var i = 0; i < bufs.length; ++i)
|
|
|
|
this.stream.write(bufs[i], done);
|
2017-10-12 05:57:45 +00:00
|
|
|
this.stream.uncork();
|
|
|
|
|
2018-02-08 17:32:32 +00:00
|
|
|
// Only set the request here, because the `write()` calls could throw.
|
|
|
|
this[kCurrentWriteRequest] = req;
|
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
function done(err) {
|
|
|
|
if (!err && --pending !== 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensure that this is called once in case of error
|
|
|
|
pending = 0;
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
let errCode = 0;
|
|
|
|
if (err) {
|
2017-12-29 15:17:12 +00:00
|
|
|
errCode = uv[`UV_${err.code}`] || uv.UV_EPIPE;
|
2017-10-12 05:57:45 +00:00
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
// Ensure that write was dispatched
|
2017-12-29 15:17:12 +00:00
|
|
|
setImmediate(() => {
|
|
|
|
self.finishWrite(handle, errCode);
|
2017-10-12 05:57:45 +00:00
|
|
|
});
|
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-10-12 05:17:48 +00:00
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
// handle === this._handle except when called from doClose().
|
|
|
|
finishWrite(handle, errCode) {
|
|
|
|
// The write request might already have been cancelled.
|
|
|
|
if (this[kCurrentWriteRequest] === null)
|
|
|
|
return;
|
|
|
|
const req = this[kCurrentWriteRequest];
|
|
|
|
this[kCurrentWriteRequest] = null;
|
2017-10-12 05:57:45 +00:00
|
|
|
|
2017-12-29 15:17:12 +00:00
|
|
|
handle.finishWrite(req, errCode);
|
2018-11-10 20:09:42 +00:00
|
|
|
if (this[kPendingShutdownRequest]) {
|
|
|
|
const req = this[kPendingShutdownRequest];
|
|
|
|
this[kPendingShutdownRequest] = null;
|
|
|
|
this.doShutdown(req);
|
|
|
|
}
|
2017-10-12 05:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
doClose(cb) {
|
2023-08-24 15:05:02 +00:00
|
|
|
this[kPendingClose] = true;
|
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
const handle = this._handle;
|
|
|
|
|
2018-10-14 12:13:03 +00:00
|
|
|
// When sockets of the "net" module destroyed, they will call
|
|
|
|
// `this._handle.close()` which will also emit EOF if not emitted before.
|
|
|
|
// This feature makes sockets on the other side emit "end" and "close"
|
|
|
|
// even though we haven't called `end()`. As `stream` are likely to be
|
|
|
|
// instances of `net.Socket`, calling `stream.destroy()` manually will
|
|
|
|
// avoid issues that don't properly close wrapped connections.
|
|
|
|
this.stream.destroy();
|
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
setImmediate(() => {
|
|
|
|
// Should be already set by net.js
|
2019-03-10 14:37:51 +00:00
|
|
|
assert(this._handle === null);
|
2017-12-29 15:17:12 +00:00
|
|
|
|
|
|
|
this.finishWrite(handle, uv.UV_ECANCELED);
|
|
|
|
this.finishShutdown(handle, uv.UV_ECANCELED);
|
|
|
|
|
2023-08-24 15:05:02 +00:00
|
|
|
this[kPendingClose] = false;
|
|
|
|
|
2017-10-12 05:57:45 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2024-01-12 16:09:48 +00:00
|
|
|
|
2024-04-08 09:53:18 +00:00
|
|
|
get [kBoundSession]() {
|
|
|
|
return this.stream[kBoundSession];
|
2024-01-12 16:09:48 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 09:53:18 +00:00
|
|
|
set [kBoundSession](session) {
|
|
|
|
this.stream[kBoundSession] = session;
|
2024-01-12 16:09:48 +00:00
|
|
|
}
|
2017-10-12 05:57:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 22:14:57 +00:00
|
|
|
module.exports = JSStreamSocket;
|