Skip to content

Commit

Permalink
remove third party everything support in fetch (#3502)
Browse files Browse the repository at this point in the history
* remove third party everything support in fetch

* fixup

* fixup

* test

* test

* test

* fixup

* fixup

* fixup

* fixup! remove is*Like, *Like
  • Loading branch information
KhafraDev authored Aug 27, 2024
1 parent 409222c commit de0424f
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 361 deletions.
10 changes: 4 additions & 6 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const util = require('../../core/util')
const {
ReadableStreamFrom,
isBlobLike,
isReadableStreamLike,
readableStreamClose,
createDeferredPromise,
fullyReadBody,
Expand Down Expand Up @@ -44,7 +42,7 @@ function extractBody (object, keepalive = false) {
// 2. If object is a ReadableStream object, then set stream to object.
if (object instanceof ReadableStream) {
stream = object
} else if (isBlobLike(object)) {
} else if (object instanceof Blob) {
// 3. Otherwise, if object is a Blob object, set stream to the
// result of running object’s get stream.
stream = object.stream()
Expand All @@ -67,7 +65,7 @@ function extractBody (object, keepalive = false) {
}

// 5. Assert: stream is a ReadableStream object.
assert(isReadableStreamLike(stream))
assert(stream instanceof ReadableStream)

// 6. Let action be null.
let action = null
Expand Down Expand Up @@ -112,7 +110,7 @@ function extractBody (object, keepalive = false) {

// Set source to a copy of the bytes held by object.
source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength))
} else if (util.isFormDataLike(object)) {
} else if (object instanceof FormData) {
const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`
const prefix = `--${boundary}\r\nContent-Disposition: form-data`

Expand Down Expand Up @@ -178,7 +176,7 @@ function extractBody (object, keepalive = false) {
// followed by the multipart/form-data boundary string generated
// by the multipart/form-data encoding algorithm.
type = `multipart/form-data; boundary=${boundary}`
} else if (isBlobLike(object)) {
} else if (object instanceof Blob) {
// Blob

// Set source to object.
Expand Down
126 changes: 0 additions & 126 deletions lib/web/fetch/file.js

This file was deleted.

3 changes: 1 addition & 2 deletions lib/web/fetch/formdata-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { isUSVString, bufferToLowerCasedHeaderName } = require('../../core/util')
const { utf8DecodeBytes } = require('./util')
const { HTTP_TOKEN_CODEPOINTS, isomorphicDecode } = require('./data-url')
const { isFileLike } = require('./file')
const { makeEntry } = require('./formdata')
const assert = require('node:assert')
const { File: NodeFile } = require('node:buffer')
Expand Down Expand Up @@ -195,7 +194,7 @@ function multipartFormDataParser (input, mimeType) {

// 5.12. Assert: name is a scalar value string and value is either a scalar value string or a File object.
assert(isUSVString(name))
assert((typeof value === 'string' && isUSVString(value)) || isFileLike(value))
assert((typeof value === 'string' && isUSVString(value)) || value instanceof File)

// 5.13. Create an entry with name and value, and append it to entry list.
entryList.push(makeEntry(name, value, filename))
Expand Down
25 changes: 10 additions & 15 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

const { isBlobLike, iteratorMixin } = require('./util')
const { iteratorMixin } = require('./util')
const { kState } = require('./symbols')
const { kEnumerableProperty } = require('../../core/util')
const { FileLike, isFileLike } = require('./file')
const { webidl } = require('./webidl')
const { File: NativeFile } = require('node:buffer')
const nodeUtil = require('node:util')
Expand Down Expand Up @@ -31,7 +30,7 @@ class FormData {
const prefix = 'FormData.append'
webidl.argumentLengthCheck(arguments, 2, prefix)

if (arguments.length === 3 && !isBlobLike(value)) {
if (arguments.length === 3 && !(value instanceof Blob)) {
throw new TypeError(
"Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'"
)
Expand All @@ -40,8 +39,8 @@ class FormData {
// 1. Let value be value if given; otherwise blobValue.

name = webidl.converters.USVString(name)
value = isBlobLike(value)
? webidl.converters.Blob(value, prefix, 'value', { strict: false })
value = value instanceof Blob
? webidl.converters.Blob(value, prefix, 'value')
: webidl.converters.USVString(value)
filename = arguments.length === 3
? webidl.converters.USVString(filename)
Expand Down Expand Up @@ -124,7 +123,7 @@ class FormData {
const prefix = 'FormData.set'
webidl.argumentLengthCheck(arguments, 2, prefix)

if (arguments.length === 3 && !isBlobLike(value)) {
if (arguments.length === 3 && !(value instanceof Blob)) {
throw new TypeError(
"Failed to execute 'set' on 'FormData': parameter 2 is not of type 'Blob'"
)
Expand All @@ -136,8 +135,8 @@ class FormData {
// 1. Let value be value if given; otherwise blobValue.

name = webidl.converters.USVString(name)
value = isBlobLike(value)
? webidl.converters.Blob(value, prefix, 'name', { strict: false })
value = value instanceof Blob
? webidl.converters.Blob(value, prefix, 'name')
: webidl.converters.USVString(value)
filename = arguments.length === 3
? webidl.converters.USVString(filename)
Expand Down Expand Up @@ -222,10 +221,8 @@ function makeEntry (name, value, filename) {

// 1. If value is not a File object, then set value to a new File object,
// representing the same bytes, whose name attribute value is "blob"
if (!isFileLike(value)) {
value = value instanceof Blob
? new File([value], 'blob', { type: value.type })
: new FileLike(value, 'blob', { type: value.type })
if (!(value instanceof File)) {
value = new File([value], 'blob', { type: value.type })
}

// 2. If filename is given, then set value to a new File object,
Expand All @@ -237,9 +234,7 @@ function makeEntry (name, value, filename) {
lastModified: value.lastModified
}

value = value instanceof NativeFile
? new File([value], filename, options)
: new FileLike(value, filename, options)
value = new File([value], filename, options)
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
determineRequestsReferrer,
coarsenedSharedCurrentTime,
createDeferredPromise,
isBlobLike,
sameOrigin,
isCancelled,
isAborted,
Expand Down Expand Up @@ -810,7 +809,7 @@ function schemeFetch (fetchParams) {

// 2. If request’s method is not `GET`, blobURLEntry is null, or blobURLEntry’s
// object is not a Blob object, then return a network error.
if (request.method !== 'GET' || !isBlobLike(blob)) {
if (request.method !== 'GET' || !(blob instanceof Blob)) {
return Promise.resolve(makeNetworkError('invalid method'))
}

Expand Down
13 changes: 1 addition & 12 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,6 @@ class Request {

// 29. If signal is not null, then make this’s signal follow signal.
if (signal != null) {
if (
!signal ||
typeof signal.aborted !== 'boolean' ||
typeof signal.addEventListener !== 'function'
) {
throw new TypeError(
"Failed to construct 'Request': member signal is not of type AbortSignal."
)
}

if (signal.aborted) {
ac.abort(signal.reason)
} else {
Expand Down Expand Up @@ -1011,8 +1001,7 @@ webidl.converters.RequestInit = webidl.dictionaryConverter([
(signal) => webidl.converters.AbortSignal(
signal,
'RequestInit',
'signal',
{ strict: false }
'signal'
)
)
},
Expand Down
9 changes: 4 additions & 5 deletions lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
isValidReasonPhrase,
isCancelled,
isAborted,
isBlobLike,
serializeJavascriptValueToJSONString,
isErrorLike,
isomorphicEncode,
Expand Down Expand Up @@ -541,16 +540,16 @@ webidl.converters.XMLHttpRequestBodyInit = function (V, prefix, name) {
return webidl.converters.USVString(V, prefix, name)
}

if (isBlobLike(V)) {
return webidl.converters.Blob(V, prefix, name, { strict: false })
if (V instanceof Blob) {
return webidl.converters.Blob(V, prefix, name)
}

if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) {
return webidl.converters.BufferSource(V, prefix, name)
}

if (util.isFormDataLike(V)) {
return webidl.converters.FormData(V, prefix, name, { strict: false })
if (V instanceof FormData) {
return webidl.converters.FormData(V, prefix, name)
}

if (V instanceof URLSearchParams) {
Expand Down
11 changes: 1 addition & 10 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { redirectStatusSet, referrerPolicySet: referrerPolicyTokens, badPortsSet
const { getGlobalOrigin } = require('./global')
const { collectASequenceOfCodePoints, collectAnHTTPQuotedString, removeChars, parseMIMEType } = require('./data-url')
const { performance } = require('node:perf_hooks')
const { isBlobLike, ReadableStreamFrom, isValidHTTPToken, normalizedMethodRecordsBase } = require('../../core/util')
const { ReadableStreamFrom, isValidHTTPToken, normalizedMethodRecordsBase } = require('../../core/util')
const assert = require('node:assert')
const { isUint8Array } = require('node:util/types')
const { webidl } = require('./webidl')
Expand Down Expand Up @@ -1061,13 +1061,6 @@ async function fullyReadBody (body, processBody, processBodyError) {
}
}

function isReadableStreamLike (stream) {
return stream instanceof ReadableStream || (
stream[Symbol.toStringTag] === 'ReadableStream' &&
typeof stream.tee === 'function'
)
}

/**
* @param {ReadableStreamController<Uint8Array>} controller
*/
Expand Down Expand Up @@ -1589,7 +1582,6 @@ module.exports = {
requestCurrentURL,
responseURL,
responseLocationURL,
isBlobLike,
isURLPotentiallyTrustworthy,
isValidReasonPhrase,
sameOrigin,
Expand All @@ -1602,7 +1594,6 @@ module.exports = {
isErrorLike,
fullyReadBody,
bytesMatch,
isReadableStreamLike,
readableStreamClose,
isomorphicEncode,
urlIsLocal,
Expand Down
Loading

0 comments on commit de0424f

Please sign in to comment.