@lowdefy/server-dev 0.0.0-experimental-20260928120643 → 0.0.0-experimental-20260929110023
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/docs/isPageReady.js +3 -0
- package/lib/docs/isPageReady.test.mjs +9 -0
- package/lib/server/createLowdefyContext.js +6 -3
- package/lib/server/createLowdefyContext.test.mjs +17 -2
- package/manager/processes/startProxy.test.mjs +12 -0
- package/package.json +36 -35
- package/package.original.json +36 -35
- package/src/app.js +18 -0
- package/src/middleware/requestTimeout.js +38 -0
package/lib/docs/isPageReady.js
CHANGED
|
@@ -35,6 +35,9 @@ function isPageReady(pageId) {
|
|
|
35
35
|
// runOnInitAsync is always invoked once runOnInit resolves, so this settles
|
|
36
36
|
// even for a page that declares no onInitAsync event.
|
|
37
37
|
if (context._internal?.onInitAsyncDone !== true) return false;
|
|
38
|
+
// The app onInitAsync runs once per app load, alongside the first page's events.
|
|
39
|
+
const appContext = window.lowdefy?.appContext;
|
|
40
|
+
if (appContext && appContext._internal?.onInitAsyncDone !== true) return false;
|
|
38
41
|
|
|
39
42
|
// Covers onMountAsync (fired without await by Block.js) and any other event
|
|
40
43
|
// still running, such as an onClick from an earlier tool call.
|
|
@@ -70,6 +70,15 @@ test('isPageReady returns false while onInitAsync has not finished', () => {
|
|
|
70
70
|
expect(isPageReady('home')).toBe(false);
|
|
71
71
|
});
|
|
72
72
|
|
|
73
|
+
test('isPageReady returns false while the app onInitAsync has not finished', () => {
|
|
74
|
+
setWindow(settledContext());
|
|
75
|
+
global.window.lowdefy.appContext = { _internal: { onInitDone: true } };
|
|
76
|
+
expect(isPageReady('home')).toBe(false);
|
|
77
|
+
|
|
78
|
+
global.window.lowdefy.appContext._internal.onInitAsyncDone = true;
|
|
79
|
+
expect(isPageReady('home')).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
73
82
|
test('isPageReady returns false while a block onMountAsync event is loading', () => {
|
|
74
83
|
setWindow(
|
|
75
84
|
settledContext({
|
|
@@ -106,9 +106,12 @@ async function createLowdefyContext({ c, user }) {
|
|
|
106
106
|
},
|
|
107
107
|
scrubSecrets,
|
|
108
108
|
secrets,
|
|
109
|
-
// Aborts when the client disconnects before the response is sent
|
|
110
|
-
//
|
|
111
|
-
signal: createRequestSignal({
|
|
109
|
+
// Aborts when the client disconnects before the response is sent, or when the
|
|
110
|
+
// request timeout (src/middleware/requestTimeout.js) answers first.
|
|
111
|
+
signal: createRequestSignal({
|
|
112
|
+
clientSignal: c.req.raw.signal,
|
|
113
|
+
timeoutSignal: c.get('requestTimeoutSignal'),
|
|
114
|
+
}),
|
|
112
115
|
steps,
|
|
113
116
|
websockets,
|
|
114
117
|
};
|
|
@@ -27,7 +27,9 @@ process.env.LOWDEFY_SECRET_TEST = secret;
|
|
|
27
27
|
|
|
28
28
|
jest.unstable_mockModule('@lowdefy/api', () => ({
|
|
29
29
|
createApiContext: jest.fn(),
|
|
30
|
-
createRequestSignal: jest.fn(({ clientSignal }) =>
|
|
30
|
+
createRequestSignal: jest.fn(({ clientSignal, timeoutSignal }) =>
|
|
31
|
+
timeoutSignal ? AbortSignal.any([clientSignal, timeoutSignal]) : clientSignal
|
|
32
|
+
),
|
|
31
33
|
// Returns what the server passes, so the test reads the auth-hook system context inputs.
|
|
32
34
|
createSystemContext: jest.fn((options) => options),
|
|
33
35
|
ensureMcpOauthResource: jest.fn(async () => {}),
|
|
@@ -107,8 +109,10 @@ afterAll(() => {
|
|
|
107
109
|
const { default: createLowdefyContext } = await import('./createLowdefyContext.js');
|
|
108
110
|
const { default: createSystemContext } = await import('./auth/createSystemContext.js');
|
|
109
111
|
|
|
110
|
-
function createHonoContext({ path: reqPath = '/api/request/foo' } = {}) {
|
|
112
|
+
function createHonoContext({ path: reqPath = '/api/request/foo', requestTimeoutSignal } = {}) {
|
|
113
|
+
const variables = { requestTimeoutSignal };
|
|
111
114
|
return {
|
|
115
|
+
get: (key) => variables[key],
|
|
112
116
|
req: {
|
|
113
117
|
header: (name) => (name ? undefined : {}),
|
|
114
118
|
path: reqPath,
|
|
@@ -130,6 +134,17 @@ test('createLowdefyContext cancels the request work with the incoming request si
|
|
|
130
134
|
expect(context.signal).toBe(c.req.raw.signal);
|
|
131
135
|
});
|
|
132
136
|
|
|
137
|
+
test('createLowdefyContext cancels the request work when the request timeout answers first', async () => {
|
|
138
|
+
const timeout = new AbortController();
|
|
139
|
+
const c = createHonoContext({ requestTimeoutSignal: timeout.signal });
|
|
140
|
+
const context = await createLowdefyContext({ c });
|
|
141
|
+
expect(context.signal.aborted).toBe(false);
|
|
142
|
+
const reason = new DOMException('The request timeout of 20ms was exceeded.', 'TimeoutError');
|
|
143
|
+
timeout.abort(reason);
|
|
144
|
+
expect(context.signal.aborted).toBe(true);
|
|
145
|
+
expect(context.signal.reason).toBe(reason);
|
|
146
|
+
});
|
|
147
|
+
|
|
133
148
|
test('createLowdefyContext scrubSecrets redacts a planted secret', async () => {
|
|
134
149
|
const context = await createLowdefyContext({ c: createHonoContext() });
|
|
135
150
|
expect(context.scrubSecrets(`token ${secret} end`)).toEqual('token [REDACTED] end');
|
|
@@ -238,12 +238,24 @@ test.each([
|
|
|
238
238
|
context.buildActivity = createBuildActivity({ onChange: () => {} });
|
|
239
239
|
context.devServer = { exitCode: null, signalCode: null };
|
|
240
240
|
context.buildActivity.setBusy(true);
|
|
241
|
+
// waitedMs is timed from when the proxy starts waiting, a moment after the request is sent.
|
|
242
|
+
// Holding for 200ms from that moment, not from the fetch call, keeps the waitedMs assertion
|
|
243
|
+
// below from racing the request's arrival.
|
|
244
|
+
const { waitForIdle } = context.buildActivity;
|
|
245
|
+
const waitStarted = new Promise((resolve) => {
|
|
246
|
+
context.buildActivity.waitForIdle = (options) => {
|
|
247
|
+
const waiting = waitForIdle(options);
|
|
248
|
+
resolve();
|
|
249
|
+
return waiting;
|
|
250
|
+
};
|
|
251
|
+
});
|
|
241
252
|
|
|
242
253
|
const { path: requestPath, init } = makeRequest();
|
|
243
254
|
const pending = fetch(`http://localhost:${port}${requestPath}`, {
|
|
244
255
|
...init,
|
|
245
256
|
headers: { 'x-lowdefy-build-wait': 'settled=true&sawBuild=false&waitedMs=0' },
|
|
246
257
|
}).then((response) => response.json());
|
|
258
|
+
await waitStarted;
|
|
247
259
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
248
260
|
|
|
249
261
|
// The restart the build needed: the old server stops, a new one answers.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/server-dev",
|
|
3
|
-
"version": "0.0.0-experimental-
|
|
3
|
+
"version": "0.0.0-experimental-20260929110023",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -45,40 +45,41 @@
|
|
|
45
45
|
"@hono/mcp": "0.3.0",
|
|
46
46
|
"@hono/node-server": "2.0.10",
|
|
47
47
|
"@hono/vite-dev-server": "0.26.0",
|
|
48
|
-
"@lowdefy/actions-core": "0.0.0-experimental-
|
|
49
|
-
"@lowdefy/api": "0.0.0-experimental-
|
|
50
|
-
"@lowdefy/block-utils": "0.0.0-experimental-
|
|
51
|
-
"@lowdefy/blocks-aggrid": "0.0.0-experimental-
|
|
52
|
-
"@lowdefy/blocks-antd": "0.0.0-experimental-
|
|
53
|
-
"@lowdefy/blocks-antd-x": "0.0.0-experimental-
|
|
54
|
-
"@lowdefy/blocks-basic": "0.0.0-experimental-
|
|
55
|
-
"@lowdefy/blocks-captcha": "0.0.0-experimental-
|
|
56
|
-
"@lowdefy/blocks-echarts": "0.0.0-experimental-
|
|
57
|
-
"@lowdefy/blocks-loaders": "0.0.0-experimental-
|
|
58
|
-
"@lowdefy/blocks-markdown": "0.0.0-experimental-
|
|
59
|
-
"@lowdefy/blocks-
|
|
60
|
-
"@lowdefy/
|
|
61
|
-
"@lowdefy/
|
|
62
|
-
"@lowdefy/
|
|
63
|
-
"@lowdefy/connection-
|
|
64
|
-
"@lowdefy/
|
|
65
|
-
"@lowdefy/
|
|
66
|
-
"@lowdefy/
|
|
67
|
-
"@lowdefy/
|
|
68
|
-
"@lowdefy/
|
|
69
|
-
"@lowdefy/
|
|
70
|
-
"@lowdefy/
|
|
71
|
-
"@lowdefy/
|
|
72
|
-
"@lowdefy/operators-
|
|
73
|
-
"@lowdefy/operators-
|
|
74
|
-
"@lowdefy/operators-
|
|
75
|
-
"@lowdefy/operators-
|
|
76
|
-
"@lowdefy/operators-
|
|
77
|
-
"@lowdefy/operators-
|
|
78
|
-
"@lowdefy/operators-
|
|
79
|
-
"@lowdefy/operators-
|
|
80
|
-
"@lowdefy/
|
|
81
|
-
"@lowdefy/
|
|
48
|
+
"@lowdefy/actions-core": "0.0.0-experimental-20260929110023",
|
|
49
|
+
"@lowdefy/api": "0.0.0-experimental-20260929110023",
|
|
50
|
+
"@lowdefy/block-utils": "0.0.0-experimental-20260929110023",
|
|
51
|
+
"@lowdefy/blocks-aggrid": "0.0.0-experimental-20260929110023",
|
|
52
|
+
"@lowdefy/blocks-antd": "0.0.0-experimental-20260929110023",
|
|
53
|
+
"@lowdefy/blocks-antd-x": "0.0.0-experimental-20260929110023",
|
|
54
|
+
"@lowdefy/blocks-basic": "0.0.0-experimental-20260929110023",
|
|
55
|
+
"@lowdefy/blocks-captcha": "0.0.0-experimental-20260929110023",
|
|
56
|
+
"@lowdefy/blocks-echarts": "0.0.0-experimental-20260929110023",
|
|
57
|
+
"@lowdefy/blocks-loaders": "0.0.0-experimental-20260929110023",
|
|
58
|
+
"@lowdefy/blocks-markdown": "0.0.0-experimental-20260929110023",
|
|
59
|
+
"@lowdefy/blocks-table": "0.0.0-experimental-20260929110023",
|
|
60
|
+
"@lowdefy/blocks-tiptap": "0.0.0-experimental-20260929110023",
|
|
61
|
+
"@lowdefy/build": "0.0.0-experimental-20260929110023",
|
|
62
|
+
"@lowdefy/client": "0.0.0-experimental-20260929110023",
|
|
63
|
+
"@lowdefy/connection-axios-http": "0.0.0-experimental-20260929110023",
|
|
64
|
+
"@lowdefy/connection-mongodb": "0.0.0-experimental-20260929110023",
|
|
65
|
+
"@lowdefy/docs-content": "0.0.0-experimental-20260929110023",
|
|
66
|
+
"@lowdefy/e2e-utils": "0.0.0-experimental-20260929110023",
|
|
67
|
+
"@lowdefy/errors": "0.0.0-experimental-20260929110023",
|
|
68
|
+
"@lowdefy/helpers": "0.0.0-experimental-20260929110023",
|
|
69
|
+
"@lowdefy/layout": "0.0.0-experimental-20260929110023",
|
|
70
|
+
"@lowdefy/logger": "0.0.0-experimental-20260929110023",
|
|
71
|
+
"@lowdefy/node-utils": "0.0.0-experimental-20260929110023",
|
|
72
|
+
"@lowdefy/operators-change-case": "0.0.0-experimental-20260929110023",
|
|
73
|
+
"@lowdefy/operators-cron": "0.0.0-experimental-20260929110023",
|
|
74
|
+
"@lowdefy/operators-dayjs": "0.0.0-experimental-20260929110023",
|
|
75
|
+
"@lowdefy/operators-diff": "0.0.0-experimental-20260929110023",
|
|
76
|
+
"@lowdefy/operators-js": "0.0.0-experimental-20260929110023",
|
|
77
|
+
"@lowdefy/operators-mql": "0.0.0-experimental-20260929110023",
|
|
78
|
+
"@lowdefy/operators-nunjucks": "0.0.0-experimental-20260929110023",
|
|
79
|
+
"@lowdefy/operators-uuid": "0.0.0-experimental-20260929110023",
|
|
80
|
+
"@lowdefy/operators-yaml": "0.0.0-experimental-20260929110023",
|
|
81
|
+
"@lowdefy/plugin-better-auth": "0.0.0-experimental-20260929110023",
|
|
82
|
+
"@lowdefy/websockets-core": "0.0.0-experimental-20260929110023",
|
|
82
83
|
"@modelcontextprotocol/sdk": "1.29.0",
|
|
83
84
|
"@tailwindcss/postcss": "4.3.3",
|
|
84
85
|
"@vitejs/plugin-react": "6.0.2",
|
package/package.original.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/server-dev",
|
|
3
|
-
"version": "0.0.0-experimental-
|
|
3
|
+
"version": "0.0.0-experimental-20260929110023",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -51,40 +51,41 @@
|
|
|
51
51
|
"@hono/mcp": "0.3.0",
|
|
52
52
|
"@hono/node-server": "2.0.10",
|
|
53
53
|
"@hono/vite-dev-server": "0.26.0",
|
|
54
|
-
"@lowdefy/actions-core": "0.0.0-experimental-
|
|
55
|
-
"@lowdefy/api": "0.0.0-experimental-
|
|
56
|
-
"@lowdefy/block-utils": "0.0.0-experimental-
|
|
57
|
-
"@lowdefy/blocks-aggrid": "0.0.0-experimental-
|
|
58
|
-
"@lowdefy/blocks-antd": "0.0.0-experimental-
|
|
59
|
-
"@lowdefy/blocks-antd-x": "0.0.0-experimental-
|
|
60
|
-
"@lowdefy/blocks-basic": "0.0.0-experimental-
|
|
61
|
-
"@lowdefy/blocks-captcha": "0.0.0-experimental-
|
|
62
|
-
"@lowdefy/blocks-echarts": "0.0.0-experimental-
|
|
63
|
-
"@lowdefy/blocks-loaders": "0.0.0-experimental-
|
|
64
|
-
"@lowdefy/blocks-markdown": "0.0.0-experimental-
|
|
65
|
-
"@lowdefy/blocks-
|
|
66
|
-
"@lowdefy/
|
|
67
|
-
"@lowdefy/
|
|
68
|
-
"@lowdefy/
|
|
69
|
-
"@lowdefy/connection-
|
|
70
|
-
"@lowdefy/
|
|
71
|
-
"@lowdefy/
|
|
72
|
-
"@lowdefy/
|
|
73
|
-
"@lowdefy/
|
|
74
|
-
"@lowdefy/
|
|
75
|
-
"@lowdefy/
|
|
76
|
-
"@lowdefy/
|
|
77
|
-
"@lowdefy/
|
|
78
|
-
"@lowdefy/operators-
|
|
79
|
-
"@lowdefy/operators-
|
|
80
|
-
"@lowdefy/operators-
|
|
81
|
-
"@lowdefy/operators-
|
|
82
|
-
"@lowdefy/operators-
|
|
83
|
-
"@lowdefy/operators-
|
|
84
|
-
"@lowdefy/operators-
|
|
85
|
-
"@lowdefy/operators-
|
|
86
|
-
"@lowdefy/
|
|
87
|
-
"@lowdefy/
|
|
54
|
+
"@lowdefy/actions-core": "0.0.0-experimental-20260929110023",
|
|
55
|
+
"@lowdefy/api": "0.0.0-experimental-20260929110023",
|
|
56
|
+
"@lowdefy/block-utils": "0.0.0-experimental-20260929110023",
|
|
57
|
+
"@lowdefy/blocks-aggrid": "0.0.0-experimental-20260929110023",
|
|
58
|
+
"@lowdefy/blocks-antd": "0.0.0-experimental-20260929110023",
|
|
59
|
+
"@lowdefy/blocks-antd-x": "0.0.0-experimental-20260929110023",
|
|
60
|
+
"@lowdefy/blocks-basic": "0.0.0-experimental-20260929110023",
|
|
61
|
+
"@lowdefy/blocks-captcha": "0.0.0-experimental-20260929110023",
|
|
62
|
+
"@lowdefy/blocks-echarts": "0.0.0-experimental-20260929110023",
|
|
63
|
+
"@lowdefy/blocks-loaders": "0.0.0-experimental-20260929110023",
|
|
64
|
+
"@lowdefy/blocks-markdown": "0.0.0-experimental-20260929110023",
|
|
65
|
+
"@lowdefy/blocks-table": "0.0.0-experimental-20260929110023",
|
|
66
|
+
"@lowdefy/blocks-tiptap": "0.0.0-experimental-20260929110023",
|
|
67
|
+
"@lowdefy/build": "0.0.0-experimental-20260929110023",
|
|
68
|
+
"@lowdefy/client": "0.0.0-experimental-20260929110023",
|
|
69
|
+
"@lowdefy/connection-axios-http": "0.0.0-experimental-20260929110023",
|
|
70
|
+
"@lowdefy/connection-mongodb": "0.0.0-experimental-20260929110023",
|
|
71
|
+
"@lowdefy/docs-content": "0.0.0-experimental-20260929110023",
|
|
72
|
+
"@lowdefy/e2e-utils": "0.0.0-experimental-20260929110023",
|
|
73
|
+
"@lowdefy/errors": "0.0.0-experimental-20260929110023",
|
|
74
|
+
"@lowdefy/helpers": "0.0.0-experimental-20260929110023",
|
|
75
|
+
"@lowdefy/layout": "0.0.0-experimental-20260929110023",
|
|
76
|
+
"@lowdefy/logger": "0.0.0-experimental-20260929110023",
|
|
77
|
+
"@lowdefy/node-utils": "0.0.0-experimental-20260929110023",
|
|
78
|
+
"@lowdefy/operators-change-case": "0.0.0-experimental-20260929110023",
|
|
79
|
+
"@lowdefy/operators-cron": "0.0.0-experimental-20260929110023",
|
|
80
|
+
"@lowdefy/operators-dayjs": "0.0.0-experimental-20260929110023",
|
|
81
|
+
"@lowdefy/operators-diff": "0.0.0-experimental-20260929110023",
|
|
82
|
+
"@lowdefy/operators-js": "0.0.0-experimental-20260929110023",
|
|
83
|
+
"@lowdefy/operators-mql": "0.0.0-experimental-20260929110023",
|
|
84
|
+
"@lowdefy/operators-nunjucks": "0.0.0-experimental-20260929110023",
|
|
85
|
+
"@lowdefy/operators-uuid": "0.0.0-experimental-20260929110023",
|
|
86
|
+
"@lowdefy/operators-yaml": "0.0.0-experimental-20260929110023",
|
|
87
|
+
"@lowdefy/plugin-better-auth": "0.0.0-experimental-20260929110023",
|
|
88
|
+
"@lowdefy/websockets-core": "0.0.0-experimental-20260929110023",
|
|
88
89
|
"@modelcontextprotocol/sdk": "1.29.0",
|
|
89
90
|
"@tailwindcss/postcss": "4.3.3",
|
|
90
91
|
"@vitejs/plugin-react": "6.0.2",
|
package/src/app.js
CHANGED
|
@@ -74,6 +74,7 @@ import pingHandler from './routes/ping.js';
|
|
|
74
74
|
import reloadHandler from './routes/reload.js';
|
|
75
75
|
import renderDevPage from './html/renderDevPage.js';
|
|
76
76
|
import requestHandler from './routes/request.js';
|
|
77
|
+
import requestTimeout from './middleware/requestTimeout.js';
|
|
77
78
|
import rootHandler from './routes/root.js';
|
|
78
79
|
import staleFlag from './middleware/staleFlag.js';
|
|
79
80
|
import usageHandler from './routes/usage.js';
|
|
@@ -82,6 +83,10 @@ import websocketHandler from './routes/websocket.js';
|
|
|
82
83
|
|
|
83
84
|
const basePath = lowdefyConfig.basePath ?? '';
|
|
84
85
|
|
|
86
|
+
// Same default as the production server, so a request that would time out in production (a 504
|
|
87
|
+
// the page shows as a generic error, beside the other requests' results) also times out in dev.
|
|
88
|
+
const requestTimeoutMs = lowdefyConfig.requestTimeout ?? 30000;
|
|
89
|
+
|
|
85
90
|
// Dev Hono app — mounted into Vite via @hono/vite-dev-server (Vite owns HTTP
|
|
86
91
|
// and serves /client/* modules with HMR; everything else lands here).
|
|
87
92
|
// No compression in dev (parity with the old next.config compress: false,
|
|
@@ -178,6 +183,19 @@ function createApp() {
|
|
|
178
183
|
app.get('/lowdefy-docs/content/:slug{.+}', docsContentHandler);
|
|
179
184
|
app.get('/lowdefy-docs/:kind', docsTypesHandler);
|
|
180
185
|
|
|
186
|
+
// Only the routes that run app logic: JIT page builds, the docs/MCP routes and the SSE streams
|
|
187
|
+
// are dev-only and can legitimately run longer than any production request.
|
|
188
|
+
if (requestTimeoutMs > 0) {
|
|
189
|
+
const timeoutMiddleware = requestTimeout({ timeoutMs: requestTimeoutMs });
|
|
190
|
+
[
|
|
191
|
+
'/api/request/*',
|
|
192
|
+
'/api/endpoints/*',
|
|
193
|
+
'/api/cron/*',
|
|
194
|
+
'/api/cron-forward/*',
|
|
195
|
+
'/api/detached/*',
|
|
196
|
+
].forEach((route) => app.use(route, timeoutMiddleware));
|
|
197
|
+
}
|
|
198
|
+
|
|
181
199
|
app.use('/api/*', apiContext());
|
|
182
200
|
// Unified dev get-session: createLowdefyContext resolves the mock or headless
|
|
183
201
|
// caller into context.user on every path, so read it here rather than
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { HTTPException } from 'hono/http-exception';
|
|
18
|
+
import { timeout } from 'hono/timeout';
|
|
19
|
+
|
|
20
|
+
// Caps how long a request may run, and cancels the work it leaves behind. The 504 is
|
|
21
|
+
// answered while the handler is still running; the TimeoutError on the request's
|
|
22
|
+
// timeout signal (which apiContext folds into context.signal) cancels the upstream
|
|
23
|
+
// calls the handler is waiting on, such as an AI provider call, instead of letting
|
|
24
|
+
// them run, and bill, after the response has gone.
|
|
25
|
+
function requestTimeout({ timeoutMs }) {
|
|
26
|
+
return function requestTimeoutMiddleware(c, next) {
|
|
27
|
+
const controller = new AbortController();
|
|
28
|
+
c.set('requestTimeoutSignal', controller.signal);
|
|
29
|
+
return timeout(timeoutMs, () => {
|
|
30
|
+
controller.abort(
|
|
31
|
+
new DOMException(`The request timeout of ${timeoutMs}ms was exceeded.`, 'TimeoutError')
|
|
32
|
+
);
|
|
33
|
+
return new HTTPException(504, { message: 'Gateway Timeout' });
|
|
34
|
+
})(c, next);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default requestTimeout;
|