| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 18abb3c commit 1952844
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -750,10 +750,11 @@ console.log(`Spawned child pid: ${grep.pid}`); | |||
| 750 | 750 | grep.stdin.end(); | |
| 751 | 751 | ``` | |
| 752 | 752 | ||
| 753 | - ### child.send(message[, sendHandle][, callback]) | ||
| 753 | + ### child.send(message[, sendHandle[, options]][, callback]) | ||
| 754 | 754 | ||
| 755 | 755 | * `message` {Object} | |
| 756 | 756 | * `sendHandle` {Handle} | |
| 757 | + * `options` {Object} | ||
| 757 | 758 | * `callback` {Function} | |
| 758 | 759 | * Return: {Boolean} | |
| 759 | 760 | ||
@@ -801,6 +802,9 @@ passing a TCP server or socket object to the child process. The child will | |||
| 801 | 802 | receive the object as the second argument passed to the callback function | |
| 802 | 803 | registered on the `process.on('message')` event. | |
| 803 | 804 | ||
| 805 | + The `options` argument, if present, is an object used to parameterize the | ||
| 806 | + sending of certain types of handles. | ||
| 807 | + | ||
| 804 | 808 | The optional `callback` is a function that is invoked after the message is | |
| 805 | 809 | sent but before the child may have received it. The function is called with a | |
| 806 | 810 | single argument: `null` on success, or an [`Error`][] object on failure. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -825,10 +825,11 @@ In custom builds from non-release versions of the source tree, only the | |||
| 825 | 825 | `name` property may be present. The additional properties should not be | |
| 826 | 826 | relied upon to exist. | |
| 827 | 827 | ||
| 828 | - ## process.send(message[, sendHandle][, callback]) | ||
| 828 | + ## process.send(message[, sendHandle[, options]][, callback]) | ||
| 829 | 829 | ||
| 830 | 830 | * `message` {Object} | |
| 831 | 831 | * `sendHandle` {Handle object} | |
| 832 | + * `options` {Object} | ||
| 832 | 833 | * `callback` {Function} | |
| 833 | 834 | * Return: {Boolean} | |
| 834 | 835 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,7 @@ const handleConversion = { | |||
| 35 | 35 | 'net.Native': { | |
| 36 | 36 | simultaneousAccepts: true, | |
| 37 | 37 | ||
| 38 | - send: function(message, handle) { | ||
| 38 | + send: function(message, handle, options) { | ||
| 39 | 39 | return handle; | |
| 40 | 40 | }, | |
| 41 | 41 | ||
@@ -47,7 +47,7 @@ const handleConversion = { | |||
| 47 | 47 | 'net.Server': { | |
| 48 | 48 | simultaneousAccepts: true, | |
| 49 | 49 | ||
| 50 | - send: function(message, server) { | ||
| 50 | + send: function(message, server, options) { | ||
| 51 | 51 | return server._handle; | |
| 52 | 52 | }, | |
| 53 | 53 | ||
@@ -60,7 +60,7 @@ const handleConversion = { | |||
| 60 | 60 | }, | |
| 61 | 61 | ||
| 62 | 62 | 'net.Socket': { | |
| 63 | - send: function(message, socket) { | ||
| 63 | + send: function(message, socket, options) { | ||
| 64 | 64 | if (!socket._handle) | |
| 65 | 65 | return; | |
| 66 | 66 | ||
@@ -90,7 +90,7 @@ const handleConversion = { | |||
| 90 | 90 | return handle; | |
| 91 | 91 | }, | |
| 92 | 92 | ||
| 93 | - postSend: function(handle) { | ||
| 93 | + postSend: function(handle, options) { | ||
| 94 | 94 | // Close the Socket handle after sending it | |
| 95 | 95 | if (handle) | |
| 96 | 96 | handle.close(); | |
@@ -117,7 +117,7 @@ const handleConversion = { | |||
| 117 | 117 | 'dgram.Native': { | |
| 118 | 118 | simultaneousAccepts: false, | |
| 119 | 119 | ||
| 120 | - send: function(message, handle) { | ||
| 120 | + send: function(message, handle, options) { | ||
| 121 | 121 | return handle; | |
| 122 | 122 | }, | |
| 123 | 123 | ||
@@ -129,7 +129,7 @@ const handleConversion = { | |||
| 129 | 129 | 'dgram.Socket': { | |
| 130 | 130 | simultaneousAccepts: false, | |
| 131 | 131 | ||
| 132 | - send: function(message, socket) { | ||
| 132 | + send: function(message, socket, options) { | ||
| 133 | 133 | message.dgramType = socket.type; | |
| 134 | 134 | ||
| 135 | 135 | return socket._handle; | |
@@ -466,7 +466,7 @@ function setupChannel(target, channel) { | |||
| 466 | 466 | target._handleQueue = null; | |
| 467 | 467 | ||
| 468 | 468 | queue.forEach(function(args) { | |
| 469 | - target._send(args.message, args.handle, false, args.callback); | ||
| 469 | + target._send(args.message, args.handle, args.options, args.callback); | ||
| 470 | 470 | }); | |
| 471 | 471 | ||
| 472 | 472 | // Process a pending disconnect (if any). | |
@@ -498,13 +498,23 @@ function setupChannel(target, channel) { | |||
| 498 | 498 | }); | |
| 499 | 499 | }); | |
| 500 | 500 | ||
| 501 | - target.send = function(message, handle, callback) { | ||
| 501 | + target.send = function(message, handle, options, callback) { | ||
| 502 | 502 | if (typeof handle === 'function') { | |
| 503 | 503 | callback = handle; | |
| 504 | 504 | handle = undefined; | |
| 505 | + options = undefined; | ||
| 506 | + } else if (typeof options === 'function') { | ||
| 507 | + callback = options; | ||
| 508 | + options = undefined; | ||
| 509 | + } else if (options !== undefined && | ||
| 510 | + (options === null || typeof options !== 'object')) { | ||
| 511 | + throw new TypeError('"options" argument must be an object'); | ||
| 505 | 512 | } | |
| 513 | + | ||
| 514 | + options = Object.assign({swallowErrors: false}, options); | ||
| 515 | + | ||
| 506 | 516 | if (this.connected) { | |
| 507 | - return this._send(message, handle, false, callback); | ||
| 517 | + return this._send(message, handle, options, callback); | ||
| 508 | 518 | } | |
| 509 | 519 | const ex = new Error('channel closed'); | |
| 510 | 520 | if (typeof callback === 'function') { | |
@@ -515,12 +525,17 @@ function setupChannel(target, channel) { | |||
| 515 | 525 | return false; | |
| 516 | 526 | }; | |
| 517 | 527 | ||
| 518 | - target._send = function(message, handle, swallowErrors, callback) { | ||
| 528 | + target._send = function(message, handle, options, callback) { | ||
| 519 | 529 | assert(this.connected || this._channel); | |
| 520 | 530 | ||
| 521 | 531 | if (message === undefined) | |
| 522 | 532 | throw new TypeError('"message" argument cannot be undefined'); | |
| 523 | 533 | ||
| 534 | + // Support legacy function signature | ||
| 535 | + if (typeof options === 'boolean') { | ||
| 536 | + options = {swallowErrors: options}; | ||
| 537 | + } | ||
| 538 | + | ||
| 524 | 539 | // package messages with a handle object | |
| 525 | 540 | if (handle) { | |
| 526 | 541 | // this message will be handled by an internalMessage event handler | |
@@ -549,6 +564,7 @@ function setupChannel(target, channel) { | |||
| 549 | 564 | this._handleQueue.push({ | |
| 550 | 565 | callback: callback, | |
| 551 | 566 | handle: handle, | |
| 567 | + options: options, | ||
| 552 | 568 | message: message.msg, | |
| 553 | 569 | }); | |
| 554 | 570 | return this._handleQueue.length === 1; | |
@@ -557,8 +573,10 @@ function setupChannel(target, channel) { | |||
| 557 | 573 | var obj = handleConversion[message.type]; | |
| 558 | 574 | ||
| 559 | 575 | // convert TCP object to native handle object | |
| 560 | - handle = | ||
| 561 | - handleConversion[message.type].send.call(target, message, handle); | ||
| 576 | + handle = handleConversion[message.type].send.call(target, | ||
| 577 | + message, | ||
| 578 | + handle, | ||
| 579 | + options); | ||
| 562 | 580 | ||
| 563 | 581 | // If handle was sent twice, or it is impossible to get native handle | |
| 564 | 582 | // out of it - just send a text without the handle. | |
@@ -575,6 +593,7 @@ function setupChannel(target, channel) { | |||
| 575 | 593 | this._handleQueue.push({ | |
| 576 | 594 | callback: callback, | |
| 577 | 595 | handle: null, | |
| 596 | + options: options, | ||
| 578 | 597 | message: message, | |
| 579 | 598 | }); | |
| 580 | 599 | return this._handleQueue.length === 1; | |
@@ -593,7 +612,7 @@ function setupChannel(target, channel) { | |||
| 593 | 612 | if (this.async === true) | |
| 594 | 613 | control.unref(); | |
| 595 | 614 | if (obj && obj.postSend) | |
| 596 | - obj.postSend(handle); | ||
| 615 | + obj.postSend(handle, options); | ||
| 597 | 616 | if (typeof callback === 'function') | |
| 598 | 617 | callback(null); | |
| 599 | 618 | }; | |
@@ -605,9 +624,9 @@ function setupChannel(target, channel) { | |||
| 605 | 624 | } else { | |
| 606 | 625 | // Cleanup handle on error | |
| 607 | 626 | if (obj && obj.postSend) | |
| 608 | - obj.postSend(handle); | ||
| 627 | + obj.postSend(handle, options); | ||
| 609 | 628 | ||
| 610 | - if (!swallowErrors) { | ||
| 629 | + if (!options.swallowErrors) { | ||
| 611 | 630 | const ex = errnoException(err, 'write'); | |
| 612 | 631 | if (typeof callback === 'function') { | |
| 613 | 632 | process.nextTick(callback, ex); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,25 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const cp = require('child_process'); | ||
| 5 | + | ||
| 6 | + function noop() {} | ||
| 7 | + | ||
| 8 | + function fail(proc, args) { | ||
| 9 | + assert.throws(() => { | ||
| 10 | + proc.send.apply(proc, args); | ||
| 11 | + }, /"options" argument must be an object/); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + let target = process; | ||
| 15 | + | ||
| 16 | + if (process.argv[2] !== 'child') | ||
| 17 | + target = cp.fork(__filename, ['child']); | ||
| 18 | + | ||
| 19 | + fail(target, ['msg', null, null]); | ||
| 20 | + fail(target, ['msg', null, '']); | ||
| 21 | + fail(target, ['msg', null, 'foo']); | ||
| 22 | + fail(target, ['msg', null, 0]); | ||
| 23 | + fail(target, ['msg', null, NaN]); | ||
| 24 | + fail(target, ['msg', null, 1]); | ||
| 25 | + fail(target, ['msg', null, null, noop]); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments