@amritk/nish-x86_64-linux 0.38.0 → 0.40.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 (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
@@ -116,6 +116,7 @@ import {
116
116
  quicStreamOverhead,
117
117
  } from "nish/net/quic-frame"
118
118
  import { BufferPool } from "nish/net/buffer-pool"
119
+ import { copyBytes } from "nish/bytes"
119
120
 
120
121
  // ---- What the stream calls answer -----------------------------------------------
121
122
 
@@ -248,6 +249,118 @@ export const quicStreamSetBit = (bits: u8[], k: i32, on: boolean): void => {
248
249
  bits[at] = toU8(on ? old | mask : old & ~mask)
249
250
  }
250
251
 
252
+ /**
253
+ * Sets or clears bits `[from, to)` of `bits`: the ragged ends a bit at a
254
+ * time, the whole bytes between with one `fill`. Bits outside the array are
255
+ * left alone.
256
+ */
257
+ const quicStreamSetBits = (bits: u8[], from: i32, to: i32, on: boolean): void => {
258
+ // Compared a byte at a time, so a bitmap of 2^28 bytes or more cannot wrap `bits.length << 3`
259
+ // negative: the shift runs only when `to` is past the array, so the bit count fits below `to`.
260
+ const bytes: i32 = toI32(bits.length)
261
+ let k: i32 = from < 0 ? 0 : from
262
+ const top: i32 = to >> 3 < bytes ? to : bytes << 3
263
+ while (k < top && (k & 7) !== 0) {
264
+ quicStreamSetBit(bits, k, on)
265
+ k = k + 1
266
+ }
267
+ const whole: i32 = top & ~7
268
+ if (k < whole) {
269
+ bits.fill(on ? 255 : 0, k >> 3, whole >> 3)
270
+ k = whole
271
+ }
272
+ while (k < top) {
273
+ quicStreamSetBit(bits, k, on)
274
+ k = k + 1
275
+ }
276
+ }
277
+
278
+ /**
279
+ * The first bit in `[from, to)` of `bits` that is not `on`, or `to` when
280
+ * they all are; a whole byte of them is passed over at once. A bit outside
281
+ * the array reads as clear.
282
+ */
283
+ const quicStreamScan = (bits: u8[], from: i32, to: i32, on: boolean): i32 => {
284
+ const full: i32 = on ? 255 : 0
285
+ let k: i32 = from
286
+ while (k < to && (k & 7) !== 0) {
287
+ if (quicStreamBit(bits, k) !== on) {
288
+ return k
289
+ }
290
+ k = k + 1
291
+ }
292
+ const bytes: i32 = toI32(bits.length)
293
+ // As in `quicStreamSetBits`, the shift runs only when `to` is past the array, so it cannot wrap.
294
+ const whole: i32 = (to >> 3 < bytes ? to : bytes << 3) & ~7
295
+ let at: i32 = k >> 3
296
+ // `at` is `k >> 3` and `whole` is within the array, so the bounds on `at` hold whenever `k`'s do; they
297
+ // are what the bounds pass reads to drop the check on `bits[at]`.
298
+ while (at >= 0 && at < bytes && k < whole && toI32(bits[at]) === full) {
299
+ k = k + 8
300
+ at = at + 1
301
+ }
302
+ while (k < to) {
303
+ if (quicStreamBit(bits, k) !== on) {
304
+ return k
305
+ }
306
+ k = k + 1
307
+ }
308
+ return to
309
+ }
310
+
311
+ /**
312
+ * The first of `count` bytes of a ring of `size` from index `slot`: they run
313
+ * to the ring's end at most, and the rest wrap to index 0.
314
+ */
315
+ const quicStreamFirstRun = (size: i32, slot: i32, count: i32): i32 =>
316
+ count < size - slot ? count : size - slot
317
+
318
+ /** Sets or clears the bits of `count` ring indexes from `slot`, wrapping at `size`: at most two runs. */
319
+ const quicStreamMarkRing = (bits: u8[], size: i32, slot: i32, count: i32, on: boolean): void => {
320
+ const first: i32 = quicStreamFirstRun(size, slot, count)
321
+ quicStreamSetBits(bits, slot, slot + first, on)
322
+ if (count > first) {
323
+ quicStreamSetBits(bits, 0, count - first, on)
324
+ }
325
+ }
326
+
327
+ /** How many of `count` ring indexes from `slot`, wrapping at `size`, have their bit `on` before the first that does not. */
328
+ const quicStreamRingRun = (bits: u8[], size: i32, slot: i32, count: i32, on: boolean): i32 => {
329
+ const first: i32 = quicStreamFirstRun(size, slot, count)
330
+ const got: i32 = quicStreamScan(bits, slot, slot + first, on) - slot
331
+ if (got < first || count <= first) {
332
+ return got
333
+ }
334
+ return first + quicStreamScan(bits, 0, count - first, on)
335
+ }
336
+
337
+ /**
338
+ * Copies `count` bytes of `src` from `from` into the ring `ring` at index `slot`, wrapping at its end: at most two slices.
339
+ * Every caller passes `slot` from `QuicStream.at`, so below the ring's length, and a `count` of a ring at most:
340
+ * `onStream` keeps a frame's last ring's worth, and `write` clamps to `room()`. So both slices are in range,
341
+ * and `copyBytes` panicking on one that is not marks a broken invariant, not a byte to skip.
342
+ */
343
+ const quicStreamRingIn = (ring: u8[], slot: i32, src: u8[], from: i32, count: i32): void => {
344
+ const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
345
+ copyBytes(ring, slot, src, from, first)
346
+ if (count > first) {
347
+ copyBytes(ring, 0, src, from + first, count - first)
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Copies `count` bytes of the ring `ring` from index `slot`, wrapping at its end, into `dst` at `at`: at most two slices.
353
+ * `read` passes a `count` of at most what is contiguous past `recvRead`, never more than a ring, so, as for
354
+ * `quicStreamRingIn`, both slices are in range.
355
+ */
356
+ const quicStreamRingOut = (dst: u8[], at: i32, ring: u8[], slot: i32, count: i32): void => {
357
+ const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
358
+ copyBytes(dst, at, ring, slot, first)
359
+ if (count > first) {
360
+ copyBytes(dst, at + first, ring, 0, count - first)
361
+ }
362
+ }
363
+
251
364
  /** The smaller of two `i64`s. */
252
365
  const quicStreamMin = (a: i64, b: i64): i64 => (a < b ? a : b)
253
366
 
@@ -613,7 +726,11 @@ export class QuicStream {
613
726
  }
614
727
 
615
728
  /**
616
- * How many more bytes the send buffer takes.
729
+ * How many more bytes the send buffer takes. The buffer is the stream's
730
+ * `maxStreamData` and is the memory bound of its send side: bytes stay in
731
+ * it until the peer acknowledges them, so one lost packet holds back
732
+ * everything written after it until its resend is acknowledged (quic.ts
733
+ * probes early meanwhile, so a peer whose address changes is still seen).
617
734
  *
618
735
  * @returns The buffer size less the bytes written and not yet acknowledged.
619
736
  */
@@ -1068,6 +1185,22 @@ export class QuicStreams {
1068
1185
  this.streamsBlockedUniOwed = false
1069
1186
  }
1070
1187
 
1188
+ /**
1189
+ * Whether a stream's send buffer is full behind bytes the peer has not
1190
+ * acknowledged, with nothing of it left to send: the application offered
1191
+ * more than `room` took, and every byte the buffer holds is in flight. The
1192
+ * stream then sends nothing new until an ACK frees room.
1193
+ * @internal Read by quic.ts, for its early probe.
1194
+ */
1195
+ bufferBlocked(): boolean {
1196
+ for (const stream of this.slots) {
1197
+ if (stream.id >= 0 && stream.wantsWrite && stream.room() <= 0 && !stream.wantsToSend()) {
1198
+ return true
1199
+ }
1200
+ }
1201
+ return false
1202
+ }
1203
+
1071
1204
  /**
1072
1205
  * Takes the peer's transport parameters (RFC 9000 §18.2): its limits on
1073
1206
  * the streams this side opens, the credit each kind of stream starts with,
@@ -1441,22 +1574,35 @@ export class QuicStreams {
1441
1574
  this.borrowRecv(stream)
1442
1575
  }
1443
1576
  const size: i32 = toI32(stream.recvBuf.length)
1444
- for (let k2: i32 = 0; k2 < length; k2 += 1) {
1445
- const position: i64 = offset + toI64(k2)
1446
- const src: i32 = from + k2
1447
- if (position >= stream.recvContiguous && src >= 0 && src < toI32(buf.length)) {
1448
- const slot: i32 = stream.at(position)
1449
- if (slot >= 0 && slot < size) {
1450
- stream.recvBuf[slot] = buf[src]
1451
- quicStreamSetBit(stream.recvHave, slot, true)
1452
- }
1577
+ // A side holds either no buffer (length 0) or one from the pools, whose buffers are the table's
1578
+ // `bufferSize` (`share` refuses pools of another size), so the size test fails only with no buffer.
1579
+ if (size > 0 && size === stream.size) {
1580
+ // The frame's bytes from the first not yet contiguous, inside `buf`; of
1581
+ // more than a buffer, the last buffer's worth, which a byte-wise copy
1582
+ // would have left there. Both clamps below are defensive: the credit
1583
+ // check above caps a frame's end at `recvRead` plus a ring, so no peer
1584
+ // frame reaches them, and they keep the copy in range if that changes.
1585
+ const skipped: i64 = quicStreamMin(stream.recvContiguous - offset, toI64(length))
1586
+ let low: i32 = skipped > 0 ? toI32(skipped) : 0
1587
+ if (low < -from) {
1588
+ low = -from
1589
+ }
1590
+ const high: i32 = length < toI32(buf.length) - from ? length : toI32(buf.length) - from
1591
+ if (high - low > size) {
1592
+ low = high - size
1593
+ }
1594
+ if (low < high) {
1595
+ const slot: i32 = stream.at(offset + toI64(low))
1596
+ quicStreamRingIn(stream.recvBuf, slot, buf, from + low, high - low)
1597
+ quicStreamMarkRing(stream.recvHave, size, slot, high - low, true)
1598
+ }
1599
+ if (stream.recvContiguous < stream.recvHighest) {
1600
+ const pending: i64 = stream.recvHighest - stream.recvContiguous
1601
+ const count: i32 = toI32(quicStreamMin(pending, toI64(size)))
1602
+ stream.recvContiguous =
1603
+ stream.recvContiguous +
1604
+ toI64(quicStreamRingRun(stream.recvHave, size, stream.at(stream.recvContiguous), count, true))
1453
1605
  }
1454
- }
1455
- while (
1456
- stream.recvContiguous < stream.recvHighest &&
1457
- quicStreamBit(stream.recvHave, stream.at(stream.recvContiguous))
1458
- ) {
1459
- stream.recvContiguous = stream.recvContiguous + 1
1460
1606
  }
1461
1607
  if (stream.recvFinal >= 0 && stream.recvContiguous === stream.recvFinal) {
1462
1608
  stream.recvState = QUIC_RECV_DATA_RECVD
@@ -1686,12 +1832,9 @@ export class QuicStreams {
1686
1832
  if (n > 0) {
1687
1833
  this.borrowSend(stream)
1688
1834
  }
1689
- const size: i32 = toI32(stream.sendBuf.length)
1690
- for (let j: i32 = 0; j < n; j += 1) {
1691
- const slot: i32 = stream.at(stream.sendEnd + toI64(j))
1692
- if (slot >= 0 && slot < size && from + j >= 0 && from + j < toI32(buf.length)) {
1693
- stream.sendBuf[slot] = buf[from + j]
1694
- }
1835
+ // No buffer (length 0) is the one way this fails: a pool buffer is always `bufferSize` bytes.
1836
+ if (n > 0 && toI32(stream.sendBuf.length) === stream.size) {
1837
+ quicStreamRingIn(stream.sendBuf, stream.at(stream.sendEnd), buf, from, n)
1695
1838
  }
1696
1839
  stream.sendEnd = stream.sendEnd + toI64(n)
1697
1840
  this.writtenTotal = this.writtenTotal + toI64(n)
@@ -1745,12 +1888,11 @@ export class QuicStreams {
1745
1888
  }
1746
1889
  const n: i32 = toI64(length) < available ? length : toI32(available)
1747
1890
  const size: i32 = toI32(stream.recvBuf.length)
1748
- for (let j: i32 = 0; j < n; j += 1) {
1749
- const slot: i32 = stream.at(stream.recvRead + toI64(j))
1750
- if (slot >= 0 && slot < size && at + j >= 0 && at + j < toI32(buf.length)) {
1751
- buf[at + j] = stream.recvBuf[slot]
1752
- quicStreamSetBit(stream.recvHave, slot, false)
1753
- }
1891
+ // With bytes owed, the buffer is held (the test above), and a held buffer is `bufferSize` bytes.
1892
+ if (n > 0 && size === stream.size) {
1893
+ const slot: i32 = stream.at(stream.recvRead)
1894
+ quicStreamRingOut(buf, at, stream.recvBuf, slot, n)
1895
+ quicStreamMarkRing(stream.recvHave, size, slot, n, false)
1754
1896
  }
1755
1897
  stream.recvRead = stream.recvRead + toI64(n)
1756
1898
  if (n > 0) {
@@ -1919,8 +2061,10 @@ export class QuicStreams {
1919
2061
  private putResend(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
1920
2062
  if (stream.resendLow >= 0) {
1921
2063
  let low: i64 = stream.resendLow < stream.sendBase ? stream.sendBase : stream.resendLow
1922
- while (low < stream.resendHigh && quicStreamBit(stream.sendAcked, stream.at(low))) {
1923
- low = low + 1
2064
+ if (low < stream.resendHigh && stream.size > 0) {
2065
+ const span: i64 = stream.resendHigh - low
2066
+ const count: i32 = toI32(quicStreamMin(span, toI64(stream.size)))
2067
+ low = low + toI64(quicStreamRingRun(stream.sendAcked, stream.size, stream.at(low), count, true))
1924
2068
  }
1925
2069
  const want: i32 = toI32(stream.resendHigh - low)
1926
2070
  if (want <= 0) {
@@ -2197,14 +2341,28 @@ export class QuicStreams {
2197
2341
  // Done or reset: its buffer may be back in the pool, and nothing is owed.
2198
2342
  return
2199
2343
  }
2200
- const top: i64 = offset + toI64(length) < stream.sendEnd ? offset + toI64(length) : stream.sendEnd
2201
- for (let o: i64 = offset < stream.sendBase ? stream.sendBase : offset; o < top; o += 1) {
2202
- quicStreamSetBit(stream.sendAcked, stream.at(o), true)
2203
- }
2204
2344
  const before: i64 = stream.sendBase
2205
- while (stream.sendBase < stream.sendNext && quicStreamBit(stream.sendAcked, stream.at(stream.sendBase))) {
2206
- quicStreamSetBit(stream.sendAcked, stream.at(stream.sendBase), false)
2207
- stream.sendBase = stream.sendBase + 1
2345
+ const size: i32 = stream.size
2346
+ if (size > 0) {
2347
+ const top: i64 = offset + toI64(length) < stream.sendEnd ? offset + toI64(length) : stream.sendEnd
2348
+ const low: i64 = offset < stream.sendBase ? stream.sendBase : offset
2349
+ // The bytes in flight are fewer than a buffer, so a chunk is too.
2350
+ if (low < top) {
2351
+ quicStreamMarkRing(
2352
+ stream.sendAcked,
2353
+ size,
2354
+ stream.at(low),
2355
+ toI32(quicStreamMin(top - low, toI64(size))),
2356
+ true
2357
+ )
2358
+ }
2359
+ if (stream.sendBase < stream.sendNext) {
2360
+ const slot: i32 = stream.at(stream.sendBase)
2361
+ const count: i32 = toI32(quicStreamMin(stream.sendNext - stream.sendBase, toI64(size)))
2362
+ const done: i32 = quicStreamRingRun(stream.sendAcked, size, slot, count, true)
2363
+ quicStreamMarkRing(stream.sendAcked, size, slot, done, false)
2364
+ stream.sendBase = stream.sendBase + toI64(done)
2365
+ }
2208
2366
  }
2209
2367
  if (fin) {
2210
2368
  stream.finAcked = true