@lowdefy/server 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/Page.jsx +14 -1
- package/client/clientBuildImports.test.mjs +61 -0
- package/client/shouldReloadForBuild.js +48 -0
- package/client/shouldReloadForBuild.test.mjs +75 -0
- package/lib/client/auth/AuthConfigured.jsx +6 -7
- package/lib/server/auth/getAuthConfig.js +2 -0
- package/lib/server/log/createHandleError.js +9 -4
- package/lib/server/log/createHandleError.test.mjs +85 -0
- package/lib/server/log/createLogger.js +10 -0
- package/lib/server/log/createLogger.test.mjs +102 -0
- package/lib/server/log/logRequest.js +7 -1
- package/lib/server/log/maskCredentialKeys.js +37 -0
- package/lib/server/log/projectErrorForLog.js +55 -0
- package/lib/server/log/redactUrlQuery.js +75 -0
- package/lib/server/log/redactUrlQuery.test.mjs +73 -0
- package/lib/server/log/serializeErrorForLog.js +25 -0
- package/lib/server/log/serializeErrorForLog.test.mjs +325 -0
- package/lib/server/scrubSecrets.js +21 -0
- package/lib/server/sentry/captureSentryError.js +12 -0
- package/lib/server/sentry/captureSentryError.test.mjs +104 -0
- package/lib/server/sentry/initSentry.js +11 -0
- package/lib/server/sentry/initSentry.test.mjs +237 -0
- package/lib/server/sentry/scrubEvent.js +29 -0
- package/lib/server/sentry/scrubEvent.test.mjs +88 -0
- package/package.json +30 -31
- package/package.original.json +30 -31
- package/src/index.js +10 -0
- package/src/middleware/apiContext.js +3 -0
- package/src/middleware/apiContext.test.mjs +118 -0
- package/src/routes/apiPage.js +4 -1
- package/src/routes/apiPage.test.mjs +59 -0
- package/src/routes/cronForward.js +1 -1
- package/lib/server/apiWrapper.js +0 -87
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import fs from 'fs';
|
|
18
|
+
import path from 'path';
|
|
19
|
+
import { fileURLToPath } from 'url';
|
|
20
|
+
|
|
21
|
+
import { Hono } from 'hono';
|
|
22
|
+
import { jest } from '@jest/globals';
|
|
23
|
+
|
|
24
|
+
const secret = 'planted-apicontext-secret';
|
|
25
|
+
process.env.LOWDEFY_SECRET_TEST = secret;
|
|
26
|
+
|
|
27
|
+
jest.unstable_mockModule('@lowdefy/api', () => ({ createApiContext: jest.fn() }));
|
|
28
|
+
jest.unstable_mockModule('../../lib/build/appMeta.js', () => ({ default: {} }));
|
|
29
|
+
jest.unstable_mockModule('../../lib/build/config.js', () => ({ default: {} }));
|
|
30
|
+
jest.unstable_mockModule('../../lib/build/i18n.js', () => ({ default: {} }));
|
|
31
|
+
jest.unstable_mockModule('../../lib/build/logger.js', () => ({ default: { sentry: {} } }));
|
|
32
|
+
jest.unstable_mockModule('../../lib/server/log/createHandleError.js', () => ({
|
|
33
|
+
default: jest.fn(() => jest.fn()),
|
|
34
|
+
}));
|
|
35
|
+
jest.unstable_mockModule('../../lib/server/log/createLogger.js', () => ({
|
|
36
|
+
default: jest.fn(() => ({ debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() })),
|
|
37
|
+
}));
|
|
38
|
+
jest.unstable_mockModule('../../lib/server/fileCache.js', () => ({ default: {} }));
|
|
39
|
+
jest.unstable_mockModule('../../lib/server/auth/session.js', () => ({
|
|
40
|
+
default: jest.fn(async () => undefined),
|
|
41
|
+
}));
|
|
42
|
+
jest.unstable_mockModule('../../lib/server/auth/strategies.js', () => ({
|
|
43
|
+
default: jest.fn(async () => null),
|
|
44
|
+
}));
|
|
45
|
+
jest.unstable_mockModule('../../lib/server/log/logRequest.js', () => ({ default: jest.fn() }));
|
|
46
|
+
jest.unstable_mockModule('../../lib/server/sentry/setSentryUser.js', () => ({
|
|
47
|
+
default: jest.fn(),
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
// apiContext statically imports the app build's plugin artifacts, which only
|
|
51
|
+
// exist in a built app (build/** is gitignored). Materialize the minimal set
|
|
52
|
+
// it needs so the import resolves; clean up only what this test created.
|
|
53
|
+
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
54
|
+
const pluginsDir = path.resolve(dirname, '../../build/plugins');
|
|
55
|
+
const fixtures = [
|
|
56
|
+
{ file: 'agents.js', content: 'export default {};\n' },
|
|
57
|
+
{ file: 'connections.js', content: 'export default {};\n' },
|
|
58
|
+
{
|
|
59
|
+
file: 'notifications.js',
|
|
60
|
+
content:
|
|
61
|
+
'export default {};\nexport const interpolateProperties = () => {};\nexport const renderEmail = () => {};\n',
|
|
62
|
+
},
|
|
63
|
+
{ file: path.join('operators', 'server.js'), content: 'export default {};\n' },
|
|
64
|
+
{ file: path.join('operators', 'serverJsMap.js'), content: 'export default {};\n' },
|
|
65
|
+
{ file: 'websockets.js', content: 'export default {};\n' },
|
|
66
|
+
].map(({ file, content }) => ({ absolutePath: path.join(pluginsDir, file), content }));
|
|
67
|
+
|
|
68
|
+
const createdFixtures = [];
|
|
69
|
+
fixtures.forEach(({ absolutePath, content }) => {
|
|
70
|
+
if (!fs.existsSync(absolutePath)) {
|
|
71
|
+
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
|
72
|
+
fs.writeFileSync(absolutePath, content);
|
|
73
|
+
createdFixtures.push(absolutePath);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// A checkout that has built an app already holds real artifacts here, which import plugins this
|
|
78
|
+
// package does not depend on, so the stubs are also mocked over whatever is on disk.
|
|
79
|
+
fixtures.forEach(({ absolutePath }) => {
|
|
80
|
+
jest.unstable_mockModule(absolutePath, () => ({
|
|
81
|
+
default: {},
|
|
82
|
+
interpolateProperties: () => {},
|
|
83
|
+
renderEmail: () => {},
|
|
84
|
+
}));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
afterAll(() => {
|
|
88
|
+
createdFixtures.forEach((absolutePath) => fs.rmSync(absolutePath, { force: true }));
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const { default: apiContext } = await import('./apiContext.js');
|
|
92
|
+
|
|
93
|
+
function createApp() {
|
|
94
|
+
const app = new Hono();
|
|
95
|
+
app.use('*', apiContext());
|
|
96
|
+
app.get('/api/ping', (c) => {
|
|
97
|
+
const context = c.get('lowdefyContext');
|
|
98
|
+
return c.json({
|
|
99
|
+
mode: context.mode,
|
|
100
|
+
scrubbed: context.scrubSecrets(`token ${secret} end`),
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
return app;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
test('apiContext sets mode to prod', async () => {
|
|
107
|
+
const res = await createApp().request('/api/ping');
|
|
108
|
+
expect(res.status).toEqual(200);
|
|
109
|
+
const body = await res.json();
|
|
110
|
+
expect(body.mode).toEqual('prod');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('apiContext scrubSecrets redacts a planted secret', async () => {
|
|
114
|
+
const res = await createApp().request('/api/ping');
|
|
115
|
+
expect(res.status).toEqual(200);
|
|
116
|
+
const body = await res.json();
|
|
117
|
+
expect(body.scrubbed).toEqual('token [REDACTED] end');
|
|
118
|
+
});
|
package/src/routes/apiPage.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
import { getPageConfig } from '@lowdefy/api';
|
|
18
18
|
|
|
19
|
+
import appMeta from '../../lib/build/appMeta.js';
|
|
19
20
|
import getPathSegments from '../lib/getPathSegments.js';
|
|
20
21
|
|
|
21
22
|
// Page config as JSON for client-side SPA navigation. The first page load is
|
|
@@ -31,7 +32,9 @@ async function apiPageHandler(c) {
|
|
|
31
32
|
return c.json({ pageConfig: null }, 404);
|
|
32
33
|
}
|
|
33
34
|
context.logger.info({ event: 'api_page_view', pageId });
|
|
34
|
-
|
|
35
|
+
// buildId lets a tab that loaded its bundle from an earlier deploy notice
|
|
36
|
+
// that this config comes from a newer build and reload (client/Page.jsx).
|
|
37
|
+
return c.json({ buildId: appMeta.buildId, pageConfig });
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
export default apiPageHandler;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Hono } from 'hono';
|
|
18
|
+
import { jest } from '@jest/globals';
|
|
19
|
+
|
|
20
|
+
const mockGetPageConfig = jest.fn();
|
|
21
|
+
jest.unstable_mockModule('@lowdefy/api', () => ({
|
|
22
|
+
getPageConfig: mockGetPageConfig,
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
jest.unstable_mockModule('../../lib/build/appMeta.js', () => ({
|
|
26
|
+
default: { buildId: 'build-abc' },
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
const { default: apiPageHandler } = await import('./apiPage.js');
|
|
30
|
+
|
|
31
|
+
function createApp() {
|
|
32
|
+
const app = new Hono();
|
|
33
|
+
app.use('*', async (c, next) => {
|
|
34
|
+
c.set('lowdefyContext', {
|
|
35
|
+
logger: { debug: jest.fn(), info: jest.fn(), error: jest.fn() },
|
|
36
|
+
});
|
|
37
|
+
await next();
|
|
38
|
+
});
|
|
39
|
+
app.get('/api/page/*', apiPageHandler);
|
|
40
|
+
return app;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
mockGetPageConfig.mockReset();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('apiPageHandler returns the pageConfig stamped with the build id', async () => {
|
|
48
|
+
mockGetPageConfig.mockResolvedValue({ id: 'invoices' });
|
|
49
|
+
const res = await createApp().request('/api/page/invoices');
|
|
50
|
+
expect(res.status).toEqual(200);
|
|
51
|
+
expect(await res.json()).toEqual({ buildId: 'build-abc', pageConfig: { id: 'invoices' } });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('apiPageHandler returns 404 when the page is not found', async () => {
|
|
55
|
+
mockGetPageConfig.mockResolvedValue(null);
|
|
56
|
+
const res = await createApp().request('/api/page/missing');
|
|
57
|
+
expect(res.status).toEqual(404);
|
|
58
|
+
expect(await res.json()).toEqual({ pageConfig: null });
|
|
59
|
+
});
|
|
@@ -20,7 +20,7 @@ import getPathSegments from '../lib/getPathSegments.js';
|
|
|
20
20
|
|
|
21
21
|
// Triggered by Vercel Cron on the production deployment: an HTTP GET to
|
|
22
22
|
// /api/cron-forward/<environment>/<endpointId>, registered by `lowdefy vercel-output` for the
|
|
23
|
-
// schedules of every environment declared with a
|
|
23
|
+
// schedules of every environment declared with a cron.secret in config.environments. Vercel fires crons
|
|
24
24
|
// only on production, so production pings that environment's own /api/cron/<endpointId> (with the
|
|
25
25
|
// environment's CRON_SECRET) and answers immediately. Same transport auth as cron (CRON_SECRET,
|
|
26
26
|
// fails closed).
|
package/lib/server/apiWrapper.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { createApiContext, redactErrorResponse } from '@lowdefy/api';
|
|
19
|
-
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
20
|
-
import { v4 as uuid } from 'uuid';
|
|
21
|
-
|
|
22
|
-
import agents from '../../build/plugins/agents.js';
|
|
23
|
-
import appMeta from '../build/appMeta.js';
|
|
24
|
-
import config from '../build/config.js';
|
|
25
|
-
import i18nConfig from '../build/i18n.js';
|
|
26
|
-
import connections from '../../build/plugins/connections.js';
|
|
27
|
-
import createLogger from './log/createLogger.js';
|
|
28
|
-
import fileCache from './fileCache.js';
|
|
29
|
-
import getServerSession from './auth/getServerSession.js';
|
|
30
|
-
import createHandleError from './log/createHandleError.js';
|
|
31
|
-
import logRequest from './log/logRequest.js';
|
|
32
|
-
import operators from '../../build/plugins/operators/server.js';
|
|
33
|
-
import jsMap from '../../build/plugins/operators/serverJsMap.js';
|
|
34
|
-
import getAuthOptions from './auth/getAuthOptions.js';
|
|
35
|
-
import loggerConfig from '../build/logger.js';
|
|
36
|
-
import setSentryUser from './sentry/setSentryUser.js';
|
|
37
|
-
|
|
38
|
-
const secrets = getSecretsFromEnv();
|
|
39
|
-
|
|
40
|
-
function apiWrapper(handler) {
|
|
41
|
-
return async function wrappedHandler(req, res) {
|
|
42
|
-
const context = {
|
|
43
|
-
// Important to give absolute path so Next can trace build files
|
|
44
|
-
rid: uuid(),
|
|
45
|
-
agents,
|
|
46
|
-
appMeta,
|
|
47
|
-
buildDirectory: path.join(process.cwd(), 'build'),
|
|
48
|
-
config,
|
|
49
|
-
connections,
|
|
50
|
-
fileCache,
|
|
51
|
-
headers: req?.headers,
|
|
52
|
-
i18n: i18nConfig,
|
|
53
|
-
jsMap,
|
|
54
|
-
handleError: async (err) => {
|
|
55
|
-
console.error(err);
|
|
56
|
-
},
|
|
57
|
-
logger: console,
|
|
58
|
-
operators,
|
|
59
|
-
req,
|
|
60
|
-
res,
|
|
61
|
-
secrets,
|
|
62
|
-
};
|
|
63
|
-
try {
|
|
64
|
-
context.logger = createLogger({ rid: context.rid });
|
|
65
|
-
context.handleError = createHandleError({ context });
|
|
66
|
-
context.authOptions = getAuthOptions(context);
|
|
67
|
-
if (!req.url.startsWith('/api/auth')) {
|
|
68
|
-
context.session = await getServerSession(context);
|
|
69
|
-
// Set Sentry user context for authenticated requests
|
|
70
|
-
setSentryUser({
|
|
71
|
-
user: context.session?.user,
|
|
72
|
-
sentryConfig: loggerConfig.sentry,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
createApiContext(context);
|
|
76
|
-
logRequest({ context });
|
|
77
|
-
// Await here so that if handler throws it is caught.
|
|
78
|
-
const response = await handler({ context, req, res });
|
|
79
|
-
return response;
|
|
80
|
-
} catch (error) {
|
|
81
|
-
await context.handleError(error);
|
|
82
|
-
res.status(500).json(redactErrorResponse(context, error));
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export default apiWrapper;
|