summaryrefslogtreecommitdiff
path: root/lib/internal/net.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2017-12-31 00:27:56 +0100
committerAnna Henningsen <anna@addaleax.net>2018-01-14 14:41:11 +0100
commit8b751f7eb7b05a0b27f52e2288a636fdd78e9ecb (patch)
tree1c1040fbf33fa73344e0ee152c12ad7695f92f4c /lib/internal/net.js
parentc84582cbb6362fa8b2eb2d3bc153617fef2982d1 (diff)
downloadandroid-node-v8-8b751f7eb7b05a0b27f52e2288a636fdd78e9ecb.tar.gz
android-node-v8-8b751f7eb7b05a0b27f52e2288a636fdd78e9ecb.tar.bz2
android-node-v8-8b751f7eb7b05a0b27f52e2288a636fdd78e9ecb.zip
process: use more direct sync I/O for stdio
This avoids routing writes through the full LibuvStreamWrap write machinery. In particular, it enables the next commit, because otherwise the callback passed to `_write()` would not be called synchronously for pipes on Windows (because the latter does not support `uv_try_write()`, even for blocking I/O). PR-URL: https://github.com/nodejs/node/pull/18019 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib/internal/net.js')
-rw-r--r--lib/internal/net.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/internal/net.js b/lib/internal/net.js
index d4558cfca8..847539d576 100644
--- a/lib/internal/net.js
+++ b/lib/internal/net.js
@@ -1,5 +1,8 @@
'use strict';
+const Buffer = require('buffer').Buffer;
+const { writeBuffer } = process.binding('fs');
+
// Check that the port number is not NaN when coerced to a number,
// is an integer and that it falls within the legal range of port numbers.
function isLegalPort(port) {
@@ -9,7 +12,28 @@ function isLegalPort(port) {
return +port === (+port >>> 0) && port <= 0xFFFF;
}
+function makeSyncWrite(fd) {
+ return function(chunk, enc, cb) {
+ if (enc !== 'buffer')
+ chunk = Buffer.from(chunk, enc);
+
+ this._bytesDispatched += chunk.length;
+
+ try {
+ writeBuffer(fd, chunk, 0, chunk.length, null);
+ } catch (ex) {
+ // Legacy: net writes have .code === .errno, whereas writeBuffer gives the
+ // raw errno number in .errno.
+ if (typeof ex.code === 'string')
+ ex.errno = ex.code;
+ return cb(ex);
+ }
+ cb();
+ };
+}
+
module.exports = {
isLegalPort,
+ makeSyncWrite,
normalizedArgsSymbol: Symbol('normalizedArgs')
};