Skip to content

Commit 7455a69

Browse files
authored
Accept Buffer inputs in isBase64() (#120)
1 parent 7b21565 commit 7455a69

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

create-or-update-files.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
function isBase64(str) {
2+
// Handle buffer inputs
3+
if (Buffer.isBuffer(str)) {
4+
str = str.toString("utf8");
5+
}
6+
27
var notBase64 = /[^A-Z0-9+\/=]/i;
3-
const isString = (typeof str === 'string' || str instanceof String);
8+
const isString = typeof str === "string" || str instanceof String;
49

510
if (!isString) {
611
let invalidType;
712
if (str === null) {
8-
invalidType = 'null';
13+
invalidType = "null";
914
} else {
1015
invalidType = typeof str;
11-
if (invalidType === 'object' && str.constructor && str.constructor.hasOwnProperty('name')) {
16+
if (
17+
invalidType === "object" &&
18+
str.constructor &&
19+
str.constructor.hasOwnProperty("name")
20+
) {
1221
invalidType = str.constructor.name;
1322
} else {
1423
invalidType = `a ${invalidType}`;
@@ -21,10 +30,12 @@ function isBase64(str) {
2130
if (!len || len % 4 !== 0 || notBase64.test(str)) {
2231
return false;
2332
}
24-
const firstPaddingChar = str.indexOf('=');
25-
return firstPaddingChar === -1 ||
33+
const firstPaddingChar = str.indexOf("=");
34+
return (
35+
firstPaddingChar === -1 ||
2636
firstPaddingChar === len - 1 ||
27-
(firstPaddingChar === len - 2 && str[len - 1] === '=');
37+
(firstPaddingChar === len - 2 && str[len - 1] === "=")
38+
);
2839
}
2940

3041
module.exports = function (octokit, opts) {

create-or-update-files.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,36 @@ test(`success (base64 encoded body)`, async () => {
235235
await expect(run(body)).resolves.toEqual(mockCommitList);
236236
});
237237

238+
test(`success (buffer body provided)`, async () => {
239+
const body = {
240+
...validRequest,
241+
changes: [
242+
{
243+
message: "Your commit message",
244+
files: {
245+
"test.md": Buffer.from(
246+
`# This is a test
247+
248+
I hope it works`,
249+
),
250+
"test2.md": {
251+
contents: `Something else`,
252+
},
253+
},
254+
},
255+
],
256+
};
257+
258+
mockGetRef(branch, `sha-${branch}`, true);
259+
mockCreateBlobFileOne();
260+
mockCreateBlobFileTwo();
261+
mockCreateTree(`sha-${branch}`);
262+
mockCommit(`sha-${branch}`);
263+
mockUpdateRef(branch);
264+
265+
await expect(run(body)).resolves.toEqual(mockCommitList);
266+
});
267+
238268
test(`success (committer details)`, async () => {
239269
const committer = {
240270
name: "Ashley Person",

0 commit comments

Comments
 (0)