• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const child_process = require('child_process');
5const { once } = require('events');
6
7if (process.argv[2] !== 'child') {
8  for (const value of [null, 42, Infinity, 'foo']) {
9    assert.throws(() => {
10      child_process.spawn(process.execPath, [], { serialization: value });
11    }, {
12      code: 'ERR_INVALID_OPT_VALUE',
13      message: `The value "${value}" is invalid ` +
14      'for option "options.serialization". ' +
15      "Must be one of: undefined, 'json', 'advanced'"
16    });
17  }
18
19  (async () => {
20    const cp = child_process.spawn(process.execPath, [__filename, 'child'],
21                                   {
22                                     stdio: ['ipc', 'inherit', 'inherit'],
23                                     serialization: 'advanced'
24                                   });
25
26    const circular = {};
27    circular.circular = circular;
28    for await (const message of [
29      { uint8: new Uint8Array(4) },
30      { float64: new Float64Array([ Math.PI ]) },
31      { buffer: Buffer.from('Hello!') },
32      { map: new Map([{ a: 1 }, { b: 2 }]) },
33      { bigInt: 1337n },
34      circular,
35      new Error('Something went wrong'),
36      new RangeError('Something range-y went wrong'),
37    ]) {
38      cp.send(message);
39      const [ received ] = await once(cp, 'message');
40      assert.deepStrictEqual(received, message);
41    }
42
43    cp.disconnect();
44  })().then(common.mustCall());
45} else {
46  process.on('message', (msg) => process.send(msg));
47}
48