Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
with:
node-version: "24"
cache: "npm"
- id: chrome
uses: browser-actions/setup-chrome@48ad923757ca74d66703209fe939badbdf80f2f4 # v2.2.0
- run: cargo fmt --check
- run: cargo clippy --locked --lib -- -D warnings
- run: cargo build --locked --release
Expand All @@ -59,3 +61,5 @@ jobs:
env:
RUSTDOCFLAGS: "-D warnings"
- run: npm run verify:pack
env:
CHROME_BIN: ${{ steps.chrome.outputs.chrome-path }}
6 changes: 5 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ jobs:
with:
node-version: "24"
cache: "npm"
- id: chrome
uses: browser-actions/setup-chrome@48ad923757ca74d66703209fe939badbdf80f2f4 # v2.2.0
- run: npm ci
# Check the archive files and loadTunelet() before upload.
# Check the archive files, the types and the loaders before upload.
- run: npm run verify:pack
env:
CHROME_BIN: ${{ steps.chrome.outputs.chrome-path }}
# Upload the built dist/ and pkg/ files for the publish jobs.
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
Expand Down
14 changes: 9 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ cargo clippy --locked --lib --target wasm32-unknown-unknown --features wasm -- -
RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps --target wasm32-unknown-unknown --features wasm
```

If you change the Wasm or TypeScript code, also run these commands.
`npm run check:wasm` runs Clippy and rustdoc for `wasm32-unknown-unknown`.
`npm run build` compiles `src/wasm.rs` and `src/index.ts`.
`npm run verify:pack` runs `npm run build` and checks the archives:
If you change the Wasm or TypeScript code, install Node.js, `wasm-pack`,
Binaryen `wasm-opt` and Chrome or Chromium. If the browser binary is not on
`PATH`, set `CHROME_BIN` to its executable path. `npm run check:wasm` runs
Clippy and rustdoc for `wasm32-unknown-unknown`. `npm run build` compiles
`src/wasm.rs`, normalizes `pkg/tunelet.d.ts` and compiles `src/index.ts`.
`npm run verify:pack` runs `npm run build`, checks the archive and checks the
TypeScript fixtures and runtime loaders in Node.js and headless Chromium:

```sh
npm ci
Expand All @@ -112,7 +115,8 @@ npm run verify:pack
```

- `.github/workflows/ci.yml` runs these checks on each pull request and each
push to `main`.
push to `main`. `.github/workflows/release.yml` also runs `npm run verify:pack`
before it uploads the package files.
- Make sure that there are no new warnings. This includes broken
rustdoc links.
- The crate has no tests (`test = false`). Do not add tests or test
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"sideEffects": false,
"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 --input-type=module -e \"import fs from 'node:fs'; fs.rmSync('pkg/.gitignore', { force: true })\"",
"build:wasm": "wasm-pack build --target web --out-dir pkg --release --no-pack --features wasm --locked && node scripts/normalize-pkg.mjs",
"build:ts": "tsc",
"check:wasm": "cargo clippy --locked --lib --target wasm32-unknown-unknown --features wasm -- -D warnings && cargo doc --locked --no-deps --target wasm32-unknown-unknown --features wasm",
"prepack": "npm run build",
Expand Down
94 changes: 94 additions & 0 deletions scripts/normalize-pkg.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* This script removes `pkg/.gitignore` and makes `pkg/tunelet.d.ts`
* portable across browser, worker and Node.js TypeScript configurations.
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const DISPOSABLE_REFERENCE = '/// <reference lib="esnext.disposable" />';

const RAW_INIT_INPUT =
"export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;";
const PORTABLE_INIT_INPUT =
"export type InitInput = Request | string | URL | Response | ArrayBufferView | ArrayBuffer | WebAssembly.Module;";

const RAW_SYNC_INIT_INPUT =
"export type SyncInitInput = BufferSource | WebAssembly.Module;";
const PORTABLE_SYNC_INIT_INPUT =
"export type SyncInitInput = ArrayBufferView | ArrayBuffer | WebAssembly.Module;";

const AMBIENT_INTERFACES_BLOCK = [
"declare global {",
" interface Request {}",
" interface Response {}",
" interface URL {}",
" namespace WebAssembly {",
" interface Memory {}",
" interface Module {}",
" interface Table {}",
" }",
"}",
"",
].join("\n");

const repoRoot = fileURLToPath(new URL("..", import.meta.url));
const pkgDir = path.join(repoRoot, "pkg");
const gitignorePath = path.join(pkgDir, ".gitignore");
const declarationsPath = path.join(pkgDir, "tunelet.d.ts");

/** Remove the ignore file that wasm-pack writes into pkg/. */
fs.rmSync(gitignorePath, { force: true });

/** Rewrite the generated declarations into a self-contained module. */
let content = fs.readFileSync(declarationsPath, "utf8").replace(/\r\n/g, "\n");

if (!content.startsWith(`${DISPOSABLE_REFERENCE}\n`)) {
content = `${DISPOSABLE_REFERENCE}\n${content}`;
}

if (content.includes(RAW_INIT_INPUT)) {
content = content.replace(RAW_INIT_INPUT, PORTABLE_INIT_INPUT);
}

if (content.includes(RAW_SYNC_INIT_INPUT)) {
content = content.replace(RAW_SYNC_INIT_INPUT, PORTABLE_SYNC_INIT_INPUT);
}

if (!content.includes(AMBIENT_INTERFACES_BLOCK.trim())) {
content = `${content.trimEnd()}\n\n${AMBIENT_INTERFACES_BLOCK}`;
}

assert.ok(
content.startsWith(`${DISPOSABLE_REFERENCE}\n`),
"pkg/tunelet.d.ts must start with the esnext.disposable reference",
);
assert.ok(
content.includes("[Symbol.dispose](): void;"),
"pkg/tunelet.d.ts must declare [Symbol.dispose]()",
);
assert.ok(
content.includes(PORTABLE_INIT_INPUT),
"pkg/tunelet.d.ts must declare the portable InitInput union",
);
assert.ok(
content.includes(PORTABLE_SYNC_INIT_INPUT),
"pkg/tunelet.d.ts must declare the portable SyncInitInput union",
);
assert.ok(
content.includes(AMBIENT_INTERFACES_BLOCK.trim()),
"pkg/tunelet.d.ts must declare open-ended ambient interfaces",
);

/** Check that no non-portable ambient aliases remain in code lines. */
const codeOnly = content
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/^\s*\/\/(?!\/).*$/gm, "");
assert.equal(
/\b(?:RequestInfo|BufferSource)\b/.test(codeOnly),
false,
"pkg/tunelet.d.ts must not reference RequestInfo or BufferSource in types",
);

fs.writeFileSync(declarationsPath, content);
Loading