minimalistic-server 0.0.64 → 0.0.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +53 -22
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1707,6 +1707,7 @@ export class FileResponse extends Response {
|
|
|
1707
1707
|
|
|
1708
1708
|
#blocked = false;
|
|
1709
1709
|
#proxiedResponse = null;
|
|
1710
|
+
#enableFragments = false;
|
|
1710
1711
|
|
|
1711
1712
|
constructor(filePath, code = 200, contentType = null, cookies = null) {
|
|
1712
1713
|
super();
|
|
@@ -1795,6 +1796,11 @@ export class FileResponse extends Response {
|
|
|
1795
1796
|
this.#dataPromise = this.#proxiedResponse = null;
|
|
1796
1797
|
}
|
|
1797
1798
|
|
|
1799
|
+
enableFraments() {
|
|
1800
|
+
this.#enableFragments = true;
|
|
1801
|
+
this.#dataPromise = this.#proxiedResponse = null;
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1798
1804
|
setUrlPathForDirectory(urlPathForDirectory) {
|
|
1799
1805
|
this.#urlPathForDirectory = urlPathForDirectory;
|
|
1800
1806
|
this.#dataPromise = this.#proxiedResponse = null;
|
|
@@ -1812,7 +1818,7 @@ export class FileResponse extends Response {
|
|
|
1812
1818
|
#getFragmentRequest(headers) {
|
|
1813
1819
|
let promise = this.#fragmentRequestMap.get(headers);
|
|
1814
1820
|
|
|
1815
|
-
if (promise === undefined && headers?.['range']) {
|
|
1821
|
+
if (promise === undefined && headers?.['range'] && this.#enableFragments) {
|
|
1816
1822
|
const getFragmentRequest = async () => {
|
|
1817
1823
|
if (headers['range'].includes(',')) {
|
|
1818
1824
|
return null;
|
|
@@ -2460,6 +2466,7 @@ async function handleRequest(req, routes, staticFileDirectories, handleNotFoundE
|
|
|
2460
2466
|
|
|
2461
2467
|
if (!resp) {
|
|
2462
2468
|
resp = new FileResponse(filePath);
|
|
2469
|
+
resp.enableFraments();
|
|
2463
2470
|
|
|
2464
2471
|
if (staticFileOrDirectory.maxAgeInSeconds > 0) {
|
|
2465
2472
|
resp.addCustomHeaders({
|
|
@@ -2492,39 +2499,63 @@ async function handleRequest(req, routes, staticFileDirectories, handleNotFoundE
|
|
|
2492
2499
|
|
|
2493
2500
|
routeHandler = wrapInMiddlewares(routeHandler, staticFileOrDirectory.preMiddlewares, staticFileOrDirectory.postMiddlewares);
|
|
2494
2501
|
} else {
|
|
2495
|
-
|
|
2496
|
-
if (!
|
|
2497
|
-
|
|
2502
|
+
function getRouteHandler(fragments, root, methodPath, accumulatedPathParams) {
|
|
2503
|
+
if (!fragments.at(0) || !root) {
|
|
2504
|
+
return typeof root?.[methodPath] === 'function' ? root[methodPath] : null;
|
|
2498
2505
|
}
|
|
2499
2506
|
|
|
2500
|
-
|
|
2507
|
+
const fragment = fragments.shift();
|
|
2508
|
+
let newRoot = root[fragment];
|
|
2509
|
+
let result = null;
|
|
2501
2510
|
|
|
2502
|
-
if (
|
|
2503
|
-
|
|
2511
|
+
if (newRoot) {
|
|
2512
|
+
result = getRouteHandler(fragments, newRoot, methodPath, accumulatedPathParams);
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
if (!result) {
|
|
2516
|
+
for (let k in root) {
|
|
2504
2517
|
if (k.match(/^{\w+}$/gm)) {
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2518
|
+
newRoot = root[k];
|
|
2519
|
+
result = getRouteHandler(fragments, newRoot, methodPath, accumulatedPathParams);
|
|
2520
|
+
|
|
2521
|
+
if (result) {
|
|
2522
|
+
setObjectProperty(accumulatedPathParams, k.replace(/[{}]/gm, ''), decodeURIComponent(fragment));
|
|
2523
|
+
break;
|
|
2524
|
+
}
|
|
2508
2525
|
}
|
|
2509
2526
|
}
|
|
2510
2527
|
}
|
|
2511
2528
|
|
|
2512
|
-
|
|
2529
|
+
fragments.unshift(fragment);
|
|
2530
|
+
return result;
|
|
2531
|
+
}
|
|
2513
2532
|
|
|
2514
|
-
|
|
2515
|
-
|
|
2533
|
+
const fragments = path.split('/');
|
|
2534
|
+
let accumulatedPathParams = {};
|
|
2535
|
+
routeHandler = getRouteHandler(fragments, routes, `/${method}/`, accumulatedPathParams);
|
|
2536
|
+
|
|
2537
|
+
if (typeof routeHandler !== 'function') {
|
|
2538
|
+
if (method === 'OPTIONS') {
|
|
2539
|
+
handleOptions = true;
|
|
2540
|
+
accumulatedPathParams = {};
|
|
2541
|
+
routeHandler = getRouteHandler(
|
|
2542
|
+
fragments,
|
|
2543
|
+
routes,
|
|
2544
|
+
`/${requestHeaders?.['access-control-request-method']}/`.toUpperCase(),
|
|
2545
|
+
accumulatedPathParams,
|
|
2546
|
+
);
|
|
2547
|
+
} else if (!responseBodyIsIncluded) {
|
|
2548
|
+
accumulatedPathParams = {};
|
|
2549
|
+
routeHandler = getRouteHandler(
|
|
2550
|
+
fragments,
|
|
2551
|
+
routes,
|
|
2552
|
+
`/GET/`,
|
|
2553
|
+
accumulatedPathParams,
|
|
2554
|
+
);
|
|
2516
2555
|
}
|
|
2517
2556
|
}
|
|
2518
2557
|
|
|
2519
|
-
|
|
2520
|
-
if (method === 'OPTIONS' && typeof routeHandler?.[`/${method}/`] !== 'function') {
|
|
2521
|
-
handleOptions = true;
|
|
2522
|
-
routeHandler = routeHandler?.[`/${requestHeaders?.['access-control-request-method']}/`.toUpperCase()];
|
|
2523
|
-
} else if (!responseBodyIsIncluded && typeof routeHandler?.[`/${method}/`] !== 'function') {
|
|
2524
|
-
routeHandler = routeHandler?.[`/GET/`];
|
|
2525
|
-
} else {
|
|
2526
|
-
routeHandler = routeHandler?.[`/${method}/`];
|
|
2527
|
-
}
|
|
2558
|
+
Object.assign(pathParams, accumulatedPathParams);
|
|
2528
2559
|
}
|
|
2529
2560
|
|
|
2530
2561
|
if (typeof routeHandler === 'function') {
|