@amritk/nish-aarch64-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
@@ -4,6 +4,7 @@
4
4
  * PEM, and mint the short-lived self-signed ECDSA P-256 certificate a browser
5
5
  * pins by its SHA-256.
6
6
  *
7
+ * import { Secret, wipe } from "nish:secret";
7
8
  * import { x509MintSelfSigned, x509CertificateHash, derToPem } from "nish/crypto/x509";
8
9
  * // Native only: a program that imports this does not compile for wasm32, which has no entropy.
9
10
  * import { x509DrawP256Key, x509DrawSerial } from "nish/crypto/x509-random";
@@ -36,9 +37,9 @@
36
37
  * validator needs: subjectAltName, basicConstraints, keyUsage, extKeyUsage,
37
38
  * nameConstraints and the two key identifiers. A duplicate or malformed one
38
39
  * refuses the certificate, and any other critical one sets `unknownCritical`.
39
- * It parses and does not validate: no module in `std/` does RFC 5280 §6 path
40
- * validation yet, so applying these is the caller's. The mint writes two extensions of its
41
- * own (below).
40
+ * It parses and does not validate: applying these to a chain is RFC 5280 §6
41
+ * path validation, which `nish/crypto/pki`'s `pkiVerifyServer` does. The mint
42
+ * writes two extensions of its own (below).
42
43
  *
43
44
  * **PEM is RFC 7468's strict grammar**, with standard base64 (`+` and `/`,
44
45
  * padded with `=`), not `base64url`: 64 characters a line and a shorter last
@@ -214,6 +215,7 @@ export class X509GeneralSubtree {
214
215
  /** For a directoryName, the Name's DER, whole. */
215
216
  dirName: u8[]
216
217
 
218
+ /** A subtree with no form and no name; `x509ParseCertificate` fills each one it reads. */
217
219
  constructor() {
218
220
  this.ip = []
219
221
  this.mask = []
@@ -302,6 +304,11 @@ export class X509Certificate {
302
304
  /** Whether a critical extension is one this module does not decode: RFC 5280 §4.2 has a verifier refuse it. */
303
305
  unknownCritical: boolean = false
304
306
 
307
+ /**
308
+ * A certificate with every field empty, zero, `false` or `null`, and
309
+ * `bcPathLen` and `keyUsage` at -1 for absent; `x509ParseCertificate` fills
310
+ * one in.
311
+ */
305
312
  constructor() {
306
313
  this.der = []
307
314
  this.tbs = []
@@ -323,10 +330,13 @@ export class X509Certificate {
323
330
 
324
331
  /** One element's identifier octet and where its contents lie: `der[start .. end)`. */
325
332
  class X509DerElement {
333
+ /** @internal */
326
334
  tag: i32
327
- /** Where the identifier octet is, so the whole element can be copied. */
335
+ /** @internal Where the identifier octet is, so the whole element can be copied. */
328
336
  at: i32
337
+ /** @internal */
329
338
  start: i32
339
+ /** @internal */
330
340
  end: i32
331
341
 
332
342
  constructor(tag: i32, at: i32, start: i32, end: i32) {
@@ -755,8 +765,11 @@ const x509DerHashAlgorithm = (der: u8[], start: i32, end: i32): i32 => {
755
765
 
756
766
  /** The signature algorithm `X509Certificate` reports, and for RSASSA-PSS its hash and salt length. */
757
767
  class X509SigAlg {
768
+ /** @internal */
758
769
  alg: i32 = 0
770
+ /** @internal */
759
771
  hash: i32 = 0
772
+ /** @internal */
760
773
  salt: i32 = 0
761
774
  }
762
775
 
@@ -899,6 +912,10 @@ const x509DerUnsignedInto = (der: u8[], e: X509DerElement, out: u8[], off: i32,
899
912
  * 2^(8 * width), and `width` is 32 (P-256) or 48 (P-384). This is the form a
900
913
  * TLS `CertificateVerify` or an X.509 signature carries; that each value is
901
914
  * below the curve's order is the curve's check.
915
+ *
916
+ * @param sig The DER signature, read and not kept.
917
+ * @param width The octets each of `r` and `s` is padded to: 32 for P-256, 48 for P-384.
918
+ * @returns A fresh array of `2 * width` octets, `r` then `s`, or `null`.
902
919
  */
903
920
  export const x509DerSignatureRSWidth = (sig: u8[], width: i32): u8[] | null => {
904
921
  if (width !== 32 && width !== 48) {
@@ -927,6 +944,9 @@ export const x509DerSignatureRSWidth = (sig: u8[], width: i32): u8[] | null => {
927
944
  /**
928
945
  * `x509DerSignatureRSWidth` for P-256: `r || s`, 32 octets each, the form
929
946
  * `nish/crypto/p256` takes.
947
+ *
948
+ * @param sig The DER signature, read and not kept.
949
+ * @returns A fresh 64-octet array, `r` then `s`, or `null`.
930
950
  */
931
951
  export const x509DerSignatureRS = (sig: u8[]): u8[] | null => x509DerSignatureRSWidth(sig, 32)
932
952
 
@@ -1117,6 +1137,10 @@ const x509EndsWith = (text: string, suffix: string): boolean => {
1117
1137
  * `der` as a PEM block (RFC 7468 §2): `-----BEGIN label-----`, the base64 of
1118
1138
  * the DER in lines of 64 characters, and `-----END label-----`, each line
1119
1139
  * ending in a line feed.
1140
+ *
1141
+ * @param der The bytes to encode; any length, empty included.
1142
+ * @param label The text between `BEGIN ` and the dashes, such as `CERTIFICATE`; written as given, unchecked.
1143
+ * @returns The PEM text, ending in a line feed.
1120
1144
  */
1121
1145
  export const derToPem = (der: u8[], label: string): string => {
1122
1146
  const body: string = base64StdPadEncode(der)
@@ -1136,7 +1160,12 @@ export const derToPem = (der: u8[], label: string): string => {
1136
1160
  * an unterminated block, an END whose label is not its BEGIN's, a line longer
1137
1161
  * than 64 characters or a short line before the last, a header line, or
1138
1162
  * base64 that is not canonical. Blocks with other labels must be as well
1139
- * formed, and are skipped; so is text outside the blocks.
1163
+ * formed, and are skipped; so is text outside the blocks, though an
1164
+ * `-----END` line outside a block answers `null`. A line may end in CRLF.
1165
+ *
1166
+ * @param pem The PEM text.
1167
+ * @param label The label to collect, compared byte for byte.
1168
+ * @returns A fresh array holding each matching block's DER, freshly decoded, or `null`.
1140
1169
  */
1141
1170
  export const pemToDer = (pem: string, label: string): u8[][] | null => {
1142
1171
  const out: u8[][] = []
@@ -1352,6 +1381,9 @@ const x509WipeBlocks = (blocks: u8[][] | null): void => {
1352
1381
  * and answers `null`. Every block decoded from `pem` is wiped before this
1353
1382
  * returns, whichever answer it gives; `pem` itself is the caller's string,
1354
1383
  * which nothing can wipe (X509-7 in docs/security/crypto-x509.md).
1384
+ *
1385
+ * @param pem The PEM text holding the key.
1386
+ * @returns The 32-byte scalar, big-endian, in a fresh `Secret` the caller wipes, or `null`.
1355
1387
  */
1356
1388
  export const x509ParseP256PrivateKey = (pem: string): Secret<u8[]> | null => {
1357
1389
  const sec1: u8[][] | null = pemToDer(pem, "EC PRIVATE KEY")
@@ -1415,6 +1447,10 @@ const x509DerNameSound = (der: u8[], e: X509DerElement): boolean => {
1415
1447
  * each attribute — is not implemented (X509-12 in docs/security/crypto-x509.md),
1416
1448
  * so two Names spelled differently that it would match answer `false`. That
1417
1449
  * fails closed: a chain whose issuer and subject differ only so is not built.
1450
+ *
1451
+ * @param a One Name's DER, whole, as `X509Certificate.issuer` or `subject` holds it.
1452
+ * @param b The other Name's DER.
1453
+ * @returns Whether the two are byte for byte equal.
1418
1454
  */
1419
1455
  export const x509NameEqual = (a: u8[], b: u8[]): boolean => x509BytesEqual(a, b)
1420
1456
 
@@ -1424,6 +1460,10 @@ export const x509NameEqual = (a: u8[], b: u8[]): boolean => x509BytesEqual(a, b)
1424
1460
  * `name`'s RDN at the same place, byte for byte — how a directoryName
1425
1461
  * constraint (RFC 5280 §4.2.1.10) is matched. An empty `base` is a prefix of
1426
1462
  * every Name. `false` when either is not a Name.
1463
+ *
1464
+ * @param base The constraint's Name, whole DER.
1465
+ * @param name The Name tested against it, whole DER.
1466
+ * @returns Whether `base` is a prefix of `name`.
1427
1467
  */
1428
1468
  export const x509NameIsPrefix = (base: u8[], name: u8[]): boolean => {
1429
1469
  const b: X509DerElement | null = x509DerExpect(base, 0, toI32(base.length), X509_TAG_SEQUENCE)
@@ -1998,7 +2038,10 @@ const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate):
1998
2038
  * and what its extensions say. It does not check the signature
1999
2039
  * (`x509VerifySignature` does), that `notBefore` is not after `notAfter` or
2000
2040
  * that either is near any clock, or what the extensions mean for a chain,
2001
- * which is a path validator's to apply.
2041
+ * which is a path validator's to apply: `nish/crypto/pki`'s `pkiVerifyServer`.
2042
+ *
2043
+ * @param der One certificate's DER, read and not kept.
2044
+ * @returns A new certificate whose byte fields are copies of `der`'s, or `null`.
2002
2045
  */
2003
2046
  export const x509ParseCertificate = (der: u8[]): X509Certificate | null => {
2004
2047
  const total: i32 = toI32(der.length)
@@ -2143,7 +2186,10 @@ export const x509ParseCertificate = (der: u8[]): X509Certificate | null => {
2143
2186
  * clock, nor what each one's basicConstraints, keyUsage and name constraints
2144
2187
  * allow, nor a trust anchor. Nothing here does RFC 5280 §6's path
2145
2188
  * validation, and a chain this answers is no evidence that anyone
2146
- * vouches for the leaf.
2189
+ * vouches for the leaf; `nish/crypto/pki`'s `pkiVerifyServer` is what does.
2190
+ *
2191
+ * @param pem The PEM text, such as a server's chain file.
2192
+ * @returns A fresh array of new certificates, or `null`.
2147
2193
  */
2148
2194
  export const x509ParseChain = (pem: string): X509Certificate[] | null => {
2149
2195
  const blocks: u8[][] | null = pemToDer(pem, "CERTIFICATE")
@@ -2169,6 +2215,10 @@ export const x509ParseChain = (pem: string): X509Certificate[] | null => {
2169
2215
  * `issuer.subject` and that the times hold are fields the caller compares, and
2170
2216
  * whether `issuer` may issue at all (`bcCa`, `keyUsage`) is a path
2171
2217
  * validator's to decide, so a `true` here is not path validation.
2218
+ *
2219
+ * @param cert The certificate whose signature is checked.
2220
+ * @param issuer The certificate whose key is meant to have made it; `cert` itself for a self-signed one.
2221
+ * @returns Whether the signature verifies.
2172
2222
  */
2173
2223
  export const x509VerifySignature = (cert: X509Certificate, issuer: X509Certificate): boolean => {
2174
2224
  if (!cert.ecdsaSha256 || !issuer.p256) {
@@ -2219,6 +2269,13 @@ const x509MintExtensions = (): u8[] => [
2219
2269
  * `serial` — big-endian, leading zeros ignored — is zero or
2220
2270
  * needs more than `X509_MAX_SERIAL` octets as a positive INTEGER (clear the top
2221
2271
  * bit of 20 random bytes, or use fewer).
2272
+ *
2273
+ * @param priv The 32-byte P-256 private scalar, big-endian; read, and left for the caller to wipe.
2274
+ * @param commonName The subject's and issuer's common name, 1 to 64 bytes of UTF-8 without a NUL.
2275
+ * @param notBeforeMs When validity starts, in milliseconds since 1970-01-01 UTC.
2276
+ * @param days How long it is valid, 1 to `X509_MAX_DAYS`.
2277
+ * @param serial The serial number's octets, big-endian.
2278
+ * @returns The certificate's DER, a fresh array, or `null`.
2222
2279
  */
2223
2280
  export const x509MintSelfSigned = (
2224
2281
  priv: Secret<u8[]>,
@@ -2295,5 +2352,8 @@ export const x509MintSelfSigned = (
2295
2352
  * The SHA-256 of a certificate's DER: the value WebTransport's
2296
2353
  * `serverCertificateHashes` pins, and what `openssl x509 -outform DER |
2297
2354
  * sha256sum` prints.
2355
+ *
2356
+ * @param der The certificate's DER, whole.
2357
+ * @returns The 32-byte digest, a fresh array.
2298
2358
  */
2299
2359
  export const x509CertificateHash = (der: u8[]): u8[] => sha256(der)
package/std/json-equal.ts CHANGED
@@ -144,6 +144,12 @@ const jsonEqualNumbers = (a: string, b: string): boolean => {
144
144
  * JSON Schema's rule (the module header): numbers by value, objects whatever
145
145
  * their member order. The two may be one document. `false` when either
146
146
  * handle names no value. Iterative, so any depth compares.
147
+ *
148
+ * @param da - the document that holds `a`.
149
+ * @param a - a value handle from `da`.
150
+ * @param db - the document that holds `b`, which may be `da` itself.
151
+ * @param b - a value handle from `db`.
152
+ * @returns true when the two values are equal as JSON Schema compares them.
147
153
  */
148
154
  export const jsonEqual = (da: JsonDocument, a: i32, db: JsonDocument, b: i32): boolean => {
149
155
  const left: i32[] = [a]
@@ -281,6 +287,11 @@ const jsonEqualCanonicalText = (doc: JsonDocument, v: i32): string => {
281
287
  * members in key order, each key a length, `:` and the key (orders and lengths
282
288
  * as the module header says).
283
289
  * `""` for a handle that names no value.
290
+ *
291
+ * @param doc - the document that holds `v`.
292
+ * @param v - the value to write out.
293
+ * @returns the canonical text, which two values share exactly when `jsonEqual`
294
+ * calls them equal.
284
295
  */
285
296
  export const jsonEqualCanonical = (doc: JsonDocument, v: i32): string => jsonEqualCanonicalText(doc, v)
286
297
 
@@ -290,6 +301,10 @@ export const jsonEqualCanonical = (doc: JsonDocument, v: i32): string => jsonEqu
290
301
  * Node, so a string past U+00FF hashes differently there), unkeyed (the
291
302
  * module header says what that means for a table). The offset basis for a handle that names no
292
303
  * value.
304
+ *
305
+ * @param doc - the document that holds `v`.
306
+ * @param v - the value to hash.
307
+ * @returns the hash, the same for any two values `jsonEqual` calls equal.
293
308
  */
294
309
  export const jsonEqualHash = (doc: JsonDocument, v: i32): u64 => {
295
310
  const text: string = jsonEqualCanonicalText(doc, v)
@@ -317,6 +332,10 @@ export const jsonEqualHash = (doc: JsonDocument, v: i32): u64 => {
317
332
  * earlier one, or `-1` when every element is distinct or `v` is not an array:
318
333
  * JSON Schema's `uniqueItems` holds exactly when this is `-1`. Linear in the
319
334
  * elements read, through a `Map` of their canonical texts.
335
+ *
336
+ * @param doc - the document that holds `v`.
337
+ * @param v - the array to search.
338
+ * @returns the index of the first repeated element, or `-1` when there is none.
320
339
  */
321
340
  export const jsonEqualFirstDuplicate = (doc: JsonDocument, v: i32): i32 => {
322
341
  if (doc.kind(v) !== JSON_VALUE_ARRAY) {
@@ -238,14 +238,32 @@ const jfFullTimeFrom = (s: string, at: i32): boolean => {
238
238
  return second !== 60 || (hour * 60 + minute - offset + 1440) % 1440 === 1439
239
239
  }
240
240
 
241
- /** Whether `s` is an RFC 3339 `date-time`, `1963-06-19T08:30:06.283185Z`. */
241
+ /**
242
+ * Whether `s` is an RFC 3339 `date-time`, `1963-06-19T08:30:06.283185Z`.
243
+ *
244
+ * @param s - the text to check, whole: nothing before or after the date-time is
245
+ * allowed.
246
+ * @returns true when `s` is a date-time, false otherwise.
247
+ */
242
248
  export const isDateTime = (s: string): boolean =>
243
249
  jfFullDateAt(s, 0) && jfLower(jfUnitAt(s, 10)) === 116 && jfFullTimeFrom(s, 11)
244
250
 
245
- /** Whether `s` is an RFC 3339 `full-date`, `1963-06-19`. */
251
+ /**
252
+ * Whether `s` is an RFC 3339 `full-date`, `1963-06-19`.
253
+ *
254
+ * @param s - the text to check, whole: nothing before or after the date is
255
+ * allowed.
256
+ * @returns true when `s` is a date, false otherwise.
257
+ */
246
258
  export const isDate = (s: string): boolean => toI32(s.length) === 10 && jfFullDateAt(s, 0)
247
259
 
248
- /** Whether `s` is an RFC 3339 `full-time`, `08:30:06Z` or `08:30:06+01:00`. */
260
+ /**
261
+ * Whether `s` is an RFC 3339 `full-time`, `08:30:06Z` or `08:30:06+01:00`.
262
+ *
263
+ * @param s - the text to check, whole: nothing before or after the time is
264
+ * allowed.
265
+ * @returns true when `s` is a time, false otherwise.
266
+ */
249
267
  export const isTime = (s: string): boolean => jfFullTimeFrom(s, 0)
250
268
 
251
269
  /** The place of unit `u` in `units`, or -1 if it is not one of them. */
@@ -267,6 +285,10 @@ const jfUnitIndex = (u: i32, units: string): i32 => {
267
285
  * 1*DIGIT "Y" [dur-month]`), so the units of each half must be consecutive
268
286
  * in `YMD` and `HMS` order: `P1Y1D` and `PT1H1S` are invalid as written,
269
287
  * though ISO 8601 allows them.
288
+ *
289
+ * @param s - the text to check, whole: nothing before or after the duration is
290
+ * allowed.
291
+ * @returns true when `s` is a duration, false otherwise.
270
292
  */
271
293
  export const isDuration = (s: string): boolean => {
272
294
  const length: i32 = toI32(s.length)
@@ -386,10 +408,22 @@ const jfIpv6Range = (s: string, from: i32, to: i32): boolean => {
386
408
  return elided ? groups <= 7 : groups === 8
387
409
  }
388
410
 
389
- /** Whether `s` is a dotted-quad IPv4 address, `192.168.0.1`. */
411
+ /**
412
+ * Whether `s` is a dotted-quad IPv4 address, `192.168.0.1`.
413
+ *
414
+ * @param s - the text to check, whole: nothing before or after the IPv4 address
415
+ * is allowed.
416
+ * @returns true when `s` is an IPv4 address, false otherwise.
417
+ */
390
418
  export const isIpv4 = (s: string): boolean => jfIpv4Range(s, 0, toI32(s.length))
391
419
 
392
- /** Whether `s` is an IPv6 address, `::ffff:192.168.0.1`. */
420
+ /**
421
+ * Whether `s` is an IPv6 address, `::ffff:192.168.0.1`.
422
+ *
423
+ * @param s - the text to check, whole: nothing before or after the IPv6 address
424
+ * is allowed.
425
+ * @returns true when `s` is an IPv6 address, false otherwise.
426
+ */
393
427
  export const isIpv6 = (s: string): boolean => jfIpv6Range(s, 0, toI32(s.length))
394
428
 
395
429
  const jfIsLdh = (c: i32): boolean => jfIsAlpha(c) || jfIsDigit(c) || c === 45
@@ -416,7 +450,13 @@ const jfHostnameRange = (s: string, from: i32, to: i32): boolean => {
416
450
  return true
417
451
  }
418
452
 
419
- /** Whether `s` is a host name, `www.example.com`. */
453
+ /**
454
+ * Whether `s` is a host name, `www.example.com`.
455
+ *
456
+ * @param s - the text to check, whole: nothing before or after the host name is
457
+ * allowed.
458
+ * @returns true when `s` is a host name, false otherwise.
459
+ */
420
460
  export const isHostname = (s: string): boolean => jfHostnameRange(s, 0, toI32(s.length))
421
461
 
422
462
  /** Whether `c` is RFC 5322's `atext`: a letter, a digit or one of ``!#$%&'*+-/=?^_`{|}~``. */
@@ -501,7 +541,13 @@ const jfGeneralLiteral = (s: string, from: i32, to: i32): boolean => {
501
541
  return true
502
542
  }
503
543
 
504
- /** Whether `s` is an RFC 5321 mailbox, `joe.bloggs@example.com`. */
544
+ /**
545
+ * Whether `s` is an RFC 5321 mailbox, `joe.bloggs@example.com`.
546
+ *
547
+ * @param s - the text to check, whole: nothing before or after the mailbox is
548
+ * allowed.
549
+ * @returns true when `s` is a mailbox, false otherwise.
550
+ */
505
551
  export const isEmail = (s: string): boolean => {
506
552
  const length: i32 = toI32(s.length)
507
553
  const at = jfLocalPartEnd(s)
@@ -533,7 +579,13 @@ export const isEmail = (s: string): boolean => {
533
579
  return jfIpv4Range(s, inner, end) || jfGeneralLiteral(s, inner, end)
534
580
  }
535
581
 
536
- /** Whether `s` is a UUID, `2eb8aa08-aa98-11ea-b4aa-73b441d16380`. */
582
+ /**
583
+ * Whether `s` is a UUID, `2eb8aa08-aa98-11ea-b4aa-73b441d16380`.
584
+ *
585
+ * @param s - the text to check, whole: nothing before or after the UUID is
586
+ * allowed.
587
+ * @returns true when `s` is a UUID, false otherwise.
588
+ */
537
589
  export const isUuid = (s: string): boolean => {
538
590
  if (toI32(s.length) !== 36) {
539
591
  return false
@@ -813,16 +865,40 @@ const jfUriCheck = (s: string, iri: boolean, reference: boolean): boolean => {
813
865
  return true
814
866
  }
815
867
 
816
- /** Whether `s` is an absolute URI, `http://example.com/a?b#c`. */
868
+ /**
869
+ * Whether `s` is an absolute URI, `http://example.com/a?b#c`.
870
+ *
871
+ * @param s - the text to check, whole: nothing before or after the URI is
872
+ * allowed.
873
+ * @returns true when `s` is a URI, false otherwise.
874
+ */
817
875
  export const isUri = (s: string): boolean => jfUriCheck(s, false, false)
818
876
 
819
- /** Whether `s` is a URI or a relative reference, `../a?b#c`. */
877
+ /**
878
+ * Whether `s` is a URI or a relative reference, `../a?b#c`.
879
+ *
880
+ * @param s - the text to check, whole: nothing before or after the URI
881
+ * reference is allowed.
882
+ * @returns true when `s` is a URI reference, false otherwise.
883
+ */
820
884
  export const isUriReference = (s: string): boolean => jfUriCheck(s, false, true)
821
885
 
822
- /** Whether `s` is an absolute IRI, `http://ƒøø.ßår/?∂éœ=πîx`. */
886
+ /**
887
+ * Whether `s` is an absolute IRI, `http://ƒøø.ßår/?∂éœ=πîx`.
888
+ *
889
+ * @param s - the text to check, whole: nothing before or after the IRI is
890
+ * allowed.
891
+ * @returns true when `s` is an IRI, false otherwise.
892
+ */
823
893
  export const isIri = (s: string): boolean => jfUriCheck(s, true, false)
824
894
 
825
- /** Whether `s` is an IRI or a relative IRI reference. */
895
+ /**
896
+ * Whether `s` is an IRI or a relative IRI reference.
897
+ *
898
+ * @param s - the text to check, whole: nothing before or after the IRI
899
+ * reference is allowed.
900
+ * @returns true when `s` is an IRI reference, false otherwise.
901
+ */
826
902
  export const isIriReference = (s: string): boolean => jfUriCheck(s, true, true)
827
903
 
828
904
  /** The code units of RFC 6570's `varchar` at `i` of `s`, `ALPHA / DIGIT / "_" / pct-encoded`, or 0. */
@@ -909,7 +985,14 @@ const jfIsTemplateLiteral = (c: i32): boolean =>
909
985
  (c >= 97 && c <= 122) ||
910
986
  c === 126
911
987
 
912
- /** Whether `s` is an RFC 6570 URI template, `http://example.com/{term:1}/{term}`. */
988
+ /**
989
+ * Whether `s` is an RFC 6570 URI template,
990
+ * `http://example.com/{term:1}/{term}`.
991
+ *
992
+ * @param s - the text to check, whole: nothing before or after the URI template
993
+ * is allowed.
994
+ * @returns true when `s` is a URI template, false otherwise.
995
+ */
913
996
  export const isUriTemplate = (s: string): boolean => {
914
997
  const length: i32 = toI32(s.length)
915
998
  let i: i32 = 0
@@ -945,12 +1028,20 @@ export const isUriTemplate = (s: string): boolean => {
945
1028
  * Whether `s` is a regular expression `nish/regex` compiles with no flags.
946
1029
  * Lookaround, backreferences and what the `u` flag refuses are answered
947
1030
  * false, though ECMA-262 accepts them (see the module header).
1031
+ *
1032
+ * @param s - the pattern source, without slashes or flags.
1033
+ * @returns true when `nish/regex` compiles `s`, false otherwise.
948
1034
  */
949
1035
  export const isRegex = (s: string): boolean => regexCompile(s, "").isOk()
950
1036
 
951
1037
  /**
952
1038
  * The id of the format `name` for `jsonFormatCheck`, one of the
953
1039
  * `JSON_FORMAT_*` ids, or -1 for a format this module does not check.
1040
+ *
1041
+ * @param name - the name a schema's `format` keyword gives, such as
1042
+ * `"date-time"`.
1043
+ * @returns the format's `JSON_FORMAT_*` id, or -1 when this module does not
1044
+ * check that format.
954
1045
  */
955
1046
  export const jsonFormatId = (name: string): i32 => {
956
1047
  if (name === "date-time") {
@@ -1052,6 +1143,10 @@ const jfPasses = (id: i32, value: string): boolean => {
1052
1143
  * `value` checked against the format `id` from `jsonFormatId`:
1053
1144
  * `JSON_FORMAT_VALID` or `JSON_FORMAT_INVALID`, or `JSON_FORMAT_UNKNOWN`
1054
1145
  * for an id that names no format, -1 included.
1146
+ *
1147
+ * @param id - a format id from `jsonFormatId`.
1148
+ * @param value - the string instance to check.
1149
+ * @returns `JSON_FORMAT_VALID`, `JSON_FORMAT_INVALID` or `JSON_FORMAT_UNKNOWN`.
1055
1150
  */
1056
1151
  export const jsonFormatCheck = (id: i32, value: string): i32 => {
1057
1152
  if (id < JSON_FORMAT_DATE_TIME || id > JSON_FORMAT_REGEX) {
@@ -1065,6 +1160,10 @@ export const jsonFormatCheck = (id: i32, value: string): i32 => {
1065
1160
  * `JSON_FORMAT_INVALID`, or `JSON_FORMAT_UNKNOWN` for a format this module
1066
1161
  * does not check. A validator that checks many instances against one schema
1067
1162
  * resolves the name once with `jsonFormatId` and calls `jsonFormatCheck`.
1163
+ *
1164
+ * @param name - the name a schema's `format` keyword gives.
1165
+ * @param value - the string instance to check.
1166
+ * @returns `JSON_FORMAT_VALID`, `JSON_FORMAT_INVALID` or `JSON_FORMAT_UNKNOWN`.
1068
1167
  */
1069
1168
  export const jsonFormatValidate = (name: string, value: string): i32 =>
1070
1169
  jsonFormatCheck(jsonFormatId(name), value)
@@ -83,25 +83,57 @@ const jsonNumberIsDigit = (c: i32): boolean => c >= JSON_NUMBER_DIGIT_0 && c <=
83
83
  * stepping over the point.
84
84
  */
85
85
  class JsonNumberParts {
86
- text: string
87
- /** `0` when the text is a number, else a `JSON_NUMBER_ERR_*` code. */
86
+ private text: string
87
+ /**
88
+ * `0` when the text is a number, else a `JSON_NUMBER_ERR_*` code.
89
+ *
90
+ * @internal Read by the `jsonNumber*` free functions of this module; the
91
+ * class is not exported.
92
+ */
88
93
  code: i32
94
+ /**
95
+ * @internal Read by the `jsonNumber*` free functions of this module; the
96
+ * class is not exported.
97
+ */
89
98
  negative: boolean
90
- /** Whether every digit is zero, `-0` and `0.0e5` included. */
99
+ /**
100
+ * Whether every digit is zero, `-0` and `0.0e5` included.
101
+ *
102
+ * @internal Read by the `jsonNumber*` free functions of this module; the
103
+ * class is not exported.
104
+ */
91
105
  zero: boolean
92
106
  /** Where the integer part's digits start, and how many there are. */
93
- intStart: i32
94
- intLen: i32
107
+ private intStart: i32
108
+ private intLen: i32
95
109
  /** Where the fraction's digits start, after the point, and how many there are. */
96
- fracStart: i32
97
- fracLen: i32
110
+ private fracStart: i32
111
+ private fracLen: i32
98
112
  /** The first significant digit's place among the integer and fraction digits run together. */
99
- first: i32
100
- /** How many significant digits there are, from the first non-zero one to the last. */
113
+ private first: i32
114
+ /**
115
+ * How many significant digits there are, from the first non-zero one to the
116
+ * last.
117
+ *
118
+ * @internal Read by the `jsonNumber*` free functions of this module; the
119
+ * class is not exported.
120
+ */
101
121
  count: i32
102
- /** The decimal exponent of the first significant digit: `3` for `1234`, `-2` for `0.05`. */
122
+ /**
123
+ * The decimal exponent of the first significant digit: `3` for `1234`, `-2`
124
+ * for `0.05`.
125
+ *
126
+ * @internal Read by the `jsonNumber*` free functions of this module; the
127
+ * class is not exported.
128
+ */
103
129
  lead: i64
104
- /** The decimal exponent of the last significant digit: `0` for `1234`, `2` for `1200`, `-2` for `0.05`. */
130
+ /**
131
+ * The decimal exponent of the last significant digit: `0` for `1234`, `2` for
132
+ * `1200`, `-2` for `0.05`.
133
+ *
134
+ * @internal Read by the `jsonNumber*` free functions of this module; the
135
+ * class is not exported.
136
+ */
105
137
  last: i64
106
138
 
107
139
  constructor(text: string) {
@@ -120,19 +152,24 @@ class JsonNumberParts {
120
152
  this.code = this.read()
121
153
  }
122
154
 
123
- /** The k-th significant digit, `0 <= k < count`, as `0` to `9`. */
155
+ /**
156
+ * The k-th significant digit, `0 <= k < count`, as `0` to `9`.
157
+ *
158
+ * @internal Read by the `jsonNumber*` free functions of this module; the
159
+ * class is not exported.
160
+ */
124
161
  digit(k: i32): i32 {
125
162
  return this.placeDigit(this.first + k)
126
163
  }
127
164
 
128
165
  /** The digit at place `p` of the integer and fraction digits run together. */
129
- placeDigit(p: i32): i32 {
166
+ private placeDigit(p: i32): i32 {
130
167
  const at: i32 = p < this.intLen ? this.intStart + p : this.fracStart + (p - this.intLen)
131
168
  return toI32(this.text.charCodeAt(at)) - JSON_NUMBER_DIGIT_0
132
169
  }
133
170
 
134
171
  /** Reads the text against RFC 8259's number grammar; answers `0` or a code. */
135
- read(): i32 {
172
+ private read(): i32 {
136
173
  const s: string = this.text
137
174
  const n: i32 = toI32(s.length)
138
175
  let i: i32 = 0
@@ -249,6 +286,11 @@ const jsonNumberSign = (p: JsonNumberParts): i32 => {
249
286
  * and `-0` equals `0`. A text that is not a number, or whose exponent is past
250
287
  * `JSON_NUMBER_MAX_EXPONENT`, is that `JSON_NUMBER_ERR_*` code, `a`'s first.
251
288
  * Linear in the two lengths; allocates two small records.
289
+ *
290
+ * @param a - the first JSON number text.
291
+ * @param b - the second JSON number text.
292
+ * @returns `-1`, `0` or `1` as `a` is below, equal to or above `b`, or a
293
+ * negative `JSON_NUMBER_ERR_*` code.
252
294
  */
253
295
  export const jsonNumberCompare = (a: string, b: string): i32 => {
254
296
  const x = new JsonNumberParts(a)
@@ -279,6 +321,9 @@ export const jsonNumberCompare = (a: string, b: string): i32 => {
279
321
  * `jsonNumberCompare` calls them equal, which makes it a key for a set or a
280
322
  * hash. `""` for a text that is not a number or whose exponent is past
281
323
  * `JSON_NUMBER_MAX_EXPONENT`. Allocates the answer.
324
+ *
325
+ * @param text - a JSON number text.
326
+ * @returns the canonical text of its value, or `""` when `text` is refused.
282
327
  */
283
328
  export const jsonNumberCanonical = (text: string): string => {
284
329
  const p = new JsonNumberParts(text)
@@ -391,6 +436,11 @@ const jsonNumberDividesShort = (v: JsonNumberParts, d: JsonNumberParts, total: i
391
436
  * `JSON_NUMBER_MAX_STEPS` steps, which is `JSON_NUMBER_ERR_GAP`: the value's
392
437
  * digits plus the gap between the two last digits' exponents, times the
393
438
  * divisor's digits when it has more than 18.
439
+ *
440
+ * @param value - the JSON number text to test.
441
+ * @param divisor - the JSON number text it should be a multiple of.
442
+ * @returns `1` for a multiple, `0` for not one, or a negative
443
+ * `JSON_NUMBER_ERR_*` code when the question is refused.
394
444
  */
395
445
  export const jsonNumberMultipleOf = (value: string, divisor: string): i32 => {
396
446
  const v = new JsonNumberParts(value)
@@ -77,7 +77,13 @@ const jsonPointerWellFormed = (s: string, from: i32): boolean => {
77
77
  return true
78
78
  }
79
79
 
80
- /** Whether `s` is an RFC 6901 JSON Pointer (the module header's grammar). Allocates nothing. */
80
+ /**
81
+ * Whether `s` is an RFC 6901 JSON Pointer (the module header's grammar).
82
+ * Allocates nothing.
83
+ *
84
+ * @param s - the text to check.
85
+ * @returns true when `s` is a JSON Pointer, the empty one included.
86
+ */
81
87
  export const isJsonPointer = (s: string): boolean => jsonPointerWellFormed(s, 0)
82
88
 
83
89
  /**
@@ -103,6 +109,9 @@ const jsonPointerIntegerEnd = (s: string, from: i32): i32 => {
103
109
  * leading zero (`0`, `12`, not `01`, `+1` or `-1`), then `#`, or an optional
104
110
  * `+` or `-` and another such integer and then a JSON Pointer, which may be
105
111
  * empty. Allocates nothing.
112
+ *
113
+ * @param s - the text to check.
114
+ * @returns true when `s` is a relative JSON Pointer.
106
115
  */
107
116
  export const isRelativeJsonPointer = (s: string): boolean => {
108
117
  let i: i32 = jsonPointerIntegerEnd(s, 0)
@@ -157,6 +166,10 @@ const jsonPointerTokenEnd = (p: string, from: i32): i32 => {
157
166
  /**
158
167
  * The reference tokens of JSON Pointer `p`, decoded: `[]` for `""`,
159
168
  * `["a/b", "", "~"]` for `/a~1b//~0`. `null` when `p` is not a JSON Pointer.
169
+ *
170
+ * @param p - the pointer text.
171
+ * @returns the decoded reference tokens, in order, or `null` when `p` is not a
172
+ * JSON Pointer.
160
173
  */
161
174
  export const jsonPointerParse = (p: string): string[] | null => {
162
175
  if (!isJsonPointer(p)) {
@@ -175,6 +188,9 @@ export const jsonPointerParse = (p: string): string[] | null => {
175
188
  /**
176
189
  * `token` written as one token of a JSON Pointer: `~` as `~0` and `/` as
177
190
  * `~1`, so `"a/b"` is `a~1b`. Everything else is copied whole.
191
+ *
192
+ * @param token - one reference token, as it reads in an object key.
193
+ * @returns the token as it is written inside a pointer.
178
194
  */
179
195
  export const jsonPointerEscape = (token: string): string => {
180
196
  const parts: string[] = []
@@ -220,6 +236,12 @@ const jsonPointerIndex = (p: string, from: i32, end: i32, length: i32): i32 => {
220
236
  * `""`, else each token taken in turn, as the module header says.
221
237
  * `JSON_POINTER_NONE` when the pointer names nothing there (or `root` names
222
238
  * no value), and `JSON_POINTER_ERR_SYNTAX` when `p` is not a JSON Pointer.
239
+ *
240
+ * @param doc - the document to look in.
241
+ * @param root - the value the pointer starts from.
242
+ * @param p - the pointer text.
243
+ * @returns the handle of the value named, `JSON_POINTER_NONE`, or
244
+ * `JSON_POINTER_ERR_SYNTAX`.
223
245
  */
224
246
  export const jsonPointerResolve = (doc: JsonDocument, root: i32, p: string): i32 => {
225
247
  if (!isJsonPointer(p)) {