jeasx 0.1.1 → 0.2.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 +3 -3
- package/serverless.js +1 -1
- package/serverless.ts +10 -9
- /package/{CHANGELOG → CHANGELOG.md} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jeasx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "jeasx - the ease of JSX with the power of SSR",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jsx",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"@fastify/request-context": "5.1.0",
|
|
31
31
|
"@fastify/static": "6.12.0",
|
|
32
32
|
"@fastify/url-data": "5.4.0",
|
|
33
|
-
"@types/node": "20.11.
|
|
33
|
+
"@types/node": "20.11.10",
|
|
34
34
|
"dotenv": "16.4.1",
|
|
35
|
-
"esbuild": "0.
|
|
35
|
+
"esbuild": "0.20.0",
|
|
36
36
|
"fastify": "4.25.2",
|
|
37
37
|
"jsx-async-runtime": "0.1.3",
|
|
38
38
|
"pm2": "5.3.1"
|
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 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 serverless=Fastify({logger:true,disableRequestLogging:
|
|
1
|
+
import fastifyAccepts from"@fastify/accepts";import fastifyCookie from"@fastify/cookie";import fastifyFormbody from"@fastify/formbody";import fastifyMultipart from"@fastify/multipart";import fastifyRequestContext 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(`${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")}if(isJSX(response)){return await renderToString(response)}else{return response}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
|
@@ -12,10 +12,12 @@ import { createHash } from "node:crypto";
|
|
|
12
12
|
import { readFile, stat } from "node:fs/promises";
|
|
13
13
|
import { join } from "node:path";
|
|
14
14
|
|
|
15
|
+
const NODE_ENV_IS_DEVELOPMENT = process.env.NODE_ENV === "development";
|
|
16
|
+
|
|
15
17
|
// Create a Fastify app instance
|
|
16
18
|
const serverless = Fastify({
|
|
17
19
|
logger: true,
|
|
18
|
-
disableRequestLogging:
|
|
20
|
+
disableRequestLogging: NODE_ENV_IS_DEVELOPMENT,
|
|
19
21
|
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) || undefined,
|
|
20
22
|
});
|
|
21
23
|
|
|
@@ -29,7 +31,7 @@ serverless.register(fastifyUrlData);
|
|
|
29
31
|
|
|
30
32
|
// Setup static file plugin
|
|
31
33
|
const FASTIFY_STATIC_HEADERS =
|
|
32
|
-
|
|
34
|
+
!NODE_ENV_IS_DEVELOPMENT && process.env.FASTIFY_STATIC_HEADERS
|
|
33
35
|
? JSON.parse(String(process.env.FASTIFY_STATIC_HEADERS))
|
|
34
36
|
: undefined;
|
|
35
37
|
|
|
@@ -103,13 +105,12 @@ serverless.all("*", async (request, reply) => {
|
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
// Build content hash in development, so we can refresh code via "query string hack".
|
|
106
|
-
const hash =
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
: "";
|
|
108
|
+
const hash = NODE_ENV_IS_DEVELOPMENT
|
|
109
|
+
? "?" +
|
|
110
|
+
createHash("sha1")
|
|
111
|
+
.update(await readFile(modulePath, "utf-8"))
|
|
112
|
+
.digest("hex")
|
|
113
|
+
: "";
|
|
113
114
|
|
|
114
115
|
// Call the handler with request, reply and optional props
|
|
115
116
|
response = await (
|
|
File without changes
|