minimalistic-server 0.0.53 → 0.0.54

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.
Files changed (2) hide show
  1. package/index.mjs +64 -50
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -2133,6 +2133,59 @@ export function unserve(port = 80) {
2133
2133
  servers.delete(port);
2134
2134
  }
2135
2135
 
2136
+ function wrapInMiddlewares(callback, preMiddlewares = [], postMiddlewares = [], handleServerError = null) {
2137
+ let resultCallback = async (request, handleOptions = false) => {
2138
+ let response;
2139
+
2140
+ try {
2141
+ for (const pre of preMiddlewares) {
2142
+ const req = await pre(request);
2143
+
2144
+ if (req instanceof Request) {
2145
+ request = req;
2146
+ }
2147
+ }
2148
+
2149
+ response = wrapInResponseClass(handleOptions ? new CustomResponse(Buffer.from(''), 200, { 'Content-Type': null }) : await callback(request));
2150
+ } catch (error) {
2151
+ if (error instanceof Response) {
2152
+ response = error;
2153
+ } else {
2154
+ throw error;
2155
+ }
2156
+ }
2157
+
2158
+ for (const post of postMiddlewares) {
2159
+ const res = await post(request, response);
2160
+
2161
+ if (res !== undefined) {
2162
+ response = wrapInResponseClass(res);
2163
+ }
2164
+ }
2165
+
2166
+ return response;
2167
+ };
2168
+
2169
+ if (handleServerError) {
2170
+ const tempCallback = resultCallback;
2171
+
2172
+ resultCallback = async (request, handleOptions = false) => {
2173
+ try {
2174
+ return await tempCallback(request, handleOptions);
2175
+ } catch (error) {
2176
+ if (error instanceof Response) {
2177
+ throw error;
2178
+ }
2179
+
2180
+ safePrint(error);
2181
+ return wrapInResponseClass(await handleServerError(request, error));
2182
+ }
2183
+ };
2184
+ }
2185
+
2186
+ return resultCallback;
2187
+ }
2188
+
2136
2189
  function normalizeStaticFileDirectories(staticFileDirectoryOrDirectories) {
2137
2190
  if (staticFileDirectoryOrDirectories !== null && staticFileDirectoryOrDirectories !== undefined) {
2138
2191
  if (!Array.isArray(staticFileDirectoryOrDirectories)) {
@@ -2140,27 +2193,33 @@ function normalizeStaticFileDirectories(staticFileDirectoryOrDirectories) {
2140
2193
  }
2141
2194
 
2142
2195
  staticFileDirectoryOrDirectories = staticFileDirectoryOrDirectories.filter(x => x !== null && typeof x === 'object' || typeof x === 'string').map(x => {
2143
- let serverFilePath = '', urlPath = '', showWholeDirectory = false, maxAgeInSeconds = -1;
2196
+ let serverFilePath = '', urlPath = '', showWholeDirectory = false, maxAgeInSeconds = -1, preMiddlewares = [], postMiddlewares = [];
2144
2197
  const defaultMaxAge = 5 * 24 * 60 * 60;
2145
2198
 
2146
2199
  if (typeof x === 'string') {
2147
2200
  serverFilePath = urlPath = x;
2148
2201
  showWholeDirectory = false;
2149
2202
  maxAgeInSeconds = defaultMaxAge;
2203
+ preMiddlewares = [];
2204
+ postMiddlewares = [];
2150
2205
  } else {
2151
- ({ serverFilePath, urlPath, showWholeDirectory, maxAgeInSeconds } = x);
2206
+ ({ serverFilePath, urlPath, showWholeDirectory, maxAgeInSeconds, preMiddlewares, postMiddlewares } = x);
2152
2207
  }
2153
2208
 
2154
2209
  urlPath = `${urlPath}`.split('/').filter(x => x).join('/');
2155
2210
  serverFilePath = `${serverFilePath}`.split('/').filter(x => x).join('/');
2156
2211
  showWholeDirectory = !!showWholeDirectory;
2157
2212
  maxAgeInSeconds = Math.floor(+(maxAgeInSeconds ?? defaultMaxAge));
2213
+ preMiddlewares = Array.isArray(preMiddlewares) ? preMiddlewares.filter(x => typeof x === 'function') : [];
2214
+ postMiddlewares = Array.isArray(postMiddlewares) ? postMiddlewares.filter(x => typeof x === 'function') : [];
2158
2215
 
2159
2216
  return {
2160
2217
  urlPath,
2161
2218
  serverFilePath,
2162
2219
  showWholeDirectory,
2163
2220
  maxAgeInSeconds,
2221
+ preMiddlewares,
2222
+ postMiddlewares,
2164
2223
  };
2165
2224
  });
2166
2225
  } else {
@@ -2192,54 +2251,7 @@ function normalizeRoutes(routes, handleServerError) {
2192
2251
  flattenRecursively(root[prop], newPath, preMiddlewares, postMiddlewares);
2193
2252
  }
2194
2253
  } else if (typeof root === 'function') {
2195
- flatten[path] = async (request, handleOptions = false) => {
2196
- let response;
2197
-
2198
- try {
2199
- for (const pre of preMiddlewares) {
2200
- const req = await pre(request);
2201
-
2202
- if (req instanceof Request) {
2203
- request = req;
2204
- }
2205
- }
2206
-
2207
- response = wrapInResponseClass(handleOptions ? new CustomResponse(Buffer.from(''), 200, { 'Content-Type': null }) : await root(request));
2208
- } catch (error) {
2209
- if (error instanceof Response) {
2210
- response = error;
2211
- } else {
2212
- throw error;
2213
- }
2214
- }
2215
-
2216
- for (const post of postMiddlewares) {
2217
- const res = await post(request, response);
2218
-
2219
- if (res !== undefined) {
2220
- response = wrapInResponseClass(res);
2221
- }
2222
- }
2223
-
2224
- return response;
2225
- };
2226
-
2227
- if (handleServerError) {
2228
- const callback = flatten[path];
2229
-
2230
- flatten[path] = async (request, handleOptions = false) => {
2231
- try {
2232
- return await callback(request, handleOptions);
2233
- } catch (error) {
2234
- if (error instanceof Response) {
2235
- throw error;
2236
- }
2237
-
2238
- safePrint(error);
2239
- return wrapInResponseClass(await handleServerError(request, error));
2240
- }
2241
- };
2242
- }
2254
+ flatten[path] = wrapInMiddlewares(root, preMiddlewares, postMiddlewares, handleServerError);
2243
2255
  }
2244
2256
  }
2245
2257
 
@@ -2378,6 +2390,8 @@ async function handleRequest(req, routes, staticFileDirectories, handleNotFoundE
2378
2390
 
2379
2391
  return resp;
2380
2392
  };
2393
+
2394
+ routeHandler = wrapInMiddlewares(routeHandler, staticFileOrDirectory.preMiddlewares, staticFileOrDirectory.postMiddlewares);
2381
2395
  } else {
2382
2396
  for (const fragment of path.split('/')) {
2383
2397
  if (!fragment) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minimalistic-server",
3
- "version": "0.0.53",
3
+ "version": "0.0.54",
4
4
  "engines" : {
5
5
  "npm" : ">=8.6.0",
6
6
  "node" : ">=22.0.0"