Skip to content

Commit 62d3a95

Browse files
committed
fix: support use on Node.js 16
1 parent e030ec9 commit 62d3a95

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { LRU } from 'ylru';
2+
import { patchForNode16 } from './utils.js';
3+
4+
patchForNode16();
5+
26
import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';
37
import { RequestOptions, RequestURL } from './Request.js';
48

src/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { randomBytes, createHash } from 'node:crypto';
22
import { Readable } from 'node:stream';
33
import { performance } from 'node:perf_hooks';
4+
import { ReadableStream } from 'node:stream/web';
5+
import { Blob } from 'node:buffer';
46
import type { FixJSONCtlChars } from './Request.js';
57
import { SocketInfo } from './Response.js';
68
import symbols from './symbols.js';
@@ -205,3 +207,29 @@ export function convertHeader(headers: Headers): IncomingHttpHeaders {
205207
}
206208
return res;
207209
}
210+
211+
// support require from Node.js 16
212+
export function patchForNode16() {
213+
if (typeof global.ReadableStream === 'undefined') {
214+
// @ts-ignore
215+
global.ReadableStream = ReadableStream;
216+
}
217+
if (typeof global.Blob === 'undefined') {
218+
// @ts-ignore
219+
global.Blob = Blob;
220+
}
221+
if (typeof global.DOMException === 'undefined') {
222+
// @ts-ignore
223+
global.DOMException = getDOMExceptionClass();
224+
}
225+
}
226+
227+
// https://github.com/jimmywarting/node-domexception/blob/main/index.js
228+
function getDOMExceptionClass() {
229+
try {
230+
// @ts-ignore
231+
atob(0);
232+
} catch (err: any) {
233+
return err.constructor;
234+
}
235+
}

0 commit comments

Comments
 (0)