Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/php-wasm/node/src/test/php.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3053,4 +3053,11 @@ describe('sandboxedSpawnHandlerFactory', () => {
});
expect(response.text).toEqual(expected);
});

it('Should be able to run CLI commands via php.cli()', async () => {
const response = await php.cli(['ls', '/tmp/shared-test-directory']);
expect(await response.stdoutText).toEqual(
['README.md', 'code', ''].join('\n')
);
});
});
87 changes: 85 additions & 2 deletions packages/php-wasm/universal/src/lib/php.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { logger } from '@php-wasm/logger';
import { Semaphore, createSpawnHandler, joinPaths } from '@php-wasm/util';
import {
Semaphore,
basename,
createSpawnHandler,
joinPaths,
} from '@php-wasm/util';
import type { Emscripten } from './emscripten-types';
import type { ListFilesOptions, RmDirOptions } from './fs-helpers';
import { FSHelpers } from './fs-helpers';
Expand All @@ -9,6 +14,7 @@ import { getLoadedRuntime } from './load-php-runtime';
import type { PHPRequestHandler } from './php-request-handler';
import { PHPResponse, StreamedPHPResponse } from './php-response';
import type {
ChildProcess,
MessageListener,
PHPEvent,
PHPEventListener,
Expand Down Expand Up @@ -1446,8 +1452,12 @@ export class PHP implements Disposable {
*/
async cli(
argv: string[],
options: { env?: Record<string, string> } = {}
options: { env?: Record<string, string>; cwd?: string } = {}
): Promise<StreamedPHPResponse> {
if (basename(argv[0] ?? '') !== 'php') {
return this.subProcess(argv, options);
}

if (this.#phpWasmInitCalled) {
this.#rotationOptions.needsRotating = true;
}
Expand Down Expand Up @@ -1483,6 +1493,79 @@ export class PHP implements Disposable {
});
}

/**
* Runs an arbitrary CLI command using the spawn handler associated
* with this PHP instance.
*
* @param argv
* @param options
* @returns StreamedPHPResponse.
*/
private async subProcess(
argv: string[],
options: { env?: Record<string, string>; cwd?: string } = {}
): Promise<StreamedPHPResponse> {
const process = this[__private__dont__use].spawnProcess(
argv[0],
argv.slice(1),
{
env: options.env,
cwd: options.cwd ?? this.cwd(),
}
) as ChildProcess;

const stderrStream = await createInvertedReadableStream<Uint8Array>();
process.on('error', (error) => {
stderrStream.controller.error(error);
});
process.stderr.on('data', (data) => {
stderrStream.controller.enqueue(data);
});

const stdoutStream = await createInvertedReadableStream<Uint8Array>();
process.stdout.on('data', (data) => {
stdoutStream.controller.enqueue(data);
});

process.on('exit', () => {
// Delay until next tick to ensure we don't close the streams before
// emitting the error event on the stderrStream.
setTimeout(() => {
/**
* Ignore any close() errors, e.g. "stream already closed". We just
* need to try to call close() and forget about this subprocess.
*/
try {
stderrStream.controller.close();
} catch {
// Ignore error
}
try {
stdoutStream.controller.close();
} catch {
// Ignore error
}
}, 0);
});

return new StreamedPHPResponse(
// Headers stream
new ReadableStream({
start(controller) {
controller.close();
},
}),
stdoutStream.stream,
stderrStream.stream,
// Exit code
new Promise((resolve) => {
process.on('exit', (code) => {
resolve(code);
});
})
);
}

setSkipShebang(shouldSkip: boolean) {
this[__private__dont__use].ccall(
'wasm_set_skip_shebang',
Expand Down