oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.12k stars 2.77k forks source link

`--preload` with `new Worker` #15044

Open SlexAxton opened 3 hours ago

SlexAxton commented 3 hours ago

What version of Bun is running?

1.1.33+247456b67

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

bun --preload preload.ts ./app.ts
# preload.ts

globalThis.EXPANDO = 'EXPANDO';
# app.ts
console.log('From app', globalThis.EXPANDO);

const worker = new Worker('./worker.ts');
# worker.ts
console.log('From worker', globalThis.EXPANDO);

What is the expected behavior?

From app EXPANDO
From worker EXPANDO

What do you see instead?

From app EXPANDO
From worker undefined

Additional information

I'm not super sure how it should work for sure! But! For something like opentelemetry, it means that all of my telemetry is silently ignored inside of my workers.

I'd hope there's some way to make it to where I could reliably load the telemetry.ts file for everything.

It's possible there's a better way than --preload to add the telemetry file, but that's how @Jarred-Sumner is suggesting it in this issue: https://github.com/oven-sh/bun/issues/3775#issuecomment-1892843091

SlexAxton commented 3 hours ago

It's possible workers should have their own --worker-preload or simply take the preload argument as an option on new Worker(path, {preload: './file/from/root.ts'})

Jarred-Sumner commented 2 hours ago

--worker-preload sounds good + adding a preload argument

truongan07 commented 32 minutes ago

i may transfer data to worker by globalThis variable without postMessage method ?

Jarred-Sumner commented 22 minutes ago

i may transfer data to worker by globalThis variable without postMessage method ?

i wish

you'll need to use postMessage or a SharedArrayBuffer if you want to be very fancy