Skip to content

Commit 99c885e

Browse files
authored
chore: remove Buffer usage for browser environments (#423)
Closes #418
1 parent cc856a0 commit 99c885e

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/base64-string.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Base64 string encoding and decoding module.
2+
// Uses Buffer for Node.js and btoa/atob for browser environments.
3+
// We use TextEncoder/TextDecoder for browser environments because
4+
// they can handle non-ASCII characters, unlike btoa/atob.
5+
6+
export const stringToBase64 =
7+
typeof Buffer !== 'undefined'
8+
? (str: string) => Buffer.from(str).toString('base64')
9+
: (str: string) =>
10+
btoa(
11+
new TextEncoder()
12+
.encode(str)
13+
.reduce((acc, byte) => acc + String.fromCharCode(byte), ''),
14+
);
15+
16+
export const base64ToString =
17+
typeof Buffer !== 'undefined'
18+
? (str: string) => Buffer.from(str, 'base64').toString()
19+
: (str: string) =>
20+
new TextDecoder().decode(Uint8Array.from(atob(str), (c) => c.charCodeAt(0)));

src/embed.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from './print/node-helpers';
2222
import { CommentNode, ElementNode, Node, ScriptNode, StyleNode } from './print/nodes';
2323
import { extractAttributes } from './lib/extractAttributes';
24+
import { base64ToString } from './base64-string';
2425

2526
const {
2627
builders: { group, hardline, softline, indent, dedent, literalline },
@@ -253,7 +254,7 @@ function getSnippedContent(node: Node) {
253254
const encodedContent = getAttributeTextValue(snippedTagContentAttribute, node);
254255

255256
if (encodedContent) {
256-
return Buffer.from(encodedContent, 'base64').toString('utf-8');
257+
return base64ToString(encodedContent);
257258
} else {
258259
return '';
259260
}

src/lib/snipTagContent.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { base64ToString, stringToBase64 } from '../base64-string';
2+
13
export const snippedTagContentAttribute = '✂prettier:content✂';
24

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

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

98100
export function unsnipContent(text: string): string {
99101
return text.replace(regex, (_, start, encodedContent) => {
100-
const content = Buffer.from(encodedContent, 'base64').toString('utf8');
102+
const content = base64ToString(encodedContent);
101103
return `${start}>${content}`;
102104
});
103105
}

0 commit comments

Comments
 (0)