Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch): add in native File class support #1779

Merged
merged 2 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/fetch/body.js
Expand Up @@ -7,18 +7,21 @@ 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')
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
const { StringDecoder } = require('string_decoder')
const { parseMIMEType, serializeAMimeType } = require('./dataURL')

/** @type {globalThis['ReadableStream']} */
let 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"
]
}
}
}