Skip to content

Commit

Permalink
remove DOMException, structuredClone, WeakRef, FinalizationRegistry w…
Browse files Browse the repository at this point in the history
…orkarounds
  • Loading branch information
KhafraDev committed Dec 1, 2023
1 parent bc6672a commit b10c0db
Show file tree
Hide file tree
Showing 14 changed files with 6 additions and 64 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ Implements [fetch](https://fetch.spec.whatwg.org/#fetch-method).
* https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
* https://fetch.spec.whatwg.org/#fetch-method

Only supported on Node 16.8+.

Basic usage example:

```js
Expand Down
7 changes: 1 addition & 6 deletions lib/compat/dispatcher-weakref.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

/* istanbul ignore file: only for Node 12 */

const { kConnected, kSize } = require('../core/symbols')

class CompatWeakRef {
Expand Down Expand Up @@ -41,8 +39,5 @@ module.exports = function () {
FinalizationRegistry: CompatFinalizer
}
}
return {
WeakRef: global.WeakRef || CompatWeakRef,
FinalizationRegistry: global.FinalizationRegistry || CompatFinalizer
}
return { WeakRef, FinalizationRegistry }
}
1 change: 0 additions & 1 deletion lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
const { FormData } = require('./formdata')
const { kState } = require('./symbols')
const { webidl } = require('./webidl')
const { DOMException, structuredClone } = require('./constants')
const { Blob, File: NativeFile } = require('buffer')
const { kBodyUsed } = require('../core/symbols')
const assert = require('assert')
Expand Down
36 changes: 0 additions & 36 deletions lib/fetch/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const { MessageChannel, receiveMessageOnPort } = require('worker_threads')

const corsSafeListedMethods = ['GET', 'HEAD', 'POST']
const corsSafeListedMethodsSet = new Set(corsSafeListedMethods)

Expand Down Expand Up @@ -92,41 +90,7 @@ const subresource = [
]
const subresourceSet = new Set(subresource)

/** @type {globalThis['DOMException']} */
const DOMException = globalThis.DOMException ?? (() => {
// DOMException was only made a global in Node v17.0.0,
// but fetch supports >= v16.8.
try {
atob('~')
} catch (err) {
return Object.getPrototypeOf(err).constructor
}
})()

let channel

/** @type {globalThis['structuredClone']} */
const structuredClone =
globalThis.structuredClone ??
// https://github.com/nodejs/node/blob/b27ae24dcc4251bad726d9d84baf678d1f707fed/lib/internal/structured_clone.js
// structuredClone was added in v17.0.0, but fetch supports v16.8
function structuredClone (value, options = undefined) {
if (arguments.length === 0) {
throw new TypeError('missing argument')
}

if (!channel) {
channel = new MessageChannel()
}
channel.port1.unref()
channel.port2.unref()
channel.port1.postMessage(value, options?.transfer)
return receiveMessageOnPort(channel.port2).message
}

module.exports = {
DOMException,
structuredClone,
subresource,
forbiddenMethods,
requestBodyHeader,
Expand Down
3 changes: 1 addition & 2 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ const {
nullBodyStatus,
safeMethodsSet,
requestBodyHeader,
subresourceSet,
DOMException
subresourceSet
} = require('./constants')
const { kHeadersList } = require('../core/symbols')
const EE = require('events')
Expand Down
3 changes: 1 addition & 2 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const {
} = require('./util')
const {
redirectStatusSet,
nullBodyStatus,
DOMException
nullBodyStatus
} = require('./constants')
const { kState, kHeaders, kGuard, kRealm } = require('./symbols')
const { webidl } = require('./webidl')
Expand Down
6 changes: 0 additions & 6 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1179,11 +1179,6 @@ function buildContentRange (rangeStart, rangeEnd, fullLength) {
return contentRange
}

/**
* Fetch supports node >= 16.8.0, but Object.hasOwn was added in v16.9.0.
*/
const hasOwn = Object.hasOwn || ((dict, key) => Object.prototype.hasOwnProperty.call(dict, key))

module.exports = {
isAborted,
isCancelled,
Expand Down Expand Up @@ -1216,7 +1211,6 @@ module.exports = {
makeIterator,
isValidHeaderName,
isValidHeaderValue,
hasOwn,
isErrorLike,
fullyReadBody,
bytesMatch,
Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/webidl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { types } = require('util')
const { hasOwn, toUSVString } = require('./util')
const { toUSVString } = require('./util')

/** @type {import('../../types/webidl').Webidl} */
const webidl = {}
Expand Down Expand Up @@ -346,7 +346,7 @@ webidl.dictionaryConverter = function (converters) {
const { key, defaultValue, required, converter } = options

if (required === true) {
if (!hasOwn(dictionary, key)) {
if (!Object.hasOwn(dictionary, key)) {
throw webidl.errors.exception({
header: 'Dictionary',
message: `Missing required key "${key}".`
Expand All @@ -355,7 +355,7 @@ webidl.dictionaryConverter = function (converters) {
}

let value = dictionary[key]
const hasDefault = hasOwn(options, 'defaultValue')
const hasDefault = Object.hasOwn(options, 'defaultValue')

// Only use defaultValue if value is undefined and
// a defaultValue options was provided.
Expand Down
1 change: 0 additions & 1 deletion lib/fileapi/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
} = require('./symbols')
const { ProgressEvent } = require('./progressevent')
const { getEncoding } = require('./encoding')
const { DOMException } = require('../fetch/constants')
const { serializeAMimeType, parseMIMEType } = require('../fetch/dataURL')
const { types } = require('util')
const { StringDecoder } = require('string_decoder')
Expand Down
1 change: 0 additions & 1 deletion lib/websocket/websocket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { webidl } = require('../fetch/webidl')
const { DOMException } = require('../fetch/constants')
const { URLSerializer } = require('../fetch/dataURL')
const { getGlobalOrigin } = require('../fetch/global')
const { staticPropertyDescriptors, states, opcodes, emptyBuffer } = require('./constants')
Expand Down
1 change: 0 additions & 1 deletion test/fetch/abort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { test } = require('tap')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
const { DOMException } = require('../../lib/fetch/constants')

const { AbortController: NPMAbortController } = require('abort-controller')

Expand Down
1 change: 0 additions & 1 deletion test/fetch/abort2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { test } = require('tap')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
const { DOMException } = require('../../lib/fetch/constants')

/* global AbortController */

Expand Down
1 change: 0 additions & 1 deletion test/fetch/issue-2171.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { fetch } = require('../..')
const { DOMException } = require('../../lib/fetch/constants')
const { once } = require('events')
const { createServer } = require('http')
const { test } = require('tap')
Expand Down
1 change: 0 additions & 1 deletion test/jest/instanceof-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jest.useRealTimers()

it('isErrorLike sanity check', () => {
const { isErrorLike } = require('../../lib/fetch/util')
const { DOMException } = require('../../lib/fetch/constants')
const error = new DOMException('')

// https://github.com/facebook/jest/issues/2549
Expand Down

0 comments on commit b10c0db

Please sign in to comment.