prowl-tools 0.1.8 → 0.1.9

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/dist/lib.cjs CHANGED
@@ -524,10 +524,15 @@ ${macdriverBuildInstructions()}`
524
524
  ${macdriverBuildInstructions()}`);
525
525
  }
526
526
  var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
527
+ function errorMessage(error) {
528
+ return error instanceof Error ? error.message : String(error);
529
+ }
527
530
  var SpawnMacHelperClient = class {
528
531
  child;
529
532
  pending = /* @__PURE__ */ new Map();
530
533
  requestTimeoutMs;
534
+ onEvent;
535
+ onEventError;
531
536
  stdoutBuffer = "";
532
537
  stderrBuffer = "";
533
538
  nextId = 1;
@@ -535,6 +540,9 @@ var SpawnMacHelperClient = class {
535
540
  terminalError;
536
541
  constructor(binaryPath, options = {}) {
537
542
  this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
543
+ this.onEvent = options.onEvent;
544
+ this.onEventError = options.onEventError ?? ((message) => process.stderr.write(`${message}
545
+ `));
538
546
  this.child = (0, import_node_child_process.spawn)(binaryPath, ["serve"], { stdio: ["pipe", "pipe", "pipe"] });
539
547
  this.child.stdout?.setEncoding("utf-8");
540
548
  this.child.stderr?.setEncoding("utf-8");
@@ -571,6 +579,10 @@ var SpawnMacHelperClient = class {
571
579
  } catch {
572
580
  return;
573
581
  }
582
+ if (typeof message.event === "string" && typeof message.id !== "number") {
583
+ this.handleEvent(message);
584
+ return;
585
+ }
574
586
  const id = typeof message.id === "number" ? message.id : void 0;
575
587
  if (id === void 0) {
576
588
  return;
@@ -587,6 +599,18 @@ var SpawnMacHelperClient = class {
587
599
  pending.reject(new Error(typeof message.error === "string" ? message.error : "prowl-macdriver error"));
588
600
  }
589
601
  }
602
+ handleEvent(event) {
603
+ try {
604
+ this.onEvent?.(event);
605
+ } catch (error) {
606
+ try {
607
+ this.onEventError(
608
+ `prowl-macdriver event sink failed for event "${event.event}": ${errorMessage(error)}`
609
+ );
610
+ } catch {
611
+ }
612
+ }
613
+ }
590
614
  failAll(error) {
591
615
  for (const pending of this.pending.values()) {
592
616
  clearTimeout(pending.timer);