unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.98k stars 496 forks source link

Preset cloudflare-pages/cloudflare-module not working with @aws-sdk #1882

Open rhaenni opened 11 months ago

rhaenni commented 11 months ago

Environment

Nuxt 3.8.0 with Nitro 2.7.2 vite v4.5.0

Reproduction

https://codesandbox.io/p/github/unjs/nitro/csb-z43zd7/draft/tender-wu

  1. Run "Nitro Build" Task from the "Run Task or Terminal" button
  2. Run "Wrangler" Task 3.-> observe following error when the code is trying to load the @aws-sdk
    [nitro] [request error] [unhandled] Cannot convert object to primitive value
    at e2.defaultUserAgent (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:13983:77)  
    at Um.getRuntimeConfig (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:15730:388)  
    at new PollyClient (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:15755:44)  
    at file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:16681:14  
    at file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:2318:26  
    at Object.handler (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:3428:54)  
    at Object.handler (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:2343:29)  
    at async file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:2420:7  
    at async file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:3451:20  
    at async jsonError (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:16695:12)

Describe the bug

Can not use AWS-SDK with cloudflare-pages/cloudflare-module Nitro Preset and using wrangler to deploy to Cloudflare, always getting errors from AWS-SDK.

I found a somewhat related issue but don't understand how to workaround: https://github.com/cloudflare/workers-sdk/issues/2081

Additional context

No response

Logs

No response

manniL commented 11 months ago

Most likely related to or even a dupe of https://github.com/unjs/unenv/issues/86 -> https://github.com/unjs/unenv/issues/92

cosbgn commented 10 months ago

I think cloudflare doesn't support aws-sdk. Try using aws4fetch instead.

juretopolak commented 9 months ago

@cosbgn cloudflare supports aws-sdk, I can send emails in a simple worker using script below.

But I can't do it in the Nuxt / Nitro event handler, cloudflare throws an error.

@manniL issues you mentioned were closed in unjs/unenv v1.9.0... Any idea why SESClient still throws an error:

Cannot convert object to primitive value

import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses'

export default eventHandler(async () => {
  const message = {
    Subject: { Data: 'subject' },
    Body: { Html: { Data: 'body' } },
  }

  const mail = new SendEmailCommand({
    Source: 'test@test.com',
    ReturnPath: 'test@test.com',
    Destination: { ToAddresses: ['test@test.com'] },
    Message: message,
  })

  const credentials = {
    accessKeyId: '***',
    secretAccessKey: '***',
  }

  const client = new SESClient({
    region: 'eu-central-1',
    credentials,
  });

  const send_mail = await client.send(mail).catch(console.error)

  return send_mail ? { message: 'Email sent' } : { message: 'Error' }
})
Cannot convert object to primitive value
  at e.defaultUserAgent (chunks/index.mjs:1:394301)  
  at gp.getRuntimeConfig (chunks/index.mjs:1:487967)  
  at new SESClient (chunks/index.mjs:1:489918)  
  at chunks/test.mjs:1:347  
  at chunks/nitro/cloudflare-pages.mjs:1:62477  
  at async Object.handler (chunks/nitro/cloudflare-pages.mjs:1:62913)  
  at async chunks/nitro/cloudflare-pages.mjs:1:65282  
  at async chunks/nitro/cloudflare-pages.mjs:1:104325
leogargioni commented 6 months ago

@cosbgn cloudflare supports aws-sdk, I can send emails in a simple worker using script below.

But I can't do it in the Nuxt / Nitro event handler, cloudflare throws an error.

Agree, there's a working example from Cloudflare here.

Most likely related to or even a dupe of unjs/unenv#86 -> unjs/unenv#92

I don't think it's the case since I'm still having this issue.

pi0 commented 5 months ago

@leogargioni do you mind making a recent repro? What is latest issue/error you get?

leogargioni commented 5 months ago

@pi0 sure, thanks for looking into it.

I've created this repo with api examples:

For /api/test1, the log in Cloudflare is:

"[nuxt] [request error] [unhandled] [500]", "Cannot convert object to primitive value\n at defaultUserAgent (chunks//index.mjs:1:314339) \n at Id.getRuntimeConfig (chunks//index.mjs:1:519815) \n at new DynamoDBClient (chunks//index.mjs:1:521815) \n at chunks//ddbClient.mjs:1:206"

Please note that all tests work in dev (npm run dev), but when trying to preview the test1 and test2 also don't work, but locally (with wrangler) the error is "TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute" for both endpoints.

To test, you'll need to have a table called "Example" in DynamoDB, I've deployed the local version of DynamoDB to test it locally in development environment. To create the "Example" table in local DynamoDB run the following command:

aws dynamodb create-table --table-name Example --attribute-definitions AttributeName=PK,AttributeType=S --key-schema AttributeName=PK,KeyType=HASH  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 --table-class STANDARD --region=sa-east-1 --endpoint-url http://localhost:8000/

Lastly, FYI, I've followed this guide to create the Nuxt app and deploy in Cloudflare.

Please let me know if you need more info. Thanks.