@dxos/teleport 0.6.12-main.ed7cda7 → 0.6.12-staging.0b4bb48
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/lib/{node-esm/chunk-K64VAYIG.mjs → browser/chunk-ISJQDU2V.mjs} +45 -164
 - package/dist/lib/browser/chunk-ISJQDU2V.mjs.map +7 -0
 - package/dist/lib/browser/index.mjs +2 -7
 - package/dist/lib/browser/index.mjs.map +3 -3
 - package/dist/lib/browser/meta.json +1 -1
 - package/dist/lib/browser/testing/index.mjs +1 -1
 - package/dist/lib/node/{chunk-F45T5HZ2.cjs → chunk-TC7PLXKV.cjs} +47 -155
 - package/dist/lib/node/chunk-TC7PLXKV.cjs.map +7 -0
 - package/dist/lib/node/index.cjs +11 -11
 - package/dist/lib/node/meta.json +1 -1
 - package/dist/lib/node/testing/index.cjs +6 -6
 - package/dist/lib/node/testing/index.cjs.map +1 -1
 - package/dist/types/src/muxing/muxer.d.ts.map +1 -1
 - package/dist/types/src/teleport.d.ts.map +1 -1
 - package/package.json +17 -17
 - package/src/muxing/balancer.test.ts +9 -5
 - package/src/muxing/balancer.ts +1 -1
 - package/src/muxing/framer.test.ts +10 -6
 - package/src/muxing/muxer.test.ts +7 -3
 - package/src/muxing/muxer.ts +0 -3
 - package/src/muxing/rpc-port.test.ts +1 -2
 - package/src/teleport.test.ts +5 -4
 - package/src/teleport.ts +0 -1
 - package/src/testing/test-extension-with-streams.ts +2 -2
 - package/testing.d.ts +11 -0
 - package/testing.js +5 -0
 - package/dist/lib/browser/chunk-NLPOT6AJ.mjs +0 -2130
 - package/dist/lib/browser/chunk-NLPOT6AJ.mjs.map +0 -7
 - package/dist/lib/node/chunk-F45T5HZ2.cjs.map +0 -7
 - package/dist/lib/node-esm/chunk-K64VAYIG.mjs.map +0 -7
 - package/dist/lib/node-esm/index.mjs +0 -86
 - package/dist/lib/node-esm/index.mjs.map +0 -7
 - package/dist/lib/node-esm/meta.json +0 -1
 - package/dist/lib/node-esm/testing/index.mjs +0 -16
 - package/dist/lib/node-esm/testing/index.mjs.map +0 -7
 
| 
         @@ -1,2130 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import "@dxos/node-std/globals";
         
     | 
| 
       2 
     | 
    
         
            -
            var __create = Object.create;
         
     | 
| 
       3 
     | 
    
         
            -
            var __defProp = Object.defineProperty;
         
     | 
| 
       4 
     | 
    
         
            -
            var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
         
     | 
| 
       5 
     | 
    
         
            -
            var __getOwnPropNames = Object.getOwnPropertyNames;
         
     | 
| 
       6 
     | 
    
         
            -
            var __getProtoOf = Object.getPrototypeOf;
         
     | 
| 
       7 
     | 
    
         
            -
            var __hasOwnProp = Object.prototype.hasOwnProperty;
         
     | 
| 
       8 
     | 
    
         
            -
            var __esm = (fn, res) => function __init() {
         
     | 
| 
       9 
     | 
    
         
            -
              return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
         
     | 
| 
       10 
     | 
    
         
            -
            };
         
     | 
| 
       11 
     | 
    
         
            -
            var __commonJS = (cb, mod) => function __require() {
         
     | 
| 
       12 
     | 
    
         
            -
              return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
         
     | 
| 
       13 
     | 
    
         
            -
            };
         
     | 
| 
       14 
     | 
    
         
            -
            var __copyProps = (to, from, except, desc) => {
         
     | 
| 
       15 
     | 
    
         
            -
              if (from && typeof from === "object" || typeof from === "function") {
         
     | 
| 
       16 
     | 
    
         
            -
                for (let key of __getOwnPropNames(from))
         
     | 
| 
       17 
     | 
    
         
            -
                  if (!__hasOwnProp.call(to, key) && key !== except)
         
     | 
| 
       18 
     | 
    
         
            -
                    __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
         
     | 
| 
       19 
     | 
    
         
            -
              }
         
     | 
| 
       20 
     | 
    
         
            -
              return to;
         
     | 
| 
       21 
     | 
    
         
            -
            };
         
     | 
| 
       22 
     | 
    
         
            -
            var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
         
     | 
| 
       23 
     | 
    
         
            -
              // If the importer is in node compatibility mode or this is not an ESM
         
     | 
| 
       24 
     | 
    
         
            -
              // file that has been converted to a CommonJS file using a Babel-
         
     | 
| 
       25 
     | 
    
         
            -
              // compatible transform (i.e. "__esModule" has not been set), then set
         
     | 
| 
       26 
     | 
    
         
            -
              // "default" to the CommonJS "module.exports" for node compatibility.
         
     | 
| 
       27 
     | 
    
         
            -
              isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
         
     | 
| 
       28 
     | 
    
         
            -
              mod
         
     | 
| 
       29 
     | 
    
         
            -
            ));
         
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
            // inject-globals:@inject-globals
         
     | 
| 
       32 
     | 
    
         
            -
            import {
         
     | 
| 
       33 
     | 
    
         
            -
              global,
         
     | 
| 
       34 
     | 
    
         
            -
              Buffer as Buffer2,
         
     | 
| 
       35 
     | 
    
         
            -
              process
         
     | 
| 
       36 
     | 
    
         
            -
            } from "@dxos/node-std/inject-globals";
         
     | 
| 
       37 
     | 
    
         
            -
            var init_inject_globals = __esm({
         
     | 
| 
       38 
     | 
    
         
            -
              "inject-globals:@inject-globals"() {
         
     | 
| 
       39 
     | 
    
         
            -
              }
         
     | 
| 
       40 
     | 
    
         
            -
            });
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
            // node_modules/.pnpm/varint@6.0.0/node_modules/varint/encode.js
         
     | 
| 
       43 
     | 
    
         
            -
            var require_encode = __commonJS({
         
     | 
| 
       44 
     | 
    
         
            -
              "node_modules/.pnpm/varint@6.0.0/node_modules/varint/encode.js"(exports, module) {
         
     | 
| 
       45 
     | 
    
         
            -
                init_inject_globals();
         
     | 
| 
       46 
     | 
    
         
            -
                module.exports = encode;
         
     | 
| 
       47 
     | 
    
         
            -
                var MSB = 128;
         
     | 
| 
       48 
     | 
    
         
            -
                var REST = 127;
         
     | 
| 
       49 
     | 
    
         
            -
                var MSBALL = ~REST;
         
     | 
| 
       50 
     | 
    
         
            -
                var INT = Math.pow(2, 31);
         
     | 
| 
       51 
     | 
    
         
            -
                function encode(num, out, offset) {
         
     | 
| 
       52 
     | 
    
         
            -
                  if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
         
     | 
| 
       53 
     | 
    
         
            -
                    encode.bytes = 0;
         
     | 
| 
       54 
     | 
    
         
            -
                    throw new RangeError("Could not encode varint");
         
     | 
| 
       55 
     | 
    
         
            -
                  }
         
     | 
| 
       56 
     | 
    
         
            -
                  out = out || [];
         
     | 
| 
       57 
     | 
    
         
            -
                  offset = offset || 0;
         
     | 
| 
       58 
     | 
    
         
            -
                  var oldOffset = offset;
         
     | 
| 
       59 
     | 
    
         
            -
                  while (num >= INT) {
         
     | 
| 
       60 
     | 
    
         
            -
                    out[offset++] = num & 255 | MSB;
         
     | 
| 
       61 
     | 
    
         
            -
                    num /= 128;
         
     | 
| 
       62 
     | 
    
         
            -
                  }
         
     | 
| 
       63 
     | 
    
         
            -
                  while (num & MSBALL) {
         
     | 
| 
       64 
     | 
    
         
            -
                    out[offset++] = num & 255 | MSB;
         
     | 
| 
       65 
     | 
    
         
            -
                    num >>>= 7;
         
     | 
| 
       66 
     | 
    
         
            -
                  }
         
     | 
| 
       67 
     | 
    
         
            -
                  out[offset] = num | 0;
         
     | 
| 
       68 
     | 
    
         
            -
                  encode.bytes = offset - oldOffset + 1;
         
     | 
| 
       69 
     | 
    
         
            -
                  return out;
         
     | 
| 
       70 
     | 
    
         
            -
                }
         
     | 
| 
       71 
     | 
    
         
            -
              }
         
     | 
| 
       72 
     | 
    
         
            -
            });
         
     | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
       74 
     | 
    
         
            -
            // node_modules/.pnpm/varint@6.0.0/node_modules/varint/decode.js
         
     | 
| 
       75 
     | 
    
         
            -
            var require_decode = __commonJS({
         
     | 
| 
       76 
     | 
    
         
            -
              "node_modules/.pnpm/varint@6.0.0/node_modules/varint/decode.js"(exports, module) {
         
     | 
| 
       77 
     | 
    
         
            -
                init_inject_globals();
         
     | 
| 
       78 
     | 
    
         
            -
                module.exports = read;
         
     | 
| 
       79 
     | 
    
         
            -
                var MSB = 128;
         
     | 
| 
       80 
     | 
    
         
            -
                var REST = 127;
         
     | 
| 
       81 
     | 
    
         
            -
                function read(buf, offset) {
         
     | 
| 
       82 
     | 
    
         
            -
                  var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
         
     | 
| 
       83 
     | 
    
         
            -
                  do {
         
     | 
| 
       84 
     | 
    
         
            -
                    if (counter >= l || shift > 49) {
         
     | 
| 
       85 
     | 
    
         
            -
                      read.bytes = 0;
         
     | 
| 
       86 
     | 
    
         
            -
                      throw new RangeError("Could not decode varint");
         
     | 
| 
       87 
     | 
    
         
            -
                    }
         
     | 
| 
       88 
     | 
    
         
            -
                    b = buf[counter++];
         
     | 
| 
       89 
     | 
    
         
            -
                    res += shift < 28 ? (b & REST) << shift : (b & REST) * Math.pow(2, shift);
         
     | 
| 
       90 
     | 
    
         
            -
                    shift += 7;
         
     | 
| 
       91 
     | 
    
         
            -
                  } while (b >= MSB);
         
     | 
| 
       92 
     | 
    
         
            -
                  read.bytes = counter - offset;
         
     | 
| 
       93 
     | 
    
         
            -
                  return res;
         
     | 
| 
       94 
     | 
    
         
            -
                }
         
     | 
| 
       95 
     | 
    
         
            -
              }
         
     | 
| 
       96 
     | 
    
         
            -
            });
         
     | 
| 
       97 
     | 
    
         
            -
             
     | 
| 
       98 
     | 
    
         
            -
            // node_modules/.pnpm/varint@6.0.0/node_modules/varint/length.js
         
     | 
| 
       99 
     | 
    
         
            -
            var require_length = __commonJS({
         
     | 
| 
       100 
     | 
    
         
            -
              "node_modules/.pnpm/varint@6.0.0/node_modules/varint/length.js"(exports, module) {
         
     | 
| 
       101 
     | 
    
         
            -
                init_inject_globals();
         
     | 
| 
       102 
     | 
    
         
            -
                var N1 = Math.pow(2, 7);
         
     | 
| 
       103 
     | 
    
         
            -
                var N2 = Math.pow(2, 14);
         
     | 
| 
       104 
     | 
    
         
            -
                var N3 = Math.pow(2, 21);
         
     | 
| 
       105 
     | 
    
         
            -
                var N4 = Math.pow(2, 28);
         
     | 
| 
       106 
     | 
    
         
            -
                var N5 = Math.pow(2, 35);
         
     | 
| 
       107 
     | 
    
         
            -
                var N6 = Math.pow(2, 42);
         
     | 
| 
       108 
     | 
    
         
            -
                var N7 = Math.pow(2, 49);
         
     | 
| 
       109 
     | 
    
         
            -
                var N8 = Math.pow(2, 56);
         
     | 
| 
       110 
     | 
    
         
            -
                var N9 = Math.pow(2, 63);
         
     | 
| 
       111 
     | 
    
         
            -
                module.exports = function(value) {
         
     | 
| 
       112 
     | 
    
         
            -
                  return value < N1 ? 1 : value < N2 ? 2 : value < N3 ? 3 : value < N4 ? 4 : value < N5 ? 5 : value < N6 ? 6 : value < N7 ? 7 : value < N8 ? 8 : value < N9 ? 9 : 10;
         
     | 
| 
       113 
     | 
    
         
            -
                };
         
     | 
| 
       114 
     | 
    
         
            -
              }
         
     | 
| 
       115 
     | 
    
         
            -
            });
         
     | 
| 
       116 
     | 
    
         
            -
             
     | 
| 
       117 
     | 
    
         
            -
            // node_modules/.pnpm/varint@6.0.0/node_modules/varint/index.js
         
     | 
| 
       118 
     | 
    
         
            -
            var require_varint = __commonJS({
         
     | 
| 
       119 
     | 
    
         
            -
              "node_modules/.pnpm/varint@6.0.0/node_modules/varint/index.js"(exports, module) {
         
     | 
| 
       120 
     | 
    
         
            -
                init_inject_globals();
         
     | 
| 
       121 
     | 
    
         
            -
                module.exports = {
         
     | 
| 
       122 
     | 
    
         
            -
                  encode: require_encode(),
         
     | 
| 
       123 
     | 
    
         
            -
                  decode: require_decode(),
         
     | 
| 
       124 
     | 
    
         
            -
                  encodingLength: require_length()
         
     | 
| 
       125 
     | 
    
         
            -
                };
         
     | 
| 
       126 
     | 
    
         
            -
              }
         
     | 
| 
       127 
     | 
    
         
            -
            });
         
     | 
| 
       128 
     | 
    
         
            -
             
     | 
| 
       129 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/testing/index.ts
         
     | 
| 
       130 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       131 
     | 
    
         
            -
             
     | 
| 
       132 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/testing/test-builder.ts
         
     | 
| 
       133 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       134 
     | 
    
         
            -
            import { pipeline } from "@dxos/node-std/stream";
         
     | 
| 
       135 
     | 
    
         
            -
            import { waitForCondition } from "@dxos/async";
         
     | 
| 
       136 
     | 
    
         
            -
            import { invariant as invariant5 } from "@dxos/invariant";
         
     | 
| 
       137 
     | 
    
         
            -
            import { PublicKey as PublicKey2 } from "@dxos/keys";
         
     | 
| 
       138 
     | 
    
         
            -
            import { log as log6 } from "@dxos/log";
         
     | 
| 
       139 
     | 
    
         
            -
             
     | 
| 
       140 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/teleport.ts
         
     | 
| 
       141 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       142 
     | 
    
         
            -
            import { runInContextAsync, synchronized, scheduleTask } from "@dxos/async";
         
     | 
| 
       143 
     | 
    
         
            -
            import { Context as Context3 } from "@dxos/context";
         
     | 
| 
       144 
     | 
    
         
            -
            import { failUndefined as failUndefined2 } from "@dxos/debug";
         
     | 
| 
       145 
     | 
    
         
            -
            import { invariant as invariant4 } from "@dxos/invariant";
         
     | 
| 
       146 
     | 
    
         
            -
            import { PublicKey } from "@dxos/keys";
         
     | 
| 
       147 
     | 
    
         
            -
            import { log as log5, logInfo as logInfo2 } from "@dxos/log";
         
     | 
| 
       148 
     | 
    
         
            -
            import { RpcClosedError as RpcClosedError2, TimeoutError as TimeoutError2 } from "@dxos/protocols";
         
     | 
| 
       149 
     | 
    
         
            -
             
     | 
| 
       150 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/control-extension.ts
         
     | 
| 
       151 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       152 
     | 
    
         
            -
            import { asyncTimeout, scheduleTaskInterval, TimeoutError as AsyncTimeoutError } from "@dxos/async";
         
     | 
| 
       153 
     | 
    
         
            -
            import { Context } from "@dxos/context";
         
     | 
| 
       154 
     | 
    
         
            -
            import { log } from "@dxos/log";
         
     | 
| 
       155 
     | 
    
         
            -
            import { RpcClosedError } from "@dxos/protocols";
         
     | 
| 
       156 
     | 
    
         
            -
            import { schema } from "@dxos/protocols/proto";
         
     | 
| 
       157 
     | 
    
         
            -
            import { createProtoRpcPeer } from "@dxos/rpc";
         
     | 
| 
       158 
     | 
    
         
            -
            import { Callback } from "@dxos/util";
         
     | 
| 
       159 
     | 
    
         
            -
            var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/control-extension.ts";
         
     | 
| 
       160 
     | 
    
         
            -
            var HEARTBEAT_RTT_WARN_THRESH = 1e4;
         
     | 
| 
       161 
     | 
    
         
            -
            var DEBUG_PRINT_HEARTBEAT = false;
         
     | 
