@memlab/api
Version:
125 lines (124 loc) • 4.03 kB
JavaScript
"use strict";
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall memory_lab
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@memlab/core");
const fs_extra_1 = __importDefault(require("fs-extra"));
/**
* A utility entity to read all generated files from
* the directory holding the data and results from
* a memlab run
*/
class BaseResultReader {
/**
* build a result reader
* @param workDir absolute path of the directory where the data
* and generated files of the memlab run were stored
*/
constructor(workDir = '') {
this.fileManager = new core_1.FileManager();
if (workDir === '') {
workDir = this.fileManager.getWorkDir();
}
this.workDir = workDir;
this.check();
}
check() {
this.isValid = fs_extra_1.default.existsSync(this.workDir);
if (!this.isValid) {
core_1.utils.haltOrThrow(`invalid/removed data directory: ${this.workDir}`);
}
}
/**
* internal
* @param workDir
* @returns
*/
static from(workDir = '') {
return new BaseResultReader(workDir);
}
/**
* get the directory where the data and generated files of
* the memlab run were stored
* @returns absolute path of the directory
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* // get the directory that stores all the files
* // generated from the takeSnapshots call
* const dataDir = result.getRootDirectory();
* })();
* ```
*/
getRootDirectory() {
this.check();
return this.workDir;
}
/**
* This method gets the backup file of the console output.
*
* The memlab CLI commands (e.g., `memlab find-leaks`) outputs a
* non-structured string representation for easy reading, while the
* APIs (e.g., <code>{@link findLeaks}</code>) return structured leaks
* representation that is handy for post-processing. If you need to
* obtain all the string output from the CLI in the current working directory,
* you can read them from the CLI output backup file returned by this method.
*
* @returns the absolute path of the backup file
* * **Examples**:
* ```javascript
* const {takeSnapshots, findLeaks} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
* const leaks = await findLeaks(result);
*
* // get the console output backup file
* const consoleBackupFile = result.getConsoleBackupFile();
* })();
* ```
*/
getConsoleBackupFile() {
return this.fileManager.getConsoleBackupFile({ workDir: this.workDir });
}
/**
* clean up data/files generated from the memlab browser interaction run
* @returns no return value
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* // delete all data/files generated by takeSnapshots
* result.cleanup();
* })();
* ```
*/
cleanup() {
if (!this.isValid) {
return;
}
fs_extra_1.default.removeSync(this.workDir);
this.isValid = false;
}
}
exports.default = BaseResultReader;