@amritk/nish-x86_64-linux 0.36.0 → 0.38.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.
Files changed (103) hide show
  1. package/INSTALL.md +92 -71
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +75 -14
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/std/LICENSE-fdlibm +8 -0
  22. package/std/README.md +109 -82
  23. package/std/array.ts +266 -0
  24. package/std/bytes.ts +23 -19
  25. package/std/compress/checksum.ts +1 -1
  26. package/std/crypto/aes.ts +48 -43
  27. package/std/crypto/base64url.ts +7 -5
  28. package/std/crypto/bcrypt-batch.ts +13 -13
  29. package/std/crypto/bcrypt.ts +33 -27
  30. package/std/crypto/chacha20poly1305.ts +16 -14
  31. package/std/crypto/ed25519.ts +19 -16
  32. package/std/crypto/hmac.ts +7 -6
  33. package/std/crypto/jwt.ts +23 -20
  34. package/std/crypto/p256.ts +63 -56
  35. package/std/crypto/p384.ts +23 -20
  36. package/std/crypto/pki.ts +18 -15
  37. package/std/crypto/rsa.ts +23 -19
  38. package/std/crypto/x25519.ts +11 -10
  39. package/std/crypto/x509.ts +69 -62
  40. package/std/host/ring.ts +124 -0
  41. package/std/host/text.ts +86 -0
  42. package/std/json-equal.ts +12 -11
  43. package/std/json-number.ts +12 -12
  44. package/std/json-tokenizer.ts +26 -22
  45. package/std/json-typed.ts +92 -78
  46. package/std/json-value.ts +38 -33
  47. package/std/json.ts +32 -28
  48. package/std/math.ts +743 -41
  49. package/std/net/dns-resolver.ts +46 -6
  50. package/std/net/dns.ts +50 -13
  51. package/std/net/fetch.ts +1744 -295
  52. package/std/net/handler.ts +30 -0
  53. package/std/net/hpack.ts +29 -26
  54. package/std/net/http-compress.ts +42 -28
  55. package/std/net/http-fields.ts +19 -4
  56. package/std/net/http1-client-codec.ts +29 -25
  57. package/std/net/http1-codec.ts +62 -91
  58. package/std/net/http1-server.ts +79 -57
  59. package/std/net/http2-frame.ts +17 -16
  60. package/std/net/http2-tls.ts +17 -15
  61. package/std/net/http2.ts +67 -54
  62. package/std/net/http3-frame.ts +11 -10
  63. package/std/net/http3-server.ts +36 -35
  64. package/std/net/http3.ts +70 -58
  65. package/std/net/postgres-async.ts +1005 -0
  66. package/std/net/postgres.ts +185 -164
  67. package/std/net/prefork.ts +38 -34
  68. package/std/net/qpack.ts +15 -14
  69. package/std/net/quic-conn-ack.ts +1 -0
  70. package/std/net/quic-conn-cid.ts +11 -0
  71. package/std/net/quic-conn-params.ts +41 -14
  72. package/std/net/quic-datagram.ts +7 -0
  73. package/std/net/quic-frame.ts +48 -12
  74. package/std/net/quic-listener.ts +61 -44
  75. package/std/net/quic-packet.ts +71 -52
  76. package/std/net/quic-recovery.ts +59 -21
  77. package/std/net/quic-stream.ts +83 -55
  78. package/std/net/quic.ts +204 -152
  79. package/std/net/serve.ts +763 -0
  80. package/std/net/tls/client.ts +1 -0
  81. package/std/net/tls/codec.ts +81 -12
  82. package/std/net/tls/record-client.ts +30 -18
  83. package/std/net/tls/record-server.ts +74 -46
  84. package/std/net/tls/record.ts +33 -24
  85. package/std/net/tls/ticket.ts +2 -0
  86. package/std/net/tls/verify.ts +8 -7
  87. package/std/net/tls-tcp.ts +58 -50
  88. package/std/net/tls.ts +55 -43
  89. package/std/net/url.ts +12 -9
  90. package/std/net/websocket.ts +23 -19
  91. package/std/net/webtransport.ts +24 -18
  92. package/std/random.ts +8 -6
  93. package/std/regex.ts +70 -59
  94. package/std/sync/ring.ts +24 -21
  95. package/std/sync/shm.ts +9 -7
  96. package/std/term-io.ts +17 -14
  97. package/std/term-probe.ts +11 -10
  98. package/std/term-rate.ts +71 -61
  99. package/std/term.ts +73 -59
  100. package/std/text.ts +242 -15
  101. package/std/threads.ts +19 -15
  102. package/std/yaml-json.ts +17 -15
  103. package/std/yaml-write.ts +21 -14
package/std/regex.ts CHANGED
@@ -17,51 +17,59 @@
17
17
  * }
18
18
  *
19
19
  * **The syntax is JavaScript's**, with the `u` flag's strictness: literals,
20
- * `.`, classes `[a-z]` and `[^…]`, `\d \w \s` and their negations, `\b \B ^
21
- * $`, groups `(…)`, `(?:…)` and `(?<name>…)`, `|`, and the quantifiers `* + ?
22
- * {n} {n,} {n,m}` with their lazy `?` forms. Escapes are `\n \r \t \f \v \0`,
20
+ * `.`, classes `[a-z]` and `[^…]`, `\d \w \s` and their negations, `\b \B ^ $`,
21
+ * groups `(…)`, `(?:…)` and `(?<name>…)`, `|`, and the quantifiers `* + ? {n}
22
+ * {n,} {n,m}` with their lazy `?` forms. Escapes are `\n \r \t \f \v \0`,
23
23
  * `\cX`, `\xHH`, `\uHHHH`, `\u{H…}` and a backslash before any syntax
24
- * character; a `{`, `}` or `]` that does not belong to a quantifier or a
25
- * class is an error rather than a literal, so write `\{`. `\p{…}` and
26
- * `\P{…}` are Unicode 16.0's properties as JavaScript names them: a
27
- * General_Category value (`L`, `Lu`, `Letter`, `gc=Nd`), `Script=` or
28
- * `Script_Extensions=` a script (`sc=Grek`, `scx=Deva`), or a binary property
29
- * (`Alphabetic`, `Emoji`, `ASCII`, `Any`, `Assigned`). The flags are `i`, `m`
30
- * (`^` and `$` at line terminators), `s` (`.` matches them too) and `u`,
31
- * which changes nothing. `i` folds A-Z to a-z, and above ASCII follows
32
- * Unicode's simple case folding, so `σ` matches `Σ` and `ς` and `[α-ω]` the
33
- * capitals; unlike JavaScript nothing folds across ASCII, so the Kelvin sign
34
- * and `ſ` match no `k` or `s`, though under `i` both are word characters to
35
- * `\w`, `\W`, `\b` and `\B`, as with JavaScript's `iu`. **Backreferences and
36
- * lookaround are refused at compile time**: they are what makes a matcher
37
- * backtrack, and this one never does. A repeat count is at most 1,000, a
38
- * pattern at most 100,000 instructions once its repeats are written out,
39
- * 1,024 capturing groups and 10,000 ranges in one class, and its property
40
- * escapes and case folding add at most 100,000 ranges to its classes.
24
+ * character; a `{`, `}` or `]` that does not belong to a quantifier or a class
25
+ * is an error rather than a literal, so write `\{`.
41
26
  *
