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

Fix FormData and add tests #140

Merged
merged 9 commits into from Sep 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/rotten-poets-teach.md
@@ -0,0 +1,6 @@
---
'@whatwg-node/fetch': patch
'@whatwg-node/server': patch
---

Fix Request.formData method
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,7 +47,7 @@
"typescript": "4.8.3"
},
"lint-staged": {
"packages/**/src/**/*.{ts,tsx}": [
"packages/**/*.{ts,tsx}": [
"eslint --fix"
],
"**/*.{ts,tsx,graphql,yml,md,mdx}": [
Expand Down
16 changes: 0 additions & 16 deletions packages/fetch/dist/create-node-ponyfill.js
Expand Up @@ -165,22 +165,6 @@ module.exports = function createNodePonyfill(opts = {}) {
constructor(requestOrUrl, options) {
if (typeof requestOrUrl === "string") {
options = options || {};
if (options.body != null && options.body.read && options.body.on) {
const readable = options.body;
options.body = new ponyfills.ReadableStream({
pull(controller) {
const chunk = readable.read();
if (chunk != null) {
controller.enqueue(chunk);
} else {
controller.close();
}
},
close(e) {
readable.destroy(e);
}
})
}
super(requestOrUrl, options);
const contentType = this.headers.get("content-type");
if (contentType && contentType.startsWith("multipart/form-data")) {
Expand Down
4 changes: 2 additions & 2 deletions packages/fetch/dist/getFormDataMethod.js
Expand Up @@ -16,13 +16,13 @@ module.exports = function getFormDataMethod(File, limits) {
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
})
fileStream.on('data', (chunk) => {
chunks.push(chunk);
chunks.push(...chunk);
})
fileStream.on('close', () => {
if (fileStream.truncated) {
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
}
const file = new File(chunks, filename, { type: mimeType });
const file = new File([new Uint8Array(chunks)], filename, { type: mimeType });
formData.set(name, file);
resolve(file);
});
Expand Down
43 changes: 43 additions & 0 deletions packages/fetch/tests/getFormDataMethod.spec.ts
@@ -0,0 +1,43 @@
import { createFetch } from '@whatwg-node/fetch';

describe('getFormDataMethod', () => {
['fieldsFirst:true', 'fieldsFirst:false'].forEach(fieldsFirstFlag => {
const fetchAPI = createFetch({
formDataLimits: {
fieldsFirst: fieldsFirstFlag === 'fieldsFirst:true',
},
});
describe(fieldsFirstFlag, () => {
it('should parse fields correctly', async () => {
const formData = new fetchAPI.FormData();
formData.append('greetings', 'Hello world!');
formData.append('bye', 'Goodbye world!');
const request = new fetchAPI.Request('http://localhost:8080', {
method: 'POST',
body: formData,
});
const formdata = await request.formData();
expect(formdata.get('greetings')).toBe('Hello world!');
expect(formdata.get('bye')).toBe('Goodbye world!');
});
it('should parse and receive text files correctly', async () => {
const formData = new fetchAPI.FormData();
const greetingsFile = new fetchAPI.File(['Hello world!'], 'greetings.txt', { type: 'text/plain' });
const byeFile = new fetchAPI.File(['Goodbye world!'], 'bye.txt', { type: 'text/plain' });
formData.append('greetings', greetingsFile);
formData.append('bye', byeFile);
const request = new fetchAPI.Request('http://localhost:8080', {
method: 'POST',
body: formData,
});
const formdata = await request.formData();
const receivedGreetingsFile = formdata.get('greetings') as File;
const receivedGreetingsText = await receivedGreetingsFile.text();
expect(receivedGreetingsText).toBe('Hello world!');
const receivedByeFile = formdata.get('bye') as File;
const receivedByeText = await receivedByeFile.text();
expect(receivedByeText).toBe('Goodbye world!');
});
});
});
});
2 changes: 1 addition & 1 deletion packages/server/src/utils.ts
Expand Up @@ -173,8 +173,8 @@ export async function sendNodeResponse(
serverResponse.end(body, resolve);
} else if (isReadable(body)) {
serverResponse.once('close', () => {
body.destroy();
resolve();
body.destroy();
});
body.pipe(serverResponse);
} else if (isAsyncIterable(body)) {
Expand Down
64 changes: 64 additions & 0 deletions packages/server/test/formdata.spec.ts
@@ -0,0 +1,64 @@
import { createFetch } from '@whatwg-node/fetch';
import { createServerAdapter } from '@whatwg-node/server';
import { createServer, Server } from 'http';

describe('FormData', () => {
let server: Server;
let url: string;
afterEach(done => {
server.close(done);
});
['fieldsFirst:true', 'fieldsFirst:false'].forEach(fieldsFirstFlag => {
const fetchAPI = createFetch({
formDataLimits: {
fieldsFirst: fieldsFirstFlag === 'fieldsFirst:true',
},
});

describe(fieldsFirstFlag, () => {
it('should forward formdata correctly', async () => {
let receivedFieldContent: string | undefined;
let receivedFileName: string | undefined;
let receivedFileType: string | undefined;
let receivedFileContent: string | undefined;
const adapter = createServerAdapter(async request => {
try {
const body = await request.formData();
receivedFieldContent = body.get('foo') as string;
const file = body.get('baz') as File;
receivedFileName = file.name;
receivedFileType = file.type;
receivedFileContent = await file.text();
} catch (e: any) {
return new fetchAPI.Response(e.stack, {
status: 500,
});
}
return new fetchAPI.Response(null, {
status: 204,
});
}, fetchAPI.Request);
server = createServer(adapter);
await new Promise<void>(resolve => {
server.listen(0, () => {
url = `http://localhost:${(server.address() as any).port}`;
resolve();
});
});
const formData = new fetchAPI.FormData();
formData.append('foo', 'bar');
formData.append('baz', new fetchAPI.File(['baz'], 'baz.txt', { type: 'text/plain' }));
const response = await fetchAPI.fetch(url, {
method: 'POST',
body: formData,
});
expect(await response.text()).toBe('');
expect(response.status).toBe(204);
expect(receivedFieldContent).toBe('bar');
expect(receivedFileName).toBe('baz.txt');
expect(receivedFileType).toBe('text/plain');
expect(receivedFileContent).toBe('baz');
});
});
});
});