Skip to content

Commit fcaf04d

Browse files
test: more
1 parent cae799e commit fcaf04d

File tree

7 files changed

+325
-239
lines changed

7 files changed

+325
-239
lines changed

.changeset/late-snails-argue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack-dev-middleware": major
33
---
44

5-
The `getFilenameFromUrl` function now returns an object with the found `filename` (or `undefined` if the file was not found) and throws an error if the URL cannot be processed. Additionally, the object contains the `extra` property with `stats` (file system stats) and `outputFileSystem` (output file system where file was found) properties.
5+
The `getFilenameFromUrl` function is now asynchronous, returning a Promise that resolves to the object with the found `filename` (or `undefined` if the file was not found) or throws an error if the URL cannot be processed. Additionally, the object contains the `extra` property with `stats` (file system stats) and `outputFileSystem` (output file system where file was found) properties.

README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,16 @@ const app = new express();
460460
app.use(instance);
461461

462462
instance.waitUntilValid(() => {
463-
let resolver;
464-
465-
try {
466-
resolved = instance.getFilenameFromUrl("/bundle.js");
467-
} catch (err) {
468-
console.log(`Error: ${err}`);
469-
}
470-
471-
if (!resolved) {
472-
console.log("Not found");
473-
return;
474-
}
475-
476-
console.log(`Filename is ${filename}`);
463+
instance
464+
.getFilenameFromUrl("/bundle.js")
465+
.then(() => {
466+
if (filename) {
467+
console.log(`Filename is ${filename}`);
468+
}
469+
})
470+
.catch((err) => {
471+
console.log(`Error: ${err}`);
472+
});
477473
});
478474
```
479475

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const noop = () => {};
1414
/** @typedef {import("webpack").Stats} Stats */
1515
/** @typedef {import("webpack").MultiStats} MultiStats */
1616
/** @typedef {import("fs").ReadStream} ReadStream */
17-
/** @typedef {import("./middleware").Extra} Extra */
17+
/** @typedef {import("./middleware").FilenameWithExtra} FilenameWithExtra */
1818

1919
// eslint-disable-next-line jsdoc/reject-any-type
2020
/** @typedef {any} EXPECTED_ANY */
@@ -126,7 +126,7 @@ const noop = () => {};
126126
/**
127127
* @callback GetFilenameFromUrl
128128
* @param {string} url request URL
129-
* @returns {{ filename: string, extra: Extra } | undefined} a filename with additional information, or `undefined` if nothing is found
129+
* @returns {Promise<FilenameWithExtra | undefined>} a filename with additional information, or `undefined` if nothing is found
130130
*/
131131

132132
/**

src/middleware.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ function isNotFoundError(error) {
109109
* @template {ServerResponse} Response
110110
* @param {import("./index.js").FilledContext<Request, Response>} context context
111111
* @param {string} url url
112-
* @returns {FilenameWithExtra | undefined} result of get filename from url
112+
* @returns {Promise<FilenameWithExtra | undefined>} result of get filename from url
113113
*/
114-
function getFilenameFromUrl(context, url) {
114+
async function getFilenameFromUrl(context, url) {
115115
/** @type {URL} */
116116
let urlObject;
117117

@@ -198,9 +198,9 @@ function getFilenameFromUrl(context, url) {
198198

199199
/**
200200
* @param {string} filename filename
201-
* @returns {FilenameWithExtra | undefined} filename when found, otherwise undefined
201+
* @returns {Promise<FilenameWithExtra | undefined>} filename when found, otherwise undefined
202202
*/
203-
const resolveIndex = (filename) => {
203+
const resolveIndex = async (filename) => {
204204
if (index.length === 0) {
205205
return;
206206
}
@@ -234,9 +234,9 @@ function getFilenameFromUrl(context, url) {
234234

235235
/**
236236
* @param {string} filename filename
237-
* @returns {FilenameWithExtra | undefined} filename when found, otherwise undefined
237+
* @returns {Promise<FilenameWithExtra | undefined>} filename when found, otherwise undefined
238238
*/
239-
const resolveFile = (filename) => {
239+
const resolveFile = async (filename) => {
240240
let stats;
241241

242242
try {
@@ -270,7 +270,7 @@ function getFilenameFromUrl(context, url) {
270270

271271
// send index logic
272272
if (index.length > 0 && pathname.endsWith("/")) {
273-
const result = resolveIndex(filename);
273+
const result = await resolveIndex(filename);
274274

275275
if (!result) {
276276
continue;
@@ -280,7 +280,7 @@ function getFilenameFromUrl(context, url) {
280280
}
281281

282282
// send file logic
283-
const result = resolveFile(filename);
283+
const result = await resolveFile(filename);
284284

285285
if (!result) {
286286
continue;
@@ -771,7 +771,7 @@ function wrapper(context) {
771771
const requestUrl = /** @type {string} */ (getRequestURL(req));
772772

773773
try {
774-
resolved = getFilenameFromUrl(context, requestUrl);
774+
resolved = await getFilenameFromUrl(context, requestUrl);
775775
} catch (err) {
776776
// Fallback to 403 for unknown errors
777777
const errorCode =

0 commit comments

Comments
 (0)