Skip to main content

Package: @memlab/core

Interfaces

Type Aliases

CheckPageLoadCallback: (page: Page) => Promise<boolean>

Callback function to provide if the page is loaded. For concrete example, check out isPageLoaded.

  • Parameters:

    • page: Page | puppeteer's Page object. To import this type, check out Page.
  • Returns: Promise<boolean> | a boolean value, if it returns true, memlab will consider the navigation completes, if it returns false, memlab will keep calling this callback until it returns true. This is an async callback, you can also await and returns true until some async logic is resolved.

  • Source:


A single cookie entry in a Cookies list. The name and value field is mandatory. It is better to also specify the domain field, otherwise MemLab will try to infer domain automatically. The other fields are optional. For concrete use case, please check out cookies.

NameTypeDescription
domain?stringAdd when possible: Defines the domain associated with the cookie
expires?Undefinable<number>Optional: Indicates when the cookie will expire, in Unix time (seconds)
httpOnly?Undefinable<boolean>Optional: Flag to determine if the cookie is accessible only over HTTP
namestringMandatory: Represents the name of the cookie
path?Undefinable<string>Optional: Defines the domain associated with the cookie
sameSite?Undefinable<"Strict" | "Lax">Optional: Determines if a cookie is transmitted with cross-site requests, offering a degree of defense against cross-site request forgery attacks.
secure?Undefinable<boolean>Optional: Flag to indicate if the cookie transmission requires a secure protocol (e.g., HTTPS)
session?Undefinable<boolean>Optional: Flag to check if the cookie is a session cookie
url?Undefinable<string>Optional: Specifies the request-URI linked with the cookie setup. This can influence the cookie's default domain and path
valuestringMandatory: Represents the value assigned to the cookie

Data structure for holding cookies. For concrete use case, please check out cookies.


EdgeIterationCallback: (edge: IHeapEdge) => Optional<{ stop: boolean }> | void

Executes a provided callback once for JavaScript references. For concrete examples, check out forEachReference or forEachReferrer.


InitLeakFilterCallback: (snapshot: IHeapSnapshot, leakedNodeIds: HeapNodeIdSet) => void

Lifecycle function callback that is invoked initially once before calling any leak filter function. For concrete example, check out beforeLeakFilter.


InteractionsCallback: (page: Page, args?: OperationArgs) => Promise<void>

The callback defines browser interactions which are used by memlab to interact with the web app under test. For concrete examples, check out action or back.

  • Parameters:

    • page: Page | the puppeteer Page object, which provides APIs to interact with the web browser. To import this type, check out Page.
    • args?: OperationArgs
  • Returns: Promise<void> | no return value

  • Source:


LeakFilterCallback: (node: IHeapNode, snapshot: IHeapSnapshot, leakedNodeIds: HeapNodeIdSet) => boolean

Callback that can be used to define a logic to filter the leaked objects. The callback is only called for every node allocated but not released from the target interaction in the heap snapshot.

For concrete examples, check out leakFilter.

  • Parameters:
    • node: IHeapNode | the node that is kept alive in the memory in the heap snapshot
    • snapshot: IHeapSnapshot | the snapshot of target interaction
    • leakedNodeIds: HeapNodeIdSet | the set of leaked node ids
  • Returns: boolean | the value indicating whether the given node in the snapshot should be considered as leaked.
  • Examples:
// any node in the heap snapshot that is greater than 1MB
function leakFilter(node, _snapshot, _leakedNodeIds) {
return node.retainedSize > 1000000;
};

Nullable<T>: T | null

Given any type T, return the union type T and null

Type parameters

NameDescription
TThe type that will be made nullable.

Optional<T>: T | null | undefined

Given any type T, return the union type T, null, and undefined.

Type parameters

NameDescription
TThe type that will be made both nullable and undefinable.

Page: PuppeteerPage

This is the puppeteer Page class used by MemLab. The puppeteer Page class instance provides APIs to interact with the web browser.

The puppeteer Page type can be incompatible across different versions. Your local npm-installed puppeteer version may be different from the puppeteer used by MemLab. This may cause some type errors, for example:

import type {Page} from 'puppeteer';
import type {RunOptions} from '@memlab/api';

const runOptions: RunOptions = {
scenario: {
// initial page load url: Google Maps
url: () => {
return "https://www.google.com/maps/@37.386427,-122.0428214,11z";
},
// type error here if your local puppeeter version is different
// from the puppeteer used by MemLab
action: async function (page: Page) {
await page.click('text/Hotels');
},
},
};

To avoid the type error in the code example above, MemLab exports the puppeteer Page type used by MemLab so that your code can import it when necessary:

import type {Page} from '@memlab/core' // import Page type from memlab
import type {RunOptions} from 'memlab';

const runOptions: RunOptions = {
scenario: {
// initial page load url: Google Maps
url: () => {
return "https://www.google.com/maps/@37.386427,-122.0428214,11z";
},
// no type error here
action: async function (page: Page) {
await page.click('text/Hotels');
},
},
};

Predicator<T>: (entity: T) => boolean

Type parameters

NameDescription
Tthe type of the entity to be checked

the predicate callback is used to decide if a entity of type T. For more concrete examples on where it is used, check out findAnyReference, findAnyReferrer, and findReferrers.

  • Parameters:

    • entity: T | the entity to be checked
  • Returns: boolean | whether the entity passes the predicate check

  • Source:


ReferenceFilterCallback: (edge: IHeapEdge, snapshot: IHeapSnapshot, isReferenceUsedByDefault: boolean) => boolean

Callback that can be used to define a logic to decide whether a reference should be filtered (included) for some calculations (e.g., retainer trace calculation)

For concrete examples, check out leakFilter.

  • Parameters:
    • edge: IHeapEdge | the reference (edge) that is considered for calcualting the retainer trace
    • snapshot: IHeapSnapshot | the final snapshot taken after all browser interactions are done.
    • isReferenceUsedByDefault: boolean | MemLab has its own default logic for whether a reference should be filtered (included), if this parameter is true, it means MemLab will consider this reference for inclusion
  • Returns: boolean | the value indicating whether the given reference should be filtered (i.e., included)

Please also be aware that some edges like self-referencing edges, JS engine's internal edges, and hidden edges should not be considered as part of the retainer trace. These edges could make the retainer trace unncessarily complex and cause confusion. isReferenceUsedByDefault will be false for these types of edges.

  • Examples:
// exclude react fiber references
function retainerReferenceFilter(edge, _snapshot, _isReferenceUsedByDefault) {
if (edge.name_or_index.toString().startsWith('__reactFiber$')) {
return false;
}
// exclude other references here
// ...
return true;
};

RunMetaInfo: Object

This data structure holds the information about memlab run. You can retrieve the instance of this type through getRunMetaInfo.

NameTypeDescription
browserInfoIBrowserInfoinput configuration for the browser and output data from the browser
typestringtype of the memlab run

Undefinable<T>: T | undefined

Given any type T, return the union type T and undefined.

Type parameters

NameDescription
TThe type that will be made undefinable.

Functions

dumpNodeHeapSnapshot()

Take a heap snapshot of the current program state and save it as a .heapsnapshot file under a randomly generated folder inside the system's temp folder.

Note: All .heapsnapshot files could also be loaded by Chrome DevTools.

  • Returns: string | the absolute file path to the saved .heapsnapshot file.

  • Examples:

import type {IHeapSnapshot} from '@memlab/core';
import {dumpNodeHeapSnapshot} from '@memlab/core';
import {getFullHeapFromFile} from '@memlab/heap-analysis';

(async function () {
const heapFile = dumpNodeHeapSnapshot();
const heap: IHeapSnapshot = await getFullHeapFromFile(heapFile);
})();

tagObject<T>(o, tag)

Tags a string marker to an object instance, which can later be checked by hasObjectWithTag. This API does not modify the object instance in any way (e.g., no additional or hidden properties added to the tagged object).

Type parameters

NameType
Textends object
  • Parameters:
    • o: T | specify the object instance you want to tag, you cannot tag a primitive.
    • tag: string | marker name to tag on the object instance
  • Returns: T | returns the tagged object instance (same reference as the input argument o)
  • Examples:
import type {IHeapSnapshot, AnyValue} from '@memlab/core';
import {config, takeNodeMinimalHeap, tagObject} from '@memlab/core';

test('memory test', async () => {
config.muteConsole = true;
const o1: AnyValue = {};
let o2: AnyValue = {};

// tag o1 with marker: "memlab-mark-1", does not modify o1 in any way
tagObject(o1, 'memlab-mark-1');
// tag o2 with marker: "memlab-mark-2", does not modify o2 in any way
tagObject(o2, 'memlab-mark-2');

o2 = null;

const heap: IHeapSnapshot = await takeNodeMinimalHeap();

// expect object with marker "memlab-mark-1" exists
expect(heap.hasObjectWithTag('memlab-mark-1')).toBe(true);

// expect object with marker "memlab-mark-2" can be GCed
expect(heap.hasObjectWithTag('memlab-mark-2')).toBe(false);

}, 30000);

takeNodeMinimalHeap()

Take a heap snapshot of the current program state and parse it as IHeapSnapshot. Notice that this API does not calculate some heap analysis meta data for heap analysis. But this also means faster heap parsing.

  • Returns: Promise<IHeapSnapshot> | heap representation without heap analysis meta data.

  • Examples:

import type {IHeapSnapshot} from '@memlab/core';
import {takeNodeMinimalHeap} from '@memlab/core';

(async function () {
const heap: IHeapSnapshot = await takeNodeMinimalHeap();
})();

If you need to get the heap snapshot with heap analysis meta data, please use getFullHeapFromFile.

  • Interfaces
  • Type Aliases
    • CheckPageLoadCallback: (page: Page) => Promise<boolean>
    • Cookie: Object
    • Cookies: Cookie[]
    • EdgeIterationCallback: (edge: IHeapEdge) => Optional<{ stop: boolean }> | void
    • InitLeakFilterCallback: (snapshot: IHeapSnapshot, leakedNodeIds: HeapNodeIdSet) => void
    • InteractionsCallback: (page: Page, args?: OperationArgs) => Promise<void>
    • LeakFilterCallback: (node: IHeapNode, snapshot: IHeapSnapshot, leakedNodeIds: HeapNodeIdSet) => boolean
    • Nullable<T>: T | null
    • Optional<T>: T | null | undefined
    • Page: PuppeteerPage
    • Predicator<T>: (entity: T) => boolean
    • ReferenceFilterCallback: (edge: IHeapEdge, snapshot: IHeapSnapshot, isReferenceUsedByDefault: boolean) => boolean
    • RunMetaInfo: Object
    • Undefinable<T>: T | undefined
  • Functions
    • dumpNodeHeapSnapshot()
    • tagObject<T>(o, tag)
    • takeNodeMinimalHeap()