@lowdefy/server-dev 0.0.0-experimental-20260203215128 → 0.0.0-experimental-20260203220133
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/lib/client/App.js +2 -2
- package/lib/client/BuildErrorPage.js +99 -0
- package/lib/client/Page.js +17 -1
- package/lib/client/utils/useMutateCache.js +15 -2
- package/lib/client/utils/usePageConfig.js +49 -4
- package/lib/server/apiWrapper.js +30 -2
- package/lib/server/jitPageBuilder.js +139 -0
- package/lib/server/pageCache.mjs +104 -0
- package/lib/server/pageCache.test.mjs +182 -0
- package/manager/getContext.mjs +19 -1
- package/manager/processes/lowdefyBuild.mjs +24 -10
- package/manager/processes/nextBuild.mjs +20 -5
- package/manager/processes/startServer.mjs +15 -1
- package/manager/run.mjs +3 -7
- package/manager/utils/checkPortAvailable.mjs +40 -0
- package/manager/watchers/lowdefyBuildWatcher.mjs +40 -11
- package/package.json +31 -31
- package/package.original.json +31 -31
- package/pages/api/js/[env].js +42 -0
- package/pages/api/page/[pageId].js +31 -1
package/lib/client/App.js
CHANGED
|
@@ -30,7 +30,7 @@ import actions from '../../build/plugins/actions.js';
|
|
|
30
30
|
import blocks from '../../build/plugins/blocks.js';
|
|
31
31
|
import icons from '../../build/plugins/icons.js';
|
|
32
32
|
import operators from '../../build/plugins/operators/client.js';
|
|
33
|
-
import
|
|
33
|
+
import staticJsMap from '../../build/plugins/operators/clientJsMap.js';
|
|
34
34
|
|
|
35
35
|
const App = ({ auth, lowdefy }) => {
|
|
36
36
|
const router = useRouter();
|
|
@@ -50,7 +50,7 @@ const App = ({ auth, lowdefy }) => {
|
|
|
50
50
|
config={{
|
|
51
51
|
rootConfig,
|
|
52
52
|
}}
|
|
53
|
-
jsMap={
|
|
53
|
+
jsMap={staticJsMap}
|
|
54
54
|
lowdefy={lowdefy}
|
|
55
55
|
pageId={pageId}
|
|
56
56
|
resetContext={resetContext}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2024 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 React from 'react';
|
|
18
|
+
|
|
19
|
+
const typeColors = {
|
|
20
|
+
ConfigError: '#cf1322',
|
|
21
|
+
ConfigWarning: '#d48806',
|
|
22
|
+
PluginError: '#531dab',
|
|
23
|
+
ServiceError: '#096dd9',
|
|
24
|
+
LowdefyError: '#cf1322',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function getTypeColor(type) {
|
|
28
|
+
return typeColors[type] ?? '#595959';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ErrorItem = ({ type, message, source }) => {
|
|
32
|
+
const color = getTypeColor(type);
|
|
33
|
+
return (
|
|
34
|
+
<div
|
|
35
|
+
style={{
|
|
36
|
+
borderLeft: `3px solid ${color}`,
|
|
37
|
+
paddingLeft: 12,
|
|
38
|
+
marginBottom: 16,
|
|
39
|
+
}}
|
|
40
|
+
>
|
|
41
|
+
<span
|
|
42
|
+
style={{
|
|
43
|
+
fontSize: 11,
|
|
44
|
+
fontWeight: 600,
|
|
45
|
+
color,
|
|
46
|
+
textTransform: 'uppercase',
|
|
47
|
+
letterSpacing: '0.05em',
|
|
48
|
+
}}
|
|
49
|
+
>
|
|
50
|
+
{type}
|
|
51
|
+
</span>
|
|
52
|
+
<p style={{ fontSize: 14, margin: '4px 0' }}>{message}</p>
|
|
53
|
+
{source && (
|
|
54
|
+
<p style={{ fontSize: 13, color: '#8c8c8c', margin: 0 }}>{source}</p>
|
|
55
|
+
)}
|
|
56
|
+
</div>
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const BuildErrorPage = ({ errors, message, source }) => {
|
|
61
|
+
// Support both new errors array and legacy single message/source props
|
|
62
|
+
const errorList = errors ?? (message ? [{ type: 'Error', message, source }] : []);
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<div
|
|
66
|
+
style={{
|
|
67
|
+
height: '100vh',
|
|
68
|
+
display: 'flex',
|
|
69
|
+
flexDirection: 'column',
|
|
70
|
+
alignItems: 'center',
|
|
71
|
+
justifyContent: 'center',
|
|
72
|
+
fontFamily: 'monospace',
|
|
73
|
+
padding: '0 24px',
|
|
74
|
+
}}
|
|
75
|
+
>
|
|
76
|
+
<div style={{ maxWidth: 720, width: '100%' }}>
|
|
77
|
+
<h2 style={{ color: '#cf1322', marginBottom: 16 }}>
|
|
78
|
+
{errorList.length > 1
|
|
79
|
+
? `Build Errors (${errorList.length})`
|
|
80
|
+
: 'Build Error'}
|
|
81
|
+
</h2>
|
|
82
|
+
{errorList.map((err, i) => (
|
|
83
|
+
<ErrorItem
|
|
84
|
+
key={i}
|
|
85
|
+
type={err.type}
|
|
86
|
+
message={err.message}
|
|
87
|
+
source={err.source}
|
|
88
|
+
/>
|
|
89
|
+
))}
|
|
90
|
+
<p style={{ fontSize: 13, color: '#8c8c8c', marginTop: 24 }}>
|
|
91
|
+
Fix the error{errorList.length > 1 ? 's' : ''} in your config and the page will rebuild
|
|
92
|
+
automatically.
|
|
93
|
+
</p>
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export default BuildErrorPage;
|
package/lib/client/Page.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import React from 'react';
|
|
18
18
|
import Client from '@lowdefy/client';
|
|
19
19
|
|
|
20
|
+
import BuildErrorPage from './BuildErrorPage.js';
|
|
20
21
|
import RestartingPage from './RestartingPage.js';
|
|
21
22
|
import usePageConfig from './utils/usePageConfig.js';
|
|
22
23
|
|
|
@@ -37,9 +38,24 @@ const Page = ({
|
|
|
37
38
|
router.replace(`/404`);
|
|
38
39
|
return '';
|
|
39
40
|
}
|
|
41
|
+
if (pageConfig.buildError) {
|
|
42
|
+
return (
|
|
43
|
+
<BuildErrorPage
|
|
44
|
+
errors={pageConfig.errors}
|
|
45
|
+
message={pageConfig.message}
|
|
46
|
+
source={pageConfig.source}
|
|
47
|
+
/>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
40
50
|
if (resetContext.restarting) {
|
|
41
51
|
return <RestartingPage />;
|
|
42
52
|
}
|
|
53
|
+
|
|
54
|
+
// Merge dynamic JS entries fetched after JIT build with the static jsMap
|
|
55
|
+
const mergedJsMap = pageConfig._jsEntries
|
|
56
|
+
? { ...jsMap, ...pageConfig._jsEntries }
|
|
57
|
+
: jsMap;
|
|
58
|
+
|
|
43
59
|
return (
|
|
44
60
|
<Client
|
|
45
61
|
auth={auth}
|
|
@@ -48,7 +64,7 @@ const Page = ({
|
|
|
48
64
|
...config,
|
|
49
65
|
pageConfig,
|
|
50
66
|
}}
|
|
51
|
-
jsMap={
|
|
67
|
+
jsMap={mergedJsMap}
|
|
52
68
|
lowdefy={lowdefy}
|
|
53
69
|
resetContext={resetContext}
|
|
54
70
|
router={router}
|
|
@@ -16,10 +16,23 @@
|
|
|
16
16
|
|
|
17
17
|
import { useSWRConfig } from 'swr';
|
|
18
18
|
|
|
19
|
+
let reloadVersion = 0;
|
|
20
|
+
|
|
21
|
+
function getReloadVersion() {
|
|
22
|
+
return reloadVersion;
|
|
23
|
+
}
|
|
24
|
+
|
|
19
25
|
function useMutateCache(basePath) {
|
|
20
26
|
const { mutate } = useSWRConfig();
|
|
21
|
-
return () =>
|
|
22
|
-
|
|
27
|
+
return () => {
|
|
28
|
+
// Increment the reload version so that usePageConfig SWR keys change.
|
|
29
|
+
// Old cached page entries become orphaned (different key), forcing fresh
|
|
30
|
+
// fetches when navigating to any page after a reload.
|
|
31
|
+
reloadVersion += 1;
|
|
32
|
+
// Revalidate root config in the background (stale-while-revalidate, no suspend).
|
|
33
|
+
return mutate((key) => key === `${basePath}/api/root`);
|
|
34
|
+
};
|
|
23
35
|
}
|
|
24
36
|
|
|
37
|
+
export { getReloadVersion };
|
|
25
38
|
export default useMutateCache;
|
|
@@ -13,14 +13,59 @@
|
|
|
13
13
|
|
|
14
14
|
import useSWR from 'swr';
|
|
15
15
|
|
|
16
|
-
import
|
|
16
|
+
import { getReloadVersion } from './useMutateCache.js';
|
|
17
17
|
|
|
18
|
-
function
|
|
19
|
-
|
|
18
|
+
function parseJsModule(text) {
|
|
19
|
+
const fn = new Function('exports', text.replace('export default', 'exports.default ='));
|
|
20
|
+
const mod = {};
|
|
21
|
+
fn(mod);
|
|
22
|
+
return mod.default ?? {};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function fetchJsEntries(basePath) {
|
|
26
|
+
try {
|
|
27
|
+
const res = await fetch(`${basePath}/api/js/client`);
|
|
28
|
+
if (!res.ok) return {};
|
|
29
|
+
return parseJsModule(await res.text());
|
|
30
|
+
} catch {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function fetchPageConfig(url) {
|
|
36
|
+
const res = await fetch(url, {
|
|
37
|
+
headers: { 'Content-Type': 'application/json' },
|
|
38
|
+
});
|
|
39
|
+
if (res.status === 404) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const data = await res.json();
|
|
43
|
+
if (data?.buildError) {
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
if (!res.ok) {
|
|
47
|
+
throw new Error(data.message || 'Request error');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Fetch jsMap after page build completes (JIT build may have added new entries).
|
|
51
|
+
// Extract basePath from the URL to construct the jsMap endpoint.
|
|
52
|
+
const basePath = url.replace(/\/api\/page\/.*$/, '');
|
|
53
|
+
const jsEntries = await fetchJsEntries(basePath);
|
|
54
|
+
data._jsEntries = jsEntries;
|
|
55
|
+
|
|
56
|
+
return data;
|
|
20
57
|
}
|
|
21
58
|
|
|
22
59
|
function usePageConfig(pageId, basePath) {
|
|
23
|
-
const
|
|
60
|
+
const url = `${basePath}/api/page/${pageId}`;
|
|
61
|
+
// Include reloadVersion in the SWR key so that after a config reload,
|
|
62
|
+
// previously cached page data is not reused. The fetcher receives
|
|
63
|
+
// [url, version] but only uses url — the version just busts the cache.
|
|
64
|
+
const { data } = useSWR(
|
|
65
|
+
[url, getReloadVersion()],
|
|
66
|
+
([fetchUrl]) => fetchPageConfig(fetchUrl),
|
|
67
|
+
{ suspense: true }
|
|
68
|
+
);
|
|
24
69
|
return { data };
|
|
25
70
|
}
|
|
26
71
|
|
package/lib/server/apiWrapper.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
import fs from 'fs';
|
|
16
17
|
import path from 'path';
|
|
17
18
|
import { createApiContext } from '@lowdefy/api';
|
|
18
19
|
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
@@ -26,7 +27,7 @@ import getServerSession from './auth/getServerSession.js';
|
|
|
26
27
|
import logError from './log/logError.js';
|
|
27
28
|
import logRequest from './log/logRequest.js';
|
|
28
29
|
import operators from '../../build/plugins/operators/server.js';
|
|
29
|
-
import
|
|
30
|
+
import staticJsMap from '../../build/plugins/operators/serverJsMap.js';
|
|
30
31
|
import getAuthOptions from './auth/getAuthOptions.js';
|
|
31
32
|
import setSentryUser from './sentry/setSentryUser.js';
|
|
32
33
|
|
|
@@ -39,12 +40,39 @@ try {
|
|
|
39
40
|
|
|
40
41
|
const secrets = getSecretsFromEnv();
|
|
41
42
|
|
|
43
|
+
// Dynamic JS map loading for JIT-built pages
|
|
44
|
+
let cachedJsMapMtime = null;
|
|
45
|
+
let cachedJsMap = staticJsMap;
|
|
46
|
+
|
|
47
|
+
function loadDynamicJsMap(buildDirectory) {
|
|
48
|
+
const jsMapPath = path.join(buildDirectory, 'plugins', 'operators', 'serverJsMap.js');
|
|
49
|
+
try {
|
|
50
|
+
const stat = fs.statSync(jsMapPath);
|
|
51
|
+
if (cachedJsMapMtime && stat.mtimeMs === cachedJsMapMtime) {
|
|
52
|
+
return cachedJsMap;
|
|
53
|
+
}
|
|
54
|
+
cachedJsMapMtime = stat.mtimeMs;
|
|
55
|
+
// For server-side, we can read and eval the JS file
|
|
56
|
+
const content = fs.readFileSync(jsMapPath, 'utf8');
|
|
57
|
+
const fn = new Function('exports', content.replace('export default', 'exports.default ='));
|
|
58
|
+
const exports = {};
|
|
59
|
+
fn(exports);
|
|
60
|
+
cachedJsMap = { ...staticJsMap, ...(exports.default ?? {}) };
|
|
61
|
+
return cachedJsMap;
|
|
62
|
+
} catch {
|
|
63
|
+
return cachedJsMap;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
42
67
|
function apiWrapper(handler) {
|
|
43
68
|
return async function wrappedHandler(req, res) {
|
|
69
|
+
const buildDirectory = path.join(process.cwd(), 'build');
|
|
70
|
+
const jsMap = loadDynamicJsMap(buildDirectory);
|
|
71
|
+
|
|
44
72
|
const context = {
|
|
45
73
|
// Important to give absolute path so Next can trace build files
|
|
46
74
|
rid: uuid(),
|
|
47
|
-
buildDirectory
|
|
75
|
+
buildDirectory,
|
|
48
76
|
configDirectory: process.env.LOWDEFY_DIRECTORY_CONFIG || process.cwd(),
|
|
49
77
|
config,
|
|
50
78
|
connections,
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2024 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 fs from 'fs';
|
|
18
|
+
import path from 'path';
|
|
19
|
+
import { serializer } from '@lowdefy/helpers';
|
|
20
|
+
import { buildPageJit, createContext } from '@lowdefy/build/dev';
|
|
21
|
+
|
|
22
|
+
import createLogger from './log/createLogger.js';
|
|
23
|
+
import PageCache from './pageCache.mjs';
|
|
24
|
+
|
|
25
|
+
const jitLogger = createLogger({ component: 'jit-build' });
|
|
26
|
+
|
|
27
|
+
const pageCache = new PageCache();
|
|
28
|
+
let cachedRegistryMtime = null;
|
|
29
|
+
let cachedRegistry = null;
|
|
30
|
+
let cachedBuildContext = null;
|
|
31
|
+
let lastInvalidationMtime = null;
|
|
32
|
+
|
|
33
|
+
function readJsonFile(filePath) {
|
|
34
|
+
try {
|
|
35
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
36
|
+
return serializer.deserialize(JSON.parse(content));
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function checkPageInvalidations(buildDirectory) {
|
|
43
|
+
const invalidationPath = path.join(buildDirectory, 'invalidatePages.json');
|
|
44
|
+
try {
|
|
45
|
+
const stat = fs.statSync(invalidationPath);
|
|
46
|
+
if (lastInvalidationMtime && stat.mtimeMs === lastInvalidationMtime) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
lastInvalidationMtime = stat.mtimeMs;
|
|
50
|
+
const content = fs.readFileSync(invalidationPath, 'utf8');
|
|
51
|
+
const pageIds = JSON.parse(content);
|
|
52
|
+
if (Array.isArray(pageIds) && pageIds.length > 0) {
|
|
53
|
+
pageCache.invalidatePages(pageIds);
|
|
54
|
+
cachedBuildContext = null;
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// File doesn't exist yet or read error — nothing to invalidate
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function loadPageRegistry(buildDirectory) {
|
|
62
|
+
const registryPath = path.join(buildDirectory, 'pageRegistry.json');
|
|
63
|
+
try {
|
|
64
|
+
const stat = fs.statSync(registryPath);
|
|
65
|
+
// Only reload if file has changed
|
|
66
|
+
if (cachedRegistryMtime && stat.mtimeMs === cachedRegistryMtime) {
|
|
67
|
+
return cachedRegistry;
|
|
68
|
+
}
|
|
69
|
+
cachedRegistryMtime = stat.mtimeMs;
|
|
70
|
+
cachedRegistry = readJsonFile(registryPath);
|
|
71
|
+
// Invalidate all pages when registry changes (skeleton rebuild happened)
|
|
72
|
+
pageCache.invalidateAll();
|
|
73
|
+
cachedBuildContext = null;
|
|
74
|
+
return cachedRegistry;
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getBuildContext(buildDirectory, configDirectory) {
|
|
81
|
+
if (cachedBuildContext) return cachedBuildContext;
|
|
82
|
+
|
|
83
|
+
const refMap = readJsonFile(path.join(buildDirectory, 'refMap.json')) ?? {};
|
|
84
|
+
const keyMap = readJsonFile(path.join(buildDirectory, 'keyMap.json')) ?? {};
|
|
85
|
+
const jsMap = readJsonFile(path.join(buildDirectory, 'jsMap.json')) ?? { client: {}, server: {} };
|
|
86
|
+
|
|
87
|
+
cachedBuildContext = createContext({
|
|
88
|
+
directories: {
|
|
89
|
+
build: buildDirectory,
|
|
90
|
+
config: configDirectory,
|
|
91
|
+
},
|
|
92
|
+
logger: jitLogger,
|
|
93
|
+
stage: 'dev',
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Restore refMap, keyMap, and jsMap from skeleton build
|
|
97
|
+
Object.assign(cachedBuildContext.refMap, refMap);
|
|
98
|
+
Object.assign(cachedBuildContext.keyMap, keyMap);
|
|
99
|
+
cachedBuildContext.jsMap.client = jsMap.client ?? {};
|
|
100
|
+
cachedBuildContext.jsMap.server = jsMap.server ?? {};
|
|
101
|
+
|
|
102
|
+
return cachedBuildContext;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function buildPageIfNeeded({ pageId, buildDirectory, configDirectory }) {
|
|
106
|
+
checkPageInvalidations(buildDirectory);
|
|
107
|
+
const registry = loadPageRegistry(buildDirectory);
|
|
108
|
+
if (!registry || !registry[pageId]) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (pageCache.isCompiled(pageId)) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const shouldBuild = await pageCache.acquireBuildLock(pageId);
|
|
117
|
+
if (!shouldBuild) {
|
|
118
|
+
// Another request completed the build
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const context = getBuildContext(buildDirectory, configDirectory);
|
|
124
|
+
const startTime = Date.now();
|
|
125
|
+
await buildPageJit({
|
|
126
|
+
pageId,
|
|
127
|
+
pageRegistry: registry,
|
|
128
|
+
context,
|
|
129
|
+
});
|
|
130
|
+
pageCache.markCompiled(pageId);
|
|
131
|
+
jitLogger.info(`Built page "${pageId}" in ${Date.now() - startTime}ms.`);
|
|
132
|
+
return true;
|
|
133
|
+
} finally {
|
|
134
|
+
pageCache.releaseBuildLock(pageId);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default buildPageIfNeeded;
|
|
139
|
+
export { pageCache };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2024 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
|
+
class PageCache {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.compiledPages = new Set();
|
|
20
|
+
this.buildLocks = new Map();
|
|
21
|
+
this.skeletonLock = null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
isCompiled(pageId) {
|
|
25
|
+
return this.compiledPages.has(pageId);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
markCompiled(pageId) {
|
|
29
|
+
this.compiledPages.add(pageId);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async acquireBuildLock(pageId) {
|
|
33
|
+
// Wait for any skeleton rebuild first
|
|
34
|
+
if (this.skeletonLock) {
|
|
35
|
+
await this.skeletonLock;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// If page build already in progress, wait for it
|
|
39
|
+
if (this.buildLocks.has(pageId)) {
|
|
40
|
+
await this.buildLocks.get(pageId);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Create new lock
|
|
45
|
+
let resolve;
|
|
46
|
+
const promise = new Promise((r) => {
|
|
47
|
+
resolve = r;
|
|
48
|
+
});
|
|
49
|
+
promise.resolve = resolve;
|
|
50
|
+
this.buildLocks.set(pageId, promise);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
releaseBuildLock(pageId) {
|
|
55
|
+
const lock = this.buildLocks.get(pageId);
|
|
56
|
+
if (lock) {
|
|
57
|
+
lock.resolve();
|
|
58
|
+
this.buildLocks.delete(pageId);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async acquireSkeletonLock() {
|
|
63
|
+
// Wait for all in-progress page builds to complete
|
|
64
|
+
await Promise.all(this.buildLocks.values());
|
|
65
|
+
let resolve;
|
|
66
|
+
this.skeletonLock = new Promise((r) => {
|
|
67
|
+
resolve = r;
|
|
68
|
+
});
|
|
69
|
+
this.skeletonLock.resolve = resolve;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
releaseSkeletonLock() {
|
|
73
|
+
if (this.skeletonLock) {
|
|
74
|
+
this.skeletonLock.resolve();
|
|
75
|
+
this.skeletonLock = null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
invalidateAll() {
|
|
80
|
+
this.compiledPages.clear();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
invalidatePages(pageIds) {
|
|
84
|
+
for (const pageId of pageIds) {
|
|
85
|
+
this.compiledPages.delete(pageId);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
invalidateByFiles(changedFiles, fileDependencyMap) {
|
|
90
|
+
const affectedPages = new Set();
|
|
91
|
+
for (const filePath of changedFiles) {
|
|
92
|
+
const pageIds = fileDependencyMap.get(filePath);
|
|
93
|
+
if (pageIds) {
|
|
94
|
+
for (const pageId of pageIds) {
|
|
95
|
+
affectedPages.add(pageId);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
this.invalidatePages(affectedPages);
|
|
100
|
+
return affectedPages;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export default PageCache;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2024 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 PageCache from './pageCache.mjs';
|
|
18
|
+
|
|
19
|
+
test('isCompiled returns false for uncompiled page', () => {
|
|
20
|
+
const cache = new PageCache();
|
|
21
|
+
expect(cache.isCompiled('home')).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('isCompiled returns true after markCompiled', () => {
|
|
25
|
+
const cache = new PageCache();
|
|
26
|
+
cache.markCompiled('home');
|
|
27
|
+
expect(cache.isCompiled('home')).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('invalidateAll clears all compiled pages', () => {
|
|
31
|
+
const cache = new PageCache();
|
|
32
|
+
cache.markCompiled('home');
|
|
33
|
+
cache.markCompiled('dashboard');
|
|
34
|
+
cache.invalidateAll();
|
|
35
|
+
expect(cache.isCompiled('home')).toBe(false);
|
|
36
|
+
expect(cache.isCompiled('dashboard')).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('invalidatePages removes specific pages', () => {
|
|
40
|
+
const cache = new PageCache();
|
|
41
|
+
cache.markCompiled('home');
|
|
42
|
+
cache.markCompiled('dashboard');
|
|
43
|
+
cache.markCompiled('settings');
|
|
44
|
+
cache.invalidatePages(['home', 'settings']);
|
|
45
|
+
expect(cache.isCompiled('home')).toBe(false);
|
|
46
|
+
expect(cache.isCompiled('dashboard')).toBe(true);
|
|
47
|
+
expect(cache.isCompiled('settings')).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('invalidateByFiles returns affected pages and invalidates them', () => {
|
|
51
|
+
const cache = new PageCache();
|
|
52
|
+
cache.markCompiled('home');
|
|
53
|
+
cache.markCompiled('dashboard');
|
|
54
|
+
|
|
55
|
+
const fileDependencyMap = new Map([
|
|
56
|
+
['pages/home/blocks.yaml', new Set(['home'])],
|
|
57
|
+
['shared/layout.yaml', new Set(['home', 'dashboard'])],
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const affected = cache.invalidateByFiles(['pages/home/blocks.yaml'], fileDependencyMap);
|
|
61
|
+
expect(affected).toEqual(new Set(['home']));
|
|
62
|
+
expect(cache.isCompiled('home')).toBe(false);
|
|
63
|
+
expect(cache.isCompiled('dashboard')).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('invalidateByFiles handles shared files affecting multiple pages', () => {
|
|
67
|
+
const cache = new PageCache();
|
|
68
|
+
cache.markCompiled('home');
|
|
69
|
+
cache.markCompiled('dashboard');
|
|
70
|
+
|
|
71
|
+
const fileDependencyMap = new Map([
|
|
72
|
+
['shared/layout.yaml', new Set(['home', 'dashboard'])],
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
const affected = cache.invalidateByFiles(['shared/layout.yaml'], fileDependencyMap);
|
|
76
|
+
expect(affected).toEqual(new Set(['home', 'dashboard']));
|
|
77
|
+
expect(cache.isCompiled('home')).toBe(false);
|
|
78
|
+
expect(cache.isCompiled('dashboard')).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('invalidateByFiles returns empty set for unknown files', () => {
|
|
82
|
+
const cache = new PageCache();
|
|
83
|
+
cache.markCompiled('home');
|
|
84
|
+
|
|
85
|
+
const fileDependencyMap = new Map();
|
|
86
|
+
const affected = cache.invalidateByFiles(['unknown.yaml'], fileDependencyMap);
|
|
87
|
+
expect(affected).toEqual(new Set());
|
|
88
|
+
expect(cache.isCompiled('home')).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('acquireBuildLock returns true for first request', async () => {
|
|
92
|
+
const cache = new PageCache();
|
|
93
|
+
const shouldBuild = await cache.acquireBuildLock('home');
|
|
94
|
+
expect(shouldBuild).toBe(true);
|
|
95
|
+
cache.releaseBuildLock('home');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('acquireBuildLock returns false for concurrent request (waits for first)', async () => {
|
|
99
|
+
const cache = new PageCache();
|
|
100
|
+
|
|
101
|
+
// First request acquires lock
|
|
102
|
+
const shouldBuild1 = await cache.acquireBuildLock('home');
|
|
103
|
+
expect(shouldBuild1).toBe(true);
|
|
104
|
+
|
|
105
|
+
// Second request waits for lock and returns false
|
|
106
|
+
const promise2 = cache.acquireBuildLock('home');
|
|
107
|
+
|
|
108
|
+
// Release first lock
|
|
109
|
+
cache.releaseBuildLock('home');
|
|
110
|
+
|
|
111
|
+
const shouldBuild2 = await promise2;
|
|
112
|
+
expect(shouldBuild2).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('acquireBuildLock for different pages does not block', async () => {
|
|
116
|
+
const cache = new PageCache();
|
|
117
|
+
|
|
118
|
+
const shouldBuild1 = await cache.acquireBuildLock('home');
|
|
119
|
+
const shouldBuild2 = await cache.acquireBuildLock('dashboard');
|
|
120
|
+
|
|
121
|
+
expect(shouldBuild1).toBe(true);
|
|
122
|
+
expect(shouldBuild2).toBe(true);
|
|
123
|
+
|
|
124
|
+
cache.releaseBuildLock('home');
|
|
125
|
+
cache.releaseBuildLock('dashboard');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('releaseBuildLock does nothing for non-existent lock', () => {
|
|
129
|
+
const cache = new PageCache();
|
|
130
|
+
// Should not throw
|
|
131
|
+
cache.releaseBuildLock('nonexistent');
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('acquireSkeletonLock waits for page builds', async () => {
|
|
135
|
+
const cache = new PageCache();
|
|
136
|
+
const buildOrder = [];
|
|
137
|
+
|
|
138
|
+
// Start a page build
|
|
139
|
+
await cache.acquireBuildLock('home');
|
|
140
|
+
|
|
141
|
+
// Start skeleton lock acquisition (should wait)
|
|
142
|
+
const skeletonPromise = cache.acquireSkeletonLock().then(() => {
|
|
143
|
+
buildOrder.push('skeleton-acquired');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Release page build
|
|
147
|
+
buildOrder.push('page-released');
|
|
148
|
+
cache.releaseBuildLock('home');
|
|
149
|
+
|
|
150
|
+
await skeletonPromise;
|
|
151
|
+
expect(buildOrder).toEqual(['page-released', 'skeleton-acquired']);
|
|
152
|
+
cache.releaseSkeletonLock();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('acquireBuildLock waits for skeleton lock', async () => {
|
|
156
|
+
const cache = new PageCache();
|
|
157
|
+
const buildOrder = [];
|
|
158
|
+
|
|
159
|
+
// Acquire skeleton lock
|
|
160
|
+
await cache.acquireSkeletonLock();
|
|
161
|
+
|
|
162
|
+
// Start page build (should wait for skeleton)
|
|
163
|
+
const pagePromise = cache.acquireBuildLock('home').then((result) => {
|
|
164
|
+
buildOrder.push('page-acquired');
|
|
165
|
+
return result;
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// Release skeleton
|
|
169
|
+
buildOrder.push('skeleton-released');
|
|
170
|
+
cache.releaseSkeletonLock();
|
|
171
|
+
|
|
172
|
+
const shouldBuild = await pagePromise;
|
|
173
|
+
expect(shouldBuild).toBe(true);
|
|
174
|
+
expect(buildOrder).toEqual(['skeleton-released', 'page-acquired']);
|
|
175
|
+
cache.releaseBuildLock('home');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test('releaseSkeletonLock does nothing when no lock exists', () => {
|
|
179
|
+
const cache = new PageCache();
|
|
180
|
+
// Should not throw
|
|
181
|
+
cache.releaseSkeletonLock();
|
|
182
|
+
});
|
package/manager/getContext.mjs
CHANGED
|
@@ -32,6 +32,7 @@ import shutdownServer from './processes/shutdownServer.mjs';
|
|
|
32
32
|
import startWatchers from './processes/startWatchers.mjs';
|
|
33
33
|
|
|
34
34
|
import getNextBin from './utils/getNextBin.mjs';
|
|
35
|
+
import PageCache from '../lib/server/pageCache.mjs';
|
|
35
36
|
|
|
36
37
|
const argv = yargs(hideBin(process.argv)).array('watch').array('watchIgnore').argv;
|
|
37
38
|
|
|
@@ -59,13 +60,30 @@ async function getContext() {
|
|
|
59
60
|
: [],
|
|
60
61
|
},
|
|
61
62
|
version: env.npm_package_version,
|
|
63
|
+
|
|
64
|
+
// JIT build state
|
|
65
|
+
pageCache: new PageCache(),
|
|
66
|
+
pageRegistry: null,
|
|
67
|
+
fileDependencyMap: null,
|
|
68
|
+
buildContext: null,
|
|
62
69
|
};
|
|
63
70
|
|
|
64
71
|
context.packageManagerCmd = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm';
|
|
65
72
|
context.checkMockUserWarning = checkMockUserWarning(context);
|
|
66
73
|
context.initialBuild = initialBuild(context);
|
|
67
74
|
context.installPlugins = installPlugins(context);
|
|
68
|
-
|
|
75
|
+
|
|
76
|
+
// Wrap lowdefyBuild to capture and store the shallow build result
|
|
77
|
+
const buildFn = lowdefyBuild(context);
|
|
78
|
+
context.lowdefyBuild = async () => {
|
|
79
|
+
const result = await buildFn();
|
|
80
|
+
if (result) {
|
|
81
|
+
context.pageRegistry = result.pageRegistry;
|
|
82
|
+
context.fileDependencyMap = result.fileDependencyMap;
|
|
83
|
+
context.buildContext = result.context;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
69
87
|
context.nextBuild = nextBuild(context);
|
|
70
88
|
context.readDotEnv = readDotEnv(context);
|
|
71
89
|
context.reloadClients = reloadClients(context);
|
|
@@ -14,21 +14,35 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import
|
|
17
|
+
import { shallowBuild } from '@lowdefy/build/dev';
|
|
18
18
|
import createCustomPluginTypesMap from '../utils/createCustomPluginTypesMap.mjs';
|
|
19
19
|
|
|
20
|
-
function lowdefyBuild({ directories, logger, options }) {
|
|
20
|
+
function lowdefyBuild({ directories, logger, options, pageCache }) {
|
|
21
21
|
return async () => {
|
|
22
22
|
logger.ui.spin('Building config...');
|
|
23
23
|
const customTypesMap = await createCustomPluginTypesMap({ directories, logger });
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
|
|
25
|
+
if (pageCache) {
|
|
26
|
+
await pageCache.acquireSkeletonLock();
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const result = await shallowBuild({
|
|
30
|
+
customTypesMap,
|
|
31
|
+
directories,
|
|
32
|
+
logger,
|
|
33
|
+
refResolver: options.refResolver,
|
|
34
|
+
stage: 'dev',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Return result so getContext can store registries
|
|
38
|
+
logger.ui.log('Built config.');
|
|
39
|
+
return result;
|
|
40
|
+
} finally {
|
|
41
|
+
if (pageCache) {
|
|
42
|
+
pageCache.invalidateAll();
|
|
43
|
+
pageCache.releaseSkeletonLock();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
32
46
|
};
|
|
33
47
|
}
|
|
34
48
|
|
|
@@ -19,11 +19,26 @@ import { spawnProcess } from '@lowdefy/node-utils';
|
|
|
19
19
|
function nextBuild({ bin, logger }) {
|
|
20
20
|
return async () => {
|
|
21
21
|
logger.ui.spin('Building app...');
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
const errorLines = [];
|
|
23
|
+
try {
|
|
24
|
+
await spawnProcess({
|
|
25
|
+
command: 'node',
|
|
26
|
+
args: [bin.next, 'build'],
|
|
27
|
+
stdOutLineHandler: (line) => logger.debug(line),
|
|
28
|
+
stdErrLineHandler: (line) => {
|
|
29
|
+
logger.debug(line);
|
|
30
|
+
errorLines.push(line);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if (errorLines.length > 0) {
|
|
35
|
+
errorLines.forEach((line) => logger.error(line));
|
|
36
|
+
}
|
|
37
|
+
const error = new Error('Next.js build failed. See above for details.');
|
|
38
|
+
error.isFormatted = true;
|
|
39
|
+
error.hideStack = true;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
27
42
|
logger.ui.log('Built app.');
|
|
28
43
|
};
|
|
29
44
|
}
|
|
@@ -16,6 +16,19 @@
|
|
|
16
16
|
|
|
17
17
|
import { spawn } from 'child_process';
|
|
18
18
|
|
|
19
|
+
function createStdErrLineHandler({ context }) {
|
|
20
|
+
const port = context.options.port;
|
|
21
|
+
return function stdErrLineHandler(line) {
|
|
22
|
+
if (line.includes('EADDRINUSE')) {
|
|
23
|
+
context.logger.error(
|
|
24
|
+
`Port ${port} is already in use. Stop the other process or use a different port with --port.`
|
|
25
|
+
);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
context.logger.error(line);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
19
32
|
function startServer(context) {
|
|
20
33
|
context.shutdownServer();
|
|
21
34
|
|
|
@@ -28,12 +41,13 @@ function startServer(context) {
|
|
|
28
41
|
},
|
|
29
42
|
});
|
|
30
43
|
|
|
44
|
+
const stdErrLineHandler = createStdErrLineHandler({ context });
|
|
31
45
|
nextServer.stderr.on('data', (data) => {
|
|
32
46
|
data
|
|
33
47
|
.toString('utf8')
|
|
34
48
|
.split('\n')
|
|
35
49
|
.forEach((line) => {
|
|
36
|
-
if (line)
|
|
50
|
+
if (line) stdErrLineHandler(line);
|
|
37
51
|
});
|
|
38
52
|
});
|
|
39
53
|
|
package/manager/run.mjs
CHANGED
|
@@ -19,6 +19,7 @@ import { wait } from '@lowdefy/helpers';
|
|
|
19
19
|
import opener from 'opener';
|
|
20
20
|
import getContext from './getContext.mjs';
|
|
21
21
|
import startServer from './processes/startServer.mjs';
|
|
22
|
+
import checkPortAvailable from './utils/checkPortAvailable.mjs';
|
|
22
23
|
|
|
23
24
|
/*
|
|
24
25
|
The run script does the following:
|
|
@@ -85,6 +86,7 @@ try {
|
|
|
85
86
|
// because chokidar sometimes doesn't fire this event, and it seems like there isn't an issue with not waiting.
|
|
86
87
|
context.startWatchers();
|
|
87
88
|
|
|
89
|
+
await checkPortAvailable(context.options.port);
|
|
88
90
|
startServer(context);
|
|
89
91
|
await wait(800);
|
|
90
92
|
if (process.env.LOWDEFY_SERVER_DEV_OPEN_BROWSER === 'true') {
|
|
@@ -93,13 +95,7 @@ try {
|
|
|
93
95
|
}
|
|
94
96
|
await new Promise(() => {});
|
|
95
97
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
if (error.isFormatted || error.hideStack) {
|
|
98
|
-
context.logger.error(error.message);
|
|
99
|
-
} else {
|
|
100
|
-
// Otherwise, show full error with stack trace
|
|
101
|
-
context.logger.error(error);
|
|
102
|
-
}
|
|
98
|
+
context.logger.error(error);
|
|
103
99
|
context.shutdownServer();
|
|
104
100
|
process.exit();
|
|
105
101
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2024 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 net from 'net';
|
|
18
|
+
|
|
19
|
+
function checkPortAvailable(port) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const server = net.createServer();
|
|
22
|
+
server.once('error', (err) => {
|
|
23
|
+
if (err.code === 'EADDRINUSE') {
|
|
24
|
+
const error = new Error(
|
|
25
|
+
`Port ${port} is already in use. Stop the other process or use a different port with --port.`
|
|
26
|
+
);
|
|
27
|
+
error.isFormatted = true;
|
|
28
|
+
reject(error);
|
|
29
|
+
} else {
|
|
30
|
+
reject(err);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
server.once('listening', () => {
|
|
34
|
+
server.close(() => resolve());
|
|
35
|
+
});
|
|
36
|
+
server.listen(port);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default checkPortAvailable;
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
import fs from 'fs';
|
|
17
18
|
import path from 'path';
|
|
18
19
|
import getLowdefyVersion from '../utils/getLowdefyVersion.mjs';
|
|
19
20
|
import setupWatcher from '../utils/setupWatcher.mjs';
|
|
@@ -23,9 +24,13 @@ function lowdefyBuildWatcher(context) {
|
|
|
23
24
|
path.isAbsolute(item) ? item : path.resolve(context.directories.config, item);
|
|
24
25
|
|
|
25
26
|
const callback = async (filePaths) => {
|
|
26
|
-
const
|
|
27
|
+
const changedFiles = filePaths
|
|
27
28
|
.flat()
|
|
28
|
-
.
|
|
29
|
+
.map((filePath) => path.relative(context.directories.config, filePath));
|
|
30
|
+
|
|
31
|
+
const lowdefyYamlModified = changedFiles.some(
|
|
32
|
+
(filePath) => filePath === 'lowdefy.yaml' || filePath === 'lowdefy.yml'
|
|
33
|
+
);
|
|
29
34
|
if (lowdefyYamlModified) {
|
|
30
35
|
const lowdefyVersion = await getLowdefyVersion(context);
|
|
31
36
|
if (lowdefyVersion !== context.version && lowdefyVersion !== 'local') {
|
|
@@ -36,17 +41,41 @@ function lowdefyBuildWatcher(context) {
|
|
|
36
41
|
}
|
|
37
42
|
|
|
38
43
|
try {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
// Check if only page-level files changed (targeted invalidation)
|
|
45
|
+
const isSkeletonChange =
|
|
46
|
+
lowdefyYamlModified ||
|
|
47
|
+
changedFiles.some(
|
|
48
|
+
(f) =>
|
|
49
|
+
!context.fileDependencyMap?.has(f) &&
|
|
50
|
+
!f.startsWith('pages/') &&
|
|
51
|
+
!f.startsWith('./')
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
if (isSkeletonChange || !context.pageCache) {
|
|
55
|
+
// Full skeleton rebuild
|
|
56
|
+
await context.lowdefyBuild();
|
|
45
57
|
} else {
|
|
46
|
-
//
|
|
47
|
-
context.
|
|
58
|
+
// Targeted invalidation: only clear affected pages
|
|
59
|
+
const affectedPages = context.pageCache.invalidateByFiles(
|
|
60
|
+
changedFiles,
|
|
61
|
+
context.fileDependencyMap
|
|
62
|
+
);
|
|
63
|
+
if (affectedPages.size > 0) {
|
|
64
|
+
// Write invalidated page IDs to a file so the Next.js server process
|
|
65
|
+
// (which has its own PageCache) knows which pages to rebuild.
|
|
66
|
+
const invalidationPath = path.join(context.directories.build, 'invalidatePages.json');
|
|
67
|
+
fs.writeFileSync(invalidationPath, JSON.stringify([...affectedPages]));
|
|
68
|
+
context.logger.ui.log(
|
|
69
|
+
`Invalidated ${affectedPages.size} page(s): ${[...affectedPages].join(', ')}`
|
|
70
|
+
);
|
|
71
|
+
} else {
|
|
72
|
+
// Unknown file changed - do full rebuild to be safe
|
|
73
|
+
await context.lowdefyBuild();
|
|
74
|
+
}
|
|
48
75
|
}
|
|
49
|
-
|
|
76
|
+
context.reloadClients();
|
|
77
|
+
} catch (error) {
|
|
78
|
+
context.logger.error(error);
|
|
50
79
|
}
|
|
51
80
|
};
|
|
52
81
|
return setupWatcher({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/server-dev",
|
|
3
|
-
"version": "0.0.0-experimental-
|
|
3
|
+
"version": "0.0.0-experimental-20260203220133",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -36,36 +36,36 @@
|
|
|
36
36
|
".npmrc"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@lowdefy/actions-core": "0.0.0-experimental-
|
|
40
|
-
"@lowdefy/api": "0.0.0-experimental-
|
|
41
|
-
"@lowdefy/block-utils": "0.0.0-experimental-
|
|
42
|
-
"@lowdefy/blocks-aggrid": "0.0.0-experimental-
|
|
43
|
-
"@lowdefy/blocks-antd": "0.0.0-experimental-
|
|
44
|
-
"@lowdefy/blocks-basic": "0.0.0-experimental-
|
|
45
|
-
"@lowdefy/blocks-color-selectors": "0.0.0-experimental-
|
|
46
|
-
"@lowdefy/blocks-echarts": "0.0.0-experimental-
|
|
47
|
-
"@lowdefy/blocks-loaders": "0.0.0-experimental-
|
|
48
|
-
"@lowdefy/blocks-markdown": "0.0.0-experimental-
|
|
49
|
-
"@lowdefy/blocks-qr": "0.0.0-experimental-
|
|
50
|
-
"@lowdefy/build": "0.0.0-experimental-
|
|
51
|
-
"@lowdefy/client": "0.0.0-experimental-
|
|
52
|
-
"@lowdefy/connection-axios-http": "0.0.0-experimental-
|
|
53
|
-
"@lowdefy/connection-test": "0.0.0-experimental-
|
|
54
|
-
"@lowdefy/engine": "0.0.0-experimental-
|
|
55
|
-
"@lowdefy/errors": "0.0.0-experimental-
|
|
56
|
-
"@lowdefy/helpers": "0.0.0-experimental-
|
|
57
|
-
"@lowdefy/layout": "0.0.0-experimental-
|
|
58
|
-
"@lowdefy/logger": "0.0.0-experimental-
|
|
59
|
-
"@lowdefy/node-utils": "0.0.0-experimental-
|
|
60
|
-
"@lowdefy/operators-change-case": "0.0.0-experimental-
|
|
61
|
-
"@lowdefy/operators-diff": "0.0.0-experimental-
|
|
62
|
-
"@lowdefy/operators-js": "0.0.0-experimental-
|
|
63
|
-
"@lowdefy/operators-moment": "0.0.0-experimental-
|
|
64
|
-
"@lowdefy/operators-mql": "0.0.0-experimental-
|
|
65
|
-
"@lowdefy/operators-nunjucks": "0.0.0-experimental-
|
|
66
|
-
"@lowdefy/operators-uuid": "0.0.0-experimental-
|
|
67
|
-
"@lowdefy/operators-yaml": "0.0.0-experimental-
|
|
68
|
-
"@lowdefy/plugin-next-auth": "0.0.0-experimental-
|
|
39
|
+
"@lowdefy/actions-core": "0.0.0-experimental-20260203220133",
|
|
40
|
+
"@lowdefy/api": "0.0.0-experimental-20260203220133",
|
|
41
|
+
"@lowdefy/block-utils": "0.0.0-experimental-20260203220133",
|
|
42
|
+
"@lowdefy/blocks-aggrid": "0.0.0-experimental-20260203220133",
|
|
43
|
+
"@lowdefy/blocks-antd": "0.0.0-experimental-20260203220133",
|
|
44
|
+
"@lowdefy/blocks-basic": "0.0.0-experimental-20260203220133",
|
|
45
|
+
"@lowdefy/blocks-color-selectors": "0.0.0-experimental-20260203220133",
|
|
46
|
+
"@lowdefy/blocks-echarts": "0.0.0-experimental-20260203220133",
|
|
47
|
+
"@lowdefy/blocks-loaders": "0.0.0-experimental-20260203220133",
|
|
48
|
+
"@lowdefy/blocks-markdown": "0.0.0-experimental-20260203220133",
|
|
49
|
+
"@lowdefy/blocks-qr": "0.0.0-experimental-20260203220133",
|
|
50
|
+
"@lowdefy/build": "0.0.0-experimental-20260203220133",
|
|
51
|
+
"@lowdefy/client": "0.0.0-experimental-20260203220133",
|
|
52
|
+
"@lowdefy/connection-axios-http": "0.0.0-experimental-20260203220133",
|
|
53
|
+
"@lowdefy/connection-test": "0.0.0-experimental-20260203220133",
|
|
54
|
+
"@lowdefy/engine": "0.0.0-experimental-20260203220133",
|
|
55
|
+
"@lowdefy/errors": "0.0.0-experimental-20260203220133",
|
|
56
|
+
"@lowdefy/helpers": "0.0.0-experimental-20260203220133",
|
|
57
|
+
"@lowdefy/layout": "0.0.0-experimental-20260203220133",
|
|
58
|
+
"@lowdefy/logger": "0.0.0-experimental-20260203220133",
|
|
59
|
+
"@lowdefy/node-utils": "0.0.0-experimental-20260203220133",
|
|
60
|
+
"@lowdefy/operators-change-case": "0.0.0-experimental-20260203220133",
|
|
61
|
+
"@lowdefy/operators-diff": "0.0.0-experimental-20260203220133",
|
|
62
|
+
"@lowdefy/operators-js": "0.0.0-experimental-20260203220133",
|
|
63
|
+
"@lowdefy/operators-moment": "0.0.0-experimental-20260203220133",
|
|
64
|
+
"@lowdefy/operators-mql": "0.0.0-experimental-20260203220133",
|
|
65
|
+
"@lowdefy/operators-nunjucks": "0.0.0-experimental-20260203220133",
|
|
66
|
+
"@lowdefy/operators-uuid": "0.0.0-experimental-20260203220133",
|
|
67
|
+
"@lowdefy/operators-yaml": "0.0.0-experimental-20260203220133",
|
|
68
|
+
"@lowdefy/plugin-next-auth": "0.0.0-experimental-20260203220133",
|
|
69
69
|
"@sentry/nextjs": "8.53.0",
|
|
70
70
|
"chokidar": "3.5.3",
|
|
71
71
|
"dotenv": "16.3.1",
|
package/package.original.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/server-dev",
|
|
3
|
-
"version": "0.0.0-experimental-
|
|
3
|
+
"version": "0.0.0-experimental-20260203220133",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -43,36 +43,36 @@
|
|
|
43
43
|
"prepublishOnly": "pnpm build"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@lowdefy/actions-core": "0.0.0-experimental-
|
|
47
|
-
"@lowdefy/api": "0.0.0-experimental-
|
|
48
|
-
"@lowdefy/block-utils": "0.0.0-experimental-
|
|
49
|
-
"@lowdefy/blocks-aggrid": "0.0.0-experimental-
|
|
50
|
-
"@lowdefy/blocks-antd": "0.0.0-experimental-
|
|
51
|
-
"@lowdefy/blocks-basic": "0.0.0-experimental-
|
|
52
|
-
"@lowdefy/blocks-color-selectors": "0.0.0-experimental-
|
|
53
|
-
"@lowdefy/blocks-echarts": "0.0.0-experimental-
|
|
54
|
-
"@lowdefy/blocks-loaders": "0.0.0-experimental-
|
|
55
|
-
"@lowdefy/blocks-markdown": "0.0.0-experimental-
|
|
56
|
-
"@lowdefy/blocks-qr": "0.0.0-experimental-
|
|
57
|
-
"@lowdefy/build": "0.0.0-experimental-
|
|
58
|
-
"@lowdefy/client": "0.0.0-experimental-
|
|
59
|
-
"@lowdefy/connection-axios-http": "0.0.0-experimental-
|
|
60
|
-
"@lowdefy/connection-test": "0.0.0-experimental-
|
|
61
|
-
"@lowdefy/engine": "0.0.0-experimental-
|
|
62
|
-
"@lowdefy/errors": "0.0.0-experimental-
|
|
63
|
-
"@lowdefy/helpers": "0.0.0-experimental-
|
|
64
|
-
"@lowdefy/layout": "0.0.0-experimental-
|
|
65
|
-
"@lowdefy/logger": "0.0.0-experimental-
|
|
66
|
-
"@lowdefy/node-utils": "0.0.0-experimental-
|
|
67
|
-
"@lowdefy/operators-change-case": "0.0.0-experimental-
|
|
68
|
-
"@lowdefy/operators-diff": "0.0.0-experimental-
|
|
69
|
-
"@lowdefy/operators-js": "0.0.0-experimental-
|
|
70
|
-
"@lowdefy/operators-moment": "0.0.0-experimental-
|
|
71
|
-
"@lowdefy/operators-mql": "0.0.0-experimental-
|
|
72
|
-
"@lowdefy/operators-nunjucks": "0.0.0-experimental-
|
|
73
|
-
"@lowdefy/operators-uuid": "0.0.0-experimental-
|
|
74
|
-
"@lowdefy/operators-yaml": "0.0.0-experimental-
|
|
75
|
-
"@lowdefy/plugin-next-auth": "0.0.0-experimental-
|
|
46
|
+
"@lowdefy/actions-core": "0.0.0-experimental-20260203220133",
|
|
47
|
+
"@lowdefy/api": "0.0.0-experimental-20260203220133",
|
|
48
|
+
"@lowdefy/block-utils": "0.0.0-experimental-20260203220133",
|
|
49
|
+
"@lowdefy/blocks-aggrid": "0.0.0-experimental-20260203220133",
|
|
50
|
+
"@lowdefy/blocks-antd": "0.0.0-experimental-20260203220133",
|
|
51
|
+
"@lowdefy/blocks-basic": "0.0.0-experimental-20260203220133",
|
|
52
|
+
"@lowdefy/blocks-color-selectors": "0.0.0-experimental-20260203220133",
|
|
53
|
+
"@lowdefy/blocks-echarts": "0.0.0-experimental-20260203220133",
|
|
54
|
+
"@lowdefy/blocks-loaders": "0.0.0-experimental-20260203220133",
|
|
55
|
+
"@lowdefy/blocks-markdown": "0.0.0-experimental-20260203220133",
|
|
56
|
+
"@lowdefy/blocks-qr": "0.0.0-experimental-20260203220133",
|
|
57
|
+
"@lowdefy/build": "0.0.0-experimental-20260203220133",
|
|
58
|
+
"@lowdefy/client": "0.0.0-experimental-20260203220133",
|
|
59
|
+
"@lowdefy/connection-axios-http": "0.0.0-experimental-20260203220133",
|
|
60
|
+
"@lowdefy/connection-test": "0.0.0-experimental-20260203220133",
|
|
61
|
+
"@lowdefy/engine": "0.0.0-experimental-20260203220133",
|
|
62
|
+
"@lowdefy/errors": "0.0.0-experimental-20260203220133",
|
|
63
|
+
"@lowdefy/helpers": "0.0.0-experimental-20260203220133",
|
|
64
|
+
"@lowdefy/layout": "0.0.0-experimental-20260203220133",
|
|
65
|
+
"@lowdefy/logger": "0.0.0-experimental-20260203220133",
|
|
66
|
+
"@lowdefy/node-utils": "0.0.0-experimental-20260203220133",
|
|
67
|
+
"@lowdefy/operators-change-case": "0.0.0-experimental-20260203220133",
|
|
68
|
+
"@lowdefy/operators-diff": "0.0.0-experimental-20260203220133",
|
|
69
|
+
"@lowdefy/operators-js": "0.0.0-experimental-20260203220133",
|
|
70
|
+
"@lowdefy/operators-moment": "0.0.0-experimental-20260203220133",
|
|
71
|
+
"@lowdefy/operators-mql": "0.0.0-experimental-20260203220133",
|
|
72
|
+
"@lowdefy/operators-nunjucks": "0.0.0-experimental-20260203220133",
|
|
73
|
+
"@lowdefy/operators-uuid": "0.0.0-experimental-20260203220133",
|
|
74
|
+
"@lowdefy/operators-yaml": "0.0.0-experimental-20260203220133",
|
|
75
|
+
"@lowdefy/plugin-next-auth": "0.0.0-experimental-20260203220133",
|
|
76
76
|
"@sentry/nextjs": "8.53.0",
|
|
77
77
|
"chokidar": "3.5.3",
|
|
78
78
|
"dotenv": "16.3.1",
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2024 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 fs from 'fs';
|
|
18
|
+
import path from 'path';
|
|
19
|
+
|
|
20
|
+
async function handler(req, res) {
|
|
21
|
+
const { env } = req.query;
|
|
22
|
+
|
|
23
|
+
if (env !== 'client' && env !== 'server') {
|
|
24
|
+
res.status(400).send('Invalid env parameter. Use "client" or "server".');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const fileName = env === 'client' ? 'clientJsMap.js' : 'serverJsMap.js';
|
|
29
|
+
const filePath = path.join(process.cwd(), 'build', 'plugins', 'operators', fileName);
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
33
|
+
res.setHeader('Content-Type', 'application/javascript');
|
|
34
|
+
res.status(200).send(content);
|
|
35
|
+
} catch {
|
|
36
|
+
// Return empty default export if file doesn't exist yet
|
|
37
|
+
res.setHeader('Content-Type', 'application/javascript');
|
|
38
|
+
res.status(200).send('export default {};');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default handler;
|
|
@@ -17,11 +17,41 @@
|
|
|
17
17
|
import { getPageConfig } from '@lowdefy/api';
|
|
18
18
|
|
|
19
19
|
import apiWrapper from '../../../lib/server/apiWrapper.js';
|
|
20
|
+
import buildPageIfNeeded from '../../../lib/server/jitPageBuilder.js';
|
|
21
|
+
import logError from '../../../lib/server/log/logError.js';
|
|
20
22
|
|
|
21
23
|
async function handler({ context, req, res }) {
|
|
22
24
|
const { pageId } = req.query;
|
|
25
|
+
|
|
26
|
+
// Attempt JIT build if page not yet compiled
|
|
27
|
+
try {
|
|
28
|
+
await buildPageIfNeeded({
|
|
29
|
+
pageId,
|
|
30
|
+
buildDirectory: context.buildDirectory,
|
|
31
|
+
configDirectory: context.configDirectory,
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
const rawErrors = error.buildErrors ?? [error];
|
|
35
|
+
const errors = [];
|
|
36
|
+
for (const err of rawErrors) {
|
|
37
|
+
await logError({ context, error: err });
|
|
38
|
+
errors.push({
|
|
39
|
+
type: err.name ?? 'Error',
|
|
40
|
+
message: err.message,
|
|
41
|
+
source: err.source ?? null,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
res.status(500).json({
|
|
45
|
+
buildError: true,
|
|
46
|
+
errors,
|
|
47
|
+
// Keep top-level message/source for backward compatibility
|
|
48
|
+
message: error.message,
|
|
49
|
+
source: error.source ?? null,
|
|
50
|
+
});
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
23
54
|
const pageConfig = await getPageConfig(context, { pageId });
|
|
24
|
-
// TODO: Do we log here?
|
|
25
55
|
if (pageConfig === null) {
|
|
26
56
|
res.status(404).send('Page not found.');
|
|
27
57
|
} else {
|