@fanboynz/network-scanner 2.0.35 → 2.0.37

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.
@@ -3,6 +3,10 @@
3
3
  * Provides graceful and forced browser closure functionality with comprehensive temp file cleanup
4
4
  */
5
5
 
6
+
7
+ const fs = require('fs');
8
+ const { execSync } = require('child_process');
9
+
6
10
  // Constants for temp file cleanup
7
11
  const CHROME_TEMP_PATHS = [
8
12
  '/tmp',
@@ -13,8 +17,7 @@ const CHROME_TEMP_PATHS = [
13
17
  const CHROME_TEMP_PATTERNS = [
14
18
  '.com.google.Chrome.*', // Google Chrome temp files
15
19
  '.org.chromium.Chromium.*',
16
- 'puppeteer-*',
17
- '.com.google.Chrome.*' // Ensure Google Chrome pattern is included
20
+ 'puppeteer-*'
18
21
  ];
19
22
 
20
23
  /**
@@ -33,7 +36,6 @@ async function cleanupChromeTempFiles(options = {}) {
33
36
  } = options;
34
37
 
35
38
  try {
36
- const { execSync } = require('child_process');
37
39
 
38
40
  // Base cleanup commands for standard temp directories
39
41
  const cleanupCommands = [
@@ -56,17 +58,17 @@ async function cleanupChromeTempFiles(options = {}) {
56
58
 
57
59
  for (const command of cleanupCommands) {
58
60
  try {
59
- // Get file count before cleanup for reporting
60
- const listCommand = command.replace('rm -rf', 'ls -1d').replace(' 2>/dev/null || true', ' 2>/dev/null | wc -l || echo 0');
61
- const fileCount = parseInt(execSync(listCommand, { stdio: 'pipe' }).toString().trim()) || 0;
61
+ // Extract glob pattern and count matches before deletion
62
+ const globPattern = command.match(/rm -rf ([^ ]+)/)?.[1];
63
+ if (!globPattern) continue;
64
+ const fileCount = parseInt(execSync(`ls -1d ${globPattern} 2>/dev/null | wc -l || echo 0`, { stdio: 'pipe' }).toString().trim()) || 0;
62
65
 
63
66
  if (fileCount > 0) {
64
67
  execSync(command, { stdio: 'ignore' });
65
68
  totalCleaned += fileCount;
66
69
 
67
70
  if (forceDebug) {
68
- const pathPattern = command.match(/rm -rf ([^ ]+)/)?.[1] || 'unknown';
69
- console.log(`[debug] [temp-cleanup] Cleaned ${fileCount} items from ${pathPattern}`);
71
+ console.log(`[debug] [temp-cleanup] Cleaned ${fileCount} items from ${globPattern}`);
70
72
  }
71
73
  }
72
74
  } catch (cmdErr) {
@@ -102,7 +104,6 @@ async function comprehensiveChromeTempCleanup(options = {}) {
102
104
  const { forceDebug = false, verbose = false } = options;
103
105
 
104
106
  try {
105
- const { execSync } = require('child_process');
106
107
  let totalCleaned = 0;
107
108
 
108
109
  if (verbose && !forceDebug) {
@@ -112,8 +113,7 @@ async function comprehensiveChromeTempCleanup(options = {}) {
112
113
  for (const basePath of CHROME_TEMP_PATHS) {
113
114
  // Check if the base path exists before trying to clean it
114
115
  try {
115
- const pathExists = execSync(`test -d "${basePath}" && echo "exists" || echo "missing"`, { stdio: 'pipe' })
116
- .toString().trim() === 'exists';
116
+ const pathExists = fs.existsSync(basePath);
117
117
 
118
118
  if (!pathExists) {
119
119
  if (forceDebug) {
@@ -149,7 +149,7 @@ async function comprehensiveChromeTempCleanup(options = {}) {
149
149
  if (verbose && totalCleaned > 0) {
150
150
  console.log(`[temp-cleanup] ? Removed ${totalCleaned} temporary file(s)/folder(s)`);
151
151
  } else if (verbose && totalCleaned === 0) {
152
- console.log(`[temp-cleanup] ?? No temporary files found to remove`);
152
+ console.log(`[temp-cleanup] ? Clean - no remaining temporary files`);
153
153
  } else if (forceDebug) {
154
154
  console.log(`[debug] [temp-cleanup] Comprehensive cleanup completed (${totalCleaned} items)`);
155
155
  }
@@ -179,7 +179,6 @@ async function cleanupUserDataDir(userDataDir, forceDebug = false) {
179
179
  }
180
180
 
181
181
  try {
182
- const fs = require('fs');
183
182
 
184
183
  if (!fs.existsSync(userDataDir)) {
185
184
  if (forceDebug) {
@@ -281,9 +280,7 @@ async function forceBrowserKill(browser, forceDebug = false) {
281
280
  const mainPid = browserProcess.pid;
282
281
  if (forceDebug) console.log(`[debug] [browser] Main Chrome PID: ${mainPid}`);
283
282
 
284
- // Find and kill ALL related Chrome processes
285
- const { execSync } = require('child_process');
286
-
283
+ // Find and kill ALL related Chrome processes
287
284
 
288
285
  try {
289
286
  // Find all Chrome processes with puppeteer in command line
@@ -390,9 +387,7 @@ async function forceBrowserKill(browser, forceDebug = false) {
390
387
  * @returns {Promise<void>}
391
388
  */
392
389
  async function killAllPuppeteerChrome(forceDebug = false) {
393
- try {
394
- const { execSync } = require('child_process');
395
-
390
+ try {
396
391
  if (forceDebug) console.log(`[debug] [browser] Nuclear option: killing all puppeteer Chrome processes...`);
397
392
 
398
393
  try {
@@ -439,7 +434,7 @@ async function handleBrowserExit(browser, options = {}) {
439
434
 
440
435
  const results = {
441
436
  browserClosed: false,
442
- tempFilescleaned: 0,
437
+ tempFilesCleanedCount: 0,
443
438
  userDataCleaned: false,
444
439
  success: false,
445
440
  errors: []
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  const { formatLogMessage, messageColors } = require('./colorize');
7
-
7
+ const { execSync } = require('child_process');
8
8
 
9
9
  // Window cleanup delay constant
10
10
  const WINDOW_CLEANUP_DELAY_MS = 15000;
@@ -15,12 +15,23 @@ const REALTIME_CLEANUP_MIN_PAGES = 6; // Minimum pages before cleanup kicks in
15
15
 
16
16
  // Track page creation order for realtime cleanup
17
17
  const pageCreationTracker = new Map(); // Maps page -> creation timestamp
18
- let pageCreationCounter = 0;
19
18
 
20
19
  // Track page usage for realtime cleanup safety
21
20
  const pageUsageTracker = new Map(); // Maps page -> { lastActivity: timestamp, isProcessing: boolean }
22
21
  const PAGE_IDLE_THRESHOLD = 25000; // 25 seconds of inactivity before considering page safe to clean
23
22
 
23
+ /**
24
+ * Format bytes to human readable string
25
+ * @param {number} bytes
26
+ * @returns {string}
27
+ */
28
+ function formatMemory(bytes) {
29
+ if (bytes >= 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)}GB`;
30
+ if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
31
+ if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)}KB`;
32
+ return `${bytes}B`;
33
+ }
34
+
24
35
  /**
25
36
  * Performs group-level window cleanup after all URLs in a site group complete
26
37
  * Closes all extra windows except the main browser window
@@ -162,6 +173,8 @@ async function performGroupWindowCleanup(browserInstance, groupDescription, forc
162
173
  if (forceDebug) {
163
174
  console.log(formatLogMessage('debug', `[group_window_cleanup] Closing page: ${pageUrl}`));
164
175
  }
176
+ pageCreationTracker.delete(page);
177
+ pageUsageTracker.delete(page);
165
178
  await page.close();
166
179
  return { success: true, url: pageUrl || `page-${index}`, estimatedMemory: pageMemoryEstimates[index] };
167
180
  }
@@ -180,19 +193,6 @@ async function performGroupWindowCleanup(browserInstance, groupDescription, forc
180
193
  .filter(result => result.success === true)
181
194
  .reduce((sum, result) => sum + (result.estimatedMemory || 0), 0);
182
195
 
183
- // Format memory for human readability
184
- const formatMemory = (bytes) => {
185
- if (bytes >= 1024 * 1024 * 1024) {
186
- return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)}GB`;
187
- } else if (bytes >= 1024 * 1024) {
188
- return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
189
- } else if (bytes >= 1024) {
190
- return `${(bytes / 1024).toFixed(1)}KB`;
191
- } else {
192
- return `${bytes}B`;
193
- }
194
- };
195
-
196
196
  if (forceDebug) {
197
197
  console.log(formatLogMessage('debug', `[group_window_cleanup] Closed ${successfulCloses}/${pagesToClose.length} old windows for completed group: ${groupDescription} after ${WINDOW_CLEANUP_DELAY_MS}ms delay`));
198
198
  console.log(formatLogMessage('debug', `[group_window_cleanup] Estimated memory freed: ${formatMemory(actualMemoryFreed)}`));
@@ -438,6 +438,7 @@ async function performRealtimeWindowCleanup(browserInstance, threshold = REALTIM
438
438
  if (!isPageClosed) {
439
439
  await page.close();
440
440
  pageCreationTracker.delete(page); // Remove from tracker
441
+ pageUsageTracker.delete(page);
441
442
  closedCount++;
442
443
 
443
444
  if (forceDebug) {
@@ -545,7 +546,7 @@ async function isPageFromPreviousScan(page, forceDebug) {
545
546
  * @param {import('puppeteer').Page} page - Page to track
546
547
  */
547
548
  function trackPageForRealtime(page) {
548
- pageCreationTracker.set(page, ++pageCreationCounter);
549
+ pageCreationTracker.set(page, Date.now());
549
550
  updatePageUsage(page, false); // Initialize usage tracking
550
551
  }
551
552
 
@@ -781,7 +782,6 @@ async function checkBrowserMemory(browserInstance) {
781
782
 
782
783
  // Try to get process memory info (Linux/Unix)
783
784
  try {
784
- const { execSync } = require('child_process');
785
785
  const memInfo = execSync(`ps -p ${browserProcess.pid} -o rss=`, { encoding: 'utf8', timeout: 2000 });
786
786
  const memoryKB = parseInt(memInfo.trim());
787
787
 
@@ -1200,18 +1200,3 @@ module.exports = {
1200
1200
  updatePageUsage,
1201
1201
  cleanupPageBeforeReload
1202
1202
  };
1203
-
1204
- // Clean up tracking maps when pages are closed
1205
- const originalPageClose = require('puppeteer').Page.prototype.close;
1206
- if (originalPageClose) {
1207
- require('puppeteer').Page.prototype.close = async function(...args) {
1208
- try {
1209
- // Clean up tracking data
1210
- pageCreationTracker.delete(this);
1211
- pageUsageTracker.delete(this);
1212
- } catch (err) {
1213
- // Ignore cleanup errors
1214
- }
1215
- return originalPageClose.apply(this, args);
1216
- };
1217
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fanboynz/network-scanner",
3
- "version": "2.0.35",
3
+ "version": "2.0.37",
4
4
  "description": "A Puppeteer-based network scanner for analyzing web traffic, generating adblock filter rules, and identifying third-party requests. Features include fingerprint spoofing, Cloudflare bypass, content analysis with curl/grep, and multiple output formats.",
5
5
  "main": "nwss.js",
6
6
  "scripts": {