Skip to content

Commit

Permalink
Merge pull request #484 from nats-io/timestamp
Browse files Browse the repository at this point in the history
timestamp types were incorrectly tagged as Nanos rather than strings
  • Loading branch information
aricart committed Feb 21, 2023
2 parents dbf2668 + 708609a commit 2a085a1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
6 changes: 5 additions & 1 deletion nats-base-client/jsmdirect_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ export class DirectMsgImpl implements DirectMsg {
}

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

get timestamp(): string {
return this.header.get(DirectMsgHeaders.TimeStamp);
}

get stream(): string {
Expand Down
48 changes: 34 additions & 14 deletions nats-base-client/jsmstream_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,44 @@ export class StreamAPIImpl extends BaseApiClient implements StreamAPI {
}

export class StoredMsgImpl implements StoredMsg {
subject: string;
seq: number;
data: Uint8Array;
time: Date;
header: MsgHdrs;
_header?: MsgHdrs;
smr: StreamMsgResponse;
static jc?: Codec<unknown>;

constructor(smr: StreamMsgResponse) {
this.subject = smr.message.subject;
this.seq = smr.message.seq;
this.time = new Date(Date.parse(smr.message.time));
this.data = smr.message.data ? this._parse(smr.message.data) : Empty;
if (smr.message.hdrs) {
const hd = this._parse(smr.message.hdrs);
this.header = MsgHdrsImpl.decode(hd);
} else {
this.header = headers();
this.smr = smr;
}

get subject(): string {
return this.smr.message.subject;
}

get seq(): number {
return this.smr.message.seq;
}

get timestamp(): string {
return this.smr.message.time;
}

get time(): Date {
return new Date(Date.parse(this.timestamp));
}

get data(): Uint8Array {
return this.smr.message.data ? this._parse(this.smr.message.data) : Empty;
}

get header(): MsgHdrs {
if (!this._header) {
if (this.smr.message.hdrs) {
const hd = this._parse(this.smr.message.hdrs);
this._header = MsgHdrsImpl.decode(hd);
} else {
this._header = headers();
}
}
return this._header;
}

_parse(s: string): Uint8Array {
Expand Down
15 changes: 10 additions & 5 deletions nats-base-client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,11 @@ export interface StoredMsg {
*/
time: Date;

/**
* The raw ISO formatted date returned by the server
*/
timestamp: string;

/**
* Convenience method to parse the message payload as JSON. This method
* will throw an exception if there's a parsing error;
Expand Down Expand Up @@ -1760,9 +1765,9 @@ export interface StreamInfo extends ApiPaged {
*/
config: StreamConfig;
/**
* Timestamp when the stream was created
* The ISO Timestamp when the stream was created
*/
created: Nanos;
created: string;
/**
* Detail about the current State of the Stream
*/
Expand Down Expand Up @@ -2092,15 +2097,15 @@ export interface StreamState {
*/
"first_seq": number;
/**
* The timestamp of the first message in the Stream
* The ISO timestamp of the first message in the Stream
*/
"first_ts": number;
"first_ts": string;
/**
* Sequence number of the last message in the Stream
*/
"last_seq": number;
/**
* The timestamp of the last message in the Stream
* The ISO timestamp of the last message in the Stream
*/
"last_ts": string;
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/headers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ Deno.test("headers - codec", async () => {
});

Deno.test("headers - malformed headers", async () => {
const { ns, nc } = await setup({ debug: true }, { debug: true });
const { ns, nc } = await setup();
const nci = nc as NatsConnectionImpl;

type t = {
Expand Down

0 comments on commit 2a085a1

Please sign in to comment.