Skip to content

Commit

Permalink
fix(Bun): close only listening PIDs (#245)
Browse files Browse the repository at this point in the history
Allows to use startScript with Bun by passing a port to end method (the same solution as Deno).
  • Loading branch information
wellwelwel authored May 15, 2024
1 parent 281699e commit e9e36a2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/modules/create-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ export const startService = async (
* By default, it uses **npm**, but you can costumize it using the `runner` option.
*
* Useful for servers, APIs, etc.
*
* ---
*
* For **Bun**, please see https://github.com/oven-sh/bun/issues/11055
*/
export const startScript = async (
script: string,
Expand Down
16 changes: 14 additions & 2 deletions src/services/pid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ export const findPID = {
new Promise((resolve) => {
try {
const PIDs: Set<number> = new Set();
const service = spawn('lsof', ['-t', '-i', `:${Number(port)}`]);
const service = spawn('lsof', [
'-t',
'-i',
`:${Number(port)}`,
'-s',
'TCP:LISTEN',
]);

service.stdout.on('data', (data: Buffer) => {
const output = data.toString().trim().split(EOL);
Expand Down Expand Up @@ -65,8 +71,13 @@ export const findPID = {

lines.map((line) => {
const tokens = line.trim().split(/\s+/);
const stateIndex = tokens.indexOf('LISTENING');

if (stateIndex !== -1 && tokens[stateIndex + 1]) {
const pid = Number(tokens[stateIndex + 1]);

PIDs.add(Number(tokens[4]));
if (!isNaN(pid)) PIDs.add(pid);
}
});
});

Expand All @@ -80,4 +91,5 @@ export const findPID = {
} catch {}
}),
};

/* c8 ignore stop */
2 changes: 0 additions & 2 deletions test/e2e/node/background-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import { getRuntime } from '../../../src/helpers/get-runtime.js';
});

await test(async () => {
if (runtime === 'bun') return;

describe('Start Script', { background: false, icon: '🔀' });

const server = await startScript(`start:${ext}`, {
Expand Down

0 comments on commit e9e36a2

Please sign in to comment.