chore: move crypto to server/util/ (#34759)

This commit is contained in:
Pavel Feldman 2025-02-12 14:43:52 -08:00 committed by GitHub
parent c2c336b97d
commit 0eeba380f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
35 changed files with 104 additions and 65 deletions

View File

@ -16,7 +16,7 @@
import { PlaywrightServer } from './remote/playwrightServer';
import { createPlaywright } from './server/playwright';
import { createGuid } from './utils';
import { createGuid } from './server/utils/crypto';
import { ws } from './utilsBundle';
import type { BrowserServer } from './client/browserType';

View File

@ -20,7 +20,7 @@ import { PlaywrightServer } from './remote/playwrightServer';
import { helper } from './server/helper';
import { serverSideCallMetadata } from './server/instrumentation';
import { createPlaywright } from './server/playwright';
import { createGuid } from './utils';
import { createGuid } from './server/utils/crypto';
import { rewriteErrorMessage } from './utils/stackTrace';
import { ws } from './utilsBundle';

View File

@ -51,7 +51,7 @@ export class LocalUtils extends ChannelOwner<channels.LocalUtilsChannel> {
}
async harOpen(params: channels.LocalUtilsHarOpenParams): Promise<channels.LocalUtilsHarOpenResult> {
return await localUtils.harOpen(this._harBackends, params);
return await localUtils.harOpen(this._platform, this._harBackends, params);
}
async harLookup(params: channels.LocalUtilsHarLookupParams): Promise<channels.LocalUtilsHarLookupResult> {

View File

@ -15,7 +15,6 @@
*/
import { TimeoutError } from './errors';
import { createGuid } from '../utils/crypto';
import { rewriteErrorMessage } from '../utils/stackTrace';
import { zones } from '../utils/zones';
@ -35,7 +34,7 @@ export class Waiter {
private _savedZone: Zone;
constructor(channelOwner: ChannelOwner<channels.EventTargetChannel>, event: string) {
this._waitId = createGuid();
this._waitId = channelOwner._platform.createGuid();
this._channelOwner = channelOwner;
this._savedZone = zones.current().without('apiZone');

View File

@ -21,7 +21,10 @@ import * as path from 'path';
import { TimeoutSettings } from '../../common/timeoutSettings';
import { PipeTransport } from '../../utils/pipeTransport';
import { createGuid, getPackageManagerExecCommand, isUnderTest, makeWaitForNextTask } from '../../utils';
import { createGuid } from '../utils/crypto';
import { isUnderTest } from '../../utils/debug';
import { getPackageManagerExecCommand } from '../../utils/env';
import { makeWaitForNextTask } from '../../utils/task';
import { RecentLogsCollector } from '../../utils/debugLogger';
import { debug } from '../../utilsBundle';
import { wsReceiver, wsSender } from '../../utilsBundle';

View File

@ -17,7 +17,8 @@
import { EventEmitter } from 'events';
import * as net from 'net';
import { assert, createGuid } from '../../utils';
import { assert } from '../../utils/debug';
import { createGuid } from '../utils/crypto';
import { debug } from '../../utilsBundle';
import type { Backend, DeviceBackend, SocketBackend } from './android';

View File

@ -19,7 +19,8 @@ import * as fs from 'fs';
import * as path from 'path';
import { TimeoutSettings } from '../common/timeoutSettings';
import { createGuid, debugMode } from '../utils';
import { createGuid } from './utils/crypto';
import { debugMode } from '../utils/debug';
import { Clock } from './clock';
import { Debugger } from './debugger';
import { BrowserContextAPIRequestContext } from './fetch';

View File

@ -17,7 +17,8 @@
import * as path from 'path';
import { assert, createGuid } from '../../utils';
import { assert } from '../../utils/debug';
import { createGuid } from '../utils/crypto';
import { Artifact } from '../artifact';
import { Browser } from '../browser';
import { BrowserContext, assertBrowserContextIsNotOwned, verifyGeolocation } from '../browserContext';

View File

@ -17,7 +17,8 @@
import * as path from 'path';
import { assert, createGuid } from '../../utils';
import { assert } from '../../utils/debug';
import { createGuid } from '../utils/crypto';
import { eventsHelper } from '../../utils/eventsHelper';
import { rewriteErrorMessage } from '../../utils/stackTrace';
import * as dialog from '../dialog';

View File

@ -31,7 +31,8 @@ import { Recorder } from '../recorder';
import { TracingDispatcher } from './tracingDispatcher';
import { WebSocketRouteDispatcher } from './webSocketRouteDispatcher';
import { WritableStreamDispatcher } from './writableStreamDispatcher';
import { createGuid, urlMatches } from '../../utils';
import { createGuid } from '../utils/crypto';
import { urlMatches } from '../../utils/isomorphic/urlMatch';
import { RecorderApp } from '../recorder/recorderApp';
import type { Artifact } from '../artifact';

View File

@ -15,7 +15,7 @@
*/
import { Dispatcher } from './dispatcher';
import { createGuid } from '../../utils';
import { createGuid } from '../utils/crypto';
import type { LocalUtilsDispatcher } from './localUtilsDispatcher';
import type * as channels from '@protocol/channels';

View File

@ -54,7 +54,7 @@ export class LocalUtilsDispatcher extends Dispatcher<{ guid: string }, channels.
}
async harOpen(params: channels.LocalUtilsHarOpenParams, metadata: CallMetadata): Promise<channels.LocalUtilsHarOpenResult> {
return await localUtils.harOpen(this._harBackends, params);
return await localUtils.harOpen(nodePlatform, this._harBackends, params);
}
async harLookup(params: channels.LocalUtilsHarLookupParams, metadata: CallMetadata): Promise<channels.LocalUtilsHarLookupResult> {

View File

@ -25,7 +25,8 @@ import { RequestDispatcher } from './networkDispatchers';
import { ResponseDispatcher } from './networkDispatchers';
import { RouteDispatcher, WebSocketDispatcher } from './networkDispatchers';
import { WebSocketRouteDispatcher } from './webSocketRouteDispatcher';
import { createGuid, urlMatches } from '../../utils';
import { createGuid } from '../utils/crypto';
import { urlMatches } from '../../utils/isomorphic/urlMatch';
import type { Artifact } from '../artifact';
import type { BrowserContext } from '../browserContext';

View File

@ -25,7 +25,7 @@ import { ElectronDispatcher } from './electronDispatcher';
import { LocalUtilsDispatcher } from './localUtilsDispatcher';
import { APIRequestContextDispatcher } from './networkDispatchers';
import { SelectorsDispatcher } from './selectorsDispatcher';
import { createGuid } from '../../utils';
import { createGuid } from '../utils/crypto';
import { eventsHelper } from '../../utils/eventsHelper';
import type { RootDispatcher } from './dispatcher';

View File

@ -15,7 +15,8 @@
*/
import { Dispatcher } from './dispatcher';
import { ManualPromise, createGuid } from '../../utils';
import { ManualPromise } from '../../utils/isomorphic/manualPromise';
import { createGuid } from '../utils/crypto';
import type { ArtifactDispatcher } from './artifactDispatcher';
import type * as channels from '@protocol/channels';

View File

@ -18,7 +18,8 @@ import { Page } from '../page';
import { Dispatcher, existingDispatcher } from './dispatcher';
import { PageDispatcher } from './pageDispatcher';
import * as webSocketMockSource from '../../generated/webSocketMockSource';
import { createGuid, urlMatches } from '../../utils';
import { createGuid } from '../utils/crypto';
import { urlMatches } from '../../utils/isomorphic/urlMatch';
import { eventsHelper } from '../../utils/eventsHelper';
import type { BrowserContextDispatcher } from './browserContextDispatcher';

View File

@ -17,7 +17,7 @@
import * as fs from 'fs';
import { Dispatcher } from './dispatcher';
import { createGuid } from '../../utils';
import { createGuid } from '../utils/crypto';
import type { BrowserContextDispatcher } from './browserContextDispatcher';
import type * as channels from '@protocol/channels';

View File

@ -22,7 +22,8 @@ import * as url from 'url';
import * as zlib from 'zlib';
import { TimeoutSettings } from '../common/timeoutSettings';
import { assert, constructURLBasedOnBaseURL, createGuid, eventsHelper, monotonicTime } from '../utils';
import { assert, constructURLBasedOnBaseURL, eventsHelper, monotonicTime } from '../utils';
import { createGuid } from './utils/crypto';
import { getUserAgent } from '../utils/userAgent';
import { HttpsProxyAgent, SocksProxyAgent } from '../utilsBundle';
import { BrowserContext, verifyClientCertificates } from './browserContext';

View File

@ -19,7 +19,7 @@ import * as path from 'path';
import { Artifact } from '../artifact';
import { HarTracer } from './harTracer';
import { createGuid } from '../../utils';
import { createGuid } from '../utils/crypto';
import { ManualPromise } from '../../utils/isomorphic/manualPromise';
import { yazl } from '../../zipBundle';

View File

@ -16,7 +16,7 @@
import { EventEmitter } from 'events';
import { createGuid } from '../utils';
import { createGuid } from './utils/crypto';
import type { Browser } from './browser';
import type { BrowserContext } from './browserContext';

View File

@ -29,7 +29,8 @@ import * as js from './javascript';
import { ProgressController } from './progress';
import { Screenshotter, validateScreenshotOptions } from './screenshotter';
import { TimeoutSettings } from '../common/timeoutSettings';
import { LongStandingScope, assert, compressCallLog, createGuid, trimStringWithEllipsis } from '../utils';
import { LongStandingScope, assert, compressCallLog, trimStringWithEllipsis } from '../utils';
import { createGuid } from './utils/crypto';
import { asLocator } from '../utils';
import { getComparator } from './utils/comparators';
import { debugLogger } from '../utils/debugLogger';

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
import { createGuid } from '../utils';
import { createGuid } from './utils/crypto';
import { InvalidSelectorError, parseSelector, stringifySelector, visitAllSelectorParts } from '../utils/isomorphic/selectorParser';
import type { ParsedSelector } from '../utils/isomorphic/selectorParser';

View File

@ -5,6 +5,7 @@
../../../protocol/
../../../utils/
../../../utilsBundle.ts
../../../utils/isomorphic/
../../../zipBundle.ts
../../dispatchers/dispatcher.ts
../../utils

View File

@ -15,7 +15,8 @@
*/
import { frameSnapshotStreamer } from './snapshotterInjected';
import { calculateSha1, createGuid, monotonicTime } from '../../../utils';
import { monotonicTime } from '../../../utils/isomorphic/time';
import { calculateSha1, createGuid } from '../../utils/crypto';
import { debugLogger } from '../../../utils/debugLogger';
import { eventsHelper } from '../../../utils/eventsHelper';
import { mime } from '../../../utilsBundle';

View File

@ -20,7 +20,10 @@ import * as path from 'path';
import { Snapshotter } from './snapshotter';
import { commandsWithTracingSnapshots } from '../../../protocol/debug';
import { assert, createGuid, eventsHelper, monotonicTime } from '../../../utils';
import { assert } from '../../../utils/debug';
import { monotonicTime } from '../../../utils/isomorphic/time';
import { eventsHelper } from '../../../utils/eventsHelper';
import { createGuid } from '../../utils/crypto';
import { Artifact } from '../../artifact';
import { BrowserContext } from '../../browserContext';
import { Dispatcher } from '../../dispatchers/dispatcher';

View File

@ -16,7 +16,7 @@
import * as crypto from 'crypto';
import { assert } from './debug';
import { assert } from '../../utils/debug';
export function createGuid(): string {
return crypto.randomBytes(16).toString('hex');

View File

@ -18,7 +18,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { mime, wsServer } from '../../utilsBundle';
import { createGuid } from '../../utils/crypto';
import { createGuid } from './crypto';
import { assert } from '../../utils/debug';
import { ManualPromise } from '../../utils/isomorphic/manualPromise';
import { createHttpServer } from './network';

View File

@ -17,7 +17,8 @@
import EventEmitter from 'events';
import * as net from 'net';
import { assert, createGuid, } from '../../utils';
import { assert } from '../../utils/debug';
import { createGuid } from './crypto';
import { debugLogger } from '../../utils/debugLogger';
import { createSocket } from './happyEyeballs';

View File

@ -17,7 +17,9 @@
import * as path from 'path';
import { assert, createGuid, debugAssert, headersArrayToObject } from '../../utils';
import { assert, debugAssert } from '../../utils';
import { headersArrayToObject } from '../../utils/isomorphic/headers';
import { createGuid } from '../utils/crypto';
import { eventsHelper } from '../../utils/eventsHelper';
import { hostPlatform } from '../../utils/hostPlatform';
import { splitErrorMessage } from '../../utils/stackTrace';

View File

@ -14,39 +14,39 @@
* limitations under the License.
*/
export * from './utils/isomorphic/manualPromise';
export * from './utils/isomorphic/locatorGenerators';
export * from './utils/isomorphic/manualPromise';
export * from './utils/isomorphic/mimeType';
export * from './utils/isomorphic/stringUtils';
export * from './utils/isomorphic/urlMatch';
export * from './utils/isomorphic/multimap';
export * from './utils/isomorphic/rtti';
export * from './utils/isomorphic/stringUtils';
export * from './utils/isomorphic/time';
export * from './utils/isomorphic/timeoutRunner';
export * from './utils/isomorphic/urlMatch';
export * from './utils/crypto';
export * from './utils/debug';
export * from './utils/debugLogger';
export * from './utils/env';
export * from './utils/eventsHelper';
export * from './utils/expectUtils';
export * from './utils/isomorphic/headers';
export * from './utils/hostPlatform';
export * from './utils/platform';
export * from './utils/isomorphic/headers';
export * from './utils/isomorphic/semaphore';
export * from './utils/platform';
export * from './utils/stackTrace';
export * from './utils/task';
export * from './utils/userAgent';
export * from './utils/zipFile';
export * from './utils/zones';
export * from './server/utils/socksProxy';
export * from './server/utils/processLauncher';
export * from './server/utils/ascii';
export * from './server/utils/comparators';
export * from './server/utils/crypto';
export * from './server/utils/fileUtils';
export * from './server/utils/httpServer';
export * from './server/utils/network';
export * from './server/utils/processLauncher';
export * from './server/utils/profiler';
export * from './server/utils/wsServer';
export * from './server/utils/socksProxy';
export * from './server/utils/spawnAsync';
export * from './server/utils/wsServer';

View File

@ -14,24 +14,24 @@
* limitations under the License.
*/
import * as fs from 'fs';
import * as path from 'path';
import { createGuid } from './crypto';
import { ZipFile } from './zipFile';
import type { HeadersArray } from '../common/types';
import type * as har from '@trace/har';
import type { Platform } from './platform';
const redirectStatus = [301, 302, 303, 307, 308];
export class HarBackend {
readonly id = createGuid();
readonly id: string;
private _harFile: har.HARFile;
private _zipFile: ZipFile | null;
private _baseDir: string | null;
private _platform: Platform;
constructor(harFile: har.HARFile, baseDir: string | null, zipFile: ZipFile | null) {
constructor(platform: Platform, harFile: har.HARFile, baseDir: string | null, zipFile: ZipFile | null) {
this._platform = platform;
this.id = platform.createGuid();
this._harFile = harFile;
this._baseDir = baseDir;
this._zipFile = zipFile;
@ -79,7 +79,7 @@ export class HarBackend {
if (this._zipFile)
buffer = await this._zipFile.read(file);
else
buffer = await fs.promises.readFile(path.resolve(this._baseDir!, file));
buffer = await this._platform.fs().promises.readFile(this._platform.path().resolve(this._baseDir!, file));
} else {
buffer = Buffer.from(content.text || '', content.encoding === 'base64' ? 'base64' : 'utf-8');
}

View File

@ -25,7 +25,6 @@ import { ZipFile } from './zipFile';
import { yauzl, yazl } from '../zipBundle';
import { serializeClientSideCallMetadata } from '../utils/isomorphic/traceUtils';
import { assert } from '../utils/debug';
import { calculateSha1 } from '../utils/crypto';
import type { Platform } from './platform';
import type * as channels from '@protocol/channels';
@ -78,7 +77,7 @@ export async function zip(platform: Platform, stackSessions: Map<string, StackSe
sourceFiles.add(file);
}
for (const sourceFile of sourceFiles)
addFile(sourceFile, 'resources/src@' + calculateSha1(sourceFile) + '.txt');
addFile(sourceFile, 'resources/src@' + await platform.calculateSha1(sourceFile) + '.txt');
}
if (params.mode === 'write') {
@ -138,7 +137,7 @@ async function deleteStackSession(platform: Platform, stackSessions: Map<string,
stackSessions.delete(stacksId!);
}
export async function harOpen(harBackends: Map<string, HarBackend>, params: channels.LocalUtilsHarOpenParams): Promise<channels.LocalUtilsHarOpenResult> {
export async function harOpen(platform: Platform, harBackends: Map<string, HarBackend>, params: channels.LocalUtilsHarOpenParams): Promise<channels.LocalUtilsHarOpenResult> {
let harBackend: HarBackend;
if (params.file.endsWith('.zip')) {
const zipFile = new ZipFile(params.file);
@ -148,10 +147,10 @@ export async function harOpen(harBackends: Map<string, HarBackend>, params: chan
return { error: 'Specified archive does not have a .har file' };
const har = await zipFile.read(harEntryName);
const harFile = JSON.parse(har.toString()) as har.HARFile;
harBackend = new HarBackend(harFile, null, zipFile);
harBackend = new HarBackend(platform, harFile, null, zipFile);
} else {
const harFile = JSON.parse(await fs.promises.readFile(params.file, 'utf-8')) as har.HARFile;
harBackend = new HarBackend(harFile, path.dirname(params.file), null);
harBackend = new HarBackend(platform, harFile, path.dirname(params.file), null);
}
harBackends.set(harBackend.id, harBackend);
return { harId: harBackend.id };

View File

@ -14,36 +14,56 @@
* limitations under the License.
*/
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import * as util from 'util';
export type Platform = {
calculateSha1(text: string): Promise<string>;
createGuid: () => string;
fs: () => typeof fs;
path: () => typeof path;
inspectCustom: symbol | undefined;
path: () => typeof path;
ws?: (url: string) => WebSocket;
};
export const emptyPlatform: Platform = {
export const nodePlatform: Platform = {
calculateSha1: (text: string) => {
const sha1 = crypto.createHash('sha1');
sha1.update(text);
return Promise.resolve(sha1.digest('hex'));
},
createGuid: () => crypto.randomBytes(16).toString('hex'),
fs: () => fs,
inspectCustom: util.inspect.custom,
path: () => path,
};
export const webPlatform: Platform = {
calculateSha1: async (text: string) => {
const bytes = new TextEncoder().encode(text);
const hashBuffer = await crypto.subtle.digest('SHA-1', bytes);
return Array.from(new Uint8Array(hashBuffer), b => b.toString(16).padStart(2, '0')).join('');
},
createGuid: () => {
return Array.from(crypto.getRandomValues(new Uint8Array(16)), b => b.toString(16).padStart(2, '0')).join('');
},
fs: () => {
throw new Error('File system is not available');
},
inspectCustom: undefined,
path: () => {
throw new Error('Path module is not available');
},
inspectCustom: undefined,
};
export const nodePlatform: Platform = {
fs: () => fs,
path: () => path,
inspectCustom: util.inspect.custom,
};
export const webPlatform: Platform = {
...emptyPlatform,
ws: (url: string) => new WebSocket(url),
};

View File

@ -16,7 +16,7 @@
import { expect, playwrightTest as baseTest } from '../config/browserTest';
import { PlaywrightServer } from '../../packages/playwright-core/lib/remote/playwrightServer';
import { createGuid } from '../../packages/playwright-core/lib/utils/crypto';
import { createGuid } from '../../packages/playwright-core/lib/server/utils/crypto';
import { Backend } from '../config/debugControllerBackend';
import type { Browser, BrowserContext } from '@playwright/test';
import type * as channels from '@protocol/channels';

View File

@ -22,7 +22,7 @@ import { cleanEnv, cliEntrypoint, test as base, writeFiles, removeFolders } from
import type { Files, RunOptions } from './playwright-test-fixtures';
import type { Browser, Page, TestInfo } from './stable-test-runner';
import { chromium } from './stable-test-runner';
import { createGuid } from '../../packages/playwright-core/src/utils/crypto';
import { createGuid } from '../../packages/playwright-core/src/server/utils/crypto';
type Latch = {
blockingCode: string;