nish 0.34.1 → 0.36.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 (127) hide show
  1. package/README.md +7 -6
  2. package/docs/AI.md +86 -16
  3. package/docs/INSTALL.md +22 -13
  4. package/llms.txt +5 -2
  5. package/package.json +15 -5
  6. package/runtime/async.d.mts +49 -0
  7. package/runtime/async.mjs +27 -0
  8. package/runtime/nish.d.ts +4681 -427
  9. package/runtime/nish.h +6 -2
  10. package/runtime/nish.mjs +4 -0
  11. package/runtime/runtime-net.c +22 -0
  12. package/runtime/runtime-shm.c +93 -2
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +108 -13
  16. package/std/README.md +131 -131
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +258 -42
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +80 -9
  28. package/std/crypto/base64url.ts +22 -0
  29. package/std/crypto/bcrypt-batch.ts +88 -21
  30. package/std/crypto/bcrypt.ts +60 -0
  31. package/std/crypto/chacha20poly1305.ts +53 -0
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +47 -0
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +235 -31
  36. package/std/crypto/jwt.ts +192 -63
  37. package/std/crypto/p256.ts +159 -16
  38. package/std/crypto/p384.ts +28 -3
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +81 -15
  41. package/std/crypto/rsa.ts +33 -3
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +152 -14
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +67 -7
  48. package/std/json-equal.ts +19 -0
  49. package/std/json-format.ts +112 -13
  50. package/std/json-number.ts +64 -14
  51. package/std/json-pointer.ts +23 -1
  52. package/std/json-tokenizer.ts +100 -29
  53. package/std/json-typed.ts +713 -66
  54. package/std/json-value.ts +400 -124
  55. package/std/json.ts +138 -10
  56. package/std/map.ts +25 -7
  57. package/std/math.ts +36 -4
  58. package/std/net/buffer-pool.ts +42 -11
  59. package/std/net/dns-config.ts +109 -23
  60. package/std/net/dns-message.ts +62 -7
  61. package/std/net/dns-resolver.ts +247 -53
  62. package/std/net/dns.ts +203 -35
  63. package/std/net/fetch.ts +151 -61
  64. package/std/net/handler.ts +278 -80
  65. package/std/net/hpack.ts +262 -52
  66. package/std/net/http-compress.ts +125 -15
  67. package/std/net/http-fields.ts +85 -9
  68. package/std/net/http1-client-codec.ts +100 -19
  69. package/std/net/http1-codec.ts +297 -77
  70. package/std/net/http1-server.ts +430 -94
  71. package/std/net/http2-frame.ts +272 -35
  72. package/std/net/http2-tls.ts +135 -34
  73. package/std/net/http2.ts +247 -106
  74. package/std/net/http3-frame.ts +154 -17
  75. package/std/net/http3-server.ts +275 -101
  76. package/std/net/http3.ts +307 -115
  77. package/std/net/outbound.ts +37 -3
  78. package/std/net/postgres-carrier.ts +144 -8
  79. package/std/net/postgres.ts +801 -112
  80. package/std/net/prefork.ts +164 -31
  81. package/std/net/qpack.ts +105 -18
  82. package/std/net/quic-conn-ack.ts +59 -12
  83. package/std/net/quic-conn-cid.ts +124 -27
  84. package/std/net/quic-conn-params.ts +70 -10
  85. package/std/net/quic-datagram.ts +43 -3
  86. package/std/net/quic-frame.ts +286 -12
  87. package/std/net/quic-listener.ts +101 -32
  88. package/std/net/quic-packet.ts +302 -21
  89. package/std/net/quic-recovery.ts +264 -52
  90. package/std/net/quic-stream.ts +353 -91
  91. package/std/net/quic.ts +1124 -201
  92. package/std/net/router.ts +87 -40
  93. package/std/net/tls/client.ts +166 -51
  94. package/std/net/tls/codec.ts +413 -56
  95. package/std/net/tls/record-client.ts +110 -55
  96. package/std/net/tls/record-server.ts +159 -59
  97. package/std/net/tls/record.ts +109 -12
  98. package/std/net/tls/schedule.ts +235 -26
  99. package/std/net/tls/ticket.ts +159 -40
  100. package/std/net/tls/verify.ts +49 -3
  101. package/std/net/tls-tcp.ts +250 -45
  102. package/std/net/tls.ts +221 -59
  103. package/std/net/upstream.ts +81 -45
  104. package/std/net/url.ts +97 -13
  105. package/std/net/websocket.ts +122 -12
  106. package/std/net/webtransport.ts +220 -89
  107. package/std/pair.ts +10 -2
  108. package/std/random.ts +34 -9
  109. package/std/regex.ts +352 -198
  110. package/std/region.ts +70 -15
  111. package/std/secret.ts +52 -11
  112. package/std/sort.ts +17 -2
  113. package/std/sqlite.ts +256 -35
  114. package/std/sync/ring.ts +430 -40
  115. package/std/sync/shm.ts +222 -15
  116. package/std/term-io.ts +137 -64
  117. package/std/term-pixels.ts +144 -19
  118. package/std/term-probe.ts +66 -25
  119. package/std/term-rate.ts +128 -57
  120. package/std/term.ts +332 -121
  121. package/std/testing.ts +130 -32
  122. package/std/text.ts +73 -7
  123. package/std/threads.ts +229 -41
  124. package/std/yaml-json.ts +94 -82
  125. package/std/yaml-lines.ts +47 -15
  126. package/std/yaml-write.ts +24 -16
  127. package/std/yaml.ts +764 -384
