forked from tinylibs/tinypool
-
Notifications
You must be signed in to change notification settings - Fork 0
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 tinylibs#70, use Bun's native Workers API
- Loading branch information
1 parent
a5b6669
commit 8c10873
Showing
6 changed files
with
276 additions
and
25 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
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,77 @@ | ||
import { | ||
StartupMessage, | ||
ReadyMessage, | ||
RequestMessage, | ||
ResponseMessage, | ||
} from '../common' | ||
import { getHandler, throwInNextTick } from './utils' | ||
import { stderr, stdout } from 'src/utils' | ||
|
||
process.__tinypool_state__ = { | ||
isTinypoolWorker: 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) | ||
} |
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,76 @@ | ||
import { fileURLToPath } from 'url' | ||
import { dirname, resolve } from 'path' | ||
import { TransferListItem } from 'worker_threads' | ||
import { TinypoolWorker, TinypoolChannel } from '../common' | ||
|
||
let ids = 1 | ||
|
||
export default class BunWorker implements TinypoolWorker { | ||
name = 'BunWorker' | ||
runtime = 'bun_workers' | ||
worker!: Worker | ||
threadId!: number | ||
port?: MessagePort | ||
channel?: TinypoolChannel | ||
waitForExit!: Promise<void> | ||
onExit!: () => void | ||
|
||
initialize(_: Parameters<TinypoolWorker['initialize']>[0]) { | ||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
this.worker = new Worker(resolve(__dirname, './entry/bun-worker.js')) | ||
this.threadId = ids++ | ||
this.waitForExit = new Promise((resolve) => { | ||
this.onExit = resolve | ||
}) | ||
} | ||
|
||
async terminate() { | ||
console.log('Terminating') | ||
this.worker.terminate() | ||
this.onExit() | ||
|
||
return this.waitForExit | ||
} | ||
|
||
postMessage(message: any, transferListItem?: Readonly<TransferListItem[]>) { | ||
return this.worker.postMessage( | ||
message, | ||
getOnlyMessagePorts(transferListItem) | ||
) | ||
} | ||
|
||
on(event: string, callback: (...args: any[]) => void) { | ||
if (event === 'message') { | ||
this.worker.onmessage = (e) => callback(e.data) | ||
} | ||
if (event === 'error') { | ||
this.worker.onerror = callback | ||
} | ||
if (event === 'exit') { | ||
this.waitForExit.then(callback) | ||
} | ||
} | ||
|
||
once(event: string, callback: (...args: any[]) => void) { | ||
if (event === 'exit') { | ||
this.waitForExit.then(callback) | ||
} | ||
} | ||
|
||
emit(_event: string, ..._data: any[]) {} | ||
|
||
ref() {} | ||
|
||
unref() {} | ||
|
||
setChannel() { | ||
throw new Error('BunWorker does not support channel') | ||
} | ||
} | ||
|
||
function getOnlyMessagePorts(list?: Readonly<unknown[]>): MessagePort[] { | ||
return (list || []).filter( | ||
(item): item is MessagePort => item instanceof MessagePort | ||
) | ||
} |
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