|
| 1 | +/** |
| 2 | + * Server-root landing page. |
| 3 | + * |
| 4 | + * Renders src/ui/server-root.html with runtime values, and seeds |
| 5 | + * DATA_ROOT/index.html + DATA_ROOT/.acl on first start (skip-if-exists, |
| 6 | + * so operator customisation is preserved). |
| 7 | + * |
| 8 | + * See issue #276. |
| 9 | + */ |
| 10 | + |
| 11 | +import { readFileSync } from 'fs'; |
| 12 | +import { fileURLToPath } from 'url'; |
| 13 | +import { dirname, join } from 'path'; |
| 14 | +import * as storage from '../storage/filesystem.js'; |
| 15 | + |
| 16 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 17 | +const TEMPLATE_PATH = join(__dirname, 'server-root.html'); |
| 18 | + |
| 19 | +/** |
| 20 | + * Collect the list of enabled features for display on the landing page. |
| 21 | + */ |
| 22 | +function listFeatures(options = {}) { |
| 23 | + const f = []; |
| 24 | + if (options.idp) f.push('idp'); |
| 25 | + if (options.nostr) f.push('nostr'); |
| 26 | + if (options.webrtc) f.push('webrtc'); |
| 27 | + if (options.activitypub) f.push('activitypub'); |
| 28 | + if (options.git) f.push('git'); |
| 29 | + if (options.pay) f.push('payments'); |
| 30 | + if (options.notifications) f.push('notifications'); |
| 31 | + if (options.mashlib) f.push('mashlib'); |
| 32 | + if (options.mongo) f.push('mongo'); |
| 33 | + if (options.tunnel) f.push('tunnel'); |
| 34 | + return f; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Build an HTML snippet of action buttons based on server mode. |
| 39 | + */ |
| 40 | +function renderActions({ singleUser, idp }) { |
| 41 | + const buttons = []; |
| 42 | + if (!singleUser && idp) { |
| 43 | + buttons.push('<a href="/.account/new" class="btn btn-primary">Create a pod</a>'); |
| 44 | + buttons.push('<a href="/idp/auth" class="btn btn-secondary">Sign in</a>'); |
| 45 | + } else if (singleUser && idp) { |
| 46 | + buttons.push('<a href="/idp/auth" class="btn btn-primary">Sign in</a>'); |
| 47 | + } |
| 48 | + buttons.push('<a href="https://javascriptsolidserver.github.io/docs/" class="btn btn-secondary">Docs</a>'); |
| 49 | + return `<div class="actions">${buttons.join('\n ')}</div>`; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Render the landing page as an HTML string. |
| 54 | + * |
| 55 | + * @param {object} ctx |
| 56 | + * @param {string} ctx.version - JSS version |
| 57 | + * @param {boolean} [ctx.singleUser] |
| 58 | + * @param {boolean} [ctx.idp] |
| 59 | + * @param {string} [ctx.singleUserName] |
| 60 | + * @param {object} [ctx.enabled] - Map of feature flags |
| 61 | + * @returns {string} HTML |
| 62 | + */ |
| 63 | +export function renderServerRoot(ctx = {}) { |
| 64 | + const { version = 'unknown', singleUser = false, idp = false, singleUserName, enabled = {} } = ctx; |
| 65 | + |
| 66 | + const tpl = readFileSync(TEMPLATE_PATH, 'utf8'); |
| 67 | + const mode = singleUser ? 'single-user' : 'multi-user'; |
| 68 | + const features = listFeatures(enabled) |
| 69 | + .map(f => `<span>${f}</span>`) |
| 70 | + .join(' '); |
| 71 | + |
| 72 | + const heading = 'JSS'; |
| 73 | + const subtitle = singleUser |
| 74 | + ? `Personal pod${singleUserName && singleUserName !== '/' ? ` for ${escape(singleUserName)}` : ''}` |
| 75 | + : 'A personal data server'; |
| 76 | + const description = singleUser |
| 77 | + ? 'This server hosts a personal data pod. Apps come to the data rather than the other way around.' |
| 78 | + : 'This server hosts personal data pods on the web. Each pod is a space you own, with your own identity and access control.'; |
| 79 | + |
| 80 | + return tpl |
| 81 | + .replace(/{{title}}/g, heading) |
| 82 | + .replace(/{{heading}}/g, heading) |
| 83 | + .replace(/{{subtitle}}/g, subtitle) |
| 84 | + .replace(/{{description}}/g, description) |
| 85 | + .replace(/{{actions}}/g, renderActions({ singleUser, idp })) |
| 86 | + .replace(/{{version}}/g, escape(version)) |
| 87 | + .replace(/{{mode}}/g, mode) |
| 88 | + .replace(/{{features}}/g, features); |
| 89 | +} |
| 90 | + |
| 91 | +function escape(s = '') { |
| 92 | + return String(s) |
| 93 | + .replace(/&/g, '&') |
| 94 | + .replace(/</g, '<') |
| 95 | + .replace(/>/g, '>') |
| 96 | + .replace(/"/g, '"'); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Seed DATA_ROOT/index.html and DATA_ROOT/.acl if they don't already |
| 101 | + * exist. Operator's own files are never overwritten. |
| 102 | + * |
| 103 | + * Default ACL at root: public read. Write access is not granted — the |
| 104 | + * operator edits the file on disk, not via the web. |
| 105 | + * |
| 106 | + * @param {object} ctx - Same context passed to renderServerRoot |
| 107 | + * @returns {Promise<{seeded: boolean}>} |
| 108 | + */ |
| 109 | +export async function seedServerRoot(ctx = {}) { |
| 110 | + let seededHtml = false; |
| 111 | + let seededAcl = false; |
| 112 | + |
| 113 | + // Seed /index.html if operator hasn't written one. |
| 114 | + if (!(await storage.exists('/index.html'))) { |
| 115 | + const html = renderServerRoot(ctx); |
| 116 | + await storage.write('/index.html', html); |
| 117 | + seededHtml = true; |
| 118 | + } |
| 119 | + |
| 120 | + // Seed /.acl if one doesn't already exist. Public read on the container |
| 121 | + // itself — so GET / serves the landing page. Independent of index.html. |
| 122 | + // (createRootPodStructure in single-user mode writes its own ACL and |
| 123 | + // runs in a later hook, which will overwrite this if needed.) |
| 124 | + if (!(await storage.exists('/.acl'))) { |
| 125 | + const acl = JSON.stringify({ |
| 126 | + '@context': { acl: 'http://www.w3.org/ns/auth/acl#', foaf: 'http://xmlns.com/foaf/0.1/' }, |
| 127 | + '@graph': [ |
| 128 | + { |
| 129 | + '@id': '#public', |
| 130 | + '@type': 'acl:Authorization', |
| 131 | + 'acl:agentClass': { '@id': 'foaf:Agent' }, |
| 132 | + 'acl:accessTo': { '@id': '/' }, |
| 133 | + 'acl:mode': [{ '@id': 'acl:Read' }] |
| 134 | + } |
| 135 | + ] |
| 136 | + }, null, 2); |
| 137 | + await storage.write('/.acl', acl); |
| 138 | + seededAcl = true; |
| 139 | + } |
| 140 | + |
| 141 | + // Dedicated ACL for the landing page itself — public read. The container |
| 142 | + // ACL above has no acl:default (we don't want to implicitly publish all |
| 143 | + // children), so /index.html needs its own rule when fetched directly. |
| 144 | + if (!(await storage.exists('/index.html.acl'))) { |
| 145 | + const pageAcl = JSON.stringify({ |
| 146 | + '@context': { acl: 'http://www.w3.org/ns/auth/acl#', foaf: 'http://xmlns.com/foaf/0.1/' }, |
| 147 | + '@graph': [ |
| 148 | + { |
| 149 | + '@id': '#public', |
| 150 | + '@type': 'acl:Authorization', |
| 151 | + 'acl:agentClass': { '@id': 'foaf:Agent' }, |
| 152 | + 'acl:accessTo': { '@id': '/index.html' }, |
| 153 | + 'acl:mode': [{ '@id': 'acl:Read' }] |
| 154 | + } |
| 155 | + ] |
| 156 | + }, null, 2); |
| 157 | + await storage.write('/index.html.acl', pageAcl); |
| 158 | + } |
| 159 | + |
| 160 | + return { seededHtml, seededAcl }; |
| 161 | +} |
0 commit comments