diff --git a/create-or-update-files.js b/create-or-update-files.js index 0eef604..838597f 100644 --- a/create-or-update-files.js +++ b/create-or-update-files.js @@ -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}`; @@ -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) { diff --git a/create-or-update-files.test.js b/create-or-update-files.test.js index 9d63692..69ffe07 100644 --- a/create-or-update-files.test.js +++ b/create-or-update-files.test.js @@ -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",