javascript-solid-server 0.0.226 → 0.0.227

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/jss.js CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
 
12
12
  import { Command } from 'commander';
13
+ import { sortPanes } from '../src/mashlib/index.js';
13
14
  import { createServer } from '../src/server.js';
14
15
  import { loadConfig, saveConfig, printConfig, defaults, parsePluginFlag } from '../src/config.js';
15
16
  import { createInvite, listInvites, revokeInvite } from '../src/idp/invites.js';
@@ -106,6 +107,7 @@ program
106
107
  .option('--mashlib-module <url>', 'Enable ES module data browser from a URL')
107
108
  .option('--no-mashlib', 'Disable Mashlib data browser')
108
109
  .option('--mashlib-version <version>', 'Mashlib version for CDN mode (default: 2.0.0)')
110
+ .option('--panes <urls>', 'Comma-separated pane module URLs the data browser loads at start-up')
109
111
  .option('--git', 'Enable Git HTTP backend (clone/push support)')
110
112
  .option('--no-git', 'Disable Git HTTP backend')
111
113
  .option('--cors-proxy', 'Enable CORS proxy at /proxy?url=... for browser apps (WAC-gated)')
@@ -241,6 +243,7 @@ program
241
243
  mashlibCdn: config.mashlibCdn,
242
244
  mashlibVersion: config.mashlibVersion,
243
245
  mashlibModule: config.mashlibModule,
246
+ panes: config.panes,
244
247
  git: config.git,
245
248
  corsProxy: config.corsProxy,
246
249
  corsProxyMaxBytes: config.corsProxyMaxBytes,
@@ -303,6 +306,17 @@ program
303
306
  console.log(` Mashlib: local (data browser enabled)`);
304
307
  }
305
308
  if (config.mashlibModule) console.log(` Mashlib module: ${config.mashlibModule}`);
309
+ {
310
+ const { urls, dropped } = sortPanes(config.panes);
311
+ const paneMode = config.mashlibCdn && !config.mashlibModule;
312
+ if (urls.length && paneMode) console.log(` Panes: ${urls.join(', ')}`);
313
+ if (urls.length && !paneMode) {
314
+ console.log(config.mashlibModule
315
+ ? ' Panes: ignored (--panes does not apply to --mashlib-module yet)'
316
+ : ' Panes: ignored (--panes needs --mashlib-cdn)');
317
+ }
318
+ for (const d of dropped) console.log(` Panes: ignored "${d}" (not an http(s) URL or an absolute path)`);
319
+ }
306
320
  if (config.git) console.log(' Git: enabled (clone/push support)');
307
321
  if (config.corsProxy) console.log(' CORS proxy: enabled (/proxy?url=..., WAC-gated)');
308
322
  if (config.nostr) console.log(` Nostr: enabled (${config.nostrPath})`);
@@ -147,6 +147,7 @@ Server: pub http://localhost:4443/alice/public/data.json (on change)
147
147
  | `--mashlib-cdn` | Enable Mashlib (CDN mode) | false |
148
148
  | `--mashlib-module <url>` | Enable ES module data browser from a URL | - |
149
149
  | `--mashlib-version <ver>` | Mashlib CDN version | 2.0.0 |
150
+ | `--panes <urls>` | Comma-separated pane module URLs the data browser loads at start-up | - |
150
151
  | `--solidos-ui` | Enable modern SolidOS UI (requires --mashlib) | false |
151
152
  | `--git` | Enable Git HTTP backend | false |
152
153
  | `--nostr` | Enable Nostr relay | false |
