aws/aws-sdk-js

Use Fetch in preference of XMLHttpRequest (on supported platforms)

Closed this issue · 5 comments

Hey guys,
I am trying to make aws Kinesis putRecord(s) calls in a service worker for reliability and performance reasons but seems like it is not supported due to dependency on XMLHttpRequest (Similar issue here - #1902)

While looking for alternates I could not find any aws recommended way of using either -

  1. Use Kinesis REST Api directly so that I can use fetch, OR
  2. Override the default mechanism in the aws-sdk to use fetch
  3. OR any other suitable alternate without incurring additional costs (like using api gateway is very expensive for us)

It would be nice to know what are the team's thought on this and are there any future plans to address this.

We considered running on main UI thread but here are the problems -

  1. The SDK itself is 230kb and adds immensely to the bundle.
  2. In order to ensure reliability for the Kinesis calls, we need to wait until the calls succeed, and we need the code to run on a separate thread to be more performant, like service-workers.
MickL commented

Im running in the same issue like you an #1902 and #2807. I wonder why these have been closed.

Any request that I want to make results in an error: XMLHttpRequest is not defined in my serverless environment (Cloudflare Worker). This should be the case for Amazon Lambda, too.

Example:

const s3 = new S3({
  // credentials
});

s3.getObject({
          Bucket: 'myBucket',
          Key:    'myKey',
        },
        (error, data) => {
             // Error: XMLHttpRequest is not defined
        }
);

this is not an issue with aws sdk, but rather cloudflare which blocks the use of xmlhttp. like the solution from other post, just change xmlhttp to fetch

ppl are asking how they can use my StreamSaver to download files without using much of the memory in the browser.
currently it seems like it isn't possible due to this issue

It would also have value to provide them with the actual response object instead of giving developers a blob/arrayBuffer or a stream.

developers can use the response object to convert it to any shape they like

  • .blob() // as promise
  • .text() // as promise
  • .arrayBuffer() // as promise
  • res.body // readable stream

developer can then be able to pipe it, cache it to cache storage or save it to native fs system or any browser transformer (de/compress(gzip) & textTransformer)

maybe it should have any value if you used node-fetch on the server side and also returned the response object. either that or you return the readable stream from both https and fetch


was a long time ago since i used aws-sdk-js myself but i remember that 230kb was huge and it didn't work in web workers b/c of XHR.

ppl are asking how they can use my StreamSaver to download files without using much of the memory in the browser.
currently it seems like it isn't possible due to this issue

It would also have value to provide them with the actual response object instead of giving developers a blob/arrayBuffer or a stream.

developers can use the response object to convert it to any shape they like

* .blob() // as promise

* .text() // as promise

* .arrayBuffer() // as promise

* res.body // readable stream

developer can then be able to pipe it, cache it to cache storage or save it to native fs system or any browser transformer (de/compress(gzip) & textTransformer)

maybe it should have any value if you used node-fetch on the server side and also returned the response object. either that or you return the readable stream from both https and fetch

was a long time ago since i used aws-sdk-js myself but i remember that 230kb was huge and it didn't work in web workers b/c of XHR.

I've made PR #3505 to address this.
You can try out the working demo here: https://codepen.io/heri16/pen/ExyypGd

Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue.