Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add formData method in superclass rather than monkey-patch #5629

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-maps-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add formData method in superclass rather than monkey-patch
15 changes: 1 addition & 14 deletions packages/kit/src/node/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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} */
Expand Down
15 changes: 14 additions & 1 deletion packages/kit/src/node/polyfills.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
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<string, any>} */
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,
Expand Down