@relaybus/kafka 0.4.1 → 1.0.0-next.1

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
@@ -442,6 +442,7 @@ function jsonEnvelope(mapping) {
442
442
  const ownHeaders = (envelope) => mapping.id === RECORD_POSITION ? { ...contentType, [EVENT_ID_HEADER]: envelope.id } : contentType;
443
443
  const recordKey = resolveRecordKey(mapping.recordKey);
444
444
  return {
445
+ withWireTypes: (declared) => jsonEnvelope({ ...mapping, wireTypes: internal.mergeWireTypes(mapping, declared) }),
445
446
  encode: (envelope) => encodeJson(envelope, { format, stampsTime, ownHeaders, recordKey }),
446
447
  decode: (record) => decodeJson(record, format)
447
448
  };
@@ -863,6 +864,18 @@ function assertCanSubscribe(state, {
863
864
  }
864
865
  }
865
866
  }
867
+ function withDeclaredWireTypeSettings(settings, {
868
+ wireTypes,
869
+ started
870
+ }) {
871
+ if (started) {
872
+ throw new core.WireTypesUnsupportedError(
873
+ `Transport '${settings.name}' already started; declare wire names before it connects or subscribes`
874
+ );
875
+ }
876
+ const codec = internal.withDeclaredWireTypes(settings.codec, { wireTypes, transport: settings.name });
877
+ return { ...settings, codec };
878
+ }
866
879
 
867
880
  // src/kafka-records.ts
868
881
  function decodeRecord(codec, { topic, partition, message }) {
@@ -996,6 +1009,21 @@ var KafkaTransport = class extends internal.SubscribingTransport {
996
1009
  async connect() {
997
1010
  await this.#producer.connected();
998
1011
  }
1012
+ /**
1013
+ * Checks the wire names a route or catalog declares and returns the function that makes the codec
1014
+ * write and read events under them. The bus calls it when it is created, before the transport
1015
+ * connects.
1016
+ *
1017
+ * @throws WireTypesUnsupportedError when the codec does not map type names (use `jsonEnvelope`),
1018
+ * WireTypeCollisionError when a name clashes with one of the codec's own `wireTypes`.
1019
+ */
1020
+ declareWireTypes(wireTypes) {
1021
+ const settings = withDeclaredWireTypeSettings(this.#settings, {
1022
+ wireTypes,
1023
+ started: this.#state !== "idle" || this.#producer.pending !== void 0
1024
+ });
1025
+ return () => void (this.#settings = settings);
1026
+ }
999
1027
  async publish(envelope, destination) {
1000
1028
  const producer = await this.#producer.connected();
1001
1029
  await this.#producer.send(producer, {