Repository navigation
Expand file tree
/
Copy pathdeploy-boot.js
More file actions
181 lines (172 loc) · 8.39 KB
/
Copy pathdeploy-boot.js
File metadata and controls
181 lines (172 loc) · 8.39 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
// deploy-boot.js — shared boot for deployed Ampleforth documents.
//
// Each deployed page is lean:
// <body data-ampleforth-doc="Name">
// <div id="ampleforth-ssr"> ...crawlable static prose... </div>
// <script src="deploy-boot.js"></script>
//
// This script reads the document name, removes the static fallback once the
// live UI mounts, and boots the document viewer on one of two runtimes
// (EURASIA_WEB_PUBLISHING_DESIGN_2026-11-23.md, section 2.2):
//
// - psoup: points the URL at AmpleforthViewer.vfuel + <Name>.zip and replays
// the standard primordialsoup.html loader, fetched at run time, so there is
// no second copy of its pinned boot boilerplate.
// - Eurasia: loads the libraries, then runs viewer/base.eus on eurasia_web.wasm
// through eurasia.mjs. The URL is left alone: the viewer reads the name from
// data-ampleforth-doc, or from ?docName= on a page without one
// (eurasia-viewer.html).
//
// The runtime is DEFAULT_RUNTIME, unless the page's body says otherwise
// (data-ampleforth-runtime="psoup" or "eurasia"), and the URL overrides both
// (?psoup or ?eurasia). On Eurasia, ?interpret runs without the compiler,
// and Save Image keeps the image in the browser, to be offered at the next
// load (?image=<name> opens one).
//
// Eurasia's files are fetched relative to this script, so a page in a
// subdirectory that loads ../deploy-boot.js finds the runtime beside it;
// psoup's, as before, relative to the page.
(function () {
// psoup while Eurasia is offered beside it; 'eurasia' once the sites swap.
var DEFAULT_RUNTIME = 'psoup';
var script = document.currentScript;
var base = new URL('.', script ? script.src : window.location.href);
var here = new URL(window.location);
var docName = document.body.getAttribute('data-ampleforth-doc') || '';
var runtime = here.searchParams.has('eurasia') ? 'eurasia'
: here.searchParams.has('psoup') ? 'psoup'
: document.body.getAttribute('data-ampleforth-runtime') || DEFAULT_RUNTIME;
var ssr = document.getElementById('ampleforth-ssr');
if (ssr) {
var obs = new MutationObserver(function (muts) {
for (var i = 0; i < muts.length; i++) {
var added = muts[i].addedNodes;
for (var j = 0; j < added.length; j++) {
var n = added[j];
if (n.nodeType === 1 && n.id !== 'ampleforth-ssr' && n.id !== 'eurasia-images' && n.tagName !== 'SCRIPT' && n.tagName !== 'LINK') {
obs.disconnect();
requestAnimationFrame(function () { if (ssr && ssr.parentNode) ssr.parentNode.removeChild(ssr); });
return;
}
}
}
});
obs.observe(document.body, { childList: true });
}
function at(path) { return new URL(path, base).href; }
// Appends the scripts one after the other, each after the previous has
// loaded, then calls done.
function loadScripts(srcs, done) {
(function next(i) {
if (i >= srcs.length) { done(); return; }
var s = document.createElement('script');
s.src = at(srcs[i]);
s.async = false;
s.onload = function () { next(i + 1); };
s.onerror = function () { fail('cannot load ' + srcs[i]); };
document.body.appendChild(s);
})(0);
}
function fail(message) {
console.error('deploy-boot:', message);
var p = document.createElement('p');
p.textContent = 'The live document could not start: ' + message;
document.body.appendChild(p);
}
// Eurasia. The libraries are the ones eurasia/web/libs holds; every one must
// be loaded before the VM, which starts the viewer as soon as it runs.
var EURASIA_LIBRARIES = [
'vendor/buffer-6.0.3.bundle.min.js',
'CodeMirror/lib/codemirror.js',
'CodeMirror/addon/display/autorefresh.js',
'jszip.min.js',
'vendor/isomorphic-git-1.38.4.umd.min.js',
'vendor/lightning-fs-4.6.2.min.js',
'vendor/isomorphic-git-1.38.4-http-web.umd.js',
'vendor/diff_match_patch-2019-07.js',
'custom-post.js'
];
function bootEurasia() {
var css = document.createElement('link');
css.rel = 'stylesheet';
css.href = at('CodeMirror/lib/codemirror.css');
document.head.appendChild(css);
loadScripts(EURASIA_LIBRARIES, function () {
window.Buffer = window.buffer.Buffer;
function bytes(path) {
return fetch(at(path)).then(function (r) {
if (!r.ok) throw new Error('cannot fetch ' + path + ': ' + r.status);
return r.arrayBuffer();
}).then(function (b) { return new Uint8Array(b); });
}
var jit = here.searchParams.has('interpret') ? 0 : 1;
// Saved images (eurasia's images.mjs): Save Image keeps the running
// image in the browser, and the page offers the saved ones, or a fresh
// start on its own document, the next time it loads; ?image=<name>
// opens one directly.
var images = import(at('images.mjs'))
.then(function (m) { return m.imageStore('eurasia-document-images'); })
.catch(function (e) { console.warn('deploy-boot: images are not kept:', e.message); return null; });
var start = images.then(function (store) {
if (!store) return null;
if (here.searchParams.has('image')) return store.read(here.searchParams.get('image'));
return store.list().then(function (saved) {
return saved.length > 0 ? store.choose(saved, 'Start fresh') : null;
});
}).catch(function (e) {
console.warn('deploy-boot: no saved images:', e.message);
return null;
}).then(function (saved) { return saved || bytes('viewer/base.eus'); });
Promise.all([import(at('eurasia.mjs')), bytes('eurasia_web.wasm'), start, images])
.then(function (parts) {
var store = parts[3];
return parts[0].run(parts[1], parts[2], {
print: function (line) { console.log(line); },
jit: jit,
onSaveImage: store && function (name, image) {
store.store(name, image).catch(function (e) {
console.error('deploy-boot: the image was not saved:', e);
alert('The image ' + name + ' was not saved: ' + e.message);
});
}
});
})
.then(function (result) {
if (result.status === 'exited') console.log('eurasia: exited ' + result.code);
})
.catch(function (e) { fail(e.message); });
});
}
// psoup. Replay the standard loader's <script>/<link>/<style>. IMPORTANT:
// load primordialsoup.js (the WASM runtime) LAST. It boots as soon as it
// loads and immediately runs main: -> loadFromServerNamed: -> JSZip
// loadAsync:, so every vendor lib it reaches for (jszip, isomorphic-git, ...)
// must already be present. Replaying in document order raced the WASM boot
// against jszip.min.js (loaded later in the chain), intermittently DNU-ing on
// an undefined JSZip. Deferring primordialsoup.js to the end guarantees the
// libs are loaded before boot.
function bootPsoup() {
var url = new URL(window.location), changed = false;
if (!url.searchParams.get('snapshot')) { url.searchParams.set('snapshot', 'AmpleforthViewer.vfuel'); changed = true; }
if (!url.searchParams.get('docName') && docName) { url.searchParams.set('docName', docName); changed = true; }
if (changed) history.replaceState(null, '', url);
fetch('primordialsoup.html').then(function (r) { return r.text(); }).then(function (html) {
var loaded = new DOMParser().parseFromString(html, 'text/html');
var nodes = Array.prototype.slice.call(loaded.querySelectorAll('script, link, style'));
var boot = nodes.filter(function (n) { return n.tagName === 'SCRIPT' && (n.getAttribute('src') || '').indexOf('primordialsoup.js') !== -1; });
nodes = nodes.filter(function (n) { return boot.indexOf(n) === -1; }).concat(boot);
(function next(i) {
if (i >= nodes.length) return;
var src = nodes[i];
if (src.tagName === 'SCRIPT') {
var s = document.createElement('script');
for (var a = 0; a < src.attributes.length; a++) s.setAttribute(src.attributes[a].name, src.attributes[a].value);
s.async = false;
if (src.getAttribute('src')) { s.onload = s.onerror = function () { next(i + 1); }; document.body.appendChild(s); }
else { s.textContent = src.textContent; document.body.appendChild(s); next(i + 1); }
} else { document.head.appendChild(src.cloneNode(true)); next(i + 1); }
})(0);
});
}
if (runtime === 'eurasia') bootEurasia(); else bootPsoup();
})();