• 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    });
16  }
17
18  (async () => {
19    const cp = child_process.spawn(process.execPath, [__filename, 'child'],
20                                   {
21                                     stdio: ['ipc', 'inherit', 'inherit'],
22                                     serialization: 'advanced'
23                                   });
24
25    const circular = {};
26    circular.circular = circular;
27    for await (const message of [
28      { uint8: new Uint8Array(4) },
29      { float64: new Float64Array([ Math.PI ]) },
30      { buffer: Buffer.from('Hello!') },
31      { map: new Map([{ a: 1 }, { b: 2 }]) },
32      { bigInt: 1337n },
33      circular,
34      new Error('Something went wrong'),
35      new RangeError('Something range-y went wrong'),
36    ]) {
37      cp.send(message);
38      const [ received ] = await once(cp, 'message');
39      assert.deepStrictEqual(received, message);
40    }
41
42    cp.disconnect();
43  })().then(common.mustCall());
44} else {
45  process.on('message', (msg) => process.send(msg));
46}
47