bun icon indicating copy to clipboard operation
bun copied to clipboard

Support for worker_threads

Open haroldiedema opened this issue 3 years ago • 3 comments

What is the problem this feature will solve?

Some heavy server-side applications rely on multithreading. We run a couple of these in our company. I wanted to see if bun is a viable alternative to Node, but it seems bun doesn't support worker_threads (yet).

What is the feature you are proposing to solve the problem?

I would like to see support for the worker_threads module in bun.

What alternatives have you considered?

No response

haroldiedema avatar Jul 28 '22 14:07 haroldiedema

Bun needs to do this

I think it'd be good for Worker to be a global, like in browsers but also support importing from "worker_threads" for node compatibility

Jarred-Sumner avatar Jul 28 '22 21:07 Jarred-Sumner

Looking forward to supporting bun in https://github.com/asilvas/ram64 !

asilvas avatar Aug 01 '22 12:08 asilvas

Also looking forward to support for this!

alexreyes avatar Jan 19 '23 19:01 alexreyes

I suggest you impl real spec'ed web worker instead and not NodeJS specific worker. Deno did this. and there is a highly upvoted github issue: https://github.com/nodejs/node/issues/43583 to actually provide NodeJS with a spec'ed web worker. it should be based on postMessages, EventTarget (not EventEmitter) and it should support { type: 'module' }, and support loading worker from objectURLs (like those created from dynamic code / blobs + URL.createObjectURL)

it's better to be cross-platform compatible instead.

jimmywarting avatar May 06 '23 20:05 jimmywarting

I suggest you impl real spec'ed web worker instead and not NodeJS specific worker. Deno did this. and there is a highly upvoted github issue: nodejs/node#43583 to actually provide NodeJS with a spec'ed web worker. it should be based on postMessages, EventTarget (not EventEmitter) and it should support { type: 'module' }

it's better to be cross-platform compatible instead.

I agree, we are currently in a mess, I hope we all will write 1 language on the server one day and JS Server APIs are one to one the same in all runtimes.

ivanjeremic avatar Jun 23 '23 05:06 ivanjeremic

A small update

We haven't added worker_threads support yet, but we did add Web Workers in https://github.com/oven-sh/bun/pull/3645. It will be part of Bun v0.7.0.

Bun will support worker_threads along with Web Workers, but worker_threads support will come in a release shortly after 0.7 (it needs BroadcastChannel, MessagePort, customizing stdout, stderr, and a few other things so it's more complicated)

Note that we still haven't landed support for resolving blob: or data: URLs yet. We are planning to do that.

Jarred-Sumner avatar Jul 18 '23 08:07 Jarred-Sumner

When the CI for this PR finishes, you'll be able to try it out using bun upgrade --canary. This will be released in Bun v0.7.2.

Electroid avatar Aug 03 '23 01:08 Electroid

Can confirm canary is working. 🙏

➜  vote git:(main) ✗ bun run test
$ vitest run --coverage
[0.06ms] ".env"
SyntaxError: Import named 'receiveMessageOnPort' not found in module 'node:worker_threads'.
➜  vote git:(main) ✗ bun upgrade --canary
[5.93s] Upgraded.

Welcome to bun's latest canary build!

Report any bugs:

    https://github.com/oven-sh/bun/issues

Changelog:

    https://github.com/oven-sh/bun/compare/53cc4df191aa823c3a5c5a2a377701439a2d3f89...main
➜  vote git:(main) ✗ bun run test        
$ vitest run --coverage
[0.04ms] ".env"
SyntaxError: Import named 'Console' not found in module 'node:console'.
error: script "test" exited with code 1 (SIGHUP)

ImLunaHey avatar Aug 06 '23 02:08 ImLunaHey