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

Remove Buffer usage for Browser Environments #423

Merged
merged 4 commits into from
Feb 5, 2024
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
20 changes: 20 additions & 0 deletions src/base64-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Base64 string encoding and decoding module.
// Uses Buffer for Node.js and btoa/atob for browser environments.
// We use TextEncoder/TextDecoder for browser environments because
// they can handle non-ASCII characters, unlike btoa/atob.

export const stringToBase64 =
typeof Buffer !== 'undefined'
? (str: string) => Buffer.from(str).toString('base64')
: (str: string) =>
btoa(
new TextEncoder()
.encode(str)
.reduce((acc, byte) => acc + String.fromCharCode(byte), ''),
);

export const base64ToString =
typeof Buffer !== 'undefined'
? (str: string) => Buffer.from(str, 'base64').toString()
: (str: string) =>
new TextDecoder().decode(Uint8Array.from(atob(str), (c) => c.charCodeAt(0)));
3 changes: 2 additions & 1 deletion src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from './print/node-helpers';
import { CommentNode, ElementNode, Node, ScriptNode, StyleNode } from './print/nodes';
import { extractAttributes } from './lib/extractAttributes';
import { base64ToString } from './base64-string';

const {
builders: { group, hardline, softline, indent, dedent, literalline },
Expand Down Expand Up @@ -252,7 +253,7 @@ function getSnippedContent(node: Node) {
const encodedContent = getAttributeTextValue(snippedTagContentAttribute, node);

if (encodedContent) {
return Buffer.from(encodedContent, 'base64').toString('utf-8');
return base64ToString(encodedContent);
} else {
return '';
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/snipTagContent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { base64ToString, stringToBase64 } from '../base64-string';

export const snippedTagContentAttribute = '✂prettier:content✂';

const scriptRegex =
Expand Down Expand Up @@ -42,7 +44,7 @@ export function snipScriptAndStyleTagContent(source: string): string {
if (match.startsWith('<!--') || withinOtherSpan(index)) {
return match;
}
const encodedContent = Buffer.from(content).toString('base64');
const encodedContent = stringToBase64(content);
const newContent = `<${tagName}${attributes} ${snippedTagContentAttribute}="${encodedContent}">${placeholder}</${tagName}>`;

// Adjust the spans because the source now has a different content length
Expand Down Expand Up @@ -97,7 +99,7 @@ const regex = /(<\w+.*?)\s*✂prettier:content✂="(.*?)">.*?(?=<\/)/gi;

export function unsnipContent(text: string): string {
return text.replace(regex, (_, start, encodedContent) => {
const content = Buffer.from(encodedContent, 'base64').toString('utf8');
const content = base64ToString(encodedContent);
return `${start}>${content}`;
});
}
Loading