| 
       162 
     | 
    
         
            -
            var ControlExtension = class {
         
     | 
| 
       163 
     | 
    
         
            -
              constructor(opts, localPeerId, remotePeerId) {
         
     | 
| 
       164 
     | 
    
         
            -
                this.opts = opts;
         
     | 
| 
       165 
     | 
    
         
            -
                this.localPeerId = localPeerId;
         
     | 
| 
       166 
     | 
    
         
            -
                this.remotePeerId = remotePeerId;
         
     | 
| 
       167 
     | 
    
         
            -
                this._ctx = new Context({
         
     | 
| 
       168 
     | 
    
         
            -
                  onError: (err) => {
         
     | 
| 
       169 
     | 
    
         
            -
                    this._extensionContext.close(err);
         
     | 
| 
       170 
     | 
    
         
            -
                  }
         
     | 
| 
       171 
     | 
    
         
            -
                }, {
         
     | 
| 
       172 
     | 
    
         
            -
                  F: __dxlog_file,
         
     | 
| 
       173 
     | 
    
         
            -
                  L: 31
         
     | 
| 
       174 
     | 
    
         
            -
                });
         
     | 
| 
       175 
     | 
    
         
            -
                this.onExtensionRegistered = new Callback();
         
     | 
| 
       176 
     | 
    
         
            -
              }
         
     | 
| 
       177 
     | 
    
         
            -
              async registerExtension(name) {
         
     | 
| 
       178 
     | 
    
         
            -
                await this._rpc.rpc.Control.registerExtension({
         
     | 
| 
       179 
     | 
    
         
            -
                  name
         
     | 
| 
       180 
     | 
    
         
            -
                });
         
     | 
| 
       181 
     | 
    
         
            -
              }
         
     | 
| 
       182 
     | 
    
         
            -
              async onOpen(extensionContext) {
         
     | 
| 
       183 
     | 
    
         
            -
                this._extensionContext = extensionContext;
         
     | 
| 
       184 
     | 
    
         
            -
                this._rpc = createProtoRpcPeer({
         
     | 
| 
       185 
     | 
    
         
            -
                  requested: {
         
     | 
| 
       186 
     | 
    
         
            -
                    Control: schema.getService("dxos.mesh.teleport.control.ControlService")
         
     | 
| 
       187 
     | 
    
         
            -
                  },
         
     | 
| 
       188 
     | 
    
         
            -
                  exposed: {
         
     | 
| 
       189 
     | 
    
         
            -
                    Control: schema.getService("dxos.mesh.teleport.control.ControlService")
         
     | 
| 
       190 
     | 
    
         
            -
                  },
         
     | 
| 
       191 
     | 
    
         
            -
                  handlers: {
         
     | 
| 
       192 
     | 
    
         
            -
                    Control: {
         
     | 
| 
       193 
     | 
    
         
            -
                      registerExtension: async (request) => {
         
     | 
| 
       194 
     | 
    
         
            -
                        this.onExtensionRegistered.call(request.name);
         
     | 
| 
       195 
     | 
    
         
            -
                      },
         
     | 
| 
       196 
     | 
    
         
            -
                      heartbeat: async (request) => {
         
     | 
| 
       197 
     | 
    
         
            -
                        if (DEBUG_PRINT_HEARTBEAT) {
         
     | 
| 
       198 
     | 
    
         
            -
                          log("received heartbeat request", {
         
     | 
| 
       199 
     | 
    
         
            -
                            ts: request.requestTimestamp,
         
     | 
| 
       200 
     | 
    
         
            -
                            localPeerId: this.localPeerId.truncate(),
         
     | 
| 
       201 
     | 
    
         
            -
                            remotePeerId: this.remotePeerId.truncate()
         
     | 
| 
       202 
     | 
    
         
            -
                          }, {
         
     | 
| 
       203 
     | 
    
         
            -
                            F: __dxlog_file,
         
     | 
| 
       204 
     | 
    
         
            -
                            L: 69,
         
     | 
| 
       205 
     | 
    
         
            -
                            S: this,
         
     | 
| 
       206 
     | 
    
         
            -
                            C: (f, a) => f(...a)
         
     | 
| 
       207 
     | 
    
         
            -
                          });
         
     | 
| 
       208 
     | 
    
         
            -
                        }
         
     | 
| 
       209 
     | 
    
         
            -
                        return {
         
     | 
| 
       210 
     | 
    
         
            -
                          requestTimestamp: request.requestTimestamp
         
     | 
| 
       211 
     | 
    
         
            -
                        };
         
     | 
| 
       212 
     | 
    
         
            -
                      }
         
     | 
| 
       213 
     | 
    
         
            -
                    }
         
     | 
| 
       214 
     | 
    
         
            -
                  },
         
     | 
| 
       215 
     | 
    
         
            -
                  port: await extensionContext.createPort("rpc", {
         
     | 
| 
       216 
     | 
    
         
            -
                    contentType: 'application/x-protobuf; messageType="dxos.rpc.Message"'
         
     | 
| 
       217 
     | 
    
         
            -
                  }),
         
     | 
| 
       218 
     | 
    
         
            -
                  timeout: this.opts.heartbeatTimeout
         
     | 
| 
       219 
     | 
    
         
            -
                });
         
     | 
| 
       220 
     | 
    
         
            -
                await this._rpc.open();
         
     | 
| 
       221 
     | 
    
         
            -
                scheduleTaskInterval(this._ctx, async () => {
         
     | 
| 
       222 
     | 
    
         
            -
                  const reqTS = /* @__PURE__ */ new Date();
         
     | 
| 
       223 
     | 
    
         
            -
                  try {
         
     | 
| 
       224 
     | 
    
         
            -
                    const resp = await asyncTimeout(this._rpc.rpc.Control.heartbeat({
         
     | 
| 
       225 
     | 
    
         
            -
                      requestTimestamp: reqTS
         
     | 
| 
       226 
     | 
    
         
            -
                    }), this.opts.heartbeatTimeout);
         
     | 
| 
       227 
     | 
    
         
            -
                    const now = Date.now();
         
     | 
| 
       228 
     | 
    
         
            -
                    if (resp.requestTimestamp instanceof Date) {
         
     | 
| 
       229 
     | 
    
         
            -
                      if (now - resp.requestTimestamp.getTime() > (HEARTBEAT_RTT_WARN_THRESH < this.opts.heartbeatTimeout ? HEARTBEAT_RTT_WARN_THRESH : this.opts.heartbeatTimeout / 2)) {
         
     | 
| 
       230 
     | 
    
         
            -
                        log.warn(`heartbeat RTT for Teleport > ${HEARTBEAT_RTT_WARN_THRESH / 1e3}s`, {
         
     | 
| 
       231 
     | 
    
         
            -
                          rtt: now - resp.requestTimestamp.getTime(),
         
     | 
| 
       232 
     | 
    
         
            -
                          localPeerId: this.localPeerId.truncate(),
         
     | 
| 
       233 
     | 
    
         
            -
                          remotePeerId: this.remotePeerId.truncate()
         
     | 
| 
       234 
     | 
    
         
            -
                        }, {
         
     | 
| 
       235 
     | 
    
         
            -
                          F: __dxlog_file,
         
     | 
| 
       236 
     | 
    
         
            -
                          L: 107,
         
     | 
| 
       237 
     | 
    
         
            -
                          S: this,
         
     | 
| 
       238 
     | 
    
         
            -
                          C: (f, a) => f(...a)
         
     | 
| 
       239 
     | 
    
         
            -
                        });
         
     | 
| 
       240 
     | 
    
         
            -
                      } else {
         
     | 
| 
       241 
     | 
    
         
            -
                        if (DEBUG_PRINT_HEARTBEAT) {
         
     | 
| 
       242 
     | 
    
         
            -
                          log("heartbeat RTT", {
         
     | 
| 
       243 
     | 
    
         
            -
                            rtt: now - resp.requestTimestamp.getTime(),
         
     | 
| 
       244 
     | 
    
         
            -
                            localPeerId: this.localPeerId.truncate(),
         
     | 
| 
       245 
     | 
    
         
            -
                            remotePeerId: this.remotePeerId.truncate()
         
     | 
| 
       246 
     | 
    
         
            -
                          }, {
         
     | 
| 
       247 
     | 
    
         
            -
                            F: __dxlog_file,
         
     | 
| 
       248 
     | 
    
         
            -
                            L: 114,
         
     | 
| 
       249 
     | 
    
         
            -
                            S: this,
         
     | 
| 
       250 
     | 
    
         
            -
                            C: (f, a) => f(...a)
         
     | 
| 
       251 
     | 
    
         
            -
                          });
         
     | 
| 
       252 
     | 
    
         
            -
                        }
         
     | 
| 
       253 
     | 
    
         
            -
                      }
         
     | 
| 
       254 
     | 
    
         
            -
                    }
         
     | 
| 
       255 
     | 
    
         
            -
                  } catch (err) {
         
     | 
| 
       256 
     | 
    
         
            -
                    const now = Date.now();
         
     | 
| 
       257 
     | 
    
         
            -
                    if (err instanceof RpcClosedError) {
         
     | 
| 
       258 
     | 
    
         
            -
                      log("ignoring RpcClosedError in heartbeat", void 0, {
         
     | 
| 
       259 
     | 
    
         
            -
                        F: __dxlog_file,
         
     | 
| 
       260 
     | 
    
         
            -
                        L: 126,
         
     | 
| 
       261 
     | 
    
         
            -
                        S: this,
         
     | 
| 
       262 
     | 
    
         
            -
                        C: (f, a) => f(...a)
         
     | 
| 
       263 
     | 
    
         
            -
                      });
         
     | 
| 
       264 
     | 
    
         
            -
                      this._extensionContext.close(err);
         
     | 
| 
       265 
     | 
    
         
            -
                      return;
         
     | 
| 
       266 
     | 
    
         
            -
                    }
         
     | 
| 
       267 
     | 
    
         
            -
                    if (err instanceof AsyncTimeoutError) {
         
     | 
| 
       268 
     | 
    
         
            -
                      log("timeout waiting for heartbeat response", {
         
     | 
| 
       269 
     | 
    
         
            -
                        err,
         
     | 
| 
       270 
     | 
    
         
            -
                        delay: now - reqTS.getTime()
         
     | 
| 
       271 
     | 
    
         
            -
                      }, {
         
     | 
| 
       272 
     | 
    
         
            -
                        F: __dxlog_file,
         
     | 
| 
       273 
     | 
    
         
            -
                        L: 131,
         
     | 
| 
       274 
     | 
    
         
            -
                        S: this,
         
     | 
| 
       275 
     | 
    
         
            -
                        C: (f, a) => f(...a)
         
     | 
| 
       276 
     | 
    
         
            -
                      });
         
     | 
| 
       277 
     | 
    
         
            -
                      this.opts.onTimeout(err);
         
     | 
| 
       278 
     | 
    
         
            -
                    } else {
         
     | 
| 
       279 
     | 
    
         
            -
                      log.info("other error waiting for heartbeat response", {
         
     | 
| 
       280 
     | 
    
         
            -
                        err,
         
     | 
| 
       281 
     | 
    
         
            -
                        delay: now - reqTS.getTime()
         
     | 
| 
       282 
     | 
    
         
            -
                      }, {
         
     | 
| 
       283 
     | 
    
         
            -
                        F: __dxlog_file,
         
     | 
| 
       284 
     | 
    
         
            -
                        L: 134,
         
     | 
| 
       285 
     | 
    
         
            -
                        S: this,
         
     | 
| 
       286 
     | 
    
         
            -
                        C: (f, a) => f(...a)
         
     | 
| 
       287 
     | 
    
         
            -
                      });
         
     | 
| 
       288 
     | 
    
         
            -
                      this.opts.onTimeout(err);
         
     | 
| 
       289 
     | 
    
         
            -
                    }
         
     | 
| 
       290 
     | 
    
         
            -
                  }
         
     | 
| 
       291 
     | 
    
         
            -
                }, this.opts.heartbeatInterval);
         
     | 
| 
       292 
     | 
    
         
            -
              }
         
     | 
| 
       293 
     | 
    
         
            -
              async onClose(err) {
         
     | 
| 
       294 
     | 
    
         
            -
                await this._ctx.dispose();
         
     | 
| 
       295 
     | 
    
         
            -
                await this._rpc.close();
         
     | 
| 
       296 
     | 
    
         
            -
              }
         
     | 
| 
       297 
     | 
    
         
            -
              async onAbort(err) {
         
     | 
| 
       298 
     | 
    
         
            -
                await this._ctx.dispose();
         
     | 
| 
       299 
     | 
    
         
            -
                await this._rpc.abort();
         
     | 
| 
       300 
     | 
    
         
            -
              }
         
     | 
| 
       301 
     | 
    
         
            -
            };
         
     | 
| 
       302 
     | 
    
         
            -
             
     | 
| 
       303 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/muxing/index.ts
         
     | 
| 
       304 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       305 
     | 
    
         
            -
             
     | 
| 
       306 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/muxing/framer.ts
         
     | 
| 
       307 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       308 
     | 
    
         
            -
            import { Duplex } from "@dxos/node-std/stream";
         
     | 
| 
       309 
     | 
    
         
            -
            import { Event } from "@dxos/async";
         
     | 
| 
       310 
     | 
    
         
            -
            import { invariant } from "@dxos/invariant";
         
     | 
| 
       311 
     | 
    
         
            -
            import { log as log2 } from "@dxos/log";
         
     | 
| 
       312 
     | 
    
         
            -
            var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/framer.ts";
         
     | 
| 
       313 
     | 
    
         
            -
            var FRAME_LENGTH_SIZE = 2;
         
     | 
| 
       314 
     | 
    
         
            -
            var Framer = class {
         
     | 
| 
       315 
     | 
    
         
            -
              constructor() {
         
     | 
| 
       316 
     | 
    
         
            -
                // private readonly _tagBuffer = Buffer.alloc(4)
         
     | 
| 
       317 
     | 
    
         
            -
                this._messageCb = void 0;
         
     | 
| 
       318 
     | 
    
         
            -
                this._subscribeCb = void 0;
         
     | 
| 
       319 
     | 
    
         
            -
                this._buffer = void 0;
         
     | 
| 
       320 
     | 
    
         
            -
                this._sendCallbacks = [];
         
     | 
| 
       321 
     | 
    
         
            -
                this._bytesSent = 0;
         
     | 
| 
       322 
     | 
    
         
            -
                this._bytesReceived = 0;
         
     | 
| 
       323 
     | 
    
         
            -
                this._writable = true;
         
     | 
| 
       324 
     | 
    
         
            -
                this.drain = new Event();
         
     | 
| 
       325 
     | 
    
         
            -
                // TODO(egorgripasov): Consider using a Transform stream if it provides better backpressure handling.
         
     | 
| 
       326 
     | 
    
         
            -
                this._stream = new Duplex({
         
     | 
| 
       327 
     | 
    
         
            -
                  objectMode: false,
         
     | 
| 
       328 
     | 
    
         
            -
                  read: () => {
         
     | 
| 
       329 
     | 
    
         
            -
                    this._processResponseQueue();
         
     | 
| 
       330 
     | 
    
         
            -
                  },
         
     | 
| 
       331 
     | 
    
         
            -
                  write: (chunk, encoding, callback) => {
         
     | 
| 
       332 
     | 
    
         
            -
                    invariant(!this._subscribeCb, "Internal Framer bug. Concurrent writes detected.", {
         
     | 
| 
       333 
     | 
    
         
            -
                      F: __dxlog_file2,
         
     | 
| 
       334 
     | 
    
         
            -
                      L: 40,
         
     | 
| 
       335 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       336 
     | 
    
         
            -
                      A: [
         
     | 
| 
       337 
     | 
    
         
            -
                        "!this._subscribeCb",
         
     | 
| 
       338 
     | 
    
         
            -
                        "'Internal Framer bug. Concurrent writes detected.'"
         
     | 
| 
       339 
     | 
    
         
            -
                      ]
         
     | 
| 
       340 
     | 
    
         
            -
                    });
         
     | 
| 
       341 
     | 
    
         
            -
                    this._bytesReceived += chunk.length;
         
     | 
| 
       342 
     | 
    
         
            -
                    if (this._buffer && this._buffer.length > 0) {
         
     | 
| 
       343 
     | 
    
         
            -
                      this._buffer = Buffer2.concat([
         
     | 
| 
       344 
     | 
    
         
            -
                        this._buffer,
         
     | 
| 
       345 
     | 
    
         
            -
                        chunk
         
     | 
| 
       346 
     | 
    
         
            -
                      ]);
         
     | 
| 
       347 
     | 
    
         
            -
                    } else {
         
     | 
| 
       348 
     | 
    
         
            -
                      this._buffer = chunk;
         
     | 
| 
       349 
     | 
    
         
            -
                    }
         
     | 
| 
       350 
     | 
    
         
            -
                    if (this._messageCb) {
         
     | 
| 
       351 
     | 
    
         
            -
                      this._popFrames();
         
     | 
| 
       352 
     | 
    
         
            -
                      callback();
         
     | 
| 
       353 
     | 
    
         
            -
                    } else {
         
     | 
| 
       354 
     | 
    
         
            -
                      this._subscribeCb = () => {
         
     | 
| 
       355 
     | 
    
         
            -
                        this._popFrames();
         
     | 
| 
       356 
     | 
    
         
            -
                        this._subscribeCb = void 0;
         
     | 
| 
       357 
     | 
    
         
            -
                        callback();
         
     | 
| 
       358 
     | 
    
         
            -
                      };
         
     | 
| 
       359 
     | 
    
         
            -
                    }
         
     | 
| 
       360 
     | 
    
         
            -
                  }
         
     | 
| 
       361 
     | 
    
         
            -
                });
         
     | 
| 
       362 
     | 
    
         
            -
                this.port = {
         
     | 
| 
       363 
     | 
    
         
            -
                  send: (message) => {
         
     | 
| 
       364 
     | 
    
         
            -
                    return new Promise((resolve) => {
         
     | 
| 
       365 
     | 
    
         
            -
                      const frame = encodeFrame(message);
         
     | 
| 
       366 
     | 
    
         
            -
                      this._bytesSent += frame.length;
         
     | 
| 
       367 
     | 
    
         
            -
                      this._writable = this._stream.push(frame);
         
     | 
| 
       368 
     | 
    
         
            -
                      if (!this._writable) {
         
     | 
| 
       369 
     | 
    
         
            -
                        this._sendCallbacks.push(resolve);
         
     | 
| 
       370 
     | 
    
         
            -
                      } else {
         
     | 
| 
       371 
     | 
    
         
            -
                        resolve();
         
     | 
| 
       372 
     | 
    
         
            -
                      }
         
     | 
| 
       373 
     | 
    
         
            -
                    });
         
     | 
| 
       374 
     | 
    
         
            -
                  },
         
     | 
| 
       375 
     | 
    
         
            -
                  subscribe: (callback) => {
         
     | 
| 
       376 
     | 
    
         
            -
                    invariant(!this._messageCb, "Rpc port already has a message listener.", {
         
     | 
| 
       377 
     | 
    
         
            -
                      F: __dxlog_file2,
         
     | 
| 
       378 
     | 
    
         
            -
                      L: 79,
         
     | 
| 
       379 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       380 
     | 
    
         
            -
                      A: [
         
     | 
| 
       381 
     | 
    
         
            -
                        "!this._messageCb",
         
     | 
| 
       382 
     | 
    
         
            -
                        "'Rpc port already has a message listener.'"
         
     | 
| 
       383 
     | 
    
         
            -
                      ]
         
     | 
| 
       384 
     | 
    
         
            -
                    });
         
     | 
| 
       385 
     | 
    
         
            -
                    this._messageCb = callback;
         
     | 
| 
       386 
     | 
    
         
            -
                    this._subscribeCb?.();
         
     | 
| 
       387 
     | 
    
         
            -
                    return () => {
         
     | 
| 
       388 
     | 
    
         
            -
                      this._messageCb = void 0;
         
     | 
| 
       389 
     | 
    
         
            -
                    };
         
     | 
| 
       390 
     | 
    
         
            -
                  }
         
     | 
| 
       391 
     | 
    
         
            -
                };
         
     | 
| 
       392 
     | 
    
         
            -
              }
         
     | 
| 
       393 
     | 
    
         
            -
              get stream() {
         
     | 
| 
       394 
     | 
    
         
            -
                return this._stream;
         
     | 
| 
       395 
     | 
    
         
            -
              }
         
     | 
| 
       396 
     | 
    
         
            -
              get bytesSent() {
         
     | 
| 
       397 
     | 
    
         
            -
                return this._bytesSent;
         
     | 
| 
       398 
     | 
    
         
            -
              }
         
     | 
| 
       399 
     | 
    
         
            -
              get bytesReceived() {
         
     | 
| 
       400 
     | 
    
         
            -
                return this._bytesReceived;
         
     | 
| 
       401 
     | 
    
         
            -
              }
         
     | 
| 
       402 
     | 
    
         
            -
              get writable() {
         
     | 
| 
       403 
     | 
    
         
            -
                return this._writable;
         
     | 
| 
       404 
     | 
    
         
            -
              }
         
     | 
| 
       405 
     | 
    
         
            -
              _processResponseQueue() {
         
     | 
| 
       406 
     | 
    
         
            -
                const responseQueue = this._sendCallbacks;
         
     | 
| 
       407 
     | 
    
         
            -
                this._sendCallbacks = [];
         
     | 
| 
       408 
     | 
    
         
            -
                this._writable = true;
         
     | 
| 
       409 
     | 
    
         
            -
                this.drain.emit();
         
     | 
| 
       410 
     | 
    
         
            -
                responseQueue.forEach((cb) => cb());
         
     | 
| 
       411 
     | 
    
         
            -
              }
         
     | 
| 
       412 
     | 
    
         
            -
              /**
         
     | 
| 
       413 
     | 
    
         
            -
              * Attempts to pop frames from the buffer and call the message callback.
         
     | 
| 
       414 
     | 
    
         
            -
              */
         
     | 
| 
       415 
     | 
    
         
            -
              _popFrames() {
         
     | 
| 
       416 
     | 
    
         
            -
                let offset = 0;
         
     | 
| 
       417 
     | 
    
         
            -
                while (offset < this._buffer.length) {
         
     | 
| 
       418 
     | 
    
         
            -
                  const frame = decodeFrame(this._buffer, offset);
         
     | 
| 
       419 
     | 
    
         
            -
                  if (!frame) {
         
     | 
| 
       420 
     | 
    
         
            -
                    break;
         
     | 
| 
       421 
     | 
    
         
            -
                  }
         
     | 
| 
       422 
     | 
    
         
            -
                  offset += frame.bytesConsumed;
         
     | 
| 
       423 
     | 
    
         
            -
                  this._messageCb(frame.payload);
         
     | 
| 
       424 
     | 
    
         
            -
                }
         
     | 
| 
       425 
     | 
    
         
            -
                if (offset < this._buffer.length) {
         
     | 
| 
       426 
     | 
    
         
            -
                  this._buffer = this._buffer.subarray(offset);
         
     | 
| 
       427 
     | 
    
         
            -
                } else {
         
     | 
| 
       428 
     | 
    
         
            -
                  this._buffer = void 0;
         
     | 
| 
       429 
     | 
    
         
            -
                }
         
     | 
| 
       430 
     | 
    
         
            -
              }
         
     | 
| 
       431 
     | 
    
         
            -
              destroy() {
         
     | 
| 
       432 
     | 
    
         
            -
                if (this._stream.readableLength > 0) {
         
     | 
| 
       433 
     | 
    
         
            -
                  log2("framer destroyed while there are still read bytes in the buffer.", void 0, {
         
     | 
| 
       434 
     | 
    
         
            -
                    F: __dxlog_file2,
         
     | 
| 
       435 
     | 
    
         
            -
                    L: 140,
         
     | 
| 
       436 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       437 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       438 
     | 
    
         
            -
                  });
         
     | 
| 
       439 
     | 
    
         
            -
                }
         
     | 
| 
       440 
     | 
    
         
            -
                if (this._stream.writableLength > 0) {
         
     | 
| 
       441 
     | 
    
         
            -
                  log2.warn("framer destroyed while there are still write bytes in the buffer.", void 0, {
         
     | 
| 
       442 
     | 
    
         
            -
                    F: __dxlog_file2,
         
     | 
| 
       443 
     | 
    
         
            -
                    L: 143,
         
     | 
| 
       444 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       445 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       446 
     | 
    
         
            -
                  });
         
     | 
| 
       447 
     | 
    
         
            -
                }
         
     | 
| 
       448 
     | 
    
         
            -
                this._stream.destroy();
         
     | 
| 
       449 
     | 
    
         
            -
              }
         
     | 
| 
       450 
     | 
    
         
            -
            };
         
     | 
| 
       451 
     | 
    
         
            -
            var decodeFrame = (buffer, offset) => {
         
     | 
| 
       452 
     | 
    
         
            -
              if (buffer.length < offset + FRAME_LENGTH_SIZE) {
         
     | 
| 
       453 
     | 
    
         
            -
                return void 0;
         
     | 
| 
       454 
     | 
    
         
            -
              }
         
     | 
| 
       455 
     | 
    
         
            -
              const frameLength = buffer.readUInt16BE(offset);
         
     | 
| 
       456 
     | 
    
         
            -
              const bytesConsumed = FRAME_LENGTH_SIZE + frameLength;
         
     | 
| 
       457 
     | 
    
         
            -
              if (buffer.length < offset + bytesConsumed) {
         
     | 
| 
       458 
     | 
    
         
            -
                return void 0;
         
     | 
| 
       459 
     | 
    
         
            -
              }
         
     | 
| 
       460 
     | 
    
         
            -
              const payload = buffer.subarray(offset + FRAME_LENGTH_SIZE, offset + bytesConsumed);
         
     | 
| 
       461 
     | 
    
         
            -
              return {
         
     | 
| 
       462 
     | 
    
         
            -
                payload,
         
     | 
| 
       463 
     | 
    
         
            -
                bytesConsumed
         
     | 
| 
       464 
     | 
    
         
            -
              };
         
     | 
| 
       465 
     | 
    
         
            -
            };
         
     | 
| 
       466 
     | 
    
         
            -
            var encodeFrame = (payload) => {
         
     | 
| 
       467 
     | 
    
         
            -
              const frame = Buffer2.allocUnsafe(FRAME_LENGTH_SIZE + payload.length);
         
     | 
| 
       468 
     | 
    
         
            -
              frame.writeUInt16BE(payload.length, 0);
         
     | 
| 
       469 
     | 
    
         
            -
              frame.set(payload, FRAME_LENGTH_SIZE);
         
     | 
| 
       470 
     | 
    
         
            -
              return frame;
         
     | 
| 
       471 
     | 
    
         
            -
            };
         
     | 
| 
       472 
     | 
    
         
            -
             
     | 
| 
       473 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/muxing/muxer.ts
         
     | 
| 
       474 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       475 
     | 
    
         
            -
            import { Duplex as Duplex2 } from "@dxos/node-std/stream";
         
     | 
| 
       476 
     | 
    
         
            -
            import { scheduleTaskInterval as scheduleTaskInterval2, Event as Event3, Trigger, asyncTimeout as asyncTimeout2 } from "@dxos/async";
         
     | 
| 
       477 
     | 
    
         
            -
            import { Context as Context2 } from "@dxos/context";
         
     | 
| 
       478 
     | 
    
         
            -
            import { failUndefined } from "@dxos/debug";
         
     | 
| 
       479 
     | 
    
         
            -
            import { invariant as invariant3 } from "@dxos/invariant";
         
     | 
| 
       480 
     | 
    
         
            -
            import { log as log4, logInfo } from "@dxos/log";
         
     | 
| 
       481 
     | 
    
         
            -
            import { TimeoutError } from "@dxos/protocols";
         
     | 
| 
       482 
     | 
    
         
            -
            import { schema as schema2 } from "@dxos/protocols/proto";
         
     | 
| 
       483 
     | 
    
         
            -
             
     | 
| 
       484 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/muxing/balancer.ts
         
     | 
| 
       485 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       486 
     | 
    
         
            -
            var import_varint = __toESM(require_varint());
         
     | 
| 
       487 
     | 
    
         
            -
            import { Event as Event2 } from "@dxos/async";
         
     | 
| 
       488 
     | 
    
         
            -
            import { invariant as invariant2 } from "@dxos/invariant";
         
     | 
| 
       489 
     | 
    
         
            -
            import { log as log3 } from "@dxos/log";
         
     | 
| 
       490 
     | 
    
         
            -
            var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
         
     | 
| 
       491 
     | 
    
         
            -
            var MAX_CHUNK_SIZE = 8192;
         
     | 
| 
       492 
     | 
    
         
            -
            var Balancer = class {
         
     | 
| 
       493 
     | 
    
         
            -
              constructor(_sysChannelId) {
         
     | 
| 
       494 
     | 
    
         
            -
                this._sysChannelId = _sysChannelId;
         
     | 
| 
       495 
     | 
    
         
            -
                this._lastCallerIndex = 0;
         
     | 
| 
       496 
     | 
    
         
            -
                this._channels = [];
         
     | 
| 
       497 
     | 
    
         
            -
                this._framer = new Framer();
         
     | 
| 
       498 
     | 
    
         
            -
                this._sendBuffers = /* @__PURE__ */ new Map();
         
     | 
| 
       499 
     | 
    
         
            -
                this._receiveBuffers = /* @__PURE__ */ new Map();
         
     | 
| 
       500 
     | 
    
         
            -
                this._sending = false;
         
     | 
| 
       501 
     | 
    
         
            -
                this.incomingData = new Event2();
         
     | 
| 
       502 
     | 
    
         
            -
                this.stream = this._framer.stream;
         
     | 
| 
       503 
     | 
    
         
            -
                this._channels.push(_sysChannelId);
         
     | 
| 
       504 
     | 
    
         
            -
                this._framer.port.subscribe(this._processIncomingMessage.bind(this));
         
     | 
| 
       505 
     | 
    
         
            -
              }
         
     | 
| 
       506 
     | 
    
         
            -
              get bytesSent() {
         
     | 
| 
       507 
     | 
    
         
            -
                return this._framer.bytesSent;
         
     | 
| 
       508 
     | 
    
         
            -
              }
         
     | 
| 
       509 
     | 
    
         
            -
              get bytesReceived() {
         
     | 
| 
       510 
     | 
    
         
            -
                return this._framer.bytesReceived;
         
     | 
| 
       511 
     | 
    
         
            -
              }
         
     | 
| 
       512 
     | 
    
         
            -
              get buffersCount() {
         
     | 
| 
       513 
     | 
    
         
            -
                return this._sendBuffers.size;
         
     | 
| 
       514 
     | 
    
         
            -
              }
         
     | 
| 
       515 
     | 
    
         
            -
              addChannel(channel) {
         
     | 
| 
       516 
     | 
    
         
            -
                this._channels.push(channel);
         
     | 
| 
       517 
     | 
    
         
            -
              }
         
     | 
| 
       518 
     | 
    
         
            -
              pushData(data, trigger, channelId) {
         
     | 
| 
       519 
     | 
    
         
            -
                this._enqueueChunk(data, trigger, channelId);
         
     | 
| 
       520 
     | 
    
         
            -
                this._sendChunks().catch((err) => log3.catch(err, void 0, {
         
     | 
| 
       521 
     | 
    
         
            -
                  F: __dxlog_file3,
         
     | 
| 
       522 
     | 
    
         
            -
                  L: 75,
         
     | 
| 
       523 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       524 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       525 
     | 
    
         
            -
                }));
         
     | 
| 
       526 
     | 
    
         
            -
              }
         
     | 
| 
       527 
     | 
    
         
            -
              destroy() {
         
     | 
| 
       528 
     | 
    
         
            -
                if (this._sendBuffers.size !== 0) {
         
     | 
| 
       529 
     | 
    
         
            -
                  log3.info("destroying balancer with pending calls", void 0, {
         
     | 
| 
       530 
     | 
    
         
            -
                    F: __dxlog_file3,
         
     | 
| 
       531 
     | 
    
         
            -
                    L: 80,
         
     | 
| 
       532 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       533 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       534 
     | 
    
         
            -
                  });
         
     | 
| 
       535 
     | 
    
         
            -
                }
         
     | 
| 
       536 
     | 
    
         
            -
                this._sendBuffers.clear();
         
     | 
| 
       537 
     | 
    
         
            -
                this._framer.destroy();
         
     | 
| 
       538 
     | 
    
         
            -
              }
         
     | 
| 
       539 
     | 
    
         
            -
              _processIncomingMessage(msg) {
         
     | 
| 
       540 
     | 
    
         
            -
                const { channelId, dataLength, chunk } = decodeChunk(msg, (channelId2) => !this._receiveBuffers.has(channelId2));
         
     | 
| 
       541 
     | 
    
         
            -
                if (!this._receiveBuffers.has(channelId)) {
         
     | 
| 
       542 
     | 
    
         
            -
                  if (chunk.length < dataLength) {
         
     | 
| 
       543 
     | 
    
         
            -
                    this._receiveBuffers.set(channelId, {
         
     | 
| 
       544 
     | 
    
         
            -
                      buffer: Buffer2.from(chunk),
         
     | 
| 
       545 
     | 
    
         
            -
                      msgLength: dataLength
         
     | 
| 
       546 
     | 
    
         
            -
                    });
         
     | 
| 
       547 
     | 
    
         
            -
                  } else {
         
     | 
| 
       548 
     | 
    
         
            -
                    this.incomingData.emit(chunk);
         
     | 
| 
       549 
     | 
    
         
            -
                  }
         
     | 
| 
       550 
     | 
    
         
            -
                } else {
         
     | 
| 
       551 
     | 
    
         
            -
                  const channelBuffer = this._receiveBuffers.get(channelId);
         
     | 
| 
       552 
     | 
    
         
            -
                  channelBuffer.buffer = Buffer2.concat([
         
     | 
| 
       553 
     | 
    
         
            -
                    channelBuffer.buffer,
         
     | 
| 
       554 
     | 
    
         
            -
                    chunk
         
     | 
| 
       555 
     | 
    
         
            -
                  ]);
         
     | 
| 
       556 
     | 
    
         
            -
                  if (channelBuffer.buffer.length < channelBuffer.msgLength) {
         
     | 
| 
       557 
     | 
    
         
            -
                    return;
         
     | 
| 
       558 
     | 
    
         
            -
                  }
         
     | 
| 
       559 
     | 
    
         
            -
                  const msg2 = channelBuffer.buffer;
         
     | 
| 
       560 
     | 
    
         
            -
                  this._receiveBuffers.delete(channelId);
         
     | 
| 
       561 
     | 
    
         
            -
                  this.incomingData.emit(msg2);
         
     | 
| 
       562 
     | 
    
         
            -
                }
         
     | 
| 
       563 
     | 
    
         
            -
              }
         
     | 
| 
       564 
     | 
    
         
            -
              _getNextCallerId() {
         
     | 
| 
       565 
     | 
    
         
            -
                if (this._sendBuffers.has(this._sysChannelId)) {
         
     | 
| 
       566 
     | 
    
         
            -
                  return this._sysChannelId;
         
     | 
| 
       567 
     | 
    
         
            -
                }
         
     | 
| 
       568 
     | 
    
         
            -
                const index = this._lastCallerIndex;
         
     | 
| 
       569 
     | 
    
         
            -
                this._lastCallerIndex = (this._lastCallerIndex + 1) % this._channels.length;
         
     | 
| 
       570 
     | 
    
         
            -
                return this._channels[index];
         
     | 
| 
       571 
     | 
    
         
            -
              }
         
     | 
| 
       572 
     | 
    
         
            -
              _enqueueChunk(data, trigger, channelId) {
         
     | 
| 
       573 
     | 
    
         
            -
                if (!this._channels.includes(channelId)) {
         
     | 
| 
       574 
     | 
    
         
            -
                  throw new Error(`Unknown channel ${channelId}`);
         
     | 
| 
       575 
     | 
    
         
            -
                }
         
     | 
| 
       576 
     | 
    
         
            -
                if (!this._sendBuffers.has(channelId)) {
         
     | 
| 
       577 
     | 
    
         
            -
                  this._sendBuffers.set(channelId, []);
         
     | 
| 
       578 
     | 
    
         
            -
                }
         
     | 
| 
       579 
     | 
    
         
            -
                const sendBuffer = this._sendBuffers.get(channelId);
         
     | 
| 
       580 
     | 
    
         
            -
                const chunks = [];
         
     | 
| 
       581 
     | 
    
         
            -
                for (let idx = 0; idx < data.length; idx += MAX_CHUNK_SIZE) {
         
     | 
| 
       582 
     | 
    
         
            -
                  chunks.push(data.subarray(idx, idx + MAX_CHUNK_SIZE));
         
     | 
| 
       583 
     | 
    
         
            -
                }
         
     | 
| 
       584 
     | 
    
         
            -
                chunks.forEach((chunk, index) => {
         
     | 
| 
       585 
     | 
    
         
            -
                  const msg = encodeChunk({
         
     | 
| 
       586 
     | 
    
         
            -
                    chunk,
         
     | 
| 
       587 
     | 
    
         
            -
                    channelId,
         
     | 
| 
       588 
     | 
    
         
            -
                    dataLength: index === 0 ? data.length : void 0
         
     | 
| 
       589 
     | 
    
         
            -
                  });
         
     | 
| 
       590 
     | 
    
         
            -
                  sendBuffer.push({
         
     | 
| 
       591 
     | 
    
         
            -
                    msg,
         
     | 
| 
       592 
     | 
    
         
            -
                    trigger: index === chunks.length - 1 ? trigger : void 0
         
     | 
| 
       593 
     | 
    
         
            -
                  });
         
     | 
| 
       594 
     | 
    
         
            -
                });
         
     | 
| 
       595 
     | 
    
         
            -
              }
         
     | 
| 
       596 
     | 
    
         
            -
              // get the next chunk or null if there are no chunks remaining
         
     | 
| 
       597 
     | 
    
         
            -
              _getNextChunk() {
         
     | 
| 
       598 
     | 
    
         
            -
                let chunk;
         
     | 
| 
       599 
     | 
    
         
            -
                while (this._sendBuffers.size > 0) {
         
     | 
| 
       600 
     | 
    
         
            -
                  const channelId = this._getNextCallerId();
         
     | 
| 
       601 
     | 
    
         
            -
                  const sendBuffer = this._sendBuffers.get(channelId);
         
     | 
| 
       602 
     | 
    
         
            -
                  if (!sendBuffer) {
         
     | 
| 
       603 
     | 
    
         
            -
                    continue;
         
     | 
| 
       604 
     | 
    
         
            -
                  }
         
     | 
| 
       605 
     | 
    
         
            -
                  chunk = sendBuffer.shift();
         
     | 
| 
       606 
     | 
    
         
            -
                  if (!chunk) {
         
     | 
| 
       607 
     | 
    
         
            -
                    continue;
         
     | 
| 
       608 
     | 
    
         
            -
                  }
         
     | 
| 
       609 
     | 
    
         
            -
                  if (sendBuffer.length === 0) {
         
     | 
| 
       610 
     | 
    
         
            -
                    this._sendBuffers.delete(channelId);
         
     | 
| 
       611 
     | 
    
         
            -
                  }
         
     | 
| 
       612 
     | 
    
         
            -
                  return chunk;
         
     | 
| 
       613 
     | 
    
         
            -
                }
         
     | 
| 
       614 
     | 
    
         
            -
                return null;
         
     | 
| 
       615 
     | 
    
         
            -
              }
         
     | 
| 
       616 
     | 
    
         
            -
              async _sendChunks() {
         
     | 
| 
       617 
     | 
    
         
            -
                if (this._sending) {
         
     | 
| 
       618 
     | 
    
         
            -
                  return;
         
     | 
| 
       619 
     | 
    
         
            -
                }
         
     | 
| 
       620 
     | 
    
         
            -
                this._sending = true;
         
     | 
| 
       621 
     | 
    
         
            -
                let chunk;
         
     | 
| 
       622 
     | 
    
         
            -
                chunk = this._getNextChunk();
         
     | 
| 
       623 
     | 
    
         
            -
                while (chunk) {
         
     | 
| 
       624 
     | 
    
         
            -
                  if (!this._framer.writable) {
         
     | 
| 
       625 
     | 
    
         
            -
                    log3("PAUSE for drain", void 0, {
         
     | 
| 
       626 
     | 
    
         
            -
                      F: __dxlog_file3,
         
     | 
| 
       627 
     | 
    
         
            -
                      L: 179,
         
     | 
| 
       628 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       629 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       630 
     | 
    
         
            -
                    });
         
     | 
| 
       631 
     | 
    
         
            -
                    await this._framer.drain.waitForCount(1);
         
     | 
| 
       632 
     | 
    
         
            -
                    log3("RESUME for drain", void 0, {
         
     | 
| 
       633 
     | 
    
         
            -
                      F: __dxlog_file3,
         
     | 
| 
       634 
     | 
    
         
            -
                      L: 181,
         
     | 
| 
       635 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       636 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       637 
     | 
    
         
            -
                    });
         
     | 
| 
       638 
     | 
    
         
            -
                  }
         
     | 
| 
       639 
     | 
    
         
            -
                  try {
         
     | 
| 
       640 
     | 
    
         
            -
                    await this._framer.port.send(chunk.msg);
         
     | 
| 
       641 
     | 
    
         
            -
                    chunk.trigger?.wake();
         
     | 
| 
       642 
     | 
    
         
            -
                  } catch (err) {
         
     | 
| 
       643 
     | 
    
         
            -
                    log3("Error sending chunk", {
         
     | 
| 
       644 
     | 
    
         
            -
                      err
         
     | 
| 
       645 
     | 
    
         
            -
                    }, {
         
     | 
| 
       646 
     | 
    
         
            -
                      F: __dxlog_file3,
         
     | 
| 
       647 
     | 
    
         
            -
                      L: 187,
         
     | 
| 
       648 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       649 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       650 
     | 
    
         
            -
                    });
         
     | 
| 
       651 
     | 
    
         
            -
                    chunk.trigger?.throw(err);
         
     | 
| 
       652 
     | 
    
         
            -
                  }
         
     | 
| 
       653 
     | 
    
         
            -
                  chunk = this._getNextChunk();
         
     | 
| 
       654 
     | 
    
         
            -
                }
         
     | 
| 
       655 
     | 
    
         
            -
                invariant2(this._sendBuffers.size === 0, "sendBuffers not empty", {
         
     | 
| 
       656 
     | 
    
         
            -
                  F: __dxlog_file3,
         
     | 
| 
       657 
     | 
    
         
            -
                  L: 192,
         
     | 
| 
       658 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       659 
     | 
    
         
            -
                  A: [
         
     | 
| 
       660 
     | 
    
         
            -
                    "this._sendBuffers.size === 0",
         
     | 
| 
       661 
     | 
    
         
            -
                    "'sendBuffers not empty'"
         
     | 
| 
       662 
     | 
    
         
            -
                  ]
         
     | 
| 
       663 
     | 
    
         
            -
                });
         
     | 
| 
       664 
     | 
    
         
            -
                this._sending = false;
         
     | 
| 
       665 
     | 
    
         
            -
              }
         
     | 
| 
       666 
     | 
    
         
            -
            };
         
     | 
| 
       667 
     | 
    
         
            -
            var encodeChunk = ({ channelId, dataLength, chunk }) => {
         
     | 
| 
       668 
     | 
    
         
            -
              const channelTagLength = import_varint.default.encodingLength(channelId);
         
     | 
| 
       669 
     | 
    
         
            -
              const dataLengthLength = dataLength ? import_varint.default.encodingLength(dataLength) : 0;
         
     | 
| 
       670 
     | 
    
         
            -
              const message = Buffer2.allocUnsafe(channelTagLength + dataLengthLength + chunk.length);
         
     | 
| 
       671 
     | 
    
         
            -
              import_varint.default.encode(channelId, message);
         
     | 
| 
       672 
     | 
    
         
            -
              if (dataLength) {
         
     | 
| 
       673 
     | 
    
         
            -
                import_varint.default.encode(dataLength, message, channelTagLength);
         
     | 
| 
       674 
     | 
    
         
            -
              }
         
     | 
| 
       675 
     | 
    
         
            -
              message.set(chunk, channelTagLength + dataLengthLength);
         
     | 
| 
       676 
     | 
    
         
            -
              return message;
         
     | 
| 
       677 
     | 
    
         
            -
            };
         
     | 
| 
       678 
     | 
    
         
            -
            var decodeChunk = (data, withLength) => {
         
     | 
| 
       679 
     | 
    
         
            -
              const channelId = import_varint.default.decode(data);
         
     | 
| 
       680 
     | 
    
         
            -
              let dataLength;
         
     | 
| 
       681 
     | 
    
         
            -
              let offset = import_varint.default.decode.bytes;
         
     | 
| 
       682 
     | 
    
         
            -
              if (withLength(channelId)) {
         
     | 
| 
       683 
     | 
    
         
            -
                dataLength = import_varint.default.decode(data, offset);
         
     | 
| 
       684 
     | 
    
         
            -
                offset += import_varint.default.decode.bytes;
         
     | 
| 
       685 
     | 
    
         
            -
              }
         
     | 
| 
       686 
     | 
    
         
            -
              const chunk = data.subarray(offset);
         
     | 
| 
       687 
     | 
    
         
            -
              return {
         
     | 
| 
       688 
     | 
    
         
            -
                channelId,
         
     | 
| 
       689 
     | 
    
         
            -
                dataLength,
         
     | 
| 
       690 
     | 
    
         
            -
                chunk
         
     | 
| 
       691 
     | 
    
         
            -
              };
         
     | 
| 
       692 
     | 
    
         
            -
            };
         
     | 
| 
       693 
     | 
    
         
            -
             
     | 
| 
       694 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/muxing/muxer.ts
         
     | 
| 
       695 
     | 
    
         
            -
            function _ts_decorate(decorators, target, key, desc) {
         
     | 
| 
       696 
     | 
    
         
            -
              var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
         
     | 
| 
       697 
     | 
    
         
            -
              if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
         
     | 
| 
       698 
     | 
    
         
            -
              else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
         
     | 
| 
       699 
     | 
    
         
            -
              return c > 3 && r && Object.defineProperty(target, key, r), r;
         
     | 
| 
       700 
     | 
    
         
            -
            }
         
     | 
| 
       701 
     | 
    
         
            -
            var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/muxer.ts";
         
     | 
| 
       702 
     | 
    
         
            -
            var Command = schema2.getCodecForType("dxos.mesh.muxer.Command");
         
     | 
| 
       703 
     | 
    
         
            -
            var DEFAULT_SEND_COMMAND_TIMEOUT = 6e4;
         
     | 
| 
       704 
     | 
    
         
            -
            var DESTROY_COMMAND_SEND_TIMEOUT = 5e3;
         
     | 
| 
       705 
     | 
    
         
            -
            var STATS_INTERVAL = 1e3;
         
     | 
| 
       706 
     | 
    
         
            -
            var MAX_SAFE_FRAME_SIZE = 1e6;
         
     | 
| 
       707 
     | 
    
         
            -
            var SYSTEM_CHANNEL_ID = 0;
         
     | 
| 
       708 
     | 
    
         
            -
            var GRACEFUL_CLOSE_TIMEOUT = 3e3;
         
     | 
| 
       709 
     | 
    
         
            -
            var Muxer = class {
         
     | 
| 
       710 
     | 
    
         
            -
              constructor() {
         
     | 
| 
       711 
     | 
    
         
            -
                this._balancer = new Balancer(SYSTEM_CHANNEL_ID);
         
     | 
| 
       712 
     | 
    
         
            -
                this._channelsByLocalId = /* @__PURE__ */ new Map();
         
     | 
| 
       713 
     | 
    
         
            -
                this._channelsByTag = /* @__PURE__ */ new Map();
         
     | 
| 
       714 
     | 
    
         
            -
                this._ctx = new Context2(void 0, {
         
     | 
| 
       715 
     | 
    
         
            -
                  F: __dxlog_file4,
         
     | 
| 
       716 
     | 
    
         
            -
                  L: 108
         
     | 
| 
       717 
     | 
    
         
            -
                });
         
     | 
| 
       718 
     | 
    
         
            -
                this._nextId = 1;
         
     | 
| 
       719 
     | 
    
         
            -
                this._closing = false;
         
     | 
| 
       720 
     | 
    
         
            -
                this._destroying = false;
         
     | 
| 
       721 
     | 
    
         
            -
                this._disposed = false;
         
     | 
| 
       722 
     | 
    
         
            -
                this._lastStats = void 0;
         
     | 
| 
       723 
     | 
    
         
            -
                this._lastChannelStats = /* @__PURE__ */ new Map();
         
     | 
| 
       724 
     | 
    
         
            -
                this.afterClosed = new Event3();
         
     | 
| 
       725 
     | 
    
         
            -
                this.statsUpdated = new Event3();
         
     | 
| 
       726 
     | 
    
         
            -
                this.stream = this._balancer.stream;
         
     | 
| 
       727 
     | 
    
         
            -
                this._balancer.incomingData.on(async (msg) => {
         
     | 
| 
       728 
     | 
    
         
            -
                  await this._handleCommand(Command.decode(msg));
         
     | 
| 
       729 
     | 
    
         
            -
                });
         
     | 
| 
       730 
     | 
    
         
            -
              }
         
     | 
| 
       731 
     | 
    
         
            -
              setSessionId(sessionId) {
         
     | 
| 
       732 
     | 
    
         
            -
                this._sessionId = sessionId;
         
     | 
| 
       733 
     | 
    
         
            -
              }
         
     | 
| 
       734 
     | 
    
         
            -
              get sessionIdString() {
         
     | 
| 
       735 
     | 
    
         
            -
                return this._sessionId ? this._sessionId.truncate() : "none";
         
     | 
| 
       736 
     | 
    
         
            -
              }
         
     | 
| 
       737 
     | 
    
         
            -
              /**
         
     | 
| 
       738 
     | 
    
         
            -
              * Creates a duplex Node.js-style stream.
         
     | 
| 
       739 
     | 
    
         
            -
              * The remote peer is expected to call `createStream` with the same tag.
         
     | 
| 
       740 
     | 
    
         
            -
              * The stream is immediately readable and writable.
         
     | 
| 
       741 
     | 
    
         
            -
              * NOTE: The data will be buffered until the stream is opened remotely with the same tag (may cause a memory leak).
         
     | 
| 
       742 
     | 
    
         
            -
              */
         
     | 
| 
       743 
     | 
    
         
            -
              async createStream(tag, opts = {}) {
         
     | 
| 
       744 
     | 
    
         
            -
                const channel = this._getOrCreateStream({
         
     | 
| 
       745 
     | 
    
         
            -
                  tag,
         
     | 
| 
       746 
     | 
    
         
            -
                  contentType: opts.contentType
         
     | 
| 
       747 
     | 
    
         
            -
                });
         
     | 
| 
       748 
     | 
    
         
            -
                invariant3(!channel.push, `Channel already open: ${tag}`, {
         
     | 
| 
       749 
     | 
    
         
            -
                  F: __dxlog_file4,
         
     | 
| 
       750 
     | 
    
         
            -
                  L: 152,
         
     | 
| 
       751 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       752 
     | 
    
         
            -
                  A: [
         
     | 
| 
       753 
     | 
    
         
            -
                    "!channel.push",
         
     | 
| 
       754 
     | 
    
         
            -
                    "`Channel already open: ${tag}`"
         
     | 
| 
       755 
     | 
    
         
            -
                  ]
         
     | 
| 
       756 
     | 
    
         
            -
                });
         
     | 
| 
       757 
     | 
    
         
            -
                const stream = new Duplex2({
         
     | 
| 
       758 
     | 
    
         
            -
                  write: (data, encoding, callback) => {
         
     | 
| 
       759 
     | 
    
         
            -
                    this._sendData(channel, data).then(() => callback()).catch(callback);
         
     | 
| 
       760 
     | 
    
         
            -
                  },
         
     | 
| 
       761 
     | 
    
         
            -
                  read: () => {
         
     | 
| 
       762 
     | 
    
         
            -
                  }
         
     | 
| 
       763 
     | 
    
         
            -
                });
         
     | 
| 
       764 
     | 
    
         
            -
                channel.push = (data) => {
         
     | 
| 
       765 
     | 
    
         
            -
                  channel.stats.bytesReceived += data.length;
         
     | 
| 
       766 
     | 
    
         
            -
                  stream.push(data);
         
     | 
| 
       767 
     | 
    
         
            -
                };
         
     | 
| 
       768 
     | 
    
         
            -
                channel.destroy = (err) => {
         
     | 
| 
       769 
     | 
    
         
            -
                  if (err) {
         
     | 
| 
       770 
     | 
    
         
            -
                    if (stream.listeners("error").length > 0) {
         
     | 
| 
       771 
     | 
    
         
            -
                      stream.destroy(err);
         
     | 
| 
       772 
     | 
    
         
            -
                    } else {
         
     | 
| 
       773 
     | 
    
         
            -
                      stream.destroy();
         
     | 
| 
       774 
     | 
    
         
            -
                    }
         
     | 
| 
       775 
     | 
    
         
            -
                  } else {
         
     | 
| 
       776 
     | 
    
         
            -
                    stream.destroy();
         
     | 
| 
       777 
     | 
    
         
            -
                  }
         
     | 
| 
       778 
     | 
    
         
            -
                };
         
     | 
| 
       779 
     | 
    
         
            -
                try {
         
     | 
| 
       780 
     | 
    
         
            -
                  await this._sendCommand({
         
     | 
| 
       781 
     | 
    
         
            -
                    openChannel: {
         
     | 
| 
       782 
     | 
    
         
            -
                      id: channel.id,
         
     | 
| 
       783 
     | 
    
         
            -
                      tag: channel.tag,
         
     | 
| 
       784 
     | 
    
         
            -
                      contentType: channel.contentType
         
     | 
| 
       785 
     | 
    
         
            -
                    }
         
     | 
| 
       786 
     | 
    
         
            -
                  }, SYSTEM_CHANNEL_ID);
         
     | 
| 
       787 
     | 
    
         
            -
                } catch (err) {
         
     | 
| 
       788 
     | 
    
         
            -
                  this._destroyChannel(channel, err);
         
     | 
| 
       789 
     | 
    
         
            -
                  throw err;
         
     | 
| 
       790 
     | 
    
         
            -
                }
         
     | 
| 
       791 
     | 
    
         
            -
                return stream;
         
     | 
| 
       792 
     | 
    
         
            -
              }
         
     | 
| 
       793 
     | 
    
         
            -
              /**
         
     | 
| 
       794 
     | 
    
         
            -
              * Creates an RPC port.
         
     | 
| 
       795 
     | 
    
         
            -
              * The remote peer is expected to call `createPort` with the same tag.
         
     | 
| 
       796 
     | 
    
         
            -
              * The port is immediately usable.
         
     | 
| 
       797 
     | 
    
         
            -
              * NOTE: The data will be buffered until the stream is opened remotely with the same tag (may cause a memory leak).
         
     | 
| 
       798 
     | 
    
         
            -
              */
         
     | 
| 
       799 
     | 
    
         
            -
              async createPort(tag, opts = {}) {
         
     | 
| 
       800 
     | 
    
         
            -
                const channel = this._getOrCreateStream({
         
     | 
| 
       801 
     | 
    
         
            -
                  tag,
         
     | 
| 
       802 
     | 
    
         
            -
                  contentType: opts.contentType
         
     | 
| 
       803 
     | 
    
         
            -
                });
         
     | 
| 
       804 
     | 
    
         
            -
                invariant3(!channel.push, `Channel already open: ${tag}`, {
         
     | 
| 
       805 
     | 
    
         
            -
                  F: __dxlog_file4,
         
     | 
| 
       806 
     | 
    
         
            -
                  L: 212,
         
     | 
| 
       807 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       808 
     | 
    
         
            -
                  A: [
         
     | 
| 
       809 
     | 
    
         
            -
                    "!channel.push",
         
     | 
| 
       810 
     | 
    
         
            -
                    "`Channel already open: ${tag}`"
         
     | 
| 
       811 
     | 
    
         
            -
                  ]
         
     | 
| 
       812 
     | 
    
         
            -
                });
         
     | 
| 
       813 
     | 
    
         
            -
                let inboundBuffer = [];
         
     | 
| 
       814 
     | 
    
         
            -
                let callback;
         
     | 
| 
       815 
     | 
    
         
            -
                channel.push = (data) => {
         
     | 
| 
       816 
     | 
    
         
            -
                  channel.stats.bytesReceived += data.length;
         
     | 
| 
       817 
     | 
    
         
            -
                  if (callback) {
         
     | 
| 
       818 
     | 
    
         
            -
                    callback(data);
         
     | 
| 
       819 
     | 
    
         
            -
                  } else {
         
     | 
| 
       820 
     | 
    
         
            -
                    inboundBuffer.push(data);
         
     | 
| 
       821 
     | 
    
         
            -
                  }
         
     | 
| 
       822 
     | 
    
         
            -
                };
         
     | 
| 
       823 
     | 
    
         
            -
                const port = {
         
     | 
| 
       824 
     | 
    
         
            -
                  send: async (data, timeout) => {
         
     | 
| 
       825 
     | 
    
         
            -
                    await this._sendData(channel, data, timeout);
         
     | 
| 
       826 
     | 
    
         
            -
                  },
         
     | 
| 
       827 
     | 
    
         
            -
                  subscribe: (cb) => {
         
     | 
| 
       828 
     | 
    
         
            -
                    invariant3(!callback, "Only one subscriber is allowed", {
         
     | 
| 
       829 
     | 
    
         
            -
                      F: __dxlog_file4,
         
     | 
| 
       830 
     | 
    
         
            -
                      L: 234,
         
     | 
| 
       831 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       832 
     | 
    
         
            -
                      A: [
         
     | 
| 
       833 
     | 
    
         
            -
                        "!callback",
         
     | 
| 
       834 
     | 
    
         
            -
                        "'Only one subscriber is allowed'"
         
     | 
| 
       835 
     | 
    
         
            -
                      ]
         
     | 
| 
       836 
     | 
    
         
            -
                    });
         
     | 
| 
       837 
     | 
    
         
            -
                    callback = cb;
         
     | 
| 
       838 
     | 
    
         
            -
                    for (const data of inboundBuffer) {
         
     | 
| 
       839 
     | 
    
         
            -
                      cb(data);
         
     | 
| 
       840 
     | 
    
         
            -
                    }
         
     | 
| 
       841 
     | 
    
         
            -
                    inboundBuffer = [];
         
     | 
| 
       842 
     | 
    
         
            -
                  }
         
     | 
| 
       843 
     | 
    
         
            -
                };
         
     | 
| 
       844 
     | 
    
         
            -
                try {
         
     | 
| 
       845 
     | 
    
         
            -
                  await this._sendCommand({
         
     | 
| 
       846 
     | 
    
         
            -
                    openChannel: {
         
     | 
| 
       847 
     | 
    
         
            -
                      id: channel.id,
         
     | 
| 
       848 
     | 
    
         
            -
                      tag: channel.tag,
         
     | 
| 
       849 
     | 
    
         
            -
                      contentType: channel.contentType
         
     | 
| 
       850 
     | 
    
         
            -
                    }
         
     | 
| 
       851 
     | 
    
         
            -
                  }, SYSTEM_CHANNEL_ID);
         
     | 
| 
       852 
     | 
    
         
            -
                } catch (err) {
         
     | 
| 
       853 
     | 
    
         
            -
                  this._destroyChannel(channel, err);
         
     | 
| 
       854 
     | 
    
         
            -
                  throw err;
         
     | 
| 
       855 
     | 
    
         
            -
                }
         
     | 
| 
       856 
     | 
    
         
            -
                return port;
         
     | 
| 
       857 
     | 
    
         
            -
              }
         
     | 
| 
       858 
     | 
    
         
            -
              // initiate graceful close
         
     | 
| 
       859 
     | 
    
         
            -
              async close(err) {
         
     | 
| 
       860 
     | 
    
         
            -
                if (this._destroying) {
         
     | 
| 
       861 
     | 
    
         
            -
                  log4("already destroying, ignoring graceful close request", void 0, {
         
     | 
| 
       862 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       863 
     | 
    
         
            -
                    L: 267,
         
     | 
| 
       864 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       865 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       866 
     | 
    
         
            -
                  });
         
     | 
| 
       867 
     | 
    
         
            -
                  return;
         
     | 
| 
       868 
     | 
    
         
            -
                }
         
     | 
| 
       869 
     | 
    
         
            -
                if (this._closing) {
         
     | 
| 
       870 
     | 
    
         
            -
                  log4("already closing, ignoring graceful close request", void 0, {
         
     | 
| 
       871 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       872 
     | 
    
         
            -
                    L: 271,
         
     | 
| 
       873 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       874 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       875 
     | 
    
         
            -
                  });
         
     | 
| 
       876 
     | 
    
         
            -
                  return;
         
     | 
| 
       877 
     | 
    
         
            -
                }
         
     | 
| 
       878 
     | 
    
         
            -
                this._closing = true;
         
     | 
| 
       879 
     | 
    
         
            -
                await this._sendCommand({
         
     | 
| 
       880 
     | 
    
         
            -
                  close: {
         
     | 
| 
       881 
     | 
    
         
            -
                    error: err?.message
         
     | 
| 
       882 
     | 
    
         
            -
                  }
         
     | 
| 
       883 
     | 
    
         
            -
                }, SYSTEM_CHANNEL_ID, DESTROY_COMMAND_SEND_TIMEOUT).catch(async (err2) => {
         
     | 
| 
       884 
     | 
    
         
            -
                  log4("error sending close command", {
         
     | 
| 
       885 
     | 
    
         
            -
                    err: err2
         
     | 
| 
       886 
     | 
    
         
            -
                  }, {
         
     | 
| 
       887 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       888 
     | 
    
         
            -
                    L: 286,
         
     | 
| 
       889 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       890 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       891 
     | 
    
         
            -
                  });
         
     | 
| 
       892 
     | 
    
         
            -
                  await this._dispose(err2);
         
     | 
| 
       893 
     | 
    
         
            -
                });
         
     | 
| 
       894 
     | 
    
         
            -
                await asyncTimeout2(this._dispose(err), GRACEFUL_CLOSE_TIMEOUT, new TimeoutError("gracefully closing muxer"));
         
     | 
| 
       895 
     | 
    
         
            -
              }
         
     | 
| 
       896 
     | 
    
         
            -
              // force close without confirmation
         
     | 
| 
       897 
     | 
    
         
            -
              async destroy(err) {
         
     | 
| 
       898 
     | 
    
         
            -
                if (this._destroying) {
         
     | 
| 
       899 
     | 
    
         
            -
                  log4("already destroying, ignoring destroy request", void 0, {
         
     | 
| 
       900 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       901 
     | 
    
         
            -
                    L: 299,
         
     | 
| 
       902 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       903 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       904 
     | 
    
         
            -
                  });
         
     | 
| 
       905 
     | 
    
         
            -
                  return;
         
     | 
| 
       906 
     | 
    
         
            -
                }
         
     | 
| 
       907 
     | 
    
         
            -
                this._destroying = true;
         
     | 
| 
       908 
     | 
    
         
            -
                void this._ctx.dispose();
         
     | 
| 
       909 
     | 
    
         
            -
                if (this._closing) {
         
     | 
| 
       910 
     | 
    
         
            -
                  log4("destroy cancelling graceful close", void 0, {
         
     | 
| 
       911 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       912 
     | 
    
         
            -
                    L: 305,
         
     | 
| 
       913 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       914 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       915 
     | 
    
         
            -
                  });
         
     | 
| 
       916 
     | 
    
         
            -
                  this._closing = false;
         
     | 
| 
       917 
     | 
    
         
            -
                } else {
         
     | 
| 
       918 
     | 
    
         
            -
                  await this._sendCommand({
         
     | 
| 
       919 
     | 
    
         
            -
                    close: {
         
     | 
| 
       920 
     | 
    
         
            -
                      error: err?.message
         
     | 
| 
       921 
     | 
    
         
            -
                    }
         
     | 
| 
       922 
     | 
    
         
            -
                  }, SYSTEM_CHANNEL_ID).catch(async (err2) => {
         
     | 
| 
       923 
     | 
    
         
            -
                    log4("error sending courtesy close command", {
         
     | 
| 
       924 
     | 
    
         
            -
                      err: err2
         
     | 
| 
       925 
     | 
    
         
            -
                    }, {
         
     | 
| 
       926 
     | 
    
         
            -
                      F: __dxlog_file4,
         
     | 
| 
       927 
     | 
    
         
            -
                      L: 318,
         
     | 
| 
       928 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       929 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       930 
     | 
    
         
            -
                    });
         
     | 
| 
       931 
     | 
    
         
            -
                  });
         
     | 
| 
       932 
     | 
    
         
            -
                }
         
     | 
| 
       933 
     | 
    
         
            -
                this._dispose(err).catch((err2) => {
         
     | 
| 
       934 
     | 
    
         
            -
                  log4("error disposing after destroy", {
         
     | 
| 
       935 
     | 
    
         
            -
                    err: err2
         
     | 
| 
       936 
     | 
    
         
            -
                  }, {
         
     | 
| 
       937 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       938 
     | 
    
         
            -
                    L: 323,
         
     | 
| 
       939 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       940 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       941 
     | 
    
         
            -
                  });
         
     | 
| 
       942 
     | 
    
         
            -
                });
         
     | 
| 
       943 
     | 
    
         
            -
              }
         
     | 
| 
       944 
     | 
    
         
            -
              // complete the termination, graceful or otherwise
         
     | 
| 
       945 
     | 
    
         
            -
              async _dispose(err) {
         
     | 
| 
       946 
     | 
    
         
            -
                if (this._disposed) {
         
     | 
| 
       947 
     | 
    
         
            -
                  log4("already destroyed, ignoring dispose request", void 0, {
         
     | 
| 
       948 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       949 
     | 
    
         
            -
                    L: 331,
         
     | 
| 
       950 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       951 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       952 
     | 
    
         
            -
                  });
         
     | 
| 
       953 
     | 
    
         
            -
                  return;
         
     | 
| 
       954 
     | 
    
         
            -
                }
         
     | 
| 
       955 
     | 
    
         
            -
                void this._ctx.dispose();
         
     | 
| 
       956 
     | 
    
         
            -
                await this._balancer.destroy();
         
     | 
| 
       957 
     | 
    
         
            -
                for (const channel of this._channelsByTag.values()) {
         
     | 
| 
       958 
     | 
    
         
            -
                  channel.destroy?.(err);
         
     | 
| 
       959 
     | 
    
         
            -
                }
         
     | 
| 
       960 
     | 
    
         
            -
                this._disposed = true;
         
     | 
| 
       961 
     | 
    
         
            -
                await this._emitStats();
         
     | 
| 
       962 
     | 
    
         
            -
                this.afterClosed.emit(err);
         
     | 
| 
       963 
     | 
    
         
            -
                this._channelsByLocalId.clear();
         
     | 
| 
       964 
     | 
    
         
            -
                this._channelsByTag.clear();
         
     | 
| 
       965 
     | 
    
         
            -
              }
         
     | 
| 
       966 
     | 
    
         
            -
              async _handleCommand(cmd) {
         
     | 
| 
       967 
     | 
    
         
            -
                if (this._disposed) {
         
     | 
| 
       968 
     | 
    
         
            -
                  log4.warn("Received command after disposed", {
         
     | 
| 
       969 
     | 
    
         
            -
                    cmd
         
     | 
| 
       970 
     | 
    
         
            -
                  }, {
         
     | 
| 
       971 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       972 
     | 
    
         
            -
                    L: 354,
         
     | 
| 
       973 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       974 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       975 
     | 
    
         
            -
                  });
         
     | 
| 
       976 
     | 
    
         
            -
                  return;
         
     | 
| 
       977 
     | 
    
         
            -
                }
         
     | 
| 
       978 
     | 
    
         
            -
                if (cmd.close) {
         
     | 
| 
       979 
     | 
    
         
            -
                  if (!this._closing) {
         
     | 
| 
       980 
     | 
    
         
            -
                    log4("received peer close, initiating my own graceful close", void 0, {
         
     | 
| 
       981 
     | 
    
         
            -
                      F: __dxlog_file4,
         
     | 
| 
       982 
     | 
    
         
            -
                      L: 360,
         
     | 
| 
       983 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       984 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       985 
     | 
    
         
            -
                    });
         
     | 
| 
       986 
     | 
    
         
            -
                    await this.close(new Error("received peer close"));
         
     | 
| 
       987 
     | 
    
         
            -
                  } else {
         
     | 
| 
       988 
     | 
    
         
            -
                    log4("received close from peer, already closing", void 0, {
         
     | 
| 
       989 
     | 
    
         
            -
                      F: __dxlog_file4,
         
     | 
| 
       990 
     | 
    
         
            -
                      L: 363,
         
     | 
| 
       991 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       992 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       993 
     | 
    
         
            -
                    });
         
     | 
| 
       994 
     | 
    
         
            -
                  }
         
     | 
| 
       995 
     | 
    
         
            -
                  return;
         
     | 
| 
       996 
     | 
    
         
            -
                }
         
     | 
| 
       997 
     | 
    
         
            -
                if (cmd.openChannel) {
         
     | 
| 
       998 
     | 
    
         
            -
                  const channel = this._getOrCreateStream({
         
     | 
| 
       999 
     | 
    
         
            -
                    tag: cmd.openChannel.tag,
         
     | 
| 
       1000 
     | 
    
         
            -
                    contentType: cmd.openChannel.contentType
         
     | 
| 
       1001 
     | 
    
         
            -
                  });
         
     | 
| 
       1002 
     | 
    
         
            -
                  channel.remoteId = cmd.openChannel.id;
         
     | 
| 
       1003 
     | 
    
         
            -
                  for (const data of channel.buffer) {
         
     | 
| 
       1004 
     | 
    
         
            -
                    await this._sendCommand({
         
     | 
| 
       1005 
     | 
    
         
            -
                      data: {
         
     | 
| 
       1006 
     | 
    
         
            -
                        channelId: channel.remoteId,
         
     | 
| 
       1007 
     | 
    
         
            -
                        data
         
     | 
| 
       1008 
     | 
    
         
            -
                      }
         
     | 
| 
       1009 
     | 
    
         
            -
                    }, channel.id);
         
     | 
| 
       1010 
     | 
    
         
            -
                  }
         
     | 
| 
       1011 
     | 
    
         
            -
                  channel.buffer = [];
         
     | 
| 
       1012 
     | 
    
         
            -
                } else if (cmd.data) {
         
     | 
| 
       1013 
     | 
    
         
            -
                  const stream = this._channelsByLocalId.get(cmd.data.channelId) ?? failUndefined();
         
     | 
| 
       1014 
     | 
    
         
            -
                  if (!stream.push) {
         
     | 
| 
       1015 
     | 
    
         
            -
                    log4.warn("Received data for channel before it was opened", {
         
     | 
| 
       1016 
     | 
    
         
            -
                      tag: stream.tag
         
     | 
| 
       1017 
     | 
    
         
            -
                    }, {
         
     | 
| 
       1018 
     | 
    
         
            -
                      F: __dxlog_file4,
         
     | 
| 
       1019 
     | 
    
         
            -
                      L: 392,
         
     | 
| 
       1020 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1021 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1022 
     | 
    
         
            -
                    });
         
     | 
| 
       1023 
     | 
    
         
            -
                    return;
         
     | 
| 
       1024 
     | 
    
         
            -
                  }
         
     | 
| 
       1025 
     | 
    
         
            -
                  stream.push(cmd.data.data);
         
     | 
| 
       1026 
     | 
    
         
            -
                }
         
     | 
| 
       1027 
     | 
    
         
            -
              }
         
     | 
| 
       1028 
     | 
    
         
            -
              async _sendCommand(cmd, channelId = -1, timeout = DEFAULT_SEND_COMMAND_TIMEOUT) {
         
     | 
| 
       1029 
     | 
    
         
            -
                if (this._disposed) {
         
     | 
| 
       1030 
     | 
    
         
            -
                  return;
         
     | 
| 
       1031 
     | 
    
         
            -
                }
         
     | 
| 
       1032 
     | 
    
         
            -
                try {
         
     | 
| 
       1033 
     | 
    
         
            -
                  const trigger = new Trigger();
         
     | 
| 
       1034 
     | 
    
         
            -
                  this._balancer.pushData(Command.encode(cmd), trigger, channelId);
         
     | 
| 
       1035 
     | 
    
         
            -
                  await trigger.wait({
         
     | 
| 
       1036 
     | 
    
         
            -
                    timeout
         
     | 
| 
       1037 
     | 
    
         
            -
                  });
         
     | 
| 
       1038 
     | 
    
         
            -
                } catch (err) {
         
     | 
| 
       1039 
     | 
    
         
            -
                  await this.destroy(err);
         
     | 
| 
       1040 
     | 
    
         
            -
                }
         
     | 
| 
       1041 
     | 
    
         
            -
              }
         
     | 
| 
       1042 
     | 
    
         
            -
              _getOrCreateStream(params) {
         
     | 
| 
       1043 
     | 
    
         
            -
                if (this._channelsByTag.size === 0) {
         
     | 
| 
       1044 
     | 
    
         
            -
                  scheduleTaskInterval2(this._ctx, async () => this._emitStats(), STATS_INTERVAL);
         
     | 
| 
       1045 
     | 
    
         
            -
                }
         
     | 
| 
       1046 
     | 
    
         
            -
                let channel = this._channelsByTag.get(params.tag);
         
     | 
| 
       1047 
     | 
    
         
            -
                if (!channel) {
         
     | 
| 
       1048 
     | 
    
         
            -
                  channel = {
         
     | 
| 
       1049 
     | 
    
         
            -
                    id: this._nextId++,
         
     | 
| 
       1050 
     | 
    
         
            -
                    remoteId: null,
         
     | 
| 
       1051 
     | 
    
         
            -
                    tag: params.tag,
         
     | 
| 
       1052 
     | 
    
         
            -
                    contentType: params.contentType,
         
     | 
| 
       1053 
     | 
    
         
            -
                    buffer: [],
         
     | 
| 
       1054 
     | 
    
         
            -
                    push: null,
         
     | 
| 
       1055 
     | 
    
         
            -
                    destroy: null,
         
     | 
| 
       1056 
     | 
    
         
            -
                    stats: {
         
     | 
| 
       1057 
     | 
    
         
            -
                      bytesSent: 0,
         
     | 
| 
       1058 
     | 
    
         
            -
                      bytesReceived: 0
         
     | 
| 
       1059 
     | 
    
         
            -
                    }
         
     | 
| 
       1060 
     | 
    
         
            -
                  };
         
     | 
| 
       1061 
     | 
    
         
            -
                  this._channelsByTag.set(channel.tag, channel);
         
     | 
| 
       1062 
     | 
    
         
            -
                  this._channelsByLocalId.set(channel.id, channel);
         
     | 
| 
       1063 
     | 
    
         
            -
                  this._balancer.addChannel(channel.id);
         
     | 
| 
       1064 
     | 
    
         
            -
                }
         
     | 
| 
       1065 
     | 
    
         
            -
                return channel;
         
     | 
| 
       1066 
     | 
    
         
            -
              }
         
     | 
| 
       1067 
     | 
    
         
            -
              async _sendData(channel, data, timeout) {
         
     | 
| 
       1068 
     | 
    
         
            -
                if (data.length > MAX_SAFE_FRAME_SIZE) {
         
     | 
| 
       1069 
     | 
    
         
            -
                  log4.warn("frame size exceeds maximum safe value", {
         
     | 
| 
       1070 
     | 
    
         
            -
                    size: data.length,
         
     | 
| 
       1071 
     | 
    
         
            -
                    threshold: MAX_SAFE_FRAME_SIZE
         
     | 
| 
       1072 
     | 
    
         
            -
                  }, {
         
     | 
| 
       1073 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       1074 
     | 
    
         
            -
                    L: 442,
         
     | 
| 
       1075 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       1076 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1077 
     | 
    
         
            -
                  });
         
     | 
| 
       1078 
     | 
    
         
            -
                }
         
     | 
| 
       1079 
     | 
    
         
            -
                channel.stats.bytesSent += data.length;
         
     | 
| 
       1080 
     | 
    
         
            -
                if (channel.remoteId === null) {
         
     | 
| 
       1081 
     | 
    
         
            -
                  channel.buffer.push(data);
         
     | 
| 
       1082 
     | 
    
         
            -
                  return;
         
     | 
| 
       1083 
     | 
    
         
            -
                }
         
     | 
| 
       1084 
     | 
    
         
            -
                await this._sendCommand({
         
     | 
| 
       1085 
     | 
    
         
            -
                  data: {
         
     | 
| 
       1086 
     | 
    
         
            -
                    channelId: channel.remoteId,
         
     | 
| 
       1087 
     | 
    
         
            -
                    data
         
     | 
| 
       1088 
     | 
    
         
            -
                  }
         
     | 
| 
       1089 
     | 
    
         
            -
                }, channel.id, timeout);
         
     | 
| 
       1090 
     | 
    
         
            -
              }
         
     | 
| 
       1091 
     | 
    
         
            -
              _destroyChannel(channel, err) {
         
     | 
| 
       1092 
     | 
    
         
            -
                if (err) {
         
     | 
| 
       1093 
     | 
    
         
            -
                  log4.warn("destroying channel with error", {
         
     | 
| 
       1094 
     | 
    
         
            -
                    err
         
     | 
| 
       1095 
     | 
    
         
            -
                  }, {
         
     | 
| 
       1096 
     | 
    
         
            -
                    F: __dxlog_file4,
         
     | 
| 
       1097 
     | 
    
         
            -
                    L: 465,
         
     | 
| 
       1098 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       1099 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1100 
     | 
    
         
            -
                  });
         
     | 
| 
       1101 
     | 
    
         
            -
                }
         
     | 
| 
       1102 
     | 
    
         
            -
                if (channel.destroy) {
         
     | 
| 
       1103 
     | 
    
         
            -
                  channel.destroy(err);
         
     | 
| 
       1104 
     | 
    
         
            -
                }
         
     | 
| 
       1105 
     | 
    
         
            -
                this._channelsByLocalId.delete(channel.id);
         
     | 
| 
       1106 
     | 
    
         
            -
                this._channelsByTag.delete(channel.tag);
         
     | 
| 
       1107 
     | 
    
         
            -
              }
         
     | 
| 
       1108 
     | 
    
         
            -
              async _emitStats() {
         
     | 
| 
       1109 
     | 
    
         
            -
                if (this._disposed || this._destroying) {
         
     | 
| 
       1110 
     | 
    
         
            -
                  if (!this._lastStats) {
         
     | 
| 
       1111 
     | 
    
         
            -
                    return;
         
     | 
| 
       1112 
     | 
    
         
            -
                  }
         
     | 
| 
       1113 
     | 
    
         
            -
                  const lastStats = this._lastStats;
         
     | 
| 
       1114 
     | 
    
         
            -
                  this._lastStats = void 0;
         
     | 
| 
       1115 
     | 
    
         
            -
                  lastStats.readBufferSize = 0;
         
     | 
| 
       1116 
     | 
    
         
            -
                  lastStats.writeBufferSize = 0;
         
     | 
| 
       1117 
     | 
    
         
            -
                  for (const c of lastStats.channels) {
         
     | 
| 
       1118 
     | 
    
         
            -
                    c.writeBufferSize = 0;
         
     | 
| 
       1119 
     | 
    
         
            -
                  }
         
     | 
| 
       1120 
     | 
    
         
            -
                  this.statsUpdated.emit(lastStats);
         
     | 
| 
       1121 
     | 
    
         
            -
                  this._lastChannelStats.clear();
         
     | 
| 
       1122 
     | 
    
         
            -
                  return;
         
     | 
| 
       1123 
     | 
    
         
            -
                }
         
     | 
| 
       1124 
     | 
    
         
            -
                const bytesSent = this._balancer.bytesSent;
         
     | 
| 
       1125 
     | 
    
         
            -
                const bytesReceived = this._balancer.bytesReceived;
         
     | 
| 
       1126 
     | 
    
         
            -
                const now = Date.now();
         
     | 
| 
       1127 
     | 
    
         
            -
                const interval = this._lastStats ? (now - this._lastStats.timestamp) / 1e3 : 0;
         
     | 
| 
       1128 
     | 
    
         
            -
                const calculateThroughput = (current, last) => last ? {
         
     | 
| 
       1129 
     | 
    
         
            -
                  bytesSentRate: interval ? (current.bytesSent - last.bytesSent) / interval : void 0,
         
     | 
| 
       1130 
     | 
    
         
            -
                  bytesReceivedRate: interval ? (current.bytesReceived - last.bytesReceived) / interval : void 0
         
     | 
| 
       1131 
     | 
    
         
            -
                } : {};
         
     | 
| 
       1132 
     | 
    
         
            -
                this._lastStats = {
         
     | 
| 
       1133 
     | 
    
         
            -
                  timestamp: now,
         
     | 
| 
       1134 
     | 
    
         
            -
                  channels: Array.from(this._channelsByTag.values()).map((channel) => {
         
     | 
| 
       1135 
     | 
    
         
            -
                    const stats = {
         
     | 
| 
       1136 
     | 
    
         
            -
                      id: channel.id,
         
     | 
| 
       1137 
     | 
    
         
            -
                      tag: channel.tag,
         
     | 
| 
       1138 
     | 
    
         
            -
                      contentType: channel.contentType,
         
     | 
| 
       1139 
     | 
    
         
            -
                      writeBufferSize: channel.buffer.length,
         
     | 
| 
       1140 
     | 
    
         
            -
                      bytesSent: channel.stats.bytesSent,
         
     | 
| 
       1141 
     | 
    
         
            -
                      bytesReceived: channel.stats.bytesReceived,
         
     | 
| 
       1142 
     | 
    
         
            -
                      ...calculateThroughput(channel.stats, this._lastChannelStats.get(channel.id))
         
     | 
| 
       1143 
     | 
    
         
            -
                    };
         
     | 
| 
       1144 
     | 
    
         
            -
                    this._lastChannelStats.set(channel.id, stats);
         
     | 
| 
       1145 
     | 
    
         
            -
                    return stats;
         
     | 
| 
       1146 
     | 
    
         
            -
                  }),
         
     | 
| 
       1147 
     | 
    
         
            -
                  bytesSent,
         
     | 
| 
       1148 
     | 
    
         
            -
                  bytesReceived,
         
     | 
| 
       1149 
     | 
    
         
            -
                  ...calculateThroughput({
         
     | 
| 
       1150 
     | 
    
         
            -
                    bytesSent,
         
     | 
| 
       1151 
     | 
    
         
            -
                    bytesReceived
         
     | 
| 
       1152 
     | 
    
         
            -
                  }, this._lastStats),
         
     | 
| 
       1153 
     | 
    
         
            -
                  readBufferSize: this._balancer.stream.readableLength,
         
     | 
| 
       1154 
     | 
    
         
            -
                  writeBufferSize: this._balancer.stream.writableLength
         
     | 
| 
       1155 
     | 
    
         
            -
                };
         
     | 
| 
       1156 
     | 
    
         
            -
                this.statsUpdated.emit(this._lastStats);
         
     | 
| 
       1157 
     | 
    
         
            -
              }
         
     | 
| 
       1158 
     | 
    
         
            -
            };
         
     | 
