nish 0.46.0 → 0.48.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 (45) hide show
  1. package/docs/AI.md +29 -18
  2. package/package.json +5 -5
  3. package/runtime/gfx/README.md +55 -0
  4. package/runtime/gfx/nish-gfx.c +1063 -0
  5. package/runtime/gfx/nish-gfx.h +221 -0
  6. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  7. package/runtime/nish.d.ts +22 -5
  8. package/runtime/nish.h +1 -0
  9. package/runtime/nish.mjs +1 -0
  10. package/runtime/runtime-bytes.c +167 -7
  11. package/runtime/runtime-net.c +122 -52
  12. package/runtime/runtime-uring.c +150 -69
  13. package/runtime/sqlite/nish-sqlite.c +166 -39
  14. package/scripts/build.sh +88 -3
  15. package/std/README.md +2 -2
  16. package/std/base64.ts +28 -30
  17. package/std/compress/deflate.ts +1371 -704
  18. package/std/crypto/aes.ts +81 -45
  19. package/std/crypto/bcrypt-batch.ts +23 -4
  20. package/std/crypto/bcrypt.ts +36 -11
  21. package/std/crypto/chacha20poly1305.ts +93 -52
  22. package/std/crypto/x25519.ts +7 -4
  23. package/std/host/game.ts +52 -20
  24. package/std/host/ring.ts +115 -31
  25. package/std/host/text.ts +9 -3
  26. package/std/json.ts +83 -0
  27. package/std/math.ts +161 -131
  28. package/std/net/handler.ts +24 -6
  29. package/std/net/http-server.ts +358 -141
  30. package/std/net/http1-server.ts +44 -13
  31. package/std/net/prefork.ts +1 -2
  32. package/std/net/static.ts +7 -2
  33. package/std/random.ts +34 -51
  34. package/std/raster.ts +377 -113
  35. package/std/regex.ts +46 -15
  36. package/std/release.ts +19 -13
  37. package/std/sqlite.ts +380 -16
  38. package/std/synth.ts +20 -3
  39. package/std/temporal.ts +740 -363
  40. package/std/term-io.ts +81 -33
  41. package/std/term-pixels.ts +287 -112
  42. package/std/term-probe.ts +7 -3
  43. package/std/term-rate.ts +142 -66
  44. package/std/term.ts +782 -406
  45. package/std/testing.ts +14 -7
@@ -141,6 +141,7 @@ import {
141
141
  } from "nish/net/http-compress"
142
142
  import { netClose, netMss, netRead, netShutdown, netWrite, netWrite2, tcpAccept } from "nish:net"
