Skip to content

apisurface: npm packages from their TypeScript declarations (I19.12) - #243

Merged
adssib merged 4 commits into
mainfrom
pl4-npm
Nov 21, 2026
Merged

adssib merged 4 commits into
mainfrom
pl4-npm

Conversation

@adssib

@adssib adssib commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #242.

Closes #212

What this changes

The apisurface kind reads npm packages: plugins/apisurface/dtsurface reads each release's TypeScript declarations (.d.ts, .d.mts, .d.cts) out of the registry tarball, in memory, as text. Needs a minor release (new reader, new ecosystem, Release gains fields).

How it parses, and why this way. Declaration files hold declarations only, never statements, so a tokenizer (comments, strings, template literal types, =>, single > so Array<Array<T>> closes) and a parser of declarations that skips each type expression by its brackets read every name, member and parameter a caller can see: imports and every export form (export { a as b } from, export *, export * as ns, export =, export default, export import X = A.B), classes (public members, constructor, overloads, accessors), functions, interfaces, type aliases, enums, variables, namespaces (merged with classes, the ambient-export rule), and declare module "x". Entry points come from package.json (exports with conditions and wildcards, else types/typings). About a thousand lines of standard-library Go: no dependency, the make imports allowlist is unchanged. Not chosen: the TypeScript compiler (JavaScript: running Node and npm code next to the plugins), Go ports of TS parsers (internal to their projects, or cgo via tree-sitter), a type checker (far more than an API diff needs). No package code runs and no install script runs.

Compared like Python, but by position: a parameter gone off the end (no rest parameter now) is removed_parameter; one optional before and required now is changed_default; a renamed parameter is no change. Notes, not events: a member of a name now re-exported from another package or now a value/interface/type; a method gone from a class that extends another package's class; names an entry point may get from export * from "other".

One declaration reachable at several import paths is now one event (both ecosystems), every path in subject.symbols, the shortest first. PyPI goldens from #242 change accordingly (fewer, merged events; the wanted lists in pypi_test.go say exactly which).

Ten npm sources, each with a golden and a drop pair of real consecutive releases (trimmed tarballs):

source package golden pair drop pair
openai/node-surface npm:openai 7.59.0 → 7.60.0 7.60.0 → 7.61.0
anthropic/typescript-surface npm:@anthropic-ai/sdk 1.6.0 → 1.7.0 1.7.0 → 1.8.0
stripe/node-surface npm:stripe 22.6.2 → 23.0.0 23.0.0 → 23.0.1
google/genai-js-surface npm:@google/genai 0.15.0 → 1.0.0 1.0.0 → 1.1.0
sentry/node-surface npm:@sentry/node 9.47.2 → 10.0.0 11.12.0 → 11.13.0 (logger namespace made a const: notes only)
mongodb/node-surface npm:mongodb 6.22.0 → 7.0.0 7.0.0 → 7.1.0
redis/node-redis-surface npm:@redis/client, npm:redis 1.6.1 → 5.0.0 6.3.0 → 6.3.1
supabase/js-surface npm:@supabase/supabase-js 1.35.7 → 2.0.0 2.118.0 → 2.119.0
neon/serverless-surface npm:@neondatabase/serverless 0.10.4 → 1.0.0 1.0.0 → 1.0.1
convex/js-surface npm:convex 1.35.0 → 1.35.1 1.42.2 → 1.42.3

new-plugin's apisurface template takes the ecosystem: readers pypi (default) and npm, picked by an npm: package when --reader is left out.

#212 acceptance criteria

  • A golden test over two saved (trimmed) releases of a real npm package (ten of them)
  • Names not exported, and declarations marked @internal, never produce events (TestRead)
  • No package code is executed and no install script runs (only package.json and declaration files are read out of the tarball)
  • Subtasks: npm releases listed and tarballs read in memory; the reader; the same four events; sources registered with npm: packages and pages; the template takes the ecosystem

Checklist

  • Metadata complete in the registration (plugins/apisurface/npm_packages.go), and I read the words.
  • Every commit is signed off (DCO)
  • Golden tests (TestNPMGolden), drop tests (TestNPMDrop), reader tests (dtsurface_test.go, incl. a truncated-input test)
  • testdata/README.md lists each file
  • providers/<provider>-<source>.md for each, with Why this source
  • Fetches only through env.Get
  • make check passes locally
  • Nothing private

Calls I made (please check)

  • Interfaces and type aliases inside namespaces aren't read (an entry point's own are): Stainless SDKs re-export thousands of types through namespaces at many paths (openai: 81k symbols → 13k). Stripe's Stripe.* resource types are therefore not read (its API spec covers fields).
  • Property types aren't followed: client.chat.completions.create is reached as Completions.create through namespaces and entry points.
  • redis reads @redis/client (the redis package only re-exports it wholesale), covering npm:redis too.
  • Skipped from the audit's npm list: @octokit/rest (its API is a mapped object type from a plugin package: 2 symbols, nothing to compare without type resolution), and the low-priority @vercel/sdk, @planetscale/database.
  • Import entry "import" condition preferred over "require" (Stripe's CJS typings re-wrap the class).
  • Release bump: minor.

Related PRs

🤖 Generated with Claude Code

@claude

claude Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Claude finished @adssib's task in 2m 2s —— View job


✅ Ready to merge. Every rule is met, the 10 npm sources have complete registrations, pages, goldens and drop tests, and every check I could run passes. Two small non-blocking notes are inline.

Rules

Rule Status Evidence
Metadata in the registration (Rule 1) ✅ plugins/apisurface/npm_packages.go: each provider has an id, name, category and homepage. Each source has an id, LayerSDK, Watches, URLs (the npm page and the repo), npm: packages (redis also covers npm:redis) and its Page. No hand-kept list was added. TestNPMRegistersItself and plugins/all pass.
Provider pages ✅ 10 new providers/*-surface.md, each with Why this source: the link, checked 2026-11-21, why it's best, the alternatives and a quote (I read openai-node-surface.md and convex-js-surface.md in full).
Golden test and quote ✅ TestNPMGolden runs over real trimmed tarballs. Every event cites registry.npmjs.org/...tgz and quotes the file and line in both releases. testdata/README.md has a row per file with the URL, the date, the license and how it was trimmed.
Drop test ✅ TestNPMDrop uses one real consecutive pair per source.
Golden edits explained ✅ The PyPI goldens change only because one declaration at several paths is now one event. The PR body says so, and pypi_test.go's wanted lists show exactly which events merged.
Fetch only via env ✅ readNPM uses env.Get for the version document and the tarball. The tarball is opened in memory; links and non-regular entries are skipped, and so are node_modules/ and ../ paths. Sizes are capped. Only package.json and .d.ts/.d.mts/.d.cts files are kept. No net/http.
Nothing private (Rule 2) ✅ Nothing private in the code, tests, docs or PR body. leak_guard.py --all passes.

Bugs

None found. Precision looks right:

  • Parameters are compared only when neither side is overloaded and both have params, so variables, interfaces and a class with no constructor are skipped.
  • A rest parameter turns a removal into a note.
  • Removals that may not be real are notes, not events: an export * from another package, a parent that is now re-exported or is now a value or type, a base class from another package.
  • @internal, private and protected members are left out.

Verified

  • go build ./...: OK
  • go vet ./...: OK
  • gofmt -l .: printed nothing
  • go test -race ./...: all packages pass, including plugins/apisurface and plugins/apisurface/dtsurface, plus cmd/new-plugin
  • python3 scripts/leak_guard.py --all: "no banned terms (8 hashed terms checked)"
  • Not run:
    • make check and make imports nethttp todos: they need approval in this runner. The CI_go run on the PR covers them.
    • Whether npm's per-version endpoint answers @scope%2Fname/<version> for scoped packages: I couldn't reach the registry. It's worth a single live ReadAt against @anthropic-ai/sdk before release, since the tests only serve saved copies.

Non-blocking

  • comparePositional (inline): when a required parameter is inserted in front, the event names the new parameter and says it "is no longer optional". The break is real, but the wording is off.
  • Convex test comment (inline): it mentions convex/react's QueryOptions and UseQueryResult, but the golden has only convex/browser.QueryOptions. Please also confirm the removal isn't caused by the trimmed tarball leaving a file out.
  • @google/genai: one removal is still 3 events (., /node, /web), because each entry point is its own rollup file and the merge groups by file and line. That's acceptable, but worth a line on the provider page under Known gaps.
  • PR body: "combined in #COMBINED" is still a placeholder.

for i := len(old.Params); i < len(now.Params); i++ {
if np := now.Params[i]; !np.HasDef && np.Kind != "args" && !slices.ContainsFunc(old.Params, func(p pysurface.Param) bool { return p.Kind == "args" }) {
out = append(out, Change{Symbol: k, Note: fmt.Sprintf("%s: new required parameter %s (no subject for it in the contract yet)", k, np.Name)})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking: this pairs parameters by position, so when a release inserts a required parameter in front (Stainless SDKs do this when a path parameter moves, create(params?) → create(id, params?)), the event names now.Params[i].Name (id) and says it "is no longer optional", though id didn't exist before. The break is real and the hint ("Pass id") is right, so this is about the wording. You could check op.Name == now.Params[i].Name and, when the names differ, say "a required parameter id now comes at position i+1" in the description. A rename-only case already produces nothing, which is good.

"removed_symbol Transaction ",
}, [2]string{"7.0.0", "7.1.0"}},
// convex 1.35.1 removed the react entry's QueryOptions and
// UseQueryResult types.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking: this comment says 1.35.1 removed the react entry's QueryOptions and UseQueryResult, but the wanted list (and the golden) has only convex/browser.QueryOptions. Could you make the comment match? If UseQueryResult really was removed from convex/react, also say why it isn't an event: for example, it's an interface inside a namespace, or convex/react re-exports it. Please also confirm that 1.35.1 doesn't still declare QueryOptions in a file the trimmed tarball left out. The trimmed tarball only keeps browser/index.d.ts and browser/query_options.d.ts, so if the type moved to another file that browser/index.d.ts re-exports, this golden would come from the trimming and not from a real removal.

adssib and others added 4 commits November 20, 2026 21:54
dtsurface reads a tarball's .d.ts, .d.mts and .d.cts files as text (a
tokenizer and a parser of declarations, standard library only): every
entry point package.json declares, what each exports, and below classes
and namespaces their public members, with parameters compared by
position. Nothing in the package runs.

Registers ten npm sources (openai, @anthropic-ai/sdk, stripe,
@google/genai, @sentry/node, mongodb, @redis/client, @supabase/supabase-js,
@neondatabase/serverless, convex), each with a golden and a drop pair of
real releases. One declaration reachable at several import paths is now
one event for both ecosystems, its paths in the subject's symbols.
new-plugin's apisurface template takes the ecosystem (an npm: package
picks the npm reader).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: adssib <75389300+adssib@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: adssib <75389300+adssib@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: adssib <75389300+adssib@users.noreply.github.com>
Under another name it may have been inserted before the optional one, so changed_default (and its wording) applies only when the name is the same. Also: OpenAI's Lister check beside its adapter; the class-by-name gap on the npm pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: adssib <75389300+adssib@users.noreply.github.com>
Base automatically changed from pl4-pysurface to main November 21, 2026 03:21
@adssib
adssib merged commit 54e521f into main Nov 21, 2026
8 of 12 checks passed
@adssib
adssib deleted the pl4-npm branch November 21, 2026 03:21
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.

I19.12 · apisurface for npm: a package's exported API from its .d.ts files

1 participant