Cannot read properties of null (reading 'pipeTo')

Hi, would appreciate anyone that can give me a pointer! :face_with_monocle:

I’m using a Worker to proxy AWS S3 requests. This works perfectly, but if I change the script to use Streams then I get an intermittent exception.

My script is not that different from the example on the Using Streams documentation (https://developers.cloudflare.com/workers/learning/using-streams ). Here’s my code:

const hostname = 's3.us-east-2.amazonaws.com'

async function handleRequest(request) {
  const url = new URL(request.url);
  url.hostname = hostname;

  const reqHeaders = new Headers(request.headers);
  reqHeaders.delete('X-Version');

  const response = await fetch(url, { headers: reqHeaders });

  // If not using streams:
  // return new Response(response.body, response);

  // If using streams:
  const { readable, writable } = new TransformStream();
  response.body.pipeTo(writable);
  return new Response(readable, response);
}

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request));
});

Some requests get a Cloudflare 1101 error. Real time logs for the Worker show:

"name": "TypeError",
"message": "Cannot read properties of null (reading 'pipeTo')",

Oddly, if repeating the request then it alternates between HTTP 200 and throwing this exception. What did I do wrong? :sweat_smile:

It’s possible you didn’t do anything wrong. response.body can be null in some cases, such as 204, etc… I would recommend checking for this before doing response.body.pipeTo.

Thanks for the reply @cherryjimbo !

These requests are AWS S3 Presigned URLs for images, and every request returns 200 (or 403 if the link expiry time has been reached), so I’m not expecting the body to be null at all.

It doesn’t feel like a problem with AWS S3, as it only occurs when using streams.

And if the Presigned URL is bad, I wouldn’t expect any particular URL to keep alternating between working and not working (ie, if I make 1 request per second for 5 seconds for a particular URL, request 1 will return the image, request 2 will give a Cloudflare 1101 error, request 3 will return the image, request 4 will get 1101 etc. If I do the same without streams, all 5 requests return the image).

So this feels very weird!