Skip to content
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: 4 additions & 1 deletion src/stream/writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ export class Writable extends EventEmitter implements NodeStream.Writable {
const data = arg1 === callback ? undefined : arg1;
if (data) {
const encoding = arg2 === callback ? undefined : arg2;
this.write(data, encoding, callback);
this.write(data, encoding);
}
this.writableEnded = true;
this.writableFinished = true;
this.emit("close");
this.emit("finish");
if (callback) {
callback();
}
return this;
}

Expand Down
41 changes: 41 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,44 @@ describe("fetchNodeRequestHandler", () => {
});
});
});

describe("ServerResponse#end callback", () => {
it("invokes callback with no chunk", async () => {
const calls: string[] = [];
const res = await fetchNodeRequestHandler((_req, res) => {
res.on("finish", () => calls.push("finish"));
res.end(() => calls.push("callback"));
}, "/test");
expect(await res.text()).toBe("");
expect(calls).toEqual(["finish", "callback"]);
});

it("invokes callback once with chunk", async () => {
const calls: string[] = [];
const res = await fetchNodeRequestHandler((_req, res) => {
res.on("finish", () => calls.push("finish"));
res.end("hello", () => calls.push("callback"));
}, "/test");
expect(await res.text()).toBe("hello");
expect(calls).toEqual(["finish", "callback"]);
});

it("invokes callback once with chunk and encoding", async () => {
let calls = 0;
const res = await fetchNodeRequestHandler((_req, res) => {
res.end("hello", "utf8", () => calls++);
}, "/test");
expect(await res.text()).toBe("hello");
expect(calls).toBe(1);
});

it("invokes callback on an already ended response", async () => {
let calls = 0;
const res = await fetchNodeRequestHandler((_req, res) => {
res.end("hello");
res.end(() => calls++);
}, "/test");
expect(await res.text()).toBe("hello");
expect(calls).toBe(1);
});
});
Loading