@rpcbase/server 0.444.0 → 0.446.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;AAsDxB,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,OAAO,EACZ,UAAU,EAAE,GAAG,EAAE,GAChB,OAAO,CAAC,oBAAoB,CAAC,CAkG/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;AAoExB,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,OAAO,EACZ,UAAU,EAAE,GAAG,EAAE,GAChB,OAAO,CAAC,oBAAoB,CAAC,CAqH/B"}
package/dist/index.js CHANGED
@@ -362,6 +362,15 @@ function getShortCircuitMatches(routes) {
362
362
  route
363
363
  };
364
364
  }
365
+ const NOT_FOUND_STATUS = 404;
366
+ const getErrorStatus = (error) => {
367
+ if (!error) return void 0;
368
+ const candidate = error?.reason ?? error;
369
+ if (typeof candidate?.status === "number") return candidate.status;
370
+ if (typeof candidate?.statusCode === "number") return candidate.statusCode;
371
+ if (typeof candidate?.response?.status === "number") return candidate.response.status;
372
+ return void 0;
373
+ };
365
374
  async function applyRouteLoaders(req, dataRoutes) {
366
375
  const baseUrl = `${req.protocol}://${req.get("host")}`;
367
376
  const url = new URL(req.originalUrl, baseUrl);
@@ -418,6 +427,8 @@ async function applyRouteLoaders(req, dataRoutes) {
418
427
  );
419
428
  const loaderData = {};
420
429
  let errors = null;
430
+ let hasNotFoundError = false;
431
+ let hasNonNotFoundError = false;
421
432
  for (const result of loaderPromisesResults) {
422
433
  if (result.status === "fulfilled") {
423
434
  if (result.value) {
@@ -431,16 +442,32 @@ async function applyRouteLoaders(req, dataRoutes) {
431
442
  if (!errors) {
432
443
  errors = {};
433
444
  }
445
+ const status = getErrorStatus(result.reason);
446
+ if (status === NOT_FOUND_STATUS) {
447
+ hasNotFoundError = true;
448
+ } else {
449
+ hasNonNotFoundError = true;
450
+ }
434
451
  errors[id] = result.reason;
435
452
  }
436
453
  }
454
+ let statusCode = 200;
455
+ if (errors) {
456
+ if (hasNonNotFoundError) {
457
+ statusCode = 500;
458
+ } else if (hasNotFoundError) {
459
+ statusCode = NOT_FOUND_STATUS;
460
+ } else {
461
+ statusCode = 500;
462
+ }
463
+ }
437
464
  return {
438
465
  ...baseContext,
439
466
  matches,
440
467
  loaderData,
441
468
  actionData: null,
442
469
  errors,
443
- statusCode: Object.keys(errors || {}).length > 0 ? 500 : 200
470
+ statusCode
444
471
  };
445
472
  }
446
473
  async function renderSSR(req, dataRoutes) {
@@ -470,7 +497,7 @@ async function renderSSR(req, dataRoutes) {
470
497
  }
471
498
  ) });
472
499
  const isMatched = routerContext.matches.length > 0;
473
- return { element, isMatched };
500
+ return { element, isMatched, statusCode: routerContext.statusCode ?? (routerContext.errors ? 500 : 200) };
474
501
  }
475
502
  const ABORT_DELAY_MS = 1e4;
476
503
  const APP_HTML_PLACEHOLDER = "<!--app-html-->";
@@ -538,7 +565,7 @@ const sendErrorResponse = ({
538
565
  null,
539
566
  createElement(SsrErrorFallback, {
540
567
  state,
541
- renderErrorExtra: (payload) => createElement(errorExtraComponent, { state: payload })
568
+ renderErrorExtra: errorExtraComponent ? (payload) => createElement(errorExtraComponent, { state: payload }) : void 0
542
569
  })
543
570
  )
544
571
  );
