@amritk/nish-x86_64-linux 0.38.0 → 0.40.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.
- package/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/json.ts
CHANGED
|
@@ -851,6 +851,10 @@ const JSON_SLASH_T: i32 = 116
|
|
|
851
851
|
const JSON_DIGIT_ZERO: i32 = 48
|
|
852
852
|
const JSON_MINUS: i32 = 45
|
|
853
853
|
const JSON_DECIMAL_BASE: i32 = 10
|
|
854
|
+
/** The most decimal digits an `i64` has: `-9223372036854775808` has nineteen. */
|
|
855
|
+
const JSON_I64_DIGITS: i32 = 19
|
|
856
|
+
/** The most decimal digits an `i32` has: `-2147483648` has ten. */
|
|
857
|
+
const JSON_I32_DIGITS: i32 = 10
|
|
854
858
|
const JSON_LOWER_A: i32 = 97
|
|
855
859
|
|
|
856
860
|
/**
|
|
@@ -1175,6 +1179,48 @@ export class JsonWriter {
|
|
|
1175
1179
|
}
|
|
1176
1180
|
}
|
|
1177
1181
|
|
|
1182
|
+
/**
|
|
1183
|
+
* `key(name)` from a key already quoted: when the writer is minified, in an
|
|
1184
|
+
* object, owes a key and has room for all of it, the comma and `quoted` go
|
|
1185
|
+
* in as one copy; any other way, `key(name)` runs, so the bytes and
|
|
1186
|
+
* `failed` are always the ones `key(name)` leaves.
|
|
1187
|
+
*
|
|
1188
|
+
* @internal Called by the code `jsonEncode` writes for a class (src/emit-json.ts).
|
|
1189
|
+
* @param name - the key, unquoted.
|
|
1190
|
+
* @param quoted - `name` quoted and escaped as `key` escapes it, then a colon.
|
|
1191
|
+
*/
|
|
1192
|
+
keyQuoted(name: string, quoted: string): void {
|
|
1193
|
+
const buf: u8[] = this.buf
|
|
1194
|
+
const cap: i32 = toI32(buf.length)
|
|
1195
|
+
const sep: i32 = this.sep
|
|
1196
|
+
const n: i32 = toI32(quoted.length)
|
|
1197
|
+
const at: i32 = this.end
|
|
1198
|
+
if (
|
|
1199
|
+
this.failed !== JSON_WRITE_OK ||
|
|
1200
|
+
sep < JSON_WRITER_SEP_NONE ||
|
|
1201
|
+
sep > JSON_WRITER_SEP_COMMA ||
|
|
1202
|
+
this.afterKey ||
|
|
1203
|
+
!jsonWriterInObject(this) ||
|
|
1204
|
+
at < 0 ||
|
|
1205
|
+
at > cap - sep - n
|
|
1206
|
+
) {
|
|
1207
|
+
this.key(name)
|
|
1208
|
+
return
|
|
1209
|
+
}
|
|
1210
|
+
if (sep === JSON_WRITER_SEP_COMMA && at < cap) {
|
|
1211
|
+
buf[at] = toU8(JSON_COMMA)
|
|
1212
|
+
}
|
|
1213
|
+
let j: i32 = at + sep
|
|
1214
|
+
let k: i32 = 0
|
|
1215
|
+
while (k < n && j >= 0 && j < cap) {
|
|
1216
|
+
buf[j] = toU8(quoted.charCodeAt(k))
|
|
1217
|
+
j += 1
|
|
1218
|
+
k += 1
|
|
1219
|
+
}
|
|
1220
|
+
this.end = j
|
|
1221
|
+
this.afterKey = true
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1178
1224
|
/**
|
|
1179
1225
|
* `s` quoted and escaped as `JSON.stringify` escapes it; every other byte
|
|
1180
1226
|
* goes through as itself.
|
|
@@ -1856,13 +1902,18 @@ const jsonWriterPutRuns = (w: JsonWriter, bytes: u8[], off: i32, len: i32): bool
|
|
|
1856
1902
|
* step the same under Node, where `/` does not truncate.
|
|
1857
1903
|
*/
|
|
1858
1904
|
const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
1905
|
+
// Counted against powers of ten on `-|v|`, as `jsonWriterPutDigits64` counts.
|
|
1906
|
+
const negative: i32 = v < 0 ? v : -v
|
|
1859
1907
|
let digits: i32 = 1
|
|
1860
|
-
let
|
|
1861
|
-
while (
|
|
1862
|
-
rest = (rest - (rest % JSON_DECIMAL_BASE)) / JSON_DECIMAL_BASE
|
|
1908
|
+
let bound: i32 = -JSON_DECIMAL_BASE
|
|
1909
|
+
while (digits < JSON_I32_DIGITS && negative <= bound) {
|
|
1863
1910
|
digits += 1
|
|
1911
|
+
if (digits < JSON_I32_DIGITS) {
|
|
1912
|
+
bound = bound * JSON_DECIMAL_BASE
|
|
1913
|
+
}
|
|
1864
1914
|
}
|
|
1865
1915
|
const sign: i32 = v < 0 ? 1 : 0
|
|
1916
|
+
let rest: i32 = v
|
|
1866
1917
|
const extra: i32 = suffix >= 0 ? 1 : 0
|
|
1867
1918
|
const buf: u8[] = w.buf
|
|
1868
1919
|
const at: i32 = w.end
|
|
@@ -1877,7 +1928,6 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
1877
1928
|
if (extra === 1 && at + width - 1 < toI32(buf.length) && at + width - 1 >= 0) {
|
|
1878
1929
|
buf[at + width - 1] = toU8(JSON_DIGIT_ZERO + suffix)
|
|
1879
1930
|
}
|
|
1880
|
-
rest = v
|
|
1881
1931
|
for (let k: i32 = at + sign + digits - 1; k >= at + sign && k >= 0 && k < toI32(buf.length); k -= 1) {
|
|
1882
1932
|
const digit: i32 = rest % JSON_DECIMAL_BASE
|
|
1883
1933
|
buf[k] = toU8(JSON_DIGIT_ZERO + (digit < 0 ? -digit : digit))
|
|
@@ -1891,14 +1941,20 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
1891
1941
|
const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
|
|
1892
1942
|
// Built with `toI64`: under Node an `i64` is a BigInt, and a bare `10` would
|
|
1893
1943
|
// stay a number that `%` and `/` refuse to mix with one.
|
|
1944
|
+
// The digits are counted against powers of ten on `-|v|`, which every `i64`
|
|
1945
|
+
// has, so the count costs a compare and a multiply per digit, not a divide.
|
|
1894
1946
|
const ten: i64 = toI64(10)
|
|
1947
|
+
const negative: i64 = v < 0 ? v : -v
|
|
1895
1948
|
let digits: i32 = 1
|
|
1896
|
-
let
|
|
1897
|
-
while (
|
|
1898
|
-
rest = rest / ten
|
|
1949
|
+
let bound: i64 = -ten
|
|
1950
|
+
while (digits < JSON_I64_DIGITS && negative <= bound) {
|
|
1899
1951
|
digits += 1
|
|
1952
|
+
if (digits < JSON_I64_DIGITS) {
|
|
1953
|
+
bound = bound * ten
|
|
1954
|
+
}
|
|
1900
1955
|
}
|
|
1901
1956
|
const sign: i32 = v < 0 ? 1 : 0
|
|
1957
|
+
let rest: i64 = v
|
|
1902
1958
|
const buf: u8[] = w.buf
|
|
1903
1959
|
const at: i32 = w.end
|
|
1904
1960
|
const width: i32 = sign + digits
|
|
@@ -1909,7 +1965,6 @@ const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
|
|
|
1909
1965
|
if (sign === 1) {
|
|
1910
1966
|
buf[at] = toU8(JSON_MINUS)
|
|
1911
1967
|
}
|
|
1912
|
-
rest = v
|
|
1913
1968
|
for (let k: i32 = at + width - 1; k >= at + sign && k >= 0 && k < toI32(buf.length); k -= 1) {
|
|
1914
1969
|
const digit: i32 = toI32(rest % ten)
|
|
1915
1970
|
buf[k] = toU8(JSON_DIGIT_ZERO + (digit < 0 ? -digit : digit))
|
package/std/net/fetch.ts
CHANGED
|
@@ -130,7 +130,7 @@ import { PkiRoots, pkiLoadRoots, pkiLoadSystemRoots } from "nish/crypto/pki-root
|
|
|
130
130
|
import { TlsClient, TlsClientConfig, tlsClientIsIpLiteral } from "nish/net/tls/client"
|
|
131
131
|
import { TLS_RECORD_AUTH, TlsRecordClient } from "nish/net/tls/record-client"
|
|
132
132
|
import { TLS_RECORD_AGAIN, TLS_RECORD_STATE_FAILED, TLS_RECORD_STATE_OPEN } from "nish/net/tls/record-server"
|
|
133
|
-
import { TlsClientTrust, tlsClientTrust, tlsClientVerdict } from "nish/net/tls/verify"
|
|
133
|
+
import { TlsClientTrust, tlsClientDistrustSpki, tlsClientTrust, tlsClientVerdict } from "nish/net/tls/verify"
|
|
134
134
|
|
|
135
135
|
/** No error. */
|
|
136
136
|
export const HTTP_E_NONE: i32 = 0
|
|
@@ -295,6 +295,14 @@ export class HttpOptions {
|
|
|
295
295
|
maxBodyBytes: i64 = 268435456
|
|
296
296
|
/** A PEM file of the roots an https server's chain must end at; null for the system's. */
|
|
297
297
|
rootsPemPath: string | null = null
|
|
298
|
+
/**
|
|
299
|
+
* Keys no https server's chain may hold, each the SHA-256 of a
|
|
300
|
+
* certificate's subjectPublicKeyInfo DER, 32 bytes: a chain with a leaf,
|
|
301
|
+
* intermediate or root whose key is listed fails with `HTTP_E_CERTIFICATE`
|
|
302
|
+
* (`tlsClientDistrustSpki`). Null, the default, for none; a hash of
|
|
303
|
+
* another length fails every https fetch rather than being skipped.
|
|
304
|
+
*/
|
|
305
|
+
distrustSpkiSha256: u8[][] | null = null
|
|
298
306
|
/**
|
|
299
307
|
* For a URL the program did not choose: only the addresses and hosts the
|
|
300
308
|
* policy allows are reached, no proxy is used, and its caps apply (see
|
|
@@ -1691,8 +1699,10 @@ class HttpClientFault {
|
|
|
1691
1699
|
|
|
1692
1700
|
/**
|
|
1693
1701
|
* The roots an https fetch verifies against: the caller's `rootsPemPath`, or
|
|
1694
|
-
* the system's bundle without one
|
|
1695
|
-
*
|
|
1702
|
+
* the system's bundle without one, distrusting the keys of
|
|
1703
|
+
* `distrustSpkiSha256`; null with `fault` set when no root could be read,
|
|
1704
|
+
* which fails the fetch rather than trusting nothing, or when a hash to
|
|
1705
|
+
* distrust is not one, which fails it rather than trusting the key.
|
|
1696
1706
|
*/
|
|
1697
1707
|
const httpClientLoadTrust = (options: HttpOptions, fault: HttpClientFault): TlsClientTrust | null => {
|
|
1698
1708
|
const path: string | null = options.rootsPemPath
|
|
@@ -1702,7 +1712,15 @@ const httpClientLoadTrust = (options: HttpOptions, fault: HttpClientFault): TlsC
|
|
|
1702
1712
|
fault.fail(HTTP_E_CERTIFICATE, `no trusted root certificate could be read from ${where}`)
|
|
1703
1713
|
return null
|
|
1704
1714
|
}
|
|
1705
|
-
|
|
1715
|
+
const trust: TlsClientTrust = tlsClientTrust(roots)
|
|
1716
|
+
const distrusted: u8[][] = options.distrustSpkiSha256 ?? []
|
|
1717
|
+
for (const hash of distrusted) {
|
|
1718
|
+
if (!tlsClientDistrustSpki(trust, hash)) {
|
|
1719
|
+
fault.fail(HTTP_E_CERTIFICATE, `a key to distrust is ${hash.length} bytes, not a SHA-256 of 32`)
|
|
1720
|
+
return null
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
return trust
|
|
1706
1724
|
}
|
|
1707
1725
|
|
|
1708
1726
|
/**
|
package/std/net/handler.ts
CHANGED
|
@@ -237,9 +237,15 @@ export class HttpRequest {
|
|
|
237
237
|
targetEnd: i32 = 0
|
|
238
238
|
/**
|
|
239
239
|
* Where the path ends: the target's first `?`, or its end.
|
|
240
|
-
* @internal Set by `HttpHandler` in this module
|
|
240
|
+
* @internal Set by `HttpHandler` in this module from the router's `pathEnd` once it has routed the request,
|
|
241
|
+
* so the path is scanned once; copied by std/net/serve.ts with the head, and read by `pathIs` and `queryIndex`.
|
|
241
242
|
*/
|
|
242
243
|
pathEnd: i32 = 0
|
|
244
|
+
/**
|
|
245
|
+
* How many bytes of `head` are this request's head: every span lies inside `head[0 .. headLength)`.
|
|
246
|
+
* @internal Set by `HttpHandler` in this module when it copies the head, and read by std/net/serve.ts to copy a request.
|
|
247
|
+
*/
|
|
248
|
+
headLength: i32 = 0
|
|
243
249
|
/**
|
|
244
250
|
* How many fields `spans` describes.
|
|
245
251
|
* @internal Set by `HttpHandler` in this module when it copies the head, and read by `header`.
|
|
@@ -488,6 +494,10 @@ export class HttpHandler {
|
|
|
488
494
|
private slotGen: i32[]
|
|
489
495
|
private current: i32[]
|
|
490
496
|
private open: boolean[]
|
|
497
|
+
/** Whether a slot is in `owed`: an answer's send was put off for the request queued behind it. */
|
|
498
|
+
private owing: boolean[]
|
|
499
|
+
/** The slots whose send `flush` still owes, `owedCount` of them. */
|
|
500
|
+
private owed: i32[]
|
|
491
501
|
/** `pollWait`'s output. */
|
|
492
502
|
private readyFds: i32[]
|
|
493
503
|
/** Per watched id: whether its descriptor was ready in the last `wait`. */
|
|
@@ -535,9 +545,13 @@ export class HttpHandler {
|
|
|
535
545
|
loop: i32 = 0
|
|
536
546
|
private queueStart: i32 = 0
|
|
537
547
|
private queueCount: i32 = 0
|
|
548
|
+
private owedCount: i32 = 0
|
|
549
|
+
private sent: i64 = 0
|
|
538
550
|
private outputSize: i32 = 0
|
|
539
551
|
/** Set by `drain`: accept nothing more and close each connection once it is idle. */
|
|
540
552
|
private draining: boolean = false
|
|
553
|
+
/** Set by `deferSends`: a send may wait for `flush`. Off, every answer is sent as it ends. */
|
|
554
|
+
private deferring: boolean = false
|
|
541
555
|
/** Whether `json` gave out the writer for a value `sendJson` has not yet sent. */
|
|
542
556
|
private jsonBegun: boolean = false
|
|
543
557
|
|
|
@@ -570,6 +584,8 @@ export class HttpHandler {
|
|
|
570
584
|
this.slotGen = new Array<i32>(limits.slots)
|
|
571
585
|
this.current = new Array<i32>(limits.slots)
|
|
572
586
|
this.open = new Array<boolean>(limits.slots)
|
|
587
|
+
this.owing = new Array<boolean>(limits.slots)
|
|
588
|
+
this.owed = new Array<i32>(limits.slots)
|
|
573
589
|
for (let s: i32 = 0; s < limits.slots; s++) {
|
|
574
590
|
this.current[s] = -1
|
|
575
591
|
}
|
|
@@ -631,6 +647,7 @@ export class HttpHandler {
|
|
|
631
647
|
* @returns how many whole requests are queued for `take` after this turn
|
|
632
648
|
*/
|
|
633
649
|
wait(timeoutMs: i32): i32 {
|
|
650
|
+
this.flush()
|
|
634
651
|
for (let id: i32 = 0; id < HANDLER_MAX_WATCHED; id++) {
|
|
635
652
|
this.watchedFired[id] = false
|
|
636
653
|
}
|
|
@@ -648,6 +665,12 @@ export class HttpHandler {
|
|
|
648
665
|
timeout = rest
|
|
649
666
|
}
|
|
650
667
|
}
|
|
668
|
+
if (this.deferring && this.queueCount > 0) {
|
|
669
|
+
// A send `flush` made let a parked request in: it is ready now. Only
|
|
670
|
+
// owed sends can do that; without `deferSends` a request left queued
|
|
671
|
+
// is the program's to take, and the turn blocks as it always has.
|
|
672
|
+
timeout = 0
|
|
673
|
+
}
|
|
651
674
|
const n: i32 = pollWait(this.loop, this.readyFds, timeout)
|
|
652
675
|
const now: i64 = monotonicNanos()
|
|
653
676
|
this.turnAt = now
|
|
@@ -679,6 +702,17 @@ export class HttpHandler {
|
|
|
679
702
|
return this.queueCount
|
|
680
703
|
}
|
|
681
704
|
|
|
705
|
+
/**
|
|
706
|
+
* Lets the handler put a connection's send off until `flush`, so the
|
|
707
|
+
* answers to a pipelined burst go out in one write. Off by default, so an
|
|
708
|
+
* answer is sent as it ends. A program that turns it on calls `flush` (or
|
|
709
|
+
* `wait`, which flushes) after each turn of answers, before it blocks;
|
|
710
|
+
* `nish/net/serve` does.
|
|
711
|
+
*/
|
|
712
|
+
deferSends(): void {
|
|
713
|
+
this.deferring = true
|
|
714
|
+
}
|
|
715
|
+
|
|
682
716
|
/**
|
|
683
717
|
* Begins a graceful stop: the handler accepts no more connections, closes
|
|
684
718
|
* each idle one now, and closes each other one once the request on it is
|
|
@@ -732,9 +766,36 @@ export class HttpHandler {
|
|
|
732
766
|
}
|
|
733
767
|
}
|
|
734
768
|
|
|
769
|
+
/**
|
|
770
|
+
* Sends every answer whose send was put off, so the connection's answers to
|
|
771
|
+
* a pipelined burst go out in one write. `wait` calls it before it blocks; a
|
|
772
|
+
* program that blocks elsewhere, as `nish/net/serve` does, calls it first.
|
|
773
|
+
*/
|
|
774
|
+
flush(): void {
|
|
775
|
+
const n: i32 = this.owedCount
|
|
776
|
+
this.owedCount = 0
|
|
777
|
+
for (let k: i32 = 0; k < n; k++) {
|
|
778
|
+
const slot: i32 = this.owed[k]
|
|
779
|
+
this.owing[slot] = false
|
|
780
|
+
if (this.open[slot] && this.server.holds(slot)) {
|
|
781
|
+
this.settle(slot, this.server.connection(slot))
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* How many times the handler handed a connection's waiting output to its
|
|
788
|
+
* socket: one per connection per pass, however many answers it holds.
|
|
789
|
+
* @returns the count of sends since the handler was made, each one write or a few for a partial one
|
|
790
|
+
*/
|
|
791
|
+
sends(): i64 {
|
|
792
|
+
return this.sent
|
|
793
|
+
}
|
|
794
|
+
|
|
735
795
|
/**
|
|
736
796
|
* Takes `slot`'s events until it needs the socket again, then sends what it
|
|
737
|
-
* holds and re-arms it, or closes it once it is over.
|
|
797
|
+
* holds and re-arms it, or closes it once it is over. While `owable` allows,
|
|
798
|
+
* the send waits for `flush`, so the answers behind this one go out with it.
|
|
738
799
|
*/
|
|
739
800
|
private drive(slot: i32): void {
|
|
740
801
|
const conn: Http1Connection = this.server.connection(slot)
|
|
@@ -755,7 +816,46 @@ export class HttpHandler {
|
|
|
755
816
|
// The carrier has written its refusal and ends the connection; a
|
|
756
817
|
// request it was reading is nobody's.
|
|
757
818
|
this.releaseReading(slot)
|
|
819
|
+
} else if (this.owable(slot, conn)) {
|
|
820
|
+
if (!this.owing[slot]) {
|
|
821
|
+
this.owing[slot] = true
|
|
822
|
+
this.owed[this.owedCount] = slot
|
|
823
|
+
this.owedCount += 1
|
|
824
|
+
}
|
|
825
|
+
return
|
|
826
|
+
}
|
|
827
|
+
this.settle(slot, conn)
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Whether `slot`'s send can wait for `flush`: `deferSends` turned that on, it has output to send, the
|
|
832
|
+
* output is still roomy, so more answers fit behind it, and no request on
|
|
833
|
+
* it is parked waiting for the room a send makes. A connection with nothing
|
|
834
|
+
* to send is settled at once, so a hang-up closes in the pass that reads it.
|
|
835
|
+
* Nor does a held write wait: its `H1_WRITE` comes only from `next`, so a
|
|
836
|
+
* send put off until the peer has read would empty the output, arm the slot
|
|
837
|
+
* for reading alone, and leave the rest of the response unsent.
|
|
838
|
+
* `flush` runs before every poll, so a send put off here goes out before
|
|
839
|
+
* the handler waits.
|
|
840
|
+
*/
|
|
841
|
+
private owable(slot: i32, conn: Http1Connection): boolean {
|
|
842
|
+
const at: i32 = this.current[slot]
|
|
843
|
+
return (
|
|
844
|
+
this.deferring &&
|
|
845
|
+
!this.draining &&
|
|
846
|
+
conn.wantsWrite() &&
|
|
847
|
+
!conn.writeHeld &&
|
|
848
|
+
this.roomy(conn) &&
|
|
849
|
+
(at < 0 || this.requests[at].state !== HANDLER_PARKED)
|
|
850
|
+
)
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/** Sends what `slot` holds and re-arms it, or closes it once it is over; enqueues a parked request once there is room. */
|
|
854
|
+
private settle(slot: i32, conn: Http1Connection): void {
|
|
855
|
+
if (conn.wantsWrite()) {
|
|
856
|
+
this.sent += 1
|
|
758
857
|
}
|
|
858
|
+
// `flush` arms a held write for writing even once its send emptied the output.
|
|
759
859
|
const wants: i32 = this.server.flush(slot)
|
|
760
860
|
if ((wants & H1_DONE) !== 0) {
|
|
761
861
|
this.closeSlot(slot)
|
|
@@ -792,23 +892,18 @@ export class HttpHandler {
|
|
|
792
892
|
r.head = new Array<u8>(handlerGrown(toI32(r.head.length), length))
|
|
793
893
|
}
|
|
794
894
|
copyBytes(r.head, HANDLER_ZERO, p.head, HANDLER_ZERO, length)
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
for (let k: i32 = 0; k < spanCount; k++) {
|
|
800
|
-
r.spans[k] = p.spans[k]
|
|
895
|
+
// The parser's spans are copied whole, as one `set`: only the first
|
|
896
|
+
// `4 * headerCount` are this head's, and only those are ever read.
|
|
897
|
+
if (toI32(r.spans.length) < toI32(p.spans.length)) {
|
|
898
|
+
r.spans = new Array<i32>(handlerGrown(toI32(r.spans.length), toI32(p.spans.length)))
|
|
801
899
|
}
|
|
900
|
+
r.spans.set(p.spans)
|
|
901
|
+
r.headLength = length
|
|
802
902
|
r.headerCount = p.headerCount
|
|
803
903
|
r.methodStart = p.methodStart
|
|
804
904
|
r.methodEnd = p.methodEnd
|
|
805
905
|
r.targetStart = p.targetStart
|
|
806
906
|
r.targetEnd = p.targetEnd
|
|
807
|
-
let pathEnd: i32 = r.targetStart
|
|
808
|
-
while (pathEnd < r.targetEnd && toI32(r.head[pathEnd]) !== 63) {
|
|
809
|
-
pathEnd += 1
|
|
810
|
-
}
|
|
811
|
-
r.pathEnd = pathEnd
|
|
812
907
|
r.bodyLength = 0
|
|
813
908
|
r.slot = slot
|
|
814
909
|
r.gen = this.slotGen[slot]
|
|
@@ -817,6 +912,8 @@ export class HttpHandler {
|
|
|
817
912
|
r.pendingStart = 0
|
|
818
913
|
r.pendingEnd = 0
|
|
819
914
|
this.route(r)
|
|
915
|
+
// The router found where the path ends, before any query, as it matched.
|
|
916
|
+
r.pathEnd = this.router.pathEnd
|
|
820
917
|
this.current[slot] = r.index
|
|
821
918
|
}
|
|
822
919
|
|
|
@@ -1155,6 +1252,21 @@ export class HttpHandler {
|
|
|
1155
1252
|
* @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed first
|
|
1156
1253
|
*/
|
|
1157
1254
|
write(r: HttpRequest, buf: u8[], off: i32, len: i32): i32 {
|
|
1255
|
+
const put: i32 = this.put(r, buf, off, len)
|
|
1256
|
+
if (put === HANDLER_SENT) {
|
|
1257
|
+
this.drive(r.slot)
|
|
1258
|
+
}
|
|
1259
|
+
return put
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/**
|
|
1263
|
+
* `write` without its pass over the connection: the bytes are in the
|
|
1264
|
+
* carrier's output or pending on the request, and nothing is sent or
|
|
1265
|
+
* re-armed. The caller owes the slot a `drive` — `write` gives it at once,
|
|
1266
|
+
* `sendFields` leaves it to the `end` that follows, so a whole answer takes
|
|
1267
|
+
* one pass instead of two.
|
|
1268
|
+
*/
|
|
1269
|
+
private put(r: HttpRequest, buf: u8[], off: i32, len: i32): i32 {
|
|
1158
1270
|
if (r.state !== HANDLER_STREAMING) {
|
|
1159
1271
|
panic(`HttpHandler.write: the response is not started (state ${r.state})`)
|
|
1160
1272
|
}
|
|
@@ -1187,7 +1299,6 @@ export class HttpHandler {
|
|
|
1187
1299
|
if (taken < len) {
|
|
1188
1300
|
this.keep(r, buf, off + taken, len - taken)
|
|
1189
1301
|
}
|
|
1190
|
-
this.drive(r.slot)
|
|
1191
1302
|
return HANDLER_SENT
|
|
1192
1303
|
}
|
|
1193
1304
|
|
|
@@ -1213,7 +1324,9 @@ export class HttpHandler {
|
|
|
1213
1324
|
/**
|
|
1214
1325
|
* Ends `r`'s response. `r` is the handler's again: it goes back once the
|
|
1215
1326
|
* carrier has taken every byte, and the connection's next request is read.
|
|
1216
|
-
* Answers `HANDLER_SENT` or `HANDLER_DROPPED`.
|
|
1327
|
+
* Answers `HANDLER_SENT` or `HANDLER_DROPPED`. After `deferSends` the send
|
|
1328
|
+
* may be put off so a pipelined burst goes out in one write: call `wait` or
|
|
1329
|
+
* `flush` after answering, or it waits for the next of them.
|
|
1217
1330
|
* @param r a request whose response `start` began; the handler's again after this call
|
|
1218
1331
|
* @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed first
|
|
1219
1332
|
*/
|
|
@@ -1281,7 +1394,8 @@ export class HttpHandler {
|
|
|
1281
1394
|
return HANDLER_DROPPED
|
|
1282
1395
|
}
|
|
1283
1396
|
if (len > 0 && status !== 204 && status !== 304) {
|
|
1284
|
-
|
|
1397
|
+
// No pass between the body and the end: `end` drives the slot once.
|
|
1398
|
+
this.put(r, body, off, len)
|
|
1285
1399
|
}
|
|
1286
1400
|
return this.end(r)
|
|
1287
1401
|
}
|