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

Conversation

tothandras
Copy link
Contributor

@tothandras tothandras commented Sep 21, 2022

I'm opening this PR mainly to open a discussion around language cross compatibility (with Go). This would be a breaking change.
https://github.com/nats-io/nats.go/blob/f4102422df7df83b2032318fd14a248c45f113e1/object.go#L442

also related: nats-io/nats.py#331 (review)

@aricart
Copy link
Member

aricart commented Sep 21, 2022

Yes, this is a bug in the javascript client, the proposed fix doesn't work because Buffer is only available in the node.js ecosystem.

@tothandras
Copy link
Contributor Author

oh, indeed! https://deno.land/std/encoding/base64url.ts should work, I'll replace the Buffer and try it out as well.

@aricart
Copy link
Member

aricart commented Sep 21, 2022

the solution has to work on a browser where std/encoding is not available.

@aricart
Copy link
Member

aricart commented Sep 21, 2022

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;
    }
    const bytes = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; i++) {
      bytes[i] = bin.charCodeAt(i);
    }
    return bytes;
  }
}

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 {
    b64str = b64str.replace(/=/g, "");
    b64str = b64str.replace(/\+/g, "-");
    return b64str.replace(/\//g, "_");
  }

  static fromB64URLEncoding(b64str: string): string {
    // pads are % 4, but not necessary on decoding
    b64str = b64str.replace(/_/g, "/");
    b64str = b64str.replace(/-/g, "+");
    return b64str;
  }
}

@aricart
Copy link
Member

aricart commented Sep 21, 2022

I'll put the above somewhere conventient

@tothandras
Copy link
Contributor Author

@aricart Something like this b4cd346?

Copy link
Member

@aricart aricart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aricart
Copy link
Member

aricart commented Sep 27, 2022

@tothandras thanks for the contribution!

@aricart aricart merged commit 6bdf6ff into nats-io:main Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants