Skip to content

Commit 4ad7b7e

Browse files
authored
fix: make it work in browsers (#30)
1 parent 207dd65 commit 4ad7b7e

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/utils.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ const isNode =
1515
globalThis.process.release &&
1616
globalThis.process.release.name;
1717

18-
function browserUtf8ToBase64(data: string) {
19-
return btoa(unescape(encodeURIComponent(data)));
20-
}
21-
22-
function browserBase64ToUtf8(data: string) {
23-
return decodeURIComponent(escape(atob(data)));
24-
}
25-
2618
function nodeUtf8ToBase64(data: string) {
2719
return Buffer.from(data, "utf-8").toString("base64");
2820
}
@@ -31,5 +23,35 @@ function nodeBase64ToUtf8(data: string) {
3123
return Buffer.from(data, "base64").toString("utf-8");
3224
}
3325

26+
// browserUtf8ToBase64 & browserBase64ToUtf8
27+
// (c) Brandon Rylow - CC BY-SA 4.0
28+
// https://stackoverflow.com/a/30106551/206879
29+
function browserUtf8ToBase64(data: string) {
30+
// first we use encodeURIComponent to get percent-encoded UTF-8,
31+
// then we convert the percent encodings into raw bytes which
32+
// can be fed into btoa.
33+
return btoa(
34+
encodeURIComponent(data).replace(
35+
/%([0-9A-F]{2})/g,
36+
function toSolidBytes(_match, p1) {
37+
// @ts-expect-error - we know what we are doing here
38+
return String.fromCharCode("0x" + p1);
39+
}
40+
)
41+
);
42+
}
43+
44+
function browserBase64ToUtf8(data: string) {
45+
// Going backwards: from bytestream, to percent-encoding, to original string.
46+
return decodeURIComponent(
47+
atob(data)
48+
.split("")
49+
.map(function (c) {
50+
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
51+
})
52+
.join("")
53+
);
54+
}
55+
3456
export const utf8ToBase64 = isNode ? nodeUtf8ToBase64 : browserUtf8ToBase64;
3557
export const base64ToUtf8 = isNode ? nodeBase64ToUtf8 : browserBase64ToUtf8;

test/create-or-update-text-file.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,26 @@ describe("README usage examples", () => {
290290
expect(data).toStrictEqual({ ok: true });
291291
});
292292
});
293+
294+
// TODO: add test for browsers
295+
// https://github.com/octokit/plugin-create-or-update-text-file.js/issues/15#issuecomment-840734343
296+
// example response
297+
//
298+
// {
299+
// "name": "update-prettier.yml",
300+
// "path": ".github/workflows/update-prettier.yml",
301+
// "sha": "c5ce51905f4941cfce3ee663113f4dbda080570c",
302+
// "size": 650,
303+
// "url": "https://api.github.com/repos/wolfy1339/test-repository/contents/.github/workflows/update-prettier.yml?ref=6c46223b6d3464bb5d17773d42ad557524e56d2c",
304+
// "html_url": "https://github.com/wolfy1339/test-repository/blob/6c46223b6d3464bb5d17773d42ad557524e56d2c/.github/workflows/update-prettier.yml",
305+
// "git_url": "https://api.github.com/repos/wolfy1339/test-repository/git/blobs/c5ce51905f4941cfce3ee663113f4dbda080570c",
306+
// "download_url": "https://raw.githubusercontent.com/wolfy1339/test-repository/6c46223b6d3464bb5d17773d42ad557524e56d2c/.github/workflows/update-prettier.yml",
307+
// "type": "file",
308+
// "content": "bmFtZTogVXBkYXRlIFByZXR0aWVyCiJvbiI6CiAgcHVzaDoKICAgIGJyYW5j\naGVzOgogICAgICAtIGRlcGVuZGFib3QvbnBtX2FuZF95YXJuL3ByZXR0aWVy\nLSoKam9iczoKICB1cGRhdGVfcHJldHRpZXI6CiAgICBydW5zLW9uOiB1YnVu\ndHUtbGF0ZXN0CiAgICBzdGVwczoKICAgICAgLSB1c2VzOiBhY3Rpb25zL2No\nZWNrb3V0QHYyCiAgICAgIC0gdXNlczogYWN0aW9ucy9zZXR1cC1ub2RlQHYy\nCiAgICAgICAgd2l0aDoKICAgICAgICAgIG5vZGUtdmVyc2lvbjogMTIueAog\nICAgICAtIHJ1bjogbnBtIGNpCiAgICAgIC0gcnVuOiAibnBtIHJ1biBsaW50\nOmZpeCIKICAgICAgLSB1c2VzOiBncjJtL2NyZWF0ZS1vci11cGRhdGUtcHVs\nbC1yZXF1ZXN0LWFjdGlvbkB2MS54CiAgICAgICAgZW52OgogICAgICAgICAg\nR0lUSFVCX1RPS0VOOiAiJHt7IHNlY3JldHMuT0NUT0tJVEJPVF9QQVQgfX0i\nCiAgICAgICAgd2l0aDoKICAgICAgICAgIHRpdGxlOiBQcmV0dGllciB1cGRh\ndGVkCiAgICAgICAgICBib2R5OiBBbiB1cGRhdGUgdG8gcHJldHRpZXIgcmVx\ndWlyZWQgdXBkYXRlcyB0byB5b3VyIGNvZGUuCiAgICAgICAgICBicmFuY2g6\nICIke3sgZ2l0aHViLnJlZiB9fSIKICAgICAgICAgIGNvbW1pdC1tZXNzYWdl\nOiAic3R5bGU6IHByZXR0aWVyIgo=\n",
309+
// "encoding": "base64",
310+
// "_links": {
311+
// "self": "https://api.github.com/repos/wolfy1339/test-repository/contents/.github/workflows/update-prettier.yml?ref=6c46223b6d3464bb5d17773d42ad557524e56d2c",
312+
// "git": "https://api.github.com/repos/wolfy1339/test-repository/git/blobs/c5ce51905f4941cfce3ee663113f4dbda080570c",
313+
// "html": "https://github.com/wolfy1339/test-repository/blob/6c46223b6d3464bb5d17773d42ad557524e56d2c/.github/workflows/update-prettier.yml"
314+
// }
315+
// }

0 commit comments

Comments
 (0)