import { mkdir, writeFile, chmod, access } from 'node:fs/promises'; import { resolve } from 'node:path'; const root = resolve(import.meta.dir, '..'); const local = resolve(root, '.local'); const files = ['pg_password', 'database_url', 'cache_url', 'valkey.conf']; const present = await Promise.all(files.map(async name => {try {await access(resolve(local, name));return true;} catch {return false;}})); if (present.some(Boolean)) { if (!present.every(Boolean)) throw new Error('Incomplete local configuration; preserve it and investigate before bootstrap'); console.log('Local configuration exists'); } else { await mkdir(local, { recursive: true, mode: 0o700 }); const pg = crypto.randomUUID().replaceAll('-', '') + crypto.randomUUID().replaceAll('-', ''); const cache = crypto.randomUUID().replaceAll('-', ''); const values = [pg, `postgres://messenger:${pg}@postgres:5432/messenger?sslmode=disable`, `redis://:${cache}@valkey:6379/0`, `bind 0.0.0.0\nprotected-mode yes\nrequirepass ${cache}\nappendonly yes\nappendfsync always\ndir /data\n`]; for (let i = 0; i < files.length; i++) { const destination = resolve(local, files[i]); await writeFile(destination, values[i] + '\n', { mode: 0o600, flag: 'wx' }); await chmod(destination, 0o644); // Compose secrets must be readable by nonroot containers; parent is 0700. } console.log('Created local synthetic stand credentials'); }