@amritk/nish-x86_64-linux 0.37.0 → 0.38.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/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +32 -10
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/nish.d.ts +344 -48
- package/runtime/nish.h +29 -7
- package/runtime/runtime-async.c +228 -45
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-region.c +85 -7
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +3 -3
- package/runtime/runtime.c +50 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +86 -28
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +2 -0
- package/std/array.ts +266 -0
- package/std/host/text.ts +86 -0
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +12 -0
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +18 -4
- package/std/net/http1-server.ts +13 -3
- package/std/net/postgres-async.ts +1005 -0
- package/std/net/postgres.ts +7 -4
- package/std/net/serve.ts +763 -0
- package/std/text.ts +231 -5
package/std/net/handler.ts
CHANGED
|
@@ -1061,6 +1061,18 @@ export class HttpHandler {
|
|
|
1061
1061
|
return r
|
|
1062
1062
|
}
|
|
1063
1063
|
|
|
1064
|
+
/**
|
|
1065
|
+
* Whether the client of `r` has hung up: its connection is closed or
|
|
1066
|
+
* reused, or the peer ended its stream while `r` is held. A half-close
|
|
1067
|
+
* counts, as nginx counts it, so a hung task is cancelled rather than
|
|
1068
|
+
* kept for a reader who may be gone.
|
|
1069
|
+
* @param r a request `take` handed over
|
|
1070
|
+
* @returns true once `r`'s connection is closed or reused, or its peer's stream has ended
|
|
1071
|
+
*/
|
|
1072
|
+
hungUp(r: HttpRequest): boolean {
|
|
1073
|
+
return !this.live(r) || this.server.connection(r.slot).ended()
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1064
1076
|
/** Whether `r`'s connection is the one it was read on, still open. */
|
|
1065
1077
|
private live(r: HttpRequest): boolean {
|
|
1066
1078
|
return this.server.holds(r.slot) && this.slotGen[r.slot] === r.gen
|
package/std/net/http-compress.ts
CHANGED
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
* otherwise the compressed bytes are dropped and the body goes as it is. With
|
|
29
29
|
* `segmentMode` set, `respondEncoded` also sends it compressed only when the
|
|
30
30
|
* whole response, head included, takes fewer TCP segments of `mss` bytes (the
|
|
31
|
-
* connection's `mss` if set, else the compressor's,
|
|
31
|
+
* connection's `mss` if set, else the compressor's `mss` if set, else the MSS
|
|
32
|
+
* the server read from the socket, else `HTTP_DEFAULT_MSS`, 1448): a
|
|
32
33
|
* 1300-byte body that gzips to 700 still fits in one segment either way.
|
|
33
34
|
*
|
|
34
35
|
* After each call `decision` says why (`HTTP_COMPRESS_SENT`, `_NOT_ACCEPTED`,
|
|
@@ -435,11 +436,17 @@ export class HttpCompressor {
|
|
|
435
436
|
/**
|
|
436
437
|
* Whether `respondEncoded` also sends the compressed response only when,
|
|
437
438
|
* head included, it takes fewer TCP segments of `mss` bytes than the body
|
|
438
|
-
* as it is; off by default.
|
|
439
|
-
*
|
|
439
|
+
* as it is; off by default. Segments are the connection's own `mss` if the
|
|
440
|
+
* program set it, else this compressor's `mss` if set, else the socket's MSS
|
|
441
|
+
* the server read on accept, else `HTTP_DEFAULT_MSS`.
|
|
440
442
|
*/
|
|
441
443
|
segmentMode: boolean
|
|
442
|
-
/**
|
|
444
|
+
/**
|
|
445
|
+
* The TCP segment size `segmentMode` counts in, in bytes, when the program
|
|
446
|
+
* sets it: 0 (the default) or less means unset, so a connection's socket MSS
|
|
447
|
+
* is used, or `HTTP_DEFAULT_MSS` where there is none. A value set here wins
|
|
448
|
+
* over the socket's; a connection's own `mss` wins over it.
|
|
449
|
+
*/
|
|
443
450
|
mss: i32
|
|
444
451
|
/** `HTTP_ENCODER_AUTO` (the default), `HTTP_ENCODER_FIXED` or `HTTP_ENCODER_LEVEL`. */
|
|
445
452
|
encoder: i32
|
|
@@ -485,7 +492,7 @@ export class HttpCompressor {
|
|
|
485
492
|
this.minSaving = 1
|
|
486
493
|
this.minSavingPercent = 0
|
|
487
494
|
this.segmentMode = false
|
|
488
|
-
this.mss =
|
|
495
|
+
this.mss = 0
|
|
489
496
|
this.encoder = HTTP_ENCODER_AUTO
|
|
490
497
|
this.fixedLimit = 16384
|
|
491
498
|
this.decision = HTTP_COMPRESS_NOT_ACCEPTED
|
|
@@ -610,10 +617,11 @@ export class HttpCompressor {
|
|
|
610
617
|
/**
|
|
611
618
|
* `segmentMode`'s rule, for a response of `identityBytes` bytes sent as it
|
|
612
619
|
* is or `encodedBytes` compressed, heads included, at `mss` bytes a segment
|
|
613
|
-
* (`this.mss` when 0 or less
|
|
614
|
-
*
|
|
615
|
-
*
|
|
616
|
-
* driving HTTP/2 or HTTP/3 by hand may
|
|
620
|
+
* (`this.mss` when 0 or less, and `HTTP_DEFAULT_MSS` when both are, as
|
|
621
|
+
* `segmentSize` reads it): whether the compressed one takes fewer segments.
|
|
622
|
+
* When it does not, `decision` becomes `HTTP_COMPRESS_NO_FEWER_SEGMENTS`.
|
|
623
|
+
* `respondEncoded` calls it; a program driving HTTP/2 or HTTP/3 by hand may
|
|
624
|
+
* with its own head sizes.
|
|
617
625
|
*
|
|
618
626
|
* @param identityBytes the whole response's length sent uncompressed, head
|
|
619
627
|
* included, in bytes
|
package/std/net/http-fields.ts
CHANGED
|
@@ -48,7 +48,11 @@
|
|
|
48
48
|
* compared without case (`HTTP_FIELDS_AUTHORITY_MISMATCH`);
|
|
49
49
|
* - a `content-length` that is not one run of at most eighteen digits, or two
|
|
50
50
|
* that disagree (`HTTP_FIELDS_BAD_CONTENT_LENGTH`). Matching it against
|
|
51
|
-
* the body is the version's job, since only it sees the body
|
|
51
|
+
* the body is the version's job, since only it sees the body;
|
|
52
|
+
* - in a section a program sends, a second `date` field: `Date` is a
|
|
53
|
+
* singleton (RFC 9110 §5.3, §6.6.1) whose value holds a comma, so two
|
|
54
|
+
* cannot be combined into one and a reader cannot tell which to believe
|
|
55
|
+
* (`HTTP_FIELDS_DATE_REPEATED`).
|
|
52
56
|
*
|
|
53
57
|
* **The date.** `httpDateWrite` writes a second since 1970 as an IMF-fixdate
|
|
54
58
|
* (RFC 9110 §5.6.7), `Fri, 16 Oct 2026 08:11:54 GMT`, the form every version's
|
|
@@ -102,6 +106,9 @@ export const HTTP_FIELDS_AUTHORITY_MISMATCH: i32 = -10
|
|
|
102
106
|
/** A `content-length` that is not a run of digits, or two that disagree. */
|
|
103
107
|
export const HTTP_FIELDS_BAD_CONTENT_LENGTH: i32 = -11
|
|
104
108
|
|
|
109
|
+
/** A `date` field that a section about to be sent holds more than once (RFC 9110 §5.3). */
|
|
110
|
+
export const HTTP_FIELDS_DATE_REPEATED: i32 = -12
|
|
111
|
+
|
|
105
112
|
/** `contentLength` when the section has no `content-length`. */
|
|
106
113
|
export const HTTP_FIELDS_NO_LENGTH: i64 = -1
|
|
107
114
|
|
|
@@ -380,9 +387,10 @@ const httpFieldsHasUserinfo = (authority: u8[]): boolean => {
|
|
|
380
387
|
/**
|
|
381
388
|
* Checks a field section a program is about to send, pseudo-headers left
|
|
382
389
|
* out: every name valid and in lowercase, every value valid, nothing
|
|
383
|
-
* connection-specific,
|
|
384
|
-
*
|
|
385
|
-
*
|
|
390
|
+
* connection-specific, at most one `date` (a singleton, RFC 9110 §5.3), and
|
|
391
|
+
* as many values as names. Answers `HTTP_FIELDS_OK` or the first refusal. A
|
|
392
|
+
* version's writer refuses what this refuses, so that a program's string can
|
|
393
|
+
* never reach the wire as a field it was not.
|
|
386
394
|
*
|
|
387
395
|
* @param names the regular field names about to be sent, in order
|
|
388
396
|
* @param values the value for each name, at the same index
|
|
@@ -395,6 +403,7 @@ export const httpFieldsCheckOutgoing = (names: u8[][], values: u8[][]): i32 => {
|
|
|
395
403
|
if (n !== toI32(values.length)) {
|
|
396
404
|
return HTTP_FIELDS_BAD_VALUE
|
|
397
405
|
}
|
|
406
|
+
let dated: boolean = false
|
|
398
407
|
for (let k: i32 = 0; k < n && k < toI32(names.length) && k < toI32(values.length); k++) {
|
|
399
408
|
const name: u8[] = names[k]
|
|
400
409
|
const value: u8[] = values[k]
|
|
@@ -407,6 +416,12 @@ export const httpFieldsCheckOutgoing = (names: u8[][], values: u8[][]): i32 => {
|
|
|
407
416
|
if (httpFieldConnectionSpecific(name, value)) {
|
|
408
417
|
return HTTP_FIELDS_CONNECTION_SPECIFIC
|
|
409
418
|
}
|
|
419
|
+
if (httpFieldIs(name, "date")) {
|
|
420
|
+
if (dated) {
|
|
421
|
+
return HTTP_FIELDS_DATE_REPEATED
|
|
422
|
+
}
|
|
423
|
+
dated = true
|
|
424
|
+
}
|
|
410
425
|
}
|
|
411
426
|
return HTTP_FIELDS_OK
|
|
412
427
|
}
|
package/std/net/http1-codec.ts
CHANGED
|
@@ -1648,7 +1648,8 @@ export const http1IsFieldText = (text: string): boolean => {
|
|
|
1648
1648
|
* (which would end the line early and let the rest be read as a field or a
|
|
1649
1649
|
* second response); a field name that is not a token; `names` and `values` of
|
|
1650
1650
|
* different lengths; a `Content-Length` or `Transfer-Encoding` among the
|
|
1651
|
-
* caller's fields, since the framing is decided here;
|
|
1651
|
+
* caller's fields, since the framing is decided here; two `Date` fields in
|
|
1652
|
+
* any case, since `Date` is a singleton (RFC 9110 §5.3); a 1xx or 204 response
|
|
1652
1653
|
* with a body length (RFC 9110 §15.2, §15.3.5); and any other `bodyLength`
|
|
1653
1654
|
* below zero.
|
|
1654
1655
|
*
|
|
@@ -1680,6 +1681,7 @@ export const http1ResponseHead = (
|
|
|
1680
1681
|
return null
|
|
1681
1682
|
}
|
|
1682
1683
|
const parts: string[] = [`HTTP/1.1 ${status} ${reason}\r\n`]
|
|
1684
|
+
let dated: boolean = false
|
|
1683
1685
|
for (let i: i32 = 0; i < count && i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
1684
1686
|
const name: string = names[i]
|
|
1685
1687
|
const value: string = values[i]
|
|
@@ -1693,6 +1695,12 @@ export const http1ResponseHead = (
|
|
|
1693
1695
|
) {
|
|
1694
1696
|
return null
|
|
1695
1697
|
}
|
|
1698
|
+
if (http1SameWord(name, 0, nameLength, "date")) {
|
|
1699
|
+
if (dated) {
|
|
1700
|
+
return null
|
|
1701
|
+
}
|
|
1702
|
+
dated = true
|
|
1703
|
+
}
|
|
1696
1704
|
parts.push(`${name}: ${value}\r\n`)
|
|
1697
1705
|
}
|
|
1698
1706
|
if (bodyLength >= 0) {
|
|
@@ -2007,7 +2015,8 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
|
2007
2015
|
*
|
|
2008
2016
|
* Answers where the head ends; `HTTP1_REFUSED` for everything
|
|
2009
2017
|
* `http1ResponseHead` refuses, a `Connection` among the caller's fields when
|
|
2010
|
-
* `connection` is given, a `connection` that is not field text,
|
|
2018
|
+
* `connection` is given, a `connection` that is not field text, two `date`
|
|
2019
|
+
* fields among the caller's (a singleton, RFC 9110 §5.3), and a `date`
|
|
2011
2020
|
* that is not field text on a final status, the only one it is sent on;
|
|
2012
2021
|
* `HTTP1_NO_ROOM` when it is well formed but does not fit, the `Date` line
|
|
2013
2022
|
* counted when it is sent. Either way `out` is untouched.
|
|
@@ -2051,7 +2060,7 @@ export const http1WriteResponseHead = (
|
|
|
2051
2060
|
// field, the connection field and the empty line: counted before anything
|
|
2052
2061
|
// is written, in 64 bits so that no caller's lengths can wrap the sum.
|
|
2053
2062
|
let need: i64 = toI64(17) + toI64(reason.length)
|
|
2054
|
-
let
|
|
2063
|
+
let dated: boolean = false
|
|
2055
2064
|
for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
2056
2065
|
const name: u8[] = names[i]
|
|
2057
2066
|
const value: u8[] = values[i]
|
|
@@ -2065,10 +2074,15 @@ export const http1WriteResponseHead = (
|
|
|
2065
2074
|
return HTTP1_REFUSED
|
|
2066
2075
|
}
|
|
2067
2076
|
if (http1BytesAre(name, "date")) {
|
|
2068
|
-
|
|
2077
|
+
// Date is a singleton (RFC 9110 §5.3): a second one is refused.
|
|
2078
|
+
if (dated) {
|
|
2079
|
+
return HTTP1_REFUSED
|
|
2080
|
+
}
|
|
2081
|
+
dated = true
|
|
2069
2082
|
}
|
|
2070
2083
|
need = need + toI64(name.length) + toI64(value.length) + toI64(4)
|
|
2071
2084
|
}
|
|
2085
|
+
const sendDate: boolean = status >= 200 && toI32(date.length) > 0 && !dated
|
|
2072
2086
|
if (sendDate) {
|
|
2073
2087
|
// "Date: " value CRLF: 37 bytes for an IMF-fixdate.
|
|
2074
2088
|
need = need + toI64(8) + toI64(date.length)
|
package/std/net/http1-server.ts
CHANGED
|
@@ -887,6 +887,16 @@ export class Http1Connection {
|
|
|
887
887
|
this.inputEnded = true
|
|
888
888
|
}
|
|
889
889
|
|
|
890
|
+
/**
|
|
891
|
+
* Whether the peer's byte stream has ended (`endInput`): a request still
|
|
892
|
+
* held on the connection is one its client hung up on, or half-closed.
|
|
893
|
+
*
|
|
894
|
+
* @returns true once the carrier read the end of the peer's stream
|
|
895
|
+
*/
|
|
896
|
+
ended(): boolean {
|
|
897
|
+
return this.inputEnded
|
|
898
|
+
}
|
|
899
|
+
|
|
890
900
|
/** The socket failed: nothing more is sent or read, and the connection is done. */
|
|
891
901
|
abort(): void {
|
|
892
902
|
this.state = H1_S_FAILED
|
|
@@ -1506,8 +1516,8 @@ export class Http1Connection {
|
|
|
1506
1516
|
/**
|
|
1507
1517
|
* The segment `respondEncoded` counts in under `enc.segmentMode`: this
|
|
1508
1518
|
* connection's `mss` when the program set it, else `enc.mss` when the
|
|
1509
|
-
* program set it
|
|
1510
|
-
*
|
|
1519
|
+
* program set it (above 0), else the socket's (`socketMss`), else
|
|
1520
|
+
* `HTTP_DEFAULT_MSS`.
|
|
1511
1521
|
*
|
|
1512
1522
|
* @param enc the compressor whose own `mss` is the fallback after this connection's
|
|
1513
1523
|
* @returns the segment size in bytes that segment counting uses for this connection
|
|
@@ -1516,7 +1526,7 @@ export class Http1Connection {
|
|
|
1516
1526
|
if (this.mss > 0) {
|
|
1517
1527
|
return this.mss
|
|
1518
1528
|
}
|
|
1519
|
-
if (enc.mss > 0
|
|
1529
|
+
if (enc.mss > 0) {
|
|
1520
1530
|
return enc.mss
|
|
1521
1531
|
}
|
|
1522
1532
|
return this.socketMss > 0 ? this.socketMss : HTTP_DEFAULT_MSS
|