sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
MIT License
11.83k stars 341 forks source link

Compatibility issue with Cloudflare Worker #569

Open lpellegr opened 3 months ago

lpellegr commented 3 months ago

Using ky 1.2.3 as follows in a Cloudflare Worker instance:

try {
    const response = await ky.post('https://api.ipregistry.co/?key=tryout', {
        json: ["AS1", "AS6", "AS42"],
        headers: {
            "Content-Type": "application/json"
        }
    }).json();

    console.log(response);
} catch (error) {
    console.error(error);
}

produces the following ouput and errors upon execution in a Cloudflare Worker runtime:

✘ [ERROR] Your worker created multiple branches of a single stream (for instance, by calling response.clone() or request.clone()) but did not read the body of both branches. This is wasteful, as it forces the system to buffer the entire stream of data in memory, rather than streaming it through. This may cause your worker to be unexpectedly terminated for going over the memory limit. If you only meant to copy the request or response headers and metadata (e.g. in order to be able to modify them), use the appropriate constructors instead (for instance, new Response(response.body, response), new Request(request), etc).

{ results: [ { allocated: '2001-09-20T00:00:00.000+00:00', asn: 1, country_code: 'US', domain: 'level3.com', name: 'Level 3 Parent, LLC', prefixes: [Object], relationships: [Object], registry: 'ARIN', type: 'business', updated: '2024-02-28T00:00:00.000+00:00' }, { allocated: '1984-02-02T00:00:00.000+00:00', asn: 6, country_code: 'US', domain: 'atos.net', name: 'Atos IT Solutions And Services, Inc.', prefixes: [Object], relationships: [Object], registry: 'ARIN', type: 'business', updated: '2021-01-21T00:00:00.000+00:00' }, { allocated: '2001-05-16T00:00:00.000+00:00', asn: 42, country_code: 'US', domain: 'pch.net', name: 'Woodynet, Inc.', prefixes: [Object], relationships: [Object], registry: 'ARIN', type: 'business', updated: '2012-03-02T00:00:00.000+00:00' } ] } ✘ [ERROR] Your worker created multiple branches of a single stream (for instance, by calling response.clone() or request.clone()) but did not read the body of both branches. This is wasteful, as it forces the system to buffer the entire stream of data in memory, rather than streaming it through. This may cause your worker to be unexpectedly terminated for going over the memory limit. If you only meant to copy the request or response headers and metadata (e.g. in order to be able to modify them), use the appropriate constructors instead (for instance, new Response(response.body, response), new Request(request), etc).

✘ [ERROR] Your worker created multiple branches of a single stream (for instance, by calling response.clone() or request.clone()) but did not read the body of both branches. This is wasteful, as it forces the system to buffer the entire stream of data in memory, rather than streaming it through. This may cause your worker to be unexpectedly terminated for going over the memory limit. If you only meant to copy the request or response headers and metadata (e.g. in order to be able to modify them), use the appropriate constructors instead (for instance, new Response(response.body, response), new Request(request), etc).

I have no idea about how to work around this. Any idea?

riderx commented 2 months ago

did you added this in wrangler config compatibility_flags = ["nodejs_compat"]

lpellegr commented 2 months ago

Yes, it was.

blitss commented 1 month ago

Any update here?