generated from well-known-components/base-ts-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Juan Scolari
committed
Jun 17, 2022
1 parent
7399b65
commit e170cf1
Showing
8 changed files
with
9,471 additions
and
2,848 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,64 @@ | ||
/** | ||
* A function that does something | ||
* @public | ||
*/ | ||
export function example(){ | ||
return true | ||
} | ||
import { IBaseComponent } from "@well-known-components/interfaces" | ||
import { connect, NatsConnection } from "nats" | ||
import mitt from "mitt" | ||
import { natsComponent, INatsComponent, NatsEvents, Subscription } from "./types" | ||
|
||
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" | ||
const natsConfig = { servers: `${natsUrl}` } | ||
let natsConnection: NatsConnection | ||
|
||
const events = mitt<NatsEvents>() | ||
|
||
function publish(topic: string, message?: Uint8Array): void { | ||
natsConnection.publish(topic, message) | ||
} | ||
|
||
function subscribe(topic: string): Subscription { | ||
const sub = natsConnection.subscribe(topic) | ||
sub.closed | ||
.then(() => { | ||
logger.info(`subscription closed for ${topic}`) | ||
}) | ||
.catch((err) => { | ||
logger.error(`subscription closed with an error ${err.message}`) | ||
}) | ||
return { | ||
unsubscribe: () => sub.unsubscribe(), | ||
generator: sub, | ||
} | ||
} | ||
|
||
async function start() { | ||
try { | ||
natsConnection = await connect(natsConfig) | ||
events.emit("connected") | ||
logger.info(`Connected to NATS: ${natsUrl}`) | ||
} catch (error) { | ||
logger.error(`An error occurred trying to connect to the NATS server: ${natsUrl}`) | ||
throw error | ||
} | ||
} | ||
|
||
async function stop() { | ||
try { | ||
await natsConnection.close() | ||
} catch (error) { | ||
logger.error(`An error occurred trying to close the connection to the NATS server: ${natsUrl}`) | ||
} | ||
} | ||
|
||
return { | ||
publish, | ||
subscribe, | ||
start, | ||
stop, | ||
events, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { IBaseComponent, IConfigComponent } from "@well-known-components/interfaces" | ||
const { connect } = require("mock-nats-client") | ||
import { natsComponent, INatsComponent, Subscription, NatsEvents } from "./types" | ||
import mitt from "mitt" | ||
|
||
export async function createLocalNatsComponent( | ||
components: natsComponent.NeededComponents | ||
): Promise<INatsComponent & IBaseComponent> { | ||
const events = mitt<NatsEvents>() | ||
const client = connect({ preserveBuffers: true }) | ||
|
||
function publish(topic: string, message: any): void { | ||
message ? client.publish(topic, message) : client.publish(topic, []) | ||
} | ||
|
||
function subscribe(topic: string): Subscription { | ||
const sub = client.subscribe(topic) | ||
return { | ||
unsubscribe: () => client.unsubscribe(sub), | ||
generator: sub, | ||
} | ||
} | ||
|
||
async function start() { | ||
events.emit("connected") | ||
} | ||
|
||
async function stop() {} | ||
|
||
return { | ||
publish, | ||
subscribe, | ||
start, | ||
stop, | ||
events, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { IConfigComponent, ILoggerComponent } from "@well-known-components/interfaces" | ||
import { Emitter } from "mitt" | ||
|
||
export namespace natsComponent { | ||
export type NeededComponents = { | ||
logs: ILoggerComponent | ||
config: IConfigComponent | ||
} | ||
} | ||
|
||
export type NatsMsg = { | ||
subject: string | ||
data: Uint8Array | ||
} | ||
|
||
export type Subscription = { | ||
generator: AsyncIterable<NatsMsg> | ||
unsubscribe: () => void | ||
} | ||
|
||
export type NatsEvents = { | ||
connected: void | ||
} | ||
|
||
export type INatsComponent = { | ||
publish(topic: string, message?: Uint8Array): void | ||
subscribe(topic: string): Subscription | ||
|
||
start(): Promise<void> | ||
stop(): Promise<void> | ||
|
||
events: Emitter<NatsEvents> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { test } from "./components" | ||
|
||
test("smoke test", function ({ components }) { | ||
it("smoke test", async () => { | ||
const { nats } = components | ||
expect(nats).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This file is the "test-environment" analogous for src/components.ts | ||
// Here we define the test components to be used in the testing environment | ||
|
||
import { ILoggerComponent } from "@well-known-components/interfaces" | ||
import { createRunner } from "@well-known-components/test-helpers" | ||
import { createConfigComponent } from "@well-known-components/env-config-provider" | ||
import { INatsComponent, natsComponent } from "../src/types" | ||
import { createLocalNatsComponent } from "../src/test-component" | ||
|
||
export type TestComponents = natsComponent.NeededComponents & { nats: INatsComponent } | ||
|
||
export const logger = { | ||
log: jest.fn(), | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
} | ||
function createTestConsoleLogComponent(): ILoggerComponent { | ||
return { | ||
getLogger: () => logger, | ||
} | ||
} | ||
|
||
/** | ||
* Behaves like Jest "describe" function, used to describe a test for a | ||
* use case, it creates a whole new program and components to run an | ||
* isolated test. | ||
* | ||
* State is persistent within the steps of the test. | ||
*/ | ||
export const test = createRunner<TestComponents>({ | ||
async main({ startComponents }) { | ||
await startComponents() | ||
}, | ||
async initComponents(): Promise<TestComponents> { | ||
const config = createConfigComponent(process.env) | ||
|
||
const logs = createTestConsoleLogComponent() | ||
|
||
const nats = await createLocalNatsComponent({ config, logs }) | ||
|
||
return { | ||
config, | ||
logs, | ||
nats, | ||
} | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.