securenow 8.0.3 → 8.2.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.
@@ -527,4 +527,51 @@ async function doctor(_args, flags) {
527
527
  process.exit(ok ? 0 : 1);
528
528
  }
529
529
 
530
- module.exports = { testSpan, logSend, doctor, env };
530
+ async function eventSend(args, flags) {
531
+ const type = (args[0] || '').trim();
532
+ if (!type) {
533
+ ui.error('Missing event type.');
534
+ console.log(` ${ui.c.bold('Usage:')} securenow event send <type> [--user <id>] [--session <id>] [--ip <ip>] [--user-agent <ua>] [--level info|warn|error] [--attrs k=v,k=v]`);
535
+ process.exit(1);
536
+ }
537
+
538
+ const cfg = resolvedConfig(flags);
539
+ const eventsEndpoint = `${String(cfg.instance || '').replace(/\/$/, '')}/v1/events`;
540
+
541
+ const attributes = {};
542
+ if (flags.attrs) {
543
+ for (const pair of String(flags.attrs).split(',')) {
544
+ const [k, ...rest] = pair.split('=');
545
+ if (k && rest.length) attributes[k.trim()] = rest.join('=').trim();
546
+ }
547
+ }
548
+
549
+ const ev = { type, ts: Date.now() };
550
+ if (flags.user) ev.user_id = String(flags.user);
551
+ if (flags.session) ev.session_id = String(flags.session);
552
+ if (flags.ip) ev.ip = String(flags.ip);
553
+ if (flags['user-agent']) ev.user_agent = String(flags['user-agent']);
554
+ if (flags.level) ev.severity = String(flags.level);
555
+ if (Object.keys(attributes).length) ev.attributes = attributes;
556
+
557
+ const headers = { 'Content-Type': 'application/json', ...cfg.headers };
558
+ const spin = ui.spinner(`Sending event to ${eventsEndpoint}`);
559
+ try {
560
+ const res = await httpRequest({ endpoint: eventsEndpoint, headers, body: JSON.stringify({ events: [ev] }) });
561
+ if (res.status >= 200 && res.status < 300) {
562
+ spin.stop(`Event accepted (HTTP ${res.status})`);
563
+ if (flags.json) ui.json({ ok: true, status: res.status, endpoint: eventsEndpoint, type });
564
+ return;
565
+ }
566
+ spin.fail(`Events ingest returned HTTP ${res.status}`);
567
+ if (res.body) console.log(ui.c.dim(res.body.slice(0, 500)));
568
+ if (flags.json) ui.json({ ok: false, status: res.status, body: res.body });
569
+ process.exit(1);
570
+ } catch (err) {
571
+ spin.fail(`Failed: ${err.message}`);
572
+ if (flags.json) ui.json({ ok: false, error: err.message });
573
+ process.exit(1);
574
+ }
575
+ }
576
+
577
+ module.exports = { testSpan, logSend, eventSend, doctor, env };