Skip to content

Commit 08e8143

Browse files
committed
fix: remove Bun specific exceptions from Node compatible code
1 parent 968d529 commit 08e8143

14 files changed

+25
-91
lines changed

.github/workflows/bun.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,12 @@ on:
1010
jobs:
1111
test:
1212
name: Test (Bun)
13-
strategy:
14-
fail-fast: false
15-
matrix:
16-
os: [ubuntu-latest, macos-latest, windows-latest]
17-
bun-version: [latest]
1813

19-
runs-on: ${{matrix.os}}
14+
runs-on: ubuntu-latest
2015
steps:
2116
- uses: actions/checkout@v4
2217

23-
- name: Use Bun ${{ matrix.bun-version }}
24-
uses: oven-sh/setup-bun@v2
25-
with:
26-
bun-version: ${{ matrix.bun-version }}
18+
- uses: oven-sh/setup-bun@v2
2719

2820
- uses: pnpm/action-setup@v2
2921

@@ -33,5 +25,15 @@ jobs:
3325
- name: Build
3426
run: pnpm build
3527

28+
# Exclude tests that Bun is not compatible with
3629
- name: Test
37-
run: bun run --bun test run
30+
run: >-
31+
bun run --bun test run --fileParallelism=false
32+
--exclude **/async-context.test.ts
33+
--exclude **/atomic.test.ts
34+
--exclude **/pool-destroy.test.ts
35+
--exclude **/resource-limits.test.ts
36+
--exclude **/teardown.test.ts
37+
--exclude **/termination.test.ts
38+
--exclude **/uncaught-exception-from-handler.test.ts
39+
--exclude **/worker-stdio.test.ts

src/index.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
} from './common'
3636
import ThreadWorker from './runtime/thread-worker'
3737
import ProcessWorker from './runtime/process-worker'
38-
import { isBun } from './utils'
3938

