@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.d.cts CHANGED
@@ -344,6 +344,12 @@ type KafkaEncodedRecord = KafkaOutgoingMessage & {
344
344
  interface KafkaCodec {
345
345
  readonly encode: (envelope: EventEnvelope) => KafkaEncodedRecord;
346
346
  readonly decode: KafkaMessageDecoder;
347
+ /**
348
+ * Returns this codec with more wire names for event types, which a route or catalog declares
349
+ * (`wireTypes` of `route(...)` or `defineCatalog`). Only a codec that maps type names has it
350
+ * ({@link jsonEnvelope}); declaring wire names for a transport whose codec lacks it fails at boot.
351
+ */
352
+ readonly withWireTypes?: (wireTypes: Readonly<Record<string, string>>) => KafkaCodec;
347
353
  }
348
354
  /**
349
355
  * CloudEvents 1.0 binary content mode, the default: the value is the JSON
@@ -578,6 +584,15 @@ declare class KafkaTransport extends SubscribingTransport {
578
584
  isReady(): boolean;
579
585
  /** Connects the producer. Called by the bus on start; `publish` also connects on demand. */
580
586
  connect(): Promise<void>;
587
+ /**
588
+ * Checks the wire names a route or catalog declares and returns the function that makes the codec
589
+ * write and read events under them. The bus calls it when it is created, before the transport
590
+ * connects.
591
+ *
592
+ * @throws WireTypesUnsupportedError when the codec does not map type names (use `jsonEnvelope`),
593
+ * WireTypeCollisionError when a name clashes with one of the codec's own `wireTypes`.
594
+ */
595
+ declareWireTypes(wireTypes: Readonly<Record<string, string>>): () => void;
581
596
  publish(envelope: EventEnvelope, destination: string): Promise<void>;
582
597
  /**
583
598
  * Starts the consumer and resolves once it joined its group, so a start-up
package/dist/index.d.ts CHANGED
@@ -344,6 +344,12 @@ type KafkaEncodedRecord = KafkaOutgoingMessage & {
344
344
  interface KafkaCodec {
345
345
  readonly encode: (envelope: EventEnvelope) => KafkaEncodedRecord;
346
346
  readonly decode: KafkaMessageDecoder;
347
+ /**
348
+ * Returns this codec with more wire names for event types, which a route or catalog declares
349
+ * (`wireTypes` of `route(...)` or `defineCatalog`). Only a codec that maps type names has it
350
+ * ({@link jsonEnvelope}); declaring wire names for a transport whose codec lacks it fails at boot.
351
+ */
352
+ readonly withWireTypes?: (wireTypes: Readonly<Record<string, string>>) => KafkaCodec;
347
353
  }
348
354
  /**
349
355
  * CloudEvents 1.0 binary content mode, the default: the value is the JSON
@@ -578,6 +584,15 @@ declare class KafkaTransport extends SubscribingTransport {
578
584
  isReady(): boolean;
579
585
  /** Connects the producer. Called by the bus on start; `publish` also connects on demand. */
580
586
  connect(): Promise<void>;
587
+ /**
588
+ * Checks the wire names a route or catalog declares and returns the function that makes the codec
589
+ * write and read events under them. The bus calls it when it is created, before the transport
590
+ * connects.
591
+ *
592
+ * @throws WireTypesUnsupportedError when the codec does not map type names (use `jsonEnvelope`),
593
+ * WireTypeCollisionError when a name clashes with one of the codec's own `wireTypes`.
594
+ */
595
+ declareWireTypes(wireTypes: Readonly<Record<string, string>>): () => void;
581
596
  publish(envelope: EventEnvelope, destination: string): Promise<void>;
582
597
  /**
583
598
  * Starts the consumer and resolves once it joined its group, so a start-up
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { RelayBusError, NonRetryableError, assertEventType, ConfigurationError, toErrorInfo, systemClock } from '@relaybus/core';
2
- import { createCloudEventReaders, DecodedEventReader, mediaTypeOf, isJsonObject, observeRejection, createJsonEnvelopeFormat, SubscribingTransport, writeStructuredCloudEvent, schedule, BoundedInteger, PositiveInteger, ExponentialBackoff, Milliseconds } from '@relaybus/core/internal';
1
+ import { RelayBusError, NonRetryableError, assertEventType, ConfigurationError, toErrorInfo, systemClock, WireTypesUnsupportedError } from '@relaybus/core';
2
+ import { createCloudEventReaders, DecodedEventReader, mediaTypeOf, isJsonObject, observeRejection, createJsonEnvelopeFormat, mergeWireTypes, SubscribingTransport, writeStructuredCloudEvent, schedule, BoundedInteger, withDeclaredWireTypes, PositiveInteger, ExponentialBackoff, Milliseconds } from '@relaybus/core/internal';
3
3
  export { fromHeader, fromPayloadField } from '@relaybus/core/internal';
4
4
 
5
5
  // src/errors.ts
@@ -441,6 +441,7 @@ function jsonEnvelope(mapping) {
441
441
  const ownHeaders = (envelope) => mapping.id === RECORD_POSITION ? { ...contentType, [EVENT_ID_HEADER]: envelope.id } : contentType;
442
442
  const recordKey = resolveRecordKey(mapping.recordKey);
443
443
  return {
444
+ withWireTypes: (declared) => jsonEnvelope({ ...mapping, wireTypes: mergeWireTypes(mapping, declared) }),
444
445
  encode: (envelope) => encodeJson(envelope, { format, stampsTime, ownHeaders, recordKey }),
445
446
  decode: (record) => decodeJson(record, format)
446
447
  };
@@ -862,6 +863,18 @@ function assertCanSubscribe(state, {
862
863
  }
863
864
  }
864
865
  }
866
+ function withDeclaredWireTypeSettings(settings, {
867
+ wireTypes,
868
+ started
869
+ }) {
870
+ if (started) {
871
+ throw new WireTypesUnsupportedError(
872
+ `Transport '${settings.name}' already started; declare wire names before it connects or subscribes`
873
+ );
874
+ }
875
+ const codec = withDeclaredWireTypes(settings.codec, { wireTypes, transport: settings.name });
876
+ return { ...settings, codec };
877
+ }
865
878
 
866
879
  // src/kafka-records.ts
867
880
  function decodeRecord(codec, { topic, partition, message }) {
@@ -995,6 +1008,21 @@ var KafkaTransport = class extends SubscribingTransport {
995
1008
  async connect() {
996
1009
  await this.#producer.connected();
997
1010
  }
1011
+ /**
1012
+ * Checks the wire names a route or catalog declares and returns the function that makes the codec
1013
+ * write and read events under them. The bus calls it when it is created, before the transport
1014
+ * connects.
1015
+ *
1016
+ * @throws WireTypesUnsupportedError when the codec does not map type names (use `jsonEnvelope`),
1017
+ * WireTypeCollisionError when a name clashes with one of the codec's own `wireTypes`.
1018
+ */
1019
+ declareWireTypes(wireTypes) {
1020
+ const settings = withDeclaredWireTypeSettings(this.#settings, {
1021
+ wireTypes,
1022
+ started: this.#state !== "idle" || this.#producer.pending !== void 0
1023
+ });
1024
+ return () => void (this.#settings = settings);
1025
+ }
998
1026
  async publish(envelope, destination) {
999
1027
  const producer = await this.#producer.connected();
1000
1028
  await this.#producer.send(producer, {