mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-21 05:40:29 +08:00
182 lines
6.6 KiB
JavaScript
182 lines
6.6 KiB
JavaScript
'use strict';Object.defineProperty(exports,Symbol.toStringTag,{value:'Module'});const index=require('../../_virtual/index2.cjs'),require$$2=require('foxts/noop'),require$$0=require('node:path'),require$$1=require('node:process'),require$$0$1=require('picocolors');var hasRequiredTrace;
|
|
|
|
function requireTrace () {
|
|
if (hasRequiredTrace) return index.__exports;
|
|
hasRequiredTrace = 1;
|
|
(function (exports) {
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
function _export(target, all) {
|
|
for(var name in all)Object.defineProperty(target, name, {
|
|
enumerable: true,
|
|
get: all[name]
|
|
});
|
|
}
|
|
_export(exports, {
|
|
createSpan: function() {
|
|
return createSpan;
|
|
},
|
|
dummySpan: function() {
|
|
return dummySpan;
|
|
},
|
|
printTraceResult: function() {
|
|
return printTraceResult;
|
|
},
|
|
task: function() {
|
|
return task;
|
|
},
|
|
whyIsNodeRunning: function() {
|
|
return whyIsNodeRunning;
|
|
}
|
|
});
|
|
const _noop = require$$2;
|
|
const _nodepath = require$$0;
|
|
const _nodeprocess = /*#__PURE__*/ _interop_require_default(require$$1);
|
|
const _picocolors = /*#__PURE__*/ _interop_require_default(require$$0$1);
|
|
function _interop_require_default(obj) {
|
|
return obj && obj.__esModule ? obj : {
|
|
default: obj
|
|
};
|
|
}
|
|
const SPAN_STATUS_START = 0;
|
|
const SPAN_STATUS_END = 1;
|
|
const spanTag = Symbol('span');
|
|
const rootTraceResult = {
|
|
name: 'root',
|
|
start: 0,
|
|
end: 0,
|
|
children: []
|
|
};
|
|
function createSpan(name, parentTraceResult) {
|
|
const start = performance.now();
|
|
let curTraceResult;
|
|
if (parentTraceResult == null) {
|
|
curTraceResult = rootTraceResult;
|
|
} else {
|
|
curTraceResult = {
|
|
name,
|
|
start,
|
|
end: 0,
|
|
children: []
|
|
};
|
|
parentTraceResult.children.push(curTraceResult);
|
|
}
|
|
let status = SPAN_STATUS_START;
|
|
const stop = (time)=>{
|
|
if (status === SPAN_STATUS_END) {
|
|
throw new Error(`span already stopped: ${name}`);
|
|
}
|
|
const end = time ?? performance.now();
|
|
curTraceResult.end = end;
|
|
status = SPAN_STATUS_END;
|
|
};
|
|
const traceChild = (name)=>createSpan(name, curTraceResult);
|
|
const span = {
|
|
[spanTag]: true,
|
|
stop,
|
|
traceChild,
|
|
traceSyncFn (fn) {
|
|
const res = fn(span);
|
|
span.stop();
|
|
return res;
|
|
},
|
|
async traceAsyncFn (fn) {
|
|
const res = await fn(span);
|
|
span.stop();
|
|
return res;
|
|
},
|
|
traceResult: curTraceResult,
|
|
async tracePromise (promise) {
|
|
const res = await promise;
|
|
span.stop();
|
|
return res;
|
|
},
|
|
traceChildSync: (name, fn)=>traceChild(name).traceSyncFn(fn),
|
|
traceChildAsync: (name, fn)=>traceChild(name).traceAsyncFn(fn),
|
|
traceChildPromise: (name, promise)=>traceChild(name).tracePromise(promise)
|
|
};
|
|
// eslint-disable-next-line sukka/no-redundant-variable -- self reference
|
|
return span;
|
|
}
|
|
const dummySpan = createSpan('');
|
|
function task(importMetaMain, importMetaPath) {
|
|
return (fn, customName)=>{
|
|
const taskName = customName ?? (0, _nodepath.basename)(importMetaPath, (0, _nodepath.extname)(importMetaPath));
|
|
let cleanup = _noop.noop;
|
|
const onCleanup = (cb)=>{
|
|
cleanup = cb;
|
|
};
|
|
const dummySpan = createSpan(taskName);
|
|
if (importMetaMain) {
|
|
_nodeprocess.default.on('uncaughtException', (error)=>{
|
|
console.error('Uncaught exception:', error);
|
|
_nodeprocess.default.exit(1);
|
|
});
|
|
_nodeprocess.default.on('unhandledRejection', (reason)=>{
|
|
console.error('Unhandled rejection:', reason);
|
|
_nodeprocess.default.exit(1);
|
|
});
|
|
dummySpan.traceChildAsync('dummy', (childSpan)=>fn(childSpan, onCleanup)).finally(()=>{
|
|
dummySpan.stop();
|
|
printTraceResult(dummySpan.traceResult);
|
|
_nodeprocess.default.nextTick(whyIsNodeRunning);
|
|
});
|
|
}
|
|
return async (span)=>{
|
|
if (span) {
|
|
return span.traceChildAsync(taskName, (childSpan)=>fn(childSpan, onCleanup).finally(()=>cleanup()));
|
|
}
|
|
return fn(dummySpan, onCleanup).finally(()=>cleanup());
|
|
};
|
|
};
|
|
}
|
|
async function whyIsNodeRunning() {
|
|
const mod = await import('why-is-node-running');
|
|
return mod.default();
|
|
}
|
|
function printTraceResult(traceResult = rootTraceResult) {
|
|
printStats(traceResult.children);
|
|
printTree(traceResult, (node)=>{
|
|
if (node.end - node.start < 0) {
|
|
return node.name;
|
|
}
|
|
return `${node.name} ${_picocolors.default.bold(`${(node.end - node.start).toFixed(3)}ms`)}`;
|
|
});
|
|
}
|
|
function printTree(initialTree, printNode) {
|
|
function printBranch(tree, branch, isGraphHead, isChildOfLastBranch) {
|
|
const children = tree.children;
|
|
let branchHead = '';
|
|
if (!isGraphHead) {
|
|
branchHead = children.length > 0 ? '┬ ' : '─ ';
|
|
}
|
|
const toPrint = printNode(tree, `${branch}${branchHead}`);
|
|
if (typeof toPrint === 'string') {
|
|
console.log(`${branch}${branchHead}${toPrint}`);
|
|
}
|
|
let baseBranch = branch;
|
|
if (!isGraphHead) {
|
|
baseBranch = branch.slice(0, -2) + (isChildOfLastBranch ? ' ' : '│ ');
|
|
}
|
|
const nextBranch = `${baseBranch}├─`;
|
|
const lastBranch = `${baseBranch}└─`;
|
|
children.forEach((child, index)=>{
|
|
const last = children.length - 1 === index;
|
|
printBranch(child, last ? lastBranch : nextBranch, false, last);
|
|
});
|
|
}
|
|
printBranch(initialTree, '', true, false);
|
|
}
|
|
function printStats(stats) {
|
|
const longestTaskName = Math.max(...stats.map((i)=>i.name.length));
|
|
const realStart = Math.min(...stats.map((i)=>i.start));
|
|
const realEnd = Math.max(...stats.map((i)=>i.end));
|
|
const statsStep = (realEnd - realStart) / 120 | 0;
|
|
stats.sort((a, b)=>a.start - b.start).forEach((stat)=>{
|
|
console.log(`[${stat.name}]${' '.repeat(longestTaskName - stat.name.length)}`, ' '.repeat((stat.start - realStart) / statsStep | 0), '='.repeat(Math.max((stat.end - stat.start) / statsStep | 0, 1)));
|
|
});
|
|
}
|
|
} (index.__exports));
|
|
return index.__exports;
|
|
}exports.__require=requireTrace; |