withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.68k stars 2.47k forks source link

API routes throwing errors on Vercel serverless #5489

Closed pilcrowonpaper closed 1 year ago

pilcrowonpaper commented 1 year ago

What version of astro are you using?

1.6.11

Are you using an SSR adapter? If so, which one?

Vercel

What package manager are you using?

npm

What operating system are you using?

Mac

Describe the Bug

API routes returning Response or { body } with any value (string, null, undefined) will throw:

2022-11-27T04:05:04.552Z    fe1c9835-8d64-44e3-add5-a41d141488d7    ERROR    Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError [ERR_INVALID_ARG_TYPE]: The \"chunk\" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArrayBuffer","reason":{"errorType":"TypeError","errorMessage":"The \"chunk\" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArrayBuffer","code":"ERR_INVALID_ARG_TYPE","stack":["TypeError [ERR_INVALID_ARG_TYPE]: The \"chunk\" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArrayBuffer","    at new NodeError (node:internal/errors:393:5)","    at write_ (node:_http_outgoing:843:11)","    at ServerResponse.write (node:_http_outgoing:808:15)","    at setResponse (file:///var/task/node_modules/@astrojs/vercel/dist/serverless/request-transform.js:74:11)","    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","    at async Server.handler (file:///var/task/node_modules/@astrojs/vercel/dist/serverless/entrypoint.js:22:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError [ERR_INVALID_ARG_TYPE]: The \"chunk\" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArrayBuffer","    at process.<anonymous> (file:///var/runtime/index.mjs:1194:17)","    at process.emit (node:events:525:35)","    at process.emit (node:domain:489:12)","    at emit (node:internal/process/promises:149:20)","    at processPromiseRejections (node:internal/process/promises:283:27)","    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown

Interestingly, this doesn't happen on edge functions.

Link to Minimal Reproducible Example

(error with any api route)

Participation

marcfielding1 commented 1 year ago

Same here on vercel using an endpoint like so:

import { google } from 'googleapis'

export async function get({ params }) {

    const oauth2Client = new google.auth.OAuth2(
       ......
    );

    const scopes = [
        'https://www.googleapis.com/auth/calendar.events.freebusy',
        'https://www.googleapis.com/auth/calendar.events.owned',
        'https://www.googleapis.com/auth/userinfo.email'
    ];

    const authorizationUrl = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: scopes,
        include_granted_scopes: true
    });

    return new Response(JSON.stringify({login: authorizationUrl}), {
        status: 200,
        headers: {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin" : "*"
        }
    });
}

The error is the same:

ERROR   Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError [ERR_INVALID_ARG_TYPE]: The \"chunk\" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArrayBuffer"
matthewp commented 1 year ago

@pilcrowOnPaper have you tried in the newest version? If so I can take a look. Also what version of Node are you using on Vercel?

marcfielding1 commented 1 year ago

For me it's ^1.6.10, node 18 - I'm not sure it's astro it's probably the vercel adaptor which for me is ^2.3.2 .

pilcrowonpaper commented 1 year ago

@matthewp

No issue with astro@1.6.15 and @astrojs/vercel@2.3.6, both on Node v 16.x and 18.x.

marcfielding1 commented 1 year ago

Hey, really? I shall test it now!

matthewp commented 1 year ago

@pilcrowOnPaper thanks for testing it.

@marcfielding1 please report back what you find!

marcfielding1 commented 1 year ago

@matthewp You tested this with an async handler? My initial results show the same problem, lemmy double check when I get home.(2 hours)

matthewp commented 1 year ago

@marcfielding1 what did you find?

matthewp commented 1 year ago

Closing as this issue is stale. Please file a new issue if you still see the same problem.