Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { LRU } from 'ylru';
import { patchForNode16 } from './utils.js';

patchForNode16();
Comment thread
fengmk2 marked this conversation as resolved.

import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';
import { RequestOptions, RequestURL } from './Request.js';

Expand Down
28 changes: 28 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { randomBytes, createHash } from 'node:crypto';
import { Readable } from 'node:stream';
import { performance } from 'node:perf_hooks';
import { ReadableStream } from 'node:stream/web';
import { Blob } from 'node:buffer';
import type { FixJSONCtlChars } from './Request.js';
import { SocketInfo } from './Response.js';
import symbols from './symbols.js';
Expand Down Expand Up @@ -205,3 +207,29 @@ export function convertHeader(headers: Headers): IncomingHttpHeaders {
}
return res;
}

// support require from Node.js 16
export function patchForNode16() {
if (typeof global.ReadableStream === 'undefined') {
// @ts-ignore
global.ReadableStream = ReadableStream;
}
if (typeof global.Blob === 'undefined') {
// @ts-ignore
global.Blob = Blob;
}
if (typeof global.DOMException === 'undefined') {
// @ts-ignore
global.DOMException = getDOMExceptionClass();
}
}
Comment on lines +212 to +228
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Replace @ts-ignore with @ts-expect-error

The static analysis tools correctly flag the usage of @ts-ignore. Using @ts-expect-error is preferred as it will error if the type error is fixed, preventing stale suppressions.

-    // @ts-ignore
+    // @ts-expect-error
     global.ReadableStream = ReadableStream;
-    // @ts-ignore
+    // @ts-expect-error
     global.Blob = Blob;
-    // @ts-ignore
+    // @ts-expect-error
     global.DOMException = getDOMExceptionClass();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function patchForNode16() {
if (typeof global.ReadableStream === 'undefined') {
// @ts-ignore
global.ReadableStream = ReadableStream;
}
if (typeof global.Blob === 'undefined') {
// @ts-ignore
global.Blob = Blob;
}
if (typeof global.DOMException === 'undefined') {
// @ts-ignore
global.DOMException = getDOMExceptionClass();
}
}
export function patchForNode16() {
if (typeof global.ReadableStream === 'undefined') {
// @ts-expect-error
global.ReadableStream = ReadableStream;
}
if (typeof global.Blob === 'undefined') {
// @ts-expect-error
global.Blob = Blob;
}
if (typeof global.DOMException === 'undefined') {
// @ts-expect-error
global.DOMException = getDOMExceptionClass();
}
}
🧰 Tools
🪛 eslint

[error] 214-214: Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free.

(@typescript-eslint/ban-ts-comment)


[error] 218-218: Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free.

(@typescript-eslint/ban-ts-comment)


[error] 222-222: Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free.

(@typescript-eslint/ban-ts-comment)

🪛 GitHub Check: Node.js / Test (windows-latest, 23)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (windows-latest, 22)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (windows-latest, 20)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (windows-latest, 18.19.0)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (windows-latest, 18)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 23)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 22)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 20)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 18.19.0)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (ubuntu-latest, 18)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (macos-latest, 23)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (macos-latest, 22)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (macos-latest, 18.19.0)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (macos-latest, 18)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

🪛 GitHub Check: Node.js / Test (macos-latest, 20)

[failure] 214-214:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 218-218:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free


[failure] 222-222:
Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free

Comment on lines +212 to +228
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add test coverage for Node.js 16 compatibility patches

The new patchForNode16 function lacks test coverage. Please add tests to verify:

  1. Polyfills are correctly applied when globals are undefined
  2. Existing globals are preserved when already defined

Would you like me to help create test cases for this function?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 216-217: src/utils.ts#L216-L217
Added lines #L216 - L217 were not covered by tests


[warning] 221-222: src/utils.ts#L221-L222
Added lines #L221 - L222 were not covered by tests


[warning] 226-227: src/utils.ts#L226-L227
Added lines #L226 - L227 were not covered by tests


// https://github.com/jimmywarting/node-domexception/blob/main/index.js
function getDOMExceptionClass() {
try {
// @ts-ignore
atob(0);
} catch (err: any) {
return err.constructor;
}
}
Comment on lines +231 to +239
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add test coverage for DOMException class retrieval

The getDOMExceptionClass function lacks test coverage. Please add tests to verify:

  1. The function returns the correct DOMException class
  2. Error handling for environments without atob

Would you like me to help create test cases for this function?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 231-232: src/utils.ts#L231-L232
Added lines #L231 - L232 were not covered by tests


[warning] 235-239: src/utils.ts#L235-L239
Added lines #L235 - L239 were not covered by tests