net: fix usage of writeBuffer in makeSyncWrite

The binding writeBuffer has been changed in
https://github.com/nodejs/node/pull/19041 and it now requires
the last argument to be a context object. makeSyncWrite
was not updated accordingly, resulting assertions on Windows.
This patch fixes the usage of writeBuffer there.

Also fix errors.uvException() so error.message are no longer
enumerable, this fixes the deepStrictEqual assertion on the
error object in test-stdout-close-catch.

PR-URL: https://github.com/nodejs/node/pull/19103
Refs: https://github.com/nodejs/node/pull/19041
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Joyee Cheung 2018-03-03 15:43:14 +08:00
parent 4bfc03b57d
commit 67b5985c08
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 25 additions and 15 deletions

View File

@ -426,7 +426,23 @@ function message(key, args) {
* @returns {Error} * @returns {Error}
*/ */
function uvException(ctx) { function uvException(ctx) {
const err = new Error(); const [ code, uvmsg ] = errmap.get(ctx.errno);
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
let path;
let dest;
if (ctx.path) {
path = ctx.path.toString();
message += ` '${path}'`;
}
if (ctx.dest) {
dest = ctx.dest.toString();
message += ` -> '${dest}'`;
}
// Pass the message to the constructor instead of setting it on the object
// to make sure it is the same as the one created in C++
const err = new Error(message);
for (const prop of Object.keys(ctx)) { for (const prop of Object.keys(ctx)) {
if (prop === 'message' || prop === 'path' || prop === 'dest') { if (prop === 'message' || prop === 'path' || prop === 'dest') {
@ -435,20 +451,13 @@ function uvException(ctx) {
err[prop] = ctx[prop]; err[prop] = ctx[prop];
} }
const [ code, uvmsg ] = errmap.get(ctx.errno);
err.code = code; err.code = code;
let message = `${code}: ${uvmsg}, ${ctx.syscall}`; if (path) {
if (ctx.path) {
const path = ctx.path.toString();
message += ` '${path}'`;
err.path = path; err.path = path;
} }
if (ctx.dest) { if (dest) {
const dest = ctx.dest.toString();
message += ` -> '${dest}'`;
err.dest = dest; err.dest = dest;
} }
err.message = message;
Error.captureStackTrace(err, uvException); Error.captureStackTrace(err, uvException);
return err; return err;

View File

@ -3,6 +3,7 @@
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
const { isIPv6 } = process.binding('cares_wrap'); const { isIPv6 } = process.binding('cares_wrap');
const { writeBuffer } = process.binding('fs'); const { writeBuffer } = process.binding('fs');
const errors = require('internal/errors');
const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`); const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`);
@ -33,13 +34,13 @@ function makeSyncWrite(fd) {
this._bytesDispatched += chunk.length; this._bytesDispatched += chunk.length;
try { const ctx = {};
writeBuffer(fd, chunk, 0, chunk.length, null); writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
} catch (ex) { if (ctx.errno !== undefined) {
const ex = errors.uvException(ctx);
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the // Legacy: net writes have .code === .errno, whereas writeBuffer gives the
// raw errno number in .errno. // raw errno number in .errno.
if (typeof ex.code === 'string') ex.errno = ex.code;
ex.errno = ex.code;
return cb(ex); return cb(ex);
} }
cb(); cb();