@anansi/core 0.20.44 → 0.21.1
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/CHANGELOG.md +17 -0
- package/dist/client.js +120 -2
- package/dist/server.js +162 -8
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -1
- package/lib/index.server.d.ts +3 -0
- package/lib/index.server.d.ts.map +1 -1
- package/lib/index.server.js +3 -1
- package/lib/laySpouts.d.ts.map +1 -1
- package/lib/laySpouts.js +9 -5
- package/lib/scripts/index.server.js +3 -1
- package/lib/scripts/laySpouts.js +9 -5
- package/lib/scripts/scripts/serve.js +17 -7
- package/lib/scripts/scripts/ssrErrorHandler.js +82 -0
- package/lib/scripts/scripts/startDevserver.js +18 -4
- package/lib/scripts/serve.d.ts.map +1 -1
- package/lib/scripts/serve.js +17 -7
- package/lib/scripts/spouts/antd.js +19 -0
- package/lib/scripts/spouts/antd.server.js +4 -3
- package/lib/scripts/spouts/navigator.context.js +32 -0
- package/lib/scripts/spouts/navigator.js +24 -0
- package/lib/scripts/spouts/navigator.server.js +27 -0
- package/lib/scripts/spouts/prefetch.server.js +5 -1
- package/lib/scripts/ssrErrorHandler.d.ts +26 -0
- package/lib/scripts/ssrErrorHandler.d.ts.map +1 -0
- package/lib/scripts/ssrErrorHandler.js +82 -0
- package/lib/scripts/startDevserver.d.ts.map +1 -1
- package/lib/scripts/startDevserver.js +18 -4
- package/lib/spouts/antd.d.ts +3 -0
- package/lib/spouts/antd.d.ts.map +1 -0
- package/lib/spouts/antd.js +19 -0
- package/lib/spouts/antd.server.js +4 -3
- package/lib/spouts/navigator.context.d.ts +11 -0
- package/lib/spouts/navigator.context.d.ts.map +1 -0
- package/lib/spouts/navigator.context.js +33 -0
- package/lib/spouts/navigator.d.ts +5 -0
- package/lib/spouts/navigator.d.ts.map +1 -0
- package/lib/spouts/navigator.js +24 -0
- package/lib/spouts/navigator.server.d.ts +10 -0
- package/lib/spouts/navigator.server.d.ts.map +1 -0
- package/lib/spouts/navigator.server.js +27 -0
- package/lib/spouts/prefetch.server.d.ts.map +1 -1
- package/lib/spouts/prefetch.server.js +6 -1
- package/package.json +2 -2
- package/src/index.server.ts +3 -0
- package/src/index.ts +4 -0
- package/src/laySpouts.tsx +9 -5
- package/src/scripts/__tests__/ssrErrorHandler.test.ts +249 -0
- package/src/scripts/serve.ts +18 -6
- package/src/scripts/ssrErrorHandler.ts +98 -0
- package/src/scripts/startDevserver.ts +19 -3
- package/src/spouts/__tests__/navigator.test.tsx +103 -0
- package/src/spouts/antd.server.tsx +5 -5
- package/src/spouts/antd.tsx +15 -0
- package/src/spouts/navigator.context.tsx +42 -0
- package/src/spouts/navigator.server.tsx +40 -0
- package/src/spouts/navigator.tsx +35 -0
- package/src/spouts/prefetch.server.tsx +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigator.d.ts","sourceRoot":"","sources":["../../src/spouts/navigator.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,cAAc,IAAI,WAAW,CAAC;IACpD,cAAc,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CAC/C,CAAC,CA0BD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { NavigatorContext } from './navigator.context.js';
|
|
3
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
4
|
+
export default function navigatorSpout() {
|
|
5
|
+
return next => async props => {
|
|
6
|
+
const nextProps = await next(props);
|
|
7
|
+
const navigatorProps = await props.getInitialData('navigator').catch(e => {
|
|
8
|
+
console.warn('Navigator initial data could not load, using client navigator. Error:', e);
|
|
9
|
+
return {
|
|
10
|
+
language: navigator.language,
|
|
11
|
+
languages: [...navigator.languages]
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
...nextProps,
|
|
16
|
+
navigator: navigatorProps,
|
|
17
|
+
app: /*#__PURE__*/_jsx(NavigatorContext, {
|
|
18
|
+
value: navigatorProps,
|
|
19
|
+
children: nextProps.app
|
|
20
|
+
})
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIk5hdmlnYXRvckNvbnRleHQiLCJqc3giLCJfanN4IiwibmF2aWdhdG9yU3BvdXQiLCJuZXh0IiwicHJvcHMiLCJuZXh0UHJvcHMiLCJuYXZpZ2F0b3JQcm9wcyIsImdldEluaXRpYWxEYXRhIiwiY2F0Y2giLCJlIiwiY29uc29sZSIsIndhcm4iLCJsYW5ndWFnZSIsIm5hdmlnYXRvciIsImxhbmd1YWdlcyIsImFwcCIsInZhbHVlIiwiY2hpbGRyZW4iXSwic291cmNlcyI6WyIuLi8uLi9zcmMvc3BvdXRzL25hdmlnYXRvci50c3giXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JztcblxuaW1wb3J0IHsgTmF2aWdhdG9yQ29udGV4dCB9IGZyb20gJy4vbmF2aWdhdG9yLmNvbnRleHQuanMnO1xuaW1wb3J0IHR5cGUgeyBOYXZpZ2F0b3JQcm9wZXJ0aWVzIH0gZnJvbSAnLi9uYXZpZ2F0b3IuY29udGV4dC5qcyc7XG5pbXBvcnQgdHlwZSB7IENsaWVudFNwb3V0IH0gZnJvbSAnLi90eXBlcy5qcyc7XG5cbmV4cG9ydCBkZWZhdWx0IGZ1bmN0aW9uIG5hdmlnYXRvclNwb3V0KCk6IENsaWVudFNwb3V0PHtcbiAgZ2V0SW5pdGlhbERhdGE6IChrZXk6IHN0cmluZykgPT4gUHJvbWlzZTxhbnk+O1xufT4ge1xuICByZXR1cm4gbmV4dCA9PiBhc3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E1BQU1DLFNBQVMsR0FBRyxNQUFNRixJQUFJLENBQUNDLEtBQUssQ0FBQztJQUNuQyxNQUFNRSxjQUFtQyxHQUFHLE1BQU1GLEtBQUssQ0FDcERHLGNBQWMsQ0FBQyxXQUFXLENBQUMsQ0FDM0JDLEtBQUssQ0FBQ0MsQ0FBQyxJQUFJO01BQ1ZDLE9BQU8sQ0FBQ0MsSUFBSSxDQUNWLHVFQUF1RSxFQUN2RUYsQ0FDRixDQUFDO01BQ0QsT0FBTztRQUNMRyxRQUFRLEVBQUVDLFNBQVMsQ0FBQ0QsUUFBUTtRQUM1QkUsU0FBUyxFQUFFLENBQUMsR0FBR0QsU0FBUyxDQUFDQyxTQUFTO01BQ3BDLENBQUM7SUFDSCxDQUFDLENBQUM7SUFFSixPQUFPO01BQ0wsR0FBR1QsU0FBUztNQUNaUSxTQUFTLEVBQUVQLGNBQWM7TUFDekJTLEdBQUcsZUFDRGQsSUFBQSxDQUFDRixnQkFBZ0I7UUFBQ2lCLEtBQUssRUFBRVYsY0FBZTtRQUFBVyxRQUFBLEVBQ3JDWixTQUFTLENBQUNVO01BQUcsQ0FDRTtJQUV0QixDQUFDO0VBQ0gsQ0FBQztBQUNIIiwiaWdub3JlTGlzdCI6W119
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ServerSpout } from './types.js';
|
|
2
|
+
type NeededNext = {
|
|
3
|
+
initData?: Record<string, () => unknown>;
|
|
4
|
+
};
|
|
5
|
+
export default function navigatorSpout(): ServerSpout<Record<string, unknown>, {
|
|
6
|
+
language: string;
|
|
7
|
+
languages: readonly string[];
|
|
8
|
+
}, NeededNext>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=navigator.server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigator.server.d.ts","sourceRoot":"","sources":["../../src/spouts/navigator.server.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,KAAK,UAAU,GAAG;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC;CAC1C,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,cAAc,IAAI,WAAW,CACnD,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvB;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,EAClD,UAAU,CACX,CA0BA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { NavigatorContext, parseAcceptLanguage } from './navigator.context.js';
|
|
3
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
4
|
+
export default function navigatorSpout() {
|
|
5
|
+
return next => async props => {
|
|
6
|
+
const acceptLanguage = props.req.headers['accept-language'];
|
|
7
|
+
const header = typeof acceptLanguage === 'string' ? acceptLanguage : acceptLanguage == null ? void 0 : acceptLanguage[0];
|
|
8
|
+
const navigatorProps = parseAcceptLanguage(header);
|
|
9
|
+
const nextProps = await next({
|
|
10
|
+
...props,
|
|
11
|
+
...navigatorProps
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
...nextProps,
|
|
15
|
+
...navigatorProps,
|
|
16
|
+
initData: {
|
|
17
|
+
...nextProps.initData,
|
|
18
|
+
navigator: () => navigatorProps
|
|
19
|
+
},
|
|
20
|
+
app: /*#__PURE__*/_jsx(NavigatorContext, {
|
|
21
|
+
value: navigatorProps,
|
|
22
|
+
children: nextProps.app
|
|
23
|
+
})
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIk5hdmlnYXRvckNvbnRleHQiLCJwYXJzZUFjY2VwdExhbmd1YWdlIiwianN4IiwiX2pzeCIsIm5hdmlnYXRvclNwb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pB,GAAG,CAAC,CACN,MAEa,OAAO,CAAC,gBAJjB,CAAC,
|
|
1
|
+
{"version":3,"file":"prefetch.server.d.ts","sourceRoot":"","sources":["../../src/spouts/prefetch.server.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D,KAAK,WAAW,CAAC,SAAS,IAAI;IAC5B,aAAa,EAAE,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;IAClC,YAAY,EAAE,eAAe,CAAC;CAC/B,GAAG,YAAY,CAAC;AAEjB,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,IAE5D,SAAS,EACT,CAAC,SAAS,WAAW,CAAC,SAAS,CAAC,EAChC,CAAC,SAAS,WAAW,EAErB,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CACzB,GACG,CAAC,IAAI,CAAC,GAAG,SAAS,GACpB,GAAG,CAAC,CACN,MAEa,OAAO,CAAC,gBAJjB,CAAC,0BA+BT"}
|
|
@@ -5,6 +5,11 @@ export default function prefetchSpout(field) {
|
|
|
5
5
|
try {
|
|
6
6
|
const toFetch = [];
|
|
7
7
|
nextProps.matchedRoutes.forEach(route => {
|
|
8
|
+
var _route$component;
|
|
9
|
+
// Preload lazy component so it's ready for SSR render
|
|
10
|
+
if (typeof ((_route$component = route.component) == null ? void 0 : _route$component.preload) === 'function') {
|
|
11
|
+
toFetch.push(route.component.preload());
|
|
12
|
+
}
|
|
8
13
|
if (typeof route.resolveData === 'function') {
|
|
9
14
|
toFetch.push(route.resolveData(nextProps[field], route, nextProps.searchParams));
|
|
10
15
|
}
|
|
@@ -17,4 +22,4 @@ export default function prefetchSpout(field) {
|
|
|
17
22
|
};
|
|
18
23
|
};
|
|
19
24
|
}
|
|
20
|
-
//# sourceMappingURL=data:application/json;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ICAgICAgIFtLIGluIEZdOiBSb3V0ZVdpdGg7XG4gICAgICB9ICYgTlxuICAgID4sXG4gICkge1xuICAgIHJldHVybiBhc3luYyAocHJvcHM6IEkpID0+IHtcbiAgICAgIGNvbnN0IG5leHRQcm9wcyA9IGF3YWl0IG5leHQocHJvcHMpO1xuXG4gICAgICB0cnkge1xuICAgICAgICBjb25zdCB0b0ZldGNoOiBQcm9taXNlPHVua25vd24+W10gPSBbXTtcbiAgICAgICAgbmV4dFByb3BzLm1hdGNoZWRSb3V0ZXMuZm9yRWFjaChyb3V0ZSA9PiB7XG4gICAgICAgICAgLy8gUHJlbG9hZCBsYXp5IGNvbXBvbmVudCBzbyBpdCdzIHJlYWR5IGZvciBTU1IgcmVuZGVyXG4gICAgICAgICAgaWYgKHR5cGVvZiByb3V0ZS5jb21wb25lbnQ/LnByZWxvYWQgPT09ICdmdW5jdGlvbicpIHtcbiAgICAgICAgICAgIHRvRmV0Y2gucHVzaChyb3V0ZS5jb21wb25lbnQucHJlbG9hZCgpKTtcbiAgICAgICAgICB9XG4gICAgICAgICAgaWYgKHR5cGVvZiByb3V0ZS5yZXNvbHZlRGF0YSA9PT0gJ2Z1bmN0aW9uJykge1xuICAgICAgICAgICAgdG9GZXRjaC5wdXNoKFxuICAgICAgICAgICAgICByb3V0ZS5yZXNvbHZlRGF0YShcbiAgICAgICAgICAgICAgICBuZXh0UHJvcHNbZmllbGRdLFxuICAgICAgICAgICAgICAgIHJvdXRlLFxuICAgICAgICAgICAgICAgIG5leHRQcm9wcy5zZWFyY2hQYXJhbXMsXG4gICAgICAgICAgICAgICksXG4gICAgICAgICAgICApO1xuICAgICAgICAgIH1cbiAgICAgICAgfSk7XG4gICAgICAgIGF3YWl0IFByb21pc2UuYWxsKHRvRmV0Y2gpO1xuICAgICAgfSBjYXRjaCAoZSkge1xuICAgICAgICBjb25zb2xlLmVycm9yKGUpO1xuICAgICAgfVxuICAgICAgcmV0dXJuIG5leHRQcm9wcztcbiAgICB9O1xuIC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sQ0FBQztBQUNIIiwiaWdub3JlTGlzdCI6W119
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anansi/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1",
|
|
4
4
|
"description": "React 19 Framework",
|
|
5
5
|
"homepage": "https://github.com/ntucker/anansi/tree/master/packages/core#readme",
|
|
6
6
|
"repository": {
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"react-dom": "19.2.0"
|
|
82
82
|
},
|
|
83
83
|
"dependencies": {
|
|
84
|
-
"@anansi/router": "^0.10.
|
|
84
|
+
"@anansi/router": "^0.10.19",
|
|
85
85
|
"@babel/runtime-corejs3": "^7.26.0",
|
|
86
86
|
"chalk": "^4.1.2",
|
|
87
87
|
"compression": "^1.8.1",
|
package/src/index.server.ts
CHANGED
|
@@ -6,6 +6,9 @@ export { default as routerSpout } from './spouts/router.server.js';
|
|
|
6
6
|
export { default as prefetchSpout } from './spouts/prefetch.server.js';
|
|
7
7
|
export { default as JSONSpout } from './spouts/json.server.js';
|
|
8
8
|
export { default as appSpout } from './spouts/app.server.js';
|
|
9
|
+
export { default as navigatorSpout } from './spouts/navigator.server.js';
|
|
10
|
+
export { useNavigator } from './spouts/navigator.context.js';
|
|
11
|
+
export type { NavigatorProperties } from './spouts/navigator.context.js';
|
|
9
12
|
export type { ServerProps } from './spouts/types.js';
|
|
10
13
|
export type { ServerSpout as Spout } from './spouts/types.js';
|
|
11
14
|
export * from './scripts/types.js';
|
package/src/index.ts
CHANGED
|
@@ -4,5 +4,9 @@ export { default as dataClientSpout } from './spouts/dataClient.js';
|
|
|
4
4
|
export { default as routerSpout } from './spouts/router.js';
|
|
5
5
|
export { default as JSONSpout } from './spouts/json.js';
|
|
6
6
|
export { default as appSpout } from './spouts/app.js';
|
|
7
|
+
export { default as navigatorSpout } from './spouts/navigator.js';
|
|
8
|
+
export { default as antdSpout } from './spouts/antd.js';
|
|
9
|
+
export { useNavigator } from './spouts/navigator.context.js';
|
|
10
|
+
export type { NavigatorProperties } from './spouts/navigator.context.js';
|
|
7
11
|
export type { ServerProps } from './spouts/types.js';
|
|
8
12
|
export type { ClientSpout as Spout } from './spouts/types.js';
|
package/src/laySpouts.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import crypto from 'crypto';
|
|
|
2
2
|
import type { JSX } from 'react';
|
|
3
3
|
import { renderToPipeableStream as reactRender } from 'react-dom/server';
|
|
4
4
|
|
|
5
|
+
import { getErrorStatus } from './scripts/ssrErrorHandler.js';
|
|
5
6
|
import type { Render } from './scripts/types.js';
|
|
6
7
|
import type { ServerProps } from './spouts/types.js';
|
|
7
8
|
|
|
@@ -21,25 +22,28 @@ export default function laySpouts(
|
|
|
21
22
|
const { app } = await spouts({ clientManifest, req, res, nonce });
|
|
22
23
|
|
|
23
24
|
let didError = false;
|
|
25
|
+
let lastError: unknown;
|
|
24
26
|
const { pipe, abort } = reactRender(app, {
|
|
25
27
|
nonce,
|
|
26
28
|
//bootstrapScripts: assets.filter(asset => asset.endsWith('.js')),
|
|
27
29
|
onShellReady() {
|
|
28
30
|
//managers.forEach(manager => manager.cleanup());
|
|
29
31
|
// If something errored before we started streaming, we set the error code appropriately.
|
|
30
|
-
res.statusCode = didError ?
|
|
32
|
+
res.statusCode = didError ? getErrorStatus(lastError) : 200;
|
|
31
33
|
res.setHeader('Content-type', 'text/html');
|
|
32
34
|
pipe(res);
|
|
33
35
|
},
|
|
34
|
-
onShellError() {
|
|
36
|
+
onShellError(e: unknown) {
|
|
35
37
|
didError = true;
|
|
36
|
-
|
|
38
|
+
lastError = e;
|
|
39
|
+
res.statusCode = getErrorStatus(e);
|
|
37
40
|
pipe(res);
|
|
38
41
|
},
|
|
39
|
-
onError(e:
|
|
42
|
+
onError(e: unknown) {
|
|
40
43
|
didError = true;
|
|
44
|
+
lastError = e;
|
|
41
45
|
console.error(e);
|
|
42
|
-
res.statusCode =
|
|
46
|
+
res.statusCode = getErrorStatus(e);
|
|
43
47
|
//pipe(res); Removing this avoids, "React currently only supports piping to one writable stream."
|
|
44
48
|
if (onError) onError(e);
|
|
45
49
|
},
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import {
|
|
2
|
+
escapeHtml,
|
|
3
|
+
getErrorStatus,
|
|
4
|
+
renderErrorPage,
|
|
5
|
+
} from '../ssrErrorHandler';
|
|
6
|
+
|
|
7
|
+
describe('ssrErrorHandler', () => {
|
|
8
|
+
describe('getErrorStatus', () => {
|
|
9
|
+
it('should return 500 for null', () => {
|
|
10
|
+
expect(getErrorStatus(null)).toBe(500);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should return 500 for undefined', () => {
|
|
14
|
+
expect(getErrorStatus(undefined)).toBe(500);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should return 500 for plain Error without status', () => {
|
|
18
|
+
expect(getErrorStatus(new Error('test'))).toBe(500);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should return 500 for string error', () => {
|
|
22
|
+
expect(getErrorStatus('something went wrong')).toBe(500);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return 500 for number error', () => {
|
|
26
|
+
expect(getErrorStatus(42)).toBe(500);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should extract numeric status from error object', () => {
|
|
30
|
+
expect(getErrorStatus({ status: 404, message: 'Not Found' })).toBe(404);
|
|
31
|
+
expect(getErrorStatus({ status: 429, message: 'Rate Limited' })).toBe(
|
|
32
|
+
429,
|
|
33
|
+
);
|
|
34
|
+
expect(
|
|
35
|
+
getErrorStatus({ status: 503, message: 'Service Unavailable' }),
|
|
36
|
+
).toBe(503);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should extract string status from error object', () => {
|
|
40
|
+
expect(getErrorStatus({ status: '404', message: 'Not Found' })).toBe(404);
|
|
41
|
+
expect(getErrorStatus({ status: '500', message: 'Error' })).toBe(500);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return 500 for invalid numeric status codes', () => {
|
|
45
|
+
expect(getErrorStatus({ status: 99 })).toBe(500); // too low
|
|
46
|
+
expect(getErrorStatus({ status: 600 })).toBe(500); // too high
|
|
47
|
+
expect(getErrorStatus({ status: -1 })).toBe(500); // negative
|
|
48
|
+
expect(getErrorStatus({ status: 0 })).toBe(500); // zero
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should return 500 for invalid string status codes', () => {
|
|
52
|
+
expect(getErrorStatus({ status: '99' })).toBe(500); // too low
|
|
53
|
+
expect(getErrorStatus({ status: '600' })).toBe(500); // too high
|
|
54
|
+
expect(getErrorStatus({ status: 'abc' })).toBe(500); // not a number
|
|
55
|
+
expect(getErrorStatus({ status: '' })).toBe(500); // empty string
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should return 500 for non-number/string status values', () => {
|
|
59
|
+
expect(getErrorStatus({ status: null })).toBe(500);
|
|
60
|
+
expect(getErrorStatus({ status: undefined })).toBe(500);
|
|
61
|
+
expect(getErrorStatus({ status: {} })).toBe(500);
|
|
62
|
+
expect(getErrorStatus({ status: [] })).toBe(500);
|
|
63
|
+
expect(getErrorStatus({ status: true })).toBe(500);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should work with Error objects that have status property', () => {
|
|
67
|
+
const error = new Error('Rate Limited') as Error & { status: number };
|
|
68
|
+
error.status = 429;
|
|
69
|
+
expect(getErrorStatus(error)).toBe(429);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should handle boundary values', () => {
|
|
73
|
+
expect(getErrorStatus({ status: 100 })).toBe(100); // min valid
|
|
74
|
+
expect(getErrorStatus({ status: 599 })).toBe(599); // max valid
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('escapeHtml', () => {
|
|
79
|
+
it('should escape ampersands', () => {
|
|
80
|
+
expect(escapeHtml('foo & bar')).toBe('foo & bar');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should escape less than', () => {
|
|
84
|
+
expect(escapeHtml('<script>')).toBe('<script>');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should escape greater than', () => {
|
|
88
|
+
expect(escapeHtml('a > b')).toBe('a > b');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should escape double quotes', () => {
|
|
92
|
+
expect(escapeHtml('say "hello"')).toBe('say "hello"');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should escape single quotes', () => {
|
|
96
|
+
expect(escapeHtml("it's")).toBe('it's');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should escape multiple special characters', () => {
|
|
100
|
+
expect(escapeHtml('<script>alert("xss")</script>')).toBe(
|
|
101
|
+
'<script>alert("xss")</script>',
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should return empty string for empty input', () => {
|
|
106
|
+
expect(escapeHtml('')).toBe('');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should not modify strings without special characters', () => {
|
|
110
|
+
expect(escapeHtml('hello world')).toBe('hello world');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('renderErrorPage', () => {
|
|
115
|
+
it('should render basic error page with status code', () => {
|
|
116
|
+
const html = renderErrorPage(new Error('Test error'), '/test', 500);
|
|
117
|
+
|
|
118
|
+
expect(html).toContain('<!DOCTYPE html>');
|
|
119
|
+
expect(html).toContain('<title>500 - Server Error</title>');
|
|
120
|
+
expect(html).toContain('<h1>500 - Server Error</h1>');
|
|
121
|
+
expect(html).toContain('Test error');
|
|
122
|
+
expect(html).toContain('/test');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should render different status codes', () => {
|
|
126
|
+
const html404 = renderErrorPage({ message: 'Not Found' }, '/page', 404);
|
|
127
|
+
const html429 = renderErrorPage({ message: 'Rate Limited' }, '/api', 429);
|
|
128
|
+
|
|
129
|
+
expect(html404).toContain('<title>404 - Server Error</title>');
|
|
130
|
+
expect(html404).toContain('<h1>404 - Server Error</h1>');
|
|
131
|
+
expect(html429).toContain('<title>429 - Server Error</title>');
|
|
132
|
+
expect(html429).toContain('<h1>429 - Server Error</h1>');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should escape URL in output', () => {
|
|
136
|
+
const html = renderErrorPage(
|
|
137
|
+
new Error('error'),
|
|
138
|
+
'/test?foo=<script>',
|
|
139
|
+
500,
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(html).toContain('<script>');
|
|
143
|
+
expect(html).not.toContain('<script>');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should escape error message in output', () => {
|
|
147
|
+
const html = renderErrorPage(
|
|
148
|
+
new Error('<script>alert("xss")</script>'),
|
|
149
|
+
'/test',
|
|
150
|
+
500,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
expect(html).toContain('<script>');
|
|
154
|
+
expect(html).not.toContain('<script>alert');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should show stack trace when showStack is true', () => {
|
|
158
|
+
const error = new Error('Test error');
|
|
159
|
+
const html = renderErrorPage(error, '/test', 500, { showStack: true });
|
|
160
|
+
|
|
161
|
+
expect(html).toContain('<pre class="stack">');
|
|
162
|
+
expect(html).toContain('Error: Test error');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should hide stack trace when showStack is false', () => {
|
|
166
|
+
const error = new Error('Test error');
|
|
167
|
+
const html = renderErrorPage(error, '/test', 500, { showStack: false });
|
|
168
|
+
|
|
169
|
+
expect(html).not.toContain('<pre class="stack">');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should hide stack trace by default', () => {
|
|
173
|
+
const error = new Error('Test error');
|
|
174
|
+
const html = renderErrorPage(error, '/test', 500);
|
|
175
|
+
|
|
176
|
+
expect(html).not.toContain('<pre class="stack">');
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('should show badge when provided', () => {
|
|
180
|
+
const html = renderErrorPage(new Error('error'), '/test', 500, {
|
|
181
|
+
badge: 'DEV MODE',
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
expect(html).toContain('<span class="badge">DEV MODE</span>');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should not show badge when not provided', () => {
|
|
188
|
+
const html = renderErrorPage(new Error('error'), '/test', 500);
|
|
189
|
+
|
|
190
|
+
expect(html).not.toContain('class="badge"');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('should show hint when provided', () => {
|
|
194
|
+
const html = renderErrorPage(new Error('error'), '/test', 500, {
|
|
195
|
+
hint: 'Try again later',
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(html).toContain('<p class="hint">Try again later</p>');
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('should not show hint when not provided', () => {
|
|
202
|
+
const html = renderErrorPage(new Error('error'), '/test', 500);
|
|
203
|
+
|
|
204
|
+
expect(html).not.toContain('class="hint"');
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('should escape badge text', () => {
|
|
208
|
+
const html = renderErrorPage(new Error('error'), '/test', 500, {
|
|
209
|
+
badge: '<script>alert("xss")</script>',
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(html).toContain('<script>');
|
|
213
|
+
expect(html).not.toContain('<script>alert');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('should escape hint text', () => {
|
|
217
|
+
const html = renderErrorPage(new Error('error'), '/test', 500, {
|
|
218
|
+
hint: '<script>alert("xss")</script>',
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
expect(html).toContain('<script>');
|
|
222
|
+
expect(html).not.toContain('<script>alert');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should handle string errors', () => {
|
|
226
|
+
const html = renderErrorPage('Something went wrong', '/test', 500);
|
|
227
|
+
|
|
228
|
+
expect(html).toContain('Something went wrong');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should handle object errors without message', () => {
|
|
232
|
+
const html = renderErrorPage({ code: 'ERR_001' }, '/test', 500);
|
|
233
|
+
|
|
234
|
+
expect(html).toContain('[object Object]');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should include retry link with correct URL', () => {
|
|
238
|
+
const html = renderErrorPage(new Error('error'), '/my-page', 500);
|
|
239
|
+
|
|
240
|
+
expect(html).toContain('<a href="/my-page">Retry</a>');
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('should escape retry link URL', () => {
|
|
244
|
+
const html = renderErrorPage(new Error('error'), '/page?a=1&b=2', 500);
|
|
245
|
+
|
|
246
|
+
expect(html).toContain('href="/page?a=1&b=2"');
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
});
|
package/src/scripts/serve.ts
CHANGED
|
@@ -10,12 +10,12 @@ import diskFs from 'fs';
|
|
|
10
10
|
import { Server, IncomingMessage, ServerResponse } from 'http';
|
|
11
11
|
import ora from 'ora';
|
|
12
12
|
import path from 'path';
|
|
13
|
-
import { promisify } from 'util';
|
|
14
13
|
import webpack from 'webpack';
|
|
15
14
|
|
|
16
15
|
import 'cross-fetch/dist/node-polyfill.js';
|
|
17
16
|
import getProxyMiddlewares from './getProxyMiddlewares.js';
|
|
18
17
|
import { getWebpackConfig } from './getWebpackConfig.js';
|
|
18
|
+
import { getErrorStatus, renderErrorPage } from './ssrErrorHandler.js';
|
|
19
19
|
import { Render } from './types.js';
|
|
20
20
|
|
|
21
21
|
// run directly from node
|
|
@@ -45,7 +45,6 @@ export default async function serve(
|
|
|
45
45
|
webpackConfig({}, { mode: 'production' }),
|
|
46
46
|
);
|
|
47
47
|
|
|
48
|
-
const readFile = promisify(diskFs.readFile);
|
|
49
48
|
let server: Server | undefined;
|
|
50
49
|
|
|
51
50
|
function handleErrors<
|
|
@@ -57,12 +56,25 @@ export default async function serve(
|
|
|
57
56
|
return async function (
|
|
58
57
|
req: Request | IncomingMessage,
|
|
59
58
|
res: Response | ServerResponse,
|
|
60
|
-
|
|
59
|
+
_next: NextFunction,
|
|
61
60
|
) {
|
|
62
61
|
try {
|
|
63
62
|
return await fn(req, res);
|
|
64
|
-
} catch (
|
|
65
|
-
|
|
63
|
+
} catch (error: unknown) {
|
|
64
|
+
console.error('SSR rendering error:', error);
|
|
65
|
+
|
|
66
|
+
// Return error response with status from error if available
|
|
67
|
+
const expressRes = res as express.Response;
|
|
68
|
+
if (!expressRes.headersSent) {
|
|
69
|
+
const statusCode = getErrorStatus(error);
|
|
70
|
+
expressRes.status(statusCode);
|
|
71
|
+
expressRes.setHeader('Content-Type', 'text/html');
|
|
72
|
+
expressRes.send(
|
|
73
|
+
renderErrorPage(error, req.url ?? '/', statusCode, {
|
|
74
|
+
showStack: process.env.NODE_ENV !== 'production',
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
66
78
|
}
|
|
67
79
|
};
|
|
68
80
|
}
|
|
@@ -104,7 +116,7 @@ export default async function serve(
|
|
|
104
116
|
) {
|
|
105
117
|
try {
|
|
106
118
|
res.sendFile(assetPath);
|
|
107
|
-
} catch (
|
|
119
|
+
} catch (_e) {
|
|
108
120
|
return next();
|
|
109
121
|
}
|
|
110
122
|
} else {
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for handling SSR errors gracefully
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extract HTTP status code from an error object.
|
|
7
|
+
* Looks for a `status` property that is a number or parseable string.
|
|
8
|
+
* Returns 500 if no valid status found.
|
|
9
|
+
*/
|
|
10
|
+
export function getErrorStatus(error: unknown): number {
|
|
11
|
+
if (error && typeof error === 'object' && 'status' in error) {
|
|
12
|
+
const status = (error as { status: unknown }).status;
|
|
13
|
+
if (typeof status === 'number' && status >= 100 && status < 600) {
|
|
14
|
+
return status;
|
|
15
|
+
}
|
|
16
|
+
if (typeof status === 'string') {
|
|
17
|
+
const parsed = parseInt(status, 10);
|
|
18
|
+
if (!isNaN(parsed) && parsed >= 100 && parsed < 600) {
|
|
19
|
+
return parsed;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return 500;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Escape HTML special characters to prevent XSS
|
|
28
|
+
*/
|
|
29
|
+
export function escapeHtml(str: string): string {
|
|
30
|
+
return str
|
|
31
|
+
.replace(/&/g, '&')
|
|
32
|
+
.replace(/</g, '<')
|
|
33
|
+
.replace(/>/g, '>')
|
|
34
|
+
.replace(/"/g, '"')
|
|
35
|
+
.replace(/'/g, ''');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RenderErrorPageOptions {
|
|
39
|
+
/** Show stack trace in output */
|
|
40
|
+
showStack?: boolean;
|
|
41
|
+
/** Additional hint message to display */
|
|
42
|
+
hint?: string;
|
|
43
|
+
/** Badge text to display (e.g., "DEV MODE") */
|
|
44
|
+
badge?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Render an HTML error page for SSR failures
|
|
49
|
+
*/
|
|
50
|
+
export function renderErrorPage(
|
|
51
|
+
error: unknown,
|
|
52
|
+
url: string,
|
|
53
|
+
statusCode: number,
|
|
54
|
+
options: RenderErrorPageOptions = {},
|
|
55
|
+
): string {
|
|
56
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
57
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
58
|
+
const { showStack = false, hint, badge } = options;
|
|
59
|
+
|
|
60
|
+
return `<!DOCTYPE html>
|
|
61
|
+
<html lang="en">
|
|
62
|
+
<head>
|
|
63
|
+
<meta charset="UTF-8">
|
|
64
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
65
|
+
<title>${statusCode} - Server Error</title>
|
|
66
|
+
<style>
|
|
67
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
68
|
+
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #1a1a2e; color: #eee; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 2rem; }
|
|
69
|
+
.container { max-width: 800px; width: 100%; }
|
|
70
|
+
h1 { color: #ff6b6b; font-size: 2.5rem; margin-bottom: 1rem; }
|
|
71
|
+
.badge { display: inline-block; background: #4ecdc4; color: #1a1a2e; padding: 0.25rem 0.5rem; border-radius: 4px; font-size: 0.75rem; font-weight: 600; margin-bottom: 1rem; }
|
|
72
|
+
.url { color: #888; font-size: 0.9rem; margin-bottom: 1.5rem; word-break: break-all; }
|
|
73
|
+
.message { background: #16213e; border-left: 4px solid #ff6b6b; padding: 1rem 1.5rem; border-radius: 0 8px 8px 0; margin-bottom: 1.5rem; }
|
|
74
|
+
.message code { color: #ff6b6b; font-size: 1.1rem; }
|
|
75
|
+
.stack { background: #0f0f23; border-radius: 8px; padding: 1.5rem; overflow-x: auto; font-family: 'Monaco', 'Menlo', monospace; font-size: 0.85rem; line-height: 1.6; color: #aaa; white-space: pre-wrap; word-break: break-word; }
|
|
76
|
+
.retry { margin-top: 2rem; }
|
|
77
|
+
.retry a { color: #4ecdc4; text-decoration: none; padding: 0.75rem 1.5rem; border: 2px solid #4ecdc4; border-radius: 6px; display: inline-block; transition: all 0.2s; }
|
|
78
|
+
.retry a:hover { background: #4ecdc4; color: #1a1a2e; }
|
|
79
|
+
.hint { margin-top: 1.5rem; color: #888; font-size: 0.9rem; }
|
|
80
|
+
</style>
|
|
81
|
+
</head>
|
|
82
|
+
<body>
|
|
83
|
+
<div class="container">
|
|
84
|
+
${badge ? `<span class="badge">${escapeHtml(badge)}</span>` : ''}
|
|
85
|
+
<h1>${statusCode} - Server Error</h1>
|
|
86
|
+
<p class="url">Error rendering: ${escapeHtml(url)}</p>
|
|
87
|
+
<div class="message">
|
|
88
|
+
<code>${escapeHtml(errorMessage)}</code>
|
|
89
|
+
</div>
|
|
90
|
+
${showStack && stack ? `<pre class="stack">${escapeHtml(stack)}</pre>` : ''}
|
|
91
|
+
<div class="retry">
|
|
92
|
+
<a href="${escapeHtml(url)}">Retry</a>
|
|
93
|
+
</div>
|
|
94
|
+
${hint ? `<p class="hint">${escapeHtml(hint)}</p>` : ''}
|
|
95
|
+
</div>
|
|
96
|
+
</body>
|
|
97
|
+
</html>`;
|
|
98
|
+
}
|
|
@@ -20,6 +20,7 @@ import WebpackDevServer from 'webpack-dev-server';
|
|
|
20
20
|
import 'cross-fetch/dist/node-polyfill.js';
|
|
21
21
|
import { createHybridRequire } from './createHybridRequire.js';
|
|
22
22
|
import { getWebpackConfig } from './getWebpackConfig.js';
|
|
23
|
+
import { getErrorStatus, renderErrorPage } from './ssrErrorHandler.js';
|
|
23
24
|
import { BoundRender } from './types.js';
|
|
24
25
|
|
|
25
26
|
// run directly from node
|
|
@@ -117,12 +118,27 @@ export default async function startDevServer(
|
|
|
117
118
|
return async function (
|
|
118
119
|
req: Request | IncomingMessage,
|
|
119
120
|
res: Response | ServerResponse,
|
|
120
|
-
|
|
121
|
+
_next: NextFunction,
|
|
121
122
|
) {
|
|
122
123
|
try {
|
|
123
124
|
return await fn(req, res);
|
|
124
|
-
} catch (
|
|
125
|
-
|
|
125
|
+
} catch (error: unknown) {
|
|
126
|
+
log.error('SSR rendering error:', error);
|
|
127
|
+
|
|
128
|
+
// Return error response with status from error if available
|
|
129
|
+
const expressRes = res as any;
|
|
130
|
+
if (!expressRes.headersSent) {
|
|
131
|
+
const statusCode = getErrorStatus(error);
|
|
132
|
+
expressRes.status(statusCode);
|
|
133
|
+
expressRes.setHeader('Content-Type', 'text/html');
|
|
134
|
+
expressRes.send(
|
|
135
|
+
renderErrorPage(error, req.url ?? '/', statusCode, {
|
|
136
|
+
showStack: true,
|
|
137
|
+
badge: 'DEV MODE',
|
|
138
|
+
hint: 'The dev server is still running. Fix the error and retry, or check the console for more details.',
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
126
142
|
}
|
|
127
143
|
};
|
|
128
144
|
}
|