Skip to content

Commit

Permalink
feat: relay ipc messages (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 27, 2023
1 parent 83dea6e commit 4f4a99a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { constants as osConstants } from 'os';
import type { ChildProcess } from 'child_process';
import type { ChildProcess, Serializable } from 'child_process';
import type { Server } from 'net';
import { cli } from 'cleye';
import {
Expand Down Expand Up @@ -212,6 +212,18 @@ cli({

relaySignals(childProcess, ipc);

if (process.send) {
childProcess.on('message', (message) => {
process.send!(message);
});
}

if (childProcess.send) {
process.on('message', (message) => {
childProcess.send(message as Serializable);
});
}

childProcess.on(
'close',
(exitCode) => {
Expand Down
6 changes: 5 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ export const run = (
'inherit', // stdin
'inherit', // stdout
'inherit', // stderr
'ipc', // parent-child communication
];

// If parent process spawns tsx with ipc, spawn child with ipc
if (process.send) {
stdio.push('ipc');
}

if (options) {
if (options.noCache) {
environment.TSX_DISABLE_CACHE = '1';
Expand Down
30 changes: 28 additions & 2 deletions tests/specs/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { setTimeout } from 'timers/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import packageJson from '../../package.json';
import { tsxPath } from '../utils/tsx.js';
import { ptyShell, isWindows } from '../utils/pty-shell/index';
import { expectMatchInOrder } from '../utils/expect-match-in-order.js';
import type { NodeApis } from '../utils/tsx.js';
import { tsxPath, type NodeApis } from '../utils/tsx.js';
import { isProcessAlive } from '../utils/is-process-alive.js';

export default testSuite(({ describe }, node: NodeApis) => {
Expand Down Expand Up @@ -344,5 +343,32 @@ export default testSuite(({ describe }, node: NodeApis) => {
}, 10_000);
});
});

test('relays ipc message to child and back', async ({ onTestFinish }) => {
const fixture = await createFixture({
'file.js': `
process.on('message', (received) => {
process.send('goodbye');
process.exit();
});
`,
});

onTestFinish(async () => await fixture.rm());

const tsxProcess = tsx(['file.js'], {
cwd: fixture.path,
stdio: ['ipc'],
reject: false,
});

tsxProcess.send('hello');
const received = await new Promise((resolve) => {
tsxProcess.once('message', resolve);
});
expect(received).toBe('goodbye');

await tsxProcess;
});
});
});
10 changes: 7 additions & 3 deletions tests/utils/tsx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'url';
import { execaNode } from 'execa';
import { execaNode, type NodeOptions } from 'execa';
import getNode from 'get-node';
import { compareNodeVersion, type Version } from './node-features.js';

Expand Down Expand Up @@ -62,12 +62,11 @@ export const createNode = async (

tsx: (
args: string[],
cwd?: string,
cwdOrOptions?: string | NodeOptions,
) => execaNode(
tsxPath,
args,
{
cwd,
env: {
TSX_DISABLE_CACHE: '1',
DEBUG: '1',
Expand All @@ -76,6 +75,11 @@ export const createNode = async (
nodeOptions: [],
reject: false,
all: true,
...(
typeof cwdOrOptions === 'string'
? { cwd: cwdOrOptions }
: cwdOrOptions
),
},
),

Expand Down

0 comments on commit 4f4a99a

Please sign in to comment.