sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.09k stars 126 forks source link

Server responses in sst.aws.Function are wrapped inside another Response #559

Closed roguesherlock closed 1 week ago

roguesherlock commented 2 weeks ago

Basically if you run any server inside an aws function like say hono, it seems to wrap the response of the server inside a another response with its body being the response of the server.

For example here's the response I get in dev mode,

{
    "body": "{\"isBase64Encoded\":false,\"awsRequestId\":\"298b1fd2-2a50-48ae-b624-ab9ac1693a21\"}",
    "headers": {
        "content-type": "application/json; charset=UTF-8"
    },
    "statusCode": 200,
    "isBase64Encoded": false
}

And here's the expected response for an api response,

{"isBase64Encoded":false,"awsRequestId":"7d3c788f-adde-4f58-995e-98a250fb20c4"}

Note that it works correctly in prod.

Edit: I didn't test this properly. It looks like it doesn't work in prod either

roguesherlock commented 2 weeks ago

Here's what my api function looks like,

import { env } from "../env"
import { auth } from "./auth"

export const api = new sst.aws.Function("API", {
  url: true,
  streaming: true,
  handler: "./packages/functions/app/api/index.handler",
  environment: {
    // TODO: figure out a bette way to do this
    // SITE_URL: frontend.url
    SITE_URL: env.SITE_URL,
    RESEND_API_KEY: env.RESEND_API_KEY,
    IS_LOCAL: $dev ? "true" : "false",
    XATA_BRANCH: env.XATA_BRANCH ?? "dev",
    XATA_API_KEY: env.XATA_API_KEY,
  },

  link: [auth],
})

And here's the api handler function,

const route = app
  .get("/", (c) => {
    return c.text("Hello, world!")
  })
  .get("/aws-lambda-info/", (c) => {
    return c.json({
      isBase64Encoded: c.env.event.isBase64Encoded,
      awsRequestId: c.env.lambdaContext?.awsRequestId,
      context: c.env.context?.awsRequestId,
    })
  })

export const handler = process.env.SST_LIVE
  ? handle(app)
  : (streamHandle(app) as ReturnType<typeof handle>)

export type API = typeof route
roguesherlock commented 1 week ago

Here's the url if you want check, https://lj4dxontstbdlr5lbisqa43psm0wokeu.lambda-url.us-east-1.on.aws/aws-lambda-info

arjunyel commented 1 week ago

This is happening to me on SST dev but not once I deploy, @roguesherlock is it the same for you?

EDIT: Fixed when disabling streaming and using the regular Hono handler

thdxr commented 1 week ago

so the issue here is streaming needs to be disabled in dev mode - we'll make it so this happens automatically

streaming: !$dev

roguesherlock commented 1 week ago

so the issue here is streaming needs to be disabled in dev mode - we'll make it so this happens automatically

streaming: !$dev

Sorry I didn't mention it earlier but I already had that when I deployed,

import { env } from "../env"
import { auth } from "./auth"

console.log("$dev =>", $dev)
export const api = new sst.aws.Function("API", {
  url: true,
  // TODO: Streaming is not working in sst live.
  streaming: $dev ? false : true,
  handler: "./packages/functions/app/api/index.handler",
  environment: {
    // TODO: figure out a better way to handle cyclical dependencies
    // SITE_URL: frontend.url
    SITE_URL: env.SITE_URL,
    RESEND_API_KEY: env.RESEND_API_KEY,
    IS_LOCAL: $dev ? "true" : "false",
    XATA_BRANCH: env.XATA_BRANCH ?? "dev",
    XATA_API_KEY: env.XATA_API_KEY,
    OPENAI_API_KEY: env.OPENAI_API_KEY,
  },
  architecture: "arm64",
  logging: {
    retention: "1 week",
  },
  link: [auth],
})
roguesherlock commented 1 week ago

This is happening to me on SST dev but not once I deploy, @roguesherlock is it the same for you?

EDIT: Fixed when disabling streaming and using the regular Hono handler

nah unfortunately it happens to me on prod too :(