@@ -4,11 +4,21 @@
4
4
  * half-block cells or ASCII.
5
5
  *
6
6
  * A program draws `0xRRGGBB` pixels into a `Framebuffer` and never thinks about
7
- * the terminal; `std/term-io` picks the backend and calls one encoder per frame.
7
+ * the terminal; `nish/term-io` picks the backend and calls one encoder per frame.
8
8
  * Everything here is pure: no capability, no I/O, and the bytes are the same
9
9
  * natively and under Node, because the kitty and Sixel output is ASCII and the
10
10
  * cell drawers go through `Screen`, which already is.
11
11
  *
12
+ * import { Framebuffer, fitFramebuffer, GRAPHICS_KITTY, kittyFrame, PixelBytes } from "nish/term-pixels"
13
+ *
14
+ * const fb = new Framebuffer(160, 90)
15
+ * fb.fill(0x000020)
16
+ * fb.set(80, 45, 0xffcc00)
17
+ * const rect = fitFramebuffer(fb.width, fb.height, 80, 24, 0, 0, GRAPHICS_KITTY)
18
+ * const out = new PixelBytes(1 << 16)
19
+ * kittyFrame(fb, rect, 1, out)
20
+ * // out.text() is the frame's bytes, for one write
21
+ *
12
22
  * - `Framebuffer(width, height)` holds the pixels, with `set`, `get`, `fill`,
13
23
  * `clear` and `resize`. `scaleFramebuffer(src, divisor, dst)` shrinks one by
14
24
  * an integer divisor, nearest-neighbour, for the refresh controller's
@@ -159,7 +169,12 @@ export class Framebuffer {
159
169
  /** One colour per pixel, `y * width + x`. */
160
170
  pixels: i32[]
161
171
 
162
- /** A black framebuffer of `width × height`; a negative size, or more than 2^31 - 1 pixels, panics. */
172
+ /**
173
+ * A black framebuffer of `width × height`; a negative size, or more than 2^31 - 1 pixels, panics.
174
+ *
175
+ * @param width Pixels across.
176
+ * @param height Pixels down.
177
+ */
163
178
  constructor(width: i32, height: i32) {
164
179
  pixelsCheckSize("Framebuffer", width, height)
165
180
  this.width = width
@@ -168,7 +183,12 @@ export class Framebuffer {
168
183
  this.pixels.fill(0)
169
184
  }
170
185
 
171
- /** Make the framebuffer `width × height` and black; the array is replaced only when the size changes. */
186
+ /**
187
+ * Make the framebuffer `width × height` and black; the array is replaced only when the size changes.
188
+ *
189
+ * @param width The new pixels across; negative panics, as in the constructor.
190
+ * @param height The new pixels down; negative panics, as in the constructor.
191
+ */
172
192
  resize(width: i32, height: i32): void {
173
193
  pixelsCheckSize("Framebuffer.resize", width, height)
174
194
  if (width !== this.width || height !== this.height) {
@@ -179,7 +199,13 @@ export class Framebuffer {
179
199
  this.pixels.fill(0)
180
200
  }
181
201
 
182
- /** Set the pixel at `x`, `y` to `colour`'s low 24 bits; a pixel off the framebuffer is not drawn. */
202
+ /**
203
+ * Set the pixel at `x`, `y` to `colour`'s low 24 bits; a pixel off the framebuffer is not drawn.
204
+ *
205
+ * @param x The pixel column, from 0 at the left.
206
+ * @param y The pixel row, from 0 at the top.
207
+ * @param colour An `0xRRGGBB` colour; the bits above it are dropped.
208
+ */
183
209
  set(x: i32, y: i32, colour: i32): void {
184
210
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
185
211
  return
@@ -187,7 +213,13 @@ export class Framebuffer {
187
213
  this.pixels[y * this.width + x] = colour & 0xffffff
188
214
  }
189
215
 
190
- /** The colour of the pixel at `x`, `y`, or black off the framebuffer. */
216
+ /**
217
+ * The colour of the pixel at `x`, `y`, or black off the framebuffer.
218
+ *
219
+ * @param x The pixel column, from 0 at the left.
220
+ * @param y The pixel row, from 0 at the top.
221
+ * @returns The colour as `0xRRGGBB`; 0, black, off the framebuffer.
222
+ */
191
223
  get(x: i32, y: i32): i32 {
192
224
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
193
225
  return 0
@@ -195,7 +227,11 @@ export class Framebuffer {
195
227
  return this.pixels[y * this.width + x]
196
228
  }
197
229
 
198
- /** Set every pixel to `colour`'s low 24 bits. */
230
+ /**
231
+ * Set every pixel to `colour`'s low 24 bits.
232
+ *
233
+ * @param colour An `0xRRGGBB` colour; the bits above it are dropped.
234
+ */
199
235
  fill(colour: i32): void {
200
236
  this.pixels.fill(colour & 0xffffff)
201
237
  }
@@ -213,6 +249,10 @@ export class Framebuffer {
213
249
  * frame costs a quarter or a sixteenth of the bytes at divisor 2 or 4. A
214
250
  * divisor below 1 panics; 1 copies. `dst` must be another framebuffer than
215
251
  * `src`, which resizing it would blank.
252
+ *
253
+ * @param src The full-size framebuffer, read only.
254
+ * @param divisor How many pixels across and down become one, 1 or more; 2 or 4 for the controller's steps.
255
+ * @param dst Where the shrunk frame goes; resized to fit.
216
256
  */
217
257
  export const scaleFramebuffer = (src: Framebuffer, divisor: i32, dst: Framebuffer): void => {
218
258
  if (divisor < 1) {
@@ -251,7 +291,17 @@ export class PixelRect {
251
291
  /** The backend's pixels per framebuffer pixel across; ASCII's rows are half that down. */
252
292
  scale: f64
253
293
 
254
- /** A rect of the given cells and size; `fitFramebuffer` is how one is usually made. */
294
+ /**
295
+ * A rect of the given cells and size; `fitFramebuffer` is how one is usually made.
296
+ *
297
+ * @param col The first column covered, from 0.
298
+ * @param row The first row covered, from 0.
299
+ * @param cols The columns covered.
300
+ * @param rows The rows covered.
301
+ * @param width The image's width in the backend's pixels.
302
+ * @param height The image's height in the backend's pixels.
303
+ * @param scale The backend's pixels per framebuffer pixel across.
304
+ */
255
305
  constructor(col: i32, row: i32, cols: i32, rows: i32, width: i32, height: i32, scale: f64) {
256
306
  this.col = col
257
307
  this.row = row
@@ -278,6 +328,15 @@ const pixelsCeilDiv = (a: i32, b: i32): i32 => (a === 0 ? 0 : (((a - 1) / b) | 0
278
328
  * and ASCII take a cell as one pixel wide and two tall, the shape of a usual
279
329
  * terminal font, so a square framebuffer stays square. An empty framebuffer or
280
330
  * area answers an empty rect at 0, 0.
331
+ *
332
+ * @param fbW The framebuffer's width in pixels.
333
+ * @param fbH The framebuffer's height in pixels.
334
+ * @param cols The area's width in cells.
335
+ * @param rows The area's height in cells.
336
+ * @param cellW A cell's width in pixels as the terminal reported it, or 0 or less when it did not say.
337
+ * @param cellH A cell's height in pixels likewise; with either at 0 or less, kitty and Sixel take the cell as 8×16.
338
+ * @param backend The `GRAPHICS_` backend that will draw it, which decides what a pixel of the rect is.
339
+ * @returns Where to draw, in the area's cells and the backend's pixels.
281
340
  */
282
341
  export const fitFramebuffer = (
283
342
  fbW: i32,
@@ -342,22 +401,46 @@ export class PixelBytes {
342
401
  bytes: u8[]
343
402
  /** How many bytes are written. */
344
403
  length: i32
345
- /** One kitty chunk's RGB bytes. */
404
+ /**
405
+ * One kitty chunk's RGB bytes.
406
+ * @internal Used by `kittyFrame`.
407
+ */
346
408
  kittyRaw: u8[]
347
- /** The framebuffer column and row each destination pixel samples. */
409
+ /**
410
+ * The framebuffer column and row each destination pixel samples.
411
+ * @internal Used by `sixelFrame`.
412
+ */
348
413
  sampleXs: i32[]
414
+ /** @internal Used by `sixelFrame`. */
349
415
  sampleYs: i32[]
350
- /** Every destination pixel's Sixel register, row by row. */
416
+ /**
417
+ * Every destination pixel's Sixel register, row by row.
418
+ * @internal Used by `sixelFrame`.
419
+ */
351
420
  sixelReg: i32[]
352
- /** Which registers a Sixel frame uses, 1 or 0. */
421
+ /**
422
+ * Which registers a Sixel frame uses, 1 or 0.
423
+ * @internal Used by `sixelFrame`.
424
+ */
353
425
  sixelUsed: u8[]
354
- /** The first and last column of a band each register covers, -1 for none. */
426
+ /**
427
+ * The first and last column of a band each register covers, -1 for none.
428
+ * @internal Used by `sixelFrame`.
429
+ */
355
430
  sixelFirst: i32[]
431
+ /** @internal Used by `sixelFrame`. */
356
432
  sixelLast: i32[]
357
- /** Each register's sixels across a band, all zero between bands. */
433
+ /**
434
+ * Each register's sixels across a band, all zero between bands.
435
+ * @internal Used by `sixelFrame`.
436
+ */
358
437
  sixelBits: i32[]
359
438
 
360
- /** An empty buffer with room for `capacity` bytes before it grows. */
439
+ /**
440
+ * An empty buffer with room for `capacity` bytes before it grows.
441
+ *
442
+ * @param capacity The bytes to make room for at the start; a frame's worth saves growing.
443
+ */
361
444
  constructor(capacity: i32) {
362
445
  this.bytes = new Array<u8>(Math.max(capacity, 16))
363
446
  this.bytes.fill(0)
@@ -381,7 +464,11 @@ export class PixelBytes {
381
464
  this.length = 0
382
465
  }
383
466
 
384
- /** Make room for `n` more bytes, doubling the array as often as that takes. */
467
+ /**
468
+ * Make room for `n` more bytes, doubling the array as often as that takes.
469
+ *
470
+ * @param n How many bytes the caller is about to `put`.
471
+ */
385
472
  reserve(n: i32): void {
386
473
  const need: i32 = this.length + n
387
474
  if (need <= toI32(this.bytes.length)) {
@@ -398,14 +485,22 @@ export class PixelBytes {
398
485
  this.bytes = grown
399
486
  }
400
487
 
401
- /** Append the byte `b`. */
488
+ /**
489
+ * Append the byte `b`.
490
+ *
491
+ * @param b A byte value, 0–255; only the low 8 bits are kept.
492
+ */
402
493
  put(b: i32): void {
403
494
  this.reserve(PIXELS_ONE)
404
495
  this.bytes[this.length] = toU8(b)
405
496
  this.length = this.length + 1
406
497
  }
407
498
 
408
- /** Append `s`, which is ASCII. */
499
+ /**
500
+ * Append `s`, which is ASCII.
501
+ *
502
+ * @param s Text of ASCII characters only, such as a command's keys.
503
+ */
409
504
  putText(s: string): void {
410
505
  const n: i32 = toI32(s.length)
411
506
  this.reserve(n)
@@ -415,7 +510,11 @@ export class PixelBytes {
415
510
  this.length = this.length + n
416
511
  }
417
512
 
418
- /** Append `v`, at least 0, in decimal. */
513
+ /**
514
+ * Append `v`, at least 0, in decimal.
515
+ *
516
+ * @param v A count or size, 0 or more.
517
+ */
419
518
  putDecimal(v: i32): void {
420
519
  if (v >= 10) {
421
520
  this.putDecimal((v / 10) | 0)
@@ -423,7 +522,11 @@ export class PixelBytes {
423
522
  this.put(PIXELS_DIGIT_0 + (v % 10))
424
523
  }
425
524
 
426
- /** What was written, as one string. */
525
+ /**
526
+ * What was written, as one string.
527
+ *
528
+ * @returns The bytes appended since it was made or last `reset`, to `write`.
529
+ */
427
530
  text(): string {
428
531
  return String.fromBytes(this.bytes, 0, this.length)
429
532
  }
@@ -513,6 +616,11 @@ const pixelsStringEnd = (out: PixelBytes): void => {
513
616
  *
514
617
  * `imageId` is 1 to 2^31 - 1, since 0 is the protocol's "no id"; anything
515
618
  * else panics. An empty framebuffer or rect writes nothing.
619
+ *
620
+ * @param fb The frame's pixels, at its own size.
621
+ * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_KITTY`.
622
+ * @param imageId The image's id, 1 or more (below 1 panics), the same for every frame of one program.
623
+ * @param out Where the command bytes are appended.
516
624
  */
517
625
  export const kittyFrame = (fb: Framebuffer, rect: PixelRect, imageId: i32, out: PixelBytes): void => {
518
626
  pixelsCheckImageId("kittyFrame", imageId)
@@ -572,6 +680,9 @@ const pixelsCheckImageId = (who: string, imageId: i32): void => {
572
680
  * terminal does not answer. A program writes it when it stops drawing, and
573
681
  * puts it in its terminal restore string, so the last frame does not stay
574
682
  * behind. Panics, like `kittyFrame`, on an id below 1.
683
+ *
684
+ * @param imageId The id `kittyFrame` drew with.
685
+ * @returns The command, to `write`.
575
686
  */
576
687
  export const kittyDelete = (imageId: i32): string => {
577
688
  pixelsCheckImageId("kittyDelete", imageId)
@@ -636,6 +747,11 @@ const pixelsSixelRun = (out: PixelBytes, bits: i32, count: i32): void => {
636
747
  * matrix's threshold before they are quantised (ordered dithering), so a
637
748
  * gradient becomes a fine, stable pattern rather than steps. An empty rect
638
749
  * writes nothing.
750
+ *
751
+ * @param fb The frame's pixels; shrunk or grown to the rect by nearest neighbour.
752
+ * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_SIXEL`.
753
+ * @param dither Whether to dither gradients instead of banding them.
754
+ * @param out Where the Sixel bytes are appended.
639
755
  */
640
756
  export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, out: PixelBytes): void => {
641
757
  const w: i32 = rect.width
@@ -785,6 +901,10 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
785
901
  * not reach is set to `COLOR_DEFAULT`. Each covered cell is repainted once,
786
902
  * and `Screen.flush` sends only the cells whose glyph or colours changed, so
787
903
  * a frame that barely moves costs almost nothing.
904
+ *
905
+ * @param fb The frame's pixels.
906
+ * @param canvas The half-block canvas of the screen to draw on.
907
+ * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_HALFBLOCK`.
788
908
  */
789
909
  export const drawHalfBlocks = (fb: Framebuffer, canvas: Canvas, rect: PixelRect): void => {
790
910
  if (rect.width <= 0 || rect.height <= 0 || fb.width <= 0 || fb.height <= 0) {
@@ -851,6 +971,11 @@ const pixelsNearestAnsi16 = (colour: i32): i32 => {
851
971
  * nearest of xterm's 16 standard colours. `Screen` sends colours as 24-bit or
852
972
  * 256-palette SGR only, and a console that reduces those to its 16 itself then
853
973
  * lands on the colour chosen here, not on a neighbour of it.
974
+ *
975
+ * @param fb The frame's pixels.
976
+ * @param screen The screen to draw on.
977
+ * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_ASCII`.
978
+ * @param colors How many colours the terminal shows: 16 for the Linux console, anything else for more.
854
979
  */
855
980
  export const drawAscii = (fb: Framebuffer, screen: Screen, rect: PixelRect, colors: i32): void => {
856
981
  if (rect.width <= 0 || rect.height <= 0 || fb.width <= 0 || fb.height <= 0) {
package/std/term-probe.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * `std/term-probe` — what the terminal a program runs in can do (its colours,
2
+ * `nish/term-probe` — what the terminal a program runs in can do (its colours,
3
3
  * keyboard protocol, graphics and size), asked of it once at start and read
4
4
  * from its answer.
5
5
  *
@@ -10,16 +10,14 @@
10
10
  * caller's milliseconds as `f64` from any origin, and nothing here reads,
11
11
  * writes or waits.
12
12
  *
13
- * ```ts
14
- * import { TermEnv, TermProbe, probeQueries } from "nish/term-probe";
13
+ * import { TermEnv, TermProbe, probeQueries } from "nish/term-probe"
15
14
  *
16
- * const env: TermEnv = new TermEnv();
17
- * env.term = "xterm-256color";
18
- * const probe: TermProbe = new TermProbe(env, 80, 24, 0.0);
19
- * const queries: string = probeQueries(env); // write these to the terminal
20
- * // …feed each read: probe.feed(bytes, count, nowMs), until probe.done() or probe.timedOut(nowMs)
21
- * console.log(`${probe.result().colors}`);
22
- * ```
15
+ * const env: TermEnv = new TermEnv()
16
+ * env.term = "xterm-256color"
17
+ * const probe: TermProbe = new TermProbe(env, 80, 24, 0.0)
18
+ * const queries: string = probeQueries(env) // write these to the terminal
19
+ * // …feed each read: probe.feed(bytes, count, nowMs), until probe.done() or probe.timedOut(nowMs)
20
+ * console.log(`${probe.result().colors}`)
23
21
  *
24
22
  * - `TermEnv` — the environment variables the probe reads, each "" when
25
23
  * unset, one field each in camelCase: `term` (`TERM`), `colorterm`
@@ -50,8 +48,8 @@
50
48
  * (`nish/term`), keeping the replies and holding back every other event (a
51
49
  * key typed while the probe ran) for `heldEvents()`, in order. `done()` is
52
50
  * true once DA1 is answered; `timedOut(nowMs)` once `timeoutMs` (250 by
53
- * default) has passed since the send with no answer to DA1. Either way
54
- * the program then calls `finish(nowMs)` once: it gives up whatever the
51
+ * default) has passed since the send with no answer to DA1. `timedOut`
52
+ * calls `finish(nowMs)` itself; after `done()` the program calls it once: it gives up whatever the
55
53
  * decoder still holds (a key cut off at the end of the read with DA1, say)
56
54
  * and returns `heldEvents()` complete, and later reads go to the program's
57
55
  * own decoder. `rttMs()` is the time from the
@@ -218,7 +216,14 @@ const termPassthrough = (multiplexer: string, query: string): string => {
218
216
  return multiplexer === "screen" ? `\x1bP${query}\x1b\\` : query
219
217
  }
220
218
 
221
- /** Every query the probe asks, as one write; `TermProbe` reads the answers. */
219
+ /**
220
+ * Every query the probe asks, as one write; `TermProbe` reads the answers.
221
+ *
222
+ * @param env The environment, which picks the queries: under a multiplexer some are wrapped to
223
+ * reach the terminal outside it, and under `screen` or the Linux console the kitty graphics and
224
+ * DECRQSS queries, which answer in strings, are left out.
225
+ * @returns The queries, ending in DA1, to `write` at once.
226
+ */
222
227
  export const probeQueries = (env: TermEnv): string => {
223
228
  const mux: string = termMultiplexer(env)
224
229
  const parts: string[] = ["\x1b[>q", "\x1b[?2026$p", "\x1b[?1016$p", "\x1b[?u"]
@@ -276,15 +281,22 @@ export class TermProbe {
276
281
  /** How long to wait for the answer to DA1 (`TERM_PROBE_TIMEOUT_MS` by default). */
277
282
  timeoutMs: f64
278
283
  /** The decoder the terminal's bytes go through. */
279
- decoder: KeyDecoder
284
+ private decoder: KeyDecoder
280
285
  /** The replies so far, in order. */
281
286
  replies: TermEvent[]
282
287
  /** Every other event so far, in order: what the user typed during the probe. */
283
- held: TermEvent[]
288
+ private held: TermEvent[]
284
289
  /** When DA1 was answered; -1 before. */
285
- answeredAtMs: f64
290
+ private answeredAtMs: f64
286
291
 
287
- /** A probe whose queries were written at `sentAtMs` to a window of `cols` by `rows` cells. */
292
+ /**
293
+ * A probe whose queries were written at `sentAtMs` to a window of `cols` by `rows` cells.
294
+ *
295
+ * @param env The environment the queries were made for, also read for what replies cannot say.
296
+ * @param cols The window's width in cells, for working out a cell's pixels from the window's.
297
+ * @param rows The window's height in cells, likewise.
298
+ * @param sentAtMs When the queries were written, in milliseconds on the clock `feed` is given.
299
+ */
288
300
  constructor(env: TermEnv, cols: i32, rows: i32, sentAtMs: f64) {
289
301
  this.env = env
290
302
  this.cols = cols
@@ -298,7 +310,7 @@ export class TermProbe {
298
310
  }
299
311
 
300
312
  /** Take in `events`: replies kept, DA1 marking the probe done, the rest held. */
301
- take(events: TermEvent[], nowMs: f64): void {
313
+ private take(events: TermEvent[], nowMs: f64): void {
302
314
  for (const e of events) {
303
315
  if (e.type !== EVENT_REPLY) {
304
316
  this.held.push(e)
@@ -311,12 +323,22 @@ export class TermProbe {
311
323
  }
312
324
  }
313
325
 
314
- /** Decode `bytes[0, count)`, read at `nowMs`. */
326
+ /**
327
+ * Decode `bytes[0, count)`, read at `nowMs`.
328
+ *
329
+ * @param bytes The bytes read from the terminal.
330
+ * @param count How many of `bytes`, from the first, were read.
331
+ * @param nowMs When they were read, in milliseconds on the clock `sentAtMs` used.
332
+ */
315
333
  feed(bytes: u8[], count: i32, nowMs: f64): void {
316
334
  this.take(this.decoder.feedEvents(bytes, count, nowMs), nowMs)
317
335
  }
318
336
 
319
- /** Whether DA1 has been answered, so every reply is in. */
337
+ /**
338
+ * Whether DA1 has been answered, so every reply is in.
339
+ *
340
+ * @returns True once DA1 is answered.
341
+ */
320
342
  done(): boolean {
321
343
  return this.answeredAtMs >= 0.0
322
344
  }
@@ -324,6 +346,9 @@ export class TermProbe {
324
346
  /**
325
347
  * Whether `timeoutMs` has passed since the send with DA1 unanswered. Once it
326
348
  * has, it calls `finish(nowMs)`, so `heldEvents` is complete.
349
+ *
350
+ * @param nowMs The time now, in milliseconds on the clock `sentAtMs` used.
351
+ * @returns True once the time is up with DA1 unanswered; false while waiting, and always once DA1 is answered.
327
352
  */
328
353
  timedOut(nowMs: f64): boolean {
329
354
  if (this.done() || nowMs - this.sentAtMs < this.timeoutMs) {
@@ -337,25 +362,41 @@ export class TermProbe {
337
362
  * End the probe at `nowMs`: what the decoder still holds — an ESC or the
338
363
  * start of a key that came in the read with DA1, or an unfinished reply on a
339
364
  * timeout — is given up (the ESC as a key, the reply as nothing) and
340
- * `heldEvents` returned complete. Call it once, when `done()` or `timedOut`
341
- * ends the probe, and hand every later read to the program's own decoder.
365
+ * `heldEvents` returned complete. Call it once when `done()` ends the probe
366
+ * (`timedOut` calls it itself), and hand every later read to the program's own decoder.
367
+ *
368
+ * @param nowMs The time now, in milliseconds on the clock `sentAtMs` used.
369
+ * @returns The events that were not replies, in the order they came: the probe's own array that `heldEvents` gives,
370
+ * not a copy.
342
371
  */
343
372
  finish(nowMs: f64): TermEvent[] {
344
373
  this.take(this.decoder.pollEvents(nowMs + this.decoder.escapeTimeoutMs), nowMs)
345
374
  return this.held
346
375
  }
347
376
 
348
- /** The events that were not replies — keys typed, a paste — in the order they came, for the program to handle. */
377
+ /**
378
+ * The events that were not replies — keys typed, a paste — in the order they came, for the program to handle.
379
+ *
380
+ * @returns The held events, oldest first; complete only after `finish`. The probe's own array, not a copy.
381
+ */
349
382
  heldEvents(): TermEvent[] {
350
383
  return this.held
351
384
  }
352
385
 
353
- /** The time from the send to the answer to DA1, in milliseconds; -1 before it. */
386
+ /**
387
+ * The time from the send to the answer to DA1, in milliseconds; -1 before it.
388
+ *
389
+ * @returns The round trip in milliseconds, or -1 while DA1 is unanswered.
390
+ */
354
391
  rttMs(): f64 {
355
392
  return this.done() ? this.answeredAtMs - this.sentAtMs : -1.0
356
393
  }
357
394
 
358
- /** What the terminal can do, from the replies so far and the environment. */
395
+ /**
396
+ * What the terminal can do, from the replies so far and the environment.
397
+ *
398
+ * @returns The capabilities found; what no reply settled comes from the environment.
399
+ */
359
400
  result(): TermCaps {
360
401
  const env: TermEnv = this.env
361
402
  const caps: TermCaps = new TermCaps()