channel-worker 1.0.9 → 1.0.11
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 +18 -21
- 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,13 +102,14 @@ 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
|
-
//
|
|
108
|
+
// If already running, stop first to reload with fresh extension
|
|
118
109
|
if (await this.isProfileRunning(profileId)) {
|
|
119
|
-
console.log(`[nst] Profile ${profileId} already running —
|
|
120
|
-
|
|
110
|
+
console.log(`[nst] Profile ${profileId} already running — restarting to load extension`);
|
|
111
|
+
await this.stopProfile(profileId);
|
|
112
|
+
await new Promise(r => setTimeout(r, 3000)); // wait for browser to close
|
|
121
113
|
}
|
|
122
114
|
|
|
123
115
|
// Set proxy before launch
|
|
@@ -125,13 +117,11 @@ class NstManager {
|
|
|
125
117
|
await this.setProxy(profileId, options.proxy);
|
|
126
118
|
}
|
|
127
119
|
|
|
128
|
-
// Use connectBrowser instead of startBrowser — allows passing --load-extension args
|
|
129
120
|
const connectConfig = {
|
|
130
121
|
headless: false,
|
|
131
122
|
autoClose: false,
|
|
132
123
|
};
|
|
133
124
|
|
|
134
|
-
// Add extension path if provided
|
|
135
125
|
if (options.extensionPath) {
|
|
136
126
|
connectConfig.args = {
|
|
137
127
|
'--load-extension': options.extensionPath,
|
|
@@ -141,9 +131,16 @@ class NstManager {
|
|
|
141
131
|
}
|
|
142
132
|
|
|
143
133
|
console.log(`[nst] Connecting browser for profile: ${profileId}`);
|
|
144
|
-
const res = await this.client.cdpEndpoints().connectBrowser(profileId, connectConfig);
|
|
145
|
-
console.log(`[nst] Browser started`);
|
|
146
134
|
|
|
135
|
+
// SDK uses axios which serializes nested objects as config[key]=value
|
|
136
|
+
// But Nstbrowser API expects ?config=<JSON-string>
|
|
137
|
+
// Workaround: call the API directly with fetch instead of SDK
|
|
138
|
+
const apiUrl = `http://localhost:8848/api/v2/connect/${profileId}?config=${encodeURIComponent(JSON.stringify(connectConfig))}`;
|
|
139
|
+
const rawRes = await fetch(apiUrl, { headers: { 'x-api-key': this.apiKey } });
|
|
140
|
+
const res = await rawRes.json();
|
|
141
|
+
if (res.err) throw new Error(res.msg || 'Failed to connect browser');
|
|
142
|
+
|
|
143
|
+
console.log(`[nst] Browser started`);
|
|
147
144
|
return { profileId, wsEndpoint: res?.data?.webSocketDebuggerUrl, response: res };
|
|
148
145
|
}
|
|
149
146
|
|