Skip to content

Isomorphic PHP, Isomorphic Blueprints #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
79c2bc7
UniversalPHP
adamziel Apr 22, 2023
263aade
Make PHP Isomorphic and universal with respect to Local / Remote runtime
adamziel Apr 23, 2023
7730c98
Extract blueprints to a separate package to decouple them from Playgr…
adamziel Apr 23, 2023
4e27f3b
Add defineSiteUrl and runWpInstallationWizard helpers
adamziel Apr 23, 2023
5b96ff7
Move WordPress patcher to blueprints
adamziel Apr 23, 2023
0a57903
Add playground/node package to run Blueprints in node.js
adamziel Apr 23, 2023
2b11f8c
Settle the API shape
adamziel Apr 24, 2023
1685f64
Build blueprints and php-wasm/universal as both ESM and CJS
adamziel Apr 24, 2023
643bb29
Rename files
adamziel Apr 24, 2023
f31a4fe
Revert unnecessary function rename in php-wasm-node
adamziel Apr 24, 2023
4435b92
Rename test.sh to ensure-both-outputs-run-in-node.sh
adamziel Apr 24, 2023
10340e5
Use mkdirSync with recursive: true instead of checking whether fileEx…
adamziel Apr 24, 2023
e734f30
Revert changes from packages/php-wasm/node/src/lib/networking/outboun…
adamziel Apr 24, 2023
a819f19
Expand STR and NUM to STRING and NUMBER
adamziel Apr 24, 2023
373c233
Remove WithProgress interface
adamziel Apr 24, 2023
94dd955
Adjust doc generation conf
adamziel Apr 24, 2023
9175526
Remove parseWorkerStartupOptions from php-wasm/node
adamziel Apr 24, 2023
e44bef0
Spawn node directly in assert-built-esm-and-cjs executor
adamziel Apr 24, 2023
5594e8e
Correct typos in loadPHPRuntime()
adamziel Apr 24, 2023
a377bb0
Rename patchFile to updateFile
adamziel Apr 24, 2023
68bbbb0
Prevent defining WP_HOME and WP_SITEURL contants twice
adamziel Apr 24, 2023
ad7ffce
Relocate step types to the handler files
adamziel Apr 24, 2023
43d02ca
Document admin_password2
adamziel Apr 24, 2023
364eab9
Export step types
adamziel Apr 24, 2023
7e1c652
Export SemaphoreOptions as a type
adamziel Apr 24, 2023
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: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": []
"depConstraints": [
{
"sourceTag": "*",
"notDependOnLibsWithTags": ["scope:web-client"]
}
]
}
],
"no-inner-declarations": 0,
Expand Down
5 changes: 5 additions & 0 deletions packages/nx-extensions/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"implementation": "./src/executors/package-json/executor",
"schema": "./src/executors/package-json/schema.json",
"description": "package-json executor"
},
"assert-built-esm-and-cjs": {
"implementation": "./src/executors/assert-built-esm-and-cjs/executor",
"schema": "./src/executors/assert-built-esm-and-cjs/schema.json",
"description": "assert-built-esm-and-cjs executor"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ExecutorContext } from '@nrwl/devkit';
import { spawnSync } from 'child_process';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import * as path from 'path';
import { AssertBuiltEsmAndCjsExecutorSchema } from './schema';

/**
* Test whether a module can be imported as both ESM and CJS.
*
* @param options
* @param context
* @returns
*/
export default async function runExecutor(
options: AssertBuiltEsmAndCjsExecutorSchema,
context: ExecutorContext
) {
const buildDir = options.outputPath.split('/')[0];
const testsPath = path.join(context.root, buildDir, 'test-esm-cjs');
if (!existsSync(testsPath)) {
mkdirSync(testsPath);
}

writeFileSync(
path.join(testsPath, 'test-esm.mjs'),
`import * as result from '../../${options.outputPath}/index.js';`
);
writeFileSync(
path.join(testsPath, 'test-cjs.cjs'),
`require('../../${options.outputPath}');`
);
writeFileSync(
path.join(testsPath, 'test.sh'),
`#!/bin/sh
set -e
node test-esm.mjs;
node test-cjs.cjs;
echo Success;`
);

// Run test.sh and check exit code:
const test = spawnSync('sh', ['test.sh'], {
cwd: testsPath,
stdio: 'pipe',
encoding: 'utf-8',
});
// Trim spaces and newlines
const stdout = test.output.toString();
const success =
test.status === 0 && stdout.replace(/[^A-Za-z]/g, '') === 'Success';
if (!success) {
throw `${context.targetName} could not be imported as both ESM and CJS: ${stdout}`;
}
return {
success,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AssertBuiltEsmAndCjsExecutorSchema {
outputPath: string;
} // eslint-disable-line
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/schema",
"version": 2,
"cli": "nx",
"title": "AssertBuiltEsmAndCjs executor",
"description": "",
"type": "object",
"properties": {
"outputPath": {
"type": "string",
"description": "The path to the built module"
}
},
"required": ["outputPath"]
}
12 changes: 0 additions & 12 deletions packages/php-wasm/abstract/README.md

This file was deleted.

39 changes: 0 additions & 39 deletions packages/php-wasm/abstract/project.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/php-wasm/cli/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"options": {
"scriptPath": "dist/packages/php-wasm/cli/main.js"
},
"dependsOn": ["build", "^build-for-cli-run"]
"dependsOn": ["build"]
},
"publish": {
"executor": "nx:run-commands",
Expand Down
7 changes: 4 additions & 3 deletions packages/php-wasm/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { rootCertificates } from 'tls';

import {
LatestSupportedPHPVersion,
PHP,
SupportedPHPVersion,
SupportedPHPVersionsList,
} from '@php-wasm/node';
} from '@php-wasm/universal';

