@amritk/nish-x86_64-linux 0.49.1 → 0.51.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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -141,7 +141,6 @@ import {
141
141
  H3_REQUEST_REJECTED,
142
142
  H3_WT_SESSION_GONE,
143
143
  h3Count,
144
- h3CheckWindow,
145
144
  h3Greased,
146
145
  h3PutFrameHeader,
147
146
  h3PutVarint,
@@ -313,10 +312,32 @@ export const wtCodeFromHttp3 = (code: i64): i64 => {
313
312
  return toI64(shifted - Math.floor(shifted / 31.0))
314
313
  }
315
314
 
316
- /** Panics unless `value` lies in `[low, high]`: a cap out of range is the program's mistake. */
317
- const webtransportCheckCap = (what: string, value: i32, low: i32, high: i32): void => {
318
- if (value < low || value > high) {
319
- panic(`WebTransport: ${what} of ${value}, outside ${low} to ${high}`)
315
+ /** Why `value`, the cap `what`, lies outside `[low, high]`, or `""` when it lies inside. */
316
+ const webtransportCapFault = (what: string, value: i32, low: i32, high: i32): string =>
317
+ value < low || value > high ? `WebTransport: ${what} of ${value}, outside ${low} to ${high}` : ""
318
+
319
+ /** Why `config` cannot make a layer, or `""` when it can: the first cap out of range. */
320
+ const webtransportConfigFault = (config: WebTransportConfig): string => {
321
+ const streams: string = webtransportCapFault("maxStreams", config.maxStreams, 1, WT_MAX_CAP)
322
+ if (streams.length > 0) {
323
+ return streams
324
+ }
325
+ const pending: string = webtransportCapFault("maxPending", config.maxPending, 0, WT_MAX_CAP)
326
+ if (pending.length > 0) {
327
+ return pending
328
+ }
329
+ const idle: string = webtransportCapFault("idleTimeout", config.idleTimeout, 1, WT_MAX_IDLE)
330
+ if (idle.length > 0) {
331
+ return idle
332
+ }
333
+ return webtransportCapFault("maxDatagramsPerSecond", config.maxDatagramsPerSecond, 1, WT_MAX_RATE)
334
+ }
335
+
336
+ /** Panics unless `config` can make a layer: a cap out of range is the program's mistake. */
337
+ const webtransportCheckConfig = (config: WebTransportConfig): void => {
338
+ const fault: string = webtransportConfigFault(config)
339
+ if (fault.length > 0) {
340
+ panic(fault)
320
341
  }
321
342
  }
322
343
 
@@ -408,12 +429,19 @@ export class WebTransportWaiting {
408
429
  private heads: i32[]
409
430
  private tails: i32[]
410
431
  private salt: i64 = 0
432
+ /** Whether `salt` holds its 4 bytes from the entropy source; drawn by the first `add` that finds the source answering. */
433
+ private salted: boolean = false
434
+ /** The 4 bytes `drawSalt` reads into, made once here so an `add` the source refuses allocates nothing. */
435
+ private entropy: u8[]
411
436
  private freeHead: i32 = -1
412
437
  /** How many streams are waiting now. */
413
438
  count: i32 = 0
414
439
 
415
440
  /**
416
- * Makes an empty room, its hash salted with 4 bytes from `crypto.getRandomValues`.
441
+ * Makes an empty room. Its hash is salted with 4 bytes from
442
+ * `crypto.tryGetRandomValues`, drawn by the first `add`, so a room that
443
+ * no stream ever waits in draws nothing; `add` answers false while the
444
+ * source fails.
417
445
  * @param size How many streams may wait at once; 0 or less makes room for 1.
418
446
  */
419
447
  constructor(size: i32) {
@@ -425,12 +453,31 @@ export class WebTransportWaiting {
425
453
  const buckets: i32 = wtBuckets(n)
426
454
  this.heads = wtI32s(buckets)
427
455
  this.tails = wtI32s(buckets)
428
- const entropy: u8[] = wtBytes(4)
429
- crypto.getRandomValues(entropy)
456
+ this.entropy = wtBytes(4)
457
+ this.reset()
458
+ }
459
+
460
+ /**
461
+ * Draws the salt if it is not drawn yet. Only an empty room draws it, so
462
+ * no entry was filed under another salt; the room starts empty and every
463
+ * `add` before the draw answers false.
464
+ * @returns Whether the room is salted: false while the entropy source fails.
465
+ */
466
+ private drawSalt(): boolean {
467
+ if (this.salted) {
468
+ return true
469
+ }
470
+ const entropy: u8[] = this.entropy
471
+ if (!crypto.tryGetRandomValues(entropy)) {
472
+ return false
473
+ }
474
+ let salt: i64 = 0
430
475
  for (let j: i32 = 0; j < toI32(entropy.length); j++) {
431
- this.salt = (this.salt << toI64(8)) | toI64(toI32(entropy[j]))
476
+ salt = (salt << toI64(8)) | toI64(toI32(entropy[j]))
432
477
  }
433
- this.reset()
478
+ this.salt = salt
479
+ this.salted = true
480
+ return true
434
481
  }
435
482
 
436
483
  /** @internal Empties the room; called by `WebTransport` in this module, not for callers. */
@@ -453,10 +500,15 @@ export class WebTransportWaiting {
453
500
  }
454
501
 
455
502
  /**
456
- * Files stream `stream`, waiting for session `session`; answers whether there was room.
503
+ * Files stream `stream`, waiting for session `session`; answers whether
504
+ * there was room, false too when the salt is not drawn and the entropy
505
+ * source fails.
457
506
  * @internal Called by `WebTransport` in this module; not for callers.
458
507
  */
459
508
  add(stream: i64, session: i64): boolean {
509
+ if (!this.drawSalt()) {
510
+ return false
511
+ }
460
512
  const next: i32[] = this.next
461
513
  const prev: i32[] = this.prev
462
514
  const heads: i32[] = this.heads
@@ -744,23 +796,21 @@ export class WebTransport {
744
796
  closedByPeer: boolean = false
745
797
 
746
798
  /**
747
- * The WebTransport layer of `h3`, whose `Http3Config` must have
748
- * `webtransportSessions` above 0. A cap out of range panics.
799
+ * The WebTransport layer of `h3`, one session for each of its `Http3Config`'s
800
+ * `webtransportSessions`. Over a connection with WebTransport off (0) the
801
+ * layer has no sessions and answers every request for one 501, as for a
802
+ * `:protocol` it does not serve; `openWebTransport` answers that as an `Err`.
803
+ * A cap out of range panics, a contract of this constructor: `openWebTransport`
804
+ * answers it as an `Err`.
749
805
  * @param config The caps: `maxStreams` 1 to 4096, `maxPending` 0 to 4096, `idleTimeout` 1 to
750
806
  * 86,400,000 and `maxDatagramsPerSecond` 1 to 1,048,576; kept, not copied.
751
807
  * @param h3 The connection this layer reads and writes, kept for the layer's life; its restarts
752
808
  * restart this layer too.
753
809
  */
754
810
  constructor(config: WebTransportConfig, h3: Http3Connection) {
811
+ webtransportCheckConfig(config)
812
+ // `Http3Connection` holds the count to 0 through `H3_MAX_SESSIONS`.
755
813
  const sessions: i32 = h3.config.webtransportSessions
756
- // `Http3Connection` already holds the count to `H3_MAX_SESSIONS`; what can be wrong here is a connection with WebTransport off.
757
- if (sessions < 1) {
758
- panic("WebTransport: the Http3Connection has WebTransport off (webtransportSessions 0)")
759
- }
760
- webtransportCheckCap("maxStreams", config.maxStreams, 1, WT_MAX_CAP)
761
- webtransportCheckCap("maxPending", config.maxPending, 0, WT_MAX_CAP)
762
- webtransportCheckCap("idleTimeout", config.idleTimeout, 1, WT_MAX_IDLE)
763
- webtransportCheckCap("maxDatagramsPerSecond", config.maxDatagramsPerSecond, 1, WT_MAX_RATE)
764
814
  this.config = config
765
815
  this.h3 = h3
766
816
  this.quic = h3.quic
@@ -1206,7 +1256,7 @@ export class WebTransport {
1206
1256
  }
1207
1257
  let status: i32 = 0
1208
1258
  const peer = h3.peer
1209
- if (!httpFieldIs(f.protocol, "webtransport")) {
1259
+ if (!httpFieldIs(f.protocol, "webtransport") || toI32(this.table.length) === 0) {
1210
1260
  status = 501
1211
1261
  } else if (
1212
1262
  !httpFieldIs(f.scheme, "https") ||
@@ -1400,16 +1450,20 @@ export class WebTransport {
1400
1450
  * @param id The session's ID.
1401
1451
  * @param code The application's code; only its low 32 bits are sent.
1402
1452
  * @param buf The buffer holding the reason; only read, and copied into the capsule. A window outside
1403
- * it panics, whatever the session's state.
1453
+ * it answers H3_INVALID, whatever the session's state.
1404
1454
  * @param off Where the reason starts in `buf`.
1405
1455
  * @param len How many bytes the reason has, at most 1024.
1406
- * @returns 0 once the capsule and the FIN are written; H3_INVALID for a reason past 1024 bytes;
1456
+ * @returns 0 once the capsule and the FIN are written; H3_INVALID for a reason past 1024 bytes or a
1457
+ * window outside `buf`;
1407
1458
  * H3_AGAIN while the CONNECT stream has no room for the capsule; H3_TOO_LARGE for a capsule past
1408
1459
  * the stream's buffer; or H3_CLOSED for a session not accepted or already gone, or a CONNECT
1409
1460
  * stream that can no longer be written.
1410
1461
  */
1411
1462
  close(id: i64, code: i64, buf: u8[], off: i32, len: i32): i32 {
1412
- h3CheckWindow("WebTransport.close", buf, off, len)
1463
+ const size: i32 = toI32(buf.length)
1464
+ if (off < 0 || len < 0 || off > size || len > size - off) {
1465
+ return H3_INVALID
1466
+ }
1413
1467
  const w: WebTransportSession | null = this.openSession(id)
1414
1468
  if (w === null) {
1415
1469
  return H3_CLOSED
@@ -1603,7 +1657,12 @@ export class WebTransport {
1603
1657
  head[have] = bytes[at]
1604
1658
  this.capAt = wtAdd(at, 1)
1605
1659
  w.capHeadLen = have + 1
1606
- if (h3ReadFrameHeader(this.header, head, WT_ZERO, have + 1) === 0) {
1660
+ const filled: i32 = have + 1
1661
+ if (filled > toI32(head.length)) {
1662
+ return this.malformed(w, H3_DATAGRAM_ERROR) // never: `have` was below `head.length`
1663
+ }
1664
+ const start: i32 = 0
1665
+ if (h3ReadFrameHeader(this.header, head, start, filled) === 0) {
1607
1666
  continue
1608
1667
  }
1609
1668
  w.capHeadLen = 0
@@ -1850,17 +1909,22 @@ export class WebTransport {
1850
1909
  /**
1851
1910
  * Writes up to `len` bytes of `buf` from `off` to session stream `id`, with
1852
1911
  * its FIN when `fin` and all were taken. Answers how many it took, or
1853
- * H3_AGAIN, or H3_CLOSED (see `Http3Connection.writeStream`).
1912
+ * H3_AGAIN, or H3_CLOSED (see `Http3Connection.writeStream`). A window
1913
+ * outside `buf` answers H3_INVALID, whatever stream `id` names.
1854
1914
  * @param id The stream's ID, one of a session this layer took or opened.
1855
1915
  * @param buf The buffer holding the bytes; only read, and what is taken is copied.
1856
1916
  * @param off Where the bytes start in `buf`.
1857
1917
  * @param len How many bytes to write.
1858
1918
  * @param fin Whether to end this side of the stream once every byte is taken.
1859
1919
  * @returns How many bytes it took, fewer than `len` only when the send buffer filled (0 for a bare
1860
- * FIN); H3_AGAIN when it took none; or H3_CLOSED for a stream this layer does not hold or with no
1861
- * side here to write.
1920
+ * FIN); H3_AGAIN when it took none; H3_CLOSED for a stream this layer does not hold or with no
1921
+ * side here to write; or H3_INVALID for a window outside `buf`.
1862
1922
  */
1863
1923
  write(id: i64, buf: u8[], off: i32, len: i32, fin: boolean): i32 {
1924
+ const size: i32 = toI32(buf.length)
1925
+ if (off < 0 || len < 0 || off > size || len > size - off) {
1926
+ return H3_INVALID
1927
+ }
1864
1928
  this.current()
1865
1929
  const t: WebTransportStream | null = this.streamOf(id)
1866
1930
  if (t === null) {
@@ -1943,15 +2007,18 @@ export class WebTransport {
1943
2007
  * for a session not accepted.
1944
2008
  * @param id The session's ID.
1945
2009
  * @param buf The buffer holding the payload; only read, and copied into the datagram. A window
1946
- * outside it panics, whatever the session's state.
2010
+ * outside it answers H3_INVALID, whatever the session's state.
1947
2011
  * @param off Where the payload starts in `buf`.
1948
2012
  * @param len How many bytes the payload has.
1949
2013
  * @returns 0 once the datagram is queued; H3_AGAIN while QUIC's queue is full; H3_TOO_LARGE past
1950
- * `maxDatagramPayload`, or when QUIC refuses it for any other reason; or H3_CLOSED for a session
1951
- * not accepted, or one already gone.
2014
+ * `maxDatagramPayload`, or when QUIC refuses it for any other reason; H3_CLOSED for a session
2015
+ * not accepted, or one already gone; or H3_INVALID for a window outside `buf`.
1952
2016
  */
1953
2017
  sendDatagram(id: i64, buf: u8[], off: i32, len: i32): i32 {
1954
- h3CheckWindow("WebTransport.sendDatagram", buf, off, len)
2018
+ const size: i32 = toI32(buf.length)
2019
+ if (off < 0 || len < 0 || off > size || len > size - off) {
2020
+ return H3_INVALID
2021
+ }
1955
2022
  const w: WebTransportSession | null = this.openSession(id)
1956
2023
  if (w === null) {
1957
2024
  return H3_CLOSED
@@ -1972,3 +2039,26 @@ export class WebTransport {
1972
2039
  return result === QUIC_DATAGRAM_ERR_FULL ? H3_AGAIN : H3_TOO_LARGE
1973
2040
  }
1974
2041
  }
2042
+
2043
+ /**
2044
+ * A layer as `new WebTransport(config, h3)` makes one, with a cap out of
2045
+ * range, which that constructor panics on, and a connection with WebTransport
2046
+ * off, over which that constructor's layer would refuse every session,
2047
+ * answered as an `Err`.
2048
+ * @param config The caps, as the constructor reads them; kept, not copied.
2049
+ * @param h3 The connection the layer reads and writes, kept for the layer's life.
2050
+ * @returns `Ok` with the layer, or `Err` saying which cap is out of range or that `h3` has WebTransport off.
2051
+ */
2052
+ export const openWebTransport = (
2053
+ config: WebTransportConfig,
2054
+ h3: Http3Connection
2055
+ ): Result<WebTransport, string> => {
2056
+ if (h3.config.webtransportSessions < 1) {
2057
+ return Err("WebTransport: the Http3Connection has WebTransport off (webtransportSessions 0)")
2058
+ }
2059
+ const fault: string = webtransportConfigFault(config)
2060
+ if (fault.length > 0) {
2061
+ return Err(fault)
2062
+ }
2063
+ return Ok(new WebTransport(config, h3))
2064
+ }
package/std/raster.ts CHANGED
@@ -686,7 +686,12 @@ export class RasterTrail {
686
686
  * @param capacity - the points it remembers, at least one
687
687
  */
688
688
  constructor(capacity: i32) {
689
- const n: i32 = Math.max(capacity, 1)
689
+ // Written as a test, not `Math.max`, so the prover sees `n` is
690
+ // positive at each `new Array` below.
691
+ let n: i32 = 1
692
+ if (capacity > 1) {
693
+ n = capacity
694
+ }
690
695
  this.xs = new Array<f64>(n)
691
696
  this.xs.fill(0.0)
692
697
  this.ys = new Array<f64>(n)
@@ -840,7 +845,12 @@ export class RasterParticles {
840
845
  * @param capacity - the particles it holds at once, at least one
841
846
  */
842
847
  constructor(capacity: i32) {
843
- const n: i32 = Math.max(capacity, 1)
848
+ // Written as a test, not `Math.max`, so the prover sees `n` is
849
+ // positive at each `new Array` below.
850
+ let n: i32 = 1
851
+ if (capacity > 1) {
852
+ n = capacity
853
+ }
844
854
  this.xs = new Array<f64>(n)
845
855
  this.xs.fill(0.0)
846
856
  this.ys = new Array<f64>(n)