@foldrun/core 1.4.1 → 2.0.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.
@@ -1,113 +0,0 @@
1
- import type { DiffOp, Revision, RevisionFile, RevisionSummary } from "./history.ts";
2
- /** Where a workspace's bare repository lives. `@library` is the account shelf. */
3
- export declare function repoDir(tenant: string, scope: string): string;
4
- /** Is there a git binary? Checked once; an install without one keeps the journal. */
5
- export declare function gitAvailable(): boolean;
6
- export declare function repoExists(tenant: string, scope: string): boolean;
7
- /** Create the repository if it is not there. Idempotent. */
8
- export declare function ensureRepo(tenant: string, scope: string): string;
9
- export declare function headSha(tenant: string, scope: string, ref?: string): string | null;
10
- /**
11
- * Apply a change set as one commit on main, using plumbing against a scratch
12
- * index seeded from HEAD. No working copy, no checkout: each changed path is
13
- * hashed straight from its content into the object store.
14
- *
15
- * Returns the new commit, or null when the tree did not change.
16
- */
17
- export declare function commitChanges(tenant: string, scope: string, files: RevisionFile[], meta?: {
18
- by?: string;
19
- message?: string;
20
- at?: string;
21
- }): string | null;
22
- /** Commits on main, newest first, as the same summary the journal gave. */
23
- export declare function listCommits(tenant: string, scope: string, opts?: {
24
- forPath?: string;
25
- limit?: number;
26
- ref?: string;
27
- }): RevisionSummary[];
28
- /** One commit in full: every changed file with its before and after. */
29
- export declare function readCommit(tenant: string, scope: string, sha: string, forPath?: string): Revision | null;
30
- export interface RefInfo {
31
- name: string;
32
- sha: string;
33
- /** Commit subject and time, for a branch list that says what each is. */
34
- subject: string;
35
- at: string;
36
- }
37
- export declare function listRefs(tenant: string, scope: string, kind: "heads" | "tags"): RefInfo[];
38
- /**
39
- * The commit a restore means by `to`, on main's history:
40
- *
41
- * a sha or a prefix of one that commit
42
- * a branch or tag name where it points
43
- * an ISO date or time the last commit on main at or before it
44
- * "3d", "12h", "90m", "2w" the same, that long ago
45
- *
46
- * Null when nothing matches — before the first commit, or a name that is
47
- * not there. Throws on a `to` that cannot be any of these, so a typo is a
48
- * 400 rather than a silent "nothing found".
49
- */
50
- export declare function resolveRestorePoint(tenant: string, scope: string, to: string, now?: number): {
51
- sha: string;
52
- at: string;
53
- by: string;
54
- message: string;
55
- } | null;
56
- /** The files at a commit, for a browser or a deploy. */
57
- export declare function listTree(tenant: string, scope: string, ref?: string): {
58
- path: string;
59
- mode: string;
60
- }[];
61
- export declare function readBlob(tenant: string, scope: string, ref: string, p: string): string | null;
62
- /** The whole tree at a ref as deploy files — what a push turns into a workspace. */
63
- export declare function filesAt(tenant: string, scope: string, ref: string): {
64
- path: string;
65
- content: string;
66
- }[];
67
- /**
68
- * What a ref would change on main: every file that differs, with both sides.
69
- * The branch's own commits are `listCommits({ ref })`; this is the net
70
- * effect, which is what a reviewer reads.
71
- */
72
- export declare function diffRefs(tenant: string, scope: string, base: string, head: string): RevisionFile[];
73
- /**
74
- * Merge a branch into main and return the new main. A fast-forward when it
75
- * is one; otherwise a merge commit whose tree is the BRANCH's — the branch
76
- * is what the person reviewed and chose, and a three-way textual merge of
77
- * markdown with no one to resolve conflicts would deploy text nobody wrote.
78
- * Both parents are recorded, so history shows the merge for what it was.
79
- */
80
- export declare function mergeBranch(tenant: string, scope: string, branch: string, meta?: {
81
- by?: string;
82
- }): {
83
- sha: string;
84
- fastForward: boolean;
85
- };
86
- export declare function deleteBranch(tenant: string, scope: string, branch: string): void;
87
- /** Push main to another remote — a GitHub or GitLab mirror. Blocking; callers
88
- * run it off the request path. The token never touches disk: it is in the
89
- * URL for this one process and nowhere else. */
90
- export declare function pushMirror(tenant: string, scope: string, url: string): {
91
- ok: boolean;
92
- detail: string;
93
- };
94
- /**
95
- * Where the compiled pre-receive check is, found on disk rather than through
96
- * require.resolve — a bundler rewrites require.resolve("pkg") into a module
97
- * id, and the hook was once told to run a script called "51113". Searched
98
- * from the working directory upward: the monorepo layout, then an installed
99
- * package. Empty when there is no compiled check (a dev server on the source
100
- * tree), in which case a push is checked at deploy instead of refused — the
101
- * same rules, one step later.
102
- */
103
- export declare function hookScriptPath(): string;
104
- /**
105
- * The pre-receive hook: a push that fails the deploy checks is refused with
106
- * the errors in the pusher's terminal, instead of accepted and then not
107
- * deployed. The hook shells out to node with the compiled check, so it runs
108
- * the same rules as a dashboard deploy; which node and which script are
109
- * passed in the environment by the HTTP route, because a hook inherits git's
110
- * environment and nothing else.
111
- */
112
- export declare function installHooks(dir: string): void;
113
- export { type DiffOp };
@@ -1,404 +0,0 @@
1
- // A git repository per workspace — the platform as the remote.
2
- //
3
- // git remote add foldrun https://app.foldrun.io/git/<tenant>/<workspace>.git
4
- // git push foldrun main → deploys, commit on record
5
- // git clone …/<workspace>.git → the workspace, with its whole history
6
- //
7
- // One bare repository per workspace on the data volume. Every change the
8
- // platform makes — a dashboard save, a deploy, a deletion — is a commit into
9
- // it, made with git's own plumbing against a scratch index so no working copy
10
- // is ever kept. Every change a person makes is a push into it. So there is
11
- // one history, it is real git, and anyone can take it away with `git clone`.
12
- //
13
- // This deliberately stops at being a remote. Pull requests, reviews and
14
- // issues are a forge, which is a product; GitHub and GitLab remain a fine
15
- // mirror for anyone who wants that on top.
16
- //
17
- // history.ts delegates here when a repository exists, so the History page,
18
- // the per-file panel and Observe's eval attribution all read git without
19
- // knowing it. The journal it kept before stays as the fallback for an install
20
- // without a git binary.
21
- import fs from "node:fs";
22
- import os from "node:os";
23
- import path from "node:path";
24
- import { spawnSync } from "node:child_process";
25
- import { dataRoot } from "./paths.js";
26
- const SAFE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,120}$/;
27
- const safe = (v, what) => {
28
- if (!SAFE.test(v))
29
- throw new Error(`illegal ${what}: ${v}`);
30
- return v;
31
- };
32
- /** Where a workspace's bare repository lives. `@library` is the account shelf. */
33
- export function repoDir(tenant, scope) {
34
- safe(tenant, "tenant");
35
- const name = scope === "@library" ? "_library" : safe(scope, "workspace");
36
- return path.join(dataRoot(), tenant, ".git", `${name}.git`);
37
- }
38
- let gitChecked = null;
39
- /** Is there a git binary? Checked once; an install without one keeps the journal. */
40
- export function gitAvailable() {
41
- if (gitChecked === null) {
42
- gitChecked = spawnSync("git", ["--version"], { stdio: "ignore" }).status === 0;
43
- }
44
- return gitChecked;
45
- }
46
- function git(dir, args, opts = {}) {
47
- const r = spawnSync("git", ["--git-dir", dir, ...args], {
48
- input: opts.input,
49
- env: { ...process.env, GIT_TERMINAL_PROMPT: "0", ...opts.env },
50
- maxBuffer: 64 * 1024 * 1024,
51
- });
52
- if (r.status !== 0) {
53
- throw new Error(`git ${args[0]} failed: ${(r.stderr?.toString() ?? "").trim().slice(0, 500)}`);
54
- }
55
- return r.stdout.toString();
56
- }
57
- export function repoExists(tenant, scope) {
58
- return fs.existsSync(path.join(repoDir(tenant, scope), "HEAD"));
59
- }
60
- /** Create the repository if it is not there. Idempotent. */
61
- export function ensureRepo(tenant, scope) {
62
- const dir = repoDir(tenant, scope);
63
- if (!fs.existsSync(path.join(dir, "HEAD"))) {
64
- fs.mkdirSync(path.dirname(dir), { recursive: true });
65
- spawnSync("git", ["init", "--bare", "--quiet", "--initial-branch=main", dir], { stdio: "ignore" });
66
- // Pushes over HTTP need this; it is the one config a bare remote wants.
67
- git(dir, ["config", "http.receivepack", "true"]);
68
- }
69
- installHooks(dir);
70
- return dir;
71
- }
72
- export function headSha(tenant, scope, ref = "refs/heads/main") {
73
- if (!repoExists(tenant, scope))
74
- return null;
75
- const r = spawnSync("git", ["--git-dir", repoDir(tenant, scope), "rev-parse", "--verify", "--quiet", ref]);
76
- return r.status === 0 ? r.stdout.toString().trim() : null;
77
- }
78
- /**
79
- * Apply a change set as one commit on main, using plumbing against a scratch
80
- * index seeded from HEAD. No working copy, no checkout: each changed path is
81
- * hashed straight from its content into the object store.
82
- *
83
- * Returns the new commit, or null when the tree did not change.
84
- */
85
- export function commitChanges(tenant, scope, files, meta = {}) {
86
- const dir = ensureRepo(tenant, scope);
87
- const parent = headSha(tenant, scope);
88
- const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "foldrun-index-"));
89
- const index = path.join(scratch, "index");
90
- // update-index insists on a work tree even when every entry arrives as
91
- // --cacheinfo and nothing on disk is read. The scratch directory is one.
92
- const env = { GIT_INDEX_FILE: index, GIT_WORK_TREE: scratch };
93
- try {
94
- if (parent)
95
- git(dir, ["read-tree", parent], { env });
96
- for (const f of files) {
97
- if (f.after === null) {
98
- if (parent)
99
- git(dir, ["update-index", "--force-remove", "--", f.path], { env });
100
- continue;
101
- }
102
- const blob = git(dir, ["hash-object", "-w", "--stdin"], { input: f.after }).trim();
103
- // Executable where code lives, matching what the writers chmod on disk.
104
- const mode = /\.(py|sh|mjs|js|rb)$/.test(f.path) && /(^|\/)(scripts|tools)\//.test(f.path) ? "100755" : "100644";
105
- git(dir, ["update-index", "--add", "--cacheinfo", `${mode},${blob},${f.path}`], { env });
106
- }
107
- const tree = git(dir, ["write-tree"], { env }).trim();
108
- if (parent && git(dir, ["rev-parse", `${parent}^{tree}`]).trim() === tree)
109
- return null;
110
- const who = meta.by ?? "system";
111
- const email = who.includes("@") ? who : `${who}@foldrun`;
112
- const name = who.includes("@") ? who.split("@")[0] : who;
113
- const date = meta.at ? new Date(meta.at).toISOString() : new Date().toISOString();
114
- const commit = git(dir, ["commit-tree", tree, ...(parent ? ["-p", parent] : []), "-m", meta.message ?? describe(files)], {
115
- env: {
116
- GIT_AUTHOR_NAME: name, GIT_AUTHOR_EMAIL: email, GIT_AUTHOR_DATE: date,
117
- GIT_COMMITTER_NAME: "foldrun", GIT_COMMITTER_EMAIL: "platform@foldrun", GIT_COMMITTER_DATE: date,
118
- },
119
- }).trim();
120
- git(dir, ["update-ref", "refs/heads/main", commit, ...(parent ? [parent] : [])]);
121
- return commit;
122
- }
123
- finally {
124
- fs.rmSync(scratch, { recursive: true, force: true });
125
- }
126
- }
127
- function describe(files) {
128
- if (files.length === 1) {
129
- const f = files[0];
130
- return `${f.before === null ? "created" : f.after === null ? "deleted" : "edited"} ${f.path}`;
131
- }
132
- return `changed ${files.length} files`;
133
- }
134
- // ------------------------------------------------------------------ reading
135
- const SEP = "";
136
- /** Commits on main, newest first, as the same summary the journal gave. */
137
- export function listCommits(tenant, scope, opts = {}) {
138
- if (!headSha(tenant, scope, opts.ref))
139
- return [];
140
- const dir = repoDir(tenant, scope);
141
- const out = git(dir, [
142
- "log",
143
- opts.ref ?? "refs/heads/main",
144
- `--max-count=${opts.limit ?? 200}`,
145
- `--format=%H${SEP}%aI${SEP}%ae${SEP}%s`,
146
- "--name-only",
147
- ...(opts.forPath ? ["--", opts.forPath] : []),
148
- ]);
149
- const rows = [];
150
- let current = null;
151
- for (const line of out.split("\n")) {
152
- if (line.includes(SEP)) {
153
- const [id, at, by, message] = line.split(SEP);
154
- current = { id, at, by: by.endsWith("@foldrun") ? by.replace(/@foldrun$/, "") : by, message, commit: id, paths: [] };
155
- rows.push(current);
156
- }
157
- else if (line.trim() && current) {
158
- current.paths.push(line.trim());
159
- }
160
- }
161
- return rows;
162
- }
163
- /** One commit in full: every changed file with its before and after. */
164
- export function readCommit(tenant, scope, sha, forPath) {
165
- if (!repoExists(tenant, scope) || !/^[0-9a-f]{4,40}$/.test(sha))
166
- return null;
167
- const dir = repoDir(tenant, scope);
168
- const head = spawnSync("git", ["--git-dir", dir, "rev-parse", "--verify", "--quiet", `${sha}^{commit}`]);
169
- if (head.status !== 0)
170
- return null;
171
- const full = head.stdout.toString().trim();
172
- const [at, by, ...msg] = git(dir, ["log", "-1", `--format=%aI${SEP}%ae${SEP}%s`, full]).trim().split(SEP);
173
- const hasParent = spawnSync("git", ["--git-dir", dir, "rev-parse", "--verify", "--quiet", `${full}^`]).status === 0;
174
- const names = git(dir, [
175
- "diff-tree", "--no-commit-id", "--name-status", "-r", "--root", full,
176
- ...(forPath ? ["--", forPath] : []),
177
- ]);
178
- const files = [];
179
- for (const line of names.split("\n").filter(Boolean)) {
180
- const [status, p] = line.split("\t");
181
- const before = status === "A" || !hasParent ? null : blob(dir, `${full}^`, p);
182
- const after = status === "D" ? null : blob(dir, full, p);
183
- files.push({ path: p, before, after });
184
- }
185
- return {
186
- id: full,
187
- at,
188
- by: by.endsWith("@foldrun") ? by.replace(/@foldrun$/, "") : by,
189
- message: msg.join(SEP),
190
- commit: full,
191
- files,
192
- };
193
- }
194
- function blob(dir, rev, p) {
195
- const r = spawnSync("git", ["--git-dir", dir, "show", `${rev}:${p}`], { maxBuffer: 64 * 1024 * 1024 });
196
- return r.status === 0 ? r.stdout.toString() : null;
197
- }
198
- export function listRefs(tenant, scope, kind) {
199
- if (!repoExists(tenant, scope))
200
- return [];
201
- const out = git(repoDir(tenant, scope), [
202
- "for-each-ref", `refs/${kind}`, "--sort=-committerdate",
203
- `--format=%(refname:short)${SEP}%(objectname)${SEP}%(subject)${SEP}%(committerdate:iso-strict)`,
204
- ]);
205
- return out.split("\n").filter(Boolean).map((l) => {
206
- const [name, sha, subject, at] = l.split(SEP);
207
- return { name, sha, subject, at };
208
- });
209
- }
210
- /**
211
- * The commit a restore means by `to`, on main's history:
212
- *
213
- * a sha or a prefix of one that commit
214
- * a branch or tag name where it points
215
- * an ISO date or time the last commit on main at or before it
216
- * "3d", "12h", "90m", "2w" the same, that long ago
217
- *
218
- * Null when nothing matches — before the first commit, or a name that is
219
- * not there. Throws on a `to` that cannot be any of these, so a typo is a
220
- * 400 rather than a silent "nothing found".
221
- */
222
- export function resolveRestorePoint(tenant, scope, to, now = Date.now()) {
223
- const t = to.trim();
224
- if (!t || t.startsWith("-"))
225
- throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
226
- if (!headSha(tenant, scope))
227
- return null;
228
- const dir = repoDir(tenant, scope);
229
- let sha = null;
230
- const rel = /^(\d+)\s*(m|h|d|w)$/.exec(t);
231
- const moment = rel
232
- ? now - Number(rel[1]) * { m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 }[rel[2]]
233
- : /^\d{4}-\d{2}-\d{2}/.test(t)
234
- ? Date.parse(t)
235
- : null;
236
- if (moment !== null) {
237
- if (Number.isNaN(moment))
238
- throw Object.assign(new Error(`not a date: ${to}`), { status: 400 });
239
- sha = git(dir, ["log", "-1", "--format=%H", `--until=@${Math.floor(moment / 1000)}`, "refs/heads/main"]).trim() || null;
240
- }
241
- else {
242
- if (!/^[A-Za-z0-9][A-Za-z0-9._/-]{0,120}$/.test(t))
243
- throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
244
- for (const ref of [/^[0-9a-f]{4,40}$/.test(t) ? `${t}^{commit}` : null, `refs/heads/${t}^{commit}`, `refs/tags/${t}^{commit}`]) {
245
- if (!ref)
246
- continue;
247
- const r = spawnSync("git", ["--git-dir", dir, "rev-parse", "--verify", "--quiet", ref]);
248
- if (r.status === 0) {
249
- sha = r.stdout.toString().trim();
250
- break;
251
- }
252
- }
253
- }
254
- if (!sha)
255
- return null;
256
- const [at, by, ...msg] = git(dir, ["log", "-1", `--format=%cI${SEP}%ae${SEP}%s`, sha]).trim().split(SEP);
257
- return { sha, at, by: by.endsWith("@foldrun") ? by.replace(/@foldrun$/, "") : by, message: msg.join(SEP) };
258
- }
259
- /** The files at a commit, for a browser or a deploy. */
260
- export function listTree(tenant, scope, ref = "refs/heads/main") {
261
- if (!headSha(tenant, scope, ref))
262
- return [];
263
- return git(repoDir(tenant, scope), ["ls-tree", "-r", "--format=%(objectmode) %(path)", ref])
264
- .split("\n")
265
- .filter(Boolean)
266
- .map((l) => {
267
- const [mode, ...rest] = l.split(" ");
268
- return { mode, path: rest.join(" ") };
269
- });
270
- }
271
- export function readBlob(tenant, scope, ref, p) {
272
- if (!repoExists(tenant, scope))
273
- return null;
274
- return blob(repoDir(tenant, scope), ref, p);
275
- }
276
- /** The whole tree at a ref as deploy files — what a push turns into a workspace. */
277
- export function filesAt(tenant, scope, ref) {
278
- const dir = repoDir(tenant, scope);
279
- return listTree(tenant, scope, ref).flatMap((t) => {
280
- const content = blob(dir, ref, t.path);
281
- return content === null ? [] : [{ path: t.path, content }];
282
- });
283
- }
284
- /**
285
- * What a ref would change on main: every file that differs, with both sides.
286
- * The branch's own commits are `listCommits({ ref })`; this is the net
287
- * effect, which is what a reviewer reads.
288
- */
289
- export function diffRefs(tenant, scope, base, head) {
290
- if (!headSha(tenant, scope, base) || !headSha(tenant, scope, head))
291
- return [];
292
- const dir = repoDir(tenant, scope);
293
- const out = git(dir, ["diff", "--name-status", `${base}...${head}`]);
294
- return out.split("\n").filter(Boolean).map((line) => {
295
- const [status, p] = line.split("\t");
296
- return {
297
- path: p,
298
- before: status === "A" ? null : blob(dir, base, p),
299
- after: status === "D" ? null : blob(dir, head, p),
300
- };
301
- });
302
- }
303
- /**
304
- * Merge a branch into main and return the new main. A fast-forward when it
305
- * is one; otherwise a merge commit whose tree is the BRANCH's — the branch
306
- * is what the person reviewed and chose, and a three-way textual merge of
307
- * markdown with no one to resolve conflicts would deploy text nobody wrote.
308
- * Both parents are recorded, so history shows the merge for what it was.
309
- */
310
- export function mergeBranch(tenant, scope, branch, meta = {}) {
311
- const dir = repoDir(tenant, scope);
312
- const main = headSha(tenant, scope);
313
- const head = headSha(tenant, scope, `refs/heads/${safe(branch, "branch")}`);
314
- if (!head)
315
- throw new Error(`no branch "${branch}"`);
316
- if (!main) {
317
- git(dir, ["update-ref", "refs/heads/main", head]);
318
- return { sha: head, fastForward: true };
319
- }
320
- const isAncestor = spawnSync("git", ["--git-dir", dir, "merge-base", "--is-ancestor", main, head]).status === 0;
321
- if (isAncestor) {
322
- git(dir, ["update-ref", "refs/heads/main", head, main]);
323
- return { sha: head, fastForward: true };
324
- }
325
- const tree = git(dir, ["rev-parse", `${head}^{tree}`]).trim();
326
- const who = meta.by ?? "foldrun";
327
- const email = who.includes("@") ? who : `${who}@foldrun`;
328
- const name = who.includes("@") ? who.split("@")[0] : who;
329
- const date = new Date().toISOString();
330
- const sha = git(dir, ["commit-tree", tree, "-p", main, "-p", head, "-m", `merge ${branch} into main`], {
331
- env: {
332
- GIT_AUTHOR_NAME: name, GIT_AUTHOR_EMAIL: email, GIT_AUTHOR_DATE: date,
333
- GIT_COMMITTER_NAME: "foldrun", GIT_COMMITTER_EMAIL: "platform@foldrun", GIT_COMMITTER_DATE: date,
334
- },
335
- }).trim();
336
- git(dir, ["update-ref", "refs/heads/main", sha, main]);
337
- return { sha, fastForward: false };
338
- }
339
- export function deleteBranch(tenant, scope, branch) {
340
- if (branch === "main")
341
- throw new Error("main is the default branch");
342
- git(repoDir(tenant, scope), ["update-ref", "-d", `refs/heads/${safe(branch, "branch")}`]);
343
- }
344
- /** Push main to another remote — a GitHub or GitLab mirror. Blocking; callers
345
- * run it off the request path. The token never touches disk: it is in the
346
- * URL for this one process and nowhere else. */
347
- export function pushMirror(tenant, scope, url) {
348
- if (!headSha(tenant, scope))
349
- return { ok: false, detail: "nothing to push yet" };
350
- const r = spawnSync("git", ["--git-dir", repoDir(tenant, scope), "push", "--quiet", url, "+refs/heads/main:refs/heads/main"], {
351
- env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
352
- timeout: 60_000,
353
- });
354
- const detail = (r.stderr?.toString() ?? "").trim()
355
- // Never echo the URL back: it carries the token.
356
- .replaceAll(url, "<mirror>")
357
- .slice(0, 500);
358
- return { ok: r.status === 0, detail: r.status === 0 ? "pushed" : detail || "push failed" };
359
- }
360
- /**
361
- * Where the compiled pre-receive check is, found on disk rather than through
362
- * require.resolve — a bundler rewrites require.resolve("pkg") into a module
363
- * id, and the hook was once told to run a script called "51113". Searched
364
- * from the working directory upward: the monorepo layout, then an installed
365
- * package. Empty when there is no compiled check (a dev server on the source
366
- * tree), in which case a push is checked at deploy instead of refused — the
367
- * same rules, one step later.
368
- */
369
- export function hookScriptPath() {
370
- let dir = process.cwd();
371
- for (let i = 0; i < 6; i++) {
372
- for (const rel of ["packages/core/dist/src/git-hook.js", "node_modules/@foldrun/core/dist/src/git-hook.js"]) {
373
- const candidate = path.join(dir, rel);
374
- if (fs.existsSync(candidate))
375
- return candidate;
376
- }
377
- const parent = path.dirname(dir);
378
- if (parent === dir)
379
- break;
380
- dir = parent;
381
- }
382
- return "";
383
- }
384
- /**
385
- * The pre-receive hook: a push that fails the deploy checks is refused with
386
- * the errors in the pusher's terminal, instead of accepted and then not
387
- * deployed. The hook shells out to node with the compiled check, so it runs
388
- * the same rules as a dashboard deploy; which node and which script are
389
- * passed in the environment by the HTTP route, because a hook inherits git's
390
- * environment and nothing else.
391
- */
392
- export function installHooks(dir) {
393
- const hooks = path.join(dir, "hooks");
394
- fs.mkdirSync(hooks, { recursive: true });
395
- const hook = path.join(hooks, "pre-receive");
396
- const script = `#!/bin/sh
397
- # Installed by foldrun. Refuses a push to main whose tree fails the checks.
398
- [ -n "$FOLDRUN_HOOK_NODE" ] && [ -n "$FOLDRUN_HOOK_SCRIPT" ] || exit 0
399
- exec "$FOLDRUN_HOOK_NODE" "$FOLDRUN_HOOK_SCRIPT"
400
- `;
401
- if (!fs.existsSync(hook) || fs.readFileSync(hook, "utf8") !== script) {
402
- fs.writeFileSync(hook, script, { mode: 0o755 });
403
- }
404
- }
package/dist/src/tar.d.ts DELETED
@@ -1,30 +0,0 @@
1
- export interface TarEntry {
2
- path: string;
3
- content: Buffer;
4
- }
5
- /**
6
- * Every regular file in a gzipped tarball.
7
- *
8
- * Directories, symlinks, and pax/global headers are skipped: a workspace is
9
- * files, and following a link out of the archive is exactly the thing a deploy
10
- * from an untrusted repository must not do.
11
- */
12
- export declare function readTarGz(gz: Buffer): TarEntry[];
13
- /**
14
- * Drop the single wrapper directory an archive is rooted at.
15
- *
16
- * GitHub tarballs are rooted at `owner-repo-<sha>/`, which is not part of
17
- * anyone's workspace. Stripped only when *every* entry shares one first
18
- * segment, so an archive that is already flat is left alone.
19
- */
20
- export declare function stripRoot(entries: TarEntry[]): TarEntry[];
21
- /**
22
- * Read a tarball as the file list a deploy ships.
23
- *
24
- * `subdir` picks a workspace living inside a larger repository — the common
25
- * case, since a repo usually holds more than one thing.
26
- */
27
- export declare function filesFromTarball(gz: Buffer, subdir?: string): {
28
- path: string;
29
- content: string;
30
- }[];
package/dist/src/tar.js DELETED
@@ -1,112 +0,0 @@
1
- // Reading a .tar.gz, so a deploy can come from a GitHub tarball.
2
- //
3
- // `GET /repos/{owner}/{repo}/tarball/{sha}` is one HTTP call and needs no git
4
- // binary, no clone, and no working copy on the server — which is the whole
5
- // reason a markdown workspace can deploy from a webhook in a few hundred
6
- // milliseconds. What it returns is a tar.gz, and Node has gzip but not tar.
7
- //
8
- // Deliberately dependency-free, for the same reason the cron parser is: tar is
9
- // 512-byte blocks with an octal size field, and a reader is shorter than the
10
- // argument about which library to add.
11
- import zlib from "node:zlib";
12
- import { trimSlashes } from "./paths.js";
13
- const BLOCK = 512;
14
- const str = (b, off, len) => {
15
- const raw = b.subarray(off, off + len);
16
- const end = raw.indexOf(0);
17
- return raw.subarray(0, end === -1 ? raw.length : end).toString("utf8");
18
- };
19
- // Sizes are octal, space- or NUL-padded. GNU also writes base-256 for large
20
- // files, flagged by the high bit — irrelevant for markdown, but a silent
21
- // misparse would be worse than a clear refusal.
22
- function size(b, off) {
23
- if (b[off] & 0x80)
24
- throw new Error("tar: base-256 size field is not supported");
25
- const raw = str(b, off, 12).trim();
26
- return raw ? parseInt(raw, 8) : 0;
27
- }
28
- /**
29
- * Every regular file in a gzipped tarball.
30
- *
31
- * Directories, symlinks, and pax/global headers are skipped: a workspace is
32
- * files, and following a link out of the archive is exactly the thing a deploy
33
- * from an untrusted repository must not do.
34
- */
35
- export function readTarGz(gz) {
36
- const buf = zlib.gunzipSync(gz);
37
- const out = [];
38
- let longName = null;
39
- for (let off = 0; off + BLOCK <= buf.length;) {
40
- const header = buf.subarray(off, off + BLOCK);
41
- // Two consecutive zero blocks end the archive; one is enough to stop.
42
- if (header.every((b) => b === 0))
43
- break;
44
- const name = str(header, 0, 100);
45
- const bytes = size(header, 124);
46
- const type = String.fromCharCode(header[156]) || "0";
47
- const prefix = str(header, 345, 155);
48
- off += BLOCK;
49
- const body = buf.subarray(off, off + bytes);
50
- off += Math.ceil(bytes / BLOCK) * BLOCK;
51
- // GNU long name: the next header's name lives in this entry's body.
52
- if (type === "L") {
53
- longName = body.toString("utf8").replace(/\0+$/, "");
54
- continue;
55
- }
56
- if (type !== "0" && type !== "\0") {
57
- longName = null;
58
- continue;
59
- }
60
- const full = longName ?? (prefix ? `${prefix}/${name}` : name);
61
- longName = null;
62
- if (full)
63
- out.push({ path: full, content: Buffer.from(body) });
64
- }
65
- return out;
66
- }
67
- /**
68
- * Drop the single wrapper directory an archive is rooted at.
69
- *
70
- * GitHub tarballs are rooted at `owner-repo-<sha>/`, which is not part of
71
- * anyone's workspace. Stripped only when *every* entry shares one first
72
- * segment, so an archive that is already flat is left alone.
73
- */
74
- export function stripRoot(entries) {
75
- if (entries.length === 0)
76
- return entries;
77
- const first = entries[0].path.split("/")[0];
78
- if (!entries.every((e) => e.path.split("/")[0] === first && e.path.includes("/"))) {
79
- return entries;
80
- }
81
- return entries.map((e) => ({ ...e, path: e.path.slice(first.length + 1) }));
82
- }
83
- /**
84
- * Read a tarball as the file list a deploy ships.
85
- *
86
- * `subdir` picks a workspace living inside a larger repository — the common
87
- * case, since a repo usually holds more than one thing.
88
- */
89
- export function filesFromTarball(gz, subdir = "") {
90
- const prefix = trimSlashes(subdir, "both");
91
- const out = [];
92
- for (const entry of stripRoot(readTarGz(gz))) {
93
- let rel = entry.path;
94
- if (prefix) {
95
- if (!rel.startsWith(`${prefix}/`))
96
- continue;
97
- rel = rel.slice(prefix.length + 1);
98
- }
99
- // A tarball is untrusted input: an entry can name anything at all, and
100
- // `../` in an archive is the oldest way to write outside the destination.
101
- if (rel.split("/").includes("..") || rel.startsWith("/"))
102
- continue;
103
- // AppleDouble sidecars. macOS `tar` writes a `._name` beside every file to
104
- // carry metadata no other system wants; they are not part of anyone's
105
- // workspace, and letting one through means a deploy rejected for shipping
106
- // a file the author never wrote.
107
- if ((rel.split("/").pop() ?? "").startsWith("._"))
108
- continue;
109
- out.push({ path: rel, content: entry.content.toString("utf8") });
110
- }
111
- return out;
112
- }