How do I get the total length of response header and response body?

How do I get the total length of response header and response body?

worker’s code as below, all console.log returns null (or nil, not sure, would like to have the length of both headers and body if possible. Thanks.)

resp = new Response(` User-agent: *
Disallow: /activate/
Disallow: /register/
Disallow: /signup/
Disallow: /cgi-bin/
Disallow: /wp-admin/
Disallow: /admn/
Disallow: /cart/
Disallow: /wp-login.php
Disallow: /login.php
Disallow: /*add-to*=*
Allow: /`, {
    headers: { 'content-type': 'text/plain' },
  })


      let clonedResponse = resp.clone();
  

let value = resp.headers.get('Content-Length');
console.log(clonedResponse.body.length);
console.log(clonedResponse.headers.length);
console.log(clonedResponse.header);
console.log(value);

i’ve tried storing response.body and response.header into a kv and retrieving the value and did the below but the size doesn’t seem right. Can anyone help?

cache = await EDGE_CACHE.get(url.hostname+url.pathname);
size = roughSizeOfObject(cache)

function roughSizeOfObject( object ) {

    var objectList = [];
    var stack = [ object ];
    var bytes = 0;

    while ( stack.length ) {
        var value = stack.pop();

        if ( typeof value === 'boolean' ) {
          console.log("bool")
            bytes += 4;
        }
        else if ( typeof value === 'string' ) {
          console.log("string = "+value)
            bytes += value.length * 2;
        }
        else if ( typeof value === 'number' ) {
          console.log("num = ")
            bytes += 8;
        }
        else if
        (
            typeof value === 'object'
            && objectList.indexOf( value ) === -1
        )
        {
          console.log("obj = ")
            objectList.push( value );

            for( var i in value ) {
                stack.push( value[ i ] );
            }
        }
    }
    console.log("no. bytes = "+bytes)
    return bytes;
}

Counting the body length (possibly even the content length could work) should be enough. For the headers you simply iterate through them and count the length of the name and value (+2 for the colon and space).

That should be it. What you probably cant access is the status line, so the calculated value will be off by a few bytes.

yes i understand. can you show an example code? i can’t seem to figure out how to do the header part especially.

That would require some custom JavaScript code, which would be a tad beyond the scope of the forum.

Post what you have tried so far. Why did it not work?

i’ve shown the code in my 2nd message of this topic. (that’s why i’ve tried so far)
can you help check? thx

I am not sure what that does, but it does not access the response headers. Can you post that bit?

function roughSizeOfObject( object ) {

var objectList = [];
var stack = [ object ];
var bytes = 0;

while ( stack.length ) {
    var value = stack.pop();

    if ( typeof value === 'boolean' ) {
      console.log("bool")
        bytes += 4;
    }
    else if ( typeof value === 'string' ) {
      console.log("string = "+value)
        bytes += value.length * 2;
    }
    else if ( typeof value === 'number' ) {
      console.log("num = ")
        bytes += 8;
    }
    else if
    (
        typeof value === 'object'
        && objectList.indexOf( value ) === -1
    )
    {
      console.log("obj = ")
        objectList.push( value );

        for( var i in value ) {
            stack.push( value[ i ] );
        }
    }
}
console.log("no. bytes = "+bytes)
return bytes;
}
resp = new Response(`User-agent: *
Allow: / `, {
    headers: { 'content-type': 'text/plain' },
  })
let clonedResponse = resp.clone();

console.log(roughSizeOfObject(clonedResponse.headers));

I am sorry, but I do not understand the logic of roughSizeOfObject. How did you get to implement it that way and why do you handle booleans and numbers? Can you walk me through the code?

i actually got it from here:

just wanted to have the response.headers length that’s all the body’s size seemed doubled in this. i removed the ( * 2 ) part to get the size. However. This is kind of irrelevant if i can figure out how to store
" response " into KV.

Do you know how?
This is related to this question here

Copy-pasting code really rarely is a good idea, especially if you are not familiar with what you are doing.

I’d strongly recommend to go through https://developer.mozilla.org/en-US/docs/Web/API/Headers and familiarise yourself with the necessary calls. If you then apply the logic I mentioned earlier, you should get to a number which is pretty close.

Oh no, pretty close is not good enough. I need exact for exact calculations.
If pretty close is all I can do, then…

do you know how to save “response” into KV? the fastest way?

From what I can tell, you wont be able to get the exact number as you dont have access to the status line.

What is it you actually want to achieve?

KV is covered at https://developers.cloudflare.com/workers/reference/apis/kv

i would like to cache “event.response” into KV. what’s the fastest / most efficient way (for the program) to do this? fastest retrieval / de-serializer / un-serializer will do.

Why exactly? Cloudflare caches out of the box anyhow. Maybe you also want to look into Worker sites.

to better control the caching with KV instead of the caches.default.

can you just give the solution to cache “event.response” into KV. what’s the fastest / most efficient way (for the program) to do this? fastest retrieval / de-serializer / un-serializer will do.

I believe there are several threads with examples on the forum, but I dont know the concrete links. You’d need to use the search for that.

I cannot believe this is the kind of answer you post as reply. Not to sound rude but please only answer what the questioner needs. Thanks in advance.

For starters, this is a community and not a support channel, so I’d be careful with demands. Second, your question was answered and I provided you with an approach how to calculate the size. Third, you have a dedicated thread for that other question and should not cross-post. Fourth, you received a reasonable response even for your unrelated question and it really is not asked too much to use the search, something you should have done to begin with.