mhart / aws4fetch

A compact AWS client and signing utility for modern JS environments
MIT License
582 stars 39 forks source link

Node.js support? #22

Open march08 opened 4 years ago

march08 commented 4 years ago

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 4 years ago

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

march08 commented 4 years ago

@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 4 years ago

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)

march08 commented 4 years ago

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 4 years ago

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

rafaelnogueira1 commented 3 years ago

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

rafaelnogueira1 commented 3 years ago

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

half2me commented 3 years ago

@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

badalya1 commented 2 years ago

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

half2me commented 2 years ago

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

mhart commented 1 year ago

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!

n1ru4l commented 1 year ago

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
  * 
0x1abin commented 1 year ago

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