Skip to content

Commit 26d525a

Browse files
authored
Merge pull request #695 from dscho/fix-flaky-unzips
Fix flaky unzips
2 parents 0e497be + c3a1f03 commit 26d525a

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

__tests__/main.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ if (process.env.RUN_NETWORK_TESTS !== 'true') {
4545
env: {
4646
INPUT_REPOSITORY: 'git/git',
4747
INPUT_DEFINITIONID: '10',
48+
INPUT_ARTIFACT: 'sparse-20.04',
49+
INPUT_PATH: '.',
4850
INPUT_VERBOSE: 'true',
4951
INPUT_CACHE: 'true'
5052
}

dist/index.js

+19-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/downloader.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ export async function unzip(
7070
process.stderr.write(`Writing ${storeZipAs}\n`)
7171
res.pipe(fs.createWriteStream(storeZipAs)).on('error', reject)
7272
}
73+
74+
let writingEntry = false
75+
let wroteLastEntry = false
76+
const finish = (): void => {
77+
if (writingEntry || !wroteLastEntry) return
78+
if (bytesToExtract === 0) resolve(files)
79+
// eslint-disable-next-line prefer-promise-reject-errors
80+
else reject(`${bytesToExtract} bytes left to extract`)
81+
}
82+
7383
res
7484
.on('error', reject)
7585
.pipe(unzipper.Parse())
@@ -78,6 +88,8 @@ export async function unzip(
7888
process.stderr.write(
7989
`warning: skipping ${entry.path} because it does not start with ${stripPrefix}\n`
8090
)
91+
entry.autodrain()
92+
return
8193
}
8294
const entryPath = `${outputDirectory}/${entry.path.substring(
8395
stripPrefix.length
@@ -86,21 +98,23 @@ export async function unzip(
8698
mkdirp(entryPath.replace(/\/$/, ''))
8799
entry.autodrain()
88100
} else {
101+
writingEntry = true
89102
progress(entryPath)
90103
entry
91104
.pipe(fs.createWriteStream(`${entryPath}`))
105+
.on('error', reject)
92106
.on('finish', () => {
93107
bytesToExtract -= fs.statSync(entryPath).size
108+
writingEntry = false
109+
finish()
94110
})
95111
}
96112
})
97113
.on('error', reject)
98114
.on('finish', progress)
99115
.on('finish', () => {
100-
bytesToExtract === 0
101-
? resolve(files)
102-
: // eslint-disable-next-line prefer-promise-reject-errors
103-
reject(`${bytesToExtract} bytes left to extract`)
116+
wroteLastEntry = true
117+
finish()
104118
})
105119
}
106120
if (url.startsWith('file:')) {

0 commit comments

Comments
 (0)