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: IncomingForm end event emitted twice #852

Merged
merged 3 commits into from
Apr 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
1 change: 0 additions & 1 deletion src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ class IncomingForm extends EventEmitter {
if (this._parser) {
this._parser.end();
}
this._maybeEnd();
GrosSacASac marked this conversation as resolved.
Show resolved Hide resolved
});

return this;
Expand Down
57 changes: 57 additions & 0 deletions test/standalone/end-event-emitted-twice.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {strictEqual} from 'node:assert';
import { createServer } from 'node:http';
import { connect } from 'node:net';
import formidable from '../../src/index.js';

const PORT = 13539;

test('end event emitted twice', (done) => {
const server = createServer((req) => {
const form = formidable();

let i = 0;
form.on('end', () => {
i += 1;
strictEqual(i, 1, 'end should be emitted once');
});
form.parse(req, () => {

server.close();
strictEqual(i, 1, 'end should be emitted once');
done();
});
});

server.listen(PORT, 'localhost', () => {
const choosenPort = server.address().port;

const client = connect(choosenPort);

client.write(
`POST /api/upload HTTP/1.1
Host: localhost:${choosenPort}
User-Agent: N
Content-Type: multipart/form-data; boundary=---------------------------13068458571765726332503797717
-----------------------------13068458571765726332503797717
Content-Disposition: form-data; name="title"
a
-----------------------------13068458571765726332503797717
Content-Disposition: form-data; name="multipleFiles"; filename="x.txt"
Content-Type: application/x-javascript
a
b
c
d
-----------------------------13068458571765726332503797717--
`,
);
client.end();
});
});
3 changes: 1 addition & 2 deletions test/standalone/keep-alive-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ test('keep alive error', (done) => {
clientTwo.end();

setTimeout(() => {
// ? yup, quite true, it makes sense to be 2
strictEqual(ok, 2, `should "ok" count === 2, has: ${ok}`);
strictEqual(ok, 1, `should "ok" count === 1, has: ${ok}`);

server.close();
done();
Expand Down