@mcp-fe/mcp-worker 0.0.11 → 0.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/index.js +282 -137
- package/mcp-service-worker.js +28029 -29
- package/mcp-shared-worker.js +28034 -45
- package/package.json +1 -1
- package/src/index.d.ts.map +1 -0
- package/src/lib/database.d.ts.map +1 -0
- package/{lib → src/lib}/mcp-controller.d.ts +6 -0
- package/src/lib/mcp-controller.d.ts.map +1 -0
- package/{lib → src/lib}/mcp-server.d.ts +4 -0
- package/src/lib/mcp-server.d.ts.map +1 -0
- package/src/lib/websocket-transport.d.ts.map +1 -0
- package/src/lib/worker-client.d.ts.map +1 -0
- package/src/mcp-service-worker.d.ts.map +1 -0
- package/src/mcp-shared-worker.d.ts.map +1 -0
- package/database.js +0 -49
- package/database.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/lib/database.d.ts.map +0 -1
- package/lib/mcp-controller.d.ts.map +0 -1
- package/lib/mcp-server.d.ts.map +0 -1
- package/lib/websocket-transport.d.ts.map +0 -1
- package/lib/worker-client.d.ts.map +0 -1
- package/mcp-controller.js +0 -10565
- package/mcp-controller.js.map +0 -1
- package/mcp-service-worker.d.ts.map +0 -1
- package/mcp-service-worker.js.map +0 -1
- package/mcp-shared-worker.d.ts.map +0 -1
- package/mcp-shared-worker.js.map +0 -1
- /package/{index.d.ts → src/index.d.ts} +0 -0
- /package/{lib → src/lib}/database.d.ts +0 -0
- /package/{lib → src/lib}/websocket-transport.d.ts +0 -0
- /package/{lib → src/lib}/worker-client.d.ts +0 -0
- /package/{mcp-service-worker.d.ts → src/mcp-service-worker.d.ts} +0 -0
- /package/{mcp-shared-worker.d.ts → src/mcp-shared-worker.d.ts} +0 -0
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
class
|
|
1
|
+
// libs/mcp-worker/src/lib/worker-client.ts
|
|
2
|
+
var WorkerClient = class {
|
|
3
3
|
serviceWorkerRegistration = null;
|
|
4
4
|
sharedWorker = null;
|
|
5
5
|
sharedWorkerPort = null;
|
|
@@ -11,224 +11,369 @@ class l {
|
|
|
11
11
|
// Mutex/promise to prevent concurrent init runs
|
|
12
12
|
initPromise = null;
|
|
13
13
|
// Initialize and choose worker implementation (prefer SharedWorker)
|
|
14
|
-
async init(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
async init(registration) {
|
|
15
|
+
if (this.initPromise) {
|
|
16
|
+
return this.initPromise.then(async () => {
|
|
17
|
+
if (registration && this.workerType !== "service") {
|
|
18
|
+
await this.init(registration);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
this.initPromise = (async () => {
|
|
18
23
|
try {
|
|
19
|
-
if (
|
|
20
|
-
this.serviceWorkerRegistration =
|
|
24
|
+
if (registration) {
|
|
25
|
+
this.serviceWorkerRegistration = registration;
|
|
26
|
+
this.workerType = "service";
|
|
27
|
+
console.log("[WorkerClient] Using ServiceWorker (explicit registration)");
|
|
28
|
+
if (this.pendingAuthToken) {
|
|
29
|
+
this.sendAuthTokenToServiceWorker(this.pendingAuthToken);
|
|
30
|
+
this.pendingAuthToken = null;
|
|
31
|
+
}
|
|
21
32
|
return;
|
|
22
33
|
}
|
|
23
|
-
if (typeof SharedWorker
|
|
34
|
+
if (typeof SharedWorker !== "undefined") {
|
|
24
35
|
try {
|
|
25
|
-
|
|
36
|
+
this.sharedWorker = new SharedWorker("/mcp-shared-worker.js", { type: "module" });
|
|
37
|
+
this.sharedWorkerPort = this.sharedWorker.port;
|
|
38
|
+
this.sharedWorkerPort.start();
|
|
39
|
+
if (this.pendingAuthToken && this.sharedWorkerPort) {
|
|
26
40
|
try {
|
|
27
41
|
this.sharedWorkerPort.postMessage({ type: "SET_AUTH_TOKEN", token: this.pendingAuthToken });
|
|
28
|
-
} catch (
|
|
29
|
-
console.warn("[WorkerClient] Immediate postMessage to SharedWorker failed (will retry after init):",
|
|
42
|
+
} catch (err) {
|
|
43
|
+
console.warn("[WorkerClient] Immediate postMessage to SharedWorker failed (will retry after init):", err);
|
|
30
44
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
}
|
|
46
|
+
this.sharedWorker.onerror = (event) => {
|
|
47
|
+
console.error("[WorkerClient] SharedWorker error:", event.message || event.error || event);
|
|
48
|
+
if (this.workerType !== "shared") {
|
|
49
|
+
this.initServiceWorkerFallback().catch((err) => {
|
|
50
|
+
console.error("[WorkerClient] Failed to initialize ServiceWorker fallback:", err);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
await new Promise((resolve, reject) => {
|
|
55
|
+
let resolved = false;
|
|
56
|
+
const timeout = setTimeout(() => {
|
|
57
|
+
if (!resolved) {
|
|
58
|
+
const p2 = this.sharedWorkerPort;
|
|
59
|
+
if (p2)
|
|
60
|
+
p2.onmessage = null;
|
|
61
|
+
reject(new Error("SharedWorker initialization timeout"));
|
|
41
62
|
}
|
|
42
|
-
}, 2e3)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
63
|
+
}, 2e3);
|
|
64
|
+
const p = this.sharedWorkerPort;
|
|
65
|
+
if (!p) {
|
|
66
|
+
clearTimeout(timeout);
|
|
67
|
+
return reject(new Error("SharedWorker port not available"));
|
|
68
|
+
}
|
|
69
|
+
p.onmessage = (ev) => {
|
|
46
70
|
try {
|
|
47
|
-
const
|
|
48
|
-
|
|
71
|
+
const data = ev.data;
|
|
72
|
+
if (data && data.type === "CONNECTION_STATUS") {
|
|
73
|
+
clearTimeout(timeout);
|
|
74
|
+
resolved = true;
|
|
75
|
+
this.workerType = "shared";
|
|
76
|
+
p.onmessage = null;
|
|
77
|
+
resolve();
|
|
78
|
+
}
|
|
49
79
|
} catch {
|
|
50
80
|
}
|
|
51
81
|
};
|
|
52
82
|
});
|
|
53
|
-
const
|
|
54
|
-
if (this.pendingAuthToken &&
|
|
83
|
+
const portAfterInit = this.sharedWorkerPort;
|
|
84
|
+
if (this.pendingAuthToken && portAfterInit) {
|
|
55
85
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
86
|
+
portAfterInit.postMessage({ type: "SET_AUTH_TOKEN", token: this.pendingAuthToken });
|
|
87
|
+
this.pendingAuthToken = null;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
console.error("[WorkerClient] Failed to send pending auth token to SharedWorker:", e);
|
|
59
90
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
91
|
+
}
|
|
92
|
+
if (portAfterInit) {
|
|
93
|
+
portAfterInit.onmessage = (ev) => {
|
|
94
|
+
try {
|
|
95
|
+
const data = ev.data;
|
|
96
|
+
if (data && data.type === "CONNECTION_STATUS") {
|
|
97
|
+
const connected = !!data.connected;
|
|
98
|
+
this.connectionStatusCallbacks.forEach((cb) => {
|
|
99
|
+
try {
|
|
100
|
+
cb(connected);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
71
106
|
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
console.log("[WorkerClient] Using SharedWorker");
|
|
75
110
|
return;
|
|
76
|
-
} catch (
|
|
77
|
-
console.warn("[WorkerClient] SharedWorker not available, falling back to ServiceWorker:",
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.warn("[WorkerClient] SharedWorker not available, falling back to ServiceWorker:", error);
|
|
78
113
|
}
|
|
79
|
-
|
|
114
|
+
}
|
|
115
|
+
console.log("this should not be called");
|
|
116
|
+
await this.initServiceWorkerFallback();
|
|
117
|
+
if (this.pendingAuthToken && this.workerType === "service") {
|
|
118
|
+
this.sendAuthTokenToServiceWorker(this.pendingAuthToken);
|
|
119
|
+
this.pendingAuthToken = null;
|
|
120
|
+
}
|
|
80
121
|
} finally {
|
|
81
122
|
this.initPromise = null;
|
|
82
123
|
}
|
|
83
|
-
})()
|
|
124
|
+
})();
|
|
125
|
+
return this.initPromise;
|
|
84
126
|
}
|
|
85
127
|
async initServiceWorkerFallback() {
|
|
86
|
-
|
|
128
|
+
console.log("initServiceWorkerFallback called");
|
|
129
|
+
if ("serviceWorker" in navigator) {
|
|
87
130
|
try {
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
this.serviceWorkerRegistration =
|
|
131
|
+
const existingRegistration = await navigator.serviceWorker.getRegistration();
|
|
132
|
+
if (existingRegistration) {
|
|
133
|
+
this.serviceWorkerRegistration = existingRegistration;
|
|
134
|
+
this.workerType = "service";
|
|
135
|
+
console.log("[WorkerClient] Using existing ServiceWorker registration");
|
|
91
136
|
return;
|
|
92
137
|
}
|
|
93
|
-
const
|
|
94
|
-
this.serviceWorkerRegistration =
|
|
138
|
+
const reg = await navigator.serviceWorker.register("/mcp-service-worker.js");
|
|
139
|
+
this.serviceWorkerRegistration = reg;
|
|
140
|
+
this.workerType = "service";
|
|
141
|
+
console.log("[WorkerClient] Using ServiceWorker (fallback)");
|
|
142
|
+
if (this.serviceWorkerMessageHandler) {
|
|
143
|
+
navigator.serviceWorker.removeEventListener("message", this.serviceWorkerMessageHandler);
|
|
144
|
+
this.serviceWorkerMessageHandler = null;
|
|
145
|
+
}
|
|
146
|
+
this.serviceWorkerMessageHandler = (ev) => {
|
|
95
147
|
try {
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
98
|
-
const
|
|
99
|
-
this.connectionStatusCallbacks.forEach((
|
|
148
|
+
const data = ev.data;
|
|
149
|
+
if (data && data.type === "CONNECTION_STATUS") {
|
|
150
|
+
const connected = !!data.connected;
|
|
151
|
+
this.connectionStatusCallbacks.forEach((cb) => {
|
|
100
152
|
try {
|
|
101
|
-
|
|
102
|
-
} catch {
|
|
153
|
+
cb(connected);
|
|
154
|
+
} catch (e) {
|
|
103
155
|
}
|
|
104
156
|
});
|
|
105
157
|
}
|
|
106
158
|
} catch {
|
|
107
159
|
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
160
|
+
};
|
|
161
|
+
navigator.serviceWorker.addEventListener("message", this.serviceWorkerMessageHandler);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.error("[WorkerClient] Failed to register ServiceWorker:", error);
|
|
164
|
+
throw error;
|
|
111
165
|
}
|
|
112
|
-
else
|
|
166
|
+
} else {
|
|
113
167
|
throw new Error("Neither SharedWorker nor ServiceWorker is supported");
|
|
168
|
+
}
|
|
114
169
|
}
|
|
115
170
|
// Low-level request that expects a reply via MessageChannel
|
|
116
|
-
async request(
|
|
117
|
-
if (this.workerType === "shared" && this.sharedWorkerPort)
|
|
118
|
-
return new Promise((
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
171
|
+
async request(type, payload, timeoutMs = 5e3) {
|
|
172
|
+
if (this.workerType === "shared" && this.sharedWorkerPort) {
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
const mc = new MessageChannel();
|
|
175
|
+
const timer = setTimeout(() => {
|
|
176
|
+
mc.port1.onmessage = null;
|
|
177
|
+
reject(new Error("Request timeout"));
|
|
178
|
+
}, timeoutMs);
|
|
179
|
+
mc.port1.onmessage = (ev) => {
|
|
180
|
+
clearTimeout(timer);
|
|
181
|
+
if (ev.data && ev.data.success) {
|
|
182
|
+
resolve(ev.data);
|
|
183
|
+
} else if (ev.data && ev.data.success === false) {
|
|
184
|
+
reject(new Error(ev.data.error || "Worker error"));
|
|
185
|
+
} else {
|
|
186
|
+
resolve(ev.data);
|
|
187
|
+
}
|
|
124
188
|
};
|
|
125
189
|
try {
|
|
126
|
-
const
|
|
127
|
-
if (!
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
190
|
+
const port = this.sharedWorkerPort;
|
|
191
|
+
if (!port) {
|
|
192
|
+
clearTimeout(timer);
|
|
193
|
+
return reject(new Error("SharedWorker port not available"));
|
|
194
|
+
}
|
|
195
|
+
port.postMessage({ type, ...payload || {} }, [mc.port2]);
|
|
196
|
+
} catch (e) {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
reject(e instanceof Error ? e : new Error(String(e)));
|
|
132
199
|
}
|
|
133
200
|
});
|
|
201
|
+
}
|
|
134
202
|
if (this.workerType === "service" && this.serviceWorkerRegistration) {
|
|
135
|
-
const
|
|
136
|
-
if (!
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
203
|
+
const reg = this.serviceWorkerRegistration;
|
|
204
|
+
if (!reg)
|
|
205
|
+
throw new Error("Service worker registration missing");
|
|
206
|
+
if (!reg.active) {
|
|
207
|
+
await navigator.serviceWorker.ready;
|
|
208
|
+
if (!reg.active) {
|
|
209
|
+
throw new Error("Service worker not active");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return new Promise((resolve, reject) => {
|
|
213
|
+
const mc = new MessageChannel();
|
|
214
|
+
const timer = setTimeout(() => {
|
|
215
|
+
mc.port1.onmessage = null;
|
|
216
|
+
reject(new Error("Request timeout"));
|
|
217
|
+
}, timeoutMs);
|
|
218
|
+
mc.port1.onmessage = (ev) => {
|
|
219
|
+
clearTimeout(timer);
|
|
220
|
+
if (ev.data && ev.data.success) {
|
|
221
|
+
resolve(ev.data);
|
|
222
|
+
} else if (ev.data && ev.data.success === false) {
|
|
223
|
+
reject(new Error(ev.data.error || "Worker error"));
|
|
224
|
+
} else {
|
|
225
|
+
resolve(ev.data);
|
|
226
|
+
}
|
|
145
227
|
};
|
|
146
228
|
try {
|
|
147
|
-
const
|
|
148
|
-
if (!
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
229
|
+
const active = reg.active;
|
|
230
|
+
if (!active) {
|
|
231
|
+
clearTimeout(timer);
|
|
232
|
+
return reject(new Error("Service worker active instance not available"));
|
|
233
|
+
}
|
|
234
|
+
active.postMessage({ type, ...payload || {} }, [mc.port2]);
|
|
235
|
+
} catch (e) {
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
reject(e instanceof Error ? e : new Error(String(e)));
|
|
153
238
|
}
|
|
154
239
|
});
|
|
155
240
|
}
|
|
156
241
|
throw new Error("No worker registered");
|
|
157
242
|
}
|
|
158
243
|
// Fire-and-forget postMessage (no response expected)
|
|
159
|
-
async post(
|
|
244
|
+
async post(type, payload) {
|
|
160
245
|
if (this.workerType === "shared" && this.sharedWorkerPort) {
|
|
161
246
|
try {
|
|
162
|
-
this.sharedWorkerPort.postMessage({ type
|
|
163
|
-
} catch (
|
|
164
|
-
console.error("[WorkerClient] Failed to post to SharedWorker:",
|
|
247
|
+
this.sharedWorkerPort.postMessage({ type, ...payload || {} });
|
|
248
|
+
} catch (e) {
|
|
249
|
+
console.error("[WorkerClient] Failed to post to SharedWorker:", e);
|
|
165
250
|
}
|
|
166
251
|
return;
|
|
167
252
|
}
|
|
168
253
|
if (this.workerType === "service" && this.serviceWorkerRegistration?.active) {
|
|
169
254
|
try {
|
|
170
|
-
this.serviceWorkerRegistration.active.postMessage({ type
|
|
171
|
-
} catch (
|
|
172
|
-
console.error("[WorkerClient] Failed to post to ServiceWorker (active):",
|
|
255
|
+
this.serviceWorkerRegistration.active.postMessage({ type, ...payload || {} });
|
|
256
|
+
} catch (e) {
|
|
257
|
+
console.error("[WorkerClient] Failed to post to ServiceWorker (active):", e);
|
|
173
258
|
}
|
|
174
259
|
return;
|
|
175
260
|
}
|
|
176
261
|
if ("serviceWorker" in navigator && navigator.serviceWorker.controller) {
|
|
177
262
|
try {
|
|
178
|
-
navigator.serviceWorker.controller.postMessage({ type
|
|
179
|
-
} catch (
|
|
180
|
-
console.error("[WorkerClient] Failed to post to ServiceWorker.controller:",
|
|
263
|
+
navigator.serviceWorker.controller.postMessage({ type, ...payload || {} });
|
|
264
|
+
} catch (e) {
|
|
265
|
+
console.error("[WorkerClient] Failed to post to ServiceWorker.controller:", e);
|
|
181
266
|
}
|
|
182
267
|
return;
|
|
183
268
|
}
|
|
184
|
-
if (
|
|
185
|
-
const
|
|
186
|
-
typeof
|
|
269
|
+
if (type === "SET_AUTH_TOKEN" && payload) {
|
|
270
|
+
const token = payload["token"];
|
|
271
|
+
if (typeof token === "string")
|
|
272
|
+
this.pendingAuthToken = token;
|
|
187
273
|
}
|
|
188
274
|
}
|
|
189
|
-
sendAuthTokenToServiceWorker(
|
|
190
|
-
if (this.serviceWorkerRegistration?.active)
|
|
275
|
+
sendAuthTokenToServiceWorker(token) {
|
|
276
|
+
if (this.serviceWorkerRegistration?.active) {
|
|
191
277
|
try {
|
|
192
|
-
this.serviceWorkerRegistration.active.postMessage({ type: "SET_AUTH_TOKEN", token
|
|
193
|
-
} catch (
|
|
194
|
-
console.error("[WorkerClient] Failed to send auth token to ServiceWorker:",
|
|
278
|
+
this.serviceWorkerRegistration.active.postMessage({ type: "SET_AUTH_TOKEN", token });
|
|
279
|
+
} catch (e) {
|
|
280
|
+
console.error("[WorkerClient] Failed to send auth token to ServiceWorker:", e);
|
|
195
281
|
}
|
|
196
|
-
else if ("serviceWorker" in navigator && navigator.serviceWorker.controller)
|
|
282
|
+
} else if ("serviceWorker" in navigator && navigator.serviceWorker.controller) {
|
|
197
283
|
try {
|
|
198
|
-
navigator.serviceWorker.controller.postMessage({ type: "SET_AUTH_TOKEN", token
|
|
199
|
-
} catch (
|
|
200
|
-
console.error("[WorkerClient] Failed to send auth token to ServiceWorker.controller:",
|
|
284
|
+
navigator.serviceWorker.controller.postMessage({ type: "SET_AUTH_TOKEN", token });
|
|
285
|
+
} catch (e) {
|
|
286
|
+
console.error("[WorkerClient] Failed to send auth token to ServiceWorker.controller:", e);
|
|
201
287
|
}
|
|
202
|
-
else
|
|
203
|
-
this.pendingAuthToken =
|
|
288
|
+
} else {
|
|
289
|
+
this.pendingAuthToken = token;
|
|
290
|
+
}
|
|
204
291
|
}
|
|
205
292
|
// Subscription API for consumers to listen for connection status updates
|
|
206
|
-
onConnectionStatus(
|
|
207
|
-
this.connectionStatusCallbacks.add(
|
|
293
|
+
onConnectionStatus(cb) {
|
|
294
|
+
this.connectionStatusCallbacks.add(cb);
|
|
208
295
|
}
|
|
209
|
-
offConnectionStatus(
|
|
210
|
-
this.connectionStatusCallbacks.delete(
|
|
296
|
+
offConnectionStatus(cb) {
|
|
297
|
+
this.connectionStatusCallbacks.delete(cb);
|
|
211
298
|
}
|
|
212
299
|
async getConnectionStatus() {
|
|
213
300
|
try {
|
|
214
|
-
const
|
|
215
|
-
|
|
301
|
+
const res = await this.request("GET_CONNECTION_STATUS", void 0, 2e3);
|
|
302
|
+
if (res && typeof res === "object" && "connected" in res)
|
|
303
|
+
return !!res.connected;
|
|
304
|
+
return !!res.connected;
|
|
216
305
|
} catch {
|
|
217
|
-
return
|
|
306
|
+
return false;
|
|
218
307
|
}
|
|
219
308
|
}
|
|
220
|
-
setAuthToken(
|
|
221
|
-
|
|
309
|
+
setAuthToken(token) {
|
|
310
|
+
this.pendingAuthToken = token;
|
|
311
|
+
if (this.workerType === "shared" && this.sharedWorkerPort) {
|
|
222
312
|
try {
|
|
223
|
-
this.sharedWorkerPort.postMessage({ type: "SET_AUTH_TOKEN", token
|
|
224
|
-
|
|
225
|
-
|
|
313
|
+
this.sharedWorkerPort.postMessage({ type: "SET_AUTH_TOKEN", token });
|
|
314
|
+
this.pendingAuthToken = null;
|
|
315
|
+
} catch (e) {
|
|
316
|
+
console.error("[WorkerClient] Failed to set auth token on SharedWorker:", e);
|
|
226
317
|
}
|
|
227
|
-
else this.workerType === "service"
|
|
318
|
+
} else if (this.workerType === "service") {
|
|
319
|
+
this.sendAuthTokenToServiceWorker(token);
|
|
320
|
+
this.pendingAuthToken = null;
|
|
321
|
+
} else {
|
|
322
|
+
}
|
|
228
323
|
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// libs/mcp-worker/src/lib/database.ts
|
|
327
|
+
var DB_NAME = "user-activity-db";
|
|
328
|
+
var DB_VERSION = 1;
|
|
329
|
+
var STORE_NAME = "user-events";
|
|
330
|
+
async function initDB() {
|
|
331
|
+
return new Promise((resolve, reject) => {
|
|
332
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
333
|
+
request.onerror = () => reject(request.error);
|
|
334
|
+
request.onsuccess = () => resolve(request.result);
|
|
335
|
+
request.onupgradeneeded = (event) => {
|
|
336
|
+
const db = event.target.result;
|
|
337
|
+
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
338
|
+
const store = db.createObjectStore(STORE_NAME, { keyPath: "id" });
|
|
339
|
+
store.createIndex("timestamp", "timestamp", { unique: false });
|
|
340
|
+
store.createIndex("type", "type", { unique: false });
|
|
341
|
+
store.createIndex("path", "path", { unique: false });
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
async function queryEvents(filters) {
|
|
347
|
+
const db = await initDB();
|
|
348
|
+
const transaction = db.transaction([STORE_NAME], "readonly");
|
|
349
|
+
const store = transaction.objectStore(STORE_NAME);
|
|
350
|
+
const index = store.index("timestamp");
|
|
351
|
+
return new Promise((resolve, reject) => {
|
|
352
|
+
const request = index.getAll();
|
|
353
|
+
request.onsuccess = () => {
|
|
354
|
+
let events = request.result;
|
|
355
|
+
if (filters?.type) {
|
|
356
|
+
events = events.filter((e) => e.type === filters.type);
|
|
357
|
+
}
|
|
358
|
+
if (filters?.startTime) {
|
|
359
|
+
events = events.filter((e) => e.timestamp >= filters.startTime);
|
|
360
|
+
}
|
|
361
|
+
if (filters?.endTime) {
|
|
362
|
+
events = events.filter((e) => e.timestamp <= filters.endTime);
|
|
363
|
+
}
|
|
364
|
+
if (filters?.path) {
|
|
365
|
+
events = events.filter((e) => e.path?.includes(filters.path));
|
|
366
|
+
}
|
|
367
|
+
events.sort((a, b) => b.timestamp - a.timestamp);
|
|
368
|
+
if (filters?.limit) {
|
|
369
|
+
events = events.slice(0, filters.limit);
|
|
370
|
+
}
|
|
371
|
+
resolve(events);
|
|
372
|
+
};
|
|
373
|
+
request.onerror = () => reject(request.error);
|
|
374
|
+
});
|
|
229
375
|
}
|
|
230
376
|
export {
|
|
231
|
-
|
|
232
|
-
|
|
377
|
+
WorkerClient,
|
|
378
|
+
queryEvents
|
|
233
379
|
};
|
|
234
|
-
//# sourceMappingURL=index.js.map
|