From c8112ead79360d50dcdde87948971a83ae9fb4e5 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Wed, 28 Feb 2024 14:07:53 +0000 Subject: [PATCH 1/2] Accept Buffer inputs in isBase64() --- create-or-update-files.js | 5 +++++ create-or-update-files.test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/create-or-update-files.js b/create-or-update-files.js index 0eef604..d3eda7c 100644 --- a/create-or-update-files.js +++ b/create-or-update-files.js @@ -1,4 +1,9 @@ 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); diff --git a/create-or-update-files.test.js b/create-or-update-files.test.js index 9d63692..63e49ac 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", From 462a34cbe9fb9a302ee621980de66d25ee3c1dab Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Wed, 28 Feb 2024 14:09:56 +0000 Subject: [PATCH 2/2] Fix linting errors --- create-or-update-files.js | 18 ++++++++++++------ create-or-update-files.test.js | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/create-or-update-files.js b/create-or-update-files.js index d3eda7c..838597f 100644 --- a/create-or-update-files.js +++ b/create-or-update-files.js @@ -5,15 +5,19 @@ function isBase64(str) { } 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}`; @@ -26,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 63e49ac..69ffe07 100644 --- a/create-or-update-files.test.js +++ b/create-or-update-files.test.js @@ -245,7 +245,7 @@ test(`success (buffer body provided)`, async () => { "test.md": Buffer.from( `# This is a test -I hope it works` +I hope it works`, ), "test2.md": { contents: `Something else`,