From 0eeba380f293c54fd24981b4b38ecbcb0a6861e2 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 12 Feb 2025 14:43:52 -0800 Subject: [PATCH] chore: move crypto to server/util/ (#34759) --- .../playwright-core/src/androidServerImpl.ts | 2 +- .../playwright-core/src/browserServerImpl.ts | 2 +- .../playwright-core/src/client/localUtils.ts | 2 +- packages/playwright-core/src/client/waiter.ts | 3 +- .../src/server/android/android.ts | 5 +- .../src/server/android/backendAdb.ts | 3 +- .../src/server/browserContext.ts | 3 +- .../src/server/chromium/crBrowser.ts | 3 +- .../src/server/chromium/crPage.ts | 3 +- .../dispatchers/browserContextDispatcher.ts | 3 +- .../server/dispatchers/jsonPipeDispatcher.ts | 2 +- .../dispatchers/localUtilsDispatcher.ts | 2 +- .../src/server/dispatchers/pageDispatcher.ts | 3 +- .../dispatchers/playwrightDispatcher.ts | 2 +- .../server/dispatchers/streamDispatcher.ts | 3 +- .../dispatchers/webSocketRouteDispatcher.ts | 3 +- .../dispatchers/writableStreamDispatcher.ts | 2 +- packages/playwright-core/src/server/fetch.ts | 3 +- .../src/server/har/harRecorder.ts | 2 +- .../src/server/instrumentation.ts | 2 +- packages/playwright-core/src/server/page.ts | 3 +- .../playwright-core/src/server/selectors.ts | 2 +- .../src/server/trace/recorder/DEPS.list | 1 + .../src/server/trace/recorder/snapshotter.ts | 3 +- .../src/server/trace/recorder/tracing.ts | 5 +- .../src/{ => server}/utils/crypto.ts | 2 +- .../src/server/utils/httpServer.ts | 2 +- .../src/server/utils/socksProxy.ts | 3 +- .../src/server/webkit/wkPage.ts | 4 +- packages/playwright-core/src/utils.ts | 18 ++++---- .../playwright-core/src/utils/harBackend.ts | 14 +++--- .../playwright-core/src/utils/localUtils.ts | 9 ++-- .../playwright-core/src/utils/platform.ts | 46 +++++++++++++------ tests/library/debug-controller.spec.ts | 2 +- tests/playwright-test/ui-mode-fixtures.ts | 2 +- 35 files changed, 104 insertions(+), 65 deletions(-) rename packages/playwright-core/src/{ => server}/utils/crypto.ts (99%) diff --git a/packages/playwright-core/src/androidServerImpl.ts b/packages/playwright-core/src/androidServerImpl.ts index aee8bf7b31..5f3758119a 100644 --- a/packages/playwright-core/src/androidServerImpl.ts +++ b/packages/playwright-core/src/androidServerImpl.ts @@ -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'; diff --git a/packages/playwright-core/src/browserServerImpl.ts b/packages/playwright-core/src/browserServerImpl.ts index 3c5244b7b0..e8d66bc4c4 100644 --- a/packages/playwright-core/src/browserServerImpl.ts +++ b/packages/playwright-core/src/browserServerImpl.ts @@ -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'; diff --git a/packages/playwright-core/src/client/localUtils.ts b/packages/playwright-core/src/client/localUtils.ts index 8ab7421980..29f2cc8312 100644 --- a/packages/playwright-core/src/client/localUtils.ts +++ b/packages/playwright-core/src/client/localUtils.ts @@ -51,7 +51,7 @@ export class LocalUtils extends ChannelOwner { } async harOpen(params: channels.LocalUtilsHarOpenParams): Promise { - return await localUtils.harOpen(this._harBackends, params); + return await localUtils.harOpen(this._platform, this._harBackends, params); } async harLookup(params: channels.LocalUtilsHarLookupParams): Promise { diff --git a/packages/playwright-core/src/client/waiter.ts b/packages/playwright-core/src/client/waiter.ts index 90e48108a2..b125ed034a 100644 --- a/packages/playwright-core/src/client/waiter.ts +++ b/packages/playwright-core/src/client/waiter.ts @@ -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, event: string) { - this._waitId = createGuid(); + this._waitId = channelOwner._platform.createGuid(); this._channelOwner = channelOwner; this._savedZone = zones.current().without('apiZone'); diff --git a/packages/playwright-core/src/server/android/android.ts b/packages/playwright-core/src/server/android/android.ts index c3e2cbb22c..68db8ab77c 100644 --- a/packages/playwright-core/src/server/android/android.ts +++ b/packages/playwright-core/src/server/android/android.ts @@ -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'; diff --git a/packages/playwright-core/src/server/android/backendAdb.ts b/packages/playwright-core/src/server/android/backendAdb.ts index a5f28f1d35..737d415050 100644 --- a/packages/playwright-core/src/server/android/backendAdb.ts +++ b/packages/playwright-core/src/server/android/backendAdb.ts @@ -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'; diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts index 6241b8d855..48c87f7dc4 100644 --- a/packages/playwright-core/src/server/browserContext.ts +++ b/packages/playwright-core/src/server/browserContext.ts @@ -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'; diff --git a/packages/playwright-core/src/server/chromium/crBrowser.ts b/packages/playwright-core/src/server/chromium/crBrowser.ts index 51ad0ae887..abd4aee685 100644 --- a/packages/playwright-core/src/server/chromium/crBrowser.ts +++ b/packages/playwright-core/src/server/chromium/crBrowser.ts @@ -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'; diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts index 65122407a0..943dd32f27 100644 --- a/packages/playwright-core/src/server/chromium/crPage.ts +++ b/packages/playwright-core/src/server/chromium/crPage.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts b/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts index 3488964e8e..d3f110f54e 100644 --- a/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/jsonPipeDispatcher.ts b/packages/playwright-core/src/server/dispatchers/jsonPipeDispatcher.ts index 67f36e94a1..75c1c89fad 100644 --- a/packages/playwright-core/src/server/dispatchers/jsonPipeDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/jsonPipeDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts b/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts index 1b8301a1ac..37ff8c8b81 100644 --- a/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts @@ -54,7 +54,7 @@ export class LocalUtilsDispatcher extends Dispatcher<{ guid: string }, channels. } async harOpen(params: channels.LocalUtilsHarOpenParams, metadata: CallMetadata): Promise { - return await localUtils.harOpen(this._harBackends, params); + return await localUtils.harOpen(nodePlatform, this._harBackends, params); } async harLookup(params: channels.LocalUtilsHarLookupParams, metadata: CallMetadata): Promise { diff --git a/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts b/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts index 1b8a0677dd..166284366c 100644 --- a/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts b/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts index 80a1f6a696..8581273c0f 100644 --- a/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/streamDispatcher.ts b/packages/playwright-core/src/server/dispatchers/streamDispatcher.ts index 8ceb69e669..f3a0a413b7 100644 --- a/packages/playwright-core/src/server/dispatchers/streamDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/streamDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/webSocketRouteDispatcher.ts b/packages/playwright-core/src/server/dispatchers/webSocketRouteDispatcher.ts index fea8d06eec..cc1e5f9477 100644 --- a/packages/playwright-core/src/server/dispatchers/webSocketRouteDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/webSocketRouteDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts b/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts index 7d8ce2e4e1..08757f5d88 100644 --- a/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts @@ -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'; diff --git a/packages/playwright-core/src/server/fetch.ts b/packages/playwright-core/src/server/fetch.ts index 13358856d9..08e63bb83b 100644 --- a/packages/playwright-core/src/server/fetch.ts +++ b/packages/playwright-core/src/server/fetch.ts @@ -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'; diff --git a/packages/playwright-core/src/server/har/harRecorder.ts b/packages/playwright-core/src/server/har/harRecorder.ts index 94175de35c..12489f1c4c 100644 --- a/packages/playwright-core/src/server/har/harRecorder.ts +++ b/packages/playwright-core/src/server/har/harRecorder.ts @@ -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'; diff --git a/packages/playwright-core/src/server/instrumentation.ts b/packages/playwright-core/src/server/instrumentation.ts index b33ec542ec..076008a31e 100644 --- a/packages/playwright-core/src/server/instrumentation.ts +++ b/packages/playwright-core/src/server/instrumentation.ts @@ -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'; diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.ts index 730f1210de..300cf05e50 100644 --- a/packages/playwright-core/src/server/page.ts +++ b/packages/playwright-core/src/server/page.ts @@ -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'; diff --git a/packages/playwright-core/src/server/selectors.ts b/packages/playwright-core/src/server/selectors.ts index 1d1eece9cc..886108b972 100644 --- a/packages/playwright-core/src/server/selectors.ts +++ b/packages/playwright-core/src/server/selectors.ts @@ -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'; diff --git a/packages/playwright-core/src/server/trace/recorder/DEPS.list b/packages/playwright-core/src/server/trace/recorder/DEPS.list index 11a449d26d..bf35323ecc 100644 --- a/packages/playwright-core/src/server/trace/recorder/DEPS.list +++ b/packages/playwright-core/src/server/trace/recorder/DEPS.list @@ -5,6 +5,7 @@ ../../../protocol/ ../../../utils/ ../../../utilsBundle.ts +../../../utils/isomorphic/ ../../../zipBundle.ts ../../dispatchers/dispatcher.ts ../../utils diff --git a/packages/playwright-core/src/server/trace/recorder/snapshotter.ts b/packages/playwright-core/src/server/trace/recorder/snapshotter.ts index 1ad2fdf1ca..10cd322b4d 100644 --- a/packages/playwright-core/src/server/trace/recorder/snapshotter.ts +++ b/packages/playwright-core/src/server/trace/recorder/snapshotter.ts @@ -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'; diff --git a/packages/playwright-core/src/server/trace/recorder/tracing.ts b/packages/playwright-core/src/server/trace/recorder/tracing.ts index a3dedc8485..367797c754 100644 --- a/packages/playwright-core/src/server/trace/recorder/tracing.ts +++ b/packages/playwright-core/src/server/trace/recorder/tracing.ts @@ -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'; diff --git a/packages/playwright-core/src/utils/crypto.ts b/packages/playwright-core/src/server/utils/crypto.ts similarity index 99% rename from packages/playwright-core/src/utils/crypto.ts rename to packages/playwright-core/src/server/utils/crypto.ts index c59b84caf0..0a35c3c8c9 100644 --- a/packages/playwright-core/src/utils/crypto.ts +++ b/packages/playwright-core/src/server/utils/crypto.ts @@ -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'); diff --git a/packages/playwright-core/src/server/utils/httpServer.ts b/packages/playwright-core/src/server/utils/httpServer.ts index 6308ad67fa..21992195c0 100644 --- a/packages/playwright-core/src/server/utils/httpServer.ts +++ b/packages/playwright-core/src/server/utils/httpServer.ts @@ -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'; diff --git a/packages/playwright-core/src/server/utils/socksProxy.ts b/packages/playwright-core/src/server/utils/socksProxy.ts index f5eb9eec1a..49dab675f6 100644 --- a/packages/playwright-core/src/server/utils/socksProxy.ts +++ b/packages/playwright-core/src/server/utils/socksProxy.ts @@ -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'; diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts index e80031d388..d89b97905b 100644 --- a/packages/playwright-core/src/server/webkit/wkPage.ts +++ b/packages/playwright-core/src/server/webkit/wkPage.ts @@ -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'; diff --git a/packages/playwright-core/src/utils.ts b/packages/playwright-core/src/utils.ts index 0bfd82bd96..f77a878e70 100644 --- a/packages/playwright-core/src/utils.ts +++ b/packages/playwright-core/src/utils.ts @@ -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'; diff --git a/packages/playwright-core/src/utils/harBackend.ts b/packages/playwright-core/src/utils/harBackend.ts index 5c68e87c7d..304fe4b150 100644 --- a/packages/playwright-core/src/utils/harBackend.ts +++ b/packages/playwright-core/src/utils/harBackend.ts @@ -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'); } diff --git a/packages/playwright-core/src/utils/localUtils.ts b/packages/playwright-core/src/utils/localUtils.ts index 91162a0301..49a3c43175 100644 --- a/packages/playwright-core/src/utils/localUtils.ts +++ b/packages/playwright-core/src/utils/localUtils.ts @@ -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, params: channels.LocalUtilsHarOpenParams): Promise { +export async function harOpen(platform: Platform, harBackends: Map, params: channels.LocalUtilsHarOpenParams): Promise { let harBackend: HarBackend; if (params.file.endsWith('.zip')) { const zipFile = new ZipFile(params.file); @@ -148,10 +147,10 @@ export async function harOpen(harBackends: Map, 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 }; diff --git a/packages/playwright-core/src/utils/platform.ts b/packages/playwright-core/src/utils/platform.ts index e682ad46b0..60f2cdbd3a 100644 --- a/packages/playwright-core/src/utils/platform.ts +++ b/packages/playwright-core/src/utils/platform.ts @@ -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; + 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), }; diff --git a/tests/library/debug-controller.spec.ts b/tests/library/debug-controller.spec.ts index b8163296f7..7481e04246 100644 --- a/tests/library/debug-controller.spec.ts +++ b/tests/library/debug-controller.spec.ts @@ -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'; diff --git a/tests/playwright-test/ui-mode-fixtures.ts b/tests/playwright-test/ui-mode-fixtures.ts index 43089aa1d0..7fd173e3f5 100644 --- a/tests/playwright-test/ui-mode-fixtures.ts +++ b/tests/playwright-test/ui-mode-fixtures.ts @@ -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;