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.
Files changed (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  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/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
@@ -143,7 +143,11 @@ class BcryptBatchKey {
143
143
  readonly key: Secret<u8[]>
144
144
 
145
145
  constructor(length: i32) {
146
- this.key = secret(new Array<u8>(length))
146
+ const key: u8[] = []
147
+ for (let i: i32 = 0; i < length; i++) {
148
+ key.push(toU8(0))
149
+ }
150
+ this.key = secret(key)
147
151
  }
148
152
 
149
153
  /** @internal Called by `BcryptBatcher.run`; the class is not exported. */
@@ -192,11 +196,11 @@ class BcryptBatchShare {
192
196
  */
193
197
  hashes: string[]
194
198
  /**
195
- * 16 bytes a request; a verify's are zeros and are not read.
199
+ * Each request's salt: 16 bytes for a hash, none for a verify.
196
200
  *
197
201
  * @internal Read by this module's task functions; the class is not exported.
198
202
  */
199
- salts: u8[]
203
+ salts: u8[][]
200
204
  /** @internal Read by this module's task functions; the class is not exported. */
201
205
  cost: i32
202
206
  /** @internal Read by this module's task functions; the class is not exported. */
@@ -212,9 +216,12 @@ class BcryptBatchShare {
212
216
  }
213
217
  }
214
218
 
215
- /** The holder at `list[i]` of `share`, or at `list[0]` past the batch's end (that lane is then passed as null). */
216
- const bcryptBatchLane = (share: BcryptBatchShare, list: i32[], i: i32): BcryptBatchKey => {
217
- const at: i32 = i < toI32(list.length) ? list[i] : list[0]
219
+ /** The holder at `list[i]` of `share`, or null for a lane past the first `n` of `list`, which reads nothing. */
220
+ const bcryptBatchLane = (share: BcryptBatchShare, list: i32[], n: i32, i: i32): BcryptBatchKey | null => {
221
+ if (i < 0 || i >= n || i >= toI32(list.length)) {
222
+ return null
223
+ }
224
+ const at: i32 = list[i]
218
225
  return share.passwords[at]
219
226
  }
220
227
 
@@ -229,7 +236,8 @@ const bcryptBatchVerifyAlone = (held: BcryptBatchKey, hash: string): string => {
229
236
 
230
237
  /**
231
238
  * One `bcryptVerifyN` over the first `n` of `list`: a holder's field is
232
- * passed only as a call's argument, so each lane count is its own call.
239
+ * passed only as a call's argument, so each lane count is its own call. A
240
+ * lane past `n` is null and reads nothing; an empty batch is an error.
233
241
  */
234
242
  const bcryptBatchVerifyLanes = (
235
243
  share: BcryptBatchShare,
@@ -237,25 +245,28 @@ const bcryptBatchVerifyLanes = (
237
245
  n: i32,
238
246
  hashes: string[]
239
247
  ): Result<boolean[], string> => {
240
- const a: BcryptBatchKey = bcryptBatchLane(share, list, 0)
241
- const b: BcryptBatchKey = bcryptBatchLane(share, list, 1)
242
- const c: BcryptBatchKey = bcryptBatchLane(share, list, 2)
243
- const d: BcryptBatchKey = bcryptBatchLane(share, list, 3)
244
- const e: BcryptBatchKey = bcryptBatchLane(share, list, 4)
245
- const f: BcryptBatchKey = bcryptBatchLane(share, list, 5)
246
- if (n <= 1) {
248
+ const a: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 0)
249
+ if (a === null) {
250
+ return Err("bcrypt: a batch has no lanes")
251
+ }
252
+ const b: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 1)
253
+ const c: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 2)
254
+ const d: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 3)
255
+ const e: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 4)
256
+ const f: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 5)
257
+ if (b === null) {
247
258
  return bcryptVerifyN(a.key, null, null, null, null, null, hashes)
248
259
  }
249
- if (n === 2) {
260
+ if (c === null) {
250
261
  return bcryptVerifyN(a.key, b.key, null, null, null, null, hashes)
251
262
  }
252
- if (n === 3) {
263
+ if (d === null) {
253
264
  return bcryptVerifyN(a.key, b.key, c.key, null, null, null, hashes)
254
265
  }
255
- if (n === 4) {
266
+ if (e === null) {
256
267
  return bcryptVerifyN(a.key, b.key, c.key, d.key, null, null, hashes)
257
268
  }
258
- if (n === 5) {
269
+ if (f === null) {
259
270
  return bcryptVerifyN(a.key, b.key, c.key, d.key, e.key, null, hashes)
260
271
  }
261
272
  return bcryptVerifyN(a.key, b.key, c.key, d.key, e.key, f.key, hashes)
@@ -268,25 +279,28 @@ const bcryptBatchHashLanes = (
268
279
  n: i32,
269
280
  salts: u8[]
270
281
  ): Result<string[], string> => {
271
- const a: BcryptBatchKey = bcryptBatchLane(share, list, 0)
272
- const b: BcryptBatchKey = bcryptBatchLane(share, list, 1)
273
- const c: BcryptBatchKey = bcryptBatchLane(share, list, 2)
274
- const d: BcryptBatchKey = bcryptBatchLane(share, list, 3)
275
- const e: BcryptBatchKey = bcryptBatchLane(share, list, 4)
276
- const f: BcryptBatchKey = bcryptBatchLane(share, list, 5)
277
- if (n <= 1) {
282
+ const a: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 0)
283
+ if (a === null) {
284
+ return Err("bcrypt: a batch has no lanes")
285
+ }
286
+ const b: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 1)
287
+ const c: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 2)
288
+ const d: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 3)
289
+ const e: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 4)
290
+ const f: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 5)
291
+ if (b === null) {
278
292
  return bcryptHashN(a.key, null, null, null, null, null, salts, share.cost)
279
293
  }
280
- if (n === 2) {
294
+ if (c === null) {
281
295
  return bcryptHashN(a.key, b.key, null, null, null, null, salts, share.cost)
282
296
  }
283
- if (n === 3) {
297
+ if (d === null) {
284
298
  return bcryptHashN(a.key, b.key, c.key, null, null, null, salts, share.cost)
285
299
  }
286
- if (n === 4) {
300
+ if (e === null) {
287
301
  return bcryptHashN(a.key, b.key, c.key, d.key, null, null, salts, share.cost)
288
302
  }
289
- if (n === 5) {
303
+ if (f === null) {
290
304
  return bcryptHashN(a.key, b.key, c.key, d.key, e.key, null, salts, share.cost)
291
305
  }
292
306
  return bcryptHashN(a.key, b.key, c.key, d.key, e.key, f.key, salts, share.cost)
@@ -301,10 +315,12 @@ const bcryptBatchVerifyRun = (
301
315
  out: string[]
302
316
  ): void => {
303
317
  const lanes: i32[] = []
304
- for (let i: i32 = from; i < from + n && i < toI32(list.length); i++) {
318
+ let i: i32 = from
319
+ for (let k: i32 = 0; k < n && i < toI32(list.length); k++) {
305
320
  if (i >= 0) {
306
321
  lanes.push(list[i])
307
322
  }
323
+ i += 1
308
324
  }
309
325
  if (toI32(lanes.length) === 0) {
310
326
  return
@@ -337,24 +353,31 @@ const bcryptBatchVerifyRun = (
337
353
 
338
354
  /** The hashes of `share` at `list[from..from + n)` as one `bcryptHashN`, each lane's answer into `out` at its index. */
339
355
  const bcryptBatchHashRun = (share: BcryptBatchShare, list: i32[], from: i32, n: i32, out: string[]): void => {
356
+ // Each lane's salt goes into `salts` at 16 times its lane; a request
357
+ // without a 16-byte salt is answered with bcrypt's error and joins no lane.
340
358
  const lanes: i32[] = []
341
- for (let i: i32 = from; i < from + n && i < toI32(list.length); i++) {
359
+ const salts: u8[] = new Array<u8>(96)
360
+ let q: i32 = 0
361
+ let i: i32 = from
362
+ for (let k: i32 = 0; k < n && i < toI32(list.length); k++) {
342
363
  if (i >= 0) {
343
- lanes.push(list[i])
364
+ const at: i32 = list[i]
365
+ const salt: u8[] = at >= 0 && at < toI32(share.salts.length) ? share.salts[at] : []
366
+ if (toI32(salt.length) === 16 && q >= 0 && q <= toI32(salts.length) - 16) {
367
+ for (let j: i32 = 0; j < 16 && q < toI32(salts.length); j++) {
368
+ salts[q] = salt[j]
369
+ q += 1
370
+ }
371
+ lanes.push(at)
372
+ } else if (at >= 0 && at < toI32(out.length)) {
373
+ out[at] = "!bcrypt: the salt is not 16 bytes"
374
+ }
344
375
  }
376
+ i += 1
345
377
  }
346
378
  if (toI32(lanes.length) === 0) {
347
379
  return
348
380
  }
349
- const salts: u8[] = new Array<u8>(96)
350
- for (let s: i32 = 0; s < toI32(lanes.length) && s < 6; s++) {
351
- const at: i32 = lanes[s]
352
- for (let j: i32 = 0; j < 16 && 16 * at + j < toI32(share.salts.length); j++) {
353
- if (at >= 0) {
354
- salts[16 * s + j] = share.salts[16 * at + j]
355
- }
356
- }
357
- }
358
381
  const made: Result<string[], string> = bcryptBatchHashLanes(share, lanes, toI32(lanes.length), salts)
359
382
  for (let s: i32 = 0; s < toI32(lanes.length); s++) {
360
383
  const at: i32 = lanes[s]
@@ -394,14 +417,22 @@ const bcryptBatchShareRun = (share: BcryptBatchShare): string => {
394
417
  for (let i: i32 = 0; i < toI32(share.kinds.length); i++) {
395
418
  out.push("")
396
419
  }
397
- const width: i32 = share.width < 1 ? 1 : share.width
420
+ // `bcryptBatcher` refuses a width outside 1 to 6, so this keeps it as given.
421
+ const least: i32 = share.width < 1 ? 1 : share.width
422
+ const width: i32 = least > 6 ? 6 : least
398
423
  const verifies: i32[] = bcryptBatchOfKind(share, BCRYPT_BATCH_VERIFY)
399
- for (let i: i32 = 0; i < toI32(verifies.length); i += width) {
400
- bcryptBatchVerifyRun(share, verifies, i, width, out)
401
- }
402
424
  const hashes: i32[] = bcryptBatchOfKind(share, BCRYPT_BATCH_HASH)
403
- for (let i: i32 = 0; i < toI32(hashes.length); i += width) {
404
- bcryptBatchHashRun(share, hashes, i, width, out)
425
+ if (width > 0) {
426
+ for (let i: i32 = 0; i < toI32(verifies.length); i++) {
427
+ if (i % width === 0) {
428
+ bcryptBatchVerifyRun(share, verifies, i, width, out)
429
+ }
430
+ }
431
+ for (let i: i32 = 0; i < toI32(hashes.length); i++) {
432
+ if (i % width === 0) {
433
+ bcryptBatchHashRun(share, hashes, i, width, out)
434
+ }
435
+ }
405
436
  }
406
437
  return out.join("\n")
407
438
  }
@@ -429,11 +460,11 @@ const bcryptBatchLines = (joined: string): string[] => {
429
460
  let from: i32 = 0
430
461
  while (from >= 0 && from <= toI32(joined.length)) {
431
462
  const at: i32 = toI32(joined.indexOf("\n", from))
432
- if (at < from || at > toI32(joined.length)) {
433
- lines.push(joined.slice(from))
463
+ if (at < 0 || at < from || at >= toI32(joined.length)) {
464
+ lines.push(joined.substring(from))
434
465
  return lines
435
466
  }
436
- lines.push(joined.slice(from, at))
467
+ lines.push(joined.substring(from, at))
437
468
  from = at + 1
438
469
  }
439
470
  return lines
@@ -457,11 +488,11 @@ const bcryptBatchAnswerOf = (ticket: i32, kind: i32, said: string): BcryptAnswer
457
488
  */
458
489
  export class BcryptBatcher {
459
490
  private holdNanos: i64
460
- /** The queue, from `head`: each request's copy of its password, kind, stored hash, salt (16 bytes each), ticket and arrival. */
491
+ /** The queue, from `head`: each request's copy of its password, kind, stored hash, salt (16 bytes for a hash, none for a verify), ticket and arrival. */
461
492
  private passwords: BcryptBatchKey[]
462
493
  private kinds: i32[]
463
494
  private hashes: string[]
464
- private salts: u8[]
495
+ private salts: u8[][]
465
496
  private tickets: i32[]
466
497
  private arrived: i64[]
467
498
  /** Answers made without a batch (a salt of the wrong length), handed out by the next `run`. */
@@ -470,7 +501,10 @@ export class BcryptBatcher {
470
501
  private cores: i32
471
502
  private cost: i32
472
503
  private head: i32
473
- /** The next ticket. */
504
+ /**
505
+ * The next ticket; after 2^31 - 1 it wraps to 0. Two requests share a
506
+ * ticket only if one is still queued after 2^31 others are made.
507
+ */
474
508
  private next: i32
475
509
 
476
510
  /**
@@ -515,12 +549,15 @@ export class BcryptBatcher {
515
549
  * @param hash The stored hash; its cost sets the work, so it must be one you
516
550
  * stored.
517
551
  * @param now The time of the call, from `monotonicNanos()`.
518
- * @returns The ticket its `BcryptAnswer` will carry.
552
+ * @returns The ticket its `BcryptAnswer` will carry: tickets count up from
553
+ * 0 and wrap to 0 after 2^31 - 1, so a later request can carry a smaller
554
+ * one.
519
555
  */
520
556
  verify(password: Secret<u8[]>, hash: string, now: i64): i32 {
521
557
  const ticket: i32 = this.next
522
- this.next = this.next + 1
523
- this.enqueue(password, BCRYPT_BATCH_VERIFY, hash, new Array<u8>(16), ticket, now)
558
+ this.next = ticket < 0x7fffffff ? ticket + 1 : 0
559
+ const noSalt: u8[] = []
560
+ this.enqueue(password, BCRYPT_BATCH_VERIFY, hash, noSalt, ticket, now)
524
561
  return ticket
525
562
  }
526
563
 
@@ -535,11 +572,13 @@ export class BcryptBatcher {
535
572
  * is bad, and the caller still owns and wipes it.
536
573
  * @param salt 16 fresh random bytes; read, not kept.
537
574
  * @param now The time of the call, from `monotonicNanos()`.
538
- * @returns The ticket its `BcryptAnswer` will carry.
575
+ * @returns The ticket its `BcryptAnswer` will carry: tickets count up from
576
+ * 0 and wrap to 0 after 2^31 - 1, so a later request can carry a smaller
577
+ * one.
539
578
  */
540
579
  hash(password: Secret<u8[]>, salt: u8[], now: i64): i32 {
541
580
  const ticket: i32 = this.next
542
- this.next = this.next + 1
581
+ this.next = ticket < 0x7fffffff ? ticket + 1 : 0
543
582
  if (toI32(salt.length) !== 16) {
544
583
  this.early.push(new BcryptAnswer(ticket, false, "", "bcrypt: the salt is not 16 bytes"))
545
584
  return ticket
@@ -553,9 +592,11 @@ export class BcryptBatcher {
553
592
  this.passwords.push(bcryptBatchTake(password))
554
593
  this.kinds.push(kind)
555
594
  this.hashes.push(hash)
595
+ const own: u8[] = []
556
596
  for (let j: i32 = 0; j < 16 && j < toI32(salt.length); j++) {
557
- this.salts.push(salt[j])
597
+ own.push(salt[j])
558
598
  }
599
+ this.salts.push(own)
559
600
  this.tickets.push(ticket)
560
601
  this.arrived.push(now)
561
602
  }
@@ -573,14 +614,18 @@ export class BcryptBatcher {
573
614
  if (toI32(this.early.length) > 0) {
574
615
  return 0
575
616
  }
576
- const queued: i32 = toI32(this.kinds.length) - this.head
617
+ const head: i32 = this.head
618
+ if (head < 0) {
619
+ return 0
620
+ }
621
+ const queued: i32 = toI32(this.kinds.length) - head
577
622
  if (queued <= 0) {
578
623
  return -1
579
624
  }
580
- if (queued >= this.width || this.head >= toI32(this.arrived.length)) {
625
+ if (queued >= this.width || head >= toI32(this.arrived.length)) {
581
626
  return 0
582
627
  }
583
- const waited: i64 = now - this.arrived[this.head]
628
+ const waited: i64 = now - this.arrived[head]
584
629
  return waited >= this.holdNanos ? 0 : this.holdNanos - waited
585
630
  }
586
631
 
@@ -606,30 +651,45 @@ export class BcryptBatcher {
606
651
  if (this.waitNanos(now) !== 0) {
607
652
  return answers
608
653
  }
609
- const queued: i32 = toI32(this.kinds.length) - this.head
654
+ const head: i32 = this.head
655
+ if (head < 0) {
656
+ return answers
657
+ }
658
+ const queued: i32 = toI32(this.kinds.length) - head
610
659
  if (queued <= 0) {
611
660
  return answers
612
661
  }
613
- const most: i32 = this.width * this.cores
614
- const taken: i32 = queued < most ? queued : most
662
+ const most: i64 = toI64(this.width) * toI64(this.cores)
663
+ const taken: i32 = toI64(queued) < most ? queued : toI32(most)
615
664
  const order: i32[] = this.orderOf(taken)
616
665
  const tasks: i32 = taken < this.cores ? taken : this.cores
666
+ const parts: i64 = toI64(tasks)
667
+ if (parts <= 0) {
668
+ return answers
669
+ }
617
670
  const shares: BcryptBatchShare[] = []
618
671
  // Share k takes order[taken·k/tasks, taken·(k+1)/tasks): contiguous, so one cost's verifies stay together.
619
672
  const owner: i32[] = []
620
673
  for (let k: i32 = 0; k < tasks; k++) {
621
674
  const share: BcryptBatchShare = new BcryptBatchShare(this.cost, this.width)
622
- const lo: i32 = (taken * k) / tasks
623
- const hi: i32 = (taken * (k + 1)) / tasks
675
+ const lo: i32 = toI32((toI64(taken) * toI64(k)) / parts)
676
+ const hi: i32 = toI32((toI64(taken) * (toI64(k) + 1)) / parts)
624
677
  for (let i: i32 = lo; i >= 0 && i < hi && i < toI32(order.length); i++) {
625
678
  const at: i32 = order[i]
626
- if (at >= 0 && at < toI32(this.passwords.length) && at < toI32(this.kinds.length)) {
679
+ const queue: i32[] = this.kinds
680
+ const saltQueue: u8[][] = this.salts
681
+ if (
682
+ at >= 0 &&
683
+ at < toI32(this.passwords.length) &&
684
+ at < toI32(queue.length) &&
685
+ at < toI32(saltQueue.length)
686
+ ) {
687
+ const kind: i32 = queue[at]
688
+ const salt: u8[] = saltQueue[at]
627
689
  share.passwords.push(this.passwords[at])
628
- share.kinds.push(this.kinds[at])
690
+ share.kinds.push(kind)
629
691
  share.hashes.push(at < toI32(this.hashes.length) ? this.hashes[at] : "")
630
- for (let j: i32 = 0; j < 16; j++) {
631
- share.salts.push(16 * at + j < toI32(this.salts.length) ? this.salts[16 * at + j] : toU8(0))
632
- }
692
+ share.salts.push(salt)
633
693
  owner.push(at)
634
694
  }
635
695
  }
@@ -641,7 +701,7 @@ export class BcryptBatcher {
641
701
  }
642
702
  {
643
703
  using s = scope()
644
- for (let k: i32 = 0; k < toI32(shares.length); k++) {
704
+ for (let k: i32 = 0; k < toI32(shares.length) && k < toI32(said.length); k++) {
645
705
  s.spawn(bcryptBatchShareRun, shares[k], said, k)
646
706
  }
647
707
  }
@@ -674,10 +734,14 @@ export class BcryptBatcher {
674
734
  private orderOf(taken: i32): i32[] {
675
735
  const keyed: i32[] = []
676
736
  const keys: i32[] = []
677
- for (let i: i32 = this.head; i < this.head + taken && i < toI32(this.kinds.length); i++) {
737
+ let n: i32 = 0
738
+ const queue: i32[] = this.kinds
739
+ for (let i: i32 = this.head; n < taken && i >= 0 && i < toI32(queue.length); i++) {
740
+ n += 1
741
+ const kind: i32 = queue[i]
678
742
  keyed.push(i)
679
743
  let key: i32 = 1000
680
- if (this.kinds[i] !== BCRYPT_BATCH_HASH) {
744
+ if (kind !== BCRYPT_BATCH_HASH) {
681
745
  key = i < toI32(this.hashes.length) ? bcryptBatchCostOf(this.hashes[i]) : -1
682
746
  }
683
747
  keys.push(key)
@@ -708,30 +772,40 @@ export class BcryptBatcher {
708
772
 
709
773
  /** Drops the requests before `head` once they are most of the queue, so a long-lived batcher's arrays stay small. */
710
774
  private compact(): void {
711
- if (this.head < 64 || this.head * 2 < toI32(this.kinds.length)) {
775
+ if (this.head < 64 || toI64(this.head) * 2 < toI64(this.kinds.length)) {
712
776
  return
713
777
  }
714
778
  const passwords: BcryptBatchKey[] = []
715
779
  const kinds: i32[] = []
716
780
  const hashes: string[] = []
717
- const salts: u8[] = []
781
+ const salts: u8[][] = []
718
782
  const tickets: i32[] = []
719
783
  const arrived: i64[] = []
720
- for (let i: i32 = this.head; i < toI32(this.kinds.length); i++) {
784
+ const oldPasswords: BcryptBatchKey[] = this.passwords
785
+ const oldKinds: i32[] = this.kinds
786
+ const oldHashes: string[] = this.hashes
787
+ const oldSalts: u8[][] = this.salts
788
+ const oldTickets: i32[] = this.tickets
789
+ const oldArrived: i64[] = this.arrived
790
+ for (let i: i32 = this.head; i >= 0 && i < toI32(oldKinds.length); i++) {
721
791
  if (
722
- i < toI32(this.passwords.length) &&
723
- i < toI32(this.hashes.length) &&
724
- i < toI32(this.tickets.length) &&
725
- i < toI32(this.arrived.length)
792
+ i < toI32(oldPasswords.length) &&
793
+ i < toI32(oldHashes.length) &&
794
+ i < toI32(oldSalts.length) &&
795
+ i < toI32(oldTickets.length) &&
796
+ i < toI32(oldArrived.length)
726
797
  ) {
727
- passwords.push(this.passwords[i])
728
- kinds.push(this.kinds[i])
729
- hashes.push(this.hashes[i])
730
- for (let j: i32 = 0; j < 16; j++) {
731
- salts.push(16 * i + j < toI32(this.salts.length) ? this.salts[16 * i + j] : toU8(0))
732
- }
733
- tickets.push(this.tickets[i])
734
- arrived.push(this.arrived[i])
798
+ const kind: i32 = oldKinds[i]
799
+ const hash: string = oldHashes[i]
800
+ const salt: u8[] = oldSalts[i]
801
+ const ticket: i32 = oldTickets[i]
802
+ const time: i64 = oldArrived[i]
803
+ passwords.push(oldPasswords[i])
804
+ kinds.push(kind)
805
+ hashes.push(hash)
806
+ salts.push(salt)
807
+ tickets.push(ticket)
808
+ arrived.push(time)
735
809
  }
736
810
  }
737
811
  this.passwords = passwords