@amritk/nish-aarch64-linux 0.38.0 → 0.39.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.
Files changed (58) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +348 -37
  3. package/package.json +1 -1
  4. package/runtime/core/host.d.ts +11 -0
  5. package/runtime/core/host.mjs +35 -0
  6. package/runtime/nish.d.ts +301 -18
  7. package/runtime/nish.h +74 -10
  8. package/runtime/nish.mjs +69 -6
  9. package/runtime/node.mjs +117 -0
  10. package/runtime/runtime-aes.c +12 -4
  11. package/runtime/runtime-async.c +43 -10
  12. package/runtime/runtime-checksum.c +207 -15
  13. package/runtime/runtime-net.c +32 -3
  14. package/runtime/runtime-os.c +174 -27
  15. package/runtime/runtime-region.c +94 -10
  16. package/runtime/runtime-sandbox.c +377 -17
  17. package/runtime/runtime-sha256.c +11 -4
  18. package/runtime/runtime-wasm.c +15 -0
  19. package/runtime/runtime.c +5 -0
  20. package/scripts/build.sh +27 -8
  21. package/std/README.md +5 -4
  22. package/std/compress/checksum.ts +13 -0
  23. package/std/compress/deflate.ts +0 -2
  24. package/std/crypto/p256.ts +247 -74
  25. package/std/crypto/p384.ts +3 -1
  26. package/std/crypto/pki.ts +35 -9
  27. package/std/crypto/x25519.ts +153 -64
  28. package/std/crypto/x509.ts +113 -10
  29. package/std/json-tokenizer.ts +20 -0
  30. package/std/json-typed.ts +217 -8
  31. package/std/net/handler.ts +130 -16
  32. package/std/net/http1-codec.ts +499 -150
  33. package/std/net/http1-server.ts +495 -22
  34. package/std/net/http2-cleartext.ts +526 -0
  35. package/std/net/http2-tls.ts +48 -0
  36. package/std/net/http2.ts +84 -0
  37. package/std/net/postgres-async.ts +1304 -86
  38. package/std/net/postgres-carrier.ts +343 -10
  39. package/std/net/postgres.ts +122 -7
  40. package/std/net/prefork.ts +195 -11
  41. package/std/net/quic-datagram.ts +30 -0
  42. package/std/net/quic-listener.ts +38 -12
  43. package/std/net/quic-packet.ts +19 -6
  44. package/std/net/quic-recovery.ts +134 -19
  45. package/std/net/quic-stream.ts +174 -36
  46. package/std/net/quic.ts +418 -18
  47. package/std/net/router.ts +39 -17
  48. package/std/net/serve.ts +162 -58
  49. package/std/net/tls/codec.ts +110 -32
  50. package/std/net/tls/record-server.ts +37 -7
  51. package/std/net/tls-tcp.ts +56 -1
  52. package/std/net/tls.ts +196 -50
  53. package/std/net/websocket.ts +55 -18
  54. package/std/net/webtransport.ts +179 -1
  55. package/std/regex.ts +69 -47
  56. package/std/region.ts +62 -11
  57. package/std/secret.ts +26 -59
  58. package/std/text.ts +233 -0
package/std/text.ts CHANGED
@@ -243,6 +243,239 @@ export const split = (text: string, sep: string): string[] => {
243
243
  }
244
244
  }
245
245
 
246
+ /**
247
+ * The first `limit` pieces of `text` between the occurrences of `sep`:
248
+ * JavaScript's `text.split(sep, limit)`. The pieces past the limit are
249
+ * dropped, not joined into the last one, so `splitLimit("a,b,c", ",", 2)` is `["a", "b"]`, and a
250
+ * limit of 0 gives `[]`.
251
+ *
252
+ * **A negative limit keeps every piece**, as it does under Node: JavaScript
253
+ * reads the limit with `ToUint32`, so `-1` is 4294967295, and any negative
254
+ * `i32` is at least 2147483648 there, more pieces than a string can split
255
+ * into. So `splitLimit(text, sep, -1)` is `split(text, sep)`, and the two
256
+ * readings agree on every `i32`. Refusing a negative limit would have been a
257
+ * panic where JavaScript answers.
258
+ *
259
+ * **An empty separator panics**, whatever the limit, for the reason `split`
260
+ * gives; JavaScript would answer `[]` for a limit of 0, and its UTF-16 units
261
+ * otherwise.
262
+ *
263
+ * @param text The text to split.
264
+ * @param sep The separator, compared byte for byte; it must not be empty.
265
+ * Occurrences are found left to right and do not overlap.
266
+ * @param limit The most pieces kept; a negative one keeps them all.
267
+ * @returns A fresh array of at most `limit` pieces, in order.
268
+ */
269
+ export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
270
+ const sepLength: i32 = toI32(sep.length)
271
+ if (sepLength === 0) {
272
+ panic("splitLimit: an empty separator")
273
+ }
274
+ const length: i32 = toI32(text.length)
275
+ const parts: string[] = []
276
+ if (limit === 0) {
277
+ return parts
278
+ }
279
+ let start: i32 = 0
280
+ while (true) {
281
+ // Searching from `start` rather than cutting `text` down after each match
282
+ // copies nothing until the piece is taken.
283
+ const at: i32 = toI32(text.indexOf(sep, start))
284
+ // `start` is 0 or one past a match, so it is never negative and never
285
+ // past `length`, and a hit starts at or after `start` and a whole
286
+ // separator before `length`. The tests around the `substring`s therefore
287
+ // always hold, but for the empty tail after a final separator; they are
288
+ // written because they are what proves both bounds within `text`, as in
289
+ // `splitLines`.
290
+ if (at < 0) {
291
+ parts.push(start >= 0 && start < length ? text.substring(start, length) : "")
292
+ return parts
293
+ }
294
+ if (start >= 0 && start < length && at >= 0 && at < length) {
295
+ parts.push(text.substring(start, at))
296
+ }
297
+ if (toI32(parts.length) === limit) {
298
+ return parts
299
+ }
300
+ start = at + sepLength
301
+ }
302
+ }
303
+
304
+ /** The most digits `toFixed` takes after the point; JavaScript throws a `RangeError` past it. */
305
+ const TO_FIXED_MAX_DIGITS: i32 = 100
306
+
307
+ /** Where `toFixed` stops writing fixed notation and answers `String(x)`, as JavaScript does. */
308
+ const TO_FIXED_LIMIT: f64 = 1e21
309
+
310
+ /** 2^52 and 2^53: an `f64` from the first up to the second is an integer, and its whole significand. */
311
+ const TWO_TO_52: f64 = 4503599627370496
312
+ const TWO_TO_53: f64 = 9007199254740992
313
+
314
+ /** The byte `0`; a decimal digit `d` is written as this plus `d`. */
315
+ const ASCII_ZERO: i32 = 48
316
+
317
+ /** The byte `.`. */
318
+ const ASCII_DOT: i32 = 46
319
+
320
+ /** The byte `-`. */
321
+ const ASCII_MINUS: i32 = 45
322
+
323
+ /**
324
+ * `digits`, a decimal number with its units digit first, halved in place,
325
+ * reading only its lowest `count` digits (the rest are zero). The answer is
326
+ * the bit the halving dropped, 0 or 1.
327
+ *
328
+ * It is written with `>>` and `&` rather than `/` and `%` because under Node
329
+ * an `i32` is a JavaScript number, and `/` there would give a fraction. The
330
+ * name is long for the reason `isTextBlankByte`'s is.
331
+ */
332
+ const fixedDigitsHalve = (digits: i32[], count: i32): i32 => {
333
+ let carry: i32 = 0
334
+ let i: i32 = count - 1
335
+ while (i >= 0 && i < toI32(digits.length)) {
336
+ const value: i32 = carry * 10 + digits[i]
337
+ digits[i] = value >> 1
338
+ carry = value & 1
339
+ i -= 1
340
+ }
341
+ return carry
342
+ }
343
+
344
+ /**
345
+ * `digits`, a decimal number with its units digit first, doubled in place when
346
+ * `doubling` is set, and then with `amount`, 0 or 1, added at the units. The
347
+ * array grows by a digit when the answer carries out of the top. The name is
348
+ * long for the reason `isTextBlankByte`'s is.
349
+ */
350
+ const fixedDigitsAdd = (digits: i32[], doubling: boolean, amount: i32): void => {
351
+ let carry: i32 = amount
352
+ let i: i32 = 0
353
+ const length: i32 = toI32(digits.length)
354
+ while (i < length) {
355
+ const value: i32 = (doubling ? digits[i] * 2 : digits[i]) + carry
356
+ digits[i] = value >= 10 ? value - 10 : value
357
+ carry = value >= 10 ? 1 : 0
358
+ i += 1
359
+ }
360
+ if (carry > 0) {
361
+ digits.push(carry)
362
+ }
363
+ }
364
+
365
+ /**
366
+ * `x` written with exactly `digits` digits after the point: JavaScript's
367
+ * `x.toFixed(digits)`, and the same text natively and under Node.
368
+ *
369
+ * The answer is the decimal closest to the **exact binary value** of `x`, and
370
+ * a tie between two takes the larger, as ECMA-262 says. That is not rounding
371
+ * the shortest printed decimal: `1.005` is stored as a little less than
372
+ * 1.005, so `toFixed(1.005, 2)` is `"1.00"`, while `2.5` is exact and a tie, so
373
+ * `toFixed(2.5, 0)` is `"3"` and `toFixed(-2.5, 0)` is `"-3"`. The digits are
374
+ * worked out in full, one decimal digit per array element, so the value is
375
+ * never rounded on the way; that is why this is written here and not with a
376
+ * multiplication by a power of ten, which rounds twice.
377
+ *
378
+ * The edges are JavaScript's. `NaN` gives `"NaN"`. `x` at or beyond 1e21 in
379
+ * size gives `String(x)`, the text a template hole gives, so
380
+ * `toFixed(1e21, 2)` is `"1e+21"`, and the infinities give `"Infinity"` and
381
+ * `"-Infinity"`. `-0` loses its sign, `"0.00"`, but a negative value that
382
+ * rounds to zero keeps it: `toFixed(-0.0001, 2)` is `"-0.00"`.
383
+ *
384
+ * **`digits` outside 0 to 100 panics**, where JavaScript throws a
385
+ * `RangeError`: the count is almost always a literal the caller chose, so a
386
+ * bad one is a mistake in the program, as `split`'s empty separator is.
387
+ *
388
+ * @param x The number to write.
389
+ * @param digits How many digits to write after the point, from 0 to 100; with
390
+ * 0 there is no point.
391
+ * @returns `x` in fixed notation, or `String(x)` when `x` is NaN or at least
392
+ * 1e21 in size.
393
+ */
394
+ export const toFixed = (x: f64, digits: i32): string => {
395
+ if (digits < 0 || digits > TO_FIXED_MAX_DIGITS) {
396
+ panic("toFixed: digits must be 0 to 100")
397
+ }
398
+ // biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
399
+ if (x !== x || x >= TO_FIXED_LIMIT || x <= -TO_FIXED_LIMIT) {
400
+ return `${x}`
401
+ }
402
+ const negative: boolean = x < 0
403
+ // `x` is `m * 2^exponent` for an integer `m` below 2^53: scaling it into
404
+ // [2^52, 2^53) by twos is exact, a subnormal included, and says `exponent`.
405
+ let m: f64 = negative ? -x : x
406
+ let exponent: i32 = 0
407
+ if (m > 0) {
408
+ while (m < TWO_TO_52) {
409
+ m *= 2
410
+ exponent -= 1
411
+ }
412
+ while (m >= TWO_TO_53) {
413
+ m /= 2
414
+ exponent += 1
415
+ }
416
+ }
417
+ // The number written is `m * 10^digits * 2^exponent`, rounded to an
418
+ // integer, with its last `digits` digits after the point. Multiplying by
419
+ // `10^digits` is that many zeros under the units; `m`'s own digits are exact
420
+ // to take out, since every step stays an integer below 2^53.
421
+ const decimal: i32[] = []
422
+ let i: i32 = 0
423
+ while (i < digits) {
424
+ decimal.push(0)
425
+ i += 1
426
+ }
427
+ while (m > 0) {
428
+ const last: f64 = m % 10
429
+ decimal.push(toI32(last))
430
+ m = (m - last) / 10
431
+ }
432
+ while (exponent > 0) {
433
+ fixedDigitsAdd(decimal, true, 0)
434
+ exponent -= 1
435
+ }
436
+ // A negative exponent halves the number that many times. The integer part
437
+ // is what is left; it rounds up exactly when the last bit dropped is 1,
438
+ // which is a remainder of at least a half, a tie included. Once the number
439
+ // is zero every bit still to drop is zero too, so the walk stops there.
440
+ let count: i32 = toI32(decimal.length)
441
+ let dropped: i32 = 0
442
+ while (exponent < 0 && count > 0) {
443
+ dropped = fixedDigitsHalve(decimal, count)
444
+ while (count > 0 && count <= toI32(decimal.length) && decimal[count - 1] === 0) {
445
+ count -= 1
446
+ }
447
+ exponent += 1
448
+ }
449
+ if (exponent === 0 && dropped === 1) {
450
+ fixedDigitsAdd(decimal, false, 1)
451
+ }
452
+ while (toI32(decimal.length) <= digits) {
453
+ decimal.push(0)
454
+ }
455
+ // The text is the digits from the highest that is not zero down, but never
456
+ // from below the units, which is index `digits`; the halving leaves zeros
457
+ // above the number. The point goes before the last `digits` of them, and a
458
+ // minus in front of any negative `x`.
459
+ let at: i32 = toI32(decimal.length) - 1
460
+ while (at > digits && at >= 0 && at < toI32(decimal.length) && decimal[at] === 0) {
461
+ at -= 1
462
+ }
463
+ const bytes: u8[] = []
464
+ if (negative) {
465
+ bytes.push(toU8(ASCII_MINUS))
466
+ }
467
+ while (at >= 0 && at < toI32(decimal.length)) {
468
+ // Read before the pushes: a push may move an array, so it ends the proof.
469
+ const digit: i32 = decimal[at]
470
+ if (at === digits - 1) {
471
+ bytes.push(toU8(ASCII_DOT))
472
+ }
473
+ bytes.push(toU8(ASCII_ZERO + digit))
474
+ at -= 1
475
+ }
476
+ return String.fromBytes(bytes, 0, toI32(bytes.length))
477
+ }
478
+
246
479
  /**
247
480
  * `text` with every occurrence of `needle` replaced by `replacement`.
248
481
  *