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
@@ -133,6 +133,8 @@ const DNS_FLAG_TC: i32 = 0x0200
133
133
  const DNS_FLAG_RD: i32 = 0x0100
134
134
  /** The fewest bytes a record can take: a root name and its ten fixed bytes. */
135
135
  const DNS_MIN_RECORD_BYTES: i32 = 11
136
+ /** `DNS_MAX_ANSWERS` addresses of `DNS_ADDRESS_BYTES` each. */
137
+ const DNS_ADDRESS_TABLE_BYTES: i32 = 144
136
138
 
137
139
  /**
138
140
  * One reply read by `dnsParseReply`, made once and reused.
@@ -178,7 +180,7 @@ export class DnsReply {
178
180
  * and `DNS_MAX_NAME_BYTES`-byte names, for `dnsParseReply` to fill.
179
181
  */
180
182
  constructor() {
181
- this.addresses = new Array<u8>(DNS_MAX_ANSWERS * DNS_ADDRESS_BYTES)
183
+ this.addresses = new Array<u8>(DNS_ADDRESS_TABLE_BYTES)
182
184
  this.ttls = new Array<i32>(DNS_MAX_ANSWERS)
183
185
  this.owner = new Array<u8>(DNS_MAX_NAME_BYTES)
184
186
  this.chain = new Array<u8>(DNS_MAX_NAME_BYTES)
@@ -232,8 +234,9 @@ export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32
232
234
  if (n === 0) {
233
235
  return DNS_ERR_EMPTY_LABEL
234
236
  }
235
- // n bytes of name become n + 1 of labels and one root byte.
236
- if (n + 2 > DNS_MAX_NAME_BYTES) {
237
+ // n bytes of name become n + 1 of labels and one root byte, so n is at most
238
+ // `DNS_MAX_NAME_BYTES - 2`: a literal, which is what bounds the sums below.
239
+ if (n > 253) {
237
240
  return DNS_ERR_NAME_LONG
238
241
  }
239
242
  const total: i32 = DNS_HEADER_BYTES + n + 2 + 4
@@ -246,32 +249,46 @@ export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32
246
249
  setU16BE(out, 6, toU16(0))
247
250
  setU16BE(out, 8, toU16(0))
248
251
  setU16BE(out, 10, toU16(0))
249
- // `lengthAt` is the length byte of the label being written.
250
- let lengthAt: i32 = DNS_HEADER_BYTES
252
+ // The bytes of the label being written so far, held at 64 once it is past
253
+ // `DNS_MAX_LABEL_BYTES`: its length byte goes just before them.
254
+ let label: i32 = 0
251
255
  let i: i32 = 0
252
256
  while (i <= n) {
253
257
  const b: i32 = i < n ? toI32(name.charCodeAt(i)) : 46
254
258
  if (b === 46) {
255
- const label: i32 = DNS_HEADER_BYTES + i - lengthAt
256
- if (label === 0) {
259
+ if (label <= 0) {
257
260
  return DNS_ERR_EMPTY_LABEL
258
261
  }
259
- if (label > DNS_MAX_LABEL_BYTES) {
262
+ // `DNS_MAX_LABEL_BYTES`.
263
+ if (label > 63) {
260
264
  return DNS_ERR_LABEL_LONG
261
265
  }
266
+ const lengthAt: i32 = DNS_HEADER_BYTES + i - label
262
267
  if (lengthAt < 0 || lengthAt >= toI32(out.length)) {
263
268
  return DNS_ERR_SPACE
264
269
  }
265
270
  out[lengthAt] = toU8(label)
266
- lengthAt = DNS_HEADER_BYTES + i + 1
271
+ label = 0
267
272
  } else if (b < 33 || b > 126) {
268
273
  return DNS_ERR_NAME_BYTE
269
274
  } else {
270
- out[DNS_HEADER_BYTES + i + 1] = toU8(b)
275
+ if (label < 64) {
276
+ label += 1
277
+ }
278
+ // Within `total`, so never past `out.length`.
279
+ const at: i32 = DNS_HEADER_BYTES + i + 1
280
+ if (at >= toI32(out.length)) {
281
+ return DNS_ERR_SPACE
282
+ }
283
+ out[at] = toU8(b)
271
284
  }
272
285
  i += 1
273
286
  }
274
- out[DNS_HEADER_BYTES + n + 1] = toU8(0)
287
+ const rootAt: i32 = DNS_HEADER_BYTES + n + 1
288
+ if (rootAt >= toI32(out.length)) {
289
+ return DNS_ERR_SPACE
290
+ }
291
+ out[rootAt] = toU8(0)
275
292
  setU16BE(out, DNS_HEADER_BYTES + n + 2, toU16(qtype))
276
293
  setU16BE(out, DNS_HEADER_BYTES + n + 4, toU16(DNS_CLASS_IN))
277
294
  return total
@@ -296,11 +313,14 @@ const dnsReadName = (msg: u8[], len: i32, off: i32, out: u8[]): i32 => {
296
313
  const msgLen: i32 = toI32(msg.length)
297
314
  const outLen: i32 = toI32(out.length)
298
315
  while (true) {
299
- if (pos < 0 || pos >= len || pos >= msgLen) {
316
+ // No DNS message is longer than 65535 bytes (TCP frames it with a 16-bit
317
+ // length, and no datagram is longer), so no name reads past that.
318
+ if (pos < 0 || pos >= len || pos >= msgLen || pos >= 65535) {
300
319
  return DNS_ERR_SHORT
301
320
  }
302
321
  const b: i32 = toI32(msg[pos])
303
- if (w < 0 || w >= outLen) {
322
+ // The label check below keeps `w` under `DNS_MAX_NAME_BYTES`.
323
+ if (w < 0 || w >= outLen || w >= 255) {
304
324
  return DNS_ERR_NAME_LONG
305
325
  }
306
326
  if (b === 0) {
@@ -309,17 +329,18 @@ const dnsReadName = (msg: u8[], len: i32, off: i32, out: u8[]): i32 => {
309
329
  }
310
330
  const kind: i32 = b & 0xc0
311
331
  if (kind === 0xc0) {
312
- if (pos + 1 >= len || pos + 1 >= msgLen) {
332
+ const low: i32 = pos + 1
333
+ if (low >= len || low >= msgLen) {
313
334
  return DNS_ERR_SHORT
314
335
  }
315
- const target: i32 = ((b & 0x3f) << 8) | toI32(msg[pos + 1])
336
+ const target: i32 = ((b & 0x3f) << 8) | toI32(msg[low])
316
337
  if (target >= runStart || target < DNS_HEADER_BYTES) {
317
338
  return DNS_ERR_POINTER
318
339
  }
319
- hops += 1
320
- if (hops > DNS_MAX_POINTER_HOPS) {
340
+ if (hops >= DNS_MAX_POINTER_HOPS) {
321
341
  return DNS_ERR_HOPS
322
342
  }
343
+ hops += 1
323
344
  if (after < 0) {
324
345
  after = pos + 2
325
346
  }
@@ -328,7 +349,7 @@ const dnsReadName = (msg: u8[], len: i32, off: i32, out: u8[]): i32 => {
328
349
  } else if (kind !== 0) {
329
350
  return DNS_ERR_LABEL_TYPE
330
351
  } else {
331
- if (b > len - pos - 1) {
352
+ if (pos + b >= len) {
332
353
  return DNS_ERR_SHORT
333
354
  }
334
355
  // The label, its length byte and the root byte still to come.
@@ -338,7 +359,17 @@ const dnsReadName = (msg: u8[], len: i32, off: i32, out: u8[]): i32 => {
338
359
  out[w] = toU8(b)
339
360
  let k: i32 = 1
340
361
  while (k <= b) {
341
- out[w + k] = toU8(dnsLower(toI32(msg[pos + k])))
362
+ // `pos + b < len` and `w + b < DNS_MAX_NAME_BYTES` put both in range.
363
+ const from: i32 = pos + k
364
+ if (from >= toI32(msg.length)) {
365
+ return DNS_ERR_SHORT
366
+ }
367
+ const lower: i32 = dnsLower(toI32(msg[from]))
368
+ const to: i32 = w + k
369
+ if (to >= toI32(out.length)) {
370
+ return DNS_ERR_NAME_LONG
371
+ }
372
+ out[to] = toU8(lower)
342
373
  k += 1
343
374
  }
344
375
  w += b + 1
@@ -353,7 +384,8 @@ const dnsSameName = (a: u8[], b: u8[]): boolean => {
353
384
  const aLen: i32 = toI32(a.length)
354
385
  const bLen: i32 = toI32(b.length)
355
386
  while (true) {
356
- if (i < 0 || i >= aLen || i >= bLen) {
387
+ // Neither name is longer than `DNS_MAX_NAME_BYTES` (255).
388
+ if (i < 0 || i >= aLen || i >= bLen || i >= 255) {
357
389
  return false
358
390
  }
359
391
  const n: i32 = toI32(a[i])
@@ -365,7 +397,8 @@ const dnsSameName = (a: u8[], b: u8[]): boolean => {
365
397
  }
366
398
  let k: i32 = 1
367
399
  while (k <= n) {
368
- if (a[i + k] !== b[i + k]) {
400
+ const at: i32 = i + k
401
+ if (at >= aLen || at >= bLen || a[at] !== b[at]) {
369
402
  return false
370
403
  }
371
404
  k += 1
@@ -378,14 +411,20 @@ const dnsSameName = (a: u8[], b: u8[]): boolean => {
378
411
  const dnsCopyName = (name: u8[], into: u8[]): void => {
379
412
  let i: i32 = 0
380
413
  const nameLen: i32 = toI32(name.length)
414
+ const intoLen: i32 = toI32(into.length)
381
415
  while (true) {
382
- if (i < 0 || i >= nameLen) {
416
+ // The name is no longer than `DNS_MAX_NAME_BYTES` (255).
417
+ if (i < 0 || i >= nameLen || i >= 255) {
383
418
  return
384
419
  }
385
420
  const n: i32 = toI32(name[i])
386
421
  let k: i32 = 0
387
422
  while (k <= n) {
388
- into[i + k] = name[i + k]
423
+ const at: i32 = i + k
424
+ if (at >= nameLen || at >= intoLen) {
425
+ return
426
+ }
427
+ into[at] = name[at]
389
428
  k += 1
390
429
  }
391
430
  if (n === 0) {
@@ -395,27 +434,54 @@ const dnsCopyName = (name: u8[], into: u8[]): void => {
395
434
  }
396
435
  }
397
436
 
398
- /** Keeps the RDATA at `msg[at]`, `size` bytes, as the next address of `reply`, with `port` and `ttl`. */
399
- const dnsKeepAddress = (msg: u8[], at: i32, size: i32, port: i32, ttl: i32, reply: DnsReply): void => {
400
- const base: i32 = reply.count * DNS_ADDRESS_BYTES
437
+ /**
438
+ * Keeps the RDATA at `msg[at]`, `size` bytes, as the next address of `reply`,
439
+ * with `port` and `ttl`, and answers whether it was kept: an address past
440
+ * `DNS_MAX_ANSWERS`, or one neither 4 nor 16 bytes, is not.
441
+ */
442
+ const dnsKeepAddress = (msg: u8[], at: i32, size: i32, port: i32, ttl: i32, reply: DnsReply): boolean => {
443
+ const count: i32 = reply.count
444
+ // `DNS_MAX_ANSWERS` is 8.
445
+ if (count < 0 || count >= 8 || (size !== 4 && size !== 16)) {
446
+ return false
447
+ }
448
+ // The caller read the RDATA's length against the message.
449
+ if (at < 0 || at > 65535 || size > toI32(msg.length) - at) {
450
+ return false
451
+ }
452
+ const addresses: u8[] = reply.addresses
453
+ const ttls: i32[] = reply.ttls
454
+ if (toI32(addresses.length) < 144 || toI32(ttls.length) < 8) {
455
+ return false
456
+ }
457
+ ttls[count] = ttl
458
+ const base: i32 = count * DNS_ADDRESS_BYTES
401
459
  const lead: i32 = 16 - size
402
460
  let k: i32 = 0
403
461
  while (k < lead) {
404
- reply.addresses[base + k] = toU8(0)
462
+ addresses[base + k] = toU8(0)
405
463
  k += 1
406
464
  }
407
465
  if (size === 4) {
408
- reply.addresses[base + 10] = toU8(255)
409
- reply.addresses[base + 11] = toU8(255)
466
+ addresses[base + 10] = toU8(255)
467
+ addresses[base + 11] = toU8(255)
410
468
  }
411
469
  k = 0
412
470
  while (k < size) {
413
- reply.addresses[base + lead + k] = msg[at + k]
471
+ const from: i32 = at + k
472
+ if (from >= toI32(msg.length)) {
473
+ return false
474
+ }
475
+ const to: i32 = base + lead + k
476
+ if (to < 0 || to >= toI32(addresses.length)) {
477
+ return false
478
+ }
479
+ addresses[to] = msg[from]
414
480
  k += 1
415
481
  }
416
- setU16BE(reply.addresses, base + 16, toU16(port))
417
- reply.ttls[reply.count] = ttl
418
- reply.count += 1
482
+ setU16BE(addresses, base + 16, toU16(port))
483
+ reply.count = count + 1
484
+ return true
419
485
  }
420
486
 
421
487
  /**
@@ -468,7 +534,7 @@ export const dnsParseReply = (
468
534
  reply.dropped = 0
469
535
  reply.rcode = 0
470
536
  reply.truncated = false
471
- if (len < DNS_HEADER_BYTES) {
537
+ if (len < 12) {
472
538
  return DNS_ERR_HEADER
473
539
  }
474
540
  const flags: i32 = toI32(getU16BE(msg, 2))
@@ -478,29 +544,29 @@ export const dnsParseReply = (
478
544
  if (((flags >> 11) & 15) !== 0) {
479
545
  return DNS_ERR_OPCODE
480
546
  }
481
- if (queryLen < DNS_HEADER_BYTES || msg[0] !== query[0] || msg[1] !== query[1]) {
547
+ if (queryLen < 12 || getU16BE(msg, 0) !== getU16BE(query, 0)) {
482
548
  return DNS_ERR_ID
483
549
  }
484
550
  if (toI32(getU16BE(msg, 4)) !== 1) {
485
551
  return DNS_ERR_QUESTION
486
552
  }
487
553
  const asked: i32 = dnsReadName(query, queryLen, DNS_HEADER_BYTES, reply.asked)
488
- if (asked < 0 || asked + 4 > queryLen) {
554
+ // `DNS_QUERY_MAX_BYTES` (271) holds any query dnsBuildQuery writes.
555
+ if (asked < 0 || asked > queryLen - 4 || asked > 267) {
489
556
  panic("dnsParseReply: the query is not one dnsBuildQuery wrote")
490
557
  }
491
558
  const named: i32 = dnsReadName(msg, len, DNS_HEADER_BYTES, reply.chain)
492
559
  if (named < 0) {
493
560
  return named
494
561
  }
495
- if (named + 4 > len) {
562
+ // dnsReadName reads no name past 65535, the longest a message can be.
563
+ if (named > len - 4 || named > 65535) {
496
564
  return DNS_ERR_SHORT
497
565
  }
498
566
  if (
499
567
  !dnsSameName(reply.chain, reply.asked) ||
500
- msg[named] !== query[asked] ||
501
- msg[named + 1] !== query[asked + 1] ||
502
- msg[named + 2] !== query[asked + 2] ||
503
- msg[named + 3] !== query[asked + 3]
568
+ getU16BE(msg, named) !== getU16BE(query, asked) ||
569
+ getU16BE(msg, named + 2) !== getU16BE(query, asked + 2)
504
570
  ) {
505
571
  return DNS_ERR_QUESTION
506
572
  }
@@ -525,13 +591,14 @@ export const dnsParseReply = (
525
591
  return rcode
526
592
  }
527
593
  const answers: i32 = toI32(getU16BE(msg, 6))
594
+ let dropped: i32 = 0
528
595
  let i: i32 = 0
529
596
  while (i < answers) {
530
597
  const fixed: i32 = dnsReadName(msg, len, pos, reply.owner)
531
598
  if (fixed < 0) {
532
599
  return fixed
533
600
  }
534
- if (fixed + 10 > len) {
601
+ if (fixed > len - 10 || fixed > 65535) {
535
602
  return DNS_ERR_SHORT
536
603
  }
537
604
  const type: i32 = toI32(getU16BE(msg, fixed))
@@ -556,11 +623,11 @@ export const dnsParseReply = (
556
623
  return DNS_ERR_RDATA
557
624
  }
558
625
  dnsCopyName(reply.owner, reply.chain)
559
- } else if (type === qtype) {
560
- if (reply.count < DNS_MAX_ANSWERS) {
561
- dnsKeepAddress(msg, data, size, port, rawTtl < 0 ? 0 : rawTtl, reply)
562
- } else {
563
- reply.dropped += 1
626
+ } else if (type === qtype && !dnsKeepAddress(msg, data, size, port, rawTtl < 0 ? 0 : rawTtl, reply)) {
627
+ // One per record before this one at most, so under `answers`.
628
+ if (dropped < answers) {
629
+ dropped += 1
630
+ reply.dropped = dropped
564
631
  }
565
632
  }
566
633
  }