@lowdefy/server 7.3.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 +27 -27
- package/package.original.json +27 -27
- 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 +1 -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",
|
|
@@ -44,31 +44,31 @@
|
|
|
44
44
|
"@better-auth/passkey": "1.7.7",
|
|
45
45
|
"@hono/mcp": "0.3.2",
|
|
46
46
|
"@hono/node-server": "2.1.3",
|
|
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.
|
|
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",
|
|
@@ -52,31 +52,31 @@
|
|
|
52
52
|
"@better-auth/passkey": "1.7.7",
|
|
53
53
|
"@hono/mcp": "0.3.2",
|
|
54
54
|
"@hono/node-server": "2.1.3",
|
|
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.
|
|
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>'),
|
package/src/app.timeout.test.mjs
CHANGED
|
@@ -55,7 +55,7 @@ vi.doMock('./routes/mountOauthDiscovery.js', () => ({ default: () => {} }));
|
|
|
55
55
|
vi.doMock('./routes/request.js', () => ({ default: slowHandler }));
|
|
56
56
|
vi.doMock('./routes/usage.js', () => ({ default: handler }));
|
|
57
57
|
vi.doMock('./routes/user.js', () => ({ default: handler }));
|
|
58
|
-
vi.doMock('./routes/websocket.js', () => ({ default: handler }));
|
|
58
|
+
vi.doMock('./routes/websocket.js', () => ({ default: () => handler }));
|
|
59
59
|
vi.doMock('./routes/wellKnownFallback.js', () => ({ default: handler }));
|
|
60
60
|
vi.doMock('./html/renderPage.js', () => ({
|
|
61
61
|
default: (c) => c.html('<html></html>'),
|
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
|
+
});
|