Skip to content

Commit

Permalink
chore: remove Buffer usage for browser environments (#423)
Browse files Browse the repository at this point in the history
Closes #418
  • Loading branch information
curran authored Feb 5, 2024
1 parent cc856a0 commit 99c885e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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 @@ -21,6 +21,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 @@ -253,7 +254,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}`;
});
}

0 comments on commit 99c885e

Please sign in to comment.