@remixhq/core 0.1.2

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.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -0
  3. package/dist/api.d.ts +494 -0
  4. package/dist/api.js +7 -0
  5. package/dist/auth.d.ts +27 -0
  6. package/dist/auth.js +15 -0
  7. package/dist/binding.d.ts +16 -0
  8. package/dist/binding.js +11 -0
  9. package/dist/chunk-2WGZS7CD.js +0 -0
  10. package/dist/chunk-34WDQCPF.js +242 -0
  11. package/dist/chunk-4OCNZHHR.js +0 -0
  12. package/dist/chunk-54CBEP2W.js +570 -0
  13. package/dist/chunk-55K5GHAZ.js +252 -0
  14. package/dist/chunk-5H5CZKGN.js +691 -0
  15. package/dist/chunk-5NTOJXEZ.js +223 -0
  16. package/dist/chunk-7WUKH3ZD.js +221 -0
  17. package/dist/chunk-AE2HPMUZ.js +80 -0
  18. package/dist/chunk-AEAOYVIL.js +200 -0
  19. package/dist/chunk-BJFCN2C3.js +46 -0
  20. package/dist/chunk-DCU3646I.js +12 -0
  21. package/dist/chunk-DEWAIK5X.js +11 -0
  22. package/dist/chunk-DRD6EVTT.js +447 -0
  23. package/dist/chunk-E4KAGBU7.js +134 -0
  24. package/dist/chunk-EF3677RE.js +93 -0
  25. package/dist/chunk-EVWDYCBL.js +223 -0
  26. package/dist/chunk-FAZUMWBS.js +93 -0
  27. package/dist/chunk-GC2MOT3U.js +12 -0
  28. package/dist/chunk-GFOBGYW4.js +252 -0
  29. package/dist/chunk-INDDXWAH.js +92 -0
  30. package/dist/chunk-K57ZFDGC.js +15 -0
  31. package/dist/chunk-NDA7EJJA.js +286 -0
  32. package/dist/chunk-NK2DA4X6.js +357 -0
  33. package/dist/chunk-OJMTW22J.js +286 -0
  34. package/dist/chunk-OMUDRPUI.js +195 -0
  35. package/dist/chunk-ONKKRS2C.js +239 -0
  36. package/dist/chunk-OWFBBWU7.js +196 -0
  37. package/dist/chunk-P7EM3N73.js +46 -0
  38. package/dist/chunk-PR5QKMHM.js +46 -0
  39. package/dist/chunk-RIP2MIZL.js +710 -0
  40. package/dist/chunk-TQHLFQY4.js +448 -0
  41. package/dist/chunk-TY3SSQQK.js +688 -0
  42. package/dist/chunk-UGKPOCN5.js +710 -0
  43. package/dist/chunk-VM3CGCNX.js +46 -0
  44. package/dist/chunk-XOQIADCH.js +223 -0
  45. package/dist/chunk-YZ34ICNN.js +17 -0
  46. package/dist/chunk-ZBMOGUSJ.js +17 -0
  47. package/dist/collab.d.ts +680 -0
  48. package/dist/collab.js +1917 -0
  49. package/dist/config.d.ts +22 -0
  50. package/dist/config.js +9 -0
  51. package/dist/errors.d.ts +21 -0
  52. package/dist/errors.js +12 -0
  53. package/dist/index.cjs +1269 -0
  54. package/dist/index.d.cts +482 -0
  55. package/dist/index.d.ts +6 -0
  56. package/dist/index.js +34 -0
  57. package/dist/repo.d.ts +66 -0
  58. package/dist/repo.js +62 -0
  59. package/dist/tokenProvider-BWTusyj4.d.ts +63 -0
  60. package/package.json +72 -0
