Skip to content

fix: handling buffer with node or browser#60

Merged
jaredwray merged 1 commit into
mainfrom
fix-handling-buffer-with-node-or-browser
Apr 9, 2026
Merged

fix: handling buffer with node or browser#60
jaredwray merged 1 commit into
mainfrom
fix-handling-buffer-with-node-or-browser

Conversation

@jaredwray

@jaredwray jaredwray commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Please check if the PR fulfills these requirements

  • Followed the Contributing guidelines and Code of Conduct
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
fix: handling buffer with node or browser

@jaredwray jaredwray merged commit 8477be9 into main Apr 9, 2026
6 checks passed
@jaredwray jaredwray deleted the fix-handling-buffer-with-node-or-browser branch April 9, 2026 22:14

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request removes the global Node.js type dependency by refactoring the getOrgIdFromPublicKey method to use globalThis.atob when available, falling back to a dynamically resolved Buffer. A review comment suggests a more robust implementation for the Buffer fallback to prevent runtime errors in environments where Buffer might be present but missing the from method.

Comment thread src/index.ts
Comment on lines +519 to +528
const nodeBuffer = (
globalThis as typeof globalThis & {
Buffer?: {
from(input: string, encoding: string): { toString(): string };
};
}
).Buffer;
const decoded = globalThis.atob
? globalThis.atob(keyWithoutPrefix)
: Buffer.from(keyWithoutPrefix, "base64").toString();
: (nodeBuffer?.from(keyWithoutPrefix, "base64").toString() ?? "");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current implementation for detecting and using Buffer is quite verbose and potentially unsafe. If globalThis.Buffer exists but does not have a from method (e.g., in certain polyfilled environments or very old Node.js versions), nodeBuffer?.from(...) will attempt to call undefined as a function, leading to a runtime error. A more robust and concise approach would be to use optional chaining on the method call itself and check the type of atob more strictly.

const decoded = typeof globalThis.atob === "function"
				? globalThis.atob(keyWithoutPrefix)
				: (globalThis as any).Buffer?.from?.(keyWithoutPrefix, "base64")?.toString() ?? "";

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.

1 participant