{"id":18836753,"url":"https://github.com/andresusanto/easydl","last_synced_at":"2025-04-09T13:03:57.943Z","repository":{"id":38141976,"uuid":"266075286","full_name":"andresusanto/easydl","owner":"andresusanto","description":"Node.js file downloader with built-in parallel downloads and resume support","archived":false,"fork":false,"pushed_at":"2024-05-06T10:44:14.000Z","size":103,"stargazers_count":82,"open_issues_count":8,"forks_count":15,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T10:46:44.864Z","etag":null,"topics":["download","download-manager","downloader","downloading","easy-to-use","nodejs","parallel","resumable"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andresusanto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-22T09:46:56.000Z","updated_at":"2025-04-07T09:15:31.000Z","dependencies_parsed_at":"2024-05-03T09:48:32.959Z","dependency_job_id":"29d1383b-f60a-443f-b609-adaadf0ff82e","html_url":"https://github.com/andresusanto/easydl","commit_stats":{"total_commits":8,"total_committers":3,"mean_commits":"2.6666666666666665","dds":0.375,"last_synced_commit":"8fd0d19b14f2dff126eff980d9db4efa94578bf4"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andresusanto%2Feasydl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andresusanto%2Feasydl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andresusanto%2Feasydl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andresusanto%2Feasydl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andresusanto","download_url":"https://codeload.github.com/andresusanto/easydl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045230,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["download","download-manager","downloader","downloading","easy-to-use","nodejs","parallel","resumable"],"created_at":"2024-11-08T02:31:52.245Z","updated_at":"2025-04-09T13:03:57.916Z","avatar_url":"https://github.com/andresusanto.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyDl\n\n[![Install via NPM](https://nodei.co/npm/easydl.png)](https://www.npmjs.com/package/easydl)\n\nEasily download a file and save it to a local disk. It supports resuming previously downloaded files, multi-connection/parallel downloads, and retry on failure out of the box!\n\n### Features\n\n- Resumes previous downloads, even after the program has terminated.\n- Faster download speed with multiple concurrent connections.\n- Automatic retry on failure.\n- Supports HTTP redirects with redirect loop detection.\n- No native, 100% Javascript code with zero dependency.\n- Runs on Node.js, Electron, NW.js\n- Easy! Dead simple API, but highly configurable when you need it.\n\n## Install\n\n```bash\nnpm i -S easydl\n```\n\nor if you use `yarn`\n\n```bash\nyarn add easydl\n```\n\n## Quickstart\n\n`EasyDl` is an [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter), but it also provides Promise APIs for ease of use:\n\n```js\ntry {\n  const completed = await new EasyDl(\n    \"http://www.ovh.net/files/1Gio.dat\",\n    \"/tmp/1GB.zip\",\n    { connections: 10, maxRetry: 5 }\n  ).wait();\n  console.log(\"Downloaded?\", completed);\n} catch (err) {\n  console.log(\"[error]\", err);\n}\n```\n\nor if you prefer using the `Promise` chain:\n\n```js\nconst EasyDl = require(\"easydl\");\n\nnew EasyDl(\"http://www.ovh.net/files/10Gio.dat\", \"~/Downloads\")\n  .wait()\n  .then((completed) =\u003e {\n    console.log(\"Downloaded?\", completed);\n  })\n  .catch((err) =\u003e {\n    console.log(\"[error]\", err);\n  });\n```\n\n- See all available options [here](#constructor).\n- See all examples [here](https://github.com/andresusanto/easydl/tree/master/examples).\n\n---\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\u003cb\u003eGetting the Metadata\u003c/b\u003e\n\u003c/summary\u003e\nIf you want to get the metadata of the current download, such as file size, HTTP response header, etc. You can get one by using:\n\n```ts\ntry {\n  const dl = new EasyDl(url, dest, options);\n  const metadata = await dl.metadata();\n  console.log(\"[metadata]\", metadata);\n\n  const success = await dl.wait();\n  console.log(\"download complete.\");\n} catch (err) {\n  console.log(\"[error]\", err);\n}\n```\n\nAlternatively, you can also listen to the `metadata` event:\n\n```ts\ntry {\n  const dl = new EasyDl(url, dest, options);\n  await dl\n    .on(\"metadata\", (metadata) =\u003e {\n      // do something with the metadata\n    })\n    .wait();\n} catch (err) {\n  console.log(\"[error]\", err);\n}\n```\n\nAnd you will get something like this:\n\n```ts\n{\n  size: 104857600,                      // file size\n  chunks: [10485760, 10485760 ....],    // size of each chunks\n  isResume: true,                       // whether the current download resumes previous download (using detected chunks)\n  progress: [100, 0, 100 .....],        // current progress of each chunk, values should be 0 or 100\n  finalAddress: 'http://www.ovh.net/files/100Mio.dat', // final address of the file, if redirection occured, it will be different from the original url\n  parallel: true,                        // whether multi-connection download is supported\n  resumable: true,                      // whether the download can be stopped and resumed back later on (some servers do not support/allow it)\n  headers: {\n    ....\n    // some http headers\n    ....\n  },\n  savedFilePath: '/tmp/100Mio.dat'      // final file path. it may be different if you supplied directory as the dest param or you use \"new_file\" as the existBehavior\n}\n```\n\n- Details of the metadata can be seen at [the metadata section](#Metadata).\n- Related example [can be found here](examples/metadata.ts).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\u003cb\u003eUsing Progress Status\u003c/b\u003e\n\u003c/summary\u003e\n\n```ts\ntry {\n  const dl = new EasyDl(url, dest);\n  await dl\n    .on(\"progress\", ({ details, total }) =\u003e {\n      // do something with the progress\n      console.log(\"[details]\", details);\n      // you will get an array:\n      //[\n      //   { speed: 0, bytes: 0, percentage: 0 },\n      //   { speed: 0, bytes: 0, percentage: 0 },\n      //   ....\n      //   {\n      //     speed: 837509.8149186764,\n      //     bytes: 7355193,\n      //     percentage: 70.14458656311035\n      //   },\n      //   {\n      //     speed: 787249.3573264781,\n      //     bytes: 7507833,\n      //     percentage: 71.60027503967285\n      //   },\n      //   ...\n      //]\n\n      console.log(\"[total]\", total);\n      // You will get:\n      // {\n      //   speed: 4144377.1053382815,\n      //   bytes: 41647652,\n      //   percentage: 39.71829605102539\n      // }\n    })\n    .wait();\n} catch (err) {\n  console.log(\"[error]\", err);\n}\n```\n\n**Note:**\n\n- `details` - Array containing progress information for each file chunks.\n- `total` - The total download progress of the file.\n\nMore info: See On Progress\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\u003cb\u003ePausing/Resuming Downloads\u003c/b\u003e\n\u003c/summary\u003e\nEasyDl is a resilient downloader designed to survive even in the event of abrupt program termination. It can automaticaly recover the already downloaded parts of files (chunks) and resume the download instead of starting from scratch. As a result, to pause/stop the download, all you need to do is destroying the `EasyDl` instances. You will be able to resume them by creating new `EasyDl` instances later on.\n\n```ts\ntry {\n  const dl = new EasyDl(url, dest, opts);\n  const downloaded = await dl.wait();\n\n  // somewhere in the app you call: dl.destroy();\n  // afterwards, dl.wait() will resolve and\n  // this \"downloaded\" will be false\n  console.log(\"[downloaded]\", downloaded);\n} catch (err) {\n  console.log(\"[error]\", err);\n}\n\n// later on, you decide to resume the download.\n// all you need to do, is simply creating a new\n// EasyDl instance with the same chunk size as before (if you're not using the default one)\ntry {\n  // if some complete chunks of file are available, they would not be re-downloaded twice.\n  const dl = new EasyDl(url, dest, opts);\n  const downloaded = await dl.wait();\n\n  console.log(\"[downloaded]\", downloaded);\n} catch (err) {\n  console.log(\"[error]\", err);\n}\n```\n\n- More example [can be found here](examples/pause-resume.ts).\n- Learn more about EasyDl file chunking: [Chunks](#Chunks).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\u003cb\u003eWithout using Promise\u003c/b\u003e\n\u003c/summary\u003e\n\n`EasyDl` is an EventEmitter, so if you need to, you can listen to its events instead of using the Promise APIs.\n\n```ts\nlet downloaded = false;\nconst dl = new EasyDl(url, dest, opts)\n  .on(\"end\", () =\u003e {\n    console.log(\"download success!\");\n    downloaded = true;\n  })\n  .on(\"close\", () =\u003e {\n    console.log(\n      \"this close event will be fired after when instance is stopped (destroyed)\"\n    );\n    console.log(\n      \"If the download is complete (not cancelled), the .on(end) event will be fired before this event.\"\n    );\n    console.log(\"otherwise, only this event will be fired.\");\n\n    // downloaded will be true if .on('end') is fired\n    console.log(\"[downloaded]\", downloaded);\n  })\n  .on(\"error\", (err) =\u003e {\n    console.log(\"[error]\", err);\n    // handle some error here\n  })\n  .start(); // download will not be started unless you call .start()\n```\n\n\u003c/details\u003e\n\n## CLI\n\n\u003cimg src=\"https://user-images.githubusercontent.com/7076809/82736366-a0525300-9d6c-11ea-9ec5-e22dda09131f.png\" alt=\"Demo CLI\" width=\"600\"/\u003e\n\nA CLI version of `EasyDl` is available as a separate package [easydl-cli](https://github.com/andresusanto/easydl-cli).\n\n## API\n\n### Constructor\n\n```js\nnew EasyDl(url, dest, options);\n```\n\n**`url`** - the URL of the file\n\n**`dest`** - where to save the file. It can be a file name or an existing folder. If you supply a folder location (for example `~/`) the file name will be derrived from the url.\n\n**`options`** - (Object) optional configurable options:\n\n- `connections` - Number of maximum parallel connections. Defaults to `5`.\n- `existBehavior` - What to do if the destination file already exists. Possible values:\n  - `new_file` **(default)** - create a new file by appending `(COPY)` to the file name.\n  - `overwrite` - overwrite the file. Proceed with caution.\n  - `error` - throws error.\n  - `ignore` - ignore and skip this download\n- `followRedirect` - (Boolean) Whether `EasyDl` should follow HTTP redirection. Defaults to `true`\n- `httpOptions` - Options passed to the http client. You can modify the HTTP methods, Auth, Headers, Proxy, etc here. See [Node.js docs](https://nodejs.org/api/http.html#http_http_request_url_options_callback) for more information.\n- `chunkSize` - The maximum size of chunks (bytes) of the file. It accepts a fixed `number` or a `function` which let you calculate the chunk size dynamically based on the file size.\n\n  - the default value of `chunkSize` is this function:\n\n  ```ts\n  function(size) {\n      return Math.min(size / 10, 10 * 1024 * 1024);\n  }\n  ```\n\n  - Using chunk size that is too small may lead to slower download speed due to the nature of TCP connections, but using chunk size that is too big will make resume ineffective due to many incomplete chunks.\n\n- `maxRetry` - Maximum number of retries (for each chunks) when error occured. Defaults to `3`.\n- `retryDelay` - Delay in ms before attempting a retry. Defaults to `2000`.\n- `retryBackoff` - Incremental back-off in ms for each failed retries. Defaults to `3000`.\n- `reportInterval` - Set how frequent `progress` event emitted by `EasyDL`. Defaults to `2500`.\n- `methodFallback` - use `GET` method instead of `HEAD` to calculate metadata. Useful for downloading S3 signed URLs.\n\n### Metadata\n\nYou can get the metadata by using the `.metadata()` function or listening to the `.on('metadata')` event.\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\u003cb\u003eExample\u003c/b\u003e\n\u003c/summary\u003e\n\n```ts\n{\n  size: 104857600,\n  chunks: [10485760, 10485760 ....],\n  isResume: true,\n  progress: [100, 0, 100 .....],\n  finalAddress: 'http://www.ovh.net/files/100Mio.dat',\n  parallel: true,\n  resumable: true,\n  headers: {\n    ....\n    // some http headers\n    ....\n  },\n  savedFilePath: '/tmp/100Mio.dat'\n}\n```\n\n\u003c/details\u003e\n\n**`size`** - Size of the file in bytes. **Note:** If there is no information given about file size by the server, the value would be `0`.\n\n**`chunks`** - An array containing the size of each chunk in bytes.\n\n**`isResume`** - A boolean indicating whether the current download resumes previous download (using detected chunks).\n\n**`progress`** - Current progress of each chunk. Valid values are `0` and `100`. (Incomplete chunks are discarded)\n\n**`finalAddress`** - The final URL of the file being downloaded. If redirection occured, it will be different from the original url.\n\n**`parallel`** - A boolean indicating whether multi-connection download is supported.\n\n**`resumable`** - A boolean indicating whether the download can be stopped and resumed back later on (some servers do not support/allow it).\n\n**`headers`** - Raw http headers from the server\n\n**`savedFilePath`** - The final file path. It may be different if you supplied directory as the `dest` param or you use `new_file` as the `existBehavior`.\n\n### Chunks\n\n- When a file is being downloaded, it will be divided into chunks.\n- When all chunks are downloaded, it will be merged into a single file.\n- When the download is stopped, and later resumed, all completed chunks will be kept while incomplete chunks will be discarded and re-downloaded.\n- Using chunk size that is too small may lead to slower download speed due to the nature of TCP connections, but using chunk size that is too big will make resume ineffective due to many incomplete chunks.\n\n### Clean-up\n\nAll downloaded chunks are kept until the download has finished. If you decide to abort the download and do not plan to resume it, you can use the cleaning utility provided by `EasyDl` to delete all temporary chunk files.\n\n```ts\nimport { clean } from \"easydl/utils\";\n\ntry {\n  // this will delete chunks belonging to the 100Mb.dat file\n  const deletedChunks1 = await clean(\"/tmp/100Mb.dat\");\n  console.log(deletedChunks1);\n\n  // this will delete all remaining chunks in /tmp directory\n  const deletedChunks2 = await clean(\"/tmp\");\n  console.log(deletedChunks2);\n} catch (err) {\n  console.log(\"error when cleaning-up\", err);\n}\n```\n\nSee [clean-up examples](examples/cleanup.ts) for more detail.\n\n### .wait() `:async`\n\nThe `.wait()` method will be resolved after the instance has finished and destoryed. It returns a boolean value `true` if the file is downloaded and saved to the destination, or `false` otherwise.\n\n### .metadata() `:async`\n\nThe `.metadata()` method will be resolved after the metadata for the download is ready. It returns the [Metadata](#Metadata) object.\n\n### .on(`'error'`, function (`err`) {})\n\nThe `error` event is emitted when errors occured. An error object will be passed to the callback function.\n\n### .on(`'end'`, function () {})\n\nThe `end` event is emitted after the download had finished and the file being downloaded is ready.\n\n### .on(`'close'`, function () {})\n\nEmitted when the `EasyDl` instance is closed and being destroyed.\n\n### .on(`'metadata'`, function (`metadata`) {})\n\nThe `metadata` event will be emitted after the metadata for the download is ready. It will pass the [Metadata](#Metadata) object to its callback function.\n\n### .on(`'retry'`, function (`retryInfo`) {})\n\nThe `metadata` event will be emitted after the metadata for the download is ready. It will pass the [Metadata](#Metadata) object to its callback function.\n\n### .on(`'progress'`, function (`progressReport`) {})\n\nThe `progress` event is emitted when the file is being transferred from the server. A `progressReport` object is given to the callback function:\n\n```ts\n{\n  details: {\n    { speed: 0, bytes: 0, percentage: 0 },\n    { speed: 0, bytes: 0, percentage: 0 },\n    ...\n    {\n      speed: 727088.6075949367, // bytes per second\n      bytes: 6511840,\n      percentage: 62.10174560546875 // 0 - 100%\n    },\n    {\n      speed: 961224.5116403532,\n      bytes: 9179673,\n      percentage: 87.5441837310791\n    }\n  },\n  total: {\n    speed: 4144377.1053382815,\n    bytes: 41647652,\n    percentage: 39.71829605102539\n  }\n}\n```\n\n### .on(`'build'`, function (`progress`) {})\n\nThe `build` event is emitted when all chunks are downloaded and the file is being built by merging chunks together. This event will be fired after [progress](#onprogress-function-progressreport-) event reached `100%`. When the build process has completed, the [end](#onend-function--) event will be emitted.\n\nThis `progress` object is given in the callback:\n\n```ts\n{\n  percentage: 0; // values between 0 - 100\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandresusanto%2Feasydl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandresusanto%2Feasydl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandresusanto%2Feasydl/lists"}