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