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