nish 0.45.1 → 0.47.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.
- package/docs/AI.md +29 -18
- package/docs/INSTALL.md +3 -1
- package/package.json +5 -5
- package/runtime/nish.d.ts +41 -0
- package/runtime/nish.h +11 -0
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +272 -9
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +394 -85
- package/runtime/sqlite/nish-sqlite.c +448 -148
- package/std/README.md +14 -14
- package/std/base64.ts +28 -30
- package/std/crypto/aes.ts +81 -45
- package/std/crypto/bcrypt-batch.ts +23 -4
- package/std/crypto/bcrypt.ts +36 -11
- package/std/crypto/chacha20poly1305.ts +93 -52
- package/std/crypto/x25519.ts +7 -4
- package/std/host/game.ts +52 -20
- package/std/host/ring.ts +115 -31
- package/std/host/text.ts +9 -3
- package/std/json.ts +83 -0
- package/std/math.ts +161 -131
- package/std/net/handler.ts +42 -7
- package/std/net/http1-server.ts +36 -11
- package/std/net/prefork.ts +1 -2
- package/std/net/static.ts +7 -2
- package/std/random.ts +34 -51
- package/std/raster.ts +360 -110
- package/std/regex.ts +46 -15
- package/std/release.ts +19 -13
- package/std/sqlite.ts +551 -16
- package/std/synth.ts +11 -2
- package/std/temporal.ts +740 -363
- package/std/term-io.ts +81 -33
- package/std/term-pixels.ts +287 -112
- package/std/term-probe.ts +7 -3
- package/std/term-rate.ts +142 -66
- package/std/term.ts +782 -406
- package/std/testing.ts +14 -7
package/std/net/http1-server.ts
CHANGED
|
@@ -2769,6 +2769,8 @@ export class Http1Server {
|
|
|
2769
2769
|
private expireAfter: i64 = 0
|
|
2770
2770
|
/** How many calls of `expire` walked every slot, for a program measuring its cost. */
|
|
2771
2771
|
expireScans: i32 = 0
|
|
2772
|
+
/** The instant `tick` gave, in `monotonicNanos`; 0 while the server reads the clock itself. */
|
|
2773
|
+
private ticked: i64 = 0
|
|
2772
2774
|
|
|
2773
2775
|
/**
|
|
2774
2776
|
* A pool of `size` slots, at least one, serving `config` on `listener`.
|
|
@@ -3062,7 +3064,7 @@ export class Http1Server {
|
|
|
3062
3064
|
this.armed[k] = H1_WANT_READ
|
|
3063
3065
|
// The slot starts resting: it is given a connection when it first reads.
|
|
3064
3066
|
this.at[k] = -1
|
|
3065
|
-
this.rested[k] =
|
|
3067
|
+
this.rested[k] = this.clock()
|
|
3066
3068
|
this.owner[k] = this.accepts
|
|
3067
3069
|
// A serial only keys a kept head to its peer: in u64, which wraps where
|
|
3068
3070
|
// an i64 would be a site, after more accepts than a server ever sees.
|
|
@@ -3074,6 +3076,25 @@ export class Http1Server {
|
|
|
3074
3076
|
return k
|
|
3075
3077
|
}
|
|
3076
3078
|
|
|
3079
|
+
/**
|
|
3080
|
+
* Sets the instant, in `monotonicNanos`, that the server stamps activity
|
|
3081
|
+
* with until the next `tick`: a loop that calls it once a pass spares every
|
|
3082
|
+
* `readable` and `flush` of the pass its own clock read, and a timeout,
|
|
3083
|
+
* counted in seconds, moves by the pass's length at most. 0 goes back to
|
|
3084
|
+
* reading the clock at each stamp, which is what a server that is never
|
|
3085
|
+
* ticked does.
|
|
3086
|
+
*
|
|
3087
|
+
* @param now the current time in `monotonicNanos`, or 0
|
|
3088
|
+
*/
|
|
3089
|
+
tick(now: i64): void {
|
|
3090
|
+
this.ticked = now
|
|
3091
|
+
}
|
|
3092
|
+
|
|
3093
|
+
/** The instant `tick` gave, or the clock when it gave none. */
|
|
3094
|
+
private clock(): i64 {
|
|
3095
|
+
return this.ticked !== 0 ? this.ticked : monotonicNanos()
|
|
3096
|
+
}
|
|
3097
|
+
|
|
3077
3098
|
/**
|
|
3078
3099
|
* The socket of `slot` is readable: reads it while the connection has room
|
|
3079
3100
|
* and the socket has more, then sends. Answers the interest. It begins a
|
|
@@ -3091,13 +3112,16 @@ export class Http1Server {
|
|
|
3091
3112
|
this.date.hold()
|
|
3092
3113
|
const fd: i32 = conn.fd
|
|
3093
3114
|
let moved: boolean = false
|
|
3115
|
+
const scratch: u8[] = this.scratch
|
|
3094
3116
|
while (true) {
|
|
3095
3117
|
const room: i32 = conn.inputRoom()
|
|
3096
|
-
const
|
|
3097
|
-
|
|
3118
|
+
const size: i32 = toI32(scratch.length)
|
|
3119
|
+
const want: i32 = room < size ? room : size
|
|
3120
|
+
// `0 < want <= scratch.length`, spelled so the prover reads the window.
|
|
3121
|
+
if (want <= 0 || want > toI32(scratch.length)) {
|
|
3098
3122
|
break
|
|
3099
3123
|
}
|
|
3100
|
-
const n: i32 = netRead(fd,
|
|
3124
|
+
const n: i32 = netRead(fd, scratch, 0, want)
|
|
3101
3125
|
if (n <= 0) {
|
|
3102
3126
|
if (n === 0) {
|
|
3103
3127
|
conn.endInput()
|
|
@@ -3107,7 +3131,7 @@ export class Http1Server {
|
|
|
3107
3131
|
break
|
|
3108
3132
|
}
|
|
3109
3133
|
moved = true
|
|
3110
|
-
conn.feed(
|
|
3134
|
+
conn.feed(scratch, H1_ZERO, n)
|
|
3111
3135
|
// A short read emptied the socket. The loop's readiness is
|
|
3112
3136
|
// level-triggered, so more input, or the peer's close, wakes it again;
|
|
3113
3137
|
// reading once more here would only answer -11, at the price of a
|
|
@@ -3117,7 +3141,7 @@ export class Http1Server {
|
|
|
3117
3141
|
}
|
|
3118
3142
|
}
|
|
3119
3143
|
if (moved) {
|
|
3120
|
-
const now: i64 =
|
|
3144
|
+
const now: i64 = this.clock()
|
|
3121
3145
|
conn.lastActive = now
|
|
3122
3146
|
conn.headArrived(now)
|
|
3123
3147
|
}
|
|
@@ -3197,7 +3221,7 @@ export class Http1Server {
|
|
|
3197
3221
|
// sends (RFC 9112 §9.6), for `lingerTimeout` and `lingerBytes` at most.
|
|
3198
3222
|
// Closed with those bytes unread, the socket would answer them with a
|
|
3199
3223
|
// reset that can destroy the response before the client reads it (H1-2).
|
|
3200
|
-
const now: i64 =
|
|
3224
|
+
const now: i64 = this.clock()
|
|
3201
3225
|
const conn: Http1Connection | null = this.attach(slot)
|
|
3202
3226
|
if (conn === null || !conn.lingerStart(now) || netShutdown(conn.fd, H1_SHUT_WRITE) < 0) {
|
|
3203
3227
|
return H1_DONE
|
|
@@ -3217,13 +3241,14 @@ export class Http1Server {
|
|
|
3217
3241
|
*/
|
|
3218
3242
|
private drain(conn: Http1Connection): i32 {
|
|
3219
3243
|
const fd: i32 = conn.fd
|
|
3244
|
+
const scratch: u8[] = this.scratch
|
|
3220
3245
|
while (true) {
|
|
3221
|
-
const size: i32 = toI32(
|
|
3246
|
+
const size: i32 = toI32(scratch.length)
|
|
3222
3247
|
// `0 <= 0 <= size <= scratch.length`: a scratch with no room drains nothing.
|
|
3223
3248
|
if (size <= 0) {
|
|
3224
3249
|
return H1_DONE
|
|
3225
3250
|
}
|
|
3226
|
-
const n: i32 = netRead(fd,
|
|
3251
|
+
const n: i32 = netRead(fd, scratch, 0, size)
|
|
3227
3252
|
if (n === H1_AGAIN) {
|
|
3228
3253
|
return H1_WANT_READ
|
|
3229
3254
|
}
|
|
@@ -3279,7 +3304,7 @@ export class Http1Server {
|
|
|
3279
3304
|
conn.abort()
|
|
3280
3305
|
break
|
|
3281
3306
|
}
|
|
3282
|
-
const n: i32 = netWrite(fd, conn.output, start,
|
|
3307
|
+
const n: i32 = netWrite(fd, conn.output, start, end - start)
|
|
3283
3308
|
if (n <= 0) {
|
|
3284
3309
|
if (n !== H1_AGAIN) {
|
|
3285
3310
|
conn.abort()
|
|
@@ -3289,7 +3314,7 @@ export class Http1Server {
|
|
|
3289
3314
|
conn.consume(n)
|
|
3290
3315
|
}
|
|
3291
3316
|
if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
|
|
3292
|
-
conn.lastActive =
|
|
3317
|
+
conn.lastActive = this.clock()
|
|
3293
3318
|
}
|
|
3294
3319
|
conn.release()
|
|
3295
3320
|
}
|
package/std/net/prefork.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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(
|
|
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.**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
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
|
-
*
|
|
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
|
|
51
|
-
const GOLDEN:
|
|
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^
|
|
63
|
-
const
|
|
57
|
+
/** 2^31: an unsigned count at least this is a negative `i32`. */
|
|
58
|
+
const TWO_31: f64 = 2147483648.0
|
|
64
59
|
|
|
65
|
-
/**
|
|
66
|
-
const
|
|
60
|
+
/** 2^32, the number of states. */
|
|
61
|
+
const TWO_32: f64 = 4294967296.0
|
|
67
62
|
|
|
68
|
-
/** 2^
|
|
69
|
-
const
|
|
63
|
+
/** 2^53, which scales a 53-bit integer into [0, 1). */
|
|
64
|
+
const TWO_53: f64 = 9007199254740992.0
|
|
70
65
|
|
|
71
|
-
/**
|
|
72
|
-
const
|
|
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
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
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
|
|
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 =
|
|
80
|
+
x = Math.imul(x, MIX_1)
|
|
99
81
|
x = x ^ (x >>> 13)
|
|
100
|
-
x =
|
|
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
|
-
|
|
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
|
-
|
|
172
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
|
164
|
+
return toI32(toF64(lo) + (draw % span))
|
|
182
165
|
}
|
|
183
166
|
}
|