Skip to content

Commit 9895cd0

Browse files
authored
chore: optionally create downloads folder (#2188)
1 parent 8c08348 commit 9895cd0

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/chromium/crBrowser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export class CRBrowser extends BrowserBase {
4646
private _tracingClient: CRSession | undefined;
4747
readonly _isHeadful: boolean;
4848

49-
static async connect(transport: ConnectionTransport, isPersistent: boolean, logger: InnerLogger, options: { slowMo?: number, headless?: boolean } = {}): Promise<CRBrowser> {
49+
static async connect(transport: ConnectionTransport, isPersistent: boolean, logger: InnerLogger, options: { slowMo?: number, headless?: boolean, viewport?: types.Size | null } = {}): Promise<CRBrowser> {
5050
const connection = new CRConnection(SlowMoTransport.wrap(transport, options.slowMo), logger);
51-
const browser = new CRBrowser(connection, logger, isPersistent, !options.headless);
51+
const browser = new CRBrowser(connection, logger, { persistent: isPersistent, headful: !options.headless, viewport: options.viewport });
5252
const session = connection.rootSession;
5353
if (!isPersistent) {
5454
await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });
@@ -83,14 +83,14 @@ export class CRBrowser extends BrowserBase {
8383
return browser;
8484
}
8585

86-
constructor(connection: CRConnection, logger: InnerLogger, isPersistent: boolean, isHeadful: boolean) {
86+
constructor(connection: CRConnection, logger: InnerLogger, options: { headful?: boolean, persistent?: boolean, viewport?: types.Size | null } = {}) {
8787
super(logger);
8888
this._connection = connection;
8989
this._session = this._connection.rootSession;
9090

91-
if (isPersistent)
92-
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({}));
93-
this._isHeadful = isHeadful;
91+
if (options.persistent)
92+
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({ viewport: options.viewport }));
93+
this._isHeadful = !!options.headful;
9494
this._connection.on(ConnectionEvents.Disconnected, () => {
9595
for (const context of this._contexts.values())
9696
context._browserClosed();

src/chromium/crPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ class FrameSession {
375375
}
376376

377377
async _initialize(hasUIWindow: boolean) {
378-
if (hasUIWindow) {
378+
if (hasUIWindow && this._crPage._browserContext._options.viewport !== null) {
379379
const { windowId } = await this._client.send('Browser.getWindowForTarget');
380380
this._windowId = windowId;
381381
}

src/install/installer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
7070
}
7171

7272
// 3. Install missing browsers for this package.
73-
const myBrowsers = JSON.parse((await fsReadFileAsync(path.join(packagePath, 'browsers.json'))).toString())['browsers'];
73+
const myBrowsers = JSON.parse((await fsReadFileAsync(path.join(packagePath, 'browsers.json'))).toString())['browsers'] as browserPaths.BrowserDescriptor[];
7474
for (const browser of myBrowsers) {
7575
const browserPath = browserPaths.browserDirectory(browsersPath, browser);
7676
await browserFetcher.downloadBrowserWithProgressBar(browserPath, browser);

src/server/processLauncher.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export type LaunchProcessOptions = {
5656
pipe?: boolean,
5757
tempDir?: string,
5858

59+
cwd?: string,
60+
omitDownloads?: boolean,
61+
5962
// Note: attemptToGracefullyClose should reject if it does not close the browser.
6063
attemptToGracefullyClose: () => Promise<any>,
6164
onkill: (exitCode: number | null, signal: string | null) => void,
@@ -81,7 +84,8 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
8184
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
8285
detached: process.platform !== 'win32',
8386
env: (options.env as {[key: string]: string}),
84-
stdio
87+
cwd: options.cwd,
88+
stdio,
8589
}
8690
);
8791
if (!spawnedProcess.pid) {
@@ -104,7 +108,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
104108
logger._log(browserStdErrLog, data);
105109
});
106110

107-
const downloadsPath = await mkdtempAsync(DOWNLOADS_FOLDER);
111+
const downloadsPath = options.omitDownloads ? '' : await mkdtempAsync(DOWNLOADS_FOLDER);
108112

109113
let processClosed = false;
110114
const waitForProcessToClose = new Promise((fulfill, reject) => {
@@ -115,7 +119,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
115119
options.onkill(exitCode, signal);
116120
// Cleanup as processes exit.
117121
Promise.all([
118-
removeFolderAsync(downloadsPath),
122+
options.omitDownloads ? Promise.resolve() : removeFolderAsync(downloadsPath),
119123
options.tempDir ? removeFolderAsync(options.tempDir) : Promise.resolve()
120124
]).catch((err: Error) => console.error(err)).then(fulfill);
121125
});

0 commit comments

Comments
 (0)