package/docs/mashlib.md CHANGED
@@ -35,6 +35,27 @@ Loads an ES module-based data browser from any URL. Uses `<script type="module">
35
35
 
36
36
  **Note:** Mashlib works best with `--conneg` enabled for Turtle support.
37
37
 
38
+ ## Extra panes at start-up
39
+
40
+ ```bash
41
+ jss start --mashlib-cdn --conneg --panes https://melvincarvalho.github.io/pane-loader/examples/meetings-pane.js
42
+ ```
43
+
44
+ `--panes` (env `JSS_PANES`, config `panes`) takes a comma-separated list of pane module URLs. Each is an ES module whose default export is a SolidOS pane (`name`, `label`, `render`). The data browser page loads and registers them before it starts SolidOS, so they are in the registry at first render. Mashlib is not changed or rebuilt.
45
+
46
+ - A URL is an `http(s)` address or an absolute path on this server (`/public/panes/my-pane.js`); anything else is ignored.
47
+ - A pane that fails to load, or is not a pane, is skipped with a console warning, and the data browser still starts.
48
+ - Panes are fetched at the same time and registered in the order listed. The page waits at most 5 seconds for them. A pane slower than that is registered when it arrives and is offered from the next view drawn; the view already on show is not redrawn.
49
+ - It applies to `--mashlib-cdn`. It does not apply to `--mashlib-module` yet: a module entry point starts itself, so that mode needs its own design. The server says so at start-up.
50
+ - On the command line and in `JSS_PANES` the list is split on commas. A pane URL that contains a comma has to go in the config file, as an array.
51
+ - Entries that are not usable are listed at start-up, not silently ignored.
52
+ - A loaded pane is registered after the built-in ones, so it is offered alongside them and does not replace the default view.
53
+ - With no `--panes`, the page is exactly as before.
54
+
55
+ The loader is [pane-loader](https://github.com/melvincarvalho/pane-loader), shipped with JSS and served at `/.well-known/jss/pane-loader.js` when panes are configured. The shipped copy leaves out the upstream `?pane=URL` shortcut, so the only panes loaded are the ones in this list. Background: [SolidOS Panes, section 7](https://melvincarvalho.github.io/solidos-panes/#dynamic).
56
+
57
+ **Trust:** a pane runs in the page with the logged-in session and can do whatever the person can. This list is set by whoever runs the server; add only panes you would install for every user. Signing and consent are tracked in https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/651.
58
+
38
59
  ## Modern UI (SolidOS UI)
39
60
 
40
61
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-solid-server",
3
- "version": "0.0.226",
3
+ "version": "0.0.227",
4
4
  "description": "A minimal, fast Solid server",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -181,7 +181,7 @@ export function handleUnauthorized(request, reply, isAuthenticated, wacAllow, au
181
181
  if (request.mashlibEnabled) {
182
182
  const html = request.mashlibModule
183
183
  ? generateModuleDatabrowserHtml(request.mashlibModule)
184
- : generateDatabrowserHtml(request.url, request.mashlibCdn ? request.mashlibVersion : null);
184
+ : generateDatabrowserHtml(request.url, request.mashlibCdn ? request.mashlibVersion : null, { panes: request.panes });
185
185
  return reply.code(statusCode).type('text/html').send(html);
186
186
  }
187
187
  return reply.code(statusCode).type('text/html').send(getErrorPage(statusCode, isAuthenticated, request));
package/src/config.js CHANGED
@@ -49,6 +49,9 @@ export const defaults = {
49
49
  mashlibCdn: false,
50
50
  mashlibVersion: '2.0.0',
51
51
  mashlibModule: false,
52
+ // Pane module URLs the data browser loads at start-up (#652):
53
+ // an array, or a comma-separated string. Empty: off.
54
+ panes: null,
52
55
 
53
56
  // Git HTTP backend
54
57
  git: false,
@@ -169,6 +172,7 @@ const envMap = {
169
172
  JSS_MASHLIB_CDN: 'mashlibCdn',
170
173
  JSS_MASHLIB_VERSION: 'mashlibVersion',
171
174
  JSS_MASHLIB_MODULE: 'mashlibModule',
175
+ JSS_PANES: 'panes',
172
176
  JSS_GIT: 'git',
173
177
  JSS_CORS_PROXY: 'corsProxy',
174
178
  JSS_CORS_PROXY_MAX_BYTES: 'corsProxyMaxBytes',
@@ -317,7 +317,7 @@ export async function handleGet(request, reply) {
317
317
  : generateDatabrowserHtml(
318
318
  resourceUrl,
319
319
  request.mashlibCdn ? request.mashlibVersion : null,
320
- { embedJsonLd }
320
+ { embedJsonLd, panes: request.panes }
321
321
  );
322
322
  const headers = getAllHeaders({
323
323
  isContainer: true,
@@ -454,7 +454,7 @@ export async function handleGet(request, reply) {
454
454
  : generateDatabrowserHtml(
455
455
  resourceUrl,
456
456
  request.mashlibCdn ? request.mashlibVersion : null,
457
- { embedJsonLd }
457
+ { embedJsonLd, panes: request.panes }
458
458
  );
459
459
  const headers = getAllHeaders({
460
460
  isContainer: false,
@@ -15,6 +15,8 @@
15
15
  * static-site exporters — and gives Phase 2 a zero-network fast path.
16
16
  */
17
17
 
18
+ import { readFileSync } from 'node:fs';
19
+
18
20
  /**
19
21
  * Cap on how much JSON-LD we'll inline. A 256 KB resource fits any
20
22
  * realistic profile, type index, or container listing. Above that we
@@ -245,6 +247,137 @@ export function roundTripOptimizationScript() {
245
247
  </script>`;
246
248
  }
247
249
 
