Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: relay ipc messages #413

Merged
merged 42 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d0caef5
wip
privatenumber Nov 24, 2023
65e64b5
wip
privatenumber Nov 25, 2023
b50adcc
wip
privatenumber Nov 25, 2023
f42269c
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 25, 2023
1fd9e2f
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 25, 2023
35adac9
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 25, 2023
4b60826
wip
privatenumber Nov 26, 2023
5dd6cd8
wip
privatenumber Nov 26, 2023
e2b4678
wip
privatenumber Nov 26, 2023
b48b7fb
wip
privatenumber Nov 26, 2023
3ee6546
wip
privatenumber Nov 26, 2023
7d6c6fd
wip
privatenumber Nov 26, 2023
6e990ab
wip
privatenumber Nov 26, 2023
d63d2a8
wip
privatenumber Nov 26, 2023
f1f4d70
wip
privatenumber Nov 26, 2023
bf4595e
wip
privatenumber Nov 26, 2023
f473f79
wip
privatenumber Nov 26, 2023
75cefc7
wip
privatenumber Nov 26, 2023
7ade232
wip
privatenumber Nov 26, 2023
c6511d7
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 26, 2023
73dfb1e
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 26, 2023
4a4ef67
Merge branch 'develop' of github.com:esbuild-kit/tsx into relay-ipc-m…
privatenumber Nov 26, 2023
ff01de6
wip
privatenumber Nov 26, 2023
13ec705
wip
privatenumber Nov 26, 2023
ce3f560
wip
privatenumber Nov 26, 2023
cf704e3
wip
privatenumber Nov 26, 2023
b1ca54b
wip
privatenumber Nov 26, 2023
1869feb
wip
privatenumber Nov 27, 2023
b9911eb
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 27, 2023
ae1505c
style: lint fix
privatenumber Nov 27, 2023
1fbea48
Merge branch 'develop' of github.com:esbuild-kit/tsx into private-ipc
privatenumber Nov 27, 2023
b8d468f
wip
privatenumber Nov 27, 2023
53b868b
Merge branch 'develop' of github.com:esbuild-kit/tsx into relay-ipc-m…
privatenumber Nov 27, 2023
3f540b7
Merge branch 'private-ipc' of github.com:esbuild-kit/tsx into relay-i…
privatenumber Nov 27, 2023
617bc63
wip
privatenumber Nov 27, 2023
8e4f9d6
Merge branch 'private-ipc' of github.com:esbuild-kit/tsx into relay-i…
privatenumber Nov 27, 2023
cdb382c
wip
privatenumber Nov 27, 2023
6c970ec
wip
privatenumber Nov 27, 2023
254bcc1
wip
privatenumber Nov 27, 2023
9ad4b99
Merge branch 'private-ipc' into relay-ipc-messages
privatenumber Nov 27, 2023
4ee891d
Merge branch 'develop' of github.com:esbuild-kit/tsx into relay-ipc-m…
privatenumber Nov 27, 2023
64fb783
wip
privatenumber Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -142,7 +142,7 @@
argv.showHelp({
description: 'Node.js runtime enhanced with esbuild for loading TypeScript & ESM',
});
console.log(`${'-'.repeat(45)}\n`);

Check warning on line 145 in src/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement
}

const interceptFlags = {
Expand Down Expand Up @@ -212,6 +212,18 @@

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
Loading