Skip to content
Merged
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
13 changes: 4 additions & 9 deletions .github/workflows/release-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ on:
paths:
- 'js/**'
- '.github/workflows/release-js.yml'
# Dry-run: builds, packs, and runs `deno publish --dry-run`, which catches
# JSR-only failures before they break a release on `release`. Only a push
# publishes; release-gate.yml calls this on pull requests.
# Dry-run: builds and packs, which catches packaging failures before they
# break a release on `release`. Only a push publishes; release-gate.yml calls
# this on pull requests.
workflow_call:

# Only publishing runs share the serialized group. GitHub keeps a single pending
Expand All @@ -28,9 +28,7 @@ jobs:
runs-on: ubuntu-latest
if: github.repository_owner == 'moq-dev'
# npm trusted publishing can require this Environment, which only `release`
# can deploy to, so a branch's OIDC identity cannot publish. It also holds
# the JSR token, since JSR's OIDC trust cannot require an Environment.
# Dry-runs skip it.
# can deploy to, so a branch's OIDC identity cannot publish. Dry-runs skip it.
environment: ${{ github.event_name == 'push' && 'npm' || '' }}

steps:
Expand Down Expand Up @@ -58,7 +56,4 @@ jobs:
- name: Release packages
env:
DRY_RUN: ${{ github.event_name != 'push' }}
# Only the `npm` Environment holds it, so dry-runs get none.
# release.ts hands it to `deno publish` alone.
JSR_TOKEN: ${{ secrets.JSR_TOKEN }}
run: nix develop --command bun --filter '*' release
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ __pycache__/
# installs of the py/moq-ffi package.
/py/moq-ffi/moq_ffi/_uniffi/

# Generated JSR manifest (derived from package.json by js/common/package.ts)
jsr.json

# msys/cygwin crash dumps, from running the shell scripts on Windows.
*.stackdump
3 changes: 0 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@
bun
# Only for NPM publishing
nodejs_24
# JSR publishing. We call `deno publish` directly instead of `bunx jsr`
# so the release doesn't race on a runtime binary download.
deno
];

# Python dependencies
Expand Down
2 changes: 1 addition & 1 deletion js/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Plain custom elements on `@moq/signals`, no framework. Attributes are the public
# Conventions

- ESM only. Match the import style of the file you're editing.
- Document every export and add a `@module` block to each entrypoint; JSR builds docs from them. Deprecated exports are marked `@internal` or dropped from the entrypoint, never annotated "use X".
- Document every export and add a `@module` block to each entrypoint. Deprecated exports are marked `@internal` or dropped from the entrypoint, never annotated "use X".
- `bun` for everything. Biome formats and lints (repo-root `biome.jsonc`). Tests are `*.test.ts` under `bun test`.
- `just js check` type-checks, lints, and builds; the build is part of `check` because declaration emit catches errors `--noEmit` misses. It also runs `common/deps.ts`: importing a `@moq/*` package a manifest does not declare works here through hoisting and breaks for a consumer.
- For UI or playback changes, run `just dev` and exercise it in a real browser; WebTransport and WebCodecs only fail at runtime. `<moq-watch>` renders black in a background tab: set `visible="always"` or bring the window frontmost.
124 changes: 2 additions & 122 deletions js/common/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// This creates a dist/ folder with the correct paths and dependencies for publishing
// Split from release.ts to allow building packages without publishing

import { copyFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
import { basename, join, resolve } from "node:path";
import { copyFileSync, readFileSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";
Comment thread
kixelated marked this conversation as resolved.
Resolved
import { publint } from "publint";
import { formatMessage } from "publint/utils";
import { checkReports } from "./api.ts";
Expand All @@ -12,17 +12,9 @@ import { problems } from "./declarations.ts";
console.log("✍️ Rewriting package.json...");
const pkg = JSON.parse(readFileSync("package.json", "utf8"));

// Capture the source exports before the npm rewrite below mutates them, so the
// JSR config can map them to the built (.js) entrypoints.
const srcExports: Record<string, unknown> = structuredClone(pkg.exports ?? {});

// Capture whether the package publishes before pkg.scripts is cleared below.
const published = Boolean(pkg.scripts?.release);

// Publish to JSR unless the package opts out. Web components opt out because
// JSR forbids the HTMLElementTagNameMap global augmentation they need.
const publishJsr = published && pkg.jsr !== false;

function rewritePath(p: string, ext: string): string {
return p.replace(/^\.\/src/, ".").replace(/\.ts(x)?$/, `.${ext}`);
}
Expand Down Expand Up @@ -99,7 +91,6 @@ rewriteWorkspaceDependency(pkg.peerDependencies);

pkg.devDependencies = undefined;
pkg.scripts = undefined;
pkg.jsr = undefined; // JSR opt-out flag, not part of the npm package

// Write the rewritten package.json
writeFileSync("dist/package.json", JSON.stringify(pkg, null, 2));
Expand Down Expand Up @@ -141,114 +132,3 @@ if (published) {
}

console.log("📦 Package built successfully in dist/");

// Optionally emit a jsr.json so the package can also publish to JSR (jsr.io).
// Generated from package.json so version/exports never drift. We publish the
// built dist (.js + .d.ts) rather than source: tsc emits explicit types into
// the .d.ts, which sidesteps JSR's "slow types" and ships real declarations,
// while JSR still builds the API docs from the .d.ts (JSDoc is preserved).
if (publishJsr) {
writeJsrConfig();
}

function writeJsrConfig() {
console.log("✍️ Generating jsr.json...");

const exports: Record<string, string> = {};
for (const [key, val] of Object.entries(srcExports)) {
if (typeof val !== "string") continue;
// CSS exports are dev-only and not published, same as the npm package.
if (val.endsWith(".css")) continue;
// rewritePath turns "./src/index.ts" into "./index.js".
exports[key] = `./dist/${rewritePath(val, "js").slice(2)}`;
}

// Self-contained import map so we don't rely on JSR's package.json merge
// behavior. Deps resolve via npm, so packages can publish to JSR in any
// order. Flip @moq/* entries to "jsr:" once the whole graph is on JSR if you
// want JSR-native cross-links between the docs.
const imports: Record<string, string> = {};
const deps = { ...(pkg.dependencies ?? {}), ...(pkg.peerDependencies ?? {}) };
for (const [name, range] of Object.entries(deps) as [string, string][]) {
if (name.startsWith("@types/")) continue; // type-only, never imported at runtime
imports[name] = `npm:${name}@${range}`;
// Trailing-slash subpath mapping (e.g. @moq/signals/dom). The leading slash
// in "npm:/" is required: jsr.json's imports is a standalone import map, so
// the value must parse as a base URL for relative resolution. The
// "npm:name@range/" form (no slash) fails to URL-parse the appended subpath.
imports[`${name}/`] = `npm:/${name}@${range}/`;
}

injectSelfTypes();
rewriteDtsImports();

// JSR validates the license field as a single recognized SPDX identifier, not
// an expression: it rejects "(MIT OR Apache-2.0)". Take the first identifier
// from a dual-license expression so the package keeps a valid license field.
const license =
typeof pkg.license === "string"
? pkg.license
.replace(/[()]/g, "")
.split(/\s+(?:OR|AND)\s+/i)[0]
.trim()
: undefined;

const jsr = {
name: pkg.name,
version: pkg.version,
// JSR reads this for the package description and the "has a description"
// score check; it isn't pulled from the npm package.json automatically.
...(pkg.description ? { description: pkg.description } : {}),
...(license ? { license } : {}),
exports,
...(Object.keys(imports).length ? { imports } : {}),
// A sibling @moq package is published to npm minutes before its dependents
// in the same release, and Deno otherwise refuses npm versions under 24h old.
minimumDependencyAge: 0,
// dist is gitignored, so un-ignore it with a "!" negation; JSR honors
// .gitignore otherwise and would drop the whole build from the graph.
publish: { include: ["dist", "README.md", "LICENSE*"], exclude: ["!dist"] },
};

writeFileSync("jsr.json", JSON.stringify(jsr, null, 2));
console.log("📦 jsr.json written");
}

function rewriteDtsImports() {
// JSR's doc generator resolves the .d.ts import graph and can't handle a
// ".ts" extension (the dist ships .js/.d.ts, not .ts) or a bare "." specifier.
// Normalize those in the declarations so doc generation resolves. Only the
// .d.ts needs this; the .js keeps its own specifiers for npm consumers.
const glob = new Bun.Glob("**/*.d.ts");
for (const rel of glob.scanSync("dist")) {
const file = join("dist", rel);
const body = readFileSync(file, "utf8");
const next = body
.replace(/(from\s+"\.[^"]*?)\.ts"/g, '$1"') // "./x.ts" -> "./x"
.replace(/(from\s+")\.(")/g, "$1./index$2"); // "." -> "./index"
if (next !== body) writeFileSync(file, next);
}
}

function injectSelfTypes() {
// JSR ignores a sibling .d.ts unless the .js references it explicitly;
// without this it infers types from the JS and reports "slow type" warnings.
const glob = new Bun.Glob("**/*.js");
for (const rel of glob.scanSync("dist")) {
const js = join("dist", rel);
const dts = js.replace(/\.js$/, ".d.ts");
if (!existsSync(dts)) continue;
const body = readFileSync(js, "utf8");
if (body.includes("@ts-self-types")) continue;
// Sibling .d.ts (same directory), so just its basename.
const directive = `/* @ts-self-types="./${basename(dts)}" */\n`;
// A shebang (bin entrypoints) must stay on line 1, so insert after it;
// otherwise the directive goes first.
if (body.startsWith("#!")) {
const nl = body.indexOf("\n") + 1;
writeFileSync(js, body.slice(0, nl) + directive + body.slice(nl));
} else {
writeFileSync(js, directive + body);
}
}
}
93 changes: 5 additions & 88 deletions js/common/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,9 @@ import { execFileSync } from "node:child_process";

const dryRun = process.argv.includes("--dry-run") || process.env.DRY_RUN === "true";

// JSR publish token, from the `npm` Environment. Remove it from this process's
// environment so only `deno publish` receives it, not builds or npm.
const jsrToken = process.env.JSR_TOKEN;
delete process.env.JSR_TOKEN;
// Read package.json to get the name and version.
const { name, version } = JSON.parse(await Bun.file("package.json").text());

// Read package.json to get name, version, and whether to also publish to JSR.
const pkg = JSON.parse(await Bun.file("package.json").text());
const { name, version } = pkg;
// Publish to JSR alongside npm, unless the package opts out with "jsr": false
// (the web-component packages do, since JSR forbids the global type augmentation
// custom elements need). Same predicate as package.ts so the manifest it
// generates and the publish here stay in lockstep; the release-script clause is
// always true here (this file is the release script).
const publishJsr = Boolean(pkg.scripts?.release) && pkg.jsr !== false;

// Whether this exact version already exists on each registry. The two are
// checked independently so a package can be onboarded onto JSR even when its
// current version is already on npm.
function npmHasVersion(): boolean {
try {
const published = execFileSync("npm", ["view", name, "version"], {
Expand All @@ -32,23 +17,9 @@ function npmHasVersion(): boolean {
}
}

async function jsrHasVersion(): Promise<boolean> {
try {
const res = await fetch(`https://jsr.io/${name}/meta.json`);
if (!res.ok) return false;
const meta = (await res.json()) as { versions?: Record<string, unknown> };
return Boolean(meta.versions?.[version]);
} catch {
return false;
}
}

// Skip the registry checks in dry-run mode so we always exercise the build +
// publish manifest, even when the version is already published.
const npmDone = !dryRun && npmHasVersion();
const jsrDone = !dryRun && publishJsr && (await jsrHasVersion());

if (!dryRun && npmDone && (!publishJsr || jsrDone)) {
// Skip the registry check in dry-run mode so we always exercise the build and
// pack, even when the version is already published.
if (!dryRun && npmHasVersion()) {
console.log(`⏭️ ${name}@${version} already published, skipping`);
process.exit(0);
}
Expand All @@ -64,62 +35,8 @@ if (dryRun) {
// same packaging path without any registry roundtrip.
console.log(`🧪 Packing ${name}@${version} (dry-run)...`);
execFileSync("npm", ["pack", "--dry-run"], { stdio: "inherit", cwd: "dist" });
} else if (npmDone) {
console.log(`⏭️ ${name}@${version} already on npm, skipping`);
} else {
console.log(`🚀 Publishing ${name}@${version} to npm...`);
// Use npm for publishing to support OIDC trusted publishing
execFileSync("npm", ["publish", "--access", "public"], { stdio: "inherit", cwd: "dist" });
}

// JSR (jsr.json is generated by the build for packages with a release script).
if (publishJsr) {
// Call `deno publish` directly (what `jsr publish` wraps) using the deno from
// the nix devshell, so parallel package releases don't race on a runtime
// jsr/deno download into a shared bunx cache. jsr.json and the generated dist
// leave the tree dirty, so allow it. Without a token, Deno tries JSR's GitHub
// OIDC trust under GitHub Actions, which the packages should not grant since
// any branch of the repository could claim it, and a browser login locally.
const args = [
"publish",
"--unstable-bare-node-builtins",
"--unstable-sloppy-imports",
"--no-check",
"--allow-dirty",
];
// Ignore package.json so deps resolve through jsr.json's npm: import map, as
// a JSR consumer would. Otherwise Deno follows the workspace symlinks into a
// sibling's src/, whose Vite-only imports (`?inline`) only its npm build
// resolves. This means a dependency must already be on npm, which holds since
// `bun --filter` releases dependencies first.
const env = { ...process.env, DENO_NO_PACKAGE_JSON: "1" };
if (dryRun) {
// A PR that bumps a sibling depends on a version npm won't have until the
// release publishes it, so Deno can't resolve the dependent yet.
const jsr = JSON.parse(await Bun.file("jsr.json").text()) as { imports?: Record<string, string> };
const unpublished = Object.values(jsr.imports ?? {})
.filter((spec) => spec.startsWith("npm:@moq/"))
.map((spec) => spec.slice("npm:".length))
.filter((spec) => {
try {
execFileSync("npm", ["view", spec, "version"], { stdio: "ignore" });
return false;
} catch {
return true;
}
});
if (unpublished.length > 0) {
console.log(`⏭️ Skipping JSR dry-run: ${unpublished.join(", ")} not on npm yet`);
} else {
console.log(`🧪 Publishing ${name}@${version} to JSR (dry-run)...`);
args.push("--dry-run");
execFileSync("deno", args, { stdio: "inherit", env });
}
} else if (jsrDone) {
console.log(`⏭️ ${name}@${version} already on JSR, skipping`);
} else {
console.log(`🚀 Publishing ${name}@${version} to JSR...`);
if (jsrToken) args.push("--token", jsrToken);
execFileSync("deno", args, { stdio: "inherit", env });
}
}
2 changes: 1 addition & 1 deletion js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default:
# `build` runs here rather than as its own gate: the per-package `check` script
# is `tsc --noEmit`, which never runs declaration emit, so the errors that only
# surface when emitting a `.d.ts` (TS4023 and friends) pass `check` and then
# break a JSR/npm publish. It costs about 20s for every package.
# break an npm publish. It costs about 20s for every package.
#
# `common/deps.ts` runs first and cheaply: bundlers resolve an undeclared
# workspace import through hoisting, so nothing else in `check` notices one.
Expand Down
1 change: 0 additions & 1 deletion js/moq-boy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@moq/boy",
"type": "module",
"version": "0.4.1",
"jsr": false,
"description": "MoQ Boy - Crowd-controlled Game Boy streaming via MoQ",
"license": "(MIT OR Apache-2.0)",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion js/net/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module
*/

/** Re-export of {@link https://jsr.io/@moq/signals | @moq/signals}, the reactive primitives used throughout this package. */
/** Re-export of {@link https://www.npmjs.com/package/@moq/signals | @moq/signals}, the reactive primitives used throughout this package. */
export * as Signals from "@moq/signals";
/** Broadcast announcement streams. */
export * as Announce from "./announce.ts";
Expand Down
1 change: 0 additions & 1 deletion js/publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@moq/publish",
"type": "module",
"version": "0.5.5",
"jsr": false,
"description": "Publish Media over QUIC broadcasts",
"license": "(MIT OR Apache-2.0)",
"repository": {
Expand Down
1 change: 0 additions & 1 deletion js/watch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@moq/watch",
"type": "module",
"version": "0.6.6",
"jsr": false,
"description": "Watch Media over QUIC broadcasts",
"license": "(MIT OR Apache-2.0)",
"repository": {
Expand Down
Loading