Skip to content

Commit 78060b6

Browse files
authored
feat(fetch): add in native File class support (nodejs#1779)
1 parent cd1387a commit 78060b6

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

lib/fetch/body.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ const { FormData } = require('./formdata')
77
const { kState } = require('./symbols')
88
const { webidl } = require('./webidl')
99
const { DOMException, structuredClone } = require('./constants')
10-
const { Blob } = require('buffer')
10+
const { Blob, File: NativeFile } = require('buffer')
1111
const { kBodyUsed } = require('../core/symbols')
1212
const assert = require('assert')
1313
const { isErrored } = require('../core/util')
1414
const { isUint8Array, isArrayBuffer } = require('util/types')
15-
const { File } = require('./file')
15+
const { File: UndiciFile } = require('./file')
1616
const { StringDecoder } = require('string_decoder')
1717
const { parseMIMEType, serializeAMimeType } = require('./dataURL')
1818

1919
let ReadableStream = globalThis.ReadableStream
2020

21+
/** @type {globalThis['File']} */
22+
const File = NativeFile ?? UndiciFile
23+
2124
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
2225
function extractBody (object, keepalive = false) {
2326
if (!ReadableStream) {

lib/fetch/file.js

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

3-
const { Blob } = require('buffer')
3+
const { Blob, File: NativeFile } = require('buffer')
44
const { types } = require('util')
55
const { kState } = require('./symbols')
66
const { isBlobLike } = require('./util')
@@ -329,11 +329,14 @@ function convertLineEndingsNative (s) {
329329
// rollup) will warn about circular dependencies. See:
330330
// https://github.com/nodejs/undici/issues/1629
331331
function isFileLike (object) {
332-
return object instanceof File || (
333-
object &&
334-
(typeof object.stream === 'function' ||
335-
typeof object.arrayBuffer === 'function') &&
336-
object[Symbol.toStringTag] === 'File'
332+
return (
333+
(NativeFile && object instanceof NativeFile) ||
334+
object instanceof File || (
335+
object &&
336+
(typeof object.stream === 'function' ||
337+
typeof object.arrayBuffer === 'function') &&
338+
object[Symbol.toStringTag] === 'File'
339+
)
337340
)
338341
}
339342

lib/fetch/formdata.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
const { isBlobLike, toUSVString, makeIterator } = require('./util')
44
const { kState } = require('./symbols')
5-
const { File, FileLike, isFileLike } = require('./file')
5+
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
66
const { webidl } = require('./webidl')
7-
const { Blob } = require('buffer')
7+
const { Blob, File: NativeFile } = require('buffer')
8+
9+
/** @type {globalThis['File']} */
10+
const File = NativeFile ?? UndiciFile
811

912
// https://xhr.spec.whatwg.org/#formdata
1013
class FormData {

test/wpt/status/FileAPI.status.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55
]
66
},
77
"idlharness.any.js": {
8-
"fail": [
8+
"note": "These flaky tests only fail in < node v19; add in a way to mark them as such eventually",
9+
"flaky": [
910
"Blob interface: attribute size",
1011
"Blob interface: attribute type",
1112
"Blob interface: operation slice(optional long long, optional long long, optional DOMString)",
1213
"Blob interface: operation stream()",
1314
"Blob interface: operation text()",
1415
"Blob interface: operation arrayBuffer()",
16+
"URL interface: operation createObjectURL((Blob or MediaSource))",
17+
"URL interface: operation revokeObjectURL(DOMString)"
18+
],
19+
"fail": [
1520
"FileList interface: existence and properties of interface object",
1621
"FileList interface object length",
1722
"FileList interface object name",
1823
"FileList interface: existence and properties of interface prototype object",
1924
"FileList interface: existence and properties of interface prototype object's \"constructor\" property",
2025
"FileList interface: existence and properties of interface prototype object's @@unscopables property",
2126
"FileList interface: operation item(unsigned long)",
22-
"FileList interface: attribute length",
23-
"URL interface: operation createObjectURL((Blob or MediaSource))",
24-
"URL interface: operation revokeObjectURL(DOMString)"
27+
"FileList interface: attribute length"
2528
]
2629
},
2730
"filereader_events.any.js": {
@@ -30,4 +33,4 @@
3033
"events are dispatched in the correct order for a non-empty blob"
3134
]
3235
}
33-
}
36+
}

0 commit comments

Comments
 (0)