aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-07-19 22:53:28 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-07-19 22:53:28 -0700
commited8be46a7bf00321b4a10571e245e8fd744676d9 (patch)
treec8de521c4970a5b6c918103d202572e329177018
parent36866c4d7941d36656ab684e4dccecab2f06ebef (diff)
downloadbun-ed8be46a7bf00321b4a10571e245e8fd744676d9.tar.gz
bun-ed8be46a7bf00321b4a10571e245e8fd744676d9.tar.zst
bun-ed8be46a7bf00321b4a10571e245e8fd744676d9.zip
Inline`bun` object from workers
-rw-r--r--docs/api/workers.md8
-rw-r--r--packages/bun-types/globals.d.ts38
-rw-r--r--src/bun.js/bindings/webcore/JSWorker.cpp22
-rw-r--r--test/js/web/worker.test.ts4
4 files changed, 33 insertions, 39 deletions
diff --git a/docs/api/workers.md b/docs/api/workers.md
index 9a4e0d799..af459d18e 100644
--- a/docs/api/workers.md
+++ b/docs/api/workers.md
@@ -132,9 +132,7 @@ Alternatively, you can also pass an `options` object to `Worker`:
```ts
const worker = new Worker(new URL("worker.ts", import.meta.url).href, {
- bun: {
- ref: true,
- },
+ ref: true,
});
```
@@ -157,9 +155,7 @@ JavaScript instances can use a lot of memory. Bun's `Worker` supports a `smol` m
```js
const worker = new Worker("./i-am-smol.ts", {
- bun: {
- smol: true,
- },
+ smol: true,
});
```
diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts
index e4fefcc11..885294b0d 100644
--- a/packages/bun-types/globals.d.ts
+++ b/packages/bun-types/globals.d.ts
@@ -3545,23 +3545,27 @@ declare var Worker: {
interface WorkerOptions {
name?: string;
- bun?: {
- /**
- * Use less memory, but make the worker slower.
- *
- * Internally, this sets the heap size configuration in JavaScriptCore to be
- * the small heap instead of the large heap.
- */
- smol?: boolean;
-
- /**
- * When `true`, the worker will keep the parent thread alive until the worker is terminated or `unref`'d.
- * When `false`, the worker will not keep the parent thread alive.
- *
- * By default, this is `false`.
- */
- ref?: boolean;
- };
+
+ /**
+ * Use less memory, but make the worker slower.
+ *
+ * Internally, this sets the heap size configuration in JavaScriptCore to be
+ * the small heap instead of the large heap.
+ */
+ smol?: boolean;
+
+ /**
+ * When `true`, the worker will keep the parent thread alive until the worker is terminated or `unref`'d.
+ * When `false`, the worker will not keep the parent thread alive.
+ *
+ * By default, this is `false`.
+ */
+ ref?: boolean;
+
+ /**
+ * Does nothing in Bun
+ */
+ type?: string
}
interface WorkerEventMap {
diff --git a/src/bun.js/bindings/webcore/JSWorker.cpp b/src/bun.js/bindings/webcore/JSWorker.cpp
index bcf670d63..4ddd97520 100644
--- a/src/bun.js/bindings/webcore/JSWorker.cpp
+++ b/src/bun.js/bindings/webcore/JSWorker.cpp
@@ -137,19 +137,15 @@ template<> EncodedJSValue JSC_HOST_CALL_ATTRIBUTES JSWorkerDOMConstructor::const
}
}
- if (auto bunValue = optionsObject->getIfPropertyExists(lexicalGlobalObject, Identifier::fromString(vm, "bun"_s))) {
- if (bunValue.isObject()) {
- if (auto* bunObject = bunValue.getObject()) {
- if (auto miniModeValue = bunObject->getIfPropertyExists(lexicalGlobalObject, Identifier::fromString(vm, "smol"_s))) {
- options.bun.mini = miniModeValue.toBoolean(lexicalGlobalObject);
- RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
- }
-
- if (auto ref = bunObject->getIfPropertyExists(lexicalGlobalObject, Identifier::fromString(vm, "ref"_s))) {
- options.bun.unref = !ref.toBoolean(lexicalGlobalObject);
- RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
- }
- }
+ if (auto* bunObject = optionsObject) {
+ if (auto miniModeValue = bunObject->getIfPropertyExists(lexicalGlobalObject, Identifier::fromString(vm, "smol"_s))) {
+ options.bun.mini = miniModeValue.toBoolean(lexicalGlobalObject);
+ RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
+ }
+
+ if (auto ref = bunObject->getIfPropertyExists(lexicalGlobalObject, Identifier::fromString(vm, "ref"_s))) {
+ options.bun.unref = !ref.toBoolean(lexicalGlobalObject);
+ RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
}
}
}
diff --git a/test/js/web/worker.test.ts b/test/js/web/worker.test.ts
index 2120bd11a..02d9af5d8 100644
--- a/test/js/web/worker.test.ts
+++ b/test/js/web/worker.test.ts
@@ -2,9 +2,7 @@ import { expect, test } from "bun:test";
test("worker", done => {
const worker = new Worker(new URL("worker-fixture.js", import.meta.url).href, {
- bun: {
- smol: true,
- },
+ smol: true,
});
worker.postMessage("hello");
worker.onerror = e => {