@jini-ai/http-kit 0.7.2 → 0.7.3-next.1
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/CHANGELOG.md +2 -10
- package/README.md +6 -6
- package/dist/adapter.d.ts +7 -50
- package/dist/adapter.js +12 -54
- package/dist/api-security-middleware.d.ts +0 -16
- package/dist/api-security-middleware.js +2 -18
- package/dist/index.d.ts +10 -15
- package/dist/index.js +7 -10
- package/dist/internal-error.d.ts +42 -0
- package/dist/internal-error.js +48 -0
- package/dist/local-daemon-request.d.ts +11 -4
- package/dist/local-daemon-request.js +1 -28
- package/dist/middleware.d.ts +1 -0
- package/dist/middleware.js +1 -0
- package/dist/node-bridge.d.ts +108 -0
- package/dist/node-bridge.js +356 -0
- package/dist/observability.d.ts +3 -14
- package/dist/origin.d.ts +13 -30
- package/dist/origin.js +13 -13
- package/dist/range.d.ts +53 -0
- package/dist/range.js +93 -0
- package/dist/rate-limit.d.ts +1 -13
- package/dist/rate-limit.js +5 -2
- package/dist/request.d.ts +2 -13
- package/dist/request.js +0 -13
- package/dist/response.d.ts +8 -7
- package/dist/response.js +0 -36
- package/dist/route-registration-guard.d.ts +14 -0
- package/dist/route-registration-guard.js +17 -0
- package/dist/run-credentials.d.ts +73 -0
- package/dist/run-credentials.js +71 -0
- package/dist/settings/contracts.d.ts +49 -0
- package/dist/settings/contracts.js +2 -0
- package/dist/settings/http-request.d.ts +36 -0
- package/dist/settings/http-request.js +50 -0
- package/dist/settings/http.d.ts +10 -0
- package/dist/settings/http.js +216 -0
- package/dist/settings/index.d.ts +4 -0
- package/dist/settings/index.js +3 -0
- package/dist/settings/shared.d.ts +16 -0
- package/dist/settings/shared.js +16 -0
- package/dist/settings/stream.d.ts +25 -0
- package/dist/settings/stream.js +180 -0
- package/dist/sse.d.ts +1 -15
- package/dist/sse.js +77 -72
- package/dist/trust-proxy.d.ts +36 -0
- package/dist/trust-proxy.js +45 -0
- package/dist/types.d.ts +4 -52
- package/dist/types.js +1 -4
- package/package.json +24 -14
- package/dist/active-context.d.ts +0 -99
- package/dist/active-context.js +0 -77
- package/dist/agents.d.ts +0 -64
- package/dist/agents.js +0 -23
- package/dist/attachments.d.ts +0 -534
- package/dist/attachments.js +0 -1032
- package/dist/cancel-owned-runs.d.ts +0 -44
- package/dist/cancel-owned-runs.js +0 -35
- package/dist/component-catalog.d.ts +0 -74
- package/dist/component-catalog.js +0 -74
- package/dist/connectors.d.ts +0 -165
- package/dist/connectors.js +0 -474
- package/dist/daemon-status.d.ts +0 -72
- package/dist/daemon-status.js +0 -60
- package/dist/db-ops.d.ts +0 -116
- package/dist/db-ops.js +0 -191
- package/dist/delegated-tools.d.ts +0 -146
- package/dist/delegated-tools.js +0 -286
- package/dist/frontend-control.d.ts +0 -131
- package/dist/frontend-control.js +0 -77
- package/dist/frontend-sessions.d.ts +0 -100
- package/dist/frontend-sessions.js +0 -180
- package/dist/health.d.ts +0 -91
- package/dist/health.js +0 -53
- package/dist/host-tools.d.ts +0 -174
- package/dist/host-tools.js +0 -338
- package/dist/media.d.ts +0 -131
- package/dist/media.js +0 -352
- package/dist/memory.d.ts +0 -230
- package/dist/memory.js +0 -331
- package/dist/model-proxy.d.ts +0 -45
- package/dist/model-proxy.js +0 -619
- package/dist/origin-validation.d.ts +0 -64
- package/dist/origin-validation.js +0 -226
- package/dist/raw-sse.d.ts +0 -49
- package/dist/raw-sse.js +0 -92
- package/dist/read-only-tools.d.ts +0 -67
- package/dist/read-only-tools.js +0 -59
- package/dist/remote-run-events.d.ts +0 -66
- package/dist/remote-run-events.js +0 -230
- package/dist/research.d.ts +0 -42
- package/dist/research.js +0 -316
- package/dist/route-manifest.d.ts +0 -31
- package/dist/route-manifest.js +0 -103
- package/dist/routines.d.ts +0 -114
- package/dist/routines.js +0 -320
- package/dist/runs.d.ts +0 -79
- package/dist/runs.js +0 -232
- package/dist/terminals.d.ts +0 -125
- package/dist/terminals.js +0 -349
- package/dist/tool-catalog.d.ts +0 -68
- package/dist/tool-catalog.js +0 -68
- package/dist/verified-origin/configured-origin.d.ts +0 -37
- package/dist/verified-origin/configured-origin.js +0 -99
- package/dist/verified-origin/index.d.ts +0 -7
- package/dist/verified-origin/index.js +0 -6
- package/dist/verified-origin/loopback-host.d.ts +0 -9
- package/dist/verified-origin/loopback-host.js +0 -17
- package/dist/verified-origin/origin.d.ts +0 -64
- package/dist/verified-origin/origin.js +0 -162
- package/dist/verified-origin/ports.d.ts +0 -38
- package/dist/verified-origin/ports.js +0 -2
- package/dist/verified-origin/repo.memory.d.ts +0 -32
- package/dist/verified-origin/repo.memory.js +0 -34
- package/dist/verified-origin/site-path.d.ts +0 -113
- package/dist/verified-origin/site-path.js +0 -188
- package/dist/verified-origin/types.d.ts +0 -33
- package/dist/verified-origin/types.js +0 -30
- package/dist/verified-origin.d.ts +0 -2
- package/dist/verified-origin.js +0 -2
- package/dist/workspace-root.d.ts +0 -69
- package/dist/workspace-root.js +0 -63
- package/dist/xai.d.ts +0 -86
- package/dist/xai.js +0 -668
package/dist/sse.js
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
|
+
import { DEFAULT_MAX_QUEUED_SSE_EVENTS } from '@jini-ai/core/contracts/server';
|
|
1
2
|
import { createApiErrorResponse } from '@jini-ai/protocol';
|
|
2
|
-
/**
|
|
3
|
-
* Cap on events queued for one client — covers both a pre-header replay
|
|
4
|
-
* burst and any backlog built up while `res.write` reports backpressure
|
|
5
|
-
* (`write() === false`, awaiting `'drain'`). A stalled or malicious client
|
|
6
|
-
* would otherwise let an unbounded producer grow this array without limit
|
|
7
|
-
* (SEC-006, carried forward from `runs.ts`'s original constant). Once
|
|
8
|
-
* exceeded, the connection is dropped rather than accepting unbounded
|
|
9
|
-
* memory growth.
|
|
10
|
-
*/
|
|
11
|
-
export const DEFAULT_MAX_QUEUED_SSE_EVENTS = 1000;
|
|
12
3
|
/** Default wire format: `id: <cursor>\nevent: <kind>\ndata: <json>\n\n`, matching the SSE spec's field syntax. */
|
|
13
4
|
function defaultFormatEvent(event) {
|
|
14
5
|
return `id: ${event.opaqueCursor}\nevent: ${event.kind}\ndata: ${JSON.stringify(event)}\n\n`;
|
|
@@ -43,19 +34,46 @@ function defaultFormatEvent(event) {
|
|
|
43
34
|
export function createSseChannel({ res }, options = {}) {
|
|
44
35
|
const maxQueuedEvents = options.maxQueuedEvents ?? DEFAULT_MAX_QUEUED_SSE_EVENTS;
|
|
45
36
|
const formatEvent = options.formatEvent ?? (({ event }) => defaultFormatEvent(event));
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
37
|
+
const state = {
|
|
38
|
+
queue: [], flowing: false, writable: true, closed: false, ending: false, closeCallbacks: [],
|
|
39
|
+
};
|
|
40
|
+
const { markClosed, finish, end } = createSseLifecycle(res, state);
|
|
41
|
+
const isTerminalEvent = (event) => options.isEndEvent?.({ event });
|
|
42
|
+
const pump = createSsePump(res, state, { formatEvent, options, finish, end, isTerminalEvent });
|
|
43
|
+
const { enqueue, open } = createSseProducer(res, state, { maxQueuedEvents, finish, pump });
|
|
44
|
+
res.on('drain', () => {
|
|
45
|
+
state.writable = true;
|
|
46
|
+
pump();
|
|
47
|
+
});
|
|
48
|
+
// The client disconnecting only marks the channel closed — it must not itself call `res.end()`
|
|
49
|
+
// (see `end()`'s doc for why calling it a second time would be safe anyway, but a route that
|
|
50
|
+
// still wants to send a *non-SSE* response — e.g. a JSON error — after discovering a subscribe
|
|
51
|
+
// failed needs `isClosed()` to be observable without the response having been touched yet).
|
|
52
|
+
res.on('close', markClosed);
|
|
53
|
+
const onClose = ({ callback }) => {
|
|
54
|
+
if (state.closed) {
|
|
55
|
+
callback();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
state.closeCallbacks.push(callback);
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
enqueue,
|
|
62
|
+
open,
|
|
63
|
+
isClosed: () => state.closed,
|
|
64
|
+
end,
|
|
65
|
+
abandon: markClosed,
|
|
66
|
+
onClose,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function createSseLifecycle(res, state) {
|
|
52
70
|
const markClosed = () => {
|
|
53
|
-
if (closed)
|
|
71
|
+
if (state.closed)
|
|
54
72
|
return;
|
|
55
|
-
closed = true;
|
|
73
|
+
state.closed = true;
|
|
56
74
|
// Copy first: a callback that itself calls `onClose` during this loop must not be invoked
|
|
57
75
|
// twice or mutate the array being iterated.
|
|
58
|
-
const callbacks = closeCallbacks.splice(0, closeCallbacks.length);
|
|
76
|
+
const callbacks = state.closeCallbacks.splice(0, state.closeCallbacks.length);
|
|
59
77
|
for (const callback of callbacks)
|
|
60
78
|
callback();
|
|
61
79
|
};
|
|
@@ -67,86 +85,73 @@ export function createSseChannel({ res }, options = {}) {
|
|
|
67
85
|
};
|
|
68
86
|
/** Stop production now; an open backlog finishes on drain. O(1) excluding close callbacks. */
|
|
69
87
|
const end = () => {
|
|
70
|
-
if (closed)
|
|
88
|
+
if (state.closed)
|
|
71
89
|
return;
|
|
72
|
-
ending = true;
|
|
73
|
-
if (flowing && queue.length > 0)
|
|
90
|
+
state.ending = true;
|
|
91
|
+
if (state.flowing && state.queue.length > 0)
|
|
74
92
|
return;
|
|
75
93
|
finish();
|
|
76
94
|
};
|
|
95
|
+
return { markClosed, finish, end };
|
|
96
|
+
}
|
|
97
|
+
function createSsePump(res, state, { formatEvent, options, finish, end, isTerminalEvent }) {
|
|
77
98
|
const pump = () => {
|
|
78
|
-
if (!flowing || closed)
|
|
99
|
+
if (!state.flowing || state.closed)
|
|
79
100
|
return;
|
|
80
|
-
while (writable && queue.length > 0) {
|
|
81
|
-
|
|
82
|
-
let wroteOk;
|
|
83
|
-
try {
|
|
84
|
-
wroteOk = res.write(formatEvent({ event }));
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
// A dead/broken transport must never throw back through whatever is producing events —
|
|
88
|
-
// stop this channel only.
|
|
89
|
-
options.onWriteError?.({ error });
|
|
90
|
-
finish();
|
|
101
|
+
while (state.writable && state.queue.length > 0) {
|
|
102
|
+
if (!writeNextSseEvent(res, state, { formatEvent, options, finish, isTerminalEvent }))
|
|
91
103
|
return;
|
|
92
|
-
}
|
|
93
|
-
if (wroteOk === false)
|
|
94
|
-
writable = false;
|
|
95
|
-
if (isTerminalEvent(event)) {
|
|
96
|
-
finish();
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
104
|
}
|
|
100
|
-
if (ending)
|
|
105
|
+
if (state.ending)
|
|
101
106
|
end();
|
|
102
107
|
};
|
|
103
|
-
|
|
108
|
+
return pump;
|
|
109
|
+
}
|
|
110
|
+
function writeNextSseEvent(res, state, { formatEvent, options, finish, isTerminalEvent }) {
|
|
111
|
+
const event = state.queue.shift();
|
|
112
|
+
let wroteOk;
|
|
113
|
+
try {
|
|
114
|
+
wroteOk = res.write(formatEvent({ event }));
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
// A dead/broken transport must never throw back through whatever is producing events —
|
|
118
|
+
// stop this channel only.
|
|
119
|
+
options.onWriteError?.({ error });
|
|
120
|
+
finish();
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
if (wroteOk === false)
|
|
124
|
+
state.writable = false;
|
|
125
|
+
if (isTerminalEvent(event)) {
|
|
126
|
+
finish();
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
function createSseProducer(res, state, { maxQueuedEvents, finish, pump }) {
|
|
104
132
|
const enqueue = ({ event }) => {
|
|
105
|
-
if (closed || ending)
|
|
133
|
+
if (state.closed || state.ending)
|
|
106
134
|
return;
|
|
107
|
-
if (queue.length >= maxQueuedEvents) {
|
|
135
|
+
if (state.queue.length >= maxQueuedEvents) {
|
|
108
136
|
// Slow/stalled consumer — disconnect rather than grow memory without bound.
|
|
109
137
|
finish();
|
|
110
138
|
return;
|
|
111
139
|
}
|
|
112
|
-
queue.push(event);
|
|
140
|
+
state.queue.push(event);
|
|
113
141
|
pump();
|
|
114
142
|
};
|
|
115
143
|
const open = () => {
|
|
116
|
-
if (closed || flowing)
|
|
144
|
+
if (state.closed || state.flowing)
|
|
117
145
|
return;
|
|
118
146
|
res.statusCode = 200;
|
|
119
147
|
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
|
120
148
|
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
|
121
149
|
res.setHeader('Connection', 'keep-alive');
|
|
122
150
|
res.flushHeaders();
|
|
123
|
-
flowing = true;
|
|
151
|
+
state.flowing = true;
|
|
124
152
|
pump();
|
|
125
153
|
};
|
|
126
|
-
|
|
127
|
-
writable = true;
|
|
128
|
-
pump();
|
|
129
|
-
});
|
|
130
|
-
// The client disconnecting only marks the channel closed — it must not itself call `res.end()`
|
|
131
|
-
// (see `end()`'s doc for why calling it a second time would be safe anyway, but a route that
|
|
132
|
-
// still wants to send a *non-SSE* response — e.g. a JSON error — after discovering a subscribe
|
|
133
|
-
// failed needs `isClosed()` to be observable without the response having been touched yet).
|
|
134
|
-
res.on('close', markClosed);
|
|
135
|
-
const onClose = ({ callback }) => {
|
|
136
|
-
if (closed) {
|
|
137
|
-
callback();
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
closeCallbacks.push(callback);
|
|
141
|
-
};
|
|
142
|
-
return {
|
|
143
|
-
enqueue,
|
|
144
|
-
open,
|
|
145
|
-
isClosed: () => closed,
|
|
146
|
-
end,
|
|
147
|
-
abandon: markClosed,
|
|
148
|
-
onClose,
|
|
149
|
-
};
|
|
154
|
+
return { enqueue, open };
|
|
150
155
|
}
|
|
151
156
|
/** Reads a reconnect cursor from the standard `Last-Event-ID` header, falling back to an `afterCursor` query parameter. Shared by any SSE route that supports reconnect replay (the header takes precedence since it is what browsers set automatically on `EventSource` reconnect). */
|
|
152
157
|
export function requestedAfterCursor(req) {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The `trust proxy` decision for a server behind a platform edge, without any server framework.
|
|
3
|
+
*
|
|
4
|
+
* Without it, every visitor behind a platform edge shares the edge's IP: per-IP rate limits become
|
|
5
|
+
* one global bucket and stored source IPs are the proxy's. Trusting X-Forwarded-For everywhere would
|
|
6
|
+
* be worse — a direct client could name any IP it likes. So it is trusted for exactly the hops a
|
|
7
|
+
* known platform puts in front of the app, and never when running locally or directly.
|
|
8
|
+
*
|
|
9
|
+
* With a hop count of N, the server takes the Nth address from the RIGHT of X-Forwarded-For — the
|
|
10
|
+
* one the outermost trusted proxy appended — so whatever the client wrote to the left is ignored.
|
|
11
|
+
*/
|
|
12
|
+
/** `false`, a hop count, or an address/subnet list (e.g. `"loopback, 10.0.0.0/8"`). */
|
|
13
|
+
export type TrustProxySetting = false | number | string;
|
|
14
|
+
/**
|
|
15
|
+
* Decide `trust proxy`.
|
|
16
|
+
*
|
|
17
|
+
* - `override` wins when non-empty: `false`/`0`/`off`/`no` -> trust none; an integer -> that many
|
|
18
|
+
* hops; anything else -> an address/subnet list. `true` is refused: it trusts every
|
|
19
|
+
* X-Forwarded-For entry, so any client could spoof its IP.
|
|
20
|
+
* - Otherwise the platform's declared hop count (`platformHops`, read only in this case).
|
|
21
|
+
* - Otherwise `false`: the socket peer is the client.
|
|
22
|
+
*
|
|
23
|
+
* @param required.override The operator's raw setting (e.g. an env value), or undefined.
|
|
24
|
+
* @param optional.platformHops Lazily reads the matched platform's hop count.
|
|
25
|
+
* @param optional.overrideName Names the setting in the refusal message. @param optional.serverLabel Names the server there.
|
|
26
|
+
* @throws Error when `override` is `true`.
|
|
27
|
+
* @complexity O(1) plus `platformHops`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveTrustProxySetting({ override }: {
|
|
30
|
+
override: string | undefined;
|
|
31
|
+
}, { platformHops, overrideName, serverLabel }?: {
|
|
32
|
+
platformHops?: () => number | undefined;
|
|
33
|
+
overrideName?: string;
|
|
34
|
+
serverLabel?: string;
|
|
35
|
+
}): TrustProxySetting;
|
|
36
|
+
//# sourceMappingURL=trust-proxy.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The `trust proxy` decision for a server behind a platform edge, without any server framework.
|
|
3
|
+
*
|
|
4
|
+
* Without it, every visitor behind a platform edge shares the edge's IP: per-IP rate limits become
|
|
5
|
+
* one global bucket and stored source IPs are the proxy's. Trusting X-Forwarded-For everywhere would
|
|
6
|
+
* be worse — a direct client could name any IP it likes. So it is trusted for exactly the hops a
|
|
7
|
+
* known platform puts in front of the app, and never when running locally or directly.
|
|
8
|
+
*
|
|
9
|
+
* With a hop count of N, the server takes the Nth address from the RIGHT of X-Forwarded-For — the
|
|
10
|
+
* one the outermost trusted proxy appended — so whatever the client wrote to the left is ignored.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Decide `trust proxy`.
|
|
14
|
+
*
|
|
15
|
+
* - `override` wins when non-empty: `false`/`0`/`off`/`no` -> trust none; an integer -> that many
|
|
16
|
+
* hops; anything else -> an address/subnet list. `true` is refused: it trusts every
|
|
17
|
+
* X-Forwarded-For entry, so any client could spoof its IP.
|
|
18
|
+
* - Otherwise the platform's declared hop count (`platformHops`, read only in this case).
|
|
19
|
+
* - Otherwise `false`: the socket peer is the client.
|
|
20
|
+
*
|
|
21
|
+
* @param required.override The operator's raw setting (e.g. an env value), or undefined.
|
|
22
|
+
* @param optional.platformHops Lazily reads the matched platform's hop count.
|
|
23
|
+
* @param optional.overrideName Names the setting in the refusal message. @param optional.serverLabel Names the server there.
|
|
24
|
+
* @throws Error when `override` is `true`.
|
|
25
|
+
* @complexity O(1) plus `platformHops`.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveTrustProxySetting({ override }, { platformHops, overrideName = 'TRUST_PROXY', serverLabel = 'this server' } = {}) {
|
|
28
|
+
const value = override?.trim() ?? '';
|
|
29
|
+
if (value !== '')
|
|
30
|
+
return parseOverride({ value }, { overrideName, serverLabel });
|
|
31
|
+
return platformHops?.() ?? false;
|
|
32
|
+
}
|
|
33
|
+
/** The non-empty operator setting as `false`, a hop count or an address list; refuses `true`. */
|
|
34
|
+
function parseOverride({ value }, { overrideName, serverLabel }) {
|
|
35
|
+
const lower = value.toLowerCase();
|
|
36
|
+
if (lower === 'true') {
|
|
37
|
+
throw new Error(`${overrideName}="true" would trust X-Forwarded-For from any client. Set the number of proxy hops in front of ${serverLabel} (e.g. 1), or a list of proxy addresses/subnets.`);
|
|
38
|
+
}
|
|
39
|
+
if (['false', '0', 'off', 'no'].includes(lower))
|
|
40
|
+
return false;
|
|
41
|
+
if (/^\d+$/.test(value))
|
|
42
|
+
return Number(value);
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=trust-proxy.js.map
|
package/dist/types.d.ts
CHANGED
|
@@ -1,58 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* (`
|
|
4
|
-
*
|
|
2
|
+
* Express-bound route-spec shapes (`JsonRoutePorts`, `JsonRouteSpec`) consumed by `mountJsonRoute`.
|
|
3
|
+
* The framework-free vocabulary (`Result`, `Handler`, `InputParser`, …) lives in
|
|
4
|
+
* `@jini-ai/core/contracts/server`.
|
|
5
5
|
*/
|
|
6
|
-
import type {
|
|
7
|
-
import type { ApiError } from '@jini-ai/protocol';
|
|
6
|
+
import type { Handler, HttpMethod, InputParser, RouteInputContext } from '@jini-ai/core/contracts/server';
|
|
8
7
|
import type { Response } from 'express';
|
|
9
|
-
/**
|
|
10
|
-
* A discriminated success/failure envelope used throughout the module in place of throwing.
|
|
11
|
-
* Route parsing, handling, and origin checks all resolve to a `Result` so the Adapter can fold
|
|
12
|
-
* every failure mode into one error-handling pipeline.
|
|
13
|
-
*/
|
|
14
|
-
export type Result<T, E = ApiError> = CoreResult<T, E>;
|
|
15
|
-
/** Builds a successful `Result` carrying `value`. */
|
|
16
|
-
export declare const ok: <T, E = ApiError>({ value }: {
|
|
17
|
-
readonly value: T;
|
|
18
|
-
}, _optional?: Record<string, never>) => Result<T, E>;
|
|
19
|
-
/** Builds a failed `Result` carrying `error`. */
|
|
20
|
-
export declare const err: <T = never, E = ApiError>({ error }: {
|
|
21
|
-
readonly error: E;
|
|
22
|
-
}, _optional?: Record<string, never>) => Result<T, E>;
|
|
23
|
-
/**
|
|
24
|
-
* The normalized shape of a raw HTTP request handed to a route's `parse` function: body, query,
|
|
25
|
-
* and params, decoupled from any framework `Request` type so parsers are unit-testable without
|
|
26
|
-
* a real server.
|
|
27
|
-
*/
|
|
28
|
-
export interface RouteInputContext {
|
|
29
|
-
body: unknown;
|
|
30
|
-
query: Record<string, unknown>;
|
|
31
|
-
params: Record<string, string>;
|
|
32
|
-
}
|
|
33
|
-
/** Parses a `RouteInputContext` into a typed `Input`, or fails with an `ApiError`. */
|
|
34
|
-
export type InputParser<Input> = (raw: RouteInputContext) => Result<Input>;
|
|
35
|
-
/**
|
|
36
|
-
* Handles a parsed `Input` (plus injected `Deps`) and produces a `Result<Output>`.
|
|
37
|
-
*
|
|
38
|
-
* `signal` is optional and additive: it aborts if the underlying HTTP request's connection drops
|
|
39
|
-
* before the Adapter has written a response (see `mountJsonRoute` in `adapter.ts`) — handlers read it from their optional arguments when they perform cancellable work.
|
|
40
|
-
*/
|
|
41
|
-
export type Handler<Input, Output, Deps, Context = undefined> = (requiredArgs: {
|
|
42
|
-
readonly input: Input;
|
|
43
|
-
readonly deps: Deps;
|
|
44
|
-
readonly context?: Context | undefined;
|
|
45
|
-
readonly authorize?: JsonRouteAuthorize;
|
|
46
|
-
}, optionalArgs?: {
|
|
47
|
-
readonly signal?: AbortSignal | undefined;
|
|
48
|
-
}) => Promise<Result<Output>> | Result<Output>;
|
|
49
|
-
/** HTTP verbs the Adapter can mount a `JsonRouteSpec` under. */
|
|
50
|
-
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
|
51
|
-
/** Explicit authorization remains at the route's chosen point, preserving validation precedence. */
|
|
52
|
-
export type JsonRouteAuthorize = (requiredArgs: {
|
|
53
|
-
readonly permission: string;
|
|
54
|
-
readonly entityType: string;
|
|
55
|
-
}, optionalArgs?: Record<string, never>) => Promise<void>;
|
|
56
8
|
/** Host-owned workspace, authentication/authorization and exception policy.
|
|
57
9
|
* Guards throw on rejection; the error port owns their wire envelope. Without an error port the
|
|
58
10
|
* adapter retains its caller-safe-error and SEC-005 redaction policy. Authentication can await
|
package/dist/types.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export const ok = ({ value }, _optional = {}) => ({ ok: true, value });
|
|
3
|
-
/** Builds a failed `Result` carrying `error`. */
|
|
4
|
-
export const err = ({ error }, _optional = {}) => ({ ok: false, error });
|
|
1
|
+
export {};
|
|
5
2
|
//# sourceMappingURL=types.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/http-kit",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3-next.1",
|
|
4
4
|
"description": "Generic Express transport primitives, request parsing, response serialization, origin guards and route-pack mounting.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
".": "node",
|
|
16
16
|
"./rate-limit": "node",
|
|
17
17
|
"./middleware": "node",
|
|
18
|
-
"./
|
|
18
|
+
"./settings": "node",
|
|
19
19
|
"./observability": "node",
|
|
20
|
-
"./html-response": "node"
|
|
20
|
+
"./html-response": "node",
|
|
21
|
+
"./range": "universal",
|
|
22
|
+
"./node-bridge": "node"
|
|
21
23
|
},
|
|
22
24
|
"runtime": "node"
|
|
23
25
|
},
|
|
@@ -36,16 +38,16 @@
|
|
|
36
38
|
"import": "./dist/rate-limit.js",
|
|
37
39
|
"default": "./dist/rate-limit.js"
|
|
38
40
|
},
|
|
41
|
+
"./settings": {
|
|
42
|
+
"types": "./dist/settings/index.d.ts",
|
|
43
|
+
"import": "./dist/settings/index.js",
|
|
44
|
+
"default": "./dist/settings/index.js"
|
|
45
|
+
},
|
|
39
46
|
"./middleware": {
|
|
40
47
|
"types": "./dist/middleware.d.ts",
|
|
41
48
|
"import": "./dist/middleware.js",
|
|
42
49
|
"default": "./dist/middleware.js"
|
|
43
50
|
},
|
|
44
|
-
"./verified-origin": {
|
|
45
|
-
"types": "./dist/verified-origin.d.ts",
|
|
46
|
-
"import": "./dist/verified-origin.js",
|
|
47
|
-
"default": "./dist/verified-origin.js"
|
|
48
|
-
},
|
|
49
51
|
"./observability": {
|
|
50
52
|
"types": "./dist/observability.d.ts",
|
|
51
53
|
"import": "./dist/observability.js",
|
|
@@ -55,6 +57,16 @@
|
|
|
55
57
|
"types": "./dist/html-response.d.ts",
|
|
56
58
|
"import": "./dist/html-response.js",
|
|
57
59
|
"default": "./dist/html-response.js"
|
|
60
|
+
},
|
|
61
|
+
"./range": {
|
|
62
|
+
"types": "./dist/range.d.ts",
|
|
63
|
+
"import": "./dist/range.js",
|
|
64
|
+
"default": "./dist/range.js"
|
|
65
|
+
},
|
|
66
|
+
"./node-bridge": {
|
|
67
|
+
"types": "./dist/node-bridge.d.ts",
|
|
68
|
+
"import": "./dist/node-bridge.js",
|
|
69
|
+
"default": "./dist/node-bridge.js"
|
|
58
70
|
}
|
|
59
71
|
},
|
|
60
72
|
"files": [
|
|
@@ -77,14 +89,12 @@
|
|
|
77
89
|
"@types/express": "^4.17.21",
|
|
78
90
|
"@vitest/coverage-v8": "^2.1.9",
|
|
79
91
|
"express": "^4.21.0",
|
|
80
|
-
"@jini-ai/
|
|
81
|
-
"@jini-ai/
|
|
82
|
-
"@jini-ai/protocol": "0.7.2"
|
|
92
|
+
"@jini-ai/core": "0.7.3-next.1",
|
|
93
|
+
"@jini-ai/protocol": "0.7.3-next.1"
|
|
83
94
|
},
|
|
84
95
|
"dependencies": {
|
|
85
|
-
"@jini-ai/
|
|
86
|
-
"@jini-ai/
|
|
87
|
-
"@jini-ai/protocol": "^0.7.2"
|
|
96
|
+
"@jini-ai/protocol": "^0.7.3-next.1",
|
|
97
|
+
"@jini-ai/core": "^0.7.3-next.1"
|
|
88
98
|
},
|
|
89
99
|
"scripts": {
|
|
90
100
|
"build": "tsc -p tsconfig.json",
|
package/dist/active-context.d.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module active-context
|
|
3
|
-
*
|
|
4
|
-
* Generic "what resource is the caller currently focused on?" channel: an
|
|
5
|
-
* in-memory, TTL-scoped pointer a client POSTs to record its current focus
|
|
6
|
-
* and GETs to read back (resolved against a real display name via an
|
|
7
|
-
* injected lookup). Ported from the routes-classification table's
|
|
8
|
-
* `active-context.ts` MIXED row (see `source-map.md`) — the origin's only
|
|
9
|
-
* real coupling was `handleGetActive` calling into an OD project store to
|
|
10
|
-
* resolve a display name; the TTL store and both handlers were already
|
|
11
|
-
* product-neutral, and all handlers are synchronous (no async risk).
|
|
12
|
-
*
|
|
13
|
-
* The origin's `projectId`/`fileName` fields are generalized here to a
|
|
14
|
-
* single opaque `resourceRef` (was `projectId`) plus an optional `detail`
|
|
15
|
-
* string (was `fileName` — a sub-locator within that resource), matching
|
|
16
|
-
* the source-map's own "renaming to a generic resource ref" note. The
|
|
17
|
-
* origin's `deps.getProject` lookup becomes an injected `resolveResource`
|
|
18
|
-
* dependency, matching this branch's DI convention (see
|
|
19
|
-
* `daemon-status.ts`/`host-tools.ts`: real collaborators supplied by the
|
|
20
|
-
* caller rather than read from a global store).
|
|
21
|
-
*/
|
|
22
|
-
import type { Express } from 'express';
|
|
23
|
-
import { type AdapterContext } from './adapter.js';
|
|
24
|
-
/** How long a recorded focus stays valid before a GET treats it as stale and clears it. */
|
|
25
|
-
export declare const ACTIVE_CONTEXT_TTL_MS: number;
|
|
26
|
-
interface ActiveContext {
|
|
27
|
-
resourceRef: string;
|
|
28
|
-
detail: string | null;
|
|
29
|
-
ts: number;
|
|
30
|
-
}
|
|
31
|
-
interface ActiveContextStore {
|
|
32
|
-
current: ActiveContext | null;
|
|
33
|
-
}
|
|
34
|
-
type SetActiveInput = {
|
|
35
|
-
kind: 'clear';
|
|
36
|
-
} | {
|
|
37
|
-
kind: 'set';
|
|
38
|
-
resourceRef: string;
|
|
39
|
-
detail: string | null;
|
|
40
|
-
};
|
|
41
|
-
type SetActiveOutput = {
|
|
42
|
-
active: false;
|
|
43
|
-
} | {
|
|
44
|
-
active: true;
|
|
45
|
-
resourceRef: string;
|
|
46
|
-
detail: string | null;
|
|
47
|
-
ts: number;
|
|
48
|
-
};
|
|
49
|
-
type GetActiveOutput = {
|
|
50
|
-
active: false;
|
|
51
|
-
} | {
|
|
52
|
-
active: true;
|
|
53
|
-
resourceRef: string;
|
|
54
|
-
resourceName: string | null;
|
|
55
|
-
detail: string | null;
|
|
56
|
-
ts: number;
|
|
57
|
-
ageMs: number;
|
|
58
|
-
};
|
|
59
|
-
/** A resolved resource's display name. `resolveResource` itself may return `null`/`undefined` (not this type) when the caller has no entry at all for a given resource ref (e.g. unknown or deleted). */
|
|
60
|
-
export interface ActiveContextResource {
|
|
61
|
-
readonly name?: string | null;
|
|
62
|
-
}
|
|
63
|
-
/** What a caller must supply to mount the active-context routes. */
|
|
64
|
-
export interface ActiveContextDeps {
|
|
65
|
-
/**
|
|
66
|
-
* Resolves a display name for a resource ref (was OD's project store's
|
|
67
|
-
* `getProject`). Injected so this module has no dependency on any
|
|
68
|
-
* particular resource store; may return `null`/`undefined` if the ref is
|
|
69
|
-
* unknown.
|
|
70
|
-
*/
|
|
71
|
-
resolveResource: (resourceRef: string) => ActiveContextResource | null | undefined;
|
|
72
|
-
/** Returns the current time in epoch ms. Defaults to `Date.now` when omitted. */
|
|
73
|
-
now?: () => number;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Internal deps threaded to both handlers: the caller-supplied
|
|
77
|
-
* {@link ActiveContextDeps} plus the shared in-memory store built once per
|
|
78
|
-
* `registerActiveContextRoutes` call, so the two routes see the same state.
|
|
79
|
-
*/
|
|
80
|
-
interface ActiveContextRouteDeps {
|
|
81
|
-
store: ActiveContextStore;
|
|
82
|
-
resolveResource: ActiveContextDeps['resolveResource'];
|
|
83
|
-
now: () => number;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* `POST /api/active` — records (or, with `{active: false}`, clears) the
|
|
87
|
-
* caller's current resource focus.
|
|
88
|
-
*/
|
|
89
|
-
export declare const setActiveRoute: import("./types.js").JsonRouteSpec<SetActiveInput, SetActiveOutput, ActiveContextRouteDeps>;
|
|
90
|
-
/**
|
|
91
|
-
* `GET /api/active` — reads back the current resource focus, resolving a
|
|
92
|
-
* display name for it, or `{active: false}` if nothing is set or the
|
|
93
|
-
* recorded focus has aged past {@link ACTIVE_CONTEXT_TTL_MS}.
|
|
94
|
-
*/
|
|
95
|
-
export declare const getActiveRoute: import("./types.js").JsonRouteSpec<void, GetActiveOutput, ActiveContextRouteDeps>;
|
|
96
|
-
/** Mounts both active-context routes on `app`, sharing one in-memory store between them. A pack's `http(app, services)` calls this directly. */
|
|
97
|
-
export declare function registerActiveContextRoutes(app: Express, deps: ActiveContextDeps, adapter: AdapterContext): void;
|
|
98
|
-
export {};
|
|
99
|
-
//# sourceMappingURL=active-context.d.ts.map
|
package/dist/active-context.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { createApiError } from '@jini-ai/protocol';
|
|
2
|
-
import { defineJsonRoute, mountJsonRoute } from './adapter.js';
|
|
3
|
-
import { err, ok } from './types.js';
|
|
4
|
-
/** How long a recorded focus stays valid before a GET treats it as stale and clears it. */
|
|
5
|
-
export const ACTIVE_CONTEXT_TTL_MS = 5 * 60 * 1000;
|
|
6
|
-
function parseSetActive(raw) {
|
|
7
|
-
const body = (raw.body ?? {});
|
|
8
|
-
if (body.active === false) {
|
|
9
|
-
return ok({ kind: 'clear' });
|
|
10
|
-
}
|
|
11
|
-
const resourceRef = typeof body.resourceRef === 'string' ? body.resourceRef : '';
|
|
12
|
-
if (!resourceRef) {
|
|
13
|
-
return err(createApiError('BAD_REQUEST', 'resourceRef is required'));
|
|
14
|
-
}
|
|
15
|
-
const detail = typeof body.detail === 'string' && body.detail.length > 0 ? body.detail : null;
|
|
16
|
-
return ok({ kind: 'set', resourceRef, detail });
|
|
17
|
-
}
|
|
18
|
-
function handleSetActive(input, deps) {
|
|
19
|
-
if (input.kind === 'clear') {
|
|
20
|
-
deps.store.current = null;
|
|
21
|
-
return ok({ active: false });
|
|
22
|
-
}
|
|
23
|
-
const next = { resourceRef: input.resourceRef, detail: input.detail, ts: deps.now() };
|
|
24
|
-
deps.store.current = next;
|
|
25
|
-
return ok({ active: true, ...next });
|
|
26
|
-
}
|
|
27
|
-
function handleGetActive(_input, deps) {
|
|
28
|
-
const current = deps.store.current;
|
|
29
|
-
if (!current || deps.now() - current.ts > ACTIVE_CONTEXT_TTL_MS) {
|
|
30
|
-
deps.store.current = null;
|
|
31
|
-
return ok({ active: false });
|
|
32
|
-
}
|
|
33
|
-
const resource = deps.resolveResource(current.resourceRef);
|
|
34
|
-
return ok({
|
|
35
|
-
active: true,
|
|
36
|
-
resourceRef: current.resourceRef,
|
|
37
|
-
resourceName: resource?.name ?? null,
|
|
38
|
-
detail: current.detail,
|
|
39
|
-
ts: current.ts,
|
|
40
|
-
ageMs: deps.now() - current.ts,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* `POST /api/active` — records (or, with `{active: false}`, clears) the
|
|
45
|
-
* caller's current resource focus.
|
|
46
|
-
*/
|
|
47
|
-
export const setActiveRoute = defineJsonRoute({
|
|
48
|
-
method: 'post',
|
|
49
|
-
path: '/api/active',
|
|
50
|
-
requireSameOrigin: true,
|
|
51
|
-
parse: parseSetActive,
|
|
52
|
-
handle: handleSetActive,
|
|
53
|
-
});
|
|
54
|
-
/**
|
|
55
|
-
* `GET /api/active` — reads back the current resource focus, resolving a
|
|
56
|
-
* display name for it, or `{active: false}` if nothing is set or the
|
|
57
|
-
* recorded focus has aged past {@link ACTIVE_CONTEXT_TTL_MS}.
|
|
58
|
-
*/
|
|
59
|
-
export const getActiveRoute = defineJsonRoute({
|
|
60
|
-
method: 'get',
|
|
61
|
-
path: '/api/active',
|
|
62
|
-
requireSameOrigin: true,
|
|
63
|
-
parse: () => ok(undefined),
|
|
64
|
-
handle: handleGetActive,
|
|
65
|
-
});
|
|
66
|
-
/** Mounts both active-context routes on `app`, sharing one in-memory store between them. A pack's `http(app, services)` calls this directly. */
|
|
67
|
-
export function registerActiveContextRoutes(app, deps, adapter) {
|
|
68
|
-
const store = { current: null };
|
|
69
|
-
const routeDeps = {
|
|
70
|
-
store,
|
|
71
|
-
resolveResource: deps.resolveResource,
|
|
72
|
-
now: deps.now ?? (() => Date.now()),
|
|
73
|
-
};
|
|
74
|
-
mountJsonRoute(app, setActiveRoute, routeDeps, adapter);
|
|
75
|
-
mountJsonRoute(app, getActiveRoute, routeDeps, adapter);
|
|
76
|
-
}
|
|
77
|
-
//# sourceMappingURL=active-context.js.map
|