jeasx 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/serverless.js +1 -1
- package/serverless.ts +17 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jeasx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "jeasx - the ease of JSX with the power of SSR",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jsx",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"dotenv": "16.4.5",
|
|
36
36
|
"esbuild": "0.20.2",
|
|
37
37
|
"fastify": "4.26.2",
|
|
38
|
-
"jsx-async-runtime": "0.2.
|
|
38
|
+
"jsx-async-runtime": "0.2.1",
|
|
39
39
|
"pm2": "5.3.1"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
package/serverless.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import fastifyAccepts from"@fastify/accepts";import fastifyCookie from"@fastify/cookie";import fastifyFormbody from"@fastify/formbody";import fastifyMultipart from"@fastify/multipart";import fastifyRequestContext
|
|
1
|
+
import fastifyAccepts from"@fastify/accepts";import fastifyCookie from"@fastify/cookie";import fastifyFormbody from"@fastify/formbody";import fastifyMultipart from"@fastify/multipart";import fastifyRequestContext,{requestContext}from"@fastify/request-context";import fastifyStatic from"@fastify/static";import fastifyUrlData from"@fastify/url-data";import"dotenv/config";import Fastify from"fastify";import{renderToString}from"jsx-async-runtime";import{createHash}from"node:crypto";import{readFile,stat}from"node:fs/promises";import{join}from"node:path";const NODE_ENV_IS_DEVELOPMENT=process.env.NODE_ENV==="development";const serverless=Fastify({logger:true,disableRequestLogging:NODE_ENV_IS_DEVELOPMENT,bodyLimit:Number(process.env.FASTIFY_BODY_LIMIT)||void 0});serverless.register(fastifyAccepts);serverless.register(fastifyCookie);serverless.register(fastifyFormbody);serverless.register(fastifyMultipart);serverless.register(fastifyRequestContext);serverless.register(fastifyUrlData);const FASTIFY_STATIC_HEADERS=!NODE_ENV_IS_DEVELOPMENT&&process.env.FASTIFY_STATIC_HEADERS?JSON.parse(String(process.env.FASTIFY_STATIC_HEADERS)):void 0;serverless.register(fastifyStatic,{root:["public","dist/browser"].map(dir=>join(process.cwd(),dir)),prefix:"/",wildcard:false,setHeaders:FASTIFY_STATIC_HEADERS?(reply,path)=>{for(const[suffix,headers]of Object.entries(FASTIFY_STATIC_HEADERS)){if(path.endsWith(suffix)){for(const[key,value]of Object.entries(headers)){reply.setHeader(key,value)}return}}}:void 0});serverless.all("*",async(request,reply)=>{let response;const requestPath=request.urlData().path;const pathSegments=requestPath.split("/").filter(segment=>segment!=="").reduce((acc,segment)=>{acc.push((acc.length>0?acc[acc.length-1]:"")+"/"+segment);return acc},[]).reverse().concat("");const generateEdges=path=>{const edges=[];if(path){const lastSegment=path.lastIndexOf("/")+1;edges.push(`${path.substring(0,lastSegment)}[${path.substring(lastSegment)}]`)}edges.push(`${path}/[index]`);return edges};for(const pathname of[...pathSegments.slice().reverse().map(segment=>`routes${segment}/[...guard].js`),...generateEdges(pathSegments[0]).map(segment=>`routes${segment}.js`),...pathSegments.map(segment=>`routes${segment}/[...path].js`),...pathSegments.map(segment=>`routes${segment}/[404].js`)]){const modulePath=join(process.cwd(),"dist",pathname);try{(await stat(modulePath)).isFile()}catch{continue}const hash=NODE_ENV_IS_DEVELOPMENT?"?"+createHash("sha1").update(await readFile(modulePath,"utf-8")).digest("hex"):"";response=await(await import(`file://${modulePath}${hash}`)).default({request,reply,...typeof response==="object"?response:{}});if(reply.sent){return}else if(pathname.endsWith("/[...guard].js")&&(response===void 0||!isJSX(response))){continue}else if(pathname.endsWith("/[404].js")){reply.status(404);break}else if(reply.statusCode===404){continue}else{break}}if(!reply.hasHeader("Content-Type")){reply.header("Content-Type","text/html; charset=utf-8")}const payload=isJSX(response)?await renderToString(response):response;const responseHandler=requestContext.get("response");return typeof responseHandler==="function"?await responseHandler(payload):payload;function isJSX(obj){return typeof obj==="object"&&"type"in obj&&"props"in obj}});var serverless_default=serverless;export{serverless_default as default};
|
package/serverless.ts
CHANGED
|
@@ -2,7 +2,9 @@ import fastifyAccepts from "@fastify/accepts";
|
|
|
2
2
|
import fastifyCookie from "@fastify/cookie";
|
|
3
3
|
import fastifyFormbody from "@fastify/formbody";
|
|
4
4
|
import fastifyMultipart from "@fastify/multipart";
|
|
5
|
-
import fastifyRequestContext
|
|
5
|
+
import fastifyRequestContext, {
|
|
6
|
+
requestContext,
|
|
7
|
+
} from "@fastify/request-context";
|
|
6
8
|
import fastifyStatic from "@fastify/static";
|
|
7
9
|
import fastifyUrlData from "@fastify/url-data";
|
|
8
10
|
import "dotenv/config";
|
|
@@ -12,6 +14,12 @@ import { createHash } from "node:crypto";
|
|
|
12
14
|
import { readFile, stat } from "node:fs/promises";
|
|
13
15
|
import { join } from "node:path";
|
|
14
16
|
|
|
17
|
+
declare module "@fastify/request-context" {
|
|
18
|
+
interface RequestContextData {
|
|
19
|
+
response?: (payload: unknown) => Promise<unknown>;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
15
23
|
const NODE_ENV_IS_DEVELOPMENT = process.env.NODE_ENV === "development";
|
|
16
24
|
|
|
17
25
|
// Create a Fastify app instance
|
|
@@ -57,7 +65,7 @@ serverless.register(fastifyStatic, {
|
|
|
57
65
|
|
|
58
66
|
// Handle all requests
|
|
59
67
|
serverless.all("*", async (request, reply) => {
|
|
60
|
-
let response;
|
|
68
|
+
let response: any;
|
|
61
69
|
|
|
62
70
|
// Extract pathname without query parameters
|
|
63
71
|
const requestPath = request.urlData().path;
|
|
@@ -74,7 +82,7 @@ serverless.all("*", async (request, reply) => {
|
|
|
74
82
|
.concat("");
|
|
75
83
|
|
|
76
84
|
// Transform "/a/b/c" into ["/a/b/[c]", "/a/b/c/[index]"]
|
|
77
|
-
const generateEdges = (path) => {
|
|
85
|
+
const generateEdges = (path: string) => {
|
|
78
86
|
const edges = [];
|
|
79
87
|
if (path) {
|
|
80
88
|
const lastSegment = path.lastIndexOf("/") + 1;
|
|
@@ -143,14 +151,13 @@ serverless.all("*", async (request, reply) => {
|
|
|
143
151
|
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
144
152
|
}
|
|
145
153
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
154
|
+
const payload = isJSX(response) ? await renderToString(response) : response;
|
|
155
|
+
const responseHandler = requestContext.get("response");
|
|
156
|
+
return typeof responseHandler === "function"
|
|
157
|
+
? await responseHandler(payload)
|
|
158
|
+
: payload;
|
|
152
159
|
|
|
153
|
-
function isJSX(obj) {
|
|
160
|
+
function isJSX(obj: any) {
|
|
154
161
|
return typeof obj === "object" && "type" in obj && "props" in obj;
|
|
155
162
|
}
|
|
156
163
|
});
|