@flash-debug/silkworm 0.0.0 → 0.5.0
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/.output/nitro.json +18 -0
- package/.output/public/assets/index-Bw-GBuTw.css +1 -0
- package/.output/public/assets/index-CHthEL3v.js +6 -0
- package/.output/public/icons/silkworm/favicon-16.png +0 -0
- package/.output/public/icons/silkworm/favicon-32.png +0 -0
- package/.output/public/icons/silkworm/icon-128.png +0 -0
- package/.output/public/icons/silkworm/icon-512.png +0 -0
- package/.output/public/index.html +14 -0
- package/.output/server/_chunks/renderer-template.mjs +8 -0
- package/.output/server/_chunks/state.mjs +568 -0
- package/.output/server/_libs/h3+rou3+srvx.mjs +1597 -0
- package/.output/server/_libs/h3.mjs +139 -0
- package/.output/server/_libs/h32.mjs +48 -0
- package/.output/server/_routes/api/silkworm/auto/pause.mjs +8 -0
- package/.output/server/_routes/api/silkworm/auto/start.mjs +17 -0
- package/.output/server/_routes/api/silkworm/auto/stop.mjs +8 -0
- package/.output/server/_routes/api/silkworm/connect.mjs +21 -0
- package/.output/server/_routes/api/silkworm/history/clear.mjs +8 -0
- package/.output/server/_routes/api/silkworm/manual/send.mjs +23 -0
- package/.output/server/_routes/api/silkworm/state.mjs +5 -0
- package/.output/server/_routes/api/silkworm/stream.mjs +41 -0
- package/.output/server/_routes/api/silkworm/test.mjs +18 -0
- package/.output/server/index.mjs +542 -0
- package/dist/assets/index-Bw-GBuTw.css +1 -0
- package/dist/assets/index-CHthEL3v.js +6 -0
- package/dist/icons/silkworm/favicon-16.png +0 -0
- package/dist/icons/silkworm/favicon-32.png +0 -0
- package/dist/icons/silkworm/icon-128.png +0 -0
- package/dist/icons/silkworm/icon-512.png +0 -0
- package/dist/index.html +14 -0
- package/index.html +13 -0
- package/nitro.config.ts +12 -0
- package/package.json +42 -8
- package/public/icons/silkworm/favicon-16.png +0 -0
- package/public/icons/silkworm/favicon-32.png +0 -0
- package/public/icons/silkworm/icon-128.png +0 -0
- package/public/icons/silkworm/icon-512.png +0 -0
- package/scripts/dev.ts +56 -0
- package/server/routes/api/silkworm/auto/pause.post.ts +7 -0
- package/server/routes/api/silkworm/auto/start.post.ts +18 -0
- package/server/routes/api/silkworm/auto/stop.post.ts +7 -0
- package/server/routes/api/silkworm/connect.post.ts +28 -0
- package/server/routes/api/silkworm/history/clear.post.ts +7 -0
- package/server/routes/api/silkworm/manual/send.post.ts +38 -0
- package/server/routes/api/silkworm/state.get.ts +4 -0
- package/server/routes/api/silkworm/stream.get.ts +59 -0
- package/server/routes/api/silkworm/test.post.ts +18 -0
- package/server/state.ts +3 -0
- package/src/App.vue +418 -0
- package/src/IconGlyph.vue +33 -0
- package/src/main.ts +5 -0
- package/src/runner/random.ts +29 -0
- package/src/runner/runtime.ts +396 -0
- package/src/runner/transport.ts +109 -0
- package/src/runner/types.ts +118 -0
- package/src/scenarios/index.ts +187 -0
- package/src/styles.css +172 -0
- package/tsconfig.json +9 -0
- package/vite.config.ts +17 -0
- package/index.js +0 -1
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
// src/scenarios/index.ts
|
|
2
|
+
const levels = [
|
|
3
|
+
"debug",
|
|
4
|
+
"info",
|
|
5
|
+
"warning",
|
|
6
|
+
"error"
|
|
7
|
+
];
|
|
8
|
+
const types = [
|
|
9
|
+
"log",
|
|
10
|
+
"network",
|
|
11
|
+
"exception",
|
|
12
|
+
"performance",
|
|
13
|
+
"event"
|
|
14
|
+
];
|
|
15
|
+
const sources = [
|
|
16
|
+
"flutter",
|
|
17
|
+
"adb",
|
|
18
|
+
"server"
|
|
19
|
+
];
|
|
20
|
+
const formats = [
|
|
21
|
+
"json",
|
|
22
|
+
"jsonl",
|
|
23
|
+
"yaml",
|
|
24
|
+
"ansi",
|
|
25
|
+
"text"
|
|
26
|
+
];
|
|
27
|
+
function baseRecord(context, overrides = {}) {
|
|
28
|
+
return {
|
|
29
|
+
id: context.id,
|
|
30
|
+
timestamp: context.timestamp,
|
|
31
|
+
level: "info",
|
|
32
|
+
type: "log",
|
|
33
|
+
source: "flutter",
|
|
34
|
+
deviceId: context.deviceId,
|
|
35
|
+
message: `silkworm event ${context.sequence}`,
|
|
36
|
+
content: `Generated by ${context.scenario}`,
|
|
37
|
+
contentFormat: "text",
|
|
38
|
+
...overrides
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function contentFor(format, context) {
|
|
42
|
+
if (format === "json") return JSON.stringify({
|
|
43
|
+
requestId: `req-${context.sequence}`,
|
|
44
|
+
ok: context.random.bool(.8),
|
|
45
|
+
durationMs: context.random.int(2, 680)
|
|
46
|
+
}, null, 2);
|
|
47
|
+
if (format === "jsonl") return JSON.stringify({
|
|
48
|
+
event: "response",
|
|
49
|
+
sequence: context.sequence,
|
|
50
|
+
ok: true
|
|
51
|
+
});
|
|
52
|
+
if (format === "yaml") return `requestId: req-${context.sequence}\nok: true\ndurationMs: ${context.random.int(2, 680)}`;
|
|
53
|
+
if (format === "ansi") return "\x1B[32m[200]\x1B[0m GET /api/health 18ms";
|
|
54
|
+
return `A plain text payload produced by Silkworm at sequence ${context.sequence}.`;
|
|
55
|
+
}
|
|
56
|
+
function buildRecord(context, overrides = {}) {
|
|
57
|
+
const level = overrides.level ?? context.random.pick(levels);
|
|
58
|
+
const type = overrides.type ?? context.random.pick(types);
|
|
59
|
+
const format = overrides.contentFormat ?? context.random.pick(formats);
|
|
60
|
+
return baseRecord(context, {
|
|
61
|
+
level,
|
|
62
|
+
type,
|
|
63
|
+
contentFormat: format,
|
|
64
|
+
content: contentFor(format, context),
|
|
65
|
+
message: `${type} event from ${overrides.source ?? "flutter"}`,
|
|
66
|
+
...overrides
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function createScenarioRecord(context) {
|
|
70
|
+
switch (context.scenario) {
|
|
71
|
+
case "normal": return buildRecord(context, {
|
|
72
|
+
level: context.random.pick([
|
|
73
|
+
"debug",
|
|
74
|
+
"info",
|
|
75
|
+
"info",
|
|
76
|
+
"warning",
|
|
77
|
+
"error"
|
|
78
|
+
]),
|
|
79
|
+
type: "log",
|
|
80
|
+
source: "flutter",
|
|
81
|
+
contentFormat: "text",
|
|
82
|
+
message: context.random.pick([
|
|
83
|
+
"screen mounted",
|
|
84
|
+
"button pressed",
|
|
85
|
+
"cache refreshed",
|
|
86
|
+
"request completed"
|
|
87
|
+
]),
|
|
88
|
+
content: `Flutter debug message ${context.sequence}`
|
|
89
|
+
});
|
|
90
|
+
case "mixed-sources": return buildRecord(context, {
|
|
91
|
+
source: context.random.pick(sources),
|
|
92
|
+
tag: context.random.pick([
|
|
93
|
+
"App",
|
|
94
|
+
"Network",
|
|
95
|
+
"ADB",
|
|
96
|
+
"Router"
|
|
97
|
+
])
|
|
98
|
+
});
|
|
99
|
+
case "content-formats": {
|
|
100
|
+
const contentFormat = context.random.pick(formats);
|
|
101
|
+
return buildRecord(context, {
|
|
102
|
+
contentFormat,
|
|
103
|
+
content: contentFor(contentFormat, context)
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
case "network": return buildRecord(context, {
|
|
107
|
+
type: "network",
|
|
108
|
+
level: context.random.bool(.82) ? "info" : "error",
|
|
109
|
+
contentFormat: "json",
|
|
110
|
+
message: context.random.pick([
|
|
111
|
+
"GET /api/health",
|
|
112
|
+
"POST /api/v1/logs/batch",
|
|
113
|
+
"GET /users/me"
|
|
114
|
+
]),
|
|
115
|
+
attributes: {
|
|
116
|
+
method: context.random.pick([
|
|
117
|
+
"GET",
|
|
118
|
+
"POST",
|
|
119
|
+
"PUT"
|
|
120
|
+
]),
|
|
121
|
+
statusCode: context.random.bool(.85) ? 200 : context.random.pick([
|
|
122
|
+
400,
|
|
123
|
+
429,
|
|
124
|
+
500
|
|
125
|
+
]),
|
|
126
|
+
durationMs: context.random.int(2, 680)
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
case "exceptions": return buildRecord(context, {
|
|
130
|
+
type: "exception",
|
|
131
|
+
level: "error",
|
|
132
|
+
contentFormat: "text",
|
|
133
|
+
message: context.random.pick([
|
|
134
|
+
"Unhandled exception",
|
|
135
|
+
"TimeoutException",
|
|
136
|
+
"StateError"
|
|
137
|
+
]),
|
|
138
|
+
error: context.random.pick([
|
|
139
|
+
"TimeoutException: request timed out",
|
|
140
|
+
"StateError: invalid state",
|
|
141
|
+
"Error: failed to decode response"
|
|
142
|
+
]),
|
|
143
|
+
stackTrace: "at SilkwormScenario.generate (scenario.ts:42:11)\nat Logger.send (logger.ts:18:5)"
|
|
144
|
+
});
|
|
145
|
+
case "burst": return buildRecord(context, {
|
|
146
|
+
type: "event",
|
|
147
|
+
level: "info",
|
|
148
|
+
contentFormat: "jsonl",
|
|
149
|
+
message: `burst event ${context.sequence}`
|
|
150
|
+
});
|
|
151
|
+
case "duplicates": return buildRecord(context, {
|
|
152
|
+
id: `${context.clientId}-duplicate-${context.sequence % 3}`,
|
|
153
|
+
type: "event",
|
|
154
|
+
level: "info",
|
|
155
|
+
contentFormat: "json"
|
|
156
|
+
});
|
|
157
|
+
case "invalid":
|
|
158
|
+
if (context.sequence % 3 === 0) return {
|
|
159
|
+
timestamp: context.timestamp,
|
|
160
|
+
message: "missing id and required fields"
|
|
161
|
+
};
|
|
162
|
+
if (context.sequence % 3 === 1) return {
|
|
163
|
+
id: context.id,
|
|
164
|
+
timestamp: context.timestamp,
|
|
165
|
+
level: "unknown",
|
|
166
|
+
type: "unknown",
|
|
167
|
+
message: ""
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
id: context.id,
|
|
171
|
+
timestamp: context.timestamp,
|
|
172
|
+
message: "oversized payload",
|
|
173
|
+
content: "x".repeat(4096)
|
|
174
|
+
};
|
|
175
|
+
case "multi-device": return buildRecord(context, {
|
|
176
|
+
deviceId: `pixel-${context.random.int(5, 9)}`,
|
|
177
|
+
source: context.random.pick(["flutter", "adb"])
|
|
178
|
+
});
|
|
179
|
+
case "long-run": return buildRecord(context, {
|
|
180
|
+
level: context.random.pick([
|
|
181
|
+
"debug",
|
|
182
|
+
"info",
|
|
183
|
+
"info"
|
|
184
|
+
]),
|
|
185
|
+
type: "performance",
|
|
186
|
+
contentFormat: "text",
|
|
187
|
+
message: "heartbeat",
|
|
188
|
+
content: `Long run heartbeat ${context.sequence}`
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// src/runner/random.ts
|
|
193
|
+
var SeededRandom = class {
|
|
194
|
+
state;
|
|
195
|
+
constructor(seed) {
|
|
196
|
+
let hash = 2166136261;
|
|
197
|
+
for (const character of seed || "silkworm") {
|
|
198
|
+
hash ^= character.charCodeAt(0);
|
|
199
|
+
hash = Math.imul(hash, 16777619);
|
|
200
|
+
}
|
|
201
|
+
this.state = hash >>> 0;
|
|
202
|
+
}
|
|
203
|
+
next() {
|
|
204
|
+
this.state = Math.imul(1664525, this.state) + 1013904223 >>> 0;
|
|
205
|
+
return this.state / 4294967296;
|
|
206
|
+
}
|
|
207
|
+
int(min, max) {
|
|
208
|
+
return Math.floor(this.next() * (max - min + 1)) + min;
|
|
209
|
+
}
|
|
210
|
+
pick(items) {
|
|
211
|
+
return items[this.int(0, items.length - 1)];
|
|
212
|
+
}
|
|
213
|
+
bool(probability = .5) {
|
|
214
|
+
return this.next() < probability;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
// src/runner/transport.ts
|
|
218
|
+
var TransportError = class extends Error {
|
|
219
|
+
statusCode;
|
|
220
|
+
constructor(message, statusCode) {
|
|
221
|
+
super(message);
|
|
222
|
+
this.name = "TransportError";
|
|
223
|
+
this.statusCode = statusCode;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
function targetUrl(connection, path) {
|
|
227
|
+
const baseUrl = connection.baseUrl.trim();
|
|
228
|
+
if (!baseUrl) throw new TransportError("Target Base URL is required");
|
|
229
|
+
return new URL(path, `${baseUrl.replace(/\/$/, "")}/`).toString();
|
|
230
|
+
}
|
|
231
|
+
async function readPayload(response) {
|
|
232
|
+
const text = await response.text();
|
|
233
|
+
if (!text) return {};
|
|
234
|
+
try {
|
|
235
|
+
return JSON.parse(text);
|
|
236
|
+
} catch {
|
|
237
|
+
return { message: text };
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async function testTarget(connection) {
|
|
241
|
+
const startedAt = Date.now();
|
|
242
|
+
let response;
|
|
243
|
+
try {
|
|
244
|
+
response = await fetch(targetUrl(connection, "/healthz"), { signal: AbortSignal.timeout(3e3) });
|
|
245
|
+
} catch (error) {
|
|
246
|
+
throw new TransportError(error instanceof Error ? error.message : String(error));
|
|
247
|
+
}
|
|
248
|
+
if (!response.ok) throw new TransportError(`Target health check failed with HTTP ${response.status}`, response.status);
|
|
249
|
+
const payload = await readPayload(response);
|
|
250
|
+
if (payload.ok !== true || payload.service !== "flash-debug") throw new TransportError("Target health check returned an invalid response", response.status);
|
|
251
|
+
return {
|
|
252
|
+
statusCode: response.status,
|
|
253
|
+
latencyMs: Date.now() - startedAt
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
async function sendBatch(connection, items) {
|
|
257
|
+
let response;
|
|
258
|
+
try {
|
|
259
|
+
response = await fetch(targetUrl(connection, "/api/v1/logs/batch"), {
|
|
260
|
+
method: "POST",
|
|
261
|
+
headers: {
|
|
262
|
+
"Content-Type": "application/json",
|
|
263
|
+
...connection.token.trim() ? { Authorization: `Bearer ${connection.token.trim()}` } : {}
|
|
264
|
+
},
|
|
265
|
+
body: JSON.stringify({
|
|
266
|
+
clientId: connection.clientId,
|
|
267
|
+
items
|
|
268
|
+
}),
|
|
269
|
+
signal: AbortSignal.timeout(1e4)
|
|
270
|
+
});
|
|
271
|
+
} catch (error) {
|
|
272
|
+
throw new TransportError(error instanceof Error ? error.message : String(error));
|
|
273
|
+
}
|
|
274
|
+
const payload = await readPayload(response);
|
|
275
|
+
if (!response.ok) throw new TransportError(typeof payload.message === "string" ? payload.message : `Target rejected request with HTTP ${response.status}`, response.status);
|
|
276
|
+
if (typeof payload.accepted !== "number") throw new TransportError("Target returned an invalid logs response", response.status);
|
|
277
|
+
return {
|
|
278
|
+
accepted: typeof payload.accepted === "number" ? payload.accepted : 0,
|
|
279
|
+
cursor: typeof payload.cursor === "number" ? payload.cursor : void 0,
|
|
280
|
+
statusCode: response.status
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
// src/runner/runtime.ts
|
|
284
|
+
const defaultAutoConfig = {
|
|
285
|
+
scenario: "normal",
|
|
286
|
+
rate: 2,
|
|
287
|
+
batchSize: 1,
|
|
288
|
+
durationSeconds: 0,
|
|
289
|
+
seed: "silkworm",
|
|
290
|
+
clientId: "silkworm-01",
|
|
291
|
+
deviceId: "silkworm-device",
|
|
292
|
+
flushErrors: true
|
|
293
|
+
};
|
|
294
|
+
const defaultConnection = {
|
|
295
|
+
baseUrl: `http://127.0.0.1:4517`,
|
|
296
|
+
token: "",
|
|
297
|
+
clientId: "silkworm-01",
|
|
298
|
+
deviceId: "silkworm-device",
|
|
299
|
+
source: "flutter"
|
|
300
|
+
};
|
|
301
|
+
function cloneStats() {
|
|
302
|
+
return {
|
|
303
|
+
generated: 0,
|
|
304
|
+
queued: 0,
|
|
305
|
+
accepted: 0,
|
|
306
|
+
failed: 0,
|
|
307
|
+
duplicate: 0,
|
|
308
|
+
retrying: 0,
|
|
309
|
+
queueLength: 0
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function publicConnection(connection, state) {
|
|
313
|
+
return {
|
|
314
|
+
baseUrl: connection.baseUrl,
|
|
315
|
+
clientId: connection.clientId,
|
|
316
|
+
deviceId: connection.deviceId,
|
|
317
|
+
source: connection.source,
|
|
318
|
+
hasToken: Boolean(connection.token),
|
|
319
|
+
...state
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
var SilkwormRuntime = class {
|
|
323
|
+
connection = { ...defaultConnection };
|
|
324
|
+
connectionState = { connected: false };
|
|
325
|
+
autoStatus = "idle";
|
|
326
|
+
autoConfig = { ...defaultAutoConfig };
|
|
327
|
+
autoRunId = "";
|
|
328
|
+
autoSequence = 0;
|
|
329
|
+
autoRandom = new SeededRandom(defaultAutoConfig.seed);
|
|
330
|
+
autoRateCarry = 0;
|
|
331
|
+
autoPending = [];
|
|
332
|
+
autoTimer;
|
|
333
|
+
autoDurationTimer;
|
|
334
|
+
queue = Promise.resolve();
|
|
335
|
+
history = [];
|
|
336
|
+
stats = cloneStats();
|
|
337
|
+
lastError;
|
|
338
|
+
listeners = /* @__PURE__ */ new Set();
|
|
339
|
+
snapshot() {
|
|
340
|
+
const auto = {
|
|
341
|
+
status: this.autoStatus,
|
|
342
|
+
sequence: this.autoSequence,
|
|
343
|
+
config: { ...this.autoConfig },
|
|
344
|
+
...this.autoRunId ? { runId: this.autoRunId } : {}
|
|
345
|
+
};
|
|
346
|
+
return {
|
|
347
|
+
connection: publicConnection(this.connection, this.connectionState),
|
|
348
|
+
auto,
|
|
349
|
+
stats: { ...this.stats },
|
|
350
|
+
history: this.history.slice(),
|
|
351
|
+
...this.lastError ? { lastError: this.lastError } : {}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
subscribe(listener) {
|
|
355
|
+
this.listeners.add(listener);
|
|
356
|
+
listener(this.snapshot());
|
|
357
|
+
return () => this.listeners.delete(listener);
|
|
358
|
+
}
|
|
359
|
+
configure(connection) {
|
|
360
|
+
this.connection = {
|
|
361
|
+
...this.connection,
|
|
362
|
+
...connection,
|
|
363
|
+
baseUrl: connection.baseUrl?.trim() || this.connection.baseUrl,
|
|
364
|
+
clientId: connection.clientId?.trim() || this.connection.clientId,
|
|
365
|
+
deviceId: connection.deviceId?.trim() || this.connection.deviceId
|
|
366
|
+
};
|
|
367
|
+
this.connectionState = { connected: false };
|
|
368
|
+
this.emit();
|
|
369
|
+
return this.snapshot();
|
|
370
|
+
}
|
|
371
|
+
async testConnection() {
|
|
372
|
+
try {
|
|
373
|
+
const result = await testTarget(this.connection);
|
|
374
|
+
this.connectionState = {
|
|
375
|
+
connected: true,
|
|
376
|
+
lastStatus: result.statusCode,
|
|
377
|
+
lastLatencyMs: result.latencyMs
|
|
378
|
+
};
|
|
379
|
+
this.lastError = void 0;
|
|
380
|
+
} catch (error) {
|
|
381
|
+
this.connectionState = {
|
|
382
|
+
connected: false,
|
|
383
|
+
lastStatus: error instanceof TransportError ? error.statusCode : void 0,
|
|
384
|
+
lastError: error instanceof Error ? error.message : String(error)
|
|
385
|
+
};
|
|
386
|
+
this.lastError = this.connectionState.lastError;
|
|
387
|
+
throw error;
|
|
388
|
+
}
|
|
389
|
+
this.emit();
|
|
390
|
+
return this.snapshot();
|
|
391
|
+
}
|
|
392
|
+
clearHistory() {
|
|
393
|
+
this.history = [];
|
|
394
|
+
this.stats = cloneStats();
|
|
395
|
+
this.lastError = void 0;
|
|
396
|
+
this.emit();
|
|
397
|
+
return this.snapshot();
|
|
398
|
+
}
|
|
399
|
+
async startAuto(config = {}) {
|
|
400
|
+
if (this.autoStatus === "running" || this.autoStatus === "starting") return this.snapshot();
|
|
401
|
+
if (!this.connectionState.connected) throw new Error("Connect to a target before starting Auto Mode");
|
|
402
|
+
this.autoConfig = {
|
|
403
|
+
...this.autoConfig,
|
|
404
|
+
...config,
|
|
405
|
+
rate: Math.min(100, Math.max(.1, Number(config.rate ?? this.autoConfig.rate))),
|
|
406
|
+
batchSize: Math.min(100, Math.max(1, Math.floor(Number(config.batchSize ?? this.autoConfig.batchSize)))),
|
|
407
|
+
durationSeconds: Math.max(0, Number(config.durationSeconds ?? this.autoConfig.durationSeconds)),
|
|
408
|
+
clientId: config.clientId?.trim() || this.autoConfig.clientId,
|
|
409
|
+
deviceId: config.deviceId?.trim() || this.autoConfig.deviceId,
|
|
410
|
+
seed: config.seed?.trim() || this.autoConfig.seed
|
|
411
|
+
};
|
|
412
|
+
if (!this.autoRunId || this.autoStatus === "completed" || this.autoStatus === "stopped" || this.autoStatus === "failed") {
|
|
413
|
+
this.autoRunId = `${this.autoConfig.clientId}-${Date.now().toString(36)}`;
|
|
414
|
+
this.autoSequence = 0;
|
|
415
|
+
this.autoRandom = new SeededRandom(this.autoConfig.seed);
|
|
416
|
+
this.autoRateCarry = 0;
|
|
417
|
+
this.autoPending = [];
|
|
418
|
+
}
|
|
419
|
+
this.autoStatus = "starting";
|
|
420
|
+
this.emit();
|
|
421
|
+
this.autoStatus = "running";
|
|
422
|
+
this.startAutoTimer();
|
|
423
|
+
if (this.autoConfig.durationSeconds > 0) this.autoDurationTimer = setTimeout(() => this.completeAuto(), this.autoConfig.durationSeconds * 1e3);
|
|
424
|
+
this.emit();
|
|
425
|
+
return this.snapshot();
|
|
426
|
+
}
|
|
427
|
+
pauseAuto() {
|
|
428
|
+
if (this.autoStatus === "running") {
|
|
429
|
+
this.clearAutoTimers();
|
|
430
|
+
this.autoStatus = "paused";
|
|
431
|
+
this.emit();
|
|
432
|
+
}
|
|
433
|
+
return this.snapshot();
|
|
434
|
+
}
|
|
435
|
+
stopAuto() {
|
|
436
|
+
if (this.autoStatus !== "idle") {
|
|
437
|
+
this.clearAutoTimers();
|
|
438
|
+
this.autoStatus = "stopped";
|
|
439
|
+
this.emit();
|
|
440
|
+
}
|
|
441
|
+
return this.snapshot();
|
|
442
|
+
}
|
|
443
|
+
async sendManual(items, options = {}) {
|
|
444
|
+
await this.enqueue("manual", items, options.summary ?? "Manual event", options.duplicate);
|
|
445
|
+
return this.snapshot();
|
|
446
|
+
}
|
|
447
|
+
startAutoTimer() {
|
|
448
|
+
this.clearAutoTimers();
|
|
449
|
+
const intervalMs = 100;
|
|
450
|
+
this.autoTimer = setInterval(() => {
|
|
451
|
+
this.autoRateCarry += this.autoConfig.rate / (1e3 / intervalMs);
|
|
452
|
+
const eventsPerTick = Math.floor(this.autoRateCarry);
|
|
453
|
+
this.autoRateCarry -= eventsPerTick;
|
|
454
|
+
if (eventsPerTick > 0) this.autoPending.push(...Array.from({ length: eventsPerTick }, () => this.nextAutoRecord()));
|
|
455
|
+
while (this.autoPending.length >= this.autoConfig.batchSize) {
|
|
456
|
+
const items = this.autoPending.splice(0, this.autoConfig.batchSize);
|
|
457
|
+
this.enqueueAutoBatch(items);
|
|
458
|
+
}
|
|
459
|
+
}, intervalMs);
|
|
460
|
+
}
|
|
461
|
+
enqueueAutoBatch(items) {
|
|
462
|
+
if (this.autoConfig.flushErrors) {
|
|
463
|
+
const errors = items.filter((item) => typeof item === "object" && item !== null && item.level === "error");
|
|
464
|
+
const regular = items.filter((item) => !(typeof item === "object" && item !== null && item.level === "error"));
|
|
465
|
+
if (errors.length > 0) this.enqueue("auto", errors, `${this.autoConfig.scenario} error`);
|
|
466
|
+
if (regular.length > 0) this.enqueue("auto", regular, `${this.autoConfig.scenario} scenario`);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
this.enqueue("auto", items, `${this.autoConfig.scenario} scenario`);
|
|
470
|
+
}
|
|
471
|
+
nextAutoRecord() {
|
|
472
|
+
this.autoSequence += 1;
|
|
473
|
+
return createScenarioRecord({
|
|
474
|
+
id: `${this.autoRunId}-${this.autoSequence}`,
|
|
475
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
476
|
+
sequence: this.autoSequence,
|
|
477
|
+
clientId: this.autoConfig.clientId,
|
|
478
|
+
deviceId: this.autoConfig.deviceId,
|
|
479
|
+
scenario: this.autoConfig.scenario,
|
|
480
|
+
random: this.autoRandom
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
async enqueue(mode, items, summary, duplicate = false) {
|
|
484
|
+
this.stats.generated += items.length;
|
|
485
|
+
this.stats.queued += 1;
|
|
486
|
+
this.stats.queueLength += 1;
|
|
487
|
+
const attempt = {
|
|
488
|
+
id: `${mode}-${Date.now().toString(36)}-${this.stats.queued}`,
|
|
489
|
+
mode,
|
|
490
|
+
eventIds: items.map((item) => typeof item.id === "string" ? item.id : ""),
|
|
491
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
492
|
+
status: "queued",
|
|
493
|
+
summary
|
|
494
|
+
};
|
|
495
|
+
this.history.unshift(attempt);
|
|
496
|
+
this.history = this.history.slice(0, 500);
|
|
497
|
+
this.emit();
|
|
498
|
+
const task = this.queue.then(async () => {
|
|
499
|
+
attempt.status = "sent";
|
|
500
|
+
this.emit();
|
|
501
|
+
try {
|
|
502
|
+
const result = await sendBatch(this.connection, duplicate ? items.map((item) => ({ ...item })) : items);
|
|
503
|
+
const duplicateCount = Math.max(0, items.length - result.accepted);
|
|
504
|
+
this.stats.accepted += result.accepted;
|
|
505
|
+
this.stats.duplicate += duplicateCount;
|
|
506
|
+
attempt.status = result.accepted === 0 && items.length > 0 ? "duplicate" : "accepted";
|
|
507
|
+
attempt.statusCode = result.statusCode;
|
|
508
|
+
if (duplicateCount > 0) attempt.error = `${duplicateCount} duplicate event${duplicateCount === 1 ? "" : "s"}`;
|
|
509
|
+
this.connectionState = {
|
|
510
|
+
connected: true,
|
|
511
|
+
lastStatus: result.statusCode,
|
|
512
|
+
lastError: void 0
|
|
513
|
+
};
|
|
514
|
+
this.lastError = void 0;
|
|
515
|
+
} catch (error) {
|
|
516
|
+
this.stats.failed += items.length;
|
|
517
|
+
attempt.status = "failed";
|
|
518
|
+
attempt.statusCode = error instanceof TransportError ? error.statusCode : void 0;
|
|
519
|
+
attempt.error = error instanceof Error ? error.message : String(error);
|
|
520
|
+
this.lastError = attempt.error;
|
|
521
|
+
this.connectionState = {
|
|
522
|
+
connected: false,
|
|
523
|
+
lastStatus: attempt.statusCode,
|
|
524
|
+
lastError: attempt.error
|
|
525
|
+
};
|
|
526
|
+
if (mode === "auto") {
|
|
527
|
+
this.clearAutoTimers();
|
|
528
|
+
this.autoPending = [];
|
|
529
|
+
this.autoStatus = "failed";
|
|
530
|
+
}
|
|
531
|
+
} finally {
|
|
532
|
+
attempt.finishedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
533
|
+
this.stats.queueLength = Math.max(0, this.stats.queueLength - 1);
|
|
534
|
+
this.emit();
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
this.queue = task.catch(() => void 0);
|
|
538
|
+
await task;
|
|
539
|
+
}
|
|
540
|
+
completeAuto() {
|
|
541
|
+
if (this.autoStatus === "running" || this.autoStatus === "paused") {
|
|
542
|
+
this.clearAutoTimers();
|
|
543
|
+
if (this.autoPending.length > 0) {
|
|
544
|
+
const pending = this.autoPending.splice(0);
|
|
545
|
+
this.enqueueAutoBatch(pending);
|
|
546
|
+
}
|
|
547
|
+
this.autoStatus = "completed";
|
|
548
|
+
this.emit();
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
clearAutoTimers() {
|
|
552
|
+
if (this.autoTimer) {
|
|
553
|
+
clearInterval(this.autoTimer);
|
|
554
|
+
this.autoTimer = void 0;
|
|
555
|
+
}
|
|
556
|
+
if (this.autoDurationTimer) {
|
|
557
|
+
clearTimeout(this.autoDurationTimer);
|
|
558
|
+
this.autoDurationTimer = void 0;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
emit() {
|
|
562
|
+
const snapshot = this.snapshot();
|
|
563
|
+
for (const listener of this.listeners) listener(snapshot);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
// server/state.ts
|
|
567
|
+
const silkwormRuntime = new SilkwormRuntime();
|
|
568
|
+
export { silkwormRuntime };
|