Skip to content

Commit

Permalink
Merge branch 'main' into jimp-0.22.12
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Mar 19, 2024
2 parents cf34663 + fe8b503 commit 20b3775
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lib/core/engine/command/bidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Bidi {
*/
async onMessage(f) {
if (this.browserName === 'firefox') {
const client = this.engineDelegate.getBidi();
const client = await this.engineDelegate.getBidi();
const ws = await client.socket;
ws.on('message', f);
} else {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Bidi {
*/
async subscribe(messageType) {
if (this.browserName === 'firefox') {
const client = this.engineDelegate.getBidi();
const client = await this.engineDelegate.getBidi();
return client.subscribe(messageType, [
await this.engineDelegate.getWindowHandle()
]);
Expand All @@ -85,7 +85,7 @@ export class Bidi {
*/
async unsubscribe(messageType) {
if (this.browserName === 'firefox') {
const client = this.engineDelegate.getBidi();
const client = await this.engineDelegate.getBidi();
return client.unsubscribe(messageType, [
this.engineDelegate.getWindowHandle()
]);
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Bidi {
async send(parameters) {
if (this.browserName === 'firefox') {
try {
const client = this.engineDelegate.getBidi();
const client = await this.engineDelegate.getBidi();
return client.send(parameters);
} catch (error) {
log.error(
Expand Down
2 changes: 0 additions & 2 deletions lib/extensionserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export class ExtensionServer {
(options.cacheClearRaw ||
options.requestheader ||
options.block ||
options.basicAuth ||
options.cookie ||
options.clearCacheKeepCookies)
? true
: false;
Expand Down
10 changes: 0 additions & 10 deletions lib/extensionserver/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ function generateURL(port, testUrl, options) {
query.ba = options.basicAuth + '@' + testUrl;
}

if (options.cookie) {
const cookies = toArray(options.cookie);
query.cookie = [];
for (const cookie of cookies) {
const name = cookie.slice(0, Math.max(0, cookie.indexOf('=')));
const value = cookie.slice(Math.max(0, cookie.indexOf('=') + 1));
query.cookie.push(name + '@' + value + '@' + testUrl);
}
}

return format({
protocol: 'http',
hostname: '127.0.0.1',
Expand Down
68 changes: 67 additions & 1 deletion lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Bidi } from '../core/engine/command/bidi.js';
import intel from 'intel';
import { toArray } from '../support/util.js';
const log = intel.getLogger('browsertime.firefox.bidi');

export class FirefoxBidi {
constructor(bidi, browsingContextId, options) {
constructor(bidi, browsingContextId, driver, options) {
this.options = options;
this.bidi = bidi;
this.driver = driver;
this.browsingContextId = browsingContextId;
}

Expand All @@ -22,4 +25,67 @@ export class FirefoxBidi {
log.error('Could not inject JavaScript:' + error);
}
}

async setBasicAuth(basicAuth) {
const parts = basicAuth.split('@');
const bidi = new Bidi(this.driver, this.options.browser);

const command = {
method: 'network.addIntercept',
params: {
phases: ['authRequired']
}
};
await bidi.send(command);

await bidi.subscribe('network.authRequired');

await bidi.onMessage(async function (event) {
const parsedEvent = JSON.parse(Buffer.from(event.toString()));
if (parsedEvent.method === 'network.authRequired') {
const continueWithAuth = {
method: 'network.continueWithAuth',
params: {
request: parsedEvent.params.request.request,
action: 'provideCredentials',
credentials: {
type: 'password',
username: parts[0],
password: parts[1]
}
}
};
await bidi.send(continueWithAuth);
}
});
}

async setCookie(url, cookie) {
const cookies = toArray(cookie);
for (let cookieParts of cookies) {
const parts = new Array(
cookieParts.slice(0, cookieParts.indexOf('=')),
cookieParts.slice(cookieParts.indexOf('=') + 1, cookieParts.length)
);

const params = {
method: 'storage.setCookie',
params: {
cookie: {
name: parts[0],
value: {
type: 'string',
value: parts[1]
},
domain: new URL(url).hostname
}
}
};
try {
await this.bidi.send(params);
} catch (error) {
log.error('Could not set cookie:' + error);
}
}
}
}
12 changes: 11 additions & 1 deletion lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export class Firefox {
await this.browsertimeBidi.injectJavaScript(this.options.injectJs);
}

if (this.options.basicAuth) {
await this.browsertimeBidi.setBasicAuth(this.options.basicAuth);
}

if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
Expand All @@ -111,7 +115,7 @@ export class Firefox {
/**
* Before each URL/test runs.
*/
async beforeEachURL(runner) {
async beforeEachURL(runner, url) {
await runner.runPrivilegedScript(`
new Promise(async function(resolve) {
await Services.fog.testFlushAllChildren(); // force any data that wasn't recorded yet to be immediately put in the buffers
Expand Down Expand Up @@ -141,6 +145,12 @@ export class Firefox {
await this.geckoProfiler.start();
}

if (this.options.cookie && url) {
await this.browsertimeBidi.setCookie(url, this.options.cookie);
} else if (this.options.cookie) {
log.info('Could not set cookie because the URL is unknown');
}

if (this.firefoxConfig.perfStats) {
this.perfStats = new PerfStats(runner, this.firefoxConfig);
return this.perfStats.start();
Expand Down

0 comments on commit 20b3775

Please sign in to comment.