From e9e36a2932e8a824249202b7e1b7aaaa7bf40ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Wed, 15 May 2024 05:18:41 -0300 Subject: [PATCH] fix(Bun): close only listening PIDs (#245) Allows to use startScript with Bun by passing a port to end method (the same solution as Deno). --- src/modules/create-service.ts | 4 ---- src/services/pid.ts | 16 ++++++++++++++-- test/e2e/node/background-process.test.ts | 2 -- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/modules/create-service.ts b/src/modules/create-service.ts index 81fe717f..19b81b7b 100644 --- a/src/modules/create-service.ts +++ b/src/modules/create-service.ts @@ -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, diff --git a/src/services/pid.ts b/src/services/pid.ts index 5985e515..eadba778 100644 --- a/src/services/pid.ts +++ b/src/services/pid.ts @@ -35,7 +35,13 @@ export const findPID = { new Promise((resolve) => { try { const PIDs: Set = 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); @@ -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); + } }); }); @@ -80,4 +91,5 @@ export const findPID = { } catch {} }), }; + /* c8 ignore stop */ diff --git a/test/e2e/node/background-process.test.ts b/test/e2e/node/background-process.test.ts index b466fa0c..a5438a32 100644 --- a/test/e2e/node/background-process.test.ts +++ b/test/e2e/node/background-process.test.ts @@ -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}`, {