Skip to content

Commit

Permalink
fix(fetch): re-add support for node v16.8.0+ (nodejs#1534)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and metcoder95 committed Dec 26, 2022
1 parent e4d5249 commit 167ae9c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/nodejs.yml
Expand Up @@ -17,6 +17,9 @@ jobs:
post-test-steps: |
- name: Coverage Report
uses: codecov/codecov-action@v2
include: |
- runs-on: ubuntu-latest
node-version: 16.8
automerge:
if: >
github.event_name == 'pull_request' &&
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -176,7 +176,7 @@ 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.5+.
Only supported on Node 16.8+.

This is [experimental](https://nodejs.org/api/documentation.html#documentation_stability_index) and is not yet fully compliant with the Fetch Standard.
We plan to ship breaking changes to this feature until it is out of experimental.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -80,7 +80,7 @@ function makeDispatcher (fn) {
module.exports.setGlobalDispatcher = setGlobalDispatcher
module.exports.getGlobalDispatcher = getGlobalDispatcher

if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 5)) {
if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
let fetchImpl = null
module.exports.fetch = async function fetch (resource) {
if (!fetchImpl) {
Expand Down
4 changes: 2 additions & 2 deletions lib/core/request.js
Expand Up @@ -140,8 +140,8 @@ class Request {
}

if (util.isFormDataLike(this.body)) {
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 5)) {
throw new InvalidArgumentError('Form-Data bodies are only supported in node v16.5 and newer.')
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 8)) {
throw new InvalidArgumentError('Form-Data bodies are only supported in node v16.8 and newer.')
}

if (!extractBody) {
Expand Down
7 changes: 1 addition & 6 deletions lib/fetch/body.js
Expand Up @@ -15,12 +15,7 @@ const { isUint8Array, isArrayBuffer } = require('util/types')
let ReadableStream

async function * blobGen (blob) {
if (blob.stream) {
yield * blob.stream()
} else {
// istanbul ignore next: node < 16.7
yield await blob.arrayBuffer()
}
yield * blob.stream()
}

// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
Expand Down
2 changes: 1 addition & 1 deletion lib/fetch/constants.js
Expand Up @@ -63,7 +63,7 @@ const subresource = [
/** @type {globalThis['DOMException']} */
const DOMException = globalThis.DOMException ?? (() => {
// DOMException was only made a global in Node v17.0.0,
// but fetch supports >= v16.5.
// but fetch supports >= v16.8.
try {
atob('~')
} catch (err) {
Expand Down
8 changes: 7 additions & 1 deletion lib/fetch/util.js
Expand Up @@ -431,6 +431,11 @@ function makeIterator (iterator, name) {
return Object.setPrototypeOf({}, i)
}

/**
* 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 @@ -463,5 +468,6 @@ module.exports = {
serializeJavascriptValueToJSONString,
makeIterator,
isValidHeaderName,
isValidHeaderValue
isValidHeaderValue,
hasOwn
}
7 changes: 4 additions & 3 deletions lib/fetch/webidl.js
@@ -1,6 +1,7 @@
'use strict'

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

const webidl = {}
webidl.converters = {}
Expand Down Expand Up @@ -315,7 +316,7 @@ webidl.dictionaryConverter = function (converters) {
const { key, defaultValue, required, converter } = options

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

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

// Only use defaultValue if value is undefined and
// a defaultValue options was provided.
Expand Down

0 comments on commit 167ae9c

Please sign in to comment.