@histoire/devframe 0.0.0-stage → 1.0.0-beta.2
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/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/client/attach.d.ts +15 -0
- package/dist/client/attach.d.ts.map +1 -0
- package/dist/client/attach.js +238 -0
- package/dist/client/diagnostics.d.ts +14 -0
- package/dist/client/diagnostics.d.ts.map +1 -0
- package/dist/client/diagnostics.js +31 -0
- package/dist/client/events.d.ts +11 -0
- package/dist/client/events.d.ts.map +1 -0
- package/dist/client/events.js +52 -0
- package/dist/client/highlight-operations.d.ts +14 -0
- package/dist/client/highlight-operations.d.ts.map +1 -0
- package/dist/client/highlight-operations.js +74 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +2 -0
- package/dist/client/inspection.d.ts +9 -0
- package/dist/client/inspection.d.ts.map +1 -0
- package/dist/client/inspection.js +32 -0
- package/dist/client/pairing-budget.d.ts +3 -0
- package/dist/client/pairing-budget.d.ts.map +1 -0
- package/dist/client/pairing-budget.js +36 -0
- package/dist/host/index.d.ts +35 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +86 -0
- package/dist/node/broker.d.ts +37 -0
- package/dist/node/broker.d.ts.map +1 -0
- package/dist/node/broker.js +349 -0
- package/dist/node/devframe.d.ts +64 -0
- package/dist/node/devframe.d.ts.map +1 -0
- package/dist/node/devframe.js +193 -0
- package/dist/node/grants.d.ts +24 -0
- package/dist/node/grants.d.ts.map +1 -0
- package/dist/node/grants.js +58 -0
- package/dist/node/index.d.ts +8 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +4 -0
- package/dist/node/mcp.d.ts +10 -0
- package/dist/node/mcp.d.ts.map +1 -0
- package/dist/node/mcp.js +150 -0
- package/dist/node/retirements.d.ts +8 -0
- package/dist/node/retirements.d.ts.map +1 -0
- package/dist/node/retirements.js +34 -0
- package/dist/node/rpc-budget.d.ts +6 -0
- package/dist/node/rpc-budget.d.ts.map +1 -0
- package/dist/node/rpc-budget.js +26 -0
- package/dist/node/types.d.ts +65 -0
- package/dist/node/types.d.ts.map +1 -0
- package/dist/node/types.js +1 -0
- package/dist/node/validation.d.ts +10 -0
- package/dist/node/validation.d.ts.map +1 -0
- package/dist/node/validation.js +50 -0
- package/dist/report/content.d.ts +33 -0
- package/dist/report/content.d.ts.map +1 -0
- package/dist/report/content.js +64 -0
- package/dist/report/index.d.ts +60 -0
- package/dist/report/index.d.ts.map +1 -0
- package/dist/report/index.js +78 -0
- package/dist/shared/diagnostics.d.ts +9 -0
- package/dist/shared/diagnostics.d.ts.map +1 -0
- package/dist/shared/diagnostics.js +20 -0
- package/dist/shared/events.d.ts +27 -0
- package/dist/shared/events.d.ts.map +1 -0
- package/dist/shared/events.js +49 -0
- package/dist/shared/services.d.ts +413 -0
- package/dist/shared/services.d.ts.map +1 -0
- package/dist/shared/services.js +113 -0
- package/package.json +53 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Track pairing RPCs by caller-owned connection until upstream settlement. */
|
|
2
|
+
const pending = new WeakMap();
|
|
3
|
+
/** Bound unresolved pairing attempts even when local timeouts settle first. */
|
|
4
|
+
export function startPairing(connection, operation, timeoutRetentionMs = 60_000) {
|
|
5
|
+
const count = pending.get(connection) ?? 0;
|
|
6
|
+
if (count >= 32)
|
|
7
|
+
throw new Error('Pairing capacity reached');
|
|
8
|
+
pending.set(connection, count + 1);
|
|
9
|
+
/** Release only after the actual Devframe RPC finishes. */
|
|
10
|
+
function release() {
|
|
11
|
+
const remaining = (pending.get(connection) ?? 1) - 1;
|
|
12
|
+
if (remaining)
|
|
13
|
+
pending.set(connection, remaining);
|
|
14
|
+
else
|
|
15
|
+
pending.delete(connection);
|
|
16
|
+
}
|
|
17
|
+
let result;
|
|
18
|
+
try {
|
|
19
|
+
result = Promise.resolve(operation());
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
release();
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
void result.then(release, (error) => {
|
|
26
|
+
// Devframe's client timeout may reject before its underlying birpc call settles.
|
|
27
|
+
if (error && typeof error === 'object' && 'kind' in error && error.kind === 'timeout') {
|
|
28
|
+
const timer = setTimeout(release, timeoutRetentionMs);
|
|
29
|
+
timer.unref?.();
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
release();
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { DevframeInstance, InitDevframeOptions } from 'devframe/initiate';
|
|
2
|
+
import type { Server } from 'node:http';
|
|
3
|
+
import type { DevframeBridgeOptions } from '../node/devframe.js';
|
|
4
|
+
import { createDevframeBridge } from '../node/devframe.js';
|
|
5
|
+
/** Options for an explicitly authenticated bridge on a caller-owned server. */
|
|
6
|
+
export interface DevframeHostOptions {
|
|
7
|
+
/** Existing HTTP or HTTPS server; the host retains ownership. */
|
|
8
|
+
server: Server;
|
|
9
|
+
/** Dedicated path with a leading and trailing slash. */
|
|
10
|
+
base: string;
|
|
11
|
+
/** Public origin of the host. */
|
|
12
|
+
origin: string;
|
|
13
|
+
/** Custom transport authentication must bind verified credentials to a principal. */
|
|
14
|
+
auth: Exclude<InitDevframeOptions['auth'], boolean | undefined>;
|
|
15
|
+
/** Host policy and verified peer identity for the finite broker. */
|
|
16
|
+
bridge: DevframeBridgeOptions;
|
|
17
|
+
/** Additional origins allowed to open the WebSocket. */
|
|
18
|
+
allowedOrigins?: readonly string[];
|
|
19
|
+
/** Caller cleanup for transport identity state. */
|
|
20
|
+
onPeerDisconnect?: InitDevframeOptions['onPeerDisconnect'];
|
|
21
|
+
}
|
|
22
|
+
/** Mounted host resources owned by the adapter. */
|
|
23
|
+
export interface DevframeHost {
|
|
24
|
+
/** Authenticated finite broker and its lifecycle. */
|
|
25
|
+
bridge: ReturnType<typeof createDevframeBridge>;
|
|
26
|
+
/** Normalized middleware base. */
|
|
27
|
+
base: string;
|
|
28
|
+
/** Connect middleware for the caller's server. */
|
|
29
|
+
nodeMiddleware: DevframeInstance['nodeMiddleware'];
|
|
30
|
+
/** Retire attachments and release owned registrations. */
|
|
31
|
+
dispose: () => Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/** Mount only the bridge; caller installs middleware and owns its server and credentials. */
|
|
34
|
+
export declare function mountDevframeHost(options: DevframeHostOptions): Promise<DevframeHost>;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAA;AAEhE,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAA;AAE1D,+EAA+E;AAC/E,MAAM,WAAW,mBAAmB;IAClC,iEAAiE;IACjE,MAAM,EAAE,MAAM,CAAA;IACd,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAA;IACZ,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,qFAAqF;IACrF,IAAI,EAAE,OAAO,CAAC,mBAAmB,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC,CAAA;IAC/D,oEAAoE;IACpE,MAAM,EAAE,qBAAqB,CAAA;IAC7B,wDAAwD;IACxD,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAClC,mDAAmD;IACnD,gBAAgB,CAAC,EAAE,mBAAmB,CAAC,kBAAkB,CAAC,CAAA;CAC3D;AAED,mDAAmD;AACnD,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,MAAM,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAA;IAC/C,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAA;IACZ,kDAAkD;IAClD,cAAc,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAA;IAClD,0DAA0D;IAC1D,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC7B;AAID,6FAA6F;AAC7F,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC,CAwE3F"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { initDevframe } from 'devframe/initiate';
|
|
2
|
+
import { createDevframeBridge } from '../node/devframe.js';
|
|
3
|
+
const mounts = new WeakMap();
|
|
4
|
+
/** Mount only the bridge; caller installs middleware and owns its server and credentials. */
|
|
5
|
+
export async function mountDevframeHost(options) {
|
|
6
|
+
if (!/^\/(?:[\w-]+\/)+$/.test(options.base))
|
|
7
|
+
throw new Error('Invalid host mount base');
|
|
8
|
+
if (!options.auth || typeof options.auth !== 'object' || typeof options.auth.authorize !== 'function' || typeof options.auth.onConnect !== 'function' || !Array.isArray(options.auth.rpcFunctions))
|
|
9
|
+
throw new Error('Transport authentication required');
|
|
10
|
+
let origin;
|
|
11
|
+
try {
|
|
12
|
+
origin = new URL(options.origin);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new Error('Invalid host origin');
|
|
16
|
+
}
|
|
17
|
+
if (!['http:', 'https:'].includes(origin.protocol) || origin.origin !== options.origin)
|
|
18
|
+
throw new Error('Invalid host origin');
|
|
19
|
+
const bases = mounts.get(options.server) ?? new Set();
|
|
20
|
+
if ([...bases].some(base => base.startsWith(options.base) || options.base.startsWith(base)))
|
|
21
|
+
throw new Error('Host mount already exists');
|
|
22
|
+
const bridge = createDevframeBridge(options.bridge);
|
|
23
|
+
bases.add(options.base);
|
|
24
|
+
mounts.set(options.server, bases);
|
|
25
|
+
let instance;
|
|
26
|
+
let disposed = false;
|
|
27
|
+
let closing;
|
|
28
|
+
/** Remove only this mount's registrations and transport. */
|
|
29
|
+
function dispose() {
|
|
30
|
+
if (closing)
|
|
31
|
+
return closing;
|
|
32
|
+
const completion = Promise.withResolvers();
|
|
33
|
+
closing = completion.promise;
|
|
34
|
+
disposed = true;
|
|
35
|
+
void (async () => {
|
|
36
|
+
try {
|
|
37
|
+
await bridge.dispose();
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
try {
|
|
41
|
+
await instance?.close();
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
bases.delete(options.base);
|
|
45
|
+
if (!bases.size && mounts.get(options.server) === bases)
|
|
46
|
+
mounts.delete(options.server);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
})().then(completion.resolve, completion.reject);
|
|
50
|
+
return closing;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
instance = initDevframe(bridge.definition, {
|
|
54
|
+
base: options.base,
|
|
55
|
+
server: options.server,
|
|
56
|
+
origin: options.origin,
|
|
57
|
+
auth: options.auth,
|
|
58
|
+
allowedOrigins: options.allowedOrigins,
|
|
59
|
+
distDir: false,
|
|
60
|
+
sse: false,
|
|
61
|
+
mcp: false,
|
|
62
|
+
register: false,
|
|
63
|
+
onPeerDisconnect(connection, meta) {
|
|
64
|
+
bridge.onPeerDisconnect(meta);
|
|
65
|
+
options.onPeerDisconnect?.(connection, meta);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
await instance.ready;
|
|
69
|
+
const initialized = instance;
|
|
70
|
+
return {
|
|
71
|
+
bridge,
|
|
72
|
+
base: initialized.base,
|
|
73
|
+
/** Mount unprefixed; after disposal, pass all requests to the host. */
|
|
74
|
+
nodeMiddleware: ((request, response, next) => {
|
|
75
|
+
if (disposed)
|
|
76
|
+
return next?.();
|
|
77
|
+
initialized.nodeMiddleware(request, response, next);
|
|
78
|
+
}),
|
|
79
|
+
dispose,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
await dispose();
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Authorization, BrokerOptions, Exposure, LiveAttachment, LiveIdentity, LivePeer } from './types.js';
|
|
2
|
+
/** Finite broker is inert until an explicitly authenticated host pairs a peer. */
|
|
3
|
+
export declare function createLiveBroker<P extends LivePeer>(options: BrokerOptions<P>): {
|
|
4
|
+
/** Mint a host-delivered one-use grant; no token is published in discovery. */
|
|
5
|
+
createGrant(input: {
|
|
6
|
+
principal: string;
|
|
7
|
+
sourceUrl: string;
|
|
8
|
+
scopes: readonly Exposure[];
|
|
9
|
+
}): {
|
|
10
|
+
token: string;
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
};
|
|
13
|
+
/** Verify connection identity and source authority before recording attachment. */
|
|
14
|
+
attach(peer: P, input: {
|
|
15
|
+
grant: string;
|
|
16
|
+
sourceUrl: string;
|
|
17
|
+
identity: LiveIdentity;
|
|
18
|
+
scopes: readonly Exposure[];
|
|
19
|
+
}): Promise<LiveAttachment>;
|
|
20
|
+
/** Renew only for the exact authenticated connection and still-live record. */
|
|
21
|
+
heartbeat(peer: P, attachmentId: string): Promise<void>;
|
|
22
|
+
/** Enumerate only currently authorized records, never grants or principals. */
|
|
23
|
+
list(principal: string, signal?: AbortSignal, authorizeScopes?: boolean): Promise<LiveAttachment[]>;
|
|
24
|
+
/** Route one finite inspection to its exact peer with a bounded lifetime. */
|
|
25
|
+
call(principal: string, attachmentId: string, name: string, input: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
26
|
+
/** Host calls immediately after an authority revision to retire affected work. */
|
|
27
|
+
invalidate(attachmentId?: string): void;
|
|
28
|
+
/** Host policy changes invalidate in-flight calls before replacing policy. */
|
|
29
|
+
setAuthorization(next: Authorization): void;
|
|
30
|
+
/** Host retires a changed document before accepting another lifetime. */
|
|
31
|
+
detach(peer: P, attachmentId: string): void;
|
|
32
|
+
/** Disconnect only attachments owned by this exact transport instance. */
|
|
33
|
+
disconnect(peer: P): void;
|
|
34
|
+
/** Drop owned grants, timers and work; caller owns its connection and server. */
|
|
35
|
+
dispose(): Promise<void>;
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=broker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../src/node/broker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,cAAc,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAiBhH,kFAAkF;AAClF,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,QAAQ,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;IAkE1E,+EAA+E;IAC/E,WAAW,QAAQ;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,SAAS,QAAQ,EAAE,CAAA;KAAE;;;;IAIxF,mFAAmF;IAC7E,MAAM,OAAO,CAAC,SAAS;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,YAAY,CAAC;QAAC,MAAM,EAAE,SAAS,QAAQ,EAAE,CAAA;KAAE;IA4CtH,+EAA+E;IACzE,SAAS,OAAO,CAAC,gBAAgB,MAAM;IAK7C,+EAA+E;IACzE,IAAI,YAAY,MAAM,WAAW,WAAW;IAkDlD,6EAA6E;IACvE,IAAI,YAAY,MAAM,gBAAgB,MAAM,QAAQ,MAAM,SAAS,OAAO,WAAW,WAAW;IA+DtG,kFAAkF;IAClF,UAAU,gBAAgB,MAAM;IAYhC,8EAA8E;IAC9E,gBAAgB,OAAO,aAAa;IAQpC,yEAAyE;IACzE,MAAM,OAAO,CAAC,gBAAgB,MAAM;IAIpC,0EAA0E;IAC1E,UAAU,OAAO,CAAC;IASlB,iFAAiF;IACjF,OAAO;EAcV"}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { assertServiceOperation, LIVE_SERVICES, serviceScopes } from '../shared/services.js';
|
|
3
|
+
import { canonicalSource, createGrants } from './grants.js';
|
|
4
|
+
import { createRetirementTracker } from './retirements.js';
|
|
5
|
+
import { bounded, isSourceAllowed, readAuthorityVersion, verifiedIdentity } from './validation.js';
|
|
6
|
+
/** Finite broker is inert until an explicitly authenticated host pairs a peer. */
|
|
7
|
+
export function createLiveBroker(options) {
|
|
8
|
+
const grants = createGrants();
|
|
9
|
+
const records = new Map();
|
|
10
|
+
const generations = new WeakMap();
|
|
11
|
+
const pairing = new Set();
|
|
12
|
+
const listing = new Set();
|
|
13
|
+
const leaseMs = options.leaseMs ?? 15_000;
|
|
14
|
+
const timeoutMs = options.timeoutMs ?? 5_000;
|
|
15
|
+
if (!Number.isFinite(leaseMs) || leaseMs < 1 || !Number.isFinite(timeoutMs) || timeoutMs < 1)
|
|
16
|
+
throw new Error('Invalid broker timeout');
|
|
17
|
+
const retirements = createRetirementTracker(timeoutMs);
|
|
18
|
+
let closing;
|
|
19
|
+
let closed = false;
|
|
20
|
+
let policyVersion = 0;
|
|
21
|
+
let registryVersion = 0;
|
|
22
|
+
let authorize = options.authorize;
|
|
23
|
+
/** Invalidate before notifying pending work to prevent reentrant resurrection. */
|
|
24
|
+
function retire(record, notify = true) {
|
|
25
|
+
if (records.get(record.attachment.attachmentId) !== record)
|
|
26
|
+
return;
|
|
27
|
+
records.delete(record.attachment.attachmentId);
|
|
28
|
+
registryVersion++;
|
|
29
|
+
clearTimeout(record.timer);
|
|
30
|
+
for (const pending of record.pending)
|
|
31
|
+
pending.abort(new Error('Session unavailable'));
|
|
32
|
+
record.pending.clear();
|
|
33
|
+
if (notify && record.peer.retire)
|
|
34
|
+
retirements.start(() => record.peer.retire());
|
|
35
|
+
try {
|
|
36
|
+
options.onRetire?.(record.peer, record.attachment.attachmentId);
|
|
37
|
+
}
|
|
38
|
+
catch { /* Host cleanup cannot revive the record. */ }
|
|
39
|
+
}
|
|
40
|
+
/** Never revive an expired lease, even when its timer has not yet fired. */
|
|
41
|
+
function active(record) {
|
|
42
|
+
if (closed || records.get(record.attachment.attachmentId) !== record)
|
|
43
|
+
return false;
|
|
44
|
+
if (record.expiresAt <= Date.now()) {
|
|
45
|
+
retire(record);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
/** Check policy version, connection ownership and lease after every async policy call. */
|
|
51
|
+
async function permitted(record, principal, capability, signal) {
|
|
52
|
+
if (!active(record))
|
|
53
|
+
return false;
|
|
54
|
+
const version = policyVersion;
|
|
55
|
+
const authority = readAuthorityVersion(options.authorityVersion);
|
|
56
|
+
let allowed = false;
|
|
57
|
+
try {
|
|
58
|
+
if (!await options.isCurrent(record.peer, structuredClone(record.identity)))
|
|
59
|
+
return false;
|
|
60
|
+
signal?.throwIfAborted();
|
|
61
|
+
if (version !== policyVersion || authority !== readAuthorityVersion(options.authorityVersion) || !active(record))
|
|
62
|
+
return false;
|
|
63
|
+
allowed = await authorize({ principal, attachment: { ...structuredClone(record.attachment), principal: record.principal }, identity: structuredClone(record.identity), capability }) === true;
|
|
64
|
+
signal?.throwIfAborted();
|
|
65
|
+
if (version !== policyVersion || authority !== readAuthorityVersion(options.authorityVersion) || !active(record) || !allowed)
|
|
66
|
+
return false;
|
|
67
|
+
allowed = await options.isCurrent(record.peer, structuredClone(record.identity)) === true;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
signal?.throwIfAborted();
|
|
73
|
+
return allowed && version === policyVersion && authority === readAuthorityVersion(options.authorityVersion) && active(record) && isSourceAllowed(options.allowSource, record.attachment.sourceUrl);
|
|
74
|
+
}
|
|
75
|
+
/** Update a lease only after checking the old lease and exact connection object. */
|
|
76
|
+
function renew(record) {
|
|
77
|
+
clearTimeout(record.timer);
|
|
78
|
+
record.expiresAt = Date.now() + leaseMs;
|
|
79
|
+
record.timer = setTimeout(() => retire(record), leaseMs);
|
|
80
|
+
record.timer.unref?.();
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
/** Mint a host-delivered one-use grant; no token is published in discovery. */
|
|
84
|
+
createGrant(input) {
|
|
85
|
+
if (closed || !isSourceAllowed(options.allowSource, canonicalSource(input.sourceUrl)))
|
|
86
|
+
throw new Error('Pairing unavailable');
|
|
87
|
+
return grants.create({ ...input, authorityVersion: readAuthorityVersion(options.authorityVersion) });
|
|
88
|
+
},
|
|
89
|
+
/** Verify connection identity and source authority before recording attachment. */
|
|
90
|
+
async attach(peer, input) {
|
|
91
|
+
if (closed || records.size + pairing.size >= 32)
|
|
92
|
+
throw new Error('Pairing unavailable');
|
|
93
|
+
input = bounded(input, 16 * 1024);
|
|
94
|
+
if (!input || typeof input !== 'object' || Object.keys(input).some(key => !['grant', 'sourceUrl', 'identity', 'scopes'].includes(key)))
|
|
95
|
+
throw new Error('Pairing unavailable');
|
|
96
|
+
const controller = new AbortController();
|
|
97
|
+
const pending = { peer, controller };
|
|
98
|
+
pairing.add(pending);
|
|
99
|
+
const timer = setTimeout(() => controller.abort(new Error('Pairing unavailable')), timeoutMs);
|
|
100
|
+
let off = () => { };
|
|
101
|
+
try {
|
|
102
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
103
|
+
const abort = () => reject(controller.signal.reason);
|
|
104
|
+
off = () => controller.signal.removeEventListener('abort', abort);
|
|
105
|
+
controller.signal.addEventListener('abort', abort, { once: true });
|
|
106
|
+
});
|
|
107
|
+
const work = async () => {
|
|
108
|
+
const sourceUrl = canonicalSource(input.sourceUrl);
|
|
109
|
+
if (!isSourceAllowed(options.allowSource, sourceUrl) || !Array.isArray(input.scopes) || input.scopes.length > 6 || new Set(input.scopes).size !== input.scopes.length || input.scopes.some(scope => !['inspection', 'events', 'diagnostics', 'components', 'accessibility', 'highlight'].includes(scope)))
|
|
110
|
+
throw new Error('Pairing unavailable');
|
|
111
|
+
const version = policyVersion;
|
|
112
|
+
const authority = readAuthorityVersion(options.authorityVersion);
|
|
113
|
+
const generation = generations.get(peer) ?? 0;
|
|
114
|
+
const principal = await options.identifyPeer(peer);
|
|
115
|
+
if (controller.signal.aborted || closed || version !== policyVersion || authority !== readAuthorityVersion(options.authorityVersion) || generation !== (generations.get(peer) ?? 0) || !principal)
|
|
116
|
+
throw new Error('Pairing unavailable');
|
|
117
|
+
const grant = grants.redeem(input.grant, principal, sourceUrl, authority);
|
|
118
|
+
const verified = await options.verifyAttachment(peer, bounded(input.identity, 16 * 1024), sourceUrl);
|
|
119
|
+
const identity = verifiedIdentity(verified.identity);
|
|
120
|
+
if (!Array.isArray(verified.scopes) || verified.scopes.length > 6 || new Set(verified.scopes).size !== verified.scopes.length || verified.scopes.some(scope => !['inspection', 'events', 'diagnostics', 'components', 'accessibility', 'highlight'].includes(scope)))
|
|
121
|
+
throw new Error('Pairing unavailable');
|
|
122
|
+
if (controller.signal.aborted || closed || version !== policyVersion || authority !== readAuthorityVersion(options.authorityVersion) || generation !== (generations.get(peer) ?? 0) || records.size >= 32 || !isSourceAllowed(options.allowSource, sourceUrl))
|
|
123
|
+
throw new Error('Pairing unavailable');
|
|
124
|
+
const { sourceId, epoch, revision, runtimeId, target } = identity;
|
|
125
|
+
const attachment = { attachmentId: randomUUID(), sourceUrl, owner: { sourceId, epoch, revision, runtimeId, target }, scopes: input.scopes.filter(scope => grant.scopes.includes(scope) && verified.scopes.includes(scope)) };
|
|
126
|
+
const record = { peer, principal, attachment, identity, expiresAt: 0, timer: undefined, pending: new Set() };
|
|
127
|
+
records.set(attachment.attachmentId, record);
|
|
128
|
+
renew(record);
|
|
129
|
+
return structuredClone(attachment);
|
|
130
|
+
};
|
|
131
|
+
return await Promise.race([work(), cancelled]);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
throw new Error('Pairing unavailable');
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
off();
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
pairing.delete(pending);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
/** Renew only for the exact authenticated connection and still-live record. */
|
|
143
|
+
async heartbeat(peer, attachmentId) {
|
|
144
|
+
const record = records.get(attachmentId);
|
|
145
|
+
if (!record || !active(record) || record.peer !== peer)
|
|
146
|
+
throw new Error('Session unavailable');
|
|
147
|
+
renew(record);
|
|
148
|
+
},
|
|
149
|
+
/** Enumerate only currently authorized records, never grants or principals. */
|
|
150
|
+
async list(principal, signal, authorizeScopes = false) {
|
|
151
|
+
if (closed)
|
|
152
|
+
throw new Error('Session unavailable');
|
|
153
|
+
if (listing.size >= 16)
|
|
154
|
+
throw new Error('Request capacity reached');
|
|
155
|
+
const controller = new AbortController();
|
|
156
|
+
const timer = setTimeout(() => controller.abort(new Error('Timed out')), timeoutMs);
|
|
157
|
+
const cancel = () => controller.abort(new Error('Cancelled'));
|
|
158
|
+
signal?.addEventListener('abort', cancel, { once: true });
|
|
159
|
+
listing.add(controller);
|
|
160
|
+
if (signal?.aborted)
|
|
161
|
+
cancel();
|
|
162
|
+
let off = () => { };
|
|
163
|
+
try {
|
|
164
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
165
|
+
const abort = () => reject(controller.signal.reason);
|
|
166
|
+
off = () => controller.signal.removeEventListener('abort', abort);
|
|
167
|
+
controller.signal.addEventListener('abort', abort, { once: true });
|
|
168
|
+
if (controller.signal.aborted)
|
|
169
|
+
abort();
|
|
170
|
+
});
|
|
171
|
+
const work = async () => {
|
|
172
|
+
const result = [];
|
|
173
|
+
const version = policyVersion;
|
|
174
|
+
const authority = readAuthorityVersion(options.authorityVersion);
|
|
175
|
+
const snapshotVersion = registryVersion;
|
|
176
|
+
for (const record of records.values()) {
|
|
177
|
+
if (!await permitted(record, principal, 'discover', controller.signal))
|
|
178
|
+
continue;
|
|
179
|
+
const attachment = structuredClone(record.attachment);
|
|
180
|
+
if (authorizeScopes) {
|
|
181
|
+
const scopes = [];
|
|
182
|
+
for (const scope of attachment.scopes) {
|
|
183
|
+
if (await permitted(record, principal, scope, controller.signal))
|
|
184
|
+
scopes.push(scope);
|
|
185
|
+
}
|
|
186
|
+
attachment.scopes = scopes;
|
|
187
|
+
}
|
|
188
|
+
result.push(attachment);
|
|
189
|
+
}
|
|
190
|
+
controller.signal.throwIfAborted();
|
|
191
|
+
if (closed || version !== policyVersion || authority !== readAuthorityVersion(options.authorityVersion) || snapshotVersion !== registryVersion)
|
|
192
|
+
return [];
|
|
193
|
+
return result.filter((item) => {
|
|
194
|
+
const record = records.get(item.attachmentId);
|
|
195
|
+
return !!record && active(record);
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
return await Promise.race([work(), cancelled]);
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
off();
|
|
202
|
+
clearTimeout(timer);
|
|
203
|
+
signal?.removeEventListener('abort', cancel);
|
|
204
|
+
listing.delete(controller);
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
/** Route one finite inspection to its exact peer with a bounded lifetime. */
|
|
208
|
+
async call(principal, attachmentId, name, input, signal) {
|
|
209
|
+
const record = records.get(attachmentId);
|
|
210
|
+
if (!record || !active(record) || !Object.hasOwn(LIVE_SERVICES, name))
|
|
211
|
+
throw new Error('Session unavailable');
|
|
212
|
+
const payload = bounded(input, 16 * 1024);
|
|
213
|
+
const scopes = serviceScopes(name, payload);
|
|
214
|
+
assertServiceOperation(name, payload);
|
|
215
|
+
const authority = readAuthorityVersion(options.authorityVersion);
|
|
216
|
+
if (record.pending.size >= 4)
|
|
217
|
+
throw new Error('Request capacity reached');
|
|
218
|
+
const controller = new AbortController();
|
|
219
|
+
const timer = setTimeout(() => controller.abort(new Error('Timed out')), timeoutMs);
|
|
220
|
+
const cancel = () => controller.abort(new Error('Cancelled'));
|
|
221
|
+
signal?.addEventListener('abort', cancel, { once: true });
|
|
222
|
+
record.pending.add(controller);
|
|
223
|
+
if (signal?.aborted)
|
|
224
|
+
cancel();
|
|
225
|
+
let off = () => { };
|
|
226
|
+
let highlight;
|
|
227
|
+
let accepted = false;
|
|
228
|
+
try {
|
|
229
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
230
|
+
const abort = () => reject(controller.signal.reason);
|
|
231
|
+
off = () => controller.signal.removeEventListener('abort', abort);
|
|
232
|
+
controller.signal.addEventListener('abort', abort, { once: true });
|
|
233
|
+
if (controller.signal.aborted)
|
|
234
|
+
abort();
|
|
235
|
+
});
|
|
236
|
+
const work = async () => {
|
|
237
|
+
if (!await permitted(record, principal, 'discover', controller.signal))
|
|
238
|
+
throw new Error('Session unavailable');
|
|
239
|
+
for (const scope of scopes) {
|
|
240
|
+
if (!record.attachment.scopes.includes(scope) || !await permitted(record, principal, scope, controller.signal))
|
|
241
|
+
throw new Error('Session unavailable');
|
|
242
|
+
}
|
|
243
|
+
controller.signal.throwIfAborted();
|
|
244
|
+
if (name === 'histoire_highlight_element' && !record.peer.callHighlight)
|
|
245
|
+
throw new Error('Session unavailable');
|
|
246
|
+
const value = name === 'histoire_highlight_element'
|
|
247
|
+
? (highlight = await record.peer.callHighlight(name, payload, controller.signal)).value
|
|
248
|
+
: await record.peer.call(name, payload, controller.signal);
|
|
249
|
+
controller.signal.throwIfAborted();
|
|
250
|
+
if (!await permitted(record, principal, 'discover', controller.signal))
|
|
251
|
+
throw new Error('Session unavailable');
|
|
252
|
+
for (const scope of scopes) {
|
|
253
|
+
if (!record.attachment.scopes.includes(scope) || !await permitted(record, principal, scope, controller.signal))
|
|
254
|
+
throw new Error('Session unavailable');
|
|
255
|
+
}
|
|
256
|
+
controller.signal.throwIfAborted();
|
|
257
|
+
if (authority !== readAuthorityVersion(options.authorityVersion))
|
|
258
|
+
throw new Error('Session unavailable');
|
|
259
|
+
return bounded(value, 60 * 1024);
|
|
260
|
+
};
|
|
261
|
+
const result = await Promise.race([work(), cancelled]);
|
|
262
|
+
accepted = true;
|
|
263
|
+
return result;
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
off();
|
|
267
|
+
clearTimeout(timer);
|
|
268
|
+
signal?.removeEventListener('abort', cancel);
|
|
269
|
+
record.pending.delete(controller);
|
|
270
|
+
if (highlight) {
|
|
271
|
+
let cleanupTimer;
|
|
272
|
+
try {
|
|
273
|
+
await Promise.race([highlight.finish(accepted), new Promise((_resolve, reject) => {
|
|
274
|
+
cleanupTimer = setTimeout(() => reject(new Error('Highlight cleanup timed out')), timeoutMs);
|
|
275
|
+
})]);
|
|
276
|
+
}
|
|
277
|
+
finally {
|
|
278
|
+
clearTimeout(cleanupTimer);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
/** Host calls immediately after an authority revision to retire affected work. */
|
|
284
|
+
invalidate(attachmentId) {
|
|
285
|
+
for (const entry of listing)
|
|
286
|
+
entry.abort(new Error('Session unavailable'));
|
|
287
|
+
if (attachmentId === undefined) {
|
|
288
|
+
grants.clear();
|
|
289
|
+
for (const entry of pairing)
|
|
290
|
+
entry.controller.abort(new Error('Pairing unavailable'));
|
|
291
|
+
for (const record of records.values())
|
|
292
|
+
retire(record);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
const record = records.get(attachmentId);
|
|
296
|
+
if (record)
|
|
297
|
+
retire(record);
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
/** Host policy changes invalidate in-flight calls before replacing policy. */
|
|
301
|
+
setAuthorization(next) {
|
|
302
|
+
policyVersion++;
|
|
303
|
+
grants.clear();
|
|
304
|
+
for (const entry of pairing)
|
|
305
|
+
entry.controller.abort(new Error('Pairing unavailable'));
|
|
306
|
+
for (const entry of listing)
|
|
307
|
+
entry.abort(new Error('Session unavailable'));
|
|
308
|
+
authorize = next;
|
|
309
|
+
for (const record of records.values())
|
|
310
|
+
retire(record);
|
|
311
|
+
},
|
|
312
|
+
/** Host retires a changed document before accepting another lifetime. */
|
|
313
|
+
detach(peer, attachmentId) {
|
|
314
|
+
const record = records.get(attachmentId);
|
|
315
|
+
if (record?.peer === peer)
|
|
316
|
+
retire(record, false);
|
|
317
|
+
},
|
|
318
|
+
/** Disconnect only attachments owned by this exact transport instance. */
|
|
319
|
+
disconnect(peer) {
|
|
320
|
+
generations.set(peer, (generations.get(peer) ?? 0) + 1);
|
|
321
|
+
for (const entry of pairing) {
|
|
322
|
+
if (entry.peer === peer)
|
|
323
|
+
entry.controller.abort(new Error('Pairing unavailable'));
|
|
324
|
+
}
|
|
325
|
+
for (const record of records.values()) {
|
|
326
|
+
if (record.peer === peer)
|
|
327
|
+
retire(record, false);
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
/** Drop owned grants, timers and work; caller owns its connection and server. */
|
|
331
|
+
dispose() {
|
|
332
|
+
if (closing)
|
|
333
|
+
return closing;
|
|
334
|
+
const completion = Promise.withResolvers();
|
|
335
|
+
closing = completion.promise;
|
|
336
|
+
closed = true;
|
|
337
|
+
policyVersion++;
|
|
338
|
+
grants.clear();
|
|
339
|
+
for (const entry of listing)
|
|
340
|
+
entry.abort(new Error('Session unavailable'));
|
|
341
|
+
for (const entry of pairing)
|
|
342
|
+
entry.controller.abort(new Error('Pairing unavailable'));
|
|
343
|
+
for (const record of records.values())
|
|
344
|
+
retire(record);
|
|
345
|
+
void retirements.drain().then(completion.resolve, completion.reject);
|
|
346
|
+
return closing;
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { DevframeNodeRpcSession, DevframeNodeRpcSessionMeta } from 'devframe/types';
|
|
2
|
+
import type { Authorization, Exposure, LiveIdentity, LivePeer } from './types.js';
|
|
3
|
+
interface Peer extends LivePeer {
|
|
4
|
+
session: DevframeNodeRpcSession;
|
|
5
|
+
method: string;
|
|
6
|
+
}
|
|
7
|
+
/** Host must supply verified credentials and document/source authorization. */
|
|
8
|
+
export interface DevframeBridgeOptions {
|
|
9
|
+
id?: string;
|
|
10
|
+
leaseMs?: number;
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
identifyPeer: (session: DevframeNodeRpcSession) => string | undefined | Promise<string | undefined>;
|
|
13
|
+
verifyAttachment: (session: DevframeNodeRpcSession, identity: LiveIdentity, sourceUrl: string) => {
|
|
14
|
+
identity: LiveIdentity;
|
|
15
|
+
scopes: readonly Exposure[];
|
|
16
|
+
} | Promise<{
|
|
17
|
+
identity: LiveIdentity;
|
|
18
|
+
scopes: readonly Exposure[];
|
|
19
|
+
}>;
|
|
20
|
+
isCurrent: (session: DevframeNodeRpcSession, identity: LiveIdentity) => boolean | Promise<boolean>;
|
|
21
|
+
authorityVersion: () => number;
|
|
22
|
+
allowSource: (url: string) => boolean;
|
|
23
|
+
authorize: Authorization;
|
|
24
|
+
}
|
|
25
|
+
/** Create an inert Devframe definition; host owns installation and disconnect hooks. */
|
|
26
|
+
export declare function createDevframeBridge(options: DevframeBridgeOptions): {
|
|
27
|
+
definition: import("devframe").DevframeDefinition;
|
|
28
|
+
broker: {
|
|
29
|
+
createGrant(input: {
|
|
30
|
+
principal: string;
|
|
31
|
+
sourceUrl: string;
|
|
32
|
+
scopes: readonly Exposure[];
|
|
33
|
+
}): {
|
|
34
|
+
token: string;
|
|
35
|
+
expiresAt: number;
|
|
36
|
+
};
|
|
37
|
+
attach(peer: Peer, input: {
|
|
38
|
+
grant: string;
|
|
39
|
+
sourceUrl: string;
|
|
40
|
+
identity: LiveIdentity;
|
|
41
|
+
scopes: readonly Exposure[];
|
|
42
|
+
}): Promise<import("./types.js").LiveAttachment>;
|
|
43
|
+
heartbeat(peer: Peer, attachmentId: string): Promise<void>;
|
|
44
|
+
list(principal: string, signal?: AbortSignal, authorizeScopes?: boolean): Promise<import("./types.js").LiveAttachment[]>;
|
|
45
|
+
call(principal: string, attachmentId: string, name: string, input: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
46
|
+
invalidate(attachmentId?: string): void;
|
|
47
|
+
setAuthorization(next: Authorization): void;
|
|
48
|
+
detach(peer: Peer, attachmentId: string): void;
|
|
49
|
+
disconnect(peer: Peer): void;
|
|
50
|
+
dispose(): Promise<void>;
|
|
51
|
+
};
|
|
52
|
+
/** Host-visible count of currently owned and pending transport attachments. */
|
|
53
|
+
getStatus(): {
|
|
54
|
+
attachments: number;
|
|
55
|
+
};
|
|
56
|
+
/** Call synchronously after host authority changes; omit ID for global changes. */
|
|
57
|
+
invalidate(attachmentId?: string): void;
|
|
58
|
+
/** Wire this hook to the caller-owned Devframe instance. */
|
|
59
|
+
onPeerDisconnect(meta: DevframeNodeRpcSessionMeta): void;
|
|
60
|
+
/** Release only adapter registrations and never close the host connection. */
|
|
61
|
+
dispose(): Promise<void>;
|
|
62
|
+
};
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=devframe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devframe.d.ts","sourceRoot":"","sources":["../../src/node/devframe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AACxF,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAMjF,UAAU,IAAK,SAAQ,QAAQ;IAAG,OAAO,EAAE,sBAAsB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE;AAEnF,+EAA+E;AAC/E,MAAM,WAAW,qBAAqB;IACpC,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,YAAY,EAAE,CAAC,OAAO,EAAE,sBAAsB,KAAK,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IACnG,gBAAgB,EAAE,CAAC,OAAO,EAAE,sBAAsB,EAAE,QAAQ,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,KAAK;QAAE,QAAQ,EAAE,YAAY,CAAC;QAAC,MAAM,EAAE,SAAS,QAAQ,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC;QAAE,QAAQ,EAAE,YAAY,CAAC;QAAC,MAAM,EAAE,SAAS,QAAQ,EAAE,CAAA;KAAE,CAAC,CAAA;IAC5N,SAAS,EAAE,CAAC,OAAO,EAAE,sBAAsB,EAAE,QAAQ,EAAE,YAAY,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClG,gBAAgB,EAAE,MAAM,MAAM,CAAA;IAC9B,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAA;IACrC,SAAS,EAAE,aAAa,CAAA;CACzB;AAED,wFAAwF;AACxF,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;IA+I/D,+EAA+E;;QACxD,WAAW;;IAClC,mFAAmF;8BACzD,MAAM;IAChC,4DAA4D;2BACrC,0BAA0B;IAWjD,8EAA8E;;EAcjF"}
|