Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Parameter to Configure Custom Codec #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arinanto
Copy link

Add configuration option to use custom codec by implementing NATS codec interface.

Example usage:

// ...other imports omitted

// Custom codec (in this case CBOR based)
import CBOR from "cbor";
import { Codec } from "nats";

class CborCodec implements Codec<unknown> {
  encode(d: unknown): Uint8Array {
    return CBOR.encodeOne(d, { highWaterMark: 1049000 });
  }

  decode(a: Uint8Array): unknown {
    return CBOR.decodeFirstSync(a, { highWaterMark: 1049000 }) as unknown;
  }
}

// Module declaration
@Global()
@Module({
  imports: [
    NatsJetStreamTransport.registerAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          connectionOptions: {
            servers: "nats://broker.example.com:4222",
            codec: new CborCodec(),
          },
        };
      },
    }),
  ],
  providers: [NatsService],
  exports: [NatsService],
})
export class NatsGlobalModule {}

@kenguru33 kenguru33 self-assigned this May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants