Skip to content

Commit

Permalink
lib: add FormData global when fetch is enabled
Browse files Browse the repository at this point in the history
PR-URL: nodejs#41956
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
targos authored and aduh95 committed Apr 14, 2022
1 parent 54a5cb6 commit 90dce6a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ module.exports = {
performance: 'readable',
structuredClone: 'readable',
fetch: 'readable',
FormData: 'readable',
Headers: 'readable',
Request: 'readable',
Response: 'readable',
Expand Down
11 changes: 11 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ added: REPLACEME
A browser-compatible implementation of the [`fetch()`][] function.

## Class `FormData`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
> CLI flag.
A browser-compatible implementation of {FormData}.

## `global`

<!-- YAML
Expand Down
4 changes: 4 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ rules:
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
- name: EventTarget
message: "Use `const { EventTarget } = require('internal/event_target');` instead of the global."
- name: FormData
message: "Use `const { FormData } = require('internal/deps/undici/undici');` instead of the global."
- name: Headers
message: "Use `const { Headers } = require('internal/deps/undici/undici');` instead of the global."
# Intl is not available in primordials because it can be
# disabled with --without-intl build flag.
- name: Intl
Expand Down
1 change: 1 addition & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function setupFetch() {

const undici = require('internal/deps/undici/undici');
defineOperation(globalThis, 'fetch', undici.fetch);
exposeInterface(globalThis, 'FormData', undici.FormData);
exposeInterface(globalThis, 'Headers', undici.Headers);
exposeInterface(globalThis, 'Request', undici.Request);
exposeInterface(globalThis, 'Response', undici.Response);
Expand Down
1 change: 1 addition & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ if (global.PerformanceMeasure) {
if (global.fetch) {
knownGlobals.push(
global.fetch,
global.FormData,
global.Request,
global.Response,
global.Headers,
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import events from 'events';
import http from 'http';

assert.strictEqual(typeof globalThis.fetch, 'function');
assert.strictEqual(typeof globalThis.FormData, 'function');
assert.strictEqual(typeof globalThis.Headers, 'function');
assert.strictEqual(typeof globalThis.Request, 'function');
assert.strictEqual(typeof globalThis.Response, 'function');
Expand Down
9 changes: 9 additions & 0 deletions test/wpt/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ runner.setScriptModifier((obj) => {
// created via `document.createElement`. So we need to ignore them and just
// test `URL`.
obj.code = obj.code.replace(/\["url", "a", "area"\]/, '[ "url" ]');
} else if (typeof FormData === 'undefined' &&
obj.filename.includes('urlsearchparams-constructor.any.js')) {
// TODO(XadillaX): Remove this `else if` after `FormData` is supported.

// Ignore test named `URLSearchParams constructor, FormData.` because we do
// not have `FormData`.
obj.code = obj.code.replace(
/('URLSearchParams constructor, object\.'\);[\w\W]+)test\(function\(\) {[\w\W]*?}, 'URLSearchParams constructor, FormData\.'\);/,
'$1');
}
});
runner.pretendGlobalThisAs('Window');
Expand Down
1 change: 1 addition & 0 deletions tools/doc/type-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ const customTypesMap = {
'TextDecoderStream':
'webstreams.md#class-textdecoderstream',

'FormData': 'https://developer.mozilla.org/en-US/docs/Web/API/FormData',
'Headers': 'https://developer.mozilla.org/en-US/docs/Web/API/Headers',
'Response': 'https://developer.mozilla.org/en-US/docs/Web/API/Response',
'Request': 'https://developer.mozilla.org/en-US/docs/Web/API/Request',
Expand Down

0 comments on commit 90dce6a

Please sign in to comment.