aws4fetch icon indicating copy to clipboard operation
aws4fetch copied to clipboard

Node.js support?

Open march08 opened this issue 3 years ago • 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

march08 avatar Jul 28 '20 11:07 march08

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

mhart avatar Jul 28 '20 11:07 mhart

@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.

march08 avatar Jul 28 '20 18:07 march08

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)

mhart avatar Jul 28 '20 18:07 mhart

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 )

march08 avatar Jul 28 '20 20:07 march08

Well that must be a bug with that library, it's supported in node-fetch:

https://github.com/node-fetch/node-fetch/blob/ca50c22e3410ee288a7b4a8a89384d6df95ae3b6/%40types/index.d.ts#L46

mhart avatar Jul 28 '20 20:07 mhart

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

rafaelnogueira1 avatar Jul 14 '21 14:07 rafaelnogueira1

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

rafaelnogueira1 avatar Jul 14 '21 19:07 rafaelnogueira1

@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

half2me avatar Aug 27 '21 21:08 half2me

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

badalya1 avatar May 18 '22 18:05 badalya1

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

half2me avatar May 19 '22 10:05 half2me

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!

mhart avatar Oct 06 '22 16:10 mhart

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
  * 

n1ru4l avatar Nov 29 '22 10:11 n1ru4l

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

0x1abin avatar Aug 22 '23 08:08 0x1abin