@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,396 @@
|
|
|
1
|
+
import type { DebugLogRecord } from '@flash-debug/types'
|
|
2
|
+
import type {
|
|
3
|
+
AutoConfig,
|
|
4
|
+
AutoSnapshot,
|
|
5
|
+
AutoStatus,
|
|
6
|
+
ConnectionConfig,
|
|
7
|
+
GeneratedLog,
|
|
8
|
+
ManualEventInput,
|
|
9
|
+
PublicConnection,
|
|
10
|
+
RunnerSnapshot,
|
|
11
|
+
RunnerStats,
|
|
12
|
+
SendAttempt,
|
|
13
|
+
SendMode,
|
|
14
|
+
} from './types'
|
|
15
|
+
import { DEFAULT_DEBUG_PORT } from '@flash-debug/config/constants'
|
|
16
|
+
import { createScenarioRecord } from '../scenarios'
|
|
17
|
+
import { SeededRandom } from './random'
|
|
18
|
+
import { sendBatch, testTarget, TransportError } from './transport'
|
|
19
|
+
|
|
20
|
+
type SnapshotListener = (snapshot: RunnerSnapshot) => void
|
|
21
|
+
|
|
22
|
+
const defaultAutoConfig: AutoConfig = {
|
|
23
|
+
scenario: 'normal',
|
|
24
|
+
rate: 2,
|
|
25
|
+
batchSize: 1,
|
|
26
|
+
durationSeconds: 0,
|
|
27
|
+
seed: 'silkworm',
|
|
28
|
+
clientId: 'silkworm-01',
|
|
29
|
+
deviceId: 'silkworm-device',
|
|
30
|
+
flushErrors: true,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const defaultConnection: ConnectionConfig = {
|
|
34
|
+
baseUrl: `http://127.0.0.1:${DEFAULT_DEBUG_PORT}`,
|
|
35
|
+
token: '',
|
|
36
|
+
clientId: 'silkworm-01',
|
|
37
|
+
deviceId: 'silkworm-device',
|
|
38
|
+
source: 'flutter',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function cloneStats(): RunnerStats {
|
|
42
|
+
return {
|
|
43
|
+
generated: 0,
|
|
44
|
+
queued: 0,
|
|
45
|
+
accepted: 0,
|
|
46
|
+
failed: 0,
|
|
47
|
+
duplicate: 0,
|
|
48
|
+
retrying: 0,
|
|
49
|
+
queueLength: 0,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function publicConnection(connection: ConnectionConfig, state: Pick<PublicConnection, 'connected' | 'lastStatus' | 'lastLatencyMs' | 'lastError'>): PublicConnection {
|
|
54
|
+
return {
|
|
55
|
+
baseUrl: connection.baseUrl,
|
|
56
|
+
clientId: connection.clientId,
|
|
57
|
+
deviceId: connection.deviceId,
|
|
58
|
+
source: connection.source,
|
|
59
|
+
hasToken: Boolean(connection.token),
|
|
60
|
+
...state,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class SilkwormRuntime {
|
|
65
|
+
private connection: ConnectionConfig = { ...defaultConnection }
|
|
66
|
+
private connectionState: Pick<PublicConnection, 'connected' | 'lastStatus' | 'lastLatencyMs' | 'lastError'> = { connected: false }
|
|
67
|
+
private autoStatus: AutoStatus = 'idle'
|
|
68
|
+
private autoConfig: AutoConfig = { ...defaultAutoConfig }
|
|
69
|
+
private autoRunId = ''
|
|
70
|
+
private autoSequence = 0
|
|
71
|
+
private autoRandom = new SeededRandom(defaultAutoConfig.seed)
|
|
72
|
+
private autoRateCarry = 0
|
|
73
|
+
private autoPending: GeneratedLog[] = []
|
|
74
|
+
private autoTimer: ReturnType<typeof setInterval> | undefined
|
|
75
|
+
private autoDurationTimer: ReturnType<typeof setTimeout> | undefined
|
|
76
|
+
private queue = Promise.resolve()
|
|
77
|
+
private history: SendAttempt[] = []
|
|
78
|
+
private stats = cloneStats()
|
|
79
|
+
private lastError: string | undefined
|
|
80
|
+
private readonly listeners = new Set<SnapshotListener>()
|
|
81
|
+
|
|
82
|
+
snapshot(): RunnerSnapshot {
|
|
83
|
+
const auto: AutoSnapshot = {
|
|
84
|
+
status: this.autoStatus,
|
|
85
|
+
sequence: this.autoSequence,
|
|
86
|
+
config: { ...this.autoConfig },
|
|
87
|
+
...(this.autoRunId
|
|
88
|
+
? { runId: this.autoRunId }
|
|
89
|
+
: {}),
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
connection: publicConnection(this.connection, this.connectionState),
|
|
93
|
+
auto,
|
|
94
|
+
stats: { ...this.stats },
|
|
95
|
+
history: this.history.slice(),
|
|
96
|
+
...(this.lastError
|
|
97
|
+
? { lastError: this.lastError }
|
|
98
|
+
: {}),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
subscribe(listener: SnapshotListener): () => void {
|
|
103
|
+
this.listeners.add(listener)
|
|
104
|
+
listener(this.snapshot())
|
|
105
|
+
return () => this.listeners.delete(listener)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
configure(connection: Partial<ConnectionConfig>): RunnerSnapshot {
|
|
109
|
+
this.connection = {
|
|
110
|
+
...this.connection,
|
|
111
|
+
...connection,
|
|
112
|
+
baseUrl: connection.baseUrl?.trim() || this.connection.baseUrl,
|
|
113
|
+
clientId: connection.clientId?.trim() || this.connection.clientId,
|
|
114
|
+
deviceId: connection.deviceId?.trim() || this.connection.deviceId,
|
|
115
|
+
}
|
|
116
|
+
this.connectionState = { connected: false }
|
|
117
|
+
this.emit()
|
|
118
|
+
return this.snapshot()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async testConnection(): Promise<RunnerSnapshot> {
|
|
122
|
+
try {
|
|
123
|
+
const result = await testTarget(this.connection)
|
|
124
|
+
this.connectionState = {
|
|
125
|
+
connected: true,
|
|
126
|
+
lastStatus: result.statusCode,
|
|
127
|
+
lastLatencyMs: result.latencyMs,
|
|
128
|
+
}
|
|
129
|
+
this.lastError = undefined
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
this.connectionState = {
|
|
133
|
+
connected: false,
|
|
134
|
+
lastStatus: error instanceof TransportError
|
|
135
|
+
? error.statusCode
|
|
136
|
+
: undefined,
|
|
137
|
+
lastError: error instanceof Error
|
|
138
|
+
? error.message
|
|
139
|
+
: String(error),
|
|
140
|
+
}
|
|
141
|
+
this.lastError = this.connectionState.lastError
|
|
142
|
+
throw error
|
|
143
|
+
}
|
|
144
|
+
this.emit()
|
|
145
|
+
return this.snapshot()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
clearHistory(): RunnerSnapshot {
|
|
149
|
+
this.history = []
|
|
150
|
+
this.stats = cloneStats()
|
|
151
|
+
this.lastError = undefined
|
|
152
|
+
this.emit()
|
|
153
|
+
return this.snapshot()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async startAuto(config: Partial<AutoConfig> = {}): Promise<RunnerSnapshot> {
|
|
157
|
+
if (this.autoStatus === 'running' || this.autoStatus === 'starting') {
|
|
158
|
+
return this.snapshot()
|
|
159
|
+
}
|
|
160
|
+
if (!this.connectionState.connected) {
|
|
161
|
+
throw new Error('Connect to a target before starting Auto Mode')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.autoConfig = {
|
|
165
|
+
...this.autoConfig,
|
|
166
|
+
...config,
|
|
167
|
+
rate: Math.min(100, Math.max(0.1, Number(config.rate ?? this.autoConfig.rate))),
|
|
168
|
+
batchSize: Math.min(100, Math.max(1, Math.floor(Number(config.batchSize ?? this.autoConfig.batchSize)))),
|
|
169
|
+
durationSeconds: Math.max(0, Number(config.durationSeconds ?? this.autoConfig.durationSeconds)),
|
|
170
|
+
clientId: config.clientId?.trim() || this.autoConfig.clientId,
|
|
171
|
+
deviceId: config.deviceId?.trim() || this.autoConfig.deviceId,
|
|
172
|
+
seed: config.seed?.trim() || this.autoConfig.seed,
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!this.autoRunId || this.autoStatus === 'completed' || this.autoStatus === 'stopped' || this.autoStatus === 'failed') {
|
|
176
|
+
this.autoRunId = `${this.autoConfig.clientId}-${Date.now().toString(36)}`
|
|
177
|
+
this.autoSequence = 0
|
|
178
|
+
this.autoRandom = new SeededRandom(this.autoConfig.seed)
|
|
179
|
+
this.autoRateCarry = 0
|
|
180
|
+
this.autoPending = []
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.autoStatus = 'starting'
|
|
184
|
+
this.emit()
|
|
185
|
+
this.autoStatus = 'running'
|
|
186
|
+
this.startAutoTimer()
|
|
187
|
+
if (this.autoConfig.durationSeconds > 0) {
|
|
188
|
+
this.autoDurationTimer = setTimeout(() => this.completeAuto(), this.autoConfig.durationSeconds * 1000)
|
|
189
|
+
}
|
|
190
|
+
this.emit()
|
|
191
|
+
return this.snapshot()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
pauseAuto(): RunnerSnapshot {
|
|
195
|
+
if (this.autoStatus === 'running') {
|
|
196
|
+
this.clearAutoTimers()
|
|
197
|
+
this.autoStatus = 'paused'
|
|
198
|
+
this.emit()
|
|
199
|
+
}
|
|
200
|
+
return this.snapshot()
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
stopAuto(): RunnerSnapshot {
|
|
204
|
+
if (this.autoStatus !== 'idle') {
|
|
205
|
+
this.clearAutoTimers()
|
|
206
|
+
this.autoStatus = 'stopped'
|
|
207
|
+
this.emit()
|
|
208
|
+
}
|
|
209
|
+
return this.snapshot()
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async sendManual(items: GeneratedLog[], options: { summary?: string, duplicate?: boolean } = {}): Promise<RunnerSnapshot> {
|
|
213
|
+
await this.enqueue('manual', items, options.summary ?? 'Manual event', options.duplicate)
|
|
214
|
+
return this.snapshot()
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private startAutoTimer(): void {
|
|
218
|
+
this.clearAutoTimers()
|
|
219
|
+
const intervalMs = 100
|
|
220
|
+
this.autoTimer = setInterval(() => {
|
|
221
|
+
this.autoRateCarry += this.autoConfig.rate / (1000 / intervalMs)
|
|
222
|
+
const eventsPerTick = Math.floor(this.autoRateCarry)
|
|
223
|
+
this.autoRateCarry -= eventsPerTick
|
|
224
|
+
if (eventsPerTick > 0) {
|
|
225
|
+
this.autoPending.push(...Array.from({ length: eventsPerTick }, () => this.nextAutoRecord()))
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
while (this.autoPending.length >= this.autoConfig.batchSize) {
|
|
229
|
+
const items = this.autoPending.splice(0, this.autoConfig.batchSize)
|
|
230
|
+
this.enqueueAutoBatch(items)
|
|
231
|
+
}
|
|
232
|
+
}, intervalMs)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private enqueueAutoBatch(items: GeneratedLog[]): void {
|
|
236
|
+
if (this.autoConfig.flushErrors) {
|
|
237
|
+
const errors = items.filter(item => typeof item === 'object' && item !== null && item.level === 'error')
|
|
238
|
+
const regular = items.filter(item => !(typeof item === 'object' && item !== null && item.level === 'error'))
|
|
239
|
+
if (errors.length > 0) {
|
|
240
|
+
void this.enqueue('auto', errors, `${this.autoConfig.scenario} error`)
|
|
241
|
+
}
|
|
242
|
+
if (regular.length > 0) {
|
|
243
|
+
void this.enqueue('auto', regular, `${this.autoConfig.scenario} scenario`)
|
|
244
|
+
}
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
void this.enqueue('auto', items, `${this.autoConfig.scenario} scenario`)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private nextAutoRecord(): GeneratedLog {
|
|
251
|
+
this.autoSequence += 1
|
|
252
|
+
const id = `${this.autoRunId}-${this.autoSequence}`
|
|
253
|
+
const context = {
|
|
254
|
+
id,
|
|
255
|
+
timestamp: new Date().toISOString(),
|
|
256
|
+
sequence: this.autoSequence,
|
|
257
|
+
clientId: this.autoConfig.clientId,
|
|
258
|
+
deviceId: this.autoConfig.deviceId,
|
|
259
|
+
scenario: this.autoConfig.scenario,
|
|
260
|
+
random: this.autoRandom,
|
|
261
|
+
}
|
|
262
|
+
return createScenarioRecord(context)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private async enqueue(mode: SendMode, items: GeneratedLog[], summary: string, duplicate = false): Promise<void> {
|
|
266
|
+
this.stats.generated += items.length
|
|
267
|
+
this.stats.queued += 1
|
|
268
|
+
this.stats.queueLength += 1
|
|
269
|
+
const attempt: SendAttempt = {
|
|
270
|
+
id: `${mode}-${Date.now().toString(36)}-${this.stats.queued}`,
|
|
271
|
+
mode,
|
|
272
|
+
eventIds: items.map(item => typeof item.id === 'string'
|
|
273
|
+
? item.id
|
|
274
|
+
: ''),
|
|
275
|
+
startedAt: new Date().toISOString(),
|
|
276
|
+
status: 'queued',
|
|
277
|
+
summary,
|
|
278
|
+
}
|
|
279
|
+
this.history.unshift(attempt)
|
|
280
|
+
this.history = this.history.slice(0, 500)
|
|
281
|
+
this.emit()
|
|
282
|
+
|
|
283
|
+
const task = this.queue.then(async () => {
|
|
284
|
+
attempt.status = 'sent'
|
|
285
|
+
this.emit()
|
|
286
|
+
try {
|
|
287
|
+
const result = await sendBatch(this.connection, duplicate
|
|
288
|
+
? items.map(item => ({ ...item }))
|
|
289
|
+
: items)
|
|
290
|
+
const duplicateCount = Math.max(0, items.length - result.accepted)
|
|
291
|
+
this.stats.accepted += result.accepted
|
|
292
|
+
this.stats.duplicate += duplicateCount
|
|
293
|
+
attempt.status = result.accepted === 0 && items.length > 0
|
|
294
|
+
? 'duplicate'
|
|
295
|
+
: 'accepted'
|
|
296
|
+
attempt.statusCode = result.statusCode
|
|
297
|
+
if (duplicateCount > 0) {
|
|
298
|
+
attempt.error = `${duplicateCount} duplicate event${duplicateCount === 1
|
|
299
|
+
? ''
|
|
300
|
+
: 's'}`
|
|
301
|
+
}
|
|
302
|
+
this.connectionState = {
|
|
303
|
+
connected: true,
|
|
304
|
+
lastStatus: result.statusCode,
|
|
305
|
+
lastError: undefined,
|
|
306
|
+
}
|
|
307
|
+
this.lastError = undefined
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
this.stats.failed += items.length
|
|
311
|
+
attempt.status = 'failed'
|
|
312
|
+
attempt.statusCode = error instanceof TransportError
|
|
313
|
+
? error.statusCode
|
|
314
|
+
: undefined
|
|
315
|
+
attempt.error = error instanceof Error
|
|
316
|
+
? error.message
|
|
317
|
+
: String(error)
|
|
318
|
+
this.lastError = attempt.error
|
|
319
|
+
this.connectionState = {
|
|
320
|
+
connected: false,
|
|
321
|
+
lastStatus: attempt.statusCode,
|
|
322
|
+
lastError: attempt.error,
|
|
323
|
+
}
|
|
324
|
+
if (mode === 'auto') {
|
|
325
|
+
this.clearAutoTimers()
|
|
326
|
+
this.autoPending = []
|
|
327
|
+
this.autoStatus = 'failed'
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
finally {
|
|
331
|
+
attempt.finishedAt = new Date().toISOString()
|
|
332
|
+
this.stats.queueLength = Math.max(0, this.stats.queueLength - 1)
|
|
333
|
+
this.emit()
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
this.queue = task.catch(() => undefined)
|
|
337
|
+
await task
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private completeAuto(): void {
|
|
341
|
+
if (this.autoStatus === 'running' || this.autoStatus === 'paused') {
|
|
342
|
+
this.clearAutoTimers()
|
|
343
|
+
if (this.autoPending.length > 0) {
|
|
344
|
+
const pending = this.autoPending.splice(0)
|
|
345
|
+
this.enqueueAutoBatch(pending)
|
|
346
|
+
}
|
|
347
|
+
this.autoStatus = 'completed'
|
|
348
|
+
this.emit()
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private clearAutoTimers(): void {
|
|
353
|
+
if (this.autoTimer) {
|
|
354
|
+
clearInterval(this.autoTimer)
|
|
355
|
+
this.autoTimer = undefined
|
|
356
|
+
}
|
|
357
|
+
if (this.autoDurationTimer) {
|
|
358
|
+
clearTimeout(this.autoDurationTimer)
|
|
359
|
+
this.autoDurationTimer = undefined
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private emit(): void {
|
|
364
|
+
const snapshot = this.snapshot()
|
|
365
|
+
for (const listener of this.listeners) {
|
|
366
|
+
listener(snapshot)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function manualInputToRecord(input: ManualEventInput): DebugLogRecord {
|
|
372
|
+
let metadata: Record<string, unknown> | undefined
|
|
373
|
+
if (input.metadata.trim()) {
|
|
374
|
+
const parsed: unknown = JSON.parse(input.metadata)
|
|
375
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
376
|
+
throw new Error('Metadata must be a JSON object')
|
|
377
|
+
}
|
|
378
|
+
metadata = parsed as Record<string, unknown>
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
id: input.id.trim() || `manual-${Date.now().toString(36)}`,
|
|
383
|
+
timestamp: input.timestamp || new Date().toISOString(),
|
|
384
|
+
level: input.level,
|
|
385
|
+
type: input.type,
|
|
386
|
+
source: input.source,
|
|
387
|
+
tag: input.tag.trim() || undefined,
|
|
388
|
+
deviceId: input.deviceId.trim() || undefined,
|
|
389
|
+
message: input.message,
|
|
390
|
+
content: input.content,
|
|
391
|
+
contentFormat: input.contentFormat,
|
|
392
|
+
error: input.error.trim() || undefined,
|
|
393
|
+
stackTrace: input.stackTrace.trim() || undefined,
|
|
394
|
+
metadata,
|
|
395
|
+
}
|
|
396
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { DebugLogRecord } from '@flash-debug/types'
|
|
2
|
+
import type { ConnectionConfig, GeneratedLog } from './types'
|
|
3
|
+
|
|
4
|
+
export interface BatchResponse {
|
|
5
|
+
accepted: number
|
|
6
|
+
cursor?: number
|
|
7
|
+
statusCode: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class TransportError extends Error {
|
|
11
|
+
readonly statusCode?: number
|
|
12
|
+
|
|
13
|
+
constructor(message: string, statusCode?: number) {
|
|
14
|
+
super(message)
|
|
15
|
+
this.name = 'TransportError'
|
|
16
|
+
this.statusCode = statusCode
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function targetUrl(connection: ConnectionConfig, path: string): string {
|
|
21
|
+
const baseUrl = connection.baseUrl.trim()
|
|
22
|
+
if (!baseUrl) {
|
|
23
|
+
throw new TransportError('Target Base URL is required')
|
|
24
|
+
}
|
|
25
|
+
return new URL(path, `${baseUrl.replace(/\/$/, '')}/`).toString()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function readPayload(response: Response): Promise<Record<string, unknown>> {
|
|
29
|
+
const text = await response.text()
|
|
30
|
+
if (!text) {
|
|
31
|
+
return {}
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(text) as Record<string, unknown>
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return { message: text }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function testTarget(connection: ConnectionConfig): Promise<{ statusCode: number, latencyMs: number }> {
|
|
42
|
+
const startedAt = Date.now()
|
|
43
|
+
let response: Response
|
|
44
|
+
try {
|
|
45
|
+
response = await fetch(targetUrl(connection, '/healthz'), {
|
|
46
|
+
signal: AbortSignal.timeout(3_000),
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw new TransportError(error instanceof Error
|
|
51
|
+
? error.message
|
|
52
|
+
: String(error))
|
|
53
|
+
}
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
throw new TransportError(`Target health check failed with HTTP ${response.status}`, response.status)
|
|
56
|
+
}
|
|
57
|
+
const payload = await readPayload(response)
|
|
58
|
+
if (payload.ok !== true || payload.service !== 'flash-debug') {
|
|
59
|
+
throw new TransportError('Target health check returned an invalid response', response.status)
|
|
60
|
+
}
|
|
61
|
+
return { statusCode: response.status, latencyMs: Date.now() - startedAt }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function sendBatch(connection: ConnectionConfig, items: GeneratedLog[]): Promise<BatchResponse> {
|
|
65
|
+
let response: Response
|
|
66
|
+
try {
|
|
67
|
+
response = await fetch(targetUrl(connection, '/api/v1/logs/batch'), {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
headers: {
|
|
70
|
+
'Content-Type': 'application/json',
|
|
71
|
+
...(connection.token.trim()
|
|
72
|
+
? { Authorization: `Bearer ${connection.token.trim()}` }
|
|
73
|
+
: {}),
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
clientId: connection.clientId,
|
|
77
|
+
items: items as DebugLogRecord[],
|
|
78
|
+
}),
|
|
79
|
+
signal: AbortSignal.timeout(10_000),
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
throw new TransportError(error instanceof Error
|
|
84
|
+
? error.message
|
|
85
|
+
: String(error))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const payload = await readPayload(response)
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const message = typeof payload.message === 'string'
|
|
91
|
+
? payload.message
|
|
92
|
+
: `Target rejected request with HTTP ${response.status}`
|
|
93
|
+
throw new TransportError(message, response.status)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (typeof payload.accepted !== 'number') {
|
|
97
|
+
throw new TransportError('Target returned an invalid logs response', response.status)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
accepted: typeof payload.accepted === 'number'
|
|
102
|
+
? payload.accepted
|
|
103
|
+
: 0,
|
|
104
|
+
cursor: typeof payload.cursor === 'number'
|
|
105
|
+
? payload.cursor
|
|
106
|
+
: undefined,
|
|
107
|
+
statusCode: response.status,
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { DebugLogRecord, DebugLogSource } from '@flash-debug/types'
|
|
2
|
+
|
|
3
|
+
export type SilkwormScenario
|
|
4
|
+
= | 'normal'
|
|
5
|
+
| 'mixed-sources'
|
|
6
|
+
| 'content-formats'
|
|
7
|
+
| 'network'
|
|
8
|
+
| 'exceptions'
|
|
9
|
+
| 'burst'
|
|
10
|
+
| 'duplicates'
|
|
11
|
+
| 'invalid'
|
|
12
|
+
| 'multi-device'
|
|
13
|
+
| 'long-run'
|
|
14
|
+
|
|
15
|
+
export const SILKWORM_SCENARIOS: readonly SilkwormScenario[] = [
|
|
16
|
+
'normal',
|
|
17
|
+
'mixed-sources',
|
|
18
|
+
'content-formats',
|
|
19
|
+
'network',
|
|
20
|
+
'exceptions',
|
|
21
|
+
'burst',
|
|
22
|
+
'duplicates',
|
|
23
|
+
'invalid',
|
|
24
|
+
'multi-device',
|
|
25
|
+
'long-run',
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
export interface ConnectionConfig {
|
|
29
|
+
baseUrl: string
|
|
30
|
+
token: string
|
|
31
|
+
clientId: string
|
|
32
|
+
deviceId: string
|
|
33
|
+
source: DebugLogSource
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PublicConnection {
|
|
37
|
+
baseUrl: string
|
|
38
|
+
clientId: string
|
|
39
|
+
deviceId: string
|
|
40
|
+
source: DebugLogSource
|
|
41
|
+
connected: boolean
|
|
42
|
+
hasToken: boolean
|
|
43
|
+
lastStatus?: number
|
|
44
|
+
lastLatencyMs?: number
|
|
45
|
+
lastError?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface AutoConfig {
|
|
49
|
+
scenario: SilkwormScenario
|
|
50
|
+
rate: number
|
|
51
|
+
batchSize: number
|
|
52
|
+
durationSeconds: number
|
|
53
|
+
seed: string
|
|
54
|
+
clientId: string
|
|
55
|
+
deviceId: string
|
|
56
|
+
flushErrors: boolean
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type AutoStatus = 'idle' | 'starting' | 'running' | 'paused' | 'stopped' | 'completed' | 'failed'
|
|
60
|
+
|
|
61
|
+
export interface AutoSnapshot {
|
|
62
|
+
status: AutoStatus
|
|
63
|
+
runId?: string
|
|
64
|
+
sequence: number
|
|
65
|
+
config: AutoConfig
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type SendMode = 'auto' | 'manual'
|
|
69
|
+
export type SendStatus = 'queued' | 'sent' | 'accepted' | 'failed' | 'duplicate'
|
|
70
|
+
|
|
71
|
+
export interface SendAttempt {
|
|
72
|
+
id: string
|
|
73
|
+
mode: SendMode
|
|
74
|
+
eventIds: string[]
|
|
75
|
+
startedAt: string
|
|
76
|
+
finishedAt?: string
|
|
77
|
+
status: SendStatus
|
|
78
|
+
statusCode?: number
|
|
79
|
+
error?: string
|
|
80
|
+
summary?: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface RunnerStats {
|
|
84
|
+
generated: number
|
|
85
|
+
queued: number
|
|
86
|
+
accepted: number
|
|
87
|
+
failed: number
|
|
88
|
+
duplicate: number
|
|
89
|
+
retrying: number
|
|
90
|
+
queueLength: number
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface RunnerSnapshot {
|
|
94
|
+
connection: PublicConnection
|
|
95
|
+
auto: AutoSnapshot
|
|
96
|
+
stats: RunnerStats
|
|
97
|
+
history: SendAttempt[]
|
|
98
|
+
lastError?: string
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type GeneratedLog = DebugLogRecord | Record<string, unknown>
|
|
102
|
+
|
|
103
|
+
export interface ManualEventInput {
|
|
104
|
+
id: string
|
|
105
|
+
timestamp: string
|
|
106
|
+
level: DebugLogRecord['level']
|
|
107
|
+
type: DebugLogRecord['type']
|
|
108
|
+
source: DebugLogSource
|
|
109
|
+
tag: string
|
|
110
|
+
clientId: string
|
|
111
|
+
deviceId: string
|
|
112
|
+
contentFormat: NonNullable<DebugLogRecord['contentFormat']>
|
|
113
|
+
message: string
|
|
114
|
+
content: string
|
|
115
|
+
error: string
|
|
116
|
+
stackTrace: string
|
|
117
|
+
metadata: string
|
|
118
|
+
}
|