-
-
Notifications
You must be signed in to change notification settings - Fork 125
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
refactor(deploy): aws-lambda serve #882
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
@aheissenberger Can you test this and see if it works? |
I have been having trouble with the hono import in the built serve-aws-lambda.js file. It looks like it is not building hono/context into the bundle? Here is a section of the built serve-aws-lambda.js file: const importHono = () => import("./assets/index-BxM3V2xC.js");
const importHonoContextStorage = () => import("hono/context-storage");
const importHonoNodeServerServeStatic = () => import("./assets/serve-static-CLws5J7J.js");
const importHonoAwsLambda = () => import("./assets/index-DM8TMISb.js");
const { Hono } = await importHono();
const { contextStorage } = await importHonoContextStorage();
const { serveStatic } = await importHonoNodeServerServeStatic();
const { handle } = await importHonoAwsLambda(); And then I get a runtime error on AWS when it tries to serve a page: I'm not sure if this is caused by the way I am trying to build this branch and copy to my AWS project dependencies. I have installed hono 4.6.1 in my AWS project. |
I was able to get it working by commenting out the hono context storage from the serve js content. So that seems to be the only issue. I don't understand how hono/aws-lambda is built correctly but hono/context-storage is not. |
Oh right... probably this... 58f7ded#diff-275ea9f25448c4618b0f5d5e886b0f4072dad0167fc254e3804e46be65ec2096R249 |
It works for me if I remove
I had to leave const contextStorage = (await importHonoContextStorage()).contextStorage;
app.use(contextStorage()); But that results in I don't personally have a use case for |
I made a related documentation change in #897 |
@rmarscher Thanks for investigating this.
If that's the problem, why does cloudflare version work? |
Does it work if you install hono manually for your app? |
no - even after installing hono as a dependency the import of the const importHono = () => import("./assets/index-BxM3V2xC.js");
/* vv no ./assets/.. */
const importHonoContextStorage = () => import("hono/context-storage");
const importHonoNodeServerServeStatic = () => import("./assets/serve-static-CLws5J7J.js");
const importHonoAwsLambda = () => import("./assets/index-DM8TMISb.js");
const { Hono } = await importHono();
const { contextStorage } = await importHonoContextStorage();
const { serveStatic } = await importHonoNodeServerServeStatic();
const { handle } = await importHonoAwsLambda(); here is a simple code to debug the problem without deployment to aws: const requestHandlerPath = "./serve-aws-lambda.js";
const { handler } = await import(requestHandlerPath);
const eventData = {
version: "2.0",
routeKey: "$default",
rawPath: "/",
rawQueryString: "",
headers: {
accept: "*/*",
"content-length": "0",
host: "example.com",
"user-agent": "PostmanRuntime/7.26.8",
"x-amzn-trace-id": "Root=1-5f84c7a9-0e5b1e1e1e1e1e1e1e1e1e1e",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-port": "443",
"x-forwarded-proto": "https",
},
requestContext: {
accountId: "123456789012",
apiId: "api-id",
domainName: "example.com",
domainPrefix: "example",
http: {
method: "GET",
path: "/",
protocol: "HTTP/1.1",
sourceIp: "127.0.0.1",
userAgent: "PostmanRuntime/7.26.8",
},
requestId: "id",
routeKey: "$default",
stage: "$default",
time: "12/Mar/2021:19:03:58 +0000",
timeEpoch: 1615578238000,
},
isBase64Encoded: false,
};
const response = await handler(eventData, {});
console.log(response); SolutionI was able to fix the broken vite resolution problem with this import { defineConfig } from 'vite';
import path from "node:path";
export default defineConfig({
resolve: {
alias: {
"hono/context-storage": path.resolve(__dirname, "node_modules/hono/dist/middleware/context-storage/index.js"),
},
},
}); But having the |
It should be fixed with #898. Let me merge it. |
do I understand correctly that this fix only ignores the rollup path resolution problem by catching and ignoring the error? |
I suggest to rollback your fix and add this to the plugins e.g. export function deployAwsLambdaPlugin(opts: {
srcDir: string;
distDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
let entriesFile: string;
return {
name: 'deploy-aws-lambda-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
if (unstable_phase !== 'buildServerBundle' || deploy !== 'aws-lambda') {
return;
}
const { input } = viteConfig.build?.rollupOptions ?? {};
if (input && !(typeof input === 'string') && !(input instanceof Array)) {
input[SERVE_JS.replace(/\.js$/, '')] = `${opts.srcDir}/${SERVE_JS}`;
}
// fix for rollup resolution problem
if (viteConfig.resolve===undefined) {
viteConfig.resolve = {};
}
if (viteConfig.resolve.alias===undefined) {
viteConfig.resolve.alias = {};
}
(viteConfig.resolve.alias as Record<string, string>)['hono/context-storage'] = path.resolve('node_modules/hono/dist/middleware/context-storage/index.js'); |
No, my expectation is it should work flawlessly if users install |
What is the reason to set the
you are quicker - I was just asking why this exists in the first place. I only tried to fix aws, as I have no knowledge about the other hosting platforms. |
It was from a previous attempt to get the same module to be imported by the hono runtime and from server components. I'm pretty sure it can be safely removed. I'll give a try with Cloudflare. I have a few Waku apps. One on AWS Lambda and a few deployments on Cloudflare. I use a fork of this adapter for AWS so I can use
and enable lambda streaming. |
I confirmed it also works great with cloudflare with |
Here's my fork. It uses streaming and it separates the public assets from the lambda function. That way I don't include any non-needed static assets in the function bundle. I upload the public assets to an S3 bucket and use a fallback origin with cloudfront so to have it look there first before routing to the lambda function. That works very well. refactor/deploy/aws-lambda...rmarscher:waku:slimmer-aws-lambda-sst-patch |
How about adding AWS Lambda Streaming as an option in the config file? Both variants are useful as AWS Streaming is not supported by the API Gateway. |
It's #894. Without it, it doesn't work with But, I'm fine to remove it for aws-lambda deploy (and cloudflare too?) |
@rmarscher Does d15b11b 👉 32e90a1 work? |
Hmmm? I just tried
|
yes - I get the same error - is it possible to limit |
I'd go the other way around. The node server is the default and deploy adapters are options. |
@dai-shi What is your suggestion to add an option for a deployment adapter in |
Good question. The config stuff is still something I'll be considering. So, I would like to delay the decision. Meanwhile, if you need some configurations in deployment adapters, the easiest way would be |
With latest commit both situations, with and without package |
@rmarscher when this refactoring is merged we can discuss to add streaming to this adapter. I also deploy the static assets to s3 and use cloudfront to cache and split the requests between waku and the static assent but do not need the output folder to be rearranged. |
I can confirm having this in the if (deploy === '[build output name]' && Array.isArray(config.ssr.external)) {
config.ssr.external = config.ssr.external.filter(
(item) => item !== 'hono/context-storage',
);
} And if (source === 'hono/context-storage') {
return { id: source, external: true };
} |
Nice. Thanks for comfirning.
Ah, thanks. |
Merging this. @rmarscher Can you check the cloudflare one too and send a PR?
Nice. Looking forward to a PR based on you guys' discussions. |
re: #882 (comment) Applies the same fix used for handling hono/context-storage with aws builds. --------- Co-authored-by: daishi <daishi@axlight.com>
Prior art: