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

[FIX] cross language compatible meta subject #372

Merged
merged 1 commit into from
Sep 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions nats-base-client/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export class Base64Codec {
static encode(bytes: string | Uint8Array): string {
if (typeof bytes === "string") {
return btoa(bytes);
}
const a = Array.from(bytes);
return btoa(String.fromCharCode(...a));
}

static decode(s: string, binary = false): Uint8Array | string {
const bin = atob(s);
if (!binary) {
return bin;
}
return Uint8Array.from(bin, (c) => c.charCodeAt(0));
}
}

export class Base64UrlCodec {
static encode(bytes: string | Uint8Array): string {
return Base64UrlCodec.toB64URLEncoding(Base64Codec.encode(bytes));
}

static decode(s: string, binary = false): Uint8Array | string {
return Base64Codec.decode(Base64UrlCodec.fromB64URLEncoding(s), binary);
}

static toB64URLEncoding(b64str: string): string {
return b64str
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
}

static fromB64URLEncoding(b64str: string): string {
// pads are % 4, but not necessary on decoding
return b64str
.replace(/_/g, "/")
.replace(/-/g, "+");
}
}
5 changes: 3 additions & 2 deletions nats-base-client/objectstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
StreamInfoRequestOptions,
} from "./types.ts";
import { validateBucket, validateKey } from "./kv.ts";
import { Base64UrlCodec } from "./base64.ts";
import { JSONCodec } from "./codec.ts";
import { nuid } from "./nuid.ts";
import { deferred } from "./util.ts";
Expand Down Expand Up @@ -247,7 +248,7 @@ export class ObjectStoreImpl implements ObjectStore {
return Promise.reject(error);
}

const meta = `$O.${this.name}.M.${obj}`;
const meta = this._metaSubject(obj);
try {
const m = await this.jsm.streams.getMessage(this.stream, {
last_by_subj: meta,
Expand Down Expand Up @@ -673,7 +674,7 @@ export class ObjectStoreImpl implements ObjectStore {
}

_metaSubject(n: string): string {
return `$O.${this.name}.M.${n}`;
return `$O.${this.name}.M.${Base64UrlCodec.encode(n)}`;
}

_metaSubjectAll(): string {
Expand Down
10 changes: 8 additions & 2 deletions tests/objectstore_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { assertRejects } from "https://deno.land/std@0.152.0/testing/asserts.ts"
import { equals } from "https://deno.land/std@0.152.0/bytes/mod.ts";
import { ObjectInfo, ObjectStoreMeta } from "../nats-base-client/types.ts";
import { SHA256 } from "../nats-base-client/sha256.js";
import { Base64UrlCodec } from "../nats-base-client/base64.ts";

function readableStreamFrom(data: Uint8Array): ReadableStream<Uint8Array> {
return new ReadableStream<Uint8Array>({
Expand Down Expand Up @@ -676,9 +677,14 @@ Deno.test("objectstore - sanitize", async () => {
const info = await os.status({
subjects_filter: ">",
});
assertEquals(info.streamInfo.state?.subjects!["$O.test.M.has_dots_here"], 1);
assertEquals(
info.streamInfo.state.subjects!["$O.test.M.the_spaces_are_here"],
info.streamInfo.state
?.subjects![`$O.test.M.${Base64UrlCodec.encode("has_dots_here")}`],
1,
);
assertEquals(
info.streamInfo.state
.subjects![`$O.test.M.${Base64UrlCodec.encode("the_spaces_are_here")}`],
1,
);

Expand Down