Skip to content

Commit

Permalink
Explore: PHP server
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Apr 21, 2024
1 parent 764c55e commit 2e407d0
Show file tree
Hide file tree
Showing 23 changed files with 1,510 additions and 75 deletions.
919 changes: 877 additions & 42 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@
"private": true,
"dependencies": {
"@preact/signals-react": "1.3.6",
"@types/compressible": "2.0.2",
"@types/compression": "1.7.5",
"axios": "1.6.1",
"classnames": "^2.3.2",
"comlink": "^4.4.1",
"compressible": "2.0.18",
"compression": "1.7.4",
"file-saver": "^2.0.5",
"fs-extra": "11.1.1",
"octokit": "3.1.1",
"octokit-plugin-create-pull-request": "5.1.1",
"open": "10.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-modal": "^3.16.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function initOutboundWebsocketProxyServer(

// Handle new WebSocket client
async function onWsConnect(client: any, request: http.IncomingMessage) {
const clientAddr = client._socket.remoteAddress;
const clientAddr = client?._socket?.remoteAddress || client.url;
const clientLog = function (...args: any[]) {
log(' ' + clientAddr + ': ', ...args);
};
Expand Down
38 changes: 7 additions & 31 deletions packages/playground/blueprints/public/blueprint-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -937,37 +937,6 @@
},
"required": ["sql", "step"]
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"progress": {
"type": "object",
"properties": {
"weight": {
"type": "number"
},
"caption": {
"type": "string"
}
},
"additionalProperties": false
},
"step": {
"type": "string",
"const": "setPhpIniEntry"
},
"key": {
"type": "string",
"description": "Entry name e.g. \"display_errors\""
},
"value": {
"type": "string",
"description": "Entry value as a string e.g. \"1\""
}
},
"required": ["key", "step", "value"]
},
{
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -1418,6 +1387,13 @@
},
"description": "Environment variables to set for this run."
},
"$_SERVER": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"description": "$_SERVER entries to set for this run."
},
"code": {
"type": "string",
"description": "The code snippet to eval instead of a php file."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { StepHandler } from '.';
import rewriteWpConfigToDefineConstants from './rewrite-wp-config-to-define-constants.php?raw';
import { UniversalPHP } from '@php-wasm/universal';

const rewriteWpConfigToDefineConstants = await Bun.file(
__dirname + '/rewrite-wp-config-to-define-constants.php'
).text();

/**
* @inheritDoc defineWpConfigConsts
* @hasRunnableExample
Expand Down Expand Up @@ -94,7 +98,7 @@ export async function rewriteDefineCalls(
const js = phpVars({
consts,
});
await playground.run({
const result = await playground.run({

Check failure on line 101 in packages/playground/blueprints/src/lib/steps/define-wp-config-consts.ts

View workflow job for this annotation

GitHub Actions / Lint and typecheck

'result' is assigned a value but never used
code: `${rewriteWpConfigToDefineConstants}
$wp_config_path = '/tmp/code.php';
$wp_config = file_get_contents($wp_config_path);
Expand Down
18 changes: 18 additions & 0 deletions packages/playground/cli/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions packages/playground/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# php-wasm-cli

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build php-wasm-cli` to build the library.

## Running unit tests

Run `nx test php-wasm-cli` to execute the unit tests via [Jest](https://jestjs.io).
13 changes: 13 additions & 0 deletions packages/playground/cli/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable */
export default {
displayName: 'nx-extensions',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{ tsconfig: '<rootDir>/tsconfig.spec.json' },
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/packages/nx-extensions',
};
31 changes: 31 additions & 0 deletions packages/playground/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@wp-playground/cli",
"version": "0.6.16",
"description": "PHP.wasm CLI for node.js",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/wordpress-playground"
},
"homepage": "https://developer.wordpress.org/playground",
"author": "The WordPress contributors",
"contributors": [
{
"name": "Adam Zielinski",
"email": "adam@adamziel.com",
"url": "https://github.com/adamziel"
}
],
"publishConfig": {
"access": "public",
"directory": "../../../dist/packages/wp-playground/cli"
},
"license": "GPL-2.0-or-later",
"type": "module",
"main": "main.js",
"bin": "php-wasm.js",
"gitHead": "2f8d8f3cea548fbd75111e8659a92f601cddc593",
"devDependencies": {
"@types/compressible": "2.0.2",
"@types/compression": "1.7.5"
}
}
84 changes: 84 additions & 0 deletions packages/playground/cli/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"name": "wp-playground-cli",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/wp-playground/cli/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@wp-playground/nx-extensions:package-json",
"options": {
"tsConfig": "packages/wp-playground/cli/tsconfig.lib.json",
"outputPath": "dist/packages/wp-playground/cli",
"buildTarget": "php-wasm-cli:build:bundle:production"
}
},
"build:bundle": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"main": "dist/packages/wp-playground/cli/main.js",
"outputPath": "dist/packages/wp-playground/cli"
},
"defaultConfiguration": "production",
"configurations": {
"development": {
"minify": false
},
"production": {
"minify": true
}
}
},
"dev": {
"executor": "nx:run-commands",
"options": {
"command": "bun --watch ./packages/playground/cli/src/index.ts"
}
},
"start": {
"executor": "@wp-playground/nx-extensions:built-script",
"options": {
"scriptPath": "dist/packages/wp-playground/cli/main.js"
},
"dependsOn": ["build"]
},
"publish": {
"executor": "nx:run-commands",
"options": {
"command": "node tools/scripts/publish.mjs php-wasm-cli {args.ver} {args.tag}"
},
"dependsOn": ["build"]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/wp-playground/cli/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/wp-playground/cli/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
},
"typecheck": {
"executor": "nx:run-commands",
"options": {
"commands": [
"tsc -p packages/wp-playground/cli/tsconfig.lib.json --noEmit",
"tsc -p packages/wp-playground/cli/tsconfig.spec.json --noEmit"
]
}
}
},
"tags": ["scope:php-wasm-public"]
}
2 changes: 2 additions & 0 deletions packages/playground/cli/public/php-wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import './main.js';
3 changes: 3 additions & 0 deletions packages/playground/cli/public/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'Hello, World!';
35 changes: 35 additions & 0 deletions packages/playground/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from 'path';
import yargs from 'yargs';
import { startServer } from './server';