42
- * **Matching never backtracks.** A search runs a lazy DFA first: it builds
43
- * its states from the compiled program as the text needs them, so it reads a
44
- * byte per step and the leftmost-first match's end comes from a table lookup.
45
- * A reverse DFA, run back from that end, finds where the match starts, and a
46
- * Pike VM, the construction RE2, Go's `regexp` and Rust's `regex` use, runs
47
- * from that start only when a group's capture is wanted. A pattern with a
48
- * literal no match can avoid, such as `\w+@example`, is searched for by that
49
- * literal and the match grown both ways from it. Every engine follows every
50
- * alternative at once, so a match costs O(pattern × input) at worst and
51
- * `(a+)+$` against a thousand `a`s and a `b` is as quick as `a+$`. Finding
52
- * every match (`findAll`, `count`, `replaceAll`, `split`) is one search per
53
- * match, so it is quadratic when a preferred branch reads far ahead before a
54
- * short one wins: `<[^>]*>|<` over a text of `<`s rereads the rest of the text
55
- * for each, as it does in RE2 and Rust's `regex`. Which match
56
- * is found is the one JavaScript finds — the leftmost, and of those the one
57
- * the first alternative and the greediest quantifier prefer — and a group
58
- * inside a quantifier keeps only its last iteration's capture, as in
59
- * JavaScript. An iteration past a quantifier's minimum that matches empty is
60
- * rejected, as JavaScript rejects it. The VM keeps one thread per
61
- * instruction, position and set of iterations begun, so `(a*?)+?$` on "aa"
62
- * captures "a", as JavaScript does; past three such quantifiers nested in one
63
- * another the deeper ones share their threads, and a capture there can be a
64
- * different iteration's than JavaScript's.
27
+ * `\p{…}` and `\P{…}` are Unicode 16.0's properties as JavaScript names them: a
28
+ * General_Category value (`L`, `Lu`, `Letter`, `gc=Nd`), `Script=` or
29
+ * `Script_Extensions=` a script (`sc=Grek`, `scx=Deva`), or a binary property
30
+ * (`Alphabetic`, `Emoji`, `ASCII`, `Any`, `Assigned`). The flags are `i`, `m`
31
+ * (`^` and `$` at line terminators), `s` (`.` matches them too) and `u`, which
32
+ * changes nothing.
33
+ *
34
+ * `i` folds A-Z to a-z, and above ASCII follows Unicode's simple case folding,
35
+ * so `σ` matches `Σ` and `ς` and `[α-ω]` the capitals; unlike JavaScript
36
+ * nothing folds across ASCII, so the Kelvin sign and `ſ` match no `k` or `s`,
37
+ * though under `i` both are word characters to `\w`, `\W`, `\b` and `\B`, as
38
+ * with JavaScript's `iu`. **Backreferences and lookaround are refused at
39
+ * compile time**: they are what makes a matcher backtrack, and this one never
40
+ * does.
41
+ *
42
+ * A repeat count is at most 1,000, a pattern at most 100,000 instructions once
43
+ * its repeats are written out, 1,024 capturing groups and 10,000 ranges in one
44
+ * class, and its property escapes and case folding add at most 100,000 ranges
45
+ * to its classes.
46
+ *
47
+ * **Matching never backtracks.** A search runs a lazy DFA first: it builds its
48
+ * states from the compiled program as the text needs them, so it reads a byte
49
+ * per step and the leftmost-first match's end comes from a table lookup. A
50
+ * reverse DFA, run back from that end, finds where the match starts, and a Pike
51
+ * VM, the construction RE2, Go's `regexp` and Rust's `regex` use, runs from
52
+ * that start only when a group's capture is wanted.
53
+ *
54
+ * A pattern with a literal no match can avoid, such as `\w+@example`, is
55
+ * searched for by that literal and the match grown both ways from it. Every
56
+ * engine follows every alternative at once, so a match costs O(pattern × input)
57
+ * at worst and `(a+)+$` against a thousand `a`s and a `b` is as quick as `a+$`.
58
+ *
59
+ * Finding every match (`findAll`, `count`, `replaceAll`, `split`) is one search
60
+ * per match, so it is quadratic when a preferred branch reads far ahead before
61
+ * a short one wins: `<[^>]*>|<` over a text of `<`s rereads the rest of the
62
+ * text for each, as it does in RE2 and Rust's `regex`. Which match is found is
63
+ * the one JavaScript finds — the leftmost, and of those the one the first
64
+ * alternative and the greediest quantifier prefer — and a group inside a
65
+ * quantifier keeps only its last iteration's capture, as in JavaScript.
66
+ *
67
+ * An iteration past a quantifier's minimum that matches empty is rejected, as
68
+ * JavaScript rejects it. The VM keeps one thread per instruction, position and
69
+ * set of iterations begun, so `(a*?)+?$` on "aa" captures "a", as JavaScript
70
+ * does; past three such quantifiers nested in one another the deeper ones share
71
+ * their threads, and a capture there can be a different iteration's than
72
+ * JavaScript's.
65
73
  *
66
74
  * **Strings are UTF-8 and offsets are bytes**, like `s.length`. The VM steps
67
75
  * a code point at a time, so `.` and `[^a]` consume a whole character; a byte
@@ -70,24 +78,27 @@
70
78
  * JavaScript; `\s` is JavaScript's whitespace, U+FEFF and U+3000 included.
71
79
  * After an empty match `findAll`, `replaceAll` and `split` step one character.
72
80
  *
73
- * **What a call costs.** The VM's two thread lists, its capture tables and
74
- * its stack are allocated once by `regexCompile`. `test` tracks no captures,
81
+ * **What a call costs.** The VM's two thread lists, its capture tables and its
82
+ * stack are allocated once by `regexCompile`. `test` tracks no captures,
75
83
  * `findAll` and a `replace` whose template names no group track only the
76
84
  * match's bounds, and only `exec`, `find`, `split` and a template with `$1` or
