forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: merge stream code for http2 streams & net.Socket
Squashed from: - lib: separate writev responsibilities from writeGeneric - lib: fix calling of cb twice - lib: extract streamId out of stream_base to caller - lib: add symbols instead of methods to hide impl details - lib: remove unneeded lines - lib: use Object.assign instead of apply - lib: rename mixin StreamBase to StreamSharedMethods - lib: use stream shared funcs as top level instead of properties of prototypes - lib: mv lib/internal/stream_shared_methods.js lib/internal/stream_base_commons.js - lib: add comment for readability - lib: refactor _writev in Http2Stream - lib: rephrase comment - lib: revert usage of const,let for perf reasons PR-URL: nodejs#19527 Refs: nodejs#19060 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
Showing
4 changed files
with
108 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
const { Buffer } = require('buffer'); | ||
const errors = require('internal/errors'); | ||
const { WriteWrap } = process.binding('stream_wrap'); | ||
|
||
const errnoException = errors.errnoException; | ||
|
||
function handleWriteReq(req, data, encoding) { | ||
const { handle } = req; | ||
|
||
switch (encoding) { | ||
case 'buffer': | ||
return handle.writeBuffer(req, data); | ||
case 'latin1': | ||
case 'binary': | ||
return handle.writeLatin1String(req, data); | ||
case 'utf8': | ||
case 'utf-8': | ||
return handle.writeUtf8String(req, data); | ||
case 'ascii': | ||
return handle.writeAsciiString(req, data); | ||
case 'ucs2': | ||
case 'ucs-2': | ||
case 'utf16le': | ||
case 'utf-16le': | ||
return handle.writeUcs2String(req, data); | ||
default: | ||
return handle.writeBuffer(req, Buffer.from(data, encoding)); | ||
} | ||
} | ||
|
||
function createWriteWrap(handle, oncomplete) { | ||
var req = new WriteWrap(); | ||
|
||
req.handle = handle; | ||
req.oncomplete = oncomplete; | ||
req.async = false; | ||
|
||
return req; | ||
} | ||
|
||
function writevGeneric(self, req, data, cb) { | ||
var allBuffers = data.allBuffers; | ||
var chunks; | ||
var i; | ||
if (allBuffers) { | ||
chunks = data; | ||
for (i = 0; i < data.length; i++) | ||
data[i] = data[i].chunk; | ||
} else { | ||
chunks = new Array(data.length << 1); | ||
for (i = 0; i < data.length; i++) { | ||
var entry = data[i]; | ||
chunks[i * 2] = entry.chunk; | ||
chunks[i * 2 + 1] = entry.encoding; | ||
} | ||
} | ||
var err = req.handle.writev(req, chunks, allBuffers); | ||
|
||
// Retain chunks | ||
if (err === 0) req._chunks = chunks; | ||
|
||
if (err) | ||
return self.destroy(errnoException(err, 'write', req.error), cb); | ||
} | ||
|
||
function writeGeneric(self, req, data, encoding, cb) { | ||
var err = handleWriteReq(req, data, encoding); | ||
|
||
if (err) | ||
return self.destroy(errnoException(err, 'write', req.error), cb); | ||
} | ||
|
||
module.exports = { | ||
createWriteWrap, | ||
writevGeneric, | ||
writeGeneric | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters