From fcaa459e9786cda0875c0c2acc54be6d13cc1bcd Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 7 Mar 2019 12:22:36 +0100 Subject: [PATCH] net: use kHandle symbol for accessing native handle Use a common `kHandle` for all `StreamBase`-based streams. --- lib/internal/http2/core.js | 10 ++++------ lib/internal/stream_base_commons.js | 12 +++++++++--- lib/net.js | 15 ++++++++++----- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 8ebcebfc8d2a2d..f123e3d024f497 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -106,13 +106,13 @@ const { updateSettingsBuffer } = require('internal/http2/util'); const { - createWriteWrap, writeGeneric, writevGeneric, onStreamRead, kAfterAsyncWrite, kMaybeDestroy, kUpdateTimer, + kHandle, kSession, setStreamTimeout } = require('internal/stream_base_commons'); @@ -149,7 +149,6 @@ const TLSServer = tls.Server; const kAlpnProtocol = Symbol('alpnProtocol'); const kAuthority = Symbol('authority'); const kEncrypted = Symbol('encrypted'); -const kHandle = Symbol('handle'); const kID = Symbol('id'); const kInit = Symbol('init'); const kInfoHeaders = Symbol('sent-info-headers'); @@ -1795,13 +1794,12 @@ class Http2Stream extends Duplex { if (!this.headersSent) this[kProceed](); - const req = createWriteWrap(this[kHandle]); - req.stream = this[kID]; + let req; if (writev) - writevGeneric(this, req, data, cb); + req = writevGeneric(this, data, cb); else - writeGeneric(this, req, data, encoding, cb); + req = writeGeneric(this, data, encoding, cb); trackWriteState(this, req.bytes); } diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index 4740d5ae14d230..83f924d37be141 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -27,7 +27,8 @@ const { const kMaybeDestroy = Symbol('kMaybeDestroy'); const kUpdateTimer = Symbol('kUpdateTimer'); const kAfterAsyncWrite = Symbol('kAfterAsyncWrite'); -const kSession = Symbol('session'); +const kHandle = Symbol('kHandle'); +const kSession = Symbol('kSession'); function handleWriteReq(req, data, encoding) { const { handle } = req; @@ -98,7 +99,8 @@ function createWriteWrap(handle) { return req; } -function writevGeneric(self, req, data, cb) { +function writevGeneric(self, data, cb) { + const req = createWriteWrap(self[kHandle]); var allBuffers = data.allBuffers; var chunks; var i; @@ -120,12 +122,15 @@ function writevGeneric(self, req, data, cb) { if (err === 0) req._chunks = chunks; afterWriteDispatched(self, req, err, cb); + return req; } -function writeGeneric(self, req, data, encoding, cb) { +function writeGeneric(self, data, encoding, cb) { + const req = createWriteWrap(self[kHandle]); var err = handleWriteReq(req, data, encoding); afterWriteDispatched(self, req, err, cb); + return req; } function afterWriteDispatched(self, req, err, cb) { @@ -229,6 +234,7 @@ module.exports = { kAfterAsyncWrite, kMaybeDestroy, kUpdateTimer, + kHandle, kSession, setStreamTimeout }; diff --git a/lib/net.js b/lib/net.js index b649c6779a9484..78d8b42cf4701f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -58,11 +58,11 @@ const { symbols: { async_id_symbol, owner_symbol } } = require('internal/async_hooks'); const { - createWriteWrap, writevGeneric, writeGeneric, onStreamRead, kAfterAsyncWrite, + kHandle, kUpdateTimer, setStreamTimeout } = require('internal/stream_base_commons'); @@ -233,7 +233,7 @@ function Socket(options) { // probably be supplied by async_hooks. this[async_id_symbol] = -1; this._hadError = false; - this._handle = null; + this[kHandle] = null; this._parent = null; this._host = null; this[kLastWriteQueueSize] = 0; @@ -689,11 +689,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) { this._unrefTimer(); - var req = createWriteWrap(this._handle); + let req; if (writev) - writevGeneric(this, req, data, cb); + req = writevGeneric(this, data, cb); else - writeGeneric(this, req, data, encoding, cb); + req = writeGeneric(this, data, encoding, cb); if (req.async) this[kLastWriteQueueSize] = req.bytes; }; @@ -1584,6 +1584,11 @@ Object.defineProperty(TCP.prototype, 'owner', { set(v) { return this[owner_symbol] = v; } }); +Object.defineProperty(Socket.prototype, '_handle', { + get() { return this[kHandle]; }, + set(v) { return this[kHandle] = v; } +}); + Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) { return this.listen({ fd: fd });