mhart/aws4fetch

Node.js support?

march08 opened this issue · 13 comments

works well in the browser however there are some differences in the WebAPI vs node. Are you planning to add SSR support as well? Thanks

mhart commented

@march08 I'm not sure what you mean? Server Side Rendering is orthogonal to a library like this. Can you be more specific?

@march08 I'm not sure what you mean? Server Side Rendering is orthogonal to a library like this. Can you be more specific?

For example, crypto.subtle doesn't exist on server (node) but web crypto api only.
headers.keys is undefined on server as well.

mhart commented

Right, Node.js doesn't support fetch (you could try out https://github.com/node-fetch/node-fetch ) or crypto.subtle (you could try out https://github.com/PeculiarVentures/node-webcrypto-ossl)

fetch can be added with isomorphic-fetch but Headers.keys() doesn't work (available only via Web API). In the end I had to rewrite the code to make it work )

mhart commented

@march08 how did you solve this? 'm using nextjs and i stuck in this problem.

In the end i end up change to use aws4-axios to solve my problem.

@mhart just adding to this issue. First of all, huge thanks for this. I'm trying to break away from the bloated and buggy Amplify library for my SvelteKit app.
In my use-case SSR runs on cloudflare workers, so SubtleCrypto is available, but when testing locally with Node.js I need to disable SSR to use this libray, even though I have nodejs 16.
From what I can see, SubtleCrypto is available on Node.js as well: https://nodejs.org/api/webcrypto.html#webcrypto_class_subtlecrypto
It would be really useful if this library could be used on both browser and node.js

@march08 You are probably looking for aws4 if you want to use it in node.

@badalya1 well I want to use it on browser and cf workers in production, but in node just for testing.

mhart commented

Changing this to be a Node.js-support specific issue. The ability to use this locally should be supported by workerd and I assume Deno as well (though haven't checked).

If anyone knows what the specific issues with current Node.js support of these APIs are, let me know!

I achieved Node.js support with @whatwg-node/fetch, we run it on both Node.js and Cloudflare Workers.

diff --git a/dist/aws4fetch.esm.js b/dist/aws4fetch.esm.js
index 9a47c421432cb8dc9f92a07a49fd46d2c00c57f3..f853ca70741083272fea7e57b405c9920402be3e 100644
--- a/dist/aws4fetch.esm.js
+++ b/dist/aws4fetch.esm.js
@@ -1,3 +1,5 @@
+import { fetch, Request, Headers, crypto, TextEncoder } from '@whatwg-node/fetch';
+
 /**
  * @license MIT <https://opensource.org/licenses/MIT>
  * @copyright Michael Hart 2022
diff --git a/dist/aws4fetch.esm.mjs b/dist/aws4fetch.esm.mjs
index 9a47c421432cb8dc9f92a07a49fd46d2c00c57f3..f853ca70741083272fea7e57b405c9920402be3e 100644
--- a/dist/aws4fetch.esm.mjs
+++ b/dist/aws4fetch.esm.mjs
@@ -1,3 +1,5 @@
+import { fetch, Request, Headers, crypto, TextEncoder } from '@whatwg-node/fetch';
+
 /**
  * @license MIT <https://opensource.org/licenses/MIT>
  * @copyright Michael Hart 2022
  * 

It works on both Node v20.5.1 and Cloudflare Worker.