Skip to content

Commit 3ad04cd

Browse files
committed
Refactor callback function to pass to https.get()
This prepares for the change where we might need to cache the `.zip` file instead of the files because we want to write into a non-empty output directory. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 952824a commit 3ad04cd

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/downloader.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,32 @@ async function unzip(
6363
mkdirp(outputDirectory)
6464

6565
return new Promise<string[]>((resolve, reject) => {
66-
https
67-
.get(url, (res: Readable): void => {
68-
res
69-
.on('error', reject)
70-
.pipe(unzipper.Parse())
71-
.on('entry', entry => {
72-
if (!entry.path.startsWith(stripPrefix)) {
73-
process.stderr.write(
74-
`warning: skipping ${entry.path} because it does not start with ${stripPrefix}\n`
75-
)
76-
}
77-
const entryPath = `${outputDirectory}/${entry.path.substring(
78-
stripPrefix.length
79-
)}`
80-
if (entryPath.endsWith('/')) {
81-
mkdirp(entryPath.replace(/\/$/, ''))
82-
entry.autodrain()
83-
} else {
84-
progress(entryPath)
85-
entry.pipe(fs.createWriteStream(`${entryPath}`))
86-
}
87-
})
88-
.on('error', reject)
89-
.on('finish', progress)
90-
.on('finish', () => resolve(files))
91-
})
92-
.on('error', reject)
66+
const callback = (res: Readable): void => {
67+
res
68+
.on('error', reject)
69+
.pipe(unzipper.Parse())
70+
.on('entry', entry => {
71+
if (!entry.path.startsWith(stripPrefix)) {
72+
process.stderr.write(
73+
`warning: skipping ${entry.path} because it does not start with ${stripPrefix}\n`
74+
)
75+
}
76+
const entryPath = `${outputDirectory}/${entry.path.substring(
77+
stripPrefix.length
78+
)}`
79+
if (entryPath.endsWith('/')) {
80+
mkdirp(entryPath.replace(/\/$/, ''))
81+
entry.autodrain()
82+
} else {
83+
progress(entryPath)
84+
entry.pipe(fs.createWriteStream(`${entryPath}`))
85+
}
86+
})
87+
.on('error', reject)
88+
.on('finish', progress)
89+
.on('finish', () => resolve(files))
90+
}
91+
https.get(url, callback).on('error', reject)
9392
})
9493
}
9594

0 commit comments

Comments
 (0)