riteshjs 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,555 @@
1
+ /*! ritesh | MIT | Copyright (c) 2026 Jarrod Baumann | bundles xterm.js (MIT, see THIRD_PARTY_NOTICES) */
2
+ declare const ERRNOS: readonly ["ENOENT", "ENOTDIR", "EISDIR", "EACCES", "EPERM", "EEXIST", "ENOTEMPTY", "EROFS", "ELOOP", "EINVAL", "ENOTSUP", "ENOSPC", "EIO", "EPIPE", "ECANCELED", "ETIMEDOUT", "ENOTCONN"];
3
+ type Errno = (typeof ERRNOS)[number];
4
+ declare const ERRNO_MESSAGES: Record<Errno, string>;
5
+ /** The one error type that crosses module boundaries. */
6
+ declare class RiteError extends Error {
7
+ readonly errno: Errno;
8
+ readonly path: string | undefined;
9
+ constructor(errno: Errno, path?: string, message?: string);
10
+ }
11
+ /** Exit codes with fixed meanings (docs/language.md). Others are 0-255. */
12
+ declare const EXIT: {
13
+ readonly OK: 0;
14
+ readonly FAILURE: 1;
15
+ readonly USAGE: 2;
16
+ readonly NOT_EXECUTABLE: 126;
17
+ readonly NOT_FOUND: 127;
18
+ readonly INTERRUPTED: 130;
19
+ readonly CONNECTION_LOST: 255;
20
+ };
21
+ type TokenKind = "word" | "operator" | "redirect" | "comment" | "whitespace" | "reserved";
22
+ /** [start, end) offsets into the source line, in UTF-16 code units. */
23
+ type Span = readonly [start: number, end: number];
24
+ interface Token {
25
+ kind: TokenKind;
26
+ text: string;
27
+ span: Span;
28
+ }
29
+ /** One piece of a word, before expansion. */
30
+ type WordPart = {
31
+ kind: "literal";
32
+ text: string;
33
+ quoted: boolean;
34
+ } | {
35
+ kind: "param";
36
+ name: string;
37
+ quoted: boolean;
38
+ } | {
39
+ kind: "tilde";
40
+ text: string;
41
+ } | {
42
+ kind: "glob";
43
+ text: string;
44
+ };
45
+ interface Word {
46
+ parts: WordPart[];
47
+ span: Span;
48
+ }
49
+ type RedirectOp = ">" | ">>" | "<" | "2>" | "2>>" | "2>&1" | ">&2";
50
+ interface Redirect {
51
+ op: RedirectOp;
52
+ /** Absent for the duplication forms 2>&1 and >&2. */
53
+ target?: Word;
54
+ span: Span;
55
+ }
56
+ interface Assignment {
57
+ name: string;
58
+ value: Word;
59
+ span: Span;
60
+ }
61
+ interface SimpleCommand {
62
+ type: "command";
63
+ assignments: Assignment[];
64
+ words: Word[];
65
+ redirects: Redirect[];
66
+ span: Span;
67
+ }
68
+ interface Pipeline {
69
+ type: "pipeline";
70
+ commands: SimpleCommand[];
71
+ span: Span;
72
+ }
73
+ interface List {
74
+ type: "list";
75
+ /** First pipeline, then (operator, pipeline) pairs, left-associative. */
76
+ first: Pipeline;
77
+ rest: Array<{
78
+ op: ";" | "&&" | "||";
79
+ pipeline: Pipeline;
80
+ }>;
81
+ span: Span;
82
+ }
83
+ type ParseResult = {
84
+ ok: true;
85
+ ast: List | null;
86
+ tokens: Token[];
87
+ } | {
88
+ ok: false;
89
+ incomplete: true;
90
+ tokens: Token[];
91
+ } | {
92
+ ok: false;
93
+ incomplete: false;
94
+ error: SyntaxErrorInfo;
95
+ tokens: Token[];
96
+ };
97
+ interface SyntaxErrorInfo {
98
+ message: string;
99
+ span: Span;
100
+ }
101
+ interface ParseOptions {
102
+ /** Returns an alias body for a name in command position, if defined. */
103
+ alias?: (name: string) => string | undefined;
104
+ }
105
+ interface Parser {
106
+ parse(line: string, options?: ParseOptions): ParseResult;
107
+ /** Tokens only, for highlighting. Never throws. */
108
+ tokenize(line: string): Token[];
109
+ /** Quote a value so it parses back as one literal word. */
110
+ quoteWord(value: string, context: "bare" | "double" | "single"): string;
111
+ /** The version string printed in "not supported in rite <version>". */
112
+ readonly version: string;
113
+ }
114
+ type InodeType = "dir" | "file" | "cmd" | "link" | "action" | "dev";
115
+ interface Stat {
116
+ type: InodeType;
117
+ /** Permission bits, e.g. 0o755. */
118
+ mode: number;
119
+ uid: number;
120
+ gid: number;
121
+ size: number;
122
+ /** Milliseconds since the epoch. */
123
+ mtime: number;
124
+ /** Set when the path is inside a mount. */
125
+ mount?: string;
126
+ }
127
+ interface Entry {
128
+ name: string;
129
+ type: InodeType;
130
+ }
131
+ type ActionDef = {
132
+ navigate: string;
133
+ } | {
134
+ open: string;
135
+ } | {
136
+ event: string;
137
+ detail?: unknown;
138
+ } | {
139
+ print: string;
140
+ };
141
+ type ActionVia = "cd" | "open" | "cat";
142
+ interface ActionEvent {
143
+ kind: "navigate" | "open" | "event" | "print";
144
+ path: string;
145
+ via: ActionVia;
146
+ href?: string;
147
+ url?: string;
148
+ name?: string;
149
+ detail?: unknown;
150
+ /** The host calls this to stop rite's default handling. */
151
+ preventDefault(): void;
152
+ readonly defaultPrevented: boolean;
153
+ }
154
+ interface ActionResult {
155
+ event: ActionEvent;
156
+ /** Text the command prints (print actions, or the URL when a popup is blocked). */
157
+ output?: string;
158
+ }
159
+ type FileContent = string | (() => string | Promise<string>);
160
+ type InodeSpec = {
161
+ type: "dir";
162
+ mode?: number;
163
+ uid?: number;
164
+ } | {
165
+ type: "file";
166
+ content: FileContent;
167
+ mode?: number;
168
+ uid?: number;
169
+ } | {
170
+ type: "cmd";
171
+ command: CommandDef;
172
+ mode?: number;
173
+ } | {
174
+ type: "link";
175
+ target: string;
176
+ } | {
177
+ type: "action";
178
+ action: ActionDef;
179
+ mode?: number;
180
+ } | {
181
+ type: "dev";
182
+ read?: () => string;
183
+ write?: (data: string) => void;
184
+ mode?: number;
185
+ };
186
+ /** What the editor may read synchronously from the vfs cache. */
187
+ interface Inode {
188
+ type: InodeType;
189
+ mode: number;
190
+ uid: number;
191
+ /** Present for cmd inodes. */
192
+ command?: CommandInfo;
193
+ }
194
+ interface CallOptions {
195
+ signal?: AbortSignal;
196
+ }
197
+ interface WriteOptions extends CallOptions {
198
+ mode: "truncate" | "append";
199
+ }
200
+ /**
201
+ * Paths given to the vfs are absolute or relative to `cwd`. Tilde is a
202
+ * kernel expansion and never reaches the vfs. `..` resolves lexically.
203
+ */
204
+ interface Vfs {
205
+ stat(path: string, cwd: string, o?: CallOptions): Promise<Stat>;
206
+ list(path: string, cwd: string, o?: CallOptions): Promise<Entry[]>;
207
+ read(path: string, cwd: string, o?: CallOptions): Promise<string>;
208
+ write(path: string, cwd: string, data: string, o: WriteOptions): Promise<void>;
209
+ mkdir(path: string, cwd: string, o?: CallOptions & {
210
+ parents?: boolean;
211
+ }): Promise<void>;
212
+ remove(path: string, cwd: string, o?: CallOptions & {
213
+ recursive?: boolean;
214
+ }): Promise<void>;
215
+ /** Only cd, open, and cat call this. stat and list never trigger actions. */
216
+ activate(path: string, cwd: string, via: ActionVia): Promise<ActionResult | null>;
217
+ /** Absolute, normalized path. Does not check existence. */
218
+ resolve(path: string, cwd: string): string;
219
+ /** Cache only, synchronous, never does I/O. The editor uses only this. */
220
+ peek(path: string, cwd: string): Inode | undefined;
221
+ /** Create or replace an inode (host setup and kernel use). */
222
+ put(path: string, spec: InodeSpec): void;
223
+ mount(path: string, provider: MountProvider): void;
224
+ unmount(path: string): void;
225
+ /** Permission check for the given uid: r, w, or x. Root bypasses. */
226
+ access(path: string, cwd: string, uid: number, want: "r" | "w" | "x"): Promise<boolean>;
227
+ }
228
+ /** A subtree served by a host object or a remote server. Paths are relative to the mount root, "" is the root. */
229
+ interface MountProvider {
230
+ readonly readonly: boolean;
231
+ stat(rel: string, o: CallOptions): Promise<Stat>;
232
+ list(rel: string, o: CallOptions): Promise<Entry[]>;
233
+ read(rel: string, o: CallOptions): Promise<string>;
234
+ write?(rel: string, data: string, o: WriteOptions): Promise<void>;
235
+ mkdir?(rel: string, o: CallOptions): Promise<void>;
236
+ remove?(rel: string, o: CallOptions): Promise<void>;
237
+ /** Called with a relative path when something under the mount changes. Returns an unsubscribe. */
238
+ watch?(onChange: (rel: string) => void): () => void;
239
+ }
240
+ type Env = Record<string, string>;
241
+ interface Tty {
242
+ readonly cols: number;
243
+ readonly rows: number;
244
+ /** Read one line from the terminal. echo:false for passwords. Never recorded in history. */
245
+ read(options?: {
246
+ echo?: boolean;
247
+ prompt?: string;
248
+ signal?: AbortSignal;
249
+ }): Promise<string | null>;
250
+ /** Unfiltered output. Local commands only (clear, egg animations). */
251
+ raw(data: string): Promise<void>;
252
+ }
253
+ type StdioKind = "tty" | "pipe" | "file" | "null";
254
+ interface Stdio {
255
+ stdin: StdioKind;
256
+ stdout: StdioKind;
257
+ stderr: StdioKind;
258
+ }
259
+ type FlagValue = boolean | string | number | Array<string | number>;
260
+ type Flags = Record<string, FlagValue | undefined>;
261
+ interface Proc {
262
+ /** argv[0] is the name the command was invoked by. */
263
+ readonly argv: readonly string[];
264
+ /** Parsed flags, keyed by the long name when one exists, else the short name. */
265
+ readonly flags: Flags;
266
+ /** Operands after flag parsing; argv.slice(1) when the command declares no flags. */
267
+ readonly args: readonly string[];
268
+ /** The resolved subcommand path, e.g. ["devices", "ls"]. Empty when none. */
269
+ readonly subcommand: readonly string[];
270
+ readonly stdin: ReadableStream<string>;
271
+ readonly stdout: WritableStream<string>;
272
+ readonly stderr: WritableStream<string>;
273
+ /** Write to stdout or stderr; resolves when the sink accepted the chunk. Commands write "\n". */
274
+ out(text: string): Promise<void>;
275
+ err(text: string): Promise<void>;
276
+ /** stdin split into lines, without terminators. */
277
+ lines(): AsyncIterable<string>;
278
+ readonly env: Readonly<Env>;
279
+ readonly cwd: string;
280
+ readonly uid: number;
281
+ readonly fs: Vfs;
282
+ /** Aborted on ^C (reason "INT") or when a downstream reader exits (reason "EPIPE"). */
283
+ readonly signal: AbortSignal;
284
+ /** Null when stdout is not the terminal. */
285
+ readonly tty: Tty | null;
286
+ readonly stdio: Stdio;
287
+ readonly sys: Sys;
288
+ }
289
+ /** A command's body. void means exit 0. A throw is exit 1 with one stderr line. */
290
+ type Run = (p: Proc) => Promise<number | void>;
291
+ type Resolution = {
292
+ kind: "alias";
293
+ name: string;
294
+ body: string;
295
+ } | {
296
+ kind: "builtin";
297
+ name: string;
298
+ info: CommandInfo;
299
+ } | {
300
+ kind: "command";
301
+ name: string;
302
+ path: string;
303
+ info: CommandInfo;
304
+ } | {
305
+ kind: "egg";
306
+ name: string;
307
+ id: string;
308
+ } | {
309
+ kind: "not-found";
310
+ name: string;
311
+ };
312
+ interface SessionView {
313
+ readonly user: string;
314
+ readonly uid: number;
315
+ readonly cwd: string;
316
+ readonly hostname: string;
317
+ readonly home: string;
318
+ readonly lastStatus: number;
319
+ }
320
+ /** The kernel facade a command may use. */
321
+ interface Sys {
322
+ resolve(name: string): Resolution;
323
+ commands(): CommandInfo[];
324
+ /** Run argv as a child. stdio defaults to the caller's. */
325
+ spawn(argv: readonly string[], options?: {
326
+ uid?: number;
327
+ env?: Env;
328
+ stdio?: Partial<StdioStreams>;
329
+ }): Promise<number>;
330
+ /** Run a full line (rite -c, source). */
331
+ run(line: string, options?: {
332
+ env?: Env;
333
+ }): Promise<number>;
334
+ readonly session: SessionView;
335
+ /** Host configuration for sudo. */
336
+ readonly sudo: SudoPolicy;
337
+ history(): readonly string[];
338
+ }
339
+ interface StdioStreams {
340
+ stdin: ReadableStream<string>;
341
+ stdout: WritableStream<string>;
342
+ stderr: WritableStream<string>;
343
+ }
344
+ type SudoPolicy = boolean | ((password: string) => boolean | Promise<boolean>);
345
+ /** Builtins also receive the mutable session. In a pipeline the changes do not persist. */
346
+ interface Session {
347
+ user: string;
348
+ uid: number;
349
+ cwd: string;
350
+ oldCwd: string;
351
+ hostname: string;
352
+ home: string;
353
+ env: Env;
354
+ /** Names exported to child processes. */
355
+ exported: Set<string>;
356
+ aliases: Map<string, string>;
357
+ lastStatus: number;
358
+ /** Set by the exit builtin. */
359
+ exitRequested: number | null;
360
+ }
361
+ type BuiltinRun = (p: Proc, session: Session) => Promise<number | void>;
362
+ interface FlagDef {
363
+ long?: string;
364
+ short?: string;
365
+ type: "boolean" | "string" | "number";
366
+ /** Repeatable flags collect into an array. */
367
+ repeat?: boolean;
368
+ default?: FlagValue;
369
+ description?: string;
370
+ }
371
+ interface Candidate {
372
+ value: string;
373
+ display?: string;
374
+ description?: string;
375
+ /** Appended after insertion: " " by default, "/" for directories, "" to keep typing. */
376
+ suffix?: string;
377
+ }
378
+ interface CompleteContext {
379
+ argv: readonly string[];
380
+ /** Index into argv of the word being completed. */
381
+ index: number;
382
+ prefix: string;
383
+ env: Readonly<Env>;
384
+ cwd: string;
385
+ fs: Vfs;
386
+ signal: AbortSignal;
387
+ }
388
+ type Completer = (ctx: CompleteContext) => Promise<Candidate[]>;
389
+ interface CommandDef {
390
+ name: string;
391
+ summary: string;
392
+ usage: string;
393
+ /** Keyed by the canonical name (long when present). Omit to receive raw argv. */
394
+ flags?: Record<string, FlagDef>;
395
+ subcommands?: Record<string, Omit<CommandDef, "name">>;
396
+ complete?: Completer;
397
+ run?: Run;
398
+ /** Longer text for man. */
399
+ manual?: string;
400
+ }
401
+ interface CommandInfo {
402
+ name: string;
403
+ summary: string;
404
+ usage: string;
405
+ /** Longer text for man, when the definition has one. */
406
+ manual?: string;
407
+ flags?: Record<string, FlagDef>;
408
+ subcommands?: Record<string, {
409
+ summary: string;
410
+ usage: string;
411
+ }>;
412
+ builtin: boolean;
413
+ remote: boolean;
414
+ }
415
+ /** Supplies commands from outside the vfs tree (the remote client). */
416
+ interface CommandSource {
417
+ /** Names known before connecting, so completion and which work early. */
418
+ names(): string[];
419
+ info(name: string): CommandInfo | undefined;
420
+ run(name: string, p: Proc): Promise<number>;
421
+ complete?(argv: readonly string[], index: number, signal: AbortSignal): Promise<Candidate[]>;
422
+ }
423
+ declare const PASS: unique symbol;
424
+ type EggResult = number | void | typeof PASS;
425
+ type EggTrigger = {
426
+ command: string;
427
+ when?: (argv: readonly string[], session: SessionView) => boolean;
428
+ } | {
429
+ input: RegExp;
430
+ } | {
431
+ keys: string;
432
+ };
433
+ interface EggDef {
434
+ id: string;
435
+ trigger: EggTrigger;
436
+ run: (p: Proc) => Promise<EggResult>;
437
+ }
438
+ interface Completion {
439
+ /** The span of the line the candidates replace. */
440
+ span: Span;
441
+ candidates: Candidate[];
442
+ }
443
+ type OutputPolicy = "plain" | "sgr" | "raw";
444
+ interface TermSize {
445
+ cols: number;
446
+ rows: number;
447
+ }
448
+ /** The only boundary to xterm. The editor renders through it; tests back it with @xterm/headless. */
449
+ interface Term {
450
+ /**
451
+ * Write complete text with a policy; resolves once xterm has parsed it.
452
+ * "\n" becomes "\r\n". Each call is filtered on its own: an escape
453
+ * sequence cut off at the end of the data is dropped.
454
+ */
455
+ write(data: string, policy: OutputPolicy): Promise<void>;
456
+ /**
457
+ * A writer for one stream (a process's stdout): its filter keeps an escape
458
+ * sequence split across chunks until it completes, apart from every other
459
+ * writer.
460
+ */
461
+ writer(policy: OutputPolicy): (chunk: string) => Promise<void>;
462
+ /** Key input, already decoded into strings, terminal replies discarded. Returns an unsubscribe. */
463
+ onInput(listener: (data: string) => void): () => void;
464
+ onResize(listener: (size: TermSize) => void): () => void;
465
+ readonly size: TermSize;
466
+ /** Replace the terminal's colors (xterm ITheme keys). */
467
+ setTheme(theme: Theme): void;
468
+ /** Display width of one code point: 0, 1, or 2. */
469
+ width(codePoint: number): 0 | 1 | 2;
470
+ focus(): void;
471
+ fit(): void;
472
+ dispose(): void;
473
+ }
474
+ interface Kernel {
475
+ readonly session: SessionView;
476
+ /** Run one line as if typed. Queued behind any running job. */
477
+ exec(line: string, stdio?: Partial<StdioStreams>): Promise<number>;
478
+ /** Run one egg directly (a key trigger), queued like a line. */
479
+ runEgg(egg: EggDef): Promise<number>;
480
+ complete(line: string, cursor: number, o: {
481
+ signal: AbortSignal;
482
+ }): Promise<Completion>;
483
+ /** Abort the foreground job (^C). */
484
+ interrupt(): void;
485
+ readonly running: boolean;
486
+ register(def: CommandDef, at?: string): void;
487
+ registerEgg(def: EggDef): void;
488
+ /** Eggs with key triggers, for the editor. */
489
+ keyEggs(): readonly EggDef[];
490
+ addCommandSource(at: string, source: CommandSource): void;
491
+ readonly vfs: Vfs;
492
+ readonly parser: Parser;
493
+ }
494
+ type HistoryMode = "local" | "memory" | false;
495
+ /** Terminal colors: xterm's ITheme keys (strings, and extendedAnsi as a list). */
496
+ interface Theme {
497
+ [key: string]: string | readonly string[] | undefined;
498
+ extendedAnsi?: readonly string[];
499
+ }
500
+ interface HostOptions {
501
+ element: HTMLElement;
502
+ hostname?: string;
503
+ user?: string;
504
+ /** Printed before the first prompt; SGR colors are kept, every other escape sequence is dropped. */
505
+ motd?: string;
506
+ files?: Record<string, FileContent>;
507
+ inodes?: Record<string, InodeSpec>;
508
+ mounts?: Array<{
509
+ path: string;
510
+ provider: MountProvider;
511
+ }>;
512
+ commands?: CommandDef[];
513
+ eggs?: EggDef[] | false;
514
+ remote?: RemoteHandle;
515
+ sudo?: SudoPolicy;
516
+ history?: HistoryMode;
517
+ /** xterm ITheme keys; `themes` has brass, tide, dusk (dark) and paper (light) ready-made. */
518
+ theme?: Theme;
519
+ /** The terminal's font. Load web fonts before createRite so the cell size is measured right. */
520
+ font?: {
521
+ family?: string;
522
+ size?: number;
523
+ };
524
+ screenReaderMode?: boolean;
525
+ onAction?: (e: ActionEvent) => void;
526
+ }
527
+ /** What rite/remote hands to createRite. Core never imports the remote module. */
528
+ interface RemoteHandle {
529
+ readonly at: string;
530
+ readonly bin: string;
531
+ readonly provider: MountProvider;
532
+ readonly commands: CommandSource;
533
+ connect(): Promise<void>;
534
+ /** The terminal's size changed: tell the server (tty.resize) if connected. */
535
+ resize(cols: number, rows: number): void;
536
+ dispose(): void;
537
+ onNotify(listener: (text: string, level: "info" | "warn" | "error") => void): () => void;
538
+ }
539
+ interface Rite {
540
+ readonly ready: Promise<void>;
541
+ exec(line: string): Promise<{
542
+ code: number;
543
+ }>;
544
+ print(text: string): void;
545
+ focus(): void;
546
+ fit(): void;
547
+ /** The terminal's size in cells. */
548
+ readonly size: TermSize;
549
+ /** Called after the terminal changes size (a fit, a resize). Returns an unsubscribe. */
550
+ onResize(listener: (size: TermSize) => void): () => void;
551
+ /** Replace the terminal's colors, such as `themes.paper`. */
552
+ setTheme(theme: Theme): void;
553
+ dispose(): void;
554
+ }
555
+ export { Span as $, Inode as A, Parser as B, Errno as C, Flags as D, FlagValue as E, MountProvider as F, RemoteHandle as G, Proc as H, OutputPolicy as I, RiteError as J, Resolution as K, PASS as L, InodeType as M, Kernel as N, HistoryMode as O, List as P, SimpleCommand as Q, ParseOptions as R, Env as S, FlagDef as T, Redirect as U, Pipeline as V, RedirectOp as W, Session as X, Run as Y, SessionView as Z, EXIT as _, Assignment as a, SyntaxErrorInfo as at, EggTrigger as b, Candidate as c, TermSize as ct, CommandSource as d, TokenKind as dt, Stat as et, CompleteContext as f, Tty as ft, ERRNO_MESSAGES as g, WriteOptions as gt, ERRNOS as h, WordPart as ht, ActionVia as i, SudoPolicy as it, InodeSpec as j, HostOptions as k, CommandDef as l, Theme as lt, Completion as m, Word as mt, ActionEvent as n, StdioKind as nt, BuiltinRun as o, Sys as ot, Completer as p, Vfs as pt, Rite as q, ActionResult as r, StdioStreams as rt, CallOptions as s, Term as st, ActionDef as t, Stdio as tt, CommandInfo as u, Token as ut, EggDef as v, FileContent as w, Entry as x, EggResult as y, ParseResult as z };
@@ -0,0 +1,65 @@
1
+ /*! ritesh | MIT | Copyright (c) 2026 Jarrod Baumann | bundles xterm.js (MIT, see THIRD_PARTY_NOTICES) */
2
+ // src/contracts.ts
3
+ const ERRNOS = [
4
+ "ENOENT",
5
+ "ENOTDIR",
6
+ "EISDIR",
7
+ "EACCES",
8
+ "EPERM",
9
+ "EEXIST",
10
+ "ENOTEMPTY",
11
+ "EROFS",
12
+ "ELOOP",
13
+ "EINVAL",
14
+ "ENOTSUP",
15
+ "ENOSPC",
16
+ "EIO",
17
+ "EPIPE",
18
+ "ECANCELED",
19
+ "ETIMEDOUT",
20
+ "ENOTCONN"
21
+ ];
22
+ const ERRNO_MESSAGES = {
23
+ ENOENT: "no such file or directory",
24
+ ENOTDIR: "not a directory",
25
+ EISDIR: "is a directory",
26
+ EACCES: "permission denied",
27
+ EPERM: "operation not permitted",
28
+ EEXIST: "file exists",
29
+ ENOTEMPTY: "directory not empty",
30
+ EROFS: "read-only file system",
31
+ ELOOP: "too many levels of symbolic links",
32
+ EINVAL: "invalid argument",
33
+ ENOTSUP: "operation not supported",
34
+ ENOSPC: "no space left on device",
35
+ EIO: "input/output error",
36
+ EPIPE: "broken pipe",
37
+ ECANCELED: "operation canceled",
38
+ ETIMEDOUT: "operation timed out",
39
+ ENOTCONN: "transport endpoint is not connected"
40
+ };
41
+ /** The one error type that crosses module boundaries. */
42
+ var RiteError = class extends Error {
43
+ errno;
44
+ path;
45
+ constructor(errno, path, message) {
46
+ super(message ?? ERRNO_MESSAGES[errno]);
47
+ this.name = "RiteError";
48
+ this.errno = errno;
49
+ this.path = path;
50
+ }
51
+ };
52
+ /** Exit codes with fixed meanings (docs/language.md). Others are 0-255. */
53
+ const EXIT = {
54
+ OK: 0,
55
+ FAILURE: 1,
56
+ USAGE: 2,
57
+ NOT_EXECUTABLE: 126,
58
+ NOT_FOUND: 127,
59
+ INTERRUPTED: 130,
60
+ CONNECTION_LOST: 255
61
+ };
62
+ const PASS = Symbol("rite.pass");
63
+ export { RiteError as a, PASS as i, ERRNO_MESSAGES as n, EXIT as r, ERRNOS as t };
64
+
65
+ //# sourceMappingURL=contracts-CFeF8xY8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contracts-CFeF8xY8.js","names":[],"sources":["../src/contracts.ts"],"sourcesContent":["// Cross-module contracts. Every module talks to every other through these\n// types. They are frozen for each implementation wave: a unit that finds a\n// gap reports it instead of editing this file.\n\n// ---------------------------------------------------------------- errors\n\nexport const ERRNOS = [\n \"ENOENT\", \"ENOTDIR\", \"EISDIR\", \"EACCES\", \"EPERM\", \"EEXIST\", \"ENOTEMPTY\",\n \"EROFS\", \"ELOOP\", \"EINVAL\", \"ENOTSUP\", \"ENOSPC\", \"EIO\", \"EPIPE\",\n \"ECANCELED\", \"ETIMEDOUT\", \"ENOTCONN\",\n] as const\nexport type Errno = (typeof ERRNOS)[number]\n\nexport const ERRNO_MESSAGES: Record<Errno, string> = {\n ENOENT: \"no such file or directory\",\n ENOTDIR: \"not a directory\",\n EISDIR: \"is a directory\",\n EACCES: \"permission denied\",\n EPERM: \"operation not permitted\",\n EEXIST: \"file exists\",\n ENOTEMPTY: \"directory not empty\",\n EROFS: \"read-only file system\",\n ELOOP: \"too many levels of symbolic links\",\n EINVAL: \"invalid argument\",\n ENOTSUP: \"operation not supported\",\n ENOSPC: \"no space left on device\",\n EIO: \"input/output error\",\n EPIPE: \"broken pipe\",\n ECANCELED: \"operation canceled\",\n ETIMEDOUT: \"operation timed out\",\n ENOTCONN: \"transport endpoint is not connected\",\n}\n\n/** The one error type that crosses module boundaries. */\nexport class RiteError extends Error {\n readonly errno: Errno\n readonly path: string | undefined\n constructor(errno: Errno, path?: string, message?: string) {\n super(message ?? ERRNO_MESSAGES[errno])\n this.name = \"RiteError\"\n this.errno = errno\n this.path = path\n }\n}\n\n/** Exit codes with fixed meanings (docs/language.md). Others are 0-255. */\nexport const EXIT = {\n OK: 0,\n FAILURE: 1,\n USAGE: 2,\n NOT_EXECUTABLE: 126,\n NOT_FOUND: 127,\n INTERRUPTED: 130,\n CONNECTION_LOST: 255,\n} as const\n\n// ---------------------------------------------------------------- parser\n\nexport type TokenKind =\n | \"word\" | \"operator\" | \"redirect\" | \"comment\" | \"whitespace\" | \"reserved\"\n\n/** [start, end) offsets into the source line, in UTF-16 code units. */\nexport type Span = readonly [start: number, end: number]\n\nexport interface Token {\n kind: TokenKind\n text: string\n span: Span\n}\n\n/** One piece of a word, before expansion. */\nexport type WordPart =\n | { kind: \"literal\"; text: string; quoted: boolean }\n | { kind: \"param\"; name: string; quoted: boolean }\n | { kind: \"tilde\"; text: string }\n | { kind: \"glob\"; text: string }\n\nexport interface Word {\n parts: WordPart[]\n span: Span\n}\n\nexport type RedirectOp = \">\" | \">>\" | \"<\" | \"2>\" | \"2>>\" | \"2>&1\" | \">&2\"\n\nexport interface Redirect {\n op: RedirectOp\n /** Absent for the duplication forms 2>&1 and >&2. */\n target?: Word\n span: Span\n}\n\nexport interface Assignment {\n name: string\n value: Word\n span: Span\n}\n\nexport interface SimpleCommand {\n type: \"command\"\n assignments: Assignment[]\n words: Word[]\n redirects: Redirect[]\n span: Span\n}\n\nexport interface Pipeline {\n type: \"pipeline\"\n commands: SimpleCommand[]\n span: Span\n}\n\nexport interface List {\n type: \"list\"\n /** First pipeline, then (operator, pipeline) pairs, left-associative. */\n first: Pipeline\n rest: Array<{ op: \";\" | \"&&\" | \"||\"; pipeline: Pipeline }>\n span: Span\n}\n\nexport type ParseResult =\n | { ok: true; ast: List | null; tokens: Token[] }\n | { ok: false; incomplete: true; tokens: Token[] }\n | { ok: false; incomplete: false; error: SyntaxErrorInfo; tokens: Token[] }\n\nexport interface SyntaxErrorInfo {\n message: string\n span: Span\n}\n\nexport interface ParseOptions {\n /** Returns an alias body for a name in command position, if defined. */\n alias?: (name: string) => string | undefined\n}\n\nexport interface Parser {\n parse(line: string, options?: ParseOptions): ParseResult\n /** Tokens only, for highlighting. Never throws. */\n tokenize(line: string): Token[]\n /** Quote a value so it parses back as one literal word. */\n quoteWord(value: string, context: \"bare\" | \"double\" | \"single\"): string\n /** The version string printed in \"not supported in rite <version>\". */\n readonly version: string\n}\n\n// ---------------------------------------------------------------- vfs\n\nexport type InodeType = \"dir\" | \"file\" | \"cmd\" | \"link\" | \"action\" | \"dev\"\n\nexport interface Stat {\n type: InodeType\n /** Permission bits, e.g. 0o755. */\n mode: number\n uid: number\n gid: number\n size: number\n /** Milliseconds since the epoch. */\n mtime: number\n /** Set when the path is inside a mount. */\n mount?: string\n}\n\nexport interface Entry {\n name: string\n type: InodeType\n}\n\nexport type ActionDef =\n | { navigate: string }\n | { open: string }\n | { event: string; detail?: unknown }\n | { print: string }\n\nexport type ActionVia = \"cd\" | \"open\" | \"cat\"\n\nexport interface ActionEvent {\n kind: \"navigate\" | \"open\" | \"event\" | \"print\"\n path: string\n via: ActionVia\n href?: string\n url?: string\n name?: string\n detail?: unknown\n /** The host calls this to stop rite's default handling. */\n preventDefault(): void\n readonly defaultPrevented: boolean\n}\n\nexport interface ActionResult {\n event: ActionEvent\n /** Text the command prints (print actions, or the URL when a popup is blocked). */\n output?: string\n}\n\nexport type FileContent = string | (() => string | Promise<string>)\n\nexport type InodeSpec =\n | { type: \"dir\"; mode?: number; uid?: number }\n | { type: \"file\"; content: FileContent; mode?: number; uid?: number }\n | { type: \"cmd\"; command: CommandDef; mode?: number }\n | { type: \"link\"; target: string }\n | { type: \"action\"; action: ActionDef; mode?: number }\n | { type: \"dev\"; read?: () => string; write?: (data: string) => void; mode?: number }\n\n/** What the editor may read synchronously from the vfs cache. */\nexport interface Inode {\n type: InodeType\n mode: number\n uid: number\n /** Present for cmd inodes. */\n command?: CommandInfo\n}\n\nexport interface CallOptions {\n signal?: AbortSignal\n}\n\nexport interface WriteOptions extends CallOptions {\n mode: \"truncate\" | \"append\"\n}\n\n/**\n * Paths given to the vfs are absolute or relative to `cwd`. Tilde is a\n * kernel expansion and never reaches the vfs. `..` resolves lexically.\n */\nexport interface Vfs {\n stat(path: string, cwd: string, o?: CallOptions): Promise<Stat>\n list(path: string, cwd: string, o?: CallOptions): Promise<Entry[]>\n read(path: string, cwd: string, o?: CallOptions): Promise<string>\n write(path: string, cwd: string, data: string, o: WriteOptions): Promise<void>\n mkdir(path: string, cwd: string, o?: CallOptions & { parents?: boolean }): Promise<void>\n remove(path: string, cwd: string, o?: CallOptions & { recursive?: boolean }): Promise<void>\n /** Only cd, open, and cat call this. stat and list never trigger actions. */\n activate(path: string, cwd: string, via: ActionVia): Promise<ActionResult | null>\n /** Absolute, normalized path. Does not check existence. */\n resolve(path: string, cwd: string): string\n /** Cache only, synchronous, never does I/O. The editor uses only this. */\n peek(path: string, cwd: string): Inode | undefined\n /** Create or replace an inode (host setup and kernel use). */\n put(path: string, spec: InodeSpec): void\n mount(path: string, provider: MountProvider): void\n unmount(path: string): void\n /** Permission check for the given uid: r, w, or x. Root bypasses. */\n access(path: string, cwd: string, uid: number, want: \"r\" | \"w\" | \"x\"): Promise<boolean>\n}\n\n/** A subtree served by a host object or a remote server. Paths are relative to the mount root, \"\" is the root. */\nexport interface MountProvider {\n readonly readonly: boolean\n stat(rel: string, o: CallOptions): Promise<Stat>\n list(rel: string, o: CallOptions): Promise<Entry[]>\n read(rel: string, o: CallOptions): Promise<string>\n write?(rel: string, data: string, o: WriteOptions): Promise<void>\n mkdir?(rel: string, o: CallOptions): Promise<void>\n remove?(rel: string, o: CallOptions): Promise<void>\n /** Called with a relative path when something under the mount changes. Returns an unsubscribe. */\n watch?(onChange: (rel: string) => void): () => void\n}\n\n// ---------------------------------------------------------------- processes\n\nexport type Env = Record<string, string>\n\nexport interface Tty {\n readonly cols: number\n readonly rows: number\n /** Read one line from the terminal. echo:false for passwords. Never recorded in history. */\n read(options?: { echo?: boolean; prompt?: string; signal?: AbortSignal }): Promise<string | null>\n /** Unfiltered output. Local commands only (clear, egg animations). */\n raw(data: string): Promise<void>\n}\n\nexport type StdioKind = \"tty\" | \"pipe\" | \"file\" | \"null\"\n\nexport interface Stdio {\n stdin: StdioKind\n stdout: StdioKind\n stderr: StdioKind\n}\n\nexport type FlagValue = boolean | string | number | Array<string | number>\nexport type Flags = Record<string, FlagValue | undefined>\n\nexport interface Proc {\n /** argv[0] is the name the command was invoked by. */\n readonly argv: readonly string[]\n /** Parsed flags, keyed by the long name when one exists, else the short name. */\n readonly flags: Flags\n /** Operands after flag parsing; argv.slice(1) when the command declares no flags. */\n readonly args: readonly string[]\n /** The resolved subcommand path, e.g. [\"devices\", \"ls\"]. Empty when none. */\n readonly subcommand: readonly string[]\n readonly stdin: ReadableStream<string>\n readonly stdout: WritableStream<string>\n readonly stderr: WritableStream<string>\n /** Write to stdout or stderr; resolves when the sink accepted the chunk. Commands write \"\\n\". */\n out(text: string): Promise<void>\n err(text: string): Promise<void>\n /** stdin split into lines, without terminators. */\n lines(): AsyncIterable<string>\n readonly env: Readonly<Env>\n readonly cwd: string\n readonly uid: number\n readonly fs: Vfs\n /** Aborted on ^C (reason \"INT\") or when a downstream reader exits (reason \"EPIPE\"). */\n readonly signal: AbortSignal\n /** Null when stdout is not the terminal. */\n readonly tty: Tty | null\n readonly stdio: Stdio\n readonly sys: Sys\n}\n\n/** A command's body. void means exit 0. A throw is exit 1 with one stderr line. */\nexport type Run = (p: Proc) => Promise<number | void>\n\nexport type Resolution =\n | { kind: \"alias\"; name: string; body: string }\n | { kind: \"builtin\"; name: string; info: CommandInfo }\n | { kind: \"command\"; name: string; path: string; info: CommandInfo }\n | { kind: \"egg\"; name: string; id: string }\n | { kind: \"not-found\"; name: string }\n\nexport interface SessionView {\n readonly user: string\n readonly uid: number\n readonly cwd: string\n readonly hostname: string\n readonly home: string\n readonly lastStatus: number\n}\n\n/** The kernel facade a command may use. */\nexport interface Sys {\n resolve(name: string): Resolution\n commands(): CommandInfo[]\n /** Run argv as a child. stdio defaults to the caller's. */\n spawn(argv: readonly string[], options?: { uid?: number; env?: Env; stdio?: Partial<StdioStreams> }): Promise<number>\n /** Run a full line (rite -c, source). */\n run(line: string, options?: { env?: Env }): Promise<number>\n readonly session: SessionView\n /** Host configuration for sudo. */\n readonly sudo: SudoPolicy\n history(): readonly string[]\n}\n\nexport interface StdioStreams {\n stdin: ReadableStream<string>\n stdout: WritableStream<string>\n stderr: WritableStream<string>\n}\n\nexport type SudoPolicy = boolean | ((password: string) => boolean | Promise<boolean>)\n\n/** Builtins also receive the mutable session. In a pipeline the changes do not persist. */\nexport interface Session {\n user: string\n uid: number\n cwd: string\n oldCwd: string\n hostname: string\n home: string\n env: Env\n /** Names exported to child processes. */\n exported: Set<string>\n aliases: Map<string, string>\n lastStatus: number\n /** Set by the exit builtin. */\n exitRequested: number | null\n}\n\nexport type BuiltinRun = (p: Proc, session: Session) => Promise<number | void>\n\n// ---------------------------------------------------------------- commands\n\nexport interface FlagDef {\n long?: string\n short?: string\n type: \"boolean\" | \"string\" | \"number\"\n /** Repeatable flags collect into an array. */\n repeat?: boolean\n default?: FlagValue\n description?: string\n}\n\nexport interface Candidate {\n value: string\n display?: string\n description?: string\n /** Appended after insertion: \" \" by default, \"/\" for directories, \"\" to keep typing. */\n suffix?: string\n}\n\nexport interface CompleteContext {\n argv: readonly string[]\n /** Index into argv of the word being completed. */\n index: number\n prefix: string\n env: Readonly<Env>\n cwd: string\n fs: Vfs\n signal: AbortSignal\n}\n\nexport type Completer = (ctx: CompleteContext) => Promise<Candidate[]>\n\nexport interface CommandDef {\n name: string\n summary: string\n usage: string\n /** Keyed by the canonical name (long when present). Omit to receive raw argv. */\n flags?: Record<string, FlagDef>\n subcommands?: Record<string, Omit<CommandDef, \"name\">>\n complete?: Completer\n run?: Run\n /** Longer text for man. */\n manual?: string\n}\n\nexport interface CommandInfo {\n name: string\n summary: string\n usage: string\n /** Longer text for man, when the definition has one. */\n manual?: string\n flags?: Record<string, FlagDef>\n subcommands?: Record<string, { summary: string; usage: string }>\n builtin: boolean\n remote: boolean\n}\n\n/** Supplies commands from outside the vfs tree (the remote client). */\nexport interface CommandSource {\n /** Names known before connecting, so completion and which work early. */\n names(): string[]\n info(name: string): CommandInfo | undefined\n run(name: string, p: Proc): Promise<number>\n complete?(argv: readonly string[], index: number, signal: AbortSignal): Promise<Candidate[]>\n}\n\n// ---------------------------------------------------------------- eggs\n\nexport const PASS: unique symbol = Symbol(\"rite.pass\")\nexport type EggResult = number | void | typeof PASS\n\nexport type EggTrigger =\n | { command: string; when?: (argv: readonly string[], session: SessionView) => boolean }\n | { input: RegExp }\n | { keys: string }\n\nexport interface EggDef {\n id: string\n trigger: EggTrigger\n run: (p: Proc) => Promise<EggResult>\n}\n\n// ---------------------------------------------------------------- completion\n\nexport interface Completion {\n /** The span of the line the candidates replace. */\n span: Span\n candidates: Candidate[]\n}\n\n// ---------------------------------------------------------------- terminal\n\nexport type OutputPolicy = \"plain\" | \"sgr\" | \"raw\"\n\nexport interface TermSize {\n cols: number\n rows: number\n}\n\n/** The only boundary to xterm. The editor renders through it; tests back it with @xterm/headless. */\nexport interface Term {\n /**\n * Write complete text with a policy; resolves once xterm has parsed it.\n * \"\\n\" becomes \"\\r\\n\". Each call is filtered on its own: an escape\n * sequence cut off at the end of the data is dropped.\n */\n write(data: string, policy: OutputPolicy): Promise<void>\n /**\n * A writer for one stream (a process's stdout): its filter keeps an escape\n * sequence split across chunks until it completes, apart from every other\n * writer.\n */\n writer(policy: OutputPolicy): (chunk: string) => Promise<void>\n /** Key input, already decoded into strings, terminal replies discarded. Returns an unsubscribe. */\n onInput(listener: (data: string) => void): () => void\n onResize(listener: (size: TermSize) => void): () => void\n readonly size: TermSize\n /** Replace the terminal's colors (xterm ITheme keys). */\n setTheme(theme: Theme): void\n /** Display width of one code point: 0, 1, or 2. */\n width(codePoint: number): 0 | 1 | 2\n focus(): void\n fit(): void\n dispose(): void\n}\n\n// ---------------------------------------------------------------- kernel\n\nexport interface Kernel {\n readonly session: SessionView\n /** Run one line as if typed. Queued behind any running job. */\n exec(line: string, stdio?: Partial<StdioStreams>): Promise<number>\n /** Run one egg directly (a key trigger), queued like a line. */\n runEgg(egg: EggDef): Promise<number>\n complete(line: string, cursor: number, o: { signal: AbortSignal }): Promise<Completion>\n /** Abort the foreground job (^C). */\n interrupt(): void\n readonly running: boolean\n register(def: CommandDef, at?: string): void\n registerEgg(def: EggDef): void\n /** Eggs with key triggers, for the editor. */\n keyEggs(): readonly EggDef[]\n addCommandSource(at: string, source: CommandSource): void\n readonly vfs: Vfs\n readonly parser: Parser\n}\n\n// ---------------------------------------------------------------- host\n\nexport type HistoryMode = \"local\" | \"memory\" | false\n\n/** Terminal colors: xterm's ITheme keys (strings, and extendedAnsi as a list). */\nexport interface Theme {\n [key: string]: string | readonly string[] | undefined\n extendedAnsi?: readonly string[]\n}\n\nexport interface HostOptions {\n element: HTMLElement\n hostname?: string\n user?: string\n /** Printed before the first prompt; SGR colors are kept, every other escape sequence is dropped. */\n motd?: string\n files?: Record<string, FileContent>\n inodes?: Record<string, InodeSpec>\n mounts?: Array<{ path: string; provider: MountProvider }>\n commands?: CommandDef[]\n eggs?: EggDef[] | false\n remote?: RemoteHandle\n sudo?: SudoPolicy\n history?: HistoryMode\n /** xterm ITheme keys; `themes` has brass, tide, dusk (dark) and paper (light) ready-made. */\n theme?: Theme\n /** The terminal's font. Load web fonts before createRite so the cell size is measured right. */\n font?: { family?: string; size?: number }\n screenReaderMode?: boolean\n onAction?: (e: ActionEvent) => void\n}\n\n/** What rite/remote hands to createRite. Core never imports the remote module. */\nexport interface RemoteHandle {\n readonly at: string\n readonly bin: string\n readonly provider: MountProvider\n readonly commands: CommandSource\n connect(): Promise<void>\n /** The terminal's size changed: tell the server (tty.resize) if connected. */\n resize(cols: number, rows: number): void\n dispose(): void\n onNotify(listener: (text: string, level: \"info\" | \"warn\" | \"error\") => void): () => void\n}\n\nexport interface Rite {\n readonly ready: Promise<void>\n exec(line: string): Promise<{ code: number }>\n print(text: string): void\n focus(): void\n fit(): void\n /** The terminal's size in cells. */\n readonly size: TermSize\n /** Called after the terminal changes size (a fit, a resize). Returns an unsubscribe. */\n onResize(listener: (size: TermSize) => void): () => void\n /** Replace the terminal's colors, such as `themes.paper`. */\n setTheme(theme: Theme): void\n dispose(): void\n}\n"],"mappings":";;AAMA,MAAa,SAAS;CACpB;CAAU;CAAW;CAAU;CAAU;CAAS;CAAU;CAC5D;CAAS;CAAS;CAAU;CAAW;CAAU;CAAO;CACxD;CAAa;CAAa;AAC5B;AAGA,MAAa,iBAAwC;CACnD,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,WAAW;CACX,OAAO;CACP,OAAO;CACP,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,KAAK;CACL,OAAO;CACP,WAAW;CACX,WAAW;CACX,UAAU;AACZ;;AAGA,IAAa,YAAb,cAA+B,MAAM;CAC1B;CACA;CACT,YAAY,OAAc,MAAe,SAAkB;EACzD,MAAM,WAAW,eAAe,MAAM;EACtC,KAAK,OAAO;EACZ,KAAK,QAAQ;EACb,KAAK,OAAO;CACd;AACF;;AAGA,MAAa,OAAO;CAClB,IAAI;CACJ,SAAS;CACT,OAAO;CACP,gBAAgB;CAChB,WAAW;CACX,aAAa;CACb,iBAAiB;AACnB;AAkYA,MAAa,OAAsB,OAAO,WAAW"}
@@ -0,0 +1,6 @@
1
+ /*! ritesh | MIT | Copyright (c) 2026 Jarrod Baumann | bundles xterm.js (MIT, see THIRD_PARTY_NOTICES) */
2
+ import { l as CommandDef, v as EggDef } from "./contracts-C55jYvFz.js";
3
+ /** Identity helpers that give a command or egg definition its type. */
4
+ declare function defineCommand(def: CommandDef): CommandDef;
5
+ declare function defineEgg(def: EggDef): EggDef;
6
+ export { defineEgg as n, defineCommand as t };