@deepkit/framework
Version:
172 lines • 8.26 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileStopwatchStore = void 0;
const __ΩPick = ['T', 'K', 'Pick', 'l+e#!e"!fRb!b"Pde""N#!w#y'];
/*@ts-ignore*/
const { __ΩFrameStart } = require('@deepkit/stopwatch');
/*@ts-ignore*/
const { __ΩFrameEnd } = require('@deepkit/stopwatch');
function __assignType(fn, args) {
fn.__type = args;
return fn;
}
const stopwatch_1 = require("@deepkit/stopwatch");
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const framework_debug_api_1 = require("@deepkit/framework-debug-api");
const core_1 = require("@deepkit/core");
const module_config_js_1 = require("../../module.config.js");
const zone_js_1 = require("../../zone.js");
const cluster_1 = __importDefault(require("cluster"));
const perf_hooks_1 = require("perf_hooks");
const broker_js_1 = require("../broker.js");
const broker_1 = require("@deepkit/broker");
const logger_1 = require("@deepkit/logger");
class FileStopwatchStore extends stopwatch_1.StopwatchStore {
constructor(config, broker, logger) {
super();
this.config = config;
this.broker = broker;
this.logger = logger;
this.syncMutex = new core_1.Mutex;
this.lastId = -1;
this.lastContext = -1;
this.framesPath = (0, path_1.join)(this.config.varPath, this.config.debugStorePath, 'frames.bin');
this.framesDataPath = (0, path_1.join)(this.config.varPath, this.config.debugStorePath, 'frames-data.bin');
this.analyticsPath = (0, path_1.join)(this.config.varPath, this.config.debugStorePath, 'analytics.bin');
this.ended = false;
this.folderCreated = false;
this.frameChannel = (broker.channel.Ω = [['W']], broker.channel('_debug/frames'));
this.frameDataChannel = (broker.channel.Ω = [['W']], broker.channel('_debug/frames-data'));
}
removeAll() {
// truncate all files
for (const file of [this.framesPath, this.framesDataPath, this.analyticsPath]) {
try {
(0, fs_1.unlinkSync)(file);
}
catch {
}
}
this.lastId = -1;
this.lastContext = -1;
}
async close() {
//last sync, then stop everything
try {
// close() is called onAppExecuted so it must not throw
await this.syncNow();
}
catch (error) {
this.logger.error('Could not sync debug store', (0, core_1.formatError)(error));
}
this.ended = true;
}
run(data, cb) {
return zone_js_1.Zone.run(data, cb);
}
getZone() {
return zone_js_1.Zone.current();
}
add(frame) {
const [id, worker] = (0, stopwatch_1.decodeCompoundKey)(frame.cid);
frame.cid = (0, stopwatch_1.encodeCompoundKey)(id, cluster_1.default.isWorker ? cluster_1.default.worker.id : 0);
frame.timestamp = Math.floor(perf_hooks_1.performance.timeOrigin * 1000 + perf_hooks_1.performance.now() * 1000);
super.add(frame);
}
async loadLastNumberRange() {
if (this.lastId >= 0)
return;
if ((0, fs_1.existsSync)(this.framesPath)) {
const data = (0, fs_1.readFileSync)(this.framesPath);
if (data.byteLength === 0) {
this.lastId = 0;
this.lastContext = 0;
return;
}
let last;
(0, framework_debug_api_1.decodeFrames)(data, __assignType((frame) => {
if (frame.type === stopwatch_1.FrameType.start) {
last = frame;
}
}, ['frame', '', 'P"2!"/"']));
if (last) {
this.lastId = (0, stopwatch_1.decodeCompoundKey)(last.cid)[0];
this.lastContext = last.context;
}
}
else {
this.lastId = 0;
}
}
sync() {
if (this.lastSync)
return;
this.lastSync = setTimeout(() => this.syncNow(), 250);
}
ensureVarDebugFolder() {
if (this.folderCreated)
return;
(0, fs_1.mkdirSync)((0, path_1.join)(this.config.varPath, this.config.debugStorePath), { recursive: true });
this.folderCreated = true;
}
async syncNow() {
if (this.ended)
return;
await this.syncMutex.lock();
try {
await this.loadLastNumberRange();
const frames = this.frameQueue.slice();
const frameData = this.dataQueue.slice();
const analytics = this.analytics.slice();
this.frameQueue = [];
this.dataQueue = [];
this.analytics = [];
for (const frame of frames) {
frame.cid = (0, stopwatch_1.incrementCompoundKey)(frame.cid, this.lastId);
if (frame.type === stopwatch_1.FrameType.start)
frame.context += this.lastContext;
}
for (const frame of frameData) {
frame.cid = (0, stopwatch_1.incrementCompoundKey)(frame.cid, this.lastId);
}
const frameBytes = (0, framework_debug_api_1.encodeFrames)(frames);
const dataBytes = (0, framework_debug_api_1.encodeFrameData)(frameData);
const analyticsBytes = (0, framework_debug_api_1.encodeAnalytic)(analytics);
try {
this.ensureVarDebugFolder();
if (frameBytes.byteLength)
await (0, promises_1.appendFile)(this.framesPath, frameBytes);
if (dataBytes.byteLength)
await (0, promises_1.appendFile)(this.framesDataPath, dataBytes);
if (analyticsBytes.byteLength)
await (0, promises_1.appendFile)(this.analyticsPath, analyticsBytes);
}
catch (error) {
this.logger.error('Could not write to debug store', String(error));
}
if (!this.ended) {
//when we ended, broker connection already closed. So we just write to disk.
if (frameBytes.byteLength && this.frameChannel)
await this.frameChannel.publish(frameBytes);
if (dataBytes.byteLength && this.frameDataChannel)
await this.frameDataChannel.publish(dataBytes);
}
this.lastSync = undefined;
const more = this.dataQueue.length || this.frameQueue.length || this.analytics.length;
if (more) {
this.sync();
}
}
finally {
this.syncMutex.unlock();
}
}
}
exports.FileStopwatchStore = FileStopwatchStore;
FileStopwatchStore.__type = [() => stopwatch_1.StopwatchStore, 'lastSync', 'syncMutex', function () { return new core_1.Mutex; }, 'lastId', function () { return -1; }, 'lastContext', function () { return -1; }, () => broker_1.BrokerBusChannel, 'frameChannel', () => broker_1.BrokerBusChannel, 'frameDataChannel', 'framesPath', function () { return (0, path_1.join)(this.config.varPath, this.config.debugStorePath, 'frames.bin'); }, 'framesDataPath', function () { return (0, path_1.join)(this.config.varPath, this.config.debugStorePath, 'frames-data.bin'); }, 'analyticsPath', function () { return (0, path_1.join)(this.config.varPath, this.config.debugStorePath, 'analytics.bin'); }, 'ended', function () { return false; }, 'folderCreated', function () { return false; }, () => __ΩPick, () => module_config_js_1.FrameworkConfig, "varPath", "debugStorePath", 'config', () => broker_js_1.DebugBrokerBus, 'broker', () => logger_1.Logger, 'logger', 'constructor', 'removeAll', 'close', 'data', '', 'cb', 'run', 'getZone', () => __ΩFrameStart, () => __ΩFrameEnd, 'frame', 'add', 'loadLastNumberRange', 'sync', 'ensureVarDebugFolder', 'syncNow', 'FileStopwatchStore', 'P7!"3"8<!3#<>$\'3%<>&\'3\'<>(PPW7)-J3*PPW7+-J3,&3-<>.&3/<>0&31<>2)33<>4)35<>6PP78P.9.:Jo7#2;<P7<2=<P7>2?<"0@P"0AP"0BPP&"LM2CP"`/D2E"`0FPPP&"LM-J0GPPnHnIJ2J$0KP"0L<P"0M<P"0N<P"0O<5wP'];
//# sourceMappingURL=store.js.map