@northtalk/ts 0.5.0 → 0.6.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/README.md +11 -0
- package/base64-CDO-a06R.js +29 -0
- package/{binary-value-B3knLdHD.d.ts → binary-value-C4ZiXVjv.d.ts} +1 -1
- package/{checker-DMI4LiRm.js → checker-CUhn2XaM.js} +4 -4
- package/{core-D_9OuTRE.d.ts → core-egHbNmve.d.ts} +1 -1
- package/{debug-nN5tQ_p4.d.ts → debug--Of4MSLw.d.ts} +35 -11
- package/debug.d.ts +1 -1
- package/debug.js +1 -1
- package/{driver-Bf-7Roq6.d.ts → driver-BIm1mL77.d.ts} +1 -1
- package/driver.d.ts +1 -1
- package/driver.js +1 -1
- package/{encoding-C3Q7pIC9.js → encoding-RUuES_e4.js} +2 -2
- package/{engine-BpdGfAND.js → engine-CXf5FCwi.js} +6 -6
- package/{engine-CQqt4rTB.d.ts → engine-DbDcopGM.d.ts} +2 -2
- package/{errors-BKJES3Zu.js → errors-D11zizZg.js} +1580 -1481
- package/{group-Bz2wh72R.js → group-BpzTo0k4.js} +84 -40
- package/http-fetch-DFwVIOQg.js +517 -0
- package/http.d.ts +39 -2
- package/http.js +190 -7
- package/index.d.ts +10 -660
- package/index.js +36 -49
- package/{inline-grants-Cx0Z9XAt.js → inline-grants-XbX9HL_w.js} +72 -304
- package/lean-DiS9rUcn.d.ts +741 -0
- package/lean-aCvN6y52.js +47 -0
- package/lean.d.ts +8 -0
- package/lean.js +15 -0
- package/{manifest-Dw-8Unmf.js → manifest-CCfpn0kC.js} +3 -4
- package/{manifest-DYAbIyQm.d.ts → manifest-mpivREn-.d.ts} +2 -2
- package/message-layer.d.ts +2 -2
- package/message-layer.js +2 -2
- package/notify.d.ts +1 -1
- package/notify.js +1 -1
- package/{objects-BRcpeXwZ.d.ts → objects-DYq_YvaQ.d.ts} +5 -1
- package/{objects-BC4fS0vq.js → objects-DmUc0lha.js} +1 -1
- package/{base64-Dtqaz_0A.js → operations-DnCcztkZ.js} +1632 -32
- package/package.json +36 -2
- package/{readers-L90Hoa97.d.ts → readers-8WCimMop.d.ts} +1 -1
- package/{readers-B5e5bBrY.js → readers-CsvlahLi.js} +2 -2
- package/{request-pump-binary-BjnyqXV5.js → request-pump-binary-oqjWFqZA.js} +5 -4
- package/{router-CBuMIl6i.d.ts → router-BQsO8aPV.d.ts} +2 -2
- package/{router-BCHEfs4x.js → router-BZ-aYeXA.js} +4 -4
- package/session.d.ts +2 -2
- package/session.js +55 -16
- package/setup.d.ts +1 -1
- package/setup.js +4 -4
- package/sqlite.d.ts +2 -2
- package/sqlite.js +5 -5
- package/stdlib/bytes.d.ts +6 -0
- package/stdlib/bytes.js +11 -0
- package/stdlib/date.d.ts +6 -0
- package/stdlib/date.js +11 -0
- package/stdlib/json.d.ts +6 -0
- package/stdlib/json.js +11 -0
- package/stdlib/list.d.ts +6 -0
- package/stdlib/list.js +10 -0
- package/stdlib/map.d.ts +6 -0
- package/stdlib/map.js +10 -0
- package/stdlib/text.d.ts +6 -0
- package/stdlib/text.js +10 -0
- package/stdlib/units.d.ts +6 -0
- package/stdlib/units.js +10 -0
- package/stdlib-registry-BwitI3Ox.d.ts +18 -0
- package/{store-DTQa0lyE.js → store-DHlo6fP5.js} +2 -2
- package/{store-capability-BxXgiRM7.js → store-capability-BrUUAwVS.js} +3 -4
- package/store.d.ts +2 -2
- package/store.js +2 -2
- package/{user-capability-5-IvCeZI.js → user-capability-BcRQyyEw.js} +4 -5
- package/wasm.d.ts +8 -6
- package/wasm.js +19 -15
- package/capabilities-Bi5Qm6Fr.js +0 -1320
package/http.js
CHANGED
|
@@ -1,9 +1,187 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues, N as map, Q as text, R as num, d as bytes, i as ScriptError, t as HostError, u as bool } from "./errors-
|
|
3
|
-
import { t as defineObjectKind } from "./objects-
|
|
4
|
-
import {
|
|
2
|
+
import { A as listValues, N as map, Q as text, R as num, d as bytes, i as ScriptError, t as HostError, u as bool } from "./errors-D11zizZg.js";
|
|
3
|
+
import { t as defineObjectKind } from "./objects-DmUc0lha.js";
|
|
4
|
+
import { xt as shape } from "./operations-DnCcztkZ.js";
|
|
5
|
+
import { a as parseFetchUrl, i as fetchOrigin } from "./http-fetch-DFwVIOQg.js";
|
|
5
6
|
import { Buffer } from "node:buffer";
|
|
6
|
-
import { createServer } from "node:http";
|
|
7
|
+
import { createServer, request } from "node:http";
|
|
8
|
+
import { request as request$1 } from "node:https";
|
|
9
|
+
//#region src/http/fetch.ts
|
|
10
|
+
/** The largest response body `fetch` reads by default: 8 MiB. */
|
|
11
|
+
const HTTP_FETCH_DEFAULT_MAX_BODY_BYTES = 8388608;
|
|
12
|
+
/** The most response header bytes `fetch` reads by default: 64 KiB. */
|
|
13
|
+
const HTTP_FETCH_DEFAULT_MAX_HEADER_BYTES = 65536;
|
|
14
|
+
/** The origins as `scheme://host:port`, or throws `invalid value`. */
|
|
15
|
+
const httpFetchOrigins = (origins) => {
|
|
16
|
+
if (origins === "any") return "any";
|
|
17
|
+
if (!Array.isArray(origins)) throw new HostError("invalid value", "A fetch binding's origins are 'any' or a list of origins");
|
|
18
|
+
const out = /* @__PURE__ */ new Set();
|
|
19
|
+
for (const origin of origins) {
|
|
20
|
+
const u = typeof origin === "string" && !/:\/\/[^]*[#/?]/.test(origin) ? parseFetchUrl(origin) : void 0;
|
|
21
|
+
if (!u) throw new HostError("invalid value", `Not an origin: ${String(origin)}`);
|
|
22
|
+
out.add(fetchOrigin(u));
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
const failure = (code, message, data) => new ScriptError(code, message, data);
|
|
27
|
+
const responseHeaders = (raw) => {
|
|
28
|
+
const headers = /* @__PURE__ */ new Map();
|
|
29
|
+
for (let i = 0; i + 1 < raw.length; i += 2) {
|
|
30
|
+
const name = raw[i].toLowerCase();
|
|
31
|
+
headers.set(name, [...headers.get(name) ?? [], raw[i + 1]]);
|
|
32
|
+
}
|
|
33
|
+
return map([...headers].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, vs]) => [k, k === "set-cookie" ? listValues(vs.map((v) => text(v))) : text(vs.join(", "))]));
|
|
34
|
+
};
|
|
35
|
+
const headerBytes = (raw) => {
|
|
36
|
+
let n = 0;
|
|
37
|
+
for (let i = 0; i + 1 < raw.length; i += 2) n += Buffer.byteLength(raw[i]) + Buffer.byteLength(raw[i + 1]) + 4;
|
|
38
|
+
return n;
|
|
39
|
+
};
|
|
40
|
+
/** `fetch` for `httpCapability`, alone or beside the router's server Operations. */
|
|
41
|
+
const createHttpFetch = (o = {}) => {
|
|
42
|
+
const fixed = typeof o.origins === "function" ? void 0 : httpFetchOrigins(o.origins ?? []);
|
|
43
|
+
const maxBody = o.maxBodyBytes ?? 8388608;
|
|
44
|
+
const maxHeader = o.maxHeaderBytes ?? 65536;
|
|
45
|
+
for (const [name, n] of [["maxBodyBytes", maxBody], ["maxHeaderBytes", maxHeader]]) if (!Number.isSafeInteger(n) || n < 0) throw new HostError("invalid value", `${name} must be a whole number`);
|
|
46
|
+
const log = o.log ?? (() => {});
|
|
47
|
+
const allowed = (binding, origin) => {
|
|
48
|
+
let set = fixed;
|
|
49
|
+
if (!set) {
|
|
50
|
+
const given = o.origins(binding);
|
|
51
|
+
set = httpFetchOrigins(given ?? []);
|
|
52
|
+
}
|
|
53
|
+
return set === "any" || set.has(origin);
|
|
54
|
+
};
|
|
55
|
+
return { fetch: (call, url, request$2) => {
|
|
56
|
+
const u = parseFetchUrl(url);
|
|
57
|
+
const origin = fetchOrigin(u);
|
|
58
|
+
let ok;
|
|
59
|
+
try {
|
|
60
|
+
ok = allowed(call.binding, origin);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
log(`fetch ${url}: ${error instanceof Error ? error.message : error}`);
|
|
63
|
+
ok = false;
|
|
64
|
+
}
|
|
65
|
+
if (!ok) {
|
|
66
|
+
call.fail(failure("bad address", `${origin} isn't allowed`));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const method = request$2.get("method").asText();
|
|
70
|
+
const headers = {};
|
|
71
|
+
const named = /* @__PURE__ */ new Set();
|
|
72
|
+
for (const [name, value] of request$2.get("headers").entries()) {
|
|
73
|
+
named.add(name.toLowerCase());
|
|
74
|
+
headers[name] = value.kind === "text" ? value.asText() : Array.from({ length: value.length }, (_, i) => value.index(i + 1).asText());
|
|
75
|
+
}
|
|
76
|
+
let body;
|
|
77
|
+
const given = request$2.get("body");
|
|
78
|
+
const json = request$2.get("json");
|
|
79
|
+
if (given.kind === "text") {
|
|
80
|
+
body = Buffer.from(given.asText(), "utf8");
|
|
81
|
+
if (!named.has("content-type")) headers["content-type"] = "text/plain; charset=utf-8";
|
|
82
|
+
} else if (given.kind === "bytes") body = given.asBytes();
|
|
83
|
+
else if (json.kind === "text") {
|
|
84
|
+
body = Buffer.from(json.asText(), "utf8");
|
|
85
|
+
if (!named.has("content-type")) headers["content-type"] = "application/json";
|
|
86
|
+
}
|
|
87
|
+
if (!named.has("accept-encoding")) headers["accept-encoding"] = "identity";
|
|
88
|
+
if (body) headers["content-length"] = String(body.length);
|
|
89
|
+
let settled = false;
|
|
90
|
+
let req;
|
|
91
|
+
const abort = () => {
|
|
92
|
+
settled = true;
|
|
93
|
+
req?.destroy();
|
|
94
|
+
};
|
|
95
|
+
const settle = (f) => {
|
|
96
|
+
if (settled) return;
|
|
97
|
+
settled = true;
|
|
98
|
+
call.signal.removeEventListener("abort", abort);
|
|
99
|
+
f();
|
|
100
|
+
req?.destroy();
|
|
101
|
+
};
|
|
102
|
+
const networkError = (why) => settle(() => {
|
|
103
|
+
log(`fetch ${url}: ${why}`);
|
|
104
|
+
call.fail(failure("network error", why));
|
|
105
|
+
});
|
|
106
|
+
const tooLarge = (field, limit) => settle(() => call.fail(failure("response too large", `The response's ${field} pass ${limit} bytes`, map([["field", text(field)], ["limit", num(limit)]]))));
|
|
107
|
+
if (call.signal.aborted) return;
|
|
108
|
+
call.signal.addEventListener("abort", abort, { once: true });
|
|
109
|
+
const onResponse = (res) => {
|
|
110
|
+
const status = res.statusCode ?? 0;
|
|
111
|
+
if (status < 200 || status > 599) {
|
|
112
|
+
networkError(`status ${status}`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
let seen = headerBytes(res.rawHeaders);
|
|
116
|
+
if (seen > maxHeader) {
|
|
117
|
+
tooLarge("headers", maxHeader);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const chunks = [];
|
|
121
|
+
let size = 0;
|
|
122
|
+
res.on("data", (chunk) => {
|
|
123
|
+
size += chunk.length;
|
|
124
|
+
if (size > maxBody) {
|
|
125
|
+
tooLarge("body", maxBody);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
chunks.push(chunk);
|
|
129
|
+
});
|
|
130
|
+
res.on("end", () => {
|
|
131
|
+
seen += headerBytes(res.rawTrailers);
|
|
132
|
+
if (seen > maxHeader) {
|
|
133
|
+
tooLarge("headers", maxHeader);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!res.complete) {
|
|
137
|
+
networkError("the response ended early");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
settle(() => call.answer(map([
|
|
141
|
+
["status", num(status)],
|
|
142
|
+
["headers", responseHeaders(res.rawHeaders)],
|
|
143
|
+
["body", bytes(new Uint8Array(Buffer.concat(chunks)))]
|
|
144
|
+
])));
|
|
145
|
+
});
|
|
146
|
+
res.on("aborted", () => networkError("the response was cut off"));
|
|
147
|
+
res.on("error", (e) => networkError(e.message));
|
|
148
|
+
res.on("close", () => {
|
|
149
|
+
if (!res.complete) networkError("the connection closed early");
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
const target = `${u.path}${u.query === void 0 ? "" : `?${u.query}`}`;
|
|
153
|
+
const hostname = u.host.startsWith("[") ? u.host.slice(1, -1) : u.host;
|
|
154
|
+
try {
|
|
155
|
+
req = (u.scheme === "https" ? request$1 : request)({
|
|
156
|
+
hostname,
|
|
157
|
+
port: u.port,
|
|
158
|
+
path: target,
|
|
159
|
+
method,
|
|
160
|
+
headers,
|
|
161
|
+
agent: false,
|
|
162
|
+
maxHeaderSize: maxHeader + 16384,
|
|
163
|
+
...u.scheme === "https" ? {
|
|
164
|
+
rejectUnauthorized: true,
|
|
165
|
+
...o.ca ? { ca: o.ca } : {}
|
|
166
|
+
} : {}
|
|
167
|
+
});
|
|
168
|
+
} catch (error) {
|
|
169
|
+
networkError(error instanceof Error ? error.message : String(error));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
req.on("response", onResponse);
|
|
173
|
+
req.on("upgrade", (_res, socket) => {
|
|
174
|
+
socket.destroy();
|
|
175
|
+
networkError("the server switched protocols");
|
|
176
|
+
});
|
|
177
|
+
req.on("error", (e) => {
|
|
178
|
+
if (e.code === "HPE_HEADER_OVERFLOW") tooLarge("headers", maxHeader);
|
|
179
|
+
else networkError(e.message);
|
|
180
|
+
});
|
|
181
|
+
req.end(body);
|
|
182
|
+
} };
|
|
183
|
+
};
|
|
184
|
+
//#endregion
|
|
7
185
|
//#region src/http/router.ts
|
|
8
186
|
/** The largest request body the router reads by default: 1 MiB. */
|
|
9
187
|
const HTTP_DEFAULT_MAX_BODY_BYTES = 1048576;
|
|
@@ -383,7 +561,7 @@ const createHttpHost = (o = {}) => {
|
|
|
383
561
|
break;
|
|
384
562
|
}
|
|
385
563
|
case "unhandled": {
|
|
386
|
-
const x = byDelivery?.get(report.delivery);
|
|
564
|
+
const x = report.delivery && byDelivery?.get(report.delivery);
|
|
387
565
|
if (x) settle(x, 404);
|
|
388
566
|
break;
|
|
389
567
|
}
|
|
@@ -441,7 +619,12 @@ const createHttpHost = (o = {}) => {
|
|
|
441
619
|
await Promise.all(closing);
|
|
442
620
|
};
|
|
443
621
|
return {
|
|
444
|
-
impl: {
|
|
622
|
+
impl: o.fetch ? {
|
|
623
|
+
serve,
|
|
624
|
+
unserve,
|
|
625
|
+
respond,
|
|
626
|
+
...createHttpFetch(o.fetch)
|
|
627
|
+
} : {
|
|
445
628
|
serve,
|
|
446
629
|
unserve,
|
|
447
630
|
respond
|
|
@@ -462,4 +645,4 @@ const createHttpHost = (o = {}) => {
|
|
|
462
645
|
};
|
|
463
646
|
};
|
|
464
647
|
//#endregion
|
|
465
|
-
export { HTTP_DEFAULT_MAX_BODY_BYTES, createHttpHost, requestFields };
|
|
648
|
+
export { HTTP_DEFAULT_MAX_BODY_BYTES, HTTP_FETCH_DEFAULT_MAX_BODY_BYTES, HTTP_FETCH_DEFAULT_MAX_HEADER_BYTES, createHttpFetch, createHttpHost, httpFetchOrigins, requestFields };
|