@evalution/sdk 0.0.0-stage → 2.0.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.
@@ -0,0 +1,84 @@
1
+ //#region src/portkit.ts
2
+ const SERVICES_URL = "https://portkit.localhost/services";
3
+ /** How long to wait for portkit to answer; Caddy may be starting. */
4
+ const DISCOVERY_TIMEOUT_MS = 1e3;
5
+ /**
6
+ * How long to wait for a response when the caller sets no limit. Just over the
7
+ * 60 s portkit holds a request while an on-demand service starts.
8
+ */
9
+ const REQUEST_TIMEOUT_MS = 65e3;
10
+ /**
11
+ * Asks portkit for the evalution instance serving this directory or one of
12
+ * its parents. It never contacts the service itself, which would start one
13
+ * that's on demand.
14
+ *
15
+ * Resolves to `undefined` when portkit isn't installed — without its CA
16
+ * certificate no request is made — or has no such service, or doesn't answer
17
+ * in time.
18
+ */
19
+ async function discoverPortkit({ env = process.env, cwd = process.cwd(), servicesUrl = SERVICES_URL } = {}) {
20
+ try {
21
+ const send = localTlsFetch(await readPortkitCa(env));
22
+ const res = await send(`${servicesUrl}?${new URLSearchParams({
23
+ label: "kind=evalution",
24
+ cwd
25
+ })}`, { signal: AbortSignal.timeout(DISCOVERY_TIMEOUT_MS) });
26
+ if (!res.ok) return void 0;
27
+ const { services } = await res.json();
28
+ const endpoint = services?.[0]?.url;
29
+ return endpoint ? {
30
+ endpoint,
31
+ fetch: send
32
+ } : void 0;
33
+ } catch {
34
+ return;
35
+ }
36
+ }
37
+ async function readPortkitCa(env) {
38
+ const [{ readFile }, { homedir }, { join }] = await Promise.all([
39
+ import("node:fs/promises"),
40
+ import("node:os"),
41
+ import("node:path")
42
+ ]);
43
+ return readFile(join(env.PORTKIT_HOME || (process.platform === "darwin" ? join(homedir(), "Library", "Application Support", "portkit") : join(homedir(), ".local", "share", "portkit")), "data", "caddy", "pki", "authorities", "local", "root.crt"), "utf8");
44
+ }
45
+ /**
46
+ * A `fetch` for portkit's `*.localhost` names. It connects to loopback whatever
47
+ * the name, since not every system resolves `*.localhost`, and trusts `ca` for
48
+ * these connections only.
49
+ *
50
+ * Takes what this package sends: a URL, and a string body.
51
+ */
52
+ function localTlsFetch(ca) {
53
+ return async (input, init) => {
54
+ const { request } = await import("node:https");
55
+ const url = new URL(String(input));
56
+ return new Promise((resolve, reject) => {
57
+ const req = request({
58
+ host: "127.0.0.1",
59
+ port: url.port || 443,
60
+ servername: url.hostname,
61
+ path: url.pathname + url.search,
62
+ method: init?.method ?? "GET",
63
+ headers: {
64
+ ...Object.fromEntries(new Headers(init?.headers)),
65
+ host: url.host
66
+ },
67
+ ca,
68
+ signal: init?.signal ?? AbortSignal.timeout(REQUEST_TIMEOUT_MS)
69
+ }, (res) => {
70
+ const chunks = [];
71
+ res.on("data", (chunk) => chunks.push(chunk));
72
+ res.on("error", reject);
73
+ res.on("end", () => {
74
+ const body = chunks.length > 0 ? Buffer.concat(chunks) : null;
75
+ resolve(new Response(body, { status: res.statusCode }));
76
+ });
77
+ });
78
+ req.on("error", reject);
79
+ req.end(init?.body);
80
+ });
81
+ };
82
+ }
83
+ //#endregion
84
+ export { discoverPortkit };