250
+ /**
251
+ * Panes loaded at start-up (#652).
252
+ *
253
+ * The server can be given a list of pane module URLs (`--panes`). Each is
254
+ * an ES module whose default export is a SolidOS pane. The data browser
255
+ * page loads and registers them before it starts SolidOS, so they are in
256
+ * the registry at first render and mashlib itself is unchanged.
257
+ *
258
+ * The loader is pane-loader (https://github.com/melvincarvalho/pane-loader),
259
+ * shipped beside this file and served at PANE_LOADER_PATH.
260
+ */
261
+ export const PANE_LOADER_PATH = '/.well-known/jss/pane-loader.js';
262
+
263
+ /** How long the page waits for panes before starting SolidOS anyway. */
264
+ export const PANE_LOAD_TIMEOUT_MS = 5000;
265
+
266
+ // The loader URL carries the package version, so a browser or proxy cache
267
+ // never pairs a new page with an old loader after an upgrade.
268
+ let loaderVersion = '0';
269
+ try {
270
+ loaderVersion = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version || '0';
271
+ } catch { /* unversioned URL: still correct, only less cache-friendly */ }
272
+ const PANE_LOADER_URL = `${PANE_LOADER_PATH}?v=${encodeURIComponent(loaderVersion)}`;
273
+
274
+ /**
275
+ * Turn the `panes` option into a clean list of URLs. Accepts an array or
276
+ * a comma-separated string (CLI flag, env var). Keeps http(s) URLs and
277
+ * absolute paths on this server; anything else is dropped, so a typo
278
+ * cannot become a relative import from whatever page is on show.
279
+ *
280
+ * The string form splits on every comma. A pane URL that contains a comma
281
+ * has to be given in the array form (config file).
282
+ * @param {string[]|string|undefined|null|false} value
283
+ * @returns {string[]}
284
+ */
285
+ export function normalizePanes(value) {
286
+ return sortPanes(value).urls;
287
+ }
288
+
289
+ /**
290
+ * As normalizePanes, and also says which entries were dropped, so the CLI
291
+ * can tell the operator instead of ignoring them silently.
292
+ * @param {string[]|string|undefined|null|false} value
293
+ * @returns {{urls: string[], dropped: string[]}}
294
+ */
295
+ export function sortPanes(value) {
296
+ const urls = [];
297
+ const dropped = [];
298
+ if (!value) return { urls, dropped };
299
+ const list = Array.isArray(value) ? value : String(value).split(',');
300
+ for (const item of list) {
301
+ const url = typeof item === 'string' ? item.trim() : item;
302
+ if (url === '') continue;
303
+ const ok = typeof url === 'string' &&
304
+ (/^https?:\/\/[^\s]+$/i.test(url) || /^\/(?!\/)[^\s]*$/.test(url));
305
+ if (!ok) { dropped.push(String(url)); continue; }
306
+ if (!urls.includes(url)) urls.push(url);
307
+ }
308
+ return { urls, dropped };
309
+ }
310
+
311
+ /** JSON for use inside an inline script: no `<`, so no early `</script>`. */
312
+ function scriptJson(value) {
313
+ return JSON.stringify(value)
314
+ .replace(/</g, '\\u003c')
315
+ .replace(/\u2028/g, '\\u2028')
316
+ .replace(/\u2029/g, '\\u2029');
317
+ }
318
+
319
+ /**
320
+ * Load the configured panes, then start SolidOS. This function is written
321
+ * into the page as source (`toString()`), so it uses nothing from this
322
+ * module and is exported only so that it can be tested.
323
+ *
324
+ * - It always starts SolidOS, once: when every pane has been dealt with or
325
+ * after `timeoutMs`, whichever comes first. A loader that cannot be
326
+ * imported is logged and SolidOS starts without panes.
327
+ * - All pane modules are fetched at once, so a slow or dead host delays
328
+ * only its own pane, never the ones listed after it.
329
+ * - Panes are registered in the order configured. One that is still
330
+ * loading at `timeoutMs` is registered when it arrives; the registry is
331
+ * read at render time, so it is offered from the next view drawn, and
332
+ * the view already on show is not redrawn under the person using it.
333
+ *
334
+ * @param {string[]} urls pane module URLs
335
+ * @param {string} loaderUrl URL of pane-loader.js
336
+ * @param {number} timeoutMs longest wait before SolidOS starts
337
+ * @param {(url: string) => Promise<object>} [load] import function (tests)
338
+ * @returns {Promise<void>} settles when SolidOS has been started
339
+ */
340
+ export function jssStartDataBrowser(urls, loaderUrl, timeoutMs, load) {
341
+ load = load || function (url) { return import(url); };
342
+ var started = false;
343
+ function start() { if (!started) { started = true; panes.runDataBrowser(); } }
344
+ var gate = new Promise(function (resolve) { setTimeout(resolve, timeoutMs); });
345
+ gate.then(start);
346
+ return load(loaderUrl).then(function (loader) {
347
+ function add(url) {
348
+ return loader.loadPane(url).then(function (r) {
349
+ if (r.ok) console.info('[jss] pane "' + r.name + '" loaded from ' + url + (started ? ' (after start-up)' : ''));
350
+ else console.warn('[jss] pane skipped: ' + url + ': ' + r.reason);
351
+ });
352
+ }
353
+ var arrived = urls.map(function () { return false; });
354
+ var fetched = urls.map(function (url, i) {
355
+ // Warm the module cache; loadPane imports the same URL again from it.
356
+ return load(url).then(function () {}, function () {}).then(function () { arrived[i] = true; });
357
+ });
358
+ var chain = Promise.resolve();
359
+ urls.forEach(function (url, i) {
360
+ chain = chain
361
+ .then(function () { return Promise.race([fetched[i], gate]); })
362
+ .then(function () {
363
+ if (arrived[i]) return add(url);
364
+ fetched[i].then(function () { return add(url); });
365
+ });
366
+ });
367
+ return chain;
368
+ }).catch(function (e) {
369
+ console.warn('[jss] panes not loaded: ' + (e && e.message ? e.message : e));
370
+ }).then(start);
371
+ }
372
+
373
+ /** The inline source for the page: the function above, and its call. */
374
+ function paneStartScript(paneUrls) {
375
+ return {
376
+ fn: `\n${jssStartDataBrowser.toString()}\n`,
377
+ call: `jssStartDataBrowser(${scriptJson(paneUrls)}, ${scriptJson(PANE_LOADER_URL)}, ${PANE_LOAD_TIMEOUT_MS})`
378
+ };
379
+ }
380
+
248
381
  /**
249
382
  * Generate Mashlib databrowser HTML
250
383
  *
@@ -255,6 +388,10 @@ export function roundTripOptimizationScript() {
255
388
  * as a `<script type="application/ld+json">` data island. Accepts a
256
389
  * UTF-8 string or a Buffer (coerced via `String()`). Honors a 256 KB
257
390
  * size cap; oversize payloads are silently dropped. Phase 1 of #7.
391
+ * @param {string[]|string} [opts.panes] - Pane module URLs to load and
392
+ * register before SolidOS starts (#652). Empty or absent: the page is
393
+ * unchanged. Not applied by `generateModuleDatabrowserHtml`: a module
394
+ * entry point starts itself, so that mode needs its own design.
258
395
  * @param {boolean} [opts.roundTripOptimization=true] - Inline a small
259
396
  * reader script that lets rdflib-based clients (mashlib and friends)
260
397
  * resolve `fetcher.load(uri)` from the data island instead of issuing
@@ -265,6 +402,11 @@ export function roundTripOptimizationScript() {
265
402
  export function generateDatabrowserHtml(resourceUrl, cdnVersion = null, opts = {}) {
266
403
  const island = dataIsland(resourceUrl, opts.embedJsonLd);
267
404
  const reader = opts.roundTripOptimization === false ? '' : roundTripOptimizationScript();
405
+ const paneUrls = normalizePanes(opts.panes);
406
+ // With no panes configured the page is exactly what it was before #652.
407
+ const paneStart = paneUrls.length ? paneStartScript(paneUrls) : null;
408
+ const start = paneStart ? paneStart.call : 'panes.runDataBrowser()';
409
+ const startFn = paneStart ? paneStart.fn : '';
268
410
  if (cdnVersion) {
269
411
  // CDN mode - use script.onload to ensure mashlib is fully loaded before init
270
412
  // This avoids race conditions with defer + DOMContentLoaded
@@ -274,11 +416,11 @@ export function generateDatabrowserHtml(resourceUrl, cdnVersion = null, opts = {
274
416
  <body id="PageBody">${island}${reader}<header id="PageHeader"></header>
275
417
  <div class="TabulatorOutline" id="DummyUUID" role="main"><table id="outline"></table><div id="GlobalDashboard"></div></div>
276
418
  <footer id="PageFooter"></footer>
277
- <script>
419
+ <script>${startFn}
278
420
  (function() {
279
421
  var s = document.createElement('script');
280
422
  s.src = '${cdnBase}/mashlib.min.js';
281
- s.onload = function() { panes.runDataBrowser(); };
423
+ s.onload = function() { ${start}; };
282
424
  s.onerror = function() { document.body.innerHTML = '<p>Failed to load Mashlib from CDN</p>'; };
283
425
  document.head.appendChild(s);
284
426
  })();
@@ -286,8 +428,8 @@ export function generateDatabrowserHtml(resourceUrl, cdnVersion = null, opts = {
286
428
  }
287
429
 
288
430
  // Local mode - use defer (reliable when served locally)
289
- return `<!doctype html><html><head><meta charset="utf-8"/><title>SolidOS Web App</title><script>document.addEventListener('DOMContentLoaded', function() {
290
- panes.runDataBrowser()
431
+ return `<!doctype html><html><head><meta charset="utf-8"/><title>SolidOS Web App</title><script>${startFn}document.addEventListener('DOMContentLoaded', function() {
432
+ ${start}
291
433
  })</script><script defer="defer" src="/mashlib.min.js"></script><link href="/mash.css" rel="stylesheet"></head><body id="PageBody">${island}${reader}<header id="PageHeader"></header><div class="TabulatorOutline" id="DummyUUID" role="main"><table id="outline"></table><div id="GlobalDashboard"></div></div><footer id="PageFooter"></footer></body></html>`;
292
434
  }
293
435
 
@@ -0,0 +1,95 @@
1
+ // pane-loader.js — load SolidOS panes at run time, from URLs. No dependencies, no build.
2
+ //
3
+ // This is the copy shipped with JSS, from https://github.com/melvincarvalho/pane-loader
4
+ // with one difference: the upstream file ends with a convenience that loads whatever panes
5
+ // are named in its own URL (`pane-loader.js?pane=URL`). That is left out here, so the only
6
+ // panes this server loads are the ones in its `--panes` list.
7
+ //
8
+ // A dynamically loaded pane is an ES module at a URL whose default export is a SolidOS pane:
9
+ // { name, label(subject, context), render(subject, context), icon?, ... }. Nothing about the pane
10
+ // changes; only how it reaches the registry. See https://melvincarvalho.github.io/solidos-panes/#dynamic
11
+ //
12
+ // Works with released SolidOS as it is: mashlib puts the solid-panes module on the page as
13
+ // `window.panes`, and `panes.register` is all this needs.
14
+ //
15
+ // import { loadPanes } from './pane-loader.js'
16
+ // await loadPanes(['https://alice.example/public/panes/hello-pane.js'])
17
+
18
+ /** Is this object usable as a SolidOS pane? The same test `register` applies, plus render. */
19
+ export function isPane (p) {
20
+ return !!p && typeof p.name === 'string' && p.name !== '' &&
21
+ typeof p.label === 'function' && typeof p.render === 'function'
22
+ }
23
+
24
+ /**
25
+ * Load one pane module and register it. Never throws: a pane that cannot be loaded is skipped.
26
+ * @param {string} url URL of an ES module whose default export is a pane
27
+ * @param {object} [options]
28
+ * @param {object} [options.panes] the solid-panes module (default: the page's `panes`)
29
+ * @returns {Promise<{url: string, ok: boolean, name?: string, already?: boolean, reason?: string}>}
30
+ */
31
+ export async function loadPane (url, options = {}) {
32
+ const panes = options.panes || globalThis.panes
33
+ if (!panes || typeof panes.register !== 'function') {
34
+ return { url, ok: false, reason: 'no SolidOS pane registry on this page (window.panes.register)' }
35
+ }
36
+ let pane
37
+ try {
38
+ pane = (await import(url)).default
39
+ } catch (err) {
40
+ return { url, ok: false, reason: 'import failed: ' + (err && err.message ? err.message : err) }
41
+ }
42
+ if (!isPane(pane)) {
43
+ return { url, ok: false, reason: 'default export is not a pane (needs name, label, render)' }
44
+ }
45
+ const existing = (panes.list || []).find(p => p.name === pane.name)
46
+ if (existing === pane) return { url, ok: true, name: pane.name, already: true } // same module, loaded before
47
+ if (existing) return { url, ok: false, name: pane.name, reason: 'a different pane with this name is already registered' }
48
+ panes.register(pane)
49
+ return { url, ok: true, name: pane.name }
50
+ }
51
+
52
+ /**
53
+ * Load several panes, in order, then redraw the current subject so that new panes are offered.
54
+ * @param {string[]} urls
55
+ * @param {object} [options]
56
+ * @param {object} [options.panes] the solid-panes module (default: the page's `panes`)
57
+ * @param {boolean} [options.refresh] redraw the current subject afterwards (default true)
58
+ * @param {boolean} [options.show] open the last loaded pane that applies to the current subject
59
+ * @param {string} [options.subject] URL of the thing on show, when it is not the page's own URL
60
+ * @returns {Promise<Array>} one result per URL, see loadPane
61
+ */
62
+ export async function loadPanes (urls, options = {}) {
63
+ const results = []
64
+ for (const url of urls) {
65
+ const r = await loadPane(url, options)
66
+ results.push(r)
67
+ if (r.ok) console.info('[pane-loader] ' + (r.already ? 'already have' : 'registered') + ' "' + r.name + '" from ' + url)
68
+ else console.warn('[pane-loader] skipped ' + url + ': ' + r.reason)
69
+ }
70
+ if (options.refresh !== false && results.some(r => r.ok)) refresh(results, options)
71
+ return results
72
+ }
73
+
74
+ // Redraw the subject the data browser is showing. If `show` is set, open the newest loaded pane
75
+ // whose label accepts the subject; otherwise let SolidOS choose as usual (the new pane is a tab).
76
+ function refresh (results, options) {
77
+ const panes = options.panes || globalThis.panes
78
+ const $rdf = globalThis.$rdf
79
+ if (!panes || !$rdf || typeof panes.getOutliner !== 'function' || typeof document === 'undefined') return
80
+ try {
81
+ const outliner = panes.getOutliner(document)
82
+ const subject = $rdf.sym(options.subject || location.href.split('#')[0])
83
+ let pane
84
+ if (options.show) {
85
+ const context = outliner.context || {}
86
+ for (const r of results.filter(r => r.ok).reverse()) {
87
+ const p = panes.byName(r.name)
88
+ try { if (p && p.label(subject, context)) { pane = p; break } } catch (e) { /* a label that throws is a no */ }
89
+ }
90
+ }
91
+ outliner.GotoSubject(subject, true, pane, true, undefined)
92
+ } catch (err) {
93
+ console.warn('[pane-loader] could not redraw the page: ' + (err && err.message ? err.message : err))
94
+ }
95
+ }
package/src/server.js CHANGED
@@ -4,6 +4,7 @@ import rateLimit from '@fastify/rate-limit';
4
4
  import websocket from '@fastify/websocket';
