forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
cgi-worker.mjs
82 lines (73 loc) · 2.25 KB
/
cgi-worker.mjs
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
/* eslint-disable no-restricted-globals */
import { PhpCgiWorker } from "php-cgi-wasm/PhpCgiWorker.mjs";
import { PGlite } from '@electric-sql/pglite';
// Log requests
const onRequest = (request, response) => {
const url = new URL(request.url);
const logLine = `[${(new Date).toISOString()}]`
+ `#${php.count} 127.0.0.1 - "${request.method}`
+ ` ${url.pathname}" - HTTP/1.1 ${response.status}`;
console.log(logLine);
};
// Formatted 404s
const notFound = request => {
return new Response(
`<body><h1>404</h1>${request.url} not found</body>`,
{status: 404, headers:{'Content-Type': 'text/html'}}
);
};
const sharedLibs = [
`php\${PHP_VERSION}-zlib.so`,
`php\${PHP_VERSION}-zip.so`,
`php\${PHP_VERSION}-gd.so`,
`php\${PHP_VERSION}-iconv.so`,
`php\${PHP_VERSION}-intl.so`,
`php\${PHP_VERSION}-openssl.so`,
`php\${PHP_VERSION}-dom.so`,
`php\${PHP_VERSION}-mbstring.so`,
`php\${PHP_VERSION}-sqlite.so`,
`php\${PHP_VERSION}-pdo-sqlite.so`,
`php\${PHP_VERSION}-xml.so`,
`php\${PHP_VERSION}-simplexml.so`,
{url: `libxml2.so`, ini: false},
];
const files = [{ parent: '/preload/', name: 'icudt72l.dat', url: './icudt72l.dat' }];
const actions = {
runSql: (php, database, sql) => {
console.log({database});
const pglite = new PGlite(database);
return pglite.query(sql);
},
execSql: (php, database, sql) => {
console.log(database)
const pglite = new PGlite(database);
return pglite.exec(sql);
}
};
// Spawn the PHP-CGI binary
const php = new PhpCgiWorker({
onRequest, notFound
, sharedLibs
, files
, PGlite
, actions
, staticFS: false
, prefix: '/php-wasm/cgi-bin/'
, exclude: ['/php-wasm/cgi-bin/~!@', '/php-wasm/cgi-bin/.']
, docroot: '/persist/www'
, types: {
jpeg: 'image/jpeg'
, jpg: 'image/jpeg'
, gif: 'image/gif'
, png: 'image/png'
, svg: 'image/svg+xml'
}
});
// Set up the event handlers
self.addEventListener('install', event => php.handleInstallEvent(event));
self.addEventListener('activate', event => php.handleActivateEvent(event));
self.addEventListener('fetch', event => php.handleFetchEvent(event));
self.addEventListener('message', event => php.handleMessageEvent(event));
// Extras
self.addEventListener('install', event => console.log('Install'));
self.addEventListener('activate', event => console.log('Activate'));