Skip to content

Commit 0664654

Browse files
authored
fix: circular dependencies in fetch (nodejs#1630)
1 parent 08772d9 commit 0664654

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

lib/fetch/file.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,16 @@ function convertLineEndingsNative (s) {
312312
return s.replace(/\r?\n/g, nativeLineEnding)
313313
}
314314

315-
module.exports = { File, FileLike }
315+
// If this function is moved to ./util.js, some tools (such as
316+
// rollup) will warn about circular dependencies. See:
317+
// https://github.com/nodejs/undici/issues/1629
318+
function isFileLike (object) {
319+
return object instanceof File || (
320+
object &&
321+
(typeof object.stream === 'function' ||
322+
typeof object.arrayBuffer === 'function') &&
323+
object[Symbol.toStringTag] === 'File'
324+
)
325+
}
326+
327+
module.exports = { File, FileLike, isFileLike }

lib/fetch/formdata.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const { isBlobLike, isFileLike, toUSVString, makeIterator } = require('./util')
3+
const { isBlobLike, toUSVString, makeIterator } = require('./util')
44
const { kState } = require('./symbols')
5-
const { File, FileLike } = require('./file')
5+
const { File, FileLike, isFileLike } = require('./file')
66
const { webidl } = require('./webidl')
77
const { Blob } = require('buffer')
88

lib/fetch/util.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const { isBlobLike, toUSVString, ReadableStreamFrom } = require('../core/util')
66
const assert = require('assert')
77
const { isUint8Array } = require('util/types')
88

9-
let File
10-
119
// https://nodejs.org/api/crypto.html#determining-if-crypto-support-is-unavailable
1210
/** @type {import('crypto')|undefined} */
1311
let crypto
@@ -81,18 +79,6 @@ function requestBadPort (request) {
8179
return 'allowed'
8280
}
8381

84-
function isFileLike (object) {
85-
if (!File) {
86-
File = require('./file').File
87-
}
88-
return object instanceof File || (
89-
object &&
90-
(typeof object.stream === 'function' ||
91-
typeof object.arrayBuffer === 'function') &&
92-
/^(File)$/.test(object[Symbol.toStringTag])
93-
)
94-
}
95-
9682
function isErrorLike (object) {
9783
return object instanceof Error || (
9884
object?.constructor?.name === 'Error' ||
@@ -631,7 +617,6 @@ module.exports = {
631617
responseURL,
632618
responseLocationURL,
633619
isBlobLike,
634-
isFileLike,
635620
isValidReasonPhrase,
636621
sameOrigin,
637622
normalizeMethod,

0 commit comments

Comments
 (0)