jeasx 0.0.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/LICENSE.txt +21 -0
- package/README.md +9 -0
- package/ecosystem.config.cjs +43 -0
- package/esbuild.config.js +104 -0
- package/package.json +34 -0
- package/server.js +6 -0
- package/serverless.js +136 -0
- package/tsconfig.json +16 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Maik Jablonski (<mail@mjablonski.de>)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
apps: [
|
|
3
|
+
{
|
|
4
|
+
name: "jeasx:start:server",
|
|
5
|
+
script: "node_modules/jeasx/server.js",
|
|
6
|
+
autorestart: true,
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
name: "jeasx:build:routes",
|
|
10
|
+
script: "node_modules/jeasx/esbuild.config.js",
|
|
11
|
+
args: "routes",
|
|
12
|
+
watch: [
|
|
13
|
+
"src/**/*.js",
|
|
14
|
+
"src/**/*.jsx",
|
|
15
|
+
"src/**/*.ts",
|
|
16
|
+
"src/**/*.tsx",
|
|
17
|
+
"src/**/*.json",
|
|
18
|
+
],
|
|
19
|
+
ignore_watch: ["src/browser"],
|
|
20
|
+
autorestart: false,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "jeasx:build:js",
|
|
24
|
+
script: "node_modules/jeasx/esbuild.config.js",
|
|
25
|
+
args: "js",
|
|
26
|
+
watch: [
|
|
27
|
+
"src/browser/**/*.js",
|
|
28
|
+
"src/browser/**/*.jsx",
|
|
29
|
+
"src/browser/**/*.ts",
|
|
30
|
+
"src/browser/**/*.tsx",
|
|
31
|
+
"src/browser/**/*.json",
|
|
32
|
+
],
|
|
33
|
+
autorestart: false,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "jeasx:build:css",
|
|
37
|
+
script: "node_modules/jeasx/esbuild.config.js",
|
|
38
|
+
args: "css",
|
|
39
|
+
watch: ["src/browser/**/*.css"],
|
|
40
|
+
autorestart: false,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import * as esbuild from "esbuild";
|
|
2
|
+
|
|
3
|
+
const BUILD_TIME =
|
|
4
|
+
process.env.NODE_ENV === "development"
|
|
5
|
+
? `"snapshot"`
|
|
6
|
+
: `"${Date.now().toString(36)}"`;
|
|
7
|
+
|
|
8
|
+
const BROWSER_PUBLIC_ENV = Object.keys(process.env)
|
|
9
|
+
.filter((key) => key.startsWith("BROWSER_PUBLIC_"))
|
|
10
|
+
.reduce(
|
|
11
|
+
(env, key) => {
|
|
12
|
+
env[`process.env.${key}`] = `"${process.env[key]}"`;
|
|
13
|
+
return env;
|
|
14
|
+
},
|
|
15
|
+
{ "process.env.BROWSER_PUBLIC_BUILD_TIME": BUILD_TIME }
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const args = process.argv.slice(2);
|
|
19
|
+
|
|
20
|
+
const builds = [];
|
|
21
|
+
for (const arg of args.length ? args : ["routes", "js", "css"]) {
|
|
22
|
+
switch (arg) {
|
|
23
|
+
case "routes":
|
|
24
|
+
builds.push(
|
|
25
|
+
buildServer(
|
|
26
|
+
"src/routes/**/[*].js",
|
|
27
|
+
"src/routes/**/[*].ts",
|
|
28
|
+
"src/routes/**/[*].jsx",
|
|
29
|
+
"src/routes/**/[*].tsx"
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
break;
|
|
33
|
+
case "js":
|
|
34
|
+
builds.push(
|
|
35
|
+
buildBrowser(
|
|
36
|
+
"src/browser/**/index.js",
|
|
37
|
+
"src/browser/**/index.ts",
|
|
38
|
+
"src/browser/**/index.jsx",
|
|
39
|
+
"src/browser/**/index.tsx"
|
|
40
|
+
)
|
|
41
|
+
);
|
|
42
|
+
break;
|
|
43
|
+
case "css":
|
|
44
|
+
builds.push(buildBrowser("src/browser/**/index.css"));
|
|
45
|
+
break;
|
|
46
|
+
default:
|
|
47
|
+
console.info(`Error: Unknown argument '${arg}'.`);
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
await Promise.all(builds);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {string[]} entryPoints
|
|
56
|
+
*/
|
|
57
|
+
function buildServer(...entryPoints) {
|
|
58
|
+
esbuild.build({
|
|
59
|
+
entryPoints,
|
|
60
|
+
define: {
|
|
61
|
+
"process.env.BUILD_TIME": BUILD_TIME,
|
|
62
|
+
},
|
|
63
|
+
minify: process.env.NODE_ENV !== "development",
|
|
64
|
+
logLevel: "info",
|
|
65
|
+
color: true,
|
|
66
|
+
bundle: true,
|
|
67
|
+
outbase: "src",
|
|
68
|
+
outdir: "dist",
|
|
69
|
+
platform: "neutral",
|
|
70
|
+
packages: "external",
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {string[]} entryPoints
|
|
76
|
+
*/
|
|
77
|
+
function buildBrowser(...entryPoints) {
|
|
78
|
+
esbuild.build({
|
|
79
|
+
entryPoints,
|
|
80
|
+
define: BROWSER_PUBLIC_ENV,
|
|
81
|
+
minify: process.env.NODE_ENV !== "development",
|
|
82
|
+
logLevel: "info",
|
|
83
|
+
color: true,
|
|
84
|
+
bundle: true,
|
|
85
|
+
outbase: "src/browser",
|
|
86
|
+
outdir: "dist/browser",
|
|
87
|
+
platform: "browser",
|
|
88
|
+
format: "esm",
|
|
89
|
+
target: ["chrome109", "edge112", "firefox102", "safari16"],
|
|
90
|
+
external: [
|
|
91
|
+
"*.avif",
|
|
92
|
+
"*.gif",
|
|
93
|
+
"*.jpg",
|
|
94
|
+
"*.jpeg",
|
|
95
|
+
"*.png",
|
|
96
|
+
"*.svg",
|
|
97
|
+
"*.webp",
|
|
98
|
+
"*.ttf",
|
|
99
|
+
"*.otf",
|
|
100
|
+
"*.woff",
|
|
101
|
+
"*.woff2",
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jeasx",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Jeasx - server side rendering with the ease of JSX",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jsx",
|
|
7
|
+
"ssr"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"url": "git+https://github.com/jeasx/jeasx.git"
|
|
12
|
+
},
|
|
13
|
+
"author": {
|
|
14
|
+
"name": "Maik Jablonski",
|
|
15
|
+
"email": "mail@mjablonski.de"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "server.js",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@fastify/accepts": "4.3.0",
|
|
21
|
+
"@fastify/cookie": "9.2.0",
|
|
22
|
+
"@fastify/formbody": "7.4.0",
|
|
23
|
+
"@fastify/multipart": "8.0.0",
|
|
24
|
+
"@fastify/request-context": "5.0.0",
|
|
25
|
+
"@fastify/static": "6.12.0",
|
|
26
|
+
"@fastify/url-data": "5.4.0",
|
|
27
|
+
"@types/node": "20.10.6",
|
|
28
|
+
"dotenv": "16.3.1",
|
|
29
|
+
"esbuild": "0.19.11",
|
|
30
|
+
"fastify": "4.25.2",
|
|
31
|
+
"jsx-async-runtime": "0.1.2",
|
|
32
|
+
"pm2": "5.3.0"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/server.js
ADDED
package/serverless.js
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;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "ESNext",
|
|
4
|
+
"moduleResolution": "Node",
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"allowJs": true,
|
|
8
|
+
"checkJs": true,
|
|
9
|
+
"jsx": "react-jsx",
|
|
10
|
+
"jsxImportSource": "jsx-async-runtime",
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"resolveJsonModule": true
|
|
15
|
+
}
|
|
16
|
+
}
|