77
- * `$<name>` pay for every group. A pattern that starts with a literal is searched for with
78
- * `s.indexOf`; one whose first few bytes are each one of a set rare enough to
79
- * be worth it, like `[A-Z][a-z]` or `cat|dog`, is searched for those bytes by
80
- * a runtime kernel that tests sixteen or thirty-two starts at a time; any
81
- * other whose first characters are known skips the bytes that cannot start a
82
- * match with a 128-entry table; and a pattern that is nothing but a literal
83
- * never starts an engine at all. The DFA's states are
84
- * built as searches need them and kept, so only a search that meets new
85
- * states allocates, and only up to a bound:
86
- * its table holds at most 2,048 states and 262,144 transitions (1 MiB), and
87
- * a search that fills it clears it and goes on; one that keeps clearing it
88
- * without reading at least 10 bytes per state hands the rest of the text to
89
- * the VM, which is slower but never needs more memory. Because the scratch
90
- * memory lives in the `Regex`, one `Regex` is used by one thread at a time.
85
+ * `$<name>` pay for every group.
86
+ *
87
+ * A pattern that starts with a literal is searched for with `s.indexOf`; one
88
+ * whose first few bytes are each one of a set rare enough to be worth it, like
89
+ * `[A-Z][a-z]` or `cat|dog`, is searched for those bytes by a runtime kernel
90
+ * that tests sixteen or thirty-two starts at a time; any other whose first
91
+ * characters are known skips the bytes that cannot start a match with a
92
+ * 128-entry table; and a pattern that is nothing but a literal never starts an
93
+ * engine at all.
94
+ *
95
+ * The DFA's states are built as searches need them and kept, so only a search
96
+ * that meets new states allocates, and only up to a bound: its table holds at
97
+ * most 2,048 states and 262,144 transitions (1 MiB), and a search that fills it
98
+ * clears it and goes on; one that keeps clearing it without reading at least 10
99
+ * bytes per state hands the rest of the text to the VM, which is slower but
100
+ * never needs more memory. Because the scratch memory lives in the `Regex`, one
101
+ * `Regex` is used by one thread at a time.
91
102
  */
92
103
 
93
104
  import { uncheckedGet } from "nish:unsafe"
package/std/sync/ring.ts CHANGED
@@ -48,22 +48,24 @@
48
48
  * ring.tryPop(buf, 0) // none, or one peek cannot lend
49
49
  * }
50
50
  *
