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