@vastblast/capnweb 0.7.0 → 0.7.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.
@@ -2073,8 +2073,7 @@ var RpcSessionImpl = class {
2073
2073
  reverseExports = /* @__PURE__ */ new Map();
2074
2074
  imports = [];
2075
2075
  abortReason;
2076
- cancelReadLoop = () => {
2077
- };
2076
+ cancelReadLoop;
2078
2077
  // We assign positive numbers to imports we initiate, and negative numbers to exports we
2079
2078
  // initiate. So the next import ID is just `imports.length`, but the next export ID needs
2080
2079
  // to be tracked explicitly.
@@ -2352,7 +2351,8 @@ var RpcSessionImpl = class {
2352
2351
  }
2353
2352
  abort(error, trySendAbortMessage = true) {
2354
2353
  if (this.abortReason !== void 0) return;
2355
- this.cancelReadLoop(error);
2354
+ this.cancelReadLoop?.(error);
2355
+ this.cancelReadLoop = void 0;
2356
2356
  if (trySendAbortMessage) {
2357
2357
  try {
2358
2358
  this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
@@ -2390,19 +2390,7 @@ var RpcSessionImpl = class {
2390
2390
  }
2391
2391
  async readLoop() {
2392
2392
  while (!this.abortReason) {
2393
- let rejectFunc;
2394
- let abortPromise = new Promise((resolve, reject) => {
2395
- rejectFunc = reject;
2396
- });
2397
- this.cancelReadLoop = rejectFunc;
2398
- let msgText;
2399
- try {
2400
- msgText = await Promise.race([this.transport.receive(), abortPromise]);
2401
- } finally {
2402
- this.cancelReadLoop = () => {
2403
- };
2404
- }
2405
- let msg = JSON.parse(msgText);
2393
+ let msg = JSON.parse(await this.receiveOrAbort());
2406
2394
  if (this.abortReason) break;
2407
2395
  if (msg instanceof Array) {
2408
2396
  switch (msg[0]) {
@@ -2484,6 +2472,22 @@ var RpcSessionImpl = class {
2484
2472
  throw new Error(`bad RPC message: ${JSON.stringify(msg)}`);
2485
2473
  }
2486
2474
  }
2475
+ receiveOrAbort() {
2476
+ let readCanceled = Promise.withResolvers();
2477
+ this.cancelReadLoop = readCanceled.reject;
2478
+ let receivePromise;
2479
+ try {
2480
+ receivePromise = this.transport.receive();
2481
+ } catch (err) {
2482
+ this.cancelReadLoop = void 0;
2483
+ return Promise.reject(err);
2484
+ }
2485
+ return Promise.race([receivePromise, readCanceled.promise]).finally(() => {
2486
+ if (this.cancelReadLoop === readCanceled.reject) {
2487
+ this.cancelReadLoop = void 0;
2488
+ }
2489
+ });
2490
+ }
2487
2491
  async drain() {
2488
2492
  if (this.abortReason) {
2489
2493
  throw this.abortReason;
@@ -2530,6 +2534,7 @@ var RpcSession = class {
2530
2534
  };
2531
2535
 
2532
2536
  // src/websocket.ts
2537
+ var RESOLVED_VOID_PROMISE = Promise.resolve();
2533
2538
  function newWebSocketRpcSession(webSocket, localMain, options) {
2534
2539
  if (typeof webSocket === "string") {
2535
2540
  webSocket = new WebSocket(webSocket);
@@ -2594,18 +2599,23 @@ var WebSocketTransport = class {
2594
2599
  #receiveRejecter;
2595
2600
  #receiveQueue = [];
2596
2601
  #error;
2597
- async send(message) {
2598
- if (this.#sendQueue === void 0) {
2599
- this.#webSocket.send(message);
2600
- } else {
2601
- this.#sendQueue.push(message);
2602
+ send(message) {
2603
+ try {
2604
+ if (this.#sendQueue === void 0) {
2605
+ this.#webSocket.send(message);
2606
+ } else {
2607
+ this.#sendQueue.push(message);
2608
+ }
2609
+ return RESOLVED_VOID_PROMISE;
2610
+ } catch (err) {
2611
+ return Promise.reject(err);
2602
2612
  }
2603
2613
  }
2604
- async receive() {
2614
+ receive() {
2605
2615
  if (this.#receiveQueue.length > 0) {
2606
- return this.#receiveQueue.shift();
2616
+ return Promise.resolve(this.#receiveQueue.shift());
2607
2617
  } else if (this.#error) {
2608
- throw this.#error;
2618
+ return Promise.reject(this.#error);
2609
2619
  } else {
2610
2620
  return new Promise((resolve, reject) => {
2611
2621
  this.#receiveResolver = resolve;
@@ -2638,6 +2648,7 @@ var WebSocketTransport = class {
2638
2648
  };
2639
2649
 
2640
2650
  // src/batch.ts
2651
+ var RESOLVED_VOID_PROMISE2 = Promise.resolve();
2641
2652
  var BatchClientTransport = class {
2642
2653
  constructor(sendBatch) {
2643
2654
  this.#promise = this.#scheduleBatch(sendBatch);
@@ -2646,10 +2657,11 @@ var BatchClientTransport = class {
2646
2657
  #aborted;
2647
2658
  #batchToSend = [];
2648
2659
  #batchToReceive = null;
2649
- async send(message) {
2660
+ send(message) {
2650
2661
  if (this.#batchToSend !== null) {
2651
2662
  this.#batchToSend.push(message);
2652
2663
  }
2664
+ return RESOLVED_VOID_PROMISE2;
2653
2665
  }
2654
2666
  async receive() {
2655
2667
  if (!this.#batchToReceive) {
@@ -2699,8 +2711,9 @@ var BatchServerTransport = class {
2699
2711
  #batchToSend = [];
2700
2712
  #batchToReceive;
2701
2713
  #allReceived = Promise.withResolvers();
2702
- async send(message) {
2714
+ send(message) {
2703
2715
  this.#batchToSend.push(message);
2716
+ return RESOLVED_VOID_PROMISE2;
2704
2717
  }
2705
2718
  async receive() {
2706
2719
  let msg = this.#batchToReceive.shift();
@@ -2758,6 +2771,7 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2758
2771
  }
2759
2772
 
2760
2773
  // src/messageport.ts
2774
+ var RESOLVED_VOID_PROMISE3 = Promise.resolve();
2761
2775
  function newMessagePortRpcSession(port, localMain, options) {
2762
2776
  let transport = new MessagePortTransport(port);
2763
2777
  let rpc = new RpcSession(transport, localMain, options);
@@ -2791,17 +2805,22 @@ var MessagePortTransport = class {
2791
2805
  #receiveRejecter;
2792
2806
  #receiveQueue = [];
2793
2807
  #error;
2794
- async send(message) {
2808
+ send(message) {
2795
2809
  if (this.#error) {
2796
- throw this.#error;
2810
+ return Promise.reject(this.#error);
2811
+ }
2812
+ try {
2813
+ this.#port.postMessage(message);
2814
+ return RESOLVED_VOID_PROMISE3;
2815
+ } catch (err) {
2816
+ return Promise.reject(err);
2797
2817
  }
2798
- this.#port.postMessage(message);
2799
2818
  }
2800
- async receive() {
2819
+ receive() {
2801
2820
  if (this.#receiveQueue.length > 0) {
2802
- return this.#receiveQueue.shift();
2821
+ return Promise.resolve(this.#receiveQueue.shift());
2803
2822
  } else if (this.#error) {
2804
- throw this.#error;
2823
+ return Promise.reject(this.#error);
2805
2824
  } else {
2806
2825
  return new Promise((resolve, reject) => {
2807
2826
  this.#receiveResolver = resolve;