@remixhq/core 0.1.12 → 0.1.14
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/dist/api.d.ts +94 -2
- package/dist/api.js +1 -1
- package/dist/auth.js +1 -1
- package/dist/binding-WiIRI2fl.d.ts +43 -0
- package/dist/binding.d.ts +1 -21
- package/dist/binding.js +4 -2
- package/dist/chunk-CUUXZSKW.js +611 -0
- package/dist/chunk-E6AYE22H.js +343 -0
- package/dist/chunk-OZRXDDEL.js +46 -0
- package/dist/chunk-P6JHXOV4.js +236 -0
- package/dist/chunk-R7FVSCQW.js +415 -0
- package/dist/chunk-RKMMEML5.js +46 -0
- package/dist/chunk-UIGKSCTD.js +406 -0
- package/dist/chunk-US5SM7ZC.js +433 -0
- package/dist/chunk-UWIVJRTI.js +343 -0
- package/dist/chunk-VA6WXRWB.js +636 -0
- package/dist/chunk-WT6VRLXU.js +636 -0
- package/dist/chunk-YCFLOHJV.js +343 -0
- package/dist/collab.d.ts +433 -663
- package/dist/collab.js +4793 -1550
- package/dist/contracts-NbV3P_Rl.d.ts +677 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -2
- package/dist/repo.js +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getCurrentBranch
|
|
3
|
+
} from "./chunk-VA6WXRWB.js";
|
|
4
|
+
import {
|
|
5
|
+
RemixError
|
|
6
|
+
} from "./chunk-YZ34ICNN.js";
|
|
7
|
+
|
|
8
|
+
// src/infrastructure/binding/collabBindingStore.ts
|
|
9
|
+
import fs2 from "fs/promises";
|
|
10
|
+
import path2 from "path";
|
|
11
|
+
|
|
12
|
+
// src/shared/fs.ts
|
|
13
|
+
import fs from "fs/promises";
|
|
14
|
+
import path from "path";
|
|
15
|
+
async function reserveDirectory(targetDir) {
|
|
16
|
+
try {
|
|
17
|
+
await fs.mkdir(targetDir);
|
|
18
|
+
return targetDir;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (error?.code === "EEXIST") {
|
|
21
|
+
throw new RemixError("Output directory already exists.", {
|
|
22
|
+
exitCode: 2,
|
|
23
|
+
hint: `Choose an empty destination path: ${targetDir}`
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function reserveAvailableDirPath(preferredDir) {
|
|
30
|
+
const parent = path.dirname(preferredDir);
|
|
31
|
+
const base = path.basename(preferredDir);
|
|
32
|
+
for (let i = 1; i <= 1e3; i += 1) {
|
|
33
|
+
const candidate = i === 1 ? preferredDir : path.join(parent, `${base}-${i}`);
|
|
34
|
+
try {
|
|
35
|
+
await fs.mkdir(candidate);
|
|
36
|
+
return candidate;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error?.code === "EEXIST") continue;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw new RemixError("No available output directory name.", {
|
|
43
|
+
exitCode: 2,
|
|
44
|
+
hint: `Tried ${base} through ${base}-1000 under ${parent}.`
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async function writeJsonAtomic(filePath, value) {
|
|
48
|
+
const dir = path.dirname(filePath);
|
|
49
|
+
await fs.mkdir(dir, { recursive: true });
|
|
50
|
+
const tmp = `${filePath}.tmp-${Date.now()}`;
|
|
51
|
+
await fs.writeFile(tmp, `${JSON.stringify(value, null, 2)}
|
|
52
|
+
`, "utf8");
|
|
53
|
+
await fs.rename(tmp, filePath);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/infrastructure/binding/collabBindingStore.ts
|
|
57
|
+
function getCollabBindingPath(repoRoot) {
|
|
58
|
+
return path2.join(repoRoot, ".remix", "config.json");
|
|
59
|
+
}
|
|
60
|
+
function buildBindingFileV3(params) {
|
|
61
|
+
return {
|
|
62
|
+
schemaVersion: 3,
|
|
63
|
+
repoFingerprint: params.repoFingerprint,
|
|
64
|
+
remoteUrl: params.remoteUrl,
|
|
65
|
+
defaultBranch: params.defaultBranch,
|
|
66
|
+
branchBindings: params.branchBindings,
|
|
67
|
+
...params.explicitRootBinding ? { explicitRootBinding: params.explicitRootBinding } : {}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function normalizeBranchName(value) {
|
|
71
|
+
const normalized = String(value ?? "").trim();
|
|
72
|
+
return normalized || null;
|
|
73
|
+
}
|
|
74
|
+
function normalizeProjectId(value) {
|
|
75
|
+
const normalized = String(value ?? "").trim();
|
|
76
|
+
return normalized || null;
|
|
77
|
+
}
|
|
78
|
+
function normalizeBranchBinding(value) {
|
|
79
|
+
if (!value?.currentAppId || !value?.upstreamAppId) return null;
|
|
80
|
+
return {
|
|
81
|
+
projectId: normalizeProjectId(value.projectId),
|
|
82
|
+
currentAppId: value.currentAppId,
|
|
83
|
+
upstreamAppId: value.upstreamAppId,
|
|
84
|
+
threadId: value.threadId ?? null,
|
|
85
|
+
laneId: value.laneId ?? null,
|
|
86
|
+
bindingMode: value.bindingMode === "legacy" ? "legacy" : value.bindingMode === "explicit_root" ? "explicit_root" : "lane"
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function buildResolvedBinding(params) {
|
|
90
|
+
if (!params.binding) return null;
|
|
91
|
+
return {
|
|
92
|
+
schemaVersion: 3,
|
|
93
|
+
projectId: params.binding.projectId ?? params.fallbackProjectId,
|
|
94
|
+
currentAppId: params.binding.currentAppId,
|
|
95
|
+
upstreamAppId: params.binding.upstreamAppId,
|
|
96
|
+
threadId: params.binding.threadId,
|
|
97
|
+
repoFingerprint: params.repoFingerprint,
|
|
98
|
+
remoteUrl: params.remoteUrl,
|
|
99
|
+
defaultBranch: params.defaultBranch,
|
|
100
|
+
laneId: params.binding.laneId,
|
|
101
|
+
branchName: params.branchName,
|
|
102
|
+
bindingMode: params.binding.bindingMode
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function deriveFallbackProjectId(params) {
|
|
106
|
+
const candidates = [
|
|
107
|
+
params.currentBranch ? params.branchBindings[params.currentBranch]?.projectId ?? null : null,
|
|
108
|
+
params.defaultBranch ? params.branchBindings[params.defaultBranch]?.projectId ?? null : null,
|
|
109
|
+
...Object.values(params.branchBindings).map((binding) => binding.projectId),
|
|
110
|
+
params.legacyProjectId
|
|
111
|
+
];
|
|
112
|
+
for (const candidate of candidates) {
|
|
113
|
+
if (candidate) return candidate;
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
async function readCollabBindingState(repoRoot, options) {
|
|
118
|
+
try {
|
|
119
|
+
const persist = options?.persist === true;
|
|
120
|
+
const filePath = getCollabBindingPath(repoRoot);
|
|
121
|
+
const raw = await fs2.readFile(filePath, "utf8");
|
|
122
|
+
const parsed = JSON.parse(raw);
|
|
123
|
+
if (!parsed || typeof parsed !== "object") return null;
|
|
124
|
+
const currentBranch = normalizeBranchName(await getCurrentBranch(repoRoot).catch(() => null));
|
|
125
|
+
if (parsed.schemaVersion === 1) {
|
|
126
|
+
if (!parsed.currentAppId || !parsed.upstreamAppId) return null;
|
|
127
|
+
const projectId = normalizeProjectId(parsed.projectId);
|
|
128
|
+
const preferredBranch = normalizeBranchName(parsed.preferredBranch ?? parsed.defaultBranch ?? null);
|
|
129
|
+
const branchKey = preferredBranch ?? currentBranch ?? null;
|
|
130
|
+
const branchBindings2 = branchKey ? {
|
|
131
|
+
[branchKey]: {
|
|
132
|
+
projectId,
|
|
133
|
+
currentAppId: parsed.currentAppId,
|
|
134
|
+
upstreamAppId: parsed.upstreamAppId,
|
|
135
|
+
threadId: parsed.threadId ?? null,
|
|
136
|
+
laneId: null,
|
|
137
|
+
bindingMode: "legacy"
|
|
138
|
+
}
|
|
139
|
+
} : {};
|
|
140
|
+
const migratedFile = buildBindingFileV3({
|
|
141
|
+
repoFingerprint: parsed.repoFingerprint ?? null,
|
|
142
|
+
remoteUrl: parsed.remoteUrl ?? null,
|
|
143
|
+
defaultBranch: parsed.defaultBranch ?? null,
|
|
144
|
+
branchBindings: branchBindings2
|
|
145
|
+
});
|
|
146
|
+
if (persist) {
|
|
147
|
+
try {
|
|
148
|
+
await writeJsonAtomic(filePath, migratedFile);
|
|
149
|
+
} catch {
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
schemaVersion: 3,
|
|
154
|
+
projectId,
|
|
155
|
+
repoFingerprint: migratedFile.repoFingerprint,
|
|
156
|
+
remoteUrl: migratedFile.remoteUrl,
|
|
157
|
+
defaultBranch: migratedFile.defaultBranch,
|
|
158
|
+
currentBranch,
|
|
159
|
+
branchBindings: migratedFile.branchBindings,
|
|
160
|
+
explicitRootBinding: null,
|
|
161
|
+
binding: buildResolvedBinding({
|
|
162
|
+
fallbackProjectId: projectId,
|
|
163
|
+
repoFingerprint: migratedFile.repoFingerprint,
|
|
164
|
+
remoteUrl: migratedFile.remoteUrl,
|
|
165
|
+
defaultBranch: migratedFile.defaultBranch,
|
|
166
|
+
branchName: branchKey,
|
|
167
|
+
binding: branchKey ? migratedFile.branchBindings[branchKey] : null
|
|
168
|
+
})
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (parsed.schemaVersion !== 2 && parsed.schemaVersion !== 3) return null;
|
|
172
|
+
const file = parsed;
|
|
173
|
+
let shouldPersistNormalizedBranchBindings = false;
|
|
174
|
+
const legacyProjectId = normalizeProjectId(file.projectId);
|
|
175
|
+
const branchBindings = Object.fromEntries(
|
|
176
|
+
Object.entries(file.branchBindings ?? {}).map(([branchName, branchBinding]) => {
|
|
177
|
+
const normalized = normalizeBranchBinding(branchBinding);
|
|
178
|
+
const rawProjectId = branchBinding && typeof branchBinding === "object" && "projectId" in branchBinding ? normalizeProjectId(branchBinding.projectId) : null;
|
|
179
|
+
const rawBindingMode = branchBinding && typeof branchBinding === "object" && "bindingMode" in branchBinding ? branchBinding.bindingMode : null;
|
|
180
|
+
const hasLegacyPreferredBranch = Boolean(branchBinding) && typeof branchBinding === "object" && "preferredBranch" in branchBinding;
|
|
181
|
+
let normalizedWithProject = normalized;
|
|
182
|
+
if (normalizedWithProject && !normalizedWithProject.projectId && legacyProjectId) {
|
|
183
|
+
normalizedWithProject = {
|
|
184
|
+
...normalizedWithProject,
|
|
185
|
+
projectId: legacyProjectId
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if (normalizedWithProject && (rawBindingMode !== normalizedWithProject.bindingMode || hasLegacyPreferredBranch || rawProjectId !== normalizedWithProject.projectId)) {
|
|
189
|
+
shouldPersistNormalizedBranchBindings = true;
|
|
190
|
+
}
|
|
191
|
+
return [branchName, normalizedWithProject];
|
|
192
|
+
}).filter((entry) => Boolean(entry[1]))
|
|
193
|
+
);
|
|
194
|
+
const legacyExplicitBinding = normalizeBranchBinding(file.explicitBinding ?? null);
|
|
195
|
+
const legacyExplicitBranch = currentBranch ?? normalizeBranchName(file.defaultBranch);
|
|
196
|
+
if (legacyExplicitBinding && legacyExplicitBranch) {
|
|
197
|
+
branchBindings[legacyExplicitBranch] = {
|
|
198
|
+
...legacyExplicitBinding,
|
|
199
|
+
projectId: legacyExplicitBinding.projectId ?? branchBindings[legacyExplicitBranch]?.projectId ?? legacyProjectId,
|
|
200
|
+
bindingMode: "lane"
|
|
201
|
+
};
|
|
202
|
+
shouldPersistNormalizedBranchBindings = true;
|
|
203
|
+
}
|
|
204
|
+
let explicitRootBinding = normalizeBranchBinding(file.explicitRootBinding ?? null);
|
|
205
|
+
if (explicitRootBinding && !explicitRootBinding.projectId && legacyProjectId) {
|
|
206
|
+
explicitRootBinding = {
|
|
207
|
+
...explicitRootBinding,
|
|
208
|
+
projectId: legacyProjectId
|
|
209
|
+
};
|
|
210
|
+
shouldPersistNormalizedBranchBindings = true;
|
|
211
|
+
}
|
|
212
|
+
if (explicitRootBinding && explicitRootBinding.bindingMode !== "explicit_root") {
|
|
213
|
+
explicitRootBinding = {
|
|
214
|
+
...explicitRootBinding,
|
|
215
|
+
bindingMode: "explicit_root"
|
|
216
|
+
};
|
|
217
|
+
shouldPersistNormalizedBranchBindings = true;
|
|
218
|
+
}
|
|
219
|
+
if (persist && ("explicitBinding" in file || "explicitRootBinding" in file || shouldPersistNormalizedBranchBindings || parsed.schemaVersion === 2)) {
|
|
220
|
+
try {
|
|
221
|
+
await writeJsonAtomic(
|
|
222
|
+
filePath,
|
|
223
|
+
buildBindingFileV3({
|
|
224
|
+
repoFingerprint: file.repoFingerprint ?? null,
|
|
225
|
+
remoteUrl: file.remoteUrl ?? null,
|
|
226
|
+
defaultBranch: file.defaultBranch ?? null,
|
|
227
|
+
branchBindings,
|
|
228
|
+
explicitRootBinding
|
|
229
|
+
})
|
|
230
|
+
);
|
|
231
|
+
} catch {
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const resolvedBranch = currentBranch ?? normalizeBranchName(file.defaultBranch);
|
|
235
|
+
const fallbackProjectId = deriveFallbackProjectId({
|
|
236
|
+
branchBindings,
|
|
237
|
+
currentBranch: resolvedBranch,
|
|
238
|
+
defaultBranch: normalizeBranchName(file.defaultBranch),
|
|
239
|
+
legacyProjectId: explicitRootBinding?.projectId ?? legacyProjectId
|
|
240
|
+
});
|
|
241
|
+
const resolvedBinding = buildResolvedBinding({
|
|
242
|
+
fallbackProjectId,
|
|
243
|
+
repoFingerprint: file.repoFingerprint ?? null,
|
|
244
|
+
remoteUrl: file.remoteUrl ?? null,
|
|
245
|
+
defaultBranch: file.defaultBranch ?? null,
|
|
246
|
+
branchName: resolvedBranch,
|
|
247
|
+
binding: resolvedBranch ? branchBindings[resolvedBranch] ?? null : null
|
|
248
|
+
}) ?? (resolvedBranch && resolvedBranch === normalizeBranchName(file.defaultBranch) && explicitRootBinding ? buildResolvedBinding({
|
|
249
|
+
fallbackProjectId,
|
|
250
|
+
repoFingerprint: file.repoFingerprint ?? null,
|
|
251
|
+
remoteUrl: file.remoteUrl ?? null,
|
|
252
|
+
defaultBranch: file.defaultBranch ?? null,
|
|
253
|
+
branchName: normalizeBranchName(file.defaultBranch),
|
|
254
|
+
binding: explicitRootBinding
|
|
255
|
+
}) : null);
|
|
256
|
+
return {
|
|
257
|
+
schemaVersion: parsed.schemaVersion,
|
|
258
|
+
projectId: fallbackProjectId,
|
|
259
|
+
repoFingerprint: file.repoFingerprint ?? null,
|
|
260
|
+
remoteUrl: file.remoteUrl ?? null,
|
|
261
|
+
defaultBranch: file.defaultBranch ?? null,
|
|
262
|
+
currentBranch,
|
|
263
|
+
branchBindings,
|
|
264
|
+
explicitRootBinding: buildResolvedBinding({
|
|
265
|
+
fallbackProjectId,
|
|
266
|
+
repoFingerprint: file.repoFingerprint ?? null,
|
|
267
|
+
remoteUrl: file.remoteUrl ?? null,
|
|
268
|
+
defaultBranch: file.defaultBranch ?? null,
|
|
269
|
+
branchName: normalizeBranchName(file.defaultBranch),
|
|
270
|
+
binding: explicitRootBinding
|
|
271
|
+
}),
|
|
272
|
+
binding: resolvedBinding
|
|
273
|
+
};
|
|
274
|
+
} catch {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async function readCollabBinding(repoRoot) {
|
|
279
|
+
const state = await readCollabBindingState(repoRoot);
|
|
280
|
+
return state?.binding ?? null;
|
|
281
|
+
}
|
|
282
|
+
async function writeCollabBinding(repoRoot, binding) {
|
|
283
|
+
const filePath = getCollabBindingPath(repoRoot);
|
|
284
|
+
const currentBranch = normalizeBranchName(await getCurrentBranch(repoRoot).catch(() => null));
|
|
285
|
+
const branchName = normalizeBranchName(binding.branchName) ?? currentBranch ?? binding.defaultBranch ?? "main";
|
|
286
|
+
const existing = await readCollabBindingState(repoRoot, { persist: true });
|
|
287
|
+
const branchBindings = { ...existing?.branchBindings ?? {} };
|
|
288
|
+
branchBindings[branchName] = {
|
|
289
|
+
projectId: normalizeProjectId(binding.projectId) ?? branchBindings[branchName]?.projectId ?? existing?.projectId ?? null,
|
|
290
|
+
currentAppId: binding.currentAppId,
|
|
291
|
+
upstreamAppId: binding.upstreamAppId,
|
|
292
|
+
threadId: binding.threadId ?? null,
|
|
293
|
+
laneId: binding.laneId ?? null,
|
|
294
|
+
bindingMode: binding.bindingMode ?? "lane"
|
|
295
|
+
};
|
|
296
|
+
const explicitRootBinding = binding.bindingMode === "explicit_root" ? {
|
|
297
|
+
...branchBindings[branchName],
|
|
298
|
+
bindingMode: "explicit_root"
|
|
299
|
+
} : existing?.explicitRootBinding ? {
|
|
300
|
+
projectId: existing.explicitRootBinding.projectId,
|
|
301
|
+
currentAppId: existing.explicitRootBinding.currentAppId,
|
|
302
|
+
upstreamAppId: existing.explicitRootBinding.upstreamAppId,
|
|
303
|
+
threadId: existing.explicitRootBinding.threadId,
|
|
304
|
+
laneId: existing.explicitRootBinding.laneId,
|
|
305
|
+
bindingMode: "explicit_root"
|
|
306
|
+
} : null;
|
|
307
|
+
await writeJsonAtomic(
|
|
308
|
+
filePath,
|
|
309
|
+
buildBindingFileV3({
|
|
310
|
+
repoFingerprint: binding.repoFingerprint ?? null,
|
|
311
|
+
remoteUrl: binding.remoteUrl ?? null,
|
|
312
|
+
defaultBranch: binding.defaultBranch ?? null,
|
|
313
|
+
branchBindings,
|
|
314
|
+
explicitRootBinding
|
|
315
|
+
})
|
|
316
|
+
);
|
|
317
|
+
return filePath;
|
|
318
|
+
}
|
|
319
|
+
async function writeCollabBindingSnapshot(params) {
|
|
320
|
+
const filePath = getCollabBindingPath(params.repoRoot);
|
|
321
|
+
await writeJsonAtomic(
|
|
322
|
+
filePath,
|
|
323
|
+
buildBindingFileV3({
|
|
324
|
+
repoFingerprint: params.repoFingerprint,
|
|
325
|
+
remoteUrl: params.remoteUrl,
|
|
326
|
+
defaultBranch: params.defaultBranch,
|
|
327
|
+
branchBindings: params.branchBindings,
|
|
328
|
+
explicitRootBinding: params.explicitRootBinding ?? null
|
|
329
|
+
})
|
|
330
|
+
);
|
|
331
|
+
return filePath;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export {
|
|
335
|
+
reserveDirectory,
|
|
336
|
+
reserveAvailableDirPath,
|
|
337
|
+
writeJsonAtomic,
|
|
338
|
+
getCollabBindingPath,
|
|
339
|
+
readCollabBindingState,
|
|
340
|
+
readCollabBinding,
|
|
341
|
+
writeCollabBinding,
|
|
342
|
+
writeCollabBindingSnapshot
|
|
343
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RemixError
|
|
3
|
+
} from "./chunk-YZ34ICNN.js";
|
|
4
|
+
|
|
5
|
+
// src/config/model.ts
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
var DEFAULT_API_URL = "http://192.168.8.251:8000";
|
|
8
|
+
var DEFAULT_SUPABASE_URL = "https://xtfxwbckjpfmqubnsusu.supabase.co";
|
|
9
|
+
var DEFAULT_SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inh0Znh3YmNranBmbXF1Ym5zdXN1Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjA2MDEyMzAsImV4cCI6MjA3NjE3NzIzMH0.dzWGAWrK4CvrmHVHzf8w7JlUZohdap0ZPnLZnABMV8s";
|
|
10
|
+
function isValidUrl(value) {
|
|
11
|
+
try {
|
|
12
|
+
new URL(value);
|
|
13
|
+
return true;
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
var urlSchema = z.string().min(1).transform((s) => s.trim()).refine((s) => isValidUrl(s), "Invalid URL.").transform((s) => s.replace(/\/+$/, ""));
|
|
19
|
+
var configSchema = z.object({
|
|
20
|
+
apiUrl: urlSchema,
|
|
21
|
+
supabaseUrl: urlSchema,
|
|
22
|
+
supabaseAnonKey: z.string().min(1)
|
|
23
|
+
});
|
|
24
|
+
var cachedConfig = null;
|
|
25
|
+
async function resolveConfig(_opts) {
|
|
26
|
+
if (cachedConfig) return cachedConfig;
|
|
27
|
+
const cfgRaw = {
|
|
28
|
+
apiUrl: DEFAULT_API_URL,
|
|
29
|
+
supabaseUrl: DEFAULT_SUPABASE_URL,
|
|
30
|
+
supabaseAnonKey: DEFAULT_SUPABASE_ANON_KEY
|
|
31
|
+
};
|
|
32
|
+
const parsedCfg = configSchema.safeParse(cfgRaw);
|
|
33
|
+
if (!parsedCfg.success) {
|
|
34
|
+
throw new RemixError("Invalid core configuration.", {
|
|
35
|
+
exitCode: 1,
|
|
36
|
+
hint: parsedCfg.error.issues.map((i) => `${i.path.join(".") || "config"}: ${i.message}`).join("; ")
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
cachedConfig = parsedCfg.data;
|
|
40
|
+
return parsedCfg.data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
configSchema,
|
|
45
|
+
resolveConfig
|
|
46
|
+
};
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RemixError
|
|
3
|
+
} from "./chunk-YZ34ICNN.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() || "remix-cli";
|
|
66
|
+
const account = params?.account?.trim() || "default";
|
|
67
|
+
const filePath = params?.filePath?.trim() || path.join(xdgConfigHome(), "remix", "session.json");
|
|
68
|
+
async function readKeytar() {
|
|
69
|
+
const keytar = await maybeLoadKeytar();
|
|
70
|
+
if (!keytar) return null;
|
|
71
|
+
const raw = await keytar.getPassword(service, account).catch(() => null);
|
|
72
|
+
if (!raw) return null;
|
|
73
|
+
try {
|
|
74
|
+
const parsed = storedSessionSchema.safeParse(JSON.parse(raw));
|
|
75
|
+
return parsed.success ? parsed.data : null;
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function readFile() {
|
|
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
|
+
function pickFreshest(a, b) {
|
|
93
|
+
if (!a) return b;
|
|
94
|
+
if (!b) return a;
|
|
95
|
+
return a.expires_at >= b.expires_at ? a : b;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
async getSession() {
|
|
99
|
+
const [k, f] = await Promise.all([readKeytar(), readFile()]);
|
|
100
|
+
return pickFreshest(k, f);
|
|
101
|
+
},
|
|
102
|
+
async setSession(session) {
|
|
103
|
+
const parsed = storedSessionSchema.safeParse(session);
|
|
104
|
+
if (!parsed.success) {
|
|
105
|
+
throw new Error("Session data is invalid and was not stored.");
|
|
106
|
+
}
|
|
107
|
+
await writeSessionFileFallback(filePath, parsed.data);
|
|
108
|
+
const keytar = await maybeLoadKeytar();
|
|
109
|
+
if (keytar) {
|
|
110
|
+
try {
|
|
111
|
+
await keytar.setPassword(service, account, JSON.stringify(parsed.data));
|
|
112
|
+
} catch {
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/auth/tokenProvider.ts
|
|
120
|
+
function shouldRefreshSoon(session, skewSeconds = 60) {
|
|
121
|
+
const nowSec = Math.floor(Date.now() / 1e3);
|
|
122
|
+
return session.expires_at <= nowSec + skewSeconds;
|
|
123
|
+
}
|
|
124
|
+
function createStoredSessionTokenProvider(params) {
|
|
125
|
+
return async (opts) => {
|
|
126
|
+
const forceRefresh = Boolean(opts?.forceRefresh);
|
|
127
|
+
const envToken = process.env.COMERGE_ACCESS_TOKEN;
|
|
128
|
+
if (typeof envToken === "string" && envToken.trim().length > 0) {
|
|
129
|
+
return { token: envToken.trim(), session: null, fromEnv: true };
|
|
130
|
+
}
|
|
131
|
+
let session = await params.sessionStore.getSession();
|
|
132
|
+
if (!session) {
|
|
133
|
+
throw new RemixError("Not signed in.", {
|
|
134
|
+
exitCode: 2,
|
|
135
|
+
hint: "Run `remix login`, or set COMERGE_ACCESS_TOKEN for CI."
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (forceRefresh || shouldRefreshSoon(session)) {
|
|
139
|
+
try {
|
|
140
|
+
session = await params.refreshStoredSession({ config: params.config, session });
|
|
141
|
+
await params.sessionStore.setSession(session);
|
|
142
|
+
} catch (err) {
|
|
143
|
+
void err;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { token: session.access_token, session, fromEnv: false };
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/auth/supabase.ts
|
|
151
|
+
import { createClient } from "@supabase/supabase-js";
|
|
152
|
+
function createInMemoryStorage() {
|
|
153
|
+
const map = /* @__PURE__ */ new Map();
|
|
154
|
+
return {
|
|
155
|
+
getItem: (k) => map.get(k) ?? null,
|
|
156
|
+
setItem: (k, v) => {
|
|
157
|
+
map.set(k, v);
|
|
158
|
+
},
|
|
159
|
+
removeItem: (k) => {
|
|
160
|
+
map.delete(k);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function createSupabaseClient(config, storage) {
|
|
165
|
+
return createClient(config.supabaseUrl, config.supabaseAnonKey, {
|
|
166
|
+
auth: {
|
|
167
|
+
flowType: "pkce",
|
|
168
|
+
persistSession: false,
|
|
169
|
+
autoRefreshToken: false,
|
|
170
|
+
detectSessionInUrl: false,
|
|
171
|
+
storage
|
|
172
|
+
},
|
|
173
|
+
global: {
|
|
174
|
+
headers: {
|
|
175
|
+
"X-Requested-By": "comerge-cli"
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function toStoredSession(session) {
|
|
181
|
+
if (!session.access_token || !session.refresh_token || !session.expires_at) {
|
|
182
|
+
throw new RemixError("Supabase session is missing required fields.", { exitCode: 1 });
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
access_token: session.access_token,
|
|
186
|
+
refresh_token: session.refresh_token,
|
|
187
|
+
expires_at: session.expires_at,
|
|
188
|
+
token_type: session.token_type ?? void 0,
|
|
189
|
+
user: session.user ? { id: session.user.id, email: session.user.email ?? null } : void 0
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function createSupabaseAuthHelpers(config) {
|
|
193
|
+
const storage = createInMemoryStorage();
|
|
194
|
+
const supabase = createSupabaseClient(config, storage);
|
|
195
|
+
return {
|
|
196
|
+
async startGoogleLogin(params) {
|
|
197
|
+
const { data, error } = await supabase.auth.signInWithOAuth({
|
|
198
|
+
provider: "google",
|
|
199
|
+
options: {
|
|
200
|
+
redirectTo: params.redirectTo,
|
|
201
|
+
skipBrowserRedirect: true,
|
|
202
|
+
queryParams: {
|
|
203
|
+
access_type: "offline",
|
|
204
|
+
prompt: "consent"
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
if (error) throw error;
|
|
209
|
+
if (!data?.url) throw new RemixError("Supabase did not return an OAuth URL.", { exitCode: 1 });
|
|
210
|
+
return { url: data.url };
|
|
211
|
+
},
|
|
212
|
+
async exchangeCode(params) {
|
|
213
|
+
const { data, error } = await supabase.auth.exchangeCodeForSession(params.code);
|
|
214
|
+
if (error) throw error;
|
|
215
|
+
if (!data?.session) throw new RemixError("Supabase did not return a session.", { exitCode: 1 });
|
|
216
|
+
return toStoredSession(data.session);
|
|
217
|
+
},
|
|
218
|
+
async refreshWithStoredSession(params) {
|
|
219
|
+
const { data, error } = await supabase.auth.setSession({
|
|
220
|
+
access_token: params.session.access_token,
|
|
221
|
+
refresh_token: params.session.refresh_token
|
|
222
|
+
});
|
|
223
|
+
if (error) throw error;
|
|
224
|
+
if (!data?.session) throw new RemixError("No session returned after refresh.", { exitCode: 1 });
|
|
225
|
+
return toStoredSession(data.session);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export {
|
|
231
|
+
storedSessionSchema,
|
|
232
|
+
createLocalSessionStore,
|
|
233
|
+
shouldRefreshSoon,
|
|
234
|
+
createStoredSessionTokenProvider,
|
|
235
|
+
createSupabaseAuthHelpers
|
|
236
|
+
};
|