Skip to content

Commit

Permalink
Remove crypto.randomUUID dependency in favor of a custom function (#…
Browse files Browse the repository at this point in the history
…1016)

- Remove the crypto polyfill introduced on
#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
  • Loading branch information
sejas authored Feb 8, 2024
1 parent ef5ca8c commit ececcd3
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 28 deletions.
1 change: 0 additions & 1 deletion packages/php-wasm/node-polyfills/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import './lib/blob';
import './lib/custom-event';
import './lib/crypto';
10 changes: 0 additions & 10 deletions packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/php-wasm/node-polyfills/src/lib/crypto.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/php-wasm/node/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default defineConfig(() => {
'util',
'dns',
'ws',
'crypto',
],
output: {
entryFileNames: '[name].js',
Expand Down
2 changes: 2 additions & 0 deletions packages/php-wasm/util/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions packages/php-wasm/util/src/lib/random-filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { randomString } from './random-string';

export function randomFilename() {
return randomString(36, '-_');
}
Original file line number Diff line number Diff line change
@@ -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)];
Expand Down
4 changes: 2 additions & 2 deletions packages/playground/blueprints/src/lib/steps/install-asset.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/playground/blueprints/src/lib/steps/run-sql.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions packages/playground/blueprints/src/lib/steps/run-sql.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,7 +43,7 @@ export const runSql: StepHandler<RunSqlStep<File>> = async (
) => {
progress?.tracker.setCaption(`Executing SQL Queries`);

const sqlFilename = `/tmp/${crypto.randomUUID()}.sql`;
const sqlFilename = `/tmp/${randomFilename()}.sql`;

await playground.writeFile(
sqlFilename,
Expand Down
3 changes: 1 addition & 2 deletions packages/playground/remote/src/lib/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit ececcd3

Please sign in to comment.