4039
declare global {
4140
namespace NodeJS {
@@ -1162,19 +1161,6 @@ class Tinypool extends EventEmitterAsyncResource {
11621161
)
11631162
}
11641163

1165-
if (isBun) {
1166-
if (options.useAtomics) {
1167-
throw new Error('options.useAtomics can not be set in Bun runtime')
1168-
}
1169-
1170-
// ::bunternal:: [NotImplementedError]: worker_threads.Worker option "resourceLimits" is not yet implemented in Bun.
1171-
if (options.resourceLimits) {
1172-
throw new Error('options.resourceLimits can not be set in Bun runtime.')
1173-
}
1174-
1175-
options.useAtomics = false
1176-
}
1177-
11781164
super({ ...options, name: 'Tinypool' })
11791165

11801166
if (

src/runtime/process-worker.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ export default class ProcessWorker implements TinypoolWorker {
150150

151151
// The forked child_process adds event listener on `process.on('message)`.
152152
// This requires manual unreffing of its channel.
153-
// Bun runtime does not have `unref` for the channel
154153
this.process.channel?.unref?.()
155154

156155
if (hasUnref(this.process.stdout)) {

src/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ export function stderr(): NodeJS.WriteStream | undefined {
77
// @ts-expect-error Node.js maps process.stderr to console._stderr
88
return console._stderr || process.stderr || undefined
99
}
10-
11-
export const isBun = 'bun' in process.versions

test/async-context.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import { createHook, executionAsyncId } from 'node:async_hooks'
22
import { Tinypool } from 'tinypool'
33
import { dirname, resolve } from 'node:path'
44
import { fileURLToPath } from 'node:url'
5-
import { isBun } from './utils'
65

76
const __dirname = dirname(fileURLToPath(import.meta.url))
87

9-
test('postTask() calls the correct async hooks', async ({ skip }) => {
10-
// Async context tracking via createHook is highly experimental and even suggested by NodeJS migrate away from this.
11-
// https://nodejs.org/docs/latest/api/async_hooks.html#async-hooks
12-
// Experimental. Please migrate away from this API, if you can. We do not recommend using the createHook, AsyncHook,
13-
// and executionAsyncResource APIs as they have usability issues, safety risks, and performance implications.
14-
if (isBun) return skip('AsyncHooks are not yet supported in Bun')
15-
8+
test('postTask() calls the correct async hooks', async () => {
169
let taskId: number
1710
let initCalls = 0
1811
let beforeCalls = 0

test/atomic.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import Tinypool from 'tinypool'
22
import { dirname, resolve } from 'node:path'
33
import { fileURLToPath } from 'node:url'
4-
import { isBun } from './utils'
54

65
const __dirname = dirname(fileURLToPath(import.meta.url))
76

8-
test('coverage test for Atomics optimization', async ({ skip }) => {
9-
if (isBun) return skip('Atomics are not supported in Bun')
10-
7+
test('coverage test for Atomics optimization', async () => {
118
const pool = new Tinypool({
129
filename: resolve(__dirname, 'fixtures/notify-then-sleep-or.js'),
1310
minThreads: 2,
@@ -70,9 +67,7 @@ function popcount8(v: number): number {
7067
return v
7168
}
7269

73-
test('avoids unbounded recursion', async ({ skip }) => {
74-
if (isBun) return skip('Atomics are not yet supported in Bun')
75-
70+
test('avoids unbounded recursion', async () => {
7671
const pool = new Tinypool({
7772
filename: resolve(__dirname, 'fixtures/simple-isworkerthread.js'),
7873
minThreads: 2,

test/pool-destroy.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createHook } from 'node:async_hooks'
22
import { dirname, resolve } from 'node:path'
33
import { Tinypool } from 'tinypool'
44
import { fileURLToPath } from 'node:url'
5-
import { isBun } from './utils'
65

76
const __dirname = dirname(fileURLToPath(import.meta.url))
87

@@ -30,13 +29,7 @@ test('destroy after initializing should work (#43)', async () => {
3029
await promise
3130
})
3231

33-
test('cleans up async resources', async ({ skip }) => {
34-
// Async context tracking via createHook is highly experimental and even suggested by NodeJS migrate away from this.
35-
// https://nodejs.org/docs/latest/api/async_hooks.html#async-hooks
36-
// Experimental. Please migrate away from this API, if you can. We do not recommend using the createHook, AsyncHook,
37-
// and executionAsyncResource APIs as they have usability issues, safety risks, and performance implications.
38-
if (isBun) return skip('AsyncHooks are not yet supported in Bun')
39-
32+
test('cleans up async resources', async () => {
4033
let onCleanup = () => {}
4134
const waitForCleanup = new Promise<void>((r) => (onCleanup = r))
4235
const timeout = setTimeout(() => {

test/resource-limits.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { dirname, resolve } from 'node:path'
22
import { Tinypool } from 'tinypool'
33
import { fileURLToPath } from 'node:url'
4-
import { isBun } from './utils'
54

65
const __dirname = dirname(fileURLToPath(import.meta.url))
76

8-
test('resourceLimits causes task to reject', async ({ skip }) => {
9-
if (isBun) return skip('process resourceLimits are not yet supported in Bun')
10-
7+
test('resourceLimits causes task to reject', async () => {
118
const worker = new Tinypool({
129
filename: resolve(__dirname, 'fixtures/resource-limits.js'),
1310
resourceLimits: {
@@ -74,7 +71,7 @@ describe.each(['worker_threads', 'child_process'] as const)('%s', (runtime) => {
7471
}
7572

7673
// Test setup should not reach max memory on first round
77-
expect(rounds).toBeGreaterThan(0)
74+
expect(rounds).toBeGreaterThan(1)
7875

7976
// Thread should have been recycled
8077
expect(finalThreadId).not.toBe(originalWorkerId)

test/teardown.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ test('isolated workers call teardown on worker recycle', async () => {
1616

1717
for (const _ of [1, 2, 3, 4, 5]) {
1818
const { port1, port2 } = new MessageChannel()
19-
port2.start()
2019
const promise = new Promise((resolve) => port2.on('message', resolve))
2120

2221
const output = await pool.run({ port: port1 }, { transferList: [port1] })
@@ -42,10 +41,10 @@ test('non-isolated workers call teardown on worker recycle', async () => {
4241
}
4342

4443
const { port1, port2 } = new MessageChannel()
45-
port2.start()
46-
port2.on('message', unexpectedTeardown)
4744

4845
for (const index of [1, 2, 3, 4, 5]) {
46+
port2.on('message', unexpectedTeardown)
47+
4948
const transferList = index === 1 ? [port1] : []
5049

5150
const output = await pool.run({ port: transferList[0] }, { transferList })
@@ -56,5 +55,5 @@ test('non-isolated workers call teardown on worker recycle', async () => {
5655
const promise = new Promise((resolve) => port2.on('message', resolve))
5756

5857
await pool.destroy()
59-
await expect(promise).resolves.toEqual(`Teardown of task #5`)
58+
await expect(promise).resolves.toMatchInlineSnapshot(`"Teardown of task #5"`)
6059
})

test/termination.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { dirname, resolve } from 'node:path'
22
import { Tinypool } from 'tinypool'
33
import { fileURLToPath } from 'node:url'
4-
import { isBun } from './utils'
54

65
const __dirname = dirname(fileURLToPath(import.meta.url))
76
const cleanups: (() => Promise<unknown>)[] = []
@@ -58,12 +57,7 @@ test('writing to terminating worker does not crash', async () => {
5857
await destroyed
5958
})
6059

61-
test('recycling workers while closing pool does not crash', async ({
62-
skip,
63-
}) => {
64-
// TODO: Need to debug the weird issue for this test
65-
if (isBun) return skip()
66-
60+
test('recycling workers while closing pool does not crash', async () => {
6761
const pool = new Tinypool({
6862
runtime: 'child_process',
6963
filename: resolve(__dirname, 'fixtures/nested-pool.mjs'),

0 commit comments

Comments
 (0)