@zlink-systems/stream-connector 0.21.0 → 0.22.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.
@@ -628,7 +628,11 @@ function unwrapStreamError(error) {
628
628
  if (error instanceof ZlinkStreamException) {
629
629
  return error.error;
630
630
  }
631
- return { code: "remoteError" /* RemoteError */, message: error instanceof Error ? error.message : String(error), cause: error };
631
+ return {
632
+ code: "remoteError" /* RemoteError */,
633
+ message: error instanceof Error ? error.message : String(error),
634
+ cause: error
635
+ };
632
636
  }
633
637
  function subscription(dispose) {
634
638
  return { dispose };
@@ -663,7 +667,10 @@ function utf8Decode2(value) {
663
667
  var zlinkStreamAssert = {
664
668
  ensure(condition, message) {
665
669
  if (typeof message !== "string" || message.trim().length === 0) {
666
- throw connectorError("validationFailed" /* ValidationFailed */, "zlinkStreamAssert.ensure requires a non-empty diagnostic message.");
670
+ throw connectorError(
671
+ "validationFailed" /* ValidationFailed */,
672
+ "zlinkStreamAssert.ensure requires a non-empty diagnostic message."
673
+ );
667
674
  }
668
675
  if (!condition) {
669
676
  throw connectorError("validationFailed" /* ValidationFailed */, message);
@@ -712,10 +719,16 @@ function validateName(name, allowReserved = false) {
712
719
  throw connectorError("validationFailed" /* ValidationFailed */, "Message name must not be empty.");
713
720
  }
714
721
  if (!allowReserved && name.startsWith("$zlink.")) {
715
- throw connectorError("validationFailed" /* ValidationFailed */, "Message name uses a reserved zlink prefix.");
722
+ throw connectorError(
723
+ "validationFailed" /* ValidationFailed */,
724
+ "Message name uses a reserved zlink prefix."
725
+ );
716
726
  }
717
727
  if (utf8Encode2(name).length > 255) {
718
- throw connectorError("validationFailed" /* ValidationFailed */, "Message name must not exceed 255 UTF-8 bytes.");
728
+ throw connectorError(
729
+ "validationFailed" /* ValidationFailed */,
730
+ "Message name must not exceed 255 UTF-8 bytes."
731
+ );
719
732
  }
720
733
  }
721
734
 
@@ -732,13 +745,19 @@ var ZlinkStreamCallBuilderState = class {
732
745
  }
733
746
  ensureNotExecuted() {
734
747
  if (this.executed) {
735
- throw connectorError("validationFailed" /* ValidationFailed */, "Builder instances can be executed only once.");
748
+ throw connectorError(
749
+ "validationFailed" /* ValidationFailed */,
750
+ "Builder instances can be executed only once."
751
+ );
736
752
  }
737
753
  this.executed = true;
738
754
  }
739
755
  resolveMessageName() {
740
756
  if (this.name === void 0) {
741
- throw connectorError("validationFailed" /* ValidationFailed */, "Message name is required when the encoded stream payload has no message type.");
757
+ throw connectorError(
758
+ "validationFailed" /* ValidationFailed */,
759
+ "Message name is required when the encoded stream payload has no message type."
760
+ );
742
761
  }
743
762
  return this.name;
744
763
  }
@@ -828,7 +847,9 @@ var ZlinkStreamRequestBuilder = class {
828
847
  );
829
848
  return;
830
849
  }
831
- return operation.then((value) => (this.connector.options.codec ?? zlinkStreamJsonCodec).decode(value));
850
+ return operation.then(
851
+ (value) => (this.connector.options.codec ?? zlinkStreamJsonCodec).decode(value)
852
+ );
832
853
  }
833
854
  submitEncoded(signal) {
834
855
  this.state.ensureNotExecuted();
@@ -864,7 +885,12 @@ var ZlinkStreamWaitBuilder = class {
864
885
  async submit(signal) {
865
886
  this.markExecuted();
866
887
  const timeoutMs = this.timeoutMs ?? this.connector.options.waitTimeoutMs;
867
- const message = await this.connector.waitForMessage(this.name, timeoutMs, this.predicate, signal);
888
+ const message = await this.connector.waitForMessage(
889
+ this.name,
890
+ timeoutMs,
891
+ this.predicate,
892
+ signal
893
+ );
868
894
  if (message === void 0) {
869
895
  throw connectorError(
870
896
  "validationFailed" /* ValidationFailed */,
@@ -875,7 +901,10 @@ var ZlinkStreamWaitBuilder = class {
875
901
  }
876
902
  ensureConfigurable() {
877
903
  if (this.executed) {
878
- throw connectorError("validationFailed" /* ValidationFailed */, "Builder instances can be executed only once.");
904
+ throw connectorError(
905
+ "validationFailed" /* ValidationFailed */,
906
+ "Builder instances can be executed only once."
907
+ );
879
908
  }
880
909
  }
881
910
  markExecuted() {
@@ -901,9 +930,17 @@ var ZlinkStreamExpectNoneBuilder = class {
901
930
  async run(signal) {
902
931
  this.markExecuted();
903
932
  if (this.windowMs === void 0) {
904
- throw connectorError("validationFailed" /* ValidationFailed */, "expectNone requires within(windowMs).");
933
+ throw connectorError(
934
+ "validationFailed" /* ValidationFailed */,
935
+ "expectNone requires within(windowMs)."
936
+ );
905
937
  }
906
- const message = await this.connector.waitForMessage(this.name, this.windowMs, () => true, signal);
938
+ const message = await this.connector.waitForMessage(
939
+ this.name,
940
+ this.windowMs,
941
+ () => true,
942
+ signal
943
+ );
907
944
  if (message === void 0) {
908
945
  return;
909
946
  }
@@ -914,7 +951,10 @@ var ZlinkStreamExpectNoneBuilder = class {
914
951
  }
915
952
  ensureConfigurable() {
916
953
  if (this.executed) {
917
- throw connectorError("validationFailed" /* ValidationFailed */, "Builder instances can be executed only once.");
954
+ throw connectorError(
955
+ "validationFailed" /* ValidationFailed */,
956
+ "Builder instances can be executed only once."
957
+ );
918
958
  }
919
959
  }
920
960
  markExecuted() {
@@ -947,7 +987,10 @@ var ZlinkStreamSequenceBuilder = class {
947
987
  async run(signal) {
948
988
  this.markExecuted();
949
989
  if (this.predicates.length === 0) {
950
- throw connectorError("validationFailed" /* ValidationFailed */, "waitForSequence requires at least one expectation.");
990
+ throw connectorError(
991
+ "validationFailed" /* ValidationFailed */,
992
+ "waitForSequence requires at least one expectation."
993
+ );
951
994
  }
952
995
  const timeoutMs = this.timeoutMs ?? this.connector.options.waitTimeoutMs;
953
996
  const deadline = Date.now() + timeoutMs;
@@ -979,7 +1022,10 @@ var ZlinkStreamSequenceBuilder = class {
979
1022
  }
980
1023
  ensureConfigurable() {
981
1024
  if (this.executed) {
982
- throw connectorError("validationFailed" /* ValidationFailed */, "Builder instances can be executed only once.");
1025
+ throw connectorError(
1026
+ "validationFailed" /* ValidationFailed */,
1027
+ "Builder instances can be executed only once."
1028
+ );
983
1029
  }
984
1030
  }
985
1031
  markExecuted() {
@@ -989,7 +1035,10 @@ var ZlinkStreamSequenceBuilder = class {
989
1035
  };
990
1036
  function validateTimeout(timeoutMs) {
991
1037
  if (!Number.isFinite(timeoutMs) || timeoutMs < 0) {
992
- throw connectorError("validationFailed" /* ValidationFailed */, "Timeout must be a non-negative finite number.");
1038
+ throw connectorError(
1039
+ "validationFailed" /* ValidationFailed */,
1040
+ "Timeout must be a non-negative finite number."
1041
+ );
993
1042
  }
994
1043
  }
995
1044
 
@@ -1005,7 +1054,10 @@ var zlinkStreamLz4CompressionCodec = {
1005
1054
  function compressPayload(payload, compression, compressionCodec) {
1006
1055
  const codec = resolveCompressionCodec(compression, compressionCodec);
1007
1056
  if (codec === void 0) {
1008
- throw connectorError("compressionFailed" /* CompressionFailed */, "Compression codec is not configured.");
1057
+ throw connectorError(
1058
+ "compressionFailed" /* CompressionFailed */,
1059
+ "Compression codec is not configured."
1060
+ );
1009
1061
  }
1010
1062
  return codec.compress(payload);
1011
1063
  }
@@ -1015,7 +1067,10 @@ function decompressIfNeeded(header, payload, compression, compressionCodec, maxD
1015
1067
  }
1016
1068
  const codec = resolveCompressionCodec(compression, compressionCodec);
1017
1069
  if (codec === void 0) {
1018
- throw connectorError("decompressionFailed" /* DecompressionFailed */, "Compression codec is not configured.");
1070
+ throw connectorError(
1071
+ "decompressionFailed" /* DecompressionFailed */,
1072
+ "Compression codec is not configured."
1073
+ );
1019
1074
  }
1020
1075
  try {
1021
1076
  const decompressed = codec.decompress(payload, maxDecompressedSize);
@@ -1048,26 +1103,39 @@ var ZlinkStreamFrameCodec = class {
1048
1103
  try {
1049
1104
  return decodeStreamWireFrame(frame);
1050
1105
  } catch (cause) {
1051
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Frame length does not match prefix.", cause);
1106
+ throw connectorError(
1107
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1108
+ "Frame length does not match prefix.",
1109
+ cause
1110
+ );
1052
1111
  }
1053
1112
  }
1054
1113
  };
1055
1114
  function splitZlinkStreamFrames(chunk) {
1056
1115
  if (chunk.length === 0) {
1057
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Stream frame prefix is incomplete.");
1116
+ throw connectorError(
1117
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1118
+ "Stream frame prefix is incomplete."
1119
+ );
1058
1120
  }
1059
1121
  const frames = [];
1060
1122
  let offset = 0;
1061
1123
  while (offset < chunk.length) {
1062
1124
  const remaining = chunk.length - offset;
1063
1125
  if (remaining < 6) {
1064
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Stream frame prefix is incomplete.");
1126
+ throw connectorError(
1127
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1128
+ "Stream frame prefix is incomplete."
1129
+ );
1065
1130
  }
1066
1131
  const headerLength = chunk[offset] << 8 | chunk[offset + 1];
1067
1132
  const payloadLength = chunk[offset + 2] * 16777216 + (chunk[offset + 3] << 16) + (chunk[offset + 4] << 8) + chunk[offset + 5];
1068
1133
  const frameLength = 6 + headerLength + payloadLength;
1069
1134
  if (frameLength > remaining) {
1070
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Frame length does not match prefix.");
1135
+ throw connectorError(
1136
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1137
+ "Frame length does not match prefix."
1138
+ );
1071
1139
  }
1072
1140
  frames.push(chunk.subarray(offset, offset + frameLength));
1073
1141
  offset += frameLength;
@@ -1076,7 +1144,10 @@ function splitZlinkStreamFrames(chunk) {
1076
1144
  }
1077
1145
  function validatePayload(payloadLength, maxPayloadSize) {
1078
1146
  if (payloadLength > maxPayloadSize) {
1079
- throw connectorError("validationFailed" /* ValidationFailed */, "Payload exceeds MaxSendPayloadSize.");
1147
+ throw connectorError(
1148
+ "validationFailed" /* ValidationFailed */,
1149
+ "Payload exceeds MaxSendPayloadSize."
1150
+ );
1080
1151
  }
1081
1152
  }
1082
1153
 
@@ -1094,14 +1165,22 @@ var ZlinkStreamMetadataCodec = class {
1094
1165
  }
1095
1166
  return size;
1096
1167
  } catch (cause) {
1097
- throw connectorError("validationFailed" /* ValidationFailed */, streamWireErrorMessage(cause), cause);
1168
+ throw connectorError(
1169
+ "validationFailed" /* ValidationFailed */,
1170
+ streamWireErrorMessage(cause),
1171
+ cause
1172
+ );
1098
1173
  }
1099
1174
  }
1100
1175
  static write(metadata, destination) {
1101
1176
  try {
1102
1177
  destination.set(encodeStreamWireMetadata(metadata.values));
1103
1178
  } catch (cause) {
1104
- throw connectorError("validationFailed" /* ValidationFailed */, streamWireErrorMessage(cause), cause);
1179
+ throw connectorError(
1180
+ "validationFailed" /* ValidationFailed */,
1181
+ streamWireErrorMessage(cause),
1182
+ cause
1183
+ );
1105
1184
  }
1106
1185
  }
1107
1186
  static decode(metadata) {
@@ -1109,7 +1188,11 @@ var ZlinkStreamMetadataCodec = class {
1109
1188
  const values = decodeStreamWireMetadata(metadata);
1110
1189
  return values.size === 0 ? ZlinkStreamMetadataMap.empty : ZlinkStreamMetadataMap.from(values);
1111
1190
  } catch (cause) {
1112
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, streamWireErrorMessage(cause), cause);
1191
+ throw connectorError(
1192
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1193
+ streamWireErrorMessage(cause),
1194
+ cause
1195
+ );
1113
1196
  }
1114
1197
  }
1115
1198
  };
@@ -1137,7 +1220,11 @@ var ZlinkStreamHeaderCodec = class {
1137
1220
  flowOrigin: encodeFlowOrigin(header.flowOrigin)
1138
1221
  });
1139
1222
  } catch (cause) {
1140
- throw connectorError("validationFailed" /* ValidationFailed */, streamWireErrorMessage2(cause), cause);
1223
+ throw connectorError(
1224
+ "validationFailed" /* ValidationFailed */,
1225
+ streamWireErrorMessage2(cause),
1226
+ cause
1227
+ );
1141
1228
  }
1142
1229
  }
1143
1230
  /**
@@ -1162,7 +1249,11 @@ var ZlinkStreamHeaderCodec = class {
1162
1249
  flowOrigin: decodeFlowOrigin(wire.flowOrigin)
1163
1250
  };
1164
1251
  } catch (cause) {
1165
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, streamWireErrorMessage2(cause), cause);
1252
+ throw connectorError(
1253
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1254
+ streamWireErrorMessage2(cause),
1255
+ cause
1256
+ );
1166
1257
  }
1167
1258
  validateEnum(decoded.kind, decoded.codec, decoded.flags);
1168
1259
  if (!isReplyKind2(decoded.kind)) {
@@ -1200,19 +1291,31 @@ function validateHeaderSemantics(header) {
1200
1291
  const hasRequestSeq = header.requestSeq !== void 0 || (header.flags & 1 /* HasRequestSeq */) !== 0;
1201
1292
  const hasMetadata = header.metadata.count > 0 || (header.flags & 2 /* HasMetadata */) !== 0;
1202
1293
  if (header.kind === 1 /* Send */ && hasRequestSeq) {
1203
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Send packet must not contain a request sequence.");
1294
+ throw connectorError(
1295
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1296
+ "Send packet must not contain a request sequence."
1297
+ );
1204
1298
  }
1205
1299
  if ((header.kind === 2 /* Request */ || header.kind === 3 /* Response */) && !hasRequestSeq) {
1206
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Request and response packets must contain a request sequence.");
1300
+ throw connectorError(
1301
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1302
+ "Request and response packets must contain a request sequence."
1303
+ );
1207
1304
  }
1208
1305
  if (header.kind === 4 /* Error */ && header.codec !== 1 /* Json */) {
1209
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Error packet must use the JSON codec.");
1306
+ throw connectorError(
1307
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1308
+ "Error packet must use the JSON codec."
1309
+ );
1210
1310
  }
1211
1311
  if (header.kind === 5 /* Control */) {
1212
1312
  const hasCorrelation = header.correlationId !== void 0 && header.correlationId.length > 0 || (header.flags & 8 /* HasCorrelationId */) !== 0;
1213
1313
  const hasFlow = header.flowId !== void 0 || header.flowOrigin !== void 0 || (header.flags & 16 /* HasFlowId */) !== 0;
1214
1314
  if (header.flags !== 0 /* None */ || hasRequestSeq || hasMetadata || hasCorrelation || hasFlow || header.codec !== 0 /* Raw */) {
1215
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Control packet must use raw codec and must not contain flags.");
1315
+ throw connectorError(
1316
+ "frameDecodeFailed" /* FrameDecodeFailed */,
1317
+ "Control packet must use raw codec and must not contain flags."
1318
+ );
1216
1319
  }
1217
1320
  }
1218
1321
  }
@@ -1244,17 +1347,30 @@ var ZlinkStreamFrameProtocol = class {
1244
1347
  }
1245
1348
  encode(kind, name, payload, metadata, compress, requestSeq, correlationId, flowId, flowOrigin) {
1246
1349
  const payloadBytes = compress ? compressPayload(payload.payload, this.options.compression, this.options.compressionCodec) : payload.payload;
1247
- const header = buildHeader(kind, name, payload.codec, metadata, compress, requestSeq, correlationId, flowId, flowOrigin);
1350
+ const header = buildHeader(
1351
+ kind,
1352
+ name,
1353
+ payload.codec,
1354
+ metadata,
1355
+ compress,
1356
+ requestSeq,
1357
+ correlationId,
1358
+ flowId,
1359
+ flowOrigin
1360
+ );
1248
1361
  return this.encodeFrame(header, payloadBytes);
1249
1362
  }
1250
1363
  encodeControl(name, payload = new Uint8Array()) {
1251
- return this.encodeFrame({
1252
- kind: 5 /* Control */,
1253
- codec: 0 /* Raw */,
1254
- flags: 0 /* None */,
1255
- name,
1256
- metadata: ZlinkStreamMetadataMap.empty
1257
- }, payload);
1364
+ return this.encodeFrame(
1365
+ {
1366
+ kind: 5 /* Control */,
1367
+ codec: 0 /* Raw */,
1368
+ flags: 0 /* None */,
1369
+ name,
1370
+ metadata: ZlinkStreamMetadataMap.empty
1371
+ },
1372
+ payload
1373
+ );
1258
1374
  }
1259
1375
  decode(frameBytes, flowEnabled = this.flowEnabled()) {
1260
1376
  const frame = ZlinkStreamFrameCodec.decode(frameBytes);
@@ -1272,7 +1388,10 @@ var ZlinkStreamFrameProtocol = class {
1272
1388
  return splitZlinkStreamFrames(chunk).map((frame) => {
1273
1389
  const decoded = this.decode(frame, flowEnabled);
1274
1390
  if (decoded.payload.length > this.options.maxReceivePayloadSize) {
1275
- throw connectorError("frameTooLarge" /* FrameTooLarge */, "Payload exceeds MaxReceivePayloadSize.");
1391
+ throw connectorError(
1392
+ "frameTooLarge" /* FrameTooLarge */,
1393
+ "Payload exceeds MaxReceivePayloadSize."
1394
+ );
1276
1395
  }
1277
1396
  return decoded;
1278
1397
  });
@@ -1332,7 +1451,10 @@ function normalizeOptions(options, defaultTransportFactory) {
1332
1451
  }
1333
1452
  const inferredTransport = inferTransport(endpoint);
1334
1453
  if (options.transport !== void 0 && options.transport !== inferredTransport) {
1335
- throw connectorError("configurationError" /* ConfigurationError */, "Configured transport conflicts with endpoint scheme.");
1454
+ throw connectorError(
1455
+ "configurationError" /* ConfigurationError */,
1456
+ "Configured transport conflicts with endpoint scheme."
1457
+ );
1336
1458
  }
1337
1459
  validatePositive(options.connectTimeoutMs ?? 5e3, "ConnectTimeout");
1338
1460
  validatePositive(options.requestTimeoutMs ?? 3e4, "RequestTimeout");
@@ -1386,7 +1508,10 @@ function resolveCompressionCodec2(options) {
1386
1508
  const compression = options.compression ?? "lz4" /* Lz4 */;
1387
1509
  if (compression === "none" /* None */) {
1388
1510
  if (options.compressionCodec !== void 0) {
1389
- throw connectorError("configurationError" /* ConfigurationError */, "compressionCodec cannot be set when compression is none.");
1511
+ throw connectorError(
1512
+ "configurationError" /* ConfigurationError */,
1513
+ "compressionCodec cannot be set when compression is none."
1514
+ );
1390
1515
  }
1391
1516
  return void 0;
1392
1517
  }
@@ -1412,7 +1537,10 @@ function validateHeartbeat(options) {
1412
1537
  validatePositive(intervalMs, "Heartbeat interval");
1413
1538
  validatePositive(timeoutMs, "Heartbeat timeout");
1414
1539
  if (timeoutMs <= intervalMs) {
1415
- throw connectorError("validationFailed" /* ValidationFailed */, "Heartbeat timeout must be greater than the heartbeat interval.");
1540
+ throw connectorError(
1541
+ "validationFailed" /* ValidationFailed */,
1542
+ "Heartbeat timeout must be greater than the heartbeat interval."
1543
+ );
1416
1544
  }
1417
1545
  }
1418
1546
  function validateReconnect(options) {
@@ -1423,11 +1551,17 @@ function validateReconnect(options) {
1423
1551
  validatePositive(options?.initialDelayMs ?? 250, "Reconnect InitialDelay");
1424
1552
  validatePositive(options?.maxDelayMs ?? 5e3, "Reconnect MaxDelay");
1425
1553
  if ((options?.backoffFactor ?? 2) < 1) {
1426
- throw connectorError("validationFailed" /* ValidationFailed */, "Reconnect BackoffFactor must be at least 1.0.");
1554
+ throw connectorError(
1555
+ "validationFailed" /* ValidationFailed */,
1556
+ "Reconnect BackoffFactor must be at least 1.0."
1557
+ );
1427
1558
  }
1428
1559
  const maxAttempts = options?.maxAttempts === void 0 ? 3 : options.maxAttempts;
1429
1560
  if (maxAttempts !== null && !(Number.isFinite(maxAttempts) && maxAttempts > 0)) {
1430
- throw connectorError("validationFailed" /* ValidationFailed */, "Reconnect MaxAttempts must be null or positive.");
1561
+ throw connectorError(
1562
+ "validationFailed" /* ValidationFailed */,
1563
+ "Reconnect MaxAttempts must be null or positive."
1564
+ );
1431
1565
  }
1432
1566
  }
1433
1567
 
@@ -1465,7 +1599,9 @@ var ZlinkStreamPendingRequests = class {
1465
1599
  const promise = new Promise((resolve, reject) => {
1466
1600
  timeout = setTimeout(() => {
1467
1601
  this.active.delete(requestSeq);
1468
- reject(connectorError("requestTimeout" /* RequestTimeout */, `Request '${packetName}' timed out.`));
1602
+ reject(
1603
+ connectorError("requestTimeout" /* RequestTimeout */, `Request '${packetName}' timed out.`)
1604
+ );
1469
1605
  }, timeoutMs);
1470
1606
  resolvePending = resolve;
1471
1607
  rejectPending = (error) => reject(connectorError(error.code, error.message, error.cause));
@@ -1729,11 +1865,14 @@ var ZlinkStreamReceivedMessages = class {
1729
1865
  try {
1730
1866
  await handler(message, signal);
1731
1867
  } catch (cause) {
1732
- await this.events.publishError({
1733
- code: "userCallbackFailed" /* UserCallbackFailed */,
1734
- message: "Typed message handler failed.",
1735
- cause
1736
- }, signal);
1868
+ await this.events.publishError(
1869
+ {
1870
+ code: "userCallbackFailed" /* UserCallbackFailed */,
1871
+ message: "Typed message handler failed.",
1872
+ cause
1873
+ },
1874
+ signal
1875
+ );
1737
1876
  }
1738
1877
  }
1739
1878
  }
@@ -1792,7 +1931,17 @@ var ZlinkStreamFrameSender = class {
1792
1931
  const flow = this.protocol.flowEnabled() ? this.flowContext.currentOrCreate(explicitFlow) : void 0;
1793
1932
  await this.write(
1794
1933
  connection,
1795
- this.protocol.encode(kind, name, payload, metadata, compress, requestSeq, correlationId, flow?.flowId, flow?.flowOrigin),
1934
+ this.protocol.encode(
1935
+ kind,
1936
+ name,
1937
+ payload,
1938
+ metadata,
1939
+ compress,
1940
+ requestSeq,
1941
+ correlationId,
1942
+ flow?.flowId,
1943
+ flow?.flowOrigin
1944
+ ),
1796
1945
  signal
1797
1946
  );
1798
1947
  }
@@ -1827,11 +1976,13 @@ var reasons = {
1827
1976
  6: "TransportError"
1828
1977
  };
1829
1978
  function decodeSessionClosing(payload) {
1830
- if (payload.length < 4 || payload[0] !== 1) throw new Error("Unsupported session-closing version.");
1979
+ if (payload.length < 4 || payload[0] !== 1)
1980
+ throw new Error("Unsupported session-closing version.");
1831
1981
  const closeReason = reasons[payload[1]];
1832
1982
  if (closeReason === void 0) throw new Error("Unknown session-closing reason.");
1833
1983
  const length = payload[2] << 8 | payload[3];
1834
- if (length > 512 || payload.length !== 4 + length) throw new Error("Invalid session-closing diagnostic length.");
1984
+ if (length > 512 || payload.length !== 4 + length)
1985
+ throw new Error("Invalid session-closing diagnostic length.");
1835
1986
  const diagnostic = length === 0 ? void 0 : new TextDecoder("utf-8", { fatal: true }).decode(payload.subarray(4));
1836
1987
  return { closeReason, diagnostic };
1837
1988
  }
@@ -1887,7 +2038,14 @@ var ZlinkStreamReceiveDispatcher = class {
1887
2038
  break;
1888
2039
  }
1889
2040
  try {
1890
- await this.dispatch(connection, frame.header, frame.payload, signal, flowEnabled, connectionForSend);
2041
+ await this.dispatch(
2042
+ connection,
2043
+ frame.header,
2044
+ frame.payload,
2045
+ signal,
2046
+ flowEnabled,
2047
+ connectionForSend
2048
+ );
1891
2049
  } catch (cause) {
1892
2050
  if (frame.header.kind === 5 /* Control */ && frame.header.name === ZLINK_STREAM_HEARTBEAT_PING) {
1893
2051
  throw cause;
@@ -1907,10 +2065,13 @@ var ZlinkStreamReceiveDispatcher = class {
1907
2065
  codec: header.codec,
1908
2066
  payload: this.protocol.decodePayload(header, payload)
1909
2067
  })) {
1910
- await this.events.publishError({
1911
- code: "frameDecodeFailed" /* FrameDecodeFailed */,
1912
- message: `Response request sequence '${header.requestSeq}' has no pending request.`
1913
- }, signal);
2068
+ await this.events.publishError(
2069
+ {
2070
+ code: "frameDecodeFailed" /* FrameDecodeFailed */,
2071
+ message: `Response request sequence '${header.requestSeq}' has no pending request.`
2072
+ },
2073
+ signal
2074
+ );
1914
2075
  }
1915
2076
  } catch (cause) {
1916
2077
  const decodeError = toStreamError(
@@ -1918,10 +2079,7 @@ var ZlinkStreamReceiveDispatcher = class {
1918
2079
  "decompressionFailed" /* DecompressionFailed */,
1919
2080
  "Decompression failed."
1920
2081
  );
1921
- if (!this.pendingRequests.reject(
1922
- header.requestSeq,
1923
- decodeError
1924
- )) {
2082
+ if (!this.pendingRequests.reject(header.requestSeq, decodeError)) {
1925
2083
  await this.events.publishError(decodeError, signal);
1926
2084
  }
1927
2085
  }
@@ -1939,10 +2097,7 @@ var ZlinkStreamReceiveDispatcher = class {
1939
2097
  "frameDecodeFailed" /* FrameDecodeFailed */,
1940
2098
  "Remote error payload is invalid."
1941
2099
  );
1942
- if (!this.pendingRequests.reject(
1943
- header.requestSeq,
1944
- decodeError
1945
- )) {
2100
+ if (!this.pendingRequests.reject(header.requestSeq, decodeError)) {
1946
2101
  await this.events.publishError(decodeError, signal);
1947
2102
  }
1948
2103
  }
@@ -1958,13 +2113,16 @@ var ZlinkStreamReceiveDispatcher = class {
1958
2113
  }
1959
2114
  if (header.kind === 1 /* Send */) {
1960
2115
  const flow = flowEnabled ? this.flowContext.createInbound(header.flowId, header.flowOrigin) : void 0;
1961
- this.receivedMessages.enqueue({
1962
- name: header.name,
1963
- metadata: header.metadata,
1964
- payload: { codec: header.codec, payload: this.protocol.decodePayload(header, payload) },
1965
- flowId: flow?.flowId,
1966
- flowOrigin: flow?.flowOrigin
1967
- }, signal);
2116
+ this.receivedMessages.enqueue(
2117
+ {
2118
+ name: header.name,
2119
+ metadata: header.metadata,
2120
+ payload: { codec: header.codec, payload: this.protocol.decodePayload(header, payload) },
2121
+ flowId: flow?.flowId,
2122
+ flowOrigin: flow?.flowOrigin
2123
+ },
2124
+ signal
2125
+ );
1968
2126
  }
1969
2127
  }
1970
2128
  async dispatchControl(connection, header, payload, signal, connectionForSend) {
@@ -1974,7 +2132,10 @@ var ZlinkStreamReceiveDispatcher = class {
1974
2132
  return;
1975
2133
  }
1976
2134
  if (payload.length !== 0) {
1977
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Control packet payload must be empty.");
2135
+ throw connectorError(
2136
+ "frameDecodeFailed" /* FrameDecodeFailed */,
2137
+ "Control packet payload must be empty."
2138
+ );
1978
2139
  }
1979
2140
  if (header.name === ZLINK_STREAM_HEARTBEAT_PING) {
1980
2141
  try {
@@ -2002,7 +2163,11 @@ function decodeRemoteError(protocol, header, payload) {
2002
2163
  try {
2003
2164
  decoded = JSON.parse(utf8Decode2(decodedPayload));
2004
2165
  } catch (cause) {
2005
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "Remote error payload must be a JSON object.", cause);
2166
+ throw connectorError(
2167
+ "frameDecodeFailed" /* FrameDecodeFailed */,
2168
+ "Remote error payload must be a JSON object.",
2169
+ cause
2170
+ );
2006
2171
  }
2007
2172
  if (decoded === null || typeof decoded !== "object" || Array.isArray(decoded) || typeof decoded.code !== "string" || typeof decoded.message !== "string") {
2008
2173
  throw connectorError(
@@ -2081,7 +2246,10 @@ var ZlinkStreamConnectorLifecycle = class {
2081
2246
  this.lateConnectCleanupError = error;
2082
2247
  throw error;
2083
2248
  }
2084
- throw connectorError("disconnected" /* Disconnected */, "Connector closed while connecting.");
2249
+ throw connectorError(
2250
+ "disconnected" /* Disconnected */,
2251
+ "Connector closed while connecting."
2252
+ );
2085
2253
  }
2086
2254
  this.currentConnection = connection;
2087
2255
  this.connectionGeneration += 1;
@@ -2120,8 +2288,15 @@ var ZlinkStreamConnectorLifecycle = class {
2120
2288
  }
2121
2289
  async serverClosing(reason) {
2122
2290
  this.closeReasonValue = reason;
2123
- const error = { code: "disconnected" /* Disconnected */, message: `Server closed the session: ${reason}.` };
2124
- await this.disconnectForTransportFailure(error, this.currentConnection, this.connectionGeneration);
2291
+ const error = {
2292
+ code: "disconnected" /* Disconnected */,
2293
+ message: `Server closed the session: ${reason}.`
2294
+ };
2295
+ await this.disconnectForTransportFailure(
2296
+ error,
2297
+ this.currentConnection,
2298
+ this.connectionGeneration
2299
+ );
2125
2300
  }
2126
2301
  async closeOnce(signal) {
2127
2302
  await this.connectTask?.catch(() => void 0);
@@ -2145,7 +2320,10 @@ var ZlinkStreamConnectorLifecycle = class {
2145
2320
  } catch (error) {
2146
2321
  errors.push(error);
2147
2322
  }
2148
- this.pendingRequests.failAll({ code: "disconnected" /* Disconnected */, message: "Connector closed." });
2323
+ this.pendingRequests.failAll({
2324
+ code: "disconnected" /* Disconnected */,
2325
+ message: "Connector closed."
2326
+ });
2149
2327
  this.receivedMessages.connectionEnded();
2150
2328
  void this.setState("closed" /* Closed */, void 0, signal);
2151
2329
  this.publishDisconnectedWithoutWaiting(signal);
@@ -2270,7 +2448,11 @@ var ZlinkStreamConnectorLifecycle = class {
2270
2448
  }
2271
2449
  } catch (cause) {
2272
2450
  if (signal.aborted) return;
2273
- const error = toStreamError(cause, "frameDecodeFailed" /* FrameDecodeFailed */, "Receive loop failed.");
2451
+ const error = toStreamError(
2452
+ cause,
2453
+ "frameDecodeFailed" /* FrameDecodeFailed */,
2454
+ "Receive loop failed."
2455
+ );
2274
2456
  await this.disconnectForTransportFailure(error, connection, generation);
2275
2457
  } finally {
2276
2458
  this.receiveLoopSleeping = false;
@@ -2329,7 +2511,11 @@ var ZlinkStreamConnectorLifecycle = class {
2329
2511
  if (Date.now() - this.lastInboundAt > this.options.heartbeat.timeoutMs) {
2330
2512
  this.closeReasonValue = "HeartbeatTimeout";
2331
2513
  const error = { code: "disconnected" /* Disconnected */, message: "Heartbeat timed out." };
2332
- await this.disconnectForTransportFailure(error, this.currentConnection, this.connectionGeneration);
2514
+ await this.disconnectForTransportFailure(
2515
+ error,
2516
+ this.currentConnection,
2517
+ this.connectionGeneration
2518
+ );
2333
2519
  return;
2334
2520
  }
2335
2521
  const connection = this.currentConnection;
@@ -2584,7 +2770,11 @@ var BrowserWebSocketConnection = class {
2584
2770
  const message = toUint8Array(event.data);
2585
2771
  this.messages.push(message);
2586
2772
  } catch (cause) {
2587
- this.error = cause instanceof Error ? cause : connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "WebSocket message decode failed.", cause);
2773
+ this.error = cause instanceof Error ? cause : connectorError(
2774
+ "frameDecodeFailed" /* FrameDecodeFailed */,
2775
+ "WebSocket message decode failed.",
2776
+ cause
2777
+ );
2588
2778
  this.closed = true;
2589
2779
  this.socket.close();
2590
2780
  }
@@ -2592,19 +2782,28 @@ var BrowserWebSocketConnection = class {
2592
2782
  };
2593
2783
  onClose = () => {
2594
2784
  if (!this.closed) {
2595
- this.error = connectorError("disconnected" /* Disconnected */, "Remote stream closed the WebSocket connection.");
2785
+ this.error = connectorError(
2786
+ "disconnected" /* Disconnected */,
2787
+ "Remote stream closed the WebSocket connection."
2788
+ );
2596
2789
  }
2597
2790
  this.closed = true;
2598
2791
  this.wakeReader();
2599
2792
  };
2600
2793
  onError = () => {
2601
- this.error = connectorError("disconnected" /* Disconnected */, "Remote stream closed after a WebSocket error.");
2794
+ this.error = connectorError(
2795
+ "disconnected" /* Disconnected */,
2796
+ "Remote stream closed after a WebSocket error."
2797
+ );
2602
2798
  this.closed = true;
2603
2799
  this.wakeReader();
2604
2800
  };
2605
2801
  waitForMessage(signal) {
2606
2802
  if (this.readWaiter !== void 0) {
2607
- throw connectorError("validationFailed" /* ValidationFailed */, "Only one pending stream read is supported.");
2803
+ throw connectorError(
2804
+ "validationFailed" /* ValidationFailed */,
2805
+ "Only one pending stream read is supported."
2806
+ );
2608
2807
  }
2609
2808
  return new Promise((resolve, reject) => {
2610
2809
  const onAbort = () => {
@@ -2660,9 +2859,10 @@ function waitForClose(socket, signal) {
2660
2859
  async function waitForOpen(socket, connectTimeoutMs, signal) {
2661
2860
  throwIfAborted(signal);
2662
2861
  await new Promise((resolve, reject) => {
2663
- const timeout = setTimeout(() => finish(
2664
- connectorError("connectTimeout" /* ConnectTimeout */, "Connect timed out.")
2665
- ), connectTimeoutMs);
2862
+ const timeout = setTimeout(
2863
+ () => finish(connectorError("connectTimeout" /* ConnectTimeout */, "Connect timed out.")),
2864
+ connectTimeoutMs
2865
+ );
2666
2866
  const onOpen = () => finish();
2667
2867
  const onClose = () => finish(connectorError("connectTimeout" /* ConnectTimeout */, "Connect closed before opening."));
2668
2868
  const onError = () => finish(connectorError("connectTimeout" /* ConnectTimeout */, "Connect failed."));
@@ -2693,7 +2893,10 @@ function toUint8Array(data) {
2693
2893
  if (ArrayBuffer.isView(data)) {
2694
2894
  return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
2695
2895
  }
2696
- throw connectorError("frameDecodeFailed" /* FrameDecodeFailed */, "WebSocket text messages are not supported.");
2896
+ throw connectorError(
2897
+ "frameDecodeFailed" /* FrameDecodeFailed */,
2898
+ "WebSocket text messages are not supported."
2899
+ );
2697
2900
  }
2698
2901
 
2699
2902
  // packages/stream-connector/src/Runtime/ZlinkStreamConnector.ts
@@ -2823,13 +3026,16 @@ var DefaultZlinkStreamConnector = class {
2823
3026
  return new ZlinkStreamRequestBuilder(this, this.resolveNameOrDefault(encoded), encoded);
2824
3027
  }
2825
3028
  on(name, handler, messageType) {
2826
- const encodedHandler = (message, signal) => handler({
2827
- name: message.name,
2828
- metadata: message.metadata,
2829
- payload: this.decodePayload(message.payload, messageType),
2830
- flowId: message.flowId,
2831
- flowOrigin: message.flowOrigin
2832
- }, signal);
3029
+ const encodedHandler = (message, signal) => handler(
3030
+ {
3031
+ name: message.name,
3032
+ metadata: message.metadata,
3033
+ payload: this.decodePayload(message.payload, messageType),
3034
+ flowId: message.flowId,
3035
+ flowOrigin: message.flowOrigin
3036
+ },
3037
+ signal
3038
+ );
2833
3039
  return this.receivedMessages.on(name, encodedHandler);
2834
3040
  }
2835
3041
  waitFor(nameOrType) {
@@ -2850,7 +3056,10 @@ var DefaultZlinkStreamConnector = class {
2850
3056
  observedName(nameOrType) {
2851
3057
  const name = typeof nameOrType === "function" ? this.options.nameResolver.resolve(nameOrType) : nameOrType;
2852
3058
  if (typeof name !== "string") {
2853
- throw connectorError("validationFailed" /* ValidationFailed */, "Packet name must be a string or a payload constructor.");
3059
+ throw connectorError(
3060
+ "validationFailed" /* ValidationFailed */,
3061
+ "Packet name must be a string or a payload constructor."
3062
+ );
2854
3063
  }
2855
3064
  validateName(name);
2856
3065
  return name;
@@ -2870,7 +3079,10 @@ var DefaultZlinkStreamConnector = class {
2870
3079
  waitForMessage(name, timeoutMs, predicate, signal) {
2871
3080
  validateName(name);
2872
3081
  if (!Number.isFinite(timeoutMs) || timeoutMs < 0) {
2873
- throw connectorError("validationFailed" /* ValidationFailed */, "Timeout must be a non-negative finite number.");
3082
+ throw connectorError(
3083
+ "validationFailed" /* ValidationFailed */,
3084
+ "Timeout must be a non-negative finite number."
3085
+ );
2874
3086
  }
2875
3087
  throwIfAborted(signal);
2876
3088
  return new Promise((resolve, reject) => {
@@ -2896,30 +3108,36 @@ var DefaultZlinkStreamConnector = class {
2896
3108
  };
2897
3109
  timer = setTimeout(() => finish(), timeoutMs);
2898
3110
  signal?.addEventListener("abort", onAbort, { once: true });
2899
- disposable = this.receivedMessages.observe(name, (message) => {
2900
- if (done) {
2901
- return false;
2902
- }
2903
- try {
2904
- const decoded = {
2905
- name: message.name,
2906
- metadata: message.metadata,
2907
- payload: this.decodeWaitPayload(message.payload),
2908
- flowId: message.flowId,
2909
- flowOrigin: message.flowOrigin
2910
- };
2911
- if (!predicate(decoded)) {
3111
+ disposable = this.receivedMessages.observe(
3112
+ name,
3113
+ (message) => {
3114
+ if (done) {
2912
3115
  return false;
2913
3116
  }
2914
- finish(void 0, decoded);
2915
- } catch (cause) {
2916
- finish(cause);
2917
- }
2918
- return true;
2919
- }, () => finish(connectorError(
2920
- "disconnected" /* Disconnected */,
2921
- `The connection this wait for '${name}' observed has ended.`
2922
- )));
3117
+ try {
3118
+ const decoded = {
3119
+ name: message.name,
3120
+ metadata: message.metadata,
3121
+ payload: this.decodeWaitPayload(message.payload),
3122
+ flowId: message.flowId,
3123
+ flowOrigin: message.flowOrigin
3124
+ };
3125
+ if (!predicate(decoded)) {
3126
+ return false;
3127
+ }
3128
+ finish(void 0, decoded);
3129
+ } catch (cause) {
3130
+ finish(cause);
3131
+ }
3132
+ return true;
3133
+ },
3134
+ () => finish(
3135
+ connectorError(
3136
+ "disconnected" /* Disconnected */,
3137
+ `The connection this wait for '${name}' observed has ended.`
3138
+ )
3139
+ )
3140
+ );
2923
3141
  });
2924
3142
  }
2925
3143
  encodePayload(payload, messageType) {