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: #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 bengl committed Feb 21, 2022
1 parent ce23738 commit 4fbe064
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ module.exports = {
Crypto: 'readable',
CryptoKey: 'readable',
fetch: 'readable',
FormData: 'readable',
globalThis: 'readable',
Response: 'readable',
SubtleCrypto: '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 @@ -363,6 +363,17 @@ added: v17.5.0
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
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ 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
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ function setupFetch() {
configurable: true,
value: undici.fetch
});
ObjectDefineProperty(globalThis, 'FormData', {
writable: true,
enumerable: false,
configurable: true,
value: undici.FormData
});
ObjectDefineProperty(globalThis, 'Headers', {
writable: true,
enumerable: false,
Expand Down
1 change: 1 addition & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ if (global.structuredClone) {
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
2 changes: 1 addition & 1 deletion test/wpt/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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' && // eslint-disable-line
} else if (typeof FormData === 'undefined' &&
obj.filename.includes('urlsearchparams-constructor.any.js')) {
// TODO(XadillaX): Remove this `else if` after `FormData` is supported.

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 @@ -271,6 +271,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 4fbe064

Please sign in to comment.