@amritk/nish-aarch64-linux 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.
Files changed (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -103,6 +103,33 @@ const H2C_NANOS_PER_MS: i64 = 1000000
103
103
  /** The client connection preface, `PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n` (RFC 9113 §3.4). */
104
104
  const H2C_PREFACE: string = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
105
105
 
106
+ /**
107
+ * One slot's socket and preface: what `accept` sets and `close` clears. The
108
+ * slot is made by the first accept into it and kept, with its connection.
109
+ */
110
+ class H2cSlot {
111
+ /** When the slot was accepted, in `monotonicNanos`: its preface deadline's start. */
112
+ since: i64 = 0
113
+ /** The slot's 24 preface bytes, read here until they are whole. */
114
+ preface: u8[]
115
+ /** The slot's HTTP/2 connection, the one `connections` holds at `at`. */
116
+ conn: Http2Connection
117
+ /** The slot's number, its place in `held` and in `connections`. */
118
+ at: i32 = 0
119
+ /** The slot's socket, -1 while the slot is free. */
120
+ fd: i32 = -1
121
+ /** How many preface bytes the slot has read; 24 once the preface is whole and fed. */
122
+ got: i32 = 0
123
+ /** Whether the slot's socket has ended and `endInput` was called for it. */
124
+ ended: boolean = false
125
+
126
+ constructor(at: i32, conn: Http2Connection) {
127
+ this.at = at
128
+ this.conn = conn
129
+ this.preface = new Array<u8>(H2C_PREFACE_SIZE)
130
+ }
131
+ }
132
+
106
133
  /**
107
134
  * A pool of HTTP/2 connections over plain TCP, with prior knowledge, on one
108
135
  * listening socket. `connection(slot)` is the HTTP/2 side of each slot.
@@ -123,18 +150,10 @@ export class Http2CleartextServer {
123
150
  date: HttpDate
124
151
  /** The frame buffers every slot's connection borrows: one of each per slot at most, none made up front. */
125
152
  readonly pools: Http2Pools
126
- /** Each slot's socket, -1 while the slot is free; as long as the slots built so far. */
127
- private fds: i32[]
128
- /** The slots closed since they were used, a stack `accept` takes from before it builds a new one. */
129
- private free: i32[]
130
- /** Every slot's 24 preface bytes, slot `k`'s at `24 * k`; made whole by the constructor. */
131
- private prefaces: u8[]
132
- /** How many preface bytes each slot has read; 24 once the preface is whole and fed. */
133
- private got: i32[]
134
- /** When each slot was accepted, in `monotonicNanos`: its preface deadline's start. */
135
- private since: i64[]
136
- /** Whether each slot's socket has ended and `endInput` was called for it. */
137
- private ended: boolean[]
153
+ /** The slots accepted into so far, slot `k` at `k`; a free one keeps its socket at -1. */
154
+ private held: H2cSlot[]
155
+ /** The slots closed since they were used, a stack `accept` takes from before it makes a new one. */
156
+ private free: H2cSlot[]
138
157
  /** The last accepted peer's address, in `nish:net`'s 18-byte form. */
139
158
  private peer: u8[]
140
159
  /** How many slots the pool may hold. */
@@ -143,14 +162,18 @@ export class Http2CleartextServer {
143
162
  private listener: i32 = -1
144
163
  /** Nanoseconds a slot may wait for its whole preface before `expire` closes it. */
145
164
  private prefaceNanos: i64 = 0
146
- /** How many held slots have not read their whole preface: `expire` walks none while this is 0. */
147
- private waiting: i32 = 0
165
+ /**
166
+ * How many held slots have not read their whole preface: `expire` walks none
167
+ * while this is 0. At most one per slot, and `accept` and `close` keep it
168
+ * exact, so the unsigned count neither wraps up nor goes below 0.
169
+ */
170
+ private waiting: u32 = 0
148
171
 
149
172
  /**
150
173
  * A pool of `size` slots serving HTTP/2 under `config` on `listener`. Only
151
174
  * slot 0's connection is made here, so a config it refuses panics now, with
152
175
  * its words; every other slot's is made by the first `accept` into it and
153
- * kept. The slots' preface arrays, 24 bytes each, are made here too.
176
+ * kept, with the slot's 24-byte preface array.
154
177
  *
155
178
  * @param config The caps every slot's connection is made under; kept, and shared by every slot.
156
179
  * @param listener The listening socket from `tcpListen`; the program still owns it and closes it.
@@ -170,12 +193,8 @@ export class Http2CleartextServer {
170
193
  this.slots = size
171
194
  this.prefaceNanos = toI64(prefaceTimeout) * H2C_NANOS_PER_MS
172
195
  this.connections = []
173
- this.fds = []
196
+ this.held = []
174
197
  this.free = []
175
- this.got = []
176
- this.since = []
177
- this.ended = []
178
- this.prefaces = new Array<u8>(size * H2C_PREFACE_SIZE)
179
198
  this.peer = new Array<u8>(H2C_ADDRESS_SIZE)
180
199
  this.date = new HttpDate()
181
200
  // Formatted once now, so no slot ever sends the bytes before a first format.
@@ -187,17 +206,30 @@ export class Http2CleartextServer {
187
206
  first.pools = this.pools
188
207
  }
189
208
 
190
- /** Makes the next slot's connection, on this pool's buffers and `date`. */
191
- private build(): void {
209
+ /** Makes the next slot's connection, on this pool's buffers and `date`, and answers it. */
210
+ private build(): Http2Connection {
192
211
  const conn: Http2Connection = new Http2Connection(this.config)
193
212
  conn.date = this.date.current()
194
213
  conn.pools = this.pools
195
214
  this.connections.push(conn)
215
+ return conn
216
+ }
217
+
218
+ /** The slot `slot` names, or null when it is out of range or never accepted into. */
219
+ private at(slot: i32): H2cSlot | null {
220
+ return slot >= 0 && slot < toI32(this.held.length) ? this.held[slot] : null
221
+ }
222
+
223
+ /** The slot `slot` names when it holds a socket, or null. */
224
+ private open(slot: i32): H2cSlot | null {
225
+ const s: H2cSlot | null = this.at(slot)
226
+ return s !== null && s.fd >= 0 ? s : null
196
227
  }
197
228
 
198
229
  /** Whether `slot` holds a socket and has read its whole preface. */
199
230
  private live(slot: i32): boolean {
200
- return this.holds(slot) && this.got[slot] === H2C_PREFACE_SIZE
231
+ const s: H2cSlot | null = this.open(slot)
232
+ return s !== null && s.got === H2C_PREFACE_SIZE
201
233
  }
202
234
 
203
235
  /**
@@ -215,13 +247,14 @@ export class Http2CleartextServer {
215
247
  * @returns Slots with an open socket, whether their preface is whole or not; 0 to `size()`.
216
248
  */
217
249
  busy(): i32 {
218
- let n: i32 = 0
219
- for (const fd of this.fds) {
220
- if (fd >= 0) {
221
- n = n + 1
250
+ // At most one per slot, so the unsigned count cannot wrap.
251
+ let n: u32 = 0
252
+ for (const s of this.held) {
253
+ if (s.fd >= 0) {
254
+ n += 1
222
255
  }
223
256
  }
224
- return n
257
+ return toI32(n)
225
258
  }
226
259
 
227
260
  /**
@@ -231,7 +264,7 @@ export class Http2CleartextServer {
231
264
  * @returns True when `slot` is in range and holds a socket.
232
265
  */
233
266
  holds(slot: i32): boolean {
234
- return slot >= 0 && slot < toI32(this.fds.length) && this.fds[slot] >= 0
267
+ return this.open(slot) !== null
235
268
  }
236
269
 
237
270
  /**
@@ -242,7 +275,8 @@ export class Http2CleartextServer {
242
275
  * none.
243
276
  */
244
277
  fd(slot: i32): i32 {
245
- return this.holds(slot) ? this.fds[slot] : -1
278
+ const s: H2cSlot | null = this.open(slot)
279
+ return s !== null ? s.fd : H2C_NO_FD
246
280
  }
247
281
 
248
282
  /**
@@ -265,8 +299,14 @@ export class Http2CleartextServer {
265
299
  */
266
300
  connection(slot: i32): Http2Connection {
267
301
  this.date.current()
268
- const at: i32 = slot >= 0 && slot < toI32(this.connections.length) ? slot : H2C_ZERO
269
- return this.connections[at]
302
+ if (slot >= 0 && slot < toI32(this.connections.length)) {
303
+ return this.connections[slot]
304
+ }
305
+ // Slot 0's connection, made by the constructor, answers for any other slot, so this builds none.
306
+ if (toI32(this.connections.length) > 0) {
307
+ return this.connections[0]
308
+ }
309
+ return this.build()
270
310
  }
271
311
 
272
312
  /**
@@ -284,33 +324,44 @@ export class Http2CleartextServer {
284
324
  if (fd < 0) {
285
325
  return fd
286
326
  }
287
- let k: i32 = H2C_POOL_FULL
288
- if (this.free.length > 0) {
289
- k = this.free[this.free.length - 1]
290
- this.free.pop()
291
- } else if (toI32(this.fds.length) < this.slots) {
292
- k = toI32(this.fds.length)
293
- this.fds.push(H2C_NO_FD)
294
- this.got.push(H2C_ZERO)
295
- this.since.push(toI64(H2C_ZERO))
296
- this.ended.push(false)
297
- }
298
- if (k < 0) {
327
+ const s: H2cSlot | null = this.take()
328
+ if (s === null) {
299
329
  netClose(fd)
300
330
  return H2C_POOL_FULL
301
331
  }
302
- // Slots are built in order, so this makes one connection at most.
303
- while (toI32(this.connections.length) <= k) {
332
+ // `restart` gives back every buffer; the preface is read before any is borrowed.
333
+ s.conn.restart()
334
+ s.fd = fd
335
+ s.got = H2C_ZERO
336
+ s.since = monotonicNanos()
337
+ s.ended = false
338
+ this.waiting += 1
339
+ return s.at
340
+ }
341
+
342
+ /** A free slot for `accept`: a closed one first, then the next never used; null when every slot is busy. */
343
+ private take(): H2cSlot | null {
344
+ const free: H2cSlot[] = this.free
345
+ const f: i32 = toI32(free.length)
346
+ // `f > 0` proves the index and `free.length > 0` the `pop`, which the prover reads only off the array.
347
+ if (f > 0 && free.length > 0) {
348
+ const s: H2cSlot = free[f - 1]
349
+ free.pop()
350
+ return s
351
+ }
352
+ const k: i32 = toI32(this.held.length)
353
+ if (k >= this.slots) {
354
+ return null
355
+ }
356
+ // Slots are made in order and slot 0's connection by the constructor, so this makes one connection at
357
+ // most.
358
+ while (toI32(this.connections.length) < k) {
304
359
  this.build()
305
360
  }
306
- // `restart` gives back every buffer; the preface is read before any is borrowed.
307
- this.connections[k].restart()
308
- this.fds[k] = fd
309
- this.got[k] = H2C_ZERO
310
- this.since[k] = monotonicNanos()
311
- this.ended[k] = false
312
- this.waiting = this.waiting + 1
313
- return k
361
+ const conn: Http2Connection = k < toI32(this.connections.length) ? this.connections[k] : this.build()
362
+ const s: H2cSlot = new H2cSlot(k, conn)
363
+ this.held.push(s)
364
+ return s
314
365
  }
315
366
 
316
367
  /**
@@ -322,16 +373,18 @@ export class Http2CleartextServer {
322
373
  * @returns True once the preface is whole; false while it is not, or once the slot is closed.
323
374
  */
324
375
  private admit(slot: i32): boolean {
325
- if (!this.holds(slot)) {
376
+ const s: H2cSlot | null = this.open(slot)
377
+ if (s === null) {
326
378
  return false
327
379
  }
328
- if (this.got[slot] === H2C_PREFACE_SIZE) {
380
+ if (s.got === H2C_PREFACE_SIZE) {
329
381
  return true
330
382
  }
331
- const base: i32 = slot * H2C_PREFACE_SIZE
332
- while (this.got[slot] < H2C_PREFACE_SIZE) {
333
- const have: i32 = this.got[slot]
334
- const n: i32 = netRead(this.fds[slot], this.prefaces, base + have, H2C_PREFACE_SIZE - have)
383
+ const preface: u8[] = s.preface
384
+ const want: string = H2C_PREFACE
385
+ let have: i32 = s.got
386
+ while (have >= 0 && have < H2C_PREFACE_SIZE) {
387
+ const n: i32 = netRead(s.fd, preface, have, H2C_PREFACE_SIZE - have)
335
388
  if (n === H2C_AGAIN) {
336
389
  return false
337
390
  }
@@ -339,16 +392,22 @@ export class Http2CleartextServer {
339
392
  this.close(slot)
340
393
  return false
341
394
  }
342
- for (let k: i32 = have; k < have + n; k++) {
343
- if (toI32(this.prefaces[base + k]) !== toI32(H2C_PREFACE.charCodeAt(k))) {
395
+ // `netRead` answers at most the 24 - `have` bytes it was asked for, so the unsigned sum is at most 24.
396
+ const end: i32 = toI32(toU32(have) + toU32(n))
397
+ for (let k: i32 = have; k < end; k++) {
398
+ if (k < 0 || k >= toI32(preface.length) || k >= toI32(want.length)) {
399
+ break
400
+ }
401
+ if (toI32(preface[k]) !== toI32(want.charCodeAt(k))) {
344
402
  this.close(slot)
345
403
  return false
346
404
  }
347
405
  }
348
- this.got[slot] = have + n
406
+ s.got = end
407
+ have = end
349
408
  }
350
- this.waiting = this.waiting - 1
351
- this.connections[slot].feed(this.prefaces, base, H2C_PREFACE_SIZE)
409
+ this.waiting -= 1
410
+ s.conn.feed(preface, H2C_ZERO, H2C_PREFACE_SIZE)
352
411
  return true
353
412
  }
354
413
 
@@ -391,28 +450,29 @@ export class Http2CleartextServer {
391
450
  * that failed, a free slot, or one closed for a bad preface.
392
451
  */
393
452
  next(slot: i32): i32 {
394
- if (!this.holds(slot)) {
453
+ const s: H2cSlot | null = this.open(slot)
454
+ if (s === null) {
395
455
  return H2_ERROR
396
456
  }
397
457
  if (!this.admit(slot)) {
398
- return this.holds(slot) ? H2_NEED_MORE : H2_ERROR
458
+ return s.fd >= 0 ? H2_NEED_MORE : H2_ERROR
399
459
  }
400
460
  this.date.current()
401
- const conn: Http2Connection = this.connections[slot]
461
+ const conn: Http2Connection = s.conn
402
462
  // The socket is read straight into the connection's input, so a byte is
403
463
  // copied once on its way in.
404
- while (!this.ended[slot]) {
464
+ while (!s.ended) {
405
465
  const room: i32 = conn.inputTail()
406
466
  if (room <= 0) {
407
467
  break
408
468
  }
409
- const n: i32 = netRead(this.fds[slot], conn.input, conn.inputEnd, room)
469
+ const n: i32 = netRead(s.fd, conn.input, conn.inputEnd, room)
410
470
  if (n === H2C_AGAIN) {
411
471
  break
412
472
  }
413
473
  if (n <= 0) {
414
474
  // The peer's end, or a socket that failed: either way nothing more comes.
415
- this.ended[slot] = true
475
+ s.ended = true
416
476
  conn.endInput()
417
477
  break
418
478
  }
@@ -439,20 +499,22 @@ export class Http2CleartextServer {
439
499
  * preface.
440
500
  */
441
501
  flush(slot: i32): i32 {
442
- if (!this.holds(slot)) {
502
+ const s: H2cSlot | null = this.open(slot)
503
+ if (s === null) {
443
504
  return H2C_DONE
444
505
  }
445
- if (!this.live(slot)) {
506
+ if (s.got !== H2C_PREFACE_SIZE) {
446
507
  return H2C_WANT_READ
447
508
  }
448
- const conn: Http2Connection = this.connections[slot]
509
+ const conn: Http2Connection = s.conn
449
510
  while (conn.wantsWrite()) {
450
- const n: i32 = netWrite(
451
- this.fds[slot],
452
- conn.output,
453
- conn.outputStart,
454
- conn.outputEnd - conn.outputStart
455
- )
511
+ const start: i32 = conn.outputStart
512
+ const end: i32 = conn.outputEnd
513
+ if (start < 0 || end < start) {
514
+ break
515
+ }
516
+ // Both ends checked just above, so the unsigned difference is the span's length and does not wrap.
517
+ const n: i32 = netWrite(s.fd, conn.output, start, toI32(toU32(end) - toU32(start)))
456
518
  if (n === H2C_AGAIN) {
457
519
  break
458
520
  }
@@ -466,7 +528,7 @@ export class Http2CleartextServer {
466
528
  if (conn.isDone()) {
467
529
  return H2C_DONE
468
530
  }
469
- const read: i32 = this.ended[slot] ? H2C_ZERO : H2C_WANT_READ
531
+ const read: i32 = s.ended ? H2C_ZERO : H2C_WANT_READ
470
532
  return conn.wantsWrite() ? read | H2C_WANT_WRITE : read
471
533
  }
472
534
 
@@ -482,14 +544,21 @@ export class Http2CleartextServer {
482
544
  if (this.waiting === 0) {
483
545
  return 0
484
546
  }
485
- let n: i32 = 0
486
- for (let k: i32 = 0; k < toI32(this.fds.length); k++) {
487
- if (this.fds[k] >= 0 && this.got[k] < H2C_PREFACE_SIZE && now - this.since[k] >= this.prefaceNanos) {
488
- this.close(k)
489
- n = n + 1
547
+ // At most one per slot, so the unsigned count cannot wrap.
548
+ let n: u32 = 0
549
+ for (const s of this.held) {
550
+ // `now` at or past `since`, so the unsigned difference is the wait, which does not wrap.
551
+ if (
552
+ s.fd >= 0 &&
553
+ s.got < H2C_PREFACE_SIZE &&
554
+ now >= s.since &&
555
+ toU64(now) - toU64(s.since) >= toU64(this.prefaceNanos)
556
+ ) {
557
+ this.close(s.at)
558
+ n += 1
490
559
  }
491
560
  }
492
- return n
561
+ return toI32(n)
493
562
  }
494
563
 
495
564
  /**
@@ -500,17 +569,18 @@ export class Http2CleartextServer {
500
569
  * @param slot The slot to free; for a free or unknown slot nothing is closed.
501
570
  */
502
571
  close(slot: i32): void {
503
- if (!this.holds(slot)) {
572
+ const s: H2cSlot | null = this.open(slot)
573
+ if (s === null) {
504
574
  return
505
575
  }
506
- netClose(this.fds[slot])
507
- this.fds[slot] = -1
508
- if (this.got[slot] < H2C_PREFACE_SIZE) {
509
- this.waiting = this.waiting - 1
576
+ netClose(s.fd)
577
+ s.fd = H2C_NO_FD
578
+ if (s.got < H2C_PREFACE_SIZE) {
579
+ this.waiting -= 1
510
580
  }
511
- this.got[slot] = H2C_ZERO
512
- this.free.push(slot)
513
- this.connections[slot].drop()
581
+ s.got = H2C_ZERO
582
+ this.free.push(s)
583
+ s.conn.drop()
514
584
  }
515
585
 
516
586
  /**