forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathcgi-worker.mjs
More file actions
139 lines (127 loc) · 4.97 KB
/
cgi-worker.mjs
File metadata and controls
139 lines (127 loc) · 4.97 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
/* eslint-disable no-restricted-globals */
import { PhpCgiWorker } from "php-cgi-wasm/PhpCgiWorker.mjs";
import { PGlite } from '@electric-sql/pglite';
const buildType = process.env.BUILD_TYPE ?? 'dynamic';
const sharedLibs = [];
const files = [
{ parent: '/preload/test_www/', name: 'hello-world.php', url: './scripts/hello-world.php' },
{ parent: '/preload/test_www/', name: 'phpinfo.php', url: './scripts/phpinfo.php' },
{ parent: '/preload/', name: 'list-extensions.php', url: './scripts/list-extensions.php' },
];
if(buildType === 'dynamic')
{
sharedLibs.push(...(await Promise.all([
import('php-wasm-libxml'),
import('php-wasm-dom'),
import('php-wasm-zlib'),
import('php-wasm-libzip'),
import('php-wasm-gd'),
import('php-wasm-iconv'),
import('php-wasm-intl'),
import('php-wasm-openssl'),
import('php-wasm-mbstring'),
import('php-wasm-sqlite'),
import('php-wasm-xml'),
import('php-wasm-simplexml'),
import('php-wasm-tidy'),
import('php-wasm-yaml'),
])).map(m => m.default));
}
else if(buildType === 'shared')
{
// files.push(
// { parent: '/preload/', name: 'icudt72l.dat', url: (await import('php-wasm-intl/icudt72l.dat')).default }
// );
sharedLibs.push(
{ name: 'libxml2.so', url: (await import('php-wasm-libxml/libxml2.so' )).default },
{ name: 'libz.so', url: (await import('php-wasm-zlib/libz.so' )).default },
{ name: 'libzip.so', url: (await import('php-wasm-libzip/libzip.so' )).default },
{ name: 'libfreetype.so', url: (await import('php-wasm-gd/libfreetype.so' )).default },
{ name: 'libjpeg.so', url: (await import('php-wasm-gd/libjpeg.so' )).default },
{ name: 'libwebp.so', url: (await import('php-wasm-gd/libwebp.so' )).default },
{ name: 'libpng.so', url: (await import('php-wasm-gd/libpng.so' )).default },
{ name: 'libiconv.so', url: (await import('php-wasm-iconv/libiconv.so' )).default },
{ name: 'libicuuc.so', url: (await import('php-wasm-intl/libicuuc.so' )).default },
{ name: 'libicutu.so', url: (await import('php-wasm-intl/libicutu.so' )).default },
{ name: 'libicutest.so', url: (await import('php-wasm-intl/libicutest.so' )).default },
{ name: 'libicuio.so', url: (await import('php-wasm-intl/libicuio.so' )).default },
{ name: 'libicui18n.so', url: (await import('php-wasm-intl/libicui18n.so' )).default },
{ name: 'libicudata.so', url: (await import('php-wasm-intl/libicudata.so' )).default },
{ name: 'libcrypto.so', url: (await import('php-wasm-openssl/libcrypto.so' )).default },
{ name: 'libssl.so', url: (await import('php-wasm-openssl/libssl.so' )).default },
{ name: 'libonig.so', url: (await import('php-wasm-mbstring/libonig.so' )).default },
{ name: 'libsqlite3.so', url: (await import('php-wasm-sqlite/libsqlite3.so' )).default },
{ name: 'libtidy.so', url: (await import('php-wasm-tidy/libtidy.so' )).default },
{ name: 'libyaml.so', url: (await import('php-wasm-yaml/libyaml.so' )).default },
);
}
else
{
sharedLibs.push(
{name: 'libcrypto.so', url: (await import(('php-wasm-openssl/libcrypto.so'))).default},
{name: 'libssl.so', url: (await import(('php-wasm-openssl/libssl.so' ))).default},
);
}
// 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 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({
version: '8.3'
, 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'
},
vHosts: [
{
"pathPrefix": "/php-wasm/cgi-bin/test",
"directory": "/preload/test_www",
"entrypoint": "hello-world.php"
},
]
});
// 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'));