@lowdefy/server-dev 0.0.0-experimental-20260203124016 → 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 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 jsMap from '../../build/plugins/operators/clientJsMap.js';
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={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;
@@ -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={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
- mutate((key) => key.startsWith(`${basePath}/api/page`) || key === `${basePath}/api/root`);
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 request from './request.js';
16
+ import { getReloadVersion } from './useMutateCache.js';
17
17
 
18
- function fetchPageConfig(url) {
19
- return request({ url });
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 { data } = useSWR(`${basePath}/api/page/${pageId}`, fetchPageConfig, { suspense: true });
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
 
@@ -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 jsMap from '../../build/plugins/operators/serverJsMap.js';
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: path.join(process.cwd(), 'build'),
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 };
@@ -14,67 +14,20 @@
14
14
  limitations under the License.
15
15
  */
16
16
 
17
- import pino from 'pino';
17
+ import { createNodeLogger, wrapErrorLogger } from '@lowdefy/logger/node';
18
18
 
19
- function formatReceived(message, received) {
20
- if (received === undefined) {
21
- return message;
22
- }
23
- try {
24
- return `${message} Received: ${JSON.stringify(received)}`;
25
- } catch {
26
- return `${message} Received: [unserializable]`;
27
- }
28
- }
29
-
30
- const pinoLogger = pino({
19
+ const logger = createNodeLogger({
31
20
  name: 'lowdefy_server',
32
21
  level: process.env.LOWDEFY_LOG_LEVEL ?? 'info',
33
22
  base: { pid: undefined, hostname: undefined },
34
- serializers: {
35
- err: (err) => ({
36
- message: err.message,
37
- name: err.name,
38
- stack: err.stack,
39
- source: err.source,
40
- config: err.config,
41
- configKey: err.configKey,
42
- isServiceError: err.isServiceError,
43
- }),
44
- },
23
+ mixin: (context, level) => ({
24
+ ...context,
25
+ print: context.print ?? logger.levels.labels[level],
26
+ }),
45
27
  });
46
28
 
47
- // Wrap logger to handle error objects passed to logger.error(error)
48
- function wrapLogger(logger) {
49
- const originalError = logger.error.bind(logger);
50
- const originalInfo = logger.info.bind(logger);
51
-
52
- logger.error = (errorOrMessage, ...args) => {
53
- // If it's an Error object (has name property - covers Error instances and error-like objects)
54
- if (errorOrMessage?.name) {
55
- // Log source first if available
56
- if (errorOrMessage.source) {
57
- originalInfo(errorOrMessage.source);
58
- }
59
- const errorName = errorOrMessage.name || 'Error';
60
- // Handle undefined message (which JS converts to string "undefined")
61
- const rawMessage = errorOrMessage.message;
62
- const message =
63
- rawMessage && rawMessage !== 'undefined' ? rawMessage : 'Unknown error';
64
- const errorMessage = formatReceived(message, errorOrMessage.received);
65
- originalError(`[${errorName}] ${errorMessage}`);
66
- return;
67
- }
68
- // Pass through normal calls
69
- originalError(errorOrMessage, ...args);
70
- };
71
-
72
- return logger;
73
- }
74
-
75
29
  function createLogger(metadata = {}) {
76
- const childLogger = pinoLogger.child(metadata);
77
- return wrapLogger(childLogger);
30
+ return wrapErrorLogger(logger.child(metadata));
78
31
  }
79
32
 
80
33
  export default createLogger;
@@ -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;