titanpl-sdk 3.0.1 → 6.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.
@@ -1,259 +1,259 @@
1
- /**
2
- * Bundle.js
3
- * Handles esbuild bundling with comprehensive error reporting
4
- * RULE: This file handles ALL esbuild errors and prints error boxes directly
5
- */
6
-
7
- import esbuild from 'esbuild';
8
- import path from 'path';
9
- import fs from 'fs';
10
- import { fileURLToPath } from 'url';
11
- import { createRequire } from 'module';
12
- import { renderErrorBox, parseEsbuildError } from './error-box.js';
13
-
14
- const __filename = fileURLToPath(import.meta.url);
15
- const __dirname = path.dirname(__filename);
16
-
17
- // Required for resolving node_modules inside ESM
18
- const require = createRequire(import.meta.url);
19
-
20
- /**
21
- * Titan Node Builtin Rewrite Map
22
- * Rewrites Node builtins to @titanpl/node shims
23
- */
24
- const NODE_BUILTIN_MAP = {
25
- "fs": "@titanpl/node/fs",
26
- "node:fs": "@titanpl/node/fs",
27
-
28
- "path": "@titanpl/node/path",
29
- "node:path": "@titanpl/node/path",
30
-
31
- "os": "@titanpl/node/os",
32
- "node:os": "@titanpl/node/os",
33
-
34
- "crypto": "@titanpl/node/crypto",
35
- "node:crypto": "@titanpl/node/crypto",
36
-
37
- "process": "@titanpl/node/process",
38
-
39
- "util": "@titanpl/node/util",
40
- "node:util": "@titanpl/node/util",
41
- };
42
-
43
- /**
44
- * Titan Node Compatibility Plugin
45
- * Rewrites require/import of Node builtins
46
- * Returns absolute paths (required by esbuild)
47
- */
48
- const titanNodeCompatPlugin = {
49
- name: "titan-node-compat",
50
- setup(build) {
51
- build.onResolve({ filter: /.*/ }, args => {
52
- if (NODE_BUILTIN_MAP[args.path]) {
53
- try {
54
- const resolved = require.resolve(NODE_BUILTIN_MAP[args.path]);
55
- return { path: resolved };
56
- } catch (e) {
57
- throw new Error(
58
- `[Titan] Failed to resolve Node shim: ${NODE_BUILTIN_MAP[args.path]}`
59
- );
60
- }
61
- }
62
- });
63
- }
64
- };
65
-
66
- /**
67
- * Get Titan version for error branding
68
- */
69
- function getTitanVersion() {
70
- try {
71
- const pkgPath = require.resolve("@ezetgalaxy/titan/package.json");
72
- return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
73
- } catch (e) {
74
- return "0.1.0";
75
- }
76
- }
77
-
78
- /**
79
- * Custom error class for bundle errors
80
- */
81
- export class BundleError extends Error {
82
- constructor(message, errors = [], warnings = []) {
83
- super(message);
84
- this.name = 'BundleError';
85
- this.errors = errors;
86
- this.warnings = warnings;
87
- this.isBundleError = true;
88
- }
89
- }
90
-
91
- /**
92
- * Validate entry file exists
93
- */
94
- async function validateEntryPoint(entryPoint) {
95
- const absPath = path.resolve(entryPoint);
96
-
97
- if (!fs.existsSync(absPath)) {
98
- throw new BundleError(
99
- `Entry point does not exist: ${entryPoint}`,
100
- [{ text: `Cannot find file: ${absPath}`, location: { file: entryPoint } }]
101
- );
102
- }
103
-
104
- try {
105
- await fs.promises.access(absPath, fs.constants.R_OK);
106
- } catch {
107
- throw new BundleError(
108
- `Entry point is not readable: ${entryPoint}`,
109
- [{ text: `Cannot read file: ${absPath}`, location: { file: entryPoint } }]
110
- );
111
- }
112
- }
113
-
114
- /**
115
- * Bundles a single file
116
- */
117
- export async function bundleFile(options) {
118
- const {
119
- entryPoint,
120
- outfile,
121
- format = 'iife',
122
- minify = false,
123
- sourcemap = false,
124
- platform = 'neutral',
125
- globalName = '__titan_exports',
126
- target = 'es2020',
127
- banner = {},
128
- footer = {}
129
- } = options;
130
-
131
- await validateEntryPoint(entryPoint);
132
-
133
- const outDir = path.dirname(outfile);
134
- await fs.promises.mkdir(outDir, { recursive: true });
135
-
136
- try {
137
- const result = await esbuild.build({
138
- entryPoints: [entryPoint],
139
- bundle: true,
140
- outfile,
141
- format,
142
- globalName,
143
- platform,
144
- target,
145
- banner,
146
- footer,
147
- minify,
148
- sourcemap,
149
- logLevel: 'silent',
150
- logLimit: 0,
151
- write: true,
152
- metafile: false,
153
- plugins: [titanNodeCompatPlugin],
154
- });
155
-
156
- if (result.errors?.length) {
157
- throw new BundleError(
158
- `Build failed with ${result.errors.length} error(s)`,
159
- result.errors,
160
- result.warnings || []
161
- );
162
- }
163
-
164
- } catch (err) {
165
- if (err.errors?.length) {
166
- throw new BundleError(
167
- `Build failed with ${err.errors.length} error(s)`,
168
- err.errors,
169
- err.warnings || []
170
- );
171
- }
172
-
173
- throw new BundleError(
174
- `Unexpected build error: ${err.message}`,
175
- [{ text: err.message, location: { file: entryPoint } }]
176
- );
177
- }
178
- }
179
-
180
- /**
181
- * Main bundler
182
- */
183
- export async function bundle() {
184
- const root = process.cwd();
185
- const actionsDir = path.join(root, 'app', 'actions');
186
- const bundleDir = path.join(root, 'server', 'src', 'actions');
187
-
188
- if (fs.existsSync(bundleDir)) {
189
- fs.rmSync(bundleDir, { recursive: true, force: true });
190
- }
191
- await fs.promises.mkdir(bundleDir, { recursive: true });
192
-
193
- if (!fs.existsSync(actionsDir)) return;
194
-
195
- const files = fs.readdirSync(actionsDir).filter(f =>
196
- (f.endsWith('.js') || f.endsWith('.ts')) && !f.endsWith('.d.ts')
197
- );
198
-
199
- for (const file of files) {
200
- const actionName = path.basename(file, path.extname(file));
201
- const entryPoint = path.join(actionsDir, file);
202
- const outfile = path.join(bundleDir, actionName + ".jsbundle");
203
-
204
- try {
205
- await bundleFile({
206
- entryPoint,
207
- outfile,
208
- format: 'iife',
209
- globalName: '__titan_exports',
210
- platform: 'node',
211
- target: 'es2020',
212
- banner: { js: "var Titan = t;" },
213
- footer: {
214
- js: `
215
- (function () {
216
- const fn =
217
- __titan_exports["${actionName}"] ||
218
- __titan_exports.default;
219
-
220
- if (typeof fn !== "function") {
221
- throw new Error("[Titan] Action '${actionName}' not found or not a function");
222
- }
223
-
224
- globalThis["${actionName}"] = globalThis.defineAction(fn);
225
- })();
226
- `
227
- }
228
- });
229
-
230
- } catch (error) {
231
-
232
- console.error();
233
-
234
- const titanVersion = getTitanVersion();
235
-
236
- if (error.isBundleError && error.errors?.length) {
237
- for (let i = 0; i < error.errors.length; i++) {
238
- const errorInfo = parseEsbuildError(error.errors[i]);
239
- if (error.errors.length > 1) {
240
- errorInfo.title = `Build Error ${i + 1}/${error.errors.length}`;
241
- }
242
- errorInfo.titanVersion = titanVersion;
243
- console.error(renderErrorBox(errorInfo));
244
- console.error();
245
- }
246
- } else {
247
- const errorInfo = {
248
- title: 'Build Error',
249
- file: entryPoint,
250
- message: error.message || 'Unknown error',
251
- titanVersion
252
- };
253
- console.error(renderErrorBox(errorInfo));
254
- }
255
-
256
- throw new Error('__TITAN_BUNDLE_FAILED__');
257
- }
258
- }
259
- }
1
+ /**
2
+ * Bundle.js
3
+ * Handles esbuild bundling with comprehensive error reporting
4
+ * RULE: This file handles ALL esbuild errors and prints error boxes directly
5
+ */
6
+
7
+ import esbuild from 'esbuild';
8
+ import path from 'path';
9
+ import fs from 'fs';
10
+ import { fileURLToPath } from 'url';
11
+ import { createRequire } from 'module';
12
+ import { renderErrorBox, parseEsbuildError } from './error-box.js';
13
+
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = path.dirname(__filename);
16
+
17
+ // Required for resolving node_modules inside ESM
18
+ const require = createRequire(import.meta.url);
19
+
20
+ /**
21
+ * Titan Node Builtin Rewrite Map
22
+ * Rewrites Node builtins to @titanpl/node shims
23
+ */
24
+ const NODE_BUILTIN_MAP = {
25
+ "fs": "@titanpl/node/fs",
26
+ "node:fs": "@titanpl/node/fs",
27
+
28
+ "path": "@titanpl/node/path",
29
+ "node:path": "@titanpl/node/path",
30
+
31
+ "os": "@titanpl/node/os",
32
+ "node:os": "@titanpl/node/os",
33
+
34
+ "crypto": "@titanpl/node/crypto",
35
+ "node:crypto": "@titanpl/node/crypto",
36
+
37
+ "process": "@titanpl/node/process",
38
+
39
+ "util": "@titanpl/node/util",
40
+ "node:util": "@titanpl/node/util",
41
+ };
42
+
43
+ /**
44
+ * Titan Node Compatibility Plugin
45
+ * Rewrites require/import of Node builtins
46
+ * Returns absolute paths (required by esbuild)
47
+ */
48
+ const titanNodeCompatPlugin = {
49
+ name: "titan-node-compat",
50
+ setup(build) {
51
+ build.onResolve({ filter: /.*/ }, args => {
52
+ if (NODE_BUILTIN_MAP[args.path]) {
53
+ try {
54
+ const resolved = require.resolve(NODE_BUILTIN_MAP[args.path]);
55
+ return { path: resolved };
56
+ } catch (e) {
57
+ throw new Error(
58
+ `[Titan] Failed to resolve Node shim: ${NODE_BUILTIN_MAP[args.path]}`
59
+ );
60
+ }
61
+ }
62
+ });
63
+ }
64
+ };
65
+
66
+ /**
67
+ * Get Titan version for error branding
68
+ */
69
+ function getTitanVersion() {
70
+ try {
71
+ const pkgPath = require.resolve("titanpl/package.json");
72
+ return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
73
+ } catch (e) {
74
+ return "0.1.0";
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Custom error class for bundle errors
80
+ */
81
+ export class BundleError extends Error {
82
+ constructor(message, errors = [], warnings = []) {
83
+ super(message);
84
+ this.name = 'BundleError';
85
+ this.errors = errors;
86
+ this.warnings = warnings;
87
+ this.isBundleError = true;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Validate entry file exists
93
+ */
94
+ async function validateEntryPoint(entryPoint) {
95
+ const absPath = path.resolve(entryPoint);
96
+
97
+ if (!fs.existsSync(absPath)) {
98
+ throw new BundleError(
99
+ `Entry point does not exist: ${entryPoint}`,
100
+ [{ text: `Cannot find file: ${absPath}`, location: { file: entryPoint } }]
101
+ );
102
+ }
103
+
104
+ try {
105
+ await fs.promises.access(absPath, fs.constants.R_OK);
106
+ } catch {
107
+ throw new BundleError(
108
+ `Entry point is not readable: ${entryPoint}`,
109
+ [{ text: `Cannot read file: ${absPath}`, location: { file: entryPoint } }]
110
+ );
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Bundles a single file
116
+ */
117
+ export async function bundleFile(options) {
118
+ const {
119
+ entryPoint,
120
+ outfile,
121
+ format = 'iife',
122
+ minify = false,
123
+ sourcemap = false,
124
+ platform = 'neutral',
125
+ globalName = '__titan_exports',
126
+ target = 'es2020',
127
+ banner = {},
128
+ footer = {}
129
+ } = options;
130
+
131
+ await validateEntryPoint(entryPoint);
132
+
133
+ const outDir = path.dirname(outfile);
134
+ await fs.promises.mkdir(outDir, { recursive: true });
135
+
136
+ try {
137
+ const result = await esbuild.build({
138
+ entryPoints: [entryPoint],
139
+ bundle: true,
140
+ outfile,
141
+ format,
142
+ globalName,
143
+ platform,
144
+ target,
145
+ banner,
146
+ footer,
147
+ minify,
148
+ sourcemap,
149
+ logLevel: 'silent',
150
+ logLimit: 0,
151
+ write: true,
152
+ metafile: false,
153
+ plugins: [titanNodeCompatPlugin],
154
+ });
155
+
156
+ if (result.errors?.length) {
157
+ throw new BundleError(
158
+ `Build failed with ${result.errors.length} error(s)`,
159
+ result.errors,
160
+ result.warnings || []
161
+ );
162
+ }
163
+
164
+ } catch (err) {
165
+ if (err.errors?.length) {
166
+ throw new BundleError(
167
+ `Build failed with ${err.errors.length} error(s)`,
168
+ err.errors,
169
+ err.warnings || []
170
+ );
171
+ }
172
+
173
+ throw new BundleError(
174
+ `Unexpected build error: ${err.message}`,
175
+ [{ text: err.message, location: { file: entryPoint } }]
176
+ );
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Main bundler
182
+ */
183
+ export async function bundle() {
184
+ const root = process.cwd();
185
+ const actionsDir = path.join(root, 'app', 'actions');
186
+ const bundleDir = path.join(root, 'server', 'src', 'actions');
187
+
188
+ if (fs.existsSync(bundleDir)) {
189
+ fs.rmSync(bundleDir, { recursive: true, force: true });
190
+ }
191
+ await fs.promises.mkdir(bundleDir, { recursive: true });
192
+
193
+ if (!fs.existsSync(actionsDir)) return;
194
+
195
+ const files = fs.readdirSync(actionsDir).filter(f =>
196
+ (f.endsWith('.js') || f.endsWith('.ts')) && !f.endsWith('.d.ts')
197
+ );
198
+
199
+ for (const file of files) {
200
+ const actionName = path.basename(file, path.extname(file));
201
+ const entryPoint = path.join(actionsDir, file);
202
+ const outfile = path.join(bundleDir, actionName + ".jsbundle");
203
+
204
+ try {
205
+ await bundleFile({
206
+ entryPoint,
207
+ outfile,
208
+ format: 'iife',
209
+ globalName: '__titan_exports',
210
+ platform: 'node',
211
+ target: 'es2020',
212
+ banner: { js: "var Titan = t;" },
213
+ footer: {
214
+ js: `
215
+ (function () {
216
+ const fn =
217
+ __titan_exports["${actionName}"] ||
218
+ __titan_exports.default;
219
+
220
+ if (typeof fn !== "function") {
221
+ throw new Error("[Titan] Action '${actionName}' not found or not a function");
222
+ }
223
+
224
+ globalThis["${actionName}"] = globalThis.defineAction(fn);
225
+ })();
226
+ `
227
+ }
228
+ });
229
+
230
+ } catch (error) {
231
+
232
+ console.error();
233
+
234
+ const titanVersion = getTitanVersion();
235
+
236
+ if (error.isBundleError && error.errors?.length) {
237
+ for (let i = 0; i < error.errors.length; i++) {
238
+ const errorInfo = parseEsbuildError(error.errors[i]);
239
+ if (error.errors.length > 1) {
240
+ errorInfo.title = `Build Error ${i + 1}/${error.errors.length}`;
241
+ }
242
+ errorInfo.titanVersion = titanVersion;
243
+ console.error(renderErrorBox(errorInfo));
244
+ console.error();
245
+ }
246
+ } else {
247
+ const errorInfo = {
248
+ title: 'Build Error',
249
+ file: entryPoint,
250
+ message: error.message || 'Unknown error',
251
+ titanVersion
252
+ };
253
+ console.error(renderErrorBox(errorInfo));
254
+ }
255
+
256
+ throw new Error('__TITAN_BUNDLE_FAILED__');
257
+ }
258
+ }
259
+ }
@@ -25,7 +25,7 @@ const bold = (t) => `\x1b[1m${t}\x1b[0m`;
25
25
  function getTitanVersion() {
26
26
  try {
27
27
  const require = createRequire(import.meta.url);
28
- const pkgPath = require.resolve("@ezetgalaxy/titan/package.json");
28
+ const pkgPath = require.resolve("titanpl/package.json");
29
29
  return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
30
30
  } catch (e) {
31
31
  try {
@@ -34,7 +34,7 @@ function getTitanVersion() {
34
34
  const pkgPath = path.join(cur, "package.json");
35
35
  if (fs.existsSync(pkgPath)) {
36
36
  const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
37
- if (pkg.name === "@ezetgalaxy/titan") return pkg.version;
37
+ if (pkg.name === "titanpl") return pkg.version;
38
38
  }
39
39
  cur = path.join(cur, "..");
40
40
  }
@@ -250,7 +250,7 @@ async function startRustServer(retryCount = 0) {
250
250
  function prepareRuntime() {
251
251
  try {
252
252
  const nm = path.join(process.cwd(), "node_modules");
253
- const titanPkg = path.join(nm, "@titan");
253
+ const titanPkg = path.join(nm, "@titanpl");
254
254
  const routePkg = path.join(titanPkg, "route");
255
255
 
256
256
  if (!fs.existsSync(nm)) fs.mkdirSync(nm, { recursive: true });
@@ -259,7 +259,7 @@ function prepareRuntime() {
259
259
  if (!fs.existsSync(routePkg)) {
260
260
  fs.mkdirSync(routePkg, { recursive: true });
261
261
  fs.writeFileSync(path.join(routePkg, "package.json"), JSON.stringify({
262
- name: "@titan/route",
262
+ name: "@titanpl/route",
263
263
  main: "../../../titan/titan.js",
264
264
  type: "module"
265
265
  }, null, 2));