@quillaudits/cli 0.1.2 → 0.1.3

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.
Files changed (2) hide show
  1. package/dist/qs.js +96 -39
  2. package/package.json +1 -1
package/dist/qs.js CHANGED
@@ -154,10 +154,69 @@ class QuillShieldApi {
154
154
  async getCredits() {
155
155
  return this.request("GET", "/v1/credits/balance");
156
156
  }
157
- streamEvents(jobId) {
158
- const url = `${this.cfg.api_url}/v1/audits/${jobId}/events`;
159
- const urlWithAuth = `${url}?api_key=${encodeURIComponent(this.cfg.api_key)}`;
160
- return new EventSource(urlWithAuth);
157
+ streamEvents(jobId, onEvent, onError) {
158
+ const url = `${this.cfg.api_url}/v1/audits/${jobId}/events?api_key=${encodeURIComponent(this.cfg.api_key)}`;
159
+ const controller = new AbortController;
160
+ let closed = false;
161
+ (async () => {
162
+ try {
163
+ const res = await fetch(url, {
164
+ method: "GET",
165
+ headers: { Accept: "text/event-stream", Authorization: `Bearer ${this.cfg.api_key}` },
166
+ signal: controller.signal
167
+ });
168
+ if (!res.ok || !res.body) {
169
+ onError(new Error(`SSE connection failed: ${res.status}`));
170
+ return;
171
+ }
172
+ const reader = res.body.getReader();
173
+ const decoder = new TextDecoder;
174
+ let buffer = "";
175
+ while (!closed) {
176
+ const { done, value } = await reader.read();
177
+ if (done)
178
+ break;
179
+ buffer += decoder.decode(value, { stream: true });
180
+ let idx;
181
+ while ((idx = buffer.indexOf(`
182
+
183
+ `)) !== -1) {
184
+ const raw = buffer.slice(0, idx);
185
+ buffer = buffer.slice(idx + 2);
186
+ const event = parseSse(raw);
187
+ if (event)
188
+ onEvent(event.event, event.data);
189
+ }
190
+ }
191
+ } catch (err) {
192
+ if (!closed)
193
+ onError(err);
194
+ }
195
+ })();
196
+ return {
197
+ close: () => {
198
+ closed = true;
199
+ controller.abort();
200
+ }
201
+ };
202
+ }
203
+ }
204
+ function parseSse(raw) {
205
+ let event = "message";
206
+ let dataStr = "";
207
+ for (const line of raw.split(`
208
+ `)) {
209
+ if (line.startsWith("event:"))
210
+ event = line.slice(6).trim();
211
+ else if (line.startsWith("data:"))
212
+ dataStr += line.slice(5).trim();
213
+ }
214
+ if (!dataStr)
215
+ return null;
216
+ try {
217
+ return { event, data: JSON.parse(dataStr) };
218
+ } catch {
219
+ return { event, data: dataStr };
161
220
  }
162
221
  }
163
222
 
@@ -392,45 +451,43 @@ Job already ${colorStatus(job.status)}.`);
392
451
  return;
393
452
  }
394
453
  info("Connecting to live stream...");
395
- const es = api.streamEvents(jobId);
396
454
  const done = new Promise((resolve2) => {
397
- es.addEventListener("status", (e) => {
398
- const data = JSON.parse(e.data);
399
- console.log(` [status] ${colorStatus(data.status)}`);
400
- if (["completed", "failed", "cancelled"].includes(data.status)) {
401
- es.close();
402
- resolve2();
403
- }
404
- });
405
- es.addEventListener("phase", (e) => {
406
- const data = JSON.parse(e.data);
407
- console.log(` [phase] ${data.phase} (${data.pct}%)`);
408
- });
409
- es.addEventListener("log", (e) => {
410
- const data = JSON.parse(e.data);
411
- const prefix = data.level === "error" ? RED : data.level === "warn" ? YELLOW : DIM;
412
- console.log(` [log] ${prefix}${data.message}${RESET}`);
413
- });
414
- es.addEventListener("finding", (e) => {
415
- const data = JSON.parse(e.data);
416
- const sev = data.severity ?? "info";
417
- const sevColor = sev === "critical" ? RED : sev === "high" ? YELLOW : CYAN;
418
- console.log(` [finding] ${sevColor}[${sev.toUpperCase()}]${RESET} ${data.title}`);
419
- });
420
- es.addEventListener("done", (e) => {
421
- const data = JSON.parse(e.data);
422
- console.log(`
455
+ let stream = api.streamEvents(jobId, (event, data) => {
456
+ switch (event) {
457
+ case "status":
458
+ console.log(` [status] ${colorStatus(data.status)}`);
459
+ if (["completed", "failed", "cancelled"].includes(data.status)) {
460
+ stream.close();
461
+ resolve2();
462
+ }
463
+ break;
464
+ case "phase":
465
+ console.log(` [phase] ${data.phase} (${data.pct}%)`);
466
+ break;
467
+ case "log": {
468
+ const prefix = data.level === "error" ? RED : data.level === "warn" ? YELLOW : DIM;
469
+ console.log(` [log] ${prefix}${data.message}${RESET}`);
470
+ break;
471
+ }
472
+ case "finding": {
473
+ const sev = data.severity ?? "info";
474
+ const sevColor = sev === "critical" ? RED : sev === "high" ? YELLOW : CYAN;
475
+ console.log(` [finding] ${sevColor}[${sev.toUpperCase()}]${RESET} ${data.title}`);
476
+ break;
477
+ }
478
+ case "done":
479
+ console.log(`
423
480
  ${BOLD}Job ${data.status}.${RESET}`);
424
- if (data.error)
425
- console.log(` ${RED}Error: ${data.error}${RESET}`);
426
- es.close();
427
- resolve2();
428
- });
429
- es.onerror = () => {
430
- es.close();
481
+ if (data.error)
482
+ console.log(` ${RED}Error: ${data.error}${RESET}`);
483
+ stream.close();
484
+ resolve2();
485
+ break;
486
+ }
487
+ }, (_err) => {
431
488
  console.log(`${DIM}(stream disconnected, falling back to polling)${RESET}`);
432
489
  pollUntilDone(api, jobId, resolve2);
433
- };
490
+ });
434
491
  });
435
492
  await done;
436
493
  console.log(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillaudits/cli",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "QuillAudits CLI — submit Solidity projects for audit from the command line",
5
5
  "type": "module",
6
6
  "license": "MIT",