Skip to content

lib,src: deduplicate code - #67495

Open
jasnell wants to merge 10 commits into
nodejs:mainfrom
jasnell:consolidate-duplicate-code
Open

jasnell wants to merge 10 commits into
nodejs:mainfrom
jasnell:consolidate-duplicate-code

Conversation

@jasnell

@jasnell jasnell commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Overall simplification. Deduplicate a number of JS and C++ bits.

See each individual commit.

The RSA, EC, CFRG, ML-DSA, and ML-KEM importKey implementations each
duplicated the handling of the 'KeyObjectHandle', 'spki', and 'pkcs8'
formats. Move it into a shared importAsymmetricKeyHandle() helper in
webcrypto_util, with an optional hook that ML-DSA and ML-KEM use to
reject PKCS#8 keys without a seed.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
exportKeySpki() and exportKeyPkcs8() contained identical switch
statements mapping the algorithm name to its export function. Move
the lookup into a shared getAsymmetricKeyExporter() helper.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
ClientRequest and Server duplicated the validation of the
insecureHTTPParser and httpValidation options, including the check
that they are mutually exclusive. Move it into a shared
setHttpParserOptions() helper in internal/http.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
util.debounce() and util.throttle() duplicated the construction of the
AbortError used for cancel()/abort, and the property definitions for
the returned function's control methods, state getters, length, and
name. Move both into internal/util/wrapped_function.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
The Writable and Duplex adapters created from web streams duplicated
the logic that encodes string chunks as a Uint8Array before writing
them to the WritableStream. Move it into a shared encodeStringChunk()
helper.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
SQLTagStore::Run(), Iterate(), Get(), and All() each duplicated the
database state checks, statement preparation, and parameter binding.
Move them into a shared PrepareForExecution() helper.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
URLPattern::Exec() and URLPattern::Test() duplicated the parsing of the
input and baseURL arguments. Move it into a shared
ParseURLPatternMatchArgs() helper.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
IndexOfString() and IndexOfBuffer() duplicated the computation of the
search range and the early returns for empty needles, empty haystacks,
and out-of-range offsets. Move it into a shared
GetIndexOfSearchRange() helper.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
The two Hash::HashInit() overloads duplicated the logic that
determines the digest length and validates the requested XOF output
length after the digest context is initialized. Move it into a shared
FinishHashInit() helper.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
The SecureContext ticket key callback signatures repeated the same
backend-specific preprocessor block to select between EVP_MAC_CTX and
HMAC_CTX. Define the type once as TicketHmacCtx and use it instead.

Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/net
  • @nodejs/sqlite
  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Nov 15, 2026
@jasnell
jasnell requested review from aduh95 and panva November 15, 2026 18:20
@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.13333% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.68%. Comparing base (d105ef7) to head (5bb76ef).
⚠️ Report is 17 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/webstreams/adapters.js 45.45% 12 Missing ⚠️
src/node_buffer.cc 85.71% 0 Missing and 7 partials ⚠️
src/node_url_pattern.cc 92.59% 0 Missing and 2 partials ⚠️
src/node_sqlite.cc 94.44% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67495      +/-   ##
==========================================
+ Coverage   90.67%   90.68%   +0.01%     
==========================================
  Files         799      800       +1     
  Lines      280245   280134     -111     
  Branches    54125    54079      -46     
==========================================
- Hits       254100   254037      -63     
+ Misses      16280    16252      -28     
+ Partials     9865     9845      -20     
Files with missing lines Coverage Δ
lib/internal/crypto/cfrg.js 95.45% <100.00%> (-0.31%) ⬇️
lib/internal/crypto/ec.js 96.78% <100.00%> (-0.16%) ⬇️
lib/internal/crypto/ml_dsa.js 97.19% <100.00%> (-0.14%) ⬇️
lib/internal/crypto/ml_kem.js 94.95% <100.00%> (-0.25%) ⬇️
lib/internal/crypto/rsa.js 94.64% <100.00%> (-0.29%) ⬇️
lib/internal/crypto/webcrypto.js 98.09% <100.00%> (-0.04%) ⬇️
lib/internal/crypto/webcrypto_util.js 98.28% <100.00%> (+0.22%) ⬆️
lib/internal/http.js 93.20% <100.00%> (+0.67%) ⬆️
lib/internal/http/client.js 97.10% <100.00%> (-0.05%) ⬇️
lib/internal/http/server.js 96.66% <100.00%> (-0.04%) ⬇️
... and 12 more

... and 21 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@araujogui araujogui left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SQLite LGTM

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@nodejs-github-bot

This comment was marked as outdated.

@panva panva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

node:crypto and Web Cryptography LGTM

@panva panva added the commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. label Nov 16, 2026
@panva panva added author ready PRs with CI started, the required approvals, and no outstanding review comments. resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. labels Nov 16, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 16, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@panva panva added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants