Skip to content

Commit

Permalink
Experiment with removing the notion of a pakcage:
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Nov 9, 2022
1 parent 596e0c0 commit 4b08258
Show file tree
Hide file tree
Showing 305 changed files with 2,591 additions and 192,216 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
node_modules
package-lock.json
build/*
build-api/*
build-api
build-wp
build-php
build-api
build-scripts
!build/wp-admin
!build/wp-content
!build/wp-includes
!build/php.wasm
!build/php.js
!build/wp.data
!build/wp.js
packages/*/build*
packages/*/build*/*
*.tsbuildinfo
tsdoc-metadata.json

3 changes: 3 additions & 0 deletions api-entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './src/php-wasm';
import './src/php-wasm-browser';
import './src/wordpress-wasm';
4 changes: 2 additions & 2 deletions api-extractor-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
*/
"mainEntryPointFilePath": "<projectFolder>/packages/php-wasm-browser/build-types/index.d.ts",
"mainEntryPointFilePath": "<projectFolder>/build-types/index.d.ts",

/**
* A list of NPM package names whose exports should be treated as part of this package.
Expand Down Expand Up @@ -206,7 +206,7 @@
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
* DEFAULT VALUE: "<projectFolder>/temp/<unscopedPackageName>.api.json"
*/
"apiJsonFilePath": "<projectFolder>/build-api/<unscopedPackageName>.api.json",
"apiJsonFilePath": "<projectFolder>/build-api/distinct/<unscopedPackageName>.api.json",

/**
* Whether "forgotten exports" should be included in the doc model file. Forgotten exports are declarations
Expand Down
44 changes: 44 additions & 0 deletions build-types/php-wasm-browser/emscripten-download-monitor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Monitors the download progress of Emscripten modules
*
* Usage:
* ```js
* const downloadMonitor = new EmscriptenDownloadMonitor();
* const php = await startPHP(
* phpLoaderModule,
* 'web',
* downloadMonitor.phpArgs
* );
* downloadMonitor.addEventListener('progress', (e) => {
* console.log( e.detail.progress);
* })
* ```
*/
export declare class EmscriptenDownloadMonitor extends EventTarget {
#private;
assetsSizes: Record<string, number>;
phpArgs: any;
constructor(assetsSizes: Record<string, number>);
}
export default EmscriptenDownloadMonitor;
export interface DownloadProgressEvent {
/**
* The number of bytes loaded so far.
*/
loaded: number;
/**
* The total number of bytes to load.
*/
total: number;
}
/**
* Clones a fetch Response object and returns a version
* that calls the `onProgress` callback as the progress
* changes.
*
* @param response The fetch Response object to clone.
* @param onProgress The callback to call when the download progress changes.
* @returns The cloned response
*/
export declare function cloneResponseMonitorProgress(response: Response, onProgress: DownloadProgressCallback): Response;
export declare type DownloadProgressCallback = (event: DownloadProgressEvent) => void;
7 changes: 7 additions & 0 deletions build-types/php-wasm-browser/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { setURLScope } from './scope';
export { spawnPHPWorkerThread, SpawnedWorkerThread, } from './worker-thread/window-library';
export { registerServiceWorker } from './service-worker/window-library';
export { postMessageExpectReply, awaitReply, responseTo } from './messaging';
export { cloneResponseMonitorProgress } from './emscripten-download-monitor';
export type { DownloadProgressEvent, DownloadProgressCallback, } from './emscripten-download-monitor';
export { DEFAULT_BASE_URL, getPathQueryFragment } from './utils';
79 changes: 79 additions & 0 deletions build-types/php-wasm-browser/messaging.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Posts a message branded with a unique `requestId` to the given `target`.
* Then returns the `requestId` so it can be used to await a reply.
* Effectively, it implements the request/response dynamics on
* of JavaScript's `postMessage`
*
* @example
*
* In the main app:
*
* ```js
* import { postMessageExpectReply, awaitReply } from 'php-wasm-browser';
* const iframeWindow = iframe.contentWindow;
* const requestId = postMessageExpectReply(iframeWindow, {
* type: "get_php_version"
* });
* const response = await awaitReply(iframeWindow, requestId);
* console.log(response);
* // "8.0.24"
* ```
*
* In the iframe:
*
* ```js
* import { responseTo } from 'php-wasm-browser';
* window.addEventListener('message', (event) => {
* let response = '8.0.24';
* if(event.data.type === 'get_php_version') {
* response = '8.0.24';
* } else {
* throw new Error(`Unexpected message type: ${event.data.type}`);
* }
*
* // When `requestId` is present, the other thread expects a response:
* if (event.data.requestId) {
* const response = responseTo(event.data.requestId, response);
* window.parent.postMessage(response, event.origin);
* }
* });
* ```
*
* @param target An object that has a `postMessage` method.
* @param message A key-value object that can be serialized to JSON.
* @param postMessageArgs Additional arguments to pass to `postMessage`.
* @returns The message ID for awaitReply().
*/
export declare function postMessageExpectReply(target: PostMessageTarget, message: Record<string, any>, ...postMessageArgs: any[]): number;
/**
* Awaits a reply to the message with the given ID.
*
* @see postMessageExpectReply
* @throws {@link Error} If the reply is not received within the timeout.
* @param messageTarget EventEmitter emitting `message` events, e.g. `window`
* or a `Worker` instance.
* @param requestId The message ID returned by postMessageExpectReply().
* @param timeout The number of milliseconds to wait for a reply before
* throwing an error.
* @returns The reply from the messageTarget.
*/
export declare function awaitReply(messageTarget: EventTarget, requestId: number, timeout?: number): Promise<any>;
/**
* Creates a response message to the given message ID.
*
* @see postMessageExpectReply
* @param requestId The message ID sent from the other thread by
* `postMessageExpectReply` in the `message` event.
* @param response The response to send back to the messageTarget.
* @returns A message object that can be sent back to the other thread.
*/
export declare function responseTo<T>(requestId: number, response: T): MessageResponse<T>;
export interface MessageResponse<T> {
type: 'response';
requestId: number;
response: T;
}
interface PostMessageTarget {
postMessage(message: any, ...args: any[]): void;
}
export {};
81 changes: 81 additions & 0 deletions build-types/php-wasm-browser/scope.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Scopes are unique strings, like `96253`, used to uniquely brand
* the outgoing HTTP traffic from each browser tab. This helps the
* main thread distinguish between the relevant and irrelevant BroadcastChannel
* messages from the Service Worker.
*
* Scopes are included in the `PHPServer.absoluteUrl` as follows:
*
* An **unscoped** URL: http://localhost:8778/wp-login.php
* A **scoped** URL: http://localhost:8778/scope:96253/wp-login.php
*
* For more information, see the README section on scopes.
*/
/**
* Checks if the given URL contains scope information.
*
* @example
* ```js
* isURLScoped(new URL('http://localhost/scope:96253/index.php'));
* // true
*
* isURLScoped(new URL('http://localhost/index.php'));
* // false
* ```
*
* @param url The URL to check.
* @returns `true` if the URL contains scope information, `false` otherwise.
*/
export declare function isURLScoped(url: URL): boolean;
/**
* Returns the scope stored in the given URL.
*
* @example
* ```js
* getScopeFromURL(new URL('http://localhost/scope:96253/index.php'));
* // '96253'
*
* getScopeFromURL(new URL('http://localhost/index.php'));
* // null
* ```
*
* @param url The URL.
* @returns The scope if the URL contains a scope, `null` otherwise.
*/
export declare function getURLScope(url: URL): string | null;
/**
* Returns a new URL with the requested scope information.
*
* @example
* ```js
* setURLScope(new URL('http://localhost/index.php'), '96253');
* // URL('http://localhost/scope:96253/index.php')
*
* setURLScope(new URL('http://localhost/scope:96253/index.php'), '12345');
* // URL('http://localhost/scope:12345/index.php')
*
* setURLScope(new URL('http://localhost/index.php'), null);
* // URL('http://localhost/index.php')
* ```
*
* @param url The URL to scope.
* @param scope The scope value.
* @returns A new URL with the scope information in it.
*/
export declare function setURLScope(url: URL | string, scope: string): URL;
/**
* Returns a new URL without any scope information.
*
* @example
* ```js
* removeURLScope(new URL('http://localhost/scope:96253/index.php'));
* // URL('http://localhost/index.php')
*
* removeURLScope(new URL('http://localhost/index.php'));
* // URL('http://localhost/index.php')
* ```
*
* @param url The URL to remove scope information from.
* @returns A new URL without the scope information.
*/
export declare function removeURLScope(url: URL): URL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Run this in the main application to register the service worker.
*
* @param {string} scriptUrl The URL of the service worker script.
*/
export declare function registerServiceWorker(scriptUrl: any): Promise<void>;
32 changes: 32 additions & 0 deletions build-types/php-wasm-browser/service-worker/worker-library.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />
/**
* Run this function in the service worker to install the required event
* handlers.
*
* @param config
*/
export declare function initializeServiceWorker(config: ServiceWorkerConfiguration): void;
interface ServiceWorkerConfiguration {
broadcastChannel?: BroadcastChannel;
shouldForwardRequestToPHPServer?: (request: Request, unscopedUrl: URL) => boolean;
}
/**
* Guesses whether the given path looks like a PHP file.
*
* @example
* ```js
* seemsLikeAPHPServerPath('/index.php') // true
* seemsLikeAPHPServerPath('/index.php') // true
* seemsLikeAPHPServerPath('/index.php/foo/bar') // true
* seemsLikeAPHPServerPath('/index.html') // false
* seemsLikeAPHPServerPath('/index.html/foo/bar') // false
* seemsLikeAPHPServerPath('/') // true
* ```
*
* @param path The path to check.
* @returns Whether the path seems like a PHP server path.
*/
export declare function seemsLikeAPHPServerPath(path: string): boolean;
export {};
18 changes: 18 additions & 0 deletions build-types/php-wasm-browser/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* The default base used to convert a path into the URL object.
*/
export declare const DEFAULT_BASE_URL = "http://example.com";
/**
* Returns a string representing the path, query, and
* fragment of the given URL.
*
* @example
* ```js
* const url = new URL('http://example.com/foo/bar?baz=qux#quux');
* getPathQueryFragment(url); // '/foo/bar?baz=qux#quux'
* ```
*
* @param url The URL.
* @returns The path, query, and fragment.
*/
export declare function getPathQueryFragment(url: URL): string;
53 changes: 53 additions & 0 deletions build-types/php-wasm-browser/worker-thread/window-library.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { PHPOutput, PHPRequest, PHPResponse } from '../../php-wasm';
import type { DownloadProgressEvent } from '../emscripten-download-monitor';
interface WorkerThreadConfig {
/**
* A function to call when a download progress event is received from the worker
*/
onDownloadProgress?: (event: DownloadProgressEvent) => void;
}
/**
* Spawns a new Worker Thread.
*
* @param backendName The Worker Thread backend to use. Either 'webworker' or 'iframe'.
* @param workerScriptUrl The absolute URL of the worker script.
* @param config
* @returns The spawned Worker Thread.
*/
export declare function spawnPHPWorkerThread(backendName: string, workerScriptUrl: string, config: WorkerThreadConfig): Promise<SpawnedWorkerThread>;
export declare class SpawnedWorkerThread {
messageChannel: any;
serverUrl: any;
constructor(messageChannel: any, serverUrl: any);
/**
* Converts a path to an absolute URL based at the PHPServer
* root.
*
* @param path The server path to convert to an absolute URL.
* @returns The absolute URL.
*/
pathToInternalUrl(path: string): string;
/**
* Converts an absolute URL based at the PHPServer to a relative path
* without the server pathname and scope.
*
* @param internalUrl An absolute URL based at the PHPServer root.
* @returns The relative path.
*/
internalUrlToPath(internalUrl: string): string;
/**
* Runs PHP code.
*
* @param code The PHP code to run.
* @returns The result of the PHP code.
*/
eval(code: string): Promise<PHPOutput>;
/**
* Dispatches a request to the PHPServer.
*
* @param request - The request to dispatch.
* @returns The response from the PHPServer.
*/
HTTPRequest(request: PHPRequest): Promise<PHPResponse>;
}
export {};
Loading

0 comments on commit 4b08258

Please sign in to comment.