@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.
@@ -2045,9 +2045,6 @@ var RpcSessionImpl = class {
2045
2045
  this.options = options;
2046
2046
  this.exports.push({ hook: mainHook, refcount: 1 });
2047
2047
  this.imports.push(new ImportTableEntry(this, 0, false));
2048
- this.cancelReadLoop = (error) => {
2049
- this.activeReadAbort?.reject(error);
2050
- };
2051
2048
  this.readLoop().catch((err) => this.abort(err));
2052
2049
  }
2053
2050
  exports = [];
@@ -2055,7 +2052,6 @@ var RpcSessionImpl = class {
2055
2052
  imports = [];
2056
2053
  abortReason;
2057
2054
  cancelReadLoop;
2058
- activeReadAbort;
2059
2055
  // We assign positive numbers to imports we initiate, and negative numbers to exports we
2060
2056
  // initiate. So the next import ID is just `imports.length`, but the next export ID needs
2061
2057
  // to be tracked explicitly.
@@ -2333,7 +2329,8 @@ var RpcSessionImpl = class {
2333
2329
  }
2334
2330
  abort(error, trySendAbortMessage = true) {
2335
2331
  if (this.abortReason !== void 0) return;
2336
- this.cancelReadLoop(error);
2332
+ this.cancelReadLoop?.(error);
2333
+ this.cancelReadLoop = void 0;
2337
2334
  if (trySendAbortMessage) {
2338
2335
  try {
2339
2336
  this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
@@ -2371,17 +2368,7 @@ var RpcSessionImpl = class {
2371
2368
  }
2372
2369
  async readLoop() {
2373
2370
  while (!this.abortReason) {
2374
- let readAbort = Promise.withResolvers();
2375
- this.activeReadAbort = readAbort;
2376
- let msgText;
2377
- try {
2378
- msgText = await Promise.race([this.transport.receive(), readAbort.promise]);
2379
- } finally {
2380
- if (this.activeReadAbort === readAbort) {
2381
- this.activeReadAbort = void 0;
2382
- }
2383
- }
2384
- let msg = JSON.parse(msgText);
2371
+ let msg = JSON.parse(await this.receiveOrAbort());
2385
2372
  if (this.abortReason) break;
2386
2373
  if (msg instanceof Array) {
2387
2374
  switch (msg[0]) {
@@ -2463,6 +2450,22 @@ var RpcSessionImpl = class {
2463
2450
  throw new Error(`bad RPC message: ${JSON.stringify(msg)}`);
2464
2451
  }
2465
2452
  }
2453
+ receiveOrAbort() {
2454
+ let readCanceled = Promise.withResolvers();
2455
+ this.cancelReadLoop = readCanceled.reject;
2456
+ let receivePromise;
2457
+ try {
2458
+ receivePromise = this.transport.receive();
2459
+ } catch (err) {
2460
+ this.cancelReadLoop = void 0;
2461
+ return Promise.reject(err);
2462
+ }
2463
+ return Promise.race([receivePromise, readCanceled.promise]).finally(() => {
2464
+ if (this.cancelReadLoop === readCanceled.reject) {
2465
+ this.cancelReadLoop = void 0;
2466
+ }
2467
+ });
2468
+ }
2466
2469
  async drain() {
2467
2470
  if (this.abortReason) {
2468
2471
  throw this.abortReason;
@@ -2509,6 +2512,7 @@ var RpcSession = class {
2509
2512
  };
2510
2513
 
2511
2514
  // src/websocket.ts
2515
+ var RESOLVED_VOID_PROMISE = Promise.resolve();
2512
2516
  function newWebSocketRpcSession(webSocket, localMain, options) {
2513
2517
  if (typeof webSocket === "string") {
2514
2518
  webSocket = new WebSocket(webSocket);
@@ -2573,18 +2577,23 @@ var WebSocketTransport = class {
2573
2577
  #receiveRejecter;
2574
2578
  #receiveQueue = [];
2575
2579
  #error;
2576
- async send(message) {
2577
- if (this.#sendQueue === void 0) {
2578
- this.#webSocket.send(message);
2579
- } else {
2580
- this.#sendQueue.push(message);
2580
+ send(message) {
2581
+ try {
2582
+ if (this.#sendQueue === void 0) {
2583
+ this.#webSocket.send(message);
2584
+ } else {
2585
+ this.#sendQueue.push(message);
2586
+ }
2587
+ return RESOLVED_VOID_PROMISE;
2588
+ } catch (err) {
2589
+ return Promise.reject(err);
2581
2590
  }
2582
2591
  }
2583
- async receive() {
2592
+ receive() {
2584
2593
  if (this.#receiveQueue.length > 0) {
2585
- return this.#receiveQueue.shift();
2594
+ return Promise.resolve(this.#receiveQueue.shift());
2586
2595
  } else if (this.#error) {
2587
- throw this.#error;
2596
+ return Promise.reject(this.#error);
2588
2597
  } else {
2589
2598
  return new Promise((resolve, reject) => {
2590
2599
  this.#receiveResolver = resolve;
@@ -2617,6 +2626,7 @@ var WebSocketTransport = class {
2617
2626
  };
2618
2627
 
2619
2628
  // src/batch.ts
2629
+ var RESOLVED_VOID_PROMISE2 = Promise.resolve();
2620
2630
  var BatchClientTransport = class {
2621
2631
  constructor(sendBatch) {
2622
2632
  this.#promise = this.#scheduleBatch(sendBatch);
@@ -2625,10 +2635,11 @@ var BatchClientTransport = class {
2625
2635
  #aborted;
2626
2636
  #batchToSend = [];
2627
2637
  #batchToReceive = null;
2628
- async send(message) {
2638
+ send(message) {
2629
2639
  if (this.#batchToSend !== null) {
2630
2640
  this.#batchToSend.push(message);
2631
2641
  }
2642
+ return RESOLVED_VOID_PROMISE2;
2632
2643
  }
2633
2644
  async receive() {
2634
2645
  if (!this.#batchToReceive) {
@@ -2678,8 +2689,9 @@ var BatchServerTransport = class {
2678
2689
  #batchToSend = [];
2679
2690
  #batchToReceive;
2680
2691
  #allReceived = Promise.withResolvers();
2681
- async send(message) {
2692
+ send(message) {
2682
2693
  this.#batchToSend.push(message);
2694
+ return RESOLVED_VOID_PROMISE2;
2683
2695
  }
2684
2696
  async receive() {
2685
2697
  let msg = this.#batchToReceive.shift();
@@ -2737,6 +2749,7 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2737
2749
  }
2738
2750
 
2739
2751
  // src/messageport.ts
2752
+ var RESOLVED_VOID_PROMISE3 = Promise.resolve();
2740
2753
  function newMessagePortRpcSession(port, localMain, options) {
2741
2754
  let transport = new MessagePortTransport(port);
2742
2755
  let rpc = new RpcSession(transport, localMain, options);
@@ -2770,17 +2783,22 @@ var MessagePortTransport = class {
2770
2783
  #receiveRejecter;
2771
2784
  #receiveQueue = [];
2772
2785
  #error;
2773
- async send(message) {
2786
+ send(message) {
2774
2787
  if (this.#error) {
2775
- throw this.#error;
2788
+ return Promise.reject(this.#error);
2789
+ }
2790
+ try {
2791
+ this.#port.postMessage(message);
2792
+ return RESOLVED_VOID_PROMISE3;
2793
+ } catch (err) {
2794
+ return Promise.reject(err);
2776
2795
  }
2777
- this.#port.postMessage(message);
2778
2796
  }
2779
- async receive() {
2797
+ receive() {
2780
2798
  if (this.#receiveQueue.length > 0) {
2781
- return this.#receiveQueue.shift();
2799
+ return Promise.resolve(this.#receiveQueue.shift());
2782
2800
  } else if (this.#error) {
2783
- throw this.#error;
2801
+ return Promise.reject(this.#error);
2784
2802
  } else {
2785
2803
  return new Promise((resolve, reject) => {
2786
2804
  this.#receiveResolver = resolve;