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 a8abd99 commit ccde8d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
54 changes: 23 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ function wdm(compiler, options = {}) {
if (
Array.isArray(/** @type {MultiCompiler} */ (context.compiler).compilers)
) {
const compiler = /** @type {MultiCompiler} */ (context.compiler);
const watchOptions = compiler.compilers.map(
const c = /** @type {MultiCompiler} */ (context.compiler);
const watchOptions = c.compilers.map(
(childCompiler) => childCompiler.options.watchOptions || {},
);

context.watching = compiler.watch(watchOptions, errorHandler);
} else {
const compiler = /** @type {Compiler} */ (context.compiler);
const watchOptions = compiler.options.watchOptions || {};
const c = /** @type {Compiler} */ (context.compiler);
const watchOptions = c.options.watchOptions || {};

context.watching = compiler.watch(watchOptions, errorHandler);
}
Expand Down Expand Up @@ -333,12 +333,14 @@ function hapiWrapper() {
// @ts-ignore
server.decorate("server", "webpackDevMiddleware", devMiddleware);
// @ts-ignore
server.ext("onRequest", (request, h) => new Promise((resolve, reject) => {
server.ext("onRequest", (request, h) =>
new Promise((resolve, reject) => {
let isFinished = false;

/**
* @param {string | Buffer} [data]
*/
// eslint-disable-next-line no-param-reassign
request.raw.res.send = (data) => {
isFinished = true;
request.raw.res.end(data);
Expand All @@ -347,6 +349,7 @@ function hapiWrapper() {
/**
* @param {string | Buffer} [data]
*/
// eslint-disable-next-line no-param-reassign
request.raw.res.finish = (data) => {
isFinished = true;
request.raw.res.end(data);
Expand All @@ -366,7 +369,8 @@ function hapiWrapper() {
.then(() => h.continue)
.catch((error) => {
throw error;
}));
}),
);
},
};
}
Expand Down Expand Up @@ -492,12 +496,10 @@ function honoWrapper(compiler, options) {
*/
// 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;

c.set("webpack", { devMiddleware: devMiddleware.context });

/**
* @returns {string | undefined}
*/
Expand Down Expand Up @@ -565,24 +567,7 @@ function honoWrapper(compiler, options) {

res.getReadyReadableStreamState = () => "readable";

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

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

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

try {
await new Promise(
Expand All @@ -595,21 +580,22 @@ function honoWrapper(compiler, options) {
* @param {import("fs").ReadStream} stream readable stream
*/
res.stream = (stream) => {
responseHandler(stream);
body = stream;
// responseHandler(stream);
};

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

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

devMiddleware(req, res, (err) => {
Expand All @@ -627,6 +613,12 @@ function honoWrapper(compiler, options) {

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

Check warning on line 614 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L614

Added line #L614 was not covered by tests
}

if (typeof body !== "undefined") {
return c.body(body, status);
}

await next();
};

wrapper.devMiddleware = devMiddleware;
Expand Down
1 change: 1 addition & 0 deletions src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function getFilenameFromUrl(context, url, extra = {}) {
filename = path.join(filename, indexValue);

try {
// eslint-disable-next-line no-param-reassign
extra.stats =
/** @type {import("fs").statSync} */
(context.outputFileSystem.statSync)(filename);
Expand Down
2 changes: 2 additions & 0 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ function applyTestMiddleware(name, middlewares) {
middlewares.push(async (ctx, next) => {
if (ctx.request.url === "/file.jpg") {
ctx.set("Content-Type", "text/html");
// eslint-disable-next-line no-param-reassign
ctx.body = "welcome";
}

Expand Down Expand Up @@ -1888,6 +1889,7 @@ describe.each([
);
});
} else {
// eslint-disable-next-line no-shadow
middlewares.unshift((req, res, next) => {
// Express API
if (res.set) {
Expand Down

0 comments on commit ccde8d1

Please sign in to comment.