Skip to content

Commit

Permalink
feat(fetch): add in native File class support (#1779)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Dec 8, 2022
1 parent cd1387a commit 78060b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
7 changes: 5 additions & 2 deletions lib/fetch/body.js
Expand Up @@ -7,17 +7,20 @@ const { FormData } = require('./formdata')
const { kState } = require('./symbols')
const { webidl } = require('./webidl')
const { DOMException, structuredClone } = require('./constants')
const { Blob } = require('buffer')
const { Blob, File: NativeFile } = require('buffer')
const { kBodyUsed } = require('../core/symbols')
const assert = require('assert')
const { isErrored } = require('../core/util')
const { isUint8Array, isArrayBuffer } = require('util/types')
const { File } = require('./file')
const { File: UndiciFile } = require('./file')
const { StringDecoder } = require('string_decoder')
const { parseMIMEType, serializeAMimeType } = require('./dataURL')

let ReadableStream = globalThis.ReadableStream

/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile

// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
function extractBody (object, keepalive = false) {
if (!ReadableStream) {
Expand Down
15 changes: 9 additions & 6 deletions lib/fetch/file.js
@@ -1,6 +1,6 @@
'use strict'

const { Blob } = require('buffer')
const { Blob, File: NativeFile } = require('buffer')
const { types } = require('util')
const { kState } = require('./symbols')
const { isBlobLike } = require('./util')
Expand Down Expand Up @@ -329,11 +329,14 @@ function convertLineEndingsNative (s) {
// rollup) will warn about circular dependencies. See:
// https://github.com/nodejs/undici/issues/1629
function isFileLike (object) {
return object instanceof File || (
object &&
(typeof object.stream === 'function' ||
typeof object.arrayBuffer === 'function') &&
object[Symbol.toStringTag] === 'File'
return (
(NativeFile && object instanceof NativeFile) ||
object instanceof File || (
object &&
(typeof object.stream === 'function' ||
typeof object.arrayBuffer === 'function') &&
object[Symbol.toStringTag] === 'File'
)
)
}

Expand Down
7 changes: 5 additions & 2 deletions lib/fetch/formdata.js
Expand Up @@ -2,9 +2,12 @@

const { isBlobLike, toUSVString, makeIterator } = require('./util')
const { kState } = require('./symbols')
const { File, FileLike, isFileLike } = require('./file')
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
const { webidl } = require('./webidl')
const { Blob } = require('buffer')
const { Blob, File: NativeFile } = require('buffer')

/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile

// https://xhr.spec.whatwg.org/#formdata
class FormData {
Expand Down
13 changes: 8 additions & 5 deletions test/wpt/status/FileAPI.status.json
Expand Up @@ -5,23 +5,26 @@
]
},
"idlharness.any.js": {
"fail": [
"note": "These flaky tests only fail in < node v19; add in a way to mark them as such eventually",
"flaky": [
"Blob interface: attribute size",
"Blob interface: attribute type",
"Blob interface: operation slice(optional long long, optional long long, optional DOMString)",
"Blob interface: operation stream()",
"Blob interface: operation text()",
"Blob interface: operation arrayBuffer()",
"URL interface: operation createObjectURL((Blob or MediaSource))",
"URL interface: operation revokeObjectURL(DOMString)"
],
"fail": [
"FileList interface: existence and properties of interface object",
"FileList interface object length",
"FileList interface object name",
"FileList interface: existence and properties of interface prototype object",
"FileList interface: existence and properties of interface prototype object's \"constructor\" property",
"FileList interface: existence and properties of interface prototype object's @@unscopables property",
"FileList interface: operation item(unsigned long)",
"FileList interface: attribute length",
"URL interface: operation createObjectURL((Blob or MediaSource))",
"URL interface: operation revokeObjectURL(DOMString)"
"FileList interface: attribute length"
]
},
"filereader_events.any.js": {
Expand All @@ -30,4 +33,4 @@
"events are dispatched in the correct order for a non-empty blob"
]
}
}
}

0 comments on commit 78060b6

Please sign in to comment.