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 2dfeefc commit c2d8f13
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
44 changes: 27 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,12 @@ function honoWrapper(compiler, options) {
* @param {Function} next
* @returns {Promise<void>}
*/
// eslint-disable-next-line consistent-return
const wrapper = async function webpackDevMiddleware(c, next) {
c.set("webpack", { devMiddleware: devMiddleware.context });

await next();

const { req, res } = c;

/**
Expand Down Expand Up @@ -504,7 +509,6 @@ function honoWrapper(compiler, options) {
*/
res.setStatusCode = (code) => {
status = code;
c.status(code);
};

/**
Expand All @@ -517,7 +521,6 @@ function honoWrapper(compiler, options) {
* @param {string | number | Readonly<string[]>} value
*/
res.setHeader = (name, value) => {
// TODO use `c.header`
c.res.headers.append(name, value);
return c.res;
};
Expand All @@ -539,15 +542,28 @@ function honoWrapper(compiler, options) {
*/
res.getOutgoing = () => c.env.outgoing;

res.setState = () => {
// Do nothing, because we set it before
};

/**
* @param {string} name
* @param {any} value
* @param {string | Buffer | ReadStream} [data]
*/
res.setState = (name, value) => {
c.set(name, value);
};
const responseHandler = (data) => {
const headers = { ...c.res.headers };
const contentType = c.res.headers.get("Content-Type");

let body;
if (contentType) {
headers["Content-Type"] = contentType;
}

// eslint-disable-next-line no-param-reassign
c.res = c.body(
typeof data !== "undefined" ? data : null,
status,
headers,
);
};

try {
await new Promise(
Expand All @@ -560,23 +576,23 @@ function honoWrapper(compiler, options) {
* @param {import("fs").ReadStream} stream readable stream
*/
res.stream = (stream) => {
body = c.body(stream);
responseHandler(stream);
resolve();
};

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

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

Expand All @@ -595,12 +611,6 @@ function honoWrapper(compiler, options) {

return c.json({ message: /** @type {Error} */ (err).message });
}

if (typeof body === "undefined") {
await next();
}

return body;
};

wrapper.devMiddleware = devMiddleware;
Expand Down
10 changes: 9 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ function wrapper(context) {
}
}

let isPartialContent = false;

if (rangeHeader) {
let parsedRanges =
/** @type {import("range-parser").Ranges | import("range-parser").Result | []} */
Expand Down Expand Up @@ -689,6 +691,8 @@ function wrapper(context) {
),
);

isPartialContent = true;

[offset, len] = getOffsetAndLenFromRange(parsedRanges[0]);
}
}
Expand Down Expand Up @@ -725,14 +729,18 @@ function wrapper(context) {
setResponseHeader(res, "Content-Length", byteLength);

if (method === "HEAD") {
if (getStatusCode(res) !== 206) {
if (!isPartialContent) {
setStatusCode(res, 200);
}

finish(res);
return;
}

if (!isPartialContent) {
setStatusCode(res, 200);
}

const isPipeSupports =
typeof (
/** @type {import("fs").ReadStream} */ (bufferOrStream).pipe
Expand Down

0 comments on commit c2d8f13

Please sign in to comment.