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

未关闭
#481 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
54/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
TypeScript
星标
388
派生
88
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apple/app-store-server-library-node 的其他 Issue

查看 apple/app-store-server-library-node 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。