request-size v1.0.0
request-size
Yup, request the size in octets, of the response body, without downloading it. Might help you obtain the sizes, in octets, of images or stuff provided by web servers.
How does it work?
Easy: making a HEAD
request, it gets the Content-Length
field value for you. If omitted? Uh-oh. But unlikely it would, I guess, especially if you're dealing with images that are not transferred in chunks. At least one thing I can be sure is that this is the lightest way to do this, for you, and the server. Minimum traffic, minimum load; win-win.
The below is an excerpt for you from RFC 7230: Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing.
A server MAY send a Content-Length header field in a response to a
HEAD request (Section 4.3.2 of [RFC7231]); a server MUST NOT send
Content-Length in such a response unless its field-value equals the
decimal number of octets that would have been sent in the payload
body of a response if the same request had used the GET method.
Take a look at the code, it's simple.
And how do I use it?
Once you require
it, it would give you an await
able function, which returns a Promise
. You may feed it a URI to request, and some options for its underlying request
module if you want.
"use strict";
const request_size = require("request-size");
(async _ => {
const uri = "https://stackoverflow.com/";
const options = {followRedirect: false};
try
{
const size = await request_size(uri, options);
console.info("SIZE", size);
// The size, in octets, of the HTML source code file.
}
catch(error)
{
console.error("ERROR", error);
}
})();