Skip to content

Commit

Permalink
fix: support use on Node.js 16
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 2, 2024
1 parent e030ec9 commit 62d3a95
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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();

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 23)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 23)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 23)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

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

Check failure on line 214 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
global.ReadableStream = ReadableStream;
}
if (typeof global.Blob === 'undefined') {
// @ts-ignore

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 23)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 23)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 23)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

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

Check failure on line 218 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
global.Blob = Blob;
}
if (typeof global.DOMException === 'undefined') {
// @ts-ignore

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 23)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 23)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 23)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

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

Check failure on line 222 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
global.DOMException = getDOMExceptionClass();
}
}

// https://github.com/jimmywarting/node-domexception/blob/main/index.js
function getDOMExceptionClass() {
try {
// @ts-ignore

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 23)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 23)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 23)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

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

Check failure on line 230 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
atob(0);
} catch (err: any) {
return err.constructor;
}
}

0 comments on commit 62d3a95

Please sign in to comment.