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

Đang mở
#481 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
54/100
Loại issue
Tính năng
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
node.js, typescript
Lĩnh vực
backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
388
Fork
88
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apple/app-store-server-library-node

Tất cả issue của apple/app-store-server-library-node

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.