@userreason/wizard 0.0.0-stage → 0.2.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.
@@ -0,0 +1,115 @@
1
+ import { createRequire as createWizardRequire } from 'node:module'; import { fileURLToPath as wizardFileURLToPath } from 'node:url'; import { dirname as wizardDirname } from 'node:path'; const require = createWizardRequire(import.meta.url); const __filename = wizardFileURLToPath(import.meta.url); const __dirname = wizardDirname(__filename);
2
+ import {
3
+ detectInstallation
4
+ } from "./chunk-6DCCEVPD.js";
5
+ import {
6
+ DEFAULT_API
7
+ } from "./chunk-YMSGDC4B.js";
8
+ import {
9
+ apiUrl,
10
+ configDirectory,
11
+ uuid
12
+ } from "./chunk-GGARVSPT.js";
13
+
14
+ // src/state.ts
15
+ import { mkdir, readFile, writeFile, rename, rm, realpath, lstat } from "node:fs/promises";
16
+ import { join } from "node:path";
17
+ import { createHash, randomUUID } from "node:crypto";
18
+ function storageBlocked(error) {
19
+ if (!error || typeof error !== "object") return false;
20
+ const value = error;
21
+ return ["ERR_ACCESS_DENIED", "EACCES", "EPERM", "EROFS"].includes(value.code ?? "") || value.cause !== void 0 && storageBlocked(value.cause);
22
+ }
23
+ function parse(value) {
24
+ if (typeof value.siteId !== "string" || value.workspaceId !== void 0 && typeof value.workspaceId !== "string" || value.apiBaseUrl !== void 0 && typeof value.apiBaseUrl !== "string") throw new Error("Invalid saved SDK configuration");
25
+ return {
26
+ siteId: uuid(value.siteId),
27
+ apiBaseUrl: apiUrl(value.apiBaseUrl ?? DEFAULT_API),
28
+ ...value.workspaceId !== void 0 ? { workspaceId: uuid(value.workspaceId) } : {}
29
+ };
30
+ }
31
+ async function read(path) {
32
+ try {
33
+ const info = await lstat(path);
34
+ if (!info.isFile() || info.isSymbolicLink() || info.size > 4096) throw new Error("Invalid saved installation file");
35
+ const value = JSON.parse(await readFile(path, "utf8"));
36
+ if (value.schemaVersion !== 1) throw new Error("Unsupported saved installation");
37
+ return parse(value);
38
+ } catch (error) {
39
+ if (error.code === "ENOENT") return;
40
+ throw new Error("Unable to read saved UserReason installation. Review it before rerunning.", { cause: error });
41
+ }
42
+ }
43
+ async function statePath(directory, api) {
44
+ const key = createHash("sha256").update(JSON.stringify([await realpath(directory), api])).digest("hex");
45
+ return join(configDirectory(), "wizard", `${key}.json`);
46
+ }
47
+ async function savedInstallation(project, api, onStorageBlocked) {
48
+ const detected = await detectInstallation(project);
49
+ if (detected) return parse(detected);
50
+ const path = await statePath(project.directory, api);
51
+ let saved;
52
+ try {
53
+ saved = await read(path);
54
+ } catch (error) {
55
+ if (!storageBlocked(error)) throw error;
56
+ onStorageBlocked?.();
57
+ }
58
+ return saved ?? await read(join(project.directory, "userreason.config.json"));
59
+ }
60
+ async function saveInstallation(directory, config) {
61
+ const path = await statePath(directory, config.apiBaseUrl);
62
+ const value = JSON.stringify({ schemaVersion: 1, ...parse(config) }) + "\n";
63
+ const temporary = `${path}.${randomUUID()}.tmp`;
64
+ let created = false;
65
+ try {
66
+ await mkdir(join(configDirectory(), "wizard"), { recursive: true, mode: 448 });
67
+ await writeFile(temporary, value, { flag: "wx", mode: 384 });
68
+ created = true;
69
+ await rename(temporary, path);
70
+ created = false;
71
+ return true;
72
+ } catch (error) {
73
+ if (storageBlocked(error)) return false;
74
+ throw error;
75
+ } finally {
76
+ if (created) {
77
+ try {
78
+ await rm(temporary, { force: true });
79
+ } catch (error) {
80
+ if (!storageBlocked(error)) throw error;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ async function clearInstallation(directory, api) {
86
+ const path = await statePath(directory, api);
87
+ try {
88
+ await rm(path, { force: true });
89
+ return true;
90
+ } catch (error) {
91
+ if (storageBlocked(error)) return false;
92
+ throw error;
93
+ }
94
+ }
95
+ async function resolveInstallation(config, origin) {
96
+ if (config.workspaceId) return { ...config, workspaceId: config.workspaceId };
97
+ const response = await fetch(`${config.apiBaseUrl}/v1/sdk/${config.siteId}/config`, {
98
+ headers: { Origin: origin ?? "https://installation.userreason.invalid" },
99
+ redirect: "error",
100
+ signal: AbortSignal.timeout(2e4)
101
+ });
102
+ if (!response.ok) throw new Error(`Unable to resolve the existing site (${response.status}). For a restricted site pass --origin, or supply --workspace and --site.`);
103
+ const text = await response.text();
104
+ if (text.length > 2097152) throw new Error("Site configuration is too large");
105
+ const value = JSON.parse(text);
106
+ if (value.site_id !== config.siteId) throw new Error("Site configuration scope mismatch");
107
+ return { ...config, workspaceId: uuid(value.workspace_id) };
108
+ }
109
+
110
+ export {
111
+ savedInstallation,
112
+ saveInstallation,
113
+ clearInstallation,
114
+ resolveInstallation
115
+ };