Skip to content

Commit

Permalink
fix: logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jul 17, 2024
1 parent d9f1c54 commit c568f03
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
26 changes: 16 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,24 @@ function koaWrapper(compiler, options) {
resolve();
};
/**
* @param {string | Buffer} content content
* @param {string | Buffer} data data
*/
res.send = (content) => {
res.send = (data) => {
// eslint-disable-next-line no-param-reassign
ctx.body = content;
ctx.body = data;
resolve();
};

res.finish = () => {
/**
* @param {string | Buffer} [data] data
*/
res.finish = (data) => {
if (status === 404) {
// eslint-disable-next-line no-param-reassign
ctx.status = 200;
}

res.end();
res.end(data);
resolve();
};

Expand Down Expand Up @@ -565,15 +568,18 @@ function honoWrapper(compiler, options) {
};

/**
* @param {string | Buffer} bufferOrString readable stream
* @param {string | Buffer} data data
*/
res.send = (bufferOrString) => {
body = c.body(bufferOrString);
res.send = (data) => {
body = c.body(data);
resolve();
};

res.finish = () => {
body = c.body(null);
/**
* @param {string | Buffer} [data] data
*/
res.finish = (data) => {
body = data ? c.body(data) : c.body(null);
resolve();
};

Expand Down
3 changes: 2 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function wrapper(context) {

setResponseHeader(res, "Content-Length", byteLength);

send(res, document);
finish(res, document);
}

function isConditionalGET() {
Expand Down Expand Up @@ -632,6 +632,7 @@ function wrapper(context) {
removeResponseHeader(res, "Content-Length");
removeResponseHeader(res, "Content-Range");
removeResponseHeader(res, "Content-Type");

finish(res);

return;
Expand Down
9 changes: 5 additions & 4 deletions src/utils/compatibleAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property {(name: string, value: number | string | Readonly<string[]>) => ExpectedServerResponse} [setHeader]
* @property {(name: string) => void} [removeHeader]
* @property {(data: string | Buffer) => void} [send]
* @property {() => void} [finish]
* @property {(data?: string | Buffer) => void} [finish]
* @property {() => string[]} [getResponseHeaders]
* @property {(data: any) => void} [stream]
* @property {() => any} [getOutgoing]
Expand Down Expand Up @@ -180,16 +180,17 @@ function send(res, bufferOrString) {
/**
* @template {ServerResponse & ExpectedServerResponse} Response
* @param {Response} res
* @param {string | Buffer} [data]
*/
function finish(res) {
function finish(res, data) {
// Pseudo API and Express API and Koa API
if (typeof res.finish === "function") {
res.finish();
res.finish(data);
return;
}

// Pseudo API and Express API and Koa API
res.end();
res.end(data);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2814,7 +2814,7 @@ describe.each([
});
});

describe.skip("should handle custom fs errors and response 500 code", () => {
describe("should handle custom fs errors and response 500 code", () => {
let compiler;

const outputPath = path.resolve(
Expand Down Expand Up @@ -2886,7 +2886,7 @@ describe.each([
});
});

describe.skip("should handle known fs errors and response 404 code", () => {
describe("should handle known fs errors and response 404 code", () => {
let compiler;

const outputPath = path.resolve(
Expand Down
7 changes: 4 additions & 3 deletions types/utils/compatibleAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type ExpectedServerResponse = {
| undefined;
removeHeader?: ((name: string) => void) | undefined;
send?: ((data: string | Buffer) => void) | undefined;
finish?: (() => void) | undefined;
finish?: ((data?: string | Buffer) => void) | undefined;
getResponseHeaders?: (() => string[]) | undefined;
stream?: ((data: any) => void) | undefined;
getOutgoing?: (() => any) | undefined;
Expand Down Expand Up @@ -57,7 +57,7 @@ export function getStatusCode<
* @property {(name: string, value: number | string | Readonly<string[]>) => ExpectedServerResponse} [setHeader]
* @property {(name: string) => void} [removeHeader]
* @property {(data: string | Buffer) => void} [send]
* @property {() => void} [finish]
* @property {(data?: string | Buffer) => void} [finish]
* @property {() => string[]} [getResponseHeaders]
* @property {(data: any) => void} [stream]
* @property {() => any} [getOutgoing]
Expand Down Expand Up @@ -148,10 +148,11 @@ export function send<Response extends ServerResponse & ExpectedServerResponse>(
/**
* @template {ServerResponse & ExpectedServerResponse} Response
* @param {Response} res
* @param {string | Buffer} [data]
*/
export function finish<
Response extends ServerResponse & ExpectedServerResponse,
>(res: Response): void;
>(res: Response, data?: string | Buffer | undefined): void;
/**
* @param {string} filename
* @param {import("../index").OutputFileSystem} outputFileSystem
Expand Down

0 comments on commit c568f03

Please sign in to comment.