Skip to content

Commit

Permalink
change(core): the JSONCodec and StringCodec utilities have been remov…
Browse files Browse the repository at this point in the history
…ed - Msg payloads can be read as string or JSON by using `string/json()` methods. For publishing, simply supply JSON.stringify() results as the payload.

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Oct 18, 2024
1 parent 2acd070 commit 26a071d
Show file tree
Hide file tree
Showing 51 changed files with 234 additions and 408 deletions.
2 changes: 1 addition & 1 deletion core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-29",
"version": "3.0.0-30",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-29",
"version": "3.0.0-30",
"files": [
"lib/",
"LICENSE",
Expand Down
50 changes: 0 additions & 50 deletions core/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
* limitations under the License.
*/

import { TD, TE } from "./encoders.ts";
import { ErrorCode, NatsError } from "./core.ts";

export interface Codec<T> {
/**
* Encode T to an Uint8Array suitable for including in a message payload.
Expand All @@ -29,50 +26,3 @@ export interface Codec<T> {
*/
decode(a: Uint8Array): T;
}

/**
* Returns a {@link Codec} for encoding strings to a message payload
* and decoding message payloads into strings.
*/
export function StringCodec(): Codec<string> {
return {
encode(d: string): Uint8Array {
return TE.encode(d);
},
decode(a: Uint8Array): string {
return TD.decode(a);
},
};
}

/**
* 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
*/
export function JSONCodec<T = unknown>(
reviver?: (this: unknown, key: string, value: unknown) => unknown,
): Codec<T> {
return {
encode(d: T): Uint8Array {
try {
if (d === undefined) {
// @ts-ignore: json will not handle undefined
d = null;
}
return TE.encode(JSON.stringify(d));
} catch (err) {
throw NatsError.errorForCode(ErrorCode.BadJson, err as Error);
}
},

decode(a: Uint8Array): T {
try {
return JSON.parse(TD.decode(a), reviver);
} catch (err) {
throw NatsError.errorForCode(ErrorCode.BadJson, err as Error);
}
},
};
}
1 change: 0 additions & 1 deletion core/src/internal_mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export {
usernamePasswordAuthenticator,
} from "./authenticator.ts";
export type { Codec } from "./codec.ts";
export { JSONCodec, StringCodec } from "./codec.ts";
export * from "./nkeys.ts";
export { QueuedIteratorImpl } from "./queued_iterator.ts";
export type { MsgArg, ParserEvent } from "./parser.ts";
Expand Down
2 changes: 0 additions & 2 deletions core/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export {
ErrorCode,
Events,
headers,
JSONCodec,
jwtAuthenticator,
Match,
Metric,
Expand All @@ -41,7 +40,6 @@ export {
Nuid,
nuid,
RequestStrategy,
StringCodec,
syncIterator,
tokenAuthenticator,
usernamePasswordAuthenticator,
Expand Down
8 changes: 3 additions & 5 deletions core/src/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import { MsgHdrsImpl } from "./headers.ts";
import type { MsgArg } from "./parser.ts";
import { Empty, TD } from "./encoders.ts";
import type { Codec } from "./codec.ts";
import { JSONCodec } from "./codec.ts";
import type {
Msg,
MsgHdrs,
Payload,
Publisher,
RequestInfo,
ReviverFn,
Expand All @@ -43,7 +42,6 @@ export class MsgImpl implements Msg {
_reply!: string;
_subject!: string;
publisher: Publisher;
static jc: Codec<unknown>;

constructor(msg: MsgArg, data: Uint8Array, publisher: Publisher) {
this._msg = msg;
Expand Down Expand Up @@ -90,7 +88,7 @@ export class MsgImpl implements Msg {

// eslint-ignore-next-line @typescript-eslint/no-explicit-any
respond(
data: Uint8Array = Empty,
data: Payload = Empty,
opts?: { headers?: MsgHdrs; reply?: string },
): boolean {
if (this.reply) {
Expand All @@ -108,7 +106,7 @@ export class MsgImpl implements Msg {
}

json<T = unknown>(reviver?: ReviverFn): T {
return JSONCodec<T>(reviver).decode(this.data);
return JSON.parse(this.string(), reviver);
}

string(): string {
Expand Down
2 changes: 1 addition & 1 deletion core/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is generated - do not edit
export const version = "3.0.0-29";
export const version = "3.0.0-30";
6 changes: 2 additions & 4 deletions core/tests/auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
jwtAuthenticator,
nkeyAuthenticator,
nkeys,
StringCodec,
tokenAuthenticator,
usernamePasswordAuthenticator,
} from "../src/internal_mod.ts";
Expand Down Expand Up @@ -487,8 +486,7 @@ Deno.test("auth - creds authenticator validation", () => {
`eyJ0eXAiOiJqd3QiLCJhbGciOiJlZDI1NTE5In0.eyJqdGkiOiJFU1VQS1NSNFhGR0pLN0FHUk5ZRjc0STVQNTZHMkFGWERYQ01CUUdHSklKUEVNUVhMSDJBIiwiaWF0IjoxNTQ0MjE3NzU3LCJpc3MiOiJBQ1pTV0JKNFNZSUxLN1FWREVMTzY0VlgzRUZXQjZDWENQTUVCVUtBMzZNSkpRUlBYR0VFUTJXSiIsInN1YiI6IlVBSDQyVUc2UFY1NTJQNVNXTFdUQlAzSDNTNUJIQVZDTzJJRUtFWFVBTkpYUjc1SjYzUlE1V002IiwidHlwZSI6InVzZXIiLCJuYXRzIjp7InB1YiI6e30sInN1YiI6e319fQ.kCR9Erm9zzux4G6M-V2bp7wKMKgnSNqMBACX05nwePRWQa37aO_yObbhcJWFGYjo1Ix-oepOkoyVLxOJeuD8Bw`;
const ukp = nkeys.createUser();
const upk = ukp.getPublicKey();
const sc = StringCodec();
const seed = sc.decode(ukp.getSeed());
const seed = new TextDecoder().decode(ukp.getSeed());

function creds(ajwt = "", aseed = ""): string {
return `-----BEGIN NATS USER JWT-----
Expand All @@ -513,7 +511,7 @@ Deno.test("auth - creds authenticator validation", () => {
tests.push([jwt, seed, true, "jwt and seed"]);

tests.forEach((v) => {
const d = sc.encode(creds(v[0], v[1]));
const d = new TextEncoder().encode(creds(v[0], v[1]));
try {
const auth = credsAuthenticator(d);
if (!v[2]) {
Expand Down
Loading

0 comments on commit 26a071d

Please sign in to comment.