Skip to content

Commit

Permalink
feat(api): Support setting Chromium binary in API (#111)
Browse files Browse the repository at this point in the history
Summary:
Support setting the Chromium binary in MemLab programming API:

```
const {findLeaks, takeSnapshots} = require('memlab/api');

(async function () {
  const scenario = {
    url: () => 'https://www.facebook.com',
  };
  const result = await takeSnapshots({scenario, chromiumBinary: binaryPath});
  const leaks = findLeaks(result);
})();
```

Differential Revision: D53386483

fbshipit-source-id: 5bd0d3ce0cd839a0a4ae3186059d99c9cfc3275f
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Feb 6, 2024
1 parent d402370 commit 6de17a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
4 changes: 4 additions & 0 deletions packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export type RunOptions = {
* For more details. please check out {@link ConsoleMode}
*/
consoleMode?: ConsoleMode;
/**
* specify the Chromium binary for E2E run
*/
chromiumBinary?: string;
};

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/api/src/state/PuppeteerConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
* @oncall web_perf_infra
*/

import {MemLabConfig, PuppeteerConfig} from '@memlab/core';
import {RunOptions} from '../API';
import type {MemLabConfig, PuppeteerConfig} from '@memlab/core';
import type {RunOptions} from '../API';

import {utils} from '@memlab/core';

/**
* Manage, save, and restore the current state of the PuppeteerConfig.
Expand All @@ -20,6 +22,9 @@ class PuppeteerStateManager {
config.puppeteerConfig = {...config.puppeteerConfig};
config.externalCookiesFile = options.cookiesFile;
config.scenario = options.scenario;
if (options.chromiumBinary != null) {
utils.setChromiumBinary(config, options.chromiumBinary);
}
return existing;
}

Expand Down
15 changes: 2 additions & 13 deletions packages/cli/src/options/e2e/SetChromiumBinaryOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import type {ParsedArgs} from 'minimist';
import {MemLabConfig} from '@memlab/core';

import path from 'path';
import fs from 'fs';
import {info, utils, BaseOption} from '@memlab/core';
import {utils, BaseOption} from '@memlab/core';
import optionConstants from '../lib/OptionConstant';

export default class SetChromiumBinaryOption extends BaseOption {
Expand All @@ -28,16 +26,7 @@ export default class SetChromiumBinaryOption extends BaseOption {
const name = this.getOptionName();
const arg = args[name];
if (arg) {
const binaryPath = path.resolve(process.cwd(), arg);
if (!fs.existsSync(binaryPath)) {
throw utils.haltOrThrow(
`Chromium binary does not exist: ${binaryPath}`,
);
}
if (config.verbose) {
info.lowLevel(`Using ${binaryPath} as Chromium binary for E2E run`);
}
config.puppeteerConfig.executablePath = binaryPath;
utils.setChromiumBinary(config, arg);
}
}
}
16 changes: 15 additions & 1 deletion packages/core/src/lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fs from 'fs';
import path from 'path';
import cp from 'child_process';
import process from 'process';
import config, {ErrorHandling} from './Config';
import config, {ErrorHandling, MemLabConfig} from './Config';
import info from './Console';
import constant from './Constant';
import parser from './HeapParser';
Expand Down Expand Up @@ -2125,6 +2125,19 @@ function isStandardNumberToString(input: string): boolean {
return parseInt(input, 10).toString() === input;
}

function setChromiumBinary(config: MemLabConfig, chromiumBinary: string) {
const binaryPath = path.isAbsolute(chromiumBinary)
? path.resolve(chromiumBinary)
: path.resolve(process.cwd(), chromiumBinary);
if (!fs.existsSync(binaryPath)) {
throw utils.haltOrThrow(`Chromium binary does not exist: ${binaryPath}`);
}
if (config.verbose) {
info.lowLevel(`Using ${binaryPath} as Chromium binary for E2E run`);
}
config.puppeteerConfig.executablePath = binaryPath;
}

export default {
aggregateDominatorMetrics,
applyToNodes,
Expand Down Expand Up @@ -2232,6 +2245,7 @@ export default {
resolveFilePath,
resolveSnapshotFilePath,
runShell,
setChromiumBinary,
setIsAlternateNode,
setIsRegularFiberNode,
shouldShowMoreInfo,
Expand Down
15 changes: 8 additions & 7 deletions website/docs/api/modules/api_src.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Options for configuring browser interaction run, all fields are optional

| Name | Type | Description |
| :------ | :------ | :------ |
| `chromiumBinary?` | `string` | specify the Chromium binary for E2E run |
| `consoleMode?` | [`ConsoleMode`](../enums/api_src.ConsoleMode.md) | specifying the terminal output mode, default is `default`. For more details. please check out [ConsoleMode](../enums/api_src.ConsoleMode.md) |
| `cookiesFile?` | `string` | the absolute path of cookies file |
| `evalInBrowserAfterInitLoad?` | `AnyFunction` | function to be evaluated in browser context after the web page initial load |
Expand All @@ -47,7 +48,7 @@ A data structure holding the result of the [run](api_src.md#run) API call.
| `runResult` | [`BrowserInteractionResultReader`](../classes/api_src.BrowserInteractionResultReader.md) | a utility for reading browser interaction results from disk |

* **Source**:
* api/src/API.ts:97
* api/src/API.ts:101

## Functions

Expand Down Expand Up @@ -78,7 +79,7 @@ const {analyze, takeSnapshots, StringAnalysis} = require('@memlab/api');
```

* **Source**:
* api/src/API.ts:317
* api/src/API.ts:321

___

Expand Down Expand Up @@ -106,7 +107,7 @@ const {findLeaks, takeSnapshots} = require('@memlab/api');
```

* **Source**:
* api/src/API.ts:245
* api/src/API.ts:249

___

Expand All @@ -126,7 +127,7 @@ the `--baseline`, `--target`, and `--final` flags in CLI.
* **Returns**: `Promise`<`ISerializedInfo`[]\> | leak traces detected and clustered from the browser interaction

* **Source**:
* api/src/API.ts:273
* api/src/API.ts:277

___

Expand Down Expand Up @@ -154,7 +155,7 @@ const {run} = require('@memlab/api');
```

* **Source**:
* api/src/API.ts:177
* api/src/API.ts:181

___

Expand All @@ -179,7 +180,7 @@ const {takeSnapshots} = require('@memlab/api');
```

* **Source**:
* api/src/API.ts:210
* api/src/API.ts:214

___

Expand All @@ -205,4 +206,4 @@ const {warmupAndTakeSnapshots} = require('@memlab/api');
```

* **Source**:
* api/src/API.ts:140
* api/src/API.ts:144

0 comments on commit 6de17a7

Please sign in to comment.