nish 0.20.0 → 0.22.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 (81) hide show
  1. package/README.md +23 -59
  2. package/docs/AI.md +74 -9
  3. package/docs/INSTALL.md +52 -10
  4. package/llms.txt +1 -1
  5. package/package.json +20 -5
  6. package/runtime/browser.d.ts +6 -0
  7. package/runtime/browser.mjs +77 -0
  8. package/runtime/core/arrays.d.ts +36 -0
  9. package/runtime/core/arrays.mjs +153 -0
  10. package/runtime/core/checks.mjs +20 -0
  11. package/runtime/core/host.d.ts +80 -0
  12. package/runtime/core/host.mjs +325 -0
  13. package/runtime/core/numbers.d.ts +66 -0
  14. package/runtime/core/numbers.mjs +343 -0
  15. package/runtime/core/result.d.ts +45 -0
  16. package/runtime/core/result.mjs +140 -0
  17. package/runtime/core/strings.d.ts +14 -0
  18. package/runtime/core/strings.mjs +169 -0
  19. package/runtime/core.d.ts +9 -0
  20. package/runtime/core.mjs +55 -0
  21. package/runtime/host.d.ts +6 -0
  22. package/runtime/nish.d.ts +46 -2
  23. package/runtime/nish.h +75 -2
  24. package/runtime/nish.mjs +23 -3
  25. package/runtime/node.mjs +386 -0
  26. package/runtime/runtime-bytes.c +401 -0
  27. package/runtime/runtime-cross.c +1 -1
  28. package/runtime/runtime-host.c +1 -1
  29. package/runtime/runtime-net.c +12 -1
  30. package/runtime/runtime-os.c +1 -1
  31. package/runtime/runtime-parallel.c +1 -1
  32. package/runtime/runtime-simd.c +231 -7
  33. package/runtime/runtime-term.c +504 -0
  34. package/runtime/runtime-wasm.c +0 -11
  35. package/runtime/runtime.c +190 -6
  36. package/runtime/shim.mjs +6 -985
  37. package/runtime/sqlite/LICENSE.md +85 -0
  38. package/runtime/sqlite/README.md +45 -0
  39. package/runtime/sqlite/nish-sqlite.c +359 -0
  40. package/runtime/sqlite/sqlite3.c +271131 -0
  41. package/runtime/sqlite/sqlite3.h +14447 -0
  42. package/scripts/build.sh +311 -59
  43. package/scripts/check-hardening.mjs +202 -0
  44. package/scripts/fuzz.sh +218 -0
  45. package/scripts/gen-json-pow5.mjs +130 -0
  46. package/scripts/platform-package.mjs +3 -1
  47. package/scripts/security-scope.mjs +139 -0
  48. package/scripts/smoke.sh +3 -2
  49. package/std/LICENSE-fast-float +23 -0
  50. package/std/LICENSE-fdlibm +8 -0
  51. package/std/README.md +32 -12
  52. package/std/base64.ts +116 -0
  53. package/std/bytes.ts +256 -0
  54. package/std/cli.ts +344 -0
  55. package/std/collections.ts +6 -5
  56. package/std/crypto/LICENSE-crypt_blowfish +26 -0
  57. package/std/crypto/base64url.ts +125 -0
  58. package/std/crypto/bcrypt.ts +1557 -0
  59. package/std/crypto/hmac.ts +138 -2
  60. package/std/crypto/x509.ts +11 -122
  61. package/std/json-tokenizer.ts +2998 -57
  62. package/std/json-value.ts +22 -3
  63. package/std/json.ts +87 -8
  64. package/std/math.ts +608 -0
  65. package/std/net/http1-server.ts +282 -40
  66. package/std/net/http1.ts +40 -21
  67. package/std/net/http3-server.ts +50 -12
  68. package/std/net/postgres.ts +2294 -0
  69. package/std/net/prefork.ts +23 -3
  70. package/std/net/quic-listener.ts +20 -4
  71. package/std/random.ts +66 -4
  72. package/std/regex.ts +4274 -0
  73. package/std/sqlite.ts +391 -0
  74. package/std/term-io.ts +978 -0
  75. package/std/term-pixels.ts +874 -0
  76. package/std/term-probe.ts +459 -0
  77. package/std/term-rate.ts +993 -0
  78. package/std/term.ts +2453 -0
  79. package/std/text.ts +90 -7
  80. package/std/yaml-lines.ts +141 -0
  81. package/std/yaml.ts +4675 -0
package/std/bytes.ts CHANGED
@@ -48,6 +48,22 @@
48
48
  * the prelude saturates too (`setI32LE(b, 0, -2)` writes zeros), and the
49
49
  * 64-bit functions and `f64` meet its BigInt `u64` and `i64`, which `toU8`
50
50
  * refuses.
51
+ *
52
+ * The module also has the runs a parser walks and the copy it makes of them.
53
+ * `skipBlank`, `skipDigits`, `skipJsonText` and `skipJsonPlain` each answer
54
+ * the first index in a range whose byte is not in their class — JSON's blanks,
55
+ * the ASCII digits, the bytes a JSON string holds as themselves, the bytes a
56
+ * JSON writer copies without an escape — and `copyBytes` copies a
57
+ * window of one array into another, or into itself, as one move:
58
+ *
59
+ * const text: u8[] = [32, 32, 52, 50, 44]; // " 42,"
60
+ * const start = skipBlank(text, 0, 5); // 2
61
+ * const end = skipDigits(text, start, 5); // 4
62
+ *
63
+ * The four runs clamp their range and never panic; `copyBytes` panics on a
64
+ * range outside its array. Natively each is a runtime kernel
65
+ * (docs/LANGUAGE.md, "Byte runs"), and the runs are variable-time: they are
66
+ * for text, never for secret data.
51
67
  */
52
68
 
53
69
  /** The `u16` whose low byte is `b[off]` and high byte `b[off + 1]`. */
@@ -161,3 +177,243 @@ export const setF64LE = (b: u8[], off: i32, v: f64): void => setU64LE(b, off, to
161
177
 
162
178
  /** Writes `v`'s IEEE 754 bits as `setU64BE` does. */
163
179
  export const setF64BE = (b: u8[], off: i32, v: f64): void => setU64BE(b, off, toU64(f64ToBits(v)))
180
+
181
+ // ---- Byte-class runs and the byte copy (WP38 S6a) ----------------------------------------
182
+
183
+ /** JSON's four blanks (RFC 8259 §2): space, tab, newline and carriage return. */
184
+ const BYTES_SPACE: i32 = 32
185
+ const BYTES_TAB: i32 = 9
186
+ const BYTES_NEWLINE: i32 = 10
187
+ const BYTES_CARRIAGE_RETURN: i32 = 13
188
+ /** `0` and `9`. */
189
+ const BYTES_DIGIT_ZERO: i32 = 48
190
+ const BYTES_DIGIT_NINE: i32 = 57
191
+ /** The two printable ASCII bytes a JSON string escapes, `"` and `\`. */
192
+ const BYTES_QUOTE: i32 = 34
193
+ const BYTES_BACKSLASH: i32 = 92
194
+ /** The first byte that is not ASCII, and the continuation bytes' range. */
195
+ const BYTES_NON_ASCII: i32 = 128
196
+ const BYTES_CONTINUATION_TOP: i32 = 191
197
+
198
+ /** `to` clamped into `[0, b.length]`: where a run's scan stops. */
199
+ const bytesRunEnd = (b: u8[], to: i32): i32 => {
200
+ const n: i32 = toI32(b.length)
201
+ if (to < 0) {
202
+ return 0
203
+ }
204
+ return to < n ? to : n
205
+ }
206
+
207
+ /** `from` clamped into `[0, end]`: where a run's scan starts. */
208
+ const bytesRunStart = (from: i32, end: i32): i32 => {
209
+ if (from < 0) {
210
+ return 0
211
+ }
212
+ return from < end ? from : end
213
+ }
214
+
215
+ /**
216
+ * The first index in `[from, to)` whose byte is not one of JSON's blanks —
217
+ * space, tab, newline, carriage return — or `to` when every byte there is
218
+ * one. `to` is clamped to `[0, b.length]` and `from` to `[0, to]`, as
219
+ * `indexOfAny` clamps, so no range panics: one past the end answers the end.
220
+ *
221
+ * Natively the scan is the runtime's `nish_bytes_skip_blank`, which reads 16
222
+ * or 32 bytes at a time (docs/LANGUAGE.md, "Byte runs"). It is
223
+ * **variable-time**: how long it takes says how long the run is, so it is
224
+ * never for secret data.
225
+ */
226
+ export const skipBlank = (b: u8[], from: i32, to: i32): i32 => {
227
+ const end: i32 = bytesRunEnd(b, to)
228
+ return skipBlankWalk(b, bytesRunStart(from, end), end)
229
+ }
230
+
231
+ /** `skipBlank` for the ASCII digits `0` to `9`: the same clamps, the same kernel family, `nish_bytes_skip_digits`. */
232
+ export const skipDigits = (b: u8[], from: i32, to: i32): i32 => {
233
+ const end: i32 = bytesRunEnd(b, to)
234
+ return skipDigitsWalk(b, bytesRunStart(from, end), end)
235
+ }
236
+
237
+ /**
238
+ * `skipBlank` for the bytes a JSON string holds as themselves: printable ASCII
239
+ * other than `"` and `\`, and every well-formed UTF-8 sequence (RFC 3629 §4)
240
+ * that ends before `to`. The run stops at the first quote, backslash or
241
+ * control byte, and at the lead byte of a sequence that is malformed — an
242
+ * overlong form, a surrogate, a code point past U+10FFFF, a stray continuation
243
+ * byte — or cut short by `to`. So a whole JSON string body with no escape is
244
+ * one call, and where it stops is the byte a decoder has to look at. Natively
245
+ * the scan is `nish_bytes_skip_json_text`: blocks of ASCII 16 or 32 bytes at a
246
+ * time, each multi-byte sequence a byte at a time. Variable-time, as
247
+ * `skipBlank` is.
248
+ */
249
+ export const skipJsonText = (b: u8[], from: i32, to: i32): i32 => {
250
+ const end: i32 = bytesRunEnd(b, to)
251
+ return skipJsonTextWalk(b, bytesRunStart(from, end), end)
252
+ }
253
+
254
+ /**
255
+ * The first index in `[from, to)` whose byte a JSON writer cannot copy as
256
+ * itself — a control byte below `0x20`, a quote or a backslash — or `to`, with
257
+ * the range clamped as `skipBlank`'s is. Every byte from `0x20` up is in the
258
+ * run, `0x7f` and the bytes from `0x80` up included, and no UTF-8 is checked:
259
+ * where `skipJsonText` is the reader's run, this is the writer's, which passes
260
+ * the bytes of a string through as they are. Natively the scan is
261
+ * `nish_bytes_skip_json_plain`, 16 bytes at a time. Variable-time, as
262
+ * `skipBlank` is.
263
+ */
264
+ export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
265
+ const end: i32 = bytesRunEnd(b, to)
266
+ return skipJsonPlainWalk(b, bytesRunStart(from, end), end)
267
+ }
268
+
269
+ /**
270
+ * `count` bytes of `src` from `from` written over `dst` from `at`, as one
271
+ * copy: where `dst` and `src` are one array and the two ranges overlap, the
272
+ * bytes land as if every one were read before any was written, as
273
+ * `copyWithin` promises. Where `dst.set(src, at)` copies all of `src`, this
274
+ * copies a window of it, and allocates nothing.
275
+ *
276
+ * A negative `count`, or a range that does not lie inside its array —
277
+ * `0 <= from <= from + count <= src.length` and the same for `at` in `dst` —
278
+ * panics `copyBytes` before any byte is written. Natively the copy is one
279
+ * `llvm.memmove`.
280
+ */
281
+ export const copyBytes = (dst: u8[], at: i32, src: u8[], from: i32, count: i32): void => {
282
+ if (count < 0 || at < 0 || from < 0 || at > toI32(dst.length) - count || from > toI32(src.length) - count) {
283
+ panic("copyBytes: the range lies outside an array")
284
+ }
285
+ copyBytesWalk(dst, at, src, from, count)
286
+ }
287
+
288
+ /**
289
+ * `skipBlank`'s walk, once its range is clamped. This loop and the four
290
+ * below are what run under Node; natively the compiler replaces the one call
291
+ * each public function makes with the runtime kernel (`src/kernels.ts`), as it
292
+ * does `indexOfAny`'s walk, so each loop is its kernel's meaning written in
293
+ * the language. The `i < b.length` test never fails after the clamp; it is
294
+ * written because it is what proves the index.
295
+ */
296
+ const skipBlankWalk = (b: u8[], from: i32, to: i32): i32 => {
297
+ let i: i32 = from
298
+ while (i >= 0 && i < to && i < toI32(b.length)) {
299
+ const c: i32 = toI32(b[i])
300
+ if (c !== BYTES_SPACE && c !== BYTES_TAB && c !== BYTES_NEWLINE && c !== BYTES_CARRIAGE_RETURN) {
301
+ return i
302
+ }
303
+ i += 1
304
+ }
305
+ return i
306
+ }
307
+
308
+ /** `skipDigits`'s walk, once its range is clamped. */
309
+ const skipDigitsWalk = (b: u8[], from: i32, to: i32): i32 => {
310
+ let i: i32 = from
311
+ while (i >= 0 && i < to && i < toI32(b.length)) {
312
+ const c: i32 = toI32(b[i])
313
+ if (c < BYTES_DIGIT_ZERO || c > BYTES_DIGIT_NINE) {
314
+ return i
315
+ }
316
+ i += 1
317
+ }
318
+ return i
319
+ }
320
+
321
+ /**
322
+ * The length of the well-formed UTF-8 sequence whose lead byte is `b[i]` and
323
+ * that ends at or before `to`, or 0: RFC 3629's table, with the narrowed
324
+ * second byte after `E0`, `ED`, `F0` and `F4`.
325
+ */
326
+ const bytesUtf8Length = (b: u8[], i: i32, to: i32): i32 => {
327
+ const lead: i32 = toI32(b[i])
328
+ let low: i32 = BYTES_NON_ASCII
329
+ let high: i32 = BYTES_CONTINUATION_TOP
330
+ let n: i32 = 0
331
+ if (lead >= 0xc2 && lead <= 0xdf) {
332
+ n = 2
333
+ } else if (lead >= 0xe0 && lead <= 0xef) {
334
+ n = 3
335
+ if (lead === 0xe0) {
336
+ low = 0xa0
337
+ }
338
+ if (lead === 0xed) {
339
+ high = 0x9f
340
+ }
341
+ } else if (lead >= 0xf0 && lead <= 0xf4) {
342
+ n = 4
343
+ if (lead === 0xf0) {
344
+ low = 0x90
345
+ }
346
+ if (lead === 0xf4) {
347
+ high = 0x8f
348
+ }
349
+ } else {
350
+ return 0
351
+ }
352
+ if (to - i < n) {
353
+ return 0
354
+ }
355
+ const second: i32 = toI32(b[i + 1])
356
+ if (second < low || second > high) {
357
+ return 0
358
+ }
359
+ let k: i32 = 2
360
+ while (k < n) {
361
+ const c: i32 = toI32(b[i + k])
362
+ if (c < BYTES_NON_ASCII || c > BYTES_CONTINUATION_TOP) {
363
+ return 0
364
+ }
365
+ k += 1
366
+ }
367
+ return n
368
+ }
369
+
370
+ /** `skipJsonText`'s walk, once its range is clamped. */
371
+ const skipJsonTextWalk = (b: u8[], from: i32, to: i32): i32 => {
372
+ let i: i32 = from
373
+ while (i >= 0 && i < to && i < toI32(b.length)) {
374
+ const c: i32 = toI32(b[i])
375
+ if (c >= BYTES_NON_ASCII) {
376
+ const n: i32 = bytesUtf8Length(b, i, to)
377
+ if (n === 0) {
378
+ return i
379
+ }
380
+ i += n
381
+ continue
382
+ }
383
+ if (c < BYTES_SPACE || c === BYTES_QUOTE || c === BYTES_BACKSLASH) {
384
+ return i
385
+ }
386
+ i += 1
387
+ }
388
+ return i
389
+ }
390
+
391
+ /** `skipJsonPlain`'s walk, once its range is clamped. */
392
+ const skipJsonPlainWalk = (b: u8[], from: i32, to: i32): i32 => {
393
+ let i: i32 = from
394
+ while (i >= 0 && i < to && i < toI32(b.length)) {
395
+ const c: i32 = toI32(b[i])
396
+ if (c < BYTES_SPACE || c === BYTES_QUOTE || c === BYTES_BACKSLASH) {
397
+ return i
398
+ }
399
+ i += 1
400
+ }
401
+ return i
402
+ }
403
+
404
+ /** `copyBytes`'s walk, once its ranges are checked: forward, or backward when `dst` is `src` and the bytes move up. */
405
+ const copyBytesWalk = (dst: u8[], at: i32, src: u8[], from: i32, count: i32): void => {
406
+ if (at <= from) {
407
+ let k: i32 = 0
408
+ while (k < count) {
409
+ dst[at + k] = src[from + k]
410
+ k += 1
411
+ }
412
+ return
413
+ }
414
+ let k: i32 = count - 1
415
+ while (k >= 0) {
416
+ dst[at + k] = src[from + k]
417
+ k -= 1
418
+ }
419
+ }
package/std/cli.ts ADDED
@@ -0,0 +1,344 @@
1
+ /**
2
+ * `std/cli` — a command-line tool's help menu: whether a stream gets colour,
3
+ * the escapes that paint it, and the rows a `--help` is made of.
4
+ *
5
+ * import { HelpMenu, agentDriven, styleFor } from "nish/cli";
6
+ *
7
+ * const menu = new HelpMenu(styleFor(1, agentDriven()), "greet", 30, 40);
8
+ * menu.header("1.0.0", "say hello");
9
+ * menu.usage("<name> [flags]", "greet someone");
10
+ * menu.section("Flags");
11
+ * menu.row("--shout", "", "in capitals");
12
+ * menu.row("--times", "<n>", "say it n times");
13
+ * console.log(menu.text());
14
+ *
15
+ * **The terminal's own colours.** A `Style` paints with the eight basic SGR
16
+ * colours, bold and faint, never a fixed RGB or 256-palette tone. Every theme
17
+ * redefines those eight, so the menu comes out in whatever palette the reader
18
+ * chose, and a terminal with no theme set paints them in its built-in
19
+ * defaults. A terminal with no colours at all (`tput colors` says fewer than
20
+ * eight) gets the failsafe: the same structure in bold and underline, which a
21
+ * monochrome terminal still shows. Colour that is forced is the theme's.
22
+ *
23
+ * **Colour is for a person reading a terminal**, and nothing else may see it:
24
+ * a script that captures `--help`, a test that compares it, a pipe into `less`
25
+ * without `-R` all want the plain bytes. So `styleFor` decides per stream, by
26
+ * asking whether that stream is a terminal, and the conventions people already
27
+ * set win over the guess: `NO_COLOR` (https://no-color.org) turns it off, and
28
+ * `CLICOLOR_FORCE` or `FORCE_COLOR` turns it on for a pager that understands
29
+ * escapes. The plain text is the same bytes either way, and a row's padding is
30
+ * counted on the plain text, so the columns line up with or without escapes.
31
+ *
32
+ * **An agent is a reader of its own kind**: it may drive a pseudo-terminal, so
33
+ * the terminal check says yes, but every escape is tokens it pays for and
34
+ * reads past. `agentDriven` says when one is at the keyboard, and `styleFor`
35
+ * is then plain whatever forcing variable its shell inherited.
36
+ *
37
+ * **What it reaches.** `Style` and `HelpMenu` reach no capability. `styleFor`
38
+ * reads the environment and, on the terminal check, forks a shell
39
+ * (`env` and `process.spawn`),
40
+ * and `agentDriven` reads the environment; a program that builds its `Style`
41
+ * by hand reaches neither.
42
+ *
43
+ * **The compiler's own help is built with it**, through a relative import from
44
+ * `src/cli-style.ts`, so this file is compiled by the last release as part of
45
+ * the compiler: it may use only what that release compiles (the rolling
46
+ * freeze, `.claude/selfhost.md`), which is why it asks the shell whether a
47
+ * descriptor is a terminal rather than calling `nish:term`'s `isTerminal`.
48
+ */
49
+
50
+ /** The Control Sequence Introducer every SGR escape opens with. */
51
+ const CSI: string = "\u001b["
52
+
53
+ /** `paletteOf`'s answer: marked up in the theme's colours. */
54
+ const PALETTE_THEME: i32 = 0
55
+
56
+ /** `paletteOf`'s answer: marked up in attributes only, the failsafe. */
57
+ const PALETTE_MONO: i32 = 2
58
+
59
+ /**
60
+ * How one output stream is painted. `on` says whether to mark the text up at
61
+ * all, and `colour` whether the terminal has colours to mark it up with;
62
+ * without them each mark falls back to bold, underline or nothing.
63
+ */
64
+ export class Style {
65
+ on: boolean
66
+ colour: boolean
67
+
68
+ constructor(on: boolean, colour: boolean) {
69
+ this.on = on
70
+ this.colour = colour
71
+ }
72
+
73
+ /**
74
+ * `text` wrapped in the escape for `themed` (an SGR parameter list in the
75
+ * theme's basic colours) or `mono` (attributes only), and a reset after it;
76
+ * `text` unchanged when marking up is off or the chosen list is empty, so a
77
+ * caller never has to ask.
78
+ */
79
+ paint(text: string, themed: string, mono: string): string {
80
+ const code = this.colour ? themed : mono
81
+ if (!this.on || text.length === 0 || code.length === 0) {
82
+ return text
83
+ }
84
+ return `${CSI}${code}m${text}${CSI}0m`
85
+ }
86
+
87
+ /** A section heading: bold, in the theme's magenta. */
88
+ heading(text: string): string {
89
+ return this.paint(text, "1;35", "1")
90
+ }
91
+
92
+ /** The tool's own name where it opens a line. */
93
+ name(text: string): string {
94
+ return this.paint(text, "1;35", "1")
95
+ }
96
+
97
+ /** A flag, or a command a person would type. */
98
+ flag(text: string): string {
99
+ return this.paint(text, "36", "1")
100
+ }
101
+
102
+ /** A placeholder the person fills in: `<file.ts>`, `speed|size`. */
103
+ meta(text: string): string {
104
+ return this.paint(text, "33", "4")
105
+ }
106
+
107
+ /**
108
+ * Text that explains rather than names: descriptions, notes. Faint rather
109
+ * than bright black, which Solarized makes the background; plain without
110
+ * colour, where the bold flags already stand apart from it.
111
+ */
112
+ dim(text: string): string {
113
+ return this.paint(text, "2", "")
114
+ }
115
+ }
116
+
117
+ /** Whether the environment variable `name` is set to something other than empty. */
118
+ const envIsSet = (name: string): boolean => {
119
+ const value = getenv(name)
120
+ return value !== null && value.length > 0
121
+ }
122
+
123
+ /** Whether `value` spells `word`, which is lower case, in any ASCII case. */
124
+ const spells = (value: string, word: string): boolean => {
125
+ if (value.length !== word.length) {
126
+ return false
127
+ }
128
+ // Both bounds, though the lengths match: the checker proves an index in range
129
+ // only for a string its loop condition names.
130
+ for (let i: i32 = 0; i < toI32(value.length) && i < toI32(word.length); i++) {
131
+ let code: i32 = toI32(value.charCodeAt(i))
132
+ if (code >= 65 && code <= 90) {
133
+ code = code + 32
134
+ }
135
+ if (code !== toI32(word.charCodeAt(i))) {
136
+ return false
137
+ }
138
+ }
139
+ return true
140
+ }
141
+
142
+ /** Whether `value` is a no: `0`, `false`, `no` or `off`, in any case. */
143
+ const isNo = (value: string): boolean =>
144
+ value === "0" || spells(value, "false") || spells(value, "no") || spells(value, "off")
145
+
146
+ /** Whether a variable's `value` is set to a no (`isNo`). */
147
+ const saysNo = (value: string | null): boolean => value !== null && isNo(value)
148
+
149
+ /** Whether a variable's `value` is set and says yes: not empty, and not a no. */
150
+ const saysYes = (value: string | null): boolean => value !== null && value.length > 0 && !isNo(value)
151
+
152
+ /** Whether the variable `name` is set to a no (`saysNo`). */
153
+ const envSaysNo = (name: string): boolean => saysNo(getenv(name))
154
+
155
+ /** Whether the variable `name` says yes (`saysYes`). */
156
+ const envSaysYes = (name: string): boolean => saysYes(getenv(name))
157
+
158
+ /**
159
+ * What terminal descriptor `fd` is: `PALETTE_THEME`, `PALETTE_MONO`, or
160
+ * anything else for no terminal, which is plain text. The shell answers: a
161
+ * child inherits the descriptors and the environment, `[ -t 1 ]` is POSIX,
162
+ * and `tput colors` reads the count `TERM`'s terminfo entry declares (`-1` for
163
+ * none). `tput` is run by absolute path, never looked up on `PATH`, so a
164
+ * `PATH` with `.` in it cannot run a `tput` planted in the working directory
165
+ * (`docs/security/cli.md`). When there is no `tput` there, or it does not know
166
+ * `TERM`, the answer is the theme: almost every terminal has colours, and an
167
+ * escape a monochrome one does not know it drops. It costs one fork of the
168
+ * shell and, on a terminal, one of `tput`. Where nothing can be spawned (a
169
+ * WebAssembly build) the answer is `-1`, which is plain text, the safe side.
170
+ */
171
+ const paletteOf = (fd: i32): i32 => {
172
+ const tty = `[ -t ${fd} ] || exit 1; `
173
+ const find = "t=/usr/bin/tput; [ -x $t ] || t=/bin/tput; [ -x $t ] || exit 0; "
174
+ const count = "c=$($t colors 2>/dev/null) || exit 0; case $c in ''|*[!0-9-]*) exit 0;; esac; "
175
+ return toI32(spawnSync(["/bin/sh", "-c", `${tty}${find}${count}[ "$c" -ge 8 ] || exit ${PALETTE_MONO}`]))
176
+ }
177
+
178
+ /** The style for a `paletteOf` answer: the theme, the failsafe, or plain. */
179
+ const styleOfPalette = (palette: i32): Style => {
180
+ if (palette === PALETTE_THEME) {
181
+ return new Style(true, true)
182
+ }
183
+ return new Style(palette === PALETTE_MONO, false)
184
+ }
185
+
186
+ /**
187
+ * Whether an agent, rather than a person, is running the program.
188
+ * `NISH_AGENT` answers for any agent: a no (`envSaysNo`) for no, any other
189
+ * value for yes. Without it, the variables the agents that set one put in
190
+ * their commands' environment (Claude Code's `CLAUDECODE`, Gemini CLI's
191
+ * `GEMINI_CLI`) say yes.
192
+ */
193
+ export const agentDriven = (): boolean => {
194
+ if (envIsSet("NISH_AGENT")) {
195
+ return !envSaysNo("NISH_AGENT")
196
+ }
197
+ return envSaysYes("CLAUDECODE") || envSaysYes("GEMINI_CLI")
198
+ }
199
+
200
+ /**
201
+ * The style for descriptor `fd` (1 or 2), for a reader `agent` says is an
202
+ * agent or not (`agentDriven`, asked once by the caller). `NO_COLOR` wins,
203
+ * then an agent, then `FORCE_COLOR` saying no (`isNo`; Node reads `0` and
204
+ * `false` so), then a forcing variable, then `CLICOLOR` saying no or
205
+ * `TERM=dumb`, and only then the terminal check, where the colours `TERM`
206
+ * declares choose between the theme and the failsafe (`paletteOf`). Forced
207
+ * colour is the theme's, asked of nothing: the reader is a pager or an editor
208
+ * that renders the escapes itself (Emacs's shell sets `TERM=dumb` and paints
209
+ * ANSI colour), so `TERM` does not describe it, and forcing forks nothing.
210
+ */
211
+ export const styleFor = (fd: i32, agent: boolean): Style => {
212
+ const level = getenv("FORCE_COLOR")
213
+ if (envIsSet("NO_COLOR") || agent || saysNo(level)) {
214
+ return new Style(false, false)
215
+ }
216
+ if (saysYes(level) || envSaysYes("CLICOLOR_FORCE")) {
217
+ return new Style(true, true)
218
+ }
219
+ const term = getenv("TERM")
220
+ if (envSaysNo("CLICOLOR") || (term !== null && term === "dumb")) {
221
+ return new Style(false, false)
222
+ }
223
+ return styleOfPalette(paletteOf(fd))
224
+ }
225
+
226
+ /** A run of spaces to cut padding from, as wide as a terminal's usual 80 columns. */
227
+ const SPACE_RUN: string = " "
228
+
229
+ /** `count` spaces, or none when `count` is not positive: a cut of `SPACE_RUN`, built only past its width. */
230
+ const spaces = (count: i32): string => {
231
+ if (count <= 0) {
232
+ return ""
233
+ }
234
+ if (count <= toI32(SPACE_RUN.length)) {
235
+ return SPACE_RUN.substring(0, count)
236
+ }
237
+ const out: string[] = []
238
+ for (let i: i32 = 0; i < count; i++) {
239
+ out.push(" ")
240
+ }
241
+ return out.join("")
242
+ }
243
+
244
+ /**
245
+ * The spaces that take plain text `width` columns wide to `column`, or two
246
+ * when that would leave fewer, so a long flag still keeps apart from its text.
247
+ */
248
+ const padTo = (width: i32, column: i32): string => spaces(width + 2 <= column ? column - width : 2)
249
+
250
+ /**
251
+ * A help menu, built a line at a time and answered whole by `text`. Rows are
252
+ * indented two columns and their descriptions start at `column`; usage lines'
253
+ * descriptions start at `usageColumn`, past the longest command shape. Every
254
+ * width is counted on the plain text, one column a byte, so the columns line
255
+ * up whether or not `style` paints, as long as the text is ASCII.
256
+ */
257
+ export class HelpMenu {
258
+ style: Style
259
+ program: string
260
+ column: i32
261
+ usageColumn: i32
262
+ lines: string[]
263
+ usages: i32
264
+
265
+ constructor(style: Style, program: string, column: i32, usageColumn: i32) {
266
+ this.style = style
267
+ this.program = program
268
+ this.column = column
269
+ this.usageColumn = usageColumn
270
+ this.lines = []
271
+ this.usages = 0
272
+ }
273
+
274
+ /** The opening line, the program's name, `version` and a one-line `summary`, and a blank after it. */
275
+ header(version: string, summary: string): void {
276
+ this.title(version, summary)
277
+ this.lines.push("")
278
+ }
279
+
280
+ /**
281
+ * The opening line alone, without `header`'s blank after it, for a menu that
282
+ * puts something under its title (`line`) before the blank.
283
+ */
284
+ title(version: string, summary: string): void {
285
+ const style = this.style
286
+ this.lines.push(`${style.name(this.program)} ${style.dim(version)} ${style.dim(summary)}`)
287
+ }
288
+
289
+ /**
290
+ * A line exactly as given, painted (or not) by the caller with the menu's
291
+ * `style`: a rule, a banner, a blank. Its width is the caller's to keep.
292
+ */
293
+ line(text: string): void {
294
+ this.lines.push(text)
295
+ }
296
+
297
+ /**
298
+ * A usage line: the command `shape` after the program's name, and what it
299
+ * is for (`description` may be empty). The first opens with `usage:` and
300
+ * the rest are indented under it.
301
+ */
302
+ usage(shape: string, description: string): void {
303
+ const style = this.style
304
+ const lead = this.usages === 0 ? `${style.heading("usage:")} ` : spaces(7)
305
+ const width: i32 = 8 + toI32(this.program.length) + toI32(shape.length)
306
+ const text = description.length > 0 ? `${padTo(width, this.usageColumn)}${style.dim(description)}` : ""
307
+ this.lines.push(`${lead}${style.name(this.program)} ${style.flag(shape)}${text}`)
308
+ this.usages = this.usages + 1
309
+ }
310
+
311
+ /** A section: a blank line, then `title` as a heading. */
312
+ section(title: string): void {
313
+ this.lines.push("")
314
+ this.lines.push(this.style.heading(title))
315
+ }
316
+
317
+ /**
318
+ * One row: the `flag` (or a command), its placeholder `meta` (may be
319
+ * empty), and what it does, the description starting at `column`.
320
+ */
321
+ row(flag: string, meta: string, description: string): void {
322
+ const style = this.style
323
+ const width: i32 = 2 + toI32(flag.length) + (meta.length > 0 ? 1 + toI32(meta.length) : 0)
324
+ const placeholder = meta.length > 0 ? ` ${style.meta(meta)}` : ""
325
+ this.lines.push(
326
+ ` ${style.flag(flag)}${placeholder}${padTo(width, this.column)}${style.dim(description)}`
327
+ )
328
+ }
329
+
330
+ /** An indented line of prose, faint. */
331
+ note(text: string): void {
332
+ this.lines.push(` ${this.style.dim(text)}`)
333
+ }
334
+
335
+ /** An indented line the reader fills in or opens, a path or a value, as a placeholder. */
336
+ value(text: string): void {
337
+ this.lines.push(` ${this.style.meta(text)}`)
338
+ }
339
+
340
+ /** The menu so far, its lines joined by newlines, with no newline after the last. */
341
+ text(): string {
342
+ return this.lines.join("\n")
343
+ }
344
+ }
@@ -53,11 +53,12 @@ const INDEX_CAP: i32 = 16777215
53
53
  const INITIAL_SLOTS: i32 = 8
