Skip to content

Commit 3c987ee

Browse files
authored
Merge pull request #168 from AthennaIO/develop
feat: download without saving file
2 parents 7466125 + 15e6e56 commit 3c987ee

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/common",
3-
"version": "5.19.0",
3+
"version": "5.21.0",
44
"description": "The Athenna common helpers to use in any Node.js ESM project.",
55
"license": "MIT",
66
"author": "João Lenon <[email protected]>",

src/helpers/Exec.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,30 @@ export class Exec extends Macroable {
214214
}
215215
}
216216

217+
public static download(url: string): Promise<any>
218+
public static download(
219+
url: string,
220+
options?: { path?: string }
221+
): Promise<File>
222+
217223
/**
218-
* Download an archive to a determined path.
224+
* Download the data of an URL.
219225
*/
220-
public static async download(path: string, url: string): Promise<File> {
226+
public static async download(url: string, options?: { path?: string }) {
221227
return new Promise((resolve, reject) => {
222228
const callback = response => {
223229
const data = new Transform()
224230

225231
response.on('data', chunk => data.push(chunk))
226232

227-
response.on('end', function () {
228-
resolve(new File(path, data.read()).loadSync())
233+
response.on('end', () => {
234+
if (options.path) {
235+
resolve(new File(options.path, data.read()).loadSync())
236+
237+
return
238+
}
239+
240+
resolve(data.read())
229241
})
230242

231243
response.on('error', error => reject(error))

tests/unit/helpers/ExecTest.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ export default class ExecTest {
220220

221221
@Test()
222222
public async shouldBeAbleToDownloadFiles({ assert }: Context) {
223-
const file = await Exec.download(
224-
Path.storage('downloads/node.pkg'),
225-
'https://nodejs.org/dist/v23.5.0/node-23.5.0.pkg'
226-
)
223+
const file = await Exec.download('https://nodejs.org/dist/v23.5.0/node-23.5.0.pkg', {
224+
path: Path.storage('downloads/node.pkg')
225+
})
227226

228227
assert.equal(file.base, 'node.pkg')
229228
assert.isTrue(await File.exists(file.path))

0 commit comments

Comments
 (0)