@amritk/nish-x86_64-linux 0.34.1 → 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 (126) hide show
  1. package/INSTALL.md +22 -13
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +86 -16
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +49 -0
  6. package/runtime/async.mjs +27 -0
  7. package/runtime/nish.d.ts +4681 -427
  8. package/runtime/nish.h +6 -2
  9. package/runtime/nish.mjs +4 -0
  10. package/runtime/runtime-net.c +22 -0
  11. package/runtime/runtime-shm.c +93 -2
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +108 -13
  15. package/std/README.md +131 -131
  16. package/std/base64.ts +34 -5
  17. package/std/bytes.ts +258 -42
  18. package/std/cli-args.ts +296 -70
  19. package/std/cli-flags.ts +184 -19
  20. package/std/cli.ts +475 -54
  21. package/std/collections.ts +121 -23
  22. package/std/compress/checksum.ts +44 -14
  23. package/std/compress/deflate.ts +224 -111
  24. package/std/compress/gzip.ts +217 -53
  25. package/std/compress/zlib.ts +63 -7
  26. package/std/crypto/aes.ts +80 -9
  27. package/std/crypto/base64url.ts +22 -0
  28. package/std/crypto/bcrypt-batch.ts +88 -21
  29. package/std/crypto/bcrypt.ts +60 -0
  30. package/std/crypto/chacha20poly1305.ts +53 -0
  31. package/std/crypto/ct.ts +32 -13
  32. package/std/crypto/ed25519.ts +47 -0
  33. package/std/crypto/hkdf.ts +198 -13
  34. package/std/crypto/hmac.ts +235 -31
  35. package/std/crypto/jwt.ts +192 -63
  36. package/std/crypto/p256.ts +159 -16
  37. package/std/crypto/p384.ts +28 -3
  38. package/std/crypto/pki-roots.ts +32 -2
  39. package/std/crypto/pki.ts +81 -15
  40. package/std/crypto/rsa.ts +33 -3
  41. package/std/crypto/sha1.ts +3 -0
  42. package/std/crypto/sha256.ts +24 -1
  43. package/std/crypto/sha512.ts +99 -14
  44. package/std/crypto/x25519.ts +152 -14
  45. package/std/crypto/x509-random.ts +10 -0
  46. package/std/crypto/x509.ts +67 -7
  47. package/std/json-equal.ts +19 -0
  48. package/std/json-format.ts +112 -13
  49. package/std/json-number.ts +64 -14
  50. package/std/json-pointer.ts +23 -1
  51. package/std/json-tokenizer.ts +100 -29
  52. package/std/json-typed.ts +713 -66
  53. package/std/json-value.ts +400 -124
  54. package/std/json.ts +138 -10
  55. package/std/map.ts +25 -7
  56. package/std/math.ts +36 -4
  57. package/std/net/buffer-pool.ts +42 -11
  58. package/std/net/dns-config.ts +109 -23
  59. package/std/net/dns-message.ts +62 -7
  60. package/std/net/dns-resolver.ts +247 -53
  61. package/std/net/dns.ts +203 -35
  62. package/std/net/fetch.ts +151 -61
  63. package/std/net/handler.ts +278 -80
  64. package/std/net/hpack.ts +262 -52
  65. package/std/net/http-compress.ts +125 -15
  66. package/std/net/http-fields.ts +85 -9
  67. package/std/net/http1-client-codec.ts +100 -19
  68. package/std/net/http1-codec.ts +297 -77
  69. package/std/net/http1-server.ts +430 -94
  70. package/std/net/http2-frame.ts +272 -35
  71. package/std/net/http2-tls.ts +135 -34
  72. package/std/net/http2.ts +247 -106
  73. package/std/net/http3-frame.ts +154 -17
  74. package/std/net/http3-server.ts +275 -101
  75. package/std/net/http3.ts +307 -115
  76. package/std/net/outbound.ts +37 -3
  77. package/std/net/postgres-carrier.ts +144 -8
  78. package/std/net/postgres.ts +801 -112
  79. package/std/net/prefork.ts +164 -31
  80. package/std/net/qpack.ts +105 -18
  81. package/std/net/quic-conn-ack.ts +59 -12
  82. package/std/net/quic-conn-cid.ts +124 -27
  83. package/std/net/quic-conn-params.ts +70 -10
  84. package/std/net/quic-datagram.ts +43 -3
  85. package/std/net/quic-frame.ts +286 -12
  86. package/std/net/quic-listener.ts +101 -32
  87. package/std/net/quic-packet.ts +302 -21
  88. package/std/net/quic-recovery.ts +264 -52
  89. package/std/net/quic-stream.ts +353 -91
  90. package/std/net/quic.ts +1124 -201
  91. package/std/net/router.ts +87 -40
  92. package/std/net/tls/client.ts +166 -51
  93. package/std/net/tls/codec.ts +413 -56
  94. package/std/net/tls/record-client.ts +110 -55
  95. package/std/net/tls/record-server.ts +159 -59
  96. package/std/net/tls/record.ts +109 -12
  97. package/std/net/tls/schedule.ts +235 -26
  98. package/std/net/tls/ticket.ts +159 -40
  99. package/std/net/tls/verify.ts +49 -3
  100. package/std/net/tls-tcp.ts +250 -45
  101. package/std/net/tls.ts +221 -59
  102. package/std/net/upstream.ts +81 -45
  103. package/std/net/url.ts +97 -13
  104. package/std/net/websocket.ts +122 -12
  105. package/std/net/webtransport.ts +220 -89
  106. package/std/pair.ts +10 -2
  107. package/std/random.ts +34 -9
  108. package/std/regex.ts +352 -198
  109. package/std/region.ts +70 -15
  110. package/std/secret.ts +52 -11
  111. package/std/sort.ts +17 -2
  112. package/std/sqlite.ts +256 -35
  113. package/std/sync/ring.ts +430 -40
  114. package/std/sync/shm.ts +222 -15
  115. package/std/term-io.ts +137 -64
  116. package/std/term-pixels.ts +144 -19
  117. package/std/term-probe.ts +66 -25
  118. package/std/term-rate.ts +128 -57
  119. package/std/term.ts +332 -121
  120. package/std/testing.ts +130 -32
  121. package/std/text.ts +73 -7
  122. package/std/threads.ts +229 -41
  123. package/std/yaml-json.ts +94 -82
  124. package/std/yaml-lines.ts +47 -15
  125. package/std/yaml-write.ts +24 -16
  126. package/std/yaml.ts +764 -384
package/std/crypto/jwt.ts CHANGED
@@ -4,17 +4,26 @@
4
4
  * RFC 7518 (the HS, RS and ES algorithms), RFC 8037 (EdDSA) and the RFC 8725
5
5
  * best current practice.
6
6
  *
7
- * import { JWT_ALG_HS256, JWT_MAX_TOKEN, JWT_OK, JwtVerifier, jwtHmacKey, jwtSignString } from "nish/crypto/jwt";
7
+ * import { Secret } from "nish:secret";
8
+ * import { JWT_ALG_HS256, JWT_MAX_TOKEN, JWT_OK, JwtKey, JwtVerifier, jwtHmacKey, jwtSignString } from "nish/crypto/jwt";
8
9
  *
9
- * const key = jwtHmacKey(JWT_ALG_HS256, secretKey); // once; null for a short key
10
- * const v = new JwtVerifier(JWT_MAX_TOKEN); // once, per thread
11
- * v.issuer = "https://issuer.example";
12
- * v.audience = "api";
13
- * v.leeway = toI64(30);
14
- * if (key !== null && v.verifyString(key, token, now) === JWT_OK) {
15
- * // v.claims[0 .. v.claimsLen) is the payload's JSON; v.exp, v.nbf, v.iat are read
16
- * }
17
- * const minted: string | null = key === null ? null : jwtSignString(key, "{\"sub\":\"ana\"}");
10
+ * // secretKey: at least 32 random bytes, still the caller's to wipe; now: seconds since 1970
11
+ * const roundTrip = (secretKey: Secret<u8[]>, token: string, now: i64): string | null => {
12
+ * const key: JwtKey | null = jwtHmacKey(JWT_ALG_HS256, secretKey); // make once; null for a short key
13
+ * if (key === null) {
14
+ * return null;
15
+ * }
16
+ * const v = new JwtVerifier(JWT_MAX_TOKEN); // make once per thread
17
+ * v.issuer = "https://issuer.example";
18
+ * v.audience = "api";
19
+ * v.leeway = toI64(30);
20
+ * if (v.verifyString(key, token, now) === JWT_OK) {
21
+ * // v.claims[0 .. v.claimsLen) is the payload's JSON; v.exp, v.nbf, v.iat are read
22
+ * }
23
+ * const minted: string | null = jwtSignString(key, "{\"sub\":\"ana\"}");
24
+ * key.wipe();
25
+ * return minted;
26
+ * };
18
27
  *
19
28
  * **The key decides the algorithm, never the token.** A `JwtKey` is made for
20
29
  * one algorithm, and a token verifies only when its header's `alg` is that
@@ -167,7 +176,12 @@ const JWT_MAX_LEEWAY: i64 = 2147483647
167
176
  const JWT_MAX_NOW: i64 = 1073741824 * 1073741824 * 4
168
177
  const JWT_DOT: i32 = 46
169
178
 
170
- /** The `alg` header value for `alg`, `""` for no algorithm this module knows. */
179
+ /**
180
+ * The `alg` header value for `alg`, `""` for no algorithm this module knows.
181
+ *
182
+ * @param alg A `JWT_ALG_` constant.
183
+ * @returns The name RFC 7518 or RFC 8037 spells it with, such as `"HS256"`.
184
+ */
171
185
  export const jwtAlgName = (alg: i32): string => {
172
186
  if (alg === JWT_ALG_HS256) {
173
187
  return "HS256"
@@ -236,21 +250,35 @@ const jwtLength = (a: u8[]): i32 => toI32(a.length)
236
250
  * each worker its own.
237
251
  */
238
252
  export class JwtKey {
239
- /** An HS key's pads, keyed once; `null` for any other. */
253
+ /** @internal An HS key's pads, keyed once; `null` for any other. */
240
254
  readonly hmac: HmacKey | null
241
- /** An RS key; `null` for any other. */
255
+ /** @internal An RS key; `null` for any other. */
242
256
  readonly rsa: RsaPublicKey | null
243
- /** An ES key's uncompressed point or an EdDSA key's 32 bytes; empty for HS and RS. */
257
+ /** @internal An ES key's uncompressed point or an EdDSA key's 32 bytes; empty for HS and RS. */
244
258
  readonly pub: u8[]
245
- /** An ES256 or EdDSA signing key's private scalar or seed; empty for any other. */
259
+ /** @internal An ES256 or EdDSA signing key's private scalar or seed; empty for any other. */
246
260
  readonly priv: Secret<u8[]>
247
- /** The one algorithm this key verifies, `JWT_ALG_*`. */
261
+ /** @internal The one algorithm this key verifies, `JWT_ALG_*`. */
248
262
  readonly alg: i32
249
- /** Whether `jwtSign` signs with this key. */
263
+ /** @internal Whether `jwtSign` signs with this key. */
250
264
  readonly canSign: boolean
251
- /** Whether the key is in use: `wipe()` clears it, and a wiped key signs and verifies nothing. */
265
+ /** @internal Whether the key is in use: `wipe()` clears it, and a wiped key signs and verifies nothing. */
252
266
  live: boolean
253
267
 
268
+ /**
269
+ * A key from its parts, checked for nothing: the `jwt…Key` functions are
270
+ * the way to make one, and this exists for them and for tests that build a
271
+ * key no function would. `hmac`, `rsa` and `pub` are kept as given; `priv`
272
+ * is copied, and stays the caller's to wipe.
273
+ *
274
+ * @param alg The one algorithm the key verifies, a `JWT_ALG_` constant.
275
+ * @param hmac An HS key's keyed pads, or `null`.
276
+ * @param rsa An RS key, or `null`.
277
+ * @param pub An ES key's uncompressed point or an EdDSA key's 32 bytes;
278
+ * empty for any other.
279
+ * @param priv An ES256 private scalar or an EdDSA seed; empty for any other.
280
+ * @param canSign Whether `jwtSign` signs with the key.
281
+ */
254
282
  constructor(
255
283
  alg: i32,
256
284
  hmac: HmacKey | null,
@@ -301,6 +329,10 @@ const jwtPublicKey = (
301
329
  * a key at least as long as the hash, so a shorter one answers `null`, as does
302
330
  * an `alg` that is not HS. The pads are keyed once, here; `key` stays the
303
331
  * caller's to wipe.
332
+ *
333
+ * @param alg `JWT_ALG_HS256`, `JWT_ALG_HS384` or `JWT_ALG_HS512`.
334
+ * @param key The shared secret: at least 32, 48 or 64 bytes for the three.
335
+ * @returns A new key that verifies and signs, or `null`.
304
336
  */
305
337
  export const jwtHmacKey = (alg: i32, key: Secret<u8[]>): JwtKey | null => {
306
338
  let hash: i32 = 0
@@ -327,6 +359,10 @@ export const jwtHmacKey = (alg: i32, key: Secret<u8[]>): JwtKey | null => {
327
359
  * An RS256, RS384 or RS512 key from the DER of a PKCS#1 `RSAPublicKey`
328
360
  * (`rsaParsePublicKey`'s input). A key that does not parse, a modulus below
329
361
  * 2048 bits (RFC 7518 §3.3) or an `alg` that is not RS answers `null`.
362
+ *
363
+ * @param alg `JWT_ALG_RS256`, `JWT_ALG_RS384` or `JWT_ALG_RS512`.
364
+ * @param der The public key's DER, nothing before it and nothing after it.
365
+ * @returns A new key that verifies only, or `null`.
330
366
  */
331
367
  export const jwtRsaKey = (alg: i32, der: u8[]): JwtKey | null => {
332
368
  if (alg !== JWT_ALG_RS256 && alg !== JWT_ALG_RS384 && alg !== JWT_ALG_RS512) {
@@ -378,6 +414,11 @@ const jwtDerInteger = (out: u8[], v: u8[]): void => {
378
414
  * An RS key from its big-endian modulus and exponent, as a JWK's `n` and `e`
379
415
  * spell them once base64url-decoded (RFC 7518 §6.3.1). The same checks as
380
416
  * `jwtRsaKey`; parts too long for any key this module takes answer `null`.
417
+ *
418
+ * @param alg `JWT_ALG_RS256`, `JWT_ALG_RS384` or `JWT_ALG_RS512`.
419
+ * @param n The modulus, big-endian; leading zero bytes are dropped.
420
+ * @param e The public exponent, big-endian; leading zero bytes are dropped.
421
+ * @returns A new key that verifies only, or `null`.
381
422
  */
382
423
  export const jwtRsaKeyParts = (alg: i32, n: u8[], e: u8[]): JwtKey | null => {
383
424
  if (toI32(n.length) > 2048 || toI32(e.length) > 8 || toI32(n.length) === 0 || toI32(e.length) === 0) {
@@ -398,6 +439,10 @@ export const jwtRsaKeyParts = (alg: i32, n: u8[], e: u8[]): JwtKey | null => {
398
439
  * An ES256 or ES384 verifying key from its uncompressed point, `04 ‖ x ‖ y`
399
440
  * (65 or 97 bytes). A point of the wrong length or form answers `null`; one
400
441
  * off the curve makes every verification fail.
442
+ *
443
+ * @param alg `JWT_ALG_ES256` or `JWT_ALG_ES384`.
444
+ * @param point The public point, copied into the key.
445
+ * @returns A new key that verifies only, or `null`.
401
446
  */
402
447
  export const jwtEcKey = (alg: i32, point: u8[]): JwtKey | null => {
403
448
  let size: i32 = 0
@@ -412,7 +457,13 @@ export const jwtEcKey = (alg: i32, point: u8[]): JwtKey | null => {
412
457
  return jwtPublicKey(alg, null, null, jwtCopy(point), false)
413
458
  }
414
459
 
415
- /** An EdDSA (Ed25519) verifying key from its 32 bytes (RFC 8037 §2's `x`); another length answers `null`. */
460
+ /**
461
+ * An EdDSA (Ed25519) verifying key from its 32 bytes (RFC 8037 §2's `x`);
462
+ * another length answers `null`.
463
+ *
464
+ * @param pub The public key, copied into the key.
465
+ * @returns A new key that verifies only, or `null`.
466
+ */
416
467
  export const jwtEd25519Key = (pub: u8[]): JwtKey | null => {
417
468
  if (toI32(pub.length) !== 32) {
418
469
  return null
@@ -424,6 +475,9 @@ export const jwtEd25519Key = (pub: u8[]): JwtKey | null => {
424
475
  * An ES256 key that signs and verifies, from its 32-byte private scalar. A
425
476
  * scalar `p256PublicKey` refuses answers `null`. The key keeps a copy;
426
477
  * `priv` stays the caller's to wipe.
478
+ *
479
+ * @param priv The private scalar, big-endian.
480
+ * @returns A new key that signs and verifies, or `null`.
427
481
  */
428
482
  export const jwtEs256SigningKey = (priv: Secret<u8[]>): JwtKey | null => {
429
483
  const pub: u8[] | null = p256PublicKey(priv)
@@ -437,6 +491,9 @@ export const jwtEs256SigningKey = (priv: Secret<u8[]>): JwtKey | null => {
437
491
  * An EdDSA key that signs and verifies, from its 32-byte seed (RFC 8037 §2's
438
492
  * `d`). A seed of another length answers `null`. The key keeps a copy; `seed`
439
493
  * stays the caller's to wipe.
494
+ *
495
+ * @param seed The private seed.
496
+ * @returns A new key that signs and verifies, or `null`.
440
497
  */
441
498
  export const jwtEd25519SigningKey = (seed: Secret<u8[]>): JwtKey | null => {
442
499
  const pub: u8[] | null = ed25519PublicKey(seed)
@@ -612,22 +669,22 @@ const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
612
669
  */
613
670
  class JwtJson {
614
671
  /** The current integer's value, negated, when `integral` holds. */
615
- value: i64
616
- src: u8[]
617
- readonly stack: u8[]
618
- pos: i32
619
- end: i32
620
- depth: i32
621
- want: i32
622
- kind: i32
623
- failed: i32
672
+ private value: i64
673
+ private src: u8[]
674
+ private readonly stack: u8[]
675
+ private pos: i32
676
+ private end: i32
677
+ private depth: i32
678
+ private want: i32
679
+ private kind: i32
680
+ private failed: i32
624
681
  /** The current token's bytes, `src[tokStart, tokEnd)`; a string's and a key's include their quotes. */
625
- tokStart: i32
626
- tokEnd: i32
682
+ private tokStart: i32
683
+ private tokEnd: i32
627
684
  /** The current key's or string's length once decoded. */
628
- textBytes: i32
685
+ private textBytes: i32
629
686
  /** Whether the current number has no fraction or exponent and fits an `i64`. */
630
- integral: boolean
687
+ private integral: boolean
631
688
 
632
689
  /** A reader for values nested at most `maxDepth` containers deep; `reset` gives it a window. */
633
690
  constructor(src: u8[], maxDepth: i32) {
@@ -646,7 +703,7 @@ class JwtJson {
646
703
  this.integral = false
647
704
  }
648
705
 
649
- /** Starts over on `src[off, off + len)`; a window outside `src` fails the first `next()`. */
706
+ /** @internal Starts over on `src[off, off + len)`; a window outside `src` fails the first `next()`. */
650
707
  reset(src: u8[], off: i32, len: i32): void {
651
708
  this.src = src
652
709
  this.pos = off
@@ -659,19 +716,19 @@ class JwtJson {
659
716
  }
660
717
 
661
718
  /** The byte at `i`, or `-1` past the window. */
662
- at(i: i32): i32 {
719
+ private at(i: i32): i32 {
663
720
  return i >= 0 && i < this.end && i < toI32(this.src.length) ? toI32(this.src[i]) : -1
664
721
  }
665
722
 
666
723
  /** Fails the reader for good. */
667
- fail(): i32 {
724
+ private fail(): i32 {
668
725
  this.failed = JWT_JSON_ERR
669
726
  this.kind = JWT_JSON_ERR
670
727
  return JWT_JSON_ERR
671
728
  }
672
729
 
673
730
  /** Steps over the four blanks RFC 8259 §2 allows. */
674
- skipBlanks(): void {
731
+ private skipBlanks(): void {
675
732
  let b: i32 = this.at(this.pos)
676
733
  while (b === JWT_CH_SPACE || b === JWT_CH_TAB || b === JWT_CH_NEWLINE || b === JWT_CH_RETURN) {
677
734
  this.pos += 1
@@ -679,7 +736,7 @@ class JwtJson {
679
736
  }
680
737
  }
681
738
 
682
- /** The token after the current one, or `JWT_JSON_ERR`, answered again on every later call. */
739
+ /** @internal The token after the current one, or `JWT_JSON_ERR`, answered again on every later call. */
683
740
  next(): i32 {
684
741
  if (this.failed !== 0) {
685
742
  return this.failed
@@ -715,7 +772,7 @@ class JwtJson {
715
772
  }
716
773
 
717
774
  /** `b`, the closer of the innermost container, popped. */
718
- readClose(b: i32): i32 {
775
+ private readClose(b: i32): i32 {
719
776
  if (this.depth === 0) {
720
777
  return this.fail()
721
778
  }
@@ -733,12 +790,12 @@ class JwtJson {
733
790
  }
734
791
 
735
792
  /** What follows a complete value: nothing at the top, else a comma or a closer. */
736
- afterValue(): void {
793
+ private afterValue(): void {
737
794
  this.want = this.depth === 0 ? JWT_JSON_WANT_NOTHING : JWT_JSON_WANT_COMMA_OR_CLOSE
738
795
  }
739
796
 
740
797
  /** A member's name, its blanks and its colon. */
741
- readKey(): i32 {
798
+ private readKey(): i32 {
742
799
  if (this.at(this.pos) !== JWT_CH_QUOTE || !this.scanString()) {
743
800
  return this.fail()
744
801
  }
@@ -753,7 +810,7 @@ class JwtJson {
753
810
  }
754
811
 
755
812
  /** A value at the cursor: an opener, a string, a number or a literal. */
756
- readValue(): i32 {
813
+ private readValue(): i32 {
757
814
  const b: i32 = this.at(this.pos)
758
815
  if (b === JWT_CH_OPEN_BRACE || b === JWT_CH_OPEN_BRACKET) {
759
816
  if (this.depth >= toI32(this.stack.length)) {
@@ -786,7 +843,7 @@ class JwtJson {
786
843
  }
787
844
 
788
845
  /** Whether `word` is at the cursor, stepped over if so. */
789
- scanWord(word: string): boolean {
846
+ private scanWord(word: string): boolean {
790
847
  const n: i32 = toI32(word.length)
791
848
  for (let i: i32 = 0; i < n; i += 1) {
792
849
  if (this.at(this.pos + i) !== toI32(word.charCodeAt(i))) {
@@ -800,7 +857,7 @@ class JwtJson {
800
857
  }
801
858
 
802
859
  /** The digits at the cursor, accumulated negated into `value`; how many, and `integral` cleared on overflow. */
803
- scanDigits(bound: i64): i32 {
860
+ private scanDigits(bound: i64): i32 {
804
861
  const ten: i64 = toI64(10)
805
862
  let n: i32 = 0
806
863
  let b: i32 = this.at(this.pos)
@@ -826,7 +883,7 @@ class JwtJson {
826
883
  * `i64` for its sign; accumulating negated reaches -2^63, where a positive
827
884
  * number stops at 2^63 - 1.
828
885
  */
829
- scanNumber(): boolean {
886
+ private scanNumber(): boolean {
830
887
  this.tokStart = this.pos
831
888
  const negative: boolean = this.at(this.pos) === JWT_CH_MINUS
832
889
  if (negative) {
@@ -871,7 +928,7 @@ class JwtJson {
871
928
  * The code point of the escape at `i` (a backslash), a surrogate pair
872
929
  * joined, or `-1` for one RFC 8259 §7 refuses or a lone surrogate.
873
930
  */
874
- escapeCodePoint(i: i32): i32 {
931
+ private escapeCodePoint(i: i32): i32 {
875
932
  const c: i32 = this.at(i + 1)
876
933
  if (c === JWT_CH_QUOTE || c === JWT_CH_BACKSLASH || c === 47) {
877
934
  return c
@@ -913,7 +970,7 @@ class JwtJson {
913
970
  }
914
971
 
915
972
  /** The four hex digits at `i` as a number, or `-1`. */
916
- hex4(i: i32): i32 {
973
+ private hex4(i: i32): i32 {
917
974
  let v: i32 = 0
918
975
  for (let k: i32 = 0; k < 4; k += 1) {
919
976
  const d: i32 = jwtHexValue(this.at(i + k))
@@ -926,7 +983,7 @@ class JwtJson {
926
983
  }
927
984
 
928
985
  /** How many bytes the escape at `i` takes, given the code point it decodes to. */
929
- escapeLength(i: i32, cp: i32): i32 {
986
+ private escapeLength(i: i32, cp: i32): i32 {
930
987
  if (this.at(i + 1) !== 117) {
931
988
  return 2
932
989
  }
@@ -938,7 +995,7 @@ class JwtJson {
938
995
  * RFC 3629 §4 (no overlong form, no surrogate, nothing past U+10FFFF), or
939
996
  * `-1`.
940
997
  */
941
- utf8Tail(i: i32): i32 {
998
+ private utf8Tail(i: i32): i32 {
942
999
  const b: i32 = this.at(i)
943
1000
  let n: i32 = 0
944
1001
  let lo: i32 = 0x80
@@ -970,7 +1027,7 @@ class JwtJson {
970
1027
  }
971
1028
 
972
1029
  /** The string at the cursor, checked, stepped over, its decoded length in `textBytes`. */
973
- scanString(): boolean {
1030
+ private scanString(): boolean {
974
1031
  this.tokStart = this.pos
975
1032
  let i: i32 = this.pos + 1
976
1033
  let decoded: i32 = 0
@@ -1009,11 +1066,11 @@ class JwtJson {
1009
1066
  }
1010
1067
 
1011
1068
  /** Whether the current token is a key or a string. */
1012
- isText(): boolean {
1069
+ private isText(): boolean {
1013
1070
  return this.kind === JWT_JSON_KEY || this.kind === JWT_JSON_STRING
1014
1071
  }
1015
1072
 
1016
- /** The current key's or string's length once decoded, or `-1`. */
1073
+ /** @internal The current key's or string's length once decoded, or `-1`. */
1017
1074
  stringLength(): i32 {
1018
1075
  return this.isText() ? this.textBytes : -1
1019
1076
  }
@@ -1023,6 +1080,7 @@ class JwtJson {
1023
1080
  * `false` when the token is neither. Under Node, `charCodeAt` is a UTF-16
1024
1081
  * code unit, so only an ASCII name answers as it does natively, as with
1025
1082
  * `nish/json-tokenizer`'s `keyEquals`.
1083
+ * @internal
1026
1084
  */
1027
1085
  keyEquals(name: string): boolean {
1028
1086
  const n: i32 = toI32(name.length)
@@ -1059,6 +1117,7 @@ class JwtJson {
1059
1117
  * Writes the current key's or string's decoded bytes to `out` from `at` and
1060
1118
  * answers how many; `-1`, writing nothing, when the token is neither or
1061
1119
  * `out` has too little room.
1120
+ * @internal
1062
1121
  */
1063
1122
  stringInto(out: u8[], at: i32): i32 {
1064
1123
  if (!this.isText() || at < 0 || at > toI32(out.length) - this.textBytes) {
@@ -1090,12 +1149,12 @@ class JwtJson {
1090
1149
  return o - at
1091
1150
  }
1092
1151
 
1093
- /** Whether the current token is a number without fraction or exponent that fits an `i64`. */
1152
+ /** @internal Whether the current token is a number without fraction or exponent that fits an `i64`. */
1094
1153
  isInteger(): boolean {
1095
1154
  return this.kind === JWT_JSON_NUMBER && this.integral
1096
1155
  }
1097
1156
 
1098
- /** The current number as an `i64` when `isInteger()` holds, else `0`. */
1157
+ /** @internal The current number as an `i64` when `isInteger()` holds, else `0`. */
1099
1158
  numberI64(): i64 {
1100
1159
  if (!this.isInteger()) {
1101
1160
  return toI64(0)
@@ -1107,6 +1166,7 @@ class JwtJson {
1107
1166
  * Steps over the value the current token starts: after a key, its value;
1108
1167
  * after an opener, everything through its closer; after a scalar or a
1109
1168
  * closer, nothing. Answers the kind of the last token read, or `JWT_JSON_ERR`.
1169
+ * @internal
1110
1170
  */
1111
1171
  skipValue(): i32 {
1112
1172
  if (this.failed !== 0) {
@@ -1188,6 +1248,8 @@ const jwtVerifyPublic = (key: JwtKey, src: u8[], off: i32, len: i32, sig: u8[],
1188
1248
  * fields, read on every `verify`. After `JWT_OK`, `claims[0 .. claimsLen)`
1189
1249
  * is the payload's JSON for the caller's own claims, and `exp`, `nbf` and
1190
1250
  * `iat` hold the time claims when `hasExp`, `hasNbf` and `hasIat` say so.
1251
+ * `claims` and `sig` are the verifier's own buffers, not copies: the next
1252
+ * `verify` overwrites them.
1191
1253
  */
1192
1254
  export class JwtVerifier {
1193
1255
  /** Seconds of clock skew allowed to `exp`, `nbf` and `iat`; held to `[0, 2^31 - 1]`. */
@@ -1202,23 +1264,46 @@ export class JwtVerifier {
1202
1264
  typ: string | null
1203
1265
  /** The decoded payload of the last token, `claimsLen` bytes of it valid after `JWT_OK`. */
1204
1266
  readonly claims: u8[]
1267
+ /** The last token's `exp`, in seconds since 1970; meaningful only when `hasExp`. */
1205
1268
  exp: i64
1269
+ /** The last token's `nbf`, in seconds since 1970; meaningful only when `hasNbf`. */
1206
1270
  nbf: i64
1271
+ /** The last token's `iat`, in seconds since 1970; meaningful only when `hasIat`. */
1207
1272
  iat: i64
1208
- readonly part: u8[]
1273
+ private readonly part: u8[]
1274
+ /**
1275
+ * The last token's signature, decoded, at the front; `JWT_MAX_SIGNATURE`
1276
+ * bytes long, whatever the signature's own length. It is written only once
1277
+ * the header passes, so after a refusal before that it still holds an
1278
+ * earlier token's bytes.
1279
+ */
1209
1280
  readonly sig: u8[]
1210
- readonly text: u8[]
1211
- readonly tok: JwtJson
1281
+ private readonly text: u8[]
1282
+ private readonly tok: JwtJson
1212
1283
  /** The largest token taken, in bytes. */
1213
1284
  readonly maxToken: i32
1285
+ /** How many bytes of `claims` are the payload: its length after `JWT_OK`, 0 after any refusal. */
1214
1286
  claimsLen: i32
1215
1287
  /** Whether a token without `exp` is refused; on by default. */
1216
1288
  requireExp: boolean
1289
+ /**
1290
+ * Whether the last token's payload carried `exp`. Cleared at the start of
1291
+ * each `verify` and set as the claims are read, so a token refused for its
1292
+ * claims may leave it set.
1293
+ */
1217
1294
  hasExp: boolean
1295
+ /** Whether the last token's payload carried `nbf`, as `hasExp`. */
1218
1296
  hasNbf: boolean
1297
+ /** Whether the last token's payload carried `iat`, as `hasExp`. */
1219
1298
  hasIat: boolean
1220
1299
 
1221
- /** A verifier for tokens of up to `maxToken` bytes; outside `[16, JWT_MAX_TOKEN_LIMIT]` panics. */
1300
+ /**
1301
+ * A verifier for tokens of up to `maxToken` bytes; outside
1302
+ * `[16, JWT_MAX_TOKEN_LIMIT]` panics. Every buffer is allocated here, once.
1303
+ *
1304
+ * @param maxToken The longest token taken, in bytes; `JWT_MAX_TOKEN` is the
1305
+ * usual choice.
1306
+ */
1222
1307
  constructor(maxToken: i32) {
1223
1308
  if (maxToken < 16 || maxToken > JWT_MAX_TOKEN_LIMIT) {
1224
1309
  panic("JwtVerifier: maxToken outside [16, JWT_MAX_TOKEN_LIMIT]")
@@ -1247,8 +1332,13 @@ export class JwtVerifier {
1247
1332
 
1248
1333
  /**
1249
1334
  * `verify` on the bytes of `token`, copied into the verifier's own buffer
1250
- * first (a string's UTF-8 bytes); a token longer than `maxToken` is refused
1251
- * before the copy.
1335
+ * first; a token longer than `maxToken` is refused before the copy.
1336
+ *
1337
+ * @param key The key to verify with; it alone decides the algorithm.
1338
+ * @param token The compact token; a character above U+007F answers
1339
+ * `JWT_ERR_FORMAT`.
1340
+ * @param now The current time, in seconds since 1970.
1341
+ * @returns `JWT_OK`, or the reason the token was refused, as `verify`.
1252
1342
  */
1253
1343
  verifyString(key: JwtKey, token: string, now: i64): i32 {
1254
1344
  if (toI32(token.length) > this.maxToken) {
@@ -1271,7 +1361,25 @@ export class JwtVerifier {
1271
1361
  /**
1272
1362
  * Verifies the token `src[off .. off + len)` under `key` and checks its
1273
1363
  * claims at `now` (seconds since 1970): `JWT_OK`, or the first
1274
- * `JWT_ERR_*` that applies, in the order the module comment lists.
1364
+ * `JWT_ERR_*` that applies, in this order: the window (`JWT_ERR_SIZE`);
1365
+ * the three parts and the header's base64url (`JWT_ERR_FORMAT`); the
1366
+ * header's members in the order written (`JWT_ERR_HEADER`, `JWT_ERR_ALG`),
1367
+ * then a missing `alg` and, when the verifier names a `typ`, a missing or
1368
+ * other one; signature text too long for any signature
1369
+ * (`JWT_ERR_SIGNATURE`); the signature's base64url (`JWT_ERR_FORMAT`); its
1370
+ * length, the key's fit and the signature itself (`JWT_ERR_SIGNATURE`); the
1371
+ * payload's base64url (`JWT_ERR_FORMAT`); the payload's members in the
1372
+ * order written and, while `requireExp` is set (the default), a missing
1373
+ * `exp` (`JWT_ERR_CLAIMS`); and then `exp`,
1374
+ * `nbf`, `iat`, `iss`, `aud` and `sub`, in that order.
1375
+ *
1376
+ * @param key The key to verify with; it alone decides the algorithm, and a
1377
+ * wiped one verifies nothing.
1378
+ * @param src The bytes that hold the token; they are read, not kept.
1379
+ * @param off Where the token starts in `src`.
1380
+ * @param len The token's length in bytes.
1381
+ * @param now The current time, in seconds since 1970; held to `[0, 2^62]`.
1382
+ * @returns `JWT_OK`, or the reason the token was refused.
1275
1383
  */
1276
1384
  verify(key: JwtKey, src: u8[], off: i32, len: i32, now: i64): i32 {
1277
1385
  this.claimsLen = 0
@@ -1340,7 +1448,7 @@ export class JwtVerifier {
1340
1448
  }
1341
1449
 
1342
1450
  /** The header in `part[0 .. len)` checked against `key`'s algorithm and the verifier's `typ`. */
1343
- checkHeader(key: JwtKey, len: i32): i32 {
1451
+ private checkHeader(key: JwtKey, len: i32): i32 {
1344
1452
  const tok: JwtJson = this.tok
1345
1453
  tok.reset(this.part, toI32(0), len)
1346
1454
  if (tok.next() !== JWT_JSON_OBJECT_BEGIN) {
@@ -1401,7 +1509,7 @@ export class JwtVerifier {
1401
1509
  }
1402
1510
 
1403
1511
  /** The payload in `claims[0 .. len)` parsed and checked at `now`. */
1404
- checkClaims(len: i32, now: i64): i32 {
1512
+ private checkClaims(len: i32, now: i64): i32 {
1405
1513
  const tok: JwtJson = this.tok
1406
1514
  tok.reset(this.claims, toI32(0), len)
1407
1515
  if (tok.next() !== JWT_JSON_OBJECT_BEGIN) {
@@ -1523,6 +1631,12 @@ export class JwtVerifier {
1523
1631
  * or its `kid` does not fit `out`. This reads the header **before** any
1524
1632
  * signature is checked, so it only chooses which key to verify with; the
1525
1633
  * `kid` proves nothing until `verify` answers `JWT_OK` under that key.
1634
+ *
1635
+ * @param src The bytes that hold the token.
1636
+ * @param off Where the token starts in `src`.
1637
+ * @param len The token's length in bytes.
1638
+ * @param out Where the `kid` is written, unescaped.
1639
+ * @returns The `kid`'s length in bytes, or -1.
1526
1640
  */
1527
1641
  peekKid(src: u8[], off: i32, len: i32, out: u8[]): i32 {
1528
1642
  if (len < 0 || len > this.maxToken || off < 0 || off > toI32(src.length) - len) {
@@ -1617,6 +1731,11 @@ const jwtBytesOf = (text: string): u8[] => {
1617
1731
  * object, or when `kid` is empty, longer than 256 bytes, or holds a byte that
1618
1732
  * would need a JSON escape. The claims are signed as given: this function
1619
1733
  * neither adds nor checks any claim.
1734
+ *
1735
+ * @param key The key to sign with; a wiped one answers `null`.
1736
+ * @param kid The key id to name in the header, or `null` for none.
1737
+ * @param claims The payload: one JSON object, in bytes.
1738
+ * @returns The token, a new string, or `null`.
1620
1739
  */
1621
1740
  export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): string | null => {
1622
1741
  if (!key.canSign || !key.live || !jwtIsObject(claims)) {
@@ -1649,13 +1768,23 @@ export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): strin
1649
1768
  return input + "." + base64urlEncode(sig)
1650
1769
  }
1651
1770
 
1652
- /** `jwtSignKid` with no `kid`. */
1771
+ /**
1772
+ * `jwtSignKid` with no `kid`.
1773
+ *
1774
+ * @param key The key to sign with.
1775
+ * @param claims The payload: one JSON object, in bytes.
1776
+ * @returns The token, or `null` as `jwtSignKid` answers it.
1777
+ */
1653
1778
  export const jwtSign = (key: JwtKey, claims: u8[]): string | null => jwtSignKid(key, null, claims)
1654
1779
 
1655
1780
  /**
1656
1781
  * `jwtSign` over the bytes of the string `claims`. Answers `null` for claims
1657
1782
  * outside ASCII, as `verifyString` refuses them: escape such text (`\u00e9`)
1658
1783
  * or pass its UTF-8 bytes to `jwtSign`.
1784
+ *
1785
+ * @param key The key to sign with.
1786
+ * @param claims The payload: one JSON object, ASCII.
1787
+ * @returns The token, or `null` as `jwtSignKid` answers it.
1659
1788
  */
1660
1789
  export const jwtSignString = (key: JwtKey, claims: string): string | null =>
1661
1790
  jwtIsAscii(claims) ? jwtSignKid(key, null, jwtBytesOf(claims)) : null