From 62d3a95d10ebe381d290d271bd7ecc2da4948462 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 2 Dec 2024 15:03:20 +0800 Subject: [PATCH 1/2] fix: support use on Node.js 16 --- src/index.ts | 4 ++++ src/utils.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/index.ts b/src/index.ts index 06a495da..0d4c5f28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/utils.ts b/src/utils.ts index f1c05102..b7fa5eee 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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'; @@ -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(); + } +} + +// https://github.com/jimmywarting/node-domexception/blob/main/index.js +function getDOMExceptionClass() { + try { + // @ts-ignore + atob(0); + } catch (err: any) { + return err.constructor; + } +} From 39bba2c37d4d95c6917d573f996a08a6926f1cc0 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 2 Dec 2024 15:11:55 +0800 Subject: [PATCH 2/2] f --- src/utils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index b7fa5eee..04f7a688 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -211,14 +211,17 @@ export function convertHeader(headers: Headers): IncomingHttpHeaders { // 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; } if (typeof global.Blob === 'undefined') { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore global.Blob = Blob; } if (typeof global.DOMException === 'undefined') { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore global.DOMException = getDOMExceptionClass(); } @@ -227,6 +230,7 @@ export function patchForNode16() { // https://github.com/jimmywarting/node-domexception/blob/main/index.js function getDOMExceptionClass() { try { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore atob(0); } catch (err: any) {