diff --git a/lib/internal/js_stream_socket.js b/lib/internal/js_stream_socket.js index fd1294ec9764f9..bd902412564ceb 100644 --- a/lib/internal/js_stream_socket.js +++ b/lib/internal/js_stream_socket.js @@ -22,52 +22,41 @@ const kCurrentWriteRequest = Symbol('kCurrentWriteRequest'); const kCurrentShutdownRequest = Symbol('kCurrentShutdownRequest'); const kPendingShutdownRequest = Symbol('kPendingShutdownRequest'); -function isClosing() { - let socket = this[owner_symbol]; +function checkReusedHandle(self) { + let socket = self[owner_symbol]; - if (socket.constructor.name === 'ReusedHandle') { + if (socket.constructor.name === 'ReusedHandle') socket = socket.handle; - } + + return socket; +} + +function isClosing() { + const socket = checkReusedHandle(this); return socket.isClosing(); } function onreadstart() { - let socket = this[owner_symbol]; - - if (socket.constructor.name === 'ReusedHandle') { - socket = socket.handle; - } + const socket = checkReusedHandle(this); return socket.readStart(); } function onreadstop() { - let socket = this[owner_symbol]; - - if (socket.constructor.name === 'ReusedHandle') { - socket = socket.handle; - } + const socket = checkReusedHandle(this); return socket.readStop(); } function onshutdown(req) { - let socket = this[owner_symbol]; - - if (socket.constructor.name === 'ReusedHandle') { - socket = socket.handle; - } + const socket = checkReusedHandle(this); return socket.doShutdown(req); } function onwrite(req, bufs) { - let socket = this[owner_symbol]; - - if (socket.constructor.name === 'ReusedHandle') { - socket = socket.handle; - } + const socket = checkReusedHandle(this); return socket.doWrite(req, bufs); }