@amritk/nish-x86_64-linux 0.41.0 → 0.44.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/bin/nish +0 -0
- package/docs/AI.md +282 -126
- package/package.json +2 -2
- package/runtime/core.d.ts +16 -0
- package/runtime/core.mjs +5 -0
- package/runtime/nish.d.ts +204 -33
- package/runtime/nish.h +19 -3
- package/runtime/nish.mjs +5 -1
- package/runtime/runtime-cross.c +15 -0
- package/runtime/runtime-net.c +876 -10
- package/runtime/runtime-region.c +21 -0
- package/runtime/runtime-sandbox.c +112 -15
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +533 -77
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +474 -5
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +125 -29
- package/std/README.md +3 -3
- package/std/crypto/base64url.ts +102 -1
- package/std/crypto/scram.ts +142 -0
- package/std/crypto/sha1.ts +99 -41
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/net/fetch.ts +9 -1
- package/std/net/handler.ts +79 -15
- package/std/net/http-server.ts +2 -3
- package/std/net/http1-server.ts +294 -51
- package/std/net/http3-server.ts +26 -15
- package/std/net/http3.ts +2 -2
- package/std/net/oidc.ts +1107 -0
- package/std/net/postgres-async.ts +64 -33
- package/std/net/postgres.ts +65 -76
- package/std/net/quic-conn-ack.ts +57 -7
- package/std/net/quic-frame.ts +104 -2
- package/std/net/quic-listener.ts +9 -2
- package/std/net/quic.ts +16 -1
- package/std/net/serve.ts +10 -8
- package/std/net/websocket.ts +188 -49
- package/std/sqlite.ts +325 -0
- package/std/text.ts +80 -11
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* PgAsyncParams, PgAsyncPool, pgAsyncClaim, pgAsyncConnect, pgAsyncQuery, pgAsyncRelease,
|
|
11
11
|
* } from "nish/net/postgres-async"
|
|
12
12
|
*
|
|
13
|
-
* async
|
|
13
|
+
* const title = async (pool: PgAsyncPool, id: i32): Promise<void> => {
|
|
14
14
|
* const ticket: i32 = await pgAsyncClaim(pool)
|
|
15
15
|
* if (ticket < 0) {
|
|
16
16
|
* return // no live connection, or none came free in time
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
* }
|
|
25
25
|
* }
|
|
26
26
|
*
|
|
27
|
-
* export async
|
|
27
|
+
* export const main = async (): Promise<i32> => {
|
|
28
28
|
* const config = new PgConfig()
|
|
29
29
|
* config.user = "app"
|
|
30
30
|
* const pool = new PgAsyncPool(4)
|
|
@@ -236,11 +236,16 @@ const PG_ASYNC_SEQ_WRAP: i32 = 16777216
|
|
|
236
236
|
const PG_ASYNC_QUEUE: i32 = 16384
|
|
237
237
|
const PG_ASYNC_QUEUE_WRAP: i32 = 1073741824
|
|
238
238
|
// The wakes the queue's tasks wait on, a task on the one its number picks, so
|
|
239
|
-
// the queue's head moving wakes it and few others.
|
|
239
|
+
// the queue's head moving wakes it and few others. A seat waiting for its
|
|
240
|
+
// turn waits the same way on a turn lane, the one its seat index picks.
|
|
240
241
|
const PG_ASYNC_LANES: i32 = 128
|
|
242
|
+
// The wake the tasks waiting for a slot, or for a seat outside the queue,
|
|
243
|
+
// wait on: after the turn lanes and before the queue lanes.
|
|
244
|
+
const PG_ASYNC_POOL_WAKE: i32 = PG_ASYNC_LANES
|
|
245
|
+
const PG_ASYNC_WAKES: i32 = PG_ASYNC_LANES * 2 + 1
|
|
241
246
|
// What a query reads first; it grows by doubling up to one `maxMessage`.
|
|
242
247
|
const PG_ASYNC_INPUT_SIZE: i32 = 8192
|
|
243
|
-
// Loop turns a seat yields for its turn before it parks on its
|
|
248
|
+
// Loop turns a seat yields for its turn before it parks on its turn lane.
|
|
244
249
|
const PG_ASYNC_TURN_YIELDS: i32 = 4
|
|
245
250
|
// ReadyForQuery's transaction status for "idle", outside any transaction.
|
|
246
251
|
const PG_ASYNC_IDLE: i32 = 73
|
|
@@ -380,7 +385,7 @@ export class PgAsyncPool {
|
|
|
380
385
|
/** Per slot: 1 while a task reads past an abandoned seat's answer. */
|
|
381
386
|
readonly skipping: u8[]
|
|
382
387
|
/**
|
|
383
|
-
* Per
|
|
388
|
+
* Per turn lane, then one for the pool, then one per queue lane: a socket pair
|
|
384
389
|
* whose read end a waiting task waits on, or -1. A pair is opened the first
|
|
385
390
|
* time a task waits on its wake, so a pool nobody waits on holds none.
|
|
386
391
|
*/
|
|
@@ -400,7 +405,7 @@ export class PgAsyncPool {
|
|
|
400
405
|
|
|
401
406
|
/**
|
|
402
407
|
* A pool of `size` slots, none connected yet. A waiting task waits on a
|
|
403
|
-
* wake: one per
|
|
408
|
+
* wake: one per turn lane, one for the pool and one per queue lane. Each opens
|
|
404
409
|
* its socket pair the first time a task waits on it; `close()` closes the
|
|
405
410
|
* pairs opened.
|
|
406
411
|
* @param size The number of connections, 1 to `PG_ASYNC_MAX_SIZE`; any
|
|
@@ -434,7 +439,7 @@ export class PgAsyncPool {
|
|
|
434
439
|
this.seatClaims = new Array<i32>(size * PG_ASYNC_MAX_DEPTH)
|
|
435
440
|
this.seatAbandoned = new Array<u8>(size * PG_ASYNC_MAX_DEPTH)
|
|
436
441
|
this.skipping = new Array<u8>(size)
|
|
437
|
-
const wakes: i32 =
|
|
442
|
+
const wakes: i32 = PG_ASYNC_WAKES
|
|
438
443
|
this.wakes = new Array<i32>(wakes * 2)
|
|
439
444
|
this.wakePending = new Array<u8>(wakes)
|
|
440
445
|
this.wakeBytes = new Array<u8>(8)
|
|
@@ -852,7 +857,7 @@ const pgAsyncSince = (began: i64): i32 => {
|
|
|
852
857
|
}
|
|
853
858
|
|
|
854
859
|
/**
|
|
855
|
-
* Wakes the tasks waiting on wake `w` (a
|
|
860
|
+
* Wakes the tasks waiting on wake `w` (a turn lane, the pool's or a queue lane):
|
|
856
861
|
* one byte on its socket pair, unless one is already there or none waits.
|
|
857
862
|
* The byte stays until every task then waiting has woken: the runtime reports
|
|
858
863
|
* at most 64 ready sockets a turn, and a byte read back by the first of
|
|
@@ -905,7 +910,7 @@ const pgAsyncFreeSeats = (pool: PgAsyncPool): i32 => {
|
|
|
905
910
|
* seats freed together are taken in one loop turn and not one a turn.
|
|
906
911
|
*/
|
|
907
912
|
const pgAsyncWakePool = (pool: PgAsyncPool): void => {
|
|
908
|
-
pgAsyncSignal(pool,
|
|
913
|
+
pgAsyncSignal(pool, PG_ASYNC_POOL_WAKE)
|
|
909
914
|
const queued: i32 = (pool.seatTail - pool.seatHead + PG_ASYNC_QUEUE_WRAP) % PG_ASYNC_QUEUE_WRAP
|
|
910
915
|
if (queued === 0) {
|
|
911
916
|
return
|
|
@@ -915,7 +920,7 @@ const pgAsyncWakePool = (pool: PgAsyncPool): void => {
|
|
|
915
920
|
wake = wake > queued ? queued : wake
|
|
916
921
|
wake = wake > PG_ASYNC_LANES ? PG_ASYNC_LANES : wake
|
|
917
922
|
for (let k: i32 = 0; k < wake; k++) {
|
|
918
|
-
pgAsyncSignal(pool,
|
|
923
|
+
pgAsyncSignal(pool, PG_ASYNC_POOL_WAKE + 1 + ((pool.seatHead + k) % PG_ASYNC_LANES))
|
|
919
924
|
}
|
|
920
925
|
}
|
|
921
926
|
|
|
@@ -1005,8 +1010,8 @@ export const pgAsyncClaim = async (pool: PgAsyncPool): Promise<i32> => {
|
|
|
1005
1010
|
}
|
|
1006
1011
|
pool.waits += 1
|
|
1007
1012
|
waited = true
|
|
1008
|
-
await pgAsyncPark(pool,
|
|
1009
|
-
pgAsyncDrain(pool,
|
|
1013
|
+
await pgAsyncPark(pool, PG_ASYNC_POOL_WAKE, left)
|
|
1014
|
+
pgAsyncDrain(pool, PG_ASYNC_POOL_WAKE)
|
|
1010
1015
|
}
|
|
1011
1016
|
}
|
|
1012
1017
|
|
|
@@ -1182,7 +1187,7 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
|
|
|
1182
1187
|
if (skipped) {
|
|
1183
1188
|
continue
|
|
1184
1189
|
}
|
|
1185
|
-
const w: i32 = queued >= 0 ?
|
|
1190
|
+
const w: i32 = queued >= 0 ? PG_ASYNC_POOL_WAKE + 1 + (queued % PG_ASYNC_LANES) : PG_ASYNC_POOL_WAKE
|
|
1186
1191
|
pool.waits += 1
|
|
1187
1192
|
await pgAsyncPark(pool, w, left)
|
|
1188
1193
|
pgAsyncDrain(pool, w)
|
|
@@ -1265,6 +1270,9 @@ const pgAsyncLostSeat = (pool: PgAsyncPool, seat: i32): boolean => {
|
|
|
1265
1270
|
return at >= 0 && pgAsyncAhead(pool, s, pool.seatSeq[seat]) >= pgAsyncAhead(pool, s, at)
|
|
1266
1271
|
}
|
|
1267
1272
|
|
|
1273
|
+
/** The turn lane seat `seat` waits on: distinct for the seats of any eight slots in a row. */
|
|
1274
|
+
const pgAsyncTurnLane = (seat: i32): i32 => seat % PG_ASYNC_LANES
|
|
1275
|
+
|
|
1268
1276
|
/**
|
|
1269
1277
|
* Whether a task waiting on slot `s`, other than `except`'s, may go on: its
|
|
1270
1278
|
* turn came, its answer is lost, or an abandoned answer ahead of it waits to
|
|
@@ -1285,6 +1293,41 @@ const pgAsyncReady = (pool: PgAsyncPool, s: i32, except: i32): boolean => {
|
|
|
1285
1293
|
return waiting && pgAsyncSkippable(pool, s) >= 0
|
|
1286
1294
|
}
|
|
1287
1295
|
|
|
1296
|
+
/**
|
|
1297
|
+
* Wakes the seats waiting for their turn on slot `s`, other than `except`'s,
|
|
1298
|
+
* when one of them may go on: each on its turn lane, in the order their
|
|
1299
|
+
* answers come. A seat whose answer is read wakes the next within the same
|
|
1300
|
+
* loop turn only when the loop runs them in that order. The runtime promises
|
|
1301
|
+
* only the kernel's order: Linux epoll hands back distinct descriptors in the
|
|
1302
|
+
* order they became ready, so lanes rung in order run in order there, while
|
|
1303
|
+
* kqueue specifies no order and a seat run early re-parks and costs a turn.
|
|
1304
|
+
* One wake shared by them all ran them in the order they last waited, often
|
|
1305
|
+
* the reverse, so seats freed together were freed one a turn.
|
|
1306
|
+
*/
|
|
1307
|
+
const pgAsyncWakeTurns = (pool: PgAsyncPool, s: i32, except: i32): void => {
|
|
1308
|
+
if (!pgAsyncReady(pool, s, except)) {
|
|
1309
|
+
return
|
|
1310
|
+
}
|
|
1311
|
+
let after: i32 = -1
|
|
1312
|
+
for (let n: i32 = 0; n < PG_ASYNC_MAX_DEPTH; n++) {
|
|
1313
|
+
let next: i32 = -1
|
|
1314
|
+
let nextAhead: i32 = PG_ASYNC_SEQ_WRAP
|
|
1315
|
+
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
1316
|
+
const seat: i32 = s * PG_ASYNC_MAX_DEPTH + k
|
|
1317
|
+
const ahead: i32 = pgAsyncAhead(pool, s, pool.seatSeq[seat])
|
|
1318
|
+
if (seat !== except && toI32(pool.seatWaiting[seat]) === 1 && ahead > after && ahead < nextAhead) {
|
|
1319
|
+
next = seat
|
|
1320
|
+
nextAhead = ahead
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
if (next < 0) {
|
|
1324
|
+
return
|
|
1325
|
+
}
|
|
1326
|
+
pgAsyncSignal(pool, pgAsyncTurnLane(next))
|
|
1327
|
+
after = nextAhead
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1288
1331
|
/**
|
|
1289
1332
|
* Marks slot `s` lost from sequence number `seq` on, settles the abandoned
|
|
1290
1333
|
* seats it loses, and wakes those it fails.
|
|
@@ -1308,9 +1351,7 @@ const pgAsyncDoom = (pool: PgAsyncPool, s: i32, seq: i32): void => {
|
|
|
1308
1351
|
pgAsyncSettle(pool, seat)
|
|
1309
1352
|
}
|
|
1310
1353
|
}
|
|
1311
|
-
|
|
1312
|
-
pgAsyncSignal(pool, s)
|
|
1313
|
-
}
|
|
1354
|
+
pgAsyncWakeTurns(pool, s, -1)
|
|
1314
1355
|
}
|
|
1315
1356
|
|
|
1316
1357
|
/**
|
|
@@ -1321,9 +1362,7 @@ const pgAsyncDoom = (pool: PgAsyncPool, s: i32, seq: i32): void => {
|
|
|
1321
1362
|
const pgAsyncAbandon = (pool: PgAsyncPool, seat: i32): void => {
|
|
1322
1363
|
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
1323
1364
|
pool.seatAbandoned[seat] = 1
|
|
1324
|
-
|
|
1325
|
-
pgAsyncSignal(pool, s)
|
|
1326
|
-
}
|
|
1365
|
+
pgAsyncWakeTurns(pool, s, seat)
|
|
1327
1366
|
pgAsyncWakePool(pool)
|
|
1328
1367
|
}
|
|
1329
1368
|
|
|
@@ -1394,9 +1433,7 @@ const pgAsyncSkip = async (pool: PgAsyncPool, s: i32, seat: i32): Promise<void>
|
|
|
1394
1433
|
if (!waited && cancelled()) {
|
|
1395
1434
|
// Another task reads on from where this one stopped.
|
|
1396
1435
|
pool.skipping[s] = 0
|
|
1397
|
-
|
|
1398
|
-
pgAsyncSignal(pool, s)
|
|
1399
|
-
}
|
|
1436
|
+
pgAsyncWakeTurns(pool, s, -1)
|
|
1400
1437
|
pgAsyncWakePool(pool)
|
|
1401
1438
|
return
|
|
1402
1439
|
}
|
|
@@ -1426,9 +1463,7 @@ const pgAsyncSkip = async (pool: PgAsyncPool, s: i32, seat: i32): Promise<void>
|
|
|
1426
1463
|
pool.prepared[s * PG_ASYNC_MAX_STATEMENTS + pool.seatStatement[seat]] = 0
|
|
1427
1464
|
}
|
|
1428
1465
|
pgAsyncSettle(pool, seat)
|
|
1429
|
-
|
|
1430
|
-
pgAsyncSignal(pool, s)
|
|
1431
|
-
}
|
|
1466
|
+
pgAsyncWakeTurns(pool, s, -1)
|
|
1432
1467
|
pgAsyncWakePool(pool)
|
|
1433
1468
|
}
|
|
1434
1469
|
|
|
@@ -1593,16 +1628,14 @@ export const pgAsyncTurn = async (pool: PgAsyncPool, ticket: i32, sent: i32): Pr
|
|
|
1593
1628
|
await sleep(0)
|
|
1594
1629
|
} else {
|
|
1595
1630
|
pool.turnWaits += 1
|
|
1596
|
-
await pgAsyncPark(pool,
|
|
1631
|
+
await pgAsyncPark(pool, pgAsyncTurnLane(seat), -1)
|
|
1597
1632
|
// The last of the waiters the byte woke reads it, as in the claim and
|
|
1598
1633
|
// seat loops: a seat that waits again must not park on a byte nobody reads.
|
|
1599
|
-
pgAsyncDrain(pool,
|
|
1634
|
+
pgAsyncDrain(pool, pgAsyncTurnLane(seat))
|
|
1600
1635
|
}
|
|
1601
1636
|
}
|
|
1602
1637
|
pool.seatWaiting[seat] = 0
|
|
1603
|
-
|
|
1604
|
-
pgAsyncDrain(pool, s)
|
|
1605
|
-
}
|
|
1638
|
+
pgAsyncDrain(pool, pgAsyncTurnLane(seat))
|
|
1606
1639
|
if (pgAsyncLostSeat(pool, seat)) {
|
|
1607
1640
|
return PG_ASYNC_BROKEN + (cancelled() ? PG_ASYNC_CANCELLED : 0)
|
|
1608
1641
|
}
|
|
@@ -1759,9 +1792,7 @@ export const pgAsyncDone = (pool: PgAsyncPool, ticket: i32, turn: i32, reusable:
|
|
|
1759
1792
|
// session they did not ask for.
|
|
1760
1793
|
pgAsyncDoom(pool, s, pool.served[s])
|
|
1761
1794
|
}
|
|
1762
|
-
|
|
1763
|
-
pgAsyncSignal(pool, s)
|
|
1764
|
-
}
|
|
1795
|
+
pgAsyncWakeTurns(pool, s, seat)
|
|
1765
1796
|
}
|
|
1766
1797
|
pgAsyncFree(pool, seat)
|
|
1767
1798
|
}
|
package/std/net/postgres.ts
CHANGED
|
@@ -116,8 +116,8 @@
|
|
|
116
116
|
* `int4[]` or `int8[]` column in its binary layout, and refuses any other array
|
|
117
117
|
* (a `text[]` or a multi-dimensional one among them).
|
|
118
118
|
*
|
|
119
|
-
* **Authentication.** SCRAM-SHA-256 (RFC 5802, RFC 7677) or none (`trust`)
|
|
120
|
-
* password arrives as a `Secret<u8[]>`; the connection keeps a copy in a holder
|
|
119
|
+
* **Authentication.** SCRAM-SHA-256 (RFC 5802, RFC 7677) or none (`trust`), the
|
|
120
|
+
* key derivation and proofs from `nish/crypto/scram`. The password arrives as a `Secret<u8[]>`; the connection keeps a copy in a holder
|
|
121
121
|
* that is wiped as soon as the proof is computed, or when the connection fails
|
|
122
122
|
* or is disposed. The server's signature is checked in constant time before
|
|
123
123
|
* `AuthenticationOk` is believed, and an `AuthenticationOk` that skips the end
|
|
@@ -203,9 +203,12 @@
|
|
|
203
203
|
import { copyBytes, getF64BE, getI32BE, getI64BE, getU16BE, setI32BE, setI64BE, setU16BE } from "nish/bytes"
|
|
204
204
|
import { base64StdPadDecode, base64StdPadEncode } from "nish/crypto/base64url"
|
|
205
205
|
import { timingSafeEqual } from "nish/crypto/ct"
|
|
206
|
-
import {
|
|
207
|
-
|
|
208
|
-
|
|
206
|
+
import {
|
|
207
|
+
scramSha256ClientProof,
|
|
208
|
+
scramSha256SaltedPassword,
|
|
209
|
+
scramSha256ServerSignature,
|
|
210
|
+
} from "nish/crypto/scram"
|
|
211
|
+
import { Secret, expose, secret, wipe } from "nish:secret"
|
|
209
212
|
import { urlDecode } from "nish/net/url"
|
|
210
213
|
|
|
211
214
|
/** `next()` needs more input before it can say anything. */
|
|
@@ -701,61 +704,6 @@ const pgBytesAre = (b: u8[], start: i32, length: i32, text: string): boolean =>
|
|
|
701
704
|
return true
|
|
702
705
|
}
|
|
703
706
|
|
|
704
|
-
/**
|
|
705
|
-
* The salted password, PBKDF2-HMAC-SHA-256 over the password and the salt
|
|
706
|
-
* (RFC 5802 §3, `Hi`), 32 bytes, or `null` when it cannot be computed.
|
|
707
|
-
*
|
|
708
|
-
* The caller has bounded `iterations` by `maxIterations`, since the time is
|
|
709
|
-
* linear in it.
|
|
710
|
-
*/
|
|
711
|
-
const pgSaltedPassword = (password: Secret<u8[]>, salt: u8[], iterations: i32): Secret<u8[]> | null =>
|
|
712
|
-
pbkdf2HmacSha256(password, salt, iterations, 32)
|
|
713
|
-
|
|
714
|
-
/**
|
|
715
|
-
* SHA-256 of `data` through a hasher of its own, whose state, block and
|
|
716
|
-
* schedule are wiped before it returns: the one-shot `sha256` leaves the last
|
|
717
|
-
* block of its input in a hasher nothing wipes, and here the input is
|
|
718
|
-
* ClientKey.
|
|
719
|
-
*/
|
|
720
|
-
const pgSha256Wiped = (data: u8[]): u8[] => {
|
|
721
|
-
const h = new Sha256()
|
|
722
|
-
h.update(data, toI32(0), toI32(data.length))
|
|
723
|
-
const digest: u8[] = h.digest()
|
|
724
|
-
wipe(h.state)
|
|
725
|
-
wipe(h.block)
|
|
726
|
-
wipe(h.schedule)
|
|
727
|
-
h.fill = 0
|
|
728
|
-
h.total = 0
|
|
729
|
-
h.finished = true
|
|
730
|
-
return digest
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
/**
|
|
734
|
-
* RFC 5802 §3 from the salted password and the AuthMessage: the ClientProof
|
|
735
|
-
* (32 bytes) followed by the ServerSignature (32 bytes). Every key in between
|
|
736
|
-
* is wiped before it returns.
|
|
737
|
-
*/
|
|
738
|
-
const pgScramKeys = (salted: u8[], authMessage: u8[]): u8[] => {
|
|
739
|
-
const clientKey: u8[] = hmacSha256(salted, pgBytesOf("Client Key"))
|
|
740
|
-
const storedKey: u8[] = pgSha256Wiped(clientKey)
|
|
741
|
-
const clientSignature: u8[] = hmacSha256(storedKey, authMessage)
|
|
742
|
-
const serverKey: u8[] = hmacSha256(salted, pgBytesOf("Server Key"))
|
|
743
|
-
const serverSignature: u8[] = hmacSha256(serverKey, authMessage)
|
|
744
|
-
const out: u8[] = new Array<u8>(64)
|
|
745
|
-
if (clientKey.length < 32 || clientSignature.length < 32 || serverSignature.length < 32) {
|
|
746
|
-
panic("nish/net/postgres: an HMAC-SHA-256 shorter than 32 bytes")
|
|
747
|
-
}
|
|
748
|
-
for (let i: i32 = 0; i < 32; i++) {
|
|
749
|
-
out[i] = clientKey[i] ^ clientSignature[i]
|
|
750
|
-
out[32 + i] = serverSignature[i]
|
|
751
|
-
}
|
|
752
|
-
wipe(clientKey)
|
|
753
|
-
wipe(storedKey)
|
|
754
|
-
wipe(clientSignature)
|
|
755
|
-
wipe(serverKey)
|
|
756
|
-
return out
|
|
757
|
-
}
|
|
758
|
-
|
|
759
707
|
/** Decoders for PostgreSQL's text format, in place; a bad input sets `failed`, which stays set. */
|
|
760
708
|
export class PgText {
|
|
761
709
|
/** Whether a decode since the last `reset()` was given something it could not read. */
|
|
@@ -790,6 +738,50 @@ export class PgText {
|
|
|
790
738
|
this.failed = true
|
|
791
739
|
return 0
|
|
792
740
|
}
|
|
741
|
+
// A column of at most eighteen bytes holds at most eighteen digits, below
|
|
742
|
+
// 10^18 and so inside i64 whatever they are, and is read without the
|
|
743
|
+
// overflow test. It is read as two runs, each below 10^9, which is how the
|
|
744
|
+
// prover sees that nothing here can wrap: `head` takes digits until it
|
|
745
|
+
// passes 10^8, nine of them at least, so `tail` takes nine at most. The
|
|
746
|
+
// range tests on `head`, `tail` and `scale` cannot fail; they state those
|
|
747
|
+
// bounds.
|
|
748
|
+
if (length <= 18) {
|
|
749
|
+
let head: i64 = 0
|
|
750
|
+
while (i < end && head < 100000000) {
|
|
751
|
+
const d: i32 = toI32(b[i]) - 48
|
|
752
|
+
if (d < 0 || d > 9 || head < 0) {
|
|
753
|
+
this.failed = true
|
|
754
|
+
return 0
|
|
755
|
+
}
|
|
756
|
+
head = head * 10 + toI64(d)
|
|
757
|
+
i++
|
|
758
|
+
}
|
|
759
|
+
let tail: i64 = 0
|
|
760
|
+
let scale: i64 = 1
|
|
761
|
+
while (i < end) {
|
|
762
|
+
const d: i32 = toI32(b[i]) - 48
|
|
763
|
+
if (d < 0 || d > 9 || tail < 0 || tail >= 100000000 || scale < 1 || scale > 100000000) {
|
|
764
|
+
this.failed = true
|
|
765
|
+
return 0
|
|
766
|
+
}
|
|
767
|
+
tail = tail * 10 + toI64(d)
|
|
768
|
+
scale = scale * 10
|
|
769
|
+
i++
|
|
770
|
+
}
|
|
771
|
+
if (
|
|
772
|
+
head < 0 ||
|
|
773
|
+
head >= 1000000000 ||
|
|
774
|
+
tail < 0 ||
|
|
775
|
+
tail >= 1000000000 ||
|
|
776
|
+
scale < 1 ||
|
|
777
|
+
scale > 1000000000
|
|
778
|
+
) {
|
|
779
|
+
this.failed = true
|
|
780
|
+
return 0
|
|
781
|
+
}
|
|
782
|
+
const v: i64 = head * scale + tail
|
|
783
|
+
return negative ? -v : v
|
|
784
|
+
}
|
|
793
785
|
// Accumulated as a negative number, so that the most negative i64 fits.
|
|
794
786
|
const lastDigit: i64 = negative ? 8 : 7
|
|
795
787
|
// The most negative i64 divided by ten, which a literal cannot spell exactly.
|
|
@@ -2228,7 +2220,7 @@ export class PgConnection {
|
|
|
2228
2220
|
if (password.gone) {
|
|
2229
2221
|
return this.fail(PG_ERR_SCRAM)
|
|
2230
2222
|
}
|
|
2231
|
-
const salted: Secret<u8[]> | null =
|
|
2223
|
+
const salted: Secret<u8[]> | null = scramSha256SaltedPassword(password.bytes, salt, iterations)
|
|
2232
2224
|
if (salted === null) {
|
|
2233
2225
|
return this.fail(PG_ERR_SCRAM)
|
|
2234
2226
|
}
|
|
@@ -2236,20 +2228,10 @@ export class PgConnection {
|
|
|
2236
2228
|
const authMessage: u8[] = pgBytesOf(
|
|
2237
2229
|
`${this.firstBare()},${String.fromBytes(b, body, end)},${withoutProof}`
|
|
2238
2230
|
)
|
|
2239
|
-
const
|
|
2231
|
+
const proof: u8[] = scramSha256ClientProof(salted, authMessage)
|
|
2232
|
+
this.serverSignature = scramSha256ServerSignature(salted, authMessage)
|
|
2240
2233
|
wipe(salted)
|
|
2241
2234
|
this.dispose()
|
|
2242
|
-
const proof: u8[] = new Array<u8>(32)
|
|
2243
|
-
const signature: u8[] = new Array<u8>(32)
|
|
2244
|
-
if (toI32(keys.length) < 64) {
|
|
2245
|
-
panic("nish/net/postgres: the SCRAM keys are not 64 bytes")
|
|
2246
|
-
}
|
|
2247
|
-
for (let i: i32 = 0; i < 32; i++) {
|
|
2248
|
-
proof[i] = keys[i]
|
|
2249
|
-
signature[i] = keys[32 + i]
|
|
2250
|
-
}
|
|
2251
|
-
wipe(keys)
|
|
2252
|
-
this.serverSignature = signature
|
|
2253
2235
|
// The proof goes on the wire as it is, so its base64 string is no secret.
|
|
2254
2236
|
const encoded: string = base64StdPadEncode(proof)
|
|
2255
2237
|
wipe(proof)
|
|
@@ -2328,10 +2310,17 @@ export class PgConnection {
|
|
|
2328
2310
|
|
|
2329
2311
|
/** Makes the output buffer at least `want` bytes long. */
|
|
2330
2312
|
private reserve(want: i32): void {
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
return
|
|
2313
|
+
if (want > toI32(this.out.length)) {
|
|
2314
|
+
this.grow(want)
|
|
2334
2315
|
}
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
/**
|
|
2319
|
+
* `reserve` when the buffer is short: kept apart so that the test above,
|
|
2320
|
+
* which every put makes, is small enough to inline into it.
|
|
2321
|
+
*/
|
|
2322
|
+
private grow(want: i32): void {
|
|
2323
|
+
const size: i32 = toI32(this.out.length)
|
|
2335
2324
|
const grown: u8[] = new Array<u8>(size <= PG_MAX_CAP && want < 2 * size ? 2 * size : want)
|
|
2336
2325
|
grown.set(this.out, 0)
|
|
2337
2326
|
this.out = grown
|
package/std/net/quic-conn-ack.ts
CHANGED
|
@@ -23,15 +23,20 @@
|
|
|
23
23
|
*
|
|
24
24
|
* **When to acknowledge.** That is the connection's choice; this module records
|
|
25
25
|
* whether something ack-eliciting arrived since the last ACK it wrote
|
|
26
|
-
* (`ackPending`). ACK frames carry an ACK Delay the caller supplies
|
|
27
|
-
* ECN counts.
|
|
26
|
+
* (`ackPending`). ACK frames carry an ACK Delay the caller supplies.
|
|
28
27
|
*
|
|
29
|
-
* **
|
|
28
|
+
* **ECN counts.** `countEcn` adds a packet's ECN codepoint to the space's
|
|
29
|
+
* ECT(0), ECT(1) or ECN-CE count (RFC 9000 §13.4.1); the caller counts a
|
|
30
|
+
* packet only once `record` has accepted it, so a duplicate is not counted.
|
|
31
|
+
* Once any count is above zero every ACK written is an ACK_ECN (type 0x03)
|
|
32
|
+
* carrying all three, and before that a plain ACK (type 0x02).
|
|
33
|
+
*
|
|
34
|
+
* **Provenance.** Written from RFC 9000 §12.3, §13.2, §13.4.1 and §19.3, not ported from another
|
|
30
35
|
* implementation. Private names carry the `quicAck` prefix
|
|
31
36
|
* (`docs/wp26-stdlib.md` §3e).
|
|
32
37
|
*/
|
|
33
38
|
import { QUIC_MAX_VARINT } from "nish/net/quic-packet"
|
|
34
|
-
import { quicPushAck, quicPutAck } from "nish/net/quic-frame"
|
|
39
|
+
import { quicPushAck, quicPushAckEcn, quicPutAck, quicPutAckEcn } from "nish/net/quic-frame"
|
|
35
40
|
|
|
36
41
|
/** The most disjoint ranges a `QuicAckRanges` keeps, and so the most an ACK frame it writes lists. */
|
|
37
42
|
export const QUIC_ACK_MAX_RANGES: i32 = 32
|
|
@@ -51,6 +56,12 @@ export class QuicAckRanges {
|
|
|
51
56
|
count: i32 = 0
|
|
52
57
|
/** Whether an ack-eliciting packet arrived since the last `pushAck`. */
|
|
53
58
|
ackPending: boolean = false
|
|
59
|
+
/** Packets of this space that arrived marked ECT(0) (codepoint 2). */
|
|
60
|
+
ect0: i64 = 0
|
|
61
|
+
/** Packets of this space that arrived marked ECT(1) (codepoint 1). */
|
|
62
|
+
ect1: i64 = 0
|
|
63
|
+
/** Packets of this space that arrived marked ECN-CE (codepoint 3). */
|
|
64
|
+
ce: i64 = 0
|
|
54
65
|
|
|
55
66
|
/**
|
|
56
67
|
* Makes an empty set, nothing received yet, with room for
|
|
@@ -173,6 +184,33 @@ export class QuicAckRanges {
|
|
|
173
184
|
return true
|
|
174
185
|
}
|
|
175
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Counts one packet's ECN codepoint, the low two bits of the IP header's
|
|
189
|
+
* traffic class, in this space (RFC 9000 §13.4.1). The caller counts a
|
|
190
|
+
* packet once, after `record` accepted it; Not-ECT (0) counts nothing.
|
|
191
|
+
*
|
|
192
|
+
* @param codepoint The datagram's ECN bits: 0 Not-ECT, 1 ECT(1), 2 ECT(0), 3 CE; higher bits are ignored.
|
|
193
|
+
*/
|
|
194
|
+
countEcn(codepoint: i32): void {
|
|
195
|
+
const bits: i32 = codepoint & 3
|
|
196
|
+
if (bits === 2) {
|
|
197
|
+
this.ect0 = this.ect0 + 1
|
|
198
|
+
} else if (bits === 1) {
|
|
199
|
+
this.ect1 = this.ect1 + 1
|
|
200
|
+
} else if (bits === 3) {
|
|
201
|
+
this.ce = this.ce + 1
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Whether an ACK written now carries ECN counts.
|
|
207
|
+
*
|
|
208
|
+
* @returns True once any of `ect0`, `ect1` and `ce` is above zero.
|
|
209
|
+
*/
|
|
210
|
+
hasEcn(): boolean {
|
|
211
|
+
return this.ect0 > 0 || this.ect1 > 0 || this.ce > 0
|
|
212
|
+
}
|
|
213
|
+
|
|
176
214
|
/** Removes range `k`, moving the lower ones up. */
|
|
177
215
|
private remove(k: i32): void {
|
|
178
216
|
for (let j: i32 = k; j + 1 < this.count; j += 1) {
|
|
@@ -205,6 +243,7 @@ export class QuicAckRanges {
|
|
|
205
243
|
/**
|
|
206
244
|
* Appends an ACK frame for every range kept, with `ackDelay` (already
|
|
207
245
|
* scaled by this endpoint's ACK delay exponent), and clears `ackPending`.
|
|
246
|
+
* The frame is an ACK_ECN carrying the three counts once `hasEcn` holds.
|
|
208
247
|
* Answers `false`, appending nothing, when nothing has been received.
|
|
209
248
|
*
|
|
210
249
|
* @param out The packet payload being built; the frame is appended to its end.
|
|
@@ -213,7 +252,13 @@ export class QuicAckRanges {
|
|
|
213
252
|
* @returns True when the frame was appended; false, leaving `out` and `ackPending` alone, otherwise.
|
|
214
253
|
*/
|
|
215
254
|
pushAck(out: u8[], ackDelay: i64): boolean {
|
|
216
|
-
if (this.count === 0
|
|
255
|
+
if (this.count === 0) {
|
|
256
|
+
return false
|
|
257
|
+
}
|
|
258
|
+
const pushed: boolean = this.hasEcn()
|
|
259
|
+
? quicPushAckEcn(out, this.ranges, this.count, ackDelay, this.ect0, this.ect1, this.ce)
|
|
260
|
+
: quicPushAck(out, this.ranges, this.count, ackDelay)
|
|
261
|
+
if (!pushed) {
|
|
217
262
|
return false
|
|
218
263
|
}
|
|
219
264
|
this.ackPending = false
|
|
@@ -237,18 +282,23 @@ export class QuicAckRanges {
|
|
|
237
282
|
if (this.count === 0) {
|
|
238
283
|
return -1
|
|
239
284
|
}
|
|
240
|
-
const next: i32 =
|
|
285
|
+
const next: i32 = this.hasEcn()
|
|
286
|
+
? quicPutAckEcn(buf, at, end, this.ranges, this.count, ackDelay, this.ect0, this.ect1, this.ce)
|
|
287
|
+
: quicPutAck(buf, at, end, this.ranges, this.count, ackDelay)
|
|
241
288
|
if (next >= 0) {
|
|
242
289
|
this.ackPending = false
|
|
243
290
|
}
|
|
244
291
|
return next
|
|
245
292
|
}
|
|
246
293
|
|
|
247
|
-
/** Forgets every packet number, for a connection slot reused for another peer. */
|
|
294
|
+
/** Forgets every packet number and ECN count, for a connection slot reused for another peer. */
|
|
248
295
|
clear(): void {
|
|
249
296
|
this.largest = -1
|
|
250
297
|
this.floor = -1
|
|
251
298
|
this.count = 0
|
|
252
299
|
this.ackPending = false
|
|
300
|
+
this.ect0 = 0
|
|
301
|
+
this.ect1 = 0
|
|
302
|
+
this.ce = 0
|
|
253
303
|
}
|
|
254
304
|
}
|
package/std/net/quic-frame.ts
CHANGED
|
@@ -836,8 +836,8 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
|
|
|
836
836
|
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
837
837
|
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
838
838
|
* @param ackDelay The ACK Delay field, already scaled: microseconds shifted right by the exponent.
|
|
839
|
-
* @returns The offset past the frame, or -1 with nothing written. The frame is
|
|
840
|
-
* counts.
|
|
839
|
+
* @returns The offset past the frame, or -1 with nothing written. The frame is type 0x02, without ECN
|
|
840
|
+
* counts; `quicPutAckEcn` writes the 0x03 frame that carries them.
|
|
841
841
|
*/
|
|
842
842
|
export const quicPutAck = (
|
|
843
843
|
buf: u8[],
|
|
@@ -890,6 +890,108 @@ export const quicPushAck = (out: u8[], ranges: i64[], rangeCount: i32, ackDelay:
|
|
|
890
890
|
return quicPutAck(out, at, at + size, ranges, rangeCount, ackDelay) >= 0
|
|
891
891
|
}
|
|
892
892
|
|
|
893
|
+
/**
|
|
894
|
+
* The size of the ACK_ECN frame `quicPutAckEcn` writes: the ACK frame
|
|
895
|
+
* `quicAckSize` sizes, then the three ECN counts. Answers 0 where
|
|
896
|
+
* `quicAckSize` does, or when a count is negative or above 2^62-1.
|
|
897
|
+
*
|
|
898
|
+
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
899
|
+
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
900
|
+
* @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
|
|
901
|
+
* @param ect0 The ECT(0) count: packets received in this space marked ECT(0).
|
|
902
|
+
* @param ect1 The ECT(1) count: packets received in this space marked ECT(1).
|
|
903
|
+
* @param ce The ECN-CE count: packets received in this space marked CE.
|
|
904
|
+
* @returns The frame's size in bytes, or 0 when `quicPutAckEcn` would refuse these arguments.
|
|
905
|
+
*/
|
|
906
|
+
export const quicAckEcnSize = (
|
|
907
|
+
ranges: i64[],
|
|
908
|
+
rangeCount: i32,
|
|
909
|
+
ackDelay: i64,
|
|
910
|
+
ect0: i64,
|
|
911
|
+
ect1: i64,
|
|
912
|
+
ce: i64
|
|
913
|
+
): i32 => {
|
|
914
|
+
const base: i32 = quicAckSize(ranges, rangeCount, ackDelay)
|
|
915
|
+
const a: i32 = quicVarintSize(ect0)
|
|
916
|
+
const b: i32 = quicVarintSize(ect1)
|
|
917
|
+
const c: i32 = quicVarintSize(ce)
|
|
918
|
+
if (base === 0 || a === 0 || b === 0 || c === 0) {
|
|
919
|
+
return 0
|
|
920
|
+
}
|
|
921
|
+
return base + a + b + c
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Writes an ACK_ECN frame (type 0x03, RFC 9000 §19.3.2): the ranges and
|
|
926
|
+
* ACK Delay `quicPutAck` writes, then the ECT(0), ECT(1) and ECN-CE counts a
|
|
927
|
+
* receiver keeps for the space (§13.4.1). Answers -1, writing nothing, where
|
|
928
|
+
* `quicAckEcnSize` answers 0 or the frame does not fit.
|
|
929
|
+
*
|
|
930
|
+
* @param buf The buffer to write into.
|
|
931
|
+
* @param at The offset of the frame's first byte.
|
|
932
|
+
* @param end One past the last byte the frame may use.
|
|
933
|
+
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
934
|
+
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
935
|
+
* @param ackDelay The ACK Delay field, already scaled: microseconds shifted right by the exponent.
|
|
936
|
+
* @param ect0 The ECT(0) count.
|
|
937
|
+
* @param ect1 The ECT(1) count.
|
|
938
|
+
* @param ce The ECN-CE count.
|
|
939
|
+
* @returns The offset past the frame, or -1 with nothing written.
|
|
940
|
+
*/
|
|
941
|
+
export const quicPutAckEcn = (
|
|
942
|
+
buf: u8[],
|
|
943
|
+
at: i32,
|
|
944
|
+
end: i32,
|
|
945
|
+
ranges: i64[],
|
|
946
|
+
rangeCount: i32,
|
|
947
|
+
ackDelay: i64,
|
|
948
|
+
ect0: i64,
|
|
949
|
+
ect1: i64,
|
|
950
|
+
ce: i64
|
|
951
|
+
): i32 => {
|
|
952
|
+
const size: i32 = quicAckEcnSize(ranges, rangeCount, ackDelay, ect0, ect1, ce)
|
|
953
|
+
if (!quicFrameRoom(buf, at, end, size)) {
|
|
954
|
+
return -1
|
|
955
|
+
}
|
|
956
|
+
let cursor: i32 = quicPutAck(buf, at, end, ranges, rangeCount, ackDelay)
|
|
957
|
+
if (cursor < 0) {
|
|
958
|
+
return -1
|
|
959
|
+
}
|
|
960
|
+
quicFramePutByte(buf, at, QUIC_FRAME_ACK_ECN)
|
|
961
|
+
cursor = quicFramePutVarint(buf, cursor, ect0)
|
|
962
|
+
cursor = quicFramePutVarint(buf, cursor, ect1)
|
|
963
|
+
return quicFramePutVarint(buf, cursor, ce)
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* Appends the ACK_ECN frame `quicPutAckEcn` writes. Answers `false`, appending nothing, where it refuses.
|
|
968
|
+
*
|
|
969
|
+
* @param out The bytes to append the frame to.
|
|
970
|
+
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
971
|
+
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
972
|
+
* @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
|
|
973
|
+
* @param ect0 The ECT(0) count.
|
|
974
|
+
* @param ect1 The ECT(1) count.
|
|
975
|
+
* @param ce The ECN-CE count.
|
|
976
|
+
* @returns True when the frame was appended.
|
|
977
|
+
*/
|
|
978
|
+
export const quicPushAckEcn = (
|
|
979
|
+
out: u8[],
|
|
980
|
+
ranges: i64[],
|
|
981
|
+
rangeCount: i32,
|
|
982
|
+
ackDelay: i64,
|
|
983
|
+
ect0: i64,
|
|
984
|
+
ect1: i64,
|
|
985
|
+
ce: i64
|
|
986
|
+
): boolean => {
|
|
987
|
+
const size: i32 = quicAckEcnSize(ranges, rangeCount, ackDelay, ect0, ect1, ce)
|
|
988
|
+
if (size === 0) {
|
|
989
|
+
return false
|
|
990
|
+
}
|
|
991
|
+
const at: i32 = quicFrameGrow(out, size)
|
|
992
|
+
return quicPutAckEcn(out, at, at + size, ranges, rangeCount, ackDelay, ect0, ect1, ce) >= 0
|
|
993
|
+
}
|
|
994
|
+
|
|
893
995
|
/**
|
|
894
996
|
* The bytes a CRYPTO frame's header takes for `length` bytes of data at
|
|
895
997
|
* `offset`: the type, the offset and the length.
|
package/std/net/quic-listener.ts
CHANGED
|
@@ -102,6 +102,12 @@
|
|
|
102
102
|
* `takeDatagramInto` or `quicListenerTakePaced`, `flight.ecn` for a flight.
|
|
103
103
|
* One send carries one codepoint, so a flight ends before a datagram the
|
|
104
104
|
* connection would mark otherwise. The listener's own answers go unmarked.
|
|
105
|
+
* On the receiving side, a carrier whose platform reports the bits a datagram
|
|
106
|
+
* arrived with (`udpRecvFrom`'s `meta[1]`) sets `conn.arrivalEcn` to them
|
|
107
|
+
* before handing the datagram over, `quicListenerReceiveSegments` included:
|
|
108
|
+
* one receive is one codepoint for all its segments. The connection then
|
|
109
|
+
* counts them per packet number space and its ACKs carry the counts as an
|
|
110
|
+
* ACK_ECN (§13.4.1).
|
|
105
111
|
*
|
|
106
112
|
* const flight = new QuicFlight();
|
|
107
113
|
* const n: i32 = quicListenerTakeFlight(conn, now, buf, 0, toI32(buf.length), flight);
|
|
@@ -1102,8 +1108,9 @@ export const quicListenerTakeFlight = (
|
|
|
1102
1108
|
/**
|
|
1103
1109
|
* Hands `conn` the datagrams of one receive, `buf[off .. off + len)`, at
|
|
1104
1110
|
* `now`: one datagram when `segment` is 0, else a GRO receive cut every
|
|
1105
|
-
* `segment` bytes (the last may be shorter). Each is read in place
|
|
1106
|
-
* how many it handed
|
|
1111
|
+
* `segment` bytes (the last may be shorter). Each is read in place, with the
|
|
1112
|
+
* ECN bits the caller set in `conn.arrivalEcn`. Answers how many it handed
|
|
1113
|
+
* over.
|
|
1107
1114
|
*
|
|
1108
1115
|
* @param conn The connection the datagrams belong to.
|
|
1109
1116
|
* @param buf The receive buffer; read, not kept.
|