@amritk/nish-aarch64-darwin 0.34.1 → 0.35.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/std/bytes.ts CHANGED
@@ -301,7 +301,7 @@ export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
301
301
  * panics `copyBytes` before any byte is written. Natively the copy is one
302
302
  * `llvm.memmove`.
303
303
  */
304
- export const copyBytes = (dst: u8[], at: i32, src: u8[], from: i32, count: i32): void => {
304
+ export const copyBytes = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
305
305
  if (count < 0 || at < 0 || from < 0 || at > toI32(dst.length) - count || from > toI32(src.length) - count) {
306
306
  panic("copyBytes: the range lies outside an array")
307
307
  }
@@ -425,7 +425,7 @@ const skipJsonPlainWalk = (b: u8[], from: i32, to: i32): i32 => {
425
425
  }
426
426
 
427
427
  /** `copyBytes`'s walk, once its ranges are checked: forward, or backward when `dst` is `src` and the bytes move up. */
428
- const copyBytesWalk = (dst: u8[], at: i32, src: u8[], from: i32, count: i32): void => {
428
+ const copyBytesWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
429
429
  if (at <= from) {
430
430
  let k: i32 = 0
431
431
  while (k < count) {
@@ -165,21 +165,16 @@ class Http1ListTally {
165
165
  * on a kept-alive connection follow one another through the same parser.
166
166
  */
167
167
  export class Http1Parser {
168
- // The fields are declared widest first, references before numbers before
169
- // flags, so the struct carries no padding; the groups say what each is.
170
-
171
- // --- the request: valid from its `HTTP1_HEAD` until the `next()` after its `HTTP1_END`
172
- method: string = ""
173
- target: string = ""
174
- /** The field names, in lowercase, in the order they arrived; `values` beside them. */
175
- names: string[]
176
- values: string[]
177
- /** The `Upgrade` field when `Connection` asked for it, else empty. */
178
- upgrade: string = ""
179
- /** The buffer the last `HTTP1_BODY`'s bytes are in, at `bodyOff`, `bodyLen` long. */
180
- body: u8[]
181
- /** What was wrong, once `next()` said `HTTP1_ERROR`; for a log, not for the response. */
182
- reason: string = ""
168
+ // The fields a request reads come first, so that a kept-alive connection
169
+ // whose parser has gone cold between requests brings back two or three
170
+ // cache lines of it, not all four: the buffers, then the offsets `feed`,
171
+ // `buffered`, `http1TakeLine` and the head's lines move, then the flags,
172
+ // then the rest of the request's numbers and the caller's limits. What only
173
+ // `keepText`, an error or a chunked body reads follows. Each group packs as
174
+ // widest first does, and the flags' two bytes of padding are the only ones:
175
+ // the struct is the 200 bytes its fields sum to, rounded to 8.
176
+
177
+ // --- the request's buffers -------------------------------------------------
183
178
  /** The bytes fed and not yet consumed are `buf[start .. end)`. */
184
179
  buf: u8[]
185
180
  /**
@@ -191,52 +186,22 @@ export class Http1Parser {
191
186
  head: u8[]
192
187
  /** Four entries per field, into `head`: name start and end, value start and end (OWS trimmed). */
193
188
  spans: i32[]
194
- /** Reused by every list `http1Tally` walks. */
195
- tally: Http1ListTally
196
-
197
- // --- limits, the caller's --------------------------------------------------
198
- maxTarget: i32 = 0
199
- maxHeaderBytes: i32 = 0
200
- maxHeaders: i32 = 0
201
- maxBody: i32 = 0
202
- /** The most body bytes one `HTTP1_BODY` hands over; a caller's number, 2^30 unless set. */
203
- maxChunk: i32 = 1073741824
204
-
205
- /** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
206
- minor: i32 = 1
207
- /** The declared body length, or -1 when the request has none or is chunked. */
208
- contentLength: i32 = -1
209
- bodyOff: i32 = 0
210
- bodyLen: i32 = 0
211
- /** The status code to answer once `next()` said `HTTP1_ERROR`. */
212
- status: i32 = 0
213
- /** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
214
- methodStart: i32 = 0
215
- methodEnd: i32 = 0
216
- targetStart: i32 = 0
217
- targetEnd: i32 = 0
218
- /** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
219
- headerCount: i32 = 0
220
- /** How much of `head` the request uses. */
221
- headLength: i32 = 0
189
+ /** The buffer the last `HTTP1_BODY`'s bytes are in, at `bodyOff`, `bodyLen` long. */
190
+ body: u8[]
222
191
 
223
- // --- internal --------------------------------------------------------------
192
+ // --- where the parser is ----------------------------------------------------
224
193
  start: i32 = 0
225
194
  end: i32 = 0
226
195
  /** Where the search for the next LF resumes, so a line fed a byte at a time is scanned once. */
227
196
  scan: i32 = 0
228
197
  state: i32 = 0
229
- /** Bytes left in a length-delimited body or in the current chunk. */
230
- left: i32 = 0
231
- /** De-chunked body bytes so far, against `maxBody`. */
232
- bodyTotal: i32 = 0
233
- /** Bytes of the header or trailer section so far, against `maxHeaderBytes`. */
234
- sectionBytes: i32 = 0
235
- /** Field lines of the request so far, its trailers included, against `maxHeaders`. */
236
- fieldCount: i32 = 0
237
198
  /** The line `http1TakeLine` found is `buf[lineStart .. lineEnd)`, its CRLF dropped. */
238
199
  lineStart: i32 = 0
239
200
  lineEnd: i32 = 0
201
+ /** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
202
+ headerCount: i32 = 0
203
+ /** How much of `head` the request uses. */
204
+ headLength: i32 = 0
240
205
  /**
241
206
  * Whether the line being scanned holds a byte a field value may not: a
242
207
  * control byte other than HTAB, or DEL (RFC 9110 §5.5). `http1TakeLine`
@@ -260,6 +225,50 @@ export class Http1Parser {
260
225
  */
261
226
  keepText: boolean = true
262
227
 
228
+ // --- the request's numbers: valid from its `HTTP1_HEAD` until the `next()` after its `HTTP1_END`
229
+ /** Bytes of the header or trailer section so far, against `maxHeaderBytes`. */
230
+ sectionBytes: i32 = 0
231
+ /** Field lines of the request so far, its trailers included, against `maxHeaders`. */
232
+ fieldCount: i32 = 0
233
+ /** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
234
+ minor: i32 = 1
235
+ /** Bytes left in a length-delimited body or in the current chunk. */
236
+ left: i32 = 0
237
+ /** The declared body length, or -1 when the request has none or is chunked. */
238
+ contentLength: i32 = -1
239
+ bodyOff: i32 = 0
240
+ bodyLen: i32 = 0
241
+ /** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
242
+ methodStart: i32 = 0
243
+ methodEnd: i32 = 0
244
+ targetStart: i32 = 0
245
+ targetEnd: i32 = 0
246
+
247
+ // --- limits, the caller's --------------------------------------------------
248
+ maxTarget: i32 = 0
249
+ maxHeaderBytes: i32 = 0
250
+ maxHeaders: i32 = 0
251
+ maxBody: i32 = 0
252
+ /** The most body bytes one `HTTP1_BODY` hands over; a caller's number, 2^30 unless set. */
253
+ maxChunk: i32 = 1073741824
254
+
255
+ // --- what `keepText`, an error or a chunked body reads ----------------------
256
+ method: string = ""
257
+ target: string = ""
258
+ /** The field names, in lowercase, in the order they arrived; `values` beside them. */
259
+ names: string[]
260
+ values: string[]
261
+ /** The `Upgrade` field when `Connection` asked for it, else empty. */
262
+ upgrade: string = ""
263
+ /** What was wrong, once `next()` said `HTTP1_ERROR`; for a log, not for the response. */
264
+ reason: string = ""
265
+ /** Reused by every list `http1Tally` walks. */
266
+ tally: Http1ListTally
267
+ /** The status code to answer once `next()` said `HTTP1_ERROR`. */
268
+ status: i32 = 0
269
+ /** De-chunked body bytes so far, against `maxBody`. */
270
+ bodyTotal: i32 = 0
271
+
263
272
  /**
264
273
  * Each limit is clamped into 0 to 2^30, so that the sums the parser makes
265
274
  * of them (a request line is the method, the target and ten bytes more)
@@ -274,12 +283,15 @@ export class Http1Parser {
274
283
  // head kept, at HTTP1_FIRST_INPUT and HTTP1_FIRST_HEAD bytes: a parser
275
284
  // that is built and never fed (a pool's spare slot) costs neither, and a
276
285
  // caller that lends them out between requests puts empty ones here.
286
+ // The spans are made first: the arena places each block after the last,
287
+ // so their header and first entries sit just behind the parser's fields
288
+ // rather than behind four empty arrays.
289
+ this.spans = new Array<i32>(64)
277
290
  this.buf = []
278
291
  this.body = this.buf
292
+ this.head = []
279
293
  this.names = []
280
294
  this.values = []
281
- this.head = []
282
- this.spans = new Array<i32>(64)
283
295
  this.tally = new Http1ListTally()
284
296
  this.restart()
285
297
  }
@@ -1585,7 +1597,7 @@ const http1HexDigits = (len: i32): i32 => {
1585
1597
  * with `out` untouched. An empty window writes nothing and answers `at`,
1586
1598
  * because a chunk of size zero ends the body. A window outside `data` panics.
1587
1599
  */
1588
- export const http1WriteChunk = (out: u8[], at: i32, data: u8[], off: i32, len: i32): i32 => {
1600
+ export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i32, len: i32): i32 => {
1589
1601
  const size: i32 = toI32(data.length)
1590
1602
  if (off < 0 || len < 0 || off > size || len > size - off) {
1591
1603
  panic("http1WriteChunk: the window is outside the buffer")
@@ -51,7 +51,9 @@
51
51
  * connection answers `H1_NEED_MORE` until the socket has drained half the
52
52
  * output, then `H1_WRITE`. Until then it reads nothing, so the window the
53
53
  * program is writing from stays valid: back-pressure on the way out. Nothing
54
- * buffers a whole body.
54
+ * buffers a whole body. On an `Http1Server` slot `sendBody` sends a body with
55
+ * a length from the program's own array, the queued output with it, and
56
+ * copies only what the socket did not take, under the same rules.
55
57
  *
56
58
  * **What the server decides.** The framing field (`Content-Length` or
57
59
  * `Transfer-Encoding: chunked`) and the `Connection` field are the server's;
@@ -127,7 +129,7 @@ import {
127
129
  HttpCompressor,
128
130
  httpAcceptEncoding,
129
131
  } from "nish/net/http-compress"
130
- import { netClose, netMss, netRead, netWrite, tcpAccept } from "nish:net"
132
+ import { netClose, netMss, netRead, netWrite, netWrite2, tcpAccept } from "nish:net"
131
133
  import {
132
134
  HTTP1_BODY,
133
135
  HTTP1_CHUNKED,
@@ -247,7 +249,7 @@ const http1ServerCheckCap = (name: string, value: i32, low: i32, high: i32): voi
247
249
  }
248
250
 
249
251
  /** Panics unless `buf[off .. off + len)` is inside `buf`: a window past its buffer is the caller's mistake. */
250
- const http1ServerCheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
252
+ const http1ServerCheckWindow = (what: string, buf: readonly u8[], off: i32, len: i32): void => {
251
253
  const size: i32 = toI32(buf.length)
252
254
  if (off < 0 || len < 0 || off > size || len > size - off) {
253
255
  panic(`${what}: the window is outside the buffer`)
@@ -255,7 +257,7 @@ const http1ServerCheckWindow = (what: string, buf: u8[], off: i32, len: i32): vo
255
257
  }
256
258
 
257
259
  /** Copies `src[off .. off + n)` to `out` at `at`, which the caller has made sure holds it, and answers where it ends. */
258
- const http1ServerCopy = (out: u8[], at: i32, src: u8[], off: i32, n: i32): i32 => {
260
+ const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i32): i32 => {
259
261
  if (n > 0 && at >= 0 && off >= 0 && at <= toI32(out.length) - n && off <= toI32(src.length) - n) {
260
262
  // Both windows fit, as they do for every caller: one move, not a byte loop.
261
263
  copyBytes(out, at, src, off, n)
@@ -615,6 +617,14 @@ export class Http1Connection {
615
617
  mss: i32 = 0
616
618
  /** The MSS `Http1Server.accept` read from this connection's socket (`netMss`), or 0 when it reports none. */
617
619
  socketMss: i32 = 0
620
+ /**
621
+ * The plain TCP socket the carrier sends `output` on, which `sendBody`
622
+ * writes to directly: `Http1Server.accept` sets it, and `restart` and
623
+ * `abort` put it back to -1. -1 for a connection with no such socket (a
624
+ * sans-IO carrier, `Http1TlsServer`'s TLS records), where `sendBody` copies
625
+ * as `write` does.
626
+ */
627
+ fd: i32 = -1
618
628
  config: Http1Config
619
629
  /**
620
630
  * The frame decoder: made when the slot first switches to WebSocket, so a
@@ -656,8 +666,10 @@ export class Http1Connection {
656
666
  big - H1_LINE_OVERHEAD
657
667
  )
658
668
  this.config = config
659
- this.date = new HttpDate()
669
+ // The parser is made first: the arena places each block after the last,
670
+ // so its front fields sit on the lines just behind the connection's own.
660
671
  this.parser = new Http1Parser(config.maxTarget, config.maxHeaderBytes, config.maxHeaders, config.maxBody)
672
+ this.date = new HttpDate()
661
673
  this.parser.keepText = false
662
674
  this.parser.maxChunk = config.chunkSize
663
675
  this.ws = null
@@ -713,6 +725,7 @@ export class Http1Connection {
713
725
  this.headSince = 0
714
726
  this.mss = 0
715
727
  this.socketMss = 0
728
+ this.fd = -1
716
729
  this.release()
717
730
  this.park()
718
731
  }
@@ -745,16 +758,22 @@ export class Http1Connection {
745
758
  }
746
759
  }
747
760
 
748
- /** Takes back the buffers `park` gave away, before bytes are fed. */
761
+ /**
762
+ * Takes back the buffers `park` gave away, before bytes are fed. A parked
763
+ * buffer is `noOutput` itself, which a pointer compare finds without
764
+ * loading the empty array's header from a line the request touches nowhere
765
+ * else; any other empty buffer is still swapped for a lent one.
766
+ */
749
767
  unpark(): void {
750
768
  const inputs: Http1OutputPool | null = this.inputs
751
769
  const heads: Http1OutputPool | null = this.heads
752
770
  const p: Http1Parser = this.parser
753
- if (inputs !== null && toI32(p.buf.length) === 0) {
771
+ const none: u8[] = this.noOutput
772
+ if (inputs !== null && (p.buf === none || toI32(p.buf.length) === 0)) {
754
773
  p.buf = inputs.take()
755
774
  p.body = p.buf
756
775
  }
757
- if (heads !== null && toI32(p.head.length) === 0) {
776
+ if (heads !== null && (p.head === none || toI32(p.head.length) === 0)) {
758
777
  p.head = heads.take()
759
778
  }
760
779
  }
@@ -816,6 +835,7 @@ export class Http1Connection {
816
835
  this.outputEnd = 0
817
836
  this.pongPending = false
818
837
  this.writeHeld = false
838
+ this.fd = -1
819
839
  this.release()
820
840
  }
821
841
 
@@ -1213,7 +1233,7 @@ export class Http1Connection {
1213
1233
  * nothing. `H1_CLOSED` with no response under way. A window outside `buf`
1214
1234
  * panics.
1215
1235
  */
1216
- write(buf: u8[], off: i32, len: i32): i32 {
1236
+ write(buf: readonly u8[], off: i32, len: i32): i32 {
1217
1237
  http1ServerCheckWindow("Http1Connection.write", buf, off, len)
1218
1238
  if (this.response !== H1_R_BODY || this.state !== H1_S_HTTP) {
1219
1239
  return H1_CLOSED
@@ -1252,6 +1272,63 @@ export class Http1Connection {
1252
1272
  return take
1253
1273
  }
1254
1274
 
1275
+ /**
1276
+ * `write` of `buf[off .. off + len)`, sent from `buf` itself where it can
1277
+ * be: under a `Content-Length` response on a connection whose carrier
1278
+ * named its socket (`fd`, set by `Http1Server.accept`) and with no write
1279
+ * held back, the queued output and the body leave in one `netWrite2`, and
1280
+ * only what the socket did not take is copied into the output, the way
1281
+ * `write` copies it (what does not fit is answered short, and `H1_WRITE`
1282
+ * says when to send the rest). Once it returns the connection holds no
1283
+ * reference to `buf`, so the caller may reuse or give back its bytes at
1284
+ * once. A chunked or empty response, a write held back (`H1_AGAIN` or a
1285
+ * short answer not yet followed by `H1_WRITE`), and a connection with no
1286
+ * socket (`Http1TlsServer`, a sans-IO carrier) copy all of it: it is
1287
+ * `write`, with the same bytes on the wire. Answers what `write` answers,
1288
+ * `H1_INVALID` for more than the response still owes; `H1_CLOSED` also
1289
+ * when the socket failed during the send, which aborts the connection. A
1290
+ * window outside `buf` panics.
1291
+ */
1292
+ sendBody(buf: readonly u8[], off: i32, len: i32): i32 {
1293
+ http1ServerCheckWindow("Http1Connection.sendBody", buf, off, len)
1294
+ if (
1295
+ this.fd < 0 ||
1296
+ this.writeHeld ||
1297
+ this.response !== H1_R_BODY ||
1298
+ this.state !== H1_S_HTTP ||
1299
+ this.responseEmpty ||
1300
+ this.responseFraming < 0 ||
1301
+ len === 0 ||
1302
+ len > this.responseLeft
1303
+ ) {
1304
+ return this.write(buf, off, len)
1305
+ }
1306
+ const pending: i32 = this.outputEnd - this.outputStart
1307
+ const n: i32 = netWrite2(this.fd, this.output, this.outputStart, pending, buf, off, len)
1308
+ if (n < 0 && n !== H1_AGAIN) {
1309
+ this.abort()
1310
+ return H1_CLOSED
1311
+ }
1312
+ let sent: i32 = 0
1313
+ // pending + len > 0 here (len > 0 above), so netWrite2 never answers 0: it is a count, H1_AGAIN or a failure.
1314
+ if (n > 0) {
1315
+ this.lastActive = monotonicNanos()
1316
+ this.consume(n)
1317
+ sent = n > pending ? n - pending : 0
1318
+ this.responseLeft = this.responseLeft - sent
1319
+ }
1320
+ if (sent < len) {
1321
+ const copied: i32 = this.write(buf, off + sent, len - sent)
1322
+ if (copied > 0) {
1323
+ sent = sent + copied
1324
+ } else if (sent === 0) {
1325
+ return copied
1326
+ }
1327
+ }
1328
+ this.release()
1329
+ return sent
1330
+ }
1331
+
1255
1332
  /**
1256
1333
  * Ends the response: the last chunk of a chunked body. Answers 0;
1257
1334
  * `H1_INVALID` for a response with a length that still owes bytes;
@@ -1777,6 +1854,7 @@ export class Http1Server {
1777
1854
  this.fds[k] = fd
1778
1855
  this.armed[k] = H1_WANT_READ
1779
1856
  conn.restart()
1857
+ conn.fd = fd
1780
1858
  // Once a connection, not a request, so a keep-alive request costs no
1781
1859
  // syscall: a path whose MSS moves after the handshake is not followed.
1782
1860
  const mss: i32 = netMss(fd)
@@ -444,11 +444,16 @@ export class Http3Server {
444
444
  index: Http3CidIndex
445
445
  wheel: Http3Wheel
446
446
  /**
447
- * Each slot's client address, as its first datagram came from. Not
447
+ * Where each slot sends now: its client's address as the first datagram
448
+ * came from, then wherever the connection follows the client to
449
+ * (`QuicConnection.sendAddress`, copied here by `flushSlot`). Not
448
450
  * validated until `quic(slot).addressValidated` is set (RFC 9000 §8.1): a
449
451
  * Retry token the listener checked, with `retry` on, or a Handshake packet
450
452
  * from the client. Before then it is the source of one UDP datagram, which
451
453
  * anyone can forge, so a cap or an allow-list keyed on it waits for that.
454
+ * After the handshake it changes when the client's address does (a NAT
455
+ * rebinding, §9.3), first to an address not yet validated, so a cap keyed
456
+ * on the address a session started from keeps its own copy.
452
457
  */
453
458
  addresses: u8[][]
454
459
  /** What `receive` reads into, and where `flush` writes a flight. */
@@ -743,7 +748,7 @@ export class Http3Server {
743
748
  }
744
749
  const slot: i32 = this.index.find(buf, dcidAt, dcidLength)
745
750
  if (slot >= 0) {
746
- this.serve(slot, buf, at, len, now, key)
751
+ this.serve(slot, buf, at, len, address, now, key)
747
752
  return
748
753
  }
749
754
  this.unowned(buf, at, len, address, now, key)
@@ -793,7 +798,7 @@ export class Http3Server {
793
798
  this.state[slot] = H3_SERVER_BUSY
794
799
  this.signatures[slot] = H3_SERVER_NONE
795
800
  this.accepted = h3Count(this.accepted)
796
- this.serve(slot, buf, at, len, now, key)
801
+ this.serve(slot, buf, at, len, address, now, key)
797
802
  }
798
803
 
799
804
  /**
@@ -869,11 +874,16 @@ export class Http3Server {
869
874
  this.micros = micros
870
875
  }
871
876
 
872
- /** Hands `slot`'s connection a datagram, signs when the handshake asks, and refiles its IDs and timer. */
873
- serve(slot: i32, buf: u8[], at: i32, len: i32, now: i64, key: Secret<u8[]>): void {
877
+ /**
878
+ * Hands `slot`'s connection a datagram from `address`, signs when the
879
+ * handshake asks, and refiles its IDs and timer. The connection decides
880
+ * from the address whether the client moved (RFC 9000 §9.3); an address
881
+ * that is not 18 bytes long leaves it where it is.
882
+ */
883
+ serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
874
884
  const quic: QuicConnection = this.quics[slot]
875
885
  quic.recovery.micros = this.micros
876
- quic.receiveWindow(buf, at, len, now)
886
+ quic.receiveWindowFrom(buf, at, len, address, now)
877
887
  const input: u8[] | null = quic.signatureInput()
878
888
  if (input !== null) {
879
889
  const signature: u8[] | null = this.signer.sign(key, input)
@@ -989,7 +999,21 @@ export class Http3Server {
989
999
  if (this.conns[slot].isDone() && !quic.closed()) {
990
1000
  quic.close(H3_NO_ERROR)
991
1001
  }
1002
+ // A PATH_RESPONSE owed on a path the connection did not move to goes
1003
+ // there, alone (RFC 9000 §9.1); asking for it runs the connection's
1004
+ // timers first, so a validation that ran out has moved it back before
1005
+ // the flight below picks where it goes.
1006
+ const probe: i32 = quic.takeProbeInto(this.tx, H3_SERVER_ZERO, now)
1007
+ if (probe > 0) {
1008
+ this.send(this.tx, H3_SERVER_ZERO, probe, quic.probeAddress, H3_SERVER_ZERO, H3_SERVER_ZERO)
1009
+ }
992
1010
  const to: u8[] = this.addresses[slot]
1011
+ if (quic.pathKnown) {
1012
+ const current: u8[] = quic.sendAddress()
1013
+ for (let k: i32 = 0; k < toI32(to.length) && k < toI32(current.length); k++) {
1014
+ to[k] = current[k]
1015
+ }
1016
+ }
993
1017
  let retried: boolean = false
994
1018
  for (let guard: i32 = 0; guard < 64; guard++) {
995
1019
  const n: i32 = quicListenerTakeFlight(
@@ -157,7 +157,11 @@
157
157
  * routing each row by its ordinal (checked first: a server's ordinal is
158
158
  * refused before it routes anything), and `next(run)` answers
159
159
  * `PG_BATCH_ROW`, then `PG_BATCH_DONE`, `PG_BATCH_MISSING` or
160
- * `PG_BATCH_FAILED`, for each request. It reads no clock, and the batcher
160
+ * `PG_BATCH_FAILED`, for each request. The statement orders its rows by
161
+ * ordinal, so a key's last events come as soon as a later key's row does;
162
+ * with `sorted` false it does not (no Sort node, when `orderBy` is ""), rows
163
+ * are answered in whatever order they come, and every last event waits for
164
+ * the execution's end. It reads no clock, and the batcher
161
165
  * itself allocates only in its constructor (the connection's output buffer a
162
166
  * take writes into may still grow, up to its `maxOutput`); when to take is
163
167
  * the program's.
@@ -3349,6 +3353,14 @@ export class PgBatchConfig {
3349
3353
  runs: i32 = 16
3350
3354
  /** Whether every row of a batch comes in binary format, read with the binary decoders and its ordinal by `binaryInt`; false for text. */
3351
3355
  binary: boolean = false
3356
+ /**
3357
+ * Whether the statement orders its rows by ordinal, so each key's last
3358
+ * event comes as soon as a later key's row does. False drops `q.ord` from
3359
+ * the `order by` (and the Sort node it costs, when `orderBy` is ""): rows
3360
+ * route in whatever order they come, and every request's last event waits
3361
+ * for the execution's end.
3362
+ */
3363
+ sorted: boolean = true
3352
3364
  }
3353
3365
 
3354
3366
  /** One execution's requests, grouped by key, and how far its rows have been routed. */
@@ -3356,6 +3368,8 @@ class PgBatchRun {
3356
3368
  /** Key `k`'s requests' tags are `tags[first[k], first[k + 1])`, in the order they were added; grown by `take` to the batch. */
3357
3369
  tags: i32[]
3358
3370
  first: i32[]
3371
+ /** Under `sorted` false, whether key `k` has had a row; grown by `take` with `tags`, and empty when sorted. */
3372
+ had: boolean[]
3359
3373
  keys: i32 = 0
3360
3374
  /** The key rows are being read for, whether it has had one, and the next of its requests to answer. */
3361
3375
  cur: i32 = 0
@@ -3373,6 +3387,7 @@ class PgBatchRun {
3373
3387
  constructor() {
3374
3388
  this.tags = []
3375
3389
  this.first = []
3390
+ this.had = []
3376
3391
  }
3377
3392
 
3378
3393
  /** Opens the run for `keys` keys, none of whose rows have been read. */
@@ -3385,6 +3400,9 @@ class PgBatchRun {
3385
3400
  this.target = -1
3386
3401
  this.ended = false
3387
3402
  this.failed = false
3403
+ for (let k: i32 = 0; k < keys && k < toI32(this.had.length); k++) {
3404
+ this.had[k] = false
3405
+ }
3388
3406
  }
3389
3407
  }
3390
3408
 
@@ -3401,7 +3419,11 @@ class PgBatchRun {
3401
3419
  * connection to `fail(run)`; after each, `next(run)` answers events until
3402
3420
  * `PG_BATCH_NONE`, `tag` naming the request each is about. Every row goes to
3403
3421
  * every request for its key, and every request gets one last event:
3404
- * `PG_BATCH_DONE`, `PG_BATCH_MISSING` or `PG_BATCH_FAILED`. A batch with
3422
+ * `PG_BATCH_DONE`, `PG_BATCH_MISSING` or `PG_BATCH_FAILED`. Sorted (the
3423
+ * default), rows come in key order and a key's last events come as soon as
3424
+ * a later key's row does; with `sorted` false, rows come in any order, each
3425
+ * answered at once, and every last event waits for `end` or `fail`, then
3426
+ * comes in key order. A batch with
3405
3427
  * `binary` set comes in binary format: its rows are read with the binary
3406
3428
  * decoders, `binaryInt` for each ordinal, and the caller's `binary.failed`
3407
3429
  * is left as it was.
@@ -3445,7 +3467,7 @@ export class PgBatcher {
3445
3467
  private readonly runs: PgBatchRun[]
3446
3468
  /** The request the last `next(run)` answered about; -1 for none. */
3447
3469
  tag: i32 = -1
3448
- /** The number of runs failed by a bad ordinal (missing, NULL, not an integer, or naming no key still open), stopping at 2^31 - 1: each counts its first refused row, and the rows after it, dropped with the failed run, are not counted. */
3470
+ /** The number of runs failed by a bad ordinal (missing, NULL, not an integer, or naming no key still open; with `sorted` false, any key of the run is open), stopping at 2^31 - 1: each counts its first refused row, and the rows after it, dropped with the failed run, are not counted. */
3449
3471
  refused: i32 = 0
3450
3472
  /** The number of `row`, `end` and `fail` calls on a spent handle, each ignored, stopping at 2^31 - 1. */
3451
3473
  stray: i32 = 0
@@ -3454,6 +3476,7 @@ export class PgBatcher {
3454
3476
  private readonly keyType: i32
3455
3477
  private readonly maxBytes: i32
3456
3478
  private readonly binary: boolean
3479
+ private readonly sorted: boolean
3457
3480
  private storeEnd: i32 = 0
3458
3481
  private head: i32 = 0
3459
3482
  private count: i32 = 0
@@ -3492,10 +3515,15 @@ export class PgBatcher {
3492
3515
  this.maxRequests = maxRequests
3493
3516
  this.maxBytes = config.maxBytes
3494
3517
  this.binary = config.binary
3518
+ this.sorted = config.sorted
3495
3519
  this.name = config.name
3496
3520
  const type: string = keyType === PG_BATCH_INT ? "int8" : "text"
3497
3521
  const order: string = config.orderBy.length > 0 ? `, ${config.orderBy}` : ""
3498
- this.sql = `select t.*, q.ord from unnest($1::${type}[]) with ordinality as q(key, ord) cross join lateral (${config.select}) as t order by q.ord${order}`
3522
+ let tail: string = ` order by q.ord${order}`
3523
+ if (!config.sorted) {
3524
+ tail = config.orderBy.length > 0 ? ` order by ${config.orderBy}` : ""
3525
+ }
3526
+ this.sql = `select t.*, q.ord from unnest($1::${type}[]) with ordinality as q(key, ord) cross join lateral (${config.select}) as t${tail}`
3499
3527
  const queueSize: i32 = config.queueSize
3500
3528
  this.qTags = new Array<i32>(queueSize)
3501
3529
  this.qHashes = new Array<i32>(queueSize)
@@ -3765,6 +3793,9 @@ export class PgBatcher {
3765
3793
  // Grown together: `first` holds one more than `tags`, which bounds the keys too.
3766
3794
  run.tags = new Array<i32>(toI32(this.takenKeys.length))
3767
3795
  run.first = new Array<i32>(toI32(this.takenKeys.length) + 1)
3796
+ if (!this.sorted) {
3797
+ run.had = new Array<boolean>(toI32(this.takenKeys.length))
3798
+ }
3768
3799
  }
3769
3800
  run.first[0] = 0
3770
3801
  for (let k: i32 = 0; k < keys && k < toI32(this.keyCounts.length); k++) {
@@ -3854,11 +3885,12 @@ export class PgBatcher {
3854
3885
  /**
3855
3886
  * Routes `conn`'s current row, of run `r`'s execution, by the ordinal in
3856
3887
  * its last column: `next(r)` then answers `PG_BATCH_ROW` for each request
3857
- * of its key, after the last events of the keys before it. False, and the
3888
+ * of its key, after the last events of the keys before it (with `sorted`
3889
+ * false, at once: no last event comes before `end`). False, and the
3858
3890
  * row dropped, when the run has failed; and when the ordinal is NULL, not
3859
- * an integer, or names no key still open (one out of range, or one before
3860
- * the key the last row was for): that counts in `refused` and fails the
3861
- * run; and on a spent handle, which counts in `stray`. Panics on a handle
3891
+ * an integer, or names no key still open (one out of range, or, sorted,
3892
+ * one before the key the last row was for): that counts in `refused` and
3893
+ * fails the run; and on a spent handle, which counts in `stray`. Panics on a handle
3862
3894
  * `take` cannot give, or when the run's events are not all answered.
3863
3895
  */
3864
3896
  row(r: i32, conn: PgConnection): boolean {
@@ -3887,8 +3919,9 @@ export class PgBatcher {
3887
3919
  bad = conn.text.failed
3888
3920
  conn.text.failed = failed
3889
3921
  }
3890
- // Ordinals count from 1 and come in order: a row may name the current key again, never one before it.
3891
- if (bad || ordinal < toI64(run.cur) + 1 || ordinal > toI64(run.keys)) {
3922
+ // Ordinals count from 1 and, sorted, come in order: a row may name the current key again, never one before it.
3923
+ const least: i64 = this.sorted ? toI64(run.cur) + 1 : toI64(1)
3924
+ if (bad || ordinal < least || ordinal > toI64(run.keys)) {
3892
3925
  if (this.refused < 2147483647) {
3893
3926
  this.refused += 1
3894
3927
  }
@@ -3896,6 +3929,9 @@ export class PgBatcher {
3896
3929
  return false
3897
3930
  }
3898
3931
  run.target = toI32(ordinal) - 1
3932
+ if (!this.sorted && run.target < toI32(run.had.length)) {
3933
+ run.had[run.target] = true
3934
+ }
3899
3935
  return true
3900
3936
  }
3901
3937
 
@@ -3941,6 +3977,9 @@ export class PgBatcher {
3941
3977
  return PG_BATCH_NONE
3942
3978
  }
3943
3979
  const run: PgBatchRun = found
3980
+ if (!this.sorted) {
3981
+ return this.nextUnsorted(run)
3982
+ }
3944
3983
  // The keys before the row's, or every key once the execution is over, have had all their rows.
3945
3984
  let limit: i32 = run.cur
3946
3985
  if (run.target >= 0) {
@@ -3972,6 +4011,36 @@ export class PgBatcher {
3972
4011
  }
3973
4012
  return PG_BATCH_NONE
3974
4013
  }
4014
+
4015
+ // `next` with `sorted` false: the row's `PG_BATCH_ROW` events at once, and
4016
+ // every last event only once the execution is over, `cur` walking the keys.
4017
+ private nextUnsorted(run: PgBatchRun): i32 {
4018
+ if (run.target >= 0 && run.target < toI32(run.tags.length)) {
4019
+ const at: i32 = run.first[run.target] + run.at
4020
+ if (at < run.first[run.target + 1]) {
4021
+ run.at += 1
4022
+ this.tag = run.tags[at]
4023
+ return PG_BATCH_ROW
4024
+ }
4025
+ run.target = -1
4026
+ run.at = 0
4027
+ }
4028
+ if (!run.ended) {
4029
+ return PG_BATCH_NONE
4030
+ }
4031
+ while (run.cur < run.keys && run.cur < toI32(run.had.length)) {
4032
+ if (this.emit(run)) {
4033
+ if (run.failed) {
4034
+ return PG_BATCH_FAILED
4035
+ }
4036
+ return run.had[run.cur] ? PG_BATCH_DONE : PG_BATCH_MISSING
4037
+ }
4038
+ run.cur += 1
4039
+ run.at = 0
4040
+ }
4041
+ run.open = false
4042
+ return PG_BATCH_NONE
4043
+ }
3975
4044
  }
3976
4045
 
3977
4046
  // --- Batch timing --------------------------------------------------------------