@matterbridge/utils 3.9.3-dev-20260629-7c47082 → 3.9.3-dev-20260630-684e0d2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/export.d.ts +1 -1
- package/dist/export.js +1 -1
- package/dist/npmPrefix.js +1 -1
- package/dist/runtimeBun.d.ts +22 -0
- package/dist/runtimeBun.js +63 -0
- package/dist/tracker.d.ts +2 -1
- package/dist/tracker.js +24 -5
- package/package.json +3 -3
- package/dist/bunPrefix.d.ts +0 -2
- package/dist/bunPrefix.js +0 -14
package/dist/export.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export * from './bunPrefix.js';
|
|
2
1
|
export * from './colorUtils.js';
|
|
3
2
|
export * from './commandLine.js';
|
|
4
3
|
export * from './copyDirectory.js';
|
|
@@ -15,6 +14,7 @@ export * from './loader.js';
|
|
|
15
14
|
export * from './network.js';
|
|
16
15
|
export * from './npmPrefix.js';
|
|
17
16
|
export * from './npmVersion.js';
|
|
17
|
+
export * from './runtimeBun.js';
|
|
18
18
|
export * from './tracker.js';
|
|
19
19
|
export * from './validate.js';
|
|
20
20
|
export * from './wait.js';
|
package/dist/export.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export * from './bunPrefix.js';
|
|
2
1
|
export * from './colorUtils.js';
|
|
3
2
|
export * from './commandLine.js';
|
|
4
3
|
export * from './copyDirectory.js';
|
|
@@ -15,6 +14,7 @@ export * from './loader.js';
|
|
|
15
14
|
export * from './network.js';
|
|
16
15
|
export * from './npmPrefix.js';
|
|
17
16
|
export * from './npmVersion.js';
|
|
17
|
+
export * from './runtimeBun.js';
|
|
18
18
|
export * from './tracker.js';
|
|
19
19
|
export * from './validate.js';
|
|
20
20
|
export * from './wait.js';
|
package/dist/npmPrefix.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare function isBun(): boolean;
|
|
2
|
+
export declare function getBunVersion(): string | undefined;
|
|
3
|
+
export declare function getBunRevision(): string | undefined;
|
|
4
|
+
export declare function getNodeCompatVersion(): string | undefined;
|
|
5
|
+
export declare function getBun(): typeof Bun | undefined;
|
|
6
|
+
export declare function getGlobalBunModules(): string;
|
|
7
|
+
export declare function nanoseconds(): number;
|
|
8
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
9
|
+
export declare function which(command: string): string | null;
|
|
10
|
+
export declare function gc(force?: boolean): void;
|
|
11
|
+
export declare function setGcLevel(level?: 0 | 1 | 2): 0 | 1 | 2 | undefined;
|
|
12
|
+
export interface BunRuntimeInfo {
|
|
13
|
+
isBun: boolean;
|
|
14
|
+
version?: string;
|
|
15
|
+
revision?: string;
|
|
16
|
+
nodeCompat?: string;
|
|
17
|
+
execPath: string;
|
|
18
|
+
main?: string;
|
|
19
|
+
arch: string;
|
|
20
|
+
platform: string;
|
|
21
|
+
}
|
|
22
|
+
export declare function getBunRuntimeInfo(): BunRuntimeInfo;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { logModuleLoaded } from './loader.js';
|
|
4
|
+
logModuleLoaded('RuntimeBun');
|
|
5
|
+
const HAS_BUN_GLOBAL = typeof Bun !== 'undefined';
|
|
6
|
+
export function isBun() {
|
|
7
|
+
return HAS_BUN_GLOBAL || typeof process?.versions?.bun === 'string';
|
|
8
|
+
}
|
|
9
|
+
export function getBunVersion() {
|
|
10
|
+
if (HAS_BUN_GLOBAL)
|
|
11
|
+
return Bun.version;
|
|
12
|
+
return process?.versions?.bun;
|
|
13
|
+
}
|
|
14
|
+
export function getBunRevision() {
|
|
15
|
+
return HAS_BUN_GLOBAL ? Bun.revision : undefined;
|
|
16
|
+
}
|
|
17
|
+
export function getNodeCompatVersion() {
|
|
18
|
+
return process?.versions?.node;
|
|
19
|
+
}
|
|
20
|
+
export function getBun() {
|
|
21
|
+
return HAS_BUN_GLOBAL ? Bun : undefined;
|
|
22
|
+
}
|
|
23
|
+
export function getGlobalBunModules() {
|
|
24
|
+
if (!isBun()) {
|
|
25
|
+
throw new Error('getGlobalBunModules can only be called in a Bun environment.');
|
|
26
|
+
}
|
|
27
|
+
const bunInstall = process.env.BUN_INSTALL ?? path.join(os.homedir(), '.bun');
|
|
28
|
+
return path.join(bunInstall, 'install', 'global', 'node_modules');
|
|
29
|
+
}
|
|
30
|
+
export function nanoseconds() {
|
|
31
|
+
if (HAS_BUN_GLOBAL)
|
|
32
|
+
return Bun.nanoseconds();
|
|
33
|
+
return Number(process.hrtime.bigint());
|
|
34
|
+
}
|
|
35
|
+
export async function sleep(ms) {
|
|
36
|
+
if (HAS_BUN_GLOBAL)
|
|
37
|
+
return Bun.sleep(ms);
|
|
38
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
39
|
+
}
|
|
40
|
+
export function which(command) {
|
|
41
|
+
return HAS_BUN_GLOBAL ? Bun.which(command) : null;
|
|
42
|
+
}
|
|
43
|
+
export function gc(force = true) {
|
|
44
|
+
if (HAS_BUN_GLOBAL) {
|
|
45
|
+
Bun.gc(force);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function setGcLevel(level) {
|
|
49
|
+
return HAS_BUN_GLOBAL ? Bun.unsafe.gcAggressionLevel(level) : undefined;
|
|
50
|
+
}
|
|
51
|
+
export function getBunRuntimeInfo() {
|
|
52
|
+
const bun = getBun();
|
|
53
|
+
return {
|
|
54
|
+
isBun: isBun(),
|
|
55
|
+
version: getBunVersion(),
|
|
56
|
+
revision: getBunRevision(),
|
|
57
|
+
nodeCompat: getNodeCompatVersion(),
|
|
58
|
+
execPath: process.execPath,
|
|
59
|
+
main: bun?.main ?? process.argv[1],
|
|
60
|
+
arch: process.arch,
|
|
61
|
+
platform: process.platform,
|
|
62
|
+
};
|
|
63
|
+
}
|
package/dist/tracker.d.ts
CHANGED
|
@@ -37,6 +37,7 @@ export declare class Tracker extends EventEmitter<TrackerEvents> {
|
|
|
37
37
|
private readonly debug;
|
|
38
38
|
private readonly verbose;
|
|
39
39
|
private readonly tracker;
|
|
40
|
+
private readonly forceGc;
|
|
40
41
|
private trackerInterval?;
|
|
41
42
|
static historyIndex: number;
|
|
42
43
|
static readonly historySize = 2880;
|
|
@@ -44,7 +45,7 @@ export declare class Tracker extends EventEmitter<TrackerEvents> {
|
|
|
44
45
|
private prevCpus;
|
|
45
46
|
private prevCpuUsage;
|
|
46
47
|
private log;
|
|
47
|
-
constructor(name?: string, debug?: boolean, verbose?: boolean, tracker?: boolean);
|
|
48
|
+
constructor(name?: string, debug?: boolean, verbose?: boolean, tracker?: boolean, forceGc?: boolean);
|
|
48
49
|
start(sampleIntervalMs?: number): void;
|
|
49
50
|
resetPeaks(): void;
|
|
50
51
|
runGarbageCollector(type?: 'major' | 'minor', execution?: 'sync' | 'async'): void;
|
package/dist/tracker.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import EventEmitter from 'node:events';
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import { AnsiLogger, BRIGHT, CYAN, db, RED, RESET, YELLOW } from 'node-ansi-logger';
|
|
4
|
-
import { hasParameter } from './commandLine.js';
|
|
5
4
|
import { formatBytes, formatPercent, formatTimeStamp } from './format.js';
|
|
6
5
|
import { logModuleLoaded } from './loader.js';
|
|
6
|
+
import { isBun, gc, setGcLevel } from './runtimeBun.js';
|
|
7
7
|
logModuleLoaded('Tracker');
|
|
8
8
|
export class Tracker extends EventEmitter {
|
|
9
9
|
name;
|
|
10
10
|
debug;
|
|
11
11
|
verbose;
|
|
12
12
|
tracker;
|
|
13
|
+
forceGc;
|
|
13
14
|
trackerInterval;
|
|
14
15
|
static historyIndex = 0;
|
|
15
16
|
static historySize = 2880;
|
|
@@ -37,12 +38,13 @@ export class Tracker extends EventEmitter {
|
|
|
37
38
|
prevCpus = os.cpus();
|
|
38
39
|
prevCpuUsage = process.cpuUsage();
|
|
39
40
|
log;
|
|
40
|
-
constructor(name = 'Tracker', debug = false, verbose = false, tracker = false) {
|
|
41
|
+
constructor(name = 'Tracker', debug = false, verbose = false, tracker = false, forceGc = false) {
|
|
41
42
|
super();
|
|
42
43
|
this.name = name;
|
|
43
44
|
this.debug = debug;
|
|
44
45
|
this.verbose = verbose;
|
|
45
46
|
this.tracker = tracker;
|
|
47
|
+
this.forceGc = forceGc;
|
|
46
48
|
if (process.argv.includes('--debug') || process.argv.includes('--verbose') || process.argv.includes('--tracker')) {
|
|
47
49
|
this.debug = true;
|
|
48
50
|
}
|
|
@@ -52,6 +54,9 @@ export class Tracker extends EventEmitter {
|
|
|
52
54
|
if (process.argv.includes('--tracker')) {
|
|
53
55
|
this.tracker = true;
|
|
54
56
|
}
|
|
57
|
+
if (process.argv.includes('--force-gc')) {
|
|
58
|
+
this.forceGc = true;
|
|
59
|
+
}
|
|
55
60
|
this.log = new AnsiLogger({ logName: name, logTimestampFormat: 4, logLevel: this.debug || this.tracker ? "debug" : "info" });
|
|
56
61
|
this.log.logNameColor = YELLOW;
|
|
57
62
|
if (this.verbose) {
|
|
@@ -81,7 +86,7 @@ export class Tracker extends EventEmitter {
|
|
|
81
86
|
this.prevCpuUsage = process.cpuUsage();
|
|
82
87
|
this.trackerInterval = setInterval(() => {
|
|
83
88
|
tryGcCount += sampleIntervalMs / 1000;
|
|
84
|
-
if (tryGcCount > 60 * 60 ||
|
|
89
|
+
if (tryGcCount > 60 * 60 || this.forceGc) {
|
|
85
90
|
this.runGarbageCollector();
|
|
86
91
|
tryGcCount = 0;
|
|
87
92
|
}
|
|
@@ -131,7 +136,7 @@ export class Tracker extends EventEmitter {
|
|
|
131
136
|
entry.peakArrayBuffers = Math.max(prevEntry.peakArrayBuffers, mem.arrayBuffers);
|
|
132
137
|
this.emit('memory', entry.freeMemory, entry.totalMemory, entry.rss, entry.heapUsed, entry.heapTotal, entry.external, entry.arrayBuffers);
|
|
133
138
|
this.emit('snapshot', entry);
|
|
134
|
-
if (this.debug
|
|
139
|
+
if (this.debug) {
|
|
135
140
|
this.log.debug(`Time: ${formatTimeStamp(entry.timestamp)} ` +
|
|
136
141
|
`os ${CYAN}${BRIGHT}${formatPercent(entry.osCpu)}${RESET}${db} (${entry.peakOsCpu > prevEntry.peakOsCpu ? RED : ''}${formatPercent(entry.peakOsCpu)}${db}) ` +
|
|
137
142
|
`process ${CYAN}${BRIGHT}${formatPercent(entry.processCpu)}${RESET}${db} (${entry.peakProcessCpu > prevEntry.peakProcessCpu ? RED : ''}${formatPercent(entry.peakProcessCpu)}${db}) ` +
|
|
@@ -159,7 +164,8 @@ export class Tracker extends EventEmitter {
|
|
|
159
164
|
this.emit('reset_peaks_done');
|
|
160
165
|
}
|
|
161
166
|
runGarbageCollector(type = 'major', execution = 'async') {
|
|
162
|
-
|
|
167
|
+
const bun = isBun();
|
|
168
|
+
if (!bun && global.gc && typeof global.gc === 'function') {
|
|
163
169
|
try {
|
|
164
170
|
global.gc({ type, execution });
|
|
165
171
|
if (this.debug)
|
|
@@ -173,6 +179,19 @@ export class Tracker extends EventEmitter {
|
|
|
173
179
|
this.emit('gc_done', 'minor', 'async');
|
|
174
180
|
}
|
|
175
181
|
}
|
|
182
|
+
else if (bun && typeof gc === 'function' && typeof setGcLevel === 'function') {
|
|
183
|
+
try {
|
|
184
|
+
setGcLevel(2);
|
|
185
|
+
gc(execution === 'sync');
|
|
186
|
+
if (this.debug)
|
|
187
|
+
this.log.debug(`${CYAN}${BRIGHT}Bun garbage collection triggered at ${new Date(Date.now()).toLocaleString()}.${RESET}${db}`);
|
|
188
|
+
this.emit('gc_done', type, execution);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
if (this.debug)
|
|
192
|
+
this.log.debug(`${CYAN}${BRIGHT}Bun garbage collection failed triggered at ${new Date(Date.now()).toLocaleString()}.${RESET}${db}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
176
195
|
else {
|
|
177
196
|
if (this.debug)
|
|
178
197
|
this.log.debug(`${CYAN}${BRIGHT}Garbage collection not exposed. Start Node.js with --expose-gc to enable manual garbage collection.${RESET}${db}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matterbridge/utils",
|
|
3
|
-
"version": "3.9.3-dev-
|
|
3
|
+
"version": "3.9.3-dev-20260630-684e0d2",
|
|
4
4
|
"description": "Matterbridge utils library",
|
|
5
5
|
"author": "https://github.com/Luligu",
|
|
6
6
|
"homepage": "https://matterbridge.io/",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
"import": "./dist/export.js"
|
|
52
52
|
},
|
|
53
53
|
"./bun": {
|
|
54
|
-
"types": "./dist/
|
|
55
|
-
"import": "./dist/
|
|
54
|
+
"types": "./dist/runtimeBun.d.ts",
|
|
55
|
+
"import": "./dist/runtimeBun.js"
|
|
56
56
|
},
|
|
57
57
|
"./cli": {
|
|
58
58
|
"types": "./dist/commandLine.d.ts",
|
package/dist/bunPrefix.d.ts
DELETED
package/dist/bunPrefix.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import os from 'node:os';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { logModuleLoaded } from './loader.js';
|
|
4
|
-
logModuleLoaded('BunPrefix');
|
|
5
|
-
export function isBun() {
|
|
6
|
-
return typeof process !== 'undefined' && process.versions?.bun !== undefined;
|
|
7
|
-
}
|
|
8
|
-
export function getGlobalBunModules() {
|
|
9
|
-
if (!isBun()) {
|
|
10
|
-
throw new Error('getGlobalBunModules can only be called in a Bun environment.');
|
|
11
|
-
}
|
|
12
|
-
const bunInstall = process.env.BUN_INSTALL ?? path.join(os.homedir(), '.bun');
|
|
13
|
-
return path.join(bunInstall, 'install', 'global', 'node_modules');
|
|
14
|
-
}
|