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

[DOC] added JSDoc so that the API for the client can be generated and made available #333

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions nats-base-client/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ import { ErrorCode, NatsError } from "./error.ts";
import { TD, TE } from "./encoders.ts";

export interface Codec<T> {
/**
* Encode T to an Uint8Array suitable for including in a message payload.
* @param d
*/
encode(d: T): Uint8Array;

/**
* Decode an Uint8Array from a message payload into a T
* @param a
*/
decode(a: Uint8Array): T;
}

/**
* Returns a {@link Codec} for encoding strings to a message payload
* and decoding message payloads into strings.
* @constructor
*/
export function StringCodec(): Codec<string> {
return {
encode(d: string): Uint8Array {
Expand All @@ -32,6 +46,13 @@ export function StringCodec(): Codec<string> {
};
}

/**
* Returns a {@link Codec} for encoding JavaScript object to JSON and
* serialize them to an Uint8Array, and conversely, from an
* Uint8Array to JSON to a JavaScript Object.
* @param reviver
* @constructor
*/
export function JSONCodec<T = unknown>(
reviver?: (this: unknown, key: string, value: unknown) => unknown,
): Codec<T> {
Expand Down
8 changes: 4 additions & 4 deletions nats-base-client/jsmdirect_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ export class DirectMsgImpl implements DirectMsg {
}

get subject(): string {
return this.header.get(DirectMsgHeaders.JsSubject);
return this.header.get(DirectMsgHeaders.Subject);
}

get seq(): number {
const v = this.header.get(DirectMsgHeaders.JsSequence);
const v = this.header.get(DirectMsgHeaders.Sequence);
return typeof v === "string" ? parseInt(v) : 0;
}

get time(): Date {
return new Date(this.header.get(DirectMsgHeaders.JsTimeStamp));
return new Date(this.header.get(DirectMsgHeaders.TimeStamp));
}

get stream(): string {
return this.header.get(DirectMsgHeaders.JsStream);
return this.header.get(DirectMsgHeaders.Stream);
}
}
16 changes: 16 additions & 0 deletions nats-base-client/jsutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,26 @@ export function defaultConsumer(
}, opts);
}

/**
* Converts the specified millis into Nanos
* @param millis
*/
export function nanos(millis: number): Nanos {
return millis * 1000000;
}

/**
* Convert the specified Nanos into millis
* @param ns
*/
export function millis(ns: Nanos) {
return Math.floor(ns / 1000000);
}

/**
* Returns true if the message is a flow control message
* @param msg
*/
export function isFlowControlMsg(msg: Msg): boolean {
if (msg.data.length > 0) {
return false;
Expand All @@ -76,6 +88,10 @@ export function isFlowControlMsg(msg: Msg): boolean {
return h.code >= 100 && h.code < 200;
}

/**
* Returns true if the message is a heart beat message
* @param msg
*/
export function isHeartbeatMsg(msg: Msg): boolean {
return isFlowControlMsg(msg) && msg.headers?.description === "Idle Heartbeat";
}
Expand Down
Loading