@brud/daemon 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +67 -2
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +88 -0
- package/dist/client/DaemonClient.d.ts +55 -0
- package/dist/client/DaemonClient.js +383 -0
- package/dist/client/connectToDaemon.d.ts +19 -0
- package/dist/client/connectToDaemon.js +131 -0
- package/dist/client/errors.d.ts +74 -0
- package/dist/client/errors.js +104 -0
- package/dist/client/identity.d.ts +3 -0
- package/dist/client/identity.js +7 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +23 -0
- package/dist/client/socket.d.ts +9 -0
- package/dist/client/socket.js +101 -0
- package/dist/client/spawn.d.ts +7 -0
- package/dist/client/spawn.js +171 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +29 -0
- package/dist/instanceRegistry.d.ts +130 -0
- package/dist/instanceRegistry.js +332 -0
- package/dist/lifecycle/endpoint.d.ts +19 -0
- package/dist/lifecycle/endpoint.js +124 -0
- package/dist/lifecycle/idle.d.ts +28 -0
- package/dist/lifecycle/idle.js +69 -0
- package/dist/lifecycle/index.d.ts +4 -0
- package/dist/lifecycle/index.js +21 -0
- package/dist/lifecycle/lock.d.ts +28 -0
- package/dist/lifecycle/lock.js +155 -0
- package/dist/lifecycle/shutdown.d.ts +39 -0
- package/dist/lifecycle/shutdown.js +115 -0
- package/dist/operatorConfig.d.ts +36 -0
- package/dist/operatorConfig.js +92 -0
- package/dist/security/channel.d.ts +113 -0
- package/dist/security/channel.js +448 -0
- package/dist/security/noise.d.ts +64 -0
- package/dist/security/noise.js +313 -0
- package/dist/security/ownership.d.ts +36 -0
- package/dist/security/ownership.js +86 -0
- package/dist/security/peerCredentials.d.ts +104 -0
- package/dist/security/peerCredentials.js +244 -0
- package/dist/server/Connection.d.ts +70 -0
- package/dist/server/Connection.js +412 -0
- package/dist/server/DaemonServer.d.ts +77 -0
- package/dist/server/DaemonServer.js +396 -0
- package/dist/server/createDaemon.d.ts +52 -0
- package/dist/server/createDaemon.js +165 -0
- package/dist/server/dispatch.d.ts +52 -0
- package/dist/server/dispatch.js +291 -0
- package/dist/server/framing.d.ts +63 -0
- package/dist/server/framing.js +196 -0
- package/dist/server/handshake.d.ts +22 -0
- package/dist/server/handshake.js +94 -0
- package/dist/server/protocolValidation.d.ts +47 -0
- package/dist/server/protocolValidation.js +366 -0
- package/dist/server/wireErrors.d.ts +5 -0
- package/dist/server/wireErrors.js +190 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +6 -0
- package/package.json +55 -4
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.DaemonClient = exports.REQUEST_CANCEL_METHOD = exports.DEFAULT_REQUEST_TIMEOUT_MS = void 0;
|
|
37
|
+
const Protocol = __importStar(require("@brud/protocol"));
|
|
38
|
+
const errors_js_1 = require("./errors.js");
|
|
39
|
+
const noise_js_1 = require("../security/noise.js");
|
|
40
|
+
const channel_js_1 = require("../security/channel.js");
|
|
41
|
+
exports.DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
42
|
+
exports.REQUEST_CANCEL_METHOD = Protocol.REQUEST_CANCEL;
|
|
43
|
+
const CLOSE_GRACE_MS = 1_000;
|
|
44
|
+
const CANCEL_STATUSES = new Set([
|
|
45
|
+
'cancelled',
|
|
46
|
+
'not-abortable',
|
|
47
|
+
'aborting',
|
|
48
|
+
'not-found',
|
|
49
|
+
'invalid',
|
|
50
|
+
]);
|
|
51
|
+
function readCancelAck(value) {
|
|
52
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
if (!('id' in value) || !('status' in value)) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const id = value.id;
|
|
59
|
+
if (typeof id !== 'string' && typeof id !== 'number' && id !== null) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
const status = value.status;
|
|
63
|
+
if (typeof status !== 'string' || !CANCEL_STATUSES.has(status)) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const message = 'message' in value && typeof value.message === 'string'
|
|
67
|
+
? value.message
|
|
68
|
+
: undefined;
|
|
69
|
+
return {
|
|
70
|
+
id,
|
|
71
|
+
status: status,
|
|
72
|
+
...(message === undefined ? {} : { message }),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function normalizeRequestTimeout(value) {
|
|
76
|
+
if (value === undefined) {
|
|
77
|
+
return exports.DEFAULT_REQUEST_TIMEOUT_MS;
|
|
78
|
+
}
|
|
79
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
80
|
+
throw new RangeError('timeoutMs must be a positive finite number');
|
|
81
|
+
}
|
|
82
|
+
return Math.max(1, Math.trunc(value));
|
|
83
|
+
}
|
|
84
|
+
function castResult(value) {
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
class DaemonClient {
|
|
88
|
+
socket;
|
|
89
|
+
security;
|
|
90
|
+
channel;
|
|
91
|
+
pendingRequests = new Map();
|
|
92
|
+
cancelAckListeners = new Set();
|
|
93
|
+
nextRequestId = 1;
|
|
94
|
+
handshakeResult;
|
|
95
|
+
closed = false;
|
|
96
|
+
closePromise;
|
|
97
|
+
constructor(socket, security) {
|
|
98
|
+
this.socket = socket;
|
|
99
|
+
this.security = security;
|
|
100
|
+
}
|
|
101
|
+
get isConnected() {
|
|
102
|
+
return !this.closed
|
|
103
|
+
&& !this.socket.destroyed
|
|
104
|
+
&& this.channel !== undefined
|
|
105
|
+
&& !this.channel.isClosed;
|
|
106
|
+
}
|
|
107
|
+
get handshake() {
|
|
108
|
+
return this.handshakeResult;
|
|
109
|
+
}
|
|
110
|
+
get pendingCount() {
|
|
111
|
+
return this.pendingRequests.size;
|
|
112
|
+
}
|
|
113
|
+
/** Protocol name of the authenticated channel, for diagnostics. */
|
|
114
|
+
get securityProtocol() {
|
|
115
|
+
return noise_js_1.NOISE_PROTOCOL_NAME;
|
|
116
|
+
}
|
|
117
|
+
async connect(params, options) {
|
|
118
|
+
if (this.channel === undefined) {
|
|
119
|
+
await this.establishChannel();
|
|
120
|
+
}
|
|
121
|
+
const result = await this.request(Protocol.DAEMON_CONNECT, params, options);
|
|
122
|
+
this.handshakeResult = result;
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
request(method, params, options) {
|
|
126
|
+
if (!this.isConnected || this.channel === undefined) {
|
|
127
|
+
return Promise.reject(new errors_js_1.DaemonDisconnectedError());
|
|
128
|
+
}
|
|
129
|
+
let timeoutMs;
|
|
130
|
+
try {
|
|
131
|
+
timeoutMs = normalizeRequestTimeout(options?.timeoutMs);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return Promise.reject(error instanceof Error ? error : new Error(String(error)));
|
|
135
|
+
}
|
|
136
|
+
const id = this.nextRequestId;
|
|
137
|
+
this.nextRequestId += 1;
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
const signal = options?.signal;
|
|
140
|
+
let sent = false;
|
|
141
|
+
const timer = setTimeout(() => {
|
|
142
|
+
const pending = this.pendingRequests.get(id);
|
|
143
|
+
if (pending === undefined) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.complete(id, pending);
|
|
147
|
+
reject(new errors_js_1.DaemonRequestTimeoutError({ requestId: id, method, timeoutMs }));
|
|
148
|
+
if (sent) {
|
|
149
|
+
this.cancel(id);
|
|
150
|
+
}
|
|
151
|
+
}, timeoutMs);
|
|
152
|
+
timer.unref?.();
|
|
153
|
+
const onAbort = () => {
|
|
154
|
+
const pending = this.pendingRequests.get(id);
|
|
155
|
+
if (pending === undefined) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
this.complete(id, pending);
|
|
159
|
+
reject(new errors_js_1.DaemonRequestCancelledError({ requestId: id, method }));
|
|
160
|
+
if (sent) {
|
|
161
|
+
this.cancel(id);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const pending = {
|
|
165
|
+
method,
|
|
166
|
+
resolve: (value) => {
|
|
167
|
+
resolve(castResult(value));
|
|
168
|
+
},
|
|
169
|
+
reject,
|
|
170
|
+
timer,
|
|
171
|
+
removeAbortListener: () => {
|
|
172
|
+
signal?.removeEventListener('abort', onAbort);
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
this.pendingRequests.set(id, pending);
|
|
176
|
+
if (signal?.aborted === true) {
|
|
177
|
+
onAbort();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (signal !== undefined) {
|
|
181
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
182
|
+
}
|
|
183
|
+
const request = {
|
|
184
|
+
jsonrpc: '2.0',
|
|
185
|
+
id,
|
|
186
|
+
method,
|
|
187
|
+
...(params === undefined ? {} : { params }),
|
|
188
|
+
};
|
|
189
|
+
try {
|
|
190
|
+
this.channel?.send(request);
|
|
191
|
+
sent = true;
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
this.complete(id, pending);
|
|
195
|
+
reject(new errors_js_1.DaemonDisconnectedError('Failed to send daemon request', { cause: error }));
|
|
196
|
+
this.socket.destroy();
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
notify(method, params) {
|
|
201
|
+
if (!this.isConnected || this.channel === undefined) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const notification = {
|
|
205
|
+
jsonrpc: '2.0',
|
|
206
|
+
method,
|
|
207
|
+
...(params === undefined ? {} : { params }),
|
|
208
|
+
};
|
|
209
|
+
try {
|
|
210
|
+
this.channel.send(notification);
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
this.handleSocketFailure(new errors_js_1.DaemonDisconnectedError('Failed to send daemon notification', { cause: error }));
|
|
214
|
+
this.socket.destroy();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
cancel(requestId) {
|
|
218
|
+
this.notify(exports.REQUEST_CANCEL_METHOD, { id: requestId });
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Observes the daemon's acknowledgment of a cancellation.
|
|
222
|
+
*
|
|
223
|
+
* Additive: the pre-existing timeout/cancel contract still rejects the
|
|
224
|
+
* abandoned request locally; this listener reports what the daemon actually
|
|
225
|
+
* did with the in-flight operation.
|
|
226
|
+
*/
|
|
227
|
+
onCancelAck(listener) {
|
|
228
|
+
this.cancelAckListeners.add(listener);
|
|
229
|
+
return () => {
|
|
230
|
+
this.cancelAckListeners.delete(listener);
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async close() {
|
|
234
|
+
if (this.closePromise !== undefined) {
|
|
235
|
+
return this.closePromise;
|
|
236
|
+
}
|
|
237
|
+
this.closePromise = this.shutdown();
|
|
238
|
+
return this.closePromise;
|
|
239
|
+
}
|
|
240
|
+
async establishChannel() {
|
|
241
|
+
try {
|
|
242
|
+
const { channel } = await channel_js_1.SecureChannel.connect(this.socket, {
|
|
243
|
+
psk: (0, noise_js_1.derivePsk)(this.security.psk),
|
|
244
|
+
prologue: this.security.prologue,
|
|
245
|
+
context: {
|
|
246
|
+
principal: this.security.principal,
|
|
247
|
+
home: this.security.home,
|
|
248
|
+
},
|
|
249
|
+
...(this.security.handshakeTimeoutMs === undefined
|
|
250
|
+
? {}
|
|
251
|
+
: { handshakeTimeoutMs: this.security.handshakeTimeoutMs }),
|
|
252
|
+
});
|
|
253
|
+
this.channel = channel;
|
|
254
|
+
channel.onMessage((type, text) => {
|
|
255
|
+
this.handleMessage(type, text);
|
|
256
|
+
});
|
|
257
|
+
channel.onClose(() => {
|
|
258
|
+
this.handleSocketFailure(new errors_js_1.DaemonDisconnectedError('Daemon connection closed'));
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
this.closed = true;
|
|
263
|
+
throw new errors_js_1.DaemonAuthError(`Authenticated daemon channel failed: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
async shutdown() {
|
|
267
|
+
this.closed = true;
|
|
268
|
+
this.failPending(new errors_js_1.DaemonDisconnectedError('Daemon client closed'));
|
|
269
|
+
const channel = this.channel;
|
|
270
|
+
if (channel !== undefined) {
|
|
271
|
+
channel.end();
|
|
272
|
+
}
|
|
273
|
+
else if (!this.socket.destroyed) {
|
|
274
|
+
this.socket.destroy();
|
|
275
|
+
}
|
|
276
|
+
if (this.socket.destroyed) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
await new Promise((resolve) => {
|
|
280
|
+
let settled = false;
|
|
281
|
+
let timer;
|
|
282
|
+
const finish = () => {
|
|
283
|
+
if (settled) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
settled = true;
|
|
287
|
+
if (timer !== undefined) {
|
|
288
|
+
clearTimeout(timer);
|
|
289
|
+
}
|
|
290
|
+
this.socket.off('close', onClose);
|
|
291
|
+
resolve();
|
|
292
|
+
};
|
|
293
|
+
const onClose = () => {
|
|
294
|
+
finish();
|
|
295
|
+
};
|
|
296
|
+
timer = setTimeout(() => {
|
|
297
|
+
this.socket.destroy();
|
|
298
|
+
finish();
|
|
299
|
+
}, CLOSE_GRACE_MS);
|
|
300
|
+
timer.unref?.();
|
|
301
|
+
this.socket.once('close', onClose);
|
|
302
|
+
if (this.socket.destroyed) {
|
|
303
|
+
finish();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
handleMessage(type, text) {
|
|
308
|
+
if (this.closed) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
// The server sends responses and notifications only. A server-initiated
|
|
312
|
+
// request would be a protocol violation; it is ignored rather than
|
|
313
|
+
// dispatched by the client.
|
|
314
|
+
if (type === channel_js_1.RECORD_REQUEST) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
let message;
|
|
318
|
+
try {
|
|
319
|
+
message = JSON.parse(text);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
this.handleSocketFailure(new errors_js_1.DaemonDisconnectedError('Daemon sent invalid JSON', { cause: error }));
|
|
323
|
+
this.socket.destroy();
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (type === channel_js_1.RECORD_NOTIFICATION) {
|
|
327
|
+
if (Protocol.isRpcNotification(message) && message.method === Protocol.REQUEST_CANCELLED) {
|
|
328
|
+
const ack = readCancelAck(message.params);
|
|
329
|
+
if (ack !== undefined) {
|
|
330
|
+
for (const listener of [...this.cancelAckListeners]) {
|
|
331
|
+
listener(ack);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (!Protocol.isRpcResponse(message) || message.id === null) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const pending = this.pendingRequests.get(message.id);
|
|
341
|
+
if (pending === undefined) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
this.complete(message.id, pending);
|
|
345
|
+
if (Protocol.isRpcErrorResponse(message)) {
|
|
346
|
+
pending.reject(new errors_js_1.DaemonRpcError({
|
|
347
|
+
code: message.error.code,
|
|
348
|
+
message: message.error.message,
|
|
349
|
+
method: pending.method,
|
|
350
|
+
requestId: message.id,
|
|
351
|
+
...(message.error.data === undefined ? {} : { details: message.error.data }),
|
|
352
|
+
}));
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
pending.resolve(message.result);
|
|
356
|
+
}
|
|
357
|
+
complete(id, pending) {
|
|
358
|
+
this.pendingRequests.delete(id);
|
|
359
|
+
clearTimeout(pending.timer);
|
|
360
|
+
pending.removeAbortListener();
|
|
361
|
+
}
|
|
362
|
+
handleSocketFailure(error) {
|
|
363
|
+
if (this.closed) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
this.closed = true;
|
|
367
|
+
this.failPending(error);
|
|
368
|
+
}
|
|
369
|
+
failPending(error) {
|
|
370
|
+
if (this.pendingRequests.size === 0) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const pending = [...this.pendingRequests.values()];
|
|
374
|
+
this.pendingRequests.clear();
|
|
375
|
+
for (const entry of pending) {
|
|
376
|
+
clearTimeout(entry.timer);
|
|
377
|
+
entry.removeAbortListener();
|
|
378
|
+
entry.reject(error);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
exports.DaemonClient = DaemonClient;
|
|
383
|
+
//# sourceMappingURL=DaemonClient.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DaemonCallerTier, DaemonClientIdentity } from '@brud/protocol';
|
|
2
|
+
import { DaemonClient } from './DaemonClient.js';
|
|
3
|
+
export declare const DEFAULT_HANDSHAKE_TIMEOUT_MS = 5000;
|
|
4
|
+
export interface ConnectDaemonOptions {
|
|
5
|
+
readonly home: string;
|
|
6
|
+
/**
|
|
7
|
+
* Principal the client authenticates as. The key at the matching token path
|
|
8
|
+
* is the pre-shared key; the value is not transmitted in a request field.
|
|
9
|
+
*/
|
|
10
|
+
readonly tier: DaemonCallerTier;
|
|
11
|
+
readonly spawnIfMissing?: boolean;
|
|
12
|
+
readonly timeoutMs?: number;
|
|
13
|
+
readonly handshakeTimeoutMs?: number;
|
|
14
|
+
readonly connectTimeoutMs?: number;
|
|
15
|
+
readonly client?: DaemonClientIdentity;
|
|
16
|
+
readonly clientInfo?: DaemonClientIdentity;
|
|
17
|
+
readonly capabilities?: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
export declare function connectToDaemon(options: ConnectDaemonOptions): Promise<DaemonClient>;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.DEFAULT_HANDSHAKE_TIMEOUT_MS = void 0;
|
|
37
|
+
exports.connectToDaemon = connectToDaemon;
|
|
38
|
+
const Protocol = __importStar(require("@brud/protocol"));
|
|
39
|
+
const endpoint_js_1 = require("../lifecycle/endpoint.js");
|
|
40
|
+
const DaemonClient_js_1 = require("./DaemonClient.js");
|
|
41
|
+
const errors_js_1 = require("./errors.js");
|
|
42
|
+
const identity_js_1 = require("./identity.js");
|
|
43
|
+
const spawn_js_1 = require("./spawn.js");
|
|
44
|
+
const socket_js_1 = require("./socket.js");
|
|
45
|
+
const channel_js_1 = require("../security/channel.js");
|
|
46
|
+
exports.DEFAULT_HANDSHAKE_TIMEOUT_MS = 5_000;
|
|
47
|
+
function delay(durationMs) {
|
|
48
|
+
return new Promise((resolveDelay) => {
|
|
49
|
+
setTimeout(resolveDelay, durationMs);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function isUnavailable(error) {
|
|
53
|
+
return error instanceof errors_js_1.DaemonConnectionError
|
|
54
|
+
|| error instanceof errors_js_1.DaemonDisconnectedError
|
|
55
|
+
|| error instanceof errors_js_1.DaemonRequestTimeoutError;
|
|
56
|
+
}
|
|
57
|
+
async function openClient(endpoint, options, connectTimeoutMs, handshakeTimeoutMs) {
|
|
58
|
+
const token = await (0, socket_js_1.readDaemonToken)(endpoint, options.tier);
|
|
59
|
+
const socket = await (0, socket_js_1.connectSocket)(endpoint.socketPath, connectTimeoutMs);
|
|
60
|
+
const client = new DaemonClient_js_1.DaemonClient(socket, {
|
|
61
|
+
psk: token,
|
|
62
|
+
principal: options.tier,
|
|
63
|
+
home: endpoint.home,
|
|
64
|
+
prologue: (0, channel_js_1.channelPrologue)(endpoint.socketPath),
|
|
65
|
+
handshakeTimeoutMs,
|
|
66
|
+
});
|
|
67
|
+
try {
|
|
68
|
+
await client.connect({
|
|
69
|
+
protocolVersion: Protocol.DAEMON_PROTOCOL_VERSION,
|
|
70
|
+
client: options.clientInfo
|
|
71
|
+
?? options.client
|
|
72
|
+
?? { name: identity_js_1.DAEMON_CLIENT_NAME, version: identity_js_1.DAEMON_CLIENT_VERSION },
|
|
73
|
+
capabilities: options.capabilities ?? identity_js_1.DAEMON_CLIENT_CAPABILITIES,
|
|
74
|
+
}, { timeoutMs: handshakeTimeoutMs });
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
await client.close();
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
return client;
|
|
81
|
+
}
|
|
82
|
+
async function openClientWithRetry(endpoint, options, connectTimeoutMs, handshakeTimeoutMs, timeoutMs) {
|
|
83
|
+
const deadline = Date.now() + timeoutMs;
|
|
84
|
+
let lastError;
|
|
85
|
+
for (;;) {
|
|
86
|
+
try {
|
|
87
|
+
return await openClient(endpoint, options, connectTimeoutMs, handshakeTimeoutMs);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (!isUnavailable(error)) {
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
lastError = error;
|
|
94
|
+
}
|
|
95
|
+
const remaining = deadline - Date.now();
|
|
96
|
+
if (remaining <= 0) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
await delay(Math.min(50, remaining));
|
|
100
|
+
}
|
|
101
|
+
throw new errors_js_1.DaemonStartupTimeoutError({
|
|
102
|
+
home: endpoint.home,
|
|
103
|
+
timeoutMs,
|
|
104
|
+
cause: lastError,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
async function connectToDaemon(options) {
|
|
108
|
+
const canonicalRoot = await (0, endpoint_js_1.canonicalizeDaemonHome)(options.home);
|
|
109
|
+
const endpoint = await (0, endpoint_js_1.resolveEndpoint)(canonicalRoot);
|
|
110
|
+
const timeoutMs = (0, socket_js_1.normalizeTimeout)(options.timeoutMs, spawn_js_1.DEFAULT_SPAWN_TIMEOUT_MS);
|
|
111
|
+
const connectTimeoutMs = (0, socket_js_1.normalizeTimeout)(options.connectTimeoutMs, Math.min(socket_js_1.DEFAULT_CONNECT_TIMEOUT_MS, timeoutMs));
|
|
112
|
+
const handshakeTimeoutMs = (0, socket_js_1.normalizeTimeout)(options.handshakeTimeoutMs, exports.DEFAULT_HANDSHAKE_TIMEOUT_MS);
|
|
113
|
+
try {
|
|
114
|
+
return await openClient(endpoint, options, connectTimeoutMs, handshakeTimeoutMs);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (!isUnavailable(error)) {
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
if (options.spawnIfMissing === false) {
|
|
121
|
+
throw new errors_js_1.DaemonNotRunningError({
|
|
122
|
+
home: endpoint.home,
|
|
123
|
+
socketPath: endpoint.socketPath,
|
|
124
|
+
cause: error,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
await (0, spawn_js_1.spawnDaemon)(endpoint.home, { timeoutMs });
|
|
128
|
+
return openClientWithRetry(endpoint, options, connectTimeoutMs, handshakeTimeoutMs, timeoutMs);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=connectToDaemon.js.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { JsonRpcId, RpcErrorCode, RpcErrorDetails } from '@brud/protocol';
|
|
2
|
+
export declare class DaemonClientError extends Error {
|
|
3
|
+
constructor(message: string, options?: ErrorOptions);
|
|
4
|
+
}
|
|
5
|
+
export interface DaemonRpcErrorOptions {
|
|
6
|
+
readonly code: RpcErrorCode;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
readonly method: string;
|
|
9
|
+
readonly requestId: JsonRpcId;
|
|
10
|
+
readonly details?: RpcErrorDetails;
|
|
11
|
+
}
|
|
12
|
+
export declare class DaemonRpcError extends DaemonClientError {
|
|
13
|
+
readonly code: RpcErrorCode;
|
|
14
|
+
readonly method: string;
|
|
15
|
+
readonly requestId: JsonRpcId;
|
|
16
|
+
readonly details: RpcErrorDetails | undefined;
|
|
17
|
+
constructor(options: DaemonRpcErrorOptions);
|
|
18
|
+
}
|
|
19
|
+
export declare class DaemonDisconnectedError extends DaemonClientError {
|
|
20
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
21
|
+
}
|
|
22
|
+
export interface DaemonRequestTimeoutErrorOptions {
|
|
23
|
+
readonly requestId: JsonRpcId;
|
|
24
|
+
readonly method: string;
|
|
25
|
+
readonly timeoutMs: number;
|
|
26
|
+
}
|
|
27
|
+
export declare class DaemonRequestTimeoutError extends DaemonClientError {
|
|
28
|
+
readonly requestId: JsonRpcId;
|
|
29
|
+
readonly method: string;
|
|
30
|
+
readonly timeoutMs: number;
|
|
31
|
+
constructor(options: DaemonRequestTimeoutErrorOptions);
|
|
32
|
+
}
|
|
33
|
+
export interface DaemonRequestCancelledErrorOptions {
|
|
34
|
+
readonly requestId: JsonRpcId;
|
|
35
|
+
readonly method: string;
|
|
36
|
+
}
|
|
37
|
+
export declare class DaemonRequestCancelledError extends DaemonClientError {
|
|
38
|
+
readonly requestId: JsonRpcId;
|
|
39
|
+
readonly method: string;
|
|
40
|
+
constructor(options: DaemonRequestCancelledErrorOptions);
|
|
41
|
+
}
|
|
42
|
+
export declare class DaemonConnectionError extends DaemonClientError {
|
|
43
|
+
constructor(message: string, options?: ErrorOptions);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The authenticated channel could not be established: wrong principal key,
|
|
47
|
+
* invalid handshake payload, tampered record, or a rejected workspace scope.
|
|
48
|
+
*/
|
|
49
|
+
export declare class DaemonAuthError extends DaemonClientError {
|
|
50
|
+
constructor(message: string, options?: ErrorOptions);
|
|
51
|
+
}
|
|
52
|
+
export declare class DaemonSpawnError extends DaemonClientError {
|
|
53
|
+
constructor(message: string, options?: ErrorOptions);
|
|
54
|
+
}
|
|
55
|
+
export interface DaemonNotRunningErrorOptions {
|
|
56
|
+
readonly home: string;
|
|
57
|
+
readonly socketPath: string;
|
|
58
|
+
readonly cause?: unknown;
|
|
59
|
+
}
|
|
60
|
+
export declare class DaemonNotRunningError extends DaemonClientError {
|
|
61
|
+
readonly home: string;
|
|
62
|
+
readonly socketPath: string;
|
|
63
|
+
constructor(options: DaemonNotRunningErrorOptions);
|
|
64
|
+
}
|
|
65
|
+
export interface DaemonStartupTimeoutErrorOptions {
|
|
66
|
+
readonly home: string;
|
|
67
|
+
readonly timeoutMs: number;
|
|
68
|
+
readonly cause?: unknown;
|
|
69
|
+
}
|
|
70
|
+
export declare class DaemonStartupTimeoutError extends DaemonClientError {
|
|
71
|
+
readonly home: string;
|
|
72
|
+
readonly timeoutMs: number;
|
|
73
|
+
constructor(options: DaemonStartupTimeoutErrorOptions);
|
|
74
|
+
}
|