@matterbridge/utils 3.9.3-dev-20260628-fa05360 → 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 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';
@@ -3,33 +3,17 @@ export async function getGitHubUpdate(branch, file, timeout = 10000) {
3
3
  const https = await import('node:https');
4
4
  return new Promise((resolve, reject) => {
5
5
  const url = `https://matterbridge.io/${branch}_${file}`;
6
- let settled = false;
7
- let req;
8
- let timeoutId;
9
- const rejectOnce = (error) => {
10
- if (settled)
11
- return;
12
- settled = true;
13
- clearTimeout(timeoutId);
14
- reject(error);
15
- };
16
- const resolveOnce = (updateJson) => {
17
- if (settled)
18
- return;
19
- settled = true;
20
- clearTimeout(timeoutId);
21
- resolve(updateJson);
22
- };
23
- const timeoutError = new Error(`Request timed out after ${timeout / 1000} seconds`);
24
- timeoutId = setTimeout(() => {
25
- req?.destroy?.(timeoutError);
26
- rejectOnce(timeoutError);
6
+ const controller = new AbortController();
7
+ const timeoutId = setTimeout(() => {
8
+ controller.abort();
9
+ reject(new Error(`Request timed out after ${timeout / 1000} seconds`));
27
10
  }, timeout).unref();
28
- req = https.get(url, {}, (res) => {
11
+ const req = https.get(url, { signal: controller.signal }, (res) => {
29
12
  let data = '';
30
13
  if (res.statusCode !== 200) {
14
+ clearTimeout(timeoutId);
31
15
  res.resume();
32
- rejectOnce(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
16
+ reject(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
33
17
  return;
34
18
  }
35
19
  res.on('data', (chunk) => {
@@ -38,15 +22,18 @@ export async function getGitHubUpdate(branch, file, timeout = 10000) {
38
22
  res.on('end', () => {
39
23
  try {
40
24
  const jsonData = JSON.parse(data);
41
- resolveOnce(jsonData);
25
+ clearTimeout(timeoutId);
26
+ resolve(jsonData);
42
27
  }
43
28
  catch (error) {
44
- rejectOnce(new Error(`Failed to parse response JSON: ${getErrorMessage(error)}`));
29
+ clearTimeout(timeoutId);
30
+ reject(new Error(`Failed to parse response JSON: ${getErrorMessage(error)}`));
45
31
  }
46
32
  });
47
33
  });
48
34
  req.on('error', (error) => {
49
- rejectOnce(new Error(`Request failed: ${getErrorMessage(error)}`));
35
+ clearTimeout(timeoutId);
36
+ reject(new Error(`Request failed: ${getErrorMessage(error)}`));
50
37
  });
51
38
  });
52
39
  }
package/dist/npmPrefix.js CHANGED
@@ -1,5 +1,5 @@
1
- import { getGlobalBunModules, isBun } from './bunPrefix.js';
2
1
  import { logModuleLoaded } from './loader.js';
2
+ import { getGlobalBunModules, isBun } from './runtimeBun.js';
3
3
  logModuleLoaded('NpmPrefix');
4
4
  export async function getGlobalNodeModules() {
5
5
  if (isBun())
@@ -3,33 +3,17 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
3
3
  const https = await import('node:https');
4
4
  return new Promise((resolve, reject) => {
5
5
  const url = `https://registry.npmjs.org/${packageName}`;
6
- let settled = false;
7
- let req;
8
- let timeoutId;
9
- const rejectOnce = (error) => {
10
- if (settled)
11
- return;
12
- settled = true;
13
- clearTimeout(timeoutId);
14
- reject(error);
15
- };
16
- const resolveOnce = (version) => {
17
- if (settled)
18
- return;
19
- settled = true;
20
- clearTimeout(timeoutId);
21
- resolve(version);
22
- };
23
- const timeoutError = new Error(`Request timed out after ${timeout / 1000} seconds`);
24
- timeoutId = setTimeout(() => {
25
- req?.destroy?.(timeoutError);
26
- rejectOnce(timeoutError);
6
+ const controller = new AbortController();
7
+ const timeoutId = setTimeout(() => {
8
+ controller.abort();
9
+ reject(new Error(`Request timed out after ${timeout / 1000} seconds`));
27
10
  }, timeout).unref();
28
- req = https.get(url, {}, (res) => {
11
+ const req = https.get(url, { signal: controller.signal }, (res) => {
29
12
  let data = '';
30
13
  if (res.statusCode !== 200) {
14
+ clearTimeout(timeoutId);
31
15
  res.resume();
32
- rejectOnce(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
16
+ reject(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
33
17
  return;
34
18
  }
35
19
  res.on('data', (chunk) => {
@@ -40,19 +24,23 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
40
24
  const jsonData = JSON.parse(data);
41
25
  const version = jsonData['dist-tags']?.[tag];
42
26
  if (version) {
43
- resolveOnce(version);
27
+ clearTimeout(timeoutId);
28
+ resolve(version);
44
29
  }
45
30
  else {
46
- rejectOnce(new Error(`Tag "${tag}" not found for package "${packageName}"`));
31
+ clearTimeout(timeoutId);
32
+ reject(new Error(`Tag "${tag}" not found for package "${packageName}"`));
47
33
  }
48
34
  }
49
35
  catch (error) {
50
- rejectOnce(new Error(`Failed to parse response JSON: ${getErrorMessage(error)}`));
36
+ clearTimeout(timeoutId);
37
+ reject(new Error(`Failed to parse response JSON: ${getErrorMessage(error)}`));
51
38
  }
52
39
  });
53
40
  });
54
41
  req.on('error', (error) => {
55
- rejectOnce(new Error(`Request failed: ${getErrorMessage(error)}`));
42
+ clearTimeout(timeoutId);
43
+ reject(new Error(`Request failed: ${getErrorMessage(error)}`));
56
44
  });
57
45
  });
58
46
  }
@@ -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
@@ -36,6 +36,8 @@ export declare class Tracker extends EventEmitter<TrackerEvents> {
36
36
  private readonly name;
37
37
  private readonly debug;
38
38
  private readonly verbose;
39
+ private readonly tracker;
40
+ private readonly forceGc;
39
41
  private trackerInterval?;
40
42
  static historyIndex: number;
41
43
  static readonly historySize = 2880;
@@ -43,7 +45,7 @@ export declare class Tracker extends EventEmitter<TrackerEvents> {
43
45
  private prevCpus;
44
46
  private prevCpuUsage;
45
47
  private log;
46
- constructor(name?: string, debug?: boolean, verbose?: boolean);
48
+ constructor(name?: string, debug?: boolean, verbose?: boolean, tracker?: boolean, forceGc?: boolean);
47
49
  start(sampleIntervalMs?: number): void;
48
50
  resetPeaks(): void;
49
51
  runGarbageCollector(type?: 'major' | 'minor', execution?: 'sync' | 'async'): void;
package/dist/tracker.js CHANGED
@@ -3,11 +3,14 @@ import os from 'node:os';
3
3
  import { AnsiLogger, BRIGHT, CYAN, db, RED, RESET, YELLOW } from 'node-ansi-logger';
4
4
  import { formatBytes, formatPercent, formatTimeStamp } from './format.js';
5
5
  import { logModuleLoaded } from './loader.js';
6
+ import { isBun, gc, setGcLevel } from './runtimeBun.js';
6
7
  logModuleLoaded('Tracker');
7
8
  export class Tracker extends EventEmitter {
8
9
  name;
9
10
  debug;
10
11
  verbose;
12
+ tracker;
13
+ forceGc;
11
14
  trackerInterval;
12
15
  static historyIndex = 0;
13
16
  static historySize = 2880;
@@ -35,18 +38,26 @@ export class Tracker extends EventEmitter {
35
38
  prevCpus = os.cpus();
36
39
  prevCpuUsage = process.cpuUsage();
37
40
  log;
38
- constructor(name = 'Tracker', debug = false, verbose = false) {
41
+ constructor(name = 'Tracker', debug = false, verbose = false, tracker = false, forceGc = false) {
39
42
  super();
40
43
  this.name = name;
41
44
  this.debug = debug;
42
45
  this.verbose = verbose;
43
- if (process.argv.includes('--debug') || process.argv.includes('-debug') || process.argv.includes('--verbose') || process.argv.includes('-verbose')) {
46
+ this.tracker = tracker;
47
+ this.forceGc = forceGc;
48
+ if (process.argv.includes('--debug') || process.argv.includes('--verbose') || process.argv.includes('--tracker')) {
44
49
  this.debug = true;
45
50
  }
46
- if (process.argv.includes('--verbose') || process.argv.includes('-verbose')) {
51
+ if (process.argv.includes('--verbose')) {
47
52
  this.verbose = true;
48
53
  }
49
- this.log = new AnsiLogger({ logName: name, logTimestampFormat: 4, logLevel: this.debug ? "debug" : "info" });
54
+ if (process.argv.includes('--tracker')) {
55
+ this.tracker = true;
56
+ }
57
+ if (process.argv.includes('--force-gc')) {
58
+ this.forceGc = true;
59
+ }
60
+ this.log = new AnsiLogger({ logName: name, logTimestampFormat: 4, logLevel: this.debug || this.tracker ? "debug" : "info" });
50
61
  this.log.logNameColor = YELLOW;
51
62
  if (this.verbose) {
52
63
  this.log.debug(`os.cpus():\n${RESET}`, os.cpus());
@@ -75,7 +86,7 @@ export class Tracker extends EventEmitter {
75
86
  this.prevCpuUsage = process.cpuUsage();
76
87
  this.trackerInterval = setInterval(() => {
77
88
  tryGcCount += sampleIntervalMs / 1000;
78
- if (tryGcCount > 60 * 60) {
89
+ if (tryGcCount > 60 * 60 || this.forceGc) {
79
90
  this.runGarbageCollector();
80
91
  tryGcCount = 0;
81
92
  }
@@ -153,7 +164,8 @@ export class Tracker extends EventEmitter {
153
164
  this.emit('reset_peaks_done');
154
165
  }
155
166
  runGarbageCollector(type = 'major', execution = 'async') {
156
- if (global.gc && typeof global.gc === 'function') {
167
+ const bun = isBun();
168
+ if (!bun && global.gc && typeof global.gc === 'function') {
157
169
  try {
158
170
  global.gc({ type, execution });
159
171
  if (this.debug)
@@ -167,6 +179,19 @@ export class Tracker extends EventEmitter {
167
179
  this.emit('gc_done', 'minor', 'async');
168
180
  }
169
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
+ }
170
195
  else {
171
196
  if (this.debug)
172
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-20260628-fa05360",
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/bunPrefix.d.ts",
55
- "import": "./dist/bunPrefix.js"
54
+ "types": "./dist/runtimeBun.d.ts",
55
+ "import": "./dist/runtimeBun.js"
56
56
  },
57
57
  "./cli": {
58
58
  "types": "./dist/commandLine.d.ts",
@@ -1,2 +0,0 @@
1
- export declare function isBun(): boolean;
2
- export declare function getGlobalBunModules(): string;
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
- }