@lowdefy/server-dev 0.0.0-experimental-20260203220133 → 0.0.0-experimental-20260204091424
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/Page.js +1 -17
- package/lib/client/utils/useMutateCache.js +2 -15
- package/lib/client/utils/usePageConfig.js +4 -49
- package/lib/server/apiWrapper.js +2 -30
- package/lib/server/log/createLogger.js +54 -7
- package/manager/getContext.mjs +2 -20
- package/manager/processes/installPlugins.mjs +2 -2
- package/manager/processes/lowdefyBuild.mjs +11 -25
- package/manager/processes/nextBuild.mjs +7 -22
- package/manager/processes/restartServer.mjs +2 -2
- package/manager/processes/shutdownServer.mjs +1 -1
- package/manager/processes/startServer.mjs +14 -31
- package/manager/run.mjs +7 -3
- package/manager/utils/createLogger.mjs +44 -0
- package/manager/utils/createStdOutLineHandler.mjs +45 -0
- package/manager/watchers/lowdefyBuildWatcher.mjs +11 -40
- package/manager/watchers/nextBuildWatcher.mjs +2 -2
- package/package.json +28 -31
- package/package.original.json +28 -31
- package/pages/_app.js +2 -11
- package/pages/api/page/[pageId].js +1 -31
- package/pages/api/request/[pageId]/[requestId].js +2 -2
- package/lib/client/BuildErrorPage.js +0 -99
- package/lib/server/jitPageBuilder.js +0 -139
- package/lib/server/pageCache.mjs +0 -104
- package/lib/server/pageCache.test.mjs +0 -182
- package/manager/utils/checkPortAvailable.mjs +0 -40
- package/pages/api/js/[env].js +0 -42
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 jsMap 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={jsMap}
|
|
54
54
|
lowdefy={lowdefy}
|
|
55
55
|
pageId={pageId}
|
|
56
56
|
resetContext={resetContext}
|
package/lib/client/Page.js
CHANGED
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
import React from 'react';
|
|
18
18
|
import Client from '@lowdefy/client';
|
|
19
19
|
|
|
20
|
-
import BuildErrorPage from './BuildErrorPage.js';
|
|
21
20
|
import RestartingPage from './RestartingPage.js';
|
|
22
21
|
import usePageConfig from './utils/usePageConfig.js';
|
|
23
22
|
|
|
@@ -38,24 +37,9 @@ const Page = ({
|
|
|
38
37
|
router.replace(`/404`);
|
|
39
38
|
return '';
|
|
40
39
|
}
|
|
41
|
-
if (pageConfig.buildError) {
|
|
42
|
-
return (
|
|
43
|
-
<BuildErrorPage
|
|
44
|
-
errors={pageConfig.errors}
|
|
45
|
-
message={pageConfig.message}
|
|
46
|
-
source={pageConfig.source}
|
|
47
|
-
/>
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
40
|
if (resetContext.restarting) {
|
|
51
41
|
return <RestartingPage />;
|
|
52
42
|
}
|
|
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
|
-
|
|
59
43
|
return (
|
|
60
44
|
<Client
|
|
61
45
|
auth={auth}
|
|
@@ -64,7 +48,7 @@ const Page = ({
|
|
|
64
48
|
...config,
|
|
65
49
|
pageConfig,
|
|
66
50
|
}}
|
|
67
|
-
jsMap={
|
|
51
|
+
jsMap={jsMap}
|
|
68
52
|
lowdefy={lowdefy}
|
|
69
53
|
resetContext={resetContext}
|
|
70
54
|
router={router}
|
|
@@ -16,23 +16,10 @@
|
|
|
16
16
|
|
|
17
17
|
import { useSWRConfig } from 'swr';
|
|
18
18
|
|
|
19
|
-
let reloadVersion = 0;
|
|
20
|
-
|
|
21
|
-
function getReloadVersion() {
|
|
22
|
-
return reloadVersion;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
19
|
function useMutateCache(basePath) {
|
|
26
20
|
const { mutate } = useSWRConfig();
|
|
27
|
-
return () =>
|
|
28
|
-
|
|
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
|
-
};
|
|
21
|
+
return () =>
|
|
22
|
+
mutate((key) => key.startsWith(`${basePath}/api/page`) || key === `${basePath}/api/root`);
|
|
35
23
|
}
|
|
36
24
|
|
|
37
|
-
export { getReloadVersion };
|
|
38
25
|
export default useMutateCache;
|
|
@@ -13,59 +13,14 @@
|
|
|
13
13
|
|
|
14
14
|
import useSWR from 'swr';
|
|
15
15
|
|
|
16
|
-
import
|
|
16
|
+
import request from './request.js';
|
|
17
17
|
|
|
18
|
-
function
|
|
19
|
-
|
|
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;
|
|
18
|
+
function fetchPageConfig(url) {
|
|
19
|
+
return request({ url });
|
|
57
20
|
}
|
|
58
21
|
|
|
59
22
|
function usePageConfig(pageId, basePath) {
|
|
60
|
-
const
|
|
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
|
-
);
|
|
23
|
+
const { data } = useSWR(`${basePath}/api/page/${pageId}`, fetchPageConfig, { suspense: true });
|
|
69
24
|
return { data };
|
|
70
25
|
}
|
|
71
26
|
|
package/lib/server/apiWrapper.js
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
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';
|
|
17
16
|
import path from 'path';
|
|
18
17
|
import { createApiContext } from '@lowdefy/api';
|
|
19
18
|
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
@@ -27,7 +26,7 @@ import getServerSession from './auth/getServerSession.js';
|
|
|
27
26
|
import logError from './log/logError.js';
|
|
28
27
|
import logRequest from './log/logRequest.js';
|
|
29
28
|
import operators from '../../build/plugins/operators/server.js';
|
|
30
|
-
import
|
|
29
|
+
import jsMap from '../../build/plugins/operators/serverJsMap.js';
|
|
31
30
|
import getAuthOptions from './auth/getAuthOptions.js';
|
|
32
31
|
import setSentryUser from './sentry/setSentryUser.js';
|
|
33
32
|
|
|
@@ -40,39 +39,12 @@ try {
|
|
|
40
39
|
|
|
41
40
|
const secrets = getSecretsFromEnv();
|
|
42
41
|
|
|
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
|
-
|
|
67
42
|
function apiWrapper(handler) {
|
|
68
43
|
return async function wrappedHandler(req, res) {
|
|
69
|
-
const buildDirectory = path.join(process.cwd(), 'build');
|
|
70
|
-
const jsMap = loadDynamicJsMap(buildDirectory);
|
|
71
|
-
|
|
72
44
|
const context = {
|
|
73
45
|
// Important to give absolute path so Next can trace build files
|
|
74
46
|
rid: uuid(),
|
|
75
|
-
buildDirectory,
|
|
47
|
+
buildDirectory: path.join(process.cwd(), 'build'),
|
|
76
48
|
configDirectory: process.env.LOWDEFY_DIRECTORY_CONFIG || process.cwd(),
|
|
77
49
|
config,
|
|
78
50
|
connections,
|
|
@@ -14,20 +14,67 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import
|
|
17
|
+
import pino from 'pino';
|
|
18
18
|
|
|
19
|
-
|
|
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({
|
|
20
31
|
name: 'lowdefy_server',
|
|
21
32
|
level: process.env.LOWDEFY_LOG_LEVEL ?? 'info',
|
|
22
33
|
base: { pid: undefined, hostname: undefined },
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
+
},
|
|
27
45
|
});
|
|
28
46
|
|
|
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
|
+
|
|
29
75
|
function createLogger(metadata = {}) {
|
|
30
|
-
|
|
76
|
+
const childLogger = pinoLogger.child(metadata);
|
|
77
|
+
return wrapLogger(childLogger);
|
|
31
78
|
}
|
|
32
79
|
|
|
33
80
|
export default createLogger;
|
package/manager/getContext.mjs
CHANGED
|
@@ -19,7 +19,7 @@ import path from 'path';
|
|
|
19
19
|
import yargs from 'yargs';
|
|
20
20
|
import { hideBin } from 'yargs/helpers';
|
|
21
21
|
|
|
22
|
-
import
|
|
22
|
+
import createLogger from './utils/createLogger.mjs';
|
|
23
23
|
import checkMockUserWarning from './processes/checkMockUserWarning.mjs';
|
|
24
24
|
import initialBuild from './processes/initialBuild.mjs';
|
|
25
25
|
import installPlugins from './processes/installPlugins.mjs';
|
|
@@ -32,7 +32,6 @@ 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';
|
|
36
35
|
|
|
37
36
|
const argv = yargs(hideBin(process.argv)).array('watch').array('watchIgnore').argv;
|
|
38
37
|
|
|
@@ -60,30 +59,13 @@ async function getContext() {
|
|
|
60
59
|
: [],
|
|
61
60
|
},
|
|
62
61
|
version: env.npm_package_version,
|
|
63
|
-
|
|
64
|
-
// JIT build state
|
|
65
|
-
pageCache: new PageCache(),
|
|
66
|
-
pageRegistry: null,
|
|
67
|
-
fileDependencyMap: null,
|
|
68
|
-
buildContext: null,
|
|
69
62
|
};
|
|
70
63
|
|
|
71
64
|
context.packageManagerCmd = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm';
|
|
72
65
|
context.checkMockUserWarning = checkMockUserWarning(context);
|
|
73
66
|
context.initialBuild = initialBuild(context);
|
|
74
67
|
context.installPlugins = installPlugins(context);
|
|
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
|
-
|
|
68
|
+
context.lowdefyBuild = lowdefyBuild(context);
|
|
87
69
|
context.nextBuild = nextBuild(context);
|
|
88
70
|
context.readDotEnv = readDotEnv(context);
|
|
89
71
|
context.reloadClients = reloadClients(context);
|
|
@@ -18,7 +18,7 @@ import { spawnProcess } from '@lowdefy/node-utils';
|
|
|
18
18
|
|
|
19
19
|
function installPlugins({ logger, packageManagerCmd }) {
|
|
20
20
|
return async () => {
|
|
21
|
-
logger.
|
|
21
|
+
logger.info({ print: 'spin' }, 'Installing plugins...');
|
|
22
22
|
await spawnProcess({
|
|
23
23
|
processOptions: {
|
|
24
24
|
// https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2#command-injection-via-args-parameter-of-child_processspawn-without-shell-option-enabled-on-windows-cve-2024-27980---high
|
|
@@ -28,7 +28,7 @@ function installPlugins({ logger, packageManagerCmd }) {
|
|
|
28
28
|
args: ['install', '--no-frozen-lockfile'],
|
|
29
29
|
stdOutLineHandler: (line) => logger.debug(line),
|
|
30
30
|
});
|
|
31
|
-
logger.
|
|
31
|
+
logger.info({ print: 'log' }, 'Installed plugins.');
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -14,35 +14,21 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import
|
|
17
|
+
import build from '@lowdefy/build';
|
|
18
18
|
import createCustomPluginTypesMap from '../utils/createCustomPluginTypesMap.mjs';
|
|
19
19
|
|
|
20
|
-
function lowdefyBuild({ directories, logger, options
|
|
20
|
+
function lowdefyBuild({ directories, logger, options }) {
|
|
21
21
|
return async () => {
|
|
22
|
-
logger.
|
|
22
|
+
logger.info({ print: 'spin' }, 'Building config...');
|
|
23
23
|
const customTypesMap = await createCustomPluginTypesMap({ directories, logger });
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
}
|
|
24
|
+
await build({
|
|
25
|
+
customTypesMap,
|
|
26
|
+
directories,
|
|
27
|
+
logger,
|
|
28
|
+
refResolver: options.refResolver,
|
|
29
|
+
stage: 'dev',
|
|
30
|
+
});
|
|
31
|
+
logger.info({ print: 'log' }, 'Built config.');
|
|
46
32
|
};
|
|
47
33
|
}
|
|
48
34
|
|
|
@@ -18,28 +18,13 @@ import { spawnProcess } from '@lowdefy/node-utils';
|
|
|
18
18
|
|
|
19
19
|
function nextBuild({ bin, logger }) {
|
|
20
20
|
return async () => {
|
|
21
|
-
logger.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
}
|
|
42
|
-
logger.ui.log('Built app.');
|
|
21
|
+
logger.info({ print: 'spin' }, 'Building app...');
|
|
22
|
+
await spawnProcess({
|
|
23
|
+
command: 'node',
|
|
24
|
+
args: [bin.next, 'build'],
|
|
25
|
+
stdOutLineHandler: (line) => logger.debug(line),
|
|
26
|
+
});
|
|
27
|
+
logger.info({ print: 'log' }, 'Built app.');
|
|
43
28
|
};
|
|
44
29
|
}
|
|
45
30
|
|
|
@@ -19,9 +19,9 @@ import startServer from './startServer.mjs';
|
|
|
19
19
|
function restartServer(context) {
|
|
20
20
|
return () => {
|
|
21
21
|
context.shutdownServer();
|
|
22
|
-
context.logger.
|
|
22
|
+
context.logger.info({ print: 'spin' }, 'Restarting server...');
|
|
23
23
|
startServer(context);
|
|
24
|
-
context.logger.
|
|
24
|
+
context.logger.info({ print: 'succeed' }, 'Restarted server.');
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -21,7 +21,7 @@ function shutdownServer(context) {
|
|
|
21
21
|
`Existing next server with pid ${context.nextServer.pid}, killed: ${context.nextServer.killed}`
|
|
22
22
|
);
|
|
23
23
|
if (!context.nextServer.killed) {
|
|
24
|
-
context.logger.
|
|
24
|
+
context.logger.info({ print: 'spin' }, 'Shutting down server...');
|
|
25
25
|
context.nextServer.kill();
|
|
26
26
|
context.logger.debug(
|
|
27
27
|
`Killed next server with pid ${context.nextServer.pid}, killed: ${context.nextServer.killed}`
|
|
@@ -14,43 +14,26 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
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
|
-
}
|
|
17
|
+
import { spawnProcess } from '@lowdefy/node-utils';
|
|
18
|
+
import createStdOutLineHandler from '../utils/createStdOutLineHandler.mjs';
|
|
31
19
|
|
|
32
20
|
function startServer(context) {
|
|
33
21
|
context.shutdownServer();
|
|
34
22
|
|
|
35
|
-
const nextServer =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
23
|
+
const nextServer = spawnProcess({
|
|
24
|
+
stdOutLineHandler: createStdOutLineHandler({ context }),
|
|
25
|
+
stdErrLineHandler: (line) => context.logger.error(line),
|
|
26
|
+
command: 'node',
|
|
27
|
+
args: [context.bin.next, 'start'],
|
|
28
|
+
processOptions: {
|
|
29
|
+
env: {
|
|
30
|
+
...process.env,
|
|
31
|
+
LOWDEFY_DIRECTORY_CONFIG: context.directories.config,
|
|
32
|
+
PORT: context.options.port,
|
|
33
|
+
},
|
|
41
34
|
},
|
|
35
|
+
returnProcess: true,
|
|
42
36
|
});
|
|
43
|
-
|
|
44
|
-
const stdErrLineHandler = createStdErrLineHandler({ context });
|
|
45
|
-
nextServer.stderr.on('data', (data) => {
|
|
46
|
-
data
|
|
47
|
-
.toString('utf8')
|
|
48
|
-
.split('\n')
|
|
49
|
-
.forEach((line) => {
|
|
50
|
-
if (line) stdErrLineHandler(line);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
37
|
context.logger.debug(`Started next server with pid ${nextServer.pid}.`);
|
|
55
38
|
nextServer.on('exit', (code, signal) => {
|
|
56
39
|
context.logger.debug(`nextServer exit ${nextServer.pid}, signal: ${signal}, code: ${code}`);
|
package/manager/run.mjs
CHANGED
|
@@ -19,7 +19,6 @@ 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';
|
|
23
22
|
|
|
24
23
|
/*
|
|
25
24
|
The run script does the following:
|
|
@@ -86,7 +85,6 @@ try {
|
|
|
86
85
|
// because chokidar sometimes doesn't fire this event, and it seems like there isn't an issue with not waiting.
|
|
87
86
|
context.startWatchers();
|
|
88
87
|
|
|
89
|
-
await checkPortAvailable(context.options.port);
|
|
90
88
|
startServer(context);
|
|
91
89
|
await wait(800);
|
|
92
90
|
if (process.env.LOWDEFY_SERVER_DEV_OPEN_BROWSER === 'true') {
|
|
@@ -95,7 +93,13 @@ try {
|
|
|
95
93
|
}
|
|
96
94
|
await new Promise(() => {});
|
|
97
95
|
} catch (error) {
|
|
98
|
-
|
|
96
|
+
// If error is already formatted (from error collection), just show the message
|
|
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
|
+
}
|
|
99
103
|
context.shutdownServer();
|
|
100
104
|
process.exit();
|
|
101
105
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
import pino from 'pino';
|
|
17
|
+
|
|
18
|
+
function createLogger({ level = 'info' }) {
|
|
19
|
+
const logger = pino({
|
|
20
|
+
name: 'lowdefy build',
|
|
21
|
+
level,
|
|
22
|
+
base: { pid: undefined, hostname: undefined },
|
|
23
|
+
mixin: (context, level) => {
|
|
24
|
+
return {
|
|
25
|
+
...context,
|
|
26
|
+
print: context.print ?? logger.levels.labels[level],
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
serializers: {
|
|
30
|
+
err: (err) => ({
|
|
31
|
+
message: err.message,
|
|
32
|
+
name: err.name,
|
|
33
|
+
stack: err.stack,
|
|
34
|
+
source: err.source,
|
|
35
|
+
config: err.config,
|
|
36
|
+
configKey: err.configKey,
|
|
37
|
+
isServiceError: err.isServiceError,
|
|
38
|
+
}),
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
return logger;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default createLogger;
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
function createStdOutLineHandler({ context }) {
|
|
18
|
+
const { logger } = context;
|
|
19
|
+
function stdOutLineHandler(line) {
|
|
20
|
+
try {
|
|
21
|
+
const { level, msg, source, err } = JSON.parse(line);
|
|
22
|
+
if (!level) return;
|
|
23
|
+
|
|
24
|
+
const levelLabel = logger.levels.labels[level];
|
|
25
|
+
|
|
26
|
+
// Extract source from err (pino error serialization) or top-level (merging object)
|
|
27
|
+
const resolvedSource = err?.source ?? source;
|
|
28
|
+
|
|
29
|
+
// Error/warn with source: show source line (info/blue) before the message
|
|
30
|
+
if (resolvedSource && level >= 40) {
|
|
31
|
+
logger.info({ print: 'info' }, resolvedSource);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (msg) {
|
|
35
|
+
const print = level === 30 ? 'info' : levelLabel;
|
|
36
|
+
logger[levelLabel]({ print }, msg);
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
logger.info({ print: 'info' }, line);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return stdOutLineHandler;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default createStdOutLineHandler;
|