@amritk/nish-aarch64-linux 0.39.0 → 0.41.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 (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
package/std/json-typed.ts CHANGED
@@ -25,7 +25,8 @@
25
25
  * functions here for each field, and into `JsonWriter`'s, so all of the parsing
26
26
  * is the tokenizer's and all of the writing is the writer's. A field may be an
27
27
  * `i32`, `u32`, `i64`, `u64`, `f64` (or `number`), `boolean` or `string`, a
28
- * numeric enum (its number), a class decoded the same way, a tagged union of
28
+ * numeric enum (its number), a string enum (its member's string), a class
29
+ * decoded the same way, a tagged union of
29
30
  * classes, a `JsonNumber`, an array of any of those, a `Map<string, V>` (a
30
31
  * JSON object) whose `V` is any of these, or a string, class, union, array or
31
32
  * map `| null`; anything else is refused at the call, naming the field
@@ -53,6 +54,10 @@
53
54
  * 2^64 - 1
54
55
  * numeric enum E the number of one of E's d.i32(), then w.i32
55
56
  * members a switch
57
+ * string enum E the string of one of E's d.enumString(), w.string of
58
+ * members then its bytes the member's
59
+ * against each string
60
+ * member's
56
61
  * f64 (number in f64 mode) any number; past the double d.f64() w.f64
57
62
  * range an infinity, which the
58
63
  * writer writes as null
@@ -87,9 +92,9 @@
87
92
  * field (NL2485 for a field type, such as an `f32`, an `i32[][]`, a
88
93
  * `Level[][]`, a `Set`, an array of maps, a `Map` not keyed by `string` (an
89
94
  * enum key included) or a `Map` whose values cannot be written, and a generic
90
- * class of the program's own). An enum whose members are strings or a mix is
91
- * not one the language declares (NL1043), and a `const enum` is refused where
92
- * it is declared (NL2285).
95
+ * class of the program's own). An enum that mixes string and numeric members
96
+ * is not one the language declares (NL1043), an enum is never `| null`
97
+ * (NL2010) and a `const enum` is refused where it is declared (NL2285).
93
98
  *
94
99
  * A value that is not a declared class (an interface, a generic class or any
95
100
  * other type) is NL2486; decoding into an interface is left for a later
@@ -190,7 +195,7 @@
190
195
  * - `presence`, described below. Without it nothing is recorded.
191
196
  * - `tag`, a string literal: what a variant of a tagged union writes as its
192
197
  * tag, described below. Without it, or with `tag: ""`, the tag is the enum
193
- * member's name.
198
+ * member's name, or for a string enum the member's string.
194
199
  *
195
200
  * Each misuse has its own diagnostic:
196
201
  *
@@ -283,8 +288,9 @@
283
288
  * jsonEncode(w, e);
284
289
  *
285
290
  * The tag's key is the tag field's name, or its `rename`, and every variant
286
- * gives the same one (NL2721). Its value is the enum member's name, or the
287
- * variant's `tag` option, and no two variants write the same one (NL2720). The
291
+ * gives the same one (NL2721). Its value is the enum member's name (a string
292
+ * enum's member's string), or the variant's `tag` option, and no two variants
293
+ * write the same one (NL2720). The
288
294
  * tag may be any key of the object: when it is the first, the variant is picked
289
295
  * at once; when it is not, the keys before it are stepped over, checked but not
290
296
  * kept, and the object is read again from its first key once the variant is
@@ -345,13 +351,21 @@
345
351
  * steps over is not detected, because its value is never read. A map is
346
352
  * bounded by `maxMapKeys`, and a number's length by `JsonLimits.maxNumberBytes`.
347
353
  *
348
- * **An enum is its number.** A numeric enum is read with `d.i32()`, so a
354
+ * **A numeric enum is its number.** It is read with `d.i32()`, so a
349
355
  * number that is not an `i32` integer is that field's `JSON_DECODE_RANGE`, and
350
356
  * an integer that is not the value of one of its members is
351
357
  * `JSON_DECODE_TYPE`; a member's name, `"High"`, is a string and a type error
352
358
  * too. Two members of one value are one number. It is written as `w.i32` of
353
359
  * its value, as `JSON.stringify` writes a TypeScript numeric enum.
354
360
  *
361
+ * **A string enum is its member's string.** It is read with `d.enumString()`
362
+ * and the string's bytes are matched against the members' strings by length,
363
+ * then by a byte, then with `keyIs`, so the read allocates nothing; an escaped
364
+ * string matches as its unescaped bytes. A string that is no member's string,
365
+ * and a value that is not a string, are `JSON_DECODE_TYPE`, and the field keeps
366
+ * the member it held. It is written as `w.string` of the member's string, as
367
+ * `JSON.stringify` writes a TypeScript string enum.
368
+ *
355
369
  * **A `u64` is exact to 2^64 - 1.** It is read through the tokenizer's
356
370
  * `numberU64`, so `18446744073709551615` decodes and `18446744073709551616`,
357
371
  * `-1`, `-0` and `1.5` are `JSON_DECODE_RANGE`. It is written as its digits,
@@ -674,6 +688,27 @@ export class JsonDecoder {
674
688
  this.fail(JSON_DECODE_TYPE, this.tok.tokStart)
675
689
  }
676
690
 
691
+ /**
692
+ * Takes the next value as a string enum's string, which it leaves as the
693
+ * current token for `keyLength`, `keyByte` and `keyIs` to match against
694
+ * the members' strings.
695
+ *
696
+ * @returns `true` at a string; `false` with `JSON_DECODE_TYPE` for any other value, and after any error.
697
+ *
698
+ * @internal Called by name by the decoder the compiler writes for
699
+ * `jsonDecode`, src/emit-json.ts.
700
+ */
701
+ enumString(): boolean {
702
+ const k: i32 = this.take()
703
+ if (k === JSON_TOK_STRING) {
704
+ return true
705
+ }
706
+ if (k >= 0) {
707
+ this.wrongType()
708
+ }
709
+ return false
710
+ }
711
+
677
712
  /**
678
713
  * Takes the next value's first token: the held token, when there is one,
679
714
  * and the tokenizer's next otherwise.
package/std/json-value.ts CHANGED
@@ -149,6 +149,7 @@
149
149
  * the same under `--number-mode f64`, and it runs under the Node prelude, where
150
150
  * `tests/run.js` holds every method to the native answers.
151
151
  */
152
+ import { uncheckedSet } from "nish:unsafe"
152
153
  import { JSON_WRITE_FULL, JSON_WRITE_OK, JSON_WRITER_MAX_DEPTH, JsonWriter } from "nish/json"
153
154
  import {
154
155
  JSON_TOK_ARRAY_BEGIN,
@@ -235,6 +236,9 @@ const JSON_VALUE_DIGIT_0: i32 = 48
235
236
  const JSON_VALUE_NO_WORD: i32 = 0
236
237
  const JSON_VALUE_TRUE_WORD: i32 = 1
237
238
  const JSON_VALUE_MINUS: i32 = 45
239
+ /** A line feed and a carriage return, which `storeSpan` refuses. */
240
+ const JSON_VALUE_LF: i32 = 10
241
+ const JSON_VALUE_CR: i32 = 13
238
242
 
239
243
  /** A `JsonDocument`'s source ranges, made on first use; see `JsonDocument.ranges`. */
240
244
  class JsonValueRanges {
@@ -1463,6 +1467,18 @@ export class JsonDocument {
1463
1467
  if (start < 0 || end < start) {
1464
1468
  return JSON_VALUE_ERR_RANGE
1465
1469
  }
1470
+ this.rangeAt(x, start, end)
1471
+ return JSON_VALUE_OK
1472
+ }
1473
+
1474
+ /**
1475
+ * `setRange` for index `x`, a value of this document, with
1476
+ * `0 <= start <= end` checked by the caller.
1477
+ *
1478
+ * @internal Read by std/yaml-json.ts, which holds the index of each value
1479
+ * it makes.
1480
+ */
1481
+ rangeAt(x: i32, start: i32, end: i32): void {
1466
1482
  this.ownRanges()
1467
1483
  const r: JsonValueRanges = this.rangeArrays()
1468
1484
  // Values between the last current one and `x` have no range yet.
@@ -1475,7 +1491,6 @@ export class JsonDocument {
1475
1491
  if (x >= this.rangeTop) {
1476
1492
  this.rangeTop = x + 1
1477
1493
  }
1478
- return JSON_VALUE_OK
1479
1494
  }
1480
1495
 
1481
1496
  /**
@@ -1497,6 +1512,18 @@ export class JsonDocument {
1497
1512
  if (start < 0 || end < start) {
1498
1513
  return JSON_VALUE_ERR_RANGE
1499
1514
  }
1515
+ this.keyRangeAt(s, start, end)
1516
+ return JSON_VALUE_OK
1517
+ }
1518
+
1519
+ /**
1520
+ * `setKeyRange` for slot `s`, a member's as `append` answers it, with
1521
+ * `0 <= start <= end` checked by the caller.
1522
+ *
1523
+ * @internal Read by std/yaml-json.ts, which holds the slot of each member
1524
+ * it adds.
1525
+ */
1526
+ keyRangeAt(s: i32, start: i32, end: i32): void {
1500
1527
  this.ownRanges()
1501
1528
  const r: JsonValueRanges = this.rangeArrays()
1502
1529
  for (let t: i32 = this.slotRangeTop; t < s; t += 1) {
@@ -1508,7 +1535,6 @@ export class JsonDocument {
1508
1535
  if (s >= this.slotRangeTop) {
1509
1536
  this.slotRangeTop = s + 1
1510
1537
  }
1511
- return JSON_VALUE_OK
1512
1538
  }
1513
1539
 
1514
1540
  /** The slot of member `i` of the object at index `x`, or `-1` when `x` is not an object or has no member `i`. */
@@ -1724,11 +1750,12 @@ export class JsonDocument {
1724
1750
  * first. A container whose slots are full moves them to the end of the slot
1725
1751
  * arrays with twice the room, unless they are at the end already, where they
1726
1752
  * grow in place; the slots it leaves are not reused until `clear`.
1753
+ * Answers the new member's slot.
1727
1754
  *
1728
1755
  * @internal Read by std/yaml-json.ts, which builds a document from YAML
1729
1756
  * through it.
1730
1757
  */
1731
- append(c: i32, v: i32, keyAt: i32, keyLength: i32): void {
1758
+ append(c: i32, v: i32, keyAt: i32, keyLength: i32): i32 {
1732
1759
  let start: i32 = jsonValueGet(this.first, c)
1733
1760
  const n: i32 = jsonValueGet(this.count, c)
1734
1761
  const capacity: i32 = jsonValueGet(this.room, c)
@@ -1749,6 +1776,120 @@ export class JsonDocument {
1749
1776
  jsonValuePut(this.slotKeyLength, start + n, keyLength)
1750
1777
  jsonValuePut(this.count, c, n + 1)
1751
1778
  jsonValuePut(this.parent, v, c)
1779
+ return start + n
1780
+ }
1781
+
1782
+ /**
1783
+ * Gives the empty container at index `c`, made last, room for `n`
1784
+ * members at the end of the slot arrays, so that `n` appends fill it
1785
+ * without moving it. Nothing for a container that is not empty and last.
1786
+ *
1787
+ * @internal Read by std/yaml-json.ts, which knows each container's size
1788
+ * before it builds the members.
1789
+ */
1790
+ reserve(c: i32, n: i32): void {
1791
+ if (n <= 0 || jsonValueGet(this.first, c) !== this.slotTop || jsonValueGet(this.room, c) !== 0) {
1792
+ return
1793
+ }
1794
+ this.slotTop += n
1795
+ this.slotValue = jsonValueRoomI32(this.slotValue, this.slotTop)
1796
+ this.slotKeyAt = jsonValueRoomI32(this.slotKeyAt, this.slotTop)
1797
+ this.slotKeyLength = jsonValueRoomI32(this.slotKeyLength, this.slotTop)
1798
+ jsonValuePut(this.room, c, n)
1799
+ }
1800
+
1801
+ /**
1802
+ * Appends the bytes of `text[from, to)` to the end of `bytes`, unless a
1803
+ * line break or the byte `stop` comes first: then nothing is kept.
1804
+ *
1805
+ * @param text - the text the bytes are copied from.
1806
+ * @param from - the offset of the first byte.
1807
+ * @param to - one past the offset of the last byte.
1808
+ * @param stop - a byte that refuses the copy, or `-1` for none.
1809
+ * @returns how many bytes were stored, or `-1` when the copy was refused.
1810
+ *
1811
+ * @internal Read by std/yaml-json.ts, which stores a scalar with nothing
1812
+ * to decode straight from its source.
1813
+ */
1814
+ storeSpan(text: string, from: i32, to: i32, stop: i32): i32 {
1815
+ const at: i32 = this.byteTop
1816
+ const n: i32 = to - from
1817
+ if (from < 0 || n < 0 || to > toI32(text.length) || at < 0) {
1818
+ return -1
1819
+ }
1820
+ const end: i32 = at + n
1821
+ const buf: u8[] = jsonValueRoom(this.bytes, end)
1822
+ this.bytes = buf
1823
+ if (end > toI32(buf.length)) {
1824
+ return -1
1825
+ }
1826
+ let k: i32 = at
1827
+ for (let j: i32 = from; j < to; j += 1) {
1828
+ const code: i32 = toI32(text.charCodeAt(j))
1829
+ // Both breaks are below 14, so one compare passes almost every byte.
1830
+ if (code === stop || (code <= JSON_VALUE_CR && (code === JSON_VALUE_LF || code === JSON_VALUE_CR))) {
1831
+ return -1
1832
+ }
1833
+ // `k` runs `at` to `end` as `j` runs `from` to `to`: in `buf`.
1834
+ uncheckedSet(buf, k, toU8(code))
1835
+ k += 1
1836
+ }
1837
+ this.byteTop = end
1838
+ return n
1839
+ }
1840
+
1841
+ /**
1842
+ * Appends the bytes of `text[from, to)` to the end of `bytes`, as
1843
+ * `storeSpan` does with nothing to refuse: for a caller that knows the span
1844
+ * holds no line break.
1845
+ *
1846
+ * @param text - the text the bytes are copied from.
1847
+ * @param from - the offset of the first byte.
1848
+ * @param to - one past the offset of the last byte.
1849
+ * @returns how many bytes were stored, or `-1` when the span is not in `text`.
1850
+ *
1851
+ * @internal Read by std/yaml-json.ts, for a scalar the parser found to be
1852
+ * its own body.
1853
+ */
1854
+ copySpan(text: string, from: i32, to: i32): i32 {
1855
+ const at: i32 = this.byteTop
1856
+ const n: i32 = to - from
1857
+ if (from < 0 || n < 0 || to > toI32(text.length) || at < 0) {
1858
+ return -1
1859
+ }
1860
+ const buf: u8[] = jsonValueRoom(this.bytes, at + n)
1861
+ this.bytes = buf
1862
+ if (at + n > toI32(buf.length)) {
1863
+ return -1
1864
+ }
1865
+ const end: i32 = at + n
1866
+ let k: i32 = at
1867
+ for (let j: i32 = from; j < to; j += 1) {
1868
+ // `k` runs `at` to `end` as `j` runs `from` to `to`: in `buf`.
1869
+ uncheckedSet(buf, k, toU8(text.charCodeAt(j)))
1870
+ k += 1
1871
+ }
1872
+ this.byteTop = end
1873
+ return n
1874
+ }
1875
+
1876
+ /**
1877
+ * A new string of the `n` bytes stored from `at`, as a handle.
1878
+ *
1879
+ * @internal Read by std/yaml-json.ts, after `storeSpan`.
1880
+ */
1881
+ newStoredString(at: i32, n: i32): JsonValue {
1882
+ return this.handle(this.newNode(JSON_VALUE_STRING, at, n))
1883
+ }
1884
+
1885
+ /**
1886
+ * A new number of the `n` bytes stored from `at`, as a handle; the caller
1887
+ * has checked they are one RFC 8259 number, which `newNumber` would check.
1888
+ *
1889
+ * @internal Read by std/yaml-json.ts, after `storeSpan`.
1890
+ */
1891
+ newStoredNumber(at: i32, n: i32): JsonValue {
1892
+ return this.handle(this.newNode(JSON_VALUE_NUMBER, at, n))
1752
1893
  }
1753
1894
 
1754
1895
  /** A new value of `kind` holding `s`'s bytes, as a handle. */
package/std/json.ts CHANGED
@@ -851,6 +851,10 @@ const JSON_SLASH_T: i32 = 116
851
851
  const JSON_DIGIT_ZERO: i32 = 48
852
852
  const JSON_MINUS: i32 = 45
853
853
  const JSON_DECIMAL_BASE: i32 = 10
854
+ /** The most decimal digits an `i64` has: `-9223372036854775808` has nineteen. */
855
+ const JSON_I64_DIGITS: i32 = 19
856
+ /** The most decimal digits an `i32` has: `-2147483648` has ten. */
857
+ const JSON_I32_DIGITS: i32 = 10
854
858
  const JSON_LOWER_A: i32 = 97
855
859
 
856
860
  /**
@@ -1175,6 +1179,48 @@ export class JsonWriter {
1175
1179
  }
1176
1180
  }
1177
1181
 
1182
+ /**
1183
+ * `key(name)` from a key already quoted: when the writer is minified, in an
1184
+ * object, owes a key and has room for all of it, the comma and `quoted` go
1185
+ * in as one copy; any other way, `key(name)` runs, so the bytes and
1186
+ * `failed` are always the ones `key(name)` leaves.
1187
+ *
1188
+ * @internal Called by the code `jsonEncode` writes for a class (src/emit-json.ts).
1189
+ * @param name - the key, unquoted.
1190
+ * @param quoted - `name` quoted and escaped as `key` escapes it, then a colon.
1191
+ */
1192
+ keyQuoted(name: string, quoted: string): void {
1193
+ const buf: u8[] = this.buf
1194
+ const cap: i32 = toI32(buf.length)
1195
+ const sep: i32 = this.sep
1196
+ const n: i32 = toI32(quoted.length)
1197
+ const at: i32 = this.end
1198
+ if (
1199
+ this.failed !== JSON_WRITE_OK ||
1200
+ sep < JSON_WRITER_SEP_NONE ||
1201
+ sep > JSON_WRITER_SEP_COMMA ||
1202
+ this.afterKey ||
1203
+ !jsonWriterInObject(this) ||
1204
+ at < 0 ||
1205
+ at > cap - sep - n
1206
+ ) {
1207
+ this.key(name)
1208
+ return
1209
+ }
1210
+ if (sep === JSON_WRITER_SEP_COMMA && at < cap) {
1211
+ buf[at] = toU8(JSON_COMMA)
1212
+ }
1213
+ let j: i32 = at + sep
1214
+ let k: i32 = 0
1215
+ while (k < n && j >= 0 && j < cap) {
1216
+ buf[j] = toU8(quoted.charCodeAt(k))
1217
+ j += 1
1218
+ k += 1
1219
+ }
1220
+ this.end = j
1221
+ this.afterKey = true
1222
+ }
1223
+
1178
1224
  /**
1179
1225
  * `s` quoted and escaped as `JSON.stringify` escapes it; every other byte
1180
1226
  * goes through as itself.
@@ -1856,13 +1902,18 @@ const jsonWriterPutRuns = (w: JsonWriter, bytes: u8[], off: i32, len: i32): bool
1856
1902
  * step the same under Node, where `/` does not truncate.
1857
1903
  */
1858
1904
  const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
1905
+ // Counted against powers of ten on `-|v|`, as `jsonWriterPutDigits64` counts.
1906
+ const negative: i32 = v < 0 ? v : -v
1859
1907
  let digits: i32 = 1
1860
- let rest: i32 = v
1861
- while (rest <= -JSON_DECIMAL_BASE || rest >= JSON_DECIMAL_BASE) {
1862
- rest = (rest - (rest % JSON_DECIMAL_BASE)) / JSON_DECIMAL_BASE
1908
+ let bound: i32 = -JSON_DECIMAL_BASE
1909
+ while (digits < JSON_I32_DIGITS && negative <= bound) {
1863
1910
  digits += 1
1911
+ if (digits < JSON_I32_DIGITS) {
1912
+ bound = bound * JSON_DECIMAL_BASE
1913
+ }
1864
1914
  }
1865
1915
  const sign: i32 = v < 0 ? 1 : 0
1916
+ let rest: i32 = v
1866
1917
  const extra: i32 = suffix >= 0 ? 1 : 0
1867
1918
  const buf: u8[] = w.buf
1868
1919
  const at: i32 = w.end
@@ -1877,7 +1928,6 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
1877
1928
  if (extra === 1 && at + width - 1 < toI32(buf.length) && at + width - 1 >= 0) {
1878
1929
  buf[at + width - 1] = toU8(JSON_DIGIT_ZERO + suffix)
1879
1930
  }
1880
- rest = v
1881
1931
  for (let k: i32 = at + sign + digits - 1; k >= at + sign && k >= 0 && k < toI32(buf.length); k -= 1) {
1882
1932
  const digit: i32 = rest % JSON_DECIMAL_BASE
1883
1933
  buf[k] = toU8(JSON_DIGIT_ZERO + (digit < 0 ? -digit : digit))
@@ -1891,14 +1941,20 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
1891
1941
  const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
1892
1942
  // Built with `toI64`: under Node an `i64` is a BigInt, and a bare `10` would
1893
1943
  // stay a number that `%` and `/` refuse to mix with one.
1944
+ // The digits are counted against powers of ten on `-|v|`, which every `i64`
1945
+ // has, so the count costs a compare and a multiply per digit, not a divide.
1894
1946
  const ten: i64 = toI64(10)
1947
+ const negative: i64 = v < 0 ? v : -v
1895
1948
  let digits: i32 = 1
1896
- let rest: i64 = v
1897
- while (rest <= -ten || rest >= ten) {
1898
- rest = rest / ten
1949
+ let bound: i64 = -ten
1950
+ while (digits < JSON_I64_DIGITS && negative <= bound) {
1899
1951
  digits += 1
1952
+ if (digits < JSON_I64_DIGITS) {
1953
+ bound = bound * ten
1954
+ }
1900
1955
  }
1901
1956
  const sign: i32 = v < 0 ? 1 : 0
1957
+ let rest: i64 = v
1902
1958
  const buf: u8[] = w.buf
1903
1959
  const at: i32 = w.end
1904
1960
  const width: i32 = sign + digits
@@ -1909,7 +1965,6 @@ const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
1909
1965
  if (sign === 1) {
1910
1966
  buf[at] = toU8(JSON_MINUS)
1911
1967
  }
1912
- rest = v
1913
1968
  for (let k: i32 = at + width - 1; k >= at + sign && k >= 0 && k < toI32(buf.length); k -= 1) {
1914
1969
  const digit: i32 = toI32(rest % ten)
1915
1970
  buf[k] = toU8(JSON_DIGIT_ZERO + (digit < 0 ? -digit : digit))
package/std/net/fetch.ts CHANGED
@@ -130,7 +130,7 @@ import { PkiRoots, pkiLoadRoots, pkiLoadSystemRoots } from "nish/crypto/pki-root
130
130
  import { TlsClient, TlsClientConfig, tlsClientIsIpLiteral } from "nish/net/tls/client"
131
131
  import { TLS_RECORD_AUTH, TlsRecordClient } from "nish/net/tls/record-client"
132
132
  import { TLS_RECORD_AGAIN, TLS_RECORD_STATE_FAILED, TLS_RECORD_STATE_OPEN } from "nish/net/tls/record-server"
133
- import { TlsClientTrust, tlsClientTrust, tlsClientVerdict } from "nish/net/tls/verify"
133
+ import { TlsClientTrust, tlsClientDistrustSpki, tlsClientTrust, tlsClientVerdict } from "nish/net/tls/verify"
134
134
 
135
135
  /** No error. */
136
136
  export const HTTP_E_NONE: i32 = 0
@@ -295,6 +295,14 @@ export class HttpOptions {
295
295
  maxBodyBytes: i64 = 268435456
296
296
  /** A PEM file of the roots an https server's chain must end at; null for the system's. */
297
297
  rootsPemPath: string | null = null
298
+ /**
299
+ * Keys no https server's chain may hold, each the SHA-256 of a
300
+ * certificate's subjectPublicKeyInfo DER, 32 bytes: a chain with a leaf,
301
+ * intermediate or root whose key is listed fails with `HTTP_E_CERTIFICATE`
302
+ * (`tlsClientDistrustSpki`). Null, the default, for none; a hash of
303
+ * another length fails every https fetch rather than being skipped.
304
+ */
305
+ distrustSpkiSha256: u8[][] | null = null
298
306
  /**
299
307
  * For a URL the program did not choose: only the addresses and hosts the
300
308
  * policy allows are reached, no proxy is used, and its caps apply (see
@@ -1691,8 +1699,10 @@ class HttpClientFault {
1691
1699
 
1692
1700
  /**
1693
1701
  * The roots an https fetch verifies against: the caller's `rootsPemPath`, or
1694
- * the system's bundle without one; null with `fault` set when none could be
1695
- * read, which fails the fetch rather than trusting nothing.
1702
+ * the system's bundle without one, distrusting the keys of
1703
+ * `distrustSpkiSha256`; null with `fault` set when no root could be read,
1704
+ * which fails the fetch rather than trusting nothing, or when a hash to
1705
+ * distrust is not one, which fails it rather than trusting the key.
1696
1706
  */
1697
1707
  const httpClientLoadTrust = (options: HttpOptions, fault: HttpClientFault): TlsClientTrust | null => {
1698
1708
  const path: string | null = options.rootsPemPath
@@ -1702,7 +1712,15 @@ const httpClientLoadTrust = (options: HttpOptions, fault: HttpClientFault): TlsC
1702
1712
  fault.fail(HTTP_E_CERTIFICATE, `no trusted root certificate could be read from ${where}`)
1703
1713
  return null
1704
1714
  }
1705
- return tlsClientTrust(roots)
1715
+ const trust: TlsClientTrust = tlsClientTrust(roots)
1716
+ const distrusted: u8[][] = options.distrustSpkiSha256 ?? []
1717
+ for (const hash of distrusted) {
1718
+ if (!tlsClientDistrustSpki(trust, hash)) {
1719
+ fault.fail(HTTP_E_CERTIFICATE, `a key to distrust is ${hash.length} bytes, not a SHA-256 of 32`)
1720
+ return null
1721
+ }
1722
+ }
1723
+ return trust
1706
1724
  }
1707
1725
 
1708
1726
  /**