Skip to content

Commit 2f943f8

Browse files
committed
fix: undici File
1 parent 60922a6 commit 2f943f8

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LRU } from 'ylru';
22
import { patchForNode16 } from './utils.js';
33

4+
45
patchForNode16();
56

67
import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';

src/utils.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { randomBytes, createHash } from 'node:crypto';
22
import { Readable } from 'node:stream';
33
import { performance } from 'node:perf_hooks';
44
import { ReadableStream, TransformStream } from 'node:stream/web';
5-
import { Blob } from 'node:buffer';
5+
import { Blob, File } from 'node:buffer';
6+
import { toUSVString } from 'node:util';
67
import type { FixJSONCtlChars } from './Request.js';
78
import { SocketInfo } from './Response.js';
89
import symbols from './symbols.js';
@@ -231,6 +232,37 @@ export function patchForNode16() {
231232
// @ts-ignore
232233
global.DOMException = getDOMExceptionClass();
233234
}
235+
// multi undici version in node version less than 20 https://github.com/nodejs/undici/issues/4374
236+
if (typeof globalThis.File === 'undefined') {
237+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
238+
// @ts-ignore
239+
globalThis.File = File;
240+
}
241+
242+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
243+
// @ts-ignore
244+
if (String.prototype.toWellFormed === undefined) {
245+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
246+
// @ts-ignore
247+
String.prototype.toWellFormed = function () {
248+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
249+
// @ts-ignore
250+
return toUSVString(this);
251+
}
252+
}
253+
254+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
255+
// @ts-ignore
256+
if (String.prototype.isWellFormed === undefined) {
257+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
258+
// @ts-ignore
259+
String.prototype.isWellFormed = function () {
260+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
261+
// @ts-ignore
262+
return toUSVString(this) === this;
263+
}
264+
}
265+
234266
}
235267

236268
// https://github.com/jimmywarting/node-domexception/blob/main/index.js

0 commit comments

Comments
 (0)