5
5
  import { readFile } from 'fs/promises';
6
6
  import { readFileSync } from 'fs';
7
+ import { normalizePanes, PANE_LOADER_PATH } from './mashlib/index.js';
7
8
  import { STATUS_CODES } from 'node:http';
8
9
  import { join, dirname } from 'path';
9
10
  import { fileURLToPath } from 'url';
@@ -87,6 +88,8 @@ export function createServer(options = {}) {
87
88
  const mashlibCdn = options.mashlibCdn ?? false;
88
89
  const mashlibEnabled = mashlibCdn || !!mashlibModule;
89
90
  const mashlibVersion = options.mashlibVersion ?? '2.0.0';
91
+ // Panes loaded by the data browser page at start-up (#652). OFF by default.
92
+ const panes = normalizePanes(options.panes);
90
93
  // Git HTTP backend is OFF by default - enables clone/push via git protocol
91
94
  const gitEnabled = options.git ?? false;
92
95
  // CORS proxy (#378) — OFF by default. Numeric settings get the
@@ -343,6 +346,7 @@ export function createServer(options = {}) {
343
346
  fastify.decorateRequest('mashlibCdn', null);
344
347
  fastify.decorateRequest('mashlibVersion', null);
345
348
  fastify.decorateRequest('mashlibModule', null);
349
+ fastify.decorateRequest('panes', null);
346
350
  fastify.decorateRequest('defaultQuota', null);
347
351
  fastify.decorateRequest('provisionKeys', null);
348
352
  fastify.decorateRequest('config', null);
@@ -359,6 +363,7 @@ export function createServer(options = {}) {
359
363
  request.mashlibCdn = mashlibCdn;
360
364
  request.mashlibVersion = mashlibVersion;
361
365
  request.mashlibModule = mashlibModule;
366
+ request.panes = panes;
362
367
  request.defaultQuota = defaultQuota;
363
368
  request.provisionKeys = provisionKeysEnabled;
364
369
  request.config = { public: options.public, readOnly: options.readOnly };
@@ -957,6 +962,9 @@ export function createServer(options = {}) {
957
962
  // identity. GET/HEAD reach the LDP layer normally and serve the
958
963
  // file written by --provision-keys.
959
964
  '/.well-known/nostr.json',
965
+ // The pane loader (#652) is served from the package, never from
966
+ // storage; writes to its path are refused for the same reason.
967
+ PANE_LOADER_PATH,
960
968
  ]) {
961
969
  fastify.put(pat, methodNotAllowed);
962
970
  fastify.post(pat, methodNotAllowed);
@@ -980,6 +988,18 @@ export function createServer(options = {}) {
980
988
  });
981
989
  }
982
990
 
991
+ // The pane loader (#652): one static file from the package, served only
992
+ // when a data browser page can ask for it. Registered as a literal route
993
+ // so it wins over the LDP wildcard below; Fastify derives HEAD from GET.
994
+ // The page asks for it with ?v=<package version>, so it can be cached.
995
+ if (mashlibEnabled && panes.length) {
996
+ const paneLoaderSource = readFileSync(join(__dirname, 'mashlib', 'pane-loader.js'), 'utf8');
997
+ fastify.get(PANE_LOADER_PATH, async (request, reply) => reply
998
+ .header('Content-Type', 'text/javascript; charset=utf-8')
999
+ .header('Cache-Control', 'public, max-age=3600')
1000
+ .send(paneLoaderSource));
1001
+ }
1002
+
983
1003
  // LDP routes - using wildcard routing
984
1004
  // Read operations - no rate limit (handled by bodyLimit)
985
1005
  // The LDP wildcards never serve an app-owned subtree (#611): a request
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Panes loaded at start-up (#652).
3
+ *
4
+ * `--panes` gives the server a list of pane module URLs. The data browser
5
+ * page loads and registers them before it starts SolidOS. These tests pin:
6
+ * - the list is cleaned (http(s) URLs and absolute paths only), and the
7
+ * dropped entries are reported
8
+ * - with no panes the generated page is unchanged
9
+ * - with panes the page loads them, then starts SolidOS (CDN and local)
10
+ * - module mode is not touched
11
+ * - a URL cannot break out of the inline script
12
+ * - the start-up function: always starts once, a dead or slow pane host
13
+ * holds back only its own pane, configured order is kept
14
+ * - the loader is served only when panes are configured, its path
15
+ * cannot be written to, and it loads nothing named in its own URL
16
+ * - the 401 page is a data browser page too, and gets the panes
17
+ */
18
+
19
+ import { describe, it, before, after, beforeEach, afterEach } from 'node:test';
20
+ import assert from 'node:assert';
21
+ import fs from 'node:fs/promises';
22
+ import {
23
+ startTestServer,
24
+ stopTestServer,
25
+ request,
26
+ createTestPod,
27
+ assertStatus
28
+ } from './helpers.js';
29
+ import {
30
+ generateDatabrowserHtml,
31
+ generateModuleDatabrowserHtml,
32
+ jssStartDataBrowser,
33
+ normalizePanes,
34
+ sortPanes,
35
+ PANE_LOADER_PATH
36
+ } from '../src/mashlib/index.js';
37
+
38
+ const PANE = 'https://panes.example/calendar-pane.js';
39
+ const HTML = { Accept: 'text/html', 'Sec-Fetch-Dest': 'document' };
40
+
41
+ describe('panes at start-up — the list (unit, #652)', () => {
42
+ it('accepts an array or a comma-separated string', () => {
43
+ assert.deepStrictEqual(normalizePanes([PANE, '/public/panes/a.js']), [PANE, '/public/panes/a.js']);
44
+ assert.deepStrictEqual(normalizePanes(` ${PANE} , /public/panes/a.js ,`), [PANE, '/public/panes/a.js']);
45
+ });
46
+
47
+ it('is empty for nothing', () => {
48
+ for (const v of [undefined, null, false, '', [], ' , ']) {
49
+ assert.deepStrictEqual(normalizePanes(v), []);
50
+ }
51
+ });
52
+
53
+ it('drops anything that is not an http(s) URL or an absolute path', () => {
54
+ const bad = ['javascript:alert(1)', 'data:text/javascript,1', '../a.js', 'a.js', '//evil.example/a.js', 'ftp://x/a.js', 'https://x/a b.js', 42, null];
55
+ assert.deepStrictEqual(normalizePanes(bad), []);
56
+ });
57
+
58
+ it('removes duplicates and keeps order', () => {
59
+ assert.deepStrictEqual(normalizePanes(['/b.js', PANE, '/b.js']), ['/b.js', PANE]);
60
+ });
61
+
62
+ it('reports what it dropped', () => {
63
+ assert.deepStrictEqual(sortPanes(`${PANE}, htps://typo/a.js ,, foo.js`), {
64
+ urls: [PANE],
65
+ dropped: ['htps://typo/a.js', 'foo.js']
66
+ });
67
+ assert.deepStrictEqual(sortPanes(undefined), { urls: [], dropped: [] });
68
+ });
69
+
70
+ it('keeps a URL with a comma when given as an array', () => {
71
+ const url = 'https://cdn.example/combine/a.js,b.js';
72
+ assert.deepStrictEqual(normalizePanes([url]), [url]);
73
+ });
74
+ });
75
+
76
+ describe('panes at start-up — the page (unit, #652)', () => {
77
+ const modes = [
78
+ ['CDN', (opts) => generateDatabrowserHtml('https://pod.example/', '2.0.0', opts)],
79
+ ['local', (opts) => generateDatabrowserHtml('https://pod.example/', null, opts)]
80
+ ];
81
+
82
+ for (const [name, page] of modes) {
83
+ it(`${name} mode: no panes leaves the page unchanged`, () => {
84
+ const plain = page({});
85
+ assert.strictEqual(page({ panes: [] }), plain);
86
+ assert.strictEqual(page({ panes: null }), plain);
87
+ assert.strictEqual(page({ panes: 'not a url' }), plain);
88
+ assert.ok(!plain.includes(PANE_LOADER_PATH));
89
+ assert.ok(!plain.includes('jssStartDataBrowser'));
90
+ });
91
+
92
+ it(`${name} mode: panes are loaded through the loader, then SolidOS starts`, () => {
93
+ const html = page({ panes: [PANE] });
94
+ assert.ok(html.includes('function jssStartDataBrowser('), 'defines the start-up function');
95
+ assert.match(html, new RegExp(`jssStartDataBrowser\\(\\["${PANE.replace(/[.]/g, '\\.')}"\\], "/\\.well-known/jss/pane-loader\\.js\\?v=[^"]+", \\d+\\)`), 'calls it with the list, the versioned loader and a time limit');
96
+ // The only call to runDataBrowser is the one inside the function.
97
+ assert.strictEqual(html.split('panes.runDataBrowser()').length - 1, 1);
98
+ });
99
+ }
100
+
101
+ it('module mode is not touched', () => {
102
+ const plain = generateModuleDatabrowserHtml('https://cdn.example/mashlib.js', 'https://pod.example/', {});
103
+ const withPanes = generateModuleDatabrowserHtml('https://cdn.example/mashlib.js', 'https://pod.example/', { panes: [PANE] });
104
+ assert.strictEqual(withPanes, plain);
105
+ });
106
+
107
+ it('a pane URL cannot close the inline script', () => {
108
+ const nasty = 'https://panes.example/a.js?x=</script><script>alert(1)</script>';
109
+ for (const [, page] of modes) {
110
+ const html = page({ panes: [nasty] });
111
+ assert.ok(!html.includes('<script>alert(1)'), 'no injected script tag');
112
+ assert.ok(html.includes('\\u003c/script>'), 'the < is escaped inside the script');
113
+ }
114
+ });
115
+ });
116
+
117
+ describe('panes at start-up — the start-up function (unit, #652)', () => {
118
+ let starts, registered, log, saved;
119
+
120
+ // A fake loader: loadPane registers a pane named after its URL, or
121
+ // reports a failure for a URL the fake "network" rejects.
122
+ const fakeLoader = (failing = []) => ({
123
+ loadPane: async (url) => {
124
+ if (failing.includes(url)) return { url, ok: false, reason: 'import failed' };
125
+ registered.push(url);
126
+ return { url, ok: true, name: url };
127
+ }
128
+ });
129
+ // A fake import(): the loader URL gives the loader; each pane URL
130
+ // settles when the test says so.
131
+ const network = (loader, behaviour = {}) => (url) => {
132
+ if (url === 'LOADER') return loader ? Promise.resolve(loader) : Promise.reject(new Error('no loader'));
133
+ const b = behaviour[url];
134
+ if (b === 'hang') return new Promise(() => {});
135
+ if (b === 'fail') return Promise.reject(new Error('404'));
136
+ if (typeof b === 'number') return new Promise((resolve) => setTimeout(() => resolve({}), b));
137
+ return Promise.resolve({});
138
+ };
139
+
140
+ beforeEach(() => {
141
+ starts = 0; registered = []; log = [];
142
+ saved = { panes: globalThis.panes, info: console.info, warn: console.warn };
143
+ globalThis.panes = { runDataBrowser: () => { starts++; log.push('start'); } };
144
+ console.info = (m) => log.push('info ' + m);
145
+ console.warn = (m) => log.push('warn ' + m);
146
+ });
147
+ afterEach(() => {
148
+ globalThis.panes = saved.panes; console.info = saved.info; console.warn = saved.warn;
149
+ });
150
+
151
+ it('registers every pane, in the configured order, then starts once', async () => {
152
+ // b arrives before a, but a is listed first.
153
+ await jssStartDataBrowser(['a', 'b'], 'LOADER', 1000, network(fakeLoader(), { a: 30, b: 0 }));
154
+ assert.deepStrictEqual(registered, ['a', 'b']);
155
+ assert.strictEqual(starts, 1);
156
+ assert.strictEqual(log[log.length - 1], 'start', 'SolidOS starts after the panes');
157
+ });
158
+
159
+ it('skips a pane that fails and still starts', async () => {
160
+ await jssStartDataBrowser(['bad', 'good'], 'LOADER', 1000, network(fakeLoader(['bad']), { bad: 'fail' }));
161
+ assert.deepStrictEqual(registered, ['good']);
162
+ assert.strictEqual(starts, 1);
163
+ assert.ok(log.some((l) => l.startsWith('warn [jss] pane skipped: bad')));
164
+ });
165
+
166
+ it('starts without panes when the loader cannot be imported', async () => {
167
+ await jssStartDataBrowser(['a'], 'LOADER', 1000, network(null));
168
+ assert.deepStrictEqual(registered, []);
169
+ assert.strictEqual(starts, 1);
170
+ assert.ok(log.some((l) => l.startsWith('warn [jss] panes not loaded')));
171
+ });
172
+
173
+ it('a host that never answers holds back only its own pane', async () => {
174
+ const t0 = Date.now();
175
+ await jssStartDataBrowser(['dead', 'good'], 'LOADER', 60, network(fakeLoader(), { dead: 'hang' }));
176
+ assert.ok(Date.now() - t0 < 1000, 'did not wait for the dead host');
177
+ assert.deepStrictEqual(registered, ['good'], 'the pane listed after the dead one is registered');
178
+ assert.strictEqual(starts, 1);
179
+ });
180
+
181
+ it('a pane slower than the limit is registered when it arrives, without a second start', async () => {
182
+ await jssStartDataBrowser(['slow', 'good'], 'LOADER', 40, network(fakeLoader(), { slow: 120 }));
183
+ assert.deepStrictEqual(registered, ['good']);
184
+ assert.strictEqual(starts, 1);
185
+ await new Promise((resolve) => setTimeout(resolve, 150));
186
+ assert.deepStrictEqual(registered, ['good', 'slow']);
187
+ assert.strictEqual(starts, 1);
188
+ assert.ok(log.some((l) => l.includes('"slow"') && l.includes('(after start-up)')));
189
+ });
190
+
191
+ it('starts at the time limit even if the loader itself hangs', async () => {
192
+ const hangingLoader = (url) => new Promise(() => {});
193
+ jssStartDataBrowser(['a'], 'LOADER', 30, hangingLoader);
194
+ await new Promise((resolve) => setTimeout(resolve, 80));
195
+ assert.strictEqual(starts, 1);
196
+ });
197
+ });
198
+
199
+ describe('panes at start-up — the shipped loader (unit, #652)', () => {
200
+ it('loads nothing named in its own URL', async () => {
201
+ const source = await fs.readFile(new URL('../src/mashlib/pane-loader.js', import.meta.url), 'utf8');
202
+ assert.ok(!/searchParams|import\.meta/.test(source), 'no ?pane= handling in the shipped copy');
203
+ assert.match(source, /export async function loadPane /);
204
+ });
205
+ });
206
+
207
+ describe('panes at start-up — live server with panes (#652)', () => {
208
+ before(async () => {
209
+ await startTestServer({ mashlibCdn: true, panes: `${PANE}, /public/panes/local-pane.js` });
210
+ await createTestPod('panetest');
211
+ });
212
+ after(async () => { await stopTestServer(); });
213
+
214
+ it('serves the loader as JavaScript, with or without the version query', async () => {
215
+ for (const url of [PANE_LOADER_PATH, PANE_LOADER_PATH + '?v=1.2.3']) {
216
+ const res = await request(url);
217
+ assertStatus(res, 200);
218
+ assert.match(res.headers.get('content-type'), /^text\/javascript/);
219
+ assert.match(await res.text(), /export async function loadPanes/);
220
+ }
221
+ });
222
+
223
+ it('answers HEAD for the loader with the same length as GET', async () => {
224
+ const get = await request(PANE_LOADER_PATH);
225
+ await get.text();
226
+ const head = await request(PANE_LOADER_PATH, { method: 'HEAD' });
227
+ assertStatus(head, 200);
228
+ assert.match(head.headers.get('content-type'), /^text\/javascript/);
229
+ assert.strictEqual(head.headers.get('content-length'), get.headers.get('content-length'));
230
+ });
231
+
232
+ it('refuses writes to the loader path', async () => {
233
+ for (const method of ['PUT', 'DELETE', 'PATCH', 'POST']) {
234
+ const res = await request(PANE_LOADER_PATH, {
235
+ method,
236
+ headers: { 'Content-Type': 'text/javascript' },
237
+ body: method === 'DELETE' ? undefined : 'export default 1',
238
+ auth: 'panetest'
239
+ });
240
+ assertStatus(res, 405, `${method} must be refused`);
241
+ }
242
+ const res = await request(PANE_LOADER_PATH);
243
+ assert.match(await res.text(), /export async function loadPanes/, 'still the packaged loader');
244
+ });
245
+
246
+ it('the data browser page loads the configured panes', async () => {
247
+ const res = await request('/panetest/public/', { headers: HTML });
248
+ assertStatus(res, 200);
249
+ const html = await res.text();
250
+ assert.ok(html.includes('function jssStartDataBrowser('));
251
+ assert.ok(html.includes(JSON.stringify([PANE, '/public/panes/local-pane.js'])));
252
+ });
253
+
254
+ it('the 401 page for a private resource loads them too', async () => {
255
+ const res = await request('/panetest/private/', { headers: HTML });
256
+ assertStatus(res, 401);
257
+ const html = await res.text();
258
+ assert.ok(html.includes('function jssStartDataBrowser('), 'the unauthorised page is a data browser page');
259
+ assert.ok(html.includes(JSON.stringify([PANE, '/public/panes/local-pane.js'])));
260
+ });
261
+ });
262
+
263
+ describe('panes at start-up — live server without panes (#652)', () => {
264
+ before(async () => {
265
+ await startTestServer({ mashlibCdn: true });
266
+ await createTestPod('nopanes');
267
+ });
268
+ after(async () => { await stopTestServer(); });
269
+
270
+ it('does not serve the loader', async () => {
271
+ const res = await request(PANE_LOADER_PATH);
272
+ assertStatus(res, 404);
273
+ });
274
+
275
+ it('the data browser page starts SolidOS directly', async () => {
276
+ const res = await request('/nopanes/public/', { headers: HTML });
277
+ assertStatus(res, 200);
278
+ const html = await res.text();
279
+ assert.ok(!html.includes('jssStartDataBrowser'));
280
+ assert.ok(html.includes('panes.runDataBrowser()'));
281
+ });
282
+ });