Skip to content

Commit

Permalink
Accept Buffer inputs in isBase64() (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap authored Feb 28, 2024
1 parent 7b21565 commit 7455a69
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
23 changes: 17 additions & 6 deletions create-or-update-files.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
function isBase64(str) {
// Handle buffer inputs
if (Buffer.isBuffer(str)) {
str = str.toString("utf8");
}

var notBase64 = /[^A-Z0-9+\/=]/i;
const isString = (typeof str === 'string' || str instanceof String);
const isString = typeof str === "string" || str instanceof String;

if (!isString) {
let invalidType;
if (str === null) {
invalidType = 'null';
invalidType = "null";
} else {
invalidType = typeof str;
if (invalidType === 'object' && str.constructor && str.constructor.hasOwnProperty('name')) {
if (
invalidType === "object" &&
str.constructor &&
str.constructor.hasOwnProperty("name")
) {
invalidType = str.constructor.name;
} else {
invalidType = `a ${invalidType}`;
Expand All @@ -21,10 +30,12 @@ function isBase64(str) {
if (!len || len % 4 !== 0 || notBase64.test(str)) {
return false;
}
const firstPaddingChar = str.indexOf('=');
return firstPaddingChar === -1 ||
const firstPaddingChar = str.indexOf("=");
return (
firstPaddingChar === -1 ||
firstPaddingChar === len - 1 ||
(firstPaddingChar === len - 2 && str[len - 1] === '=');
(firstPaddingChar === len - 2 && str[len - 1] === "=")
);
}

module.exports = function (octokit, opts) {
Expand Down
30 changes: 30 additions & 0 deletions create-or-update-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,36 @@ test(`success (base64 encoded body)`, async () => {
await expect(run(body)).resolves.toEqual(mockCommitList);
});

test(`success (buffer body provided)`, async () => {
const body = {
...validRequest,
changes: [
{
message: "Your commit message",
files: {
"test.md": Buffer.from(
`# This is a test
I hope it works`,
),
"test2.md": {
contents: `Something else`,
},
},
},
],
};

mockGetRef(branch, `sha-${branch}`, true);
mockCreateBlobFileOne();
mockCreateBlobFileTwo();
mockCreateTree(`sha-${branch}`);
mockCommit(`sha-${branch}`);
mockUpdateRef(branch);

await expect(run(body)).resolves.toEqual(mockCommitList);
});

test(`success (committer details)`, async () => {
const committer = {
name: "Ashley Person",
Expand Down

0 comments on commit 7455a69

Please sign in to comment.