Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
file_size_url
Advanced tools
A lightweight Node.js library to fetch the file size from a remote URL without downloading the file. Zero dependencies. Returns a Promise with the file size formatted as 'B', 'KB', 'MB', 'GB', or 'TB'.
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".
0 dependencies.
You can install the library via npm:
npm install file_size_url
Alternatively, if you have the source code, you can directly use the function in your Node.js project:
# Simply copy the source code file to your project directory
import fileSizeUrl from 'file_size_url';
fileSizeUrl("https://example.com/file.zip")
.then(size => console.log(`File size: ${size}`))
.catch(error => console.error(`Error: ${error.message}`));
telegraf
(Telegram Bot)You can integrate file_size_url
with telegraf
to fetch and display file sizes in your Telegram bot:
import { Telegraf } from 'telegraf';
import fileSizeUrl from 'file_size_url';
const bot = new Telegraf('<YOUR_BOT_TOKEN>');
bot.command('filesize', async (ctx) => {
const url = ctx.message.text.split(' ')[1];
if (!url) return ctx.reply('Please provide a URL.');
try {
const size = await fileSizeUrl(url);
ctx.reply(`File size: ${size}`);
} catch (error) {
ctx.reply(`Error: ${error.message}`);
}
});
bot.launch();
express.js
You can use file_size_url
in an express.js
server to get file sizes via an API:
import express from 'express';
import fileSizeUrl from 'file_size_url';
const app = express();
app.get('/filesize', (req, res) => {
const { url } = req.query;
if (!url) return res.status(400).send('URL parameter is required');
fileSizeUrl(url)
.then(size => res.send(`File size: ${size}`))
.catch(error => res.status(500).send(`Error: ${error.message}`));
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
file_size_url
in a LoopYou can easily use file_size_url
inside a loop to fetch file sizes from multiple URLs:
import fileSizeUrl from './file_size_url.js';
const urls = [
'https://example.com/file1.zip',
'https://example.com/file2.zip',
'https://example.com/file3.zip'
];
urls.forEach(async (url) => {
try {
const size = await fileSizeUrl(url);
console.log(`File size for ${url}: ${size}`);
} catch (error) {
console.error(`Error fetching size for ${url}: ${error.message}`);
}
});
file_size_url
with Promise.all
Fetch file sizes concurrently for multiple URLs using Promise.all
:
import fileSizeUrl from './file_size_url.js';
const urls = [
'https://example.com/file1.zip',
'https://example.com/file2.zip',
'https://example.com/file3.zip'
];
Promise.all(urls.map(url => fileSizeUrl(url)))
.then(sizes => {
sizes.forEach((size, index) => {
console.log(`File size for ${urls[index]}: ${size}`);
});
})
.catch(error => console.error(`Error: ${error.message}`));
fileSizeUrl(url)
Fetches the file size from a given URL.
string
): The URL of the file.fileSizeUrl("https://example.com/file.zip")
.then(size => console.log(`File size: ${size}`))
.catch(error => console.error(`Error: ${error.message}`));
Invalid URL
: If the URL is invalid or missing.The address should be http or https
: If the protocol is not supported.Failed to get file size, status code: <statusCode>
: If the HTTP status code is not 200.Couldn't retrieve file size from headers
: If the content-length
header is missing or invalid.formatBytes(bytes)
Formats bytes into a human-readable string.
number
): The number of bytes.console.log(formatBytes(12345678)); // Output: "11.77 MB"
http://
or https://
.http
and https
modules.This library is available under the MIT License.
FAQs
A lightweight Node.js library to fetch the file size from a remote URL without downloading the file. Zero dependencies. Returns a Promise with the file size formatted as 'B', 'KB', 'MB', 'GB', or 'TB'.
The npm package file_size_url receives a total of 25,611 weekly downloads. As such, file_size_url popularity was classified as popular.
We found that file_size_url demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.