@@ -591,7 +618,7 @@ const ssrMiddleware = ({
591
618
  htmlStart = templateStart;
592
619
  }
593
620
  htmlEnd = template.slice(placeholderIndex + APP_HTML_PLACEHOLDER.length);
594
- const { element, isMatched } = await renderSSR(req, dataRoutes);
621
+ const { element, isMatched, statusCode } = await renderSSR(req, dataRoutes);
595
622
  if (!isMatched) {
596
623
  next();
597
624
  return;
@@ -624,7 +651,8 @@ const ssrMiddleware = ({
624
651
  return;
625
652
  }
626
653
  responseCommitted = true;
627
- res.status(didError ? 500 : 200);
654
+ const responseStatus = didError ? 500 : statusCode || 200;
655
+ res.status(responseStatus);
628
656
  res.set({ "Content-Type": "text/html" });
629
657
  const transformStream = new Transform({
630
658
  transform(chunk, encoding, callback) {
@@ -4,5 +4,6 @@ import * as express from "express";
4
4
  export declare function renderSSR(req: express.Request, dataRoutes: StaticHandler["dataRoutes"]): Promise<{
5
5
  element: ReactNode;
6
6
  isMatched: boolean;
7
+ statusCode: number;
7
8
  }>;
8
9
  //# sourceMappingURL=renderSSR.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"renderSSR.d.ts","sourceRoot":"","sources":["../src/renderSSR.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,SAAS,EAAc,MAAM,OAAO,CAAA;AAC7C,OAAO,EAGL,aAAa,EACd,MAAM,iBAAiB,CAAA;AAOxB,wBAAsB,SAAS,CAC7B,GAAG,EAAE,OAAO,CAAC,OAAO,EACpB,UAAU,EAAE,aAAa,CAAC,YAAY,CAAC,GACtC,OAAO,CAAC;IAAC,OAAO,EAAE,SAAS,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAC,CAAC,CAuCnD"}
1
+ {"version":3,"file":"renderSSR.d.ts","sourceRoot":"","sources":["../src/renderSSR.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,SAAS,EAAc,MAAM,OAAO,CAAA;AAC7C,OAAO,EAGL,aAAa,EACd,MAAM,iBAAiB,CAAA;AAOxB,wBAAsB,SAAS,CAC7B,GAAG,EAAE,OAAO,CAAC,OAAO,EACpB,UAAU,EAAE,aAAa,CAAC,YAAY,CAAC,GACtC,OAAO,CAAC;IAAC,OAAO,EAAE,SAAS,CAAC;IAAC,SAAS,EAAE,OAAO,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAC,CAAC,CAuCvE"}
@@ -1,12 +1,15 @@
1
+ import { SsrErrorStatePayload } from '../../client/src';
1
2
  import { StaticHandler } from '../../router/src';
2
3
  import { createServer } from 'vite';
3
- import { ReactNode } from 'react';
4
+ import { ComponentType } from 'react';
4
5
  import { NextFunction, Request, Response } from 'express';
5
6
  type ViteDevServer = Awaited<ReturnType<typeof createServer>>;
6
7
  export declare const ssrMiddleware: ({ viteInstance, dataRoutes, errorExtraComponent, renderTemplateStart, }: {
7
8
  viteInstance: ViteDevServer;
8
9
  dataRoutes: StaticHandler["dataRoutes"];
9
- errorExtraComponent: ReactNode;
10
+ errorExtraComponent?: ComponentType<{
11
+ state?: SsrErrorStatePayload;
12
+ }>;
10
13
  renderTemplateStart?: (req: Request, templateStart: string) => Promise<string>;
11
14
  }) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
12
15
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"ssrMiddleware.d.ts","sourceRoot":"","sources":["../src/ssrMiddleware.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AACnC,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAEjE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAKzD,KAAK,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC;AAuG9D,eAAO,MAAM,aAAa,GAAI,yEAK3B;IACD,YAAY,EAAE,aAAa,CAAC;IAC5B,UAAU,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;IACxC,mBAAmB,EAAE,SAAS,CAAC;IAC/B,mBAAmB,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAChF,MAAW,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,kBAyH1D,CAAA"}
1
+ {"version":3,"file":"ssrMiddleware.d.ts","sourceRoot":"","sources":["../src/ssrMiddleware.ts"],"names":[],"mappings":"AAKA,OAAO,EAAwE,KAAK,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACjI,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AACnC,OAAO,EAA6B,KAAK,aAAa,EAAE,MAAM,OAAO,CAAA;AAErE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAKzD,KAAK,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC;AAyG9D,eAAO,MAAM,aAAa,GAAI,yEAK3B;IACD,YAAY,EAAE,aAAa,CAAC;IAC5B,UAAU,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;IACxC,mBAAmB,CAAC,EAAE,aAAa,CAAC;QAAE,KAAK,CAAC,EAAE,oBAAoB,CAAA;KAAE,CAAC,CAAC;IACtE,mBAAmB,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAChF,MAAW,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,kBA0H1D,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.444.0",
3
+ "version": "0.446.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -32,8 +32,9 @@
32
32
  "build"
33
33
  ],
34
34
  "files": [
35
+ "../../scripts/publish.js",
35
36
  "package.json",
36
- "src/**/*"
37
+ "dist/**/*"
37
38
  ],
38
39
  "output": [],
39
40
  "env": {