@@ -0,0 +1,223 @@
1
+ import {
2
+ ComergeError
3
+ } from "./chunk-K57ZFDGC.js";
4
+
5
+ // src/auth/session.ts
6
+ import { z } from "zod";
7
+ var storedSessionSchema = z.object({
8
+ access_token: z.string().min(1),
9
+ refresh_token: z.string().min(1),
10
+ expires_at: z.number().int().positive(),
11
+ token_type: z.string().min(1).optional(),
12
+ user: z.object({
13
+ id: z.string().min(1),
14
+ email: z.string().email().optional().nullable()
15
+ }).optional()
16
+ });
17
+
18
+ // src/auth/localSessionStore.ts
19
+ import fs from "fs/promises";
20
+ import os from "os";
21
+ import path from "path";
22
+ function xdgConfigHome() {
23
+ const value = process.env.XDG_CONFIG_HOME;
24
+ if (typeof value === "string" && value.trim()) return value;
25
+ return path.join(os.homedir(), ".config");
26
+ }
27
+ async function maybeLoadKeytar() {
28
+ try {
29
+ const mod = await new Function("return import('keytar')")();
30
+ const candidates = [mod, mod?.default].filter(Boolean);
31
+ for (const candidate of candidates) {
32
+ const value = candidate;
33
+ if (typeof value.getPassword === "function" && typeof value.setPassword === "function") {
34
+ return value;
35
+ }
36
+ }
37
+ } catch {
38
+ return null;
39
+ }
40
+ return null;
41
+ }
42
+ async function ensurePathPermissions(filePath) {
43
+ const dir = path.dirname(filePath);
44
+ await fs.mkdir(dir, { recursive: true });
45
+ try {
46
+ await fs.chmod(dir, 448);
47
+ } catch {
48
+ }
49
+ try {
50
+ await fs.chmod(filePath, 384);
51
+ } catch {
52
+ }
53
+ }
54
+ async function writeJsonAtomic(filePath, value) {
55
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
56
+ const tmpPath = `${filePath}.tmp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
57
+ await fs.writeFile(tmpPath, JSON.stringify(value, null, 2) + "\n", "utf8");
58
+ await fs.rename(tmpPath, filePath);
59
+ }
60
+ async function writeSessionFileFallback(filePath, session) {
61
+ await writeJsonAtomic(filePath, session);
62
+ await ensurePathPermissions(filePath);
63
+ }
64
+ function createLocalSessionStore(params) {
65
+ const service = params?.service?.trim() || "comerge-cli";
66
+ const account = params?.account?.trim() || "default";
67
+ const filePath = params?.filePath?.trim() || path.join(xdgConfigHome(), "comerge", "session.json");
68
+ return {
69
+ async getSession() {
70
+ const keytar = await maybeLoadKeytar();
71
+ if (keytar) {
72
+ const raw2 = await keytar.getPassword(service, account);
73
+ if (!raw2) return null;
74
+ try {
75
+ const parsed = storedSessionSchema.safeParse(JSON.parse(raw2));
76
+ return parsed.success ? parsed.data : null;
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
81
+ const raw = await fs.readFile(filePath, "utf8").catch(() => null);
82
+ if (!raw) return null;
83
+ try {
84
+ const parsed = storedSessionSchema.safeParse(JSON.parse(raw));
85
+ if (!parsed.success) return null;
86
+ await ensurePathPermissions(filePath);
87
+ return parsed.data;
88
+ } catch {
89
+ return null;
90
+ }
91
+ },
92
+ async setSession(session) {
93
+ const parsed = storedSessionSchema.safeParse(session);
94
+ if (!parsed.success) {
95
+ throw new Error("Session data is invalid and was not stored.");
96
+ }
97
+ const keytar = await maybeLoadKeytar();
98
+ if (keytar) {
99
+ await keytar.setPassword(service, account, JSON.stringify(parsed.data));
100
+ }
101
+ await writeSessionFileFallback(filePath, parsed.data);
102
+ }
103
+ };
104
+ }
105
+
106
+ // src/auth/tokenProvider.ts
107
+ function shouldRefreshSoon(session, skewSeconds = 60) {
108
+ const nowSec = Math.floor(Date.now() / 1e3);
109
+ return session.expires_at <= nowSec + skewSeconds;
110
+ }
111
+ function createStoredSessionTokenProvider(params) {
112
+ return async (opts) => {
113
+ const forceRefresh = Boolean(opts?.forceRefresh);
114
+ const envToken = process.env.COMERGE_ACCESS_TOKEN;
115
+ if (typeof envToken === "string" && envToken.trim().length > 0) {
116
+ return { token: envToken.trim(), session: null, fromEnv: true };
117
+ }
118
+ let session = await params.sessionStore.getSession();
119
+ if (!session) {
120
+ throw new ComergeError("Not signed in.", {
121
+ exitCode: 2,
122
+ hint: "Run `comerge login`, or set COMERGE_ACCESS_TOKEN for CI."
123
+ });
124
+ }
125
+ if (forceRefresh || shouldRefreshSoon(session)) {
126
+ try {
127
+ session = await params.refreshStoredSession({ config: params.config, session });
128
+ await params.sessionStore.setSession(session);
129
+ } catch (err) {
130
+ void err;
131
+ }
132
+ }
133
+ return { token: session.access_token, session, fromEnv: false };
134
+ };
135
+ }
136
+
137
+ // src/auth/supabase.ts
138
+ import { createClient } from "@supabase/supabase-js";
139
+ function createInMemoryStorage() {
140
+ const map = /* @__PURE__ */ new Map();
141
+ return {
142
+ getItem: (k) => map.get(k) ?? null,
143
+ setItem: (k, v) => {
144
+ map.set(k, v);
145
+ },
146
+ removeItem: (k) => {
147
+ map.delete(k);
148
+ }
149
+ };
150
+ }
151
+ function createSupabaseClient(config, storage) {
152
+ return createClient(config.supabaseUrl, config.supabaseAnonKey, {
153
+ auth: {
154
+ flowType: "pkce",
155
+ persistSession: false,
156
+ autoRefreshToken: false,
157
+ detectSessionInUrl: false,
158
+ storage
159
+ },
160
+ global: {
161
+ headers: {
162
+ "X-Requested-By": "comerge-cli"
163
+ }
164
+ }
165
+ });
166
+ }
167
+ function toStoredSession(session) {
168
+ if (!session.access_token || !session.refresh_token || !session.expires_at) {
169
+ throw new ComergeError("Supabase session is missing required fields.", { exitCode: 1 });
170
+ }
171
+ return {
172
+ access_token: session.access_token,
173
+ refresh_token: session.refresh_token,
174
+ expires_at: session.expires_at,
175
+ token_type: session.token_type ?? void 0,
176
+ user: session.user ? { id: session.user.id, email: session.user.email ?? null } : void 0
177
+ };
178
+ }
179
+ function createSupabaseAuthHelpers(config) {
180
+ const storage = createInMemoryStorage();
181
+ const supabase = createSupabaseClient(config, storage);
182
+ return {
183
+ async startGoogleLogin(params) {
184
+ const { data, error } = await supabase.auth.signInWithOAuth({
185
+ provider: "google",
186
+ options: {
187
+ redirectTo: params.redirectTo,
188
+ skipBrowserRedirect: true,
189
+ queryParams: {
190
+ access_type: "offline",
191
+ prompt: "consent"
192
+ }
193
+ }
194
+ });
195
+ if (error) throw error;
196
+ if (!data?.url) throw new ComergeError("Supabase did not return an OAuth URL.", { exitCode: 1 });
197
+ return { url: data.url };
198
+ },
199
+ async exchangeCode(params) {
200
+ const { data, error } = await supabase.auth.exchangeCodeForSession(params.code);
201
+ if (error) throw error;
202
+ if (!data?.session) throw new ComergeError("Supabase did not return a session.", { exitCode: 1 });
203
+ return toStoredSession(data.session);
204
+ },
205
+ async refreshWithStoredSession(params) {
206
+ const { data, error } = await supabase.auth.setSession({
207
+ access_token: params.session.access_token,
208
+ refresh_token: params.session.refresh_token
209
+ });
210
+ if (error) throw error;
211
+ if (!data?.session) throw new ComergeError("No session returned after refresh.", { exitCode: 1 });
212
+ return toStoredSession(data.session);
213
+ }
214
+ };
215
+ }
216
+
217
+ export {
218
+ storedSessionSchema,
219
+ createLocalSessionStore,
220
+ shouldRefreshSoon,
221
+ createStoredSessionTokenProvider,
222
+ createSupabaseAuthHelpers
223
+ };
@@ -0,0 +1,221 @@
1
+ import {
2
+ ComergeError
3
+ } from "./chunk-K57ZFDGC.js";
4
+
5
+ // src/auth/session.ts
6
+ import { z } from "zod";
7
+ var storedSessionSchema = z.object({
8
+ access_token: z.string().min(1),
9
+ refresh_token: z.string().min(1),
10
+ expires_at: z.number().int().positive(),
11
+ token_type: z.string().min(1).optional(),
12
+ user: z.object({
13
+ id: z.string().min(1),
14
+ email: z.string().email().optional().nullable()
15
+ }).optional()
16
+ });
17
+
18
+ // src/auth/localSessionStore.ts
19
+ import fs from "fs/promises";
20
+ import os from "os";
21
+ import path from "path";
22
+ function xdgConfigHome() {
23
+ const value = process.env.XDG_CONFIG_HOME;
24
+ if (typeof value === "string" && value.trim()) return value;
25
+ return path.join(os.homedir(), ".config");
26
+ }
27
+ async function maybeLoadKeytar() {
28
+ try {
29
+ const mod = await new Function("return import('keytar')")();
30
+ const candidates = [mod, mod?.default].filter(Boolean);
31
+ for (const candidate of candidates) {
32
+ const value = candidate;
33
+ if (typeof value.getPassword === "function" && typeof value.setPassword === "function") {
34
+ return value;
35
+ }
36
+ }
37
+ } catch {
38
+ return null;
39
+ }
40
+ return null;
41
+ }
42
+ async function ensurePathPermissions(filePath) {
43
+ const dir = path.dirname(filePath);
44
+ await fs.mkdir(dir, { recursive: true });
45
+ try {
46
+ await fs.chmod(dir, 448);
47
+ } catch {
48
+ }
49
+ try {
50
+ await fs.chmod(filePath, 384);
51
+ } catch {
52
+ }
53
+ }
54
+ async function writeJsonAtomic(filePath, value) {
55
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
56
+ const tmpPath = `${filePath}.tmp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
57
+ await fs.writeFile(tmpPath, JSON.stringify(value, null, 2) + "\n", "utf8");
58
+ await fs.rename(tmpPath, filePath);
59
+ }
60
+ function createLocalSessionStore(params) {
61
+ const service = params?.service?.trim() || "comerge-cli";
62
+ const account = params?.account?.trim() || "default";
63
+ const filePath = params?.filePath?.trim() || path.join(xdgConfigHome(), "comerge", "session.json");
64
+ return {
65
+ async getSession() {
66
+ const keytar = await maybeLoadKeytar();
67
+ if (keytar) {
68
+ const raw2 = await keytar.getPassword(service, account);
69
+ if (!raw2) return null;
70
+ try {
71
+ const parsed = storedSessionSchema.safeParse(JSON.parse(raw2));
72
+ return parsed.success ? parsed.data : null;
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
77
+ const raw = await fs.readFile(filePath, "utf8").catch(() => null);
78
+ if (!raw) return null;
79
+ try {
80
+ const parsed = storedSessionSchema.safeParse(JSON.parse(raw));
81
+ if (!parsed.success) return null;
82
+ await ensurePathPermissions(filePath);
83
+ return parsed.data;
84
+ } catch {
85
+ return null;
86
+ }
87
+ },
88
+ async setSession(session) {
89
+ const parsed = storedSessionSchema.safeParse(session);
90
+ if (!parsed.success) {
91
+ throw new Error("Session data is invalid and was not stored.");
92
+ }
93
+ const keytar = await maybeLoadKeytar();
94
+ if (keytar) {
95
+ await keytar.setPassword(service, account, JSON.stringify(parsed.data));
96
+ return;
97
+ }
98
+ await writeJsonAtomic(filePath, parsed.data);
99
+ await ensurePathPermissions(filePath);
100
+ }
101
+ };
102
+ }
103
+
104
+ // src/auth/tokenProvider.ts
105
+ function shouldRefreshSoon(session, skewSeconds = 60) {
106
+ const nowSec = Math.floor(Date.now() / 1e3);
107
+ return session.expires_at <= nowSec + skewSeconds;
108
+ }
109
+ function createStoredSessionTokenProvider(params) {
110
+ return async (opts) => {
111
+ const forceRefresh = Boolean(opts?.forceRefresh);
112
+ const envToken = process.env.COMERGE_ACCESS_TOKEN;
113
+ if (typeof envToken === "string" && envToken.trim().length > 0) {
114
+ return { token: envToken.trim(), session: null, fromEnv: true };
115
+ }
116
+ let session = await params.sessionStore.getSession();
117
+ if (!session) {
118
+ throw new ComergeError("Not signed in.", {
119
+ exitCode: 2,
120
+ hint: "Run `comerge login`, or set COMERGE_ACCESS_TOKEN for CI."
121
+ });
122
+ }
123
+ if (forceRefresh || shouldRefreshSoon(session)) {
124
+ try {
125
+ session = await params.refreshStoredSession({ config: params.config, session });
126
+ await params.sessionStore.setSession(session);
127
+ } catch (err) {
128
+ void err;
129
+ }
130
+ }
131
+ return { token: session.access_token, session, fromEnv: false };
132
+ };
133
+ }
134
+
135
+ // src/auth/supabase.ts
136
+ import { createClient } from "@supabase/supabase-js";
137
+ function createInMemoryStorage() {
138
+ const map = /* @__PURE__ */ new Map();
139
+ return {
140
+ getItem: (k) => map.get(k) ?? null,
141
+ setItem: (k, v) => {
142
+ map.set(k, v);
143
+ },
144
+ removeItem: (k) => {
145
+ map.delete(k);
146
+ }
147
+ };
148
+ }
149
+ function createSupabaseClient(config, storage) {
150
+ return createClient(config.supabaseUrl, config.supabaseAnonKey, {
151
+ auth: {
152
+ flowType: "pkce",
153
+ persistSession: false,
154
+ autoRefreshToken: false,
155
+ detectSessionInUrl: false,
156
+ storage
157
+ },
158
+ global: {
159
+ headers: {
160
+ "X-Requested-By": "comerge-cli"
161
+ }
162
+ }
163
+ });
164
+ }
165
+ function toStoredSession(session) {
166
+ if (!session.access_token || !session.refresh_token || !session.expires_at) {
167
+ throw new ComergeError("Supabase session is missing required fields.", { exitCode: 1 });
168
+ }
169
+ return {
170
+ access_token: session.access_token,
171
+ refresh_token: session.refresh_token,
172
+ expires_at: session.expires_at,
173
+ token_type: session.token_type ?? void 0,
174
+ user: session.user ? { id: session.user.id, email: session.user.email ?? null } : void 0
175
+ };
176
+ }
177
+ function createSupabaseAuthHelpers(config) {
178
+ const storage = createInMemoryStorage();
179
+ const supabase = createSupabaseClient(config, storage);
180
+ return {
181
+ async startGoogleLogin(params) {
182
+ const { data, error } = await supabase.auth.signInWithOAuth({
183
+ provider: "google",
184
+ options: {
185
+ redirectTo: params.redirectTo,
186
+ skipBrowserRedirect: true,
187
+ queryParams: {
188
+ access_type: "offline",
189
+ prompt: "consent"
190
+ }
191
+ }
192
+ });
193
+ if (error) throw error;
194
+ if (!data?.url) throw new ComergeError("Supabase did not return an OAuth URL.", { exitCode: 1 });
195
+ return { url: data.url };
196
+ },
197
+ async exchangeCode(params) {
198
+ const { data, error } = await supabase.auth.exchangeCodeForSession(params.code);
199
+ if (error) throw error;
200
+ if (!data?.session) throw new ComergeError("Supabase did not return a session.", { exitCode: 1 });
201
+ return toStoredSession(data.session);
202
+ },
203
+ async refreshWithStoredSession(params) {
204
+ const { data, error } = await supabase.auth.setSession({
205
+ access_token: params.session.access_token,
206
+ refresh_token: params.session.refresh_token
207
+ });
208
+ if (error) throw error;
209
+ if (!data?.session) throw new ComergeError("No session returned after refresh.", { exitCode: 1 });
210
+ return toStoredSession(data.session);
211
+ }
212
+ };
213
+ }
214
+
215
+ export {
216
+ storedSessionSchema,
217
+ createLocalSessionStore,
218
+ shouldRefreshSoon,
219
+ createStoredSessionTokenProvider,
220
+ createSupabaseAuthHelpers
221
+ };
@@ -0,0 +1,80 @@
1
+ import {
2
+ ComergeError
3
+ } from "./chunk-K57ZFDGC.js";
4
+
5
+ // src/infrastructure/binding/collabBindingStore.ts
6
+ import fs2 from "fs/promises";
7
+ import path2 from "path";
8
+
9
+ // src/shared/fs.ts
10
+ import fs from "fs/promises";
11
+ import path from "path";
12
+ async function pathExists(targetPath) {
13
+ try {
14
+ await fs.access(targetPath);
15
+ return true;
16
+ } catch {
17
+ return false;
18
+ }
19
+ }
20
+ async function findAvailableDirPath(preferredDir) {
21
+ if (!await pathExists(preferredDir)) return preferredDir;
22
+ const parent = path.dirname(preferredDir);
23
+ const base = path.basename(preferredDir);
24
+ for (let i = 2; i <= 1e3; i++) {
25
+ const candidate = path.join(parent, `${base}-${i}`);
26
+ if (!await pathExists(candidate)) return candidate;
27
+ }
28
+ throw new ComergeError("No available output directory name.", {
29
+ exitCode: 2,
30
+ hint: `Tried ${base}-2 through ${base}-1000 under ${parent}.`
31
+ });
32
+ }
33
+ async function writeJsonAtomic(filePath, value) {
34
+ const dir = path.dirname(filePath);
35
+ await fs.mkdir(dir, { recursive: true });
36
+ const tmp = `${filePath}.tmp-${Date.now()}`;
37
+ await fs.writeFile(tmp, `${JSON.stringify(value, null, 2)}
38
+ `, "utf8");
39
+ await fs.rename(tmp, filePath);
40
+ }
41
+
42
+ // src/infrastructure/binding/collabBindingStore.ts
43
+ function getCollabBindingPath(repoRoot) {
44
+ return path2.join(repoRoot, ".comerge", "config.json");
45
+ }
46
+ async function readCollabBinding(repoRoot) {
47
+ try {
48
+ const raw = await fs2.readFile(getCollabBindingPath(repoRoot), "utf8");
49
+ const parsed = JSON.parse(raw);
50
+ if (parsed?.schemaVersion !== 1) return null;
51
+ if (!parsed.projectId || !parsed.currentAppId || !parsed.upstreamAppId) return null;
52
+ return {
53
+ schemaVersion: 1,
54
+ projectId: parsed.projectId,
55
+ currentAppId: parsed.currentAppId,
56
+ upstreamAppId: parsed.upstreamAppId,
57
+ threadId: parsed.threadId ?? null,
58
+ repoFingerprint: parsed.repoFingerprint ?? null,
59
+ remoteUrl: parsed.remoteUrl ?? null,
60
+ defaultBranch: parsed.defaultBranch ?? null
61
+ };
62
+ } catch {
63
+ return null;
64
+ }
65
+ }
66
+ async function writeCollabBinding(repoRoot, binding) {
67
+ const filePath = getCollabBindingPath(repoRoot);
68
+ await writeJsonAtomic(filePath, {
69
+ schemaVersion: 1,
70
+ ...binding
71
+ });
72
+ return filePath;
73
+ }
74
+
75
+ export {
76
+ findAvailableDirPath,
77
+ getCollabBindingPath,
78
+ readCollabBinding,
79
+ writeCollabBinding
80
+ };