Skip to content

feat(parser-pipeline): create @endo/parser-pipeline#3158

Draft
boneskull wants to merge 4 commits intomasterfrom
boneskull/ast-service
Draft

feat(parser-pipeline): create @endo/parser-pipeline#3158
boneskull wants to merge 4 commits intomasterfrom
boneskull/ast-service

Conversation

@boneskull
Copy link
Copy Markdown
Member

Babel AST parsing is expensive. When multiple consumers need to analyze or transform the same module source (@endo/evasive-transform, @endo/module-source, LavaMoat policy inspection), parsing each one independently wastes significant wall-clock time.

This package composes Babel visitor passes from multiple packages into a single parse-traverse-generate cycle. It provides two parserForLanguage-compatible parsers:

  • createComposedParser -- synchronous, in-process; for the execution path where modules load on-demand
  • createWorkerParser -- async, worker-pool-backed; for policy generation where hundreds of modules can parse in parallel

@boneskull
Copy link
Copy Markdown
Member Author

boneskull commented Apr 4, 2026

📚 Pull Request Stack


Managed by gh-stack

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 4, 2026

⚠️ No Changeset found

Latest commit: 460895c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@boneskull
Copy link
Copy Markdown
Member Author

TODO: figure out why the docs build is failing

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new @endo/parser-pipeline workspace package to avoid redundant Babel AST parsing by composing multiple analyzer/transform visitor passes into a single parse→traverse→generate cycle, and adds an async worker-pool-backed parser option for parallel policy-generation workflows. It also updates @endo/compartment-mapper’s parser typing and mapping logic to support asynchronous parsers alongside synchronous ones.

Changes:

  • Add @endo/parser-pipeline with createComposedParser (sync) and createWorkerParser (async worker pool), plus worker-side runPipelineInWorker.
  • Refactor @endo/compartment-mapper parser types and makeMapParsers to support async parsers and async module transforms.
  • Add tests for composed parsing and worker-pool behavior.

Reviewed changes

Copilot reviewed 23 out of 25 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
yarn.lock Adds new workspace package and Babel-related deps/types.
typedoc.json Formatting tweaks and excludes the new package from typedoc output.
packages/parser-pipeline/package.json Defines the new package, dependencies, and exports.
packages/parser-pipeline/index.js Public entrypoint exporting types + parser constructors.
packages/parser-pipeline/src/composed-parser.js Sync composed parser implementation.
packages/parser-pipeline/src/worker-parser.js Async parser wrapper backed by worker pool.
packages/parser-pipeline/src/worker-pool.js Worker thread pool and dispatch/queue/terminate logic.
packages/parser-pipeline/src/worker-runner.js Worker-side message listener that runs the pipeline per task.
packages/parser-pipeline/src/types/external.ts Public types for pipeline configuration and worker protocol.
packages/parser-pipeline/src/external.types.* Type-export shim for JS consumers.
packages/parser-pipeline/test/* AVA tests for composed parser, worker runner, and worker pool.
packages/parser-pipeline/README.md New package documentation and usage examples.
packages/parser-pipeline/SECURITY.md Package security policy doc.
packages/parser-pipeline/LICENSE Apache-2.0 license file.
packages/parser-pipeline/tsconfig*.json Typechecking/build config for the new package.
packages/compartment-mapper/src/types/external.ts Splits sync vs async parser types and updates ParserForLanguage typing.
packages/compartment-mapper/src/types/internal.ts Adjusts internal operator typings to accept ParseFn or AsyncParseFn.
packages/compartment-mapper/src/map-parser.js Refactors parser selection/trampolines for sync+async support.
packages/compartment-mapper/src/import-hook.js Adds a sync-parse type guard for importNow/dynamic-require constraints.
packages/compartment-mapper/src/link.js Simplifies heuristicImports access with updated parser types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@boneskull boneskull force-pushed the boneskull/ast-service branch 2 times, most recently from a9fdc61 to 9abf181 Compare April 7, 2026 23:33
This updates the code and types to handle async parsers. Currently, there are no async parsers in
`@endo/compartment-mapper`, which is a reasonable excuse for why they were not working.

While this looks like a much was changed, it is mostly type-related.  Some extra guards were added
and code split out within `map-parser.js` to support both the async and sync parser use-cases,
while maximizing logic reuse.
Babel AST parsing is expensive. When multiple consumers need to
analyze or transform the same module source
(`@endo/evasive-transform`, `@endo/module-source`, LavaMoat
policy inspection), parsing each one independently wastes
significant wall-clock time.

This package composes Babel visitor passes from multiple packages
into a single parse-traverse-generate cycle. It provides two
`parserForLanguage`-compatible parsers:

- `createComposedParser` -- synchronous, in-process; for the
  execution path where modules load on-demand
- `createWorkerParser` -- async, worker-pool-backed; for policy
  generation where hundreds of modules can parse in parallel
- Check `ast.errors` after `errorRecovery: true` parsing in worker runner
- Transfer `resultBytes.buffer` in `postMessage` to avoid copying
- Fix JSDoc: return type says `removePipelineListener`, not `stop`
- Wrap `worker.postMessage()` in try/catch to prevent leaked promises
- Restore thenable check in sync parser wrapper (`map-parser.js`)
- Remove duplicate JSDoc block on `createComposedParser`
- Fix `runWorkerPipeline` → `runPipelineInWorker` in README example
- Fix typo: "whichreceives" → "which receives" in README
- Fix `homepage` and `repository.directory` pointing at `module-analyzer`
- Fix JSDoc import path: `@endo/parser-pipeline/worker-runner.js`
@boneskull boneskull force-pushed the boneskull/ast-service branch from 9abf181 to 460895c Compare April 14, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants