@amritk/nish-x86_64-linux 0.42.0 → 0.45.1

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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -127,7 +127,7 @@ const JF_QUERY: i32 = 5 // the fragment's set, and `iprivate` in an IRI
127
127
  * The code unit of `s` at `i`, or -1 past its end, so that a grammar can look
128
128
  * one unit ahead without testing the length first.
129
129
  */
130
- const jfUnitAt = (s: string, i: i32): i32 => (i < toI32(s.length) ? toI32(s.charCodeAt(i)) : -1)
130
+ const jfUnitAt = (s: string, i: i32): i32 => (i >= 0 && i < toI32(s.length) ? toI32(s.charCodeAt(i)) : -1)
131
131
 
132
132
  const jfIsDigit = (c: i32): boolean => c >= 48 && c <= 57
133
133
 
@@ -140,17 +140,34 @@ const jfLower = (c: i32): i32 => (c >= 65 && c <= 90 ? c + 32 : c)
140
140
 
141
141
  /** The value of the `n` decimal digits of `s` at `at`, or -1 if any is not one. */
142
142
  const jfDigits = (s: string, at: i32, n: i32): i32 => {
143
- let value: i32 = 0
144
- let i: i32 = 0
145
- while (i < n) {
146
- const c = jfUnitAt(s, at + i)
147
- if (!jfIsDigit(c)) {
143
+ const length: i32 = toI32(s.length)
144
+ // Callers read at most four digits, so the `u32` value stays below 10^4.
145
+ let value: u32 = 0
146
+ let i: i32 = at
147
+ let left: i32 = n
148
+ while (left > 0) {
149
+ if (i < 0 || i >= length) {
148
150
  return -1
149
151
  }
150
- value = value * 10 + (c - 48)
152
+ const c = toI32(s.charCodeAt(i))
153
+ if (c < 48 || c > 57) {
154
+ return -1
155
+ }
156
+ value = value * 10 + toU32(c - 48)
157
+ i += 1
158
+ left -= 1
159
+ }
160
+ return toI32(value)
161
+ }
162
+
163
+ /** The end of the run of decimal digits of `s` that starts at `from`. */
164
+ const jfDigitRunEnd = (s: string, from: i32): i32 => {
165
+ const length: i32 = toI32(s.length)
166
+ let i: i32 = from
167
+ while (i >= 0 && i < length && jfIsDigit(toI32(s.charCodeAt(i)))) {
151
168
  i += 1
152
169
  }
153
- return value
170
+ return i
154
171
  }
155
172
 
156
173
  /** The number of days in `month` (1 to 12) of the Gregorian `year`. */
@@ -196,46 +213,42 @@ const jfFullTimeFrom = (s: string, at: i32): boolean => {
196
213
  if (jfUnitAt(s, at + 2) !== 58 || jfUnitAt(s, at + 5) !== 58) {
197
214
  return false
198
215
  }
216
+ const length: i32 = toI32(s.length)
199
217
  let i = at + 8
200
218
  if (jfUnitAt(s, i) === 46) {
201
219
  i += 1
202
220
  const start = i
203
- while (jfIsDigit(jfUnitAt(s, i))) {
204
- i += 1
205
- }
221
+ i = jfDigitRunEnd(s, i)
206
222
  if (i === start) {
207
223
  return false
208
224
  }
209
225
  }
210
226
  const sign = jfUnitAt(s, i)
211
- let offset: i32 = 0
227
+ const local: i32 = hour * 60 + minute
228
+ // What follows the seconds ends `s`, so its units are read back from the
229
+ // end: `Z` is the last unit and `±HH:MM` the last six.
212
230
  if (jfLower(sign) === 122) {
213
- i += 1
214
- } else if (sign === 43 || sign === 45) {
215
- const offsetHour = jfDigits(s, i + 1, 2)
216
- const offsetMinute = jfDigits(s, i + 4, 2)
217
- if (
218
- offsetHour < 0 ||
219
- offsetHour > 23 ||
220
- offsetMinute < 0 ||
221
- offsetMinute > 59 ||
222
- jfUnitAt(s, i + 3) !== 58
223
- ) {
224
- return false
225
- }
226
- offset = offsetHour * 60 + offsetMinute
227
- if (sign === 45) {
228
- offset = -offset
229
- }
230
- i += 6
231
- } else {
231
+ return i === length - 1 && (second !== 60 || local === 1439)
232
+ }
233
+ if ((sign !== 43 && sign !== 45) || i !== length - 6) {
232
234
  return false
233
235
  }
234
- if (i !== toI32(s.length)) {
236
+ const offsetHour = jfDigits(s, length - 5, 2)
237
+ const offsetMinute = jfDigits(s, length - 2, 2)
238
+ if (
239
+ offsetHour < 0 ||
240
+ offsetHour > 23 ||
241
+ offsetMinute < 0 ||
242
+ offsetMinute > 59 ||
243
+ jfUnitAt(s, length - 3) !== 58
244
+ ) {
235
245
  return false
236
246
  }
237
- // The UTC minute of the day is in -1439 to 2878 before the wrap.
238
- return second !== 60 || (hour * 60 + minute - offset + 1440) % 1440 === 1439
247
+ const offset: i32 = offsetHour * 60 + offsetMinute
248
+ // The UTC minute of the day: `+` is ahead of UTC and `-` behind it, and
249
+ // both minutes are below 1440, so one more day keeps the sum positive.
250
+ const utc: i32 = sign === 43 ? local - offset + 1440 : local + offset
251
+ return second !== 60 || utc % 1440 === 1439
239
252
  }
240
253
 
241
254
  /**
@@ -298,8 +311,8 @@ export const isDuration = (s: string): boolean => {
298
311
  let i: i32 = 1
299
312
  let inTime = false
300
313
  let last: i32 = -1
301
- let count: i32 = 0
302
- let timeCount: i32 = 0
314
+ let anyUnit = false
315
+ let anyTimeUnit = false
303
316
  while (i < length) {
304
317
  if (jfUnitAt(s, i) === 84) {
305
318
  if (inTime) {
@@ -311,29 +324,27 @@ export const isDuration = (s: string): boolean => {
311
324
  continue
312
325
  }
313
326
  const start = i
314
- while (jfIsDigit(jfUnitAt(s, i))) {
315
- i += 1
316
- }
317
- const unit = jfUnitAt(s, i)
318
- if (i === start || unit < 0) {
327
+ i = jfDigitRunEnd(s, i)
328
+ if (i === start || i >= length) {
319
329
  return false
320
330
  }
331
+ const unit = jfUnitAt(s, i)
321
332
  i += 1
322
333
  if (unit === 87) {
323
334
  // `dur-week` is the whole duration.
324
- return !inTime && count === 0 && i === length
335
+ return !inTime && !anyUnit && i === length
325
336
  }
326
337
  const k = jfUnitIndex(unit, inTime ? "HMS" : "YMD")
327
- if (k < 0 || (last >= 0 && k !== last + 1)) {
338
+ if (k < 0 || (last >= 0 && k - last !== 1)) {
328
339
  return false
329
340
  }
330
341
  last = k
331
- count += 1
342
+ anyUnit = true
332
343
  if (inTime) {
333
- timeCount += 1
344
+ anyTimeUnit = true
334
345
  }
335
346
  }
336
- return count > 0 && (!inTime || timeCount > 0)
347
+ return anyUnit && (!inTime || anyTimeUnit)
337
348
  }
338
349
 
339
350
  /** Whether `s[from, to)` is a dotted-quad IPv4 address. */
@@ -348,12 +359,18 @@ const jfIpv4Range = (s: string, from: i32, to: i32): boolean => {
348
359
  i += 1
349
360
  }
350
361
  const start = i
351
- let value: i32 = 0
352
- while (i < to && i - start < 3 && jfIsDigit(jfUnitAt(s, i))) {
353
- value = value * 10 + (jfUnitAt(s, i) - 48)
362
+ // At most three digits, so the `u32` value stays below 1000.
363
+ let value: u32 = 0
364
+ let n: i32 = 0
365
+ while (n < 3 && i < to) {
366
+ const c = jfUnitAt(s, i)
367
+ if (c < 48 || c > 57) {
368
+ break
369
+ }
370
+ value = value * 10 + toU32(c - 48)
354
371
  i += 1
372
+ n += 1
355
373
  }
356
- const n = i - start
357
374
  if (n === 0 || value > 255 || (n > 1 && jfUnitAt(s, start) === 48)) {
358
375
  return false
359
376
  }
@@ -367,14 +384,24 @@ const jfIpv6Range = (s: string, from: i32, to: i32): boolean => {
367
384
  let i = from
368
385
  let groups: i32 = 0
369
386
  let elided = false
370
- if (i + 1 < to && jfUnitAt(s, i) === 58 && jfUnitAt(s, i + 1) === 58) {
371
- elided = true
372
- i += 2
387
+ if (i < to && jfUnitAt(s, i) === 58) {
388
+ const next = i + 1
389
+ if (next < to && jfUnitAt(s, next) === 58) {
390
+ elided = true
391
+ i = next + 1
392
+ }
373
393
  }
374
394
  while (i < to) {
395
+ // Past eight groups the address is already too long.
396
+ if (groups > 8) {
397
+ return false
398
+ }
375
399
  const start = i
376
- while (i < to && jfIsHex(jfUnitAt(s, i))) {
400
+ // A fifth hex digit fails the group, so the run is not read past it.
401
+ let digits: i32 = 0
402
+ while (digits < 5 && i < to && jfIsHex(jfUnitAt(s, i))) {
377
403
  i += 1
404
+ digits += 1
378
405
  }
379
406
  if (i < to && jfUnitAt(s, i) === 46) {
380
407
  // An IPv4 tail ends the address and fills two groups.
@@ -384,11 +411,11 @@ const jfIpv6Range = (s: string, from: i32, to: i32): boolean => {
384
411
  groups += 2
385
412
  break
386
413
  }
387
- if (i === start || i - start > 4) {
414
+ if (digits === 0 || digits > 4) {
388
415
  return false
389
416
  }
390
417
  groups += 1
391
- if (i === to) {
418
+ if (i >= to) {
392
419
  break
393
420
  }
394
421
  if (jfUnitAt(s, i) !== 58) {
@@ -430,20 +457,37 @@ const jfIsLdh = (c: i32): boolean => jfIsAlpha(c) || jfIsDigit(c) || c === 45
430
457
 
431
458
  /** Whether `s[from, to)` is an RFC 1123 host name. */
432
459
  const jfHostnameRange = (s: string, from: i32, to: i32): boolean => {
433
- if (to <= from || to - from > 253) {
460
+ // `from < to`, so the `u32` difference is exact.
461
+ if (to <= from || toU32(to) - toU32(from) > 253) {
434
462
  return false
435
463
  }
436
- let start = from
464
+ // The label being read: its length, its first unit and its last.
465
+ let n: i32 = 0
466
+ let first: i32 = -1
467
+ let last: i32 = -1
437
468
  let i = from
438
469
  while (i <= to) {
439
- if (i === to || jfUnitAt(s, i) === 46) {
440
- const n = i - start
441
- if (n === 0 || n > 63 || jfUnitAt(s, start) === 45 || jfUnitAt(s, i - 1) === 45) {
470
+ const c: i32 = i < to ? jfUnitAt(s, i) : 46
471
+ if (c === 46) {
472
+ if (n === 0 || first === 45 || last === 45) {
442
473
  return false
443
474
  }
444
- start = i + 1
445
- } else if (!jfIsLdh(jfUnitAt(s, i))) {
475
+ n = 0
476
+ } else if (!jfIsLdh(c)) {
446
477
  return false
478
+ } else {
479
+ // A label longer than 63 units fails the name.
480
+ if (n >= 63) {
481
+ return false
482
+ }
483
+ if (n === 0) {
484
+ first = c
485
+ }
486
+ last = c
487
+ n += 1
488
+ }
489
+ if (i >= to) {
490
+ return true
447
491
  }
448
492
  i += 1
449
493
  }
@@ -479,36 +523,43 @@ const jfIsAtext = (c: i32): boolean =>
479
523
  * quoted string, or -1 when it is neither.
480
524
  */
481
525
  const jfLocalPartEnd = (s: string): i32 => {
526
+ const length: i32 = toI32(s.length)
482
527
  let i: i32 = 0
483
528
  if (jfUnitAt(s, 0) === 34) {
484
529
  i = 1
485
- while (true) {
530
+ while (i < length) {
486
531
  const c = jfUnitAt(s, i)
487
532
  if (c === 34) {
488
533
  return i + 1
489
534
  }
490
535
  if (c === 92) {
491
- const quoted = jfUnitAt(s, i + 1)
536
+ const next = i + 1
537
+ if (next >= length) {
538
+ return -1
539
+ }
540
+ const quoted = jfUnitAt(s, next)
492
541
  if (quoted < 32 || quoted > 126) {
493
542
  return -1
494
543
  }
495
- i += 2
544
+ i = next + 1
496
545
  } else if (c === 32 || c === 33 || (c >= 35 && c <= 91) || (c >= 93 && c <= 126)) {
497
546
  i += 1
498
547
  } else {
499
548
  return -1
500
549
  }
501
550
  }
551
+ // The quoted string has no closing quote.
552
+ return -1
502
553
  }
503
554
  while (true) {
504
555
  const start = i
505
- while (jfIsAtext(jfUnitAt(s, i))) {
556
+ while (i < length && jfIsAtext(jfUnitAt(s, i))) {
506
557
  i += 1
507
558
  }
508
559
  if (i === start) {
509
560
  return -1
510
561
  }
511
- if (jfUnitAt(s, i) !== 46) {
562
+ if (i >= length || jfUnitAt(s, i) !== 46) {
512
563
  return i
513
564
  }
514
565
  i += 1
@@ -567,7 +618,7 @@ export const isEmail = (s: string): boolean => {
567
618
  const inner = from + 1
568
619
  const end = length - 1
569
620
  if (
570
- end - inner > 5 &&
621
+ end > inner + 5 &&
571
622
  jfLower(jfUnitAt(s, inner)) === 105 &&
572
623
  jfLower(jfUnitAt(s, inner + 1)) === 112 &&
573
624
  jfLower(jfUnitAt(s, inner + 2)) === 118 &&
@@ -623,12 +674,18 @@ const jfIsUcschar = (cp: i32): boolean => {
623
674
  * outside those ranges or units that do not encode one.
624
675
  */
625
676
  const jfUcsWidth = (s: string, i: i32, privateOk: boolean): i32 => {
677
+ const length: i32 = toI32(s.length)
678
+ if (i < 0 || i >= length) {
679
+ return 0
680
+ }
626
681
  const lead = jfUnitAt(s, i)
627
682
  let cp: i32 = 0
628
683
  let width: i32 = 0
684
+ // The unit after the lead.
685
+ let j: i32 = i + 1
629
686
  if (jfUnitsAreBytes()) {
630
687
  // UTF-8: no overlong form, no surrogate, nothing past U+10FFFF.
631
- const b1 = jfUnitAt(s, i + 1)
688
+ const b1 = jfUnitAt(s, j)
632
689
  let low: i32 = 0x80
633
690
  let high: i32 = 0xbf
634
691
  if (lead >= 0xc2 && lead <= 0xdf) {
@@ -653,7 +710,11 @@ const jfUcsWidth = (s: string, i: i32, privateOk: boolean): i32 => {
653
710
  cp = (cp << 6) | (b1 & 0x3f)
654
711
  let k: i32 = 2
655
712
  while (k < width) {
656
- const b = jfUnitAt(s, i + k)
713
+ if (j >= length) {
714
+ return 0
715
+ }
716
+ j += 1
717
+ const b = jfUnitAt(s, j)
657
718
  if (b < 0x80 || b > 0xbf) {
658
719
  return 0
659
720
  }
@@ -661,12 +722,14 @@ const jfUcsWidth = (s: string, i: i32, privateOk: boolean): i32 => {
661
722
  k += 1
662
723
  }
663
724
  } else if (lead >= 0xd800 && lead <= 0xdbff) {
664
- const trail = jfUnitAt(s, i + 1)
725
+ const trail = jfUnitAt(s, j)
665
726
  if (trail < 0xdc00 || trail > 0xdfff) {
666
727
  return 0
667
728
  }
668
729
  width = 2
669
- cp = ((lead - 0xd800) << 10) + (trail - 0xdc00) + 0x10000
730
+ // A lead surrogate's low ten bits are its offset from D800, and a
731
+ // trail's from DC00.
732
+ cp = (lead & 0x3ff) * 1024 + (trail & 0x3ff) + 0x10000
670
733
  } else if (lead >= 0xdc00 && lead <= 0xdfff) {
671
734
  return 0
672
735
  } else {
@@ -711,12 +774,13 @@ const jfInSet = (c: i32, set: i32): boolean => {
711
774
  * past ASCII where `iri` allows it, and 0 where none fits.
712
775
  */
713
776
  const jfUriUnit = (s: string, i: i32, set: i32, iri: boolean): i32 => {
714
- const c = jfUnitAt(s, i)
715
- if (c < 0) {
777
+ if (i < 0 || i >= toI32(s.length)) {
716
778
  return 0
717
779
  }
780
+ const c = jfUnitAt(s, i)
718
781
  if (c === 37) {
719
- return jfIsHex(jfUnitAt(s, i + 1)) && jfIsHex(jfUnitAt(s, i + 2)) ? 3 : 0
782
+ const next = i + 1
783
+ return jfIsHex(jfUnitAt(s, next)) && next < toI32(s.length) && jfIsHex(jfUnitAt(s, next + 1)) ? 3 : 0
720
784
  }
721
785
  if (c < 0x80) {
722
786
  return jfInSet(c, set) ? 1 : 0
@@ -732,7 +796,8 @@ const jfUriRun = (s: string, from: i32, to: i32, set: i32, iri: boolean): boolea
732
796
  if (w === 0) {
733
797
  return false
734
798
  }
735
- i += w
799
+ // The `w` units are in `s`, so the `u32` sum is at most its length.
800
+ i = toI32(toU32(i) + toU32(w))
736
801
  }
737
802
  return i === to
738
803
  }
@@ -755,17 +820,23 @@ const jfFind = (s: string, from: i32, to: i32, a: i32, b: i32): i32 => {
755
820
  * or `IPvFuture`, `"v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )`.
756
821
  */
757
822
  const jfIpLiteral = (s: string, from: i32, to: i32): boolean => {
758
- if (jfLower(jfUnitAt(s, from)) !== 118) {
823
+ // An empty range is not `IPvFuture`, and not an IPv6 address either.
824
+ if (from >= to || jfLower(jfUnitAt(s, from)) !== 118) {
759
825
  return jfIpv6Range(s, from, to)
760
826
  }
761
- let i = from + 1
827
+ const digits = from + 1
828
+ let i = digits
762
829
  while (i < to && jfIsHex(jfUnitAt(s, i))) {
763
830
  i += 1
764
831
  }
765
- if (i === from + 1 || i >= to || jfUnitAt(s, i) !== 46 || i + 1 === to) {
832
+ if (i === digits || i >= to || jfUnitAt(s, i) !== 46) {
833
+ return false
834
+ }
835
+ const rest = i + 1
836
+ if (rest === to) {
766
837
  return false
767
838
  }
768
- return jfUriRun(s, i + 1, to, JF_USERINFO, false)
839
+ return jfUriRun(s, rest, to, JF_USERINFO, false)
769
840
  }
770
841
 
771
842
  /** Whether `s[from, to)` is an RFC 3986 `authority`: `[ userinfo "@" ] host [ ":" port ]`. */
@@ -781,8 +852,9 @@ const jfAuthority = (s: string, from: i32, to: i32, iri: boolean): boolean => {
781
852
  }
782
853
  let port: i32 = 0
783
854
  if (jfUnitAt(s, host) === 91) {
855
+ // `jfFind` answers at most `to`.
784
856
  const close = jfFind(s, host, to, 93, 93)
785
- if (close === to || !jfIpLiteral(s, host + 1, close)) {
857
+ if (host >= to || close >= to || !jfIpLiteral(s, host + 1, close)) {
786
858
  return false
787
859
  }
788
860
  port = close + 1
@@ -794,7 +866,7 @@ const jfAuthority = (s: string, from: i32, to: i32, iri: boolean): boolean => {
794
866
  return false
795
867
  }
796
868
  }
797
- if (port === to) {
869
+ if (port >= to) {
798
870
  return true
799
871
  }
800
872
  if (jfUnitAt(s, port) !== 58) {
@@ -819,14 +891,14 @@ const jfUriCheck = (s: string, iri: boolean, reference: boolean): boolean => {
819
891
  let i: i32 = 0
820
892
  if (jfIsAlpha(jfUnitAt(s, 0))) {
821
893
  let j: i32 = 1
822
- while (true) {
894
+ while (j < length) {
823
895
  const c = jfUnitAt(s, j)
824
896
  if (!(jfIsAlpha(c) || jfIsDigit(c) || c === 43 || c === 45 || c === 46)) {
825
897
  break
826
898
  }
827
899
  j += 1
828
900
  }
829
- if (jfUnitAt(s, j) === 58) {
901
+ if (j < length && jfUnitAt(s, j) === 58) {
830
902
  i = j + 1
831
903
  }
832
904
  }
@@ -834,9 +906,17 @@ const jfUriCheck = (s: string, iri: boolean, reference: boolean): boolean => {
834
906
  return false
835
907
  }
836
908
  const pathEnd = jfFind(s, i, length, 63, 35)
837
- if (jfUnitAt(s, i) === 47 && jfUnitAt(s, i + 1) === 47) {
838
- const authorityEnd = jfFind(s, i + 2, pathEnd, 47, 47)
839
- if (!jfAuthority(s, i + 2, authorityEnd, iri)) {
909
+ // Where the authority starts, after a `//`, or -1 without one.
910
+ let authority: i32 = -1
911
+ if (i < length && jfUnitAt(s, i) === 47) {
912
+ const next = i + 1
913
+ if (next < length && jfUnitAt(s, next) === 47) {
914
+ authority = next + 1
915
+ }
916
+ }
917
+ if (authority >= 0) {
918
+ const authorityEnd = jfFind(s, authority, pathEnd, 47, 47)
919
+ if (!jfAuthority(s, authority, authorityEnd, iri)) {
840
920
  return false
841
921
  }
842
922
  i = authorityEnd
@@ -852,14 +932,15 @@ const jfUriCheck = (s: string, iri: boolean, reference: boolean): boolean => {
852
932
  return false
853
933
  }
854
934
  i = pathEnd
855
- if (jfUnitAt(s, i) === 63) {
856
- const queryEnd = jfFind(s, i + 1, length, 35, 35)
857
- if (!jfUriRun(s, i + 1, queryEnd, JF_QUERY, iri)) {
935
+ if (i < length && jfUnitAt(s, i) === 63) {
936
+ const query = i + 1
937
+ const queryEnd = jfFind(s, query, length, 35, 35)
938
+ if (!jfUriRun(s, query, queryEnd, JF_QUERY, iri)) {
858
939
  return false
859
940
  }
860
941
  i = queryEnd
861
942
  }
862
- if (jfUnitAt(s, i) === 35) {
943
+ if (i < length && jfUnitAt(s, i) === 35) {
863
944
  return jfUriRun(s, i + 1, length, JF_FRAGMENT, iri)
864
945
  }
865
946
  return true
@@ -903,11 +984,16 @@ export const isIriReference = (s: string): boolean => jfUriCheck(s, true, true)
903
984
 
904
985
  /** The code units of RFC 6570's `varchar` at `i` of `s`, `ALPHA / DIGIT / "_" / pct-encoded`, or 0. */
905
986
  const jfVarchar = (s: string, i: i32): i32 => {
987
+ const length: i32 = toI32(s.length)
988
+ if (i < 0 || i >= length) {
989
+ return 0
990
+ }
906
991
  const c = jfUnitAt(s, i)
907
992
  if (jfIsAlpha(c) || jfIsDigit(c) || c === 95) {
908
993
  return 1
909
994
  }
910
- return c === 37 && jfIsHex(jfUnitAt(s, i + 1)) && jfIsHex(jfUnitAt(s, i + 2)) ? 3 : 0
995
+ const next = i + 1
996
+ return c === 37 && next < length && jfIsHex(jfUnitAt(s, next)) && jfIsHex(jfUnitAt(s, next + 1)) ? 3 : 0
911
997
  }
912
998
 
913
999
  /**
@@ -916,47 +1002,61 @@ const jfVarchar = (s: string, i: i32): i32 => {
916
1002
  * each a `varname` and a `:` prefix of 1 to 9999 or a `*`.
917
1003
  */
918
1004
  const jfExpressionEnd = (s: string, open: i32): i32 => {
1005
+ const length: i32 = toI32(s.length)
1006
+ // Outside `s` there is no `{` to start an expression.
1007
+ if (open < 0 || open >= length) {
1008
+ return -1
1009
+ }
919
1010
  let i = open + 1
920
- const op = jfUnitAt(s, i)
921
- if (jfUnitIndex(op, "+#./;?&=,!@|") >= 0) {
1011
+ if (i < length && jfUnitIndex(jfUnitAt(s, i), "+#./;?&=,!@|") >= 0) {
922
1012
  i += 1
923
1013
  }
924
- while (true) {
1014
+ while (i < length) {
925
1015
  let w = jfVarchar(s, i)
926
1016
  if (w === 0) {
927
1017
  return -1
928
1018
  }
929
- i += w
930
- while (true) {
1019
+ // `jfVarchar` counts only units of `s`, so the sum is at most its length.
1020
+ i = toI32(toU32(i) + toU32(w))
1021
+ while (i < length) {
931
1022
  if (jfUnitAt(s, i) === 46) {
932
- w = jfVarchar(s, i + 1)
1023
+ const name = i + 1
1024
+ w = jfVarchar(s, name)
933
1025
  if (w === 0) {
934
1026
  return -1
935
1027
  }
936
- i += 1 + w
1028
+ i = toI32(toU32(name) + toU32(w))
937
1029
  } else {
938
1030
  w = jfVarchar(s, i)
939
1031
  if (w === 0) {
940
1032
  break
941
1033
  }
942
- i += w
1034
+ i = toI32(toU32(i) + toU32(w))
943
1035
  }
944
1036
  }
1037
+ // An expression ends with a `}`, so running out of text is not one.
1038
+ if (i >= length) {
1039
+ return -1
1040
+ }
945
1041
  const modifier = jfUnitAt(s, i)
946
1042
  if (modifier === 58) {
947
- const first = jfUnitAt(s, i + 1)
948
- if (first < 49 || first > 57) {
1043
+ const digit = i + 1
1044
+ const first = jfUnitAt(s, digit)
1045
+ if (digit >= length || first < 49 || first > 57) {
949
1046
  return -1
950
1047
  }
951
- i += 2
1048
+ i = digit + 1
952
1049
  let more: i32 = 0
953
- while (more < 3 && jfIsDigit(jfUnitAt(s, i))) {
1050
+ while (more < 3 && i < length && jfIsDigit(jfUnitAt(s, i))) {
954
1051
  i += 1
955
1052
  more += 1
956
1053
  }
957
1054
  } else if (modifier === 42) {
958
1055
  i += 1
959
1056
  }
1057
+ if (i >= length) {
1058
+ return -1
1059
+ }
960
1060
  const next = jfUnitAt(s, i)
961
1061
  if (next === 125) {
962
1062
  return i + 1
@@ -966,6 +1066,7 @@ const jfExpressionEnd = (s: string, open: i32): i32 => {
966
1066
  }
967
1067
  i += 1
968
1068
  }
1069
+ return -1
969
1070
  }
970
1071
 
971
1072
  /**
@@ -1004,10 +1105,15 @@ export const isUriTemplate = (s: string): boolean => {
1004
1105
  return false
1005
1106
  }
1006
1107
  } else if (c === 37) {
1007
- if (!jfIsHex(jfUnitAt(s, i + 1)) || !jfIsHex(jfUnitAt(s, i + 2))) {
1108
+ const next = i + 1
1109
+ if (next >= length || !jfIsHex(jfUnitAt(s, next))) {
1110
+ return false
1111
+ }
1112
+ const last = next + 1
1113
+ if (last >= length || !jfIsHex(jfUnitAt(s, last))) {
1008
1114
  return false
1009
1115
  }
1010
- i += 3
1116
+ i = last + 1
1011
1117
  } else if (c < 0x80) {
1012
1118
  if (!jfIsTemplateLiteral(c)) {
1013
1119
  return false
@@ -1018,7 +1124,8 @@ export const isUriTemplate = (s: string): boolean => {
1018
1124
  if (w === 0) {
1019
1125
  return false
1020
1126
  }
1021
- i += w
1127
+ // `jfUcsWidth` counts only units of `s`, so the sum is at most its length.
1128
+ i = toI32(toU32(i) + toU32(w))
1022
1129
  }
1023
1130
  }
1024
1131
  return true