import { NodePHP } from '@php-wasm/node';

let args = process.argv.slice(2);
if (!args.length) {
Expand All @@ -31,7 +32,7 @@ if (!SupportedPHPVersionsList.includes(phpVersion)) {
throw new Error(`Unsupported PHP version ${phpVersion}`);
}

const php = await PHP.load(phpVersion, {
const php = await NodePHP.load(phpVersion, {
emscriptenOptions: {
ENV: {
...process.env,
Expand Down
10 changes: 1 addition & 9 deletions packages/php-wasm/node/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": ["@php-wasm/abstract"]
}
]
}
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
Expand Down
36 changes: 0 additions & 36 deletions packages/php-wasm/node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder as any;
global.TextDecoder = TextDecoder as any;

export * from './lib';

export {
LatestSupportedPHPVersion,
PHPBrowser,
SupportedPHPVersions,
SupportedPHPVersionsList,
} from '@php-wasm/abstract';

// Wildcard re-export of type is unfortunately unsupported by TypeScript.
export type {
DataModule,
EmscriptenOptions,
ErrnoError,
FileInfo,
HTTPMethod,
MountSettings,
PHPLoaderModule,
PHPOutput,
PHPRequest,
PHPRequestHandler,
PHPRequestHeaders,
PHPResponse,
PHPRunOptions,
PHPRuntime,
PHPRuntimeId,
PHPRequestHandlerConfiguration,
RuntimeType,
SupportedPHPVersion,
WithFilesystem,
WithPHPIniBindings,
WorkerStartupOptions,
} from '@php-wasm/abstract';
4 changes: 2 additions & 2 deletions packages/php-wasm/node/src/lib/get-php-loader-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LatestSupportedPHPVersion } from '@php-wasm/abstract';
import type { PHPLoaderModule, SupportedPHPVersion } from '@php-wasm/abstract';
import { LatestSupportedPHPVersion } from '@php-wasm/universal';
import type { PHPLoaderModule, SupportedPHPVersion } from '@php-wasm/universal';

export async function getPHPLoaderModule(
version: SupportedPHPVersion = LatestSupportedPHPVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,23 @@ function prependByte(
return chunk;
}

/**
* Send a chunk of data to the remote server.
*/
export const COMMAND_CHUNK = 0x01;
/**
* Set a TCP socket option.
*/
export const COMMAND_SET_SOCKETOPT = 0x02;

/**
* Adds support for TCP socket options to WebSocket class.
* Add socket options support to a WebSocket class. This function takes a WebSocket class and
* extends it with new methods for setting socket options. The resulting class has an additional
* 'setSocketOpt' method for setting socket options, and an overridden 'send' method that sends
* data with the COMMAND_CHUNK command type.
*
* Socket options are implemented by adopting a specific data transmission
* protocol between WS client and WS server The first byte
* of every message is a command type, and the remaining bytes
* are the actual data.
* @example
* const WebSocketWithOptions = addSocketOptionsSupportToWebSocketClass(WebSocket);
*
* @param WebSocketConstructor
* @returns Decorated constructor
* @param WebSocketConstructor - The WebSocket class to extend.
* @returns - A new WebSocket class with additional methods for setting socket options and sending
* data with the COMMAND_CHUNK command type.
*/
export function addSocketOptionsSupportToWebSocketClass(
export function withSocketOptionsSupport(
WebSocketConstructor: typeof WebSocket
) {
return class PHPWasmWebSocketConstructor extends WebSocketConstructor {
Expand Down
6 changes: 3 additions & 3 deletions packages/php-wasm/node/src/lib/networking/with-networking.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EmscriptenOptions } from '@php-wasm/abstract';
import { EmscriptenOptions } from '@php-wasm/universal';
import {
initOutboundWebsocketProxyServer,
addSocketOptionsSupportToWebSocketClass,
withSocketOptionsSupport,
} from './outbound-ws-to-tcp-proxy.js';
import { addTCPServerToWebSocketServerClass } from './inbound-tcp-to-ws-proxy.js';
import { findFreePorts } from './utils.js';
Expand All @@ -26,7 +26,7 @@ export async function withNetworking(
return `ws://127.0.0.1:${outboundProxyWsServerPort}/?${query}`;
},
subprotocol: 'binary',
decorator: addSocketOptionsSupportToWebSocketClass,
decorator: withSocketOptionsSupport,
serverDecorator: addTCPServerToWebSocketServerClass.bind(
null,
inboundProxyWsServerPort
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { WorkerStartupOptions } from '@php-wasm/abstract';

export function parseWorkerStartupOptions(): WorkerStartupOptions {
export function parseWorkerStartupOptions(): Record<string, string> {
return JSON.parse(process.env['WORKER_OPTIONS'] || '{}');
}
Loading