@relaybus/rabbitmq 0.4.1 → 0.5.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
@@ -781,6 +781,7 @@ function jsonEnvelope(mapping) {
781
781
  });
782
782
  const contentType = mapping.contentType ?? DEFAULT_CONTENT_TYPE;
783
783
  return {
784
+ withWireTypes: (declared) => jsonEnvelope({ ...mapping, wireTypes: internal.mergeWireTypes(mapping, declared) }),
784
785
  encode: (envelope) => {
785
786
  assertJsonPayload(envelope.payload, envelope.id);
786
787
  assertShortString("Event id", envelope.id);
@@ -1015,11 +1016,16 @@ function resolveCodec({
1015
1016
  if (decode !== void 0 && codec !== void 0) {
1016
1017
  throw new core.ConfigurationError("Give 'codec' or the deprecated 'decode', not both");
1017
1018
  }
1018
- const chosen = decode === void 0 ? codec ?? cloudEventsBinary : { encode: encodeEnvelope, decode };
1019
+ return codecSettings(
1020
+ decode === void 0 ? codec ?? cloudEventsBinary : { encode: encodeEnvelope, decode }
1021
+ );
1022
+ }
1023
+ function codecSettings(chosen) {
1019
1024
  if (chosen === cloudEventsBinary) {
1020
- return { encode: encodeEnvelope, decode: decodeEnvelope };
1025
+ return { codec: chosen, encode: encodeEnvelope, decode: decodeEnvelope };
1021
1026
  }
1022
1027
  return {
1028
+ codec: chosen,
1023
1029
  encode: chosen.encode,
1024
1030
  decode: (message) => decodeRabbitMqMessage(chosen, message)
1025
1031
  };
@@ -1397,6 +1403,8 @@ var RabbitMqTransport = class extends internal.SubscribingTransport {
1397
1403
  #live;
1398
1404
  #subscription;
1399
1405
  #closing;
1406
+ /** Set when the transport first connects, publishes or subscribes: its wire format is fixed from then on. */
1407
+ #used = false;
1400
1408
  constructor(options) {
1401
1409
  super();
1402
1410
  this.#settings = resolveSettings(options);
@@ -1427,6 +1435,7 @@ var RabbitMqTransport = class extends internal.SubscribingTransport {
1427
1435
  }
1428
1436
  /** Connects to the broker. Rejects when it is unreachable unless `failOnConnectError` is false. */
1429
1437
  async connect() {
1438
+ this.#used = true;
1430
1439
  await this.#supervisor.start(this.#settings.failOnConnectError);
1431
1440
  }
1432
1441
  /**
@@ -1440,7 +1449,27 @@ var RabbitMqTransport = class extends internal.SubscribingTransport {
1440
1449
  * @throws {UnroutableMessageError} no queue is bound to `destination` (unless `allowUnroutable`)
1441
1450
  * @throws whatever the `codec`'s `encode` throws, such as {@link RabbitMqUnencodableEventError}
1442
1451
  */
1452
+ /**
1453
+ * Checks the wire names a route or catalog declares and returns the function that makes the codec
1454
+ * write and read events under them. The bus calls it when it is created, before the transport
1455
+ * connects.
1456
+ *
1457
+ * @throws WireTypesUnsupportedError when the codec does not map type names (use `jsonEnvelope`),
1458
+ * WireTypeCollisionError when a name clashes with one of the codec's own `wireTypes`.
1459
+ */
1460
+ declareWireTypes(wireTypes) {
1461
+ if (this.#used) {
1462
+ throw new core.WireTypesUnsupportedError(
1463
+ `Transport '${this.name}' already started; declare wire names before it connects or subscribes`
1464
+ );
1465
+ }
1466
+ const codec = internal.withDeclaredWireTypes(this.#settings.codec, { wireTypes, transport: this.name });
1467
+ return () => {
1468
+ this.#settings = { ...this.#settings, ...codecSettings(codec) };
1469
+ };
1470
+ }
1443
1471
  async publish(envelope, destination) {
1472
+ this.#used = true;
1444
1473
  assertShortString("Destination (routing key)", destination);
1445
1474
  const { content, options } = this.#settings.encode(envelope);
1446
1475
  await this.#publishing.publish(this.#live?.publisher, {
@@ -1461,6 +1490,7 @@ var RabbitMqTransport = class extends internal.SubscribingTransport {
1461
1490
  onMessage,
1462
1491
  hooks
1463
1492
  }) {
1493
+ this.#used = true;
1464
1494
  if (this.#subscription !== void 0) {
1465
1495
  throw new core.ConfigurationError(
1466
1496
  `Transport '${this.name}' already has a subscription; create another transport for another consumer group`
@@ -1526,6 +1556,7 @@ var RabbitMqTransport = class extends internal.SubscribingTransport {
1526
1556
  * the connection's socket is destroyed instead of waiting for the broker.
1527
1557
  */
1528
1558
  async close(options = {}) {
1559
+ this.#used = true;
1529
1560
  const { signal } = options;
1530
1561
  const force = () => this.#supervisor.forceClose();
1531
1562
  if (signal?.aborted === true) {