titanpl 2.0.0 → 2.0.2

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.
Files changed (44) hide show
  1. package/package.json +1 -1
  2. package/packages/cli/README.md +17 -17
  3. package/packages/cli/index.js +160 -160
  4. package/packages/cli/package.json +5 -5
  5. package/packages/cli/src/commands/build.js +5 -5
  6. package/packages/cli/src/commands/dev.js +12 -5
  7. package/packages/cli/src/commands/init.js +196 -182
  8. package/packages/cli/src/commands/migrate.js +124 -106
  9. package/packages/cli/src/commands/start.js +4 -4
  10. package/packages/cli/src/commands/update.js +90 -90
  11. package/packages/cli/src/engine.js +152 -143
  12. package/packages/engine-darwin-arm64/README.md +13 -13
  13. package/packages/engine-darwin-arm64/package.json +1 -1
  14. package/packages/engine-linux-x64/README.md +11 -11
  15. package/packages/engine-linux-x64/package.json +1 -1
  16. package/packages/engine-win32-x64/README.md +11 -11
  17. package/packages/engine-win32-x64/bin/titan-server.exe +0 -0
  18. package/packages/engine-win32-x64/package.json +1 -1
  19. package/packages/native/README.md +11 -11
  20. package/packages/native/index.d.ts +41 -41
  21. package/packages/native/index.js +39 -39
  22. package/packages/native/package.json +1 -1
  23. package/packages/packet/README.md +11 -11
  24. package/packages/packet/index.js +83 -83
  25. package/packages/packet/js/titan/builder.js +50 -50
  26. package/packages/packet/js/titan/bundle.js +164 -164
  27. package/packages/packet/js/titan/dev.js +19 -8
  28. package/packages/packet/js/titan/error-box.js +277 -277
  29. package/packages/packet/package.json +1 -1
  30. package/packages/packet/ts/titan/bundle.js +227 -227
  31. package/packages/packet/ts/titan/dev.js +19 -9
  32. package/packages/packet/ts/titan/error-box.js +277 -277
  33. package/packages/route/README.md +24 -24
  34. package/packages/route/index.d.ts +16 -16
  35. package/packages/route/index.js +52 -52
  36. package/packages/route/package.json +1 -1
  37. package/templates/common/_gitignore +1 -1
  38. package/templates/extension/package.json +2 -2
  39. package/templates/js/package.json +5 -5
  40. package/templates/rust-js/package.json +4 -4
  41. package/templates/rust-ts/package.json +4 -4
  42. package/templates/ts/package.json +5 -5
  43. package/titanpl-sdk/package.json +1 -1
  44. package/packages/engine-win32-x64/bin/titan-engine.exe +0 -0
