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