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