Skip to content
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

Remove crypto.randomUUID dependency in favor of a custom function #1016

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
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
1 change: 1 addition & 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,6 @@ 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 * from './php-vars';
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(46));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ Question ] Why we increased the characters we are using in comparison with UUID?
UUID has 36 + 4 characters instead of 46.
Can we change that to be 40 instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the correction. UUID actually is 36 characters long. Example 'd6c556d6-32b8-4bdf-b338-20a16e9860aa'.

A string containing a randomly generated, 36 character long v4 UUID.
https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID

I added a default length of 36 to randomString function.

https://github.com/WordPress/wordpress-playground/pull/1016/files#diff-0a926c4fcfdcd984fa42eaa66f44f2a5aea8bcde6576ee5ba04698402489ad65R1

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, randomString } 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/${randomString(46)}.sql`;
const resFilename = `/tmp/${randomString(46)}.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, randomString } 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/${randomString(46)}.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
Loading