forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathInstallDemo.js
295 lines (240 loc) · 8.32 KB
/
InstallDemo.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import './Common.css';
import './InstallDemo.css';
import { PhpWeb } from 'php-wasm/PhpWeb';
import { useEffect, useState } from 'react';
import { sendMessageFor } from 'php-cgi-wasm/msg-bus';
import loader from './tail-spin.svg';
import NextIcon from './icons/forward-icon-32.png'
import BackIcon from './icons/back-icon-32.png'
import WwwIcon from './icons/www-icon-32.png'
import editorIcon from './icons/editor-icon-32.png';
navigator.serviceWorker.register(process.env.PUBLIC_URL + `/cgi-worker.js`);
const params = new URLSearchParams(window.location.search);
if(!params.has('no-service-worker'))
{
setTimeout(() => {
if(!(navigator.serviceWorker && navigator.serviceWorker.controller))
{
window.location.reload()
}
}, 350);
}
const sendMessage = sendMessageFor((`${window.location.origin}${process.env.PUBLIC_URL}/cgi-worker.mjs`))
const packages = {
'drupal-7': {
name: 'Drupal 7',
//*/
file: '/backups/drupal-7.95.zip',
/*/
file: '/backups/drupal-7.95-pgsql.zip',
sql: '/backups/drupal-7.95.sql',
//*/
path: 'drupal-7.95',
vHost: 'drupal',
dir: 'drupal-7.95',
entry: 'index.php',
},
'cakephp-5': {
name: 'CakePHP 5',
file: '/backups/cakephp-5.zip',
path: 'cakephp-5',
vHost: 'cakephp-5',
dir: 'cakephp-5/webroot',
entry: 'index.php',
},
'codeigniter-4': {
name: 'CodeIgniter 4',
file: '/backups/codeigniter-4.zip',
path: 'codeigniter-4',
vHost: 'codeigniter-4',
dir: 'codeigniter-4/public',
entry: 'index.php',
},
'laminas-3': {
name: 'Laminas 3',
file: '/backups/laminas-3.zip',
path: 'laminas-3',
vHost: 'laminas-3',
dir: 'laminas-3/public',
entry: 'index.php',
},
'laravel-11': {
name: 'Laravel 11',
file: '/backups/laravel-11.zip',
path: 'laravel-11',
vHost: 'laravel-11',
dir: 'laravel-11/public',
entry: 'index.php',
}
};
const sharedLibs = [`php${PhpWeb.phpVersion}-zip.so`, `php${PhpWeb.phpVersion}-zlib.so`];
const installDemo = async (overwrite = false) => {
const query = new URLSearchParams(window.location.search);
if(!query.has('framework'))
{
window.dispatchEvent(
new CustomEvent('install-status', {detail: 'No framework selected.'})
);
return;
}
const selectedFrameworkName = query.get('framework');
if(!(selectedFrameworkName in packages))
{
window.dispatchEvent(
new CustomEvent('install-status', {detail: 'Invalid framework selected.'})
);
return;
}
if(query.has('overwrite'))
{
overwrite = overwrite || query.get('overwrite');
}
const selectedFramework = packages[selectedFrameworkName];
await navigator.serviceWorker.register(process.env.PUBLIC_URL + `/cgi-worker.js`);
await navigator.serviceWorker.getRegistration(`${window.location.origin}${process.env.PUBLIC_URL}/cgi-worker.mjs`);
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Acquiring Lock...'}));
const initPhpCode = await (await fetch(process.env.PUBLIC_URL + '/scripts/init.php')).text();
await navigator.locks.request('php-wasm-demo-install', async () => {
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Checking for Existing Install...'}));
const checkPath = await sendMessage('analyzePath', ['/persist/' + selectedFramework.dir]);
if(!overwrite && checkPath.exists)
{
window.location = '/php-wasm/cgi-bin/' + selectedFramework.vHost;
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Already installed...'}));
if(window.opener)
{
window.opener.dispatchEvent(new CustomEvent('install-complete', {detail: selectedFrameworkName}));
}
return;
}
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Downloading package...'}));
const downloadZip = fetch(process.env.PUBLIC_URL + selectedFramework.file);
const zipContents = await (await downloadZip).arrayBuffer();
await sendMessage('writeFile', ['/config/restore-path.tmp', '/persist/' + selectedFramework.path]);
await sendMessage('writeFile', ['/persist/restore.zip', new Uint8Array(zipContents)]);
const php = new PhpWeb({sharedLibs, persist: [{mountPath:'/persist'}, {mountPath:'/config'}]});
php.addEventListener('output', event => console.log(event.detail));
php.addEventListener('error', event => console.log(event.detail));
await php.binary;
const settings = await sendMessage('getSettings');
const vHostPrefix = '/php-wasm/cgi-bin/' + selectedFramework.vHost;
const existingvHost = settings.vHosts.find(vHost => vHost.pathPrefix === vHostPrefix);
if(!existingvHost)
{
settings.vHosts.push({
pathPrefix: vHostPrefix,
directory: '/persist/' + selectedFramework.dir,
entrypoint: selectedFramework.entry
});
}
else
{
existingvHost.directory = '/persist/' + selectedFramework.dir;
existingvHost.entrypoint = selectedFramework.entry;
}
await sendMessage('setSettings', [settings]);
await sendMessage('storeInit');
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Unpacking files...'}));
await php.run(initPhpCode);
if(selectedFramework.sql)
{
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Setting up PostgreSQL...'}));
const sqlFile = await (await fetch(selectedFramework.sql)).text();
await sendMessage('execSql', [`idb://host= dbname=drupal port=5432`, sqlFile]);
await sendMessage('runSql', [`idb://host= dbname=drupal port=5432`, 'select * from information_schema.tables']);
}
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Refreshing PHP...'}));
await sendMessage('refresh', []);
window.dispatchEvent(new CustomEvent('install-status', {detail: 'Opening site...'}));
if(window.opener)
{
window.opener.dispatchEvent(new CustomEvent('install-complete', {detail: selectedFrameworkName}));
}
window.location = vHostPrefix;
});
};
const openDemo = () => {
const query = new URLSearchParams(window.location.search);
if(!query.has('framework'))
{
window.dispatchEvent(
new CustomEvent('install-status', {detail: 'No framework selected.'})
);
return;
}
const selectedFrameworkName = query.get('framework');
if(!(selectedFrameworkName in packages))
{
window.dispatchEvent(
new CustomEvent('install-status', {detail: 'Invalid framework selected.'})
);
return;
}
const selectedFramework = packages[selectedFrameworkName];
window.location = '/php-wasm/cgi-bin/' + selectedFramework.vHost;
}
const openCode = () => {
const query = new URLSearchParams(window.location.search);
if(!query.has('framework'))
{
window.dispatchEvent(
new CustomEvent('install-status', {detail: 'No framework selected.'})
);
return;
}
const selectedFrameworkName = query.get('framework');
if(!(selectedFrameworkName in packages))
{
window.dispatchEvent(
new CustomEvent('install-status', {detail: 'Invalid framework selected.'})
);
return;
}
const selectedFramework = packages[selectedFrameworkName];
window.location = process.env.PUBLIC_URL + '/code-editor.html?path=/persist/' + selectedFramework.path;
}
export default function InstallDemo() {
const [message, setMessage] = useState('Initializing...');
const onStatus = event => setMessage(event.detail);
useEffect(() => {
window.addEventListener('install-status', onStatus);
return () => {
window.removeEventListener('install-status', onStatus);
}
}, []);
window.demoInstalling = window.demoInstalling || installDemo();
return (
<div className = "install-demo">
<div className = "center">
{ message !== 'Site already exists!'
? <img className = "loader-icon" src = {loader} alt = "loading spinner" />
: ''
}
<div className = "bevel">
<div className = "inset padded">{message}</div>
{ message === 'Site already exists!'
? <div className = 'button-bar inset'>
<button className = "padded" onClick = {() => window.location = process.env.PUBLIC_URL + '/select-framework.html'}>
<img src = {BackIcon} className = "icon" />
Back
</button>
<button className = "padded" onClick = {() => openDemo()}>
<img src = {WwwIcon} className = "icon" />
Open Site
</button>
<button className = "padded" onClick = {() => openCode()}>
<img src = {editorIcon} className = "icon" />
Edit Files
</button>
<button className = "padded" onClick = {() => installDemo(true)}>
Overwrite
<img src = {NextIcon} className = "icon" />
</button>
</div>
: ''
}
</div>
</div>
</div>
);
}