const args = await yargs(process.argv)
.option('php', {
describe: 'PHP version to use.',
type: 'string',
default: 'latest',
})
.option('port', {
describe: 'Port to listen on.',
type: 'number',
default: 9400,
})
.option('mount', {
describe: 'Mount a directory to the PHP runtime.',
type: 'array',
string: true,
}).argv;

const mounts = (args.mount || []).map((mount) => {
const [source, vfsPath] = mount.split(':');
return {
hostPath: path.resolve(process.cwd(), source),
vfsPath,
};
});

console.log('Starting PHP server...');
console.log({ mounts });

startServer({
mounts,
});
19 changes: 19 additions & 0 deletions packages/playground/cli/src/middleware/add-trailing-slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request, Response, NextFunction } from 'express';

/**
* Adds redirection adding a trailing slash, when a request matches a given path.
* @param path - The path to add a trailing slash to. E.g. '/wp-admin'
* @returns - Returns a middleware function that may redirect adding a trailing slash to the given path. E.g. '/wp-admin/'
*/
export function addTrailingSlash(path: string) {
return (req: Request, res: Response, next: NextFunction) => {
const urlParts = req.url.split('?');
const url = urlParts[0];
const queryString = req.url.substr(url.length);
if (url === path) {
res.redirect(301, `${path}/${queryString}`);
} else {
next();
}
};
}
Loading

0 comments on commit 2e407d0

Please sign in to comment.