nish 0.42.0 → 0.45.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 (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
@@ -130,6 +130,8 @@ export const H3_SETTINGS_WEBTRANSPORT_MAX_SESSIONS: i64 = 0xc671706a
130
130
 
131
131
  /** How many identifiers `Http3Settings` keeps that it does not understand. */
132
132
  export const H3_SETTINGS_KEEP: i32 = 8
133
+ /** `h3Greased` values are `0x1f * N + 0x21`, so each is 2 more than a multiple of this (`0x21 % 0x1f`). */
134
+ const H3_GREASE_STEP: i64 = 0x1f
133
135
 
134
136
  // ---- Error codes (§8.1) ---------------------------------------------------------
135
137
 
@@ -265,9 +267,11 @@ export class Http3Settings {
265
267
  * identifiers this module understands, which are never kept.
266
268
  */
267
269
  unknown(id: i64): i64 {
268
- for (let k: i32 = 0; k < this.unknownCount && k < H3_SETTINGS_KEEP; k++) {
269
- if (this.unknownIds[k] === id) {
270
- return this.unknownValues[k]
270
+ const ids: i64[] = this.unknownIds
271
+ const values: i64[] = this.unknownValues
272
+ for (let k: i32 = 0; k < this.unknownCount && k < toI32(ids.length) && k < toI32(values.length); k++) {
273
+ if (ids[k] === id) {
274
+ return values[k]
271
275
  }
272
276
  }
273
277
  return H3_FRAME_NONE
@@ -325,12 +329,17 @@ export class Http3Settings {
325
329
  return H3_SETTINGS_ERROR
326
330
  }
327
331
  const at: i32 = this.unknownCount
328
- if (at >= 0 && at < H3_SETTINGS_KEEP) {
329
- this.unknownIds[at] = id
330
- this.unknownValues[at] = value
332
+ const ids: i64[] = this.unknownIds
333
+ const values: i64[] = this.unknownValues
334
+ if (at >= 0 && at < H3_SETTINGS_KEEP && at < toI32(ids.length) && at < toI32(values.length)) {
335
+ ids[at] = id
336
+ values[at] = value
331
337
  this.unknownCount = at + 1
332
- } else if (this.dropped < 2147483647) {
333
- this.dropped = this.dropped + 1
338
+ } else {
339
+ const dropped: i32 = this.dropped
340
+ if (dropped < 2147483647) {
341
+ this.dropped = dropped + 1
342
+ }
334
343
  }
335
344
  return H3_SETTINGS_OK
336
345
  }
@@ -349,6 +358,24 @@ export const h3CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void
349
358
  }
350
359
  }
351
360
 
361
+ /**
362
+ * `off + len`, after `h3CheckWindow` has panicked unless the window lies inside `buf`: the sum is then at
363
+ * most `buf.length`, so the `i64` it is taken in always narrows back exactly.
364
+ */
365
+ const h3WindowEnd = (what: string, buf: u8[], off: i32, len: i32): i32 => {
366
+ h3CheckWindow(what, buf, off, len)
367
+ return toI32(toI64(off) + toI64(len))
368
+ }
369
+
370
+ /**
371
+ * The offset just past the varint at `buf[at]`, or -1 when it does not end at or before `end` (or `at` is
372
+ * outside `buf`). Taken in `i64`, so it narrows back exactly whenever it is at most `end`.
373
+ */
374
+ const h3VarintEnd = (buf: u8[], at: i32, end: i32): i32 => {
375
+ const past: i64 = toI64(at) + toI64(quicVarintLength(buf, at))
376
+ return at >= 0 && past > toI64(at) && past <= toI64(end) ? toI32(past) : -1
377
+ }
378
+
352
379
  /**
353
380
  * `count` plus one, saturating at 2^31 - 1: every counter a peer drives.
354
381
  * @param count The counter's current value.
@@ -394,7 +421,7 @@ export const h3VarintLength = (buf: u8[], at: i32): i32 => quicVarintLength(buf,
394
421
  */
395
422
  export const h3PutVarint = (buf: u8[], at: i32, end: i32, value: i64): i32 => {
396
423
  const size: i32 = quicVarintSize(value)
397
- if (size === 0 || !h3FrameRoom(buf, at, end) || at > end - size) {
424
+ if (size === 0 || !h3FrameRoom(buf, at, end) || toI64(at) > toI64(end) - toI64(size)) {
398
425
  return -1
399
426
  }
400
427
  return quicVarintPut(buf, at, value, size)
@@ -409,7 +436,8 @@ export const h3PutVarint = (buf: u8[], at: i32, end: i32, value: i64): i32 => {
409
436
  export const h3FrameHeaderSize = (type: i64, length: i64): i32 => {
410
437
  const a: i32 = quicVarintSize(type)
411
438
  const b: i32 = quicVarintSize(length)
412
- return a === 0 || b === 0 ? H3_FRAME_ZERO : a + b
439
+ // `quicVarintSize` answers 0, 1, 2, 4 or 8.
440
+ return a <= 0 || b <= 0 || a > 8 || b > 8 ? H3_FRAME_ZERO : a + b
413
441
  }
414
442
 
415
443
  /**
@@ -424,20 +452,20 @@ export const h3FrameHeaderSize = (type: i64, length: i64): i32 => {
424
452
  * @returns The header's size, 2 to 16 bytes, or 0 when the window ends before both varints are whole.
425
453
  */
426
454
  export const h3ReadFrameHeader = (h: Http3FrameHeader, buf: u8[], off: i32, len: i32): i32 => {
427
- h3CheckWindow("h3ReadFrameHeader", buf, off, len)
428
- const end: i32 = off + len
455
+ const end: i32 = h3WindowEnd("h3ReadFrameHeader", buf, off, len)
429
456
  const type: i64 = quicVarintRead(buf, off, end)
430
- if (type < 0) {
457
+ const at: i32 = h3VarintEnd(buf, off, end)
458
+ if (type < 0 || at < 0) {
431
459
  return 0
432
460
  }
433
- const at: i32 = off + quicVarintLength(buf, off)
434
461
  const length: i64 = quicVarintRead(buf, at, end)
435
- if (length < 0) {
462
+ const past: i32 = h3VarintEnd(buf, at, end)
463
+ if (length < 0 || past < 0) {
436
464
  return 0
437
465
  }
438
466
  h.type = type
439
467
  h.length = length
440
- return at + quicVarintLength(buf, at) - off
468
+ return toI32(toI64(past) - toI64(off))
441
469
  }
442
470
 
443
471
  /**
@@ -452,7 +480,7 @@ export const h3ReadFrameHeader = (h: Http3FrameHeader, buf: u8[], off: i32, len:
452
480
  */
453
481
  export const h3PutFrameHeader = (buf: u8[], at: i32, end: i32, type: i64, length: i64): i32 => {
454
482
  const size: i32 = h3FrameHeaderSize(type, length)
455
- if (size === 0 || !h3FrameRoom(buf, at, end) || at > end - size) {
483
+ if (size === 0 || !h3FrameRoom(buf, at, end) || toI64(at) > toI64(end) - toI64(size)) {
456
484
  return -1
457
485
  }
458
486
  return h3PutVarint(buf, h3PutVarint(buf, at, end, type), end, length)
@@ -473,7 +501,7 @@ export const h3ReservedHttp2Frame = (type: i64): boolean =>
473
501
  * @returns True when `value` is 0x21 or above and 0x21 more than a multiple of 0x1f; false otherwise,
474
502
  * negatives included.
475
503
  */
476
- export const h3Greased = (value: i64): boolean => value >= 0x21 && (value - 0x21) % 0x1f === 0
504
+ export const h3Greased = (value: i64): boolean => value >= 0x21 && value % H3_GREASE_STEP === 2
477
505
 
478
506
  /**
479
507
  * Reads a whole SETTINGS payload, `buf[off, off + len)`, into `s` (which it
@@ -491,21 +519,21 @@ export const h3Greased = (value: i64): boolean => value >= 0x21 && (value - 0x21
491
519
  * error.
492
520
  */
493
521
  export const h3ReadSettings = (s: Http3Settings, buf: u8[], off: i32, len: i32): i64 => {
494
- h3CheckWindow("h3ReadSettings", buf, off, len)
522
+ const end: i32 = h3WindowEnd("h3ReadSettings", buf, off, len)
495
523
  s.reset()
496
- const end: i32 = off + len
497
524
  let at: i32 = off
498
525
  while (at < end) {
499
526
  const id: i64 = quicVarintRead(buf, at, end)
500
- if (id < 0) {
527
+ const idEnd: i32 = h3VarintEnd(buf, at, end)
528
+ if (id < 0 || idEnd < 0) {
501
529
  return H3_FRAME_ERROR
502
530
  }
503
- at = at + quicVarintLength(buf, at)
504
- const value: i64 = quicVarintRead(buf, at, end)
505
- if (value < 0) {
531
+ const value: i64 = quicVarintRead(buf, idEnd, end)
532
+ const valueEnd: i32 = h3VarintEnd(buf, idEnd, end)
533
+ if (value < 0 || valueEnd < 0) {
506
534
  return H3_FRAME_ERROR
507
535
  }
508
- at = at + quicVarintLength(buf, at)
536
+ at = valueEnd
509
537
  const result: i64 = s.take(id, value)
510
538
  if (result !== H3_SETTINGS_OK) {
511
539
  return result
@@ -534,17 +562,24 @@ export const h3PutSettings = (buf: u8[], at: i32, end: i32, ids: i64[], values:
534
562
  if (n !== toI32(values.length)) {
535
563
  return -1
536
564
  }
537
- let length: i64 = 0
565
+ let length: i32 = 0
538
566
  for (let k: i32 = 0; k < n && k < toI32(ids.length) && k < toI32(values.length); k++) {
539
567
  const a: i32 = quicVarintSize(ids[k])
540
568
  const b: i32 = quicVarintSize(values[k])
541
- if (a === 0 || b === 0) {
569
+ // `quicVarintSize` answers 0 for no varint, else 1, 2, 4 or 8.
570
+ if (a <= 0 || b <= 0 || a > 8 || b > 8) {
571
+ return -1
572
+ }
573
+ // A payload past 2^31 - 10 bytes takes a 9-byte header, so no such frame fits before an `end` that is
574
+ // at most `buf.length`.
575
+ const next: i64 = toI64(length) + toI64(a + b)
576
+ if (next > 2147483638) {
542
577
  return -1
543
578
  }
544
- length = length + toI64(a + b)
579
+ length = toI32(next)
545
580
  }
546
- let p: i32 = h3PutFrameHeader(buf, at, end, H3_FRAME_SETTINGS, length)
547
- if (p < 0 || toI64(p) + length > toI64(end)) {
581
+ let p: i32 = h3PutFrameHeader(buf, at, end, H3_FRAME_SETTINGS, toI64(length))
582
+ if (p < 0 || toI64(p) + toI64(length) > toI64(end)) {
548
583
  return -1
549
584
  }
550
585
  for (let k: i32 = 0; k < n && k < toI32(ids.length) && k < toI32(values.length); k++) {
@@ -589,7 +624,7 @@ export const h3PutIdFrame = (buf: u8[], at: i32, end: i32, type: i64, id: i64):
589
624
  * bytes after it.
590
625
  */
591
626
  export const h3ReadIdPayload = (buf: u8[], off: i32, len: i32): i64 => {
592
- h3CheckWindow("h3ReadIdPayload", buf, off, len)
593
- const id: i64 = quicVarintRead(buf, off, off + len)
594
- return id >= 0 && quicVarintLength(buf, off) === len ? id : H3_FRAME_NONE
627
+ const end: i32 = h3WindowEnd("h3ReadIdPayload", buf, off, len)
628
+ const id: i64 = quicVarintRead(buf, off, end)
629
+ return id >= 0 && h3VarintEnd(buf, off, end) === end ? id : H3_FRAME_NONE
595
630
  }