Skip to content

Commit 560466c

Browse files
addaleaxdanbev
authored andcommitted
lib,src: remove usage of _externalStream
Since 4697e1b, it is no longer necessary to use `v8::External`s to pass `StreamBase` instances to native functions. PR-URL: #26510 Refs: #25142 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a445244 commit 560466c

File tree

12 files changed

+29
-37
lines changed

12 files changed

+29
-37
lines changed

lib/_http_server.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,11 @@ function connectionListenerInternal(server, socket) {
390390
socket.on = socketOnWrap;
391391

392392
// We only consume the socket if it has never been consumed before.
393-
if (socket._handle) {
394-
var external = socket._handle._externalStream;
395-
if (!socket._handle._consumed && external) {
396-
parser._consumed = true;
397-
socket._handle._consumed = true;
398-
parser.consume(external);
399-
}
393+
if (socket._handle && socket._handle.isStreamBase &&
394+
!socket._handle._consumed) {
395+
parser._consumed = true;
396+
socket._handle._consumed = true;
397+
parser.consume(socket._handle);
400398
}
401399
parser[kOnExecute] =
402400
onParserExecute.bind(undefined, server, socket, parser, state);

lib/_tls_wrap.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,10 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
444444
const context = options.secureContext ||
445445
options.credentials ||
446446
tls.createSecureContext(options);
447-
const externalStream = handle._externalStream;
448-
assert(typeof externalStream === 'object',
449-
'handle must be a LibuvStreamWrap');
447+
assert(handle.isStreamBase, 'handle must be a StreamBase');
450448
assert(context.context instanceof NativeSecureContext,
451449
'context.context must be a NativeSecureContext');
452-
const res = tls_wrap.wrap(externalStream,
453-
context.context,
454-
!!options.isServer);
450+
const res = tls_wrap.wrap(handle, context.context, !!options.isServer);
455451
res._parent = handle; // C++ "wrap" object: TCPWrap, JSStream, ...
456452
res._parentWrap = wrap; // JS object: net.Socket, JSStreamSocket, ...
457453
res._secureContext = context;

lib/internal/http2/core.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ function setupHandle(socket, type, options) {
843843

844844
if (typeof options.selectPadding === 'function')
845845
this[kSelectPadding] = options.selectPadding;
846-
handle.consume(socket._handle._externalStream);
846+
handle.consume(socket._handle);
847847

848848
this[kHandle] = handle;
849849

@@ -933,7 +933,7 @@ class Http2Session extends EventEmitter {
933933
constructor(type, options, socket) {
934934
super();
935935

936-
if (!socket._handle || !socket._handle._externalStream) {
936+
if (!socket._handle || !socket._handle.isStreamBase) {
937937
socket = new JSStreamSocket(socket);
938938
}
939939

@@ -2092,8 +2092,7 @@ function startFilePipe(self, fd, offset, length) {
20922092
handle.onread = onPipedFileHandleRead;
20932093
handle.stream = self;
20942094

2095-
const pipe = new StreamPipe(handle._externalStream,
2096-
self[kHandle]._externalStream);
2095+
const pipe = new StreamPipe(handle, self[kHandle]);
20972096
pipe.onunpipe = onFileUnpipe;
20982097
pipe.start();
20992098

src/node_http2.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,8 +1837,8 @@ bool Http2Session::HasWritesOnSocketForStream(Http2Stream* stream) {
18371837
// (typically a net.Socket or tls.TLSSocket). The lifecycle of the two is
18381838
// tightly coupled with all data transfer between the two happening at the
18391839
// C++ layer via the StreamBase API.
1840-
void Http2Session::Consume(Local<External> external) {
1841-
StreamBase* stream = static_cast<StreamBase*>(external->Value());
1840+
void Http2Session::Consume(Local<Object> stream_obj) {
1841+
StreamBase* stream = StreamBase::FromObject(stream_obj);
18421842
stream->PushStreamListener(this);
18431843
Debug(this, "i/o stream consumed");
18441844
}
@@ -2429,8 +2429,8 @@ void Http2Session::New(const FunctionCallbackInfo<Value>& args) {
24292429
void Http2Session::Consume(const FunctionCallbackInfo<Value>& args) {
24302430
Http2Session* session;
24312431
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());
2432-
CHECK(args[0]->IsExternal());
2433-
session->Consume(args[0].As<External>());
2432+
CHECK(args[0]->IsObject());
2433+
session->Consume(args[0].As<Object>());
24342434
}
24352435

24362436
// Destroys the Http2Session instance and renders it unusable

src/node_http2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class Http2Session : public AsyncWrap, public StreamListener {
696696

697697
void Close(uint32_t code = NGHTTP2_NO_ERROR,
698698
bool socket_closed = false);
699-
void Consume(Local<External> external);
699+
void Consume(Local<Object> stream);
700700
void Goaway(uint32_t code, int32_t lastStreamID,
701701
const uint8_t* data, size_t len);
702702
void AltSvc(int32_t id,

src/node_http_parser_impl.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,8 @@ class Parser : public AsyncWrap, public StreamListener {
556556
static void Consume(const FunctionCallbackInfo<Value>& args) {
557557
Parser* parser;
558558
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
559-
CHECK(args[0]->IsExternal());
560-
Local<External> stream_obj = args[0].As<External>();
561-
StreamBase* stream = static_cast<StreamBase*>(stream_obj->Value());
559+
CHECK(args[0]->IsObject());
560+
StreamBase* stream = StreamBase::FromObject(args[0].As<Object>());
562561
CHECK_NOT_NULL(stream);
563562
stream->PushStreamListener(parser);
564563
}

src/stream_base-inl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace node {
1313

1414
using v8::Signature;
15-
using v8::External;
1615
using v8::FunctionCallbackInfo;
1716
using v8::FunctionTemplate;
1817
using v8::HandleScope;

src/stream_base.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace node {
1717
using v8::Array;
1818
using v8::ArrayBuffer;
1919
using v8::Context;
20+
using v8::External;
2021
using v8::FunctionCallbackInfo;
2122
using v8::HandleScope;
2223
using v8::Integer;
@@ -368,6 +369,9 @@ void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
368369
t, "writeUcs2String", JSMethod<&StreamBase::WriteString<UCS2>>);
369370
env->SetProtoMethod(
370371
t, "writeLatin1String", JSMethod<&StreamBase::WriteString<LATIN1>>);
372+
t->PrototypeTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
373+
"isStreamBase"),
374+
True(env->isolate()));
371375
}
372376

373377
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {

src/stream_pipe.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "node_buffer.h"
44

55
using v8::Context;
6-
using v8::External;
76
using v8::Function;
87
using v8::FunctionCallbackInfo;
98
using v8::FunctionTemplate;
@@ -226,10 +225,10 @@ void StreamPipe::WritableListener::OnStreamRead(ssize_t nread,
226225

227226
void StreamPipe::New(const FunctionCallbackInfo<Value>& args) {
228227
CHECK(args.IsConstructCall());
229-
CHECK(args[0]->IsExternal());
230-
CHECK(args[1]->IsExternal());
231-
auto source = static_cast<StreamBase*>(args[0].As<External>()->Value());
232-
auto sink = static_cast<StreamBase*>(args[1].As<External>()->Value());
228+
CHECK(args[0]->IsObject());
229+
CHECK(args[1]->IsObject());
230+
StreamBase* source = StreamBase::FromObject(args[0].As<Object>());
231+
StreamBase* sink = StreamBase::FromObject(args[1].As<Object>());
233232

234233
new StreamPipe(source, sink, args.This());
235234
}

src/tls_wrap.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,11 @@ void TLSWrap::Wrap(const FunctionCallbackInfo<Value>& args) {
151151
CHECK(args[1]->IsObject());
152152
CHECK(args[2]->IsBoolean());
153153

154-
Local<External> stream_obj = args[0].As<External>();
155154
Local<Object> sc = args[1].As<Object>();
156155
Kind kind = args[2]->IsTrue() ? SSLWrap<TLSWrap>::kServer :
157156
SSLWrap<TLSWrap>::kClient;
158157

159-
StreamBase* stream = static_cast<StreamBase*>(stream_obj->Value());
158+
StreamBase* stream = StreamBase::FromObject(args[0].As<Object>());
160159
CHECK_NOT_NULL(stream);
161160

162161
Local<Object> obj;

0 commit comments

Comments
 (0)