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 all 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
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
Loading