Skip to content

Commit

Permalink
implement it
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Dec 24, 2021
1 parent a5f0bc4 commit 3c3bb76
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ node_modules
test/download
.DS_Store
coverage
dist
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -2,12 +2,13 @@
"name": "simpledownload",
"version": "2.0.0",
"description": "call with `url` and `filename`, then download the file",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git@github.com:alsotang/simpledownload.git"
"url": "https://github.com/alsotang/simpledownload.git"
},
"keywords": [
"download"
Expand All @@ -19,6 +20,7 @@
},
"homepage": "https://github.com/alsotang/simpledownload",
"devDependencies": {
"@types/node": "^17.0.4",
"typescript": "^4.5.4"
}
}
29 changes: 29 additions & 0 deletions src/index.ts
@@ -0,0 +1,29 @@
import fs from 'fs'
import http from 'http'

interface SimpledownloadOptions {
timeout?: number;
}

function simpledownload(url: string, localPath: string, options?: SimpledownloadOptions): Promise<void> {
return new Promise((resolve, reject) => {
const request = http.get(url, (response) => {
const file = fs.createWriteStream(localPath)
response.pipe(file)
file.on('finish', () => {
file.close()
resolve()
})
})
request.on('error', (err) => {
fs.unlink(localPath, () => {})
reject(err)
})
if (options && options.timeout) {
request.setTimeout(options.timeout)
}
})
}

export {simpledownload}
export default simpledownload
8 changes: 4 additions & 4 deletions tsconfig.json
Expand Up @@ -31,7 +31,7 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "types": ["node"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "resolveJsonModule": true, /* Enable importing .json files */
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
Expand All @@ -42,12 +42,12 @@
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */

/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
Expand Down

0 comments on commit 3c3bb76

Please sign in to comment.