doc: note caveats in process message serialization

The message sent using process.send() goes through JSON
serialization and parsing, which could lead to surprising behaviors.
This commit elaborate a bit more on this and add a link to
the notes about these caveats in the ECMAScript specification.

PR-URL: https://github.com/nodejs/node/pull/12963
Refs: https://github.com/nodejs/node/issues/12497
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung authored and James M Snell committed 2017-09-25 12:55:12 -07:00
1 parent 9fc8eddfdd
commit 6cd64f36bc
2 files changed
+20 -4

No files matched your search

Regular → Executable
+12 -1
View File
@@ -891,6 +891,10 @@ added: v0.5.9
The `'message'` event is triggered when a child process uses [`process.send()`][]
to send messages.
*Note*: The message goes through JSON serialization and parsing. The resulting
message might not be the same as what is originally sent. See notes in
[the `JSON.stringify()` specification][`JSON.stringify` spec].
<a name="child_process_child_channel"></a>
### subprocess.channel
<!-- YAML
@@ -1056,6 +1060,10 @@ be used to send messages to the child process. When the child process is a
Node.js instance, these messages can be received via the
[`process.on('message')`][] event.
*Note*: The message goes through JSON serialization and parsing. The resulting
message might not be the same as what is originally sent. See notes in
[the `JSON.stringify()` specification][`JSON.stringify` spec].
For example, in the parent script:
```js
@@ -1066,6 +1074,7 @@ n.on('message', (m) => {
console.log('PARENT got message:', m);
});
// Causes the child to print: CHILD got message: { hello: 'world' }
n.send({ hello: 'world' });
```
@@ -1076,7 +1085,8 @@ process.on('message', (m) => {
console.log('CHILD got message:', m);
});
process.send({ foo: 'bar' });
// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }
process.send({ foo: 'bar', baz: NaN });
```
Child Node.js processes will have a [`process.send()`][] method of their own that
@@ -1329,6 +1339,7 @@ unavailable.
[`ChildProcess`]: #child_process_child_process
[`Error`]: errors.html#errors_class_error
[`EventEmitter`]: events.html#events_class_eventemitter
[`JSON.stringify` spec]: https://tc39.github.io/ecma262/#sec-json.stringify
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[`subprocess.connected`]: #child_process_subprocess_connected
[`subprocess.disconnect()`]: #child_process_subprocess_disconnect
+8 -3
View File
@@ -90,10 +90,13 @@ message sent by a parent process using [`childprocess.send()`][] is received by
the child process.
The listener callback is invoked with the following arguments:
* `message` {Object} a parsed JSON object or primitive value
* `message` {Object} a parsed JSON object or primitive value.
* `sendHandle` {Handle object} a [`net.Socket`][] or [`net.Server`][] object, or
undefined.
*Note*: The message goes through JSON serialization and parsing. The resulting
message might not be the same as what is originally sent. See notes in
[the `JSON.stringify()` specification][`JSON.stringify` spec].
### Event: 'rejectionHandled'
<!-- YAML
@@ -1430,8 +1433,9 @@ used to send messages to the parent process. Messages will be received as a
If Node.js was not spawned with an IPC channel, `process.send()` will be
`undefined`.
*Note*: This function uses [`JSON.stringify()`][] internally to serialize the
`message`.
*Note*: The message goes through JSON serialization and parsing. The resulting
message might not be the same as what is originally sent. See notes in
[the `JSON.stringify()` specification][`JSON.stringify` spec].
## process.setegid(id)
<!-- YAML
@@ -1833,6 +1837,7 @@ cases:
[`ChildProcess`]: child_process.html#child_process_class_childprocess
[`Error`]: errors.html#errors_class_error
[`EventEmitter`]: events.html#events_class_eventemitter
[`JSON.stringify` spec]: https://tc39.github.io/ecma262/#sec-json.stringify
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[`console.error()`]: console.html#console_console_error_data_args
[`console.log()`]: console.html#console_console_log_data_args