From afaa428fec055258eaaffadd5433f2c717b32699 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:58 +0200 Subject: [PATCH] Fixed YAML index file parser failing on duplicate keys. --- src/methods/self.js | 4 ++-- src/save/save.js | 7 ++++++- src/sources/getIndex.js | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/methods/self.js b/src/methods/self.js index 3c0afe0..31895dd 100644 --- a/src/methods/self.js +++ b/src/methods/self.js @@ -1,7 +1,7 @@ 'use strict'; function curateComments(comments) { - return comments.map(comment => ({ + return comments.map((comment) => ({ id: comment.id, url: `https://reddit.com${comment.permalink}`, author: comment.author.name, @@ -31,7 +31,7 @@ async function getFullPost(postId, reddit) { .fetch(); } -async function self(host, originalPost, reddit) { +async function self(host, originalPost, { reddit }) { const post = await getFullPost(originalPost.id, reddit) || originalPost; const curatedComments = curateComments(post.comments); diff --git a/src/save/save.js b/src/save/save.js index 0c1465d..33e21f4 100644 --- a/src/save/save.js +++ b/src/save/save.js @@ -41,7 +41,12 @@ async function writeBufferToFile(target, buffer, item) { async function save(requestedFilepath, bufferOrBuffers, item) { const pathElements = getPathElements(requestedFilepath); - const buffers = [].concat(bufferOrBuffers); // allow for single stream argument + const buffers = [].concat(bufferOrBuffers).filter(Boolean); // allow for single stream argument + + if (buffers.length === 0) { + logger.warn(`Received undefined buffer (${item.url})`); + return null; + } await fs.ensureDir(pathElements.dir); diff --git a/src/sources/getIndex.js b/src/sources/getIndex.js index a58b751..b1e7187 100644 --- a/src/sources/getIndex.js +++ b/src/sources/getIndex.js @@ -13,9 +13,9 @@ async function getIndex(user) { try { const indexFile = await fs.readFile(indexFilePath, 'utf8'); - return yaml.safeLoad(indexFile); + return yaml.safeLoad(indexFile, { json: true }); // allow duplicate keys } catch (error) { - logger.info(`No index file found for '${user.name}' at '${indexFilePath}'`); + logger.info(`No index file found for '${user.name}' at '${indexFilePath}': ${error.message}`); return { profile: { image: null, description: null }, posts: [] }; }