From bd846d02205d21aa199798351cb427bcae53ea4a Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 16 May 2023 11:03:46 -0700 Subject: [PATCH] fix: catch eexists in moveOperations promise (#206) --- lib/content/write.js | 25 ++++++++++++++----------- test/put.js | 9 +++++++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/content/write.js b/lib/content/write.js index 263e0f9..7146146 100644 --- a/lib/content/write.js +++ b/lib/content/write.js @@ -168,18 +168,21 @@ async function moveToDestination (tmp, cache, sri, opts) { moveOperations.set( destination, fs.mkdir(destDir, { recursive: true }) - .then(() => moveFile(tmp.target, destination, { overwrite: false })) + .then(async () => { + await moveFile(tmp.target, destination, { overwrite: false }) + tmp.moved = true + return tmp.moved + }) + .catch(err => { + if (!err.message.startsWith('The destination file exists')) { + throw Object.assign(err, { code: 'EEXIST' }) + } + }).finally(() => { + moveOperations.delete(destination) + }) + ) - try { - await moveOperations.get(destination) - tmp.moved = true - } catch (err) { - if (!err.message.startsWith('The destination file exists')) { - throw Object.assign(err, { code: 'EEXIST' }) - } - } finally { - moveOperations.delete(destination) - } + return moveOperations.get(destination) } function sizeError (expected, found) { diff --git a/test/put.js b/test/put.js index b16ede2..5769434 100644 --- a/test/put.js +++ b/test/put.js @@ -130,16 +130,21 @@ t.test('signals error if error writing to cache', async t => { t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write') }) -t.test('concurrent puts', async t => { +t.test('concurrent puts with identical content', async t => { const CACHE = t.testdir() await Promise.all([ put(CACHE, KEY, CONTENT), + put(CACHE, `${KEY}2`, CONTENT), put(CACHE, KEY, CONTENT), + put(CACHE, `${KEY}2`, CONTENT), put(CACHE, KEY, CONTENT), + put(CACHE, `${KEY}2`, CONTENT), put(CACHE, KEY, CONTENT), + put(CACHE, `${KEY}2`, CONTENT), put(CACHE, KEY, CONTENT), + put(CACHE, `${KEY}2`, CONTENT), put(CACHE, KEY, CONTENT), - put(CACHE, KEY, CONTENT), + put(CACHE, `${KEY}2`, CONTENT), ]) const tmpFiles = await fs.readdir(path.join(CACHE, 'tmp')) t.strictSame(tmpFiles, [], 'Nothing left in tmp')