Repository navigation
fix(schema): generate the token validator at build time (0.2.3) - #61
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
@helixar_ai/hdp0.2.2 compiles the token JSON Schema with Ajv when the module loads (ajv.compileat the top ofsrc/schema/validator.ts). Ajv compiles withnew Function, so importing the package throwsin 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.mjscompilessrc/schema/token.schema.jsononce, using Ajv's standalone code output, and writessrc/schema/validator.generated.js. The generated file is committed and also regenerated by a newprebuildstep. Its header records the SHA-256 of the schema it came from.src/schema/validator.tsimports the generated validator. A localformatErrorsreproduces Ajv's defaulterrorsTextoutput (data<instancePath> <message>, joined with", "), soHdpSchemaErrormessages are unchanged. For example,SCHEMA_INVALID: Token schema validation failed: data must have required property 'header'is identical before and after.ajvandajv-formatsmove todevDependencies. The generated code needs one runtime helper (ajv/dist/runtime/ucs2length), which tsup now bundles intodist/. The published package therefore no longer depends on Ajv..github/workflows/ci.ymlbuilds before running tests, so the new dist-level test runs in CI instead of being skipped.@helixar_ai/hdp,@helixar_ai/hdp-mcp,hdp-validateand@helixar_ai/hdp-autogenmove to0.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
verifyTokencall in a strict runtime. Precompiling removes the runtime compile entirely.Verification
tests/unit/dist-no-codegen.test.ts: loadsdist/index.jsanddist/index.mjsunder--disallow-code-generation-from-strings, issues and verifies a token, and rejects an invalid token withHdpSchemaErrorEvalError: Code generation from strings disallowedat loadtests/unit/schema-standalone.test.ts: the committed generated file matches the generator outputnpm run typechecknpx vitest run tests/unit(the validator runs inside every token operation)npm run buildinpackages/hdp-mcp,hdp-cli,hdp-autogen-tsLocal 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.3publishes the four npm packages throughrelease.yml. Release notes text:https://claude.ai/code/session_01Lwur7k4tD9t1MQ7BU2Q4R4