@titanpl/packet 1.0.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/index.js +63 -0
- package/js/titan/builder.js +73 -0
- package/js/titan/bundle.js +261 -0
- package/js/titan/dev.js +85 -0
- package/js/titan/error-box.js +277 -0
- package/js/titan/titan.js +129 -0
- package/package.json +18 -0
- package/ts/titan/builder.js +121 -0
- package/ts/titan/bundle.js +123 -0
- package/ts/titan/dev.js +454 -0
- package/ts/titan/titan.d.ts +17 -0
- package/ts/titan/titan.js +124 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Box Renderer
|
|
3
|
+
* Renders errors in a Next.js-style red terminal box
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { createRequire } from 'module';
|
|
9
|
+
import { execSync } from 'child_process';
|
|
10
|
+
import { fileURLToPath } from 'url';
|
|
11
|
+
|
|
12
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
13
|
+
const __dirname = path.dirname(__filename);
|
|
14
|
+
|
|
15
|
+
// Simple color function using ANSI escape codes
|
|
16
|
+
const red = (text) => `\x1b[31m${text}\x1b[0m`;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Wraps text to fit within a specified width
|
|
20
|
+
* @param {string} text - Text to wrap
|
|
21
|
+
* @param {number} maxWidth - Maximum width per line
|
|
22
|
+
* @returns {string[]} Array of wrapped lines
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
function getTitanVersion() {
|
|
26
|
+
try {
|
|
27
|
+
const require = createRequire(import.meta.url);
|
|
28
|
+
const pkgPath = require.resolve("@ezetgalaxy/titan/package.json");
|
|
29
|
+
return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
|
|
30
|
+
} catch (e) {
|
|
31
|
+
try {
|
|
32
|
+
let cur = __dirname;
|
|
33
|
+
for (let i = 0; i < 5; i++) {
|
|
34
|
+
const pkgPath = path.join(cur, "package.json");
|
|
35
|
+
if (fs.existsSync(pkgPath)) {
|
|
36
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
37
|
+
if (pkg.name === "@ezetgalaxy/titan") return pkg.version;
|
|
38
|
+
}
|
|
39
|
+
cur = path.join(cur, "..");
|
|
40
|
+
}
|
|
41
|
+
} catch (e2) { }
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const output = execSync("tit --version", { encoding: "utf-8" }).trim();
|
|
45
|
+
const match = output.match(/v(\d+\.\d+\.\d+)/);
|
|
46
|
+
if (match) return match[1];
|
|
47
|
+
} catch (e3) { }
|
|
48
|
+
}
|
|
49
|
+
return "0.1.0";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function wrapText(text, maxWidth) {
|
|
53
|
+
if (!text) return [''];
|
|
54
|
+
|
|
55
|
+
const lines = text.split('\n');
|
|
56
|
+
const wrapped = [];
|
|
57
|
+
|
|
58
|
+
for (const line of lines) {
|
|
59
|
+
if (line.length <= maxWidth) {
|
|
60
|
+
wrapped.push(line);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Word wrap logic
|
|
65
|
+
const words = line.split(' ');
|
|
66
|
+
let currentLine = '';
|
|
67
|
+
|
|
68
|
+
for (const word of words) {
|
|
69
|
+
const testLine = currentLine ? `${currentLine} ${word}` : word;
|
|
70
|
+
|
|
71
|
+
if (testLine.length <= maxWidth) {
|
|
72
|
+
currentLine = testLine;
|
|
73
|
+
} else {
|
|
74
|
+
if (currentLine) {
|
|
75
|
+
wrapped.push(currentLine);
|
|
76
|
+
}
|
|
77
|
+
// If single word is too long, force split it
|
|
78
|
+
if (word.length > maxWidth) {
|
|
79
|
+
let remaining = word;
|
|
80
|
+
while (remaining.length > maxWidth) {
|
|
81
|
+
wrapped.push(remaining.substring(0, maxWidth));
|
|
82
|
+
remaining = remaining.substring(maxWidth);
|
|
83
|
+
}
|
|
84
|
+
currentLine = remaining;
|
|
85
|
+
} else {
|
|
86
|
+
currentLine = word;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (currentLine) {
|
|
92
|
+
wrapped.push(currentLine);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return wrapped.length > 0 ? wrapped : [''];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Pads text to fit within box width
|
|
101
|
+
* @param {string} text - Text to pad
|
|
102
|
+
* @param {number} width - Target width
|
|
103
|
+
* @returns {string} Padded text
|
|
104
|
+
*/
|
|
105
|
+
function padLine(text, width) {
|
|
106
|
+
const padding = width - text.length;
|
|
107
|
+
return text + ' '.repeat(Math.max(0, padding));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Renders an error in a Next.js-style red box
|
|
112
|
+
* @param {Object} errorInfo - Error information
|
|
113
|
+
* @param {string} errorInfo.title - Error title (e.g., "Build Error")
|
|
114
|
+
* @param {string} errorInfo.file - File path where error occurred
|
|
115
|
+
* @param {string} errorInfo.message - Error message
|
|
116
|
+
* @param {string} [errorInfo.location] - Error location (e.g., "at hello.js:3:1")
|
|
117
|
+
* @param {number} [errorInfo.line] - Line number
|
|
118
|
+
* @param {number} [errorInfo.column] - Column number
|
|
119
|
+
* @param {string} [errorInfo.codeFrame] - Code frame showing error context
|
|
120
|
+
* @param {string} [errorInfo.suggestion] - Recommended fix
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Renders an error in a Next.js-style red box
|
|
124
|
+
* @param {Object} errorInfo - Error information
|
|
125
|
+
* @param {string} errorInfo.title - Error title (e.g., "Build Error")
|
|
126
|
+
* @param {string} errorInfo.file - File path where error occurred
|
|
127
|
+
* @param {string} errorInfo.message - Error message
|
|
128
|
+
* @param {string} [errorInfo.location] - Error location (e.g., "at hello.js:3:1")
|
|
129
|
+
* @param {number} [errorInfo.line] - Line number
|
|
130
|
+
* @param {number} [errorInfo.column] - Column number
|
|
131
|
+
* @param {string} [errorInfo.codeFrame] - Code frame showing error context
|
|
132
|
+
* @param {string} [errorInfo.suggestion] - Recommended fix
|
|
133
|
+
*/
|
|
134
|
+
export function renderErrorBox(errorInfo) {
|
|
135
|
+
const boxWidth = 72;
|
|
136
|
+
const contentWidth = boxWidth - 4; // Account for "│ " and " │"
|
|
137
|
+
|
|
138
|
+
const lines = [];
|
|
139
|
+
|
|
140
|
+
// Add title
|
|
141
|
+
if (errorInfo.title) {
|
|
142
|
+
lines.push(bold(errorInfo.title));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Add file path
|
|
146
|
+
if (errorInfo.file) {
|
|
147
|
+
lines.push(errorInfo.file);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Add message
|
|
151
|
+
if (errorInfo.message) {
|
|
152
|
+
lines.push('');
|
|
153
|
+
lines.push(...wrapText(errorInfo.message, contentWidth));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Add location
|
|
157
|
+
if (errorInfo.location) {
|
|
158
|
+
lines.push(gray(errorInfo.location));
|
|
159
|
+
} else if (errorInfo.file && errorInfo.line !== undefined) {
|
|
160
|
+
const loc = `at ${errorInfo.file}:${errorInfo.line}${errorInfo.column !== undefined ? `:${errorInfo.column}` : ''}`;
|
|
161
|
+
lines.push(gray(loc));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Add code frame if available
|
|
165
|
+
if (errorInfo.codeFrame) {
|
|
166
|
+
lines.push(''); // Empty line for separation
|
|
167
|
+
const frameLines = errorInfo.codeFrame.split('\n');
|
|
168
|
+
for (const frameLine of frameLines) {
|
|
169
|
+
lines.push(frameLine);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Add suggestion if available
|
|
174
|
+
if (errorInfo.suggestion) {
|
|
175
|
+
lines.push(''); // Empty line for separation
|
|
176
|
+
lines.push(...wrapText('Recommended fix: ' + errorInfo.suggestion, contentWidth));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Add Footer with Branding
|
|
180
|
+
lines.push('');
|
|
181
|
+
const version = getTitanVersion()
|
|
182
|
+
lines.push(gray(`⏣ Titan Planet ${version}`));
|
|
183
|
+
|
|
184
|
+
// Build the box
|
|
185
|
+
const topBorder = '┌' + '─'.repeat(boxWidth - 2) + '┐';
|
|
186
|
+
const bottomBorder = '└' + '─'.repeat(boxWidth - 2) + '┘';
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
const boxLines = [
|
|
190
|
+
red(topBorder),
|
|
191
|
+
...lines.map(line => {
|
|
192
|
+
// Strip ANSI codes for padding calculation if any were added
|
|
193
|
+
const plainLine = line.replace(/\x1b\[\d+m/g, '');
|
|
194
|
+
const padding = ' '.repeat(Math.max(0, contentWidth - plainLine.length));
|
|
195
|
+
return red('│ ') + line + padding + red(' │');
|
|
196
|
+
}),
|
|
197
|
+
red(bottomBorder)
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
return boxLines.join('\n');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Internal formatting helpers
|
|
204
|
+
function gray(t) { return `\x1b[90m${t}\x1b[0m`; }
|
|
205
|
+
function bold(t) { return `\x1b[1m${t}\x1b[0m`; }
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Parses esbuild error and extracts relevant information
|
|
209
|
+
* @param {Object} error - esbuild error object
|
|
210
|
+
* @returns {Object} Parsed error information
|
|
211
|
+
*/
|
|
212
|
+
export function parseEsbuildError(error) {
|
|
213
|
+
const errorInfo = {
|
|
214
|
+
title: 'Build Error',
|
|
215
|
+
file: error.location?.file || 'unknown',
|
|
216
|
+
message: error.text || error.message || 'Unknown error',
|
|
217
|
+
line: error.location?.line,
|
|
218
|
+
column: error.location?.column,
|
|
219
|
+
location: null,
|
|
220
|
+
codeFrame: null,
|
|
221
|
+
suggestion: error.notes?.[0]?.text || null
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// Format location
|
|
225
|
+
if (error.location) {
|
|
226
|
+
const { file, line, column } = error.location;
|
|
227
|
+
errorInfo.location = `at ${file}:${line}:${column}`;
|
|
228
|
+
|
|
229
|
+
// Format code frame if lineText is available
|
|
230
|
+
if (error.location.lineText) {
|
|
231
|
+
const lineText = error.location.lineText;
|
|
232
|
+
// Ensure column is at least 1 to prevent negative values
|
|
233
|
+
const col = Math.max(0, (column || 1) - 1);
|
|
234
|
+
const pointer = ' '.repeat(col) + '^';
|
|
235
|
+
errorInfo.codeFrame = `${line} | ${lineText}\n${' '.repeat(String(line).length)} | ${pointer}`;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return errorInfo;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Parses Node.js syntax error and extracts relevant information
|
|
244
|
+
* @param {Error} error - Node.js error object
|
|
245
|
+
* @param {string} [file] - File path (if known)
|
|
246
|
+
* @returns {Object} Parsed error information
|
|
247
|
+
*/
|
|
248
|
+
export function parseNodeError(error, file = null) {
|
|
249
|
+
const errorInfo = {
|
|
250
|
+
title: 'Syntax Error',
|
|
251
|
+
file: file || 'unknown',
|
|
252
|
+
message: error.message || 'Unknown error',
|
|
253
|
+
location: null,
|
|
254
|
+
suggestion: null
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// Try to extract line and column from error message
|
|
258
|
+
const locationMatch = error.message.match(/\((\d+):(\d+)\)/) ||
|
|
259
|
+
error.stack?.match(/:(\d+):(\d+)/);
|
|
260
|
+
|
|
261
|
+
if (locationMatch) {
|
|
262
|
+
const line = parseInt(locationMatch[1]);
|
|
263
|
+
const column = parseInt(locationMatch[2]);
|
|
264
|
+
errorInfo.line = line;
|
|
265
|
+
errorInfo.column = column;
|
|
266
|
+
errorInfo.location = `at ${errorInfo.file}:${line}:${column}`;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Extract suggestion from error message if available
|
|
270
|
+
if (error.message.includes('expected')) {
|
|
271
|
+
errorInfo.suggestion = 'Check for missing or misplaced syntax elements';
|
|
272
|
+
} else if (error.message.includes('Unexpected token')) {
|
|
273
|
+
errorInfo.suggestion = 'Remove or fix the unexpected token';
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return errorInfo;
|
|
277
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Titan.js
|
|
3
|
+
* Main Titan runtime builder
|
|
4
|
+
* RULE: This file does NOT handle esbuild errors - bundle.js handles those
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import path from "path";
|
|
9
|
+
import { bundle } from "./bundle.js";
|
|
10
|
+
|
|
11
|
+
const cyan = (t) => `\x1b[36m${t}\x1b[0m`;
|
|
12
|
+
const green = (t) => `\x1b[32m${t}\x1b[0m`;
|
|
13
|
+
|
|
14
|
+
const routes = {};
|
|
15
|
+
const dynamicRoutes = {};
|
|
16
|
+
const actionMap = {};
|
|
17
|
+
|
|
18
|
+
function addRoute(method, route) {
|
|
19
|
+
const key = `${method.toUpperCase()}:${route}`;
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
reply(value) {
|
|
23
|
+
routes[key] = {
|
|
24
|
+
type: typeof value === "object" ? "json" : "text",
|
|
25
|
+
value
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
action(name) {
|
|
30
|
+
if (route.includes(":")) {
|
|
31
|
+
if (!dynamicRoutes[method]) dynamicRoutes[method] = [];
|
|
32
|
+
dynamicRoutes[method].push({
|
|
33
|
+
method: method.toUpperCase(),
|
|
34
|
+
pattern: route,
|
|
35
|
+
action: name
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
routes[key] = {
|
|
39
|
+
type: "action",
|
|
40
|
+
value: name
|
|
41
|
+
};
|
|
42
|
+
actionMap[key] = name;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Titan App Builder
|
|
50
|
+
*/
|
|
51
|
+
const t = {
|
|
52
|
+
/**
|
|
53
|
+
* Define a GET route
|
|
54
|
+
*/
|
|
55
|
+
get(route) {
|
|
56
|
+
return addRoute("GET", route);
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Define a POST route
|
|
61
|
+
*/
|
|
62
|
+
post(route) {
|
|
63
|
+
return addRoute("POST", route);
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
log(module, msg) {
|
|
67
|
+
console.log(`[\x1b[35m${module}\x1b[0m] ${msg}`);
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Start the Titan Server
|
|
72
|
+
* RULE: Only calls bundle() - does NOT handle esbuild errors
|
|
73
|
+
* RULE: If bundle throws __TITAN_BUNDLE_FAILED__, stop immediately without printing
|
|
74
|
+
*/
|
|
75
|
+
async start(port = 3000, msg = "", threads, stack_mb = 8) {
|
|
76
|
+
try {
|
|
77
|
+
console.log(cyan("[Titan] Preparing runtime..."));
|
|
78
|
+
|
|
79
|
+
// RULE: Just call bundle() - it handles its own errors
|
|
80
|
+
await bundle();
|
|
81
|
+
|
|
82
|
+
const base = path.join(process.cwd(), "server");
|
|
83
|
+
if (!fs.existsSync(base)) {
|
|
84
|
+
fs.mkdirSync(base, { recursive: true });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const routesPath = path.join(base, "routes.json");
|
|
88
|
+
const actionMapPath = path.join(base, "action_map.json");
|
|
89
|
+
|
|
90
|
+
fs.writeFileSync(
|
|
91
|
+
routesPath,
|
|
92
|
+
JSON.stringify(
|
|
93
|
+
{
|
|
94
|
+
__config: { port, threads, stack_mb },
|
|
95
|
+
routes,
|
|
96
|
+
__dynamic_routes: Object.values(dynamicRoutes).flat()
|
|
97
|
+
},
|
|
98
|
+
null,
|
|
99
|
+
2
|
|
100
|
+
)
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
fs.writeFileSync(
|
|
104
|
+
actionMapPath,
|
|
105
|
+
JSON.stringify(actionMap, null, 2)
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
console.log(green("✔ Titan metadata written successfully"));
|
|
109
|
+
if (msg) console.log(cyan(msg));
|
|
110
|
+
|
|
111
|
+
} catch (e) {
|
|
112
|
+
// RULE: If bundle threw __TITAN_BUNDLE_FAILED__, just re-throw it
|
|
113
|
+
// The error box was already printed by bundle.js
|
|
114
|
+
if (e.message === '__TITAN_BUNDLE_FAILED__') {
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Other unexpected errors (not from bundle)
|
|
119
|
+
console.error(`\x1b[31m[Titan] Unexpected error: ${e.message}\x1b[0m`);
|
|
120
|
+
throw e;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Titan App Builder (Alias for t)
|
|
127
|
+
*/
|
|
128
|
+
export const Titan = t;
|
|
129
|
+
export default t;
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@titanpl/packet",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The bundler for TitanPl servers.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bundler",
|
|
7
|
+
"titanpl",
|
|
8
|
+
"titan",
|
|
9
|
+
"ezetgalaxy"
|
|
10
|
+
],
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"author": "ezetgalaxy",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "index.js",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { bundle } from "./bundle.js";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
const cyan = (t) => `\x1b[36m${t}\x1b[0m`;
|
|
6
|
+
const green = (t) => `\x1b[32m${t}\x1b[0m`;
|
|
7
|
+
|
|
8
|
+
const routes = {};
|
|
9
|
+
const dynamicRoutes = {};
|
|
10
|
+
const actionMap = {};
|
|
11
|
+
|
|
12
|
+
function addRoute(method, route) {
|
|
13
|
+
const key = `${method.toUpperCase()}:${route}`;
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
reply(value) {
|
|
18
|
+
routes[key] = {
|
|
19
|
+
type: typeof value === "object" ? "json" : "text",
|
|
20
|
+
value
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
action(name) {
|
|
25
|
+
if (route.includes(":")) {
|
|
26
|
+
if (!dynamicRoutes[method]) dynamicRoutes[method] = [];
|
|
27
|
+
dynamicRoutes[method].push({
|
|
28
|
+
method: method.toUpperCase(),
|
|
29
|
+
pattern: route,
|
|
30
|
+
action: name
|
|
31
|
+
});
|
|
32
|
+
} else {
|
|
33
|
+
routes[key] = {
|
|
34
|
+
type: "action",
|
|
35
|
+
value: name
|
|
36
|
+
};
|
|
37
|
+
actionMap[key] = name;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {Object} RouteHandler
|
|
45
|
+
* @property {(value: any) => void} reply - Send a direct response
|
|
46
|
+
* @property {(name: string) => void} action - Bind to a server-side action
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Titan App Builder
|
|
51
|
+
*/
|
|
52
|
+
const t = {
|
|
53
|
+
/**
|
|
54
|
+
* Define a GET route
|
|
55
|
+
* @param {string} route
|
|
56
|
+
* @returns {RouteHandler}
|
|
57
|
+
*/
|
|
58
|
+
get(route) {
|
|
59
|
+
return addRoute("GET", route);
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Define a POST route
|
|
64
|
+
* @param {string} route
|
|
65
|
+
* @returns {RouteHandler}
|
|
66
|
+
*/
|
|
67
|
+
post(route) {
|
|
68
|
+
return addRoute("POST", route);
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
log(module, msg) {
|
|
72
|
+
console.log(`[\x1b[35m${module}\x1b[0m] ${msg}`);
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Start the Titan Server
|
|
77
|
+
* @param {number} [port=3000]
|
|
78
|
+
* @param {string} [msg=""]
|
|
79
|
+
*/
|
|
80
|
+
async start(port = 3000, msg = "") {
|
|
81
|
+
try {
|
|
82
|
+
console.log(cyan("[Titan] Preparing runtime..."));
|
|
83
|
+
await bundle();
|
|
84
|
+
|
|
85
|
+
const base = path.join(process.cwd(), "server");
|
|
86
|
+
if (!fs.existsSync(base)) {
|
|
87
|
+
fs.mkdirSync(base, { recursive: true });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const routesPath = path.join(base, "routes.json");
|
|
91
|
+
const actionMapPath = path.join(base, "action_map.json");
|
|
92
|
+
|
|
93
|
+
fs.writeFileSync(
|
|
94
|
+
routesPath,
|
|
95
|
+
JSON.stringify(
|
|
96
|
+
{
|
|
97
|
+
__config: { port },
|
|
98
|
+
routes,
|
|
99
|
+
__dynamic_routes: Object.values(dynamicRoutes).flat()
|
|
100
|
+
},
|
|
101
|
+
null,
|
|
102
|
+
2
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
fs.writeFileSync(
|
|
107
|
+
actionMapPath,
|
|
108
|
+
JSON.stringify(actionMap, null, 2)
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
console.log(green("✔ Titan metadata written successfully"));
|
|
112
|
+
if (msg) console.log(cyan(msg));
|
|
113
|
+
|
|
114
|
+
} catch (e) {
|
|
115
|
+
console.error(`\x1b[31m[Titan] Build Error: ${e.message}\x1b[0m`);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export default t;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import esbuild from "esbuild";
|
|
4
|
+
import { createRequire } from "module";
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
|
|
8
|
+
/* ------------------------------------------------------------------
|
|
9
|
+
Node Builtin Rewrite Map
|
|
10
|
+
------------------------------------------------------------------ */
|
|
11
|
+
|
|
12
|
+
const NODE_BUILTIN_MAP = {
|
|
13
|
+
"fs": "@titanpl/node/fs",
|
|
14
|
+
"node:fs": "@titanpl/node/fs",
|
|
15
|
+
|
|
16
|
+
"path": "@titanpl/node/path",
|
|
17
|
+
"node:path": "@titanpl/node/path",
|
|
18
|
+
|
|
19
|
+
"os": "@titanpl/node/os",
|
|
20
|
+
"node:os": "@titanpl/node/os",
|
|
21
|
+
|
|
22
|
+
"crypto": "@titanpl/node/crypto",
|
|
23
|
+
"node:crypto": "@titanpl/node/crypto",
|
|
24
|
+
|
|
25
|
+
"process": "@titanpl/node/process",
|
|
26
|
+
"node:process": "@titanpl/node/process",
|
|
27
|
+
|
|
28
|
+
"events": "@titanpl/node/events",
|
|
29
|
+
"node:events": "@titanpl/node/events",
|
|
30
|
+
|
|
31
|
+
"util": "@titanpl/node/util",
|
|
32
|
+
"node:util": "@titanpl/node/util"
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/* ------------------------------------------------------------------
|
|
36
|
+
Titan Node Compatibility Plugin
|
|
37
|
+
------------------------------------------------------------------ */
|
|
38
|
+
|
|
39
|
+
const titanNodeCompatPlugin = {
|
|
40
|
+
name: "titan-node-compat",
|
|
41
|
+
setup(build) {
|
|
42
|
+
build.onResolve({ filter: /.*/ }, args => {
|
|
43
|
+
const replacement = NODE_BUILTIN_MAP[args.path];
|
|
44
|
+
if (!replacement) return;
|
|
45
|
+
|
|
46
|
+
// MUST return absolute path for esbuild
|
|
47
|
+
const resolved = require.resolve(replacement);
|
|
48
|
+
return { path: resolved };
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/* ------------------------------------------------------------------
|
|
54
|
+
Main Bundle Entry
|
|
55
|
+
------------------------------------------------------------------ */
|
|
56
|
+
|
|
57
|
+
export async function bundle() {
|
|
58
|
+
const root = process.cwd();
|
|
59
|
+
const actionsDir = path.join(root, "app", "actions");
|
|
60
|
+
const outDir = path.join(root, "server", "src", "actions");
|
|
61
|
+
|
|
62
|
+
await bundleJs(actionsDir, outDir);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ------------------------------------------------------------------
|
|
66
|
+
Bundle JS Actions
|
|
67
|
+
------------------------------------------------------------------ */
|
|
68
|
+
|
|
69
|
+
async function bundleJs(actionsDir, outDir) {
|
|
70
|
+
if (!fs.existsSync(actionsDir)) return;
|
|
71
|
+
|
|
72
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
73
|
+
|
|
74
|
+
// Clean old bundles
|
|
75
|
+
const oldFiles = fs.readdirSync(outDir);
|
|
76
|
+
for (const file of oldFiles) {
|
|
77
|
+
fs.unlinkSync(path.join(outDir, file));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const files = fs
|
|
81
|
+
.readdirSync(actionsDir)
|
|
82
|
+
.filter(f => f.endsWith(".js") || f.endsWith(".ts"));
|
|
83
|
+
|
|
84
|
+
if (files.length === 0) return;
|
|
85
|
+
|
|
86
|
+
for (const file of files) {
|
|
87
|
+
const actionName = path.basename(file, path.extname(file));
|
|
88
|
+
const entry = path.join(actionsDir, file);
|
|
89
|
+
const outfile = path.join(outDir, actionName + ".jsbundle");
|
|
90
|
+
|
|
91
|
+
await esbuild.build({
|
|
92
|
+
entryPoints: [entry],
|
|
93
|
+
outfile,
|
|
94
|
+
bundle: true,
|
|
95
|
+
format: "iife",
|
|
96
|
+
globalName: "__titan_exports",
|
|
97
|
+
platform: "node", // important for npm libs
|
|
98
|
+
target: "es2020",
|
|
99
|
+
logLevel: "silent",
|
|
100
|
+
plugins: [titanNodeCompatPlugin],
|
|
101
|
+
|
|
102
|
+
banner: {
|
|
103
|
+
js: "var Titan = t;"
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
footer: {
|
|
107
|
+
js: `
|
|
108
|
+
(function () {
|
|
109
|
+
const fn =
|
|
110
|
+
__titan_exports["${actionName}"] ||
|
|
111
|
+
__titan_exports.default;
|
|
112
|
+
|
|
113
|
+
if (typeof fn !== "function") {
|
|
114
|
+
throw new Error("[Titan] Action '${actionName}' not found or not a function");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
globalThis["${actionName}"] = globalThis.defineAction(fn);
|
|
118
|
+
})();
|
|
119
|
+
`
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|