Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support use on Node.js 16 #550

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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();
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
32 changes: 32 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,33 @@
}
return res;
}

// support require from Node.js 16
export function patchForNode16() {
if (typeof global.ReadableStream === 'undefined') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
global.ReadableStream = ReadableStream;
}

Check warning on line 217 in src/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils.ts#L216-L217

Added lines #L216 - L217 were not covered by tests
if (typeof global.Blob === 'undefined') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
global.Blob = Blob;
}

Check warning on line 222 in src/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils.ts#L221-L222

Added lines #L221 - L222 were not covered by tests
if (typeof global.DOMException === 'undefined') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
global.DOMException = getDOMExceptionClass();
}

Check warning on line 227 in src/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils.ts#L226-L227

Added lines #L226 - L227 were not covered by tests
}
Comment on lines +212 to +228
Copy link

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 {

Check warning on line 232 in src/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils.ts#L231-L232

Added lines #L231 - L232 were not covered by tests
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
atob(0);
} catch (err: any) {
return err.constructor;
}
}

Check warning on line 239 in src/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/utils.ts#L235-L239

Added lines #L235 - L239 were not covered by tests
Comment on lines +231 to +239
Copy link

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