nish 0.35.0 → 0.36.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 (118) hide show
  1. package/docs/AI.md +9 -2
  2. package/docs/INSTALL.md +6 -1
  3. package/package.json +15 -5
  4. package/runtime/nish.d.ts +4612 -430
  5. package/runtime/vite.d.ts +30 -0
  6. package/runtime/vite.mjs +150 -0
  7. package/scripts/build.sh +108 -13
  8. package/std/base64.ts +34 -5
  9. package/std/bytes.ts +256 -40
  10. package/std/cli-args.ts +296 -70
  11. package/std/cli-flags.ts +184 -19
  12. package/std/cli.ts +475 -54
  13. package/std/collections.ts +121 -23
  14. package/std/compress/checksum.ts +44 -14
  15. package/std/compress/deflate.ts +224 -111
  16. package/std/compress/gzip.ts +217 -53
  17. package/std/compress/zlib.ts +63 -7
  18. package/std/crypto/aes.ts +80 -9
  19. package/std/crypto/base64url.ts +22 -0
  20. package/std/crypto/bcrypt-batch.ts +88 -21
  21. package/std/crypto/bcrypt.ts +60 -0
  22. package/std/crypto/chacha20poly1305.ts +53 -0
  23. package/std/crypto/ct.ts +32 -13
  24. package/std/crypto/ed25519.ts +47 -0
  25. package/std/crypto/hkdf.ts +198 -13
  26. package/std/crypto/hmac.ts +235 -31
  27. package/std/crypto/jwt.ts +192 -63
  28. package/std/crypto/p256.ts +159 -16
  29. package/std/crypto/p384.ts +28 -3
  30. package/std/crypto/pki-roots.ts +32 -2
  31. package/std/crypto/pki.ts +81 -15
  32. package/std/crypto/rsa.ts +33 -3
  33. package/std/crypto/sha1.ts +3 -0
  34. package/std/crypto/sha256.ts +24 -1
  35. package/std/crypto/sha512.ts +99 -14
  36. package/std/crypto/x25519.ts +152 -14
  37. package/std/crypto/x509-random.ts +10 -0
  38. package/std/crypto/x509.ts +67 -7
  39. package/std/json-equal.ts +19 -0
  40. package/std/json-format.ts +112 -13
  41. package/std/json-number.ts +64 -14
  42. package/std/json-pointer.ts +23 -1
  43. package/std/json-tokenizer.ts +100 -29
  44. package/std/json-typed.ts +713 -66
  45. package/std/json-value.ts +400 -124
  46. package/std/json.ts +138 -10
  47. package/std/map.ts +25 -7
  48. package/std/math.ts +36 -4
  49. package/std/net/buffer-pool.ts +42 -11
  50. package/std/net/dns-config.ts +109 -23
  51. package/std/net/dns-message.ts +62 -7
  52. package/std/net/dns-resolver.ts +247 -53
  53. package/std/net/dns.ts +203 -35
  54. package/std/net/fetch.ts +151 -61
  55. package/std/net/handler.ts +278 -80
  56. package/std/net/hpack.ts +262 -52
  57. package/std/net/http-compress.ts +125 -15
  58. package/std/net/http-fields.ts +85 -9
  59. package/std/net/http1-client-codec.ts +100 -19
  60. package/std/net/http1-codec.ts +235 -27
  61. package/std/net/http1-server.ts +343 -85
  62. package/std/net/http2-frame.ts +272 -35
  63. package/std/net/http2-tls.ts +135 -34
  64. package/std/net/http2.ts +247 -106
  65. package/std/net/http3-frame.ts +154 -17
  66. package/std/net/http3-server.ts +246 -96
  67. package/std/net/http3.ts +307 -115
  68. package/std/net/outbound.ts +37 -3
  69. package/std/net/postgres-carrier.ts +144 -8
  70. package/std/net/postgres.ts +723 -103
  71. package/std/net/prefork.ts +164 -31
  72. package/std/net/qpack.ts +105 -18
  73. package/std/net/quic-conn-ack.ts +59 -12
  74. package/std/net/quic-conn-cid.ts +124 -27
  75. package/std/net/quic-conn-params.ts +70 -10
  76. package/std/net/quic-datagram.ts +43 -3
  77. package/std/net/quic-frame.ts +286 -12
  78. package/std/net/quic-listener.ts +101 -32
  79. package/std/net/quic-packet.ts +302 -21
  80. package/std/net/quic-recovery.ts +228 -44
  81. package/std/net/quic-stream.ts +353 -91
  82. package/std/net/quic.ts +489 -171
  83. package/std/net/router.ts +87 -40
  84. package/std/net/tls/client.ts +166 -51
  85. package/std/net/tls/codec.ts +413 -56
  86. package/std/net/tls/record-client.ts +110 -55
  87. package/std/net/tls/record-server.ts +159 -59
  88. package/std/net/tls/record.ts +109 -12
  89. package/std/net/tls/schedule.ts +235 -26
  90. package/std/net/tls/ticket.ts +159 -40
  91. package/std/net/tls/verify.ts +49 -3
  92. package/std/net/tls-tcp.ts +250 -45
  93. package/std/net/tls.ts +221 -59
  94. package/std/net/upstream.ts +81 -45
  95. package/std/net/url.ts +97 -13
  96. package/std/net/websocket.ts +122 -12
  97. package/std/net/webtransport.ts +220 -89
  98. package/std/pair.ts +10 -2
  99. package/std/random.ts +34 -9
  100. package/std/regex.ts +352 -198
  101. package/std/region.ts +70 -15
  102. package/std/secret.ts +52 -11
  103. package/std/sort.ts +17 -2
  104. package/std/sqlite.ts +256 -35
  105. package/std/sync/ring.ts +205 -35
  106. package/std/sync/shm.ts +194 -16
  107. package/std/term-io.ts +137 -64
  108. package/std/term-pixels.ts +144 -19
  109. package/std/term-probe.ts +66 -25
  110. package/std/term-rate.ts +128 -57
  111. package/std/term.ts +332 -121
  112. package/std/testing.ts +130 -32
  113. package/std/text.ts +73 -7
  114. package/std/threads.ts +229 -41
  115. package/std/yaml-json.ts +94 -82
  116. package/std/yaml-lines.ts +47 -15
  117. package/std/yaml-write.ts +24 -16
  118. package/std/yaml.ts +764 -384
package/std/net/fetch.ts CHANGED
@@ -167,7 +167,13 @@ const HTTP_CLIENT_WRITABLE: i32 = 2
167
167
  /** The size of the client random and of each ephemeral private key a TLS handshake draws. */
168
168
  const HTTP_CLIENT_TLS_KEY_BYTES: i32 = 32
169
169
 
170
- /** A short name for an `HTTP_E_*` code, for messages: "dns", "read timeout", ... */
170
+ /**
171
+ * A short name for an `HTTP_E_*` code, for messages: "dns", "read timeout",
172
+ * ... A fetch's `errorText` starts with it.
173
+ *
174
+ * @param code An `HTTP_E_*` code, such as a client's `error`.
175
+ * @returns The code's name, "none" for `HTTP_E_NONE`, or "unknown" for a number that is no code.
176
+ */
171
177
  export const httpErrorName = (code: i32): string => {
172
178
  // Indexed by code: the order of the HTTP_E_* constants above.
173
179
  const names: string[] = [
@@ -202,13 +208,22 @@ export const httpErrorName = (code: i32): string => {
202
208
  * `Authorization` field, so a password is never sent by accident.
203
209
  */
204
210
  export class HttpRequest {
211
+ /** The request method, sent as given, such as `GET` or `POST`. */
205
212
  method: string
213
+ /** The absolute URL the request is sent to. */
206
214
  url: string
215
+ /** The header field names, in the order they are sent. */
207
216
  names: string[]
217
+ /** The header field values, each beside its name in `names`. */
208
218
  values: string[]
209
219
  /** Null for no body at all; an empty array sends `Content-Length: 0`. */
210
220
  body: u8[] | null = null
211
221
 
222
+ /**
223
+ * A request with no header fields and no body.
224
+ * @param method The request method, such as `GET`.
225
+ * @param url The absolute `http://` or `https://` URL to send it to.
226
+ */
212
227
  constructor(method: string, url: string) {
213
228
  this.method = method
214
229
  this.url = url
@@ -216,7 +231,11 @@ export class HttpRequest {
216
231
  this.values = []
217
232
  }
218
233
 
219
- /** Appends one header field; nothing is checked until the request is written. */
234
+ /**
235
+ * Appends one header field; nothing is checked until the request is written.
236
+ * @param name The field name.
237
+ * @param value The field value; one holding a CR or LF makes the fetch fail.
238
+ */
220
239
  header(name: string, value: string): void {
221
240
  this.names.push(name)
222
241
  this.values.push(value)
@@ -246,10 +265,13 @@ export class HttpOptions {
246
265
  readTimeoutMs: i32 = 30000
247
266
  /** The budget for a whole fetch, its body included; 0 or less for none. */
248
267
  totalTimeoutMs: i32 = 0
268
+ /** The most redirect hops one fetch follows; past them the fetch fails. */
249
269
  maxRedirects: i32 = 5
250
270
  /** The status line and the header section, each at most this. */
251
271
  maxHeaderBytes: i32 = 65536
272
+ /** Follow a redirect rather than answer it; only to the same origin unless `crossOriginRedirects` is set. */
252
273
  followRedirects: boolean = false
274
+ /** Let a followed redirect go to another origin (see the module comment). */
253
275
  crossOriginRedirects: boolean = false
254
276
  /** Read the proxy settings from the environment (see the module comment). */
255
277
  useEnvProxy: boolean = true
@@ -261,20 +283,34 @@ export class HttpOptions {
261
283
  * `names` are lower-cased and in the order they arrived, beside `values`.
262
284
  */
263
285
  export class HttpResponse {
286
+ /** The status code, such as 200. */
264
287
  status: i32 = 0
288
+ /** The reason phrase the server sent beside the status. */
265
289
  reason: string = ""
290
+ /** The URL that answered, which differs from the request's after a redirect. */
266
291
  url: string = ""
292
+ /** The header field names, lower-cased, in the order they arrived. */
267
293
  names: string[]
294
+ /** The header field values, each beside its name in `names`. */
268
295
  values: string[]
296
+ /** The reader for the response's body. */
269
297
  body: HttpBodyReader
270
298
 
299
+ /**
300
+ * A response with no status and no fields yet; the client fills them in.
301
+ * @param body The reader for the body that follows the head.
302
+ */
271
303
  constructor(body: HttpBodyReader) {
272
304
  this.names = []
273
305
  this.values = []
274
306
  this.body = body
275
307
  }
276
308
 
277
- /** The first value of the field `name`, in any case; null when the response has none. */
309
+ /**
310
+ * The first value of the field `name`, in any case.
311
+ * @param name The field name to look for.
312
+ * @returns The value, or null when the response has no such field.
313
+ */
278
314
  header(name: string): string | null {
279
315
  const lower: string = httpClientLower(name)
280
316
  for (let i: i32 = 0; i < toI32(this.names.length) && i < toI32(this.values.length); i += 1) {
@@ -293,9 +329,15 @@ export class HttpResponse {
293
329
  * client that made it, and only until that client starts its next fetch.
294
330
  */
295
331
  export class HttpBodyReader {
332
+ /** The client that made the response; it carries the connection the body arrives on. */
296
333
  client: FetchSyncClient
297
- generation: i32
334
+ private generation: i32
298
335
 
336
+ /**
337
+ * A reader for the body of one response.
338
+ * @param client The client that made the response.
339
+ * @param generation The client's response number, so a reader stops reading once the client moves on.
340
+ */
299
341
  constructor(client: FetchSyncClient, generation: i32) {
300
342
  this.client = client
301
343
  this.generation = generation
@@ -308,6 +350,10 @@ export class HttpBodyReader {
308
350
  * event stream is read event by event. A window outside `buf` panics; an
309
351
  * empty one answers 0 at once, reading nothing, so 0 means the end only
310
352
  * when `n` is positive.
353
+ * @param buf Where the body bytes are written.
354
+ * @param off Where in `buf` the first byte goes.
355
+ * @param n The most bytes to read.
356
+ * @returns How many bytes were read, 0 at the end of the body, or a negated `HTTP_E_*` code.
311
357
  */
312
358
  read(buf: u8[], off: i32, n: i32): i32 {
313
359
  if (off < 0 || n < 0 || toI64(off) + toI64(n) > toI64(buf.length)) {
@@ -322,6 +368,8 @@ export class HttpBodyReader {
322
368
  /**
323
369
  * The rest of the body in one array, or null on an error or once it passes
324
370
  * `limit` bytes (`HTTP_E_BODY_TOO_LARGE`, and the connection is closed).
371
+ * @param limit The most body bytes to accept.
372
+ * @returns The body, or null on an error or a body past `limit`.
325
373
  */
326
374
  readAll(limit: i32): u8[] | null {
327
375
  let size: i32 = 4096
@@ -373,52 +421,59 @@ export class HttpBodyReader {
373
421
  */
374
422
  export class FetchSyncClient {
375
423
  // Declared widest first, so no byte of a client is padding.
424
+ /** The options every fetch through this client follows. */
376
425
  options: HttpOptions
426
+ /** A sentence saying why the last failure happened, for a log; "" after a fetch that answered. */
377
427
  errorText: string = ""
378
428
  /** The proxy settings in use; "" for none. `setProxies` replaces them. */
379
- httpProxy: string = ""
380
- httpsProxy: string = ""
381
- noProxy: string = ""
429
+ private httpProxy: string = ""
430
+ private httpsProxy: string = ""
431
+ private noProxy: string = ""
382
432
  /** Where `fd` leads: an origin, or the proxy an http request went through. */
383
- route: string = ""
433
+ private route: string = ""
384
434
  /** The outbound policy `fd` was dialled under; a connection is reused only under the same one. */
385
- routePolicy: OutboundPolicy | null = null
386
- parser: Http1ResponseParser
435
+ private routePolicy: OutboundPolicy | null = null
436
+ private parser: Http1ResponseParser
387
437
  /** The TLS connection over `fd` to an https origin; null over plain http. */
388
- tls: TlsRecordClient | null = null
438
+ private tls: TlsRecordClient | null = null
389
439
  /** The roots an https server's chain must end at, read on the client's first https fetch. */
390
- trust: TlsClientTrust | null = null
391
- ready: i32[]
392
- rbuf: u8[]
440
+ private trust: TlsClientTrust | null = null
441
+ private ready: i32[]
442
+ private rbuf: u8[]
393
443
  /** Where TLS application data is read before the parser takes it; empty until the first https connection. */
394
- plain: u8[]
444
+ private plain: u8[]
395
445
  /** The fetch's end in monotonic nanoseconds; `HTTP_CLIENT_NEVER` for none. */
396
- deadline: i64 = 0
446
+ private deadline: i64 = 0
397
447
  /** The last failure's `HTTP_E_*` code; `HTTP_E_NONE` after a fetch that answered. */
398
448
  error: i32 = 0
399
- fd: i32 = -1
449
+ private fd: i32 = -1
400
450
  /** The fd registered in `loop`, and for what. */
401
- loop: i32 = -1
402
- watched: i32 = -1
403
- watchedEvents: i32 = 0
451
+ private loop: i32 = -1
452
+ private watched: i32 = -1
453
+ private watchedEvents: i32 = 0
404
454
  /** Counts responses; a reader reads only while its own is the open one. */
405
- generation: i32 = 0
455
+ private generation: i32 = 0
406
456
  /** How the latest response's body failed, `HTTP_E_NONE` while it has not; its reader answers it again. */
407
- bodyError: i32 = 0
457
+ private bodyError: i32 = 0
408
458
  /** Where the latest response's unread body bytes start in `parser`, and how many. */
409
- pendOff: i32 = 0
410
- pendLen: i32 = 0
459
+ private pendOff: i32 = 0
460
+ private pendLen: i32 = 0
411
461
  /** Whether `fd` has carried a response before the one in flight. */
412
462
  reused: boolean = false
413
463
  /** Whether any byte of the response in flight has arrived. */
414
- gotByte: boolean = false
464
+ private gotByte: boolean = false
415
465
  /** Whether the failure in hand was the connection going away, which a retry may cure. */
416
- lost: boolean = false
466
+ private lost: boolean = false
417
467
  /** Whether the latest response's body is unread. */
418
- bodyOpen: boolean = false
419
- /** Whether this is `fetchSync`'s client, closed whole once its one body is done with. */
468
+ private bodyOpen: boolean = false
469
+ /** Whether this is `fetchSync`'s client, closed whole once its one body is done with. @internal */
420
470
  once: boolean = false
421
471
 
472
+ /**
473
+ * A client with no connection open yet, its proxy settings read from the
474
+ * environment when `useEnvProxy` is set.
475
+ * @param options How every fetch through the client behaves.
476
+ */
422
477
  constructor(options: HttpOptions) {
423
478
  this.options = options
424
479
  this.parser = httpClientParser(options)
@@ -433,7 +488,12 @@ export class FetchSyncClient {
433
488
  }
434
489
  }
435
490
 
436
- /** Replaces the proxy settings, as the three variables would set them; "" for none. */
491
+ /**
492
+ * Replaces the proxy settings, as the three variables would set them; "" for none.
493
+ * @param httpProxy The proxy for http URLs, as `http_proxy` would give it.
494
+ * @param httpsProxy The proxy for https URLs, as `https_proxy` would give it.
495
+ * @param noProxy The hosts reached without a proxy, as `no_proxy` would list them.
496
+ */
437
497
  setProxies(httpProxy: string, httpsProxy: string, noProxy: string): void {
438
498
  this.httpProxy = httpProxy
439
499
  this.httpsProxy = httpsProxy
@@ -445,6 +505,8 @@ export class FetchSyncClient {
445
505
  * redirects as the options say; null on a failure, with `error` and
446
506
  * `errorText` set. The body is read through the response's `body` before
447
507
  * the next fetch.
508
+ * @param req The request to send.
509
+ * @returns The response, its body still to be read, or null on a failure.
448
510
  */
449
511
  fetch(req: HttpRequest): HttpResponse | null {
450
512
  this.error = HTTP_E_NONE
@@ -540,7 +602,7 @@ export class FetchSyncClient {
540
602
  }
541
603
 
542
604
  /** Why the outbound policy refuses to let a URL name `url`'s host; "" when there is no policy or it allows the host. */
543
- hostRefusal(url: Url): string {
605
+ private hostRefusal(url: Url): string {
544
606
  const policy: OutboundPolicy | null = this.options.outbound
545
607
  if (policy === null || outboundHostAllowed(policy, url.host)) {
546
608
  return ""
@@ -562,7 +624,7 @@ export class FetchSyncClient {
562
624
  * leads to the same place, once more on a fresh one when a kept connection
563
625
  * turns out to be gone (see the module comment).
564
626
  */
565
- exchange(
627
+ private exchange(
566
628
  url: Url,
567
629
  method: string,
568
630
  names: string[],
@@ -593,7 +655,7 @@ export class FetchSyncClient {
593
655
  * there, else a new one, through a proxy when the settings say so. False on
594
656
  * a failure, with the error set.
595
657
  */
596
- open(url: Url): boolean {
658
+ private open(url: Url): boolean {
597
659
  const port: i32 = url.port >= 0 ? url.port : urlDefaultPort(url.scheme)
598
660
  // A proxy would look the name up itself, past the policy's address check.
599
661
  const proxyText: string =
@@ -668,7 +730,7 @@ export class FetchSyncClient {
668
730
  * the PEM file `rootsPemPath` names, else the system's. False, with the
669
731
  * error set, when none could be read, since no server could then be trusted.
670
732
  */
671
- loadTrust(): boolean {
733
+ private loadTrust(): boolean {
672
734
  if (this.trust !== null) {
673
735
  return true
674
736
  }
@@ -690,7 +752,7 @@ export class FetchSyncClient {
690
752
  * clock's time before its Finished is read. True once the connection is
691
753
  * open and the client's last flight sent; false with the error set.
692
754
  */
693
- secure(host: string, early: u8[]): boolean {
755
+ private secure(host: string, early: u8[]): boolean {
694
756
  const trust: TlsClientTrust | null = this.trust
695
757
  if (trust === null) {
696
758
  this.fail(HTTP_E_CERTIFICATE, "no trusted root certificates are loaded")
@@ -744,7 +806,7 @@ export class FetchSyncClient {
744
806
  * Asks `proxy`, on the fresh `fd`, for a tunnel to `host:port`. True once
745
807
  * the proxy answers 2xx, every byte after its head belonging to the tunnel.
746
808
  */
747
- tunnel(host: string, port: i32, proxy: Url): boolean {
809
+ private tunnel(host: string, port: i32, proxy: Url): boolean {
748
810
  const authority: string = `${httpClientHostText(host)}:${port}`
749
811
  const names: string[] = ["Host"]
750
812
  const values: string[] = [authority]
@@ -785,7 +847,13 @@ export class FetchSyncClient {
785
847
  }
786
848
 
787
849
  /** Writes the request on `fd` and reads its response's head. */
788
- send(url: Url, method: string, names: string[], values: string[], body: u8[] | null): HttpResponse | null {
850
+ private send(
851
+ url: Url,
852
+ method: string,
853
+ names: string[],
854
+ values: string[],
855
+ body: u8[] | null
856
+ ): HttpResponse | null {
789
857
  const viaProxy: boolean = this.route.startsWith("proxy ")
790
858
  const sendNames: string[] = []
791
859
  const sendValues: string[] = []
@@ -871,7 +939,7 @@ export class FetchSyncClient {
871
939
  }
872
940
 
873
941
  /** Reads up to the final response's head: `HTTP1_HEAD`, `HTTP1_UPGRADE`, or -1 with the error set. */
874
- head(): i32 {
942
+ private head(): i32 {
875
943
  let event: i32 = this.pump()
876
944
  while (event === HTTP1_INFORMATIONAL) {
877
945
  event = this.pump()
@@ -884,7 +952,7 @@ export class FetchSyncClient {
884
952
  }
885
953
 
886
954
  /** The parser's next event, reading from the socket as it needs; -1 with the error set. */
887
- pump(): i32 {
955
+ private pump(): i32 {
888
956
  let event: i32 = this.parser.next()
889
957
  while (event === HTTP1_NEED_MORE) {
890
958
  if (!this.fill()) {
@@ -920,7 +988,7 @@ export class FetchSyncClient {
920
988
  }
921
989
 
922
990
  /** One read of the response into the parser, waiting up to `readTimeoutMs`; false with the error set. */
923
- fill(): boolean {
991
+ private fill(): boolean {
924
992
  if (this.overdue()) {
925
993
  return false
926
994
  }
@@ -962,7 +1030,7 @@ export class FetchSyncClient {
962
1030
  * `readTimeoutMs`: the count, 0 at the end of the stream, or -1 with the
963
1031
  * error set.
964
1032
  */
965
- readRaw(want: i32): i32 {
1033
+ private readRaw(want: i32): i32 {
966
1034
  const until: i64 = this.readDeadline()
967
1035
  let n: i32 = netRead(this.fd, this.rbuf, 0, want)
968
1036
  while (n === HTTP_CLIENT_WOULD_BLOCK) {
@@ -982,7 +1050,7 @@ export class FetchSyncClient {
982
1050
  }
983
1051
 
984
1052
  /** One read of the socket into the TLS connection `conn`, waiting up to `readTimeoutMs`; false with the error set. */
985
- receiveTls(conn: TlsRecordClient): boolean {
1053
+ private receiveTls(conn: TlsRecordClient): boolean {
986
1054
  const room: i32 = conn.space()
987
1055
  const want: i32 = room < HTTP_CLIENT_READ_BYTES ? room : HTTP_CLIENT_READ_BYTES
988
1056
  if (want <= 0) {
@@ -1004,7 +1072,7 @@ export class FetchSyncClient {
1004
1072
  }
1005
1073
 
1006
1074
  /** Sends everything `conn` has sealed, waiting as `writeRaw` does; false with the error set. */
1007
- flushTls(conn: TlsRecordClient): boolean {
1075
+ private flushTls(conn: TlsRecordClient): boolean {
1008
1076
  while (conn.outputEnd > conn.outputStart) {
1009
1077
  const len: i32 = conn.outputEnd - conn.outputStart
1010
1078
  if (!this.writeRaw(conn.output, conn.outputStart, len)) {
@@ -1017,7 +1085,7 @@ export class FetchSyncClient {
1017
1085
  }
1018
1086
 
1019
1087
  /** Sets the error for the failed TLS connection `conn`, saying how it failed. */
1020
- failTls(conn: TlsRecordClient): void {
1088
+ private failTls(conn: TlsRecordClient): void {
1021
1089
  if (conn.ended && !conn.peerClosed) {
1022
1090
  this.fail(HTTP_E_TLS, "the server closed the connection without a TLS close_notify")
1023
1091
  } else if (conn.peerAlert > 0) {
@@ -1028,7 +1096,7 @@ export class FetchSyncClient {
1028
1096
  }
1029
1097
 
1030
1098
  /** Writes `bytes[off, off + len)` on the connection, through TLS for an https origin; false with the error set. */
1031
- writeAll(bytes: u8[], off: i32, len: i32): boolean {
1099
+ private writeAll(bytes: u8[], off: i32, len: i32): boolean {
1032
1100
  const conn: TlsRecordClient | null = this.tls
1033
1101
  if (conn === null) {
1034
1102
  return this.writeRaw(bytes, off, len)
@@ -1052,7 +1120,7 @@ export class FetchSyncClient {
1052
1120
  }
1053
1121
 
1054
1122
  /** Writes `bytes[off, off + len)` to `fd`, waiting up to `readTimeoutMs` each time it is full. */
1055
- writeRaw(bytes: u8[], off: i32, len: i32): boolean {
1123
+ private writeRaw(bytes: u8[], off: i32, len: i32): boolean {
1056
1124
  let at: i32 = off
1057
1125
  const end: i32 = off + len
1058
1126
  while (at < end) {
@@ -1081,7 +1149,7 @@ export class FetchSyncClient {
1081
1149
  * Looks `host` up and connects to its addresses in turn, every step within
1082
1150
  * `connectTimeoutMs` together: the connected fd, or -1 with the error set.
1083
1151
  */
1084
- dial(host: string, port: i32): i32 {
1152
+ private dial(host: string, port: i32): i32 {
1085
1153
  const start: i64 = monotonicNanos()
1086
1154
  const budget: i32 = this.options.connectTimeoutMs
1087
1155
  let until: i64 = budget > 0 ? start + toI64(budget) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
@@ -1161,7 +1229,7 @@ export class FetchSyncClient {
1161
1229
  * `HTTP_E_TOTAL_TIMEOUT` for whichever deadline passed, `HTTP_E_IO` when
1162
1230
  * the poll loop cannot be made.
1163
1231
  */
1164
- wait(fd: i32, events: i32, until: i64, code: i32): i32 {
1232
+ private wait(fd: i32, events: i32, until: i64, code: i32): i32 {
1165
1233
  if (!this.watch(fd, events)) {
1166
1234
  return HTTP_E_IO
1167
1235
  }
@@ -1190,7 +1258,7 @@ export class FetchSyncClient {
1190
1258
  }
1191
1259
 
1192
1260
  /** Puts `fd` in the poll loop, waiting for `events`: false when the loop cannot be made. */
1193
- watch(fd: i32, events: i32): boolean {
1261
+ private watch(fd: i32, events: i32): boolean {
1194
1262
  if (this.loop < 0) {
1195
1263
  this.loop = pollCreate()
1196
1264
  if (this.loop < 0) {
@@ -1220,7 +1288,7 @@ export class FetchSyncClient {
1220
1288
  * server that spoke while idle answered nothing yet asked, and is closing
1221
1289
  * or out of step, so the connection is not reused.
1222
1290
  */
1223
- quiet(): boolean {
1291
+ private quiet(): boolean {
1224
1292
  return (
1225
1293
  !this.leftover() &&
1226
1294
  this.watch(this.fd, HTTP_CLIENT_READABLE) &&
@@ -1229,7 +1297,7 @@ export class FetchSyncClient {
1229
1297
  }
1230
1298
 
1231
1299
  /** Whether bytes past the last response are held, in the parser or the TLS layer. */
1232
- leftover(): boolean {
1300
+ private leftover(): boolean {
1233
1301
  const conn: TlsRecordClient | null = this.tls
1234
1302
  if (conn !== null && (conn.plainEnd > conn.plainStart || conn.reader.end > conn.reader.start)) {
1235
1303
  return true
@@ -1238,7 +1306,7 @@ export class FetchSyncClient {
1238
1306
  }
1239
1307
 
1240
1308
  /** Takes `fd` out of the poll loop before it is closed. */
1241
- unwatch(fd: i32): void {
1309
+ private unwatch(fd: i32): void {
1242
1310
  if (this.watched === fd && this.watched >= 0) {
1243
1311
  pollRemove(this.loop, fd)
1244
1312
  this.watched = -1
@@ -1246,13 +1314,13 @@ export class FetchSyncClient {
1246
1314
  }
1247
1315
 
1248
1316
  /** When the wait for the server's next byte ends: `readTimeoutMs` from now. */
1249
- readDeadline(): i64 {
1317
+ private readDeadline(): i64 {
1250
1318
  const ms: i32 = this.options.readTimeoutMs
1251
1319
  return ms > 0 ? monotonicNanos() + toI64(ms) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
1252
1320
  }
1253
1321
 
1254
1322
  /** Closes the connection, if any, and forgets the open body. */
1255
- drop(): void {
1323
+ private drop(): void {
1256
1324
  const conn: TlsRecordClient | null = this.tls
1257
1325
  if (conn !== null) {
1258
1326
  conn.close()
@@ -1282,7 +1350,7 @@ export class FetchSyncClient {
1282
1350
  * Reads past a redirect's body so the connection can carry the next
1283
1351
  * request; a body too long to be worth reading, or any failure, closes it.
1284
1352
  */
1285
- drain(): void {
1353
+ private drain(): void {
1286
1354
  const sink: u8[] = new Array<u8>(HTTP_CLIENT_DRAIN_CHUNK)
1287
1355
  let left: i32 = HTTP_CLIENT_DRAIN_BYTES
1288
1356
  while (this.bodyOpen && left > 0) {
@@ -1299,7 +1367,7 @@ export class FetchSyncClient {
1299
1367
  this.errorText = ""
1300
1368
  }
1301
1369
 
1302
- /** `HttpBodyReader.read` for the response numbered `generation`. */
1370
+ /** `HttpBodyReader.read` for the response numbered `generation`. @internal */
1303
1371
  readBody(generation: i32, buf: u8[], off: i32, n: i32): i32 {
1304
1372
  if (generation !== this.generation) {
1305
1373
  // The client's error is the later fetch's; a stale reader leaves it be.
@@ -1337,7 +1405,7 @@ export class FetchSyncClient {
1337
1405
  }
1338
1406
 
1339
1407
  /** Closes the connection a body is done with, and `fetchSync`'s whole client with it. */
1340
- release(): void {
1408
+ private release(): void {
1341
1409
  if (this.once) {
1342
1410
  this.close()
1343
1411
  } else {
@@ -1345,7 +1413,7 @@ export class FetchSyncClient {
1345
1413
  }
1346
1414
  }
1347
1415
 
1348
- /** `readAll`'s refusal: the body passed the caller's limit. */
1416
+ /** `readAll`'s refusal: the body passed the caller's limit. @internal */
1349
1417
  refuseBody(limit: i32): void {
1350
1418
  this.release()
1351
1419
  this.fail(HTTP_E_BODY_TOO_LARGE, `the body is larger than the ${limit} bytes asked for`)
@@ -1356,7 +1424,7 @@ export class FetchSyncClient {
1356
1424
  * Fails with `HTTP_E_TOTAL_TIMEOUT` once the fetch's deadline has passed:
1357
1425
  * a server that always has the next byte ready never lets a wait see it.
1358
1426
  */
1359
- overdue(): boolean {
1427
+ private overdue(): boolean {
1360
1428
  if (this.deadline < HTTP_CLIENT_NEVER && monotonicNanos() >= this.deadline) {
1361
1429
  this.failWait(HTTP_E_TOTAL_TIMEOUT, "")
1362
1430
  return true
@@ -1365,7 +1433,7 @@ export class FetchSyncClient {
1365
1433
  }
1366
1434
 
1367
1435
  /** Sets the error for a wait that ended with `code`, `text` saying what timed out. */
1368
- failWait(code: i32, text: string): void {
1436
+ private failWait(code: i32, text: string): void {
1369
1437
  if (code === HTTP_E_TOTAL_TIMEOUT) {
1370
1438
  this.fail(code, `the fetch ran past ${httpClientTotalMs(this.options)} ms`)
1371
1439
  } else if (code === HTTP_E_IO) {
@@ -1376,13 +1444,13 @@ export class FetchSyncClient {
1376
1444
  }
1377
1445
 
1378
1446
  /** Refuses a redirect: closes the connection, whose response is left unread, and fails. */
1379
- refuse(code: i32, text: string): HttpResponse | null {
1447
+ private refuse(code: i32, text: string): HttpResponse | null {
1380
1448
  this.drop()
1381
1449
  this.fail(code, text)
1382
1450
  return null
1383
1451
  }
1384
1452
 
1385
- fail(code: i32, text: string): void {
1453
+ private fail(code: i32, text: string): void {
1386
1454
  this.error = code
1387
1455
  this.errorText = `${httpErrorName(code)}: ${text}`
1388
1456
  }
@@ -1392,6 +1460,10 @@ export class FetchSyncClient {
1392
1460
  * One fetch on a client of its own, closed once the body has been read or
1393
1461
  * the fetch failed. Null on a failure, with no way to say why: a program
1394
1462
  * that reports its errors fetches through a `FetchSyncClient`.
1463
+ *
1464
+ * @param req The request to send, as a `FetchSyncClient` takes it.
1465
+ * @param options The client's limits, timeouts, TLS trust and proxies for this one fetch.
1466
+ * @returns The response, the last one when redirects were followed, or null on any failure.
1395
1467
  */
1396
1468
  export const fetchSync = (req: HttpRequest, options: HttpOptions): HttpResponse | null => {
1397
1469
  const client: FetchSyncClient = new FetchSyncClient(options)
@@ -1406,6 +1478,11 @@ export const fetchSync = (req: HttpRequest, options: HttpOptions): HttpResponse
1406
1478
  /**
1407
1479
  * A proxy variable's value: `lower` when it is set and not empty, else
1408
1480
  * `upper` when `upperAllowed`, else "". `HTTP_PROXY` is not allowed under CGI.
1481
+ *
1482
+ * @param lower The lower-case variable's value, such as `http_proxy`'s, or null when it is unset.
1483
+ * @param upper The upper-case variable's value, such as `HTTP_PROXY`'s, or null when it is unset.
1484
+ * @param upperAllowed Whether the upper-case variable may be read: false for `HTTP_PROXY` under CGI, where a request's `Proxy` header sets it.
1485
+ * @returns The chosen variable's value, a proxy URL or for `no_proxy` a comma-separated host list, or "" when neither applies.
1409
1486
  */
1410
1487
  export const httpChooseProxy = (
1411
1488
  lower: string | null,
@@ -1425,6 +1502,14 @@ export const httpChooseProxy = (
1425
1502
  * The proxy a request to `scheme://host:port` goes through, given the three
1426
1503
  * settings: `httpsProxy` for https, `httpProxy` for http, and "" for none or
1427
1504
  * when `noProxy` names the host.
1505
+ *
1506
+ * @param scheme The request URL's scheme, lower-cased; one other than `http` or `https` gets no proxy.
1507
+ * @param host The request URL's host, matched against `noProxy`.
1508
+ * @param port The port the request goes to, matched against an entry's `:port`.
1509
+ * @param httpProxy The proxy URL for `http` requests, or "" for none.
1510
+ * @param httpsProxy The proxy URL for `https` requests, or "" for none.
1511
+ * @param noProxy The comma-separated hosts that go direct, as `httpNoProxyMatch` reads them.
1512
+ * @returns The proxy URL the request goes through, or "" when it goes direct.
1428
1513
  */
1429
1514
  export const httpProxyFor = (
1430
1515
  scheme: string,
@@ -1452,6 +1537,11 @@ export const httpProxyFor = (
1452
1537
  * (with or without a leading dot), or an IP address that matches only itself;
1453
1538
  * any of them may end in `:port`, and an IPv6 address with a port is written
1454
1539
  * in brackets. Case does not matter. Address ranges are not understood.
1540
+ *
1541
+ * @param noProxy The list, as `NO_PROXY` holds it; an entry that does not parse is skipped.
1542
+ * @param host The request's host name or IP address; trailing dots do not matter.
1543
+ * @param port The port the request goes to.
1544
+ * @returns True when an entry names the host (and its port, if it has one), so the request goes direct.
1455
1545
  */
1456
1546
  export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): boolean => {
1457
1547
  const name: string = httpClientTrimDots(httpClientLower(host))