@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/rate-limit.js
CHANGED
|
@@ -14,6 +14,10 @@ export function createMemoryCounterStore(_required, _optional = {}) {
|
|
|
14
14
|
size: async () => windows.size,
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
+
/** Whole seconds a rejected caller must wait for its window to end; never less than one. */
|
|
18
|
+
function secondsUntilWindowEnds({ windowStartMs, windowMs, nowMs }) {
|
|
19
|
+
return Math.max(1, Math.ceil((windowStartMs + windowMs - nowMs) / 1000));
|
|
20
|
+
}
|
|
17
21
|
// An injected clock makes window boundaries deterministic without real-time sleeps; a fresh
|
|
18
22
|
// dedicated store keeps limiter instances isolated instead of sharing module-global counters.
|
|
19
23
|
/**
|
|
@@ -76,8 +80,7 @@ export function createRateLimiter({ profile, clock, store }, { getMax } = {}) {
|
|
|
76
80
|
await store.set({ key, state: { ...existing, count: existing.count + 1 } });
|
|
77
81
|
return { allowed: true };
|
|
78
82
|
}
|
|
79
|
-
|
|
80
|
-
return { allowed: false, retryAfterSeconds };
|
|
83
|
+
return { allowed: false, retryAfterSeconds: secondsUntilWindowEnds({ windowStartMs: existing.windowStartMs, windowMs, nowMs }) };
|
|
81
84
|
}
|
|
82
85
|
return {
|
|
83
86
|
check: async (required) => {
|
package/dist/request.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Request-side normalization: turns a raw Express `Request` into the framework-independent
|
|
3
|
-
* `RouteInputContext` the rest of the module operates on
|
|
4
|
-
* `ApiError` for validation failures.
|
|
3
|
+
* `RouteInputContext` the rest of the module operates on.
|
|
5
4
|
*/
|
|
6
5
|
import type { Request } from 'express';
|
|
7
|
-
import
|
|
8
|
-
import type { RouteInputContext } from './types.js';
|
|
6
|
+
import type { RouteInputContext } from '@jini-ai/core/contracts/server';
|
|
9
7
|
/**
|
|
10
8
|
* Extracts `body`/`query`/`params` from an Express `Request` into a `RouteInputContext`, so a
|
|
11
9
|
* route's `parse` function can be unit-tested without constructing a real `Request`.
|
|
@@ -13,13 +11,4 @@ import type { RouteInputContext } from './types.js';
|
|
|
13
11
|
export declare function rawInput({ req }: {
|
|
14
12
|
readonly req: Request;
|
|
15
13
|
}, _optional?: Record<string, never>): RouteInputContext;
|
|
16
|
-
/**
|
|
17
|
-
* Builds a `BAD_REQUEST` `ApiError` for a failed parse. When `issues` is non-empty, attaches
|
|
18
|
-
* them as structured validation details so clients can render per-field errors.
|
|
19
|
-
*/
|
|
20
|
-
export declare function validationError({ message }: {
|
|
21
|
-
readonly message: string;
|
|
22
|
-
}, { issues }?: {
|
|
23
|
-
readonly issues?: Pick<ApiValidationIssue, 'path' | 'message'>[] | undefined;
|
|
24
|
-
}): ApiError;
|
|
25
14
|
//# sourceMappingURL=request.d.ts.map
|
package/dist/request.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createApiError } from '@jini-ai/protocol';
|
|
2
1
|
/**
|
|
3
2
|
* Extracts `body`/`query`/`params` from an Express `Request` into a `RouteInputContext`, so a
|
|
4
3
|
* route's `parse` function can be unit-tested without constructing a real `Request`.
|
|
@@ -10,16 +9,4 @@ export function rawInput({ req }, _optional = {}) {
|
|
|
10
9
|
params: (req.params ?? {}),
|
|
11
10
|
};
|
|
12
11
|
}
|
|
13
|
-
/**
|
|
14
|
-
* Builds a `BAD_REQUEST` `ApiError` for a failed parse. When `issues` is non-empty, attaches
|
|
15
|
-
* them as structured validation details so clients can render per-field errors.
|
|
16
|
-
*/
|
|
17
|
-
export function validationError({ message }, { issues = [] } = {}) {
|
|
18
|
-
if (issues.length === 0) {
|
|
19
|
-
return createApiError({ code: 'BAD_REQUEST', message: message });
|
|
20
|
-
}
|
|
21
|
-
return createApiError({ code: 'BAD_REQUEST', message: message }, {
|
|
22
|
-
details: { kind: 'validation', issues },
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
12
|
//# sourceMappingURL=request.js.map
|
package/dist/response.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Response-side serialization: writes a plain JSON body or a standard `ApiError` envelope onto
|
|
3
|
-
* an Express `Response
|
|
4
|
-
* dependencies — this is the terminal step of the Adapter's error-handling pipeline.
|
|
3
|
+
* an Express `Response` The status mapping (`statusForError`) lives in core. This is the terminal step of the Adapter's error-handling pipeline.
|
|
5
4
|
*/
|
|
6
5
|
import type { Response } from 'express';
|
|
7
6
|
import { type ApiError } from '@jini-ai/protocol';
|
|
7
|
+
/** The one response capability an API error write needs. Express's `Response` satisfies it structurally; a host on another server framework adapts its own response object to it. */
|
|
8
|
+
export interface ApiErrorResponseWriter {
|
|
9
|
+
status(code: number): {
|
|
10
|
+
json(body: unknown): unknown;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
8
13
|
/** Writes `body` as JSON with the given status code. */
|
|
9
14
|
export declare function sendJson({ res, status, body }: {
|
|
10
15
|
readonly res: Response;
|
|
@@ -15,12 +20,8 @@ export declare function sendJson({ res, status, body }: {
|
|
|
15
20
|
* Older hand-mounted routes supplied code/message and metadata separately; construct their
|
|
16
21
|
* ApiError first so both generations now share one serialization and envelope policy. */
|
|
17
22
|
export declare function sendApiError({ res, status, error }: {
|
|
18
|
-
readonly res:
|
|
23
|
+
readonly res: ApiErrorResponseWriter;
|
|
19
24
|
readonly status: number;
|
|
20
25
|
readonly error: ApiError;
|
|
21
26
|
}, _optional?: Record<string, never>): void;
|
|
22
|
-
/** Resolves the HTTP status to send for an `ApiError`, defaulting to 500 for unmapped codes. */
|
|
23
|
-
export declare function statusForError({ error }: {
|
|
24
|
-
readonly error: ApiError;
|
|
25
|
-
}, _optional?: Record<string, never>): number;
|
|
26
27
|
//# sourceMappingURL=response.d.ts.map
|
package/dist/response.js
CHANGED
|
@@ -9,40 +9,4 @@ export function sendJson({ res, status, body }, _optional = {}) {
|
|
|
9
9
|
export function sendApiError({ res, status, error }, _optional = {}) {
|
|
10
10
|
res.status(status).json(createApiErrorResponse({ error: error }));
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
13
|
-
* @internal
|
|
14
|
-
* Default HTTP status per generic `ApiErrorCode`. Codes not listed here fall back to 500 in
|
|
15
|
-
* `statusForError` — deliberately conservative, since an unmapped code (including any
|
|
16
|
-
* product-specific code a consuming pack defines) is more likely a new server-side failure mode
|
|
17
|
-
* than a client error.
|
|
18
|
-
*/
|
|
19
|
-
const ERROR_STATUS_BY_CODE = {
|
|
20
|
-
BAD_REQUEST: 400,
|
|
21
|
-
UNAUTHORIZED: 401,
|
|
22
|
-
FORBIDDEN: 403,
|
|
23
|
-
NOT_FOUND: 404,
|
|
24
|
-
CONFLICT: 409,
|
|
25
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
26
|
-
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
27
|
-
VALIDATION_FAILED: 422,
|
|
28
|
-
RATE_LIMITED: 429,
|
|
29
|
-
INTERNAL_ERROR: 500,
|
|
30
|
-
UPSTREAM_UNAVAILABLE: 502,
|
|
31
|
-
SERVICE_UNAVAILABLE: 503,
|
|
32
|
-
NOT_CONFIGURED: 503,
|
|
33
|
-
TOOL_TOKEN_MISSING: 401,
|
|
34
|
-
TOOL_TOKEN_INVALID: 401,
|
|
35
|
-
TOOL_TOKEN_EXPIRED: 401,
|
|
36
|
-
TOOL_ENDPOINT_DENIED: 403,
|
|
37
|
-
TOOL_OPERATION_DENIED: 403,
|
|
38
|
-
TOOL_NOT_AVAILABLE: 503,
|
|
39
|
-
TOOL_EXECUTION_FAILED: 422,
|
|
40
|
-
REMOTE_TOOL_BRIDGE_NOT_CONFIGURED: 503,
|
|
41
|
-
REMOTE_TOOL_BRIDGE_TOKEN_REQUIRED: 401,
|
|
42
|
-
OAUTH_FLOW_IN_PROGRESS: 409,
|
|
43
|
-
};
|
|
44
|
-
/** Resolves the HTTP status to send for an `ApiError`, defaulting to 500 for unmapped codes. */
|
|
45
|
-
export function statusForError({ error }, _optional = {}) {
|
|
46
|
-
return ERROR_STATUS_BY_CODE[error.code] ?? 500;
|
|
47
|
-
}
|
|
48
12
|
//# sourceMappingURL=response.js.map
|
|
@@ -70,4 +70,18 @@ export declare function installRouteRegistrationGuard({ app }: {
|
|
|
70
70
|
export declare function getRouteRegistrationInventory({ app }: {
|
|
71
71
|
readonly app: Express;
|
|
72
72
|
}, _optional?: Record<string, never>): RouteRegistration[];
|
|
73
|
+
/**
|
|
74
|
+
* Adds routes a host mounted as a list (one `app.use` layer, not one registration per route) to the
|
|
75
|
+
* inventory {@link installRouteRegistrationGuard} keeps, so {@link getRouteRegistrationInventory}
|
|
76
|
+
* still shows everything the app answers. Inventory only: a listed route is never checked against
|
|
77
|
+
* `guardedRouteKeys`.
|
|
78
|
+
*
|
|
79
|
+
* @param app - The app the routes were mounted on. An app the guard was never installed on is left alone.
|
|
80
|
+
* @param registrations - The method and path of each listed route, in list order.
|
|
81
|
+
* @complexity O(routes).
|
|
82
|
+
*/
|
|
83
|
+
export declare function recordRouteRegistrations({ app, registrations }: {
|
|
84
|
+
readonly app: Express;
|
|
85
|
+
readonly registrations: readonly RouteRegistration[];
|
|
86
|
+
}, _optional?: Record<string, never>): void;
|
|
73
87
|
//# sourceMappingURL=route-registration-guard.d.ts.map
|
|
@@ -68,4 +68,21 @@ export function installRouteRegistrationGuard({ app }, options = {}) {
|
|
|
68
68
|
export function getRouteRegistrationInventory({ app }, _optional = {}) {
|
|
69
69
|
return (app[routeInventorySymbol] ?? []).map((entry) => ({ ...entry }));
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Adds routes a host mounted as a list (one `app.use` layer, not one registration per route) to the
|
|
73
|
+
* inventory {@link installRouteRegistrationGuard} keeps, so {@link getRouteRegistrationInventory}
|
|
74
|
+
* still shows everything the app answers. Inventory only: a listed route is never checked against
|
|
75
|
+
* `guardedRouteKeys`.
|
|
76
|
+
*
|
|
77
|
+
* @param app - The app the routes were mounted on. An app the guard was never installed on is left alone.
|
|
78
|
+
* @param registrations - The method and path of each listed route, in list order.
|
|
79
|
+
* @complexity O(routes).
|
|
80
|
+
*/
|
|
81
|
+
export function recordRouteRegistrations({ app, registrations }, _optional = {}) {
|
|
82
|
+
const inventory = app[routeInventorySymbol];
|
|
83
|
+
if (inventory === undefined)
|
|
84
|
+
return;
|
|
85
|
+
for (const { method, path } of registrations)
|
|
86
|
+
inventory.push({ method, path });
|
|
87
|
+
}
|
|
71
88
|
//# sourceMappingURL=route-registration-guard.js.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { RequestHandler } from 'express';
|
|
2
|
+
/** Structural result of the daemon's transport-independent credential/ownership policies. */
|
|
3
|
+
export type RunAccessDecision = {
|
|
4
|
+
readonly allowed: true;
|
|
5
|
+
readonly principalHeader?: {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly value: string;
|
|
8
|
+
};
|
|
9
|
+
} | {
|
|
10
|
+
readonly allowed: false;
|
|
11
|
+
readonly status: 400 | 401 | 403 | 404 | 503;
|
|
12
|
+
readonly body: unknown;
|
|
13
|
+
};
|
|
14
|
+
export interface DaemonAuthRequired {
|
|
15
|
+
readonly authorizationHeaderName: string;
|
|
16
|
+
/** Bind the daemon policy's environment, crypto, route and credential ports at composition. */
|
|
17
|
+
readonly authorize: (required: {
|
|
18
|
+
request: {
|
|
19
|
+
method: string;
|
|
20
|
+
path: string;
|
|
21
|
+
headers: Readonly<Record<string, string | undefined>>;
|
|
22
|
+
body?: unknown;
|
|
23
|
+
};
|
|
24
|
+
}, optional: {
|
|
25
|
+
validateDelegatedRunId: boolean;
|
|
26
|
+
}) => RunAccessDecision;
|
|
27
|
+
}
|
|
28
|
+
/** Mount before JSON parsing, then again on delegated calls after parsing with body validation.
|
|
29
|
+
* Only the allowed policy result may replace the principal header; trusted proxy headers remain intact.
|
|
30
|
+
* Policy exceptions go to Express's error boundary. No request body is accessed in the first gate.
|
|
31
|
+
* @complexity O(1) adapter work, excluding the injected authorization policy.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createDaemonAuthMiddleware({ authorize, authorizationHeaderName }: DaemonAuthRequired, { validateDelegatedRunId }?: {
|
|
34
|
+
validateDelegatedRunId?: boolean;
|
|
35
|
+
}): RequestHandler;
|
|
36
|
+
export interface RunOwnershipRequired {
|
|
37
|
+
readonly principalHeaderName: string;
|
|
38
|
+
readonly isEventStream: (required: {
|
|
39
|
+
path: string;
|
|
40
|
+
}) => boolean;
|
|
41
|
+
/** Bind authorizeRunOwnership's registry, run lookup and header policy at composition. */
|
|
42
|
+
readonly authorize: (required: {
|
|
43
|
+
runId: string | undefined;
|
|
44
|
+
principalId: string | undefined;
|
|
45
|
+
eventStream: boolean;
|
|
46
|
+
}) => Promise<RunAccessDecision>;
|
|
47
|
+
}
|
|
48
|
+
/** Mount after authentication on routes with a runId parameter. Denial envelopes pass through intact.
|
|
49
|
+
* @complexity O(1) adapter work plus the injected ownership lookup; no storage or identity defaults.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createRunOwnershipMiddleware({ authorize, principalHeaderName, isEventStream }: RunOwnershipRequired, _optional?: Record<string, never>): RequestHandler;
|
|
52
|
+
export interface OwnedRunListRequired<Run> {
|
|
53
|
+
readonly principalHeaderName: string;
|
|
54
|
+
readonly listRuns: (required: Record<string, never>, optional: {
|
|
55
|
+
contextRef?: string;
|
|
56
|
+
}) => Promise<readonly Run[]>;
|
|
57
|
+
/** Bind listOwnedRuns's registry and principal-header policy at composition. */
|
|
58
|
+
readonly filterOwnedRuns: (required: {
|
|
59
|
+
runs: readonly Run[];
|
|
60
|
+
principalId: string | undefined;
|
|
61
|
+
}) => {
|
|
62
|
+
allowed: true;
|
|
63
|
+
runs: Run[];
|
|
64
|
+
} | Extract<RunAccessDecision, {
|
|
65
|
+
allowed: false;
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
/** List only the authenticated principal's runs, after the host's context filter.
|
|
69
|
+
* Missing principals receive the existing 401 envelope before the list port is called.
|
|
70
|
+
* @complexity O(n) policy filtering for n listed runs; O(1) adapter space excluding results.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createOwnedRunListHandler<Run>({ listRuns, filterOwnedRuns, principalHeaderName }: OwnedRunListRequired<Run>, _optional?: Record<string, never>): RequestHandler;
|
|
73
|
+
//# sourceMappingURL=run-credentials.d.ts.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Mount before JSON parsing, then again on delegated calls after parsing with body validation.
|
|
2
|
+
* Only the allowed policy result may replace the principal header; trusted proxy headers remain intact.
|
|
3
|
+
* Policy exceptions go to Express's error boundary. No request body is accessed in the first gate.
|
|
4
|
+
* @complexity O(1) adapter work, excluding the injected authorization policy.
|
|
5
|
+
*/
|
|
6
|
+
export function createDaemonAuthMiddleware({ authorize, authorizationHeaderName }, { validateDelegatedRunId = false } = {}) {
|
|
7
|
+
return (req, res, next) => {
|
|
8
|
+
try {
|
|
9
|
+
const decision = authorize({ request: {
|
|
10
|
+
method: req.method, path: req.path,
|
|
11
|
+
headers: { [authorizationHeaderName]: req.get(authorizationHeaderName) },
|
|
12
|
+
...(validateDelegatedRunId ? { body: req.body } : {}),
|
|
13
|
+
} }, { validateDelegatedRunId });
|
|
14
|
+
if (!decision.allowed) {
|
|
15
|
+
res.status(decision.status).json(decision.body);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (decision.principalHeader)
|
|
19
|
+
req.headers[decision.principalHeader.name.toLowerCase()] = decision.principalHeader.value;
|
|
20
|
+
next();
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
next(error);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** Mount after authentication on routes with a runId parameter. Denial envelopes pass through intact.
|
|
28
|
+
* @complexity O(1) adapter work plus the injected ownership lookup; no storage or identity defaults.
|
|
29
|
+
*/
|
|
30
|
+
export function createRunOwnershipMiddleware({ authorize, principalHeaderName, isEventStream }, _optional = {}) {
|
|
31
|
+
return async (req, res, next) => {
|
|
32
|
+
try {
|
|
33
|
+
const decision = await authorize({ runId: req.params.runId || undefined, principalId: req.get(principalHeaderName) || undefined, eventStream: isEventStream({ path: req.path }) });
|
|
34
|
+
if (!decision.allowed) {
|
|
35
|
+
res.status(decision.status).json(decision.body);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
next();
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
next(error);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** List only the authenticated principal's runs, after the host's context filter.
|
|
46
|
+
* Missing principals receive the existing 401 envelope before the list port is called.
|
|
47
|
+
* @complexity O(n) policy filtering for n listed runs; O(1) adapter space excluding results.
|
|
48
|
+
*/
|
|
49
|
+
export function createOwnedRunListHandler({ listRuns, filterOwnedRuns, principalHeaderName }, _optional = {}) {
|
|
50
|
+
return async (req, res, next) => {
|
|
51
|
+
try {
|
|
52
|
+
const principalId = req.get(principalHeaderName) || undefined;
|
|
53
|
+
if (!principalId) {
|
|
54
|
+
res.status(401).json({ error: { code: 'UNAUTHENTICATED', message: `${principalHeaderName} is required on run-scoped requests` } });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const contextRef = typeof req.query.contextRef === 'string' ? req.query.contextRef : undefined;
|
|
58
|
+
const runs = await listRuns({}, contextRef === undefined ? {} : { contextRef });
|
|
59
|
+
const decision = filterOwnedRuns({ runs, principalId });
|
|
60
|
+
if (!decision.allowed) {
|
|
61
|
+
res.status(decision.status).json(decision.body);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
res.json({ runs: decision.runs });
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
next(error);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=run-credentials.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Request, Response, Express } from "express";
|
|
2
|
+
import type { Authorize, SettingsPermissions, SettingsService, SettingsChangeFeed } from "@jini-ai/core/settings";
|
|
3
|
+
export type PrincipalResolver = (required: {
|
|
4
|
+
request: Request;
|
|
5
|
+
response: Response;
|
|
6
|
+
}) => Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
} | null> | {
|
|
9
|
+
id: string;
|
|
10
|
+
} | null;
|
|
11
|
+
export interface Scheduler {
|
|
12
|
+
/** Return an idempotent cancellation callback. */
|
|
13
|
+
every(required: {
|
|
14
|
+
intervalMs: number;
|
|
15
|
+
callback: () => void;
|
|
16
|
+
}): () => void;
|
|
17
|
+
}
|
|
18
|
+
export interface SettingsRoutes {
|
|
19
|
+
definitions: string;
|
|
20
|
+
effective: string;
|
|
21
|
+
raw: string;
|
|
22
|
+
value: string;
|
|
23
|
+
reset: string;
|
|
24
|
+
events: string;
|
|
25
|
+
workspaceParam: string;
|
|
26
|
+
}
|
|
27
|
+
export interface SettingsHttpRequired {
|
|
28
|
+
app: Pick<Express, "get" | "post" | "put" | "delete">;
|
|
29
|
+
workspaceId: string;
|
|
30
|
+
ready: Promise<void>;
|
|
31
|
+
service: SettingsService;
|
|
32
|
+
changeFeed: SettingsChangeFeed;
|
|
33
|
+
principalResolver: PrincipalResolver;
|
|
34
|
+
authorize: Authorize;
|
|
35
|
+
scheduler: Scheduler;
|
|
36
|
+
routes: SettingsRoutes;
|
|
37
|
+
permissions: SettingsPermissions;
|
|
38
|
+
}
|
|
39
|
+
export interface SettingsHttpOptions {
|
|
40
|
+
pollIntervalMs?: number;
|
|
41
|
+
keepaliveIntervalMs?: number;
|
|
42
|
+
reauthorizeIntervalMs?: number;
|
|
43
|
+
revisionPageSize?: number;
|
|
44
|
+
onError?: (required: {
|
|
45
|
+
operation: string;
|
|
46
|
+
error: unknown;
|
|
47
|
+
}) => void;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=contracts.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Request, Response } from "express";
|
|
2
|
+
import type { SettingScope } from "@jini-ai/core/settings";
|
|
3
|
+
import type { SettingsHttpOptions, SettingsHttpRequired } from "./contracts.js";
|
|
4
|
+
import { type SettingsErrorMapping } from "./shared.js";
|
|
5
|
+
/**
|
|
6
|
+
* @file Request-level steps of the settings HTTP registrar (`http.ts`): the
|
|
7
|
+
* authentication gate, the failure envelope and value-write body parsing.
|
|
8
|
+
* Pure or response-only; no service, authorization or storage effects.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SETTING_SCOPES: readonly string[];
|
|
11
|
+
export declare function unauthenticated(res: Response): void;
|
|
12
|
+
/** Readiness/principal failures preserve the authentication boundary's existing 401 envelope, so both collapse to null. */
|
|
13
|
+
export declare function resolvePrincipal(required: Pick<SettingsHttpRequired, "ready" | "principalResolver">, context: {
|
|
14
|
+
request: Request;
|
|
15
|
+
response: Response;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
id: string;
|
|
18
|
+
} | null>;
|
|
19
|
+
/** Map a handler failure to its envelope, or end a response whose headers have already been sent. */
|
|
20
|
+
export declare function failRequest(required: {
|
|
21
|
+
response: Response;
|
|
22
|
+
error: unknown;
|
|
23
|
+
mappings: readonly SettingsErrorMapping[];
|
|
24
|
+
}, optional: SettingsHttpOptions): void;
|
|
25
|
+
export type ValueMutationOperation = "set" | "clear";
|
|
26
|
+
export declare const VALUE_MUTATION_REQUIRED: Record<ValueMutationOperation, string>;
|
|
27
|
+
/** The fields a value write needs, or null when one is missing or the scope is unknown. Pure, O(1). */
|
|
28
|
+
export declare function readValueMutation({ body, operation }: {
|
|
29
|
+
body: Record<string, unknown>;
|
|
30
|
+
operation: ValueMutationOperation;
|
|
31
|
+
}, _optional?: Record<string, never>): {
|
|
32
|
+
namespace: string;
|
|
33
|
+
key: string;
|
|
34
|
+
scope: SettingScope;
|
|
35
|
+
} | null;
|
|
36
|
+
//# sourceMappingURL=http-request.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { respondToSettingsError } from "./shared.js";
|
|
2
|
+
/**
|
|
3
|
+
* @file Request-level steps of the settings HTTP registrar (`http.ts`): the
|
|
4
|
+
* authentication gate, the failure envelope and value-write body parsing.
|
|
5
|
+
* Pure or response-only; no service, authorization or storage effects.
|
|
6
|
+
*/
|
|
7
|
+
export const SETTING_SCOPES = ["global", "workspace", "user"];
|
|
8
|
+
export function unauthenticated(res) {
|
|
9
|
+
res.status(401).json({ error: "unauthenticated", code: "UNAUTHENTICATED" });
|
|
10
|
+
}
|
|
11
|
+
/** Readiness/principal failures preserve the authentication boundary's existing 401 envelope, so both collapse to null. */
|
|
12
|
+
export async function resolvePrincipal(required, context) {
|
|
13
|
+
try {
|
|
14
|
+
await required.ready;
|
|
15
|
+
return await required.principalResolver(context);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Map a handler failure to its envelope, or end a response whose headers have already been sent. */
|
|
22
|
+
export function failRequest(required, optional) {
|
|
23
|
+
const { response, error, mappings } = required;
|
|
24
|
+
if (!response.headersSent) {
|
|
25
|
+
respondToSettingsError({ response, error, mappings });
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
// Reporting cannot prevent ending a response whose headers have already been sent.
|
|
29
|
+
try {
|
|
30
|
+
optional.onError?.({ operation: "settings-request", error });
|
|
31
|
+
}
|
|
32
|
+
catch { /* Host diagnostics are best-effort. */ }
|
|
33
|
+
finally {
|
|
34
|
+
response.end();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export const VALUE_MUTATION_REQUIRED = {
|
|
38
|
+
set: "namespace, key, scope (global|workspace|user), and valueJson are required",
|
|
39
|
+
clear: "namespace, key, and scope (global|workspace|user) are required",
|
|
40
|
+
};
|
|
41
|
+
/** The fields a value write needs, or null when one is missing or the scope is unknown. Pure, O(1). */
|
|
42
|
+
export function readValueMutation({ body, operation }, _optional = {}) {
|
|
43
|
+
const namespace = String(body.namespace ?? "");
|
|
44
|
+
const key = String(body.key ?? "");
|
|
45
|
+
const scope = body.scope;
|
|
46
|
+
const hasValue = operation === "clear" || "valueJson" in body;
|
|
47
|
+
const complete = namespace && key && SETTING_SCOPES.includes(scope) && hasValue;
|
|
48
|
+
return complete ? { namespace, key, scope } : null;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=http-request.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SettingsHttpRequired, SettingsHttpOptions } from "./contracts.js";
|
|
2
|
+
/** Mount all eight endpoints. The returned disposer closes active feeds and cancels their schedules.
|
|
3
|
+
* Injected policy and services preserve the settings HTTP response envelopes.
|
|
4
|
+
* O(1) mounting; read/mutation work is delegated. Feed resources are bounded per connection.
|
|
5
|
+
* @example registerSettingsRoutes({ app, workspaceId, ready, service, changeFeed, principalResolver, authorize, scheduler, routes, permissions });
|
|
6
|
+
*/
|
|
7
|
+
export declare function registerSettingsRoutes(required: SettingsHttpRequired, optional?: SettingsHttpOptions): {
|
|
8
|
+
dispose(): void;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=http.d.ts.map
|