{"id":18064568,"url":"https://github.com/rn0x/file_size_url","last_synced_at":"2025-04-11T18:10:58.365Z","repository":{"id":252611791,"uuid":"840918786","full_name":"rn0x/file_size_url","owner":"rn0x","description":"Node.js library for fetching the size of a file from a given URL using either HTTP or HTTPS protocols. It returns the file size in a human-readable format such as \"1.23 MB\".","archived":false,"fork":false,"pushed_at":"2024-08-14T11:28:41.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-11T05:48:36.173Z","etag":null,"topics":["file-size-url","library","nodejs","remote","url"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/file_size_url","language":"JavaScript","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/rn0x.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":"2024-08-11T05:00:55.000Z","updated_at":"2024-11-20T09:03:09.000Z","dependencies_parsed_at":"2024-08-11T07:45:42.545Z","dependency_job_id":"d33d0480-d762-451e-9b42-105244b57f0f","html_url":"https://github.com/rn0x/file_size_url","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.2857142857142857,"last_synced_commit":"3998f6d19fc5be82364b5201cde3fcc0314a4888"},"previous_names":["rn0x/file_size_url"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rn0x%2Ffile_size_url","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rn0x%2Ffile_size_url/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rn0x%2Ffile_size_url/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rn0x%2Ffile_size_url/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rn0x","download_url":"https://codeload.github.com/rn0x/file_size_url/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248456364,"owners_count":21106603,"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":["file-size-url","library","nodejs","remote","url"],"created_at":"2024-10-31T06:06:48.514Z","updated_at":"2025-04-11T18:10:58.344Z","avatar_url":"https://github.com/rn0x.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# file_size_url\n\n[![NPM version](https://img.shields.io/npm/v/file_size_url.svg?style=flat)](https://www.npmjs.com/package/file_size_url)\n\n [![NPM monthly downloads](https://img.shields.io/npm/dm/file_size_url.svg?style=flat)](https://npmjs.org/package/file_size_url) \n \n [![NPM total downloads](https://img.shields.io/npm/dt/file_size_url.svg?style=flat)](https://npmjs.org/package/file_size_url) \n\n![repository-open-graph-template](https://github.com/user-attachments/assets/e97584d7-9f41-43e5-8903-e08e39437a6e)\n\n \n\n`file_size_url` is a simple Node.js library for fetching the size of a file from a given URL using either `HTTP` or `HTTPS` protocols. It returns the file size in a human-readable format such as \"1.23 MB\".\n\n**0 dependencies.**\n\n## Installation\n\nYou can install the library via npm:\n\n```bash\nnpm install file_size_url\n```\n\nAlternatively, if you have the source code, you can directly use the function in your Node.js project:\n\n```bash\n# Simply copy the source code file to your project directory\n```\n\n## Usage\n\n### Importing and Using the Library\n\n```javascript\nimport fileSizeUrl from 'file_size_url';\n\nfileSizeUrl(\"https://example.com/file.zip\")\n  .then(size =\u003e console.log(`File size: ${size}`))\n  .catch(error =\u003e console.error(`Error: ${error.message}`));\n```\n\n### Example with `telegraf` (Telegram Bot)\n\nYou can integrate `file_size_url` with `telegraf` to fetch and display file sizes in your Telegram bot:\n\n```javascript\nimport { Telegraf } from 'telegraf';\nimport fileSizeUrl from 'file_size_url';\n\nconst bot = new Telegraf('\u003cYOUR_BOT_TOKEN\u003e');\n\nbot.command('filesize', async (ctx) =\u003e {\n  const url = ctx.message.text.split(' ')[1];\n  if (!url) return ctx.reply('Please provide a URL.');\n\n  try {\n    const size = await fileSizeUrl(url);\n    ctx.reply(`File size: ${size}`);\n  } catch (error) {\n    ctx.reply(`Error: ${error.message}`);\n  }\n});\n\nbot.launch();\n```\n\n### Example with `express.js`\n\nYou can use `file_size_url` in an `express.js` server to get file sizes via an API:\n\n```javascript\nimport express from 'express';\nimport fileSizeUrl from 'file_size_url';\n\nconst app = express();\n\napp.get('/filesize', (req, res) =\u003e {\n  const { url } = req.query;\n  if (!url) return res.status(400).send('URL parameter is required');\n\n  fileSizeUrl(url)\n    .then(size =\u003e res.send(`File size: ${size}`))\n    .catch(error =\u003e res.status(500).send(`Error: ${error.message}`));\n});\n\napp.listen(3000, () =\u003e {\n  console.log('Server is running on port 3000');\n});\n```\n\n### Using `file_size_url` in a Loop\n\nYou can easily use `file_size_url` inside a loop to fetch file sizes from multiple URLs:\n\n```javascript\nimport fileSizeUrl from './file_size_url.js';\n\nconst urls = [\n  'https://example.com/file1.zip',\n  'https://example.com/file2.zip',\n  'https://example.com/file3.zip'\n];\n\nurls.forEach(async (url) =\u003e {\n  try {\n    const size = await fileSizeUrl(url);\n    console.log(`File size for ${url}: ${size}`);\n  } catch (error) {\n    console.error(`Error fetching size for ${url}: ${error.message}`);\n  }\n});\n```\n\n### Using `file_size_url` with `Promise.all`\n\nFetch file sizes concurrently for multiple URLs using `Promise.all`:\n\n```javascript\nimport fileSizeUrl from './file_size_url.js';\n\nconst urls = [\n  'https://example.com/file1.zip',\n  'https://example.com/file2.zip',\n  'https://example.com/file3.zip'\n];\n\nPromise.all(urls.map(url =\u003e fileSizeUrl(url)))\n  .then(sizes =\u003e {\n    sizes.forEach((size, index) =\u003e {\n      console.log(`File size for ${urls[index]}: ${size}`);\n    });\n  })\n  .catch(error =\u003e console.error(`Error: ${error.message}`));\n```\n\n## API Reference\n\n### `fileSizeUrl(url)`\n\nFetches the file size from a given URL.\n\n#### Parameters\n\n- **url** (`string`): The URL of the file.\n\n#### Returns\n\n- **Promise\u003cstring\u003e**: Resolves with the file size as a formatted string (e.g., \"1.23 MB\"). If an error occurs, the promise is rejected with an error message.\n\n#### Example\n\n```javascript\nfileSizeUrl(\"https://example.com/file.zip\")\n  .then(size =\u003e console.log(`File size: ${size}`))\n  .catch(error =\u003e console.error(`Error: ${error.message}`));\n```\n\n### Error Handling\n\n- `Invalid URL`: If the URL is invalid or missing.\n- `The address should be http or https`: If the protocol is not supported.\n- `Failed to get file size, status code: \u003cstatusCode\u003e`: If the HTTP status code is not 200.\n- `Couldn't retrieve file size from headers`: If the `content-length` header is missing or invalid.\n\n### `formatBytes(bytes)`\n\nFormats bytes into a human-readable string.\n\n#### Parameters\n\n- **bytes** (`number`): The number of bytes.\n\n#### Returns\n\n- **string**: The formatted size string (e.g., \"1.23 MB\").\n\n#### Example\n\n```javascript\nconsole.log(formatBytes(12345678)); // Output: \"11.77 MB\"\n```\n\n## Notes\n\n- Make sure the URL starts with `http://` or `https://`.\n- The library is designed to work only in Node.js environments that support the native `http` and `https` modules.\n\n## License\n\nThis library is available under the [MIT License](https://opensource.org/licenses/MIT).\n\n---\n\n[Arabic Documentation](/AR.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frn0x%2Ffile_size_url","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frn0x%2Ffile_size_url","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frn0x%2Ffile_size_url/lists"}