@@ -1,164 +1,164 @@
1
- /**
2
- * Bundle.js (JavaScript Version)
3
- * Handles esbuild bundling with error reporting
4
- */
5
-
6
- import esbuild from 'esbuild';
7
- import path from 'path';
8
- import fs from 'fs';
9
- import { fileURLToPath } from 'url';
10
- import { createRequire } from 'module';
11
- import { renderErrorBox, parseEsbuildError } from './error-box.js';
12
-
13
- const __filename = fileURLToPath(import.meta.url);
14
- const __dirname = path.dirname(__filename);
15
-
16
- // Required for resolving node_modules inside ESM
17
- const require = createRequire(import.meta.url);
18
-
19
- /**
20
- * Titan Node Builtin Rewrite Map
21
- */
22
- const NODE_BUILTIN_MAP = {
23
- "fs": "@titanpl/node/fs",
24
- "node:fs": "@titanpl/node/fs",
25
- "path": "@titanpl/node/path",
26
- "node:path": "@titanpl/node/path",
27
- "os": "@titanpl/node/os",
28
- "node:os": "@titanpl/node/os",
29
- "crypto": "@titanpl/node/crypto",
30
- "node:crypto": "@titanpl/node/crypto",
31
- "process": "@titanpl/node/process",
32
- "util": "@titanpl/node/util",
33
- "node:util": "@titanpl/node/util",
34
- };
35
-
36
- const titanNodeCompatPlugin = {
37
- name: "titan-node-compat",
38
- setup(build) {
39
- build.onResolve({ filter: /.*/ }, args => {
40
- if (NODE_BUILTIN_MAP[args.path]) {
41
- try {
42
- const resolved = require.resolve(NODE_BUILTIN_MAP[args.path]);
43
- return { path: resolved };
44
- } catch (e) {
45
- throw new Error(`[TitanPL] Failed to resolve Node shim: ${NODE_BUILTIN_MAP[args.path]}`);
46
- }
47
- }
48
- });
49
- }
50
- };
51
-
52
- function getTitanVersion() {
53
- try {
54
- const pkgPath = require.resolve("@titanpl/cli/package.json");
55
- return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
56
- } catch (e) {
57
- return "1.0.0";
58
- }
59
- }
60
-
61
- export class BundleError extends Error {
62
- constructor(message, errors = [], warnings = []) {
63
- super(message);
64
- this.name = 'BundleError';
65
- this.errors = errors;
66
- this.warnings = warnings;
67
- this.isBundleError = true;
68
- }
69
- }
70
-
71
- async function validateEntryPoint(entryPoint) {
72
- const absPath = path.resolve(entryPoint);
73
- if (!fs.existsSync(absPath)) {
74
- throw new BundleError(`Entry point does not exist: ${entryPoint}`, [{ text: `Cannot find file: ${absPath}`, location: { file: entryPoint } }]);
75
- }
76
- }
77
-
78
- /**
79
- * Bundles a single file
80
- */
81
- export async function bundleFile(options) {
82
- const { entryPoint, outfile, format = 'iife', globalName = '__titan_exports', target = 'es2020' } = options;
83
-
84
- await validateEntryPoint(entryPoint);
85
-
86
- const outDir = path.dirname(outfile);
87
- if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
88
-
89
- try {
90
- const result = await esbuild.build({
91
- entryPoints: [entryPoint],
92
- bundle: true,
93
- outfile,
94
- format,
95
- globalName,
96
- platform: 'node',
97
- target,
98
- logLevel: 'silent',
99
- plugins: [titanNodeCompatPlugin],
100
- banner: { js: "var Titan = t;" },
101
- footer: options.footer || {}
102
- });
103
-
104
- if (result.errors?.length) {
105
- throw new BundleError(`Build failed`, result.errors);
106
- }
107
- } catch (err) {
108
- if (err.errors) throw new BundleError(`Build failed`, err.errors);
109
- throw err;
110
- }
111
- }
112
-
113
- /**
114
- * Main JS Bundler
115
- */
116
- export async function bundle(options = {}) {
117
- const root = options.root || process.cwd();
118
- const outDir = options.outDir || path.join(root, 'dist');
119
- const titanVersion = getTitanVersion();
120
-
121
- const actionsDir = path.join(root, 'app', 'actions');
122
- const bundleDir = path.join(outDir, 'actions');
123
-
124
- if (fs.existsSync(bundleDir)) fs.rmSync(bundleDir, { recursive: true, force: true });
125
- fs.mkdirSync(bundleDir, { recursive: true });
126
-
127
- if (!fs.existsSync(actionsDir)) return;
128
-
129
- const files = fs.readdirSync(actionsDir).filter(f => f.endsWith('.js'));
130
-
131
- for (const file of files) {
132
- const actionName = path.basename(file, path.extname(file));
133
- const entryPoint = path.join(actionsDir, file);
134
- const outfile = path.join(bundleDir, actionName + ".jsbundle");
135
-
136
- try {
137
- await bundleFile({
138
- entryPoint,
139
- outfile,
140
- footer: {
141
- js: `
142
- (function () {
143
- const fn = __titan_exports["${actionName}"] || __titan_exports.default;
144
- if (typeof fn !== "function") throw new Error("[TitanPL] Action '${actionName}' not found or not a function");
145
- globalThis["${actionName}"] = globalThis.defineAction(fn);
146
- })();`
147
- }
148
- });
149
- } catch (error) {
150
- console.error();
151
- if (error.isBundleError && error.errors?.length) {
152
- for (const err of error.errors) {
153
- const errorInfo = parseEsbuildError(err);
154
- errorInfo.titanVersion = titanVersion;
155
- console.error(renderErrorBox(errorInfo));
156
- console.error();
157
- }
158
- } else {
159
- console.error(renderErrorBox({ title: 'Build Error', message: error.message, titanVersion }));
160
- }
161
- throw new Error('__TITAN_BUNDLE_FAILED__');
162
- }
163
- }
164
- }
1
+ /**
2
+ * Bundle.js (JavaScript Version)
3
+ * Handles esbuild bundling with error reporting
4
+ */
5
+
6
+ import esbuild from 'esbuild';
7
+ import path from 'path';
8
+ import fs from 'fs';
9
+ import { fileURLToPath } from 'url';
10
+ import { createRequire } from 'module';
11
+ import { renderErrorBox, parseEsbuildError } from './error-box.js';
12
+
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = path.dirname(__filename);
15
+
16
+ // Required for resolving node_modules inside ESM
17
+ const require = createRequire(import.meta.url);
18
+
19
+ /**
20
+ * Titan Node Builtin Rewrite Map
21
+ */
22
+ const NODE_BUILTIN_MAP = {
23
+ "fs": "@titanpl/node/fs",
24
+ "node:fs": "@titanpl/node/fs",
25
+ "path": "@titanpl/node/path",
26
+ "node:path": "@titanpl/node/path",
27
+ "os": "@titanpl/node/os",
28
+ "node:os": "@titanpl/node/os",
29
+ "crypto": "@titanpl/node/crypto",
30
+ "node:crypto": "@titanpl/node/crypto",
31
+ "process": "@titanpl/node/process",
32
+ "util": "@titanpl/node/util",
33
+ "node:util": "@titanpl/node/util",
34
+ };
35
+
36
+ const titanNodeCompatPlugin = {
37
+ name: "titan-node-compat",
38
+ setup(build) {
39
+ build.onResolve({ filter: /.*/ }, args => {
40
+ if (NODE_BUILTIN_MAP[args.path]) {
41
+ try {
42
+ const resolved = require.resolve(NODE_BUILTIN_MAP[args.path]);
43
+ return { path: resolved };
44
+ } catch (e) {
45
+ throw new Error(`[TitanPL] Failed to resolve Node shim: ${NODE_BUILTIN_MAP[args.path]}`);
46
+ }
47
+ }
48
+ });
49
+ }
50
+ };
51
+
52
+ function getTitanVersion() {
53
+ try {
54
+ const pkgPath = require.resolve("@titanpl/cli/package.json");
55
+ return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
56
+ } catch (e) {
57
+ return "1.0.0";
58
+ }
59
+ }
60
+
61
+ export class BundleError extends Error {
62
+ constructor(message, errors = [], warnings = []) {
63
+ super(message);
64
+ this.name = 'BundleError';
65
+ this.errors = errors;
66
+ this.warnings = warnings;
67
+ this.isBundleError = true;
68
+ }
69
+ }
70
+
71
+ async function validateEntryPoint(entryPoint) {
72
+ const absPath = path.resolve(entryPoint);
73
+ if (!fs.existsSync(absPath)) {
74
+ throw new BundleError(`Entry point does not exist: ${entryPoint}`, [{ text: `Cannot find file: ${absPath}`, location: { file: entryPoint } }]);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Bundles a single file
80
+ */
81
+ export async function bundleFile(options) {
82
+ const { entryPoint, outfile, format = 'iife', globalName = '__titan_exports', target = 'es2020' } = options;
83
+
84
+ await validateEntryPoint(entryPoint);
85
+
86
+ const outDir = path.dirname(outfile);
87
+ if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
88
+
89
+ try {
90
+ const result = await esbuild.build({
91
+ entryPoints: [entryPoint],
92
+ bundle: true,
93
+ outfile,
94
+ format,
95
+ globalName,
96
+ platform: 'node',
97
+ target,
98
+ logLevel: 'silent',
99
+ plugins: [titanNodeCompatPlugin],
100
+ banner: { js: "var Titan = t;" },
101
+ footer: options.footer || {}
102
+ });
103
+
104
+ if (result.errors?.length) {
105
+ throw new BundleError(`Build failed`, result.errors);
106
+ }
107
+ } catch (err) {
108
+ if (err.errors) throw new BundleError(`Build failed`, err.errors);
109
+ throw err;
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Main JS Bundler
115
+ */
116
+ export async function bundle(options = {}) {
117
+ const root = options.root || process.cwd();
118
+ const outDir = options.outDir || path.join(root, 'dist');
119
+ const titanVersion = getTitanVersion();
120
+
121
+ const actionsDir = path.join(root, 'app', 'actions');
122
+ const bundleDir = path.join(outDir, 'actions');
123
+
124
+ if (fs.existsSync(bundleDir)) fs.rmSync(bundleDir, { recursive: true, force: true });
125
+ fs.mkdirSync(bundleDir, { recursive: true });
126
+
127
+ if (!fs.existsSync(actionsDir)) return;
128
+
129
+ const files = fs.readdirSync(actionsDir).filter(f => f.endsWith('.js'));
130
+
131
+ for (const file of files) {
132
+ const actionName = path.basename(file, path.extname(file));
133
+ const entryPoint = path.join(actionsDir, file);
134
+ const outfile = path.join(bundleDir, actionName + ".jsbundle");
135
+
136
+ try {
137
+ await bundleFile({
138
+ entryPoint,
139
+ outfile,
140
+ footer: {
141
+ js: `
142
+ (function () {
143
+ const fn = __titan_exports["${actionName}"] || __titan_exports.default;
144
+ if (typeof fn !== "function") throw new Error("[TitanPL] Action '${actionName}' not found or not a function");
145
+ globalThis["${actionName}"] = globalThis.defineAction(fn);
146
+ })();`
147
+ }
148
+ });
149
+ } catch (error) {
150
+ console.error();
151
+ if (error.isBundleError && error.errors?.length) {
152
+ for (const err of error.errors) {
153
+ const errorInfo = parseEsbuildError(err);
154
+ errorInfo.titanVersion = titanVersion;
155
+ console.error(renderErrorBox(errorInfo));
156
+ console.error();
157
+ }
158
+ } else {
159
+ console.error(renderErrorBox({ title: 'Build Error', message: error.message, titanVersion }));
160
+ }
161
+ throw new Error('__TITAN_BUNDLE_FAILED__');
162
+ }
163
+ }
164
+ }
@@ -25,6 +25,11 @@ function getTitanVersion() {
25
25
  }
26
26
 
27
27
  function getEngineBinaryPath(root) {
28
+ // First: check if the CLI pre-resolved this for us (correct module context)
29
+ if (process.env.TITAN_ENGINE_BINARY && fs.existsSync(process.env.TITAN_ENGINE_BINARY)) {
30
+ return process.env.TITAN_ENGINE_BINARY;
31
+ }
32
+
28
33
  const platform = os.platform();
29
34
  const arch = os.arch();
30
35
  const binName = platform === 'win32' ? 'titan-server.exe' : 'titan-server';
@@ -82,22 +87,28 @@ async function killServer() {
82
87
  if (!serverProcess) return;
83
88
 
84
89
  return new Promise((resolve) => {
85
- const onExit = () => {
90
+ if (serverProcess.killed || serverProcess.exitCode !== null) {
86
91
  serverProcess = null;
87
92
  resolve();
93
+ return;
94
+ }
95
+
96
+ let isDone = false;
97
+ const onExit = () => {
98
+ if (isDone) return;
99
+ isDone = true;
100
+ serverProcess = null;
101
+ setTimeout(resolve, 300); // Grace period for OS socket release
88
102
  };
89
103
 
90
104
  serverProcess.on('exit', onExit);
105
+ serverProcess.on('error', onExit);
91
106
 
92
- if (os.platform() === 'win32') {
93
- try {
94
- execSync(`taskkill /pid ${serverProcess.pid} /f /t`, { stdio: 'ignore' });
95
- } catch (e) { }
96
- } else {
107
+ try {
97
108
  serverProcess.kill('SIGKILL');
98
- }
109
+ } catch (e) { }
99
110
 
100
- setTimeout(onExit, 500);
111
+ setTimeout(onExit, 800); // Fallback
101
112
  });
102
113
  }
103
114