@rpcbase/server 0.447.0 → 0.449.0

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.
@@ -1 +1 @@
1
- {"version":3,"file":"applyRouteLoaders.d.ts","sourceRoot":"","sources":["../src/applyRouteLoaders.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,SAAS,CAAA;AAC/B,OAAO,EACL,oBAAoB,EAMrB,MAAM,iBAAiB,CAAA;AAoExB,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,OAAO,EACZ,UAAU,EAAE,GAAG,EAAE,GAChB,OAAO,CAAC,oBAAoB,CAAC,CAqH/B"}
1
+ {"version":3,"file":"applyRouteLoaders.d.ts","sourceRoot":"","sources":["../src/applyRouteLoaders.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,SAAS,CAAA;AAC/B,OAAO,EACL,oBAAoB,EAMrB,MAAM,iBAAiB,CAAA;AAqExB,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,OAAO,EACZ,UAAU,EAAE,GAAG,EAAE,GAChB,OAAO,CAAC,oBAAoB,CAAC,CAsI/B"}
package/dist/index.js CHANGED
@@ -363,6 +363,7 @@ function getShortCircuitMatches(routes) {
363
363
  };
364
364
  }
365
365
  const NOT_FOUND_STATUS = 404;
366
+ const LOADER_TIMEOUT_MS = 4e3;
366
367
  const getErrorStatus = (error) => {
367
368
  if (!error) return void 0;
368
369
  const candidate = error?.reason ?? error;
@@ -408,22 +409,36 @@ async function applyRouteLoaders(req, dataRoutes) {
408
409
  statusCode: 200
409
410
  };
410
411
  }
411
- const loaderPromisesResults = await Promise.allSettled(
412
- matches.map(async (match) => {
413
- const { route, params } = match;
414
- if (!route.loader) return null;
412
+ const runLoaderWithTimeout = async (route, params) => {
413
+ if (!route.loader) return null;
414
+ let timeoutId;
415
+ const timeoutPromise = new Promise((_, reject) => {
416
+ timeoutId = setTimeout(() => {
417
+ const err = new Error(`Loader timeout after ${LOADER_TIMEOUT_MS}ms`);
418
+ err.status = 504;
419
+ console.error("[rpcbase timeout][server loader]", { routeId: route.id, ms: LOADER_TIMEOUT_MS, url: req.originalUrl });
420
+ reject({ id: route.id, reason: err });
421
+ }, LOADER_TIMEOUT_MS);
422
+ });
423
+ const loaderPromise = (async () => {
415
424
  try {
416
- return {
417
- id: route.id,
418
- data: await route.loader({
419
- params,
420
- ctx: { req }
421
- })
422
- };
425
+ const data = await route.loader({
426
+ params,
427
+ ctx: { req }
428
+ });
429
+ return { id: route.id, data };
423
430
  } catch (error) {
424
431
  throw { id: route.id, reason: error };
432
+ } finally {
433
+ if (timeoutId) {
434
+ clearTimeout(timeoutId);
435
+ }
425
436
  }
426
- })
437
+ })();
438
+ return Promise.race([loaderPromise, timeoutPromise]);
439
+ };
440
+ const loaderPromisesResults = await Promise.allSettled(
441
+ matches.map((match) => runLoaderWithTimeout(match.route, match.params))
427
442
  );
428
443
  const loaderData = {};
429
444
  let errors = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.447.0",
3
+ "version": "0.449.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"