@amritk/nish-aarch64-linux 0.37.0 → 0.39.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 (73) hide show
  1. package/INSTALL.md +28 -16
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +380 -47
  4. package/package.json +1 -1
  5. package/runtime/core/arrays.d.ts +4 -0
  6. package/runtime/core/arrays.mjs +33 -0
  7. package/runtime/core/host.d.ts +11 -0
  8. package/runtime/core/host.mjs +35 -0
  9. package/runtime/nish.d.ts +645 -66
  10. package/runtime/nish.h +101 -15
  11. package/runtime/nish.mjs +69 -6
  12. package/runtime/node.mjs +117 -0
  13. package/runtime/runtime-aes.c +12 -4
  14. package/runtime/runtime-async.c +267 -51
  15. package/runtime/runtime-checksum.c +207 -15
  16. package/runtime/runtime-cross.c +2 -1
  17. package/runtime/runtime-net.c +32 -3
  18. package/runtime/runtime-os.c +174 -27
  19. package/runtime/runtime-region.c +170 -8
  20. package/runtime/runtime-sandbox.c +377 -17
  21. package/runtime/runtime-sha256.c +11 -4
  22. package/runtime/runtime-text.c +1068 -0
  23. package/runtime/runtime-wasm.c +18 -3
  24. package/runtime/runtime.c +55 -961
  25. package/runtime/vite.mjs +48 -11
  26. package/scripts/build.sh +113 -36
  27. package/std/LICENSE-fdlibm +8 -0
  28. package/std/README.md +7 -4
  29. package/std/array.ts +266 -0
  30. package/std/compress/checksum.ts +13 -0
  31. package/std/compress/deflate.ts +0 -2
  32. package/std/crypto/p256.ts +247 -74
  33. package/std/crypto/p384.ts +3 -1
  34. package/std/crypto/pki.ts +35 -9
  35. package/std/crypto/x25519.ts +153 -64
  36. package/std/crypto/x509.ts +113 -10
  37. package/std/host/text.ts +86 -0
  38. package/std/json-tokenizer.ts +20 -0
  39. package/std/json-typed.ts +217 -8
  40. package/std/math.ts +743 -41
  41. package/std/net/dns-resolver.ts +46 -6
  42. package/std/net/dns.ts +50 -13
  43. package/std/net/fetch.ts +691 -129
  44. package/std/net/handler.ts +142 -16
  45. package/std/net/http-compress.ts +17 -9
  46. package/std/net/http-fields.ts +19 -4
  47. package/std/net/http1-codec.ts +517 -154
  48. package/std/net/http1-server.ts +508 -25
  49. package/std/net/http2-cleartext.ts +526 -0
  50. package/std/net/http2-tls.ts +48 -0
  51. package/std/net/http2.ts +84 -0
  52. package/std/net/postgres-async.ts +2223 -0
  53. package/std/net/postgres-carrier.ts +343 -10
  54. package/std/net/postgres.ts +129 -11
  55. package/std/net/prefork.ts +195 -11
  56. package/std/net/quic-datagram.ts +30 -0
  57. package/std/net/quic-listener.ts +38 -12
  58. package/std/net/quic-packet.ts +19 -6
  59. package/std/net/quic-recovery.ts +134 -19
  60. package/std/net/quic-stream.ts +174 -36
  61. package/std/net/quic.ts +418 -18
  62. package/std/net/router.ts +39 -17
  63. package/std/net/serve.ts +867 -0
  64. package/std/net/tls/codec.ts +110 -32
  65. package/std/net/tls/record-server.ts +37 -7
  66. package/std/net/tls-tcp.ts +56 -1
  67. package/std/net/tls.ts +196 -50
  68. package/std/net/websocket.ts +55 -18
  69. package/std/net/webtransport.ts +179 -1
  70. package/std/regex.ts +69 -47
  71. package/std/region.ts +62 -11
  72. package/std/secret.ts +26 -59
  73. package/std/text.ts +464 -5
@@ -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
 
@@ -1441,22 +1554,35 @@ export class QuicStreams {
1441
1554
  this.borrowRecv(stream)
1442
1555
  }
1443
1556
  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
- }
1557
+ // A side holds either no buffer (length 0) or one from the pools, whose buffers are the table's
1558
+ // `bufferSize` (`share` refuses pools of another size), so the size test fails only with no buffer.
1559
+ if (size > 0 && size === stream.size) {
1560
+ // The frame's bytes from the first not yet contiguous, inside `buf`; of
1561
+ // more than a buffer, the last buffer's worth, which a byte-wise copy
1562
+ // would have left there. Both clamps below are defensive: the credit
1563
+ // check above caps a frame's end at `recvRead` plus a ring, so no peer
1564
+ // frame reaches them, and they keep the copy in range if that changes.
1565
+ const skipped: i64 = quicStreamMin(stream.recvContiguous - offset, toI64(length))
1566
+ let low: i32 = skipped > 0 ? toI32(skipped) : 0
1567
+ if (low < -from) {
1568
+ low = -from
1569
+ }
1570
+ const high: i32 = length < toI32(buf.length) - from ? length : toI32(buf.length) - from
1571
+ if (high - low > size) {
1572
+ low = high - size
1573
+ }
1574
+ if (low < high) {
1575
+ const slot: i32 = stream.at(offset + toI64(low))
1576
+ quicStreamRingIn(stream.recvBuf, slot, buf, from + low, high - low)
1577
+ quicStreamMarkRing(stream.recvHave, size, slot, high - low, true)
1578
+ }
1579
+ if (stream.recvContiguous < stream.recvHighest) {
1580
+ const pending: i64 = stream.recvHighest - stream.recvContiguous
1581
+ const count: i32 = toI32(quicStreamMin(pending, toI64(size)))
1582
+ stream.recvContiguous =
1583
+ stream.recvContiguous +
1584
+ toI64(quicStreamRingRun(stream.recvHave, size, stream.at(stream.recvContiguous), count, true))
1453
1585
  }
1454
- }
1455
- while (
1456
- stream.recvContiguous < stream.recvHighest &&
1457
- quicStreamBit(stream.recvHave, stream.at(stream.recvContiguous))
1458
- ) {
1459
- stream.recvContiguous = stream.recvContiguous + 1
1460
1586
  }
1461
1587
  if (stream.recvFinal >= 0 && stream.recvContiguous === stream.recvFinal) {
1462
1588
  stream.recvState = QUIC_RECV_DATA_RECVD
@@ -1686,12 +1812,9 @@ export class QuicStreams {
1686
1812
  if (n > 0) {
1687
1813
  this.borrowSend(stream)
1688
1814
  }
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
- }
1815
+ // No buffer (length 0) is the one way this fails: a pool buffer is always `bufferSize` bytes.
1816
+ if (n > 0 && toI32(stream.sendBuf.length) === stream.size) {
1817
+ quicStreamRingIn(stream.sendBuf, stream.at(stream.sendEnd), buf, from, n)
1695
1818
  }
1696
1819
  stream.sendEnd = stream.sendEnd + toI64(n)
1697
1820
  this.writtenTotal = this.writtenTotal + toI64(n)
@@ -1745,12 +1868,11 @@ export class QuicStreams {
1745
1868
  }
1746
1869
  const n: i32 = toI64(length) < available ? length : toI32(available)
1747
1870
  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
- }
1871
+ // With bytes owed, the buffer is held (the test above), and a held buffer is `bufferSize` bytes.
1872
+ if (n > 0 && size === stream.size) {
1873
+ const slot: i32 = stream.at(stream.recvRead)
1874
+ quicStreamRingOut(buf, at, stream.recvBuf, slot, n)
1875
+ quicStreamMarkRing(stream.recvHave, size, slot, n, false)
1754
1876
  }
1755
1877
  stream.recvRead = stream.recvRead + toI64(n)
1756
1878
  if (n > 0) {
@@ -1919,8 +2041,10 @@ export class QuicStreams {
1919
2041
  private putResend(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
1920
2042
  if (stream.resendLow >= 0) {
1921
2043
  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
2044
+ if (low < stream.resendHigh && stream.size > 0) {
2045
+ const span: i64 = stream.resendHigh - low
2046
+ const count: i32 = toI32(quicStreamMin(span, toI64(stream.size)))
2047
+ low = low + toI64(quicStreamRingRun(stream.sendAcked, stream.size, stream.at(low), count, true))
1924
2048
  }
1925
2049
  const want: i32 = toI32(stream.resendHigh - low)
1926
2050
  if (want <= 0) {
@@ -2197,14 +2321,28 @@ export class QuicStreams {
2197
2321
  // Done or reset: its buffer may be back in the pool, and nothing is owed.
2198
2322
  return
2199
2323
  }
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
2324
  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
2325
+ const size: i32 = stream.size
2326
+ if (size > 0) {
2327
+ const top: i64 = offset + toI64(length) < stream.sendEnd ? offset + toI64(length) : stream.sendEnd
2328
+ const low: i64 = offset < stream.sendBase ? stream.sendBase : offset
2329
+ // The bytes in flight are fewer than a buffer, so a chunk is too.
2330
+ if (low < top) {
2331
+ quicStreamMarkRing(
2332
+ stream.sendAcked,
2333
+ size,
2334
+ stream.at(low),
2335
+ toI32(quicStreamMin(top - low, toI64(size))),
2336
+ true
2337
+ )
2338
+ }
2339
+ if (stream.sendBase < stream.sendNext) {
2340
+ const slot: i32 = stream.at(stream.sendBase)
2341
+ const count: i32 = toI32(quicStreamMin(stream.sendNext - stream.sendBase, toI64(size)))
2342
+ const done: i32 = quicStreamRingRun(stream.sendAcked, size, slot, count, true)
2343
+ quicStreamMarkRing(stream.sendAcked, size, slot, done, false)
2344
+ stream.sendBase = stream.sendBase + toI64(done)
2345
+ }
2208
2346
  }
2209
2347
  if (fin) {
2210
2348
  stream.finAcked = true