|
| 1 | +import { EmscriptenDownloadMonitor } from '@php-wasm/progress'; |
| 2 | +import { exposeAPI } from '@php-wasm/web'; |
| 3 | +import { PlaygroundWorkerEndpoint } from './worker-thread'; |
| 4 | +import { randomString } from '@php-wasm/util'; |
| 5 | +import { |
| 6 | + getSqliteDriverModuleDetails, |
| 7 | + getWordPressModuleDetails, |
| 8 | + LatestMinifiedWordPressVersion, |
| 9 | + LatestSqliteDriverVersion, |
| 10 | + MinifiedWordPressVersionsList, |
| 11 | +} from '@wp-playground/wordpress-builds'; |
| 12 | +import { directoryHandleFromMountDevice } from '@wp-playground/storage'; |
| 13 | +import { bootJustWordPress } from '@wp-playground/wordpress'; |
| 14 | +import { createDirectoryHandleMountHandler } from '@php-wasm/web'; |
| 15 | +import type { PHP } from '@php-wasm/universal'; |
| 16 | +/* @ts-ignore */ |
| 17 | +import { corsProxyUrl as defaultCorsProxyUrl } from 'virtual:cors-proxy-url'; |
| 18 | +import type { WorkerBootOptions } from './worker-thread'; |
| 19 | + |
| 20 | +// post message to parent |
| 21 | +self.postMessage('worker-script-started'); |
| 22 | + |
| 23 | +const downloadMonitor = new EmscriptenDownloadMonitor(); |
| 24 | + |
| 25 | +class ArtifactExpiredError extends Error { |
| 26 | + constructor(message = 'GitHub artifact expired') { |
| 27 | + super(message); |
| 28 | + this.name = 'ArtifactExpiredError'; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class PlaygroundWorkerEndpointV1 extends PlaygroundWorkerEndpoint { |
| 33 | + override async boot({ |
| 34 | + scope, |
| 35 | + mounts = [], |
| 36 | + wpVersion = LatestMinifiedWordPressVersion, |
| 37 | + sqliteDriverVersion = LatestSqliteDriverVersion, |
| 38 | + phpVersion, |
| 39 | + sapiName = 'cli', |
| 40 | + withICU = false, |
| 41 | + withNetworking = true, |
| 42 | + shouldInstallWordPress = true, |
| 43 | + corsProxyUrl, |
| 44 | + }: WorkerBootOptions) { |
| 45 | + if (this.booted) { |
| 46 | + throw new Error('Playground already booted'); |
| 47 | + } |
| 48 | + if (corsProxyUrl === undefined) { |
| 49 | + corsProxyUrl = defaultCorsProxyUrl as any; |
| 50 | + } |
| 51 | + this.booted = true; |
| 52 | + this.scope = scope; |
| 53 | + |
| 54 | + try { |
| 55 | + const endpoint = this; |
| 56 | + const knownRemoteAssetPaths = new Set<string>(); |
| 57 | + const siteUrl = this.computeSiteUrl(scope); |
| 58 | + const requestHandler = await this.createRequestHandler({ |
| 59 | + siteUrl, |
| 60 | + sapiName, |
| 61 | + withICU, |
| 62 | + corsProxyUrl, |
| 63 | + knownRemoteAssetPaths, |
| 64 | + withNetworking, |
| 65 | + phpVersion: phpVersion!, |
| 66 | + }); |
| 67 | + |
| 68 | + this.requestedWordPressVersion = wpVersion; |
| 69 | + wpVersion = MinifiedWordPressVersionsList.includes(wpVersion) |
| 70 | + ? wpVersion |
| 71 | + : LatestMinifiedWordPressVersion; |
| 72 | + |
| 73 | + let wordPressRequest: Promise<Response> | null = null; |
| 74 | + if (shouldInstallWordPress) { |
| 75 | + if (this.requestedWordPressVersion!.startsWith('http')) { |
| 76 | + wordPressRequest = this.downloadMonitor |
| 77 | + .monitorFetch( |
| 78 | + fetch(this.requestedWordPressVersion as string) |
| 79 | + ) |
| 80 | + .then((response) => { |
| 81 | + if (response.ok) { |
| 82 | + return response; |
| 83 | + } |
| 84 | + let json: any = null; |
| 85 | + return response.json().then( |
| 86 | + (parsedJson) => { |
| 87 | + json = parsedJson; |
| 88 | + if ( |
| 89 | + json && |
| 90 | + json.error === 'artifact_expired' |
| 91 | + ) { |
| 92 | + throw new ArtifactExpiredError(); |
| 93 | + } |
| 94 | + throw new Error( |
| 95 | + `Failed to download WordPress ZIP (HTTP ${response.status})` |
| 96 | + ); |
| 97 | + }, |
| 98 | + () => { |
| 99 | + throw new Error( |
| 100 | + `Failed to download WordPress ZIP (HTTP ${response.status})` |
| 101 | + ); |
| 102 | + } |
| 103 | + ); |
| 104 | + }); |
| 105 | + } else { |
| 106 | + const wpDetails = getWordPressModuleDetails(wpVersion); |
| 107 | + this.downloadMonitor.expectAssets({ |
| 108 | + [wpDetails.url]: wpDetails.size, |
| 109 | + }); |
| 110 | + wordPressRequest = this.downloadMonitor.monitorFetch( |
| 111 | + fetch(wpDetails.url) |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + let sqliteIntegrationRequest: Promise<Response> | null = null; |
| 117 | + const sqliteDriverModuleDetails = getSqliteDriverModuleDetails( |
| 118 | + sqliteDriverVersion! |
| 119 | + ); |
| 120 | + this.downloadMonitor.expectAssets({ |
| 121 | + [sqliteDriverModuleDetails.url]: sqliteDriverModuleDetails.size, |
| 122 | + }); |
| 123 | + sqliteIntegrationRequest = this.downloadMonitor.monitorFetch( |
| 124 | + fetch(sqliteDriverModuleDetails.url) |
| 125 | + ); |
| 126 | + |
| 127 | + await bootJustWordPress(requestHandler, { |
| 128 | + siteUrl, |
| 129 | + constants: shouldInstallWordPress |
| 130 | + ? { |
| 131 | + WP_DEBUG: true, |
| 132 | + WP_DEBUG_LOG: true, |
| 133 | + WP_DEBUG_DISPLAY: false, |
| 134 | + AUTH_KEY: randomString(40), |
| 135 | + SECURE_AUTH_KEY: randomString(40), |
| 136 | + LOGGED_IN_KEY: randomString(40), |
| 137 | + NONCE_KEY: randomString(40), |
| 138 | + AUTH_SALT: randomString(40), |
| 139 | + SECURE_AUTH_SALT: randomString(40), |
| 140 | + LOGGED_IN_SALT: randomString(40), |
| 141 | + NONCE_SALT: randomString(40), |
| 142 | + } |
| 143 | + : {}, |
| 144 | + wordPressZip: shouldInstallWordPress |
| 145 | + ? wordPressRequest! |
| 146 | + .then((r) => r.blob()) |
| 147 | + .then((b) => new File([b], 'wp.zip')) |
| 148 | + : undefined, |
| 149 | + sqliteIntegrationPluginZip: sqliteIntegrationRequest |
| 150 | + ? sqliteIntegrationRequest |
| 151 | + .then((r) => r.blob()) |
| 152 | + .then((b) => new File([b], 'sqlite.zip')) |
| 153 | + : undefined, |
| 154 | + hooks: { |
| 155 | + async beforeWordPressFiles(php: PHP) { |
| 156 | + for (const mount of mounts) { |
| 157 | + const handle = await directoryHandleFromMountDevice( |
| 158 | + mount.device |
| 159 | + ); |
| 160 | + const unmount = await php.mount( |
| 161 | + mount.mountpoint, |
| 162 | + createDirectoryHandleMountHandler(handle, { |
| 163 | + initialSync: { |
| 164 | + direction: mount.initialSyncDirection, |
| 165 | + }, |
| 166 | + }) |
| 167 | + ); |
| 168 | + endpoint.unmounts[mount.mountpoint] = unmount; |
| 169 | + } |
| 170 | + }, |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + await this.finalizeAfterBoot( |
| 175 | + requestHandler, |
| 176 | + withNetworking, |
| 177 | + knownRemoteAssetPaths |
| 178 | + ); |
| 179 | + setApiReady(); |
| 180 | + } catch (e) { |
| 181 | + setAPIError(e as Error); |
| 182 | + throw e as Error; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +const [setApiReady, setAPIError] = exposeAPI( |
| 188 | + new PlaygroundWorkerEndpointV1(downloadMonitor) |
| 189 | +); |
0 commit comments