51
- * A window is a `SharedRegion.lend` window: the ring keeps one for its
52
- * producer and one for its consumer and re-points them on every call, so
53
- * neither path allocates or makes a system call. `commit`, `abandon` and
54
- * `release` give the window back, which empties it: a reference kept past
55
- * them indexes nothing (each index panics) until the next `reserve` or
56
- * `peek` re-points it, and then sees that call's record. A reserved record
57
- * is always contiguous: when the rest of the lap is too short, `commit`
58
- * writes a skip marker there (a length word of -1, "the rest of this lap is
59
- * padding") and the record starts at the next lap. Every pop steps over a
60
- * marker. A record `tryPush` wrapped across the end cannot be lent, so `peek`
61
- * answers an empty window for it and `tryPop` takes it, as it does for a
62
- * record of length 0. A reservation is at most `capacity / 2 - 4` bytes, so
63
- * a ring of 8 can never reserve (it panics) and one of 16 allows 4. The
64
- * reserved bytes are not zeroed: `commit` only what was written. A consumer
65
- * that polls `peek` on a doorbell ring has no wait call yet (`popWait` takes
66
- * records, not windows).
51
+ * A window is a `SharedRegion.lend` window: the ring keeps one for its producer
52
+ * and one for its consumer and re-points them on every call, so neither path
53
+ * allocates or makes a system call. `commit`, `abandon` and `release` give the
54
+ * window back, which empties it: a reference kept past them indexes nothing
55
+ * (each index panics) until the next `reserve` or `peek` re-points it, and then
56
+ * sees that call's record.
57
+ *
58
+ * A reserved record is always contiguous: when the rest of the lap is too
59
+ * short, `commit` writes a skip marker there (a length word of -1, "the rest of
60
+ * this lap is padding") and the record starts at the next lap. Every pop steps
61
+ * over a marker.
62
+ *
63
+ * A record `tryPush` wrapped across the end cannot be lent, so `peek` answers
64
+ * an empty window for it and `tryPop` takes it, as it does for a record of
65
+ * length 0. A reservation is at most `capacity / 2 - 4` bytes, so a ring of 8
66
+ * can never reserve (it panics) and one of 16 allows 4. The reserved bytes are
67
+ * not zeroed: `commit` only what was written. A consumer that polls `peek` on a
68
+ * doorbell ring has no wait call yet (`popWait` takes records, not windows).
67
69
  *
68
70
  * **The format keeps its magic.** A ring no producer `reserve`s into is
69
71
  * byte for byte what it was, and a consumer built before the marker that
@@ -84,11 +86,12 @@
84
86
  * the header and sleeps on Linux's futex, and a push rings it only when that
85
87
  * flag is set (one `SEQ_CST` store and load on each side, so neither can miss
86
88
  * the other). A busy ring makes no system calls; an idle one makes two per
87
- * record, the consumer's wait and the producer's wake. Without the doorbell a
88
- * push is a release store and `popWait` answers `SHM_UNSUPPORTED`, as it does
89
- * off Linux, where the consumer polls `tryPop`. `popWait` reads the monotonic
90
- * clock for its deadline, so a program that imports this module reaches the
91
- * `clock` capability as well as `ffi`.
89
+ * record, the consumer's wait and the producer's wake.
90
+ *
91
+ * Without the doorbell a push is a release store and `popWait` answers
92
+ * `SHM_UNSUPPORTED`, as it does off Linux, where the consumer polls `tryPop`.
93
+ * `popWait` reads the monotonic clock for its deadline, so a program that
94
+ * imports this module reaches the `clock` capability as well as `ffi`.
92
95
  *
93
96
  * Private helpers share the importing program's flat symbol namespace
94
97
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
package/std/sync/shm.ts CHANGED
@@ -16,13 +16,15 @@
16
16
  * **A region is a named POSIX shared-memory object** (`/dev/shm/<name>` on
17
17
  * Linux, `shm_open("/<name>")` on Darwin) mapped shared, so every process that
18
18
  * opens the name sees the same bytes. A name is 1 to 30 of `A-Z a-z 0-9 . _ -`,
19
- * not starting with `.`. `createRegion` makes it with mode 0600 and refuses a
20
- * name that exists (`SHM_EXISTS`), so a name another user planted is never
21
- * adopted; `openRegion` refuses a region this user does not own, that anyone
22
- * else may read or write, or (on Linux) that is not a regular file or has a
23
- * second hard link (`SHM_ACCESS`). The name stays until `unlinkRegion`,
24
- * even after every process has exited, which is how a process that starts later
25
- * finds it; the mapping goes away with `close` or with the process.
19
+ * not starting with `.`.
20
+ *
21
+ * `createRegion` makes it with mode 0600 and refuses a name that exists
22
+ * (`SHM_EXISTS`), so a name another user planted is never adopted; `openRegion`
23
+ * refuses a region this user does not own, that anyone else may read or write,
24
+ * or (on Linux) that is not a regular file or has a second hard link
25
+ * (`SHM_ACCESS`). The name stays until `unlinkRegion`, even after every process
26
+ * has exited, which is how a process that starts later finds it; the mapping
27
+ * goes away with `close` or with the process.
26
28
  *
27
29
  * **Why named, and no anonymous region made before `fork`.** A Nish program
28
30
  * has no `fork`: `spawn` and `nish/net/prefork` start a program with `exec`,
package/std/term-io.ts CHANGED
@@ -22,24 +22,27 @@
22
22
  * terminal has: the kitty keyboard protocol (`CSI > 11 u`: key releases and
23
23
  * Ctrl-C as a key), mouse reports (1000, 1002 or 1003, in SGR form 1006, with
24
24
  * pixel coordinates 1016 when the terminal has them), bracketed paste (2004)
25
- * and focus reports (1004). Before each change reaches the terminal, the
26
- * runtime's restore string (`setTerminalRestore`) is set to undo everything —
27
- * cancel any half-written escape (CAN), end synchronized output,
28
- * delete the kitty image, pop the kitty keyboard flags, turn mouse, paste and
29
- * focus reports off, reset the style, show the cursor and leave the alternate
30
- * screen — so a panic or a signal leaves the terminal as it was.
25
+ * and focus reports (1004).
26
+ *
27
+ * Before each change reaches the terminal, the runtime's restore string
28
+ * (`setTerminalRestore`) is set to undo everything — cancel any half-written
29
+ * escape (CAN), end synchronized output, delete the kitty image, pop the kitty
30
+ * keyboard flags, turn mouse, paste and focus reports off, reset the style,
31
+ * show the cursor and leave the alternate screen — so a panic or a signal
32
+ * leaves the terminal as it was.
31
33
  *
32
34
  * `present()` draws the `Screen` and the `Framebuffer`, if the options gave
33
35
  * one, with the best backend the terminal has capped by the controller
34
36
  * (`min(caps.graphics, decision.backendCap)`), at the controller's resolution
35
- * divisor and palette, wrapped in synchronized output when the terminal has
36
- * it, as one write. It follows the frame with a DSR 5 query (`CSI 5 n`),
37
- * whose answer (`CSI 0 n`) acknowledges the frame to the controller and marks
38
- * the input before it as delivered to `KeyState`. No answer from the terminal
39
- * reaches the program: one that arrives after the probe gave up (a slow link)
40
- * is taken, and a cell size among them is learned. `close` reads the
41
- * acknowledgements still in flight before it leaves raw mode, so none is
42
- * echoed onto the shell's line.
37
+ * divisor and palette, wrapped in synchronized output when the terminal has it,
38
+ * as one write. It follows the frame with a DSR 5 query (`CSI 5 n`), whose
39
+ * answer (`CSI 0 n`) acknowledges the frame to the controller and marks the
40
+ * input before it as delivered to `KeyState`.
41
+ *
42
+ * No answer from the terminal reaches the program: one that arrives after the
43
+ * probe gave up (a slow link) is taken, and a cell size among them is learned.
44
+ * `close` reads the acknowledgements still in flight before it leaves raw mode,
45
+ * so none is echoed onto the shell's line.
43
46
  *
44
47
  * A frame's cost to the controller is the program's `update` and `render`
45
48
  * since the last frame plus encoding it, not the write: a write that blocks
package/std/term-probe.ts CHANGED
@@ -63,19 +63,20 @@
63
63
  * "ascii"), `name` and `overrides`; each field's comment says where it
64
64
  * comes from.
65
65
  *
66
- * The environment decides where the terminal cannot answer, and where the
67
- * user says so: `COLORTERM=truecolor` or `24bit` means true colour, as does
68
- * a DECRQSS that reads the 24-bit background back; otherwise a `TERM` with
69
- * `256color` in it means 256 colours, and anything else 16. `TERM=linux` is
70
- * the Linux console: 16 colours, ASCII graphics, no kitty keyboard and no
66
+ * The environment decides where the terminal cannot answer, and where the user
67
+ * says so: `COLORTERM=truecolor` or `24bit` means true colour, as does a
68
+ * DECRQSS that reads the 24-bit background back; otherwise a `TERM` with
69
+ * `256color` in it means 256 colours, and anything else 16. `TERM=linux` is the
70
+ * Linux console: 16 colours, ASCII graphics, no kitty keyboard and no
71
71
  * synchronized output, whatever it answered. `NO_COLOR` set to anything
72
72
  * (no-color.org) means 16 colours and touches nothing but colour.
73
+ *
73
74
  * `NISH_TERM_GRAPHICS` names a graphics backend to use instead of the one the
74
- * answers pick; a pixel backend the terminal did not answer for is refused
75
- * when it would come out as garbage or nothing — on the Linux console, under
76
- * a multiplexer, or when the terminal answered nothing at all. `overrides`
77
- * says what each variable did, refusals included. The match is exact:
78
- * `kitty`, `sixel`, `halfblock` or `ascii`, lower case, nothing around it.
75
+ * answers pick; a pixel backend the terminal did not answer for is refused when
76
+ * it would come out as garbage or nothing — on the Linux console, under a
77
+ * multiplexer, or when the terminal answered nothing at all. `overrides` says
78
+ * what each variable did, refusals included. The match is exact: `kitty`,
79
+ * `sixel`, `halfblock` or `ascii`, lower case, nothing around it.
79
80
  *
80
81
  * Under a multiplexer the probe sees the multiplexer first. tmux and screen
81
82
  * answer DA1 themselves, so `done()` comes on their answer, before any reply
package/std/term-rate.ts CHANGED
@@ -24,12 +24,14 @@
24
24
  * **The acknowledgement.** A frame is acknowledged when the terminal answers a
25
25
  * status query sent right after it: the terminal answers in order, so the
26
26
  * answer means every byte before it has been read and drawn. The query to send
27
- * is DSR 5, `CSI 5 n`, answered `CSI 0 n`. It is preferred over the cursor
28
- * position report (DSR 6, `CSI 6 n`, answered `CSI row ; col R`) because its
29
- * answer is one fixed string, and because a cursor report is the same bytes as
30
- * F3 with modifiers in the legacy key encoding (`CSI 1 ; mods R`), which a key
31
- * decoder would have to guess between. Two relays answer the query from their
32
- * own emulator, not from the terminal at the end of the link:
27
+ * is DSR 5, `CSI 5 n`, answered `CSI 0 n`.
28
+ *
29
+ * It is preferred over the cursor position report (DSR 6, `CSI 6 n`, answered
30
+ * `CSI row ; col R`) because its answer is one fixed string, and because a
31
+ * cursor report is the same bytes as F3 with modifiers in the legacy key
32
+ * encoding (`CSI 1 ; mods R`), which a key decoder would have to guess between.
33
+ * Two relays answer the query from their own emulator, not from the terminal at
34
+ * the end of the link:
33
35
  *
34
36
  * - tmux (commit 939e8e65) answers in `input.c` lines 1737–1747, the
35
37
  * `INPUT_CSI_DSR` case, which replies `\033[0n` to 5 and the position to 6
@@ -50,62 +52,70 @@
50
52
  * **What is measured.** Four running averages (exponentially weighted, so
51
53
  * recent frames count most and none is stored): the round trip from a frame's
52
54
  * send to its acknowledgement (weight 1/8, as TCP smooths its round trip, so
53
- * one late answer moves it little); the bytes a second the link delivered;
54
- * the cost to build a frame and the bytes in one (weight 1/4 each, so a
55
- * change of scene shows within a few frames).
56
- * The bandwidth and the latency are kept apart, so neither can take the
57
- * other's share of a round trip. A frame sent before the previous answer, and
58
- * answered more than the latency after it, kept the link busy from that answer
59
- * to its own: its bytes over that time measure the bandwidth. A frame sent
60
- * onto an idle link makes a trip, kept with the last 16 and the probe's:
61
- * against the quickest kept trip, one twice its bytes or half, 2 ms or more
62
- * apart, measures the bandwidth from the difference, which the latency is no
63
- * part of.
64
- * Both kinds smooth the bytes and the time apart (weight 1/4 each), so answers
65
- * arriving in a burst, each taking almost no time, do not read as unbounded
66
- * bandwidth; answers under 2 ms apart measure nothing this way. A sample
67
- * counts for the decisions for 5 seconds with no newer one, so a link that
68
- * narrows is not held to its old rate; the bandwidth reported keeps it until
69
- * a newer sample, which starts the average afresh, replaces it, or three
70
- * trips in a row take over twice what it and the quickest trip kept when
71
- * it was taken predict. The figure
72
- * is at least the most of the last 16 answers' bytes over their whole round
73
- * trips, since the bytes took no longer than that, so a link that widens shows on its first quick answer; but that
74
- * bound counts the latency as bytes' time, so alone it never makes the
75
- * bandwidth known to the decisions. The base latency is the least, over the
76
- * kept trips, of the round trip less the trip's bytes at the bandwidth: the
77
- * probe's round trip (`probeRttMs`: the status query alone is a few bytes, so
78
- * its round trip is the latency) counts from the start, the frames' once the
79
- * bandwidth is known. The kept trips roll over, so a changed route shows
80
- * within 16 trips. With frames of one size sent one at a time onto
81
- * an idle link and no probe the latency and the bytes cannot be told apart:
82
- * the bandwidth is then not known, its load counts as 0, and the trip load
83
- * governs on the frame's measured round trip: a cut is taken to shorten none
84
- * of it and a restore to lengthen all of it, so detail is neither cut for a
85
- * gain nor restored at a cost that may not be there.
86
- * Acknowledgements match sent frames oldest first. A frame due while the
87
- * unacknowledged limit is reached is dropped and counted; one not answered
88
- * within `rateAckTimeoutMs` plus four round trips is given up and counted as
89
- * lost, so a lost answer
90
- * stalls drawing for seconds, not for ever. Its answer may still come late, and
91
- * would otherwise be matched to the next frame, so after a give-up no frame is
92
- * drawn until as many answers have arrived, or one more timeout has passed
93
- * without them. A time, cost or size that is not a finite number, or is
94
- * negative, is not measured, and an empty frame says nothing about bandwidth.
55
+ * one late answer moves it little); the bytes a second the link delivered; the
56
+ * cost to build a frame and the bytes in one (weight 1/4 each, so a change of
57
+ * scene shows within a few frames). The bandwidth and the latency are kept
58
+ * apart, so neither can take the other's share of a round trip.
59
+ *
60
+ * A frame sent before the previous answer, and answered more than the latency
61
+ * after it, kept the link busy from that answer to its own: its bytes over that
62
+ * time measure the bandwidth. A frame sent onto an idle link makes a trip, kept
63
+ * with the last 16 and the probe's: against the quickest kept trip, one twice
64
+ * its bytes or half, 2 ms or more apart, measures the bandwidth from the
65
+ * difference, which the latency is no part of.
66
+ *
67
+ * Both kinds smooth the bytes and the time apart (weight 1/4 each), so answers
68
+ * arriving in a burst, each taking almost no time, do not read as unbounded
69
+ * bandwidth; answers under 2 ms apart measure nothing this way.
70
+ *
71
+ * A sample counts for the decisions for 5 seconds with no newer one, so a link
72
+ * that narrows is not held to its old rate; the bandwidth reported keeps it
73
+ * until a newer sample, which starts the average afresh, replaces it, or three
74
+ * trips in a row take over twice what it and the quickest trip kept when it was
75
+ * taken predict.
76
+ *
77
+ * The figure is at least the most of the last 16 answers' bytes over their
78
+ * whole round trips, since the bytes took no longer than that, so a link that
79
+ * widens shows on its first quick answer; but that bound counts the latency as
80
+ * bytes' time, so alone it never makes the bandwidth known to the decisions.
81
+ *
82
+ * The base latency is the least, over the kept trips, of the round trip less
83
+ * the trip's bytes at the bandwidth: the probe's round trip (`probeRttMs`: the
84
+ * status query alone is a few bytes, so its round trip is the latency) counts
85
+ * from the start, the frames' once the bandwidth is known. The kept trips roll
86
+ * over, so a changed route shows within 16 trips.
87
+ *
88
+ * With frames of one size sent one at a time onto an idle link and no probe the
89
+ * latency and the bytes cannot be told apart: the bandwidth is then not known,
90
+ * its load counts as 0, and the trip load governs on the frame's measured round
91
+ * trip: a cut is taken to shorten none of it and a restore to lengthen all of
92
+ * it, so detail is neither cut for a gain nor restored at a cost that may not
93
+ * be there. Acknowledgements match sent frames oldest first.
94
+ *
95
+ * A frame due while the unacknowledged limit is reached is dropped and counted;
96
+ * one not answered within `rateAckTimeoutMs` plus four round trips is given up
97
+ * and counted as lost, so a lost answer stalls drawing for seconds, not for
98
+ * ever. Its answer may still come late, and would otherwise be matched to the
99
+ * next frame, so after a give-up no frame is drawn until as many answers have
100
+ * arrived, or one more timeout has passed without them. A time, cost or size
101
+ * that is not a finite number, or is negative, is not measured, and an empty
102
+ * frame says nothing about bandwidth.
103
+ *
104
+ * **What is decided.**
105
+ *
106
+ * The frame rate is a tier: 120 (only with `allow120` and a `maxFps` of 120),
107
+ * 60, 30, 20, 15, 10 or 5. A tier fits when three loads are all at most 1: the
108
+ * frame cost over 70% of the frame time, the bytes a second the tier needs over
109
+ * 70% of the bytes a second the link delivers, and the tier's frames times the
110
+ * round trip over 70% of the time the unacknowledged limit allows (a link that
111
+ * answers in 100 ms carries at most ten frames a second one at a time).
95
112
  *
