nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
87 lines (76 loc) • 2.55 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/heap_utils.js
import {
kUpdateTimer,
onStreamRead,
} from "nstdlib/lib/internal/stream_base_commons";
import { symbols as __symbols__ } from "nstdlib/lib/internal/async_hooks";
import { Readable } from "nstdlib/lib/stream";
import {
validateObject,
validateBoolean,
validateFunction,
} from "nstdlib/lib/internal/validators";
import { codes as __codes__ } from "nstdlib/lib/internal/errors";
import {
kEmptyObject,
emitExperimentalWarning,
} from "nstdlib/lib/internal/util";
import { queryObjects as _queryObjects } from "nstdlib/stub/binding/internal_only_v8";
import { inspect } from "nstdlib/lib/internal/util/inspect";
const { owner_symbol } = __symbols__;
const { ERR_INVALID_ARG_VALUE } = __codes__;
const kHandle = Symbol("kHandle");
function getHeapSnapshotOptions(options = kEmptyObject) {
validateObject(options, "options");
const { exposeInternals = false, exposeNumericValues = false } = options;
validateBoolean(exposeInternals, "options.exposeInternals");
validateBoolean(exposeNumericValues, "options.exposeNumericValues");
return new Uint8Array([+exposeInternals, +exposeNumericValues]);
}
class HeapSnapshotStream extends Readable {
constructor(handle) {
super({ autoDestroy: true });
this[kHandle] = handle;
handle[owner_symbol] = this;
handle.onread = onStreamRead;
}
_read() {
if (this[kHandle]) this[kHandle].readStart();
}
_destroy() {
// Release the references on the handle so that
// it can be garbage collected.
this[kHandle][owner_symbol] = undefined;
this[kHandle] = undefined;
}
[kUpdateTimer]() {
// Does nothing
}
}
const inspectOptions = {
__proto__: null,
depth: 0,
};
function queryObjects(ctor, options = kEmptyObject) {
validateFunction(ctor, "constructor");
if (options !== kEmptyObject) {
validateObject(options, "options");
}
const format = options.format || "count";
if (format !== "count" && format !== "summary") {
throw new ERR_INVALID_ARG_VALUE("options.format", format);
}
emitExperimentalWarning("v8.queryObjects()");
// Matching the console API behavior - just access the .prototype.
const objects = _queryObjects(ctor.prototype);
if (format === "count") {
return objects.length;
}
// options.format is 'summary'.
return Array.prototype.map.call(objects, (object) =>
inspect(object, inspectOptions),
);
}
export { getHeapSnapshotOptions };
export { HeapSnapshotStream };
export { queryObjects };