|
| 1 | +import { resolve } from "node:path"; |
| 2 | +import { pathToFileURL } from "node:url"; |
| 3 | + |
| 4 | +process.env.LOCKSTEP_DEMO_MODE = "true"; |
| 5 | + |
| 6 | +const resolveDemoPort = () => |
| 7 | + process.env.LOCKSTEP_DEMO_PORT || process.env.DEMO_PORT || 4175; |
| 8 | + |
| 9 | +const validateDemoCredentials = () => { |
| 10 | + const username = (process.env.LOCKSTEP_DEMO_USER ?? "demo") |
| 11 | + .trim() |
| 12 | + .replace(/[ıİ]/g, (character) => (character === "ı" ? "i" : "I")) |
| 13 | + .normalize("NFKD") |
| 14 | + .replace(/[\u0300-\u036f]/g, "") |
| 15 | + .toLowerCase() |
| 16 | + .replace(/[^a-z0-9_-]/g, "") |
| 17 | + .slice(0, 32); |
| 18 | + const password = process.env.LOCKSTEP_DEMO_PASSWORD; |
| 19 | + if (username.length < 3) { |
| 20 | + throw new Error("LOCKSTEP_DEMO_USER must contain at least 3 valid characters"); |
| 21 | + } |
| 22 | + if ( |
| 23 | + password !== undefined && |
| 24 | + password.length < 6 && |
| 25 | + !(username === "demo" && password === "demo") |
| 26 | + ) { |
| 27 | + throw new Error("LOCKSTEP_DEMO_PASSWORD must contain at least 6 characters"); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +const validateDemoResetTimeZone = () => { |
| 32 | + const timeZone = |
| 33 | + process.env.LOCKSTEP_DEMO_RESET_TIMEZONE || "Europe/Istanbul"; |
| 34 | + try { |
| 35 | + new Intl.DateTimeFormat("en", { timeZone }).format(); |
| 36 | + } catch { |
| 37 | + throw new Error(`Invalid LOCKSTEP_DEMO_RESET_TIMEZONE: ${timeZone}`); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +let serveModule; |
| 42 | + |
| 43 | +const loadServeModule = async () => { |
| 44 | + serveModule ||= import("./serve.mjs"); |
| 45 | + return serveModule; |
| 46 | +}; |
| 47 | + |
| 48 | +export const startDemoServer = async ({ |
| 49 | + port = resolveDemoPort(), |
| 50 | + hostname = process.env.LOCKSTEP_DEMO_HOST || "127.0.0.1", |
| 51 | + log = true, |
| 52 | +} = {}) => { |
| 53 | + process.env.LOCKSTEP_DEMO_MODE = "true"; |
| 54 | + validateDemoCredentials(); |
| 55 | + validateDemoResetTimeZone(); |
| 56 | + const { startLockstepServer } = await loadServeModule(); |
| 57 | + |
| 58 | + return startLockstepServer({ |
| 59 | + port, |
| 60 | + hostname, |
| 61 | + log, |
| 62 | + label: "Lockstep demo", |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | +const isDirectRun = Boolean( |
| 67 | + process.argv[1] && |
| 68 | + import.meta.url === pathToFileURL(resolve(process.argv[1])).href, |
| 69 | +); |
| 70 | + |
| 71 | +if (isDirectRun) { |
| 72 | + const server = await startDemoServer(); |
| 73 | + let shuttingDown = false; |
| 74 | + const shutdown = () => { |
| 75 | + if (shuttingDown) return; |
| 76 | + shuttingDown = true; |
| 77 | + const forceExit = setTimeout(() => process.exit(1), 10_000); |
| 78 | + forceExit.unref(); |
| 79 | + server.close((error) => { |
| 80 | + clearTimeout(forceExit); |
| 81 | + process.exit(error ? 1 : 0); |
| 82 | + }); |
| 83 | + }; |
| 84 | + process.once("SIGINT", shutdown); |
| 85 | + process.once("SIGTERM", shutdown); |
| 86 | +} |
0 commit comments