channel-worker 1.0.3 → 1.0.4
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/lib/nst-manager.js +43 -37
- package/package.json +1 -1
package/lib/nst-manager.js
CHANGED
|
@@ -2,7 +2,7 @@ let NstBrowserV2;
|
|
|
2
2
|
try {
|
|
3
3
|
NstBrowserV2 = require('nstbrowser-sdk-node').NstBrowserV2;
|
|
4
4
|
} catch {
|
|
5
|
-
// SDK not installed
|
|
5
|
+
// SDK not installed
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
class NstManager {
|
|
@@ -16,17 +16,40 @@ class NstManager {
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// Get all running browsers
|
|
20
|
+
async getRunningBrowsers() {
|
|
21
|
+
try {
|
|
22
|
+
const res = await this.client.browsers().getBrowsers('running');
|
|
23
|
+
return res?.data || [];
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error(`[nst] Error getting running browsers:`, err.message);
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Check if profile is already running
|
|
31
|
+
async isProfileRunning(profileId) {
|
|
32
|
+
const running = await this.getRunningBrowsers();
|
|
33
|
+
return running.some(b => b.profileId === profileId);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Find profile by name, return profileId
|
|
20
37
|
async findProfile(name) {
|
|
21
38
|
try {
|
|
22
39
|
const res = await this.client.profiles().getProfiles({ keyword: name });
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
40
|
+
// Response: { data: { docs: [...], totalDocs, ... } }
|
|
41
|
+
let profiles = [];
|
|
42
|
+
if (res?.data?.docs && Array.isArray(res.data.docs)) {
|
|
43
|
+
profiles = res.data.docs;
|
|
44
|
+
} else if (Array.isArray(res?.data)) {
|
|
45
|
+
profiles = res.data;
|
|
27
46
|
}
|
|
47
|
+
|
|
28
48
|
const match = profiles.find(p => p.name === name);
|
|
29
|
-
|
|
49
|
+
if (match) {
|
|
50
|
+
return match.profileId || match._id;
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
30
53
|
} catch (err) {
|
|
31
54
|
console.error(`[nst] Error finding profile "${name}":`, err.message);
|
|
32
55
|
return null;
|
|
@@ -35,14 +58,12 @@ class NstManager {
|
|
|
35
58
|
|
|
36
59
|
// Create profile if not exists, return profileId
|
|
37
60
|
async ensureProfile(name) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return profileId;
|
|
61
|
+
const existing = await this.findProfile(name);
|
|
62
|
+
if (existing) {
|
|
63
|
+
console.log(`[nst] Profile "${name}" exists: ${existing}`);
|
|
64
|
+
return existing;
|
|
43
65
|
}
|
|
44
66
|
|
|
45
|
-
// Create new
|
|
46
67
|
console.log(`[nst] Creating profile "${name}"...`);
|
|
47
68
|
const res = await this.client.profiles().createProfile({
|
|
48
69
|
name,
|
|
@@ -61,48 +82,33 @@ class NstManager {
|
|
|
61
82
|
},
|
|
62
83
|
});
|
|
63
84
|
|
|
64
|
-
profileId = res?.data?.profileId || res?.data?.
|
|
85
|
+
const profileId = res?.data?.profileId || res?.data?._id;
|
|
65
86
|
if (!profileId) throw new Error('Failed to create profile — no ID returned');
|
|
66
87
|
|
|
67
88
|
console.log(`[nst] Profile "${name}" created: ${profileId}`);
|
|
68
89
|
return profileId;
|
|
69
90
|
}
|
|
70
91
|
|
|
71
|
-
// Launch browser
|
|
92
|
+
// Launch browser — skip if already running
|
|
72
93
|
async launchProfile(profileIdOrName) {
|
|
73
|
-
// If it looks like a name (not UUID), find/create by name
|
|
74
94
|
let profileId = profileIdOrName;
|
|
75
95
|
if (!this.isUUID(profileIdOrName)) {
|
|
76
96
|
profileId = await this.ensureProfile(profileIdOrName);
|
|
77
97
|
}
|
|
78
98
|
|
|
99
|
+
// Check if already running
|
|
100
|
+
if (await this.isProfileRunning(profileId)) {
|
|
101
|
+
console.log(`[nst] Profile ${profileId} already running — skipping`);
|
|
102
|
+
return { profileId, alreadyRunning: true };
|
|
103
|
+
}
|
|
104
|
+
|
|
79
105
|
console.log(`[nst] Starting browser for profile: ${profileId}`);
|
|
80
106
|
const res = await this.client.browsers().startBrowser(profileId);
|
|
81
107
|
console.log(`[nst] Browser started`);
|
|
82
108
|
return { profileId, response: res };
|
|
83
109
|
}
|
|
84
110
|
|
|
85
|
-
//
|
|
86
|
-
async connectProfile(profileIdOrName) {
|
|
87
|
-
let profileId = profileIdOrName;
|
|
88
|
-
if (!this.isUUID(profileIdOrName)) {
|
|
89
|
-
profileId = await this.ensureProfile(profileIdOrName);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
console.log(`[nst] Connecting to profile: ${profileId}`);
|
|
93
|
-
const res = await this.client.cdpEndpoints().connectBrowser(profileId, {
|
|
94
|
-
headless: false,
|
|
95
|
-
autoClose: false,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
const wsEndpoint = res?.data?.webSocketDebuggerUrl;
|
|
99
|
-
if (!wsEndpoint) throw new Error('No WebSocket endpoint returned');
|
|
100
|
-
|
|
101
|
-
console.log(`[nst] Connected — WS: ${wsEndpoint}`);
|
|
102
|
-
return { profileId, wsEndpoint };
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Stop browser for profile
|
|
111
|
+
// Stop browser
|
|
106
112
|
async stopProfile(profileIdOrName) {
|
|
107
113
|
let profileId = profileIdOrName;
|
|
108
114
|
if (!this.isUUID(profileIdOrName)) {
|