Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'deno' server functions #419

Merged
merged 2 commits into from
May 24, 2024
Merged

Conversation

littledivy
Copy link
Contributor

This patch adds support for running Open-next server functions with the Deno lambda runtime layer.

Discussed briefly with @conico974 on Discord, adding runtime: "deno" made most sense.

Changes

  • Compile a ES module version of cache.ts when runtime is set to "deno"
  • Add a deno.json and rename function entrypoint as index.ts (this will be redundant with Deno 2)
  • Import node globals in the entrypoint

Example

Here's how a deployment config would look like:

// open-next.config.ts
export default {
  default: {
    runtime: "deno"
  }
}
// sst.config.ts
export default $config({
  app(input) {
    return {
      name: "my-app",
      removal: input?.stage === "production" ? "retain" : "remove",
      home: "aws",
    };
  },
  async run() {
    new sst.aws.Nextjs("MyWeb", {
      transform: {
        server: {
          /* Amazon Linux 2023 */
          runtime: "provided.al2023",
          /* Deno lambda layer */
          layers: ["arn:aws:lambda:ap-south-1:381491846142:layer:deno:2"]
        }
      }
    });
  }
});
sst deploy

Next.js page: https://dvwb0nol21fnx.cloudfront.net
API route: https://dvwb0nol21fnx.cloudfront.net/api/hello

Copy link

changeset-bot bot commented May 22, 2024

🦋 Changeset detected

Latest commit: 5e5bb70

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
open-next Patch
app-pages-router Patch
app-router Patch
tests-unit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented May 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
open-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 24, 2024 11:16am

Copy link
Collaborator

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to allow the deno runtime for the other functions ? (except from middleware as this one requires edge)
Not sure if it's possible to make it work with the imageOptimization function since it requires sharp

@littledivy
Copy link
Contributor Author

littledivy commented May 23, 2024

Do we want to allow the deno runtime for the other functions ? (except from middleware as this one requires edge)
Not sure if it's possible to make it work with the imageOptimization function since it requires sharp

It should work with all of them (including imageOptimization) since Deno supports sharp via Node-API. imageOptimization options does not have the runtime option yet, it takes DefaultFunctionOptions.

I can test it out and probably make a follow-up PR adding runtime if its runnable via Deno. Does that sound good?

@conico974
Copy link
Collaborator

I can test it out and probably make a follow-up PR adding runtime if its runnable via Deno. Does that sound good?

Yeah that's fine, i'll merge this and a bunch of other PR tomorrow in 3.0.2

@conico974 conico974 merged commit f83d636 into sst:main May 24, 2024
3 checks passed
@conico974
Copy link
Collaborator

@littledivy I assume you did not test with the node wrapper, only with the default aws-lambda.
It should work out of the box, but the default generated dockerfile would not work

@littledivy littledivy deleted the deno branch May 25, 2024 06:57
@littledivy
Copy link
Contributor Author

Ah yes that makes sense. It should actually be trivial to add generated dockerfile support for Deno too:

FROM deno-lambda:latest

COPY ./.open-next/server-functions/default/. .

# Round down timestamps to seconds (workaround for https://github.com/denoland/deno/issues/23904)
RUN find node_modules -type f -exec touch -t $(date +%Y%m%d%H%M.00) {} \;

# Run index.ts to warmup the V8 code cache
RUN timeout 15s deno run -Ldebug -A index.ts \
  && timeout 15s deno run -Ldebug -A cache.mjs

CMD ["index.handler"]

Preliminary startup time results show its on-par with the node lambda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants