Skip to content

Decode npm lockfile packages as JSON - #107

Open
andrew wants to merge 1 commit into
mainfrom
npm-compact-lockfile
Open

andrew wants to merge 1 commit into
mainfrom
npm-compact-lockfile

Conversation

@andrew

@andrew andrew commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

A compact package-lock.json parsed to zero dependencies. The v2/v3 path was chosen by matching indented text in the first 200 bytes ("lockfileVersion": 3), and the scanner it selects reads entries line by line, so a document without npm's own formatting fell through to the v1 nested dependencies tree or parsed as nothing.

The line scanner stays as the primary path, since npm writes one key per line and reading it without decoding the document is what keeps large lockfiles cheap. When it comes back empty the document is decoded with a streaming JSON decoder instead, which reads the packages entries in document order and handles the v1 tree. Both paths now fill the same entry type and share one conversion to a dependency, and internal/npm.TestPackageLockPathsAgree requires them to produce identical results on the fixtures npm wrote.

Dispatch is by which key the document has rather than by how it is spaced: no "packages" anywhere means v1. On the 2.2MB 10,000-package benchmark, canonical v3 is unchanged at 15.6ms and canonical v1 costs 4.5% more for that one scan (18.9ms to 19.8ms); a compacted document of the same size parses in 20.7ms where it previously returned nothing.

A lockfile carrying a packages section is now read from it whatever lockfileVersion it declares, and direct dependencies come from the root "" entry even when it appears after the packages that reference it.

Copilot AI lite review requested due to automatic review settings September 27, 2026 05:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

The v2/v3 line scanner needs npm's own formatting, and the format check
matched indented text, so a compact package-lock.json parsed to zero
dependencies. Keep the scanner for canonical lockfiles and decode the
document when it reads nothing.
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.

2 participants