@leitwerk-dev/session-transfer 0.0.0-bootstrap.0 → 0.2.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.
- package/dist/LICENSE +13 -0
- package/dist/archive.d.ts +104 -0
- package/dist/archive.d.ts.map +1 -0
- package/dist/format.d.ts +215 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +674 -0
- package/dist/index.js.map +1 -0
- package/dist/project-evidence.d.ts +7 -0
- package/dist/project-evidence.d.ts.map +1 -0
- package/dist/symlink-confinement.d.ts +3 -0
- package/dist/symlink-confinement.d.ts.map +1 -0
- package/package.json +40 -14
- package/src/archive.ts +503 -0
- package/src/format.ts +392 -0
- package/src/index.ts +28 -0
- package/src/project-evidence.ts +48 -0
- package/src/symlink-confinement.ts +38 -0
- package/README.md +0 -1
package/src/format.ts
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
|
|
4
|
+
/** @internal */
|
|
5
|
+
export const SESSION_TRANSFER_CONTENT_TYPE = "application/vnd.leitwerk.session-transfer+tar+zstd";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const SESSION_TRANSFER_ATTEMPT_STATES = [
|
|
9
|
+
"queued",
|
|
10
|
+
"exporting",
|
|
11
|
+
"awaiting_ack",
|
|
12
|
+
"cancelled",
|
|
13
|
+
"failed",
|
|
14
|
+
"consumed",
|
|
15
|
+
] as const;
|
|
16
|
+
/** @internal */
|
|
17
|
+
export type SessionTransferAttemptState = (typeof SESSION_TRANSFER_ATTEMPT_STATES)[number];
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
export const SESSION_TRANSFER_PHASES = [
|
|
21
|
+
"queued",
|
|
22
|
+
"waiting_for_execution_chain",
|
|
23
|
+
"stopping_worker",
|
|
24
|
+
"starting_exporter",
|
|
25
|
+
"scanning",
|
|
26
|
+
"ready_to_stream",
|
|
27
|
+
"streaming",
|
|
28
|
+
"awaiting_ack",
|
|
29
|
+
"consumed",
|
|
30
|
+
"cancelled",
|
|
31
|
+
"failed",
|
|
32
|
+
] as const;
|
|
33
|
+
/** @internal */
|
|
34
|
+
export type SessionTransferPhase = (typeof SESSION_TRANSFER_PHASES)[number];
|
|
35
|
+
|
|
36
|
+
/** Derives the public coarse status from the persisted lifecycle phase. @internal */
|
|
37
|
+
export function sessionTransferAttemptStateForPhase(
|
|
38
|
+
phase: SessionTransferPhase,
|
|
39
|
+
): SessionTransferAttemptState {
|
|
40
|
+
switch (phase) {
|
|
41
|
+
case "queued":
|
|
42
|
+
case "waiting_for_execution_chain":
|
|
43
|
+
return "queued";
|
|
44
|
+
case "stopping_worker":
|
|
45
|
+
case "starting_exporter":
|
|
46
|
+
case "scanning":
|
|
47
|
+
case "ready_to_stream":
|
|
48
|
+
case "streaming":
|
|
49
|
+
return "exporting";
|
|
50
|
+
case "awaiting_ack":
|
|
51
|
+
case "consumed":
|
|
52
|
+
case "cancelled":
|
|
53
|
+
case "failed":
|
|
54
|
+
return phase;
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`Unknown session transfer phase: ${phase}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** @internal */
|
|
61
|
+
export interface SessionTransferAttemptWire {
|
|
62
|
+
/** @internal */
|
|
63
|
+
id: string;
|
|
64
|
+
/** @internal */
|
|
65
|
+
grantId: string;
|
|
66
|
+
/** @internal */
|
|
67
|
+
instanceId: string;
|
|
68
|
+
/** Derived from phase; it is not persisted in the server database. @internal */
|
|
69
|
+
state: SessionTransferAttemptState;
|
|
70
|
+
/** @internal */
|
|
71
|
+
phase: SessionTransferPhase;
|
|
72
|
+
/** @internal */
|
|
73
|
+
leaseUntil: string;
|
|
74
|
+
/** @internal */
|
|
75
|
+
hardDeadline: string;
|
|
76
|
+
/** @internal */
|
|
77
|
+
entriesTotal: number | null;
|
|
78
|
+
/** @internal */
|
|
79
|
+
logicalBytesTotal: number | null;
|
|
80
|
+
/** @internal */
|
|
81
|
+
compressedBytes: number | null;
|
|
82
|
+
/** @internal */
|
|
83
|
+
streamSha256: string | null;
|
|
84
|
+
/** @internal */
|
|
85
|
+
failureCode: string | null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const nonBlankStringSchema = v.pipe(
|
|
89
|
+
v.string(),
|
|
90
|
+
v.check((value) => value.trim().length > 0, "Expected a non-empty string"),
|
|
91
|
+
);
|
|
92
|
+
const safeIntegerSchema = v.pipe(v.number(), v.safeInteger());
|
|
93
|
+
const positiveSafeIntegerSchema = v.pipe(safeIntegerSchema, v.minValue(1));
|
|
94
|
+
const nonNegativeSafeIntegerSchema = v.pipe(safeIntegerSchema, v.minValue(0));
|
|
95
|
+
const normalizedRelativePathSchema = v.pipe(
|
|
96
|
+
nonBlankStringSchema,
|
|
97
|
+
v.check((value) => {
|
|
98
|
+
try {
|
|
99
|
+
return assertSafeRelativePath(value) === value;
|
|
100
|
+
} catch {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}, "Expected a normalized relative path"),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
/** @internal */
|
|
107
|
+
export const sessionTransferLimitsSchema = v.object({
|
|
108
|
+
/** @internal */
|
|
109
|
+
maxEntries: positiveSafeIntegerSchema,
|
|
110
|
+
/** @internal */
|
|
111
|
+
maxLogicalBytes: positiveSafeIntegerSchema,
|
|
112
|
+
/** @internal */
|
|
113
|
+
maxCompressedBytes: positiveSafeIntegerSchema,
|
|
114
|
+
});
|
|
115
|
+
/** @internal */
|
|
116
|
+
export type SessionTransferLimits = v.InferOutput<typeof sessionTransferLimitsSchema>;
|
|
117
|
+
|
|
118
|
+
/** @internal */
|
|
119
|
+
export const DEFAULT_SESSION_TRANSFER_LIMITS: SessionTransferLimits = {
|
|
120
|
+
maxEntries: 250_000,
|
|
121
|
+
maxLogicalBytes: 20 * 1024 * 1024 * 1024,
|
|
122
|
+
maxCompressedBytes: 10 * 1024 * 1024 * 1024,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** @internal */
|
|
126
|
+
export const leitwerkTransferManifestV1Schema = v.object({
|
|
127
|
+
/** @internal */
|
|
128
|
+
version: v.literal(1, "Unsupported transfer manifest version"),
|
|
129
|
+
/** @internal */
|
|
130
|
+
instanceId: nonBlankStringSchema,
|
|
131
|
+
/** @internal */
|
|
132
|
+
createdAt: v.pipe(
|
|
133
|
+
nonBlankStringSchema,
|
|
134
|
+
v.check((value) => Number.isFinite(Date.parse(value)), "Expected an ISO datetime"),
|
|
135
|
+
),
|
|
136
|
+
/** @internal */
|
|
137
|
+
session: v.object({
|
|
138
|
+
/** @internal */
|
|
139
|
+
sourceCwd: nonBlankStringSchema,
|
|
140
|
+
/** @internal */
|
|
141
|
+
cwdRelativeToWorkspace: v.nullable(v.union([v.literal("."), normalizedRelativePathSchema])),
|
|
142
|
+
}),
|
|
143
|
+
/** @internal */
|
|
144
|
+
projects: v.array(
|
|
145
|
+
v.object({
|
|
146
|
+
/** @internal */
|
|
147
|
+
key: nonBlankStringSchema,
|
|
148
|
+
/** @internal */
|
|
149
|
+
relativePath: normalizedRelativePathSchema,
|
|
150
|
+
/** @internal */
|
|
151
|
+
branch: v.nullable(v.string()),
|
|
152
|
+
/** @internal */
|
|
153
|
+
head: v.nullable(v.string()),
|
|
154
|
+
}),
|
|
155
|
+
),
|
|
156
|
+
});
|
|
157
|
+
/** @internal */
|
|
158
|
+
export type LeitwerkTransferManifestV1 = v.InferOutput<typeof leitwerkTransferManifestV1Schema>;
|
|
159
|
+
|
|
160
|
+
/** @internal */
|
|
161
|
+
export const sessionTransferPreflightSchema = v.object({
|
|
162
|
+
/** @internal */
|
|
163
|
+
entriesTotal: v.pipe(nonNegativeSafeIntegerSchema, v.minValue(3)),
|
|
164
|
+
/** @internal */
|
|
165
|
+
logicalBytesTotal: nonNegativeSafeIntegerSchema,
|
|
166
|
+
});
|
|
167
|
+
/** @internal */
|
|
168
|
+
export type SessionTransferPreflight = v.InferOutput<typeof sessionTransferPreflightSchema>;
|
|
169
|
+
|
|
170
|
+
/** @internal */
|
|
171
|
+
export const sessionTransferPreflightReportSchema = v.object({
|
|
172
|
+
/** @internal */
|
|
173
|
+
manifest: leitwerkTransferManifestV1Schema,
|
|
174
|
+
/** @internal */
|
|
175
|
+
preflight: sessionTransferPreflightSchema,
|
|
176
|
+
});
|
|
177
|
+
/** @internal */
|
|
178
|
+
export type SessionTransferPreflightReport = v.InferOutput<
|
|
179
|
+
typeof sessionTransferPreflightReportSchema
|
|
180
|
+
>;
|
|
181
|
+
|
|
182
|
+
/** @internal */
|
|
183
|
+
export interface TransferArchiveProgress {
|
|
184
|
+
/** @internal */
|
|
185
|
+
entriesProcessed: number;
|
|
186
|
+
/** @internal */
|
|
187
|
+
logicalBytesProcessed: number;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** @internal */
|
|
191
|
+
export const sessionTransferHelperSpecSchema = v.object({
|
|
192
|
+
/** @internal */
|
|
193
|
+
manifest: leitwerkTransferManifestV1Schema,
|
|
194
|
+
/** @internal */
|
|
195
|
+
limits: sessionTransferLimitsSchema,
|
|
196
|
+
});
|
|
197
|
+
/** @internal */
|
|
198
|
+
export type SessionTransferHelperSpec = v.InferOutput<typeof sessionTransferHelperSpecSchema>;
|
|
199
|
+
|
|
200
|
+
/** @internal */
|
|
201
|
+
export interface ParsedTransferLink {
|
|
202
|
+
/** @internal */
|
|
203
|
+
origin: string;
|
|
204
|
+
/** @internal */
|
|
205
|
+
instanceId: string;
|
|
206
|
+
/** @internal */
|
|
207
|
+
grantId: string;
|
|
208
|
+
/** @internal */
|
|
209
|
+
token: string;
|
|
210
|
+
/** @internal */
|
|
211
|
+
grantUrl: string;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function object(value: unknown, label: string): Record<string, unknown> {
|
|
215
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
216
|
+
throw new Error(`${label} must be an object`);
|
|
217
|
+
}
|
|
218
|
+
return value as Record<string, unknown>;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function string(value: unknown, label: string): string {
|
|
222
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
223
|
+
throw new Error(`${label} must be a non-empty string`);
|
|
224
|
+
}
|
|
225
|
+
return value;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** @internal */
|
|
229
|
+
export function parsePiSessionHeader(content: string): Record<string, unknown> {
|
|
230
|
+
const firstLine = content.split(/\r?\n/, 1)[0];
|
|
231
|
+
if (!firstLine) throw new Error("Pi session is empty");
|
|
232
|
+
const value = JSON.parse(firstLine) as unknown;
|
|
233
|
+
if (
|
|
234
|
+
!value ||
|
|
235
|
+
typeof value !== "object" ||
|
|
236
|
+
Array.isArray(value) ||
|
|
237
|
+
(value as Record<string, unknown>).type !== "session"
|
|
238
|
+
) {
|
|
239
|
+
throw new Error("Pi session header is invalid");
|
|
240
|
+
}
|
|
241
|
+
return value as Record<string, unknown>;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** @internal */
|
|
245
|
+
export function isPathInside(root: string, candidate: string): boolean {
|
|
246
|
+
const relative = path.relative(path.resolve(root), path.resolve(candidate));
|
|
247
|
+
return (
|
|
248
|
+
relative === "" ||
|
|
249
|
+
(!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative))
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** @internal */
|
|
254
|
+
export function assertSafeRelativePath(value: string, label = "path"): string {
|
|
255
|
+
if (value.includes("\0") || path.posix.isAbsolute(value) || path.win32.isAbsolute(value)) {
|
|
256
|
+
throw new Error(`${label} must be relative`);
|
|
257
|
+
}
|
|
258
|
+
const normalized = path.posix.normalize(value.replaceAll("\\", "/"));
|
|
259
|
+
if (normalized === ".." || normalized.startsWith("../") || normalized === ".") {
|
|
260
|
+
throw new Error(`${label} escapes its root`);
|
|
261
|
+
}
|
|
262
|
+
return normalized;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** @internal */
|
|
266
|
+
export function parseTransferManifest(value: unknown): LeitwerkTransferManifestV1 {
|
|
267
|
+
return v.parse(leitwerkTransferManifestV1Schema, value);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** @internal */
|
|
271
|
+
export function parseSessionTransferHelperSpec(value: unknown): SessionTransferHelperSpec {
|
|
272
|
+
return v.parse(sessionTransferHelperSpecSchema, value);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const TRANSFER_ID_PATTERN = /^[A-Za-z0-9_.-]+$/;
|
|
276
|
+
|
|
277
|
+
function decodeTransferId(value: string, label: string): string {
|
|
278
|
+
let decoded: string;
|
|
279
|
+
try {
|
|
280
|
+
decoded = decodeURIComponent(value);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
throw new Error(`Transfer link has an invalid ${label}`, { cause: error });
|
|
283
|
+
}
|
|
284
|
+
// IDs are used to construct subsequent request paths. Reject encoded path
|
|
285
|
+
// separators rather than allowing a single route parameter to become a
|
|
286
|
+
// different path when it is re-encoded by the client.
|
|
287
|
+
if (!TRANSFER_ID_PATTERN.test(decoded)) {
|
|
288
|
+
throw new Error(`Transfer link has an invalid ${label}`);
|
|
289
|
+
}
|
|
290
|
+
return decoded;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** @internal */
|
|
294
|
+
export function parseTransferLink(raw: string): ParsedTransferLink {
|
|
295
|
+
let url: URL;
|
|
296
|
+
try {
|
|
297
|
+
url = new URL(raw.trim());
|
|
298
|
+
} catch {
|
|
299
|
+
throw new Error("Transfer link is not a valid URL");
|
|
300
|
+
}
|
|
301
|
+
const loopback =
|
|
302
|
+
url.hostname === "127.0.0.1" ||
|
|
303
|
+
url.hostname === "localhost" ||
|
|
304
|
+
url.hostname === "::1" ||
|
|
305
|
+
url.hostname === "[::1]";
|
|
306
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && loopback)) {
|
|
307
|
+
throw new Error("Transfer links require HTTPS (HTTP is allowed only for loopback development)");
|
|
308
|
+
}
|
|
309
|
+
if (url.username || url.password || url.search)
|
|
310
|
+
throw new Error("Transfer link contains unexpected URL credentials or query parameters");
|
|
311
|
+
const match = /^\/api\/session-transfers\/([^/]+)\/([^/]+)$/.exec(url.pathname);
|
|
312
|
+
if (!match) throw new Error("Transfer link has an unexpected path");
|
|
313
|
+
const instanceId = decodeTransferId(match[1] as string, "instance id");
|
|
314
|
+
const grantId = decodeTransferId(match[2] as string, "grant id");
|
|
315
|
+
const token = new URLSearchParams(url.hash.slice(1)).get("token");
|
|
316
|
+
if (!token || token.length < 32)
|
|
317
|
+
throw new Error("Transfer link is missing its bearer token fragment");
|
|
318
|
+
return {
|
|
319
|
+
origin: url.origin,
|
|
320
|
+
instanceId,
|
|
321
|
+
grantId,
|
|
322
|
+
token,
|
|
323
|
+
grantUrl: `${url.origin}/api/session-transfers/${encodeURIComponent(instanceId)}/${encodeURIComponent(grantId)}`,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** @internal */
|
|
328
|
+
export interface RewrittenPiSession {
|
|
329
|
+
/** @internal */
|
|
330
|
+
content: string;
|
|
331
|
+
/** @internal */
|
|
332
|
+
header: Record<string, unknown>;
|
|
333
|
+
/** @internal */
|
|
334
|
+
entryCount: number;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** @internal */
|
|
338
|
+
export function rewritePiSession(
|
|
339
|
+
content: string,
|
|
340
|
+
localCwd: string,
|
|
341
|
+
expectedSourceCwd?: string,
|
|
342
|
+
): RewrittenPiSession {
|
|
343
|
+
const lines = content.split(/\r?\n/).filter((line) => line.trim() !== "");
|
|
344
|
+
if (lines.length === 0) throw new Error("Transferred Pi session is empty");
|
|
345
|
+
const parsed = lines.map((line, index) => {
|
|
346
|
+
try {
|
|
347
|
+
return object(JSON.parse(line), `session line ${index + 1}`);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
if (error instanceof SyntaxError)
|
|
350
|
+
throw new Error(`Session line ${index + 1} is not valid JSON`, { cause: error });
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
const sourceHeader = parsePiSessionHeader(content);
|
|
355
|
+
if (sourceHeader.version !== 3) {
|
|
356
|
+
throw new Error("unsupported_pi_session_version");
|
|
357
|
+
}
|
|
358
|
+
string(sourceHeader.id, "session header.id");
|
|
359
|
+
const sourceCwd = string(sourceHeader.cwd, "session header.cwd");
|
|
360
|
+
if (!path.isAbsolute(sourceCwd)) throw new Error("Transferred session cwd must be absolute");
|
|
361
|
+
if (expectedSourceCwd !== undefined && sourceCwd !== expectedSourceCwd) {
|
|
362
|
+
throw new Error("Transferred session cwd does not match the transfer manifest");
|
|
363
|
+
}
|
|
364
|
+
const timestamp = string(sourceHeader.timestamp, "session header.timestamp");
|
|
365
|
+
if (
|
|
366
|
+
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.test(timestamp) ||
|
|
367
|
+
!Number.isFinite(Date.parse(timestamp))
|
|
368
|
+
) {
|
|
369
|
+
throw new Error("Transferred session timestamp must be an ISO datetime");
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const ids = new Set<string>();
|
|
373
|
+
for (let index = 1; index < parsed.length; index += 1) {
|
|
374
|
+
const entry = parsed[index] as Record<string, unknown>;
|
|
375
|
+
if (entry.type === "session") throw new Error("Transferred session contains a second header");
|
|
376
|
+
string(entry.type, `session line ${index + 1}.type`);
|
|
377
|
+
const id = string(entry.id, `session line ${index + 1}.id`);
|
|
378
|
+
if (ids.has(id)) throw new Error(`Transferred session contains duplicate entry id '${id}'`);
|
|
379
|
+
const parentId = entry.parentId;
|
|
380
|
+
if (parentId !== null && (typeof parentId !== "string" || !ids.has(parentId))) {
|
|
381
|
+
throw new Error(`Transferred session entry '${id}' has an invalid parent`);
|
|
382
|
+
}
|
|
383
|
+
ids.add(id);
|
|
384
|
+
}
|
|
385
|
+
const { parentSession: _parentSession, ...rest } = sourceHeader;
|
|
386
|
+
const header = { ...rest, cwd: path.resolve(localCwd) };
|
|
387
|
+
return {
|
|
388
|
+
content: `${[header, ...parsed.slice(1)].map((entry) => JSON.stringify(entry)).join("\n")}\n`,
|
|
389
|
+
header,
|
|
390
|
+
entryCount: parsed.length - 1,
|
|
391
|
+
};
|
|
392
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createTransferArchive,
|
|
3
|
+
extractTransferArchive,
|
|
4
|
+
prepareTransferArchive,
|
|
5
|
+
scanPortableWorkspace,
|
|
6
|
+
} from "./archive.js";
|
|
7
|
+
export type {
|
|
8
|
+
LeitwerkTransferManifestV1,
|
|
9
|
+
ParsedTransferLink,
|
|
10
|
+
SessionTransferAttemptWire,
|
|
11
|
+
SessionTransferLimits,
|
|
12
|
+
SessionTransferPhase,
|
|
13
|
+
SessionTransferPreflight,
|
|
14
|
+
SessionTransferPreflightReport,
|
|
15
|
+
TransferArchiveProgress,
|
|
16
|
+
} from "./format.js";
|
|
17
|
+
export {
|
|
18
|
+
DEFAULT_SESSION_TRANSFER_LIMITS,
|
|
19
|
+
isPathInside,
|
|
20
|
+
parsePiSessionHeader,
|
|
21
|
+
parseSessionTransferHelperSpec,
|
|
22
|
+
parseTransferLink,
|
|
23
|
+
rewritePiSession,
|
|
24
|
+
SESSION_TRANSFER_CONTENT_TYPE,
|
|
25
|
+
sessionTransferAttemptStateForPhase,
|
|
26
|
+
sessionTransferPreflightReportSchema,
|
|
27
|
+
} from "./format.js";
|
|
28
|
+
export { readProjectEvidence } from "./project-evidence.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { lstat } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import type { LeitwerkTransferManifestV1 } from "./format.js";
|
|
6
|
+
import { isPathInside } from "./format.js";
|
|
7
|
+
|
|
8
|
+
const execFileAsync = promisify(execFile);
|
|
9
|
+
|
|
10
|
+
/** @internal */
|
|
11
|
+
type TransferProject = LeitwerkTransferManifestV1["projects"][number];
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export async function readProjectEvidence(
|
|
15
|
+
workspaceRoot: string,
|
|
16
|
+
projects: readonly TransferProject[],
|
|
17
|
+
signal?: AbortSignal,
|
|
18
|
+
): Promise<TransferProject[]> {
|
|
19
|
+
return Promise.all(
|
|
20
|
+
projects.map(async (project) => {
|
|
21
|
+
const projectRoot = path.resolve(workspaceRoot, ...project.relativePath.split("/"));
|
|
22
|
+
if (!isPathInside(workspaceRoot, projectRoot)) {
|
|
23
|
+
throw new Error(`Project '${project.key}' escapes the workspace`);
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
await lstat(path.join(projectRoot, ".git"));
|
|
27
|
+
} catch {
|
|
28
|
+
throw new Error(`Project '${project.key}' is not a Git worktree`);
|
|
29
|
+
}
|
|
30
|
+
const [head = "", branch = ""] = (
|
|
31
|
+
await execFileAsync(
|
|
32
|
+
"git",
|
|
33
|
+
["-C", projectRoot, "rev-parse", "HEAD", "--abbrev-ref", "HEAD"],
|
|
34
|
+
{
|
|
35
|
+
signal,
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
).stdout
|
|
39
|
+
.trim()
|
|
40
|
+
.split("\n");
|
|
41
|
+
return {
|
|
42
|
+
...project,
|
|
43
|
+
head: head || null,
|
|
44
|
+
branch: branch && branch !== "HEAD" ? branch : null,
|
|
45
|
+
};
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
/** Resolve each component before interpreting '..', including targets that do not exist yet. */
|
|
4
|
+
export function assertConfinedSymlinks(links: ReadonlyMap<string, string>): void {
|
|
5
|
+
const resolvedLinks = new Map<string, string[]>();
|
|
6
|
+
const resolving = new Set<string>();
|
|
7
|
+
const resolve = (segments: readonly string[], initial: readonly string[] = []): string[] => {
|
|
8
|
+
let resolved = [...initial];
|
|
9
|
+
for (const segment of segments) {
|
|
10
|
+
if (segment === "" || segment === ".") continue;
|
|
11
|
+
if (segment === "..") {
|
|
12
|
+
if (resolved.length === 0) throw new Error("Symlink escapes the workspace");
|
|
13
|
+
resolved.pop();
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const candidate = [...resolved, segment].join("/");
|
|
17
|
+
const target = links.get(candidate);
|
|
18
|
+
if (target === undefined) {
|
|
19
|
+
resolved.push(segment);
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (path.posix.isAbsolute(target) || path.win32.isAbsolute(target)) {
|
|
23
|
+
throw new Error(`Absolute symlink is not portable: ${candidate}`);
|
|
24
|
+
}
|
|
25
|
+
if (resolving.has(candidate)) throw new Error(`Symlink cycle in workspace: ${candidate}`);
|
|
26
|
+
let destination = resolvedLinks.get(candidate);
|
|
27
|
+
if (!destination) {
|
|
28
|
+
resolving.add(candidate);
|
|
29
|
+
destination = resolve(target.split("/"), resolved);
|
|
30
|
+
resolving.delete(candidate);
|
|
31
|
+
resolvedLinks.set(candidate, destination);
|
|
32
|
+
}
|
|
33
|
+
resolved = [...destination];
|
|
34
|
+
}
|
|
35
|
+
return resolved;
|
|
36
|
+
};
|
|
37
|
+
for (const name of links.keys()) resolve(name.split("/"));
|
|
38
|
+
}
|
package/README.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Registration placeholder only. Not a usable Leitwerk release.
|