Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ this file, rustdoc, code comments, TypeScript comments and commit messages.

- Make the smallest possible change. Do not rename, move or format
code that is not part of the task.
- The public API is all items that `src/lib.rs` exports, `src/wasm.rs`
and `src/index.ts`. Do not add, change or remove public items if the
task does not tell you to. Keep the Rust, Wasm and TypeScript APIs
equivalent.
- The public API is all items that `src/lib.rs` exports, `src/wasm.rs`,
`src/index.ts`, `src/browser.ts`, `src/node.ts` and `src/shared.ts`.
Do not add, change or remove public items if the task does not tell
you to. Keep the Rust, Wasm and TypeScript APIs equivalent.
- Keep the crate lints in `src/lib.rs`: `#![forbid(unsafe_code)]`,
`#![deny(missing_docs)]`, `#![deny(rustdoc::broken_intra_doc_links)]`
and `#![warn(unreachable_pub)]`. Each public item must have rustdoc
Expand Down Expand Up @@ -85,7 +85,8 @@ this file, rustdoc, code comments, TypeScript comments and commit messages.
- `src/numeric.rs`, `src/numeric/`: the fixed-point functions, the
random number generator and the lookup tables.
- `src/wasm.rs`: the wasm-bindgen and tsify bindings (`wasm` feature).
- `src/index.ts`: the `loadTunelet` entry point. See also `package.json`
- `src/index.ts`, `src/browser.ts`, `src/node.ts`, `src/shared.ts`: the
`loadTunelet` entries and `TuneletAudioViews`. See also `package.json`
and `tsconfig.json`.

## Build and verify
Expand Down Expand Up @@ -118,9 +119,10 @@ In `cmd.exe` on Windows, use `set "RUSTDOCFLAGS=-D warnings"` before the doc com

If you change the Wasm or TypeScript code, also run these commands.
`npm run check:wasm` runs Clippy and rustdoc in release mode for
`wasm32-unknown-unknown`. `npm run build` compiles `src/wasm.rs` and
`src/index.ts`. `npm run verify:pack` runs `npm run build` one time and
checks the `tunelet` and `@bytelandtechnology/tunelet` archives:
`wasm32-unknown-unknown`. `npm run build` compiles `src/wasm.rs`,
`src/index.ts`, `src/browser.ts`, `src/node.ts` and `src/shared.ts`.
`npm run verify:pack` runs `npm run build` one time and checks the
`tunelet` and `@bytelandtechnology/tunelet` archives:

```sh
npm ci
Expand Down
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,31 +429,36 @@ For `wasm32-unknown-unknown`, the optional `wasm` feature of the crate gives
JavaScript bindings and generated TypeScript declarations. The npm package
contains these bindings.

The npm package is one ES module for both browsers and Node.js. It contains
these items:
The npm package gives conditional ES module entries for Node.js and browsers,
and keeps the shared `dist/index.js` entry. It contains these items:

- One Wasm binary.
- The generated bindings.
- A shared `loadTunelet` entry point.
- Conditional `node` (`dist/node.js`) and `browser` (`dist/browser.js`)
`loadTunelet` entries, and `dist/index.js`, with the same API.

### Load the module

Both environments use the same imports and the same initialization call. An
import of the package does not load the binary. Call `loadTunelet()` before
you use `Tunelet` or `defaultConfig()`.

- Node.js reads the packaged binary from a file URL relative to the module.
- Browsers fetch the served Wasm asset.
- Node.js reads `../pkg/tunelet_bg.wasm` relative to `dist/node.js`. If a
bundler moves the entry, Node.js resolves `pkg/tunelet_bg.wasm` from the
installed package.
- Browsers and bundlers use `dist/browser.js` and fetch the served Wasm asset
without Node.js imports.
- `dist/index.js` keeps the `main` entry and supports direct browser imports.

Concurrent calls share one initialization promise. The package keeps the
result of a successful initialization. If a load fails, the promise rejects,
and you can try again. A Node.js file error goes to the caller.
and you can try again. File, network and Wasm errors go to the caller.

For a custom asset location, give one of these sources to
`loadTunelet(source)`:

- A URL or URL string.
- A fetch Response.
- A fetch Request or Response.
- A byte buffer.
- A compiled `WebAssembly.Module`.
- A promise for one of these.
Expand All @@ -476,11 +481,12 @@ The package gives only ESM exports. CommonJS applications can use
### Deployment

Browser bundlers must keep or emit the Wasm asset that
`new URL(..., import.meta.url)` refers to. For a browser deployment without a
bundler, do these steps:
`new URL(..., import.meta.url)` refers to, or resolve `tunelet/tunelet_bg.wasm`.
For a browser deployment without a bundler, do these steps:

1. Serve `dist/` and `pkg/`, and keep their relative layout.
2. Map the `tunelet` import to `dist/index.js`, or import that URL directly.
2. Map the `tunelet` import to `dist/index.js` or `dist/browser.js`, or
import that URL directly.

Serve the assets over HTTP(S). The package does not support a local `file:`
page that opens the JavaScript.
Expand Down Expand Up @@ -705,7 +711,7 @@ npm pack

`npm run check:wasm` runs Clippy and rustdoc in release mode for
`wasm32-unknown-unknown`. `npm pack` builds the Wasm bindings and the
TypeScript entry point. Then it creates `tunelet-<version>.tgz`. To use
TypeScript entries. Then it creates `tunelet-<version>.tgz`. To use
this archive in a browser project or a Node.js project, install it with
this command:

Expand All @@ -714,10 +720,11 @@ npm install /path/to/tunelet-<version>.tgz
```

To build without an archive, run `npm run build`. The build writes one
`web`-target binding into `pkg/` and the shared entry into `dist/`. Git
`web`-target binding into `pkg/` and compiles `src/browser.ts`,
`src/node.ts`, `src/index.ts` and `src/shared.ts` into `dist/`. Git
ignores both directories. The archive contains only these items:

- The runtime entry.
- The runtime entries.
- The declarations.
- The bindings.
- The binary.
Expand Down
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
"browser": {
"types": "./dist/browser.d.ts",
"import": "./dist/browser.js",
"default": "./dist/browser.js"
},
"node": {
"types": "./dist/node.d.ts",
"import": "./dist/node.js",
"default": "./dist/node.js"
},
"types": "./dist/browser.d.ts",
"import": "./dist/browser.js",
"default": "./dist/browser.js"
},
"./wasm": {
"types": "./pkg/tunelet.d.ts",
Expand All @@ -32,7 +43,7 @@
"scripts": {
"build": "npm run build:wasm && npm run build:ts",
"build:wasm": "wasm-pack build --target web --out-dir pkg --release --no-pack --features wasm --locked && node scripts/postbuild-wasm.mjs && node scripts/normalize-jsdoc.mjs",
"build:ts": "tsc",
"build:ts": "node --input-type=module -e \"import fs from 'node:fs'; fs.rmSync('dist', { recursive: true, force: true })\" && tsc",
"check:wasm": "cargo clippy --locked --release --lib --target wasm32-unknown-unknown --features wasm -- -D warnings && cargo rustdoc --locked --release --lib --target wasm32-unknown-unknown --features wasm -- -D warnings",
"prepack": "npm run build",
"prepare": "git config core.hooksPath .githooks || true",
Expand Down
Loading
Loading