nish 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.
- package/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/quic-datagram.ts
CHANGED
|
@@ -29,6 +29,30 @@
|
|
|
29
29
|
import { quicPutDatagram } from "nish/net/quic-frame"
|
|
30
30
|
import { quicPacketCopy } from "nish/net/quic-packet"
|
|
31
31
|
|
|
32
|
+
/** `n` zeroed bytes, allocated at once when `n` fits a u16 and grown otherwise. */
|
|
33
|
+
const quicDatagramBytes = (n: i32): u8[] => {
|
|
34
|
+
if (n >= 0 && n <= 65535) {
|
|
35
|
+
return new Array<u8>(toU16(n))
|
|
36
|
+
}
|
|
37
|
+
const out: u8[] = []
|
|
38
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
39
|
+
out.push(0)
|
|
40
|
+
}
|
|
41
|
+
return out
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `n` zeroed lengths, allocated at once when `n` fits a u16 and grown otherwise. */
|
|
45
|
+
const quicDatagramLengths = (n: i32): i32[] => {
|
|
46
|
+
if (n >= 0 && n <= 65535) {
|
|
47
|
+
return new Array<i32>(toU16(n))
|
|
48
|
+
}
|
|
49
|
+
const out: i32[] = []
|
|
50
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
51
|
+
out.push(0)
|
|
52
|
+
}
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
55
|
+
|
|
32
56
|
/** A ring of `entries` datagram payloads of at most `entrySize` bytes each, oldest first. */
|
|
33
57
|
export class QuicDatagramQueue {
|
|
34
58
|
/** Entry `k`'s bytes are `data[k * entrySize ..]`, `lengths[k]` of them. */
|
|
@@ -49,17 +73,44 @@ export class QuicDatagramQueue {
|
|
|
49
73
|
|
|
50
74
|
/**
|
|
51
75
|
* Makes an empty ring, allocating all of its storage now: `entries * entrySize` bytes of
|
|
52
|
-
* payload and a 4-byte length per entry.
|
|
76
|
+
* payload and a 4-byte length per entry. A negative size, or a product past 2^31 - 1 bytes, makes a
|
|
77
|
+
* ring of no entries, which refuses every `push`.
|
|
53
78
|
*
|
|
54
79
|
* @param entries How many datagrams the ring holds at most before `push` refuses the next.
|
|
55
80
|
* @param entrySize The largest payload, in bytes, an entry holds; a longer one is dropped, not split.
|
|
56
81
|
*/
|
|
57
82
|
constructor(entries: i32, entrySize: i32) {
|
|
58
|
-
|
|
59
|
-
|
|
83
|
+
// Two i32s, so the product fits an i64.
|
|
84
|
+
const bytes: i64 = toI64(entries) * toI64(entrySize)
|
|
85
|
+
const fits: boolean = entries >= 0 && entrySize >= 0 && bytes <= 2147483647
|
|
86
|
+
// `bytes` is at most 2^31 - 1 when it fits.
|
|
87
|
+
this.data = quicDatagramBytes(fits ? toI32(bytes) : 0)
|
|
88
|
+
this.lengths = quicDatagramLengths(fits ? entries : 0)
|
|
60
89
|
this.entrySize = entrySize
|
|
61
90
|
}
|
|
62
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Where entry `k`'s bytes start in `data`. Every entry lies inside `data`, whose length is an
|
|
94
|
+
* i32, so for an entry the offset fits; any other `k` gives an offset `quicPacketCopy` refuses.
|
|
95
|
+
*/
|
|
96
|
+
private offset(k: i32): i32 {
|
|
97
|
+
const at: i64 = toI64(k) * toI64(this.entrySize)
|
|
98
|
+
// Past 2^31 - 1 it is no entry's, and -1 is an offset every copy refuses.
|
|
99
|
+
return at < 0 || at > 2147483647 ? -1 : toI32(at)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The entry `j` places after `head`, wrapped around the ring; 0 for a ring of no entries.
|
|
104
|
+
* Unsigned, because `head` and `j` are each below 2^31, so the sum cannot wrap.
|
|
105
|
+
*/
|
|
106
|
+
private slot(j: i32): i32 {
|
|
107
|
+
const entries: u32 = toU32(this.lengths.length)
|
|
108
|
+
if (entries === 0) {
|
|
109
|
+
return 0
|
|
110
|
+
}
|
|
111
|
+
return toI32((toU32(this.head) + toU32(j)) % entries)
|
|
112
|
+
}
|
|
113
|
+
|
|
63
114
|
/** Empties the ring, for a connection slot reused for another peer. */
|
|
64
115
|
reset(): void {
|
|
65
116
|
this.head = 0
|
|
@@ -87,26 +138,24 @@ export class QuicDatagramQueue {
|
|
|
87
138
|
* @returns True when the datagram was queued; false when it was dropped instead.
|
|
88
139
|
*/
|
|
89
140
|
push(buf: u8[], from: i32, length: i32): boolean {
|
|
90
|
-
const entries: i32 = this.
|
|
141
|
+
const entries: i32 = toI32(this.lengths.length)
|
|
142
|
+
const count: i32 = this.count
|
|
91
143
|
if (
|
|
92
|
-
|
|
144
|
+
count >= entries ||
|
|
93
145
|
length < 0 ||
|
|
94
146
|
length > this.entrySize ||
|
|
95
147
|
from < 0 ||
|
|
96
148
|
from > toI32(buf.length) - length
|
|
97
149
|
) {
|
|
98
|
-
|
|
99
|
-
this.dropped = this.dropped + 1
|
|
100
|
-
}
|
|
150
|
+
this.countDropped()
|
|
101
151
|
return false
|
|
102
152
|
}
|
|
103
|
-
const k: i32 =
|
|
104
|
-
|
|
105
|
-
quicPacketCopy(this.data, base, buf, from, length)
|
|
153
|
+
const k: i32 = this.slot(count)
|
|
154
|
+
quicPacketCopy(this.data, this.offset(k), buf, from, length)
|
|
106
155
|
if (k >= 0 && k < entries) {
|
|
107
156
|
this.lengths[k] = length
|
|
108
157
|
}
|
|
109
|
-
this.count =
|
|
158
|
+
this.count = count + 1
|
|
110
159
|
return true
|
|
111
160
|
}
|
|
112
161
|
|
|
@@ -125,36 +174,50 @@ export class QuicDatagramQueue {
|
|
|
125
174
|
* that no packet can carry any more; an empty ring is left as it is.
|
|
126
175
|
*/
|
|
127
176
|
dropOldest(): void {
|
|
128
|
-
if (this.count > 0
|
|
129
|
-
this.
|
|
177
|
+
if (this.count > 0) {
|
|
178
|
+
this.countDropped()
|
|
130
179
|
}
|
|
131
180
|
this.drop()
|
|
132
181
|
}
|
|
133
182
|
|
|
183
|
+
/** Counts one datagram in `dropped`, which stops at 2^31 - 1 rather than wrap. */
|
|
184
|
+
private countDropped(): void {
|
|
185
|
+
const dropped: i32 = this.dropped
|
|
186
|
+
if (dropped < 2147483647) {
|
|
187
|
+
this.dropped = dropped + 1
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
134
191
|
/**
|
|
135
192
|
* Moves the oldest datagram behind the newest, unsent and not counted as
|
|
136
193
|
* dropped, so the ones queued after it go out first; for one too large
|
|
137
194
|
* for now that a later packet may carry. A ring of one is left as it is.
|
|
138
195
|
*/
|
|
139
196
|
deferOldest(): void {
|
|
140
|
-
const entries: i32 = this.
|
|
141
|
-
|
|
197
|
+
const entries: i32 = toI32(this.lengths.length)
|
|
198
|
+
const count: i32 = this.count
|
|
199
|
+
const head: i32 = this.head
|
|
200
|
+
// `head` is an entry whenever `count` is 1 or more; the test says so for the bounds proof.
|
|
201
|
+
if (count < 2 || head < 0 || head >= entries) {
|
|
142
202
|
return
|
|
143
203
|
}
|
|
144
|
-
if (
|
|
145
|
-
const k: i32 =
|
|
146
|
-
const length: i32 = this.lengths[
|
|
147
|
-
quicPacketCopy(this.data,
|
|
148
|
-
|
|
204
|
+
if (count < entries) {
|
|
205
|
+
const k: i32 = this.slot(count)
|
|
206
|
+
const length: i32 = this.lengths[head]
|
|
207
|
+
quicPacketCopy(this.data, this.offset(k), this.data, this.offset(head), length)
|
|
208
|
+
if (k >= 0 && k < entries) {
|
|
209
|
+
this.lengths[k] = length
|
|
210
|
+
}
|
|
149
211
|
}
|
|
150
|
-
this.head =
|
|
212
|
+
this.head = this.slot(toI32(1))
|
|
151
213
|
}
|
|
152
214
|
|
|
153
215
|
/** Forgets the oldest datagram. */
|
|
154
216
|
private drop(): void {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
this.
|
|
217
|
+
const count: i32 = this.count
|
|
218
|
+
if (count > 0) {
|
|
219
|
+
this.head = this.slot(toI32(1))
|
|
220
|
+
this.count = count - 1
|
|
158
221
|
}
|
|
159
222
|
}
|
|
160
223
|
|
|
@@ -177,7 +240,7 @@ export class QuicDatagramQueue {
|
|
|
177
240
|
if (at < 0 || cap < length || at > toI32(buf.length) - length) {
|
|
178
241
|
return -2
|
|
179
242
|
}
|
|
180
|
-
quicPacketCopy(buf, at, this.data, this.
|
|
243
|
+
quicPacketCopy(buf, at, this.data, this.offset(this.head), length)
|
|
181
244
|
this.drop()
|
|
182
245
|
return length
|
|
183
246
|
}
|
|
@@ -197,7 +260,7 @@ export class QuicDatagramQueue {
|
|
|
197
260
|
if (length < 0) {
|
|
198
261
|
return at
|
|
199
262
|
}
|
|
200
|
-
const next: i32 = quicPutDatagram(buf, at, end, this.data, this.
|
|
263
|
+
const next: i32 = quicPutDatagram(buf, at, end, this.data, this.offset(this.head), length)
|
|
201
264
|
if (next < 0) {
|
|
202
265
|
return at
|
|
203
266
|
}
|