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
@@ -164,9 +164,13 @@ export class UpstreamLimits {
164
164
  lookupMs: i32 = 5000
165
165
  /** Room for one request head: the request line, `Host` and the empty line. */
166
166
  maxHead: i32 = 8192
167
+ /** The longest status line a response may have, in bytes. */
167
168
  maxStatusLine: i32 = 8192
169
+ /** The most bytes a response's header section may take. */
168
170
  maxHeaderBytes: i32 = 65536
171
+ /** The most field lines a response may carry, trailers included. */
169
172
  maxHeaders: i32 = 100
173
+ /** The most bytes a response's trailer section may take. */
170
174
  maxTrailerBytes: i32 = 8192
171
175
  }
172
176
 
@@ -179,24 +183,33 @@ export class UpstreamResponse {
179
183
  req: HttpRequest
180
184
  /** The body, in `body[0 .. length)`; the buffer is reused, so read `length`, not `body.length`. */
181
185
  body: u8[]
182
- /** The request head, written by `get`. */
186
+ /** The request head, written by `get`. @internal */
183
187
  head: u8[]
184
- /** When the call fails `UPSTREAM_E_TIMEOUT`, on `monotonicNanos`'s clock. */
188
+ /** When the call fails `UPSTREAM_E_TIMEOUT`, on `monotonicNanos`'s clock. @internal */
185
189
  deadline: i64 = 0
186
190
  /** The final status code, once the response's head has arrived. */
187
191
  status: i32 = 0
188
192
  /** `UPSTREAM_E_NONE`, an `HTTP1_E_` refusal of the codec, or an `UPSTREAM_E_` code. */
189
193
  error: i32 = 0
194
+ /** How many bytes of `body` the response filled. */
190
195
  length: i32 = 0
196
+ /** @internal Call bookkeeping that `Upstream` reads and writes. */
191
197
  headLength: i32 = 0
198
+ /** @internal Call bookkeeping that `Upstream` reads and writes. */
192
199
  index: i32 = 0
193
200
  /** Whether the response was read to its end; `error` says why not. */
194
201
  ok: boolean = false
195
- /** Taken by the program and not yet released. */
202
+ /** Taken by the program and not yet released. @internal */
196
203
  taken: boolean = false
197
- /** Made once more already, after a reused connection dropped it unanswered. */
204
+ /** Made once more already, after a reused connection dropped it unanswered. @internal */
198
205
  retried: boolean = false
199
206
 
207
+ /**
208
+ * An empty call slot; `Upstream` makes its pool of them.
209
+ * @param index The slot's place in the pool.
210
+ * @param headRoom The most bytes the request head may take.
211
+ * @param placeholder The request the slot names until a call takes it.
212
+ */
200
213
  constructor(index: i32, headRoom: i32, placeholder: HttpRequest) {
201
214
  this.index = index
202
215
  this.req = placeholder
@@ -207,21 +220,25 @@ export class UpstreamResponse {
207
220
 
208
221
  /** One connection of the pool. */
209
222
  class UpstreamConnection {
223
+ /** @internal Connection state that `Upstream` reads and writes. */
210
224
  parser: Http1ResponseParser
225
+ /** @internal Connection state that `Upstream` reads and writes. */
211
226
  input: u8[]
227
+ /** @internal Connection state that `Upstream` reads and writes. */
212
228
  fd: i32 = -1
213
- /** Its handler watch id. */
229
+ /** Its handler watch id. @internal */
214
230
  id: i32 = 0
231
+ /** @internal Connection state that `Upstream` reads and writes. */
215
232
  state: i32 = 0
216
- /** The slot of the call it carries, or -1. */
233
+ /** The slot of the call it carries, or -1. @internal */
217
234
  call: i32 = -1
218
- /** How much of the call's head has been written. */
235
+ /** How much of the call's head has been written. @internal */
219
236
  sent: i32 = 0
220
- /** The poll interest it is registered with, 0 for none. */
237
+ /** The poll interest it is registered with, 0 for none. @internal */
221
238
  events: i32 = 0
222
- /** Whether the call it carries went out on a connection kept alive from an earlier call. */
239
+ /** Whether the call it carries went out on a connection kept alive from an earlier call. @internal */
223
240
  reused: boolean = false
224
- /** Whether any byte of the call's response has arrived. */
241
+ /** Whether any byte of the call's response has arrived. @internal */
225
242
  answered: boolean = false
226
243
 
227
244
  constructor(id: i32, parser: Http1ResponseParser) {
@@ -233,30 +250,30 @@ class UpstreamConnection {
233
250
 
234
251
  /** A keep-alive pool to one origin, on an `HttpHandler`'s loop. */
235
252
  export class Upstream {
236
- h: HttpHandler
237
- limits: UpstreamLimits
253
+ private h: HttpHandler
254
+ private limits: UpstreamLimits
238
255
  /** The one address the pool connects to, checked against the policy if there is one. */
239
- address: u8[]
240
- hostNames: string[]
241
- hostValues: string[]
242
- connections: UpstreamConnection[]
243
- calls: UpstreamResponse[]
244
- free: i32[]
256
+ private address: u8[]
257
+ private hostNames: string[]
258
+ private hostValues: string[]
259
+ private connections: UpstreamConnection[]
260
+ private calls: UpstreamResponse[]
261
+ private free: i32[]
245
262
  /** Calls written into their slot and waiting for a connection, oldest first. */
246
- waiting: i32[]
263
+ private waiting: i32[]
247
264
  /** Calls ended and not yet taken, in the order they ended. */
248
- done: i32[]
265
+ private done: i32[]
249
266
  /** Why the origin is unusable; empty when it is not. For a log. */
250
267
  errorText: string = ""
251
268
  /** The handler turn `pump` last read. */
252
- turn: i64 = -1
269
+ private turn: i64 = -1
253
270
  /** Not `UPSTREAM_E_NONE` when the origin is unusable: every call then fails with it. */
254
271
  error: i32 = 0
255
- freeCount: i32 = 0
256
- waitingStart: i32 = 0
257
- waitingCount: i32 = 0
258
- doneStart: i32 = 0
259
- doneCount: i32 = 0
272
+ private freeCount: i32 = 0
273
+ private waitingStart: i32 = 0
274
+ private waitingCount: i32 = 0
275
+ private doneStart: i32 = 0
276
+ private doneCount: i32 = 0
260
277
 
261
278
  /**
262
279
  * Parses `origin`, looks its host up once (this blocks for at most
@@ -264,6 +281,9 @@ export class Upstream {
264
281
  * when a cap is below 1 or the watch ids run past the handler's. An
265
282
  * unusable origin is not a panic: `error` says why, and every call fails
266
283
  * with it.
284
+ * @param h The handler whose poll loop the connections are watched on.
285
+ * @param origin The `http://` URL of the origin every call goes to.
286
+ * @param limits The pool's sizes, watch ids, deadlines and the codec's caps.
267
287
  */
268
288
  constructor(h: HttpHandler, origin: string, limits: UpstreamLimits) {
269
289
  if (limits.connections < 1 || limits.calls < 1 || limits.maxHead < 1 || limits.timeoutMs < 1) {
@@ -302,7 +322,7 @@ export class Upstream {
302
322
  }
303
323
 
304
324
  /** Settles the origin: its `Host` value and the one address, or `error`. */
305
- settle(origin: string): void {
325
+ private settle(origin: string): void {
306
326
  const url: Url | null = urlParse(origin)
307
327
  if (
308
328
  url === null ||
@@ -351,7 +371,7 @@ export class Upstream {
351
371
  copyBytes(this.address, 0, addresses, 0, DNS_ADDRESS_BYTES)
352
372
  }
353
373
 
354
- fail(code: i32, text: string): void {
374
+ private fail(code: i32, text: string): void {
355
375
  this.error = code
356
376
  this.errorText = text
357
377
  }
@@ -364,6 +384,9 @@ export class Upstream {
364
384
  * however it ends; `UPSTREAM_E_FULL` when every slot is taken, and then
365
385
  * nothing is held and `req` is still the program's to answer. `path` is
366
386
  * copied out before this returns.
387
+ * @param req The handler's request the call answers; it stays held until the call ends.
388
+ * @param path The origin's path to get, with its query, in origin form: it starts with `/`. An empty path, or one without the leading `/`, ends the call with `UPSTREAM_E_REQUEST`, which `take` reports, while `get` still returns `UPSTREAM_E_NONE`.
389
+ * @returns `UPSTREAM_E_NONE` when the call started, `UPSTREAM_E_FULL` when no slot is free.
367
390
  */
368
391
  get(req: HttpRequest, path: string): i32 {
369
392
  if (this.freeCount === 0) {
@@ -408,13 +431,19 @@ export class Upstream {
408
431
  return UPSTREAM_E_NONE
409
432
  }
410
433
 
411
- /** Whether a finished call is waiting for `take`; reads the upstream's sockets once per handler turn. */
434
+ /**
435
+ * Whether a finished call is waiting for `take`; reads the upstream's sockets once per handler turn.
436
+ * @returns Whether `take` has a call to hand back.
437
+ */
412
438
  ready(): boolean {
413
439
  this.pump()
414
440
  return this.doneCount > 0
415
441
  }
416
442
 
417
- /** The next finished call, which the program gives back with `release`. Panics when none is `ready`. */
443
+ /**
444
+ * The next finished call, which the program gives back with `release`. Panics when none is `ready`.
445
+ * @returns The call, with `ok`, `status` and its body, or `error` saying why it failed.
446
+ */
418
447
  take(): UpstreamResponse {
419
448
  if (!this.ready()) {
420
449
  panic("Upstream.take: no call is ready")
@@ -430,6 +459,7 @@ export class Upstream {
430
459
  * Gives a taken call's slot back to the pool. Panics for a call not taken
431
460
  * or already released: two calls sharing a slot would hand one request's
432
461
  * answer to another.
462
+ * @param r The call `take` handed back.
433
463
  */
434
464
  release(r: UpstreamResponse): void {
435
465
  if (!r.taken) {
@@ -440,12 +470,18 @@ export class Upstream {
440
470
  this.freeCount += 1
441
471
  }
442
472
 
443
- /** How many calls hold a slot: queued, in flight, or finished and not yet released. */
473
+ /**
474
+ * How many calls hold a slot: queued, in flight, or finished and not yet released.
475
+ * @returns The count of slots in use.
476
+ */
444
477
  pending(): i32 {
445
478
  return toI32(this.free.length) - this.freeCount
446
479
  }
447
480
 
448
- /** How many connections to the origin are open. */
481
+ /**
482
+ * How many connections to the origin are open.
483
+ * @returns The count of open connections.
484
+ */
449
485
  openConnections(): i32 {
450
486
  let n: i32 = 0
451
487
  for (let k: i32 = 0; k < toI32(this.connections.length); k += 1) {
@@ -459,7 +495,7 @@ export class Upstream {
459
495
  // ---- The loop ---------------------------------------------------------------
460
496
 
461
497
  /** Once per handler turn: reads and writes the connections that fired, then fails what is past its deadline. */
462
- pump(): void {
498
+ private pump(): void {
463
499
  if (this.h.turnAt === this.turn) {
464
500
  return
465
501
  }
@@ -488,7 +524,7 @@ export class Upstream {
488
524
  }
489
525
 
490
526
  /** Puts waiting calls on connections: a kept-alive one first, then a new one. */
491
- dispatch(): void {
527
+ private dispatch(): void {
492
528
  while (this.waitingCount > 0) {
493
529
  const c: UpstreamConnection | null = this.idle()
494
530
  if (c === null) {
@@ -516,7 +552,7 @@ export class Upstream {
516
552
  * is, else closed, else null. An idle connection the origin has closed or
517
553
  * sent bytes on is closed here, before a call is put on it.
518
554
  */
519
- idle(): UpstreamConnection | null {
555
+ private idle(): UpstreamConnection | null {
520
556
  let closed: UpstreamConnection | null = null
521
557
  for (let k: i32 = 0; k < toI32(this.connections.length); k += 1) {
522
558
  const c: UpstreamConnection = this.connections[k]
@@ -535,7 +571,7 @@ export class Upstream {
535
571
  * of stream or an error means the origin has given it up, and bytes no call
536
572
  * asked for would be read as the next call's response.
537
573
  */
538
- usable(c: UpstreamConnection): boolean {
574
+ private usable(c: UpstreamConnection): boolean {
539
575
  const start: i32 = 0
540
576
  if (netRead(c.fd, c.input, start, toI32(c.input.length)) === UPSTREAM_WOULD_BLOCK) {
541
577
  return true
@@ -545,7 +581,7 @@ export class Upstream {
545
581
  }
546
582
 
547
583
  /** Starts a connection for the call `c` holds; a failure ends the call. */
548
- connect(c: UpstreamConnection): void {
584
+ private connect(c: UpstreamConnection): void {
549
585
  const fd: i32 = tcpConnect(this.address)
550
586
  if (fd < 0) {
551
587
  this.finish(c, UPSTREAM_E_CONNECT)
@@ -565,7 +601,7 @@ export class Upstream {
565
601
  }
566
602
 
567
603
  /** What a connection's turn found ready. */
568
- service(c: UpstreamConnection): void {
604
+ private service(c: UpstreamConnection): void {
569
605
  if (c.state === UPSTREAM_C_CONNECTING) {
570
606
  if (connectResult(c.fd) !== 0) {
571
607
  this.finish(c, UPSTREAM_E_CONNECT)
@@ -581,7 +617,7 @@ export class Upstream {
581
617
  }
582
618
 
583
619
  /** Writes what the socket takes of the call's head, then waits to read. */
584
- send(c: UpstreamConnection): void {
620
+ private send(c: UpstreamConnection): void {
585
621
  const r: UpstreamResponse = this.calls[c.call]
586
622
  while (c.sent < r.headLength) {
587
623
  const n: i32 = netWrite(c.fd, r.head, c.sent, r.headLength - c.sent)
@@ -599,7 +635,7 @@ export class Upstream {
599
635
  }
600
636
 
601
637
  /** Reads what arrived and runs the codec over it. */
602
- receive(c: UpstreamConnection): void {
638
+ private receive(c: UpstreamConnection): void {
603
639
  while (c.fd >= 0) {
604
640
  const start: i32 = 0
605
641
  const n: i32 = netRead(c.fd, c.input, start, toI32(c.input.length))
@@ -630,7 +666,7 @@ export class Upstream {
630
666
  }
631
667
 
632
668
  /** Takes every event the codec has for the call `c` carries. */
633
- parse(c: UpstreamConnection): void {
669
+ private parse(c: UpstreamConnection): void {
634
670
  const p: Http1ResponseParser = c.parser
635
671
  while (c.call >= 0) {
636
672
  const event: i32 = p.next()
@@ -671,7 +707,7 @@ export class Upstream {
671
707
  * origin closed the connection as the request crossed it, and a `GET` is
672
708
  * safe to send again. Past its deadline it is not.
673
709
  */
674
- finish(c: UpstreamConnection, code: i32): void {
710
+ private finish(c: UpstreamConnection, code: i32): void {
675
711
  if (c.call >= 0) {
676
712
  const r: UpstreamResponse = this.calls[c.call]
677
713
  c.call = -1
@@ -691,7 +727,7 @@ export class Upstream {
691
727
  }
692
728
 
693
729
  /** Hands `r` to `ready` with `code`. */
694
- end(r: UpstreamResponse, code: i32): void {
730
+ private end(r: UpstreamResponse, code: i32): void {
695
731
  r.error = code
696
732
  r.ok = code === UPSTREAM_E_NONE
697
733
  if (!r.ok) {
@@ -703,14 +739,14 @@ export class Upstream {
703
739
  }
704
740
 
705
741
  /** Watches `c` for `events` alone. */
706
- interest(c: UpstreamConnection, events: i32): void {
742
+ private interest(c: UpstreamConnection, events: i32): void {
707
743
  if (c.events !== events) {
708
744
  pollModify(this.h.loop, c.fd, events, -2 - c.id)
709
745
  c.events = events
710
746
  }
711
747
  }
712
748
 
713
- close(c: UpstreamConnection): void {
749
+ private close(c: UpstreamConnection): void {
714
750
  if (c.fd >= 0) {
715
751
  pollRemove(this.h.loop, c.fd)
716
752
  netClose(c.fd)
package/std/net/url.ts CHANGED
@@ -207,6 +207,11 @@ const urlDecodeRun = (
207
207
  * The index of the first `?` in `bytes[off, off + len)`, or -1 when there is
208
208
  * none. The path is `[off, q)` and the query `(q, off + len)`; nothing is
209
209
  * copied. A `#` is not looked for: a request target holds no fragment.
210
+ *
211
+ * @param bytes The array holding the request target.
212
+ * @param off Where the target starts in `bytes`.
213
+ * @param len The target's length in bytes; a window outside `bytes` panics.
214
+ * @returns The index in `bytes` of the `?`, or -1 when the target has no query.
210
215
  */
211
216
  export const urlQueryStart = (bytes: u8[], off: i32, len: i32): i32 => {
212
217
  urlCheckWindow(bytes, off, len, "urlQueryStart")
@@ -230,6 +235,14 @@ export const urlQueryStart = (bytes: u8[], off: i32, len: i32): i32 => {
230
235
  * of room always suffice; with less, `URL_DECODE_ERR_SPACE` is answered before
231
236
  * any write at or past `out.length`. On an error the bytes before it have been
232
237
  * written. What is decoded is not checked as UTF-8.
238
+ *
239
+ * @param src The array holding the encoded text.
240
+ * @param off Where the encoded text starts in `src`.
241
+ * @param len The encoded text's length in bytes; a window outside `src` panics.
242
+ * @param out The array the decoded bytes are written into.
243
+ * @param at The index in `out` the first decoded byte goes to; one below 0 or above `out.length` panics.
244
+ * @param flags `URL_DECODE_FORM`, `URL_DECODE_LENIENT` and `URL_DECODE_ALLOW_NUL`, summed, or 0 for strict.
245
+ * @returns How many bytes were written from `out[at]` on, or `URL_DECODE_ERR_ESCAPE`, `URL_DECODE_ERR_NUL` or `URL_DECODE_ERR_SPACE`.
233
246
  */
234
247
  export const percentDecode = (src: u8[], off: i32, len: i32, out: u8[], at: i32, flags: i32): i32 =>
235
248
  urlDecodeRun(src, off, len, out, at, flags, false)
@@ -239,6 +252,14 @@ export const percentDecode = (src: u8[], off: i32, len: i32, out: u8[], at: i32,
239
252
  * fails at — the `%` of a malformed escape or of `%00`, or the first byte
240
253
  * `out` has no room for — or -1 when it does not fail. It writes what
241
254
  * `percentDecode` would have written before the error.
255
+ *
256
+ * @param src The array holding the encoded text.
257
+ * @param off Where the encoded text starts in `src`.
258
+ * @param len The encoded text's length in bytes; a window outside `src` panics.
259
+ * @param out The array the decoded bytes are written into, as `percentDecode` writes them.
260
+ * @param at The index in `out` the first decoded byte goes to; one below 0 or above `out.length` panics.
261
+ * @param flags The `URL_DECODE_*` sum `percentDecode` was given.
262
+ * @returns The index in `src` decoding stops at, or -1 when all of it decodes.
242
263
  */
243
264
  export const percentDecodeErrorAt = (src: u8[], off: i32, len: i32, out: u8[], at: i32, flags: i32): i32 =>
244
265
  urlDecodeRun(src, off, len, out, at, flags, true)
@@ -247,6 +268,14 @@ export const percentDecodeErrorAt = (src: u8[], off: i32, len: i32, out: u8[], a
247
268
  * `percentDecode` for one path segment: `+` is literal whatever `flags` say,
248
269
  * and `%2F` is a 0x2F byte in the segment. Split the path with
249
270
  * `UrlPathSegments` first, then decode each segment.
271
+ *
272
+ * @param src The array holding the path.
273
+ * @param off Where the segment starts in `src`, as `UrlPathSegments` gives it.
274
+ * @param len The segment's length in bytes; a window outside `src` panics.
275
+ * @param out The array the decoded bytes are written into.
276
+ * @param at The index in `out` the first decoded byte goes to; one below 0 or above `out.length` panics.
277
+ * @param flags A `URL_DECODE_*` sum; `URL_DECODE_FORM` in it is ignored.
278
+ * @returns How many bytes were written from `out[at]` on, or a negative `URL_DECODE_ERR_*` code, as `percentDecode` answers.
250
279
  */
251
280
  export const percentDecodePathSegment = (
252
281
  src: u8[],
@@ -265,21 +294,27 @@ export const percentDecodePathSegment = (
265
294
  * `percentDecodePathSegment`.
266
295
  */
267
296
  export class UrlPathSegments {
297
+ /** The array the path is read from, as `reset` was given it. */
268
298
  bytes: u8[]
269
299
  /** The current segment's first byte. */
270
300
  start: i32 = 0
271
301
  /** The current segment's length in raw bytes. */
272
302
  length: i32 = 0
273
- pos: i32 = 0
274
- end: i32 = 0
275
- done: boolean = true
303
+ private pos: i32 = 0
304
+ private end: i32 = 0
305
+ private done: boolean = true
276
306
 
277
307
  /** An iterator over nothing until `reset`. */
278
308
  constructor() {
279
309
  this.bytes = new Array<u8>(0)
280
310
  }
281
311
 
282
- /** Starts over on the path `bytes[off, off + len)`; panics when the window is outside the array. */
312
+ /**
313
+ * Starts over on the path `bytes[off, off + len)`; panics when the window is outside the array.
314
+ * @param bytes The array that holds the path.
315
+ * @param off Where the path starts in `bytes`.
316
+ * @param len How many bytes the path takes.
317
+ */
283
318
  reset(bytes: u8[], off: i32, len: i32): void {
284
319
  urlCheckWindow(bytes, off, len, "UrlPathSegments")
285
320
  this.bytes = bytes
@@ -293,7 +328,10 @@ export class UrlPathSegments {
293
328
  }
294
329
  }
295
330
 
296
- /** Moves to the next segment and answers true, or answers false when the path has no more. */
331
+ /**
332
+ * Moves to the next segment, whose bytes `start` and `length` then name.
333
+ * @returns True for a segment, false when the path has no more.
334
+ */
297
335
  next(): boolean {
298
336
  if (this.done) {
299
337
  return false
@@ -333,27 +371,40 @@ export class UrlQueryLimits {
333
371
  * with `percentDecode` and `URL_DECODE_FORM`.
334
372
  */
335
373
  export class UrlQuery {
374
+ /** @internal Read by `urlQueryFind`. */
336
375
  bytes: u8[]
376
+ /** Where the current pair's key starts in `bytes`. */
337
377
  keyStart: i32 = 0
378
+ /** The current pair's key length in raw bytes. */
338
379
  keyLength: i32 = 0
380
+ /** Where the current pair's value starts in `bytes`. */
339
381
  valueStart: i32 = 0
382
+ /** The current pair's value length in raw bytes, 0 when it has none. */
340
383
  valueLength: i32 = 0
341
384
  /** 0, or the `URL_QUERY_ERR_*` code every later `next()` answers. */
342
385
  failed: i32 = 0
343
- maxPairs: i32
344
- pos: i32 = 0
345
- end: i32 = 0
346
- count: i32 = 0
386
+ private maxPairs: i32
387
+ private pos: i32 = 0
388
+ private end: i32 = 0
389
+ private count: i32 = 0
347
390
  /** False for a pair written without `=` (`a`), true for `a=` with `valueLength` 0. */
348
391
  hasValue: boolean = false
349
392
 
350
- /** An iterator over nothing until `reset`, with the cap in `limits`. */
393
+ /**
394
+ * An iterator over nothing until `reset`, with the cap in `limits`.
395
+ * @param limits The most pairs one query yields.
396
+ */
351
397
  constructor(limits: UrlQueryLimits) {
352
398
  this.bytes = new Array<u8>(0)
353
399
  this.maxPairs = limits.maxPairs < 0 ? 0 : limits.maxPairs
354
400
  }
355
401
 
356
- /** Starts over on the query `bytes[off, off + len)`, clearing `failed`; panics when the window is outside the array. */
402
+ /**
403
+ * Starts over on the query `bytes[off, off + len)`, clearing `failed`; panics when the window is outside the array.
404
+ * @param bytes The array that holds the query.
405
+ * @param off Where the query starts in `bytes`, past its `?`.
406
+ * @param len How many bytes the query takes.
407
+ */
357
408
  reset(bytes: u8[], off: i32, len: i32): void {
358
409
  urlCheckWindow(bytes, off, len, "UrlQuery")
359
410
  this.bytes = bytes
@@ -368,7 +419,10 @@ export class UrlQuery {
368
419
  this.hasValue = false
369
420
  }
370
421
 
371
- /** `URL_QUERY_PAIR` with the windows set to the next pair, `URL_QUERY_END`, or `failed`. */
422
+ /**
423
+ * Moves to the next pair, setting the key and value windows.
424
+ * @returns `URL_QUERY_PAIR` for a pair, `URL_QUERY_END` when the query has no more, or `failed`.
425
+ */
372
426
  next(): i32 {
373
427
  if (this.failed !== 0) {
374
428
  return this.failed
@@ -457,6 +511,10 @@ const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean =>
457
511
  * matches nothing. Call it again for the next pair with the same key.
458
512
  * `name` is ASCII: natively it is compared with the key's bytes, under Node
459
513
  * with its UTF-16 code units, so a non-ASCII name answers differently.
514
+ *
515
+ * @param q The query iterator, moved on past the pair found; its windows bound that pair.
516
+ * @param name The key to look for, as it reads decoded.
517
+ * @returns `URL_QUERY_PAIR` when a pair with the key was found, `URL_QUERY_END` when none is left, or the negative `URL_QUERY_ERR_*` code `next()` answered.
460
518
  */
461
519
  export const urlQueryFind = (q: UrlQuery, name: string): i32 => {
462
520
  let k: i32 = q.next()
@@ -555,6 +613,10 @@ const urlDecodeString = (src: u8[], off: i32, len: i32, flags: i32): string | nu
555
613
  * or what it decodes to is not valid UTF-8, which is where
556
614
  * `decodeURIComponent` throws. `s` is expected to be ASCII, as a URL on the
557
615
  * wire is. It allocates its answer.
616
+ *
617
+ * @param s The encoded text; a code unit past 0xFF (possible only under Node) answers null.
618
+ * @param flags `URL_DECODE_FORM`, `URL_DECODE_LENIENT` and `URL_DECODE_ALLOW_NUL`, summed, or 0 for strict.
619
+ * @returns The decoded text, or null for a refused escape or `%00`, or text that is not UTF-8.
558
620
  */
559
621
  export const urlDecode = (s: string, flags: i32): string | null => {
560
622
  const bytes: u8[] | null = urlBytesOf(s)
@@ -570,6 +632,10 @@ export const urlDecode = (s: string, flags: i32): string | null => {
570
632
  * `URLSearchParams.get` answers, and null when the target has no query, the
571
633
  * key is absent, the value is malformed or not UTF-8, or the key comes after
572
634
  * `URL_DEFAULT_MAX_PAIRS` pairs. It allocates its answer.
635
+ *
636
+ * @param target A request target such as `/search?q=nish`; everything after its first `?` is the query.
637
+ * @param name The key to look for, as it reads decoded; ASCII, as `urlQueryFind` asks.
638
+ * @returns The first decoded value for `name`, `""` when it has no `=`, or null in the cases above.
573
639
  */
574
640
  export const urlQueryGet = (target: string, name: string): string | null => {
575
641
  const bytes: u8[] | null = urlBytesOf(target)
@@ -1000,6 +1066,9 @@ const urlFinish = (u: Url): Url | null => {
1000
1066
  * digits or is above 65535. An http, https, ws or wss URL needs a non-empty
1001
1067
  * host. The path's dot segments are removed, as RFC 3986 §5.2.2 removes them
1002
1068
  * from a reference with a scheme. Nothing is decoded. It allocates its answer.
1069
+ *
1070
+ * @param s The absolute URL, such as `https://example.com/a?b#c`.
1071
+ * @returns The URL's parts, still percent-encoded, `scheme` and `host` lower-cased; or null when `s` is refused.
1003
1072
  */
1004
1073
  export const urlParse = (s: string): Url | null => {
1005
1074
  const u: Url | null = urlReadReference(s)
@@ -1028,6 +1097,10 @@ const urlMerge = (base: Url, path: string): string => {
1028
1097
  * breaks a rule `urlParse` names or what it resolves to is an http, https, ws
1029
1098
  * or wss URL without a host. `base`'s fragment is never carried over. It
1030
1099
  * allocates its answer and does not change `base`.
1100
+ *
1101
+ * @param base The URL `ref` is relative to, as `urlParse` made it.
1102
+ * @param ref The reference: an absolute URL, a network-path `//host/p`, an absolute path, a relative path, a query or a fragment.
1103
+ * @returns The resolved URL, still percent-encoded, or null when `ref` is refused or resolves to a URL without a host its scheme needs.
1031
1104
  */
1032
1105
  export const urlResolve = (base: Url, ref: string): Url | null => {
1033
1106
  const r: Url | null = urlReadReference(ref)
@@ -1066,6 +1139,9 @@ const urlHostText = (u: Url): string => (u.host.indexOf(":") >= 0 ? "[" + u.host
1066
1139
  * The userinfo is written when either half is non-empty. A path that starts
1067
1140
  * with `//` under no authority is written after `/.`, as the WHATWG URL
1068
1141
  * Standard writes it, so the string does not read back with an authority.
1142
+ *
1143
+ * @param u The URL to write; its parts are written as they are held, so still percent-encoded.
1144
+ * @returns The URL as one string, scheme to fragment.
1069
1145
  */
1070
1146
  export const urlSerialize = (u: Url): string => {
1071
1147
  const parts: string[] = []
@@ -1096,7 +1172,12 @@ export const urlSerialize = (u: Url): string => {
1096
1172
  return parts.join("")
1097
1173
  }
1098
1174
 
1099
- /** The port a scheme uses when a URL names none: 80 for `http` and `ws`, 443 for `https` and `wss`, else -1. */
1175
+ /**
1176
+ * The port a scheme uses when a URL names none.
1177
+ *
1178
+ * @param scheme The scheme, lower-cased and without its `:`, as `Url.scheme` holds it.
1179
+ * @returns 80 for `http` and `ws`, 443 for `https` and `wss`, and -1 for any other scheme.
1180
+ */
1100
1181
  export const urlDefaultPort = (scheme: string): i32 => {
1101
1182
  if (scheme === "http" || scheme === "ws") {
1102
1183
  return 80
@@ -1115,6 +1196,9 @@ export const urlDefaultPort = (scheme: string): i32 => {
1115
1196
  * Hosts are compared as written, lower-cased, so `[::1]` and `[0::1]` are
1116
1197
  * different origins, and so are `a.com.` and `a.com`: equal strings are the
1117
1198
  * same origin, and a split errs toward "different".
1199
+ *
1200
+ * @param u The URL whose origin is wanted.
1201
+ * @returns The origin, such as `https://example.com:443`, or null for a scheme with no default port.
1118
1202
  */
1119
1203
  export const urlOrigin = (u: Url): string | null => {
1120
1204
  const fallback: i32 = urlDefaultPort(u.scheme)