|
| 1 | +import { createSpawnHandler } from '@php-wasm/util'; |
| 2 | +import type { PHPProcessManager } from './php-process-manager'; |
| 3 | + |
| 4 | +/** |
| 5 | + * An isomorphic proc_open() handler that implements typical shell in TypeScript |
| 6 | + * without relying on a server runtime. It can be used in the browser and Node.js |
| 7 | + * alike whenever you need to spawn a PHP subprocess, query the terminal size, etc. |
| 8 | + * It is open for future expansion if more shell or busybox calls are needed, but |
| 9 | + * advanced shell features such as piping, stream redirection etc. are outside of |
| 10 | + * the scope of this minimal handler. If they become vital at any point, let's |
| 11 | + * explore bringing in an actual shell implementation or at least a proper command |
| 12 | + * parser. |
| 13 | + */ |
| 14 | +export function sandboxedSpawnHandlerFactory( |
| 15 | + processManager: PHPProcessManager |
| 16 | +) { |
| 17 | + return createSpawnHandler(async function (args, processApi, options) { |
| 18 | + processApi.notifySpawn(); |
| 19 | + if (args[0] === 'exec') { |
| 20 | + args.shift(); |
| 21 | + } |
| 22 | + |
| 23 | + if (args[0].endsWith('.php') || args[0].endsWith('.phar')) { |
| 24 | + args.unshift('php'); |
| 25 | + } |
| 26 | + |
| 27 | + const binaryName = args[0].split('/').pop(); |
| 28 | + |
| 29 | + // Mock programs required by wp-cli: |
| 30 | + if ( |
| 31 | + args[0] === '/usr/bin/env' && |
| 32 | + args[1] === 'stty' && |
| 33 | + args[2] === 'size' |
| 34 | + ) { |
| 35 | + // These numbers are hardcoded because this |
| 36 | + // spawnHandler is transmitted as a string to |
| 37 | + // the PHP backend and has no access to local |
| 38 | + // scope. It would be nice to find a way to |
| 39 | + // transfer / proxy a live object instead. |
| 40 | + // @TODO: Do not hardcode this |
| 41 | + processApi.stdout(`18 140`); |
| 42 | + processApi.exit(0); |
| 43 | + } else if (binaryName === 'tput' && args[1] === 'cols') { |
| 44 | + processApi.stdout(`140`); |
| 45 | + processApi.exit(0); |
| 46 | + } else if (binaryName === 'less') { |
| 47 | + processApi.on('stdin', (data: Uint8Array) => { |
| 48 | + processApi.stdout(data.buffer as ArrayBuffer); |
| 49 | + }); |
| 50 | + processApi.exit(0); |
| 51 | + } else if (binaryName === 'php') { |
| 52 | + const { php, reap } = await processManager.acquirePHPInstance({ |
| 53 | + considerPrimary: false, |
| 54 | + }); |
| 55 | + |
| 56 | + php.chdir(options.cwd as string); |
| 57 | + try { |
| 58 | + // Figure out more about setting env, putenv(), etc. |
| 59 | + const result = await php.cli(args, { |
| 60 | + env: { |
| 61 | + ...options.env, |
| 62 | + SCRIPT_PATH: args[1], |
| 63 | + // Set SHELL_PIPE to 0 to ensure WP-CLI formats |
| 64 | + // the output as ASCII tables. |
| 65 | + // @see https://github.com/wp-cli/wp-cli/issues/1102 |
| 66 | + SHELL_PIPE: '0', |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + result.stdout.pipeTo( |
| 71 | + new WritableStream({ |
| 72 | + write(chunk) { |
| 73 | + processApi.stdout(chunk.buffer as ArrayBuffer); |
| 74 | + }, |
| 75 | + }) |
| 76 | + ); |
| 77 | + result.stderr.pipeTo( |
| 78 | + new WritableStream({ |
| 79 | + write(chunk) { |
| 80 | + processApi.stderr(chunk.buffer as ArrayBuffer); |
| 81 | + }, |
| 82 | + }) |
| 83 | + ); |
| 84 | + processApi.exit(await result.exitCode); |
| 85 | + } catch (e) { |
| 86 | + // An exception here means the PHP runtime has crashed. |
| 87 | + processApi.exit(1); |
| 88 | + throw e; |
| 89 | + } finally { |
| 90 | + reap(); |
| 91 | + } |
| 92 | + } else { |
| 93 | + processApi.exit(1); |
| 94 | + } |
| 95 | + }); |
| 96 | +} |
0 commit comments