jeasx 0.0.1 → 0.0.2
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/README.md +1 -7
- package/package.json +4 -1
- package/serverless.js +16 -58
- package/serverless.ts +136 -0
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jeasx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Jeasx - server side rendering with the ease of JSX",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jsx",
|
|
@@ -30,5 +30,8 @@
|
|
|
30
30
|
"fastify": "4.25.2",
|
|
31
31
|
"jsx-async-runtime": "0.1.2",
|
|
32
32
|
"pm2": "5.3.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "esbuild --platform=node --format=esm --outfile=serverless.js serverless.ts"
|
|
33
36
|
}
|
|
34
37
|
}
|
package/serverless.js
CHANGED
|
@@ -11,15 +11,11 @@ import { renderToString } from "jsx-async-runtime";
|
|
|
11
11
|
import { createHash } from "node:crypto";
|
|
12
12
|
import { readFile, stat } from "node:fs/promises";
|
|
13
13
|
import { join } from "node:path";
|
|
14
|
-
|
|
15
|
-
// Create a Fastify app instance
|
|
16
14
|
const serverless = Fastify({
|
|
17
15
|
logger: true,
|
|
18
16
|
disableRequestLogging: process.env.NODE_ENV === "development",
|
|
19
|
-
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) ||
|
|
17
|
+
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) || void 0
|
|
20
18
|
});
|
|
21
|
-
|
|
22
|
-
// Register required plugins
|
|
23
19
|
serverless.register(fastifyAccepts);
|
|
24
20
|
serverless.register(fastifyCookie);
|
|
25
21
|
serverless.register(fastifyFormbody);
|
|
@@ -29,28 +25,15 @@ serverless.register(fastifyUrlData);
|
|
|
29
25
|
serverless.register(fastifyStatic, {
|
|
30
26
|
root: ["public", "dist/browser"].map((dir) => join(process.cwd(), dir)),
|
|
31
27
|
prefix: "/",
|
|
32
|
-
wildcard: false
|
|
28
|
+
wildcard: false
|
|
33
29
|
});
|
|
34
|
-
|
|
35
|
-
// Handle all requests
|
|
36
30
|
serverless.all("*", async (request, reply) => {
|
|
37
31
|
let response;
|
|
38
|
-
|
|
39
|
-
// Extract pathname without query parameters
|
|
40
32
|
const requestPath = request.urlData().path;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.filter((segment) => segment !== "")
|
|
46
|
-
.reduce((acc, segment) => {
|
|
47
|
-
acc.push((acc.length > 0 ? acc[acc.length - 1] : "") + "/" + segment);
|
|
48
|
-
return acc;
|
|
49
|
-
}, [])
|
|
50
|
-
.reverse()
|
|
51
|
-
.concat("");
|
|
52
|
-
|
|
53
|
-
// Transform "/a/b/c" into ["/a/b/[c]", "/a/b/c/[index]"]
|
|
33
|
+
const pathSegments = requestPath.split("/").filter((segment) => segment !== "").reduce((acc, segment) => {
|
|
34
|
+
acc.push((acc.length > 0 ? acc[acc.length - 1] : "") + "/" + segment);
|
|
35
|
+
return acc;
|
|
36
|
+
}, []).reverse().concat("");
|
|
54
37
|
const generateEdges = (path) => {
|
|
55
38
|
const edges = [];
|
|
56
39
|
if (path) {
|
|
@@ -62,49 +45,27 @@ serverless.all("*", async (request, reply) => {
|
|
|
62
45
|
edges.push(`${path}/[index]`);
|
|
63
46
|
return edges;
|
|
64
47
|
};
|
|
65
|
-
|
|
66
|
-
// Find route handler for the request
|
|
67
48
|
for (const pathname of [
|
|
68
|
-
...pathSegments
|
|
69
|
-
.slice()
|
|
70
|
-
.reverse() // [...guard]s are evaluated from top to bottom
|
|
71
|
-
.map((segment) => `routes${segment}/[...guard].js`),
|
|
49
|
+
...pathSegments.slice().reverse().map((segment) => `routes${segment}/[...guard].js`),
|
|
72
50
|
...generateEdges(pathSegments[0]).map((segment) => `routes${segment}.js`),
|
|
73
51
|
...pathSegments.map((segment) => `routes${segment}/[...path].js`),
|
|
74
|
-
...pathSegments.map((segment) => `routes${segment}/[404].js`)
|
|
52
|
+
...pathSegments.map((segment) => `routes${segment}/[404].js`)
|
|
75
53
|
]) {
|
|
76
54
|
const modulePath = join(process.cwd(), "dist", pathname);
|
|
77
|
-
|
|
78
55
|
try {
|
|
79
56
|
(await stat(modulePath)).isFile();
|
|
80
57
|
} catch (error) {
|
|
81
58
|
continue;
|
|
82
59
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const hash =
|
|
86
|
-
process.env.NODE_ENV === "development"
|
|
87
|
-
? "?" +
|
|
88
|
-
createHash("sha1")
|
|
89
|
-
.update(await readFile(modulePath, "utf-8"))
|
|
90
|
-
.digest("hex")
|
|
91
|
-
: "";
|
|
92
|
-
|
|
93
|
-
// Call the handler with request, reply and optional props
|
|
94
|
-
response = await (
|
|
95
|
-
await import(`${modulePath}${hash}`)
|
|
96
|
-
).default({
|
|
60
|
+
const hash = process.env.NODE_ENV === "development" ? "?" + createHash("sha1").update(await readFile(modulePath, "utf-8")).digest("hex") : "";
|
|
61
|
+
response = await (await import(`${modulePath}${hash}`)).default({
|
|
97
62
|
request,
|
|
98
63
|
reply,
|
|
99
|
-
...
|
|
64
|
+
...typeof response === "object" ? response : {}
|
|
100
65
|
});
|
|
101
|
-
|
|
102
66
|
if (reply.sent) {
|
|
103
67
|
return;
|
|
104
|
-
} else if (
|
|
105
|
-
pathname.endsWith("/[...guard].js") &&
|
|
106
|
-
(response === undefined || !isJSX(response))
|
|
107
|
-
) {
|
|
68
|
+
} else if (pathname.endsWith("/[...guard].js") && (response === void 0 || !isJSX(response))) {
|
|
108
69
|
continue;
|
|
109
70
|
} else if (pathname.endsWith("/[404].js")) {
|
|
110
71
|
reply.status(404);
|
|
@@ -115,22 +76,19 @@ serverless.all("*", async (request, reply) => {
|
|
|
115
76
|
break;
|
|
116
77
|
}
|
|
117
78
|
}
|
|
118
|
-
|
|
119
|
-
// Make sure a Content-Type header is set
|
|
120
79
|
if (!reply.hasHeader("Content-Type")) {
|
|
121
80
|
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
122
81
|
}
|
|
123
|
-
|
|
124
|
-
// Render JSX or return raw response
|
|
125
82
|
if (isJSX(response)) {
|
|
126
83
|
return await renderToString(response);
|
|
127
84
|
} else {
|
|
128
85
|
return response;
|
|
129
86
|
}
|
|
130
|
-
|
|
131
87
|
function isJSX(obj) {
|
|
132
88
|
return typeof obj === "object" && "type" in obj && "props" in obj;
|
|
133
89
|
}
|
|
134
90
|
});
|
|
135
|
-
|
|
136
|
-
export
|
|
91
|
+
var serverless_default = serverless;
|
|
92
|
+
export {
|
|
93
|
+
serverless_default as default
|
|
94
|
+
};
|
package/serverless.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import fastifyAccepts from "@fastify/accepts";
|
|
2
|
+
import fastifyCookie from "@fastify/cookie";
|
|
3
|
+
import fastifyFormbody from "@fastify/formbody";
|
|
4
|
+
import fastifyMultipart from "@fastify/multipart";
|
|
5
|
+
import fastifyRequestContext from "@fastify/request-context";
|
|
6
|
+
import fastifyStatic from "@fastify/static";
|
|
7
|
+
import fastifyUrlData from "@fastify/url-data";
|
|
8
|
+
import "dotenv/config";
|
|
9
|
+
import Fastify from "fastify";
|
|
10
|
+
import { renderToString } from "jsx-async-runtime";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { readFile, stat } from "node:fs/promises";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
|
|
15
|
+
// Create a Fastify app instance
|
|
16
|
+
const serverless = Fastify({
|
|
17
|
+
logger: true,
|
|
18
|
+
disableRequestLogging: process.env.NODE_ENV === "development",
|
|
19
|
+
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) || undefined,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Register required plugins
|
|
23
|
+
serverless.register(fastifyAccepts);
|
|
24
|
+
serverless.register(fastifyCookie);
|
|
25
|
+
serverless.register(fastifyFormbody);
|
|
26
|
+
serverless.register(fastifyMultipart);
|
|
27
|
+
serverless.register(fastifyRequestContext);
|
|
28
|
+
serverless.register(fastifyUrlData);
|
|
29
|
+
serverless.register(fastifyStatic, {
|
|
30
|
+
root: ["public", "dist/browser"].map((dir) => join(process.cwd(), dir)),
|
|
31
|
+
prefix: "/",
|
|
32
|
+
wildcard: false,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Handle all requests
|
|
36
|
+
serverless.all("*", async (request, reply) => {
|
|
37
|
+
let response;
|
|
38
|
+
|
|
39
|
+
// Extract pathname without query parameters
|
|
40
|
+
const requestPath = request.urlData().path;
|
|
41
|
+
|
|
42
|
+
// Transform "/a/b/c" into ["/a/b/c", "/a/b", "/a", ""]
|
|
43
|
+
const pathSegments = requestPath
|
|
44
|
+
.split("/")
|
|
45
|
+
.filter((segment) => segment !== "")
|
|
46
|
+
.reduce((acc, segment) => {
|
|
47
|
+
acc.push((acc.length > 0 ? acc[acc.length - 1] : "") + "/" + segment);
|
|
48
|
+
return acc;
|
|
49
|
+
}, [])
|
|
50
|
+
.reverse()
|
|
51
|
+
.concat("");
|
|
52
|
+
|
|
53
|
+
// Transform "/a/b/c" into ["/a/b/[c]", "/a/b/c/[index]"]
|
|
54
|
+
const generateEdges = (path) => {
|
|
55
|
+
const edges = [];
|
|
56
|
+
if (path) {
|
|
57
|
+
const lastSegment = path.lastIndexOf("/") + 1;
|
|
58
|
+
edges.push(
|
|
59
|
+
`${path.substring(0, lastSegment)}[${path.substring(lastSegment)}]`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
edges.push(`${path}/[index]`);
|
|
63
|
+
return edges;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Find route handler for the request
|
|
67
|
+
for (const pathname of [
|
|
68
|
+
...pathSegments
|
|
69
|
+
.slice()
|
|
70
|
+
.reverse() // [...guard]s are evaluated from top to bottom
|
|
71
|
+
.map((segment) => `routes${segment}/[...guard].js`),
|
|
72
|
+
...generateEdges(pathSegments[0]).map((segment) => `routes${segment}.js`),
|
|
73
|
+
...pathSegments.map((segment) => `routes${segment}/[...path].js`),
|
|
74
|
+
...pathSegments.map((segment) => `routes${segment}/[404].js`),
|
|
75
|
+
]) {
|
|
76
|
+
const modulePath = join(process.cwd(), "dist", pathname);
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
(await stat(modulePath)).isFile();
|
|
80
|
+
} catch (error) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Build content hash in development, so we can refresh code via "query string hack".
|
|
85
|
+
const hash =
|
|
86
|
+
process.env.NODE_ENV === "development"
|
|
87
|
+
? "?" +
|
|
88
|
+
createHash("sha1")
|
|
89
|
+
.update(await readFile(modulePath, "utf-8"))
|
|
90
|
+
.digest("hex")
|
|
91
|
+
: "";
|
|
92
|
+
|
|
93
|
+
// Call the handler with request, reply and optional props
|
|
94
|
+
response = await (
|
|
95
|
+
await import(`${modulePath}${hash}`)
|
|
96
|
+
).default({
|
|
97
|
+
request,
|
|
98
|
+
reply,
|
|
99
|
+
...(typeof response === "object" ? response : {}),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (reply.sent) {
|
|
103
|
+
return;
|
|
104
|
+
} else if (
|
|
105
|
+
pathname.endsWith("/[...guard].js") &&
|
|
106
|
+
(response === undefined || !isJSX(response))
|
|
107
|
+
) {
|
|
108
|
+
continue;
|
|
109
|
+
} else if (pathname.endsWith("/[404].js")) {
|
|
110
|
+
reply.status(404);
|
|
111
|
+
break;
|
|
112
|
+
} else if (reply.statusCode === 404) {
|
|
113
|
+
continue;
|
|
114
|
+
} else {
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Make sure a Content-Type header is set
|
|
120
|
+
if (!reply.hasHeader("Content-Type")) {
|
|
121
|
+
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Render JSX or return raw response
|
|
125
|
+
if (isJSX(response)) {
|
|
126
|
+
return await renderToString(response);
|
|
127
|
+
} else {
|
|
128
|
+
return response;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isJSX(obj) {
|
|
132
|
+
return typeof obj === "object" && "type" in obj && "props" in obj;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
export default serverless;
|