@amritk/nish-x86_64-linux 0.49.1 → 0.50.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 (91) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +12 -6
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +16 -9
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. package/std/yaml.ts +808 -395
@@ -72,15 +72,19 @@ import {
72
72
  H1_AGAIN,
73
73
  H1_ALPN,
74
74
  H1_BODY,
75
+ H1_CLOSED,
75
76
  H1_DONE,
76
77
  H1_END,
77
78
  H1_ERROR,
79
+ H1_INVALID,
78
80
  H1_NEED_MORE,
79
81
  H1_POOL_FULL,
80
82
  H1_REQUEST,
81
83
  H1_WANT_READ,
82
84
  H1_WANT_WRITE,
83
85
  H1_WRITE,
86
+ H1_WS_CLOSE,
87
+ H1_WS_MESSAGE,
84
88
  Http1Config,
85
89
  Http1Connection,
86
90
  Http1Server,
@@ -147,7 +151,8 @@ const HTTP_SERVER_TOO_LARGE: i32 = 413
147
151
  const HTTP_SERVER_UNAVAILABLE: i32 = 503
148
152
 
149
153
  // A request's life: free, reading its body, whole but parked behind a full output, queued for `take`,
150
- // held by the program, streaming its response, and flushing the response's last bytes.
154
+ // held by the program, streaming its response, and flushing the response's last bytes; or, once
155
+ // `acceptWebSocket` took its upgrade, a WebSocket the program holds until `closeWebSocket`.
151
156
  const HTTP_SERVER_FREE: i32 = 0
152
157
  const HTTP_SERVER_READING: i32 = 1
153
158
  const HTTP_SERVER_PARKED: i32 = 2
@@ -155,6 +160,9 @@ const HTTP_SERVER_QUEUED: i32 = 3
155
160
  const HTTP_SERVER_HELD: i32 = 4
156
161
  const HTTP_SERVER_STREAMING: i32 = 5
157
162
  const HTTP_SERVER_FLUSHING: i32 = 6
163
+ const HTTP_SERVER_SOCKET: i32 = 7
164
+ /** The close code a WebSocket gets when its connection ends with no close frame: 1006, which `nish/net/websocket` reserves as report-only. */
165
+ const HTTP_SERVER_WS_ABNORMAL: i32 = 1006
158
166
  /** `checkParam`'s `end` for a parameter's start offset (an i32 under `--number-mode f64`). */
159
167
  const HTTP_SERVER_PARAM_START: i32 = 0
160
168
  /** `checkParam`'s `end` for a parameter's end offset. */
@@ -411,6 +419,46 @@ export class HttpServerRequest {
411
419
  * @internal Set by `HttpServer.before` and cleared when a request is read into it, in this module.
412
420
  */
413
421
  stepped: boolean = false
422
+ /**
423
+ * Whether `acceptWebSocket` took this request's upgrade: it is a WebSocket
424
+ * now, the program's until `closeWebSocket`, and comes out of `take` again
425
+ * each time a message, a close or room to send arrives.
426
+ */
427
+ socket: boolean = false
428
+ /**
429
+ * Whether a WebSocket message is held for the program: `messageData` from
430
+ * `messageStart`, `messageLength` bytes, valid until `received`, or until it
431
+ * comes out `closed`, which clears this. No further frame is read till then.
432
+ */
433
+ message: boolean = false
434
+ /** The held message's opcode: 1 (`WS_OP_TEXT`) for text, which the decoder has checked is UTF-8, or 2 (`WS_OP_BINARY`). */
435
+ opcode: i32 = 0
436
+ /**
437
+ * The bytes of the held message, the connection's own decoder buffer, not a copy:
438
+ * `messageData[messageStart .. messageStart + messageLength)`.
439
+ */
440
+ messageData: u8[]
441
+ /** Where the held message starts in `messageData`. */
442
+ messageStart: i32 = 0
443
+ /** How many bytes the held message has. */
444
+ messageLength: i32 = 0
445
+ /**
446
+ * Whether the connection has room again after `sendFrame` answered
447
+ * `H1_AGAIN`; cleared by the next `sendFrame`.
448
+ */
449
+ writable: boolean = false
450
+ /**
451
+ * Whether the WebSocket is over: the peer sent a close, broke the
452
+ * protocol, or the connection ended. `closeCode` says why; the program
453
+ * hands the request back with `closeWebSocket`.
454
+ */
455
+ closed: boolean = false
456
+ /**
457
+ * Why the WebSocket closed: the peer's close code (1005 when its close
458
+ * carried none), the code a protocol error was answered with (1002, 1007,
459
+ * 1009), or 1006 when the connection ended with no close.
460
+ */
461
+ closeCode: i32 = 0
414
462
 
415
463
  /**
416
464
  * An empty request for the server's pool; its buffers are made on its first use.
@@ -427,6 +475,7 @@ export class HttpServerRequest {
427
475
  this.trailerValues = []
428
476
  this.addedNames = []
429
477
  this.addedValues = []
478
+ this.messageData = []
430
479
  }
431
480
 
432
481
  /**
@@ -749,14 +798,28 @@ const httpServerCell = (row: i32, width: i32, col: i32, length: i32): i32 => {
749
798
  }
750
799
 
751
800
  /**
752
- * The `monotonicNanos` instant `ns` nanoseconds after `at`. Worked in `u64`,
753
- * whose wrapping sum has the bits of the signed one: an instant is centuries
754
- * short of 2^63 and every delay a constant or a checked limit, so neither wraps.
801
+ * The `monotonicNanos` instant `ns` nanoseconds after `at`, or the last `i64`
802
+ * when the sum would pass it, which no clock reading does: an instant is
803
+ * centuries short of 2^63 and every delay a constant or a checked limit.
804
+ * Both are at least 0, so they are summed in `u64`, where two values below
805
+ * 2^63 cannot wrap, and the sum is turned back only once it is known to fit.
806
+ * A negative `at` or `ns`, which no caller passes, answers `at`.
755
807
  */
756
- const httpServerAfter = (at: i64, ns: i64): i64 => toI64(toU64(at) + toU64(ns))
808
+ const httpServerAfter = (at: i64, ns: i64): i64 => {
809
+ if (at < 0 || ns < 0) {
810
+ return at
811
+ }
812
+ const sum: u64 = toU64(at) + toU64(ns)
813
+ const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
814
+ return sum > top ? toI64(top) : toI64(sum)
815
+ }
757
816
 
758
- /** `until - now` in nanoseconds, worked in `u64` for the same reason as `httpServerAfter`. */
759
- const httpServerLeft = (until: i64, now: i64): i64 => toI64(toU64(until) - toU64(now))
817
+ /**
818
+ * `until - now` in nanoseconds, or 0 once `until` is not after `now`. An
819
+ * instant is never negative, so the difference of the later and a `now` of
820
+ * at least 0 fits an `i64`; a negative `now` answers 0 too.
821
+ */
822
+ const httpServerLeft = (until: i64, now: i64): i64 => (until > now && now >= 0 ? until - now : toI64(0))
760
823
 
761
824
  /**
762
825
  * Whether more than `ns` nanoseconds lie between the `monotonicNanos`
@@ -1568,7 +1631,7 @@ export class HttpServer {
1568
1631
  * QUIC handshake with `key`, closes what is past its timeouts, and answers
1569
1632
  * how many whole requests are waiting for `take`.
1570
1633
  * @param timeoutMs the longest to block, in milliseconds, or -1 to block until something is ready
1571
- * @param key the certificate's P-256 private key; borrowed for the call, still the caller's to wipe
1634
+ * @param key the certificate's private key, P-256 or RSA as `signatureScheme` says; borrowed for the call, still the caller's to wipe
1572
1635
  * @returns how many whole requests are queued for `take` after this turn
1573
1636
  */
1574
1637
  waitSigned(timeoutMs: i32, key: Secret<u8[]>): i32 {
@@ -1923,8 +1986,19 @@ export class HttpServer {
1923
1986
  // The peer's end or a failure: HTTP/1.1 reads it again and answers it as it does today.
1924
1987
  return this.h1Decided(s, h1)
1925
1988
  }
1926
- // `netRead` answers at most the bytes it was asked for, so the unsigned sum is at most 24.
1927
- const end: i32 = toI32(toU32(got) + toU32(n))
1989
+ // `netRead` answers at most the `room` bytes it was asked for, so `gap`
1990
+ // is never negative and `end` is `got + n`, at most the head's 24. A
1991
+ // larger answer would end the head at its last byte.
1992
+ const size: i32 = toI32(head.length)
1993
+ const room: i32 = size - got
1994
+ let end: i32 = size
1995
+ if (room >= 0) {
1996
+ const gap: i32 = room - n
1997
+ if (gap >= 0) {
1998
+ end = size - gap
1999
+ }
2000
+ }
2001
+
1928
2002
  for (let k: i32 = got; k < end && k < toI32(head.length) && k < toI32(want.length); k++) {
1929
2003
  if (toI32(head[k]) !== toI32(want.charCodeAt(k))) {
1930
2004
  s.got = end
@@ -2113,7 +2187,7 @@ export class HttpServer {
2113
2187
  return
2114
2188
  }
2115
2189
  let event: i32 = H1_NEED_MORE
2116
- if (!this.starve(s) && !this.unended(s)) {
2190
+ if (!this.starve(s) && !this.unended(s) && !this.holding(s, conn)) {
2117
2191
  event = this.h1Next(s)
2118
2192
  while (event !== H1_NEED_MORE && event !== H1_ERROR) {
2119
2193
  // A TLS slot's connection is built by the read that brought its first plaintext.
@@ -2121,7 +2195,10 @@ export class HttpServer {
2121
2195
  if (live === null) {
2122
2196
  break
2123
2197
  }
2124
- if (event === H1_REQUEST) {
2198
+ const socket: HttpServerRequest | null = this.socketOf(s)
2199
+ if (socket !== null) {
2200
+ this.frame(socket, live, event)
2201
+ } else if (event === H1_REQUEST) {
2125
2202
  this.begin(s, live)
2126
2203
  } else if (event === H1_BODY) {
2127
2204
  this.gather(s, live)
@@ -2130,7 +2207,7 @@ export class HttpServer {
2130
2207
  } else if (event === H1_WRITE) {
2131
2208
  this.resume(s, live)
2132
2209
  }
2133
- if (this.starve(s) || this.unended(s)) {
2210
+ if (this.starve(s) || this.unended(s) || this.holding(s, live)) {
2134
2211
  break
2135
2212
  }
2136
2213
  event = this.h1Next(s)
@@ -2138,13 +2215,71 @@ export class HttpServer {
2138
2215
  }
2139
2216
  if (event === H1_ERROR) {
2140
2217
  // The carrier has written its refusal and ends the connection; a
2141
- // request it was reading is nobody's.
2218
+ // request it was reading is nobody's, and a WebSocket learns why.
2142
2219
  this.releaseReading(s)
2220
+ const socket: HttpServerRequest | null = this.socketOf(s)
2221
+ const live: Http1Connection | null = this.h1Conn(s)
2222
+ if (socket !== null && live !== null) {
2223
+ this.shut(socket, live.status)
2224
+ }
2143
2225
  }
2144
2226
  const after: Http1Connection | null = this.h1Conn(s)
2145
2227
  this.settle(s, after !== null ? after : conn)
2146
2228
  }
2147
2229
 
2230
+ /** The WebSocket slot `s` carries, the program's or closing, or null when it carries HTTP. */
2231
+ private socketOf(s: HttpServerH1Slot): HttpServerRequest | null {
2232
+ const r: HttpServerRequest | null = s.current
2233
+ return r !== null && r.socket ? r : null
2234
+ }
2235
+
2236
+ /**
2237
+ * Whether slot `s` must not be asked for its next event: its WebSocket
2238
+ * holds a message, whose bytes are the decoder's buffer that the next
2239
+ * frame would overwrite. A full output still lets `H1_WRITE` through,
2240
+ * which the carrier gives without decoding.
2241
+ */
2242
+ private holding(s: HttpServerH1Slot, conn: Http1Connection): boolean {
2243
+ const r: HttpServerRequest | null = this.socketOf(s)
2244
+ return r !== null && r.message && !conn.writeHeld
2245
+ }
2246
+
2247
+ /**
2248
+ * One carrier event of WebSocket `r`: a message is held for the program, a
2249
+ * close is marked, room to send is said, and the program is told by `r`
2250
+ * coming out of `take` again.
2251
+ */
2252
+ private frame(r: HttpServerRequest, conn: Http1Connection, event: i32): void {
2253
+ if (r.state !== HTTP_SERVER_SOCKET) {
2254
+ // Closing: the carrier only sends what is left, and the slot closes once it is.
2255
+ return
2256
+ }
2257
+ if (event === H1_WS_MESSAGE) {
2258
+ r.message = true
2259
+ r.opcode = conn.opcode
2260
+ r.messageData = conn.data
2261
+ r.messageStart = conn.dataStart
2262
+ r.messageLength = conn.dataLength
2263
+ this.requeue(r)
2264
+ } else if (event === H1_WS_CLOSE) {
2265
+ this.shut(r, conn.status)
2266
+ } else if (event === H1_WRITE) {
2267
+ r.writable = true
2268
+ this.requeue(r)
2269
+ }
2270
+ }
2271
+
2272
+ /** Marks WebSocket `r` closed with `code`, once, and queues it for the program. */
2273
+ private shut(r: HttpServerRequest, code: i32): void {
2274
+ if (r.closed) {
2275
+ return
2276
+ }
2277
+ r.closed = true
2278
+ r.closeCode = code
2279
+ r.message = false
2280
+ this.requeue(r)
2281
+ }
2282
+
2148
2283
  /**
2149
2284
  * Whether `slot`'s response is under way with every byte handed over but
2150
2285
  * not yet ended. The carrier finishes a `Content-Length` response once its
@@ -2214,7 +2349,9 @@ export class HttpServer {
2214
2349
  this.h1Close(s)
2215
2350
  return
2216
2351
  }
2217
- if (s.starved) {
2352
+ // A slot that cannot read yet (no free request, or a WebSocket message
2353
+ // the program holds) is not woken by input it would leave unread.
2354
+ if (s.starved || this.holding(s, conn)) {
2218
2355
  wants = wants & ~H1_WANT_READ
2219
2356
  }
2220
2357
  this.h1Arm(s, wants)
@@ -2419,6 +2556,9 @@ export class HttpServer {
2419
2556
  const r: HttpServerRequest | null = s.current
2420
2557
  s.current = null
2421
2558
  if (r !== null) {
2559
+ if (r.state === HTTP_SERVER_SOCKET) {
2560
+ this.shut(r, HTTP_SERVER_WS_ABNORMAL)
2561
+ }
2422
2562
  this.drop(r)
2423
2563
  }
2424
2564
  }
@@ -2487,6 +2627,14 @@ export class HttpServer {
2487
2627
  r.bodyEnded = true
2488
2628
  r.stepped = false
2489
2629
  r.added = 0
2630
+ r.socket = false
2631
+ r.message = false
2632
+ r.opcode = 0
2633
+ r.messageStart = 0
2634
+ r.messageLength = 0
2635
+ r.writable = false
2636
+ r.closed = false
2637
+ r.closeCode = 0
2490
2638
  this.route(r)
2491
2639
  const declared: i32 = this.declared(r)
2492
2640
  if (declared > toI32(r.body.length) && !(mayStream && this.streamsBody(r.route))) {
@@ -2676,13 +2824,18 @@ export class HttpServer {
2676
2824
  * flushing is past caring.
2677
2825
  */
2678
2826
  private requeue(r: HttpServerRequest): void {
2679
- if (r.again || (r.state !== HTTP_SERVER_HELD && r.state !== HTTP_SERVER_STREAMING)) {
2827
+ if (r.again || !this.programs(r)) {
2680
2828
  return
2681
2829
  }
2682
2830
  r.again = true
2683
2831
  this.queuePush(r.index)
2684
2832
  }
2685
2833
 
2834
+ /** Whether `r` is the program's while queued again: held, streaming or an open WebSocket. */
2835
+ private programs(r: HttpServerRequest): boolean {
2836
+ return r.state === HTTP_SERVER_HELD || r.state === HTTP_SERVER_STREAMING || r.state === HTTP_SERVER_SOCKET
2837
+ }
2838
+
2686
2839
  /** Takes `r`'s second place out of the queue, closing the gap so the ring still holds each request at most once. */
2687
2840
  private unqueue(r: HttpServerRequest): void {
2688
2841
  r.again = false
@@ -2784,7 +2937,7 @@ export class HttpServer {
2784
2937
  }
2785
2938
  // A streaming request queued again is the program's, live or not: it learns of a reset by taking it.
2786
2939
  // One whose answer ended since (`end`, `trailers`) is flushing on its own, and stays out of `take`.
2787
- const held: boolean = r.state === HTTP_SERVER_HELD || r.state === HTTP_SERVER_STREAMING
2940
+ const held: boolean = this.programs(r)
2788
2941
  if ((r.again && held) || (!r.again && this.live(r))) {
2789
2942
  return r
2790
2943
  }
@@ -2801,7 +2954,9 @@ export class HttpServer {
2801
2954
  /**
2802
2955
  * The next request, of any version, which the program now owes an answer;
2803
2956
  * a `streaming` request it already holds comes out again each time more
2804
- * of its body, its end or a reset arrives, at most once per wait. Panics
2957
+ * of its body, its end or a reset arrives, at most once per wait, and an
2958
+ * upgraded request (`acceptWebSocket`) comes out again for each message,
2959
+ * for room after `H1_AGAIN` (`writable`) and once it is `closed`. Panics
2805
2960
  * when none is `ready`.
2806
2961
  * @returns the oldest queued request, the program's until it answers it
2807
2962
  */
@@ -3602,11 +3757,13 @@ export class HttpServer {
3602
3757
  * `carry` cannot say this, since the frame is taken as 0 bytes either way.
3603
3758
  */
3604
3759
  private carryEnd(r: HttpServerRequest): i32 {
3760
+ // A local zero: the window prover reads locals, not module constants.
3761
+ const zero: i32 = 0
3605
3762
  let done: i32 = -1
3606
3763
  if (r.kind === HTTP_SERVER_H2) {
3607
3764
  const conn: Http2Connection | null = this.h2Conn(r.slot)
3608
3765
  if (conn !== null) {
3609
- done = conn.writeData(toI32(r.stream), r.pending, HTTP_SERVER_ZERO, HTTP_SERVER_ZERO, true)
3766
+ done = conn.writeData(toI32(r.stream), r.pending, zero, zero, true)
3610
3767
  }
3611
3768
  if (done === H2_AGAIN) {
3612
3769
  return 0
@@ -3614,7 +3771,7 @@ export class HttpServer {
3614
3771
  } else {
3615
3772
  const h3: Http3Server | null = this.h3
3616
3773
  if (h3 !== null) {
3617
- done = h3.connection(r.slot).writeData(r.stream, r.pending, HTTP_SERVER_ZERO, HTTP_SERVER_ZERO, true)
3774
+ done = h3.connection(r.slot).writeData(r.stream, r.pending, zero, zero, true)
3618
3775
  }
3619
3776
  if (done === H3_AGAIN) {
3620
3777
  return 0
@@ -3789,6 +3946,146 @@ export class HttpServer {
3789
3946
  return w.complete() ? w.end : -1
3790
3947
  }
3791
3948
 
3949
+ // ---- WebSockets ------------------------------------------------------------------
3950
+
3951
+ /**
3952
+ * Accepts `r`'s WebSocket opening handshake, as `Http1Connection` checks it, with
3953
+ * `protocol`, empty for none: the 101 is queued and `r` becomes a WebSocket,
3954
+ * `socket` set, the program's until `closeWebSocket`. It then comes out of
3955
+ * `take` again whenever a message is held (`message`), the connection has
3956
+ * room after `sendFrame` answered `H1_AGAIN` (`writable`), or the WebSocket
3957
+ * is over (`closed`). Only an HTTP/1.1 request upgrades: one on HTTP/2 or
3958
+ * HTTP/3 answers `H1_INVALID` (HTTP/2's extended CONNECT is not served).
3959
+ * On anything but 0, `r` is as it was, the program's to answer as any other
3960
+ * request: 400, or 426 for a wrong `Sec-WebSocket-Version`. The slot's
3961
+ * `idleTimeout` (`Http1Config`) closes a WebSocket nothing moves on, and not
3962
+ * `bodyTimeout`. A draining server upgrades no request, and leaves an open
3963
+ * WebSocket open until the program closes it. Panics on another server's
3964
+ * request; answers `H1_INVALID` for one that is not taken and unanswered.
3965
+ * @param r a request `take` handed over and the program has not answered
3966
+ * @param protocol the subprotocol to name in `Sec-WebSocket-Protocol`, one the client offered, or "" for none
3967
+ * @returns 0 once the 101 is queued; `H1_INVALID` for a request that is not
3968
+ * held, not a valid HTTP/1.1 opening handshake, a `protocol` not a token, or on a draining server;
3969
+ * `H1_AGAIN` when the output has no room yet; `H1_CLOSED` when the connection closed
3970
+ */
3971
+ acceptWebSocket(r: HttpServerRequest, protocol: string): i32 {
3972
+ this.own(r, "acceptWebSocket")
3973
+ if (r.state !== HTTP_SERVER_HELD || r.kind !== HTTP_SERVER_H1 || this.draining) {
3974
+ return H1_INVALID
3975
+ }
3976
+ const conn: Http1Connection | null = this.h1ConnOf(r)
3977
+ if (!this.live(r) || conn === null) {
3978
+ return H1_CLOSED
3979
+ }
3980
+ const done: i32 = conn.acceptWebSocket(protocol)
3981
+ if (done !== 0) {
3982
+ return done
3983
+ }
3984
+ r.state = HTTP_SERVER_SOCKET
3985
+ r.socket = true
3986
+ this.driveOf(r)
3987
+ return 0
3988
+ }
3989
+
3990
+ /** Whether `r` is an open WebSocket the program holds; panics, naming `who`, on another server's request. */
3991
+ private ownSocket(r: HttpServerRequest, who: string): boolean {
3992
+ this.own(r, who)
3993
+ return r.state === HTTP_SERVER_SOCKET
3994
+ }
3995
+
3996
+ /**
3997
+ * Sends one frame on WebSocket `r`, unmasked as a server's are: a whole
3998
+ * text or binary message, a fragment of one (`fin` false, then
3999
+ * continuations), a ping or a pong. The payload is copied once, into the
4000
+ * connection's output. On `H1_AGAIN`, `r` comes out of `take` again with
4001
+ * `writable` set once there is room. Panics on another server's request.
4002
+ * @param r a request `acceptWebSocket` upgraded and `closeWebSocket` has not handed back
4003
+ * @param fin true when this frame ends its message; false for a fragment that continuations follow
4004
+ * @param opcode the frame's `WS_OP_` opcode: text, binary, continuation, ping or pong, never close
4005
+ * @param buf the payload bytes
4006
+ * @param off where in `buf` the window starts
4007
+ * @param len how many bytes the window holds
4008
+ * @returns 0 once the frame is queued; `H1_AGAIN` when it does not fit yet;
4009
+ * `H1_INVALID` for a close, a frame the encoder refuses, one larger
4010
+ * than the output, a window outside `buf`, or an `r` that is not a WebSocket the program holds; `H1_CLOSED` when the WebSocket is closing or its connection closed
4011
+ */
4012
+ sendFrame(r: HttpServerRequest, fin: boolean, opcode: i32, buf: u8[], off: i32, len: i32): i32 {
4013
+ if (!this.ownSocket(r, "sendFrame")) {
4014
+ return H1_INVALID
4015
+ }
4016
+ // Summed in i64, where two i32s cannot overflow; a window outside `buf` leaves `end` at -1. The guard
4017
+ // around the call restates it so that `src/bounds.ts` proves the window there.
4018
+ const sum: i64 = toI64(off) + toI64(len)
4019
+ let end: i32 = -1
4020
+ if (off >= 0 && len >= 0 && sum <= toI64(buf.length)) {
4021
+ end = toI32(sum)
4022
+ }
4023
+ if (end < 0) {
4024
+ return H1_INVALID
4025
+ }
4026
+ r.writable = false
4027
+ const conn: Http1Connection | null = this.h1ConnOf(r)
4028
+ if (!this.live(r) || conn === null) {
4029
+ return H1_CLOSED
4030
+ }
4031
+ let sent: i32 = H1_INVALID
4032
+ if (off >= 0 && off <= end && end <= toI32(buf.length)) {
4033
+ sent = conn.sendFrame(fin, opcode, buf, off, end - off)
4034
+ }
4035
+ if (sent === 0) {
4036
+ this.driveOf(r)
4037
+ }
4038
+ return sent
4039
+ }
4040
+
4041
+ /**
4042
+ * Hands WebSocket `r`'s held message back: `message` is cleared and
4043
+ * `messageData` may be overwritten, as the connection reads its next
4044
+ * frame. Nothing is read past a held message, so a program that keeps one
4045
+ * stops its peer. Panics on another server's request; does nothing when
4046
+ * `r` is not a WebSocket the program holds.
4047
+ * @param r a request `acceptWebSocket` upgraded, with or without a message held
4048
+ */
4049
+ received(r: HttpServerRequest): void {
4050
+ if (!this.ownSocket(r, "received")) {
4051
+ return
4052
+ }
4053
+ r.message = false
4054
+ this.driveOf(r)
4055
+ }
4056
+
4057
+ /**
4058
+ * Ends WebSocket `r` and hands it back to the server: a close frame with
4059
+ * `code` when the WebSocket is still open (a peer's close is answered by
4060
+ * the connection already), then the connection closes once that is sent.
4061
+ * Panics on another server's request.
4062
+ * @param r a request `acceptWebSocket` upgraded; the server's again unless this answers `H1_INVALID`
4063
+ * @param code a close status code `nish/net/websocket` may send, such as 1000 for a normal close
4064
+ * @returns 0 once `r` is handed back; `H1_INVALID`, with `r` unchanged, for
4065
+ * an `r` that is not a WebSocket the program holds, or a code that may
4066
+ * not be sent while it is open
4067
+ */
4068
+ closeWebSocket(r: HttpServerRequest, code: i32): i32 {
4069
+ if (!this.ownSocket(r, "closeWebSocket")) {
4070
+ return H1_INVALID
4071
+ }
4072
+ const conn: Http1Connection | null = this.h1ConnOf(r)
4073
+ if (!this.live(r) || conn === null) {
4074
+ this.release(r)
4075
+ return 0
4076
+ }
4077
+ if (conn.closeWebSocket(code) === H1_INVALID) {
4078
+ return H1_INVALID
4079
+ }
4080
+ r.message = false
4081
+ r.state = HTTP_SERVER_FLUSHING
4082
+ if (r.again) {
4083
+ this.unqueue(r)
4084
+ }
4085
+ this.driveOf(r)
4086
+ return 0
4087
+ }
4088
+
3792
4089
  /**
3793
4090
  * Answers a request the router refused, by its `route`: 405 with an `Allow`
3794
4091
  * field naming the methods the path has, and 404 for anything else.