| 
       1159 
     | 
    
         
            -
            _ts_decorate([
         
     | 
| 
       1160 
     | 
    
         
            -
              logInfo
         
     | 
| 
       1161 
     | 
    
         
            -
            ], Muxer.prototype, "sessionIdString", null);
         
     | 
| 
       1162 
     | 
    
         
            -
             
     | 
| 
       1163 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/muxing/rpc-port.ts
         
     | 
| 
       1164 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       1165 
     | 
    
         
            -
             
     | 
| 
       1166 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/teleport.ts
         
     | 
| 
       1167 
     | 
    
         
            -
            function _ts_decorate2(decorators, target, key, desc) {
         
     | 
| 
       1168 
     | 
    
         
            -
              var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
         
     | 
| 
       1169 
     | 
    
         
            -
              if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
         
     | 
| 
       1170 
     | 
    
         
            -
              else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
         
     | 
| 
       1171 
     | 
    
         
            -
              return c > 3 && r && Object.defineProperty(target, key, r), r;
         
     | 
| 
       1172 
     | 
    
         
            -
            }
         
     | 
| 
       1173 
     | 
    
         
            -
            var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/teleport.ts";
         
     | 
| 
       1174 
     | 
    
         
            -
            var CONTROL_HEARTBEAT_INTERVAL = 1e4;
         
     | 
| 
       1175 
     | 
    
         
            -
            var CONTROL_HEARTBEAT_TIMEOUT = 6e4;
         
     | 
| 
       1176 
     | 
    
         
            -
            var Teleport = class {
         
     | 
| 
       1177 
     | 
    
         
            -
              constructor({ initiator, localPeerId, remotePeerId, ...rest }) {
         
     | 
| 
       1178 
     | 
    
         
            -
                this._ctx = new Context3({
         
     | 
| 
       1179 
     | 
    
         
            -
                  onError: (err) => {
         
     | 
| 
       1180 
     | 
    
         
            -
                    log5.info("error in teleport context", {
         
     | 
| 
       1181 
     | 
    
         
            -
                      err
         
     | 
| 
       1182 
     | 
    
         
            -
                    }, {
         
     | 
| 
       1183 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1184 
     | 
    
         
            -
                      L: 40,
         
     | 
| 
       1185 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1186 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1187 
     | 
    
         
            -
                    });
         
     | 
| 
       1188 
     | 
    
         
            -
                    void this.destroy(err).catch(() => {
         
     | 
| 
       1189 
     | 
    
         
            -
                      log5.error("Error during destroy", err, {
         
     | 
| 
       1190 
     | 
    
         
            -
                        F: __dxlog_file5,
         
     | 
| 
       1191 
     | 
    
         
            -
                        L: 42,
         
     | 
| 
       1192 
     | 
    
         
            -
                        S: this,
         
     | 
| 
       1193 
     | 
    
         
            -
                        C: (f, a) => f(...a)
         
     | 
| 
       1194 
     | 
    
         
            -
                      });
         
     | 
| 
       1195 
     | 
    
         
            -
                    });
         
     | 
| 
       1196 
     | 
    
         
            -
                  }
         
     | 
| 
       1197 
     | 
    
         
            -
                }, {
         
     | 
| 
       1198 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1199 
     | 
    
         
            -
                  L: 38
         
     | 
| 
       1200 
     | 
    
         
            -
                });
         
     | 
| 
       1201 
     | 
    
         
            -
                this._muxer = new Muxer();
         
     | 
| 
       1202 
     | 
    
         
            -
                this._extensions = /* @__PURE__ */ new Map();
         
     | 
| 
       1203 
     | 
    
         
            -
                this._remoteExtensions = /* @__PURE__ */ new Set();
         
     | 
| 
       1204 
     | 
    
         
            -
                this._open = false;
         
     | 
| 
       1205 
     | 
    
         
            -
                this._destroying = false;
         
     | 
| 
       1206 
     | 
    
         
            -
                this._aborting = false;
         
     | 
| 
       1207 
     | 
    
         
            -
                invariant4(typeof initiator === "boolean", void 0, {
         
     | 
| 
       1208 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1209 
     | 
    
         
            -
                  L: 63,
         
     | 
| 
       1210 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1211 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1212 
     | 
    
         
            -
                    "typeof initiator === 'boolean'",
         
     | 
| 
       1213 
     | 
    
         
            -
                    ""
         
     | 
| 
       1214 
     | 
    
         
            -
                  ]
         
     | 
| 
       1215 
     | 
    
         
            -
                });
         
     | 
| 
       1216 
     | 
    
         
            -
                invariant4(PublicKey.isPublicKey(localPeerId), void 0, {
         
     | 
| 
       1217 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1218 
     | 
    
         
            -
                  L: 64,
         
     | 
| 
       1219 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1220 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1221 
     | 
    
         
            -
                    "PublicKey.isPublicKey(localPeerId)",
         
     | 
| 
       1222 
     | 
    
         
            -
                    ""
         
     | 
| 
       1223 
     | 
    
         
            -
                  ]
         
     | 
| 
       1224 
     | 
    
         
            -
                });
         
     | 
| 
       1225 
     | 
    
         
            -
                invariant4(PublicKey.isPublicKey(remotePeerId), void 0, {
         
     | 
| 
       1226 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1227 
     | 
    
         
            -
                  L: 65,
         
     | 
| 
       1228 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1229 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1230 
     | 
    
         
            -
                    "PublicKey.isPublicKey(remotePeerId)",
         
     | 
| 
       1231 
     | 
    
         
            -
                    ""
         
     | 
| 
       1232 
     | 
    
         
            -
                  ]
         
     | 
| 
       1233 
     | 
    
         
            -
                });
         
     | 
| 
       1234 
     | 
    
         
            -
                this.initiator = initiator;
         
     | 
| 
       1235 
     | 
    
         
            -
                this.localPeerId = localPeerId;
         
     | 
| 
       1236 
     | 
    
         
            -
                this.remotePeerId = remotePeerId;
         
     | 
| 
       1237 
     | 
    
         
            -
                this._control = new ControlExtension({
         
     | 
| 
       1238 
     | 
    
         
            -
                  heartbeatInterval: rest.controlHeartbeatInterval ?? CONTROL_HEARTBEAT_INTERVAL,
         
     | 
| 
       1239 
     | 
    
         
            -
                  heartbeatTimeout: rest.controlHeartbeatTimeout ?? CONTROL_HEARTBEAT_TIMEOUT,
         
     | 
| 
       1240 
     | 
    
         
            -
                  onTimeout: () => {
         
     | 
| 
       1241 
     | 
    
         
            -
                    if (this._destroying || this._aborting) {
         
     | 
| 
       1242 
     | 
    
         
            -
                      return;
         
     | 
| 
       1243 
     | 
    
         
            -
                    }
         
     | 
| 
       1244 
     | 
    
         
            -
                    log5.info("abort teleport due to onTimeout in ControlExtension", void 0, {
         
     | 
| 
       1245 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1246 
     | 
    
         
            -
                      L: 78,
         
     | 
| 
       1247 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1248 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1249 
     | 
    
         
            -
                    });
         
     | 
| 
       1250 
     | 
    
         
            -
                    this.abort(new TimeoutError2("control extension")).catch((err) => log5.catch(err, void 0, {
         
     | 
| 
       1251 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1252 
     | 
    
         
            -
                      L: 79,
         
     | 
| 
       1253 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1254 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1255 
     | 
    
         
            -
                    }));
         
     | 
| 
       1256 
     | 
    
         
            -
                  }
         
     | 
| 
       1257 
     | 
    
         
            -
                }, this.localPeerId, this.remotePeerId);
         
     | 
| 
       1258 
     | 
    
         
            -
                this._control.onExtensionRegistered.set(async (name) => {
         
     | 
| 
       1259 
     | 
    
         
            -
                  log5("remote extension", {
         
     | 
| 
       1260 
     | 
    
         
            -
                    name
         
     | 
| 
       1261 
     | 
    
         
            -
                  }, {
         
     | 
| 
       1262 
     | 
    
         
            -
                    F: __dxlog_file5,
         
     | 
| 
       1263 
     | 
    
         
            -
                    L: 87,
         
     | 
| 
       1264 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       1265 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1266 
     | 
    
         
            -
                  });
         
     | 
| 
       1267 
     | 
    
         
            -
                  invariant4(!this._remoteExtensions.has(name), "Remote extension already exists", {
         
     | 
| 
       1268 
     | 
    
         
            -
                    F: __dxlog_file5,
         
     | 
| 
       1269 
     | 
    
         
            -
                    L: 88,
         
     | 
| 
       1270 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       1271 
     | 
    
         
            -
                    A: [
         
     | 
| 
       1272 
     | 
    
         
            -
                      "!this._remoteExtensions.has(name)",
         
     | 
| 
       1273 
     | 
    
         
            -
                      "'Remote extension already exists'"
         
     | 
| 
       1274 
     | 
    
         
            -
                    ]
         
     | 
| 
       1275 
     | 
    
         
            -
                  });
         
     | 
| 
       1276 
     | 
    
         
            -
                  this._remoteExtensions.add(name);
         
     | 
| 
       1277 
     | 
    
         
            -
                  if (this._extensions.has(name)) {
         
     | 
| 
       1278 
     | 
    
         
            -
                    try {
         
     | 
| 
       1279 
     | 
    
         
            -
                      await this._openExtension(name);
         
     | 
| 
       1280 
     | 
    
         
            -
                    } catch (err) {
         
     | 
| 
       1281 
     | 
    
         
            -
                      await this.destroy(err);
         
     | 
| 
       1282 
     | 
    
         
            -
                    }
         
     | 
| 
       1283 
     | 
    
         
            -
                  }
         
     | 
| 
       1284 
     | 
    
         
            -
                });
         
     | 
| 
       1285 
     | 
    
         
            -
                {
         
     | 
| 
       1286 
     | 
    
         
            -
                  this._muxer.stream.on("close", async () => {
         
     | 
| 
       1287 
     | 
    
         
            -
                    if (this._destroying || this._aborting) {
         
     | 
| 
       1288 
     | 
    
         
            -
                      log5("destroy teleport due to muxer stream close, skipping due to already destroying/aborting", void 0, {
         
     | 
| 
       1289 
     | 
    
         
            -
                        F: __dxlog_file5,
         
     | 
| 
       1290 
     | 
    
         
            -
                        L: 104,
         
     | 
| 
       1291 
     | 
    
         
            -
                        S: this,
         
     | 
| 
       1292 
     | 
    
         
            -
                        C: (f, a) => f(...a)
         
     | 
| 
       1293 
     | 
    
         
            -
                      });
         
     | 
| 
       1294 
     | 
    
         
            -
                      return;
         
     | 
| 
       1295 
     | 
    
         
            -
                    }
         
     | 
| 
       1296 
     | 
    
         
            -
                    await this.destroy();
         
     | 
| 
       1297 
     | 
    
         
            -
                  });
         
     | 
| 
       1298 
     | 
    
         
            -
                  this._muxer.stream.on("error", async (err) => {
         
     | 
| 
       1299 
     | 
    
         
            -
                    await this.destroy(err);
         
     | 
| 
       1300 
     | 
    
         
            -
                  });
         
     | 
| 
       1301 
     | 
    
         
            -
                }
         
     | 
| 
       1302 
     | 
    
         
            -
                this._muxer.statsUpdated.on((stats) => {
         
     | 
| 
       1303 
     | 
    
         
            -
                  log5.trace("dxos.mesh.teleport.stats", {
         
     | 
| 
       1304 
     | 
    
         
            -
                    localPeerId,
         
     | 
| 
       1305 
     | 
    
         
            -
                    remotePeerId,
         
     | 
| 
       1306 
     | 
    
         
            -
                    bytesSent: stats.bytesSent,
         
     | 
| 
       1307 
     | 
    
         
            -
                    bytesSentRate: stats.bytesSentRate,
         
     | 
| 
       1308 
     | 
    
         
            -
                    bytesReceived: stats.bytesReceived,
         
     | 
| 
       1309 
     | 
    
         
            -
                    bytesReceivedRate: stats.bytesReceivedRate,
         
     | 
| 
       1310 
     | 
    
         
            -
                    channels: stats.channels
         
     | 
| 
       1311 
     | 
    
         
            -
                  }, {
         
     | 
| 
       1312 
     | 
    
         
            -
                    F: __dxlog_file5,
         
     | 
| 
       1313 
     | 
    
         
            -
                    L: 117,
         
     | 
| 
       1314 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       1315 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1316 
     | 
    
         
            -
                  });
         
     | 
| 
       1317 
     | 
    
         
            -
                });
         
     | 
| 
       1318 
     | 
    
         
            -
              }
         
     | 
| 
       1319 
     | 
    
         
            -
              get isOpen() {
         
     | 
| 
       1320 
     | 
    
         
            -
                return this._open;
         
     | 
| 
       1321 
     | 
    
         
            -
              }
         
     | 
| 
       1322 
     | 
    
         
            -
              get sessionIdString() {
         
     | 
| 
       1323 
     | 
    
         
            -
                return this._sessionId ? this._sessionId.truncate() : "none";
         
     | 
| 
       1324 
     | 
    
         
            -
              }
         
     | 
| 
       1325 
     | 
    
         
            -
              get stream() {
         
     | 
| 
       1326 
     | 
    
         
            -
                return this._muxer.stream;
         
     | 
| 
       1327 
     | 
    
         
            -
              }
         
     | 
| 
       1328 
     | 
    
         
            -
              get stats() {
         
     | 
| 
       1329 
     | 
    
         
            -
                return this._muxer.statsUpdated;
         
     | 
| 
       1330 
     | 
    
         
            -
              }
         
     | 
| 
       1331 
     | 
    
         
            -
              /**
         
     | 
| 
       1332 
     | 
    
         
            -
              * Blocks until the handshake is complete.
         
     | 
| 
       1333 
     | 
    
         
            -
              */
         
     | 
| 
       1334 
     | 
    
         
            -
              async open(sessionId = PublicKey.random()) {
         
     | 
| 
       1335 
     | 
    
         
            -
                this._sessionId = sessionId;
         
     | 
| 
       1336 
     | 
    
         
            -
                log5("open", void 0, {
         
     | 
| 
       1337 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1338 
     | 
    
         
            -
                  L: 151,
         
     | 
| 
       1339 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1340 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1341 
     | 
    
         
            -
                });
         
     | 
| 
       1342 
     | 
    
         
            -
                this._setExtension("dxos.mesh.teleport.control", this._control);
         
     | 
| 
       1343 
     | 
    
         
            -
                await this._openExtension("dxos.mesh.teleport.control");
         
     | 
| 
       1344 
     | 
    
         
            -
                this._open = true;
         
     | 
| 
       1345 
     | 
    
         
            -
                this._muxer.setSessionId(sessionId);
         
     | 
| 
       1346 
     | 
    
         
            -
              }
         
     | 
| 
       1347 
     | 
    
         
            -
              async close(err) {
         
     | 
| 
       1348 
     | 
    
         
            -
                await this.destroy(err);
         
     | 
| 
       1349 
     | 
    
         
            -
              }
         
     | 
| 
       1350 
     | 
    
         
            -
              async abort(err) {
         
     | 
| 
       1351 
     | 
    
         
            -
                if (this._aborting || this._destroying) {
         
     | 
| 
       1352 
     | 
    
         
            -
                  return;
         
     | 
| 
       1353 
     | 
    
         
            -
                }
         
     | 
| 
       1354 
     | 
    
         
            -
                this._aborting = true;
         
     | 
| 
       1355 
     | 
    
         
            -
                this._open = false;
         
     | 
| 
       1356 
     | 
    
         
            -
                if (this._ctx.disposed) {
         
     | 
| 
       1357 
     | 
    
         
            -
                  return;
         
     | 
| 
       1358 
     | 
    
         
            -
                }
         
     | 
| 
       1359 
     | 
    
         
            -
                await this._ctx.dispose();
         
     | 
| 
       1360 
     | 
    
         
            -
                for (const extension of this._extensions.values()) {
         
     | 
| 
       1361 
     | 
    
         
            -
                  try {
         
     | 
| 
       1362 
     | 
    
         
            -
                    await extension.onAbort(err);
         
     | 
| 
       1363 
     | 
    
         
            -
                  } catch (err2) {
         
     | 
| 
       1364 
     | 
    
         
            -
                    log5.catch(err2, void 0, {
         
     | 
| 
       1365 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1366 
     | 
    
         
            -
                      L: 181,
         
     | 
| 
       1367 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1368 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1369 
     | 
    
         
            -
                    });
         
     | 
| 
       1370 
     | 
    
         
            -
                  }
         
     | 
| 
       1371 
     | 
    
         
            -
                }
         
     | 
| 
       1372 
     | 
    
         
            -
                await this._muxer.destroy(err);
         
     | 
| 
       1373 
     | 
    
         
            -
              }
         
     | 
| 
       1374 
     | 
    
         
            -
              async destroy(err) {
         
     | 
| 
       1375 
     | 
    
         
            -
                if (this._destroying || this._aborting) {
         
     | 
| 
       1376 
     | 
    
         
            -
                  return;
         
     | 
| 
       1377 
     | 
    
         
            -
                }
         
     | 
| 
       1378 
     | 
    
         
            -
                log5("destroying teleport...", {
         
     | 
| 
       1379 
     | 
    
         
            -
                  extensionsCount: this._extensions.size
         
     | 
| 
       1380 
     | 
    
         
            -
                }, {
         
     | 
| 
       1381 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1382 
     | 
    
         
            -
                  L: 194,
         
     | 
| 
       1383 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1384 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1385 
     | 
    
         
            -
                });
         
     | 
| 
       1386 
     | 
    
         
            -
                this._destroying = true;
         
     | 
| 
       1387 
     | 
    
         
            -
                this._open = false;
         
     | 
| 
       1388 
     | 
    
         
            -
                if (this._ctx.disposed) {
         
     | 
| 
       1389 
     | 
    
         
            -
                  return;
         
     | 
| 
       1390 
     | 
    
         
            -
                }
         
     | 
| 
       1391 
     | 
    
         
            -
                await this._ctx.dispose();
         
     | 
| 
       1392 
     | 
    
         
            -
                for (const extension of this._extensions.values()) {
         
     | 
| 
       1393 
     | 
    
         
            -
                  try {
         
     | 
| 
       1394 
     | 
    
         
            -
                    log5("destroying extension", {
         
     | 
| 
       1395 
     | 
    
         
            -
                      name: extension.constructor.name
         
     | 
| 
       1396 
     | 
    
         
            -
                    }, {
         
     | 
| 
       1397 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1398 
     | 
    
         
            -
                      L: 206,
         
     | 
| 
       1399 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1400 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1401 
     | 
    
         
            -
                    });
         
     | 
| 
       1402 
     | 
    
         
            -
                    await extension.onClose(err);
         
     | 
| 
       1403 
     | 
    
         
            -
                    log5("destroyed extension", {
         
     | 
| 
       1404 
     | 
    
         
            -
                      name: extension.constructor.name
         
     | 
| 
       1405 
     | 
    
         
            -
                    }, {
         
     | 
| 
       1406 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1407 
     | 
    
         
            -
                      L: 208,
         
     | 
| 
       1408 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1409 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1410 
     | 
    
         
            -
                    });
         
     | 
| 
       1411 
     | 
    
         
            -
                  } catch (err2) {
         
     | 
| 
       1412 
     | 
    
         
            -
                    log5.catch(err2, void 0, {
         
     | 
| 
       1413 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1414 
     | 
    
         
            -
                      L: 210,
         
     | 
| 
       1415 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1416 
     | 
    
         
            -
                      C: (f, a) => f(...a)
         
     | 
| 
       1417 
     | 
    
         
            -
                    });
         
     | 
| 
       1418 
     | 
    
         
            -
                  }
         
     | 
| 
       1419 
     | 
    
         
            -
                }
         
     | 
| 
       1420 
     | 
    
         
            -
                await this._muxer.close();
         
     | 
| 
       1421 
     | 
    
         
            -
                log5("teleport destroyed", void 0, {
         
     | 
| 
       1422 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1423 
     | 
    
         
            -
                  L: 215,
         
     | 
| 
       1424 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1425 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1426 
     | 
    
         
            -
                });
         
     | 
| 
       1427 
     | 
    
         
            -
              }
         
     | 
| 
       1428 
     | 
    
         
            -
              addExtension(name, extension) {
         
     | 
| 
       1429 
     | 
    
         
            -
                if (!this._open) {
         
     | 
| 
       1430 
     | 
    
         
            -
                  throw new Error("Not open");
         
     | 
| 
       1431 
     | 
    
         
            -
                }
         
     | 
| 
       1432 
     | 
    
         
            -
                log5("addExtension", {
         
     | 
| 
       1433 
     | 
    
         
            -
                  name
         
     | 
| 
       1434 
     | 
    
         
            -
                }, {
         
     | 
| 
       1435 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1436 
     | 
    
         
            -
                  L: 223,
         
     | 
| 
       1437 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1438 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1439 
     | 
    
         
            -
                });
         
     | 
| 
       1440 
     | 
    
         
            -
                this._setExtension(name, extension);
         
     | 
| 
       1441 
     | 
    
         
            -
                scheduleTask(this._ctx, async () => {
         
     | 
| 
       1442 
     | 
    
         
            -
                  try {
         
     | 
| 
       1443 
     | 
    
         
            -
                    await this._control.registerExtension(name);
         
     | 
| 
       1444 
     | 
    
         
            -
                  } catch (err) {
         
     | 
| 
       1445 
     | 
    
         
            -
                    if (err instanceof RpcClosedError2) {
         
     | 
| 
       1446 
     | 
    
         
            -
                      return;
         
     | 
| 
       1447 
     | 
    
         
            -
                    }
         
     | 
| 
       1448 
     | 
    
         
            -
                    throw err;
         
     | 
| 
       1449 
     | 
    
         
            -
                  }
         
     | 
| 
       1450 
     | 
    
         
            -
                });
         
     | 
| 
       1451 
     | 
    
         
            -
                if (this._remoteExtensions.has(name)) {
         
     | 
| 
       1452 
     | 
    
         
            -
                  scheduleTask(this._ctx, async () => {
         
     | 
| 
       1453 
     | 
    
         
            -
                    await this._openExtension(name);
         
     | 
| 
       1454 
     | 
    
         
            -
                  });
         
     | 
| 
       1455 
     | 
    
         
            -
                }
         
     | 
| 
       1456 
     | 
    
         
            -
              }
         
     | 
| 
       1457 
     | 
    
         
            -
              _setExtension(extensionName, extension) {
         
     | 
| 
       1458 
     | 
    
         
            -
                invariant4(!extensionName.includes("/"), "Invalid extension name", {
         
     | 
| 
       1459 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1460 
     | 
    
         
            -
                  L: 247,
         
     | 
| 
       1461 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1462 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1463 
     | 
    
         
            -
                    "!extensionName.includes('/')",
         
     | 
| 
       1464 
     | 
    
         
            -
                    "'Invalid extension name'"
         
     | 
| 
       1465 
     | 
    
         
            -
                  ]
         
     | 
| 
       1466 
     | 
    
         
            -
                });
         
     | 
| 
       1467 
     | 
    
         
            -
                invariant4(!this._extensions.has(extensionName), "Extension already exists", {
         
     | 
| 
       1468 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1469 
     | 
    
         
            -
                  L: 248,
         
     | 
| 
       1470 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1471 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1472 
     | 
    
         
            -
                    "!this._extensions.has(extensionName)",
         
     | 
| 
       1473 
     | 
    
         
            -
                    "'Extension already exists'"
         
     | 
| 
       1474 
     | 
    
         
            -
                  ]
         
     | 
| 
       1475 
     | 
    
         
            -
                });
         
     | 
| 
       1476 
     | 
    
         
            -
                this._extensions.set(extensionName, extension);
         
     | 
| 
       1477 
     | 
    
         
            -
              }
         
     | 
| 
       1478 
     | 
    
         
            -
              async _openExtension(extensionName) {
         
     | 
| 
       1479 
     | 
    
         
            -
                log5("open extension", {
         
     | 
| 
       1480 
     | 
    
         
            -
                  extensionName
         
     | 
| 
       1481 
     | 
    
         
            -
                }, {
         
     | 
| 
       1482 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1483 
     | 
    
         
            -
                  L: 253,
         
     | 
| 
       1484 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1485 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1486 
     | 
    
         
            -
                });
         
     | 
| 
       1487 
     | 
    
         
            -
                const extension = this._extensions.get(extensionName) ?? failUndefined2();
         
     | 
| 
       1488 
     | 
    
         
            -
                const context = {
         
     | 
| 
       1489 
     | 
    
         
            -
                  initiator: this.initiator,
         
     | 
| 
       1490 
     | 
    
         
            -
                  localPeerId: this.localPeerId,
         
     | 
| 
       1491 
     | 
    
         
            -
                  remotePeerId: this.remotePeerId,
         
     | 
| 
       1492 
     | 
    
         
            -
                  createPort: async (channelName, opts) => {
         
     | 
| 
       1493 
     | 
    
         
            -
                    invariant4(!channelName.includes("/"), "Invalid channel name", {
         
     | 
| 
       1494 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1495 
     | 
    
         
            -
                      L: 261,
         
     | 
| 
       1496 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1497 
     | 
    
         
            -
                      A: [
         
     | 
| 
       1498 
     | 
    
         
            -
                        "!channelName.includes('/')",
         
     | 
| 
       1499 
     | 
    
         
            -
                        "'Invalid channel name'"
         
     | 
| 
       1500 
     | 
    
         
            -
                      ]
         
     | 
| 
       1501 
     | 
    
         
            -
                    });
         
     | 
| 
       1502 
     | 
    
         
            -
                    return this._muxer.createPort(`${extensionName}/${channelName}`, opts);
         
     | 
| 
       1503 
     | 
    
         
            -
                  },
         
     | 
| 
       1504 
     | 
    
         
            -
                  createStream: async (channelName, opts) => {
         
     | 
| 
       1505 
     | 
    
         
            -
                    invariant4(!channelName.includes("/"), "Invalid channel name", {
         
     | 
| 
       1506 
     | 
    
         
            -
                      F: __dxlog_file5,
         
     | 
| 
       1507 
     | 
    
         
            -
                      L: 265,
         
     | 
| 
       1508 
     | 
    
         
            -
                      S: this,
         
     | 
| 
       1509 
     | 
    
         
            -
                      A: [
         
     | 
| 
       1510 
     | 
    
         
            -
                        "!channelName.includes('/')",
         
     | 
| 
       1511 
     | 
    
         
            -
                        "'Invalid channel name'"
         
     | 
| 
       1512 
     | 
    
         
            -
                      ]
         
     | 
| 
       1513 
     | 
    
         
            -
                    });
         
     | 
| 
       1514 
     | 
    
         
            -
                    return this._muxer.createStream(`${extensionName}/${channelName}`, opts);
         
     | 
| 
       1515 
     | 
    
         
            -
                  },
         
     | 
| 
       1516 
     | 
    
         
            -
                  close: (err) => {
         
     | 
| 
       1517 
     | 
    
         
            -
                    void runInContextAsync(this._ctx, async () => {
         
     | 
| 
       1518 
     | 
    
         
            -
                      await this.close(err);
         
     | 
| 
       1519 
     | 
    
         
            -
                    });
         
     | 
| 
       1520 
     | 
    
         
            -
                  }
         
     | 
| 
       1521 
     | 
    
         
            -
                };
         
     | 
| 
       1522 
     | 
    
         
            -
                await extension.onOpen(context);
         
     | 
| 
       1523 
     | 
    
         
            -
                log5("extension opened", {
         
     | 
| 
       1524 
     | 
    
         
            -
                  extensionName
         
     | 
| 
       1525 
     | 
    
         
            -
                }, {
         
     | 
| 
       1526 
     | 
    
         
            -
                  F: __dxlog_file5,
         
     | 
| 
       1527 
     | 
    
         
            -
                  L: 276,
         
     | 
| 
       1528 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1529 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1530 
     | 
    
         
            -
                });
         
     | 
| 
       1531 
     | 
    
         
            -
              }
         
     | 
| 
       1532 
     | 
    
         
            -
            };
         
     | 
| 
       1533 
     | 
    
         
            -
            _ts_decorate2([
         
     | 
| 
       1534 
     | 
    
         
            -
              logInfo2
         
     | 
| 
       1535 
     | 
    
         
            -
            ], Teleport.prototype, "sessionIdString", null);
         
     | 
| 
       1536 
     | 
    
         
            -
            _ts_decorate2([
         
     | 
| 
       1537 
     | 
    
         
            -
              synchronized
         
     | 
| 
       1538 
     | 
    
         
            -
            ], Teleport.prototype, "abort", null);
         
     | 
| 
       1539 
     | 
    
         
            -
            _ts_decorate2([
         
     | 
| 
       1540 
     | 
    
         
            -
              synchronized
         
     | 
| 
       1541 
     | 
    
         
            -
            ], Teleport.prototype, "destroy", null);
         
     | 
| 
       1542 
     | 
    
         
            -
             
     | 
| 
       1543 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/testing/test-builder.ts
         
     | 
| 
       1544 
     | 
    
         
            -
            var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-builder.ts";
         
     | 
| 
       1545 
     | 
    
         
            -
            var TestBuilder = class {
         
     | 
| 
       1546 
     | 
    
         
            -
              constructor() {
         
     | 
| 
       1547 
     | 
    
         
            -
                this._peers = /* @__PURE__ */ new Set();
         
     | 
| 
       1548 
     | 
    
         
            -
              }
         
     | 
| 
       1549 
     | 
    
         
            -
              createPeer(opts) {
         
     | 
| 
       1550 
     | 
    
         
            -
                const peer = opts.factory();
         
     | 
| 
       1551 
     | 
    
         
            -
                this._peers.add(peer);
         
     | 
| 
       1552 
     | 
    
         
            -
                return peer;
         
     | 
| 
       1553 
     | 
    
         
            -
              }
         
     | 
| 
       1554 
     | 
    
         
            -
              *createPeers(opts) {
         
     | 
| 
       1555 
     | 
    
         
            -
                while (true) {
         
     | 
| 
       1556 
     | 
    
         
            -
                  yield this.createPeer(opts);
         
     | 
| 
       1557 
     | 
    
         
            -
                }
         
     | 
| 
       1558 
     | 
    
         
            -
              }
         
     | 
| 
       1559 
     | 
    
         
            -
              async destroy() {
         
     | 
| 
       1560 
     | 
    
         
            -
                await Promise.all(Array.from(this._peers).map((agent) => agent.destroy()));
         
     | 
| 
       1561 
     | 
    
         
            -
              }
         
     | 
| 
       1562 
     | 
    
         
            -
              async connect(peer1, peer2) {
         
     | 
| 
       1563 
     | 
    
         
            -
                invariant5(peer1 !== peer2, void 0, {
         
     | 
| 
       1564 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1565 
     | 
    
         
            -
                  L: 38,
         
     | 
| 
       1566 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1567 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1568 
     | 
    
         
            -
                    "peer1 !== peer2",
         
     | 
| 
       1569 
     | 
    
         
            -
                    ""
         
     | 
| 
       1570 
     | 
    
         
            -
                  ]
         
     | 
| 
       1571 
     | 
    
         
            -
                });
         
     | 
| 
       1572 
     | 
    
         
            -
                invariant5(this._peers.has(peer1), void 0, {
         
     | 
| 
       1573 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1574 
     | 
    
         
            -
                  L: 39,
         
     | 
| 
       1575 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1576 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1577 
     | 
    
         
            -
                    "this._peers.has(peer1)",
         
     | 
| 
       1578 
     | 
    
         
            -
                    ""
         
     | 
| 
       1579 
     | 
    
         
            -
                  ]
         
     | 
| 
       1580 
     | 
    
         
            -
                });
         
     | 
| 
       1581 
     | 
    
         
            -
                invariant5(this._peers.has(peer1), void 0, {
         
     | 
| 
       1582 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1583 
     | 
    
         
            -
                  L: 40,
         
     | 
| 
       1584 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1585 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1586 
     | 
    
         
            -
                    "this._peers.has(peer1)",
         
     | 
| 
       1587 
     | 
    
         
            -
                    ""
         
     | 
| 
       1588 
     | 
    
         
            -
                  ]
         
     | 
| 
       1589 
     | 
    
         
            -
                });
         
     | 
| 
       1590 
     | 
    
         
            -
                const connection1 = peer1.createConnection({
         
     | 
| 
       1591 
     | 
    
         
            -
                  initiator: true,
         
     | 
| 
       1592 
     | 
    
         
            -
                  remotePeerId: peer2.peerId
         
     | 
| 
       1593 
     | 
    
         
            -
                });
         
     | 
| 
       1594 
     | 
    
         
            -
                const connection2 = peer2.createConnection({
         
     | 
| 
       1595 
     | 
    
         
            -
                  initiator: false,
         
     | 
| 
       1596 
     | 
    
         
            -
                  remotePeerId: peer1.peerId
         
     | 
| 
       1597 
     | 
    
         
            -
                });
         
     | 
| 
       1598 
     | 
    
         
            -
                pipeStreams(connection1.teleport.stream, connection2.teleport.stream);
         
     | 
| 
       1599 
     | 
    
         
            -
                await Promise.all([
         
     | 
| 
       1600 
     | 
    
         
            -
                  peer1.openConnection(connection1),
         
     | 
| 
       1601 
     | 
    
         
            -
                  peer2.openConnection(connection2)
         
     | 
| 
       1602 
     | 
    
         
            -
                ]);
         
     | 
| 
       1603 
     | 
    
         
            -
                return [
         
     | 
| 
       1604 
     | 
    
         
            -
                  connection1,
         
     | 
| 
       1605 
     | 
    
         
            -
                  connection2
         
     | 
| 
       1606 
     | 
    
         
            -
                ];
         
     | 
| 
       1607 
     | 
    
         
            -
              }
         
     | 
| 
       1608 
     | 
    
         
            -
              async disconnect(peer1, peer2) {
         
     | 
| 
       1609 
     | 
    
         
            -
                invariant5(peer1 !== peer2, void 0, {
         
     | 
| 
       1610 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1611 
     | 
    
         
            -
                  L: 52,
         
     | 
| 
       1612 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1613 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1614 
     | 
    
         
            -
                    "peer1 !== peer2",
         
     | 
| 
       1615 
     | 
    
         
            -
                    ""
         
     | 
| 
       1616 
     | 
    
         
            -
                  ]
         
     | 
| 
       1617 
     | 
    
         
            -
                });
         
     | 
| 
       1618 
     | 
    
         
            -
                invariant5(this._peers.has(peer1), void 0, {
         
     | 
| 
       1619 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1620 
     | 
    
         
            -
                  L: 53,
         
     | 
| 
       1621 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1622 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1623 
     | 
    
         
            -
                    "this._peers.has(peer1)",
         
     | 
| 
       1624 
     | 
    
         
            -
                    ""
         
     | 
| 
       1625 
     | 
    
         
            -
                  ]
         
     | 
| 
       1626 
     | 
    
         
            -
                });
         
     | 
| 
       1627 
     | 
    
         
            -
                invariant5(this._peers.has(peer1), void 0, {
         
     | 
| 
       1628 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1629 
     | 
    
         
            -
                  L: 54,
         
     | 
| 
       1630 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1631 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1632 
     | 
    
         
            -
                    "this._peers.has(peer1)",
         
     | 
| 
       1633 
     | 
    
         
            -
                    ""
         
     | 
| 
       1634 
     | 
    
         
            -
                  ]
         
     | 
| 
       1635 
     | 
    
         
            -
                });
         
     | 
| 
       1636 
     | 
    
         
            -
                const connection1 = Array.from(peer1.connections).find((connection) => connection.remotePeerId.equals(peer2.peerId));
         
     | 
| 
       1637 
     | 
    
         
            -
                const connection2 = Array.from(peer2.connections).find((connection) => connection.remotePeerId.equals(peer1.peerId));
         
     | 
| 
       1638 
     | 
    
         
            -
                invariant5(connection1, void 0, {
         
     | 
| 
       1639 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1640 
     | 
    
         
            -
                  L: 63,
         
     | 
| 
       1641 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1642 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1643 
     | 
    
         
            -
                    "connection1",
         
     | 
| 
       1644 
     | 
    
         
            -
                    ""
         
     | 
| 
       1645 
     | 
    
         
            -
                  ]
         
     | 
| 
       1646 
     | 
    
         
            -
                });
         
     | 
| 
       1647 
     | 
    
         
            -
                invariant5(connection2, void 0, {
         
     | 
| 
       1648 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1649 
     | 
    
         
            -
                  L: 64,
         
     | 
| 
       1650 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1651 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1652 
     | 
    
         
            -
                    "connection2",
         
     | 
| 
       1653 
     | 
    
         
            -
                    ""
         
     | 
| 
       1654 
     | 
    
         
            -
                  ]
         
     | 
| 
       1655 
     | 
    
         
            -
                });
         
     | 
| 
       1656 
     | 
    
         
            -
                await Promise.all([
         
     | 
| 
       1657 
     | 
    
         
            -
                  peer1.closeConnection(connection1),
         
     | 
| 
       1658 
     | 
    
         
            -
                  peer2.closeConnection(connection2)
         
     | 
| 
       1659 
     | 
    
         
            -
                ]);
         
     | 
| 
       1660 
     | 
    
         
            -
              }
         
     | 
| 
       1661 
     | 
    
         
            -
            };
         
     | 
| 
       1662 
     | 
    
         
            -
            var TestPeer = class {
         
     | 
| 
       1663 
     | 
    
         
            -
              constructor(peerId = PublicKey2.random()) {
         
     | 
| 
       1664 
     | 
    
         
            -
                this.peerId = peerId;
         
     | 
| 
       1665 
     | 
    
         
            -
                this.connections = /* @__PURE__ */ new Set();
         
     | 
| 
       1666 
     | 
    
         
            -
              }
         
     | 
| 
       1667 
     | 
    
         
            -
              async onOpen(connection) {
         
     | 
| 
       1668 
     | 
    
         
            -
              }
         
     | 
| 
       1669 
     | 
    
         
            -
              async onClose(connection) {
         
     | 
| 
       1670 
     | 
    
         
            -
              }
         
     | 
| 
       1671 
     | 
    
         
            -
              createConnection({ initiator, remotePeerId }) {
         
     | 
| 
       1672 
     | 
    
         
            -
                const connection = new TestConnection(this.peerId, remotePeerId, initiator);
         
     | 
| 
       1673 
     | 
    
         
            -
                this.connections.add(connection);
         
     | 
| 
       1674 
     | 
    
         
            -
                return connection;
         
     | 
| 
       1675 
     | 
    
         
            -
              }
         
     | 
| 
       1676 
     | 
    
         
            -
              async openConnection(connection) {
         
     | 
| 
       1677 
     | 
    
         
            -
                invariant5(this.connections.has(connection), void 0, {
         
     | 
| 
       1678 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1679 
     | 
    
         
            -
                  L: 85,
         
     | 
| 
       1680 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1681 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1682 
     | 
    
         
            -
                    "this.connections.has(connection)",
         
     | 
| 
       1683 
     | 
    
         
            -
                    ""
         
     | 
| 
       1684 
     | 
    
         
            -
                  ]
         
     | 
| 
       1685 
     | 
    
         
            -
                });
         
     | 
| 
       1686 
     | 
    
         
            -
                await connection.teleport.open(PublicKey2.random());
         
     | 
| 
       1687 
     | 
    
         
            -
                await this.onOpen(connection);
         
     | 
| 
       1688 
     | 
    
         
            -
              }
         
     | 
| 
       1689 
     | 
    
         
            -
              async closeConnection(connection) {
         
     | 
| 
       1690 
     | 
    
         
            -
                invariant5(this.connections.has(connection), void 0, {
         
     | 
| 
       1691 
     | 
    
         
            -
                  F: __dxlog_file6,
         
     | 
| 
       1692 
     | 
    
         
            -
                  L: 91,
         
     | 
| 
       1693 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1694 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1695 
     | 
    
         
            -
                    "this.connections.has(connection)",
         
     | 
| 
       1696 
     | 
    
         
            -
                    ""
         
     | 
| 
       1697 
     | 
    
         
            -
                  ]
         
     | 
| 
       1698 
     | 
    
         
            -
                });
         
     | 
| 
       1699 
     | 
    
         
            -
                await this.onClose(connection);
         
     | 
| 
       1700 
     | 
    
         
            -
                await connection.teleport.close();
         
     | 
| 
       1701 
     | 
    
         
            -
                this.connections.delete(connection);
         
     | 
| 
       1702 
     | 
    
         
            -
              }
         
     | 
| 
       1703 
     | 
    
         
            -
              async destroy() {
         
     | 
| 
       1704 
     | 
    
         
            -
                for (const teleport of this.connections) {
         
     | 
| 
       1705 
     | 
    
         
            -
                  await this.closeConnection(teleport);
         
     | 
| 
       1706 
     | 
    
         
            -
                }
         
     | 
| 
       1707 
     | 
    
         
            -
              }
         
     | 
| 
       1708 
     | 
    
         
            -
            };
         
     | 
| 
       1709 
     | 
    
         
            -
            var pipeStreams = (stream1, stream2) => {
         
     | 
| 
       1710 
     | 
    
         
            -
              pipeline(stream1, stream2, (err) => {
         
     | 
| 
       1711 
     | 
    
         
            -
                if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
         
     | 
| 
       1712 
     | 
    
         
            -
                  log6.catch(err, void 0, {
         
     | 
| 
       1713 
     | 
    
         
            -
                    F: __dxlog_file6,
         
     | 
| 
       1714 
     | 
    
         
            -
                    L: 107,
         
     | 
| 
       1715 
     | 
    
         
            -
                    S: void 0,
         
     | 
| 
       1716 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1717 
     | 
    
         
            -
                  });
         
     | 
| 
       1718 
     | 
    
         
            -
                }
         
     | 
| 
       1719 
     | 
    
         
            -
              });
         
     | 
| 
       1720 
     | 
    
         
            -
              pipeline(stream2, stream1, (err) => {
         
     | 
| 
       1721 
     | 
    
         
            -
                if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
         
     | 
| 
       1722 
     | 
    
         
            -
                  log6.catch(err, void 0, {
         
     | 
| 
       1723 
     | 
    
         
            -
                    F: __dxlog_file6,
         
     | 
| 
       1724 
     | 
    
         
            -
                    L: 112,
         
     | 
| 
       1725 
     | 
    
         
            -
                    S: void 0,
         
     | 
| 
       1726 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1727 
     | 
    
         
            -
                  });
         
     | 
| 
       1728 
     | 
    
         
            -
                }
         
     | 
| 
       1729 
     | 
    
         
            -
              });
         
     | 
| 
       1730 
     | 
    
         
            -
            };
         
     | 
| 
       1731 
     | 
    
         
            -
            var TestConnection = class {
         
     | 
| 
       1732 
     | 
    
         
            -
              constructor(localPeerId, remotePeerId, initiator) {
         
     | 
| 
       1733 
     | 
    
         
            -
                this.localPeerId = localPeerId;
         
     | 
| 
       1734 
     | 
    
         
            -
                this.remotePeerId = remotePeerId;
         
     | 
| 
       1735 
     | 
    
         
            -
                this.initiator = initiator;
         
     | 
| 
       1736 
     | 
    
         
            -
                this.teleport = new Teleport({
         
     | 
| 
       1737 
     | 
    
         
            -
                  initiator,
         
     | 
| 
       1738 
     | 
    
         
            -
                  localPeerId,
         
     | 
| 
       1739 
     | 
    
         
            -
                  remotePeerId
         
     | 
| 
       1740 
     | 
    
         
            -
                });
         
     | 
| 
       1741 
     | 
    
         
            -
              }
         
     | 
| 
       1742 
     | 
    
         
            -
              whenOpen(open) {
         
     | 
| 
       1743 
     | 
    
         
            -
                return waitForCondition({
         
     | 
| 
       1744 
     | 
    
         
            -
                  condition: () => this.teleport.isOpen === open
         
     | 
| 
       1745 
     | 
    
         
            -
                });
         
     | 
| 
       1746 
     | 
    
         
            -
              }
         
     | 
| 
       1747 
     | 
    
         
            -
            };
         
     | 
| 
       1748 
     | 
    
         
            -
             
     | 
| 
       1749 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/testing/test-extension.ts
         
     | 
| 
       1750 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       1751 
     | 
    
         
            -
            import { asyncTimeout as asyncTimeout3, Trigger as Trigger2 } from "@dxos/async";
         
     | 
| 
       1752 
     | 
    
         
            -
            import { invariant as invariant6 } from "@dxos/invariant";
         
     | 
| 
       1753 
     | 
    
         
            -
            import { log as log7 } from "@dxos/log";
         
     | 
| 
       1754 
     | 
    
         
            -
            import { schema as schema3 } from "@dxos/protocols/proto";
         
     | 
| 
       1755 
     | 
    
         
            -
            import { createProtoRpcPeer as createProtoRpcPeer2 } from "@dxos/rpc";
         
     | 
| 
       1756 
     | 
    
         
            -
            var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
         
     | 
| 
       1757 
     | 
    
         
            -
            var TestExtension = class {
         
     | 
| 
       1758 
     | 
    
         
            -
              constructor(callbacks = {}) {
         
     | 
| 
       1759 
     | 
    
         
            -
                this.callbacks = callbacks;
         
     | 
| 
       1760 
     | 
    
         
            -
                this.open = new Trigger2();
         
     | 
| 
       1761 
     | 
    
         
            -
                this.closed = new Trigger2();
         
     | 
| 
       1762 
     | 
    
         
            -
                this.aborted = new Trigger2();
         
     | 
| 
       1763 
     | 
    
         
            -
              }
         
     | 
| 
       1764 
     | 
    
         
            -
              get remotePeerId() {
         
     | 
| 
       1765 
     | 
    
         
            -
                return this.extensionContext?.remotePeerId;
         
     | 
| 
       1766 
     | 
    
         
            -
              }
         
     | 
| 
       1767 
     | 
    
         
            -
              async onOpen(context) {
         
     | 
| 
       1768 
     | 
    
         
            -
                log7("onOpen", {
         
     | 
| 
       1769 
     | 
    
         
            -
                  localPeerId: context.localPeerId,
         
     | 
| 
       1770 
     | 
    
         
            -
                  remotePeerId: context.remotePeerId
         
     | 
| 
       1771 
     | 
    
         
            -
                }, {
         
     | 
| 
       1772 
     | 
    
         
            -
                  F: __dxlog_file7,
         
     | 
| 
       1773 
     | 
    
         
            -
                  L: 34,
         
     | 
| 
       1774 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1775 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1776 
     | 
    
         
            -
                });
         
     | 
| 
       1777 
     | 
    
         
            -
                this.extensionContext = context;
         
     | 
| 
       1778 
     | 
    
         
            -
                this._rpc = createProtoRpcPeer2({
         
     | 
| 
       1779 
     | 
    
         
            -
                  port: await context.createPort("rpc", {
         
     | 
| 
       1780 
     | 
    
         
            -
                    contentType: 'application/x-protobuf; messageType="dxos.rpc.Message"'
         
     | 
| 
       1781 
     | 
    
         
            -
                  }),
         
     | 
| 
       1782 
     | 
    
         
            -
                  requested: {
         
     | 
| 
       1783 
     | 
    
         
            -
                    TestService: schema3.getService("example.testing.rpc.TestService")
         
     | 
| 
       1784 
     | 
    
         
            -
                  },
         
     | 
| 
       1785 
     | 
    
         
            -
                  exposed: {
         
     | 
| 
       1786 
     | 
    
         
            -
                    TestService: schema3.getService("example.testing.rpc.TestService")
         
     | 
| 
       1787 
     | 
    
         
            -
                  },
         
     | 
| 
       1788 
     | 
    
         
            -
                  handlers: {
         
     | 
| 
       1789 
     | 
    
         
            -
                    TestService: {
         
     | 
| 
       1790 
     | 
    
         
            -
                      voidCall: async (request) => {
         
     | 
| 
       1791 
     | 
    
         
            -
                      },
         
     | 
| 
       1792 
     | 
    
         
            -
                      testCall: async (request) => {
         
     | 
| 
       1793 
     | 
    
         
            -
                        return {
         
     | 
| 
       1794 
     | 
    
         
            -
                          data: request.data
         
     | 
| 
       1795 
     | 
    
         
            -
                        };
         
     | 
| 
       1796 
     | 
    
         
            -
                      }
         
     | 
| 
       1797 
     | 
    
         
            -
                    }
         
     | 
| 
       1798 
     | 
    
         
            -
                  },
         
     | 
| 
       1799 
     | 
    
         
            -
                  timeout: 2e3
         
     | 
| 
       1800 
     | 
    
         
            -
                });
         
     | 
| 
       1801 
     | 
    
         
            -
                await this._rpc.open();
         
     | 
| 
       1802 
     | 
    
         
            -
                await this.callbacks.onOpen?.();
         
     | 
| 
       1803 
     | 
    
         
            -
                this.open.wake();
         
     | 
| 
       1804 
     | 
    
         
            -
              }
         
     | 
| 
       1805 
     | 
    
         
            -
              async onClose(err) {
         
     | 
| 
       1806 
     | 
    
         
            -
                log7("onClose", {
         
     | 
| 
       1807 
     | 
    
         
            -
                  err
         
     | 
| 
       1808 
     | 
    
         
            -
                }, {
         
     | 
| 
       1809 
     | 
    
         
            -
                  F: __dxlog_file7,
         
     | 
| 
       1810 
     | 
    
         
            -
                  L: 68,
         
     | 
| 
       1811 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1812 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1813 
     | 
    
         
            -
                });
         
     | 
| 
       1814 
     | 
    
         
            -
                await this.callbacks.onClose?.();
         
     | 
| 
       1815 
     | 
    
         
            -
                this.closed.wake();
         
     | 
| 
       1816 
     | 
    
         
            -
                await this._rpc?.close();
         
     | 
| 
       1817 
     | 
    
         
            -
              }
         
     | 
| 
       1818 
     | 
    
         
            -
              async onAbort(err) {
         
     | 
| 
       1819 
     | 
    
         
            -
                log7("onAbort", {
         
     | 
| 
       1820 
     | 
    
         
            -
                  err
         
     | 
| 
       1821 
     | 
    
         
            -
                }, {
         
     | 
| 
       1822 
     | 
    
         
            -
                  F: __dxlog_file7,
         
     | 
| 
       1823 
     | 
    
         
            -
                  L: 75,
         
     | 
| 
       1824 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1825 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1826 
     | 
    
         
            -
                });
         
     | 
| 
       1827 
     | 
    
         
            -
                await this.callbacks.onAbort?.();
         
     | 
| 
       1828 
     | 
    
         
            -
                this.aborted.wake();
         
     | 
| 
       1829 
     | 
    
         
            -
                await this._rpc?.abort();
         
     | 
| 
       1830 
     | 
    
         
            -
              }
         
     | 
| 
       1831 
     | 
    
         
            -
              async test(message = "test") {
         
     | 
| 
       1832 
     | 
    
         
            -
                await this.open.wait({
         
     | 
| 
       1833 
     | 
    
         
            -
                  timeout: 2e3
         
     | 
| 
       1834 
     | 
    
         
            -
                });
         
     | 
| 
       1835 
     | 
    
         
            -
                const res = await asyncTimeout3(this._rpc.rpc.TestService.testCall({
         
     | 
| 
       1836 
     | 
    
         
            -
                  data: message
         
     | 
| 
       1837 
     | 
    
         
            -
                }), 1500);
         
     | 
| 
       1838 
     | 
    
         
            -
                invariant6(res.data === message, void 0, {
         
     | 
| 
       1839 
     | 
    
         
            -
                  F: __dxlog_file7,
         
     | 
| 
       1840 
     | 
    
         
            -
                  L: 84,
         
     | 
| 
       1841 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1842 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1843 
     | 
    
         
            -
                    "res.data === message",
         
     | 
| 
       1844 
     | 
    
         
            -
                    ""
         
     | 
| 
       1845 
     | 
    
         
            -
                  ]
         
     | 
| 
       1846 
     | 
    
         
            -
                });
         
     | 
| 
       1847 
     | 
    
         
            -
              }
         
     | 
| 
       1848 
     | 
    
         
            -
              /**
         
     | 
| 
       1849 
     | 
    
         
            -
              * Force-close the connection.
         
     | 
| 
       1850 
     | 
    
         
            -
              */
         
     | 
| 
       1851 
     | 
    
         
            -
              async closeConnection(err) {
         
     | 
| 
       1852 
     | 
    
         
            -
                this.extensionContext?.close(err);
         
     | 
| 
       1853 
     | 
    
         
            -
              }
         
     | 
| 
       1854 
     | 
    
         
            -
            };
         
     | 
| 
       1855 
     | 
    
         
            -
             
     | 
| 
       1856 
     | 
    
         
            -
            // packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts
         
     | 
| 
       1857 
     | 
    
         
            -
            init_inject_globals();
         
     | 
| 
       1858 
     | 
    
         
            -
            import { randomBytes } from "@dxos/node-std/crypto";
         
     | 
| 
       1859 
     | 
    
         
            -
            import { Trigger as Trigger3 } from "@dxos/async";
         
     | 
| 
       1860 
     | 
    
         
            -
            import { invariant as invariant7 } from "@dxos/invariant";
         
     | 
| 
       1861 
     | 
    
         
            -
            import { log as log8 } from "@dxos/log";
         
     | 
| 
       1862 
     | 
    
         
            -
            import { schema as schema4 } from "@dxos/protocols/proto";
         
     | 
| 
       1863 
     | 
    
         
            -
            import { createProtoRpcPeer as createProtoRpcPeer3 } from "@dxos/rpc";
         
     | 
| 
       1864 
     | 
    
         
            -
            var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts";
         
     | 
| 
       1865 
     | 
    
         
            -
            var TestExtensionWithStreams = class {
         
     | 
| 
       1866 
     | 
    
         
            -
              constructor(callbacks = {}) {
         
     | 
| 
       1867 
     | 
    
         
            -
                this.callbacks = callbacks;
         
     | 
| 
       1868 
     | 
    
         
            -
                this.open = new Trigger3();
         
     | 
| 
       1869 
     | 
    
         
            -
                this.closed = new Trigger3();
         
     | 
| 
       1870 
     | 
    
         
            -
                this.aborted = new Trigger3();
         
     | 
| 
       1871 
     | 
    
         
            -
                this._streams = /* @__PURE__ */ new Map();
         
     | 
| 
       1872 
     | 
    
         
            -
              }
         
     | 
| 
       1873 
     | 
    
         
            -
              get remotePeerId() {
         
     | 
| 
       1874 
     | 
    
         
            -
                return this.extensionContext?.remotePeerId;
         
     | 
| 
       1875 
     | 
    
         
            -
              }
         
     | 
| 
       1876 
     | 
    
         
            -
              async _openStream(streamTag, interval = 5, chunkSize = 2048) {
         
     | 
| 
       1877 
     | 
    
         
            -
                invariant7(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`, {
         
     | 
| 
       1878 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       1879 
     | 
    
         
            -
                  L: 39,
         
     | 
| 
       1880 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1881 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1882 
     | 
    
         
            -
                    "!this._streams.has(streamTag)",
         
     | 
| 
       1883 
     | 
    
         
            -
                    "`Stream already exists: ${streamTag}`"
         
     | 
| 
       1884 
     | 
    
         
            -
                  ]
         
     | 
| 
       1885 
     | 
    
         
            -
                });
         
     | 
| 
       1886 
     | 
    
         
            -
                const networkStream = await this.extensionContext.createStream(streamTag, {
         
     | 
| 
       1887 
     | 
    
         
            -
                  contentType: "application/x-test-stream"
         
     | 
| 
       1888 
     | 
    
         
            -
                });
         
     | 
| 
       1889 
     | 
    
         
            -
                const streamEntry = {
         
     | 
| 
       1890 
     | 
    
         
            -
                  networkStream,
         
     | 
| 
       1891 
     | 
    
         
            -
                  bytesSent: 0,
         
     | 
| 
       1892 
     | 
    
         
            -
                  bytesReceived: 0,
         
     | 
| 
       1893 
     | 
    
         
            -
                  sendErrors: 0,
         
     | 
| 
       1894 
     | 
    
         
            -
                  receiveErrors: 0,
         
     | 
| 
       1895 
     | 
    
         
            -
                  startTimestamp: Date.now()
         
     | 
| 
       1896 
     | 
    
         
            -
                };
         
     | 
| 
       1897 
     | 
    
         
            -
                const pushChunk = () => {
         
     | 
| 
       1898 
     | 
    
         
            -
                  streamEntry.timer = setTimeout(() => {
         
     | 
| 
       1899 
     | 
    
         
            -
                    const chunk = randomBytes(chunkSize);
         
     | 
| 
       1900 
     | 
    
         
            -
                    if (!networkStream.write(chunk, "binary", (err) => {
         
     | 
| 
       1901 
     | 
    
         
            -
                      if (!err) {
         
     | 
| 
       1902 
     | 
    
         
            -
                        streamEntry.bytesSent += chunk.length;
         
     | 
| 
       1903 
     | 
    
         
            -
                      } else {
         
     | 
| 
       1904 
     | 
    
         
            -
                        streamEntry.sendErrors += 1;
         
     | 
| 
       1905 
     | 
    
         
            -
                      }
         
     | 
| 
       1906 
     | 
    
         
            -
                    })) {
         
     | 
| 
       1907 
     | 
    
         
            -
                      networkStream.once("drain", pushChunk);
         
     | 
| 
       1908 
     | 
    
         
            -
                    } else {
         
     | 
| 
       1909 
     | 
    
         
            -
                      process.nextTick(pushChunk);
         
     | 
| 
       1910 
     | 
    
         
            -
                    }
         
     | 
| 
       1911 
     | 
    
         
            -
                  }, interval);
         
     | 
| 
       1912 
     | 
    
         
            -
                };
         
     | 
| 
       1913 
     | 
    
         
            -
                pushChunk();
         
     | 
| 
       1914 
     | 
    
         
            -
                this._streams.set(streamTag, streamEntry);
         
     | 
| 
       1915 
     | 
    
         
            -
                networkStream.on("data", (data) => {
         
     | 
| 
       1916 
     | 
    
         
            -
                  streamEntry.bytesReceived += data.length;
         
     | 
| 
       1917 
     | 
    
         
            -
                });
         
     | 
| 
       1918 
     | 
    
         
            -
                networkStream.on("error", (err) => {
         
     | 
| 
       1919 
     | 
    
         
            -
                  streamEntry.receiveErrors += 1;
         
     | 
| 
       1920 
     | 
    
         
            -
                });
         
     | 
| 
       1921 
     | 
    
         
            -
                networkStream.on("close", () => {
         
     | 
| 
       1922 
     | 
    
         
            -
                  networkStream.removeAllListeners();
         
     | 
| 
       1923 
     | 
    
         
            -
                });
         
     | 
| 
       1924 
     | 
    
         
            -
                streamEntry.reportingTimer = setInterval(() => {
         
     | 
| 
       1925 
     | 
    
         
            -
                  const { bytesSent, bytesReceived, sendErrors, receiveErrors } = streamEntry;
         
     | 
| 
       1926 
     | 
    
         
            -
                  log8.trace("dxos.test.stream-stats", {
         
     | 
| 
       1927 
     | 
    
         
            -
                    streamTag,
         
     | 
| 
       1928 
     | 
    
         
            -
                    bytesSent,
         
     | 
| 
       1929 
     | 
    
         
            -
                    bytesReceived,
         
     | 
| 
       1930 
     | 
    
         
            -
                    sendErrors,
         
     | 
| 
       1931 
     | 
    
         
            -
                    receiveErrors,
         
     | 
| 
       1932 
     | 
    
         
            -
                    from: this.extensionContext?.localPeerId,
         
     | 
| 
       1933 
     | 
    
         
            -
                    to: this.extensionContext?.remotePeerId
         
     | 
| 
       1934 
     | 
    
         
            -
                  }, {
         
     | 
| 
       1935 
     | 
    
         
            -
                    F: __dxlog_file8,
         
     | 
| 
       1936 
     | 
    
         
            -
                    L: 93,
         
     | 
| 
       1937 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       1938 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       1939 
     | 
    
         
            -
                  });
         
     | 
| 
       1940 
     | 
    
         
            -
                }, 100);
         
     | 
| 
       1941 
     | 
    
         
            -
              }
         
     | 
| 
       1942 
     | 
    
         
            -
              _closeStream(streamTag) {
         
     | 
| 
       1943 
     | 
    
         
            -
                invariant7(this._streams.has(streamTag), `Stream does not exist: ${streamTag}`, {
         
     | 
| 
       1944 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       1945 
     | 
    
         
            -
                  L: 106,
         
     | 
| 
       1946 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1947 
     | 
    
         
            -
                  A: [
         
     | 
| 
       1948 
     | 
    
         
            -
                    "this._streams.has(streamTag)",
         
     | 
| 
       1949 
     | 
    
         
            -
                    "`Stream does not exist: ${streamTag}`"
         
     | 
| 
       1950 
     | 
    
         
            -
                  ]
         
     | 
| 
       1951 
     | 
    
         
            -
                });
         
     | 
| 
       1952 
     | 
    
         
            -
                const stream = this._streams.get(streamTag);
         
     | 
| 
       1953 
     | 
    
         
            -
                clearTimeout(stream.timer);
         
     | 
| 
       1954 
     | 
    
         
            -
                clearTimeout(stream.reportingTimer);
         
     | 
| 
       1955 
     | 
    
         
            -
                const { bytesSent, bytesReceived, sendErrors, receiveErrors, startTimestamp } = stream;
         
     | 
| 
       1956 
     | 
    
         
            -
                stream.networkStream.destroy();
         
     | 
| 
       1957 
     | 
    
         
            -
                this._streams.delete(streamTag);
         
     | 
| 
       1958 
     | 
    
         
            -
                return {
         
     | 
| 
       1959 
     | 
    
         
            -
                  bytesSent,
         
     | 
| 
       1960 
     | 
    
         
            -
                  bytesReceived,
         
     | 
| 
       1961 
     | 
    
         
            -
                  sendErrors,
         
     | 
| 
       1962 
     | 
    
         
            -
                  receiveErrors,
         
     | 
| 
       1963 
     | 
    
         
            -
                  runningTime: Date.now() - (startTimestamp ?? 0)
         
     | 
| 
       1964 
     | 
    
         
            -
                };
         
     | 
| 
       1965 
     | 
    
         
            -
              }
         
     | 
| 
       1966 
     | 
    
         
            -
              async onOpen(context) {
         
     | 
| 
       1967 
     | 
    
         
            -
                log8("onOpen", {
         
     | 
| 
       1968 
     | 
    
         
            -
                  localPeerId: context.localPeerId,
         
     | 
| 
       1969 
     | 
    
         
            -
                  remotePeerId: context.remotePeerId
         
     | 
| 
       1970 
     | 
    
         
            -
                }, {
         
     | 
| 
       1971 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       1972 
     | 
    
         
            -
                  L: 128,
         
     | 
| 
       1973 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       1974 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       1975 
     | 
    
         
            -
                });
         
     | 
| 
       1976 
     | 
    
         
            -
                this.extensionContext = context;
         
     | 
| 
       1977 
     | 
    
         
            -
                this._rpc = createProtoRpcPeer3({
         
     | 
| 
       1978 
     | 
    
         
            -
                  port: await context.createPort("rpc", {
         
     | 
| 
       1979 
     | 
    
         
            -
                    contentType: 'application/x-protobuf; messageType="dxos.rpc.Message"'
         
     | 
| 
       1980 
     | 
    
         
            -
                  }),
         
     | 
| 
       1981 
     | 
    
         
            -
                  requested: {
         
     | 
| 
       1982 
     | 
    
         
            -
                    TestServiceWithStreams: schema4.getService("example.testing.rpc.TestServiceWithStreams")
         
     | 
| 
       1983 
     | 
    
         
            -
                  },
         
     | 
| 
       1984 
     | 
    
         
            -
                  exposed: {
         
     | 
| 
       1985 
     | 
    
         
            -
                    TestServiceWithStreams: schema4.getService("example.testing.rpc.TestServiceWithStreams")
         
     | 
| 
       1986 
     | 
    
         
            -
                  },
         
     | 
| 
       1987 
     | 
    
         
            -
                  handlers: {
         
     | 
| 
       1988 
     | 
    
         
            -
                    TestServiceWithStreams: {
         
     | 
| 
       1989 
     | 
    
         
            -
                      requestTestStream: async (request) => {
         
     | 
| 
       1990 
     | 
    
         
            -
                        const { data: streamTag, streamLoadInterval, streamLoadChunkSize } = request;
         
     | 
| 
       1991 
     | 
    
         
            -
                        await this._openStream(streamTag, streamLoadInterval, streamLoadChunkSize);
         
     | 
| 
       1992 
     | 
    
         
            -
                        return {
         
     | 
| 
       1993 
     | 
    
         
            -
                          data: streamTag
         
     | 
| 
       1994 
     | 
    
         
            -
                        };
         
     | 
| 
       1995 
     | 
    
         
            -
                      },
         
     | 
| 
       1996 
     | 
    
         
            -
                      closeTestStream: async (request) => {
         
     | 
| 
       1997 
     | 
    
         
            -
                        const streamTag = request.data;
         
     | 
| 
       1998 
     | 
    
         
            -
                        const { bytesSent, bytesReceived, sendErrors, receiveErrors, runningTime } = this._closeStream(streamTag);
         
     | 
| 
       1999 
     | 
    
         
            -
                        return {
         
     | 
| 
       2000 
     | 
    
         
            -
                          data: streamTag,
         
     | 
| 
       2001 
     | 
    
         
            -
                          bytesSent,
         
     | 
| 
       2002 
     | 
    
         
            -
                          bytesReceived,
         
     | 
| 
       2003 
     | 
    
         
            -
                          sendErrors,
         
     | 
| 
       2004 
     | 
    
         
            -
                          receiveErrors,
         
     | 
| 
       2005 
     | 
    
         
            -
                          runningTime
         
     | 
| 
       2006 
     | 
    
         
            -
                        };
         
     | 
| 
       2007 
     | 
    
         
            -
                      }
         
     | 
| 
       2008 
     | 
    
         
            -
                    }
         
     | 
| 
       2009 
     | 
    
         
            -
                  },
         
     | 
| 
       2010 
     | 
    
         
            -
                  timeout: 2e3
         
     | 
| 
       2011 
     | 
    
         
            -
                });
         
     | 
| 
       2012 
     | 
    
         
            -
                await this._rpc.open();
         
     | 
| 
       2013 
     | 
    
         
            -
                await this.callbacks.onOpen?.();
         
     | 
| 
       2014 
     | 
    
         
            -
                this.open.wake();
         
     | 
| 
       2015 
     | 
    
         
            -
              }
         
     | 
| 
       2016 
     | 
    
         
            -
              async onClose(err) {
         
     | 
| 
       2017 
     | 
    
         
            -
                log8("onClose", {
         
     | 
| 
       2018 
     | 
    
         
            -
                  err
         
     | 
| 
       2019 
     | 
    
         
            -
                }, {
         
     | 
| 
       2020 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       2021 
     | 
    
         
            -
                  L: 179,
         
     | 
| 
       2022 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       2023 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       2024 
     | 
    
         
            -
                });
         
     | 
| 
       2025 
     | 
    
         
            -
                await this.callbacks.onClose?.();
         
     | 
| 
       2026 
     | 
    
         
            -
                this.closed.wake();
         
     | 
| 
       2027 
     | 
    
         
            -
                for (const [streamTag, stream] of Object.entries(this._streams)) {
         
     | 
| 
       2028 
     | 
    
         
            -
                  log8("closing stream", {
         
     | 
| 
       2029 
     | 
    
         
            -
                    streamTag
         
     | 
| 
       2030 
     | 
    
         
            -
                  }, {
         
     | 
| 
       2031 
     | 
    
         
            -
                    F: __dxlog_file8,
         
     | 
| 
       2032 
     | 
    
         
            -
                    L: 183,
         
     | 
| 
       2033 
     | 
    
         
            -
                    S: this,
         
     | 
| 
       2034 
     | 
    
         
            -
                    C: (f, a) => f(...a)
         
     | 
| 
       2035 
     | 
    
         
            -
                  });
         
     | 
| 
       2036 
     | 
    
         
            -
                  clearTimeout(stream.interval);
         
     | 
| 
       2037 
     | 
    
         
            -
                  stream.networkStream.destroy();
         
     | 
| 
       2038 
     | 
    
         
            -
                }
         
     | 
| 
       2039 
     | 
    
         
            -
                await this._rpc?.close();
         
     | 
| 
       2040 
     | 
    
         
            -
              }
         
     | 
| 
       2041 
     | 
    
         
            -
              async onAbort(err) {
         
     | 
| 
       2042 
     | 
    
         
            -
                log8("onAbort", {
         
     | 
| 
       2043 
     | 
    
         
            -
                  err
         
     | 
| 
       2044 
     | 
    
         
            -
                }, {
         
     | 
| 
       2045 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       2046 
     | 
    
         
            -
                  L: 191,
         
     | 
| 
       2047 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       2048 
     | 
    
         
            -
                  C: (f, a) => f(...a)
         
     | 
| 
       2049 
     | 
    
         
            -
                });
         
     | 
| 
       2050 
     | 
    
         
            -
                await this.callbacks.onAbort?.();
         
     | 
| 
       2051 
     | 
    
         
            -
                this.aborted.wake();
         
     | 
| 
       2052 
     | 
    
         
            -
                await this._rpc?.abort();
         
     | 
| 
       2053 
     | 
    
         
            -
              }
         
     | 
| 
       2054 
     | 
    
         
            -
              async addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag) {
         
     | 
| 
       2055 
     | 
    
         
            -
                await this.open.wait({
         
     | 
| 
       2056 
     | 
    
         
            -
                  timeout: 1500
         
     | 
| 
       2057 
     | 
    
         
            -
                });
         
     | 
| 
       2058 
     | 
    
         
            -
                if (!streamTag) {
         
     | 
| 
       2059 
     | 
    
         
            -
                  streamTag = `stream-${randomBytes(4).toString("hex")}`;
         
     | 
| 
       2060 
     | 
    
         
            -
                }
         
     | 
| 
       2061 
     | 
    
         
            -
                const { data } = await this._rpc.rpc.TestServiceWithStreams.requestTestStream({
         
     | 
| 
       2062 
     | 
    
         
            -
                  data: streamTag,
         
     | 
| 
       2063 
     | 
    
         
            -
                  streamLoadInterval,
         
     | 
| 
       2064 
     | 
    
         
            -
                  streamLoadChunkSize
         
     | 
| 
       2065 
     | 
    
         
            -
                });
         
     | 
| 
       2066 
     | 
    
         
            -
                invariant7(data === streamTag, void 0, {
         
     | 
| 
       2067 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       2068 
     | 
    
         
            -
                  L: 207,
         
     | 
| 
       2069 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       2070 
     | 
    
         
            -
                  A: [
         
     | 
| 
       2071 
     | 
    
         
            -
                    "data === streamTag",
         
     | 
| 
       2072 
     | 
    
         
            -
                    ""
         
     | 
| 
       2073 
     | 
    
         
            -
                  ]
         
     | 
| 
       2074 
     | 
    
         
            -
                });
         
     | 
| 
       2075 
     | 
    
         
            -
                await this._openStream(streamTag, streamLoadInterval, streamLoadChunkSize);
         
     | 
| 
       2076 
     | 
    
         
            -
                return streamTag;
         
     | 
| 
       2077 
     | 
    
         
            -
              }
         
     | 
| 
       2078 
     | 
    
         
            -
              async closeStream(streamTag) {
         
     | 
| 
       2079 
     | 
    
         
            -
                await this.open.wait({
         
     | 
| 
       2080 
     | 
    
         
            -
                  timeout: 1500
         
     | 
| 
       2081 
     | 
    
         
            -
                });
         
     | 
| 
       2082 
     | 
    
         
            -
                const { data, bytesSent, bytesReceived, sendErrors, receiveErrors, runningTime } = await this._rpc.rpc.TestServiceWithStreams.closeTestStream({
         
     | 
| 
       2083 
     | 
    
         
            -
                  data: streamTag
         
     | 
| 
       2084 
     | 
    
         
            -
                });
         
     | 
| 
       2085 
     | 
    
         
            -
                invariant7(data === streamTag, void 0, {
         
     | 
| 
       2086 
     | 
    
         
            -
                  F: __dxlog_file8,
         
     | 
| 
       2087 
     | 
    
         
            -
                  L: 220,
         
     | 
| 
       2088 
     | 
    
         
            -
                  S: this,
         
     | 
| 
       2089 
     | 
    
         
            -
                  A: [
         
     | 
| 
       2090 
     | 
    
         
            -
                    "data === streamTag",
         
     | 
| 
       2091 
     | 
    
         
            -
                    ""
         
     | 
| 
       2092 
     | 
    
         
            -
                  ]
         
     | 
| 
       2093 
     | 
    
         
            -
                });
         
     | 
| 
       2094 
     | 
    
         
            -
                const local = this._closeStream(streamTag);
         
     | 
| 
       2095 
     | 
    
         
            -
                return {
         
     | 
| 
       2096 
     | 
    
         
            -
                  streamTag,
         
     | 
| 
       2097 
     | 
    
         
            -
                  stats: {
         
     | 
| 
       2098 
     | 
    
         
            -
                    local,
         
     | 
| 
       2099 
     | 
    
         
            -
                    remote: {
         
     | 
| 
       2100 
     | 
    
         
            -
                      bytesSent,
         
     | 
| 
       2101 
     | 
    
         
            -
                      bytesReceived,
         
     | 
| 
       2102 
     | 
    
         
            -
                      sendErrors,
         
     | 
| 
       2103 
     | 
    
         
            -
                      receiveErrors,
         
     | 
| 
       2104 
     | 
    
         
            -
                      runningTime
         
     | 
| 
       2105 
     | 
    
         
            -
                    }
         
     | 
| 
       2106 
     | 
    
         
            -
                  }
         
     | 
| 
       2107 
     | 
    
         
            -
                };
         
     | 
| 
       2108 
     | 
    
         
            -
              }
         
     | 
| 
       2109 
     | 
    
         
            -
              /**
         
     | 
| 
       2110 
     | 
    
         
            -
              * Force-close the connection.
         
     | 
| 
       2111 
     | 
    
         
            -
              */
         
     | 
| 
       2112 
     | 
    
         
            -
              async closeConnection(err) {
         
     | 
| 
       2113 
     | 
    
         
            -
                this.extensionContext?.close(err);
         
     | 
| 
       2114 
     | 
    
         
            -
              }
         
     | 
| 
       2115 
     | 
    
         
            -
            };
         
     | 
| 
       2116 
     | 
    
         
            -
             
     | 
| 
       2117 
     | 
    
         
            -
            export {
         
     | 
| 
       2118 
     | 
    
         
            -
              init_inject_globals,
         
     | 
| 
       2119 
     | 
    
         
            -
              Framer,
         
     | 
| 
       2120 
     | 
    
         
            -
              decodeFrame,
         
     | 
| 
       2121 
     | 
    
         
            -
              encodeFrame,
         
     | 
| 
       2122 
     | 
    
         
            -
              Muxer,
         
     | 
| 
       2123 
     | 
    
         
            -
              Teleport,
         
     | 
| 
       2124 
     | 
    
         
            -
              TestBuilder,
         
     | 
| 
       2125 
     | 
    
         
            -
              TestPeer,
         
     | 
| 
       2126 
     | 
    
         
            -
              TestConnection,
         
     | 
| 
       2127 
     | 
    
         
            -
              TestExtension,
         
     | 
| 
       2128 
     | 
    
         
            -
              TestExtensionWithStreams
         
     | 
| 
       2129 
     | 
    
         
            -
            };
         
     | 
| 
       2130 
     | 
    
         
            -
            //# sourceMappingURL=chunk-NLPOT6AJ.mjs.map
         
     |