Docs & types for 0.7 (#3665)

* Docs & types for 0.7

* Tweak

* Update

* Tweaks

* Tweak

* Tweaks

---------

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
Colin McDonnell
2023-07-19 23:59:15 -07:00
committed by GitHub
parent 0b365781a8
commit 53ad9b922f
8 changed files with 71 additions and 26 deletions

View File

@@ -247,7 +247,7 @@ Bun.deepEquals(new Foo(), { a: 1 }, true); // false
## `Bun.escapeHTML()`
`Bun.escapeHTML(value: string | object | number | boolean): boolean`
`Bun.escapeHTML(value: string | object | number | boolean): string`
Escapes the following characters from an input string:
@@ -402,7 +402,7 @@ The second argument supports the same set of configuration options as [`Bun.gzip
## `Bun.inflateSync()`
DEcompresses a `Uint8Array` using zlib's INFLATE algorithm.
Decompresses a `Uint8Array` using zlib's INFLATE algorithm.
```ts
const buf = Buffer.from("hello".repeat(100));

View File

@@ -1,5 +1,5 @@
{% callout %}
`Worker` support was added in Bun v0.6.15.
`Worker` support was added in Bun v0.7.0.
{% /callout %}
[`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker) lets you start and communicate with a new JavaScript instance running on a separate thread while sharing I/O resources with the main thread.

View File

@@ -32,6 +32,26 @@ The "naked" `bun` command is equivalent to `bun run`.
$ bun index.tsx
```
### `--watch`
To run a file in watch mode, use the `--watch` flag.
```bash
$ bun --watch run index.tsx
```
### `--smol`
{% callout %}
Added in Bun v0.7.0.
{% /callout %}
In memory-constrained environments, use the `--smol` flag to reduce memory usage at a cost to performance.
```bash
$ bun --smol run index.tsx
```
## Run a `package.json` script
{% note %}

View File

@@ -28,12 +28,8 @@ jsxImportSource = "react"
# Reduce memory usage at the cost of performance
smol = true
# Set a default framework to use
# By default, Bun will look for an npm package like `bun-framework-${framework}`, followed by `${framework}`
logLevel = "debug"
# publicDir = "public"
# external = ["jquery"]
# Set Bun's log level
logLevel = "debug" # "debug", "warn", "error"
[define]
# Replace any usage of "process.env.bagel" with the string `lox`.
@@ -44,16 +40,13 @@ logLevel = "debug"
[loaders]
# When loading a .bagel file, run the JS parser
".bagel" = "js"
# - "atom"
# If you pass it a file path, it will open with the file path instead
# It will recognize non-GUI editors, but I don't think it will work yet
```
## Test runner
```toml
[test]
# setup scripts to run before all test files
# Scripts to run before all test files
preload = ["./setup.ts"]
# Reduce memory usage at the cost of performance
@@ -215,13 +208,3 @@ These environment variables are checked by Bun to detect functionality and toggl
- If `DO_NOT_TRACK=1`, then analytics are [disabled](https://do-not-track.dev/). Bun records bundle timings (so we can answer with data, "is Bun getting faster?") and feature usage (e.g., "are people actually using macros?"). The request body size is about 60 bytes, so it's not a lot of data.
{% /table %}
## smol mode
To reduce Bun's memory footprint in the runtime and test runner, pass `--smol`.
```bash
$ bun --smol ./my-script.ts
```
This configures JavaScriptCore (the engine) to use a smaller heap size and run the garbage collector more frequently. This is currently disabled by default for performance reasons, but it may become the default in the future. This feature was introduced in Bun v0.6.15.

View File

@@ -16,10 +16,8 @@ The following Web APIs are partially or completely supported.
---
---
- Web Workers
- [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker) [`self.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage) [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)
- [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker) [`self.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage) [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone). Missing `MessagePort`, `MessageChannel`, `BroadcastChannel`.
---

View File

@@ -4,3 +4,6 @@ import { argv } from "process";
const path = resolve(argv.at(-1)!);
await write(stdout, file(path));
Bun.stdout;
process.stdout;

View File

@@ -0,0 +1,9 @@
import { serialize, deserialize } from "bun:jsc";
import { deepEquals } from "bun";
const obj = { a: 1, b: 2 };
const buffer = serialize(obj);
const clone = deserialize(buffer);
if (deepEquals(obj, clone)) {
console.log("They are equal!");
}

View File

@@ -0,0 +1,32 @@
const worker = new Worker("./worker.ts");
worker.addEventListener("message", (event: MessageEvent) => {
console.log("Message from worker:", event.data);
});
worker.postMessage("Hello from main thread!");
const workerURL = new URL("worker.ts", import.meta.url).href;
const _worker2 = new Worker(workerURL);
worker.postMessage("hello");
worker.onmessage = event => {
console.log(event.data);
};
// On the worker thread, `postMessage` is automatically "routed" to the parent thread.
postMessage({ hello: "world" });
// On the main thread
worker.postMessage({ hello: "world" });
// ...some time later
worker.terminate();
// Bun.pathToFileURL
const _worker3 = new Worker(new URL("worker.ts", import.meta.url).href, {
bun: {
ref: true,
smol: true,
},
});
export { worker, _worker2, _worker3 };