Safe versions for the browser, ws, vue-router and zod packs, written out in full #60
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/safe-pack-apis"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Every feature pack now ships safe versions, like the Node pack in #59, so none of them is checker-only any more.
browser-api.json+lib.dom.d.ts)/fetch,/storage,/indexeddb,/crypto,/canvas,/clipboard,/url,/encoding,/messaging,/workers,/dom,/file,/streams,/media,/codecs,/cache,/cookies,/credentials,/payment,/navigator,/midi,/webrtc,/webgpu,/webtransport,/wasm, plus/errorsws-api.json+@types/ws):send,ping,pong,handleUpgrade,newWebSocket/errorsvue-router-api.json+ vue-router's own types):push,replace,isReady,resolve/errorsparse(schema, x)/parseAsync(schema, x), hand-written;fromZodkept.No member is skipped in these packs. The Node pack still has 433 of 448 (unchanged).
The design is #59's: overloads are kept with only the return type wrapped, methods take the object first, the error is typed from the exact text, the error types and guards are exported, and a drift test covers the generated files.
QuotaExceededError | DOMError<"QuotaExceededError">. The new types areQuotaExceededError,DOMSubclassError<Class, Name, Props>(forRTCError,OverconstrainedError,WebTransportErrorandGPUPipelineError) andFirefoxException, with guards for each.localStorage.setItem(k, v)becomessetItem(localStorage, k, v), with the import added. This needed no checker change, only the data; Rust tests cover it.parseis typed throughthis. One signature oversafeParseserves zod 3, zod 4 and zod/mini without depending on zod. A misused schema (an async refinement underparse) still throws, asschema.parsedoes.vue-router errors
The texts were free-text ("Error (a guard threw, or a lazy route component failed to load)"). They are rewritten in the exact notation and checked against real vue-router 4.0.0, 4.1.6, 4.2.5 and 4.6.5:
resolve()throwsError {type: 1, location}: No match fororError: Missing required param.push()/replace()throw those two synchronously, before their promise exists. They reject with whatever a navigation guard throws, returns or passes tonext, or whatever a lazy component's import rejects with. A lazy import that resolves to nothing rejects withError: Couldn't resolve component. In 4.0 a rejecting import also turns into that error.isReady()rejects with whatever the initial navigation failed with.NavigationFailure, so it isn't a failure. The safepushkeeps vue-router'sNavigationFailure | void | undefinedas the value.safe-ts.jsonnow knows a router fromcreateRouter()/useRouter(), so the quick fix can rewriterouter.push(to)topush(router, to).Generator changes (
packages/pack-kit)Safe versions written out in full. This was the parent's request; the numbers are measured on Node 26. Each wrapper now has its own call site, for example
try { return self.readUInt32LE(...args); } catch (error) { return new Err(error); }. Functions are$module.f(...args), constructorsnew $module.C(...args), and async onesPromise.resolve(...).then(ok, failed)plus a synccatch.runtime.jskeeps onlyok/failed.Hot loop with
buf.readUInt32LE(o):attempthelper, beforenpm run bench:checknow runspackages/feature-node/bench/safe.bench.ts. The ratio is checked asmax(baseline 1.84, allowed ≤ 3.68), and the shared-helper ratio is reported as info. The bench is a script rather than a docsbenchblock because the docs site runs those in the browser, which has noBuffer.New options and rules:
Target.namespacehandlesWebAssembly.*.Target.mainputs a pack's safe versions in its main module.@types/ws(a module withexport =) and vue-router's hasheddist/router-*.d.mts(mapped by the names the package exports).typeofof a package namespace is handled.unexportedlists names anexport =module lists but can't be imported by (WebSocketEventMapof @types/ws, TS2614).core.jsfix.isErroris nowError.isError(e) || e instanceof Error. Firefox'sOverconstrainedErrorextendsErrorbut isn't one toError.isError, soisPlainErrorfailed on it in the browser test.Node pack. It is regenerated with written-out bodies. The diff is only the
.jsbodies,runtime.js/.d.tsand theisErrorline incore.js; the.d.tsfiles are identical.Packages
filesincludesafe/,"sideEffects": false, andpeerDependencieson std (ws and vue-router also on their library). Their descriptions no longer say checker-only.build-packages.mjslists them as runtime installs. They are still copied as generated.js+.d.ts, not compiled.npm run generate:saferegenerates all four generated packs.npm run typechecknow compiles each pack's declarations strictly (test/types/).Verification
Browser,
npm run test:browsers(Playwright 1.64.1): each triggered call goes through its safe version in the page, frames and sandboxed frames included, and must return anErrthat passes the guard of its declared type. Results:ws: the real-library test calls the safe versions; it passes with ws 8.0.0, 8.2.0, 8.7.0 and 8.23.0 (
SAFE_TS_WS).vue-router: the new real-library test (
SAFE_TS_VUE_ROUTER) passes with 4.0.0, 4.1.6, 4.2.5 and 4.6.5.Node: the errors test still checks 375 triggered members through their safe versions (same as #59).
node scripts/verify-packages.mjs: one safe version per pack (node, browser, ws, vue-router, zod) imports and runs from the packed tarballs, and tsc accepts their declarations.The brief's list:
cargo nextest run --workspacecargo clippy -q --workspace --all-targetsnpm run buildnpm run test:stdnpm run typechecknpm run typecheck:emitnode scripts/docs-typecheck.mjsnpm run test:docsnpm run test:vitenpm run bench:checkAll pass: nextest 205/205, test:std 280/280, bench:check 31 ratios, safe version 1.85x (allowed ≤ 3.68); no new clippy warnings.
Docs changed
docs/guide/libraries.md:docs/vue/router.md: shows the safepush, and says both forms work.README.md: repo map and the pack table's Runtime column.packages/pack-kit/README.mdpackages/feature-browser/test/browsers/README.mdCHANGELOG.mdExamples that wrapped these APIs in
unsafe(...)now show the safe version first and say both work:localStorage.setItemin the libraries guide, androuter.pushon the vue-router page.🤖 Generated with Claude Code
6e8450702c84aa25d6e184aa25d6e129defa03bf