channel-worker 1.0.10 → 1.0.12
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 +17 -22
- package/package.json +1 -1
package/lib/nst-manager.js
CHANGED
|
@@ -10,6 +10,7 @@ class NstManager {
|
|
|
10
10
|
if (!NstBrowserV2) {
|
|
11
11
|
throw new Error('nstbrowser-sdk-node not installed. Run: npm install -g nstbrowser-sdk-node');
|
|
12
12
|
}
|
|
13
|
+
this.apiKey = apiKey;
|
|
13
14
|
this.client = new NstBrowserV2(apiKey, {
|
|
14
15
|
timeout: options.timeout || 60000,
|
|
15
16
|
apiAddress: options.apiAddress || 'http://localhost:8848/api/v2',
|
|
@@ -57,7 +58,7 @@ class NstManager {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
// Create profile if not exists, return profileId
|
|
60
|
-
async ensureProfile(name
|
|
61
|
+
async ensureProfile(name) {
|
|
61
62
|
const existing = await this.findProfile(name);
|
|
62
63
|
if (existing) {
|
|
63
64
|
console.log(`[nst] Profile "${name}" exists: ${existing}`);
|
|
@@ -65,7 +66,7 @@ class NstManager {
|
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
console.log(`[nst] Creating profile "${name}"...`);
|
|
68
|
-
const
|
|
69
|
+
const res = await this.client.profiles().createProfile({
|
|
69
70
|
name,
|
|
70
71
|
platform: 'Windows',
|
|
71
72
|
kernelMilestone: '132',
|
|
@@ -80,17 +81,7 @@ class NstManager {
|
|
|
80
81
|
hardwareConcurrency: 8,
|
|
81
82
|
deviceMemory: 8,
|
|
82
83
|
},
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
// Add extension load arg if provided
|
|
86
|
-
if (options.extensionPath) {
|
|
87
|
-
profileData.args = {
|
|
88
|
-
'--load-extension': options.extensionPath,
|
|
89
|
-
'--disable-extensions-except': options.extensionPath,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const res = await this.client.profiles().createProfile(profileData);
|
|
84
|
+
});
|
|
94
85
|
|
|
95
86
|
const profileId = res?.data?.profileId || res?.data?._id;
|
|
96
87
|
if (!profileId) throw new Error('Failed to create profile — no ID returned');
|
|
@@ -111,14 +102,13 @@ class NstManager {
|
|
|
111
102
|
async launchProfile(profileIdOrName, options = {}) {
|
|
112
103
|
let profileId = profileIdOrName;
|
|
113
104
|
if (!this.isUUID(profileIdOrName)) {
|
|
114
|
-
profileId = await this.ensureProfile(profileIdOrName
|
|
105
|
+
profileId = await this.ensureProfile(profileIdOrName);
|
|
115
106
|
}
|
|
116
107
|
|
|
117
|
-
// If already running,
|
|
108
|
+
// If already running, skip — don't restart
|
|
118
109
|
if (await this.isProfileRunning(profileId)) {
|
|
119
|
-
console.log(`[nst] Profile ${profileId} already running —
|
|
120
|
-
|
|
121
|
-
await new Promise(r => setTimeout(r, 3000)); // wait for browser to close
|
|
110
|
+
console.log(`[nst] Profile ${profileId} already running — skipping`);
|
|
111
|
+
return { profileId, alreadyRunning: true };
|
|
122
112
|
}
|
|
123
113
|
|
|
124
114
|
// Set proxy before launch
|
|
@@ -126,13 +116,11 @@ class NstManager {
|
|
|
126
116
|
await this.setProxy(profileId, options.proxy);
|
|
127
117
|
}
|
|
128
118
|
|
|
129
|
-
// Use connectBrowser instead of startBrowser — allows passing --load-extension args
|
|
130
119
|
const connectConfig = {
|
|
131
120
|
headless: false,
|
|
132
121
|
autoClose: false,
|
|
133
122
|
};
|
|
134
123
|
|
|
135
|
-
// Add extension path if provided
|
|
136
124
|
if (options.extensionPath) {
|
|
137
125
|
connectConfig.args = {
|
|
138
126
|
'--load-extension': options.extensionPath,
|
|
@@ -142,9 +130,16 @@ class NstManager {
|
|
|
142
130
|
}
|
|
143
131
|
|
|
144
132
|
console.log(`[nst] Connecting browser for profile: ${profileId}`);
|
|
145
|
-
const res = await this.client.cdpEndpoints().connectBrowser(profileId, connectConfig);
|
|
146
|
-
console.log(`[nst] Browser started`);
|
|
147
133
|
|
|
134
|
+
// SDK uses axios which serializes nested objects as config[key]=value
|
|
135
|
+
// But Nstbrowser API expects ?config=<JSON-string>
|
|
136
|
+
// Workaround: call the API directly with fetch instead of SDK
|
|
137
|
+
const apiUrl = `http://localhost:8848/api/v2/connect/${profileId}?config=${encodeURIComponent(JSON.stringify(connectConfig))}`;
|
|
138
|
+
const rawRes = await fetch(apiUrl, { headers: { 'x-api-key': this.apiKey } });
|
|
139
|
+
const res = await rawRes.json();
|
|
140
|
+
if (res.err) throw new Error(res.msg || 'Failed to connect browser');
|
|
141
|
+
|
|
142
|
+
console.log(`[nst] Browser started`);
|
|
148
143
|
return { profileId, wsEndpoint: res?.data?.webSocketDebuggerUrl, response: res };
|
|
149
144
|
}
|
|
150
145
|
|