@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.
Files changed (61) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +67 -2
  3. package/dist/cli.d.ts +3 -0
  4. package/dist/cli.js +88 -0
  5. package/dist/client/DaemonClient.d.ts +55 -0
  6. package/dist/client/DaemonClient.js +383 -0
  7. package/dist/client/connectToDaemon.d.ts +19 -0
  8. package/dist/client/connectToDaemon.js +131 -0
  9. package/dist/client/errors.d.ts +74 -0
  10. package/dist/client/errors.js +104 -0
  11. package/dist/client/identity.d.ts +3 -0
  12. package/dist/client/identity.js +7 -0
  13. package/dist/client/index.d.ts +6 -0
  14. package/dist/client/index.js +23 -0
  15. package/dist/client/socket.d.ts +9 -0
  16. package/dist/client/socket.js +101 -0
  17. package/dist/client/spawn.d.ts +7 -0
  18. package/dist/client/spawn.js +171 -0
  19. package/dist/index.d.ts +12 -0
  20. package/dist/index.js +29 -0
  21. package/dist/instanceRegistry.d.ts +130 -0
  22. package/dist/instanceRegistry.js +332 -0
  23. package/dist/lifecycle/endpoint.d.ts +19 -0
  24. package/dist/lifecycle/endpoint.js +124 -0
  25. package/dist/lifecycle/idle.d.ts +28 -0
  26. package/dist/lifecycle/idle.js +69 -0
  27. package/dist/lifecycle/index.d.ts +4 -0
  28. package/dist/lifecycle/index.js +21 -0
  29. package/dist/lifecycle/lock.d.ts +28 -0
  30. package/dist/lifecycle/lock.js +155 -0
  31. package/dist/lifecycle/shutdown.d.ts +39 -0
  32. package/dist/lifecycle/shutdown.js +115 -0
  33. package/dist/operatorConfig.d.ts +36 -0
  34. package/dist/operatorConfig.js +92 -0
  35. package/dist/security/channel.d.ts +113 -0
  36. package/dist/security/channel.js +448 -0
  37. package/dist/security/noise.d.ts +64 -0
  38. package/dist/security/noise.js +313 -0
  39. package/dist/security/ownership.d.ts +36 -0
  40. package/dist/security/ownership.js +86 -0
  41. package/dist/security/peerCredentials.d.ts +104 -0
  42. package/dist/security/peerCredentials.js +244 -0
  43. package/dist/server/Connection.d.ts +70 -0
  44. package/dist/server/Connection.js +412 -0
  45. package/dist/server/DaemonServer.d.ts +77 -0
  46. package/dist/server/DaemonServer.js +396 -0
  47. package/dist/server/createDaemon.d.ts +52 -0
  48. package/dist/server/createDaemon.js +165 -0
  49. package/dist/server/dispatch.d.ts +52 -0
  50. package/dist/server/dispatch.js +291 -0
  51. package/dist/server/framing.d.ts +63 -0
  52. package/dist/server/framing.js +196 -0
  53. package/dist/server/handshake.d.ts +22 -0
  54. package/dist/server/handshake.js +94 -0
  55. package/dist/server/protocolValidation.d.ts +47 -0
  56. package/dist/server/protocolValidation.js +366 -0
  57. package/dist/server/wireErrors.d.ts +5 -0
  58. package/dist/server/wireErrors.js +190 -0
  59. package/dist/version.d.ts +2 -0
  60. package/dist/version.js +6 -0
  61. package/package.json +55 -4
