@wp-playground/cli 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker-thread-BQ8BezvJ.js","sources":["packages/php-wasm/util/src/lib/sprintf.ts","packages/playground/cli/src/worker-thread.ts"],"sourcesContent":["/**\n * Formats a string like sprintf().\n *\n * This function:\n * - Supports basic format specifiers: %s, %d, %f, %x, %%\n * - Supports bigint values\n *\n * The purpose of this function is for use in optional php-wasm tracing.\n * If we use printf-style formatting for trace messages, we let the trace\n * function decide whether to format and do not have to pay for formatting\n * unless tracing is enabled.\n */\nexport function sprintf(format: string, ...args: any[]): string {\n\tlet result = '';\n\tlet argIndex = 0;\n\n\tfor (let i = 0; i < format.length; i++) {\n\t\tif (format[i] === '%' && i + 1 < format.length) {\n\t\t\ti++;\n\t\t\tconst specifier = format[i];\n\n\t\t\tswitch (specifier) {\n\t\t\t\tcase 's': {\n\t\t\t\t\tconst arg = args[argIndex++];\n\t\t\t\t\tlet str;\n\t\t\t\t\tif (typeof arg === 'object') {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// If an object doesn't provide its own toString(),\n\t\t\t\t\t\t\t// try to represent it as JSON.\n\t\t\t\t\t\t\tstr = JSON.stringify(\n\t\t\t\t\t\t\t\targ,\n\t\t\t\t\t\t\t\t// Represent bigint values as strings in JSON.stringify().\n\t\t\t\t\t\t\t\t(key, value) => {\n\t\t\t\t\t\t\t\t\tif (typeof value === 'bigint') {\n\t\t\t\t\t\t\t\t\t\treturn `0x${value.toString(16)}`;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\treturn value;\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t2\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// Ignore error and use default representation.\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstr = String(arg);\n\t\t\t\t\t}\n\n\t\t\t\t\tresult += str;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase 'd': {\n\t\t\t\t\tconst arg = args[argIndex++];\n\t\t\t\t\tif (typeof arg === 'bigint') {\n\t\t\t\t\t\tresult += arg.toString();\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult += Math.floor(Number(arg));\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase 'f': {\n\t\t\t\t\tconst arg = args[argIndex++];\n\t\t\t\t\tif (typeof arg === 'bigint') {\n\t\t\t\t\t\tresult += Number(arg);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult += Number(arg);\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase 'x': {\n\t\t\t\t\tconst arg = args[argIndex++];\n\t\t\t\t\tif (typeof arg === 'bigint') {\n\t\t\t\t\t\tresult += arg.toString(16);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult += Math.floor(Number(arg)).toString(16);\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase '%': {\n\t\t\t\t\tresult += '%';\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tresult += '%' + specifier;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tresult += format[i];\n\t\t}\n\t}\n\n\treturn result;\n}\n","import type { PHP, SupportedPHPVersion } from '@php-wasm/universal';\nimport { PHPWorker, consumeAPI, exposeAPI } from '@php-wasm/universal';\nimport type { FileLockManager } from '@php-wasm/node';\nimport { createNodeFsMountHandler, loadNodeRuntime } from '@php-wasm/node';\nimport { EmscriptenDownloadMonitor } from '@php-wasm/progress';\nimport { bootWordPress } from '@wp-playground/wordpress';\nimport { sprintf } from '@php-wasm/util';\nimport { parentPort } from 'worker_threads';\nimport { rootCertificates } from 'tls';\n\nexport interface Mount {\n\thostPath: string;\n\tvfsPath: string;\n}\n\nexport type PrimaryWorkerBootOptions = {\n\twpVersion?: string;\n\tphpVersion?: SupportedPHPVersion;\n\tabsoluteUrl: string;\n\tmountsBeforeWpInstall: Array<Mount>;\n\tmountsAfterWpInstall: Array<Mount>;\n\twordPressZip?: ArrayBuffer;\n\tsqliteIntegrationPluginZip?: ArrayBuffer;\n\tfirstProcessId: number;\n\tprocessIdSpaceLength: number;\n\tdataSqlPath?: string;\n\tfollowSymlinks: boolean;\n\ttrace: boolean;\n};\n\nfunction mountResources(php: PHP, mounts: Mount[]) {\n\tfor (const mount of mounts) {\n\t\tphp.mkdir(mount.vfsPath);\n\t\tphp.mount(mount.vfsPath, createNodeFsMountHandler(mount.hostPath));\n\t}\n}\n\n/**\n * Print trace messages from PHP-WASM.\n *\n * @param {number} processId - The process ID.\n * @param {string} format - The format string.\n * @param {...any} args - The arguments.\n */\nfunction tracePhpWasm(processId: number, format: string, ...args: any[]) {\n\t// eslint-disable-next-line no-console\n\tconsole.log(\n\t\tperformance.now().toFixed(6).padStart(15, '0'),\n\t\tprocessId.toString().padStart(16, '0'),\n\t\tsprintf(format, ...args)\n\t);\n}\n\nexport class PlaygroundCliWorker extends PHPWorker {\n\tbooted = false;\n\n\tconstructor(monitor: EmscriptenDownloadMonitor) {\n\t\tsuper(undefined, monitor);\n\t}\n\n\tasync boot({\n\t\tabsoluteUrl,\n\t\tmountsBeforeWpInstall,\n\t\tmountsAfterWpInstall,\n\t\tphpVersion = '8.0',\n\t\twordPressZip,\n\t\tsqliteIntegrationPluginZip,\n\t\tfirstProcessId,\n\t\tprocessIdSpaceLength,\n\t\tdataSqlPath,\n\t\tfollowSymlinks,\n\t\ttrace,\n\t}: PrimaryWorkerBootOptions) {\n\t\tif (this.booted) {\n\t\t\tthrow new Error('Playground already booted');\n\t\t}\n\t\tthis.booted = true;\n\n\t\tlet nextProcessId = firstProcessId;\n\t\tconst lastProcessId = firstProcessId + processIdSpaceLength - 1;\n\t\tconst fileLockManager = consumeAPI<FileLockManager>(parentPort!);\n\t\tawait fileLockManager.isConnected();\n\n\t\ttry {\n\t\t\tconst constants: Record<string, string | number | boolean | null> =\n\t\t\t\t{\n\t\t\t\t\tWP_DEBUG: true,\n\t\t\t\t\tWP_DEBUG_LOG: true,\n\t\t\t\t\tWP_DEBUG_DISPLAY: false,\n\t\t\t\t};\n\n\t\t\tconst requestHandler = await bootWordPress({\n\t\t\t\tsiteUrl: absoluteUrl,\n\t\t\t\tcreatePhpRuntime: async () => {\n\t\t\t\t\tconst processId = nextProcessId;\n\n\t\t\t\t\tif (nextProcessId < lastProcessId) {\n\t\t\t\t\t\tnextProcessId++;\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// We've reached the end of the process ID space. Start over.\n\t\t\t\t\t\tnextProcessId = firstProcessId;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn await loadNodeRuntime(phpVersion, {\n\t\t\t\t\t\temscriptenOptions: {\n\t\t\t\t\t\t\tfileLockManager,\n\t\t\t\t\t\t\tprocessId,\n\t\t\t\t\t\t\ttrace: trace ? tracePhpWasm : undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfollowSymlinks,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\twordPressZip:\n\t\t\t\t\twordPressZip !== undefined\n\t\t\t\t\t\t? new File([wordPressZip], 'wordpress.zip')\n\t\t\t\t\t\t: undefined,\n\t\t\t\tsqliteIntegrationPluginZip:\n\t\t\t\t\tsqliteIntegrationPluginZip !== undefined\n\t\t\t\t\t\t? new File(\n\t\t\t\t\t\t\t\t[sqliteIntegrationPluginZip],\n\t\t\t\t\t\t\t\t'sqlite-integration-plugin.zip'\n\t\t\t\t\t\t )\n\t\t\t\t\t\t: undefined,\n\t\t\t\tsapiName: 'cli',\n\t\t\t\tcreateFiles: {\n\t\t\t\t\t'/internal/shared/ca-bundle.crt':\n\t\t\t\t\t\trootCertificates.join('\\n'),\n\t\t\t\t},\n\t\t\t\tconstants,\n\t\t\t\tphpIniEntries: {\n\t\t\t\t\t'openssl.cafile': '/internal/shared/ca-bundle.crt',\n\t\t\t\t\tallow_url_fopen: '1',\n\t\t\t\t\tdisable_functions: '',\n\t\t\t\t},\n\t\t\t\thooks: {\n\t\t\t\t\tasync beforeWordPressFiles(php) {\n\t\t\t\t\t\tmountResources(php, mountsBeforeWpInstall);\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tcookieStore: false,\n\t\t\t\tdataSqlPath,\n\t\t\t});\n\t\t\tthis.__internal_setRequestHandler(requestHandler);\n\n\t\t\tconst primaryPhp = await requestHandler.getPrimaryPhp();\n\t\t\tawait this.setPrimaryPHP(primaryPhp);\n\n\t\t\tmountResources(primaryPhp, mountsAfterWpInstall);\n\n\t\t\tsetApiReady();\n\t\t} catch (e) {\n\t\t\tsetAPIError(e as Error);\n\t\t\tthrow e;\n\t\t}\n\t}\n\n\t// Provide a named disposal method that can be invoked via comlink.\n\tasync dispose() {\n\t\tawait this[Symbol.asyncDispose]();\n\t}\n}\n\nconst [setApiReady, setAPIError] = exposeAPI(\n\tnew PlaygroundCliWorker(new EmscriptenDownloadMonitor()),\n\tundefined,\n\tparentPort!\n);\n\n// Confirm that the worker script has initialized.\nparentPort!.postMessage('worker-script-initialized');\n"],"names":["sprintf","format","args","result","argIndex","specifier","arg","str","key","value","mountResources","php","mounts","mount","createNodeFsMountHandler","tracePhpWasm","processId","PlaygroundCliWorker","PHPWorker","monitor","absoluteUrl","mountsBeforeWpInstall","mountsAfterWpInstall","phpVersion","wordPressZip","sqliteIntegrationPluginZip","firstProcessId","processIdSpaceLength","dataSqlPath","followSymlinks","trace","nextProcessId","lastProcessId","fileLockManager","consumeAPI","parentPort","constants","requestHandler","bootWordPress","loadNodeRuntime","rootCertificates","primaryPhp","setApiReady","e","setAPIError","exposeAPI","EmscriptenDownloadMonitor"],"mappings":";;;;;;AAYgB,SAAAA,EAAQC,MAAmBC,GAAqB;AAC/D,MAAIC,IAAS,IACT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