1515 * limitations under the License.
1616 */
1717
18- import * as fs from 'fs' ;
19- import * as path from 'path' ;
20-
2118import { Artifact } from './artifact' ;
2219import { Browser } from './browser' ;
2320import { CDPSession } from './cdpSession' ;
@@ -38,14 +35,18 @@ import { Waiter } from './waiter';
3835import { WebError } from './webError' ;
3936import { Worker } from './worker' ;
4037import { TimeoutSettings } from '../common/timeoutSettings' ;
41- import { headersObjectToArray , isRegExp , isString , mkdirIfNeeded , urlMatchesEqual } from '../utils' ;
38+ import { mkdirIfNeeded } from '../utils/fileUtils' ;
39+ import { headersObjectToArray } from '../utils/headers' ;
40+ import { urlMatchesEqual } from '../utils/isomorphic/urlMatch' ;
41+ import { isRegExp , isString } from '../utils/rtti' ;
4242import { rewriteErrorMessage } from '../utils/stackTrace' ;
4343
4444import type { BrowserType } from './browserType' ;
4545import type { BrowserContextOptions , Headers , LaunchOptions , StorageState , WaitForEventOptions } from './types' ;
4646import type * as structs from '../../types/structs' ;
4747import type * as api from '../../types/types' ;
48- import type { URLMatch } from '../utils' ;
48+ import type { Platform } from '../common/platform' ;
49+ import type { URLMatch } from '../utils/isomorphic/urlMatch' ;
4950import type * as channels from '@protocol/channels' ;
5051
5152export class BrowserContext extends ChannelOwner < channels . BrowserContextChannel > implements api . BrowserContext {
@@ -107,7 +108,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
107108 this . emit ( Events . BrowserContext . ServiceWorker , serviceWorker ) ;
108109 } ) ;
109110 this . _channel . on ( 'console' , event => {
110- const consoleMessage = new ConsoleMessage ( event ) ;
111+ const consoleMessage = new ConsoleMessage ( this . _platform , event ) ;
111112 this . emit ( Events . BrowserContext . Console , consoleMessage ) ;
112113 const page = consoleMessage . page ( ) ;
113114 if ( page )
@@ -321,7 +322,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
321322 }
322323
323324 async addInitScript ( script : Function | string | { path ?: string , content ?: string } , arg ?: any ) : Promise < void > {
324- const source = await evaluationScript ( script , arg ) ;
325+ const source = await evaluationScript ( this . _platform , script , arg ) ;
325326 await this . _channel . addInitScript ( { source } ) ;
326327 }
327328
@@ -431,8 +432,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
431432 async storageState ( options : { path ?: string , indexedDB ?: boolean } = { } ) : Promise < StorageState > {
432433 const state = await this . _channel . storageState ( { indexedDB : options . indexedDB } ) ;
433434 if ( options . path ) {
434- await mkdirIfNeeded ( options . path ) ;
435- await fs . promises . writeFile ( options . path , JSON . stringify ( state , undefined , 2 ) , 'utf8' ) ;
435+ await mkdirIfNeeded ( this . _platform , options . path ) ;
436+ await this . _platform . fs ( ) . promises . writeFile ( options . path , JSON . stringify ( state , undefined , 2 ) , 'utf8' ) ;
436437 }
437438 return state ;
438439 }
@@ -500,11 +501,11 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
500501 }
501502}
502503
503- async function prepareStorageState ( options : BrowserContextOptions ) : Promise < channels . BrowserNewContextParams [ 'storageState' ] > {
504+ async function prepareStorageState ( platform : Platform , options : BrowserContextOptions ) : Promise < channels . BrowserNewContextParams [ 'storageState' ] > {
504505 if ( typeof options . storageState !== 'string' )
505506 return options . storageState ;
506507 try {
507- return JSON . parse ( await fs . promises . readFile ( options . storageState , 'utf8' ) ) ;
508+ return JSON . parse ( await platform . fs ( ) . promises . readFile ( options . storageState , 'utf8' ) ) ;
508509 } catch ( e ) {
509510 rewriteErrorMessage ( e , `Error reading storage state from ${ options . storageState } :\n` + e . message ) ;
510511 throw e ;
@@ -524,7 +525,7 @@ function prepareRecordHarOptions(options: BrowserContextOptions['recordHar']): c
524525 } ;
525526}
526527
527- export async function prepareBrowserContextParams ( options : BrowserContextOptions ) : Promise < channels . BrowserNewContextParams > {
528+ export async function prepareBrowserContextParams ( platform : Platform , options : BrowserContextOptions ) : Promise < channels . BrowserNewContextParams > {
528529 if ( options . videoSize && ! options . videosPath )
529530 throw new Error ( `"videoSize" option requires "videosPath" to be specified` ) ;
530531 if ( options . extraHTTPHeaders )
@@ -534,15 +535,15 @@ export async function prepareBrowserContextParams(options: BrowserContextOptions
534535 viewport : options . viewport === null ? undefined : options . viewport ,
535536 noDefaultViewport : options . viewport === null ,
536537 extraHTTPHeaders : options . extraHTTPHeaders ? headersObjectToArray ( options . extraHTTPHeaders ) : undefined ,
537- storageState : await prepareStorageState ( options ) ,
538+ storageState : await prepareStorageState ( platform , options ) ,
538539 serviceWorkers : options . serviceWorkers ,
539540 recordHar : prepareRecordHarOptions ( options . recordHar ) ,
540541 colorScheme : options . colorScheme === null ? 'no-override' : options . colorScheme ,
541542 reducedMotion : options . reducedMotion === null ? 'no-override' : options . reducedMotion ,
542543 forcedColors : options . forcedColors === null ? 'no-override' : options . forcedColors ,
543544 contrast : options . contrast === null ? 'no-override' : options . contrast ,
544545 acceptDownloads : toAcceptDownloadsProtocol ( options . acceptDownloads ) ,
545- clientCertificates : await toClientCertificatesProtocol ( options . clientCertificates ) ,
546+ clientCertificates : await toClientCertificatesProtocol ( platform , options . clientCertificates ) ,
546547 } ;
547548 if ( ! contextParams . recordVideo && options . videosPath ) {
548549 contextParams . recordVideo = {
@@ -551,7 +552,7 @@ export async function prepareBrowserContextParams(options: BrowserContextOptions
551552 } ;
552553 }
553554 if ( contextParams . recordVideo && contextParams . recordVideo . dir )
554- contextParams . recordVideo . dir = path . resolve ( process . cwd ( ) , contextParams . recordVideo . dir ) ;
555+ contextParams . recordVideo . dir = platform . path ( ) . resolve ( process . cwd ( ) , contextParams . recordVideo . dir ) ;
555556 return contextParams ;
556557}
557558
@@ -563,15 +564,15 @@ function toAcceptDownloadsProtocol(acceptDownloads?: boolean) {
563564 return 'deny' ;
564565}
565566
566- export async function toClientCertificatesProtocol ( certs ?: BrowserContextOptions [ 'clientCertificates' ] ) : Promise < channels . PlaywrightNewRequestParams [ 'clientCertificates' ] > {
567+ export async function toClientCertificatesProtocol ( platform : Platform , certs ?: BrowserContextOptions [ 'clientCertificates' ] ) : Promise < channels . PlaywrightNewRequestParams [ 'clientCertificates' ] > {
567568 if ( ! certs )
568569 return undefined ;
569570
570571 const bufferizeContent = async ( value ?: Buffer , path ?: string ) : Promise < Buffer | undefined > => {
571572 if ( value )
572573 return value ;
573574 if ( path )
574- return await fs . promises . readFile ( path ) ;
575+ return await platform . fs ( ) . promises . readFile ( path ) ;
575576 } ;
576577
577578 return await Promise . all ( certs . map ( async cert => ( {
0 commit comments