54
54
 
55
55
  /**
56
- * The key's hash, never 0: FNV-1a over a string's bytes, murmur3's `fmix32`
57
- * for an integer of 32 bits or fewer, `fmix64` folded to 32 bits for a 64-bit
58
- * integer, a float (normalised first, so that -0 and +0, and every NaN, hash
59
- * alike) and a class instance's address (§5.2). FNV, MurmurHash3 and their
60
- * constants are public domain.
56
+ * The key's hash, never 0: SipHash-1-3 of a string's bytes under the process's
57
+ * random key, folded to 32 bits (docs/security/json.md, JSON-13), murmur3's
58
+ * `fmix32` for an integer of 32 bits or fewer, `fmix64` folded to 32 bits for a
59
+ * 64-bit integer, a float (normalised first, so that -0 and +0, and every NaN,
60
+ * hash alike) and a class instance's address (§5.2). SipHash's reference is
61
+ * CC0; MurmurHash3 and its constants are public domain.
61
62
  *
62
63
  * The body is never emitted. Every call is lowered in place, per key type, by
63
64
  * `src/emit-map.ts`; the constant is what the checker and the whole-program
@@ -0,0 +1,26 @@
1
+ crypt_blowfish (https://www.openwall.com/crypt/), version 1.3, crypt_blowfish.c
2
+
3
+ Written by Solar Designer <solar at openwall.com> in 1998-2014.
4
+ No copyright is claimed, and the software is hereby placed in the public
5
+ domain. In case this attempt to disclaim copyright and place the software
6
+ in the public domain is deemed null and void, then the software is
7
+ Copyright (c) 1998-2014 Solar Designer and it is hereby released to the
8
+ general public under the following terms:
9
+
10
+ Redistribution and use in source and binary forms, with or without
11
+ modification, are permitted.
12
+
13
+ There's ABSOLUTELY NO WARRANTY, express or implied.
14
+
15
+ It is my intent that you should be able to use this on your system,
16
+ as part of a software package, or anywhere else to improve security,
17
+ ensure compatibility, or for any other purpose. I would appreciate
18
+ it if you give credit where it is due and keep your modifications in
19
+ the public domain as well, but I don't require that in order to let
20
+ you place this code and any modifications you make under a license
21
+ of your choice.
22
+
23
+ This implementation is fully compatible with OpenBSD's bcrypt.c for prefix
24
+ "$2b$", originally by Niels Provos <provos at citi.umich.edu>, and it uses
25
+ some of his ideas. The password hashing algorithm was designed by David
26
+ Mazieres <dm at lcs.mit.edu>.