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

Abierto
#481 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
54/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
node.js, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
388
Forks
88
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de apple/app-store-server-library-node

Todos los issues de apple/app-store-server-library-node

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.