-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathload-built-extension.mjs
More file actions
75 lines (71 loc) · 1.86 KB
/
Copy pathload-built-extension.mjs
File metadata and controls
75 lines (71 loc) · 1.86 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
import { loadNodeRuntime } from '@php-wasm/node';
import {
loadPHPRuntime,
PHP,
resolvePHPExtension,
withResolvedPHPExtensions,
} from '@php-wasm/universal';
import { readFile } from 'node:fs/promises';
import { pathToFileURL } from 'node:url';
const [manifestPath, phpVersion, code, expectedOutput, runtimeLoaderPath] =
process.argv.slice(2);
if (!manifestPath || !phpVersion || !code) {
throw new Error(
'Usage: load-built-extension.mjs <manifest> <php-version> <php-code> <expected-output> [runtime-loader]'
);
}
const php = runtimeLoaderPath
? await loadFreshRuntime(manifestPath, phpVersion, runtimeLoaderPath)
: new PHP(
await loadNodeRuntime(phpVersion, {
emscriptenOptions: { processId: 1 },
extensions: [
{
source: {
format: 'manifest',
manifestUrl: manifestPath,
},
},
],
})
);
try {
const result = await php.run({ code });
if (result.errors) {
throw new Error(result.errors);
}
if (result.text !== expectedOutput) {
throw new Error(
`Expected ${JSON.stringify(expectedOutput)}, got ${JSON.stringify(
result.text
)}`
);
}
} finally {
php.exit();
}
async function loadFreshRuntime(manifestPath, phpVersion, runtimeLoaderPath) {
const runtimeModule = await import(pathToFileURL(runtimeLoaderPath).href);
const extension = await resolvePHPExtension({
source: {
format: 'manifest',
manifestUrl: pathToFileURL(manifestPath).href,
},
phpVersion,
fetch: async (url) => {
const response =
new URL(url).protocol === 'file:'
? new Response(await readFile(new URL(url)))
: await fetch(url);
if (!response.ok) {
throw new Error(`Could not load extension artifact: ${url}`);
}
return response;
},
});
const runtimeId = await loadPHPRuntime(
runtimeModule,
withResolvedPHPExtensions({ phpWasmAsyncMode: 'jspi' }, [extension])
);
return new PHP(runtimeId);
}