@lowdefy/server 7.2.0 → 7.4.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/package.json +28 -28
- package/package.original.json +28 -28
- package/src/app.js +17 -3
- package/src/app.mobile.test.mjs +1 -1
- package/src/app.test.mjs +1 -1
- package/src/app.timeout.test.mjs +141 -0
- package/src/middleware/requestTimeout.js +7 -1
- package/src/routes/websocket.js +65 -39
- package/src/routes/websocket.test.mjs +75 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/server",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.4.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -43,32 +43,32 @@
|
|
|
43
43
|
"@better-auth/oauth-provider": "1.7.7",
|
|
44
44
|
"@better-auth/passkey": "1.7.7",
|
|
45
45
|
"@hono/mcp": "0.3.2",
|
|
46
|
-
"@hono/node-server": "2.
|
|
47
|
-
"@lowdefy/actions-core": "7.
|
|
48
|
-
"@lowdefy/api": "7.
|
|
49
|
-
"@lowdefy/block-utils": "7.
|
|
50
|
-
"@lowdefy/blocks-aggrid": "7.
|
|
51
|
-
"@lowdefy/blocks-antd": "7.
|
|
52
|
-
"@lowdefy/blocks-antd-x": "7.
|
|
53
|
-
"@lowdefy/blocks-basic": "7.
|
|
54
|
-
"@lowdefy/blocks-captcha": "7.
|
|
55
|
-
"@lowdefy/blocks-echarts": "7.
|
|
56
|
-
"@lowdefy/blocks-files": "7.
|
|
57
|
-
"@lowdefy/blocks-loaders": "7.
|
|
58
|
-
"@lowdefy/blocks-markdown": "7.
|
|
59
|
-
"@lowdefy/blocks-table": "7.
|
|
60
|
-
"@lowdefy/blocks-tiptap": "7.
|
|
61
|
-
"@lowdefy/client": "7.
|
|
62
|
-
"@lowdefy/connection-anthropic": "7.
|
|
63
|
-
"@lowdefy/errors": "7.
|
|
64
|
-
"@lowdefy/helpers": "7.
|
|
65
|
-
"@lowdefy/layout": "7.
|
|
66
|
-
"@lowdefy/logger": "7.
|
|
67
|
-
"@lowdefy/node-utils": "7.
|
|
68
|
-
"@lowdefy/operators-js": "7.
|
|
69
|
-
"@lowdefy/operators-nunjucks": "7.
|
|
70
|
-
"@lowdefy/plugin-better-auth": "7.
|
|
71
|
-
"@lowdefy/websockets-core": "7.
|
|
46
|
+
"@hono/node-server": "2.1.3",
|
|
47
|
+
"@lowdefy/actions-core": "7.4.0",
|
|
48
|
+
"@lowdefy/api": "7.4.0",
|
|
49
|
+
"@lowdefy/block-utils": "7.4.0",
|
|
50
|
+
"@lowdefy/blocks-aggrid": "7.4.0",
|
|
51
|
+
"@lowdefy/blocks-antd": "7.4.0",
|
|
52
|
+
"@lowdefy/blocks-antd-x": "7.4.0",
|
|
53
|
+
"@lowdefy/blocks-basic": "7.4.0",
|
|
54
|
+
"@lowdefy/blocks-captcha": "7.4.0",
|
|
55
|
+
"@lowdefy/blocks-echarts": "7.4.0",
|
|
56
|
+
"@lowdefy/blocks-files": "7.4.0",
|
|
57
|
+
"@lowdefy/blocks-loaders": "7.4.0",
|
|
58
|
+
"@lowdefy/blocks-markdown": "7.4.0",
|
|
59
|
+
"@lowdefy/blocks-table": "7.4.0",
|
|
60
|
+
"@lowdefy/blocks-tiptap": "7.4.0",
|
|
61
|
+
"@lowdefy/client": "7.4.0",
|
|
62
|
+
"@lowdefy/connection-anthropic": "7.4.0",
|
|
63
|
+
"@lowdefy/errors": "7.4.0",
|
|
64
|
+
"@lowdefy/helpers": "7.4.0",
|
|
65
|
+
"@lowdefy/layout": "7.4.0",
|
|
66
|
+
"@lowdefy/logger": "7.4.0",
|
|
67
|
+
"@lowdefy/node-utils": "7.4.0",
|
|
68
|
+
"@lowdefy/operators-js": "7.4.0",
|
|
69
|
+
"@lowdefy/operators-nunjucks": "7.4.0",
|
|
70
|
+
"@lowdefy/plugin-better-auth": "7.4.0",
|
|
71
|
+
"@lowdefy/websockets-core": "7.4.0",
|
|
72
72
|
"@sentry/browser": "10.57.0",
|
|
73
73
|
"@sentry/node": "10.57.0",
|
|
74
74
|
"@sentry/node-core": "10.57.0",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"ws": "8.21.0"
|
|
89
89
|
},
|
|
90
90
|
"devDependencies": {
|
|
91
|
-
"@lowdefy/build": "7.
|
|
91
|
+
"@lowdefy/build": "7.4.0",
|
|
92
92
|
"@tailwindcss/postcss": "4.3.3",
|
|
93
93
|
"tailwindcss": "4.3.3",
|
|
94
94
|
"vitest": "4.1.12",
|
package/package.original.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/server",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.4.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -51,32 +51,32 @@
|
|
|
51
51
|
"@better-auth/oauth-provider": "1.7.7",
|
|
52
52
|
"@better-auth/passkey": "1.7.7",
|
|
53
53
|
"@hono/mcp": "0.3.2",
|
|
54
|
-
"@hono/node-server": "2.
|
|
55
|
-
"@lowdefy/actions-core": "7.
|
|
56
|
-
"@lowdefy/api": "7.
|
|
57
|
-
"@lowdefy/block-utils": "7.
|
|
58
|
-
"@lowdefy/blocks-aggrid": "7.
|
|
59
|
-
"@lowdefy/blocks-antd": "7.
|
|
60
|
-
"@lowdefy/blocks-antd-x": "7.
|
|
61
|
-
"@lowdefy/blocks-basic": "7.
|
|
62
|
-
"@lowdefy/blocks-captcha": "7.
|
|
63
|
-
"@lowdefy/blocks-echarts": "7.
|
|
64
|
-
"@lowdefy/blocks-files": "7.
|
|
65
|
-
"@lowdefy/blocks-loaders": "7.
|
|
66
|
-
"@lowdefy/blocks-markdown": "7.
|
|
67
|
-
"@lowdefy/blocks-table": "7.
|
|
68
|
-
"@lowdefy/blocks-tiptap": "7.
|
|
69
|
-
"@lowdefy/client": "7.
|
|
70
|
-
"@lowdefy/connection-anthropic": "7.
|
|
71
|
-
"@lowdefy/errors": "7.
|
|
72
|
-
"@lowdefy/helpers": "7.
|
|
73
|
-
"@lowdefy/layout": "7.
|
|
74
|
-
"@lowdefy/logger": "7.
|
|
75
|
-
"@lowdefy/node-utils": "7.
|
|
76
|
-
"@lowdefy/operators-js": "7.
|
|
77
|
-
"@lowdefy/operators-nunjucks": "7.
|
|
78
|
-
"@lowdefy/plugin-better-auth": "7.
|
|
79
|
-
"@lowdefy/websockets-core": "7.
|
|
54
|
+
"@hono/node-server": "2.1.3",
|
|
55
|
+
"@lowdefy/actions-core": "7.4.0",
|
|
56
|
+
"@lowdefy/api": "7.4.0",
|
|
57
|
+
"@lowdefy/block-utils": "7.4.0",
|
|
58
|
+
"@lowdefy/blocks-aggrid": "7.4.0",
|
|
59
|
+
"@lowdefy/blocks-antd": "7.4.0",
|
|
60
|
+
"@lowdefy/blocks-antd-x": "7.4.0",
|
|
61
|
+
"@lowdefy/blocks-basic": "7.4.0",
|
|
62
|
+
"@lowdefy/blocks-captcha": "7.4.0",
|
|
63
|
+
"@lowdefy/blocks-echarts": "7.4.0",
|
|
64
|
+
"@lowdefy/blocks-files": "7.4.0",
|
|
65
|
+
"@lowdefy/blocks-loaders": "7.4.0",
|
|
66
|
+
"@lowdefy/blocks-markdown": "7.4.0",
|
|
67
|
+
"@lowdefy/blocks-table": "7.4.0",
|
|
68
|
+
"@lowdefy/blocks-tiptap": "7.4.0",
|
|
69
|
+
"@lowdefy/client": "7.4.0",
|
|
70
|
+
"@lowdefy/connection-anthropic": "7.4.0",
|
|
71
|
+
"@lowdefy/errors": "7.4.0",
|
|
72
|
+
"@lowdefy/helpers": "7.4.0",
|
|
73
|
+
"@lowdefy/layout": "7.4.0",
|
|
74
|
+
"@lowdefy/logger": "7.4.0",
|
|
75
|
+
"@lowdefy/node-utils": "7.4.0",
|
|
76
|
+
"@lowdefy/operators-js": "7.4.0",
|
|
77
|
+
"@lowdefy/operators-nunjucks": "7.4.0",
|
|
78
|
+
"@lowdefy/plugin-better-auth": "7.4.0",
|
|
79
|
+
"@lowdefy/websockets-core": "7.4.0",
|
|
80
80
|
"@sentry/browser": "10.57.0",
|
|
81
81
|
"@sentry/node": "10.57.0",
|
|
82
82
|
"@sentry/node-core": "10.57.0",
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"ws": "8.21.0"
|
|
97
97
|
},
|
|
98
98
|
"devDependencies": {
|
|
99
|
-
"@lowdefy/build": "7.
|
|
99
|
+
"@lowdefy/build": "7.4.0",
|
|
100
100
|
"@tailwindcss/postcss": "4.3.3",
|
|
101
101
|
"tailwindcss": "4.3.3",
|
|
102
102
|
"vitest": "4.1.12",
|
package/src/app.js
CHANGED
|
@@ -55,7 +55,7 @@ import stampBuildId from './middleware/stampBuildId.js';
|
|
|
55
55
|
import stampCaller from './middleware/stampCaller.js';
|
|
56
56
|
import usageHandler from './routes/usage.js';
|
|
57
57
|
import userHandler from './routes/user.js';
|
|
58
|
-
import
|
|
58
|
+
import createWebsocketHandler from './routes/websocket.js';
|
|
59
59
|
|
|
60
60
|
const basePath = lowdefyConfig.basePath ?? '';
|
|
61
61
|
|
|
@@ -71,7 +71,14 @@ const requestTimeoutMs = lowdefyConfig.requestTimeout ?? 30000;
|
|
|
71
71
|
// `clientAddressHeader` names the header a platform's edge sets to the client address on every
|
|
72
72
|
// request (Vercel's x-real-ip). Without it the Node server resolves the address from the
|
|
73
73
|
// connection and config.trustedProxies.
|
|
74
|
-
|
|
74
|
+
// `closeWebSocketsBeforeMaxDuration` is set by the Vercel entry, where the function stops at
|
|
75
|
+
// config.vercel.maxDuration (60 s when unset, as `lowdefy vercel-output` writes it), so each
|
|
76
|
+
// websocket is closed shortly before then (routes/websocket.js).
|
|
77
|
+
function createApp({
|
|
78
|
+
serveStaticAssets = true,
|
|
79
|
+
clientAddressHeader,
|
|
80
|
+
closeWebSocketsBeforeMaxDuration = false,
|
|
81
|
+
} = {}) {
|
|
75
82
|
const app = basePath ? new Hono().basePath(basePath) : new Hono();
|
|
76
83
|
const logger = createLogger({ server: 'lowdefy' });
|
|
77
84
|
|
|
@@ -174,7 +181,14 @@ function createApp({ serveStaticAssets = true, clientAddressHeader } = {}) {
|
|
|
174
181
|
app.all('/api/usage', usageHandler);
|
|
175
182
|
app.all('/api/agent/*', bodyLimit({ maxSize: 10 * 1024 * 1024 }), agentHandler);
|
|
176
183
|
app.all('/api/mcp', bodyLimit({ maxSize: 10 * 1024 * 1024 }), mcpHandler);
|
|
177
|
-
app.get(
|
|
184
|
+
app.get(
|
|
185
|
+
'/api/websocket',
|
|
186
|
+
createWebsocketHandler({
|
|
187
|
+
maxDurationMs: closeWebSocketsBeforeMaxDuration
|
|
188
|
+
? (lowdefyConfig.vercel?.maxDuration ?? 60) * 1000
|
|
189
|
+
: null,
|
|
190
|
+
})
|
|
191
|
+
);
|
|
178
192
|
app.get('/api/page/*', apiPageHandler);
|
|
179
193
|
app.get('/api/mobile/root', mobileRootHandler);
|
|
180
194
|
app.get('/api/user', userHandler);
|
package/src/app.mobile.test.mjs
CHANGED
|
@@ -131,7 +131,7 @@ vi.doMock('./routes/mountOauthDiscovery.js', () => ({ default: () => {} }));
|
|
|
131
131
|
vi.doMock('./routes/request.js', () => ({ default: handler }));
|
|
132
132
|
vi.doMock('./routes/usage.js', () => ({ default: handler }));
|
|
133
133
|
vi.doMock('./routes/user.js', () => ({ default: handler }));
|
|
134
|
-
vi.doMock('./routes/websocket.js', () => ({ default: handler }));
|
|
134
|
+
vi.doMock('./routes/websocket.js', () => ({ default: () => handler }));
|
|
135
135
|
vi.doMock('./html/renderPage.js', () => ({
|
|
136
136
|
default: (c) => c.html('<html></html>'),
|
|
137
137
|
}));
|
package/src/app.test.mjs
CHANGED
|
@@ -46,7 +46,7 @@ vi.doMock('./routes/mountOauthDiscovery.js', () => ({ default: () => {} }));
|
|
|
46
46
|
vi.doMock('./routes/request.js', () => ({ default: handler }));
|
|
47
47
|
vi.doMock('./routes/usage.js', () => ({ default: handler }));
|
|
48
48
|
vi.doMock('./routes/user.js', () => ({ default: handler }));
|
|
49
|
-
vi.doMock('./routes/websocket.js', () => ({ default: handler }));
|
|
49
|
+
vi.doMock('./routes/websocket.js', () => ({ default: () => handler }));
|
|
50
50
|
vi.doMock('./routes/wellKnownFallback.js', () => ({ default: handler }));
|
|
51
51
|
vi.doMock('./html/renderPage.js', () => ({
|
|
52
52
|
default: (c) => c.html('<html></html>'),
|
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
import { vi, beforeEach, expect, test } from 'vitest';
|
|
17
|
+
|
|
18
|
+
// The real app wiring and error handler with a request route that outlives the request
|
|
19
|
+
// timeout: what a request a slow upstream holds past config.requestTimeout reports.
|
|
20
|
+
const REQUEST_TIMEOUT_MS = 50;
|
|
21
|
+
|
|
22
|
+
const captureException = vi.fn();
|
|
23
|
+
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
|
|
24
|
+
// How the slow request route answers, set by each test.
|
|
25
|
+
let lateAnswer;
|
|
26
|
+
// Settles once the handler and every middleware inside apiContext have run on the late answer.
|
|
27
|
+
let lateChain;
|
|
28
|
+
|
|
29
|
+
function handler(c) {
|
|
30
|
+
return c.json({ ok: true });
|
|
31
|
+
}
|
|
32
|
+
// Answers well after the timeout.
|
|
33
|
+
async function slowHandler(c) {
|
|
34
|
+
await new Promise((resolve) => setTimeout(resolve, REQUEST_TIMEOUT_MS * 3));
|
|
35
|
+
return lateAnswer(c);
|
|
36
|
+
}
|
|
37
|
+
function largeHandler(c) {
|
|
38
|
+
return c.json({ rows: 'x'.repeat(4096) });
|
|
39
|
+
}
|
|
40
|
+
function passThrough() {
|
|
41
|
+
return (c, next) => next();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
vi.doMock('./routes/agent.js', () => ({ default: handler }));
|
|
45
|
+
vi.doMock('./routes/apiPage.js', () => ({ default: largeHandler }));
|
|
46
|
+
vi.doMock('./routes/auth.js', () => ({ default: () => handler }));
|
|
47
|
+
vi.doMock('./routes/clientError.js', () => ({ default: handler }));
|
|
48
|
+
vi.doMock('./routes/cron.js', () => ({ default: handler }));
|
|
49
|
+
vi.doMock('./routes/cronForward.js', () => ({ default: handler }));
|
|
50
|
+
vi.doMock('./routes/detached.js', () => ({ default: handler }));
|
|
51
|
+
vi.doMock('./routes/endpoints.js', () => ({ default: handler }));
|
|
52
|
+
vi.doMock('./routes/mcp.js', () => ({ default: handler }));
|
|
53
|
+
vi.doMock('./routes/mobileRoot.js', () => ({ default: handler }));
|
|
54
|
+
vi.doMock('./routes/mountOauthDiscovery.js', () => ({ default: () => {} }));
|
|
55
|
+
vi.doMock('./routes/request.js', () => ({ default: slowHandler }));
|
|
56
|
+
vi.doMock('./routes/usage.js', () => ({ default: handler }));
|
|
57
|
+
vi.doMock('./routes/user.js', () => ({ default: handler }));
|
|
58
|
+
vi.doMock('./routes/websocket.js', () => ({ default: () => handler }));
|
|
59
|
+
vi.doMock('./routes/wellKnownFallback.js', () => ({ default: handler }));
|
|
60
|
+
vi.doMock('./html/renderPage.js', () => ({
|
|
61
|
+
default: (c) => c.html('<html></html>'),
|
|
62
|
+
}));
|
|
63
|
+
// Production's context.handleError logs the fault and captures it to Sentry.
|
|
64
|
+
vi.doMock('./middleware/apiContext.js', () => ({
|
|
65
|
+
default: () => (c, next) => {
|
|
66
|
+
c.set('lowdefyContext', {
|
|
67
|
+
logger,
|
|
68
|
+
handleError: async (error) => {
|
|
69
|
+
logger.error(error);
|
|
70
|
+
captureException(error);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
lateChain = next();
|
|
74
|
+
return lateChain;
|
|
75
|
+
},
|
|
76
|
+
}));
|
|
77
|
+
vi.doMock('./middleware/sentry.js', () => ({ default: passThrough }));
|
|
78
|
+
vi.doMock('../lib/build/appMeta.js', () => ({ default: { buildId: 'build-1' } }));
|
|
79
|
+
vi.doMock('../lib/build/auth.js', () => ({ default: { configured: false } }));
|
|
80
|
+
vi.doMock('../lib/build/theme.js', () => ({ default: {} }));
|
|
81
|
+
vi.doMock('../lib/build/config.js', () => ({
|
|
82
|
+
default: { requestTimeout: REQUEST_TIMEOUT_MS },
|
|
83
|
+
}));
|
|
84
|
+
vi.doMock('../lib/build/mobileConfig.js', () => ({ default: {} }));
|
|
85
|
+
vi.doMock('../lib/server/auth/getAuth.js', () => ({ default: vi.fn() }));
|
|
86
|
+
vi.doMock('../lib/server/auth/getStrategies.js', () => ({ default: vi.fn() }));
|
|
87
|
+
vi.doMock('../lib/server/log/createLogger.js', () => ({ default: () => logger }));
|
|
88
|
+
|
|
89
|
+
const { default: sentrySdk } = await import('../lib/server/sentry/sentrySdk.js');
|
|
90
|
+
const { default: createApp } = await import('./app.js');
|
|
91
|
+
|
|
92
|
+
beforeEach(() => {
|
|
93
|
+
vi.clearAllMocks();
|
|
94
|
+
sentrySdk.current = { captureException };
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
async function requestPastTimeout() {
|
|
98
|
+
const res = await createApp().request('/api/request/home/get_data', {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
headers: { 'accept-encoding': 'gzip' },
|
|
101
|
+
});
|
|
102
|
+
expect(res.status).toEqual(504);
|
|
103
|
+
// Sending the 504 reads its body, as the Node server does.
|
|
104
|
+
expect(await res.text()).toEqual('');
|
|
105
|
+
// The handler answers after the 504 went out, and the middleware around it finishes.
|
|
106
|
+
await lateChain;
|
|
107
|
+
// compress, outside apiContext, finishes on the next turn.
|
|
108
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
test('a request whose handler answers after the timeout answers 504 and reports nothing', async () => {
|
|
112
|
+
lateAnswer = (c) => c.json({ rows: 'x'.repeat(4096) });
|
|
113
|
+
await requestPastTimeout();
|
|
114
|
+
expect(captureException).not.toHaveBeenCalled();
|
|
115
|
+
expect(logger.error).not.toHaveBeenCalled();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('a handler that sets a header after the timeout reports nothing', async () => {
|
|
119
|
+
lateAnswer = (c) => c.redirect('/home', 302);
|
|
120
|
+
await requestPastTimeout();
|
|
121
|
+
expect(captureException).not.toHaveBeenCalled();
|
|
122
|
+
expect(logger.error).not.toHaveBeenCalled();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('a handler that fails after the timeout is still reported', async () => {
|
|
126
|
+
const fault = new Error('The upstream write failed.');
|
|
127
|
+
lateAnswer = () => {
|
|
128
|
+
throw fault;
|
|
129
|
+
};
|
|
130
|
+
await requestPastTimeout();
|
|
131
|
+
expect(captureException).toHaveBeenCalledTimes(1);
|
|
132
|
+
expect(captureException).toHaveBeenCalledWith(fault);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('a response in time is still compressed', async () => {
|
|
136
|
+
const res = await createApp().request('/api/page/home', {
|
|
137
|
+
headers: { 'accept-encoding': 'gzip' },
|
|
138
|
+
});
|
|
139
|
+
expect(res.status).toEqual(200);
|
|
140
|
+
expect(res.headers.get('content-encoding')).toEqual('gzip');
|
|
141
|
+
});
|
|
@@ -22,6 +22,12 @@ import { timeout } from 'hono/timeout';
|
|
|
22
22
|
// timeout signal (which apiContext folds into context.signal) cancels the upstream
|
|
23
23
|
// calls the handler is waiting on, such as an AI provider call, instead of letting
|
|
24
24
|
// them run, and bill, after the response has gone.
|
|
25
|
+
//
|
|
26
|
+
// The 504 has no body. The handler runs on after the 504 is sent, on the same context:
|
|
27
|
+
// a header set on it then, by the middleware around the handler or by the handler
|
|
28
|
+
// itself (c.redirect sets one), makes hono rebuild the sent 504 from its body. Sending
|
|
29
|
+
// it has already read that body, so the rebuild throws. A response with no body
|
|
30
|
+
// rebuilds safely. The client reads any 504 as a timeout.
|
|
25
31
|
function requestTimeout({ timeoutMs }) {
|
|
26
32
|
return function requestTimeoutMiddleware(c, next) {
|
|
27
33
|
const controller = new AbortController();
|
|
@@ -30,7 +36,7 @@ function requestTimeout({ timeoutMs }) {
|
|
|
30
36
|
controller.abort(
|
|
31
37
|
new DOMException(`The request timeout of ${timeoutMs}ms was exceeded.`, 'TimeoutError')
|
|
32
38
|
);
|
|
33
|
-
return new HTTPException(504, {
|
|
39
|
+
return new HTTPException(504, { res: new Response(null, { status: 504 }) });
|
|
34
40
|
})(c, next);
|
|
35
41
|
};
|
|
36
42
|
}
|
package/src/routes/websocket.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { upgradeWebSocket } from '@hono/node-server';
|
|
18
|
+
import { type } from '@lowdefy/helpers';
|
|
18
19
|
import {
|
|
19
20
|
createChannelRegistry,
|
|
20
21
|
createWebSocketConnection,
|
|
@@ -27,45 +28,70 @@ import getStrategies from '../../lib/server/auth/getStrategies.js';
|
|
|
27
28
|
// resolvers are shared across all websocket connections on this instance.
|
|
28
29
|
const registry = createChannelRegistry();
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
},
|
|
46
|
-
onMessage(event) {
|
|
47
|
-
connection?.handleMessage(event.data);
|
|
48
|
-
},
|
|
49
|
-
onClose() {
|
|
50
|
-
connection?.close();
|
|
51
|
-
},
|
|
52
|
-
onError(event) {
|
|
53
|
-
// A transport fault, not routine traffic: visible at the default level with
|
|
54
|
-
// the error object attached so the cause is not reduced to a string.
|
|
55
|
-
context.logger.warn({ event: 'ws_error', err: event?.error }, 'WebSocket error.');
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
});
|
|
31
|
+
// The code a browser's WebSocket reports when the server goes away; the client
|
|
32
|
+
// reconnects and resubscribes on any close but a caller change.
|
|
33
|
+
const MAX_DURATION_CLOSE_CODE = 1001;
|
|
34
|
+
const MAX_DURATION_CLOSE_MARGIN_MS = 5 * 1000;
|
|
35
|
+
|
|
36
|
+
// maxDurationMs is how long the platform lets the function run (Vercel's
|
|
37
|
+
// maxDuration). The platform stops the function there without a close frame,
|
|
38
|
+
// and a browser only notices the dead socket about 10 s later, missing every
|
|
39
|
+
// change in between. Closing the socket a little before the limit lets the
|
|
40
|
+
// browser reconnect at once. A limit under 10 s closes at half of it, so the
|
|
41
|
+
// socket is not closed as soon as it opens.
|
|
42
|
+
function createWebsocketHandler({ maxDurationMs } = {}) {
|
|
43
|
+
const closeAfterMs = type.isNone(maxDurationMs)
|
|
44
|
+
? null
|
|
45
|
+
: Math.max(maxDurationMs - MAX_DURATION_CLOSE_MARGIN_MS, maxDurationMs / 2);
|
|
59
46
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
47
|
+
const upgrade = upgradeWebSocket((c) => {
|
|
48
|
+
const context = c.get('lowdefyContext');
|
|
49
|
+
let connection;
|
|
50
|
+
let maxDurationTimer = null;
|
|
51
|
+
return {
|
|
52
|
+
onOpen(event, ws) {
|
|
53
|
+
// The upgrade's headers stay with the connection: its caller is resolved
|
|
54
|
+
// from them again while the socket is open - a browser's session cookie,
|
|
55
|
+
// or a phone shell's "Authorization: Bearer" session, read the same way.
|
|
56
|
+
connection = createWebSocketConnection(context, {
|
|
57
|
+
closeSocket: ({ code, reason }) => ws.close(code, reason),
|
|
58
|
+
headers: c.req.raw.headers,
|
|
59
|
+
registry,
|
|
60
|
+
send: (message) => ws.send(message),
|
|
61
|
+
strategies: getStrategies({ logger: context.logger }),
|
|
62
|
+
});
|
|
63
|
+
if (!type.isNone(closeAfterMs)) {
|
|
64
|
+
maxDurationTimer = setTimeout(
|
|
65
|
+
() => ws.close(MAX_DURATION_CLOSE_CODE, 'max-duration'),
|
|
66
|
+
closeAfterMs
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
onMessage(event) {
|
|
71
|
+
connection?.handleMessage(event.data);
|
|
72
|
+
},
|
|
73
|
+
onClose() {
|
|
74
|
+
clearTimeout(maxDurationTimer);
|
|
75
|
+
connection?.close();
|
|
76
|
+
},
|
|
77
|
+
onError(event) {
|
|
78
|
+
// A transport fault, not routine traffic: visible at the default level with
|
|
79
|
+
// the error object attached so the cause is not reduced to a string.
|
|
80
|
+
context.logger.warn({ event: 'ws_error', err: event?.error }, 'WebSocket error.');
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Upgrades are accepted from the app's own pages and from clients that send no
|
|
86
|
+
// Origin (a phone shell sends the app's own); isWebSocketOriginAllowed holds the
|
|
87
|
+
// rule and logs a refusal.
|
|
88
|
+
return function websocketHandler(c, next) {
|
|
89
|
+
const context = c.get('lowdefyContext');
|
|
90
|
+
if (!isWebSocketOriginAllowed({ context, getHeader: (name) => c.req.header(name) })) {
|
|
91
|
+
return c.json({ error: 'Forbidden' }, 403);
|
|
92
|
+
}
|
|
93
|
+
return upgrade(c, next);
|
|
94
|
+
};
|
|
69
95
|
}
|
|
70
96
|
|
|
71
|
-
export default
|
|
97
|
+
export default createWebsocketHandler;
|
|
@@ -23,7 +23,7 @@ vi.doMock('../../lib/server/auth/getStrategies.js', () => ({
|
|
|
23
23
|
default: () => [],
|
|
24
24
|
}));
|
|
25
25
|
|
|
26
|
-
const { default:
|
|
26
|
+
const { default: createWebsocketHandler } = await import('./websocket.js');
|
|
27
27
|
|
|
28
28
|
let server;
|
|
29
29
|
let port;
|
|
@@ -31,7 +31,7 @@ let port;
|
|
|
31
31
|
// The production wiring (src/index.js): a Hono app under the app's basePath,
|
|
32
32
|
// served by @hono/node-server with a ws server for upgrades. The Vercel
|
|
33
33
|
// function runs the same app through the same upgrade support.
|
|
34
|
-
|
|
34
|
+
function serveApp(websocketHandler) {
|
|
35
35
|
const app = new Hono().basePath('/app');
|
|
36
36
|
app.use('*', async (c, next) => {
|
|
37
37
|
// The caller the upgrade resolved: signed in when the test says so.
|
|
@@ -50,12 +50,15 @@ beforeAll(async () => {
|
|
|
50
50
|
});
|
|
51
51
|
app.get('/api/websocket', websocketHandler);
|
|
52
52
|
const wss = new WebSocketServer({ noServer: true });
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
port
|
|
56
|
-
resolve();
|
|
53
|
+
return new Promise((resolve) => {
|
|
54
|
+
const served = serve({ fetch: app.fetch, port: 0, websocket: { server: wss } }, (info) => {
|
|
55
|
+
resolve({ server: served, port: info.port });
|
|
57
56
|
});
|
|
58
57
|
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
beforeAll(async () => {
|
|
61
|
+
({ server, port } = await serveApp(createWebsocketHandler()));
|
|
59
62
|
});
|
|
60
63
|
|
|
61
64
|
afterAll(() => {
|
|
@@ -104,3 +107,69 @@ test('a websocket whose session ended closes with 4401 on its next subscribe', a
|
|
|
104
107
|
|
|
105
108
|
expect(await closed).toEqual({ code: 4401, reason: 'caller-changed' });
|
|
106
109
|
});
|
|
110
|
+
|
|
111
|
+
function openUntilClosed({ port: socketPort }) {
|
|
112
|
+
const socket = new WebSocket(`ws://127.0.0.1:${socketPort}/app/api/websocket`, {
|
|
113
|
+
headers: { host: 'app.test' },
|
|
114
|
+
});
|
|
115
|
+
const closed = new Promise((resolve) => {
|
|
116
|
+
socket.once('close', (code, reason) => resolve({ code, reason: reason.toString() }));
|
|
117
|
+
});
|
|
118
|
+
const opened = new Promise((resolve) => socket.once('open', resolve));
|
|
119
|
+
return { socket, opened, closed };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
test('a websocket on a function with a maxDuration of 120 s is closed with 1001 at 115 s', async () => {
|
|
123
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
124
|
+
const limited = await serveApp(createWebsocketHandler({ maxDurationMs: 120 * 1000 }));
|
|
125
|
+
try {
|
|
126
|
+
const { socket, opened, closed } = openUntilClosed(limited);
|
|
127
|
+
await opened;
|
|
128
|
+
let closedAt = null;
|
|
129
|
+
closed.then(() => {
|
|
130
|
+
closedAt = 'closed';
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await vi.advanceTimersByTimeAsync(115 * 1000 - 1);
|
|
134
|
+
expect(closedAt).toBe(null);
|
|
135
|
+
expect(socket.readyState).toBe(WebSocket.OPEN);
|
|
136
|
+
|
|
137
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
138
|
+
expect(await closed).toEqual({ code: 1001, reason: 'max-duration' });
|
|
139
|
+
} finally {
|
|
140
|
+
vi.useRealTimers();
|
|
141
|
+
limited.server.close();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('a websocket on a function with a maxDuration under 10 s is closed at half of it', async () => {
|
|
146
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
147
|
+
const limited = await serveApp(createWebsocketHandler({ maxDurationMs: 4 * 1000 }));
|
|
148
|
+
try {
|
|
149
|
+
const { socket, opened, closed } = openUntilClosed(limited);
|
|
150
|
+
await opened;
|
|
151
|
+
|
|
152
|
+
await vi.advanceTimersByTimeAsync(2 * 1000 - 1);
|
|
153
|
+
expect(socket.readyState).toBe(WebSocket.OPEN);
|
|
154
|
+
|
|
155
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
156
|
+
expect(await closed).toEqual({ code: 1001, reason: 'max-duration' });
|
|
157
|
+
} finally {
|
|
158
|
+
vi.useRealTimers();
|
|
159
|
+
limited.server.close();
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('a websocket on a server with no maxDuration stays open', async () => {
|
|
164
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
165
|
+
try {
|
|
166
|
+
const { socket, opened } = openUntilClosed({ port });
|
|
167
|
+
await opened;
|
|
168
|
+
|
|
169
|
+
await vi.advanceTimersByTimeAsync(10 * 60 * 1000);
|
|
170
|
+
expect(socket.readyState).toBe(WebSocket.OPEN);
|
|
171
|
+
socket.close();
|
|
172
|
+
} finally {
|
|
173
|
+
vi.useRealTimers();
|
|
174
|
+
}
|
|
175
|
+
});
|