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