tokensafe-backend-node 0.0.0-stage → 0.2.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/README.md +75 -2
- package/dist/chunk-DXJ53S4A.js +1181 -0
- package/dist/chunk-DXJ53S4A.js.map +1 -0
- package/dist/index.cjs +1242 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +317 -0
- package/dist/index.d.ts +317 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/dist/stdio.cjs +1211 -0
- package/dist/stdio.cjs.map +1 -0
- package/dist/stdio.d.cts +1 -0
- package/dist/stdio.d.ts +1 -0
- package/dist/stdio.js +52 -0
- package/dist/stdio.js.map +1 -0
- package/package.json +47 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import http, { IncomingMessage, ServerResponse, Server } from 'node:http';
|
|
2
|
+
import { Express } from 'express';
|
|
3
|
+
import { FastifyInstance } from 'fastify';
|
|
4
|
+
|
|
5
|
+
/** A live handshake session (spec §3). Absent ids mean stateless callers. */
|
|
6
|
+
declare class SessionRegistry {
|
|
7
|
+
private readonly sessions;
|
|
8
|
+
readonly ttlSeconds: number;
|
|
9
|
+
constructor(ttlSeconds?: number);
|
|
10
|
+
create(): string;
|
|
11
|
+
/** Unknown or expired ids are invalid; expiry is enforced on read. */
|
|
12
|
+
validate(id?: string | null): boolean;
|
|
13
|
+
delete(id?: string | null): boolean;
|
|
14
|
+
count(): number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare const PROTOCOL_VERSION = "1.1.0";
|
|
18
|
+
declare const OBSERVE_LEVELS: readonly ["L0", "L1", "L2", "L3", "L4"];
|
|
19
|
+
type ObserveLevel = (typeof OBSERVE_LEVELS)[number];
|
|
20
|
+
interface RouteInfo {
|
|
21
|
+
method: string;
|
|
22
|
+
path: string;
|
|
23
|
+
handlerName?: string;
|
|
24
|
+
middlewares?: string[];
|
|
25
|
+
}
|
|
26
|
+
interface TraceEntry {
|
|
27
|
+
method: string;
|
|
28
|
+
path: string;
|
|
29
|
+
route: string;
|
|
30
|
+
handler: string;
|
|
31
|
+
statusCode: number;
|
|
32
|
+
durationMs: number;
|
|
33
|
+
timestamp: string;
|
|
34
|
+
error?: string;
|
|
35
|
+
}
|
|
36
|
+
interface RuntimeCapabilities {
|
|
37
|
+
observe: {
|
|
38
|
+
levels: string[];
|
|
39
|
+
formats: string[];
|
|
40
|
+
};
|
|
41
|
+
understand: {
|
|
42
|
+
causality: boolean;
|
|
43
|
+
signalsGraph: boolean;
|
|
44
|
+
impactAnalysis: boolean;
|
|
45
|
+
};
|
|
46
|
+
mutate: {
|
|
47
|
+
occ: boolean;
|
|
48
|
+
syntaxGuard: boolean;
|
|
49
|
+
};
|
|
50
|
+
verify: {
|
|
51
|
+
multiMetric: boolean;
|
|
52
|
+
surfaceDiff: boolean;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
interface OumvRuntimeOptions {
|
|
56
|
+
name?: string;
|
|
57
|
+
version?: string;
|
|
58
|
+
workspaceDir?: string;
|
|
59
|
+
authToken?: string;
|
|
60
|
+
sessionTtlSeconds?: number;
|
|
61
|
+
/** Application handler used by `oumv/verify` (Express app, Fastify server, plain fn). */
|
|
62
|
+
handler?: http.RequestListener;
|
|
63
|
+
capabilities?: Partial<{
|
|
64
|
+
observe: boolean;
|
|
65
|
+
understand: boolean;
|
|
66
|
+
mutate: boolean;
|
|
67
|
+
verify: boolean;
|
|
68
|
+
}>;
|
|
69
|
+
transportPath?: string;
|
|
70
|
+
maxBodyBytes?: number;
|
|
71
|
+
}
|
|
72
|
+
interface VerifyScope {
|
|
73
|
+
checked: string[];
|
|
74
|
+
notExecuted: string[];
|
|
75
|
+
notSupported: string[];
|
|
76
|
+
}
|
|
77
|
+
declare class OumvRuntime {
|
|
78
|
+
readonly name: string;
|
|
79
|
+
readonly version: string;
|
|
80
|
+
readonly workspaceDir: string;
|
|
81
|
+
readonly transportPath: string;
|
|
82
|
+
readonly maxBodyBytes: number;
|
|
83
|
+
readonly sessions: SessionRegistry;
|
|
84
|
+
authToken: string;
|
|
85
|
+
private readonly routes;
|
|
86
|
+
private readonly traces;
|
|
87
|
+
private readonly startTime;
|
|
88
|
+
private readonly enabled;
|
|
89
|
+
private transports;
|
|
90
|
+
private handler?;
|
|
91
|
+
private dispatchServer?;
|
|
92
|
+
private dispatchPort;
|
|
93
|
+
constructor(options?: OumvRuntimeOptions);
|
|
94
|
+
registerRoute(method: string, routePath: string, handlerName?: string, middlewares?: string[]): void;
|
|
95
|
+
listRoutes(): RouteInfo[];
|
|
96
|
+
/** Records an executed request: the only source of L4/causality evidence. */
|
|
97
|
+
recordTrace(entry: Omit<TraceEntry, 'timestamp'> & {
|
|
98
|
+
timestamp?: string;
|
|
99
|
+
}): void;
|
|
100
|
+
recentTraces(): TraceEntry[];
|
|
101
|
+
setHandler(handler: http.RequestListener): void;
|
|
102
|
+
setTransports(transports: string[]): void;
|
|
103
|
+
handshake(params?: Record<string, any>): Record<string, unknown>;
|
|
104
|
+
observe(params?: Record<string, any>): Record<string, unknown>;
|
|
105
|
+
understand(params?: Record<string, any>): Record<string, unknown>;
|
|
106
|
+
getSource(params?: Record<string, any>): Record<string, unknown>;
|
|
107
|
+
private readSource;
|
|
108
|
+
applyEdit(params?: Record<string, any>): Record<string, unknown>;
|
|
109
|
+
verify(params?: Record<string, any>): Promise<Record<string, unknown>>;
|
|
110
|
+
private ensureDispatchServer;
|
|
111
|
+
/** Releases the loopback dispatcher (tests, shutdown hooks). */
|
|
112
|
+
dispose(): Promise<void>;
|
|
113
|
+
private findRoute;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Wire error codes (spec §5). The numeric table is the contract; names exist for
|
|
118
|
+
* readability only.
|
|
119
|
+
*/
|
|
120
|
+
declare const ERROR_CODES: {
|
|
121
|
+
readonly CONFLICT_DETECTED: -32001;
|
|
122
|
+
readonly SYNTAX_ERROR: -32002;
|
|
123
|
+
readonly SECURITY_VIOLATION: -32003;
|
|
124
|
+
readonly TARGET_NOT_FOUND: -32004;
|
|
125
|
+
readonly ACTION_NOT_FOUND: -32005;
|
|
126
|
+
readonly UNSUPPORTED_CAPABILITY: -32006;
|
|
127
|
+
readonly PERMISSION_DENIED: -32007;
|
|
128
|
+
readonly RUNTIME_UNAVAILABLE: -32008;
|
|
129
|
+
readonly RESOURCE_NOT_FOUND: -32009;
|
|
130
|
+
readonly VALIDATION_FAILED: -32011;
|
|
131
|
+
readonly PARSE_ERROR: -32700;
|
|
132
|
+
readonly INVALID_REQUEST: -32600;
|
|
133
|
+
readonly METHOD_NOT_FOUND: -32601;
|
|
134
|
+
readonly INVALID_PARAMS: -32602;
|
|
135
|
+
readonly INTERNAL_ERROR: -32603;
|
|
136
|
+
};
|
|
137
|
+
type OumvErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
138
|
+
declare class OumvError extends Error {
|
|
139
|
+
readonly code: OumvErrorCode;
|
|
140
|
+
readonly data?: Record<string, unknown>;
|
|
141
|
+
constructor(code: OumvErrorCode, message: string, data?: Record<string, unknown>);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Realpath jail (spec §4.6): every file operation is confined to the workspace,
|
|
146
|
+
* symlinks are resolved first, credential files are refused. Runs before any other
|
|
147
|
+
* guard, which is why the error is always `-32003`.
|
|
148
|
+
*/
|
|
149
|
+
declare function resolveWorkspacePath(inputPath: string, workspaceDir: string): string;
|
|
150
|
+
/** Splits `"path#symbol"` into its parts (spec §4.1 target grammar). */
|
|
151
|
+
declare function splitSourceTarget(target: string): {
|
|
152
|
+
path: string;
|
|
153
|
+
symbol?: string;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Workspace discovery registry (spec §9.2). One file per workspace, keyed by
|
|
158
|
+
* profile, so a Vite frontend and this backend never overwrite each other.
|
|
159
|
+
* Written 0600, outside the repository.
|
|
160
|
+
*/
|
|
161
|
+
interface RuntimeDescriptor {
|
|
162
|
+
type: 'backend';
|
|
163
|
+
name: string;
|
|
164
|
+
version: string;
|
|
165
|
+
url: string;
|
|
166
|
+
sessionToken?: string;
|
|
167
|
+
workspace?: string;
|
|
168
|
+
capabilities: {
|
|
169
|
+
observeLevels: string[];
|
|
170
|
+
transports: string[];
|
|
171
|
+
};
|
|
172
|
+
startedAt?: string;
|
|
173
|
+
}
|
|
174
|
+
declare function descriptorPath(workspaceDir: string, home?: string): string;
|
|
175
|
+
declare function writeDescriptor(workspaceDir: string, descriptor: RuntimeDescriptor, home?: string): string;
|
|
176
|
+
/** Removes only this profile's entry; a frontend entry keeps the file alive. */
|
|
177
|
+
declare function deleteDescriptor(workspaceDir: string, home?: string): void;
|
|
178
|
+
|
|
179
|
+
interface JsonRpcRequest {
|
|
180
|
+
jsonrpc?: string;
|
|
181
|
+
id?: string | number | null;
|
|
182
|
+
method?: string;
|
|
183
|
+
params?: Record<string, any>;
|
|
184
|
+
}
|
|
185
|
+
interface JsonRpcResponse {
|
|
186
|
+
jsonrpc: '2.0';
|
|
187
|
+
id: string | number | null;
|
|
188
|
+
result?: unknown;
|
|
189
|
+
error?: {
|
|
190
|
+
code: number;
|
|
191
|
+
message: string;
|
|
192
|
+
data?: Record<string, unknown>;
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
declare const success: (id: JsonRpcRequest["id"], result: unknown) => JsonRpcResponse;
|
|
196
|
+
declare const failure: (id: JsonRpcRequest["id"], code: number, message: string, data?: Record<string, unknown>) => JsonRpcResponse;
|
|
197
|
+
/**
|
|
198
|
+
* JSON-RPC dispatch for every method (spec §4/§5). Transport bindings call this;
|
|
199
|
+
* session validation happens once here so HTTP, SSE and WebSocket agree.
|
|
200
|
+
*/
|
|
201
|
+
declare class Dispatcher {
|
|
202
|
+
private readonly runtime;
|
|
203
|
+
constructor(runtime: OumvRuntime);
|
|
204
|
+
dispatch(request: JsonRpcRequest): Promise<JsonRpcResponse>;
|
|
205
|
+
}
|
|
206
|
+
/** Body guard shared by every transport: object or batch array, size-capped. */
|
|
207
|
+
declare function parsePayload(raw: string): JsonRpcRequest | JsonRpcRequest[];
|
|
208
|
+
/** Batch semantics (§2.2): ordered responses, notifications get no entry. */
|
|
209
|
+
declare function dispatchAll(dispatcher: Dispatcher, payload: JsonRpcRequest | JsonRpcRequest[]): Promise<JsonRpcResponse | JsonRpcResponse[]>;
|
|
210
|
+
|
|
211
|
+
interface HttpHandlerOptions {
|
|
212
|
+
runtime: OumvRuntime;
|
|
213
|
+
authToken?: string;
|
|
214
|
+
requireAuth?: boolean;
|
|
215
|
+
maxBodyBytes?: number;
|
|
216
|
+
}
|
|
217
|
+
/** True when the request carries the expected `X-OUMV-Session-Token`/Bearer token. */
|
|
218
|
+
declare function isAuthorized(req: IncomingMessage, token: string, requireAuth: boolean): boolean;
|
|
219
|
+
/**
|
|
220
|
+
* HTTP binding (spec §2.2): POST /__oumv/rpc, single object or batch, 200 for every
|
|
221
|
+
* well-formed request including JSON-RPC errors; 4xx only for transport failures.
|
|
222
|
+
*/
|
|
223
|
+
declare function createHttpHandler(options: HttpHandlerOptions): (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
|
|
224
|
+
|
|
225
|
+
interface SseOptions {
|
|
226
|
+
runtime: OumvRuntime;
|
|
227
|
+
authToken?: string;
|
|
228
|
+
requireAuth?: boolean;
|
|
229
|
+
heartbeatMs?: number;
|
|
230
|
+
pollMs?: number;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* SSE binding (spec §2.3): `GET <base>/__oumv/events` streams L4 traces with a
|
|
234
|
+
* monotonic `id`, so `Last-Event-ID` replays what the client missed.
|
|
235
|
+
*/
|
|
236
|
+
declare function createSseHandler(options: SseOptions): (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
|
|
237
|
+
|
|
238
|
+
interface WsOptions {
|
|
239
|
+
runtime: OumvRuntime;
|
|
240
|
+
authToken?: string;
|
|
241
|
+
requireAuth?: boolean;
|
|
242
|
+
}
|
|
243
|
+
interface WsAttachment {
|
|
244
|
+
close: () => Promise<void>;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* WebSocket binding (spec §2.4): JSON-RPC text frames on `<base>/__oumv/rpc`; the
|
|
248
|
+
* connection is bound to the sessionId returned by `oumv/handshake`, and closing it
|
|
249
|
+
* disconnects that session.
|
|
250
|
+
*/
|
|
251
|
+
declare function attachWebSocket(server: Server, options: WsOptions): WsAttachment;
|
|
252
|
+
|
|
253
|
+
interface StdioOptions {
|
|
254
|
+
input?: NodeJS.ReadableStream;
|
|
255
|
+
output?: NodeJS.WritableStream;
|
|
256
|
+
}
|
|
257
|
+
interface StdioAttachment {
|
|
258
|
+
close: () => void;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* §2.5 stdio binding: newline-delimited JSON-RPC, one message per line.
|
|
262
|
+
*
|
|
263
|
+
* stdout carries responses only (diagnostics belong on stderr), notifications get
|
|
264
|
+
* no reply, and lines are processed in order so a handshake is never overtaken by
|
|
265
|
+
* the observe that follows it. Batching is optional on stdio but accepted.
|
|
266
|
+
*/
|
|
267
|
+
declare function attachStdio(runtime: OumvRuntime, options?: StdioOptions): StdioAttachment;
|
|
268
|
+
|
|
269
|
+
interface ExpressAdapterOptions extends OumvRuntimeOptions {
|
|
270
|
+
/** Reuse an existing runtime (created before routes when tracing must wrap them). */
|
|
271
|
+
runtime?: OumvRuntime;
|
|
272
|
+
/** Workspace root for path jails and OCC (defaults to cwd). */
|
|
273
|
+
workspaceDir?: string;
|
|
274
|
+
/** Public JSON-RPC URL; when set, the discovery descriptor is written. */
|
|
275
|
+
publicUrl?: string;
|
|
276
|
+
requireAuth?: boolean;
|
|
277
|
+
descriptor?: boolean;
|
|
278
|
+
}
|
|
279
|
+
interface ExpressAdapter {
|
|
280
|
+
runtime: OumvRuntime;
|
|
281
|
+
/** Mounts the WebSocket upgrade on the http server that serves this app. */
|
|
282
|
+
attachWebSocket: (server: Server) => WsAttachment;
|
|
283
|
+
dispose: () => Promise<void>;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Mounts the OUMV runtime on an Express app:
|
|
287
|
+
*
|
|
288
|
+
* const oumv = mountTokensafe(app, { publicUrl: 'http://127.0.0.1:3000/__oumv/rpc' });
|
|
289
|
+
* const server = app.listen(3000); oumv.attachWebSocket(server);
|
|
290
|
+
*
|
|
291
|
+
* Must be called before the routes you want in L1, or those routes are registered
|
|
292
|
+
* lazily on the first observe (see `runtime.registerRoute`).
|
|
293
|
+
*/
|
|
294
|
+
declare function mountTokensafe(app: Express, options?: ExpressAdapterOptions): ExpressAdapter;
|
|
295
|
+
/** Records every request as an L4 trace. Mount before your routes. */
|
|
296
|
+
declare function tracingMiddleware(runtime: OumvRuntime): (req: IncomingMessage, res: any, next: () => void) => void;
|
|
297
|
+
|
|
298
|
+
interface FastifyAdapterOptions extends OumvRuntimeOptions {
|
|
299
|
+
/** Reuse an existing runtime (one instance per app, mounted before/after routes). */
|
|
300
|
+
runtime?: OumvRuntime;
|
|
301
|
+
publicUrl?: string;
|
|
302
|
+
requireAuth?: boolean;
|
|
303
|
+
descriptor?: boolean;
|
|
304
|
+
}
|
|
305
|
+
interface FastifyAdapter {
|
|
306
|
+
runtime: OumvRuntime;
|
|
307
|
+
attachWebSocket: () => WsAttachment;
|
|
308
|
+
dispose: () => Promise<void>;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Registers the OUMV runtime on a Fastify instance. Routes are captured with the
|
|
312
|
+
* `onRoute` hook (no private APIs), and verification dispatches through Fastify's
|
|
313
|
+
* own request pipeline.
|
|
314
|
+
*/
|
|
315
|
+
declare function registerTokensafe(fastify: FastifyInstance, options?: FastifyAdapterOptions): Promise<FastifyAdapter>;
|
|
316
|
+
|
|
317
|
+
export { Dispatcher, ERROR_CODES, type ExpressAdapter, type ExpressAdapterOptions, type FastifyAdapter, type FastifyAdapterOptions, OBSERVE_LEVELS, type ObserveLevel, OumvError, OumvRuntime, type OumvRuntimeOptions, PROTOCOL_VERSION, type RouteInfo, type RuntimeCapabilities, type RuntimeDescriptor, SessionRegistry, type StdioAttachment, type StdioOptions, type TraceEntry, type VerifyScope, attachStdio, attachWebSocket, createHttpHandler, createSseHandler, deleteDescriptor, descriptorPath, dispatchAll, failure, isAuthorized, mountTokensafe, parsePayload, registerTokensafe, resolveWorkspacePath, splitSourceTarget, success, tracingMiddleware, writeDescriptor };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Dispatcher,
|
|
3
|
+
ERROR_CODES,
|
|
4
|
+
OBSERVE_LEVELS,
|
|
5
|
+
OumvError,
|
|
6
|
+
OumvRuntime,
|
|
7
|
+
PROTOCOL_VERSION,
|
|
8
|
+
SessionRegistry,
|
|
9
|
+
attachStdio,
|
|
10
|
+
attachWebSocket,
|
|
11
|
+
createHttpHandler,
|
|
12
|
+
createSseHandler,
|
|
13
|
+
deleteDescriptor,
|
|
14
|
+
descriptorPath,
|
|
15
|
+
dispatchAll,
|
|
16
|
+
failure,
|
|
17
|
+
isAuthorized,
|
|
18
|
+
mountTokensafe,
|
|
19
|
+
parsePayload,
|
|
20
|
+
registerTokensafe,
|
|
21
|
+
resolveWorkspacePath,
|
|
22
|
+
splitSourceTarget,
|
|
23
|
+
success,
|
|
24
|
+
tracingMiddleware,
|
|
25
|
+
writeDescriptor
|
|
26
|
+
} from "./chunk-DXJ53S4A.js";
|
|
27
|
+
export {
|
|
28
|
+
Dispatcher,
|
|
29
|
+
ERROR_CODES,
|
|
30
|
+
OBSERVE_LEVELS,
|
|
31
|
+
OumvError,
|
|
32
|
+
OumvRuntime,
|
|
33
|
+
PROTOCOL_VERSION,
|
|
34
|
+
SessionRegistry,
|
|
35
|
+
attachStdio,
|
|
36
|
+
attachWebSocket,
|
|
37
|
+
createHttpHandler,
|
|
38
|
+
createSseHandler,
|
|
39
|
+
deleteDescriptor,
|
|
40
|
+
descriptorPath,
|
|
41
|
+
dispatchAll,
|
|
42
|
+
failure,
|
|
43
|
+
isAuthorized,
|
|
44
|
+
mountTokensafe,
|
|
45
|
+
parsePayload,
|
|
46
|
+
registerTokensafe,
|
|
47
|
+
resolveWorkspacePath,
|
|
48
|
+
splitSourceTarget,
|
|
49
|
+
success,
|
|
50
|
+
tracingMiddleware,
|
|
51
|
+
writeDescriptor
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|