143
143
  import {
144
+ HTTP1_BAD_WINDOW,
144
145
  HTTP1_BODY,
145
146
  HTTP1_CHUNKED,
146
147
  HTTP1_CLOSED,
@@ -154,7 +155,7 @@ import {
154
155
  HTTP1_UPGRADE,
155
156
  Http1Parser,
156
157
  http1BytesAre,
157
- http1WriteChunk,
158
+ http1TryWriteChunk,
158
159
  http1WriteLastChunk,
159
160
  http1WriteResponseHead,
160
161
  } from "nish/net/http1-codec"
@@ -2090,7 +2091,12 @@ export class Http1Connection {
2090
2091
  return H1_AGAIN
2091
2092
  }
2092
2093
  if (chunked) {
2093
- this.outputEnd = http1WriteChunk(this.output, this.outputEnd, buf, off, take)
2094
+ // The window was checked on entry, and `take` only shortens it.
2095
+ const end: i32 = http1TryWriteChunk(this.output, this.outputEnd, buf, off, take)
2096
+ if (end === HTTP1_BAD_WINDOW) {
2097
+ return H1_INVALID
2098
+ }
2099
+ this.outputEnd = end
2094
2100
  } else {
2095
2101
  this.outputEnd = http1ServerCopy(this.output, this.outputEnd, buf, off, take)
2096
2102
  if (this.responseFraming >= 0) {
@@ -2769,6 +2775,8 @@ export class Http1Server {
2769
2775
  private expireAfter: i64 = 0
2770
2776
  /** How many calls of `expire` walked every slot, for a program measuring its cost. */
2771
2777
  expireScans: i32 = 0
2778
+ /** The instant `tick` gave, in `monotonicNanos`; 0 while the server reads the clock itself. */
2779
+ private ticked: i64 = 0
2772
2780
 
2773
2781
  /**
2774
2782
  * A pool of `size` slots, at least one, serving `config` on `listener`.
@@ -3062,7 +3070,7 @@ export class Http1Server {
3062
3070
  this.armed[k] = H1_WANT_READ
3063
3071
  // The slot starts resting: it is given a connection when it first reads.
3064
3072
  this.at[k] = -1
3065
- this.rested[k] = monotonicNanos()
3073
+ this.rested[k] = this.clock()
3066
3074
  this.owner[k] = this.accepts
3067
3075
  // A serial only keys a kept head to its peer: in u64, which wraps where
3068
3076
  // an i64 would be a site, after more accepts than a server ever sees.
@@ -3074,6 +3082,25 @@ export class Http1Server {
3074
3082
  return k
3075
3083
  }
3076
3084
 
3085
+ /**
3086
+ * Sets the instant, in `monotonicNanos`, that the server stamps activity
3087
+ * with until the next `tick`: a loop that calls it once a pass spares every
3088
+ * `readable` and `flush` of the pass its own clock read, and a timeout,
3089
+ * counted in seconds, moves by the pass's length at most. 0 goes back to
3090
+ * reading the clock at each stamp, which is what a server that is never
3091
+ * ticked does.
3092
+ *
3093
+ * @param now the current time in `monotonicNanos`, or 0
3094
+ */
3095
+ tick(now: i64): void {
3096
+ this.ticked = now
3097
+ }
3098
+
3099
+ /** The instant `tick` gave, or the clock when it gave none. */
3100
+ private clock(): i64 {
3101
+ return this.ticked !== 0 ? this.ticked : monotonicNanos()
3102
+ }
3103
+
3077
3104
  /**
3078
3105
  * The socket of `slot` is readable: reads it while the connection has room
3079
3106
  * and the socket has more, then sends. Answers the interest. It begins a
@@ -3091,13 +3118,16 @@ export class Http1Server {
3091
3118
  this.date.hold()
3092
3119
  const fd: i32 = conn.fd
3093
3120
  let moved: boolean = false
3121
+ const scratch: u8[] = this.scratch
3094
3122
  while (true) {
3095
3123
  const room: i32 = conn.inputRoom()
3096
- const want: i32 = room < toI32(this.scratch.length) ? room : toI32(this.scratch.length)
3097
- if (want <= 0) {
3124
+ const size: i32 = toI32(scratch.length)
3125
+ const want: i32 = room < size ? room : size
3126
+ // `0 < want <= scratch.length`, spelled so the prover reads the window.
3127
+ if (want <= 0 || want > toI32(scratch.length)) {
3098
3128
  break
3099
3129
  }
3100
- const n: i32 = netRead(fd, this.scratch, H1_ZERO, want)
3130
+ const n: i32 = netRead(fd, scratch, 0, want)
3101
3131
  if (n <= 0) {
3102
3132
  if (n === 0) {
3103
3133
  conn.endInput()
@@ -3107,7 +3137,7 @@ export class Http1Server {
3107
3137
  break
3108
3138
  }
3109
3139
  moved = true
3110
- conn.feed(this.scratch, H1_ZERO, n)
3140
+ conn.feed(scratch, H1_ZERO, n)
3111
3141
  // A short read emptied the socket. The loop's readiness is
3112
3142
  // level-triggered, so more input, or the peer's close, wakes it again;
3113
3143
  // reading once more here would only answer -11, at the price of a
@@ -3117,7 +3147,7 @@ export class Http1Server {
3117
3147
  }
3118
3148
  }
3119
3149
  if (moved) {
3120
- const now: i64 = monotonicNanos()
3150
+ const now: i64 = this.clock()
3121
3151
  conn.lastActive = now
3122
3152
  conn.headArrived(now)
3123
3153
  }
@@ -3197,7 +3227,7 @@ export class Http1Server {
3197
3227
  // sends (RFC 9112 §9.6), for `lingerTimeout` and `lingerBytes` at most.
3198
3228
  // Closed with those bytes unread, the socket would answer them with a
3199
3229
  // reset that can destroy the response before the client reads it (H1-2).
3200
- const now: i64 = monotonicNanos()
3230
+ const now: i64 = this.clock()
3201
3231
  const conn: Http1Connection | null = this.attach(slot)
3202
3232
  if (conn === null || !conn.lingerStart(now) || netShutdown(conn.fd, H1_SHUT_WRITE) < 0) {
3203
3233
  return H1_DONE
@@ -3217,13 +3247,14 @@ export class Http1Server {
3217
3247
  */
3218
3248
  private drain(conn: Http1Connection): i32 {
3219
3249
  const fd: i32 = conn.fd
3250
+ const scratch: u8[] = this.scratch
3220
3251
  while (true) {
3221
- const size: i32 = toI32(this.scratch.length)
3252
+ const size: i32 = toI32(scratch.length)
3222
3253
  // `0 <= 0 <= size <= scratch.length`: a scratch with no room drains nothing.
3223
3254
  if (size <= 0) {
3224
3255
  return H1_DONE
3225
3256
  }
3226
- const n: i32 = netRead(fd, this.scratch, H1_ZERO, size)
3257
+ const n: i32 = netRead(fd, scratch, 0, size)
3227
3258
  if (n === H1_AGAIN) {
3228
3259
  return H1_WANT_READ
3229
3260
  }
@@ -3279,7 +3310,7 @@ export class Http1Server {
3279
3310
  conn.abort()
3280
3311
  break
3281
3312
  }
3282
- const n: i32 = netWrite(fd, conn.output, start, http1ServerSub(end, start))
3313
+ const n: i32 = netWrite(fd, conn.output, start, end - start)
3283
3314
  if (n <= 0) {
3284
3315
  if (n !== H1_AGAIN) {
3285
3316
  conn.abort()
@@ -3289,7 +3320,7 @@ export class Http1Server {
3289
3320
  conn.consume(n)
3290
3321
  }
3291
3322
  if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
3292
- conn.lastActive = monotonicNanos()
3323
+ conn.lastActive = this.clock()
3293
3324
  }
3294
3325
  conn.release()
3295
3326
  }
@@ -1166,8 +1166,7 @@ const preforkInvalid = (config: PreforkConfig): string => {
1166
1166
 
1167
1167
  /** Read and drop what a channel holds: `preforkWith`'s callback. */
1168
1168
  const preforkDiscard = (buf: u8[], _index: i32, fd: i32, _events: i32): void => {
1169
- const size: i32 = toI32(buf.length)
1170
- while (netRead(fd, buf, 0, size) > 0) {
1169
+ while (netRead(fd, buf, 0, toI32(buf.length)) > 0) {
1171
1170
  // Dropped.
1172
1171
  }
1173
1172
  }
package/std/net/static.ts CHANGED
@@ -1411,9 +1411,14 @@ export class StaticFiles {
1411
1411
  }
1412
1412
  return
1413
1413
  }
1414
- if (status !== 304) {
1414
+ const path: u8[] = this.path
1415
+ const m: i32 = this.pathLength
1416
+ // `0 <= pathLength <= path.length` holds once `staticResolve` sets it;
1417
+ // the guard keeps `String.fromBytes` proven for a `--deny-panics` caller.
1418
+ if (status !== 304 && m >= 0 && m <= toI32(path.length)) {
1419
+ const type: string = staticContentType(String.fromBytes(path, 0, m))
1415
1420
  names.push(httpFieldBytes("content-type"))
1416
- values.push(httpFieldBytes(staticContentType(String.fromBytes(this.path, 0, this.pathLength))))
1421
+ values.push(httpFieldBytes(type))
1417
1422
  }
1418
1423
  if (this.etagLength > 0) {
1419
1424
  names.push(httpFieldBytes("etag"))
package/std/random.ts CHANGED
@@ -22,19 +22,15 @@
22
22
  * mixing function is the shape of SplitMix (Steele, Lea and Flood, "Fast
23
23
  * splittable pseudorandom number generators", OOPSLA 2014), on 32 bits.
24
24
  *
25
- * **The same bits under Node.** The two operations that wrap are spelled
26
- * `wrappingAdd` and `wrappingMul` from `nish:unsafe`, which define the
27
- * two's-complement wrap natively and answer `(a + b) | 0` and `Math.imul(a, b)`
28
- * under Node (docs/LANGUAGE.md, "nish:unsafe"); every shift has a constant
25
+ * **The same bits under Node.** A wrapping multiply is `Math.imul`, the low
26
+ * 32 bits of the product both ways. Every other sum, difference and remainder
27
+ * is taken in `f64` on the state read as an unsigned count below 2^32, where
28
+ * each answer is an integer below 2^33 and so exact, and comes back to an
29
+ * `i32` only once a compare has put it in range. Every shift has a constant
29
30
  * count, and `^` and `>>> 16` answer the same 32 bits in JavaScript as in the
30
- * compiled program.
31
- *
32
- * `nextInt` reads its span and its draws as unsigned counts with no `u32`
33
- * (whose conversions saturate under Node): the sign bit flipped orders them
34
- * under a signed compare, and the remainder is taken in pieces that stay below
35
- * 2^31. `nextF64` builds its double from integers below 2^53, so its products
36
- * and sums are exact both ways. The program a test prints under Node is the one
37
- * it prints compiled (docs/RUN_UNDER_NODE.md).
31
+ * compiled program. `nextF64` builds its double from integers below 2^53, so
32
+ * its products and sums are exact both ways. The program a test prints under
33
+ * Node is the one it prints compiled (docs/RUN_UNDER_NODE.md).
38
34
  *
39
35
  * **It is not for cryptography**: a key or a nonce comes from
40
36
  * `crypto.getRandomValues`.
@@ -45,10 +41,9 @@
45
41
  * would be a write their caller can see, and is refused as any such write is;
46
42
  * give each task its own, seeded from its index.
47
43
  */
48
- import { wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
49
44
 
50
- /** 0x9E3779B9 as an `i32`: the Weyl increment, odd, so the walk visits every state. */
51
- const GOLDEN: i32 = -1640531527
45
+ /** 0x9E3779B9, the Weyl increment: odd, so the walk visits every state. */
46
+ const GOLDEN: f64 = 2654435769.0
52
47
 
53
48
  /** 0x85EBCA6B as an `i32`: MurmurHash3's first finaliser multiplier (public domain). */
54
49
  const MIX_1: i32 = -2048144789
@@ -59,45 +54,32 @@ const MIX_2: i32 = -1028477387
59
54
  /** 2^26, the weight of the high draw in `nextF64`'s 53 bits. */
60
55
  const TWO_26: f64 = 67108864.0
61
56
 
62
- /** 2^53, which scales a 53-bit integer into [0, 1). */
63
- const TWO_53: f64 = 9007199254740992.0
57
+ /** 2^31: an unsigned count at least this is a negative `i32`. */
58
+ const TWO_31: f64 = 2147483648.0
64
59
 
65
- /** The sign bit: `x ^ SIGN` orders the bits of `x` as an unsigned count under a signed compare. */
66
- const SIGN: i32 = -2147483648
60
+ /** 2^32, the number of states. */
61
+ const TWO_32: f64 = 4294967296.0
67
62
 
68
- /** 2^31 - 1, the `i32` just below 2^31, from which `remainder` finds 2^31 mod m. */
69
- const MAX: i32 = 2147483647
63
+ /** 2^53, which scales a 53-bit integer into [0, 1). */
64
+ const TWO_53: f64 = 9007199254740992.0
70
65
 
71
- /** Whether `a < b` when both are read as unsigned 32-bit counts. */
72
- const below = (a: i32, b: i32): boolean => (a ^ SIGN) < (b ^ SIGN)
66
+ /** The bits of `x` read as an unsigned count, from 0 to 2^32 - 1. */
67
+ const unsigned = (x: i32): f64 => (x < 0 ? toF64(x) + TWO_32 : toF64(x))
73
68
 
74
69
  /**
75
- * `a mod m` with both read as unsigned 32-bit counts and `m` not 0. A count
76
- * of 2^31 or more is a negative `i32`, so it is taken apart into pieces a
77
- * signed `%` answers the same in JavaScript, and every sum stays below 2^31.
70
+ * The `i32` whose bits read as the unsigned count `u`, which is an integer
71
+ * from 0 to 2^32 - 1: below 2^31 it is `u`, and from there `u - 2^32`, so
72
+ * each `toI32` converts a value inside the `i32` range.
78
73
  */
79
- const remainder = (a: i32, m: i32): i32 => {
80
- if (m < 0) {
81
- // m is 2^31 or more, so a is less than 2m: at most one m comes off.
82
- return below(a, m) ? a : wrappingSub(a, m)
83
- }
84
- if (a >= 0) {
85
- return a % m
86
- }
87
- // a is 2^31 + low, so a mod m is low plus 2^31 mod m, added mod m.
88
- const low: i32 = (a ^ SIGN) % m
89
- const high: i32 = wrappingAdd(MAX % m, 1) % m
90
- const room: i32 = wrappingSub(m, high)
91
- return low >= room ? wrappingSub(low, room) : wrappingAdd(low, high)
92
- }
74
+ const bits = (u: f64): i32 => (u < TWO_31 ? toI32(u) : toI32(u - TWO_32))
93
75
 
94
76
  /** MurmurHash3's `fmix32`: every bit of `h` into every bit of the answer, a bijection on 32 bits. */
95
77
  const mix = (h: i32): i32 => {
96
78
  let x: i32 = h
97
79
  x = x ^ (x >>> 16)
98
- x = wrappingMul(x, MIX_1)
80
+ x = Math.imul(x, MIX_1)
99
81
  x = x ^ (x >>> 13)
100
- x = wrappingMul(x, MIX_2)
82
+ x = Math.imul(x, MIX_2)
101
83
  x = x ^ (x >>> 16)
102
84
  return x
103
85
  }
@@ -128,7 +110,8 @@ export class Rng {
128
110
  * steps once.
129
111
  */
130
112
  nextI32(): i32 {
131
- this.state = wrappingAdd(this.state, GOLDEN)
113
+ const next: f64 = unsigned(this.state) + GOLDEN
114
+ this.state = bits(next < TWO_32 ? next : next - TWO_32)
132
115
  return mix(this.state)
133
116
  }
134
117
 
@@ -168,16 +151,16 @@ export class Rng {
168
151
  if (lo > hi) {
169
152
  panic("Rng.nextInt: lo is greater than hi")
170
153
  }
171
- const span: i32 = wrappingAdd(wrappingSub(hi, lo), 1)
172
- if (span === 0) {
154
+ // From 1 to 2^32, and lo plus any remainder of it is at most hi.
155
+ const span: f64 = toF64(hi) - toF64(lo) + 1.0
156
+ if (span === TWO_32) {
173
157
  return this.nextI32()
174
158
  }
175
- // 2^32 mod span, as (2^32 - span) mod span.
176
- const refused: i32 = remainder(wrappingSub(0, span), span)
177
- let draw: i32 = this.nextI32()
178
- while (below(draw, refused)) {
179
- draw = this.nextI32()
159
+ const refused: f64 = TWO_32 % span
160
+ let draw: f64 = unsigned(this.nextI32())
161
+ while (draw < refused) {
162
+ draw = unsigned(this.nextI32())
180
163
  }
181
- return wrappingAdd(lo, remainder(draw, span))
164
+ return toI32(toF64(lo) + (draw % span))
182
165
  }
183
166
  }