@lowdefy/server 6.0.0 → 7.0.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/client/App.jsx +41 -21
- package/client/Page.jsx +101 -24
- package/client/clearReloadedForTypes.js +29 -0
- package/client/clientBuildImports.test.mjs +60 -0
- package/client/getShownPage.js +28 -0
- package/client/getShownPage.test.mjs +52 -0
- package/client/loadAllIcons.js +45 -0
- package/client/loadPageTypes.js +44 -0
- package/client/main.jsx +22 -1
- package/client/reloadedForTypesKey.js +21 -0
- package/client/shouldReloadForTypes.js +38 -0
- package/client/shouldReloadForTypes.test.mjs +63 -0
- package/client/types.js +28 -0
- package/lib/build/security.js +26 -0
- package/lib/client/auth/Auth.jsx +11 -6
- package/lib/client/auth/AuthConfigured.jsx +224 -39
- package/lib/client/auth/AuthNotConfigured.js +44 -4
- package/lib/client/auth/createFetchCheckingBuild.js +45 -0
- package/lib/client/auth/createFetchCheckingBuild.test.mjs +100 -0
- package/lib/client/bundleBuildId.js +23 -0
- package/lib/client/createLogUsage.js +46 -25
- package/lib/client/createLogUsage.test.mjs +90 -0
- package/lib/client/getCspNonce.js +25 -0
- package/lib/server/auth/createSystemContext.js +70 -0
- package/lib/server/auth/{getAuthConfig.js → getAuth.js} +15 -7
- package/lib/server/auth/getStrategies.js +40 -0
- package/lib/server/getClientAddress.js +56 -0
- package/lib/server/getClientAddress.test.mjs +108 -0
- package/lib/server/log/createHandleError.js +16 -4
- package/lib/server/log/createHandleError.test.mjs +102 -0
- package/lib/server/log/createLogger.js +10 -0
- package/lib/server/log/createLogger.test.mjs +117 -0
- package/lib/server/log/logRequest.js +12 -4
- package/lib/server/log/maskCredentialKeys.js +37 -0
- package/lib/server/log/projectErrorForLog.js +61 -0
- package/lib/server/log/redactUrlQuery.js +75 -0
- package/lib/server/log/redactUrlQuery.test.mjs +73 -0
- package/lib/server/{auth/session.js → log/serializeErrorForLog.js} +5 -11
- package/lib/server/log/serializeErrorForLog.test.mjs +337 -0
- package/lib/server/scrubSecrets.js +21 -0
- package/lib/server/sentry/captureSentryError.js +29 -3
- package/lib/server/sentry/captureSentryError.test.mjs +128 -0
- package/lib/server/sentry/initSentry.js +46 -14
- package/lib/server/sentry/initSentry.serverless.test.mjs +165 -0
- package/lib/server/sentry/initSentry.test.mjs +365 -0
- package/lib/server/sentry/scrubEvent.js +40 -0
- package/lib/server/sentry/scrubEvent.test.mjs +88 -0
- package/lib/server/sentry/sentrySdk.js +23 -0
- package/lib/server/sentry/setSentryUser.js +3 -3
- package/lowdefy/build.mjs +1 -0
- package/lowdefy/build.test.mjs +68 -0
- package/lowdefy/createCustomPluginTypesMap.mjs +3 -2
- package/package.json +47 -42
- package/package.original.json +47 -42
- package/src/app.js +74 -17
- package/src/app.test.mjs +118 -0
- package/src/html/collectChunkClosure.js +31 -0
- package/src/html/collectLazyImports.js +41 -0
- package/src/html/collectPageTypesAssets.js +57 -0
- package/src/html/collectPageTypesAssets.test.mjs +130 -0
- package/src/html/getAssets.js +20 -1
- package/src/html/getHomePath.js +29 -0
- package/src/html/getPageAssets.js +32 -0
- package/src/html/getPageAssets.test.mjs +47 -0
- package/src/html/renderPage.contentSecurityPolicy.test.mjs +134 -0
- package/src/html/renderPage.js +80 -19
- package/src/html/renderPage.test.mjs +189 -0
- package/src/html/template.js +50 -15
- package/src/html/template.test.mjs +126 -0
- package/src/index.js +28 -19
- package/src/initServer.js +61 -0
- package/src/initServer.test.mjs +116 -0
- package/src/lib/getRequestPath.js +29 -0
- package/src/middleware/apiContext.js +68 -13
- package/src/middleware/apiContext.test.mjs +178 -0
- package/src/middleware/createSameOriginGuard.js +36 -0
- package/src/middleware/createSameOriginGuard.test.mjs +102 -0
- package/src/middleware/errorHandler.js +56 -4
- package/src/middleware/errorHandler.test.mjs +440 -0
- package/src/middleware/noStore.js +27 -0
- package/src/middleware/refuseOtherBuild.js +49 -0
- package/src/middleware/refuseOtherBuild.test.mjs +63 -0
- package/src/middleware/requestTimeout.js +38 -0
- package/src/middleware/sentry.js +13 -5
- package/src/middleware/stampBuildId.js +30 -0
- package/src/middleware/stampBuildId.test.mjs +41 -0
- package/src/middleware/stampCaller.js +38 -0
- package/src/middleware/stampCaller.test.mjs +64 -0
- package/src/routes/agent.js +15 -3
- package/src/routes/apiPage.js +54 -9
- package/src/routes/apiPage.test.mjs +143 -0
- package/src/routes/auth.js +15 -9
- package/src/routes/auth.test.mjs +85 -0
- package/src/routes/clientError.js +11 -13
- package/src/routes/cron.js +3 -1
- package/src/routes/cronForward.js +4 -2
- package/src/routes/detached.js +22 -9
- package/src/routes/endpoints.js +27 -26
- package/src/routes/endpointsTextAnswer.test.mjs +77 -0
- package/src/routes/mcp.js +82 -5
- package/src/routes/mcp.test.mjs +215 -0
- package/src/routes/mcpMemberToken.test.mjs +125 -0
- package/src/routes/mcpProtectedResourceMetadata.js +34 -0
- package/src/routes/mcpProtectedResourceMetadata.test.mjs +65 -0
- package/src/routes/mountOauthDiscovery.js +44 -0
- package/src/routes/mountOauthDiscovery.test.mjs +79 -0
- package/src/routes/mountPageRoutes.js +29 -0
- package/src/routes/mountPageRoutes.test.mjs +56 -0
- package/src/routes/normalizeMcpProtocolVersionHeader.js +46 -0
- package/src/routes/normalizeMcpProtocolVersionHeader.test.mjs +62 -0
- package/src/routes/request.js +5 -3
- package/src/routes/requestBody.test.mjs +146 -0
- package/src/routes/usage.js +6 -2
- package/src/routes/user.js +27 -0
- package/src/routes/websocket.js +26 -3
- package/src/routes/websocket.test.mjs +106 -0
- package/src/routes/wellKnownFallback.js +30 -0
- package/vite.config.js +55 -2
- package/lib/server/apiWrapper.js +0 -87
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// Every JS file and stylesheet a dynamic entry needs, following its static
|
|
18
|
+
// imports - what the browser would otherwise discover one round trip at a time.
|
|
19
|
+
function collectChunkClosure({ manifest, key, js, css }) {
|
|
20
|
+
const chunk = manifest[key];
|
|
21
|
+
if (js.has(chunk.file)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
js.add(chunk.file);
|
|
25
|
+
(chunk.css ?? []).forEach((file) => css.add(file));
|
|
26
|
+
(chunk.imports ?? []).forEach((importKey) =>
|
|
27
|
+
collectChunkClosure({ manifest, key: importKey, js, css })
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default collectChunkClosure;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const LAZY_SUFFIX = '.lazy.js';
|
|
18
|
+
|
|
19
|
+
// The lazy block implementations a chunk's static closure imports dynamically.
|
|
20
|
+
// Only modules named *.lazy.js count: other import()s, such as an SDK loaded
|
|
21
|
+
// only when a feature is switched on, exist so they are not downloaded. The
|
|
22
|
+
// main entry's closure is not walked, since its dynamic imports are the page
|
|
23
|
+
// types chunks and app-wide optional code, never one page's lazy blocks.
|
|
24
|
+
function collectLazyImports({ manifest, key, entryFiles, visited, lazyKeys }) {
|
|
25
|
+
if (visited.has(key)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
visited.add(key);
|
|
29
|
+
const chunk = manifest[key];
|
|
30
|
+
if (entryFiles.has(chunk.file)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
(chunk.dynamicImports ?? [])
|
|
34
|
+
.filter((importKey) => importKey.endsWith(LAZY_SUFFIX))
|
|
35
|
+
.forEach((importKey) => lazyKeys.add(importKey));
|
|
36
|
+
(chunk.imports ?? []).forEach((importKey) =>
|
|
37
|
+
collectLazyImports({ manifest, key: importKey, entryFiles, visited, lazyKeys })
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default collectLazyImports;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import collectChunkClosure from './collectChunkClosure.js';
|
|
18
|
+
import collectLazyImports from './collectLazyImports.js';
|
|
19
|
+
|
|
20
|
+
const PAGE_TYPES_PREFIX = 'build/plugins/pageTypes/';
|
|
21
|
+
|
|
22
|
+
// Preloads per page types key: the key's chunk (its types and icons) and its
|
|
23
|
+
// imports, less what the main entry already loads. Prefetches: the lazy
|
|
24
|
+
// block implementations the key's chunks import on mount, with their imports,
|
|
25
|
+
// less anything already preloaded or in the main entry. Prefetch is low
|
|
26
|
+
// priority, so a lazy block that mounts at first paint finds its code in the
|
|
27
|
+
// cache without delaying the page's own chunks.
|
|
28
|
+
function collectPageTypesAssets({ manifest, entryFiles }) {
|
|
29
|
+
const pageTypes = {};
|
|
30
|
+
Object.keys(manifest)
|
|
31
|
+
.filter((key) => key.startsWith(PAGE_TYPES_PREFIX))
|
|
32
|
+
.forEach((key) => {
|
|
33
|
+
const typesKey = key.slice(PAGE_TYPES_PREFIX.length).replace(/\.js$/, '');
|
|
34
|
+
const js = new Set();
|
|
35
|
+
const css = new Set();
|
|
36
|
+
collectChunkClosure({ manifest, key, js, css });
|
|
37
|
+
|
|
38
|
+
const lazyKeys = new Set();
|
|
39
|
+
collectLazyImports({ manifest, key, entryFiles, visited: new Set(), lazyKeys });
|
|
40
|
+
const prefetchJs = new Set();
|
|
41
|
+
const prefetchCss = new Set();
|
|
42
|
+
lazyKeys.forEach((lazyKey) =>
|
|
43
|
+
collectChunkClosure({ manifest, key: lazyKey, js: prefetchJs, css: prefetchCss })
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
pageTypes[typesKey] = {
|
|
47
|
+
js: [...js].filter((file) => !entryFiles.has(file)),
|
|
48
|
+
css: [...css].filter((file) => !entryFiles.has(file)),
|
|
49
|
+
prefetch: [...prefetchJs, ...prefetchCss].filter(
|
|
50
|
+
(file) => !entryFiles.has(file) && !js.has(file) && !css.has(file)
|
|
51
|
+
),
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
return pageTypes;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default collectPageTypesAssets;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import collectPageTypesAssets from './collectPageTypesAssets.js';
|
|
18
|
+
|
|
19
|
+
const manifest = {
|
|
20
|
+
'client/main.jsx': { file: 'assets/main.js', css: ['assets/main.css'], imports: ['_react.js'] },
|
|
21
|
+
'_react.js': { file: 'assets/react.js' },
|
|
22
|
+
'build/plugins/icons.js': { file: 'assets/icons.js', imports: ['_ai.js', 'client/main.jsx'] },
|
|
23
|
+
'_ai.js': { file: 'assets/ai.js', imports: ['client/main.jsx'] },
|
|
24
|
+
'build/plugins/pageTypes/aaaaaaaaaaaa.js': {
|
|
25
|
+
file: 'assets/aaaa.js',
|
|
26
|
+
imports: ['_antd.js', 'client/main.jsx'],
|
|
27
|
+
},
|
|
28
|
+
'_antd.js': { file: 'assets/antd.js', css: ['assets/antd.css'], imports: ['_react.js'] },
|
|
29
|
+
'build/plugins/pageTypes/bbbbbbbbbbbb.js': {
|
|
30
|
+
file: 'assets/bbbb.js',
|
|
31
|
+
imports: ['client/main.jsx'],
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const entryFiles = new Set(['assets/main.js', 'assets/main.css', 'assets/react.js']);
|
|
36
|
+
|
|
37
|
+
test('collectPageTypesAssets preloads each key chunk and every import, less main and the app-wide icons', () => {
|
|
38
|
+
expect(collectPageTypesAssets({ manifest, entryFiles })).toEqual({
|
|
39
|
+
aaaaaaaaaaaa: {
|
|
40
|
+
js: ['assets/aaaa.js', 'assets/antd.js'],
|
|
41
|
+
css: ['assets/antd.css'],
|
|
42
|
+
prefetch: [],
|
|
43
|
+
},
|
|
44
|
+
bbbbbbbbbbbb: {
|
|
45
|
+
js: ['assets/bbbb.js'],
|
|
46
|
+
css: [],
|
|
47
|
+
prefetch: [],
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// A page whose chunks import a lazy block implementation, an optional SDK, and
|
|
53
|
+
// a nested lazy import inside the implementation.
|
|
54
|
+
const lazyManifest = {
|
|
55
|
+
'client/main.jsx': {
|
|
56
|
+
file: 'assets/main.js',
|
|
57
|
+
css: ['assets/main.css'],
|
|
58
|
+
imports: ['_react.js'],
|
|
59
|
+
dynamicImports: [
|
|
60
|
+
'build/plugins/pageTypes/cccccccccccc.js',
|
|
61
|
+
'build/plugins/pageTypes/dddddddddddd.js',
|
|
62
|
+
'node_modules/app-wide/Everywhere.lazy.js',
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
'_react.js': { file: 'assets/react.js' },
|
|
66
|
+
'build/plugins/icons.js': { file: 'assets/icons.js', imports: ['client/main.jsx'] },
|
|
67
|
+
'node_modules/app-wide/Everywhere.lazy.js': {
|
|
68
|
+
file: 'assets/Everywhere.lazy.js',
|
|
69
|
+
isDynamicEntry: true,
|
|
70
|
+
},
|
|
71
|
+
'build/plugins/pageTypes/cccccccccccc.js': {
|
|
72
|
+
file: 'assets/cccc.js',
|
|
73
|
+
imports: ['_blocks.js', 'client/main.jsx'],
|
|
74
|
+
},
|
|
75
|
+
'_blocks.js': {
|
|
76
|
+
file: 'assets/blocks.js',
|
|
77
|
+
imports: ['_antd.js', '_react.js'],
|
|
78
|
+
dynamicImports: [
|
|
79
|
+
'plugins/blocks-antd-x/dist/blocks/AgentChat/AgentChat.lazy.js',
|
|
80
|
+
'node_modules/posthog-js/dist/module.js',
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
'_antd.js': { file: 'assets/antd.js', css: ['assets/antd.css'], imports: ['_react.js'] },
|
|
84
|
+
'plugins/blocks-antd-x/dist/blocks/AgentChat/AgentChat.lazy.js': {
|
|
85
|
+
file: 'assets/AgentChat.lazy.js',
|
|
86
|
+
css: ['assets/AgentChat.css'],
|
|
87
|
+
isDynamicEntry: true,
|
|
88
|
+
imports: ['_x.js', '_antd.js', '_react.js'],
|
|
89
|
+
dynamicImports: ['node_modules/@ant-design/x/es/mermaid/index.js'],
|
|
90
|
+
},
|
|
91
|
+
'_x.js': { file: 'assets/x.js', imports: ['_markdown.js'] },
|
|
92
|
+
'_markdown.js': { file: 'assets/markdown.js', css: ['assets/markdown.css'] },
|
|
93
|
+
'node_modules/@ant-design/x/es/mermaid/index.js': {
|
|
94
|
+
file: 'assets/mermaid.js',
|
|
95
|
+
isDynamicEntry: true,
|
|
96
|
+
},
|
|
97
|
+
'node_modules/posthog-js/dist/module.js': { file: 'assets/posthog.js', isDynamicEntry: true },
|
|
98
|
+
'build/plugins/pageTypes/dddddddddddd.js': {
|
|
99
|
+
file: 'assets/dddd.js',
|
|
100
|
+
imports: ['client/main.jsx'],
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
test('collectPageTypesAssets prefetches the lazy block implementations a page imports, with their imports', () => {
|
|
105
|
+
const pageTypes = collectPageTypesAssets({ manifest: lazyManifest, entryFiles });
|
|
106
|
+
expect(pageTypes.cccccccccccc).toEqual({
|
|
107
|
+
js: ['assets/cccc.js', 'assets/blocks.js', 'assets/antd.js'],
|
|
108
|
+
css: ['assets/antd.css'],
|
|
109
|
+
prefetch: [
|
|
110
|
+
'assets/AgentChat.lazy.js',
|
|
111
|
+
'assets/x.js',
|
|
112
|
+
'assets/markdown.js',
|
|
113
|
+
'assets/AgentChat.css',
|
|
114
|
+
'assets/markdown.css',
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('collectPageTypesAssets does not prefetch dynamic imports that are not *.lazy.js', () => {
|
|
120
|
+
const { prefetch } = collectPageTypesAssets({ manifest: lazyManifest, entryFiles }).cccccccccccc;
|
|
121
|
+
expect(prefetch).not.toContain('assets/posthog.js');
|
|
122
|
+
// Imported only once the implementation runs, so it stays on demand.
|
|
123
|
+
expect(prefetch).not.toContain('assets/mermaid.js');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('collectPageTypesAssets does not prefetch lazy imports of the main entry or other page types', () => {
|
|
127
|
+
const pageTypes = collectPageTypesAssets({ manifest: lazyManifest, entryFiles });
|
|
128
|
+
expect(pageTypes.dddddddddddd.prefetch).toEqual([]);
|
|
129
|
+
expect(pageTypes.cccccccccccc.prefetch).not.toContain('assets/Everywhere.lazy.js');
|
|
130
|
+
});
|
package/src/html/getAssets.js
CHANGED
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
import fs from 'node:fs';
|
|
18
18
|
import path from 'node:path';
|
|
19
19
|
|
|
20
|
+
import { LowdefyInternalError } from '@lowdefy/errors';
|
|
21
|
+
|
|
22
|
+
import collectChunkClosure from './collectChunkClosure.js';
|
|
23
|
+
import collectPageTypesAssets from './collectPageTypesAssets.js';
|
|
24
|
+
|
|
20
25
|
// The Vite manifest is read once at startup — deploys start a new server
|
|
21
26
|
// process after `vite build` completes, so assets never change at runtime.
|
|
22
27
|
// In-place deploys must build before restarting (documented).
|
|
@@ -30,14 +35,28 @@ function getAssets() {
|
|
|
30
35
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
31
36
|
const entry = manifest['client/main.jsx'];
|
|
32
37
|
if (!entry) {
|
|
33
|
-
throw new
|
|
38
|
+
throw new LowdefyInternalError(
|
|
34
39
|
'Vite manifest has no "client/main.jsx" entry. Run the client build before starting the server.'
|
|
35
40
|
);
|
|
36
41
|
}
|
|
42
|
+
const entryJs = new Set();
|
|
43
|
+
const entryCss = new Set();
|
|
44
|
+
collectChunkClosure({ manifest, key: 'client/main.jsx', js: entryJs, css: entryCss });
|
|
45
|
+
const entryFiles = new Set([...entryJs, ...entryCss]);
|
|
46
|
+
// Every icon the app bundles: preloaded only for a page whose Dynamic
|
|
47
|
+
// content loads them all before its first render.
|
|
48
|
+
const iconsJs = new Set();
|
|
49
|
+
const iconsCss = new Set();
|
|
50
|
+
collectChunkClosure({ manifest, key: 'build/plugins/icons.js', js: iconsJs, css: iconsCss });
|
|
37
51
|
assets = {
|
|
38
52
|
js: entry.file,
|
|
39
53
|
css: entry.css ?? [],
|
|
54
|
+
icons: {
|
|
55
|
+
js: [...iconsJs].filter((file) => !entryFiles.has(file)),
|
|
56
|
+
css: [...iconsCss].filter((file) => !entryFiles.has(file)),
|
|
57
|
+
},
|
|
40
58
|
imports: (entry.imports ?? []).map((key) => manifest[key]?.file).filter(Boolean),
|
|
59
|
+
pageTypes: collectPageTypesAssets({ manifest, entryFiles }),
|
|
41
60
|
};
|
|
42
61
|
return assets;
|
|
43
62
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { buildPagePath } from '@lowdefy/helpers';
|
|
18
|
+
|
|
19
|
+
// The path (no leading "/", no basePath) of the home page: the configured
|
|
20
|
+
// homePageId, or the first menu link the user may see with its pathParams.
|
|
21
|
+
// The pattern comes from the route table, which lists every page whatever the
|
|
22
|
+
// caller may open.
|
|
23
|
+
async function getHomePath({ context, home }) {
|
|
24
|
+
const routes = await context.readConfigFile('routes.json');
|
|
25
|
+
const route = routes.find(({ pageId }) => pageId === home.pageId);
|
|
26
|
+
return buildPagePath({ pageId: home.pageId, path: route?.path, pathParams: home.pathParams });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default getHomePath;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// The chunks a page's first render waits for: its types key's chunk and
|
|
18
|
+
// imports, plus every icon when Dynamic content needs icons outside the
|
|
19
|
+
// page's set (loadPageTypes loads them before rendering).
|
|
20
|
+
function getPageAssets({ assets, pageConfig }) {
|
|
21
|
+
const pageAssets = assets.pageTypes[pageConfig.typesKey];
|
|
22
|
+
if (pageConfig.loadAllTypes !== true && pageConfig.loadAllIcons !== true) {
|
|
23
|
+
return pageAssets;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
...pageAssets,
|
|
27
|
+
js: [...new Set([...pageAssets.js, ...assets.icons.js])],
|
|
28
|
+
css: [...new Set([...pageAssets.css, ...assets.icons.css])],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default getPageAssets;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import getPageAssets from './getPageAssets.js';
|
|
18
|
+
|
|
19
|
+
const assets = {
|
|
20
|
+
icons: { js: ['assets/icons.js', 'assets/shared.js'], css: [] },
|
|
21
|
+
pageTypes: {
|
|
22
|
+
aaaaaaaaaaaa: {
|
|
23
|
+
js: ['assets/aaaa.js', 'assets/shared.js'],
|
|
24
|
+
css: ['assets/a.css'],
|
|
25
|
+
prefetch: [],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
test('getPageAssets preloads only the key chunks for a page that needs only its own icons', () => {
|
|
31
|
+
expect(getPageAssets({ assets, pageConfig: { typesKey: 'aaaaaaaaaaaa' } })).toBe(
|
|
32
|
+
assets.pageTypes.aaaaaaaaaaaa
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test.each([['loadAllIcons'], ['loadAllTypes']])(
|
|
37
|
+
'getPageAssets also preloads every icon for a page flagged %s',
|
|
38
|
+
(flag) => {
|
|
39
|
+
expect(
|
|
40
|
+
getPageAssets({ assets, pageConfig: { typesKey: 'aaaaaaaaaaaa', [flag]: true } })
|
|
41
|
+
).toEqual({
|
|
42
|
+
js: ['assets/aaaa.js', 'assets/shared.js', 'assets/icons.js'],
|
|
43
|
+
css: ['assets/a.css'],
|
|
44
|
+
prefetch: [],
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
);
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Hono } from 'hono';
|
|
18
|
+
import { jest } from '@jest/globals';
|
|
19
|
+
|
|
20
|
+
const { default: getPageConfig } = await import('@lowdefy/api/routes/page/getPageConfig.js');
|
|
21
|
+
const { default: createContentSecurityPolicy } = await import(
|
|
22
|
+
'@lowdefy/api/csp/createContentSecurityPolicy.js'
|
|
23
|
+
);
|
|
24
|
+
const { default: addNonceToElements } = await import('@lowdefy/api/csp/addNonceToElements.js');
|
|
25
|
+
|
|
26
|
+
const security = { contentSecurityPolicy: null };
|
|
27
|
+
|
|
28
|
+
jest.unstable_mockModule('@lowdefy/api', () => ({
|
|
29
|
+
addNonceToElements,
|
|
30
|
+
createContentSecurityPolicy,
|
|
31
|
+
getPageConfig,
|
|
32
|
+
getRootConfig: async () => ({ home: { configured: true, pageId: 'home', pathParams: {} } }),
|
|
33
|
+
}));
|
|
34
|
+
jest.unstable_mockModule('../../lib/build/app.js', () => ({
|
|
35
|
+
default: {
|
|
36
|
+
html: {
|
|
37
|
+
appendHead: '<script src="https://cdn.example.com/a.js"></script>',
|
|
38
|
+
appendBody: '<script>window.b = 1;</script>',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}));
|
|
42
|
+
jest.unstable_mockModule('../../lib/build/auth.js', () => ({
|
|
43
|
+
default: { authPages: { signIn: '/auth/login' } },
|
|
44
|
+
}));
|
|
45
|
+
jest.unstable_mockModule('../../lib/build/config.js', () => ({ default: { basePath: '' } }));
|
|
46
|
+
jest.unstable_mockModule('../../lib/build/security.js', () => ({ default: security }));
|
|
47
|
+
jest.unstable_mockModule('../../lib/build/theme.js', () => ({ default: {} }));
|
|
48
|
+
jest.unstable_mockModule('./getAssets.js', () => ({
|
|
49
|
+
default: () => ({ js: 'assets/main.js', css: [], imports: [] }),
|
|
50
|
+
}));
|
|
51
|
+
jest.unstable_mockModule('./getPageAssets.js', () => ({
|
|
52
|
+
default: () => ({ js: [], css: [], prefetch: [] }),
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
const { default: renderPage } = await import('./renderPage.js');
|
|
56
|
+
const { default: mountPageRoutes } = await import('../routes/mountPageRoutes.js');
|
|
57
|
+
|
|
58
|
+
const policy = {
|
|
59
|
+
reportOnly: false,
|
|
60
|
+
directives: {
|
|
61
|
+
'default-src': ["'self'"],
|
|
62
|
+
'script-src': ["'self'"],
|
|
63
|
+
'style-src': ["'self'"],
|
|
64
|
+
'connect-src': ["'self'"],
|
|
65
|
+
},
|
|
66
|
+
nonceDirectives: ['script-src', 'style-src'],
|
|
67
|
+
websockets: true,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
function createApp() {
|
|
71
|
+
const app = new Hono();
|
|
72
|
+
app.use('*', async (c, next) => {
|
|
73
|
+
c.set('lowdefyContext', {
|
|
74
|
+
authEnforcement: { pagesProtectedByDefault: false },
|
|
75
|
+
authorizeOutcome: () => 'allow',
|
|
76
|
+
logger: { info: jest.fn() },
|
|
77
|
+
readConfigFile: async (file) => {
|
|
78
|
+
if (file === 'routes.json') return [{ pageId: 'home', path: 'home' }];
|
|
79
|
+
if (file === 'pages/home.json') return { id: 'home' };
|
|
80
|
+
return null;
|
|
81
|
+
},
|
|
82
|
+
user: null,
|
|
83
|
+
});
|
|
84
|
+
await next();
|
|
85
|
+
});
|
|
86
|
+
mountPageRoutes({ app, basePath: '', renderPage });
|
|
87
|
+
return app;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getHeaderNonce(header) {
|
|
91
|
+
return header.match(/'nonce-([^']+)'/)[1];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
afterEach(() => {
|
|
95
|
+
security.contentSecurityPolicy = null;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('renderPage sends no Content-Security-Policy and writes no nonce when the policy is off', async () => {
|
|
99
|
+
const res = await createApp().request('https://app.example.com/home');
|
|
100
|
+
expect(res.headers.get('content-security-policy')).toBeNull();
|
|
101
|
+
expect(res.headers.get('content-security-policy-report-only')).toBeNull();
|
|
102
|
+
expect(await res.text()).not.toContain(' nonce="');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('renderPage puts the header nonce on every script element of the page', async () => {
|
|
106
|
+
security.contentSecurityPolicy = policy;
|
|
107
|
+
const res = await createApp().request('https://app.example.com/home');
|
|
108
|
+
const header = res.headers.get('content-security-policy');
|
|
109
|
+
const nonce = getHeaderNonce(header);
|
|
110
|
+
expect(header).toEqual(
|
|
111
|
+
`default-src 'self'; script-src 'self' 'nonce-${nonce}'; style-src 'self' 'nonce-${nonce}'; connect-src 'self' wss://app.example.com`
|
|
112
|
+
);
|
|
113
|
+
const scripts = (await res.text()).match(/<script\b[^>]*>/g);
|
|
114
|
+
expect(scripts.length).toEqual(6);
|
|
115
|
+
scripts.forEach((tag) => expect(tag).toContain(` nonce="${nonce}"`));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('renderPage draws a different nonce for each render', async () => {
|
|
119
|
+
security.contentSecurityPolicy = policy;
|
|
120
|
+
const app = createApp();
|
|
121
|
+
const first = await app.request('https://app.example.com/home');
|
|
122
|
+
const second = await app.request('https://app.example.com/home');
|
|
123
|
+
const firstNonce = getHeaderNonce(first.headers.get('content-security-policy'));
|
|
124
|
+
const secondNonce = getHeaderNonce(second.headers.get('content-security-policy'));
|
|
125
|
+
expect(firstNonce).not.toEqual(secondNonce);
|
|
126
|
+
expect(await second.text()).toContain(`nonce="${secondNonce}"`);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('renderPage sends the report-only header when the policy is report only', async () => {
|
|
130
|
+
security.contentSecurityPolicy = { ...policy, reportOnly: true };
|
|
131
|
+
const res = await createApp().request('https://app.example.com/home');
|
|
132
|
+
expect(res.headers.get('content-security-policy')).toBeNull();
|
|
133
|
+
expect(res.headers.get('content-security-policy-report-only')).toContain("default-src 'self'");
|
|
134
|
+
});
|