Use Node's built-in fetch instead of node-fetch (punycode DeprecationWarning, #219)

Aperta
#481 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
54/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, typescript

Direzione di ricerca

Read index.ts and jws_verification.ts, then inspect the three test files that import Response from node-fetch and the OCSP fetch mock. Check how the protected makeFetchRequest return type is used by subclasses before changing it. Done means the library uses Node's global fetch APIs, preserves the documented request and response behavior, and passes the build and tests described in the issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

On Node 22 and later, loading the library can print:

(node:8) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.

[email protected] depends on [email protected], which depends on [email protected]. whatwg-url/lib/url-state-machine.js and tr46/index.js both call require("punycode"), which loads Node's deprecated built-in module.

When the library runs straight from node_modules, Node does not print the warning, because it skips DEP0040 for code under node_modules. When it is bundled, for example by esbuild for AWS Lambda, those require calls end up in the bundle file. The bundle is outside node_modules, so the warning shows up on every cold start. Lambda logs stderr at ERROR level, so in our case this was the only ERROR line on the functions that use the library.

#219 reported the same warning and was closed because node-fetch 3 is ESM-only and the library is CommonJS. That reason still holds for node-fetch 3. This issue proposes a different route.

Proposal

The README lists Node 22+ as the requirement, and Node has shipped fetch, Headers, Response and AbortSignal.timeout as globals since Node 18. The library could use those and drop node-fetch and @types/node-fetch from its dependencies. It stays CommonJS.

I have a working branch. The library changes are small:

  • index.ts and jws_verification.ts stop importing node-fetch and use the global fetch and Headers.
  • The timeout option, which only exists in node-fetch, becomes signal: AbortSignal.timeout(...), with the same 30 s values.
  • response.buffer() in the OCSP check becomes Buffer.from(await response.arrayBuffer()).
  • response.headers.raw() in makeRequest is replaced by headers.forEach, with headers.getSetCookie() for set-cookie.
  • The error body in makeRequest is read as unknown and narrowed with a type guard (see point 4 below).
  • Three test files stop importing Response from node-fetch, and the OCSP mock spies on globalThis.fetch.

With these changes tsc -p tsconfig.build.json passes and all 456 tests pass on Node 22, 24 and 26. I also ran the built library against a local HTTP server to check a request with a Buffer body, a 429 response with Retry-After, and the request timeout.

It also removes form-data from the dependency tree. It currently comes in through @types/node-fetch, and [email protected] has an open advisory (GHSA-hmw2-7cc7-3qxx).

Compatibility

This is why I am opening an issue before a PR.

  1. makeFetchRequest is protected and can be overridden. Its return type changes from node-fetch's Response to the global Response. A subclass that builds a node-fetch Response would no longer type-check. Two of the test files do this today.
  2. APIException.headers changes slightly. The Fetch standard trims leading and trailing whitespace from header values, so one test case (Retry-After: " ") now gets [""]. Over a real connection Node's HTTP parser already trims it, so the value is the same either way. A header sent more than once comes back as one comma-joined entry (["a, b"] rather than ["a", "b"]), except set-cookie, which stays split.
  3. A request that times out in AppStoreServerAPIClient now rejects with a TimeoutError DOMException rather than node-fetch's FetchError. In SignedDataVerifier it is still wrapped as RETRYABLE_VERIFICATION_FAILURE, as before.
  4. Native response.json() returns Promise<unknown> where node-fetch returned Promise<any>, so the error path in makeRequest now checks the body's types before building the APIException. errorCode is used only when it is a number and errorMessage only when it is a string; anything else becomes null. The API always sends a numeric errorCode, so the visible difference is that a missing errorMessage is now null instead of undefined, which matches the declared string | null type.

If point 1 is a problem for a minor release, this could wait for the next major version.

Would you accept a PR along these lines? I can open it against main.

Lingua principale
TypeScript
Stelle
388
Fork
88
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apple/app-store-server-library-node

Tutte le issue di apple/app-store-server-library-node

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.