Skip to content

Commit 200a5ed

Browse files
committed
fix: preserve any upload worker error
1 parent 6fe7037 commit 200a5ed

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

__tests__/commands/upload-download.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,42 @@ describe('upload + download commands (B2Simulator)', () => {
193193
expect(completed).toEqual(expect.arrayContaining(['a.txt', 'b.txt']))
194194
})
195195

196+
it('rethrows undefined glob upload failures', async () => {
197+
const srcDir = join(fx.workDir, 'undefined-failure-bundle')
198+
await mkdir(srcDir)
199+
for (const name of ['a.txt', 'b.txt', 'c.txt']) {
200+
await writeFile(join(srcDir, name), `payload-${name}`)
201+
}
202+
203+
const started: string[] = []
204+
const originalUpload = fx.bucket.upload.bind(fx.bucket)
205+
fx.bucket.upload = async (...args: Parameters<typeof fx.bucket.upload>) => {
206+
const fileName = args[0].fileName
207+
started.push(fileName)
208+
if (fileName === 'a.txt') {
209+
throw undefined
210+
}
211+
await new Promise((resolve) => setTimeout(resolve, 25))
212+
return await originalUpload(...args)
213+
}
214+
215+
let rejected = false
216+
try {
217+
await uploadCommand(fx.bucket, {
218+
...baseInputs(),
219+
source: srcDir,
220+
concurrency: 2,
221+
})
222+
} catch (error) {
223+
rejected = true
224+
expect(error).toBeUndefined()
225+
}
226+
227+
expect(rejected).toBe(true)
228+
expect(started).toHaveLength(2)
229+
expect(started).toEqual(expect.arrayContaining(['a.txt', 'b.txt']))
230+
})
231+
196232
it('fails when an upload glob matches no files and fail-on-empty is true', async () => {
197233
await expect(
198234
uploadCommand(fx.bucket, {

dist/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40706,9 +40706,10 @@ async function mapWithConcurrency(items, concurrency, mapper) {
4070640706
const results = new Array(items.length);
4070740707
let next = 0;
4070840708
let firstError;
40709+
let failed = false;
4070940710
async function worker() {
4071040711
while (true) {
40711-
if (firstError !== undefined)
40712+
if (failed)
4071240713
return;
4071340714
const index = next++;
4071440715
if (index >= items.length)
@@ -40717,14 +40718,17 @@ async function mapWithConcurrency(items, concurrency, mapper) {
4071740718
results[index] = await mapper(items[index]);
4071840719
}
4071940720
catch (error) {
40720-
firstError ??= error;
40721+
if (!failed) {
40722+
failed = true;
40723+
firstError = error;
40724+
}
4072140725
return;
4072240726
}
4072340727
}
4072440728
}
4072540729
const workerCount = Math.min(concurrency, items.length);
4072640730
await Promise.all(Array.from({ length: workerCount }, () => worker()));
40727-
if (firstError !== undefined)
40731+
if (failed)
4072840732
throw firstError;
4072940733
return results;
4073040734
}

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/upload.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,28 @@ async function mapWithConcurrency<T, U>(
9696
const results = new Array<U>(items.length)
9797
let next = 0
9898
let firstError: unknown
99+
let failed = false
99100

100101
async function worker(): Promise<void> {
101102
while (true) {
102-
if (firstError !== undefined) return
103+
if (failed) return
103104
const index = next++
104105
if (index >= items.length) return
105106
try {
106107
results[index] = await mapper(items[index] as T)
107108
} catch (error) {
108-
firstError ??= error
109+
if (!failed) {
110+
failed = true
111+
firstError = error
112+
}
109113
return
110114
}
111115
}
112116
}
113117

114118
const workerCount = Math.min(concurrency, items.length)
115119
await Promise.all(Array.from({ length: workerCount }, () => worker()))
116-
if (firstError !== undefined) throw firstError
120+
if (failed) throw firstError
117121
return results
118122
}
119123

0 commit comments

Comments
 (0)