@exhumer/signalr-client 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
16
16
  }
17
17
  return to;
18
18
  };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
20
20
  value: mod,
21
21
  enumerable: true
22
22
  }) : target, mod));
@@ -133,7 +133,7 @@ var BaseUndiciClient = class {
133
133
  this.defaultHeaders = options.headers ?? {};
134
134
  this.defaultTimeout = options.timeout ?? 3e4;
135
135
  this.dispatcher = options.dispatcher ?? (0, undici.getGlobalDispatcher)();
136
- this.maximumResponseBodySize = options.maximumResponseBodySize ?? 32 * 1024 * 1024;
136
+ this.maximumResponseBodySize = options.maximumResponseBodySize ?? 33554432;
137
137
  }
138
138
  get(url, options) {
139
139
  return this.request("GET", url, options);
@@ -193,9 +193,10 @@ var RequestHttpClient = class extends BaseUndiciClient {
193
193
  */
194
194
  async stream(method, url, options = {}) {
195
195
  const { buf, inferredContentType } = prepareBody(options.body);
196
+ const headers = buildHeaders(this.defaultHeaders, options.headers, buf, inferredContentType);
196
197
  const { statusCode, headers: respHeaders, body } = await (0, undici.request)(url, {
197
198
  method,
198
- headers: buildHeaders(this.defaultHeaders, options.headers, buf, inferredContentType),
199
+ headers,
199
200
  ...buf !== null && { body: buf },
200
201
  headersTimeout: options.timeout ?? this.defaultTimeout,
201
202
  dispatcher: this.dispatcher
@@ -928,7 +929,8 @@ var WebSocketTransport = class {
928
929
  const token = await this.#accessTokenFactory();
929
930
  if (token) headers["Authorization"] = `Bearer ${token}`;
930
931
  }
931
- const ws = new undici.WebSocket(url.replace(/^http/, "ws"), {
932
+ const wsUrl = url.replace(/^http/, "ws");
933
+ const ws = new undici.WebSocket(wsUrl, {
932
934
  headers,
933
935
  ...this.#dispatcher !== void 0 && { dispatcher: this.#dispatcher }
934
936
  });
@@ -1286,9 +1288,7 @@ var ConsoleLogger = class {
1286
1288
  case LogLevel.Critical:
1287
1289
  console.error(line);
1288
1290
  break;
1289
- default:
1290
- console.log(line);
1291
- break;
1291
+ default: console.log(line);
1292
1292
  }
1293
1293
  }
1294
1294
  };
@@ -1458,7 +1458,7 @@ var HubConnection = class {
1458
1458
  this.#protocol = options.protocol ?? new JsonHubProtocol();
1459
1459
  this.#dispatcher = options.dispatcher;
1460
1460
  this.#ownsDispatcher = options.ownsDispatcher ?? false;
1461
- this.#maximumReceiveMessageSize = options.maximumReceiveMessageSize ?? 32 * 1024 * 1024;
1461
+ this.#maximumReceiveMessageSize = options.maximumReceiveMessageSize ?? 33554432;
1462
1462
  this.#http = options.httpClient ?? new DispatchHttpClient({
1463
1463
  headers: this.#extraHeaders,
1464
1464
  ...options.dispatcher !== void 0 && { dispatcher: options.dispatcher }
@@ -1868,9 +1868,7 @@ var HubConnection = class {
1868
1868
  } else this.#stopWithError(closeErr);
1869
1869
  break;
1870
1870
  }
1871
- default:
1872
- this.#logger.log(LogLevel.Warning, `Unknown message type: ${msg.type}`);
1873
- break;
1871
+ default: this.#logger.log(LogLevel.Warning, `Unknown message type: ${msg.type}`);
1874
1872
  }
1875
1873
  }
1876
1874
  async #invokeClientMethod(msg) {
@@ -2306,16 +2304,27 @@ const ResultKind = Object.freeze({
2306
2304
  Void: 2,
2307
2305
  NonVoid: 3
2308
2306
  });
2309
- /** Write `n` as a base-128 VarInt (little-endian groups of 7 bits). */
2310
- function writeVarInt(n) {
2311
- const bytes = [];
2312
- do {
2313
- let b = n & 127;
2307
+ /** Number of bytes needed to write `n` as a base-128 VarInt. */
2308
+ function varIntLength(n) {
2309
+ let len = 1;
2310
+ while (n >= 128) {
2314
2311
  n >>>= 7;
2315
- if (n > 0) b |= 128;
2316
- bytes.push(b);
2317
- } while (n > 0);
2318
- return new Uint8Array(bytes);
2312
+ len++;
2313
+ }
2314
+ return len;
2315
+ }
2316
+ /**
2317
+ * Write `n` as a base-128 VarInt (little-endian groups of 7 bits) into
2318
+ * `out`, returning the offset just past the last byte written.
2319
+ */
2320
+ function writeVarInt(n, out) {
2321
+ let i = 0;
2322
+ while (n >= 128) {
2323
+ out[i++] = n & 127 | 128;
2324
+ n >>>= 7;
2325
+ }
2326
+ out[i++] = n;
2327
+ return i;
2319
2328
  }
2320
2329
  /** Read a VarInt from `data` starting at `offset`. Returns value + bytes consumed. */
2321
2330
  function readVarInt(data, offset) {
@@ -2338,13 +2347,17 @@ function readVarInt(data, offset) {
2338
2347
  bytesRead
2339
2348
  };
2340
2349
  }
2350
+ /**
2351
+ * One encoder reused for every frame. `encode()` from @msgpack/msgpack builds
2352
+ * a new Encoder (and a fresh 2 KB buffer) per call, which dominates the cost of
2353
+ * encoding small hub messages. Encoding is synchronous, so sharing is safe.
2354
+ */
2355
+ const encoder = new _msgpack_msgpack.Encoder();
2341
2356
  function pack(arr) {
2342
- const payload = (0, _msgpack_msgpack.encode)(arr);
2343
- const lenPrefix = writeVarInt(payload.length);
2344
- const result = new Uint8Array(lenPrefix.length + payload.length);
2345
- result.set(lenPrefix, 0);
2346
- result.set(payload, lenPrefix.length);
2347
- return result.buffer.slice(0, result.byteLength);
2357
+ const payload = encoder.encodeSharedRef(arr);
2358
+ const frame = new Uint8Array(varIntLength(payload.length) + payload.length);
2359
+ frame.set(payload, writeVarInt(payload.length, frame));
2360
+ return frame.buffer;
2348
2361
  }
2349
2362
  var MsgpackHubProtocol = class {
2350
2363
  name = "messagepack";
@@ -2826,8 +2839,6 @@ var WebSocketClient = class extends node_events.EventEmitter {
2826
2839
  this.#state = 3;
2827
2840
  this.#socket?.destroy();
2828
2841
  this.emit("close");
2829
- break;
2830
- default: break;
2831
2842
  }
2832
2843
  }
2833
2844
  #emitData(opcode, payload) {