UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

238 lines (212 loc) 6.46 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/perf/performance.js import { codes as __codes__ } from "nstdlib/lib/internal/errors"; import { EventTarget, Event, kTrustEvent, initEventTarget, defineEventHandler, } from "nstdlib/lib/internal/event_target"; import { now, getTimeOriginTimestamp } from "nstdlib/lib/internal/perf/utils"; import { markResourceTiming } from "nstdlib/lib/internal/perf/resource_timing"; import { mark, measure, clearMarkTimings, } from "nstdlib/lib/internal/perf/usertiming"; import { clearEntriesFromBuffer, filterBufferMapByNameAndType, setResourceTimingBufferSize, setDispatchBufferFull, } from "nstdlib/lib/internal/perf/observe"; import { eventLoopUtilization } from "nstdlib/lib/internal/perf/event_loop_utilization"; import * as nodeTiming from "nstdlib/lib/internal/perf/nodetiming"; import * as timerify from "nstdlib/lib/internal/perf/timerify"; import { customInspectSymbol as kInspect, kEnumerableProperty, kEmptyObject, } from "nstdlib/lib/internal/util"; import { inspect } from "nstdlib/lib/util"; import { validateInternalField } from "nstdlib/lib/internal/validators"; import { convertToInt } from "nstdlib/lib/internal/webidl"; const { ERR_ILLEGAL_CONSTRUCTOR, ERR_MISSING_ARGS } = __codes__; const kPerformanceBrand = Symbol("performance"); class Performance extends EventTarget { constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } [kInspect](depth, options) { if (depth < 0) return this; const opts = { ...options, depth: options.depth == null ? null : options.depth - 1, }; return `Performance ${inspect( { nodeTiming: this.nodeTiming, timeOrigin: this.timeOrigin, }, opts, )}`; } clearMarks(name = undefined) { validateInternalField(this, kPerformanceBrand, "Performance"); if (name !== undefined) { name = `${name}`; } clearMarkTimings(name); clearEntriesFromBuffer("mark", name); } clearMeasures(name = undefined) { validateInternalField(this, kPerformanceBrand, "Performance"); if (name !== undefined) { name = `${name}`; } clearEntriesFromBuffer("measure", name); } clearResourceTimings(name = undefined) { validateInternalField(this, kPerformanceBrand, "Performance"); if (name !== undefined) { name = `${name}`; } clearEntriesFromBuffer("resource", name); } getEntries() { validateInternalField(this, kPerformanceBrand, "Performance"); return filterBufferMapByNameAndType(); } getEntriesByName(name) { validateInternalField(this, kPerformanceBrand, "Performance"); if (arguments.length === 0) { throw new ERR_MISSING_ARGS("name"); } name = `${name}`; return filterBufferMapByNameAndType(name, undefined); } getEntriesByType(type) { validateInternalField(this, kPerformanceBrand, "Performance"); if (arguments.length === 0) { throw new ERR_MISSING_ARGS("type"); } type = `${type}`; return filterBufferMapByNameAndType(undefined, type); } mark(name, options = kEmptyObject) { validateInternalField(this, kPerformanceBrand, "Performance"); if (arguments.length === 0) { throw new ERR_MISSING_ARGS("name"); } return mark(name, options); } measure(name, startOrMeasureOptions = kEmptyObject, endMark = undefined) { validateInternalField(this, kPerformanceBrand, "Performance"); if (arguments.length === 0) { throw new ERR_MISSING_ARGS("name"); } return measure(name, startOrMeasureOptions, endMark); } now() { validateInternalField(this, kPerformanceBrand, "Performance"); return now(); } setResourceTimingBufferSize(maxSize) { validateInternalField(this, kPerformanceBrand, "Performance"); if (arguments.length === 0) { throw new ERR_MISSING_ARGS("maxSize"); } // unsigned long maxSize = convertToInt("maxSize", maxSize, 32); return setResourceTimingBufferSize(maxSize); } get timeOrigin() { validateInternalField(this, kPerformanceBrand, "Performance"); return getTimeOriginTimestamp(); } toJSON() { validateInternalField(this, kPerformanceBrand, "Performance"); return { nodeTiming: this.nodeTiming, timeOrigin: this.timeOrigin, eventLoopUtilization: this.eventLoopUtilization(), }; } } Object.defineProperties(Performance.prototype, { clearMarks: kEnumerableProperty, clearMeasures: kEnumerableProperty, clearResourceTimings: kEnumerableProperty, getEntries: kEnumerableProperty, getEntriesByName: kEnumerableProperty, getEntriesByType: kEnumerableProperty, mark: kEnumerableProperty, measure: kEnumerableProperty, now: kEnumerableProperty, timeOrigin: kEnumerableProperty, toJSON: kEnumerableProperty, setResourceTimingBufferSize: kEnumerableProperty, [Symbol.toStringTag]: { __proto__: null, writable: false, enumerable: false, configurable: true, value: "Performance", }, // Node.js specific extensions. eventLoopUtilization: { __proto__: null, configurable: true, // Node.js specific extensions. enumerable: false, writable: true, value: eventLoopUtilization, }, nodeTiming: { __proto__: null, configurable: true, // Node.js specific extensions. enumerable: false, writable: true, value: nodeTiming, }, // In the browser, this function is not public. However, it must be used inside fetch // which is a Node.js dependency, not a internal module markResourceTiming: { __proto__: null, configurable: true, // Node.js specific extensions. enumerable: false, writable: true, value: markResourceTiming, }, timerify: { __proto__: null, configurable: true, // Node.js specific extensions. enumerable: false, writable: true, value: timerify, }, }); defineEventHandler(Performance.prototype, "resourcetimingbufferfull"); function createPerformance() { return Reflect.construct( function Performance() { initEventTarget(this); this[kPerformanceBrand] = true; }, [], Performance, ); } const performance = createPerformance(); function dispatchBufferFull(type) { const event = new Event(type, { [kTrustEvent]: true, }); performance.dispatchEvent(event); } setDispatchBufferFull(dispatchBufferFull); export { Performance }; export { performance };