arcanajs 2.0.3 → 2.1.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/bin/arcanajs.js +3 -0
- package/package.json +31 -37
- package/babel.config.js +0 -7
- package/lib/client/index.d.ts +0 -2
- package/lib/client/index.js +0 -40
- package/lib/index.d.ts +0 -17
- package/lib/index.js +0 -33
- package/lib/server/ArcanaJSMiddleware.d.ts +0 -16
- package/lib/server/ArcanaJSMiddleware.js +0 -99
- package/lib/server/ArcanaJSServer.d.ts +0 -21
- package/lib/server/ArcanaJSServer.js +0 -144
- package/lib/server/ControllerBinder.d.ts +0 -4
- package/lib/server/ControllerBinder.js +0 -32
- package/lib/server/CsrfMiddleware.d.ts +0 -2
- package/lib/server/CsrfMiddleware.js +0 -34
- package/lib/server/DynamicRouter.d.ts +0 -2
- package/lib/server/DynamicRouter.js +0 -50
- package/lib/server/ResponseHandlerMiddleware.d.ts +0 -27
- package/lib/server/ResponseHandlerMiddleware.js +0 -30
- package/lib/server/Router.d.ts +0 -94
- package/lib/server/Router.js +0 -203
- package/lib/server.d.ts +0 -6
- package/lib/server.js +0 -28
- package/lib/shared/ArcanaJSApp.d.ts +0 -11
- package/lib/shared/ArcanaJSApp.js +0 -79
- package/lib/shared/components/Body.d.ts +0 -6
- package/lib/shared/components/Body.js +0 -11
- package/lib/shared/components/Head.d.ts +0 -4
- package/lib/shared/components/Head.js +0 -125
- package/lib/shared/components/Link.d.ts +0 -6
- package/lib/shared/components/Link.js +0 -17
- package/lib/shared/components/NavLink.d.ts +0 -8
- package/lib/shared/components/NavLink.js +0 -16
- package/lib/shared/components/Page.d.ts +0 -6
- package/lib/shared/components/Page.js +0 -16
- package/lib/shared/context/HeadContext.d.ts +0 -6
- package/lib/shared/context/HeadContext.js +0 -5
- package/lib/shared/context/PageContext.d.ts +0 -1
- package/lib/shared/context/PageContext.js +0 -5
- package/lib/shared/context/RouterContext.d.ts +0 -13
- package/lib/shared/context/RouterContext.js +0 -13
- package/lib/shared/core/ArcanaJSApp.d.ts +0 -13
- package/lib/shared/core/ArcanaJSApp.js +0 -98
- package/lib/shared/hooks/useDynamicComponents.d.ts +0 -1
- package/lib/shared/hooks/useDynamicComponents.js +0 -20
- package/lib/shared/hooks/useHead.d.ts +0 -1
- package/lib/shared/hooks/useHead.js +0 -7
- package/lib/shared/hooks/useLocation.d.ts +0 -5
- package/lib/shared/hooks/useLocation.js +0 -13
- package/lib/shared/hooks/usePage.d.ts +0 -1
- package/lib/shared/hooks/usePage.js +0 -7
- package/lib/shared/hooks/useParams.d.ts +0 -1
- package/lib/shared/hooks/useParams.js +0 -13
- package/lib/shared/hooks/useQuery.d.ts +0 -1
- package/lib/shared/hooks/useQuery.js +0 -9
- package/lib/shared/hooks/useRouter.d.ts +0 -1
- package/lib/shared/hooks/useRouter.js +0 -13
- package/lib/shared/utils/createSingletonContext.d.ts +0 -11
- package/lib/shared/utils/createSingletonContext.js +0 -21
- package/postcss.config.js +0 -6
- package/tailwind.config.js +0 -8
- package/tsconfig.json +0 -16
- package/webpack.client.js +0 -71
- package/webpack.server.js +0 -39
package/bin/arcanajs.js
ADDED
package/package.json
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arcanajs",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "ArcanaJS Framework",
|
|
5
|
-
"main": "lib/index.js",
|
|
6
|
-
"types": "lib/index.d.ts",
|
|
5
|
+
"main": "framework/lib/index.js",
|
|
6
|
+
"types": "framework/lib/index.d.ts",
|
|
7
7
|
"exports": {
|
|
8
|
-
".": "./lib/index.js",
|
|
9
|
-
"./server": "./lib/server.js"
|
|
8
|
+
".": "./framework/lib/index.js",
|
|
9
|
+
"./server": "./framework/lib/server.js"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"arcanajs": "./bin/arcanajs.js"
|
|
10
13
|
},
|
|
11
14
|
"files": [
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"webpack.server.js",
|
|
15
|
-
"tsconfig.json",
|
|
16
|
-
"babel.config.js",
|
|
17
|
-
"postcss.config.js",
|
|
18
|
-
"tailwind.config.js"
|
|
15
|
+
"framework",
|
|
16
|
+
"bin"
|
|
19
17
|
],
|
|
20
18
|
"scripts": {
|
|
21
|
-
"build": "tsc -p tsconfig.lib.json",
|
|
22
|
-
"build
|
|
23
|
-
"dev
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"dev": "concurrently \"npm run dev:server\" \"npm run dev:client\"",
|
|
27
|
-
"dev:tsx": "concurrently \"npm run dev:server:tsx\" \"npm run dev:client\"",
|
|
28
|
-
"start": "cross-env NODE_ENV=production node dist/server.js"
|
|
19
|
+
"build:lib": "tsc -p tsconfig.lib.json && cp src/lib/server/default-index.html framework/lib/server/",
|
|
20
|
+
"build": "node bin/arcanajs.js build",
|
|
21
|
+
"dev": "node bin/arcanajs.js dev",
|
|
22
|
+
"start": "node bin/arcanajs.js start",
|
|
23
|
+
"lint": "eslint"
|
|
29
24
|
},
|
|
30
25
|
"dependencies": {
|
|
26
|
+
"compression": "^1.8.1",
|
|
27
|
+
"cookie-parser": "^1.4.7",
|
|
28
|
+
"cross-env": "^10.1.0",
|
|
29
|
+
"express": "^5.1.0",
|
|
30
|
+
"helmet": "^8.1.0",
|
|
31
|
+
"react": "^19.2.0",
|
|
32
|
+
"react-dom": "^19.2.0",
|
|
31
33
|
"@babel/core": "^7.23.0",
|
|
32
34
|
"@babel/preset-env": "^7.23.0",
|
|
33
35
|
"@babel/preset-react": "^7.22.15",
|
|
@@ -36,36 +38,28 @@
|
|
|
36
38
|
"autoprefixer": "^10.4.22",
|
|
37
39
|
"babel-loader": "^10.0.0",
|
|
38
40
|
"clean-webpack-plugin": "^4.0.0",
|
|
39
|
-
"compression": "^1.8.1",
|
|
40
41
|
"concurrently": "^9.2.1",
|
|
41
|
-
"cookie-parser": "^1.4.7",
|
|
42
|
-
"cross-env": "^10.1.0",
|
|
43
42
|
"css-loader": "^7.1.2",
|
|
44
|
-
"express": "^5.1.0",
|
|
45
|
-
"helmet": "^8.1.0",
|
|
46
43
|
"html-webpack-plugin": "^5.6.5",
|
|
47
44
|
"mini-css-extract-plugin": "^2.9.4",
|
|
48
45
|
"null-loader": "^4.0.1",
|
|
49
46
|
"postcss": "^8.5.6",
|
|
50
47
|
"postcss-loader": "^8.2.0",
|
|
51
|
-
"react": "^19.2.0",
|
|
52
|
-
"react-dom": "^19.2.0",
|
|
53
48
|
"style-loader": "^4.0.0",
|
|
54
49
|
"tailwindcss": "^4.1.17",
|
|
55
50
|
"ts-loader": "^9.4.4",
|
|
56
51
|
"ts-node": "^10.9.2",
|
|
57
|
-
"
|
|
58
|
-
"
|
|
52
|
+
"tsx": "^4.20.6",
|
|
53
|
+
"typescript": "^5.9.3",
|
|
54
|
+
"webpack": "^5.103.0",
|
|
59
55
|
"webpack-cli": "^6.0.1",
|
|
60
|
-
"webpack-node-externals": "^3.0.0"
|
|
61
|
-
},
|
|
62
|
-
"devDependencies": {
|
|
63
|
-
"@types/compression": "^1.8.1",
|
|
64
|
-
"@types/cookie-parser": "^1.4.10",
|
|
56
|
+
"webpack-node-externals": "^3.0.0",
|
|
65
57
|
"@types/express": "^5.0.5",
|
|
66
58
|
"@types/node": "^24.10.1",
|
|
59
|
+
"@types/compression": "^1.8.1",
|
|
60
|
+
"@types/cookie-parser": "^1.4.10",
|
|
67
61
|
"@types/react": "^19.2.7",
|
|
68
|
-
"@types/react-dom": "^19.2.3"
|
|
69
|
-
|
|
70
|
-
}
|
|
62
|
+
"@types/react-dom": "^19.2.3"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {}
|
|
71
65
|
}
|
package/babel.config.js
DELETED
package/lib/client/index.d.ts
DELETED
package/lib/client/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.hydrateArcanaJS = void 0;
|
|
7
|
-
const react_1 = __importDefault(require("react"));
|
|
8
|
-
const client_1 = require("react-dom/client");
|
|
9
|
-
const HeadContext_1 = require("../shared/context/HeadContext");
|
|
10
|
-
const ArcanaJSApp_1 = require("../shared/core/ArcanaJSApp");
|
|
11
|
-
const hydrateArcanaJS = (viewsOrContext, layout) => {
|
|
12
|
-
let views = {};
|
|
13
|
-
if (viewsOrContext.keys && typeof viewsOrContext.keys === "function") {
|
|
14
|
-
viewsOrContext.keys().forEach((key) => {
|
|
15
|
-
const viewName = key.replace(/^\.\/(.*)\.tsx$/, "$1");
|
|
16
|
-
views[viewName] = viewsOrContext(key).default;
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
views = viewsOrContext;
|
|
21
|
-
}
|
|
22
|
-
const container = document.getElementById("root");
|
|
23
|
-
const dataScript = document.getElementById("__ArcanaJS_DATA__");
|
|
24
|
-
// Client-side HeadManager (noop for push, as Head handles client updates via useEffect)
|
|
25
|
-
const headManager = {
|
|
26
|
-
tags: [],
|
|
27
|
-
push: () => { },
|
|
28
|
-
};
|
|
29
|
-
if (container && dataScript) {
|
|
30
|
-
try {
|
|
31
|
-
const { page, data, params, csrfToken } = JSON.parse(dataScript.textContent || "{}");
|
|
32
|
-
(0, client_1.hydrateRoot)(container, react_1.default.createElement(HeadContext_1.HeadContext.Provider, { value: headManager },
|
|
33
|
-
react_1.default.createElement(ArcanaJSApp_1.ArcanaJSApp, { initialPage: page, initialData: data, initialParams: params, csrfToken: csrfToken, views: views, layout: layout })));
|
|
34
|
-
}
|
|
35
|
-
catch (e) {
|
|
36
|
-
console.error("Failed to parse initial data", e);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
exports.hydrateArcanaJS = hydrateArcanaJS;
|
package/lib/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export * from "./client";
|
|
2
|
-
export * from "./shared/components/Body";
|
|
3
|
-
export * from "./shared/components/Head";
|
|
4
|
-
export * from "./shared/components/Link";
|
|
5
|
-
export * from "./shared/components/NavLink";
|
|
6
|
-
export * from "./shared/components/Page";
|
|
7
|
-
export * from "./shared/context/HeadContext";
|
|
8
|
-
export * from "./shared/context/PageContext";
|
|
9
|
-
export * from "./shared/context/RouterContext";
|
|
10
|
-
export * from "./shared/core/ArcanaJSApp";
|
|
11
|
-
export * from "./shared/hooks/useDynamicComponents";
|
|
12
|
-
export * from "./shared/hooks/useHead";
|
|
13
|
-
export * from "./shared/hooks/useLocation";
|
|
14
|
-
export * from "./shared/hooks/usePage";
|
|
15
|
-
export * from "./shared/hooks/useParams";
|
|
16
|
-
export * from "./shared/hooks/useQuery";
|
|
17
|
-
export * from "./shared/hooks/useRouter";
|
package/lib/index.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./client"), exports);
|
|
18
|
-
__exportStar(require("./shared/components/Body"), exports);
|
|
19
|
-
__exportStar(require("./shared/components/Head"), exports);
|
|
20
|
-
__exportStar(require("./shared/components/Link"), exports);
|
|
21
|
-
__exportStar(require("./shared/components/NavLink"), exports);
|
|
22
|
-
__exportStar(require("./shared/components/Page"), exports);
|
|
23
|
-
__exportStar(require("./shared/context/HeadContext"), exports);
|
|
24
|
-
__exportStar(require("./shared/context/PageContext"), exports);
|
|
25
|
-
__exportStar(require("./shared/context/RouterContext"), exports);
|
|
26
|
-
__exportStar(require("./shared/core/ArcanaJSApp"), exports);
|
|
27
|
-
__exportStar(require("./shared/hooks/useDynamicComponents"), exports);
|
|
28
|
-
__exportStar(require("./shared/hooks/useHead"), exports);
|
|
29
|
-
__exportStar(require("./shared/hooks/useLocation"), exports);
|
|
30
|
-
__exportStar(require("./shared/hooks/usePage"), exports);
|
|
31
|
-
__exportStar(require("./shared/hooks/useParams"), exports);
|
|
32
|
-
__exportStar(require("./shared/hooks/useQuery"), exports);
|
|
33
|
-
__exportStar(require("./shared/hooks/useRouter"), exports);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from "express";
|
|
2
|
-
import React from "react";
|
|
3
|
-
declare global {
|
|
4
|
-
namespace Express {
|
|
5
|
-
interface Response {
|
|
6
|
-
renderPage(page: string, data?: any, params?: Record<string, string>): void;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
interface ArcanaJSOptions {
|
|
11
|
-
views: Record<string, React.FC<any>>;
|
|
12
|
-
indexFile?: string;
|
|
13
|
-
layout?: React.FC<any>;
|
|
14
|
-
}
|
|
15
|
-
export declare const createArcanaJSMiddleware: (options: ArcanaJSOptions) => (req: Request, res: Response, next: NextFunction) => void;
|
|
16
|
-
export {};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createArcanaJSMiddleware = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const react_1 = __importDefault(require("react"));
|
|
9
|
-
const server_1 = require("react-dom/server");
|
|
10
|
-
const HeadContext_1 = require("../shared/context/HeadContext");
|
|
11
|
-
const ArcanaJSApp_1 = require("../shared/core/ArcanaJSApp");
|
|
12
|
-
const DEFAULT_HTML_TEMPLATE = `<!DOCTYPE html>
|
|
13
|
-
<html lang="en">
|
|
14
|
-
<head>
|
|
15
|
-
<meta charset="UTF-8" />
|
|
16
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
17
|
-
<!--HEAD_CONTENT-->
|
|
18
|
-
</head>
|
|
19
|
-
<body>
|
|
20
|
-
<div id="root"><!--APP_CONTENT--></div>
|
|
21
|
-
<!--ARCANAJS_DATA_SCRIPT-->
|
|
22
|
-
</body>
|
|
23
|
-
</html>`;
|
|
24
|
-
// Helper to prevent XSS in initial data
|
|
25
|
-
const safeStringify = (obj) => {
|
|
26
|
-
return JSON.stringify(obj).replace(/</g, "\\u003c");
|
|
27
|
-
};
|
|
28
|
-
const createArcanaJSMiddleware = (options) => {
|
|
29
|
-
const { views, indexFile, layout } = options;
|
|
30
|
-
let cachedIndexHtml = null;
|
|
31
|
-
const getIndexHtml = (callback) => {
|
|
32
|
-
if (process.env.NODE_ENV === "production" && cachedIndexHtml) {
|
|
33
|
-
return callback(null, cachedIndexHtml);
|
|
34
|
-
}
|
|
35
|
-
if (indexFile && fs_1.default.existsSync(indexFile)) {
|
|
36
|
-
fs_1.default.readFile(indexFile, "utf8", (err, htmlData) => {
|
|
37
|
-
if (!err && process.env.NODE_ENV === "production") {
|
|
38
|
-
cachedIndexHtml = htmlData;
|
|
39
|
-
}
|
|
40
|
-
callback(err, htmlData);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
if (process.env.NODE_ENV === "production") {
|
|
45
|
-
cachedIndexHtml = DEFAULT_HTML_TEMPLATE;
|
|
46
|
-
}
|
|
47
|
-
callback(null, DEFAULT_HTML_TEMPLATE);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
return (req, res, next) => {
|
|
51
|
-
res.renderPage = (page, data = {}, params = {}) => {
|
|
52
|
-
const csrfToken = res.locals.csrfToken;
|
|
53
|
-
if (req.headers["x-arcanajs-request"] || req.query.format === "json") {
|
|
54
|
-
return res.json({ page, data, params, csrfToken });
|
|
55
|
-
}
|
|
56
|
-
try {
|
|
57
|
-
const headTags = [];
|
|
58
|
-
const headManager = {
|
|
59
|
-
tags: headTags,
|
|
60
|
-
push: (nodes) => headTags.push(nodes),
|
|
61
|
-
};
|
|
62
|
-
const appHtml = (0, server_1.renderToString)(react_1.default.createElement(HeadContext_1.HeadContext.Provider, { value: headManager }, react_1.default.createElement(ArcanaJSApp_1.ArcanaJSApp, {
|
|
63
|
-
initialPage: page,
|
|
64
|
-
initialData: data,
|
|
65
|
-
initialParams: params,
|
|
66
|
-
initialUrl: req.path,
|
|
67
|
-
csrfToken: csrfToken,
|
|
68
|
-
views: views,
|
|
69
|
-
layout: layout,
|
|
70
|
-
})));
|
|
71
|
-
const headHtml = (0, server_1.renderToString)(react_1.default.createElement(react_1.default.Fragment, null, ...headTags));
|
|
72
|
-
getIndexHtml((err, htmlData) => {
|
|
73
|
-
if (err) {
|
|
74
|
-
console.error("Error reading index.html", err);
|
|
75
|
-
return res.status(500).send("Server Error");
|
|
76
|
-
}
|
|
77
|
-
const scriptContent = safeStringify({
|
|
78
|
-
page,
|
|
79
|
-
data,
|
|
80
|
-
params,
|
|
81
|
-
csrfToken,
|
|
82
|
-
});
|
|
83
|
-
const scriptTag = `<script id="__ArcanaJS_DATA__" type="application/json">${scriptContent}</script>`;
|
|
84
|
-
const html = htmlData
|
|
85
|
-
.replace("<!--HEAD_CONTENT-->", headHtml)
|
|
86
|
-
.replace("<!--APP_CONTENT-->", appHtml)
|
|
87
|
-
.replace("<!--ARCANAJS_DATA_SCRIPT-->", scriptTag);
|
|
88
|
-
res.send(html);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
console.error("SSR Error:", error);
|
|
93
|
-
res.status(500).send("Internal Server Error");
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
next();
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
exports.createArcanaJSMiddleware = createArcanaJSMiddleware;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Express, RequestHandler } from "express";
|
|
2
|
-
import React from "react";
|
|
3
|
-
export interface ArcanaJSConfig {
|
|
4
|
-
port?: number | string;
|
|
5
|
-
views?: Record<string, React.FC<any>>;
|
|
6
|
-
viewsDir?: string;
|
|
7
|
-
viewsContext?: any;
|
|
8
|
-
routes?: RequestHandler | RequestHandler[];
|
|
9
|
-
staticDir?: string;
|
|
10
|
-
distDir?: string;
|
|
11
|
-
indexFile?: string;
|
|
12
|
-
layout?: React.FC<any>;
|
|
13
|
-
}
|
|
14
|
-
export declare class ArcanaJSServer {
|
|
15
|
-
app: Express;
|
|
16
|
-
private config;
|
|
17
|
-
constructor(config: ArcanaJSConfig);
|
|
18
|
-
private initialize;
|
|
19
|
-
private discoverViews;
|
|
20
|
-
start(): void;
|
|
21
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ArcanaJSServer = void 0;
|
|
7
|
-
const compression_1 = __importDefault(require("compression"));
|
|
8
|
-
const cookie_parser_1 = __importDefault(require("cookie-parser"));
|
|
9
|
-
const express_1 = __importDefault(require("express"));
|
|
10
|
-
const fs_1 = __importDefault(require("fs"));
|
|
11
|
-
const helmet_1 = __importDefault(require("helmet"));
|
|
12
|
-
const path_1 = __importDefault(require("path"));
|
|
13
|
-
const ArcanaJSMiddleware_1 = require("./ArcanaJSMiddleware");
|
|
14
|
-
const CsrfMiddleware_1 = require("./CsrfMiddleware");
|
|
15
|
-
const DynamicRouter_1 = require("./DynamicRouter");
|
|
16
|
-
const ResponseHandlerMiddleware_1 = require("./ResponseHandlerMiddleware");
|
|
17
|
-
class ArcanaJSServer {
|
|
18
|
-
constructor(config) {
|
|
19
|
-
this.config = config;
|
|
20
|
-
this.app = (0, express_1.default)();
|
|
21
|
-
this.initialize();
|
|
22
|
-
}
|
|
23
|
-
initialize() {
|
|
24
|
-
let { staticDir = "public", distDir = "dist/public", indexFile = "dist/public/index.html", views, viewsContext, routes, layout, } = this.config;
|
|
25
|
-
if (!views && viewsContext) {
|
|
26
|
-
views = {};
|
|
27
|
-
viewsContext.keys().forEach((key) => {
|
|
28
|
-
const viewName = key.replace(/^\.\/(.*)\.tsx$/, "$1");
|
|
29
|
-
views[viewName] = viewsContext(key).default;
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
if (!views) {
|
|
33
|
-
// Auto-discovery using fs (Server-side only)
|
|
34
|
-
views = this.discoverViews();
|
|
35
|
-
}
|
|
36
|
-
if (!views || Object.keys(views).length === 0) {
|
|
37
|
-
console.warn("No views found. Please check your views directory.");
|
|
38
|
-
views = {};
|
|
39
|
-
}
|
|
40
|
-
// Security and Performance
|
|
41
|
-
this.app.use((0, helmet_1.default)({
|
|
42
|
-
contentSecurityPolicy: false,
|
|
43
|
-
}));
|
|
44
|
-
this.app.use((0, compression_1.default)());
|
|
45
|
-
this.app.use((0, cookie_parser_1.default)());
|
|
46
|
-
this.app.use((0, CsrfMiddleware_1.createCsrfMiddleware)());
|
|
47
|
-
this.app.use(ResponseHandlerMiddleware_1.responseHandler);
|
|
48
|
-
// Static files
|
|
49
|
-
this.app.use(express_1.default.static(path_1.default.resolve(process.cwd(), distDir), {
|
|
50
|
-
index: false,
|
|
51
|
-
maxAge: "1y",
|
|
52
|
-
}));
|
|
53
|
-
this.app.use(express_1.default.static(path_1.default.resolve(process.cwd(), staticDir), {
|
|
54
|
-
index: false,
|
|
55
|
-
maxAge: "1d",
|
|
56
|
-
}));
|
|
57
|
-
// ArcanaJS Middleware
|
|
58
|
-
this.app.use((0, ArcanaJSMiddleware_1.createArcanaJSMiddleware)({
|
|
59
|
-
views,
|
|
60
|
-
indexFile: path_1.default.resolve(process.cwd(), indexFile),
|
|
61
|
-
layout,
|
|
62
|
-
}));
|
|
63
|
-
// Custom Routes
|
|
64
|
-
if (routes) {
|
|
65
|
-
if (Array.isArray(routes)) {
|
|
66
|
-
routes.forEach((route) => this.app.use(route));
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
this.app.use(routes);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// Dynamic Router
|
|
73
|
-
this.app.use((0, DynamicRouter_1.createDynamicRouter)(views));
|
|
74
|
-
// 404 Fallback
|
|
75
|
-
this.app.use((req, res) => {
|
|
76
|
-
res.status(404).renderPage("NotFoundPage");
|
|
77
|
-
});
|
|
78
|
-
// Error Handler
|
|
79
|
-
this.app.use((err, req, res, next) => {
|
|
80
|
-
console.error(err);
|
|
81
|
-
const message = process.env.NODE_ENV === "production"
|
|
82
|
-
? "Internal Server Error"
|
|
83
|
-
: err.message;
|
|
84
|
-
res.status(500).renderPage("ErrorPage", { message });
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
discoverViews() {
|
|
88
|
-
const views = {};
|
|
89
|
-
const viewsDir = this.config.viewsDir
|
|
90
|
-
? path_1.default.resolve(process.cwd(), this.config.viewsDir)
|
|
91
|
-
: path_1.default.resolve(process.cwd(), "src/views");
|
|
92
|
-
const traverse = (dir) => {
|
|
93
|
-
if (!fs_1.default.existsSync(dir))
|
|
94
|
-
return;
|
|
95
|
-
const files = fs_1.default.readdirSync(dir);
|
|
96
|
-
files.forEach((file) => {
|
|
97
|
-
const fullPath = path_1.default.join(dir, file);
|
|
98
|
-
const stat = fs_1.default.statSync(fullPath);
|
|
99
|
-
if (stat.isDirectory()) {
|
|
100
|
-
traverse(fullPath);
|
|
101
|
-
}
|
|
102
|
-
else if (file.endsWith(".tsx") ||
|
|
103
|
-
file.endsWith(".jsx") ||
|
|
104
|
-
file.endsWith(".js") ||
|
|
105
|
-
file.endsWith(".ts")) {
|
|
106
|
-
const relativePath = path_1.default.relative(viewsDir, fullPath);
|
|
107
|
-
const viewName = relativePath.replace(/\.(tsx|jsx|js|ts)$/, "");
|
|
108
|
-
try {
|
|
109
|
-
// Use __non_webpack_require__ if available to avoid Webpack bundling issues
|
|
110
|
-
// or standard require if running in Node directly
|
|
111
|
-
const requireFunc = typeof __non_webpack_require__ !== "undefined"
|
|
112
|
-
? __non_webpack_require__
|
|
113
|
-
: module.require;
|
|
114
|
-
// We need to register ts-node if we are requiring .tsx files directly
|
|
115
|
-
// This is a simplified approach. In a real framework, we'd handle compilation.
|
|
116
|
-
if (file.endsWith(".tsx") || file.endsWith(".ts")) {
|
|
117
|
-
try {
|
|
118
|
-
requireFunc("ts-node/register");
|
|
119
|
-
}
|
|
120
|
-
catch (e) {
|
|
121
|
-
console.warn("ts-node/register failed. Ensure ts-node is installed if you are using .tsx/.ts views in runtime.");
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
const pageModule = requireFunc(fullPath);
|
|
126
|
-
views[viewName] = pageModule.default || pageModule;
|
|
127
|
-
}
|
|
128
|
-
catch (error) {
|
|
129
|
-
console.error(`Failed to load view ${viewName}:`, error);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
};
|
|
134
|
-
traverse(viewsDir);
|
|
135
|
-
return views;
|
|
136
|
-
}
|
|
137
|
-
start() {
|
|
138
|
-
const PORT = this.config.port || process.env.PORT || 3000;
|
|
139
|
-
this.app.listen(PORT, () => {
|
|
140
|
-
console.log(`Server is running on http://localhost:${PORT}`);
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
exports.ArcanaJSServer = ArcanaJSServer;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
class ControllerBinder {
|
|
4
|
-
static handle(controller, method) {
|
|
5
|
-
// Instantiate the controller once (Singleton pattern) for performance
|
|
6
|
-
// This assumes controllers are stateless, which is best practice.
|
|
7
|
-
let instance;
|
|
8
|
-
try {
|
|
9
|
-
instance = new controller();
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
// Fallback if controller is not a class or fails to instantiate
|
|
13
|
-
console.warn(`Failed to instantiate controller ${controller.name}`, e);
|
|
14
|
-
instance = controller;
|
|
15
|
-
}
|
|
16
|
-
return async (req, res, next) => {
|
|
17
|
-
try {
|
|
18
|
-
// Call the method
|
|
19
|
-
if (typeof instance[method] === "function") {
|
|
20
|
-
await instance[method](req, res, next);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
throw new Error(`Method ${method} not found on controller ${controller.name}`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
next(error);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.default = ControllerBinder;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createCsrfMiddleware = void 0;
|
|
7
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
-
const CSRF_COOKIE_NAME = "_csrf";
|
|
9
|
-
const createCsrfMiddleware = () => {
|
|
10
|
-
return (req, res, next) => {
|
|
11
|
-
// 1. Generate or retrieve token
|
|
12
|
-
let token = req.cookies[CSRF_COOKIE_NAME];
|
|
13
|
-
if (!token) {
|
|
14
|
-
token = crypto_1.default.randomBytes(32).toString("hex");
|
|
15
|
-
res.cookie(CSRF_COOKIE_NAME, token, {
|
|
16
|
-
httpOnly: true,
|
|
17
|
-
secure: process.env.NODE_ENV === "production",
|
|
18
|
-
sameSite: "strict",
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
// 2. Expose token to the response locals (for injection into the view)
|
|
22
|
-
res.locals.csrfToken = token;
|
|
23
|
-
// 3. Verify token on state-changing methods
|
|
24
|
-
const method = req.method.toUpperCase();
|
|
25
|
-
if (["POST", "PUT", "DELETE", "PATCH"].includes(method)) {
|
|
26
|
-
const headerToken = req.headers["x-csrf-token"];
|
|
27
|
-
if (!headerToken || headerToken !== token) {
|
|
28
|
-
return res.status(403).json({ error: "Invalid CSRF Token" });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
next();
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
exports.createCsrfMiddleware = createCsrfMiddleware;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDynamicRouter = void 0;
|
|
4
|
-
const createDynamicRouter = (views) => {
|
|
5
|
-
return (req, res, next) => {
|
|
6
|
-
// Remove leading slash
|
|
7
|
-
let path = req.path.substring(1);
|
|
8
|
-
// Handle root path mapping to "index" if not handled elsewhere
|
|
9
|
-
if (path === "") {
|
|
10
|
-
path = "index";
|
|
11
|
-
}
|
|
12
|
-
// 1. Exact match
|
|
13
|
-
if (views[path]) {
|
|
14
|
-
return res.renderPage(path, {});
|
|
15
|
-
}
|
|
16
|
-
// 2. Dynamic match
|
|
17
|
-
for (const viewName of Object.keys(views)) {
|
|
18
|
-
if (!viewName.includes("["))
|
|
19
|
-
continue;
|
|
20
|
-
// Convert view path to regex
|
|
21
|
-
// e.g., "users/[id]" -> "^users/([^/]+)$"
|
|
22
|
-
// We need to escape special regex characters first, but keep [ and ] for our logic
|
|
23
|
-
// Actually, simpler: split by '/' and match segments
|
|
24
|
-
const pageParts = viewName.split("/");
|
|
25
|
-
const pathParts = path.split("/");
|
|
26
|
-
if (pageParts.length !== pathParts.length)
|
|
27
|
-
continue;
|
|
28
|
-
let match = true;
|
|
29
|
-
const params = {};
|
|
30
|
-
for (let i = 0; i < pageParts.length; i++) {
|
|
31
|
-
const pagePart = pageParts[i];
|
|
32
|
-
const pathPart = pathParts[i];
|
|
33
|
-
if (pagePart.startsWith("[") && pagePart.endsWith("]")) {
|
|
34
|
-
const paramName = pagePart.slice(1, -1);
|
|
35
|
-
params[paramName] = pathPart;
|
|
36
|
-
}
|
|
37
|
-
else if (pagePart !== pathPart) {
|
|
38
|
-
match = false;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (match) {
|
|
43
|
-
return res.renderPage(viewName, {}, params);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
// If not found, pass to the next middleware (usually 404 handler)
|
|
47
|
-
next();
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
exports.createDynamicRouter = createDynamicRouter;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { NextFunction, Request, Response } from "express";
|
|
2
|
-
declare global {
|
|
3
|
-
namespace Express {
|
|
4
|
-
interface Response {
|
|
5
|
-
/**
|
|
6
|
-
* Sends a success response with a standard format.
|
|
7
|
-
*
|
|
8
|
-
* @param data - The data payload to include in the response (default: {}).
|
|
9
|
-
* @param message - A descriptive message for the success (default: "Success").
|
|
10
|
-
* @param status - The HTTP status code to return (default: 200).
|
|
11
|
-
* @returns The Express Response object.
|
|
12
|
-
*/
|
|
13
|
-
success: (data?: string | object | null, message?: string, status?: number) => Response;
|
|
14
|
-
/**
|
|
15
|
-
* Sends an error response with a standard format.
|
|
16
|
-
*
|
|
17
|
-
* @param message - A descriptive message for the error (default: "Error").
|
|
18
|
-
* @param status - The HTTP status code to return (default: 500).
|
|
19
|
-
* @param error - Additional error details or object (default: null).
|
|
20
|
-
* @param data - Optional data payload to include in the error response (default: null).
|
|
21
|
-
* @returns The Express Response object.
|
|
22
|
-
*/
|
|
23
|
-
error: (message?: string, status?: number, error?: string | object | null, data?: string | object | null) => Response;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
export declare const responseHandler: (req: Request, res: Response, next: NextFunction) => void;
|