Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,18 @@ export class Toggle extends Hookified {
public getOrgIdFromPublicKey(publicKey: string): string | undefined {
try {
const keyWithoutPrefix = publicKey.replace(/^public_/, "");
// Browsers / modern runtimes expose `atob`; fall back to Node's
// `Buffer` without depending on @types/node globally.
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() ?? "");
Comment on lines +519 to +528

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() ?? "";

const [orgId] = decoded.split(":");
const isValidOrgId = /^[a-zA-Z0-9_-]+$/.test(orgId);
return isValidOrgId ? orgId : undefined;
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"types": ["node"],
"outDir": "./dist",
"rootDir": "./src",
"resolveJsonModule": true,
Expand Down
Loading