@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
package/std/net/url.ts CHANGED
@@ -861,52 +861,100 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
861
861
  }
862
862
  h = at + 1
863
863
  }
864
- let portAt: i32 = b
864
+ const portAt: i32 = urlHostEnd(bytes, h, b)
865
+ if (portAt < 0) {
866
+ return false
867
+ }
865
868
  if (h < b && toI32(bytes[h]) === URL_OPEN_BRACKET) {
866
- const close: i32 = urlFind(bytes, h, b, URL_CLOSE_BRACKET)
867
- if (close === b || !urlIsIpv6(bytes, h + 1, close)) {
868
- return false
869
- }
870
- u.host = urlLowerString(bytes, h + 1, close)
871
- if (close + 1 < b) {
872
- if (toI32(bytes[close + 1]) !== URL_COLON) {
873
- return false
874
- }
875
- portAt = close + 1
876
- }
869
+ u.host = urlLowerString(bytes, h + 1, portAt - 1)
877
870
  } else {
878
- portAt = urlFind(bytes, h, b, URL_COLON)
879
- let i: i32 = h
880
- while (i >= 0 && i < portAt && i < toI32(bytes.length) && urlIsUnreserved(toI32(bytes[i]))) {
881
- i += 1
882
- }
883
- if (i < portAt) {
884
- return false
885
- }
886
- if (urlLooksNumeric(bytes, h, portAt) && !urlIsIpv4(bytes, h, portAt)) {
887
- return false
888
- }
889
871
  u.host = urlLowerString(bytes, h, portAt)
890
872
  }
891
873
  if (portAt + 1 < b) {
892
- let port: i32 = 0
893
- let i: i32 = portAt + 1
894
- while (i >= 0 && i < b && i < toI32(bytes.length)) {
895
- const c: i32 = toI32(bytes[i])
896
- if (!urlIsDigit(c)) {
897
- return false
898
- }
899
- port = port * 10 + c - 48
900
- if (port > URL_MAX_PORT) {
901
- return false
902
- }
903
- i += 1
874
+ const port: i32 = urlPortOf(bytes, portAt + 1, b)
875
+ if (port < 0) {
876
+ return false
904
877
  }
905
878
  u.port = port
906
879
  }
907
880
  return true
908
881
  }
909
882
 
883
+ /**
884
+ * Where the host that starts `bytes[h, b)` ends: the index of the `:` that
885
+ * starts its port, or `b` when there is none; -1 when the host breaks a rule
886
+ * `urlParse` names. The host is a bracketed IPv6 address, or a reg-name of
887
+ * unreserved bytes that is a dotted-decimal IPv4 address if it reads as a
888
+ * number at all. An empty host is allowed.
889
+ */
890
+ const urlHostEnd = (bytes: u8[], h: i32, b: i32): i32 => {
891
+ if (h >= 0 && h < b && h < toI32(bytes.length) && toI32(bytes[h]) === URL_OPEN_BRACKET) {
892
+ const close: i32 = urlFind(bytes, h, b, URL_CLOSE_BRACKET)
893
+ if (close === b || !urlIsIpv6(bytes, h + 1, close)) {
894
+ return -1
895
+ }
896
+ if (close + 1 < b && toI32(bytes[close + 1]) !== URL_COLON) {
897
+ return -1
898
+ }
899
+ return close + 1
900
+ }
901
+ const portAt: i32 = urlFind(bytes, h, b, URL_COLON)
902
+ let i: i32 = h
903
+ while (i >= 0 && i < portAt && i < toI32(bytes.length) && urlIsUnreserved(toI32(bytes[i]))) {
904
+ i += 1
905
+ }
906
+ if (i < portAt) {
907
+ return -1
908
+ }
909
+ if (urlLooksNumeric(bytes, h, portAt) && !urlIsIpv4(bytes, h, portAt)) {
910
+ return -1
911
+ }
912
+ return portAt
913
+ }
914
+
915
+ /** The port `bytes[a, b)` names, digits up to 65535, or -1 when it is not one. */
916
+ const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
917
+ let port: i32 = 0
918
+ let i: i32 = a
919
+ while (i >= 0 && i < b && i < toI32(bytes.length)) {
920
+ const c: i32 = toI32(bytes[i])
921
+ if (!urlIsDigit(c)) {
922
+ return -1
923
+ }
924
+ port = port * 10 + c - 48
925
+ if (port > URL_MAX_PORT) {
926
+ return -1
927
+ }
928
+ i += 1
929
+ }
930
+ return port
931
+ }
932
+
933
+ /**
934
+ * Whether `bytes[a, b)` is `uri-host [ ":" port ]` (RFC 3986 §3.2.2, §3.2.3)
935
+ * by the rules `urlParse` holds a URL's host and port to: a bracketed IPv6
936
+ * address, a dotted-decimal IPv4 address, or a reg-name of unreserved bytes
937
+ * that does not read as a number, then an optional `:` and a port of digits
938
+ * up to 65535. An empty host and an empty port are allowed, as in a URL; a
939
+ * percent-encoded or sub-delims byte in a reg-name is not. It is the rule an
940
+ * HTTP server holds a `Host` field to (RFC 9112 §3.2). Allocates nothing.
941
+ *
942
+ * @param bytes The bytes that hold the value.
943
+ * @param a The index of the value's first byte.
944
+ * @param b The index past the value's last byte.
945
+ * @returns True when the bytes are a host and an optional port; false otherwise, and for a window outside `bytes`.
946
+ */
947
+ export const urlIsHostPort = (bytes: u8[], a: i32, b: i32): boolean => {
948
+ if (a < 0 || b < a || b > toI32(bytes.length)) {
949
+ return false
950
+ }
951
+ const portAt: i32 = urlHostEnd(bytes, a, b)
952
+ if (portAt < 0) {
953
+ return false
954
+ }
955
+ return portAt + 1 >= b || urlPortOf(bytes, portAt + 1, b) >= 0
956
+ }
957
+
910
958
  /**
911
959
  * `s` split into a `Url` whose scheme is empty when `s` is a relative
912
960
  * reference, with its path as written; null when it breaks one of the rules
@@ -8,7 +8,8 @@
8
8
  * the subprotocols it offers, and `websocketUpgradeResponse(key, protocol)`
9
9
  * writes the 101 that accepts it, with `Sec-WebSocket-Accept` from
10
10
  * `websocketAcceptKey` (§4.2.2): the base64 of the SHA-1 of the key and a
11
- * fixed GUID. What `HTTP1_UPGRADE`'s `upgradeBytes()` answers is the first
11
+ * fixed GUID. `websocketOriginAllowed(parser, allowed)` is the opt-in check
12
+ * of `Origin` a server makes first, against a page on another site. What `HTTP1_UPGRADE`'s `upgradeBytes()` answers is the first
12
13
  * input for the decoder.
13
14
  *
14
15
  * **The client's handshake** (§4.1). `websocketClientKey(nonce)` spells 16
@@ -52,8 +53,10 @@
52
53
  * frame with a one-byte payload, or a status code that may not be sent
53
54
  * (§5.5.1, §7.4).
54
55
  * - 1007: a text message, or a close reason, that is not UTF-8 (§8.1). A text
55
- * message is checked as each fragment arrives, so a bad byte is refused
56
- * without waiting for the rest.
56
+ * message is checked as its bytes arrive, inside a frame as well as across
57
+ * fragments, so a bad byte is refused without waiting for the rest of the
58
+ * frame. The message buffer grows with the bytes received, not the length
59
+ * a frame header declares.
57
60
  * - 1009: a message longer than `maxMessage` (§7.4.1), refused from the frame
58
61
  * header that would pass it, before its payload arrives.
59
62
  *
@@ -279,6 +282,8 @@ export class WsDecoder {
279
282
  messageOpcode: i32 = 0
280
283
  /** @internal Decoder state the module's `websocket` helpers read and write. */
281
284
  messageLen: i32 = 0
285
+ /** How many payload bytes of the text frame still arriving are unmasked and UTF-8 checked. @internal */
286
+ frameChecked: i32 = 0
282
287
 
283
288
  /** Whether every frame must be masked, as a server requires of a client. */
284
289
  expectMasked: boolean = false
@@ -359,6 +364,7 @@ export class WsDecoder {
359
364
  this.closeReason = ""
360
365
  this.messageOpcode = 0
361
366
  this.messageLen = 0
367
+ this.frameChecked = 0
362
368
  this.data = this.control
363
369
  websocketUtf8Reset(this.utf8)
364
370
  }
@@ -401,7 +407,8 @@ const websocketFail = (d: WsDecoder, code: i32, reason: string): i32 => {
401
407
  /**
402
408
  * Copies the frame payload at `buf[from .. from + len)` into `to[at ..)`,
403
409
  * unmasking it with the key at `buf[maskAt .. maskAt + 4)` when `masked`
404
- * (§5.3: byte i is XORed with key byte i mod 4).
410
+ * (§5.3: byte i is XORed with key byte i mod 4). `phase` is how far into the
411
+ * payload `from` is, so a payload can be unmasked a piece at a time.
405
412
  */
406
413
  const websocketUnmaskInto = (
407
414
  buf: u8[],
@@ -409,6 +416,7 @@ const websocketUnmaskInto = (
409
416
  len: i32,
410
417
  masked: boolean,
411
418
  maskAt: i32,
419
+ phase: i32,
412
420
  to: u8[],
413
421
  at: i32
414
422
  ): void => {
@@ -422,13 +430,19 @@ const websocketUnmaskInto = (
422
430
  (toU32(buf[maskAt + 2]) << 8) |
423
431
  toU32(buf[maskAt + 3])
424
432
  }
433
+ for (let r: i32 = 0; r < (phase & 3); r += 1) {
434
+ key = (key << 8) | (key >>> 24)
435
+ }
425
436
  for (let k: i32 = 0; k < len; k += 1) {
426
437
  to[at + k] = buf[from + k] ^ toU8(key >>> 24)
427
438
  key = (key << 8) | (key >>> 24)
428
439
  }
429
440
  }
430
441
 
431
- /** Grows `d.message` so that `need` bytes fit, keeping its live bytes. */
442
+ /**
443
+ * Grows `d.message` so that `need` bytes fit, keeping its live bytes: the
444
+ * message so far and the part of a text frame already checked.
445
+ */
432
446
  const websocketMessageRoom = (d: WsDecoder, need: i32): void => {
433
447
  const old: u8[] = d.message
434
448
  let size: i32 = toI32(old.length)
@@ -439,7 +453,8 @@ const websocketMessageRoom = (d: WsDecoder, need: i32): void => {
439
453
  size = size > 1073741823 ? 2147483647 : size * 2
440
454
  }
441
455
  const grown: u8[] = new Array<u8>(size)
442
- for (let k: i32 = 0; k < d.messageLen && k < toI32(grown.length) && k < toI32(old.length); k += 1) {
456
+ const live: i32 = d.messageLen + d.frameChecked
457
+ for (let k: i32 = 0; k < live && k < toI32(grown.length) && k < toI32(old.length); k += 1) {
443
458
  grown[k] = old[k]
444
459
  }
445
460
  d.message = grown
@@ -567,15 +582,41 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
567
582
  return websocketFail(d, WS_CLOSE_TOO_BIG, "a message longer than the limit")
568
583
  }
569
584
  }
585
+ const payloadAt: i32 = at + headerLength
586
+ const maskAt: i32 = payloadAt - 4
587
+ const text: boolean =
588
+ opcode === WS_OP_TEXT || (opcode === WS_OP_CONTINUATION && d.messageOpcode === WS_OP_TEXT)
589
+ if (text) {
590
+ // A text frame is unmasked and checked as its bytes arrive, so a byte
591
+ // that cannot continue UTF-8 fails the connection at once (§8.1), not
592
+ // when the frame or the message ends.
593
+ const arrived: i32 = ready - headerLength < length ? ready - headerLength : length
594
+ const from: i32 = d.messageLen
595
+ const done: i32 = d.frameChecked
596
+ if (done === 0 && opcode === WS_OP_TEXT) {
597
+ // A new message starts its UTF-8 check afresh, in place: nothing is
598
+ // allocated per message.
599
+ websocketUtf8Reset(d.utf8)
600
+ }
601
+ if (arrived > done) {
602
+ // Room for the bytes that have arrived, not the length the header
603
+ // declares: a peer that declares much and sends little costs little.
604
+ websocketMessageRoom(d, from + arrived)
605
+ websocketUnmaskInto(buf, payloadAt + done, arrived - done, masked, maskAt, done, d.message, from + done)
606
+ d.frameChecked = arrived
607
+ if (!websocketUtf8Run(d.utf8, d.message, from + done, arrived - done)) {
608
+ return websocketFail(d, WS_CLOSE_INVALID_DATA, "a text message that is not UTF-8")
609
+ }
610
+ }
611
+ }
570
612
  if (ready - headerLength < length) {
571
613
  return WS_NEED_MORE
572
614
  }
573
615
  // The whole frame is here: take it out of the input.
574
- const payloadAt: i32 = at + headerLength
575
- const maskAt: i32 = payloadAt - 4
576
616
  d.start = payloadAt + length
617
+ d.frameChecked = 0
577
618
  if (control) {
578
- websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, d.control, 0)
619
+ websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, 0, d.control, 0)
579
620
  d.opcode = opcode
580
621
  d.data = d.control
581
622
  d.dataLen = length
@@ -586,21 +627,18 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
586
627
  }
587
628
  if (opcode !== WS_OP_CONTINUATION) {
588
629
  d.messageOpcode = opcode
589
- // A new message starts its UTF-8 check afresh, in place: nothing is
590
- // allocated per message.
591
- websocketUtf8Reset(d.utf8)
592
630
  }
593
631
  const from: i32 = d.messageLen
594
- websocketMessageRoom(d, from + length)
595
- websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, d.message, from)
596
- d.messageLen = from + length
597
- if (d.messageOpcode === WS_OP_TEXT && !websocketUtf8Run(d.utf8, d.message, from, length)) {
598
- return websocketFail(d, WS_CLOSE_INVALID_DATA, "a text message that is not UTF-8")
632
+ if (!text) {
633
+ // A text frame's payload is already in `message`, unmasked and checked.
634
+ websocketMessageRoom(d, from + length)
635
+ websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, 0, d.message, from)
599
636
  }
637
+ d.messageLen = from + length
600
638
  if (!fin) {
601
639
  return WS_CONTINUE
602
640
  }
603
- if (d.messageOpcode === WS_OP_TEXT && d.utf8.need !== 0) {
641
+ if (text && d.utf8.need !== 0) {
604
642
  return websocketFail(d, WS_CLOSE_INVALID_DATA, "a text message that ends inside a character")
605
643
  }
606
644
  // The message is handed out whole, and the next data frame starts a new one
@@ -747,7 +785,7 @@ export const websocketWriteFrame = (
747
785
  }
748
786
  // Masking and unmasking are the same XOR, so the copied payload goes
749
787
  // through the decoder's own loop, reading the key just written.
750
- websocketUnmaskInto(out, payloadAt, len, true, keyAt, out, payloadAt)
788
+ websocketUnmaskInto(out, payloadAt, len, true, keyAt, 0, out, payloadAt)
751
789
  }
752
790
  return payloadAt + len
753
791
  }
@@ -953,6 +991,78 @@ export const websocketOfferedProtocols = (p: Http1Parser): string[] | null => {
953
991
  return out
954
992
  }
955
993
 
994
+ /**
995
+ * Whether the request's `Origin` is one of `allowed`: the check a server makes
996
+ * before `acceptWebSocket` so that a page on another site cannot open a
997
+ * WebSocket that rides the user's cookies (cross-site WebSocket hijacking,
998
+ * RFC 6455 §10.2). A browser always sends `Origin` on the opening handshake
999
+ * and a page cannot change it; the handshake itself is not refused without
1000
+ * one, because a client that is not a browser need not send it, so the check
1001
+ * is the program's to opt into.
1002
+ *
1003
+ * An origin is compared whole, in any ASCII case, as the browser serializes
1004
+ * it: `https://example.com`, or `https://example.com:8443` for a port that is
1005
+ * not the scheme's default, with no path and no trailing slash. Answers false
1006
+ * for a request with no `Origin` or with more than one, so a program that also
1007
+ * serves clients that are not browsers lets those through itself. Allocates
1008
+ * nothing.
1009
+ *
1010
+ * @param p the parser holding the request's head, as `Http1Connection.parser` does at `H1_REQUEST`
1011
+ * @param allowed the origins to accept, each written as a browser sends it; "null" accepts an opaque origin, which any sandboxed page can send
1012
+ * @returns true when exactly one `Origin` field is present and its value is one of `allowed`
1013
+ */
1014
+ export const websocketOriginAllowed = (p: Http1Parser, allowed: string[]): boolean => {
1015
+ const head: u8[] = p.head
1016
+ const spans: i32[] = p.spans
1017
+ let found: i32 = -1
1018
+ for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
1019
+ if (websocketSpanIs(head, spans[4 * i], spans[4 * i + 1], "origin")) {
1020
+ if (found >= 0) {
1021
+ return false
1022
+ }
1023
+ found = i
1024
+ }
1025
+ }
1026
+ if (found < 0) {
1027
+ return false
1028
+ }
1029
+ for (let k: i32 = 0; k < toI32(allowed.length); k += 1) {
1030
+ if (websocketSpanFolds(head, spans[4 * found + 2], spans[4 * found + 3], allowed[k])) {
1031
+ return true
1032
+ }
1033
+ }
1034
+ return false
1035
+ }
1036
+
1037
+ /**
1038
+ * Whether `bytes[a .. b)` is `text` with ASCII letters in either case on
1039
+ * either side, as an origin's scheme and host compare. Allocates nothing.
1040
+ */
1041
+ const websocketSpanFolds = (bytes: u8[], a: i32, b: i32, text: string): boolean => {
1042
+ const n: i32 = toI32(text.length)
1043
+ if (a < 0 || b - a !== n || b > toI32(bytes.length)) {
1044
+ return false
1045
+ }
1046
+ for (let k: i32 = 0; k < n; k += 1) {
1047
+ const at: i32 = a + k
1048
+ if (at < 0 || at >= toI32(bytes.length) || k >= toI32(text.length)) {
1049
+ return false
1050
+ }
1051
+ let c: i32 = toI32(bytes[at])
1052
+ if (c >= 65 && c <= 90) {
1053
+ c += 32
1054
+ }
1055
+ let w: i32 = toI32(text.charCodeAt(k))
1056
+ if (w >= 65 && w <= 90) {
1057
+ w += 32
1058
+ }
1059
+ if (c !== w) {
1060
+ return false
1061
+ }
1062
+ }
1063
+ return true
1064
+ }
1065
+
956
1066
  /** Whether `bytes[a .. b)` is `word`, which is lower case, in any ASCII case. Allocates nothing. */
957
1067
  const websocketSpanIs = (bytes: u8[], a: i32, b: i32, word: string): boolean => {
958
1068
  const n: i32 = toI32(word.length)