From ececcd3e6da10f50d58d782aa6f706ed1466e249 Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Thu, 8 Feb 2024 21:55:27 +0000 Subject: [PATCH] Remove `crypto.randomUUID` dependency in favor of a custom function (#1016) - Remove the crypto polyfill introduced on https://github.com/WordPress/wordpress-playground/pull/1000 - Move the `randomString` from `remote` package to `@php-wasm/util` - Created a new custom function `randomFilename` - Replace the usage of `crypto.randomUUID` with `randomFilename()` ## What problem is it solving? `crypto` is a library that is available, but needs to be imported on node apps. ## How is the problem addressed? It replaces the function that generates a random value with our custom library ## Testing Instructions - CI tests pass --- packages/php-wasm/node-polyfills/src/index.ts | 1 - .../php-wasm/node-polyfills/src/lib/crypto.spec.ts | 10 ---------- packages/php-wasm/node-polyfills/src/lib/crypto.ts | 5 ----- packages/php-wasm/node/vite.config.ts | 1 - packages/php-wasm/util/src/lib/index.ts | 2 ++ packages/php-wasm/util/src/lib/random-filename.ts | 5 +++++ .../util/src/lib/random-string.ts} | 8 ++++++-- .../blueprints/src/lib/steps/install-asset.ts | 4 ++-- .../blueprints/src/lib/steps/run-sql.spec.ts | 6 +++--- .../playground/blueprints/src/lib/steps/run-sql.ts | 4 ++-- packages/playground/remote/src/lib/worker-thread.ts | 3 +-- 11 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts delete mode 100644 packages/php-wasm/node-polyfills/src/lib/crypto.ts create mode 100644 packages/php-wasm/util/src/lib/random-filename.ts rename packages/{playground/remote/src/lib/utils.ts => php-wasm/util/src/lib/random-string.ts} (65%) diff --git a/packages/php-wasm/node-polyfills/src/index.ts b/packages/php-wasm/node-polyfills/src/index.ts index 76fe666d7b..2432ff8272 100644 --- a/packages/php-wasm/node-polyfills/src/index.ts +++ b/packages/php-wasm/node-polyfills/src/index.ts @@ -1,3 +1,2 @@ import './lib/blob'; import './lib/custom-event'; -import './lib/crypto'; diff --git a/packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts b/packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts deleted file mode 100644 index c967c53e65..0000000000 --- a/packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts +++ /dev/null @@ -1,10 +0,0 @@ -import './crypto'; - -describe('crypto is loaded', () => { - it('Should exist', () => { - expect(crypto).not.toBe(undefined); - }); - it('Returns a random', () => { - expect(crypto.randomUUID().length).toBe(36); - }); -}); diff --git a/packages/php-wasm/node-polyfills/src/lib/crypto.ts b/packages/php-wasm/node-polyfills/src/lib/crypto.ts deleted file mode 100644 index 9f5ac5101f..0000000000 --- a/packages/php-wasm/node-polyfills/src/lib/crypto.ts +++ /dev/null @@ -1,5 +0,0 @@ -if (typeof crypto === 'undefined') { - import('crypto').then((module) => { - global.crypto = module as Crypto; - }); -} diff --git a/packages/php-wasm/node/vite.config.ts b/packages/php-wasm/node/vite.config.ts index e3edf3cfe1..8079730142 100644 --- a/packages/php-wasm/node/vite.config.ts +++ b/packages/php-wasm/node/vite.config.ts @@ -47,7 +47,6 @@ export default defineConfig(() => { 'util', 'dns', 'ws', - 'crypto', ], output: { entryFileNames: '[name].js', diff --git a/packages/php-wasm/util/src/lib/index.ts b/packages/php-wasm/util/src/lib/index.ts index fd7c47acd2..c322dc7f63 100644 --- a/packages/php-wasm/util/src/lib/index.ts +++ b/packages/php-wasm/util/src/lib/index.ts @@ -3,5 +3,7 @@ export { Semaphore }; export type { SemaphoreOptions } from './semaphore'; export { dirname, joinPaths, basename, normalizePath } from './paths'; export { createSpawnHandler } from './create-spawn-handler'; +export { randomString } from './random-string'; +export { randomFilename } from './random-filename'; export * from './php-vars'; diff --git a/packages/php-wasm/util/src/lib/random-filename.ts b/packages/php-wasm/util/src/lib/random-filename.ts new file mode 100644 index 0000000000..f60c19fc03 --- /dev/null +++ b/packages/php-wasm/util/src/lib/random-filename.ts @@ -0,0 +1,5 @@ +import { randomString } from './random-string'; + +export function randomFilename() { + return randomString(36, '-_'); +} diff --git a/packages/playground/remote/src/lib/utils.ts b/packages/php-wasm/util/src/lib/random-string.ts similarity index 65% rename from packages/playground/remote/src/lib/utils.ts rename to packages/php-wasm/util/src/lib/random-string.ts index ec870f6d84..2f76947a0e 100644 --- a/packages/playground/remote/src/lib/utils.ts +++ b/packages/php-wasm/util/src/lib/random-string.ts @@ -1,6 +1,10 @@ -export function randomString(length: number) { +export function randomString( + length = 36, + specialChars = '!@#$%^&*()_+=-[]/.,<>?' +) { const chars = - '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-[]/.,<>?'; + '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + + specialChars; let result = ''; for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]; diff --git a/packages/playground/blueprints/src/lib/steps/install-asset.ts b/packages/playground/blueprints/src/lib/steps/install-asset.ts index 9ba11aa4af..ab34418d93 100644 --- a/packages/playground/blueprints/src/lib/steps/install-asset.ts +++ b/packages/playground/blueprints/src/lib/steps/install-asset.ts @@ -1,5 +1,5 @@ import type { UniversalPHP } from '@php-wasm/universal'; -import { joinPaths } from '@php-wasm/util'; +import { joinPaths, randomString } from '@php-wasm/util'; import { unzip } from './unzip'; export interface InstallAssetOptions { @@ -33,7 +33,7 @@ export async function installAsset( const assetNameGuess = zipFileName.replace(/\.zip$/, ''); const wpContent = joinPaths(await playground.documentRoot, 'wp-content'); - const tmpDir = joinPaths(wpContent, crypto.randomUUID()); + const tmpDir = joinPaths(wpContent, randomString()); const tmpUnzippedFilesPath = joinPaths(tmpDir, 'assets', assetNameGuess); if (await playground.fileExists(tmpUnzippedFilesPath)) { diff --git a/packages/playground/blueprints/src/lib/steps/run-sql.spec.ts b/packages/playground/blueprints/src/lib/steps/run-sql.spec.ts index 42313295e8..439fda6a99 100644 --- a/packages/playground/blueprints/src/lib/steps/run-sql.spec.ts +++ b/packages/playground/blueprints/src/lib/steps/run-sql.spec.ts @@ -1,5 +1,5 @@ import { NodePHP } from '@php-wasm/node'; -import { phpVars } from '@php-wasm/util'; +import { phpVars, randomFilename } from '@php-wasm/util'; import { runSql } from './run-sql'; const phpVersion = '8.0'; @@ -16,8 +16,8 @@ describe('Blueprint step runSql', () => { it('should split and "run" sql queries', async () => { const docroot = '/wordpress'; - const sqlFilename = `/tmp/${crypto.randomUUID()}.sql`; - const resFilename = `/tmp/${crypto.randomUUID()}.json`; + const sqlFilename = `/tmp/${randomFilename()}.sql`; + const resFilename = `/tmp/${randomFilename()}.json`; const js = phpVars({ docroot, sqlFilename, resFilename }); await php.mkdir(docroot); diff --git a/packages/playground/blueprints/src/lib/steps/run-sql.ts b/packages/playground/blueprints/src/lib/steps/run-sql.ts index 94956d591f..6160d41d17 100644 --- a/packages/playground/blueprints/src/lib/steps/run-sql.ts +++ b/packages/playground/blueprints/src/lib/steps/run-sql.ts @@ -1,6 +1,6 @@ import { StepHandler } from '.'; import { rm } from './rm'; -import { phpVars } from '@php-wasm/util'; +import { phpVars, randomFilename } from '@php-wasm/util'; /** * @inheritDoc runSql @@ -43,7 +43,7 @@ export const runSql: StepHandler> = async ( ) => { progress?.tracker.setCaption(`Executing SQL Queries`); - const sqlFilename = `/tmp/${crypto.randomUUID()}.sql`; + const sqlFilename = `/tmp/${randomFilename()}.sql`; await playground.writeFile( sqlFilename, diff --git a/packages/playground/remote/src/lib/worker-thread.ts b/packages/playground/remote/src/lib/worker-thread.ts index 5740db8780..8638a375c9 100644 --- a/packages/playground/remote/src/lib/worker-thread.ts +++ b/packages/playground/remote/src/lib/worker-thread.ts @@ -38,8 +38,7 @@ import transportFetch from './playground-mu-plugin/playground-includes/wp_http_f import transportDummy from './playground-mu-plugin/playground-includes/wp_http_dummy.php?raw'; /** @ts-ignore */ import playgroundMuPlugin from './playground-mu-plugin/0-playground.php?raw'; -import { joinPaths } from '@php-wasm/util'; -import { randomString } from './utils'; +import { joinPaths, randomString } from '@php-wasm/util'; // post message to parent self.postMessage('worker-script-started');