-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Instead of using Node APIs like in #70, use Bun's native Workers API
- Loading branch information
1 parent
a5b6669
commit e7e5245
Showing
11 changed files
with
387 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.nyc_output | ||
.vscode | ||
node_modules | ||
bun.lockb | ||
dist | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import add from './add.mjs' | ||
|
||
self.onmessage = (event) => { | ||
postMessage(add(event.data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// eslint-disable-next-line no-eval | ||
export default function (code) { | ||
return eval(code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as path from 'path' | ||
import { fileURLToPath } from 'url' | ||
import { Tinypool } from '../dist/esm' | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
describe('Bun Workers', () => { | ||
test('runs code in Bun Worker', async () => { | ||
const pool = createPool({ runtime: 'bun_workers' }) | ||
|
||
const result = await pool.run(` | ||
(async () => { | ||
return { | ||
sum: 11 + 12, | ||
isMainThread: Bun.isMainThread, | ||
pid: process.pid, | ||
} | ||
})() | ||
`) | ||
expect(result.sum).toBe(23) | ||
expect(result.isMainThread).toBe(false) | ||
expect(result.pid).toBe(process.pid) | ||
}) | ||
|
||
test('sets tinypool state', async () => { | ||
const pool = createPool({ runtime: 'bun_workers' }) | ||
|
||
const result = await pool.run('process.__tinypool_state__') | ||
expect(result.isTinypoolWorker).toBe(true) | ||
expect(result.isBunWorker).toBe(true) | ||
expect(result.isWorkerThread).toBe(undefined) | ||
expect(result.isChildProcess).toBe(undefined) | ||
}) | ||
|
||
test('errors are serialized', async () => { | ||
const pool = createPool({ runtime: 'bun_workers' }) | ||
|
||
const error = await pool | ||
.run("throw new TypeError('Test message');") | ||
.catch((e: Error) => e) | ||
|
||
expect(error.name).toBe('TypeError') | ||
expect(error.message).toBe('Test message') | ||
|
||
// Nope Bun does not do this | ||
// expect(error.stack).toMatch('fixtures/eval.js') | ||
}) | ||
}) | ||
|
||
function createPool(options) { | ||
const pool = new Tinypool({ | ||
filename: path.resolve(__dirname, './fixtures/eval.js'), | ||
minThreads: 1, | ||
maxThreads: 1, | ||
...options, | ||
}) | ||
|
||
return pool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
StartupMessage, | ||
ReadyMessage, | ||
RequestMessage, | ||
ResponseMessage, | ||
} from '../common' | ||
import { getHandler, throwInNextTick } from './utils' | ||
import { stderr, stdout } from 'src/utils' | ||
|
||
process.__tinypool_state__ = { | ||
isTinypoolWorker: true, | ||
isBunWorker: true, | ||
workerData: null, | ||
workerId: 1, | ||
} | ||
|
||
self.onmessage = onWorkerMessage | ||
|
||
function onWorkerMessage(event: MessageEvent<StartupMessage>) { | ||
const { filename, name } = event.data | ||
|
||
;(async function () { | ||
if (filename !== null) { | ||
await getHandler(filename, name) | ||
} | ||
|
||
const readyMessage: ReadyMessage = { ready: true } | ||
self.postMessage(readyMessage, '') | ||
})().catch(throwInNextTick) | ||
|
||
if (event.ports?.[0]) { | ||
event.ports[0].start() | ||
event.ports[0].onmessage = onPortMessage.bind(null, event.ports[0]) | ||
} | ||
} | ||
|
||
function onPortMessage(port: MessagePort, event: MessageEvent<RequestMessage>) { | ||
const message = event.data | ||
const { taskId, task, filename, name } = message | ||
|
||
;(async function () { | ||
let response: ResponseMessage | ||
|
||
try { | ||
const handler = await getHandler(filename, name) | ||
if (handler === null) { | ||
throw new Error(`No handler function exported from ${filename}`) | ||
} | ||
let result = await handler(task) | ||
response = { | ||
taskId, | ||
result: result, | ||
error: null, | ||
usedMemory: process.memoryUsage().heapUsed, | ||
} | ||
|
||
// If the task used e.g. console.log(), wait for the stream to drain | ||
// before potentially entering the `Atomics.wait()` loop, and before | ||
// returning the result so that messages will always be printed even | ||
// if the process would otherwise be ready to exit. | ||
if (stdout()?.writableLength! > 0) { | ||
await new Promise((resolve) => process.stdout.write('', resolve)) | ||
} | ||
if (stderr()?.writableLength! > 0) { | ||
await new Promise((resolve) => process.stderr.write('', resolve)) | ||
} | ||
} catch (error) { | ||
response = { | ||
taskId, | ||
result: null, | ||
error, | ||
usedMemory: process.memoryUsage().heapUsed, | ||
} | ||
} | ||
|
||
port.postMessage(response) | ||
})().catch(throwInNextTick) | ||
} |
Oops, something went wrong.