@@ -0,0 +1,47 @@
1
+ import type { JsonRpcId, RpcErrorCode, RpcNotification, RpcRequest } from '@brud/protocol';
2
+ import type { RecordType } from '../security/channel.js';
3
+ /** Why a raw record did not satisfy the closed message contract. */
4
+ export declare class JsonContractError extends Error {
5
+ readonly frameId: JsonRpcId;
6
+ constructor(message: string, frameId?: JsonRpcId);
7
+ }
8
+ /**
9
+ * Extracts the best-effort request id from a malformed frame so the client can
10
+ * correlate the parse error. The raw text is never trusted beyond this regex.
11
+ */
12
+ export declare function extractFrameId(text: string): JsonRpcId;
13
+ /**
14
+ * A strict JSON parser for the wire contract.
15
+ *
16
+ * `JSON.parse` silently keeps the last value of a duplicate key. This parser
17
+ * rejects duplicate object keys, rejects trailing data, bounds nesting, and
18
+ * builds ordinary objects without invoking the `__proto__` setter.
19
+ */
20
+ export declare function parseStrictJson(text: string): unknown;
21
+ export interface IncomingRequest {
22
+ readonly ok: true;
23
+ readonly kind: 'request';
24
+ readonly request: RpcRequest;
25
+ }
26
+ export interface IncomingNotification {
27
+ readonly ok: true;
28
+ readonly kind: 'notification';
29
+ readonly notification: RpcNotification;
30
+ }
31
+ export interface IncomingFailure {
32
+ readonly ok: false;
33
+ readonly id: JsonRpcId;
34
+ readonly code: RpcErrorCode;
35
+ readonly message: string;
36
+ }
37
+ export type IncomingEnvelope = IncomingRequest | IncomingNotification | IncomingFailure;
38
+ /**
39
+ * Validates one decrypted record against the closed JSON-RPC contract.
40
+ *
41
+ * The record type is the authenticated type carried in the AEAD associated
42
+ * data. A request record may not contain a response, and a notification
43
+ * record may not contain a request id.
44
+ */
45
+ export declare function validateIncomingEnvelope(value: unknown, recordType: RecordType): IncomingEnvelope;
46
+ /** True when a value is a plain object; retained for callers that need it. */
47
+ export declare function isPlainRecord(value: unknown): value is Record<string, unknown>;
@@ -0,0 +1,366 @@
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.JsonContractError = void 0;
37
+ exports.extractFrameId = extractFrameId;
38
+ exports.parseStrictJson = parseStrictJson;
39
+ exports.validateIncomingEnvelope = validateIncomingEnvelope;
40
+ exports.isPlainRecord = isPlainRecord;
41
+ const Protocol = __importStar(require("@brud/protocol"));
42
+ const channel_js_1 = require("../security/channel.js");
43
+ const MAX_JSON_DEPTH = 64;
44
+ /** Why a raw record did not satisfy the closed message contract. */
45
+ class JsonContractError extends Error {
46
+ frameId;
47
+ constructor(message, frameId = null) {
48
+ super(message);
49
+ this.name = 'JsonContractError';
50
+ this.frameId = frameId;
51
+ }
52
+ }
53
+ exports.JsonContractError = JsonContractError;
54
+ /**
55
+ * Extracts the best-effort request id from a malformed frame so the client can
56
+ * correlate the parse error. The raw text is never trusted beyond this regex.
57
+ */
58
+ function extractFrameId(text) {
59
+ const match = /"id"\s*:\s*("(?:[^"\\]|\\.)*"|-?\d+(?:\.\d+)?|null)/u.exec(text);
60
+ if (match === null) {
61
+ return null;
62
+ }
63
+ const raw = match[1];
64
+ if (raw === 'null') {
65
+ return null;
66
+ }
67
+ try {
68
+ const value = JSON.parse(raw);
69
+ return typeof value === 'string' || typeof value === 'number' ? value : null;
70
+ }
71
+ catch {
72
+ return null;
73
+ }
74
+ }
75
+ /**
76
+ * A strict JSON parser for the wire contract.
77
+ *
78
+ * `JSON.parse` silently keeps the last value of a duplicate key. This parser
79
+ * rejects duplicate object keys, rejects trailing data, bounds nesting, and
80
+ * builds ordinary objects without invoking the `__proto__` setter.
81
+ */
82
+ function parseStrictJson(text) {
83
+ let index = 0;
84
+ const fail = (message) => {
85
+ throw new JsonContractError(`${message} at offset ${index}`, extractFrameId(text));
86
+ };
87
+ const skipWhitespace = () => {
88
+ for (;;) {
89
+ const code = text.charCodeAt(index);
90
+ if (code === 0x20 || code === 0x09 || code === 0x0a || code === 0x0d) {
91
+ index += 1;
92
+ continue;
93
+ }
94
+ return;
95
+ }
96
+ };
97
+ const expect = (character) => {
98
+ if (text[index] !== character) {
99
+ fail(`Expected "${character}"`);
100
+ }
101
+ index += 1;
102
+ };
103
+ const parseString = () => {
104
+ if (text[index] !== '"') {
105
+ fail('Expected a string');
106
+ }
107
+ const start = index;
108
+ index += 1;
109
+ for (;;) {
110
+ if (index >= text.length) {
111
+ fail('Unterminated string');
112
+ }
113
+ const character = text[index];
114
+ if (character === '"') {
115
+ index += 1;
116
+ break;
117
+ }
118
+ if (character === '\\') {
119
+ index += 2;
120
+ continue;
121
+ }
122
+ index += 1;
123
+ }
124
+ const raw = text.slice(start, index);
125
+ try {
126
+ return JSON.parse(raw);
127
+ }
128
+ catch {
129
+ return fail('Invalid string escape');
130
+ }
131
+ };
132
+ const parseNumber = () => {
133
+ const match = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/u.exec(text.slice(index));
134
+ if (match === null) {
135
+ return fail('Invalid number');
136
+ }
137
+ index += match[0].length;
138
+ return Number(match[0]);
139
+ };
140
+ const parseArray = (depth) => {
141
+ if (depth > MAX_JSON_DEPTH) {
142
+ fail('JSON nesting limit exceeded');
143
+ }
144
+ const result = [];
145
+ index += 1;
146
+ skipWhitespace();
147
+ if (text[index] === ']') {
148
+ index += 1;
149
+ return result;
150
+ }
151
+ for (;;) {
152
+ result.push(parseValue(depth + 1));
153
+ skipWhitespace();
154
+ if (text[index] === ',') {
155
+ index += 1;
156
+ skipWhitespace();
157
+ continue;
158
+ }
159
+ expect(']');
160
+ return result;
161
+ }
162
+ };
163
+ const parseObject = (depth) => {
164
+ if (depth > MAX_JSON_DEPTH) {
165
+ fail('JSON nesting limit exceeded');
166
+ }
167
+ const result = {};
168
+ const keys = new Set();
169
+ index += 1;
170
+ skipWhitespace();
171
+ if (text[index] === '}') {
172
+ index += 1;
173
+ return result;
174
+ }
175
+ for (;;) {
176
+ const key = parseString();
177
+ if (keys.has(key)) {
178
+ fail(`Duplicate object key "${key}"`);
179
+ }
180
+ keys.add(key);
181
+ skipWhitespace();
182
+ expect(':');
183
+ skipWhitespace();
184
+ Object.defineProperty(result, key, {
185
+ value: parseValue(depth + 1),
186
+ enumerable: true,
187
+ configurable: true,
188
+ writable: true,
189
+ });
190
+ skipWhitespace();
191
+ if (text[index] === ',') {
192
+ index += 1;
193
+ skipWhitespace();
194
+ continue;
195
+ }
196
+ expect('}');
197
+ return result;
198
+ }
199
+ };
200
+ const parseLiteral = (literal, value) => {
201
+ if (text.slice(index, index + literal.length) !== literal) {
202
+ fail(`Expected "${literal}"`);
203
+ }
204
+ index += literal.length;
205
+ return value;
206
+ };
207
+ const parseValue = (depth) => {
208
+ skipWhitespace();
209
+ const character = text[index];
210
+ switch (character) {
211
+ case '{':
212
+ return parseObject(depth);
213
+ case '[':
214
+ return parseArray(depth);
215
+ case '"':
216
+ return parseString();
217
+ case 't':
218
+ return parseLiteral('true', true);
219
+ case 'f':
220
+ return parseLiteral('false', false);
221
+ case 'n':
222
+ return parseLiteral('null', null);
223
+ default:
224
+ if (character === '-' || (character !== undefined && character >= '0' && character <= '9')) {
225
+ return parseNumber();
226
+ }
227
+ return fail('Unexpected token');
228
+ }
229
+ };
230
+ const value = parseValue(0);
231
+ skipWhitespace();
232
+ if (index !== text.length) {
233
+ fail('Trailing data after JSON value');
234
+ }
235
+ return value;
236
+ }
237
+ const PARAMS_KEYS = {
238
+ [Protocol.DAEMON_CONNECT]: ['protocolVersion', 'clientInfo', 'client', 'capabilities'],
239
+ [Protocol.DAEMON_PING]: ['nonce'],
240
+ [Protocol.DAEMON_INFO]: [],
241
+ [Protocol.KERNEL_REQUIREMENTS]: [],
242
+ [Protocol.KERNEL_BOOT]: ['options'],
243
+ [Protocol.KERNEL_ATTACH]: ['instanceId'],
244
+ [Protocol.KERNEL_LIST]: [],
245
+ [Protocol.KERNEL_STATUS]: ['instanceId'],
246
+ [Protocol.KERNEL_DISPOSE]: ['instanceId'],
247
+ [Protocol.KERNEL_KINDS]: ['instanceId'],
248
+ [Protocol.KERNEL_ROUTE]: ['instanceId', 'text', 'stopOnFailure', 'dryRun', 'maxOperations'],
249
+ [Protocol.REQUEST_CANCEL]: ['id'],
250
+ };
251
+ function isRecord(value) {
252
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
253
+ }
254
+ function isJsonRpcId(value) {
255
+ return value === null || typeof value === 'string' || typeof value === 'number';
256
+ }
257
+ function rejectUnknownKeys(value, allowed) {
258
+ for (const key of Object.keys(value)) {
259
+ if (!allowed.includes(key)) {
260
+ return `Unknown field "${key}"`;
261
+ }
262
+ }
263
+ return undefined;
264
+ }
265
+ function validateParams(method, params) {
266
+ if (params === undefined) {
267
+ return undefined;
268
+ }
269
+ if (!isRecord(params)) {
270
+ return 'Method params must be a JSON object';
271
+ }
272
+ const allowed = PARAMS_KEYS[method];
273
+ if (allowed !== undefined) {
274
+ return rejectUnknownKeys(params, allowed);
275
+ }
276
+ return undefined;
277
+ }
278
+ /**
279
+ * Validates one decrypted record against the closed JSON-RPC contract.
280
+ *
281
+ * The record type is the authenticated type carried in the AEAD associated
282
+ * data. A request record may not contain a response, and a notification
283
+ * record may not contain a request id.
284
+ */
285
+ function validateIncomingEnvelope(value, recordType) {
286
+ if (!isRecord(value)) {
287
+ return failure(null, Protocol.INVALID_REQUEST, 'Expected a JSON-RPC object');
288
+ }
289
+ const id = 'id' in value ? value.id : null;
290
+ const safeId = isJsonRpcId(id) ? id : null;
291
+ if (recordType === channel_js_1.RECORD_REQUEST) {
292
+ const unknown = rejectUnknownKeys(value, ['jsonrpc', 'id', 'method', 'params']);
293
+ if (unknown !== undefined) {
294
+ return failure(safeId, Protocol.INVALID_REQUEST, unknown);
295
+ }
296
+ if (value.jsonrpc !== '2.0') {
297
+ return failure(safeId, Protocol.INVALID_REQUEST, 'jsonrpc must be "2.0"');
298
+ }
299
+ if (!('id' in value) || !isJsonRpcId(value.id)) {
300
+ return failure(safeId, Protocol.INVALID_REQUEST, 'Request id must be a string, number, or null');
301
+ }
302
+ if (typeof value.method !== 'string' || value.method.length === 0) {
303
+ return failure(safeId, Protocol.INVALID_REQUEST, 'method must be a non-empty string');
304
+ }
305
+ const paramsError = validateParams(value.method, value.params);
306
+ if (paramsError !== undefined) {
307
+ return failure(safeId, Protocol.INVALID_REQUEST, paramsError);
308
+ }
309
+ if (value.method === Protocol.DAEMON_CONNECT) {
310
+ const params = value.params;
311
+ if (!isRecord(params) || typeof params.protocolVersion !== 'string') {
312
+ return failure(safeId, Protocol.INVALID_PARAMS, 'protocolVersion must be a string');
313
+ }
314
+ }
315
+ return {
316
+ ok: true,
317
+ kind: 'request',
318
+ request: {
319
+ jsonrpc: '2.0',
320
+ id: value.id,
321
+ method: value.method,
322
+ ...(value.params === undefined ? {} : { params: value.params }),
323
+ },
324
+ };
325
+ }
326
+ if (recordType === channel_js_1.RECORD_NOTIFICATION) {
327
+ const unknown = rejectUnknownKeys(value, ['jsonrpc', 'method', 'params']);
328
+ if (unknown !== undefined) {
329
+ return failure(safeId, Protocol.INVALID_REQUEST, unknown);
330
+ }
331
+ if (value.jsonrpc !== '2.0') {
332
+ return failure(safeId, Protocol.INVALID_REQUEST, 'jsonrpc must be "2.0"');
333
+ }
334
+ if ('id' in value) {
335
+ return failure(safeId, Protocol.INVALID_REQUEST, 'A notification must not carry an id');
336
+ }
337
+ if (typeof value.method !== 'string' || value.method.length === 0) {
338
+ return failure(safeId, Protocol.INVALID_REQUEST, 'method must be a non-empty string');
339
+ }
340
+ if (value.method !== Protocol.REQUEST_CANCEL) {
341
+ return failure(safeId, Protocol.INVALID_REQUEST, `Unknown notification "${value.method}"`);
342
+ }
343
+ const paramsError = validateParams(value.method, value.params);
344
+ if (paramsError !== undefined) {
345
+ return failure(safeId, Protocol.INVALID_REQUEST, paramsError);
346
+ }
347
+ return {
348
+ ok: true,
349
+ kind: 'notification',
350
+ notification: {
351
+ jsonrpc: '2.0',
352
+ method: value.method,
353
+ ...(value.params === undefined ? {} : { params: value.params }),
354
+ },
355
+ };
356
+ }
357
+ return failure(safeId, Protocol.INVALID_REQUEST, `The daemon does not accept ${recordType === 3 ? 'success' : 'error'} responses`);
358
+ }
359
+ function failure(id, code, message) {
360
+ return { ok: false, id, code, message };
361
+ }
362
+ /** True when a value is a plain object; retained for callers that need it. */
363
+ function isPlainRecord(value) {
364
+ return isRecord(value);
365
+ }
366
+ //# sourceMappingURL=protocolValidation.js.map
@@ -0,0 +1,5 @@
1
+ import * as Protocol from '@brud/protocol';
2
+ import type { JsonRpcId, RpcErrorData, RpcErrorDetails, RpcErrorResponse } from '@brud/protocol';
3
+ export declare function createRpcErrorResponse(id: JsonRpcId, code: Protocol.RpcErrorCode, message: string, data?: RpcErrorDetails): RpcErrorResponse;
4
+ export declare function toRpcErrorData(error: unknown): RpcErrorData;
5
+ export declare function toRpcErrorResponse(id: JsonRpcId, error: unknown): RpcErrorResponse;
@@ -0,0 +1,190 @@
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.createRpcErrorResponse = createRpcErrorResponse;
37
+ exports.toRpcErrorData = toRpcErrorData;
38
+ exports.toRpcErrorResponse = toRpcErrorResponse;
39
+ const Protocol = __importStar(require("@brud/protocol"));
40
+ const LIFECYCLE_ERROR_CODES = new Set(['INVALID_LIFECYCLE_STATE']);
41
+ const ACCESS_ERROR_CODES = new Set([
42
+ 'BLACKLISTED_ACCESS',
43
+ 'NOT_IN_ALLOWLIST',
44
+ ]);
45
+ const STORAGE_ERROR_CODES = new Set([
46
+ 'EACCES',
47
+ 'EEXIST',
48
+ 'EISDIR',
49
+ 'EINVAL',
50
+ 'ENOTDIR',
51
+ 'EPERM',
52
+ ]);
53
+ function readCode(error) {
54
+ if (!(error instanceof Error) || !('code' in error)) {
55
+ return undefined;
56
+ }
57
+ const code = error.code;
58
+ return typeof code === 'string' ? code : undefined;
59
+ }
60
+ function readField(error, field) {
61
+ switch (field) {
62
+ case 'rule':
63
+ return 'rule' in error && typeof error.rule === 'string' ? error.rule : undefined;
64
+ case 'root':
65
+ return 'root' in error && typeof error.root === 'string' ? error.root : undefined;
66
+ case 'path':
67
+ return 'path' in error && typeof error.path === 'string' ? error.path : undefined;
68
+ case 'candidatePath':
69
+ return 'candidatePath' in error && typeof error.candidatePath === 'string'
70
+ ? error.candidatePath
71
+ : undefined;
72
+ case 'reason':
73
+ return 'reason' in error && typeof error.reason === 'string' ? error.reason : undefined;
74
+ case 'operation':
75
+ return 'operation' in error && typeof error.operation === 'string'
76
+ ? error.operation
77
+ : undefined;
78
+ case 'syscall':
79
+ return 'syscall' in error && typeof error.syscall === 'string'
80
+ ? error.syscall
81
+ : undefined;
82
+ }
83
+ }
84
+ function errorMessage(error) {
85
+ return error instanceof Error ? error.message : String(error);
86
+ }
87
+ function genericErrorData(code) {
88
+ return code === undefined ? {} : { errorCode: code };
89
+ }
90
+ function createRpcErrorResponse(id, code, message, data) {
91
+ const error = data === undefined
92
+ ? { code, message }
93
+ : { code, message, data };
94
+ return {
95
+ jsonrpc: '2.0',
96
+ id,
97
+ error,
98
+ };
99
+ }
100
+ function toRpcErrorData(error) {
101
+ const code = readCode(error);
102
+ const message = errorMessage(error);
103
+ if (code === 'PATH_TRAVERSAL_DETECTED' && error instanceof Error) {
104
+ return {
105
+ code: Protocol.PATH_VIOLATION,
106
+ message,
107
+ data: {
108
+ errorCode: code,
109
+ rule: readField(error, 'rule'),
110
+ root: readField(error, 'root'),
111
+ path: readField(error, 'path'),
112
+ reason: readField(error, 'reason'),
113
+ },
114
+ };
115
+ }
116
+ // Kernel WorkspaceService containment failures (`WorkspaceViolationError`).
117
+ // Every rule is a path violation on the wire; `rule` tells clients which one.
118
+ // `errorCode` keeps the frozen wire value so existing clients are unaffected.
119
+ if (code === 'WORKSPACE_PATH_VIOLATION' && error instanceof Error) {
120
+ return {
121
+ code: Protocol.PATH_VIOLATION,
122
+ message,
123
+ data: {
124
+ errorCode: 'PATH_TRAVERSAL_DETECTED',
125
+ rule: readField(error, 'rule'),
126
+ root: readField(error, 'root'),
127
+ path: readField(error, 'candidatePath') ?? readField(error, 'path'),
128
+ reason: readField(error, 'reason'),
129
+ },
130
+ };
131
+ }
132
+ if (code === 'READ_ONLY_VIOLATION' && error instanceof Error) {
133
+ return {
134
+ code: Protocol.READ_ONLY_VIOLATION,
135
+ message,
136
+ data: {
137
+ errorCode: code,
138
+ operation: readField(error, 'operation'),
139
+ },
140
+ };
141
+ }
142
+ if (code !== undefined && LIFECYCLE_ERROR_CODES.has(code) && error instanceof Error) {
143
+ return {
144
+ code: Protocol.LIFECYCLE_STATE,
145
+ message,
146
+ data: {
147
+ errorCode: code,
148
+ operation: readField(error, 'operation'),
149
+ },
150
+ };
151
+ }
152
+ if (code !== undefined && ACCESS_ERROR_CODES.has(code)) {
153
+ return {
154
+ code: Protocol.ACCESS_DENIED,
155
+ message,
156
+ data: { errorCode: code },
157
+ };
158
+ }
159
+ if (code === 'ENOENT') {
160
+ return {
161
+ code: Protocol.FILE_NOT_FOUND,
162
+ message,
163
+ data: { errorCode: code },
164
+ };
165
+ }
166
+ if (code !== undefined && STORAGE_ERROR_CODES.has(code) && error instanceof Error) {
167
+ return {
168
+ code: Protocol.STORAGE_ERROR,
169
+ message,
170
+ data: {
171
+ errorCode: code,
172
+ syscall: readField(error, 'syscall'),
173
+ path: readField(error, 'path'),
174
+ },
175
+ };
176
+ }
177
+ return {
178
+ code: Protocol.INTERNAL_ERROR,
179
+ message,
180
+ data: genericErrorData(code),
181
+ };
182
+ }
183
+ function toRpcErrorResponse(id, error) {
184
+ return {
185
+ jsonrpc: '2.0',
186
+ id,
187
+ error: toRpcErrorData(error),
188
+ };
189
+ }
190
+ //# sourceMappingURL=wireErrors.js.map
@@ -0,0 +1,2 @@
1
+ export declare const DAEMON_SERVER_NAME = "brud-daemon";
2
+ export declare const DAEMON_SERVER_VERSION = "0.0.1";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DAEMON_SERVER_VERSION = exports.DAEMON_SERVER_NAME = void 0;
4
+ exports.DAEMON_SERVER_NAME = 'brud-daemon';
5
+ exports.DAEMON_SERVER_VERSION = '0.0.1';
6
+ //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@brud/daemon",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "Brud daemon: a neutral local host for BrudOS kernels",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "bin": {
8
+ "brud-daemon": "dist/cli.js"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "!dist/**/__tests__",
20
+ "!dist/**/__verification__",
21
+ "!dist/**/*.map"
22
+ ],
23
+ "engines": {
24
+ "node": ">=22"
25
+ },
26
+ "scripts": {
27
+ "build": "tsc",
28
+ "test": "node --import tsx --test \"src/__tests__/*.test.ts\""
29
+ },
30
+ "dependencies": {
31
+ "@brud/kernel": "^0.0.1",
32
+ "@brud/protocol": "^0.0.1",
33
+ "@cotal-ai/seat": "0.160.4"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "22.x"
37
+ },
38
+ "license": "Apache-2.0",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/rahatarch/brud.git",
42
+ "directory": "packages/daemon"
43
+ },
44
+ "homepage": "https://github.com/rahatarch/brud/tree/main/docs-next",
45
+ "bugs": {
46
+ "url": "https://github.com/rahatarch/brud/issues"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "keywords": [
52
+ "brud",
53
+ "brudos",
54
+ "kernel",
55
+ "participants"
56
+ ]
57
+ }