jeasx 0.10.0 → 0.11.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 +6 -3
- package/serverless.js +27 -84
- package/serverless.ts +162 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jeasx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "jeasx - the ease of JSX with the power of SSR",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jsx",
|
|
@@ -30,11 +30,14 @@
|
|
|
30
30
|
"@fastify/multipart": "8.3.0",
|
|
31
31
|
"@fastify/static": "7.0.4",
|
|
32
32
|
"@fastify/url-data": "5.4.0",
|
|
33
|
-
"@types/node": "20.14.
|
|
33
|
+
"@types/node": "20.14.12",
|
|
34
34
|
"dotenv": "16.4.5",
|
|
35
35
|
"esbuild": "0.23.0",
|
|
36
36
|
"fastify": "4.28.1",
|
|
37
|
-
"jsx-async-runtime": "0.
|
|
37
|
+
"jsx-async-runtime": "0.5.0",
|
|
38
38
|
"pm2": "5.4.2"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "esbuild --platform=node --format=esm --outdir=. serverless.ts"
|
|
39
42
|
}
|
|
40
43
|
}
|
package/serverless.js
CHANGED
|
@@ -10,71 +10,43 @@ import { jsxToString } from "jsx-async-runtime";
|
|
|
10
10
|
import { createHash } from "node:crypto";
|
|
11
11
|
import { readFile, stat } from "node:fs/promises";
|
|
12
12
|
import { join } from "node:path";
|
|
13
|
-
|
|
14
13
|
const NODE_ENV_IS_DEVELOPMENT = process.env.NODE_ENV === "development";
|
|
15
|
-
|
|
16
|
-
// Create a Fastify app instance
|
|
17
14
|
const serverless = Fastify({
|
|
18
15
|
logger: true,
|
|
19
16
|
disableRequestLogging: NODE_ENV_IS_DEVELOPMENT,
|
|
20
|
-
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) ||
|
|
17
|
+
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) || void 0
|
|
21
18
|
});
|
|
22
|
-
|
|
23
|
-
// Register required plugins
|
|
24
19
|
serverless.register(fastifyAccepts);
|
|
25
20
|
serverless.register(fastifyCookie);
|
|
26
21
|
serverless.register(fastifyFormbody);
|
|
27
22
|
serverless.register(fastifyMultipart);
|
|
28
23
|
serverless.register(fastifyUrlData);
|
|
29
|
-
|
|
30
|
-
// Setup static file plugin
|
|
31
|
-
const FASTIFY_STATIC_HEADERS =
|
|
32
|
-
!NODE_ENV_IS_DEVELOPMENT && process.env.FASTIFY_STATIC_HEADERS
|
|
33
|
-
? JSON.parse(String(process.env.FASTIFY_STATIC_HEADERS))
|
|
34
|
-
: undefined;
|
|
35
|
-
|
|
24
|
+
const FASTIFY_STATIC_HEADERS = !NODE_ENV_IS_DEVELOPMENT && process.env.FASTIFY_STATIC_HEADERS ? JSON.parse(String(process.env.FASTIFY_STATIC_HEADERS)) : void 0;
|
|
36
25
|
serverless.register(fastifyStatic, {
|
|
37
26
|
root: ["public", "dist/browser"].map((dir) => join(process.cwd(), dir)),
|
|
38
27
|
prefix: "/",
|
|
39
28
|
wildcard: false,
|
|
40
|
-
setHeaders: FASTIFY_STATIC_HEADERS
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
)) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
reply.setHeader(key, value);
|
|
48
|
-
}
|
|
49
|
-
return;
|
|
29
|
+
setHeaders: FASTIFY_STATIC_HEADERS ? (reply, path) => {
|
|
30
|
+
for (const [suffix, headers] of Object.entries(
|
|
31
|
+
FASTIFY_STATIC_HEADERS
|
|
32
|
+
)) {
|
|
33
|
+
if (path.endsWith(suffix)) {
|
|
34
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
35
|
+
reply.setHeader(key, value);
|
|
50
36
|
}
|
|
37
|
+
return;
|
|
51
38
|
}
|
|
52
39
|
}
|
|
53
|
-
|
|
40
|
+
} : void 0
|
|
54
41
|
});
|
|
55
|
-
|
|
56
|
-
// Handle all requests
|
|
57
42
|
serverless.all("*", async (request, reply) => {
|
|
58
43
|
let response;
|
|
59
|
-
|
|
60
|
-
// Global context object for route handlers
|
|
61
44
|
const context = {};
|
|
62
|
-
|
|
63
|
-
// Extract pathname without query parameters
|
|
64
45
|
const requestPath = request.urlData().path;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
.filter((segment) => segment !== "")
|
|
70
|
-
.reduce((acc, segment) => {
|
|
71
|
-
acc.push((acc.length > 0 ? acc[acc.length - 1] : "") + "/" + segment);
|
|
72
|
-
return acc;
|
|
73
|
-
}, [])
|
|
74
|
-
.reverse()
|
|
75
|
-
.concat("");
|
|
76
|
-
|
|
77
|
-
// Transform "/a/b/c" into ["/a/b/[c]", "/a/b/c/[index]"]
|
|
46
|
+
const pathSegments = requestPath.split("/").filter((segment) => segment !== "").reduce((acc, segment) => {
|
|
47
|
+
acc.push((acc.length > 0 ? acc[acc.length - 1] : "") + "/" + segment);
|
|
48
|
+
return acc;
|
|
49
|
+
}, []).reverse().concat("");
|
|
78
50
|
const generateEdges = (path) => {
|
|
79
51
|
const edges = [];
|
|
80
52
|
if (path) {
|
|
@@ -86,48 +58,27 @@ serverless.all("*", async (request, reply) => {
|
|
|
86
58
|
edges.push(`${path}/[index]`);
|
|
87
59
|
return edges;
|
|
88
60
|
};
|
|
89
|
-
|
|
90
|
-
// Find route handler for the request
|
|
91
61
|
for (const pathname of [
|
|
92
|
-
...pathSegments
|
|
93
|
-
.slice()
|
|
94
|
-
.reverse() // [...guard]s are evaluated from top to bottom
|
|
95
|
-
.map((segment) => `routes${segment}/[...guard].js`),
|
|
62
|
+
...pathSegments.slice().reverse().map((segment) => `routes${segment}/[...guard].js`),
|
|
96
63
|
...generateEdges(pathSegments[0]).map((segment) => `routes${segment}.js`),
|
|
97
64
|
...pathSegments.map((segment) => `routes${segment}/[...path].js`),
|
|
98
|
-
...pathSegments.map((segment) => `routes${segment}/[404].js`)
|
|
65
|
+
...pathSegments.map((segment) => `routes${segment}/[404].js`)
|
|
99
66
|
]) {
|
|
100
67
|
const modulePath = join(process.cwd(), "dist", pathname);
|
|
101
|
-
|
|
102
68
|
try {
|
|
103
69
|
(await stat(modulePath)).isFile();
|
|
104
70
|
} catch {
|
|
105
71
|
continue;
|
|
106
72
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const hash = NODE_ENV_IS_DEVELOPMENT
|
|
110
|
-
? "?" +
|
|
111
|
-
createHash("sha1")
|
|
112
|
-
.update(await readFile(modulePath, "utf-8"))
|
|
113
|
-
.digest("hex")
|
|
114
|
-
: "";
|
|
115
|
-
|
|
116
|
-
// Call the handler with request, reply and optional props
|
|
117
|
-
response = await (
|
|
118
|
-
await import(`file://${modulePath}${hash}`)
|
|
119
|
-
).default.call(context, {
|
|
73
|
+
const hash = NODE_ENV_IS_DEVELOPMENT ? "?" + createHash("sha1").update(await readFile(modulePath, "utf-8")).digest("hex") : "";
|
|
74
|
+
response = await (await import(`file://${modulePath}${hash}`)).default.call(context, {
|
|
120
75
|
request,
|
|
121
76
|
reply,
|
|
122
|
-
...
|
|
77
|
+
...typeof response === "object" ? response : {}
|
|
123
78
|
});
|
|
124
|
-
|
|
125
79
|
if (reply.sent) {
|
|
126
80
|
return;
|
|
127
|
-
} else if (
|
|
128
|
-
pathname.endsWith("/[...guard].js") &&
|
|
129
|
-
(response === undefined || !isJSX(response))
|
|
130
|
-
) {
|
|
81
|
+
} else if (pathname.endsWith("/[...guard].js") && (response === void 0 || !isJSX(response))) {
|
|
131
82
|
continue;
|
|
132
83
|
} else if (pathname.endsWith("/[404].js")) {
|
|
133
84
|
reply.status(404);
|
|
@@ -138,25 +89,17 @@ serverless.all("*", async (request, reply) => {
|
|
|
138
89
|
break;
|
|
139
90
|
}
|
|
140
91
|
}
|
|
141
|
-
|
|
142
|
-
// Make sure a Content-Type header is set
|
|
143
92
|
if (!reply.hasHeader("Content-Type")) {
|
|
144
93
|
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
145
94
|
}
|
|
146
|
-
|
|
147
|
-
const payload = isJSX(response)
|
|
148
|
-
? await jsxToString.call(context, response)
|
|
149
|
-
: response;
|
|
150
|
-
|
|
151
|
-
// Post-process the payload with an optional response handler
|
|
95
|
+
const payload = isJSX(response) ? await jsxToString.call(context, response) : response;
|
|
152
96
|
const responseHandler = context["response"];
|
|
153
|
-
return typeof responseHandler === "function"
|
|
154
|
-
? await responseHandler(payload)
|
|
155
|
-
: payload;
|
|
156
|
-
|
|
97
|
+
return typeof responseHandler === "function" ? await responseHandler(payload) : payload;
|
|
157
98
|
function isJSX(obj) {
|
|
158
99
|
return typeof obj === "object" && "type" in obj && "props" in obj;
|
|
159
100
|
}
|
|
160
101
|
});
|
|
161
|
-
|
|
162
|
-
export
|
|
102
|
+
var serverless_default = serverless;
|
|
103
|
+
export {
|
|
104
|
+
serverless_default as default
|
|
105
|
+
};
|
package/serverless.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
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 fastifyStatic from "@fastify/static";
|
|
6
|
+
import fastifyUrlData from "@fastify/url-data";
|
|
7
|
+
import "dotenv/config";
|
|
8
|
+
import Fastify from "fastify";
|
|
9
|
+
import { jsxToString } from "jsx-async-runtime";
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { readFile, stat } from "node:fs/promises";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
|
|
14
|
+
const NODE_ENV_IS_DEVELOPMENT = process.env.NODE_ENV === "development";
|
|
15
|
+
|
|
16
|
+
// Create a Fastify app instance
|
|
17
|
+
const serverless = Fastify({
|
|
18
|
+
logger: true,
|
|
19
|
+
disableRequestLogging: NODE_ENV_IS_DEVELOPMENT,
|
|
20
|
+
bodyLimit: Number(process.env.FASTIFY_BODY_LIMIT) || undefined,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Register required plugins
|
|
24
|
+
serverless.register(fastifyAccepts);
|
|
25
|
+
serverless.register(fastifyCookie);
|
|
26
|
+
serverless.register(fastifyFormbody);
|
|
27
|
+
serverless.register(fastifyMultipart);
|
|
28
|
+
serverless.register(fastifyUrlData);
|
|
29
|
+
|
|
30
|
+
// Setup static file plugin
|
|
31
|
+
const FASTIFY_STATIC_HEADERS =
|
|
32
|
+
!NODE_ENV_IS_DEVELOPMENT && process.env.FASTIFY_STATIC_HEADERS
|
|
33
|
+
? JSON.parse(String(process.env.FASTIFY_STATIC_HEADERS))
|
|
34
|
+
: undefined;
|
|
35
|
+
|
|
36
|
+
serverless.register(fastifyStatic, {
|
|
37
|
+
root: ["public", "dist/browser"].map((dir) => join(process.cwd(), dir)),
|
|
38
|
+
prefix: "/",
|
|
39
|
+
wildcard: false,
|
|
40
|
+
setHeaders: FASTIFY_STATIC_HEADERS
|
|
41
|
+
? (reply, path) => {
|
|
42
|
+
for (const [suffix, headers] of Object.entries(
|
|
43
|
+
FASTIFY_STATIC_HEADERS
|
|
44
|
+
)) {
|
|
45
|
+
if (path.endsWith(suffix)) {
|
|
46
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
47
|
+
reply.setHeader(key, value);
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Handle all requests
|
|
57
|
+
serverless.all("*", async (request, reply) => {
|
|
58
|
+
let response;
|
|
59
|
+
|
|
60
|
+
// Global context object for route handlers
|
|
61
|
+
const context = {};
|
|
62
|
+
|
|
63
|
+
// Extract pathname without query parameters
|
|
64
|
+
const requestPath = request.urlData().path;
|
|
65
|
+
|
|
66
|
+
// Transform "/a/b/c" into ["/a/b/c", "/a/b", "/a", ""]
|
|
67
|
+
const pathSegments = requestPath
|
|
68
|
+
.split("/")
|
|
69
|
+
.filter((segment) => segment !== "")
|
|
70
|
+
.reduce((acc, segment) => {
|
|
71
|
+
acc.push((acc.length > 0 ? acc[acc.length - 1] : "") + "/" + segment);
|
|
72
|
+
return acc;
|
|
73
|
+
}, [])
|
|
74
|
+
.reverse()
|
|
75
|
+
.concat("");
|
|
76
|
+
|
|
77
|
+
// Transform "/a/b/c" into ["/a/b/[c]", "/a/b/c/[index]"]
|
|
78
|
+
const generateEdges = (path) => {
|
|
79
|
+
const edges = [];
|
|
80
|
+
if (path) {
|
|
81
|
+
const lastSegment = path.lastIndexOf("/") + 1;
|
|
82
|
+
edges.push(
|
|
83
|
+
`${path.substring(0, lastSegment)}[${path.substring(lastSegment)}]`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
edges.push(`${path}/[index]`);
|
|
87
|
+
return edges;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// Find route handler for the request
|
|
91
|
+
for (const pathname of [
|
|
92
|
+
...pathSegments
|
|
93
|
+
.slice()
|
|
94
|
+
.reverse() // [...guard]s are evaluated from top to bottom
|
|
95
|
+
.map((segment) => `routes${segment}/[...guard].js`),
|
|
96
|
+
...generateEdges(pathSegments[0]).map((segment) => `routes${segment}.js`),
|
|
97
|
+
...pathSegments.map((segment) => `routes${segment}/[...path].js`),
|
|
98
|
+
...pathSegments.map((segment) => `routes${segment}/[404].js`),
|
|
99
|
+
]) {
|
|
100
|
+
const modulePath = join(process.cwd(), "dist", pathname);
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
(await stat(modulePath)).isFile();
|
|
104
|
+
} catch {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Build content hash in development, so we can refresh code via "query string hack".
|
|
109
|
+
const hash = NODE_ENV_IS_DEVELOPMENT
|
|
110
|
+
? "?" +
|
|
111
|
+
createHash("sha1")
|
|
112
|
+
.update(await readFile(modulePath, "utf-8"))
|
|
113
|
+
.digest("hex")
|
|
114
|
+
: "";
|
|
115
|
+
|
|
116
|
+
// Call the handler with request, reply and optional props
|
|
117
|
+
response = await (
|
|
118
|
+
await import(`file://${modulePath}${hash}`)
|
|
119
|
+
).default.call(context, {
|
|
120
|
+
request,
|
|
121
|
+
reply,
|
|
122
|
+
...(typeof response === "object" ? response : {}),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (reply.sent) {
|
|
126
|
+
return;
|
|
127
|
+
} else if (
|
|
128
|
+
pathname.endsWith("/[...guard].js") &&
|
|
129
|
+
(response === undefined || !isJSX(response))
|
|
130
|
+
) {
|
|
131
|
+
continue;
|
|
132
|
+
} else if (pathname.endsWith("/[404].js")) {
|
|
133
|
+
reply.status(404);
|
|
134
|
+
break;
|
|
135
|
+
} else if (reply.statusCode === 404) {
|
|
136
|
+
continue;
|
|
137
|
+
} else {
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Make sure a Content-Type header is set
|
|
143
|
+
if (!reply.hasHeader("Content-Type")) {
|
|
144
|
+
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const payload = isJSX(response)
|
|
148
|
+
? await jsxToString.call(context, response)
|
|
149
|
+
: response;
|
|
150
|
+
|
|
151
|
+
// Post-process the payload with an optional response handler
|
|
152
|
+
const responseHandler = context["response"];
|
|
153
|
+
return typeof responseHandler === "function"
|
|
154
|
+
? await responseHandler(payload)
|
|
155
|
+
: payload;
|
|
156
|
+
|
|
157
|
+
function isJSX(obj) {
|
|
158
|
+
return typeof obj === "object" && "type" in obj && "props" in obj;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
export default serverless;
|