camstack 1.2.213 → 1.2.215

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,56 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/http/hub-http.ts
4
+ import * as http from "http";
5
+ import * as https from "https";
6
+ import { Buffer } from "buffer";
7
+ import { createHubAgent, explainTlsFailure, resolveHubTls } from "@camstack/develop";
8
+ var DEFAULT_TIMEOUT_MS = 3e4;
9
+ function hubRequest(url, opts = {}) {
10
+ const parsed = new URL(url);
11
+ const isHttps = parsed.protocol === "https:";
12
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
13
+ return new Promise((resolve, reject) => {
14
+ const requestOptions = {
15
+ protocol: parsed.protocol,
16
+ hostname: parsed.hostname,
17
+ port: parsed.port || (isHttps ? 443 : 80),
18
+ path: `${parsed.pathname}${parsed.search}`,
19
+ method: opts.method ?? "GET",
20
+ headers: { ...opts.headers },
21
+ timeout: timeoutMs,
22
+ ...isHttps ? { agent: createHubAgent(resolveHubTls(parsed.origin, opts.env, opts.sessionDir)) } : {}
23
+ };
24
+ const onResponse = (res) => {
25
+ const chunks = [];
26
+ res.on("data", (chunk) => chunks.push(chunk));
27
+ res.on("end", () => {
28
+ const buffer = Buffer.concat(chunks);
29
+ const status = res.statusCode ?? 0;
30
+ resolve({
31
+ ok: status >= 200 && status < 300,
32
+ status,
33
+ statusText: res.statusMessage ?? "",
34
+ headers: res.headers,
35
+ text: () => Promise.resolve(buffer.toString("utf8")),
36
+ json: () => Promise.resolve().then(() => JSON.parse(buffer.toString("utf8")))
37
+ });
38
+ });
39
+ res.on("error", reject);
40
+ };
41
+ const req = isHttps ? https.request(requestOptions, onResponse) : http.request(requestOptions, onResponse);
42
+ req.on("error", (err) => {
43
+ const explained = isHttps ? explainTlsFailure(`${parsed.protocol}//${parsed.host}`, err) : null;
44
+ reject(explained === null ? err : new Error(explained, { cause: err }));
45
+ });
46
+ req.on("timeout", () => {
47
+ req.destroy(new Error(`Hub request to ${parsed.host} timed out after ${timeoutMs}ms`));
48
+ });
49
+ if (opts.body !== void 0) req.write(opts.body);
50
+ req.end();
51
+ });
52
+ }
53
+
54
+ export {
55
+ hubRequest
56
+ };
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/discovery/hub-finder.ts
4
+ import * as fs from "fs";
5
+ import * as path from "path";
6
+ import {
7
+ defaultSessionDir,
8
+ isSessionFile,
9
+ resolveHubUrl,
10
+ validateHubAddress
11
+ } from "@camstack/develop";
12
+ import {
13
+ discoverHubs
14
+ } from "@camstack/develop/hub-discovery";
15
+ function validateManualHubUrl(value) {
16
+ return validateHubAddress(value);
17
+ }
18
+ function knownHubCandidates(deps) {
19
+ const dir = deps.sessionDir ?? defaultSessionDir();
20
+ const env = deps.env ?? process.env;
21
+ const out = [];
22
+ let files = [];
23
+ try {
24
+ files = fs.readdirSync(dir);
25
+ } catch {
26
+ files = [];
27
+ }
28
+ for (const file of files) {
29
+ if (file.endsWith(".json")) {
30
+ try {
31
+ const parsed = JSON.parse(fs.readFileSync(path.join(dir, file), "utf8"));
32
+ if (isSessionFile(parsed)) {
33
+ out.push({
34
+ url: parsed.server,
35
+ source: `saved session ${parsed.username}`
36
+ });
37
+ }
38
+ } catch {
39
+ }
40
+ }
41
+ }
42
+ const fromEnv = resolveHubUrl({ env, session: null });
43
+ if (fromEnv) {
44
+ const name = (env["CAMSTACK_HUB_URL"] ?? "").trim() === "" ? "CAMSTACK_SERVER" : "CAMSTACK_HUB_URL";
45
+ out.push({ url: fromEnv.url, source: name });
46
+ }
47
+ if (deps.workspaceRoot !== void 0) {
48
+ const scratch = resolveHubUrl({
49
+ env: {},
50
+ workspaceRoot: deps.workspaceRoot,
51
+ session: null
52
+ });
53
+ if (scratch) out.push({ url: scratch.url, source: "scratchpad/.hub-url" });
54
+ }
55
+ return out;
56
+ }
57
+ async function findHubs(deps = {}) {
58
+ return discoverHubs({
59
+ known: knownHubCandidates(deps),
60
+ ...deps.browse ? { browse: deps.browse } : {},
61
+ ...deps.probe ? { probe: deps.probe } : {},
62
+ ...deps.mdnsTimeoutMs !== void 0 ? { mdnsTimeoutMs: deps.mdnsTimeoutMs } : {},
63
+ ...deps.onProblem ? { onProblem: deps.onProblem } : {}
64
+ });
65
+ }
66
+ function describeHub(hub) {
67
+ const identity = [hub.nodeId, hub.version === null ? null : `v${hub.version}`].filter((p) => p !== null).join(" ");
68
+ return `${hub.url}${identity === "" ? "" : ` ${identity}`} (${hub.sources.join(", ")})`;
69
+ }
70
+
71
+ export {
72
+ validateManualHubUrl,
73
+ findHubs,
74
+ describeHub
75
+ };