diff options
author | Brian White <mscdex@mscdex.net> | 2019-01-10 23:52:27 +0300 |
---|---|---|
committer | Brian White <mscdex@mscdex.net> | 2019-08-24 00:05:52 +0300 |
commit | 8292b280ec9e6b8c2444cbe49350facc77f5fefa (patch) | |
tree | 2f40ac41f20b53b748a7039c4f00e419efb8784b /lib/internal/stream_base_commons.js | |
parent | 9d21b0395cc248a0e5537a11cc84f61919eccca6 (diff) |
net: allow reading data into a static buffer
Co-Authored-By: Anna Henningsen <anna@addaleax.net>
PR-URL: https://github.com/nodejs/node/pull/25436
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib/internal/stream_base_commons.js')
-rw-r--r-- | lib/internal/stream_base_commons.js | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index 88896083f19..eb2e53963d8 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -23,6 +23,7 @@ const { setUnrefTimeout, getTimerDuration } = require('internal/timers'); +const { isUint8Array } = require('internal/util/types'); const { clearTimeout } = require('timers'); const kMaybeDestroy = Symbol('kMaybeDestroy'); @@ -32,6 +33,9 @@ const kHandle = Symbol('kHandle'); const kSession = Symbol('kSession'); const debug = require('internal/util/debuglog').debuglog('stream'); +const kBuffer = Symbol('kBuffer'); +const kBufferGen = Symbol('kBufferGen'); +const kBufferCb = Symbol('kBufferCb'); function handleWriteReq(req, data, encoding) { const { handle } = req; @@ -161,9 +165,23 @@ function onStreamRead(arrayBuffer) { stream[kUpdateTimer](); if (nread > 0 && !stream.destroyed) { - const offset = streamBaseState[kArrayBufferOffset]; - const buf = new FastBuffer(arrayBuffer, offset, nread); - if (!stream.push(buf)) { + let ret; + let result; + const userBuf = stream[kBuffer]; + if (userBuf) { + result = (stream[kBufferCb](nread, userBuf) !== false); + const bufGen = stream[kBufferGen]; + if (bufGen !== null) { + const nextBuf = bufGen(); + if (isUint8Array(nextBuf)) + stream[kBuffer] = ret = nextBuf; + } + } else { + const offset = streamBaseState[kArrayBufferOffset]; + const buf = new FastBuffer(arrayBuffer, offset, nread); + result = stream.push(buf); + } + if (!result) { handle.reading = false; if (!stream.destroyed) { const err = handle.readStop(); @@ -172,7 +190,7 @@ function onStreamRead(arrayBuffer) { } } - return; + return ret; } if (nread === 0) { @@ -241,5 +259,8 @@ module.exports = { kUpdateTimer, kHandle, kSession, - setStreamTimeout + setStreamTimeout, + kBuffer, + kBufferCb, + kBufferGen }; |