-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathendo.js
659 lines (608 loc) · 19.3 KB
/
endo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// @ts-nocheck
/* global process */
/* eslint-disable no-await-in-loop, no-bitwise, no-throw-literal */
// Establish a perimeter:
// TODO should this
// import '@endo/init';
// rather than the following three lines?
import 'ses';
import '@endo/eventual-send/shim.js';
import '@endo/lockdown/commit.js';
import fs from 'fs';
import url from 'url';
import { Command } from 'commander';
import { prompt } from './prompt.js';
const packageDescriptorPath = url.fileURLToPath(
new URL('../package.json', import.meta.url),
);
const commonOptions = {
as: ['-a,--as <agent>', 'Pose as named agent (as named by current agent)'],
name: [
'-n,--name <name>',
'Assigns a name to the result for future reference',
],
requiredName: [
'-n,--name <name>',
'Assigns a name to the result for future reference (required)',
],
};
const parseOptionAsMapping = (optionValueString, obj) => {
// arguments can be provided as "a:b" or just "a"
// eslint-disable-next-line prefer-const
let [from, to] = optionValueString.split(':');
if (to === undefined) {
to = from;
}
obj[from] = to;
return obj;
};
export const main = async rawArgs => {
const program = new Command();
program.storeOptionsAsProperties(false);
const packageDescriptorBytes = await fs.promises.readFile(
packageDescriptorPath,
);
const packageDescriptor = JSON.parse(packageDescriptorBytes);
program.name('endo').version(packageDescriptor.version);
program
.command('install [filePath]')
.description('installs a web page (weblet)')
.option(...commonOptions.as)
.option(
'-l,--listen,--port <number>',
'Port assignment (required)',
(arg, previous) => {
if (previous !== undefined) {
throw `-l,--listen,--port can only be specified once`;
}
const port = parseInt(arg, 10);
if (Number.isNaN(port) || (port & ~0xffff) !== 0) {
throw `-l,--listen,--port flag must be a valid port number, got ${JSON.stringify(
arg,
)}`;
}
return port;
},
)
.option('-b,--bundle <bundle>', 'Bundle for a web page (weblet)')
.option(
'-p,--powers <endowment>',
'Endowment to give the weblet (a name, NONE, SELF, or ENDO)',
)
.option(...commonOptions.requiredName)
.option('-o,--open', 'Open the new web page immediately (weblet)')
.action(async (programPath, cmd) => {
const {
name: webletName,
bundle: bundleName,
powers: powersName = 'NONE',
listen: requestedPort,
as: agentNames,
open: doOpen,
} = cmd.opts();
if (requestedPort === undefined) {
throw 'The -l, --listen, or --port <number> flag is required';
}
const { install } = await import('./commands/install.js');
return install({
doOpen,
webletName,
requestedPort,
programPath,
bundleName,
powersName,
agentNames,
});
});
program
.command('open <name>')
.description('opens a web page (weblet)')
.option(...commonOptions.as)
.action(async (webletName, cmd) => {
const { as: agentNames } = cmd.opts();
const { open } = await import('./commands/open.js');
return open({
webletName,
agentNames,
});
});
program
.command('run [<file>] [<args>...]')
.description('runs a program (runlet)')
.option(...commonOptions.as)
.option('-b,--bundle <bundle>', 'Bundle name for the caplet program')
.option(
'--UNCONFINED <path>',
'Or path of an unconfined plugin to run in Node.js',
)
.option(
'-p,--powers <endowment>',
'Endowment to give the worklet (a name, NONE, HOST, or ENDO)',
)
.action(async (filePath, args, cmd) => {
const {
as: agentNames,
bundle: bundleName,
UNCONFINED: importPath,
powers: powersName = 'NONE',
} = cmd.opts();
const { run } = await import('./commands/run.js');
return run({
filePath,
args,
bundleName,
importPath,
powersName,
agentNames,
});
});
program
.command('make [file]')
.alias('mk')
.description('make a plugin or a worker caplet (worklet)')
.option('-b,--bundle <bundle>', 'Bundle for a web page to open')
.option('--UNCONFINED <file>', 'Path to a Node.js module')
.option(...commonOptions.as)
.option('-p,--powers <name>', 'Name of powers to grant or NONE, SELF, ENDO')
.option(...commonOptions.name)
.option(
'-w,--worker <worker>',
'Reuse an existing worker rather than create a new one',
)
.action(async (filePath, cmd) => {
const {
UNCONFINED: importPath,
name: resultName,
bundle: bundleName,
worker: workerName = undefined,
as: agentNames,
powers: powersName = 'NONE',
} = cmd.opts();
const { makeCommand } = await import('./commands/make.js');
return makeCommand({
filePath,
importPath,
resultName,
bundleName,
workerName,
agentNames,
powersName,
});
});
program
.command('inbox')
.option(...commonOptions.as)
.option('-f,--follow', 'Follow the inbox for messages as they arrive')
.description('read messages')
.action(async cmd => {
const { as: agentNames, follow } = cmd.opts();
const { inbox } = await import('./commands/inbox.js');
return inbox({ follow, agentNames });
});
program
.command('request <informal-description>')
.description('ask someone for something')
.option(
'-t,--to <agent>',
'Send the request to another agent (default: HOST)',
)
.option(...commonOptions.as)
.option(...commonOptions.name)
.action(async (description, cmd) => {
const {
name: resultName,
as: agentNames,
to: toName = 'HOST',
} = cmd.opts();
const { request } = await import('./commands/request.js');
return request({ toName, description, resultName, agentNames });
});
program
.command('resolve <request-number> <resolution-name>')
.description('grant a request')
.option(...commonOptions.as)
.action(async (requestNumberText, resolutionName, cmd) => {
const { as: agentNames } = cmd.opts();
const { resolveCommand } = await import('./commands/resolve.js');
return resolveCommand({
requestNumberText,
resolutionName,
agentNames,
});
});
program
.command('reject <request-number> [message]')
.description('deny a request')
.option(...commonOptions.as)
.action(async (requestNumberText, message, cmd) => {
const { as: agentNames } = cmd.opts();
const { rejectCommand } = await import('./commands/reject.js');
return rejectCommand({
requestNumberText,
message,
agentNames,
});
});
program
.command('send <agent> <message-with-embedded-references>')
.description('send a message with @named-values @for-you:from-me')
.option(...commonOptions.as)
.action(async (agentName, message, cmd) => {
const { as: agentNames } = cmd.opts();
const { send } = await import('./commands/send.js');
return send({ message, agentName, agentNames });
});
program
.command('adopt <message-number> <name-in-message>')
.option(...commonOptions.name)
.option(...commonOptions.as)
.description('adopt a @value from a message')
.action(async (messageNumberText, edgeName, cmd) => {
const { name = edgeName, as: agentNames } = cmd.opts();
const { adoptCommand } = await import('./commands/adopt.js');
return adoptCommand({
messageNumberText,
edgeName,
name,
agentNames,
});
});
program
.command('dismiss <message-number>')
.description('delete a message')
.option(...commonOptions.as)
.action(async (messageNumberText, cmd) => {
const { as: agentNames } = cmd.opts();
const { dismissCommand } = await import('./commands/dismiss.js');
return dismissCommand({
messageNumberText,
agentNames,
});
});
program
.command('list [directory]')
.alias('ls')
.description('show names known to the current or specified directory')
.option('-f,--follow', 'Follow updates')
.option('-j,--json', 'JSON format output')
.option('-v,--verbose', 'Provide more detailed output')
.action(async (directory, cmd) => {
const { follow, json, verbose } = cmd.opts();
const { list } = await import('./commands/list.js');
return list({ directory, follow, json, verbose });
});
program
.command('remove [names...]')
.alias('rm')
.description('forget a named value')
.option(...commonOptions.as)
.action(async (petNamePaths, cmd) => {
const { as: agentNames } = cmd.opts();
const { remove } = await import('./commands/remove.js');
return remove({ petNamePaths, agentNames });
});
program
.command('move <from> <to>')
.alias('mv')
.description('change the name for a value')
.option(...commonOptions.as)
.action(async (fromPath, toPath, cmd) => {
const { as: agentNames } = cmd.opts();
const { move } = await import('./commands/move.js');
return move({ fromPath, toPath, agentNames });
});
program
.command('copy <from> <to>')
.alias('cp')
.option(...commonOptions.as)
.description('makes a duplicate of a given value with a new name')
.action(async (sourcePath, targetPath, cmd) => {
const { as: agentNames } = cmd.opts();
const { copy } = await import('./commands/copy.js');
return copy({ agentNames, sourcePath, targetPath });
});
program
.command('show <name>')
.description('prints the named value')
.option(...commonOptions.as)
.action(async (name, cmd) => {
const { as: agentNames } = cmd.opts();
const { show } = await import('./commands/show.js');
return show({ name, agentNames });
});
program
.command('follow <name>')
.option(...commonOptions.as)
.description('subscribe to a stream of values')
.action(async (name, cmd) => {
const { as: agentNames } = cmd.opts();
const { followCommand } = await import('./commands/follow.js');
return followCommand({ name, agentNames });
});
program
.command('cat <name>')
.description('dumps a blob')
.option(...commonOptions.as)
.action(async (name, cmd) => {
const { as: agentNames } = cmd.opts();
const { cat } = await import('./commands/cat.js');
return cat({ name, agentNames });
});
program
.command('store')
.description('stores a blob or structured value')
.option(...commonOptions.as)
.option(...commonOptions.name)
.option('-p,--path <path>', 'store a file as a blob')
.option('--stdin', 'store stdin as a blob')
.option('--text <text>', 'store a string of UTF-8 text')
.option('--text-stdin', 'store STDIN as UTF-8 text')
.option('--json <json>', 'store JSON')
.option('--json-stdin', 'store STDIN JSON')
.option('--bigint <bigint>', 'store a bigint')
.action(async cmd => {
const {
name,
as: agentNames,
path: storePath,
stdin: storeStdin,
text: storeText,
textStdin: storeTextStdin,
json: storeJson,
jsonStdin: storeJsonStdin,
bigint: storeBigInt,
} = cmd.opts();
const { store } = await import('./commands/store.js');
return store({
storePath,
storeStdin,
storeText,
storeTextStdin,
storeJson,
storeJsonStdin,
storeBigInt,
name,
agentNames,
});
});
program
.command('eval <source> [names...]')
.description('creates a value')
.option(...commonOptions.as)
.option(
'-w,--worker <worker>',
'Reuse an existing worker rather than create a new one',
)
.option(...commonOptions.name)
.action(async (source, names, cmd) => {
const {
name: resultName,
worker: workerName = 'MAIN',
as: agentNames,
} = cmd.opts();
const { evalCommand } = await import('./commands/eval.js');
return evalCommand({
source,
names,
resultName,
workerName,
agentNames,
});
});
program
.command('spawn [names...]')
.description('creates a worker')
.option(...commonOptions.as)
.action(async (petNamePaths, cmd) => {
const { as: agentNames } = cmd.opts();
const { spawn } = await import('./commands/spawn.js');
return spawn({ petNamePaths, agentNames });
});
program
.command('bundle <application-path>')
.description('stores a program')
.option(...commonOptions.as)
.option(...commonOptions.name)
.option(
'--common-dep <name>',
'Specify common dependency for bundle (eg node builtin package shims)',
parseOptionAsMapping,
{},
)
.action(async (applicationPath, cmd) => {
const {
name: bundleName,
as: agentNames,
commonDep: commonDependencies,
} = cmd.opts();
const { bundleCommand } = await import('./commands/bundle.js');
return bundleCommand({
applicationPath,
bundleName,
agentNames,
bundleOptions: {
commonDependencies,
},
});
});
program
.command('mkhost <handle-name> [agent-name]')
.option(...commonOptions.as)
.option(
'--introduce <name>',
'Specify initial petnames for the new host. Use multiple times for multiple petnames. Format: --introduce myPetname:theirPetname',
parseOptionAsMapping,
{},
)
.description('makes a separate mailbox and storage for you')
.action(async (handleName, agentName, cmd) => {
const { as: agentNames, introduce: introducedNames } = cmd.opts();
const { mkhost } = await import('./commands/mkhost.js');
return mkhost({ handleName, agentName, agentNames, introducedNames });
});
program
.command('mkguest <handle-name> [agent-name]')
.option(...commonOptions.as)
.option(
'--introduce <name>',
'Specify initial petnames for the new guest. Use multiple times for multiple petnames. Format: --introduce myPetname:theirPetname',
parseOptionAsMapping,
{},
)
.description('makes a mailbox and storage for a guest (peer or program)')
.action(async (handleName, agentName, cmd) => {
const { as: agentNames, introduce: introducedNames } = cmd.opts();
const { mkguest } = await import('./commands/mkguest.js');
return mkguest({ agentName, handleName, agentNames, introducedNames });
});
program
.command('mkdir <path>')
.option(...commonOptions.as)
.description('makes a directory (pet store, name hub)')
.action(async (directoryPath, cmd) => {
const { as: agentNames } = cmd.opts();
const { mkdir } = await import('./commands/mkdir.js');
return mkdir({ agentNames, directoryPath });
});
program
.command('invite <guest-name>')
.option(...commonOptions.as)
.action(async (guestName, cmd) => {
const { as: agentNames } = cmd.opts();
const { invite } = await import('./commands/invite.js');
return invite({ agentNames, guestName });
});
program
.command('accept <guest-name>')
.option(...commonOptions.as)
.action(async (guestName, cmd) => {
const { as: agentNames } = cmd.opts();
const { accept } = await import('./commands/accept.js');
return accept({ agentNames, guestName });
});
program
.command('cancel <name> [reason]')
.option(...commonOptions.as)
.description('cancel a value and its deps, recovering resources')
.action(async (name, reason, cmd) => {
const { as: agentNames } = cmd.opts();
const { cancelCommand } = await import('./commands/cancel.js');
return cancelCommand({ name, agentNames, reason });
});
const where = program
.command('where')
.description('prints paths for state, logs, caches, socket, pids');
where
.command('state')
.description('prints the state directory path')
.action(async _cmd => {
const { statePath } = await import('./config.js');
process.stdout.write(`${statePath}\n`);
});
where
.command('run')
.description('prints the daemon PID file path')
.action(async _cmd => {
const { ephemeralStatePath } = await import('./config.js');
process.stdout.write(`${ephemeralStatePath}\n`);
});
where
.command('sock')
.description('prints the UNIX domain socket or Windows named pipe path')
.action(async _cmd => {
const { sockPath } = await import('./config.js');
process.stdout.write(`${sockPath}\n`);
});
where
.command('log')
.description('prints the log file path')
.action(async _cmd => {
const { logPath } = await import('./config.js');
process.stdout.write(`${logPath}\n`);
});
where
.command('cache')
.description('prints the cache directory path')
.action(async _cmd => {
const { cachePath } = await import('./config.js');
process.stdout.write(`${cachePath}\n`);
});
program
.command('start')
.description('start the endo daemon')
.action(async _cmd => {
const { start } = await import('@endo/daemon');
await start();
});
program
.command('stop')
.description('stop the endo daemon')
.action(async _cmd => {
const { stop } = await import('@endo/daemon');
await stop();
});
program
.command('restart')
.description('stop and start the daemon')
.action(async _cmd => {
const { restart } = await import('@endo/daemon');
await restart();
});
program
.command('clean')
.description('erases ephemeral state')
.action(async _cmd => {
const { clean } = await import('@endo/daemon');
await clean();
});
program
.command('purge')
.option('-f, --force', 'skip the confirmation prompt')
.description('erases persistent state and stops if running')
.action(async cmd => {
const { force } = cmd.opts();
await null;
const doPurge =
force ||
/^y(es)?$/u.test(
await prompt(
'Are you sure you want to erase all state? This irreversible action will permanently sever all peer connections. Continue? (y/n)',
),
);
if (doPurge) {
const { purge } = await import('@endo/daemon');
await purge();
}
});
program
.command('log')
.option('-f, --follow', 'follow the tail of the log')
.option(
'-p,--ping <interval>',
'milliseconds between daemon restart checks',
)
.description('writes out the daemon log, optionally following updates')
.action(async cmd => {
const { follow, ping } = cmd.opts();
const { log: logCommand } = await import('./commands/log.js');
await logCommand({ follow, ping });
});
program
.command('ping')
.description('prints ok if the daemon is responsive')
.action(async _cmd => {
const { ping } = await import('./commands/ping.js');
await ping();
});
// Throw an error instead of exiting directly.
program.exitOverride();
try {
await program.parseAsync(rawArgs, { from: 'user' });
} catch (e) {
if (e && e.name === 'CommanderError') {
return e.exitCode;
}
throw e;
}
return 0;
};