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