Skip to content

fix(schema): generate the token validator at build time (0.2.3) - #61

Merged
asiridalugoda merged 1 commit into
mainfrom
fix/workers-safe-validator
Nov 17, 2026
Merged

asiridalugoda merged 1 commit into
mainfrom
fix/workers-safe-validator

Conversation

@asiridalugoda

Copy link
Copy Markdown
Contributor

Problem

@helixar_ai/hdp 0.2.2 compiles the token JSON Schema with Ajv when the module loads (ajv.compile at the top of src/schema/validator.ts). Ajv compiles with new Function, so importing the package throws

EvalError: Code generation from strings disallowed for this context

in any runtime that forbids code generation from strings: Cloudflare Workers, pages with a strict Content-Security-Policy, and Node started with --disallow-code-generation-from-strings. The failure happens on import, before any HDP function is called, so a Worker that imports the SDK fails at startup.

Change

  • scripts/build-validator.mjs compiles src/schema/token.schema.json once, using Ajv's standalone code output, and writes src/schema/validator.generated.js. The generated file is committed and also regenerated by a new prebuild step. Its header records the SHA-256 of the schema it came from.
  • src/schema/validator.ts imports the generated validator. A local formatErrors reproduces Ajv's default errorsText output (data<instancePath> <message>, joined with ", "), so HdpSchemaError messages are unchanged. For example, SCHEMA_INVALID: Token schema validation failed: data must have required property 'header' is identical before and after.
  • ajv and ajv-formats move to devDependencies. The generated code needs one runtime helper (ajv/dist/runtime/ucs2length), which tsup now bundles into dist/. The published package therefore no longer depends on Ajv.
  • .github/workflows/ci.yml builds before running tests, so the new dist-level test runs in CI instead of being skipped.
  • @helixar_ai/hdp, @helixar_ai/hdp-mcp, hdp-validate and @helixar_ai/hdp-autogen move to 0.2.3. There is no API change.

Alternative not taken

Keeping a runtime-compiled validator behind a lazy, first-use compile would only defer the failure to the first verifyToken call in a strict runtime. Precompiling removes the runtime compile entirely.

Verification

Check Before (0.2.2 validator) After
tests/unit/dist-no-codegen.test.ts: loads dist/index.js and dist/index.mjs under --disallow-code-generation-from-strings, issues and verifies a token, and rejects an invalid token with HdpSchemaError 2 failed: EvalError: Code generation from strings disallowed at load 2 passed
tests/unit/schema-standalone.test.ts: the committed generated file matches the generator output fails when the schema is edited without regenerating (checked by editing the schema title) passes
npm run typecheck n/a passes
npx vitest run tests/unit (the validator runs inside every token operation) n/a 17 files, 114 tests passed
npm run build in packages/hdp-mcp, hdp-cli, hdp-autogen-ts n/a pass

Local runs used Node 26. The Node 20/22 matrix, the packed-consumer smoke test (.github/scripts/smoke-npm-release.sh) and the Python suites run in CI.

Release

After merge, pushing tag v0.2.3 publishes the four npm packages through release.yml. Release notes text:

The token schema validator is generated at build time. The package no longer compiles code at load time and loads on Cloudflare Workers (workerd). No API change.

https://claude.ai/code/session_01Lwur7k4tD9t1MQ7BU2Q4R4

The token schema validator was compiled with Ajv when the module loaded.
Ajv compiles with `new Function`, so the package threw
"EvalError: Code generation from strings disallowed" on import in
runtimes that forbid code generation from strings: Cloudflare Workers,
strict Content-Security-Policy pages, and Node run with
--disallow-code-generation-from-strings.

scripts/build-validator.mjs now compiles token.schema.json once, with
Ajv's standalone code output, into src/schema/validator.generated.js.
The file is committed and regenerated by the prebuild step.
validator.ts imports it and formats errors the way Ajv's errorsText did,
so HdpSchemaError messages are unchanged. Ajv and ajv-formats move to
devDependencies: the one runtime helper the generated code uses is now
bundled into dist/, and the published package no longer depends on Ajv.

Tests:
- tests/unit/dist-no-codegen.test.ts loads dist/index.js and
  dist/index.mjs under --disallow-code-generation-from-strings, issues
  and verifies a token, and checks that an invalid token is rejected.
  Against the previous validator both cases fail with the EvalError
  above; with this change both pass.
- tests/unit/schema-standalone.test.ts fails when the committed
  generated file no longer matches the schema (checked by editing the
  schema title without regenerating).

CI builds before running tests so the dist test runs instead of being
skipped. Versions of @helixar_ai/hdp, hdp-mcp, hdp-cli and hdp-autogen
move to 0.2.3.

Claude-Session: https://claude.ai/code/session_01Lwur7k4tD9t1MQ7BU2Q4R4
@asiridalugoda
asiridalugoda merged commit d996ad4 into main Nov 17, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant