Skip to content

Commit

Permalink
fix: do not expose underlying APIs, better error handling (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
menduz authored Aug 5, 2022
1 parent 93cb36f commit 9d8321d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
7 changes: 5 additions & 2 deletions etc/nats-component.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Emitter } from 'mitt';
import { IBaseComponent } from '@well-known-components/interfaces';
import { IConfigComponent } from '@well-known-components/interfaces';
import { ILoggerComponent } from '@well-known-components/interfaces';
import { JSONCodec } from 'nats';

// Warning: (ae-forgotten-export) The symbol "INatsComponent" needs to be exported by the entry point index.d.ts
//
Expand All @@ -20,7 +19,11 @@ export function createLocalNatsComponent(): Promise<INatsComponent & IBaseCompon
// @public
export function createNatsComponent(components: natsComponent.NeededComponents): Promise<INatsComponent & IBaseComponent>;

export { JSONCodec }
// @public
export function decodeJson<T = unknown>(a: Uint8Array): T;

// @public
export function encodeJson<T = unknown>(d: T): Uint8Array;

// @public
export type Subscription = {
Expand Down
19 changes: 19 additions & 0 deletions src/codecs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { JSONCodec } from "nats"

const jsonCodec = JSONCodec()

/**
* Encode JSON objects into Uint8Array and viceversa
* @public
*/
export function encodeJson<T = unknown>(d: T): Uint8Array {
return jsonCodec.encode(d)
}

/**
* Decode JSON objects into Uint8Array and viceversa
* @public
*/
export function decodeJson<T = unknown>(a: Uint8Array): T {
return jsonCodec.decode(a) as T
}
35 changes: 20 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,46 @@ import mitt from "mitt"
import { natsComponent, INatsComponent, NatsEvents, Subscription } from "./types"

export { createLocalNatsComponent } from "./test-component"
/**
* Encode/Decode JSON objects into Uint8Array and viceversa
* @public
*/
export { JSONCodec }
export { encodeJson, decodeJson } from './codecs'

export { Subscription }

/**
* Create a NATS component (https://nats.io/)
* Connect to a NATS node on start(), via the env variable "NATS_URL" or to "localhost:4222" by default
* Connect to a NATS node on start(), via the env variable "NATS_URL"
* @public
*/
export async function createNatsComponent(
components: natsComponent.NeededComponents
): Promise<INatsComponent & IBaseComponent> {
const { config, logs } = components
const logger = logs.getLogger("NATS")

// config
const natsUrl = (await config.getString("NATS_URL")) || "localhost:4222"
let natsConnection: NatsConnection
const natsUrl = await config.requireString("NATS_URL")
const logger = logs.getLogger(`NATS(${natsUrl})`)

let natsConnection: NatsConnection | undefined

const events = mitt<NatsEvents>()

function publish(topic: string, message?: Uint8Array): void {
if (!natsConnection) {
throw new Error("NATS component was not started yet")
}
natsConnection.publish(topic, message)
}

function subscribe(topic: string): Subscription {
if (!natsConnection) {
throw new Error("NATS component was not started yet")
}

const sub = natsConnection.subscribe(topic)
sub.closed
.then(() => {
logger.info(`subscription closed for ${topic}`)
logger.info(`subscription closed for topic`, { topic })
})
.catch((err: any) => {
logger.error(`subscription closed with an error ${err.toString()}`)
logger.error(`subscription closed with an error`, err)
})
return {
unsubscribe: () => sub.unsubscribe(),
Expand All @@ -62,9 +65,11 @@ export async function createNatsComponent(

async function stop() {
try {
await natsConnection.close()
} catch (error) {
logger.error(`An error occurred trying to close the connection to the NATS server: ${natsUrl}`)
if (natsConnection) {
await natsConnection.close()
}
} catch (error: any) {
logger.error(error)
}
}

Expand Down

0 comments on commit 9d8321d

Please sign in to comment.