diff --git a/.changeset/tricky-maps-know.md b/.changeset/tricky-maps-know.md new file mode 100644 index 000000000000..f27ff744ae65 --- /dev/null +++ b/.changeset/tricky-maps-know.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add formData method in superclass rather than monkey-patch diff --git a/packages/kit/src/node/index.js b/packages/kit/src/node/index.js index 89e691267706..aced420052e4 100644 --- a/packages/kit/src/node/index.js +++ b/packages/kit/src/node/index.js @@ -1,6 +1,4 @@ import * as set_cookie_parser from 'set-cookie-parser'; -import { Request as NodeFetchRequest } from 'node-fetch'; -import { Readable } from 'stream'; /** @param {import('http').IncomingMessage} req */ function get_raw_body(req) { @@ -80,22 +78,11 @@ export async function getRequest(base, req) { delete headers[':scheme']; } - const request = new Request(base + req.url, { + return new Request(base + req.url, { method: req.method, headers, body: get_raw_body(req) }); - - request.formData = async () => { - return new NodeFetchRequest(request.url, { - method: request.method, - headers: request.headers, - // @ts-expect-error TypeScript doesn't understand that ReadableStream implements Symbol.asyncIterator - body: request.body && Readable.from(request.body) - }).formData(); - }; - - return request; } /** @type {import('@sveltejs/kit/node').setResponse} */ diff --git a/packages/kit/src/node/polyfills.js b/packages/kit/src/node/polyfills.js index ae1c8ff6c502..3e05dc98ba19 100644 --- a/packages/kit/src/node/polyfills.js +++ b/packages/kit/src/node/polyfills.js @@ -1,5 +1,7 @@ import { fetch, Response, Request, Headers } from 'undici'; import { ReadableStream, TransformStream, WritableStream } from 'stream/web'; +import { Readable } from 'stream'; +import { Request as NodeFetchRequest } from 'node-fetch'; import { webcrypto as crypto } from 'crypto'; /** @type {Record} */ @@ -7,7 +9,18 @@ const globals = { crypto, fetch, Response, - Request, + // TODO remove the superclass as soon as Undici supports formData + // https://github.com/nodejs/undici/issues/974 + Request: class extends Request { + // @ts-expect-error + formData() { + return new NodeFetchRequest(this.url, { + method: this.method, + headers: this.headers, + body: this.body && Readable.from(this.body) + }).formData(); + } + }, Headers, ReadableStream, TransformStream,