96
- * **What is decided.** The frame rate is a tier: 120 (only with `allow120` and
97
- * a `maxFps` of 120), 60, 30, 20, 15, 10 or 5. A tier fits when three loads
98
- * are all at most 1: the frame cost over 70% of the frame time, the bytes a
99
- * second the tier needs over 70% of the bytes a second the link delivers, and
100
- * the tier's frames times the round trip over 70% of the time the
101
- * unacknowledged limit allows (a link that answers in 100 ms carries at most
102
- * ten frames a second one at a time). The 30% left over absorbs a slow frame
103
- * without a dropped one. The rate falls to the best fitting tier after half a
104
- * second continuously over, and rises one tier after three seconds
105
- * continuously under the next tier's loads with a fifth to spare; after any
106
- * change it holds for a second, so the averages see the new rate before the
107
- * next decision. Falling fast and rising slowly keeps a flickering link from
108
- * flipping the rate.
113
+ * The 30% left over absorbs a slow frame without a dropped one. The rate falls
114
+ * to the best fitting tier after half a second continuously over, and rises one
115
+ * tier after three seconds continuously under the next tier's loads with a
116
+ * fifth to spare; after any change it holds for a second, so the averages see
117
+ * the new rate before the next decision. Falling fast and rising slowly keeps a
118
+ * flickering link from flipping the rate.
109
119
  *
110
120
  * It starts at the best tier on a local terminal: the probe answered under
111
121
  * 5 ms, or no `SSH_*` hint and no slow answer. Falling costs half a second and