Repository navigation
Expand file tree
/
Copy pathdev-sandbox.mjs
More file actions
142 lines (141 loc) · 4.73 KB
/
Copy pathdev-sandbox.mjs
File metadata and controls
142 lines (141 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { randomBytes, randomUUID } from "node:crypto";
import { mkdir, mkdtemp, writeFile, rm } from "node:fs/promises";
import { resolve, join } from "node:path";
import { once } from "node:events";
import { PGlite } from "@electric-sql/pglite";
import { Database } from "../dist/server/db.js";
import { migrate } from "../dist/server/migrations.js";
import { LocalAssets } from "../dist/server/assets.js";
import { Auth } from "../dist/server/auth.js";
import { createApp } from "../dist/server/app.js";
// Deliberately never loads .env, configFromEnv, PostgreSQL URLs or S3 credentials.
// Every invocation has an independent in-memory database and loopback listener.
const root = resolve(import.meta.dirname, "..");
process.chdir(root);
await mkdir(join(root, ".local"), { recursive: true, mode: 0o700 });
const directory = await mkdtemp(join(root, ".local/sandbox-"));
const pg = new PGlite();
const db = new Database(pg);
const email = "owner@example.test";
const password = randomBytes(24).toString("base64url");
let server;
let closing;
async function close() {
if (closing) return closing;
closing = (async () => {
if (server?.listening) {
server.closeAllConnections();
await new Promise((done) => server.close(done));
}
await pg.close();
await rm(directory, { recursive: true, force: true });
})();
return closing;
}
for (const signal of ["SIGINT", "SIGTERM"])
process.once(signal, () => {
void close();
});
try {
await migrate(db);
await new Auth(db).bootstrap(email, "Sandbox owner", password);
const config = {
production: false,
appOrigin: "http://127.0.0.1",
port: 0,
databaseUrl: "sandbox-only",
assetDriver: "local",
assetDirectory: join(directory, "assets"),
organizationId: randomUUID(),
trustProxyHops: 0,
databasePoolMax: 1,
turnstileSiteKey: "1x00000000000000000000AA",
turnstileSecretKey: "1x0000000000000000000000000000000AA",
};
const app = createApp(config, db, new LocalAssets(config.assetDirectory));
server = app.listen(0, "127.0.0.1");
await once(server, "listening");
config.port = server.address().port;
config.appOrigin = `http://127.0.0.1:${config.port}`;
const post = (name, input, headers = {}) =>
fetch(`${config.appOrigin}/api/${name}`, {
method: "POST",
headers: {
Origin: config.appOrigin,
"Content-Type": "application/json",
...headers,
},
body: JSON.stringify(input),
signal: AbortSignal.timeout(10000),
});
const login = await post("auth.login", { email, password });
if (!login.ok) throw new Error("Sandbox sign-in failed");
const session = await login.json();
const headers = {
Cookie: login.headers.get("set-cookie").split(";")[0],
"X-CSRF-Token": session.data.csrf,
};
const created = await post(
"projects.create",
{ name: "Sandbox review", origins: ["https://example.com"] },
headers,
);
if (!created.ok) throw new Error("Sandbox project creation failed");
const project = (await created.json()).data;
const thread = await post(
"threads.create",
{
projectId: project.id,
body: "Synthetic review: make the primary action easier to find.",
context: { url: "https://example.com/", viewport: { width: 1280, height: 800 } },
idempotencyKey: "sandbox-seed",
},
headers,
);
if (!thread.ok) throw new Error("Sandbox thread creation failed");
const threadData = (await thread.json()).data;
const read = await post("threads.get", { threadId: threadData.id }, headers);
if (!read.ok || (await read.json()).data.id !== threadData.id)
throw new Error("Sandbox readback failed");
const denied = await post("projects.list", {});
if (denied.status !== 401) throw new Error("Sandbox anonymous access was not denied");
const ready = await fetch(`${config.appOrigin}/readyz`, {
signal: AbortSignal.timeout(10000),
});
if (!ready.ok) throw new Error("Sandbox readiness failed");
if (process.argv.includes("--smoke")) {
console.log(
JSON.stringify({
sandbox: "passed",
checks: [
"migrations",
"sign-in",
"project",
"thread",
"readback",
"anonymous-denial",
"readiness",
],
productionAccess: false,
}),
);
await close();
} else {
const accessFile = join(directory, "access.json");
await writeFile(
accessFile,
JSON.stringify(
{ url: config.appOrigin, email, password, projectId: project.id },
null,
2,
) + "\n",
{ mode: 0o600 },
);
console.log(
`Sandbox: ${config.appOrigin}\nLocal access file: ${accessFile}\nSynthetic data only. Stop with Ctrl+C; this sandbox is discarded.`,
);
}
} catch (error) {
await close();
throw error;
}