Skip to content

Accept Buffer inputs in isBase64() #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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