@rpcbase/server 0.448.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.
- package/dist/applyRouteLoaders.d.ts.map +1 -1
- package/dist/index.js +27 -12
- package/package.json +1 -1
|
@@ -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;
|
|
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
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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;
|