@yu_robotics/remote-cli 1.6.247 → 1.6.252
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/dist/client/MessageHandler.d.ts +6 -2
- package/dist/client/MessageHandler.d.ts.map +1 -1
- package/dist/client/MessageHandler.js +103 -32
- package/dist/client/MessageHandler.js.map +1 -1
- package/dist/client/WebSocketClient.d.ts +3 -0
- package/dist/client/WebSocketClient.d.ts.map +1 -1
- package/dist/client/WebSocketClient.js +3 -1
- package/dist/client/WebSocketClient.js.map +1 -1
- package/dist/commands/start.d.ts +5 -0
- package/dist/commands/start.d.ts.map +1 -1
- package/dist/commands/start.js +37 -0
- package/dist/commands/start.js.map +1 -1
- package/dist/executor/ClaudePersistentExecutor.d.ts.map +1 -1
- package/dist/executor/ClaudePersistentExecutor.js +61 -9
- package/dist/executor/ClaudePersistentExecutor.js.map +1 -1
- package/dist/files/FileInbox.d.ts +1 -0
- package/dist/files/FileInbox.d.ts.map +1 -1
- package/dist/files/FileInbox.js +49 -4
- package/dist/files/FileInbox.js.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/maintenance/release-notes.json +1 -1
- package/dist/scheduling/ReviewedNativeCleanup.d.ts +1 -1
- package/dist/scheduling/ReviewedNativeCleanup.d.ts.map +1 -1
- package/dist/scheduling/ScheduleCleanup.d.ts +1 -0
- package/dist/scheduling/ScheduleCleanup.d.ts.map +1 -1
- package/dist/scheduling/ScheduleCleanup.js +2 -1
- package/dist/scheduling/ScheduleCleanup.js.map +1 -1
- package/dist/scheduling/SchedulePurgeVerification.js +4 -4
- package/dist/scheduling/SchedulePurgeVerification.js.map +1 -1
- package/dist/scheduling/ScheduleService.d.ts +22 -2
- package/dist/scheduling/ScheduleService.d.ts.map +1 -1
- package/dist/scheduling/ScheduleService.js +222 -62
- package/dist/scheduling/ScheduleService.js.map +1 -1
- package/dist/scheduling/ScheduleToolScope.d.ts.map +1 -1
- package/dist/scheduling/ScheduleToolScope.js +7 -0
- package/dist/scheduling/ScheduleToolScope.js.map +1 -1
- package/dist/scheduling/claude/ClaudeScheduleOwnership.d.ts +15 -3
- package/dist/scheduling/claude/ClaudeScheduleOwnership.d.ts.map +1 -1
- package/dist/scheduling/claude/ClaudeScheduleOwnership.js +111 -7
- package/dist/scheduling/claude/ClaudeScheduleOwnership.js.map +1 -1
- package/dist/scheduling/claude/ClaudeScheduleProcesses.d.ts +14 -0
- package/dist/scheduling/claude/ClaudeScheduleProcesses.d.ts.map +1 -0
- package/dist/scheduling/claude/ClaudeScheduleProcesses.js +86 -0
- package/dist/scheduling/claude/ClaudeScheduleProcesses.js.map +1 -0
- package/dist/scheduling/claude/ClaudeSessionSchedules.d.ts +13 -0
- package/dist/scheduling/claude/ClaudeSessionSchedules.d.ts.map +1 -1
- package/dist/scheduling/claude/ClaudeSessionSchedules.js +156 -40
- package/dist/scheduling/claude/ClaudeSessionSchedules.js.map +1 -1
- package/dist/scheduling/types.d.ts +4 -0
- package/dist/scheduling/types.d.ts.map +1 -1
- package/dist/scheduling/types.js +6 -1
- package/dist/scheduling/types.js.map +1 -1
- package/dist/service/SessionHostSupervisor.d.ts +63 -0
- package/dist/service/SessionHostSupervisor.d.ts.map +1 -0
- package/dist/service/SessionHostSupervisor.js +323 -0
- package/dist/service/SessionHostSupervisor.js.map +1 -0
- package/dist/sessionHost/HostClient.d.ts +49 -0
- package/dist/sessionHost/HostClient.d.ts.map +1 -0
- package/dist/sessionHost/HostClient.js +101 -0
- package/dist/sessionHost/HostClient.js.map +1 -0
- package/dist/sessionHost/HostProtocol.d.ts +65 -0
- package/dist/sessionHost/HostProtocol.d.ts.map +1 -0
- package/dist/sessionHost/HostProtocol.js +78 -0
- package/dist/sessionHost/HostProtocol.js.map +1 -0
- package/dist/sessionHost/HostServer.d.ts +52 -0
- package/dist/sessionHost/HostServer.d.ts.map +1 -0
- package/dist/sessionHost/HostServer.js +413 -0
- package/dist/sessionHost/HostServer.js.map +1 -0
- package/dist/sessionHost/HostStore.d.ts +43 -0
- package/dist/sessionHost/HostStore.d.ts.map +1 -0
- package/dist/sessionHost/HostStore.js +206 -0
- package/dist/sessionHost/HostStore.js.map +1 -0
- package/dist/sessionHost/RetainedFrontend.d.ts +63 -0
- package/dist/sessionHost/RetainedFrontend.d.ts.map +1 -0
- package/dist/sessionHost/RetainedFrontend.js +244 -0
- package/dist/sessionHost/RetainedFrontend.js.map +1 -0
- package/dist/sessionHost/RetainedHostLauncher.d.ts +25 -0
- package/dist/sessionHost/RetainedHostLauncher.d.ts.map +1 -0
- package/dist/sessionHost/RetainedHostLauncher.js +132 -0
- package/dist/sessionHost/RetainedHostLauncher.js.map +1 -0
- package/dist/sessionHost/RetainedRuntime.d.ts +62 -0
- package/dist/sessionHost/RetainedRuntime.d.ts.map +1 -0
- package/dist/sessionHost/RetainedRuntime.js +478 -0
- package/dist/sessionHost/RetainedRuntime.js.map +1 -0
- package/dist/sessionHost/RuntimeJournal.d.ts +47 -0
- package/dist/sessionHost/RuntimeJournal.d.ts.map +1 -0
- package/dist/sessionHost/RuntimeJournal.js +103 -0
- package/dist/sessionHost/RuntimeJournal.js.map +1 -0
- package/dist/sessionHost/entry.d.ts +13 -0
- package/dist/sessionHost/entry.d.ts.map +1 -0
- package/dist/sessionHost/entry.js +107 -0
- package/dist/sessionHost/entry.js.map +1 -0
- package/dist/update/AutomaticUpdater.d.ts +2 -1
- package/dist/update/AutomaticUpdater.d.ts.map +1 -1
- package/dist/update/AutomaticUpdater.js +26 -12
- package/dist/update/AutomaticUpdater.js.map +1 -1
- package/dist/update/PinnedHostBundle.d.ts +49 -0
- package/dist/update/PinnedHostBundle.d.ts.map +1 -0
- package/dist/update/PinnedHostBundle.js +309 -0
- package/dist/update/PinnedHostBundle.js.map +1 -0
- package/native/prebuilds/darwin-arm64/managed_native.node +0 -0
- package/native/prebuilds/darwin-arm64/manifest.json +1 -1
- package/native/prebuilds/darwin-x64/managed_native.node +0 -0
- package/native/prebuilds/darwin-x64/manifest.json +1 -1
- package/native/prebuilds/win32-arm64/managed_native.node +0 -0
- package/native/prebuilds/win32-arm64/manifest.json +1 -1
- package/native/prebuilds/win32-x64/managed_native.node +0 -0
- package/native/prebuilds/win32-x64/manifest.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type Json = null | boolean | number | string | Json[] | {
|
|
2
|
+
[key: string]: Json;
|
|
3
|
+
};
|
|
4
|
+
export declare const HOST_PROTOCOL = 1;
|
|
5
|
+
export declare const HOST_CAPABILITIES: readonly ["frontend-lease-v1", "durable-operation-v1", "output-replay-v1", "sequenced-requests-v1", "launcher-recovery-v1"];
|
|
6
|
+
export declare const MAX_REQUEST_BYTES: number;
|
|
7
|
+
export declare const MAX_PAYLOAD_BYTES: number;
|
|
8
|
+
export declare const MAX_RESPONSE_BYTES: number;
|
|
9
|
+
/** Private launcher metadata, never a model tool result or native resume proof. */
|
|
10
|
+
export interface HostAddress {
|
|
11
|
+
port: number;
|
|
12
|
+
hostId: string;
|
|
13
|
+
installationId: string;
|
|
14
|
+
}
|
|
15
|
+
export interface FrontendLease {
|
|
16
|
+
frontendId: string;
|
|
17
|
+
epoch: number;
|
|
18
|
+
token: string;
|
|
19
|
+
}
|
|
20
|
+
export interface OperationReceipt {
|
|
21
|
+
requestId: string;
|
|
22
|
+
status: 'pending' | 'completed' | 'failed' | 'unknown';
|
|
23
|
+
result?: Json;
|
|
24
|
+
error?: 'runtime_failed' | 'invalid_runtime_result' | 'host_restarted' | 'storage_unavailable';
|
|
25
|
+
}
|
|
26
|
+
export interface HostEvent extends OperationReceipt {
|
|
27
|
+
id: string;
|
|
28
|
+
sequence: number;
|
|
29
|
+
}
|
|
30
|
+
export interface HostRuntime {
|
|
31
|
+
/** Stable authority within this live host, not a PID or a restored-session claim. */
|
|
32
|
+
readonly authorityId: string;
|
|
33
|
+
/** The adapter validates native owner/scope; this protocol does not invent native APIs.
|
|
34
|
+
* Return at most MAX_PAYLOAD_BYTES of JSON. Larger native output requires an
|
|
35
|
+
* adapter-owned durable artifact reference, not truncation by this protocol.
|
|
36
|
+
*/
|
|
37
|
+
execute(operation: {
|
|
38
|
+
requestId: string;
|
|
39
|
+
method: string;
|
|
40
|
+
args: Json;
|
|
41
|
+
}): Promise<Json>;
|
|
42
|
+
/** Explicit host shutdown only. Must settle all accepted executions before resolving. */
|
|
43
|
+
stop(): Promise<void>;
|
|
44
|
+
/** Synchronous admission check before closing command transport. No native side effects. */
|
|
45
|
+
assertCanStop?(): void;
|
|
46
|
+
/** Read-only runtime snapshot; must not dispatch native work. */
|
|
47
|
+
inspect?(method: string, args: Json): Promise<Json>;
|
|
48
|
+
}
|
|
49
|
+
export declare class HostError extends Error {
|
|
50
|
+
readonly code: string;
|
|
51
|
+
constructor(code: string);
|
|
52
|
+
}
|
|
53
|
+
export declare function object(value: unknown): value is Record<string, unknown>;
|
|
54
|
+
export declare function fields(value: unknown, required: string[], optional?: string[]): asserts value is Record<string, unknown>;
|
|
55
|
+
export declare function identifier(value: unknown): asserts value is string;
|
|
56
|
+
export declare function integer(value: unknown): asserts value is number;
|
|
57
|
+
export declare function secret(value: unknown): asserts value is string;
|
|
58
|
+
/** Canonical JSON rejects lossy values and bounds nesting before hashing or dispatch. */
|
|
59
|
+
export declare function canonical(value: unknown, depth?: number): string;
|
|
60
|
+
export declare function payload(value: unknown): Json;
|
|
61
|
+
export declare function digest(value: string): string;
|
|
62
|
+
/** Domain-separated authentication; the installation secret never crosses IPC. */
|
|
63
|
+
export declare function sign(key: string, direction: 'request' | 'response', nonce: string, timestamp: string, data: string): string;
|
|
64
|
+
export declare function matchesSignature(actual: unknown, expected: string): boolean;
|
|
65
|
+
//# sourceMappingURL=HostProtocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HostProtocol.d.ts","sourceRoot":"","sources":["../../src/sessionHost/HostProtocol.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,IAAI,GAAG,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AAG,MAAM,CAE3H;AACD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAG3E"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HostError = exports.MAX_RESPONSE_BYTES = exports.MAX_PAYLOAD_BYTES = exports.MAX_REQUEST_BYTES = exports.HOST_CAPABILITIES = exports.HOST_PROTOCOL = void 0;
|
|
4
|
+
exports.object = object;
|
|
5
|
+
exports.fields = fields;
|
|
6
|
+
exports.identifier = identifier;
|
|
7
|
+
exports.integer = integer;
|
|
8
|
+
exports.secret = secret;
|
|
9
|
+
exports.canonical = canonical;
|
|
10
|
+
exports.payload = payload;
|
|
11
|
+
exports.digest = digest;
|
|
12
|
+
exports.sign = sign;
|
|
13
|
+
exports.matchesSignature = matchesSignature;
|
|
14
|
+
const crypto_1 = require("crypto");
|
|
15
|
+
exports.HOST_PROTOCOL = 1;
|
|
16
|
+
exports.HOST_CAPABILITIES = ['frontend-lease-v1', 'durable-operation-v1', 'output-replay-v1', 'sequenced-requests-v1', 'launcher-recovery-v1'];
|
|
17
|
+
exports.MAX_REQUEST_BYTES = 64 * 1024;
|
|
18
|
+
exports.MAX_PAYLOAD_BYTES = 16 * 1024;
|
|
19
|
+
exports.MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
|
20
|
+
class HostError extends Error {
|
|
21
|
+
code;
|
|
22
|
+
constructor(code) {
|
|
23
|
+
super(code);
|
|
24
|
+
this.code = code;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.HostError = HostError;
|
|
28
|
+
function object(value) {
|
|
29
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
function fields(value, required, optional = []) {
|
|
32
|
+
if (!object(value) || required.some(key => !Object.hasOwn(value, key))
|
|
33
|
+
|| Object.keys(value).some(key => !required.includes(key) && !optional.includes(key)))
|
|
34
|
+
throw new HostError('invalid_request');
|
|
35
|
+
}
|
|
36
|
+
function identifier(value) {
|
|
37
|
+
if (typeof value !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/.test(value))
|
|
38
|
+
throw new HostError('invalid_identifier');
|
|
39
|
+
}
|
|
40
|
+
function integer(value) {
|
|
41
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
42
|
+
throw new HostError('invalid_integer');
|
|
43
|
+
}
|
|
44
|
+
function secret(value) {
|
|
45
|
+
if (typeof value !== 'string' || !/^[a-f0-9]{64}$/.test(value))
|
|
46
|
+
throw new HostError('invalid_secret');
|
|
47
|
+
}
|
|
48
|
+
/** Canonical JSON rejects lossy values and bounds nesting before hashing or dispatch. */
|
|
49
|
+
function canonical(value, depth = 0) {
|
|
50
|
+
if (depth > 16)
|
|
51
|
+
throw new HostError('invalid_payload');
|
|
52
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
53
|
+
return JSON.stringify(value);
|
|
54
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
55
|
+
return JSON.stringify(value);
|
|
56
|
+
if (Array.isArray(value))
|
|
57
|
+
return '[' + value.map(item => canonical(item, depth + 1)).join(',') + ']';
|
|
58
|
+
if (object(value) && [Object.prototype, null].includes(Object.getPrototypeOf(value))) {
|
|
59
|
+
return '{' + Object.keys(value).sort().map(key => JSON.stringify(key) + ':' + canonical(value[key], depth + 1)).join(',') + '}';
|
|
60
|
+
}
|
|
61
|
+
throw new HostError('invalid_payload');
|
|
62
|
+
}
|
|
63
|
+
function payload(value) {
|
|
64
|
+
const encoded = canonical(value);
|
|
65
|
+
if (Buffer.byteLength(encoded) > exports.MAX_PAYLOAD_BYTES)
|
|
66
|
+
throw new HostError('payload_too_large');
|
|
67
|
+
return JSON.parse(encoded);
|
|
68
|
+
}
|
|
69
|
+
function digest(value) { return (0, crypto_1.createHash)('sha256').update(value).digest('hex'); }
|
|
70
|
+
/** Domain-separated authentication; the installation secret never crosses IPC. */
|
|
71
|
+
function sign(key, direction, nonce, timestamp, data) {
|
|
72
|
+
return (0, crypto_1.createHmac)('sha256', key).update([direction, nonce, timestamp, data].join('\n')).digest('hex');
|
|
73
|
+
}
|
|
74
|
+
function matchesSignature(actual, expected) {
|
|
75
|
+
return typeof actual === 'string' && /^[a-f0-9]{64}$/.test(actual)
|
|
76
|
+
&& (0, crypto_1.timingSafeEqual)(Buffer.from(actual, 'hex'), Buffer.from(expected, 'hex'));
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=HostProtocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HostProtocol.js","sourceRoot":"","sources":["../../src/sessionHost/HostProtocol.ts"],"names":[],"mappings":";;;AAsCA,wBAEC;AACD,wBAGC;AACD,gCAEC;AACD,0BAEC;AACD,wBAEC;AAED,8BASC;AACD,0BAIC;AACD,wBAA0G;AAE1G,oBAEC;AACD,4CAGC;AA9ED,mCAAiE;AAGpD,QAAA,aAAa,GAAG,CAAC,CAAC;AAClB,QAAA,iBAAiB,GAAG,CAAC,mBAAmB,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,sBAAsB,CAAU,CAAC;AAChJ,QAAA,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AAC9B,QAAA,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AAC9B,QAAA,kBAAkB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AA4BlD,MAAa,SAAU,SAAQ,KAAK;IACb;IAArB,YAAqB,IAAY;QAAI,KAAK,CAAC,IAAI,CAAC,CAAC;QAA5B,SAAI,GAAJ,IAAI,CAAQ;IAAiB,CAAC;CACpD;AAFD,8BAEC;AACD,SAAgB,MAAM,CAAC,KAAc;IACnC,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KA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C,CAAC;AACD,SAAgB,MAAM,CAAC,KAAa,IAAY,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAC1G,kFAAkF;AAClF,SAAgB,IAAI,CAAC,GAAW,EAAE,SAAiC,EAAE,KAAa,EAAE,SAAiB,EAAE,IAAY;IACjH,OAAO,IAAA,mBAAU,EAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACxG,CAAC;AACD,SAAgB,gBAAgB,CAAC,MAAe,EAAE,QAAgB;IAChE,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC;WAC7D,IAAA,wBAAe,EAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;AACjF,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type HostLimits } from './HostStore';
|
|
2
|
+
import { type HostAddress, type HostRuntime } from './HostProtocol';
|
|
3
|
+
export interface HostOptions {
|
|
4
|
+
/** Private directory. The launcher must provision a private ACL on Windows. */
|
|
5
|
+
directory: string;
|
|
6
|
+
installationId: string;
|
|
7
|
+
/** Random 32-byte hex credential from the trusted launcher; never persisted or sent. */
|
|
8
|
+
secret: string;
|
|
9
|
+
launcherSecret?: string;
|
|
10
|
+
runtime: HostRuntime;
|
|
11
|
+
limits?: HostLimits;
|
|
12
|
+
requestTimeoutMs?: number;
|
|
13
|
+
ticketLifetimeMs?: number;
|
|
14
|
+
maxSockets?: number;
|
|
15
|
+
}
|
|
16
|
+
/** Retains execution ownership independently of every HTTP frontend connection.
|
|
17
|
+
* The trusted launcher passes address/credential out of band. Losing an HTTP
|
|
18
|
+
* connection does not lose a lease: reconnect with the same lease or a prepared
|
|
19
|
+
* successor ticket. No unauthenticated discovery or automatic lease stealing.
|
|
20
|
+
* Releasing the writer lock proves neither native child exit nor safe restore;
|
|
21
|
+
* the injected runtime adapter must independently qualify that ownership.
|
|
22
|
+
*/
|
|
23
|
+
export declare class HostServer {
|
|
24
|
+
private readonly options;
|
|
25
|
+
private readonly store;
|
|
26
|
+
private readonly hostId;
|
|
27
|
+
private readonly server;
|
|
28
|
+
private readonly sockets;
|
|
29
|
+
private readonly executing;
|
|
30
|
+
private readonly nonces;
|
|
31
|
+
private lease?;
|
|
32
|
+
private ticket?;
|
|
33
|
+
private candidate?;
|
|
34
|
+
private activated?;
|
|
35
|
+
private observed;
|
|
36
|
+
private started;
|
|
37
|
+
private closing;
|
|
38
|
+
private stopped;
|
|
39
|
+
private stopping?;
|
|
40
|
+
private readonly timeout;
|
|
41
|
+
private readonly lifetime;
|
|
42
|
+
private readonly maxSockets;
|
|
43
|
+
constructor(options: HostOptions);
|
|
44
|
+
listen(): Promise<HostAddress>;
|
|
45
|
+
/** Local owner API only; frontend detach and socket close never call this. */
|
|
46
|
+
stop(timeoutMs?: number): Promise<void>;
|
|
47
|
+
private receive;
|
|
48
|
+
private route;
|
|
49
|
+
private receipt;
|
|
50
|
+
private requireLease;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=HostServer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HostServer.d.ts","sourceRoot":"","sources":["../../src/sessionHost/HostServer.ts"],"names":[],"mappings":"AAMA,OAAO,EAAa,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAC2E,KAAK,WAAW,EAAE,KAAK,WAAW,EAC3F,MAAM,gBAAgB,CAAC;AAIhD,MAAM,WAAW,WAAW;IAC1B,+EAA+E;IAC/E,SAA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AC,IAAI,CAAC;YAyBvB,OAAO;IA+CrB,OAAO,CAAC,KAAK;IA8Ib,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,YAAY;CAIrB"}
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.HostServer = void 0;
|
|
7
|
+
const http_1 = __importDefault(require("http"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const OwnershipRuntime_1 = require("../delegation/OwnershipRuntime");
|
|
10
|
+
const crypto_1 = require("crypto");
|
|
11
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
12
|
+
const HostStore_1 = require("./HostStore");
|
|
13
|
+
const HostProtocol_1 = require("./HostProtocol");
|
|
14
|
+
/** Retains execution ownership independently of every HTTP frontend connection.
|
|
15
|
+
* The trusted launcher passes address/credential out of band. Losing an HTTP
|
|
16
|
+
* connection does not lose a lease: reconnect with the same lease or a prepared
|
|
17
|
+
* successor ticket. No unauthenticated discovery or automatic lease stealing.
|
|
18
|
+
* Releasing the writer lock proves neither native child exit nor safe restore;
|
|
19
|
+
* the injected runtime adapter must independently qualify that ownership.
|
|
20
|
+
*/
|
|
21
|
+
class HostServer {
|
|
22
|
+
options;
|
|
23
|
+
store;
|
|
24
|
+
hostId = (0, crypto_1.randomUUID)();
|
|
25
|
+
server;
|
|
26
|
+
sockets = new Set();
|
|
27
|
+
executing = new Set();
|
|
28
|
+
nonces = new Map();
|
|
29
|
+
lease;
|
|
30
|
+
ticket;
|
|
31
|
+
candidate;
|
|
32
|
+
activated;
|
|
33
|
+
observed = 0;
|
|
34
|
+
started = false;
|
|
35
|
+
closing = false;
|
|
36
|
+
stopped = false;
|
|
37
|
+
stopping;
|
|
38
|
+
timeout;
|
|
39
|
+
lifetime;
|
|
40
|
+
maxSockets;
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.options = options;
|
|
43
|
+
(0, HostProtocol_1.identifier)(options.installationId);
|
|
44
|
+
(0, HostProtocol_1.identifier)(options.runtime.authorityId);
|
|
45
|
+
(0, HostProtocol_1.secret)(options.secret);
|
|
46
|
+
if (options.launcherSecret !== undefined) {
|
|
47
|
+
(0, HostProtocol_1.secret)(options.launcherSecret);
|
|
48
|
+
if (options.launcherSecret === options.secret)
|
|
49
|
+
throw new HostProtocol_1.HostError('separate_launcher_credential_required');
|
|
50
|
+
}
|
|
51
|
+
this.timeout = options.requestTimeoutMs ?? 5000;
|
|
52
|
+
this.lifetime = options.ticketLifetimeMs ?? 30_000;
|
|
53
|
+
this.maxSockets = options.maxSockets ?? 16;
|
|
54
|
+
for (const [n, max] of [[this.timeout, 30_000], [this.lifetime, 120_000], [this.maxSockets, 128]]) {
|
|
55
|
+
if (!Number.isInteger(n) || n < 1 || n > max)
|
|
56
|
+
throw new HostProtocol_1.HostError('invalid_limit');
|
|
57
|
+
}
|
|
58
|
+
this.store = new HostStore_1.HostStore(options.directory, (0, HostProtocol_1.digest)(options.installationId + '\0' + options.secret), options.limits);
|
|
59
|
+
this.server = http_1.default.createServer((request, response) => { void this.receive(request, response); });
|
|
60
|
+
this.server.headersTimeout = this.timeout;
|
|
61
|
+
this.server.requestTimeout = this.timeout;
|
|
62
|
+
this.server.keepAliveTimeout = 1;
|
|
63
|
+
this.server.maxHeadersCount = 16;
|
|
64
|
+
this.server.on('connection', socket => {
|
|
65
|
+
if (this.closing || this.sockets.size >= this.maxSockets) {
|
|
66
|
+
socket.destroy();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this.sockets.add(socket);
|
|
70
|
+
const deadline = setTimeout(() => socket.destroy(), this.timeout);
|
|
71
|
+
socket.setTimeout(this.timeout, () => socket.destroy());
|
|
72
|
+
socket.once('close', () => { clearTimeout(deadline); this.sockets.delete(socket); });
|
|
73
|
+
});
|
|
74
|
+
this.server.on('clientError', (_error, socket) => socket.destroy());
|
|
75
|
+
}
|
|
76
|
+
async listen() {
|
|
77
|
+
if (this.started || this.closing)
|
|
78
|
+
throw new HostProtocol_1.HostError('host_already_started');
|
|
79
|
+
this.started = true;
|
|
80
|
+
try {
|
|
81
|
+
await new Promise((resolve, reject) => {
|
|
82
|
+
this.server.once('error', reject);
|
|
83
|
+
this.server.listen(0, '127.0.0.1', () => { this.server.removeListener('error', reject); resolve(); });
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
this.store.close();
|
|
88
|
+
this.closing = true;
|
|
89
|
+
throw new HostProtocol_1.HostError('host_listen_failed');
|
|
90
|
+
}
|
|
91
|
+
return { port: this.server.address().port, hostId: this.hostId, installationId: this.options.installationId };
|
|
92
|
+
}
|
|
93
|
+
/** Local owner API only; frontend detach and socket close never call this. */
|
|
94
|
+
stop(timeoutMs = 5000) {
|
|
95
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 30_000)
|
|
96
|
+
return Promise.reject(new HostProtocol_1.HostError('invalid_limit'));
|
|
97
|
+
if (this.stopped)
|
|
98
|
+
return Promise.resolve();
|
|
99
|
+
if (this.stopping)
|
|
100
|
+
return this.stopping;
|
|
101
|
+
try {
|
|
102
|
+
this.options.runtime.assertCanStop?.();
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
return Promise.reject(error);
|
|
106
|
+
}
|
|
107
|
+
this.closing = true;
|
|
108
|
+
for (const socket of this.sockets)
|
|
109
|
+
socket.destroy();
|
|
110
|
+
const closed = new Promise(resolve => this.server.close(() => resolve()));
|
|
111
|
+
const work = (async () => {
|
|
112
|
+
let timer;
|
|
113
|
+
try {
|
|
114
|
+
await Promise.race([
|
|
115
|
+
(async () => { await this.options.runtime.stop(); await Promise.all([...this.executing]); await closed; })(),
|
|
116
|
+
new Promise((_, reject) => { timer = setTimeout(() => reject(new HostProtocol_1.HostError('host_stop_unconfirmed')), timeoutMs); }),
|
|
117
|
+
]);
|
|
118
|
+
this.store.close();
|
|
119
|
+
this.stopped = true;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
if (timer)
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
}
|
|
125
|
+
})();
|
|
126
|
+
this.stopping = work;
|
|
127
|
+
// Failed stop retains the writer lock. The owner may explicitly retry after
|
|
128
|
+
// its runtime settles; no new host may assume native retirement from a timeout.
|
|
129
|
+
void work.finally(() => { if (!this.stopped)
|
|
130
|
+
this.stopping = undefined; }).catch(() => { });
|
|
131
|
+
return work;
|
|
132
|
+
}
|
|
133
|
+
async receive(request, response) {
|
|
134
|
+
let authenticated;
|
|
135
|
+
const answer = (status, value) => {
|
|
136
|
+
const data = JSON.stringify(value);
|
|
137
|
+
if (!response.destroyed) {
|
|
138
|
+
response.writeHead(status, { 'Content-Type': 'application/json', Connection: 'close',
|
|
139
|
+
...(authenticated ? { 'x-session-host-signature': (0, HostProtocol_1.sign)(authenticated.key, 'response', authenticated.nonce, authenticated.timestamp, data) } : {}) });
|
|
140
|
+
response.end(data);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const deadline = setTimeout(() => request.destroy(), this.timeout);
|
|
144
|
+
try {
|
|
145
|
+
if (this.closing)
|
|
146
|
+
throw new HostProtocol_1.HostError('host_closing');
|
|
147
|
+
if (request.method !== 'POST' || request.url !== '/v1' || request.headers['content-type'] !== 'application/json')
|
|
148
|
+
throw new HostProtocol_1.HostError('invalid_transport');
|
|
149
|
+
const nonce = request.headers['x-session-host-nonce'];
|
|
150
|
+
const timestamp = request.headers['x-session-host-time'];
|
|
151
|
+
if (typeof nonce !== 'string' || !/^[a-f0-9]{64}$/.test(nonce) || typeof timestamp !== 'string'
|
|
152
|
+
|| !/^\d{1,16}$/.test(timestamp) || Math.abs(Date.now() - Number(timestamp)) > 30_000)
|
|
153
|
+
throw new HostProtocol_1.HostError('unauthorized');
|
|
154
|
+
const chunks = [];
|
|
155
|
+
let size = 0;
|
|
156
|
+
for await (const chunk of request) {
|
|
157
|
+
size += chunk.length;
|
|
158
|
+
if (size > HostProtocol_1.MAX_REQUEST_BYTES)
|
|
159
|
+
throw new HostProtocol_1.HostError('request_too_large');
|
|
160
|
+
chunks.push(Buffer.from(chunk));
|
|
161
|
+
}
|
|
162
|
+
const data = Buffer.concat(chunks).toString('utf8');
|
|
163
|
+
const launcher = !!this.options.launcherSecret && (0, HostProtocol_1.matchesSignature)(request.headers['x-session-host-signature'], (0, HostProtocol_1.sign)(this.options.launcherSecret, 'request', nonce, timestamp, data));
|
|
164
|
+
const key = launcher ? this.options.launcherSecret : this.options.secret;
|
|
165
|
+
if (!(0, HostProtocol_1.matchesSignature)(request.headers['x-session-host-signature'], (0, HostProtocol_1.sign)(key, 'request', nonce, timestamp, data)))
|
|
166
|
+
throw new HostProtocol_1.HostError('unauthorized');
|
|
167
|
+
authenticated = { nonce, timestamp, key, launcher };
|
|
168
|
+
const now = perf_hooks_1.performance.now();
|
|
169
|
+
for (const [key, expiry] of this.nonces)
|
|
170
|
+
if (expiry <= now)
|
|
171
|
+
this.nonces.delete(key);
|
|
172
|
+
if (this.nonces.has(nonce))
|
|
173
|
+
throw new HostProtocol_1.HostError('replayed_request');
|
|
174
|
+
if (this.nonces.size >= 1024)
|
|
175
|
+
throw new HostProtocol_1.HostError('authentication_capacity');
|
|
176
|
+
this.nonces.set(nonce, now + 65_000);
|
|
177
|
+
const envelope = JSON.parse(data);
|
|
178
|
+
(0, HostProtocol_1.fields)(envelope, ['protocol', 'installationId', 'hostId', 'method', 'body']);
|
|
179
|
+
if (envelope.protocol !== HostProtocol_1.HOST_PROTOCOL || envelope.installationId !== this.options.installationId || envelope.hostId !== this.hostId)
|
|
180
|
+
throw new HostProtocol_1.HostError('host_identity_mismatch');
|
|
181
|
+
if (typeof envelope.method !== 'string')
|
|
182
|
+
throw new HostProtocol_1.HostError('invalid_request');
|
|
183
|
+
answer(200, { ok: true, protocol: HostProtocol_1.HOST_PROTOCOL, hostId: this.hostId, value: await this.route(envelope.method, envelope.body, authenticated.launcher) });
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
// Neither native exception strings nor credentials/addresses cross this boundary.
|
|
187
|
+
answer(error instanceof HostProtocol_1.HostError && error.code === 'unauthorized' ? 401 : 400, { ok: false, code: error instanceof HostProtocol_1.HostError ? error.code : 'invalid_request' });
|
|
188
|
+
request.resume();
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
clearTimeout(deadline);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
route(method, body, launcher = false) {
|
|
195
|
+
if (this.closing)
|
|
196
|
+
throw new HostProtocol_1.HostError('host_closing');
|
|
197
|
+
if (method === 'hello') {
|
|
198
|
+
(0, HostProtocol_1.fields)(body, []);
|
|
199
|
+
return { capabilities: HostProtocol_1.HOST_CAPABILITIES, authorityId: this.options.runtime.authorityId,
|
|
200
|
+
acceptedThrough: this.store.snapshot().acceptedThrough, retiredThrough: this.store.snapshot().retiredThrough, epoch: this.store.snapshot().epoch, acknowledged: this.store.snapshot().acknowledged,
|
|
201
|
+
active: !!this.lease, storageHealthy: !this.store.failed };
|
|
202
|
+
}
|
|
203
|
+
if (this.store.failed)
|
|
204
|
+
throw new HostProtocol_1.HostError('storage_unavailable');
|
|
205
|
+
if (method === 'recoverFrontend') {
|
|
206
|
+
(0, HostProtocol_1.fields)(body, ['frontendId', 'epoch']);
|
|
207
|
+
(0, HostProtocol_1.identifier)(body.frontendId);
|
|
208
|
+
(0, HostProtocol_1.integer)(body.epoch);
|
|
209
|
+
if (!launcher)
|
|
210
|
+
throw new HostProtocol_1.HostError('launcher_authority_required');
|
|
211
|
+
if (body.epoch !== this.store.snapshot().epoch)
|
|
212
|
+
throw new HostProtocol_1.HostError('stale_epoch');
|
|
213
|
+
const proof = (0, OwnershipRuntime_1.kernelGate)(path_1.default.join(this.options.directory, 'frontend.lock'));
|
|
214
|
+
if (!proof)
|
|
215
|
+
throw new HostProtocol_1.HostError('frontend_still_alive');
|
|
216
|
+
try {
|
|
217
|
+
this.store.change(state => { state.epoch++; });
|
|
218
|
+
this.lease = { frontendId: body.frontendId, epoch: this.store.snapshot().epoch, token: (0, crypto_1.randomBytes)(32).toString('hex') };
|
|
219
|
+
this.ticket = undefined;
|
|
220
|
+
this.candidate = undefined;
|
|
221
|
+
this.activated = undefined;
|
|
222
|
+
this.observed = this.store.snapshot().acknowledged;
|
|
223
|
+
return this.lease;
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
proof.close();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (launcher)
|
|
230
|
+
throw new HostProtocol_1.HostError('frontend_authority_required');
|
|
231
|
+
if (method === 'inspect') {
|
|
232
|
+
(0, HostProtocol_1.fields)(body, ['lease', 'operation', 'args']);
|
|
233
|
+
this.requireLease(body.lease);
|
|
234
|
+
(0, HostProtocol_1.identifier)(body.operation);
|
|
235
|
+
if (!this.options.runtime.inspect)
|
|
236
|
+
throw new HostProtocol_1.HostError('runtime_inspection_unavailable');
|
|
237
|
+
return this.options.runtime.inspect(body.operation, (0, HostProtocol_1.payload)(body.args));
|
|
238
|
+
}
|
|
239
|
+
if (method === 'prepare') {
|
|
240
|
+
(0, HostProtocol_1.fields)(body, ['lease', 'successorId']);
|
|
241
|
+
this.requireLease(body.lease);
|
|
242
|
+
(0, HostProtocol_1.identifier)(body.successorId);
|
|
243
|
+
if (body.successorId === this.lease.frontendId)
|
|
244
|
+
throw new HostProtocol_1.HostError('invalid_successor');
|
|
245
|
+
if (this.ticket && this.ticket.deadline > perf_hooks_1.performance.now()) {
|
|
246
|
+
if (this.ticket.frontendId !== body.successorId || this.ticket.attached)
|
|
247
|
+
throw new HostProtocol_1.HostError('replacement_pending');
|
|
248
|
+
return { ticket: this.ticket.token, expires: this.ticket.expires };
|
|
249
|
+
}
|
|
250
|
+
this.candidate = undefined;
|
|
251
|
+
this.ticket = { frontendId: body.successorId, epoch: this.lease.epoch, token: (0, crypto_1.randomBytes)(32).toString('hex'),
|
|
252
|
+
expires: Date.now() + this.lifetime, deadline: perf_hooks_1.performance.now() + this.lifetime, attached: false };
|
|
253
|
+
return { ticket: this.ticket.token, expires: this.ticket.expires };
|
|
254
|
+
}
|
|
255
|
+
if (method === 'attach') {
|
|
256
|
+
(0, HostProtocol_1.fields)(body, ['frontendId', 'epoch'], ['ticket']);
|
|
257
|
+
(0, HostProtocol_1.identifier)(body.frontendId);
|
|
258
|
+
(0, HostProtocol_1.integer)(body.epoch);
|
|
259
|
+
if (body.epoch !== this.store.snapshot().epoch)
|
|
260
|
+
throw new HostProtocol_1.HostError('stale_epoch');
|
|
261
|
+
if (this.candidate && this.candidate.deadline > perf_hooks_1.performance.now())
|
|
262
|
+
throw new HostProtocol_1.HostError('replacement_pending');
|
|
263
|
+
if (this.lease) {
|
|
264
|
+
if (!this.ticket || this.ticket.attached || this.ticket.token !== body.ticket || this.ticket.frontendId !== body.frontendId
|
|
265
|
+
|| this.ticket.epoch !== body.epoch || this.ticket.deadline <= perf_hooks_1.performance.now())
|
|
266
|
+
throw new HostProtocol_1.HostError('invalid_ticket');
|
|
267
|
+
this.ticket.attached = true;
|
|
268
|
+
}
|
|
269
|
+
else if (body.ticket !== undefined)
|
|
270
|
+
throw new HostProtocol_1.HostError('invalid_ticket');
|
|
271
|
+
this.candidate = { frontendId: body.frontendId, epoch: body.epoch, token: (0, crypto_1.randomBytes)(32).toString('hex'),
|
|
272
|
+
expires: this.lease ? this.ticket.expires : Date.now() + this.lifetime,
|
|
273
|
+
deadline: this.lease ? this.ticket.deadline : perf_hooks_1.performance.now() + this.lifetime };
|
|
274
|
+
return { attachment: this.candidate.token, expires: this.candidate.expires };
|
|
275
|
+
}
|
|
276
|
+
if (method === 'activate') {
|
|
277
|
+
(0, HostProtocol_1.fields)(body, ['frontendId', 'epoch', 'attachment']);
|
|
278
|
+
(0, HostProtocol_1.identifier)(body.frontendId);
|
|
279
|
+
(0, HostProtocol_1.integer)(body.epoch);
|
|
280
|
+
(0, HostProtocol_1.secret)(body.attachment);
|
|
281
|
+
if (this.activated?.token === body.attachment && this.lease?.frontendId === body.frontendId
|
|
282
|
+
&& this.activated.lease.epoch === this.lease.epoch && body.epoch + 1 === this.lease.epoch)
|
|
283
|
+
return this.lease;
|
|
284
|
+
const candidate = this.candidate;
|
|
285
|
+
if (!candidate || candidate.token !== body.attachment || candidate.frontendId !== body.frontendId
|
|
286
|
+
|| candidate.epoch !== body.epoch || candidate.epoch !== this.store.snapshot().epoch || candidate.deadline <= perf_hooks_1.performance.now())
|
|
287
|
+
throw new HostProtocol_1.HostError('invalid_attachment');
|
|
288
|
+
this.store.change(state => { state.epoch++; });
|
|
289
|
+
this.lease = { frontendId: candidate.frontendId, epoch: this.store.snapshot().epoch, token: (0, crypto_1.randomBytes)(32).toString('hex') };
|
|
290
|
+
this.activated = { token: candidate.token, lease: this.lease };
|
|
291
|
+
this.candidate = undefined;
|
|
292
|
+
this.ticket = undefined;
|
|
293
|
+
this.observed = this.store.snapshot().acknowledged;
|
|
294
|
+
return this.lease;
|
|
295
|
+
}
|
|
296
|
+
if (method === 'detach') {
|
|
297
|
+
(0, HostProtocol_1.fields)(body, ['lease']);
|
|
298
|
+
this.requireLease(body.lease);
|
|
299
|
+
this.store.change(state => { state.epoch++; });
|
|
300
|
+
this.lease = undefined;
|
|
301
|
+
this.candidate = undefined;
|
|
302
|
+
this.ticket = undefined;
|
|
303
|
+
this.activated = undefined;
|
|
304
|
+
return { detached: true };
|
|
305
|
+
}
|
|
306
|
+
if (method === 'submit') {
|
|
307
|
+
(0, HostProtocol_1.fields)(body, ['lease', 'requestId', 'operation', 'args'], ['requestSequence']);
|
|
308
|
+
this.requireLease(body.lease);
|
|
309
|
+
(0, HostProtocol_1.identifier)(body.requestId);
|
|
310
|
+
(0, HostProtocol_1.identifier)(body.operation);
|
|
311
|
+
const args = (0, HostProtocol_1.payload)(body.args);
|
|
312
|
+
const hash = (0, HostProtocol_1.digest)((0, HostProtocol_1.canonical)([body.operation, args]));
|
|
313
|
+
const state = this.store.snapshot();
|
|
314
|
+
let requestSequence;
|
|
315
|
+
if (body.requestSequence !== undefined) {
|
|
316
|
+
(0, HostProtocol_1.integer)(body.requestSequence);
|
|
317
|
+
requestSequence = body.requestSequence;
|
|
318
|
+
if (body.requestId !== `sequenced-${requestSequence}`)
|
|
319
|
+
throw new HostProtocol_1.HostError('invalid_request_sequence');
|
|
320
|
+
if (requestSequence <= state.retiredThrough)
|
|
321
|
+
throw new HostProtocol_1.HostError('request_retired');
|
|
322
|
+
}
|
|
323
|
+
else if (body.requestId.startsWith('sequenced-'))
|
|
324
|
+
throw new HostProtocol_1.HostError('invalid_request_sequence');
|
|
325
|
+
const previous = state.operations.find(op => op.requestId === body.requestId);
|
|
326
|
+
if (previous) {
|
|
327
|
+
if (previous.requestSequence !== requestSequence)
|
|
328
|
+
throw new HostProtocol_1.HostError('invalid_request_sequence');
|
|
329
|
+
if (previous.digest !== hash)
|
|
330
|
+
throw new HostProtocol_1.HostError('request_digest_mismatch');
|
|
331
|
+
return this.receipt(previous);
|
|
332
|
+
}
|
|
333
|
+
if (requestSequence !== undefined && requestSequence !== state.acceptedThrough + 1)
|
|
334
|
+
throw new HostProtocol_1.HostError('request_sequence_gap');
|
|
335
|
+
if (state.operations.length >= this.store.limits.operations
|
|
336
|
+
|| state.events.length + state.operations.filter(op => op.status === 'pending').length >= this.store.limits.events)
|
|
337
|
+
throw new HostProtocol_1.HostError('capacity_exhausted');
|
|
338
|
+
const accepted = { requestId: body.requestId, status: 'pending' };
|
|
339
|
+
this.store.change(next => { next.operations.push({ ...accepted, digest: hash, ...(requestSequence !== undefined ? { requestSequence } : {}) }); if (requestSequence !== undefined)
|
|
340
|
+
next.acceptedThrough = requestSequence; });
|
|
341
|
+
const operation = { requestId: body.requestId, method: body.operation, args };
|
|
342
|
+
// Dispatch only this freshly committed acceptance; reconnects never enter here.
|
|
343
|
+
const work = Promise.resolve().then(async () => {
|
|
344
|
+
let completed;
|
|
345
|
+
try {
|
|
346
|
+
const result = await this.options.runtime.execute(operation);
|
|
347
|
+
try {
|
|
348
|
+
completed = { ...accepted, status: 'completed', result: (0, HostProtocol_1.payload)(result) };
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
completed = { ...accepted, status: 'failed', error: 'invalid_runtime_result' };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
completed = { ...accepted, status: 'failed', error: 'runtime_failed' };
|
|
356
|
+
}
|
|
357
|
+
this.store.change(next => {
|
|
358
|
+
Object.assign(next.operations.find(op => op.requestId === operation.requestId), completed);
|
|
359
|
+
this.store.addEvent(next, completed);
|
|
360
|
+
});
|
|
361
|
+
}).catch(() => { });
|
|
362
|
+
this.executing.add(work);
|
|
363
|
+
void work.finally(() => this.executing.delete(work));
|
|
364
|
+
return accepted;
|
|
365
|
+
}
|
|
366
|
+
if (method === 'status') {
|
|
367
|
+
(0, HostProtocol_1.fields)(body, ['lease', 'requestId']);
|
|
368
|
+
this.requireLease(body.lease);
|
|
369
|
+
(0, HostProtocol_1.identifier)(body.requestId);
|
|
370
|
+
const operation = this.store.snapshot().operations.find(op => op.requestId === body.requestId);
|
|
371
|
+
if (!operation)
|
|
372
|
+
throw new HostProtocol_1.HostError('request_not_found');
|
|
373
|
+
return this.receipt(operation);
|
|
374
|
+
}
|
|
375
|
+
if (method === 'events') {
|
|
376
|
+
(0, HostProtocol_1.fields)(body, ['lease', 'after']);
|
|
377
|
+
this.requireLease(body.lease);
|
|
378
|
+
(0, HostProtocol_1.integer)(body.after);
|
|
379
|
+
const after = body.after;
|
|
380
|
+
const state = this.store.snapshot();
|
|
381
|
+
if (after < state.acknowledged || after > state.sequence)
|
|
382
|
+
throw new HostProtocol_1.HostError('cursor_unavailable');
|
|
383
|
+
// Replaying from an older cursor is harmless; skipping unseen output cannot
|
|
384
|
+
// grant ACK authority. Each new lease must observe the unacknowledged prefix.
|
|
385
|
+
if (after > this.observed)
|
|
386
|
+
throw new HostProtocol_1.HostError('cursor_not_observed');
|
|
387
|
+
this.observed = state.sequence;
|
|
388
|
+
return { acknowledged: state.acknowledged, through: state.sequence, events: state.events.filter(event => event.sequence > after) };
|
|
389
|
+
}
|
|
390
|
+
if (method === 'ack') {
|
|
391
|
+
(0, HostProtocol_1.fields)(body, ['lease', 'through']);
|
|
392
|
+
this.requireLease(body.lease);
|
|
393
|
+
(0, HostProtocol_1.integer)(body.through);
|
|
394
|
+
const through = body.through;
|
|
395
|
+
if (through > this.observed || through < this.store.snapshot().acknowledged)
|
|
396
|
+
throw new HostProtocol_1.HostError('ack_not_observed');
|
|
397
|
+
this.store.change(state => { state.acknowledged = through; state.events = state.events.filter(event => event.sequence > through); this.store.compactAcknowledged(state); });
|
|
398
|
+
return { acknowledged: through };
|
|
399
|
+
}
|
|
400
|
+
throw new HostProtocol_1.HostError('unknown_method');
|
|
401
|
+
}
|
|
402
|
+
receipt(operation) {
|
|
403
|
+
const { requestId, status, result, error } = operation;
|
|
404
|
+
return { requestId, status, ...(result !== undefined ? { result } : {}), ...(error ? { error } : {}) };
|
|
405
|
+
}
|
|
406
|
+
requireLease(value) {
|
|
407
|
+
(0, HostProtocol_1.fields)(value, ['frontendId', 'epoch', 'token']);
|
|
408
|
+
if (!this.lease || value.frontendId !== this.lease.frontendId || value.epoch !== this.lease.epoch || value.token !== this.lease.token)
|
|
409
|
+
throw new HostProtocol_1.HostError('stale_lease');
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
exports.HostServer = HostServer;
|
|
413
|
+
//# sourceMappingURL=HostServer.js.map
|