-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmain.ts
More file actions
469 lines (408 loc) · 13.7 KB
/
main.ts
File metadata and controls
469 lines (408 loc) · 13.7 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { Hono, type MiddlewareHandler } from "@hono/hono";
import { logger } from "@hono/hono/logger";
import { parseArgs } from "@std/cli";
import * as colors from "@std/fmt/colors";
import { ensureDir } from "@std/fs";
import { dirname, join } from "@std/path";
import denoJSON from "../deno.json" with { type: "json" };
import {
BLOCKS_FOLDER,
DECO_FOLDER,
ENV_SITE_NAME,
} from "../engine/decofile/constants.ts";
import { genMetadata } from "../engine/decofile/fsFolder.ts";
import { bundleApp } from "../scripts/apps/bundle.lib.ts";
import { delay, throttle } from "../utils/async.ts";
import {
createDaemonAPIs,
DECO_ENV_NAME,
DECO_HOST,
DECO_SITE_NAME,
} from "./daemon.ts";
import { watchFS } from "./fs/api.ts";
import { ensureGit, getGitHubPackageTokens, lockerGitAPI } from "./git.ts";
import { logs } from "./loggings/stream.ts";
import { watchMeta } from "./meta.ts";
import { activityMonitor, createIdleHandler } from "./monitor.ts";
import { register } from "./tunnel.ts";
import { createWorker, type WorkerOptions } from "./worker.ts";
import { portPool } from "./workers/portpool.ts";
import { exists } from "@std/fs";
const parsedArgs = parseArgs(Deno.args, {
string: ["build-cmd"],
});
const runCommand = parsedArgs["_"];
const DECO_APP_NAME = Deno.env.get("DECO_APP_NAME");
export const DENO_DEPLOYMENT_ID: string | undefined = Deno.env.get(
"DENO_DEPLOYMENT_ID",
);
const SOURCE_PATH = Deno.env.get("SOURCE_ASSET_PATH");
const DECO_TRANSIENT_ENV = Deno.env.get("DECO_TRANSIENT_ENV") === "true";
const SHOULD_PERSIST = DENO_DEPLOYMENT_ID && SOURCE_PATH && !DECO_TRANSIENT_ENV;
export const VERBOSE: string | undefined = Deno.env.get("VERBOSE") ||
DENO_DEPLOYMENT_ID;
const DENO_AUTH_TOKENS = "DENO_AUTH_TOKENS";
const UNSTABLE_WORKER_RESPAWN_INTERVAL_MS_ENV_NAME =
"UNSTABLE_WORKER_RESPAWN_INTERVAL_MS";
const UNSTABLE_WORKER_RESPAWN_INTERVAL_MS =
Deno.env.get(UNSTABLE_WORKER_RESPAWN_INTERVAL_MS_ENV_NAME) &&
!Number.isNaN(
parseInt(Deno.env.get(UNSTABLE_WORKER_RESPAWN_INTERVAL_MS_ENV_NAME)!, 10),
)
? parseInt(Deno.env.get(UNSTABLE_WORKER_RESPAWN_INTERVAL_MS_ENV_NAME)!, 10)
: undefined; // 1hour
const HAS_PRIVATE_GITHUB_IMPORT = Deno.env.get("HAS_PRIVATE_GITHUB_IMPORT");
const WORKER_PORT = portPool.get();
const [cmd, ...args] = runCommand as string[];
const [buildCmdStr, ...buildArgs] = parsedArgs["build-cmd"]?.split(" ") ?? [];
const buildCmd = buildCmdStr
? new Deno.Command(buildCmdStr, {
args: buildArgs,
stdout: "inherit",
stderr: "inherit",
})
: null;
const getEnvVar = (envName: string, varName: string) =>
Deno.env.get(envName) ? { [varName]: Deno.env.get(envName) } : {};
const createRunCmd = cmd
? (opt?: Pick<Deno.CommandOptions, "env">) =>
new Deno.Command(cmd === "deno" ? Deno.execPath() : cmd, {
args,
stdout: "piped",
stderr: "piped",
env: {
...opt?.env,
PORT: `${WORKER_PORT}`,
...getEnvVar(DENO_AUTH_TOKENS, DENO_AUTH_TOKENS),
...getEnvVar("DENO_DIR_RUN", "DENO_DIR"),
},
})
: null;
let lastUpdateEnvUpdate: number | undefined;
const updateDenoAuthTokenEnv = async () => {
if (
!UNSTABLE_WORKER_RESPAWN_INTERVAL_MS ||
lastUpdateEnvUpdate &&
Date.now() < lastUpdateEnvUpdate
) return;
lastUpdateEnvUpdate = Date.now() + UNSTABLE_WORKER_RESPAWN_INTERVAL_MS;
const appTokens = await getGitHubPackageTokens();
// TODO: handle if DENO_AUTH_TOKENS is already set
Deno.env.set(
DENO_AUTH_TOKENS,
appTokens.map((token) => `${token}@raw.githubusercontent.com`).join(
";",
),
);
};
if (!DECO_SITE_NAME) {
console.error(
`site name not found. use ${ENV_SITE_NAME} environment variable to set it.`,
);
Deno.exit(1);
}
globalThis.addEventListener("unhandledrejection", (e: {
promise: Promise<unknown>;
reason: unknown;
}) => {
console.log("unhandled rejection at:", e.promise, "reason:", e.reason);
});
type ProjectType = "faststore" | "fresh" | "hono";
const detectProjectType = async (): Promise<ProjectType> => {
try {
const packageJsonPath = join(Deno.cwd(), "package.json");
if (await exists(packageJsonPath)) {
const packageJson = JSON.parse(await Deno.readTextFile(packageJsonPath));
if (packageJson.dependencies?.["@faststore/cli"]) {
return "faststore";
}
// Could add more Node.js framework detection here (e.g., Hono)
}
return "fresh"; // Default to Fresh for Deno projects
} catch (error) {
console.log("Error detecting project type, defaulting to fresh:", error);
return "fresh";
}
};
const createBundler = (appName?: string) => {
const bundler = bundleApp(Deno.cwd());
return async () => {
try {
await bundler({ dir: ".", name: appName ?? "site" });
} catch (error) {
console.error("Error while bundling site app", error);
}
};
};
const persist = async () => {
try {
if (!SHOULD_PERSIST) {
return;
}
const start = performance.now();
const outfilePath = join(
dirname(SOURCE_PATH!),
`${DENO_DEPLOYMENT_ID}.tar`,
);
await ensureDir(dirname(outfilePath));
const tar = new Deno.Command("tar", {
cwd: Deno.cwd(),
args: ["-cf", outfilePath, "--exclude=node_modules", "."],
});
const status = await tar.spawn().status;
console.log(
`[tar]: Tarballing took ${(performance.now() - start).toFixed(0)}ms`,
);
if (!status.success) {
throw new Error("Failed to tarball");
}
} catch (error) {
console.error("Error while persisting", error);
}
};
const bundle = createBundler(DECO_APP_NAME);
const genManifestTS = throttle(async () => {
await Promise.all([bundle(), delay(300)]);
});
const genBlocksJSON = throttle(async () => {
await Promise.all([genMetadata(), delay(300)]);
});
const persistState = throttle(async () => {
await Promise.all([persist(), delay(2 * 60 * 1_000)]);
});
// Watch for changes in filesystem
// TODO: we should be able to completely remove this after in some point in the future
const watch = async () => {
const watcher = Deno.watchFs(Deno.cwd(), { recursive: true });
for await (const event of watcher) {
using _ = await lockerGitAPI.lock.rlock();
const skip = event.paths.some((path) =>
path.includes(".git") || path.includes("node_modules")
);
if (skip) {
continue;
}
if (VERBOSE) {
console.log(event.kind, ...event.paths);
}
// TODO: remove genBlocksJSON after we stop using old FS API
const isBlockChanged = event.paths.some((path) =>
path.includes(`${DECO_FOLDER}/${BLOCKS_FOLDER}`)
);
if (isBlockChanged) {
genBlocksJSON();
}
/** We should move this to the new FS api */
const codeCreatedOrDeleted = event.kind !== "modify" &&
event.kind !== "access" && event.paths.some((path) => (
/\.tsx?$/.test(path) && !path.includes("manifest.gen.ts")
));
if (codeCreatedOrDeleted) {
genManifestTS();
}
if (HAS_PRIVATE_GITHUB_IMPORT) {
updateDenoAuthTokenEnv();
}
// TODO: We should be able to remove this after we migrate to ebs
persistState();
}
};
const createDeps = (projectType: ProjectType): MiddlewareHandler => {
let ok: Promise<unknown> | null | false = null;
const start = async () => {
let start = performance.now();
await ensureGit({ site: DECO_SITE_NAME! });
logs.push({
level: "info",
message: `${colors.bold("[step 1/4]")}: Git setup took ${
(performance.now() - start).toFixed(0)
}ms`,
});
// Only run Fresh-specific operations for Fresh projects
if (projectType === "fresh") {
start = performance.now();
await genManifestTS();
logs.push({
level: "info",
message: `${colors.bold("[step 2/4]")}: Manifest generation took ${
(performance.now() - start).toFixed(0)
}ms`,
});
start = performance.now();
await genBlocksJSON();
logs.push({
level: "info",
message: `${colors.bold("[step 3/4]")}: Blocks metadata generation took ${
(performance.now() - start).toFixed(0)
}ms`,
});
watch().catch(console.error);
watchMeta().catch(console.error);
} else {
await genManifestTS();
logs.push({
level: "info",
message: `${colors.bold("[step 2/4]")}: Skipped Fresh-specific operations (FastStore project)`,
});
}
// watchFS is needed for all project types (sends events to admin)
watchFS().catch(console.error);
logs.push({
level: "info",
message: colors.green("File system watcher started"),
});
logs.push({
level: "info",
message: `${
colors.bold("[step 4/4]")
}: Initialization complete`,
});
};
return async (c, next) => {
try {
ok ||= start();
await ok.then(next);
} catch (err) {
console.log(err);
c.res = new Response("Error while starting global deps", { status: 424 });
}
};
};
// Detect project type early for configuration
const PROJECT_TYPE = await detectProjectType();
const app = new Hono();
if (VERBOSE) {
app.use(logger());
}
app.get("/_healthcheck", (c) => {
const timestamp = +(c.req.header("x-hc-retry-timestamp") ?? "0");
const attempt = +(c.req.header("x-hc-retry-attempt") ?? "0");
console.log("healthcheck received", {
timestamp: new Date(timestamp).toISOString(),
attempt,
});
return new Response(denoJSON.version, {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
},
});
});
// idle should run even when branch is not active
app.get("/deco/_is_idle", createIdleHandler(DECO_SITE_NAME!, DECO_ENV_NAME!));
// k8s liveness probe
app.get("/deco/_liveness", () => new Response("OK", { status: 200 }));
// Globals are started after healthcheck to ensure k8s does not kill the pod before it is ready
app.use(createDeps(PROJECT_TYPE));
app.use(activityMonitor);
// These are the APIs that communicate with admin UI
app.use(createDaemonAPIs({ build: buildCmd, site: DECO_SITE_NAME }));
// Workers are only necessary if there needs to have a preview of the site
if (createRunCmd) {
// Create a function that returns fresh WorkerOptions with new tokens
const createWorkerOptions = async (): Promise<WorkerOptions> => {
if (HAS_PRIVATE_GITHUB_IMPORT) {
await updateDenoAuthTokenEnv();
}
if (UNSTABLE_WORKER_RESPAWN_INTERVAL_MS) {
/* TODO: Implement a better approach handling updating child env vars, preventing multiple child processes and with HMR.
* Also should have the git short live auth token to do git operations like: push/pull/rebase. Now, these git operations are guaranted
* because the short live git token is set once in inicialization and respawning
*/
// Kill process to allow restart with new env settings
setTimeout(() => {
Deno.exit(1);
}, UNSTABLE_WORKER_RESPAWN_INTERVAL_MS);
}
return {
command: createRunCmd(), // This will create a fresh command with new tokens
port: WORKER_PORT,
persist,
};
};
app.route("", createWorker(createWorkerOptions));
} else {
// No explicit run command, create appropriate worker based on detected type
if (PROJECT_TYPE === "faststore") {
logs.push({
level: "info",
message: colors.green("Detected FastStore project, starting Node.js worker..."),
});
// FastStore/Next.js runs on port 3000 by default
const FASTSTORE_PORT = 3000;
const createFastStoreWorkerOptions = async (): Promise<WorkerOptions> => {
const packageJsonPath = join(Deno.cwd(), "package.json");
const packageJson = JSON.parse(await Deno.readTextFile(packageJsonPath));
// Detect package manager
const hasYarnLock = await exists(join(Deno.cwd(), "yarn.lock"));
const hasPnpmLock = await exists(join(Deno.cwd(), "pnpm-lock.yaml"));
const packageManager = hasPnpmLock ? "pnpm" : hasYarnLock ? "yarn" : "npm";
// Use volta-specified node version if available
const nodeVersion = packageJson.volta?.node;
if (nodeVersion) {
logs.push({
level: "info",
message: `Using Node.js ${nodeVersion} from Volta configuration`,
});
}
logs.push({
level: "info",
message: colors.blue(`FastStore worker will run on port ${FASTSTORE_PORT}`),
});
return {
command: new Deno.Command(packageManager, {
args: ["run", "dev"],
cwd: Deno.cwd(),
stdout: "piped",
stderr: "piped",
env: {
PORT: `${FASTSTORE_PORT}`,
NODE_ENV: "development",
},
}),
port: FASTSTORE_PORT,
persist,
};
};
app.route("", createWorker(createFastStoreWorkerOptions));
}
}
const LOCAL_STORAGE_ENV_NAME = "deco_host_env_name";
const stableEnvironmentName = () => {
const savedEnvironment = localStorage.getItem(LOCAL_STORAGE_ENV_NAME);
if (savedEnvironment) {
return savedEnvironment;
}
const newEnvironment = `${crypto.randomUUID().slice(0, 6)}-localhost`;
localStorage.setItem(LOCAL_STORAGE_ENV_NAME, newEnvironment);
return newEnvironment;
};
const port = Number(Deno.env.get("APP_PORT")) || 8000;
Deno.serve({
port,
onListen: async (addr) => {
try {
const env = DECO_HOST && !DECO_ENV_NAME
? stableEnvironmentName()
: DECO_ENV_NAME;
if (env && DECO_SITE_NAME && !Deno.env.has("DECO_PREVIEW")) {
await register({
site: DECO_SITE_NAME,
env,
port: `${port}`,
decoHost: DECO_HOST,
});
} else {
console.log(
colors.green(
`Server running on http://${addr.hostname}:${addr.port}`,
),
);
}
} catch (err) {
console.log(err);
}
},
}, app.fetch);