calabasas 0.24.0 → 0.24.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 (2) hide show
  1. package/dist/index.js +137 -51
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2085,59 +2085,53 @@ Run \`calabasas logout\` to clear credentials.`, "Already logged in");
2085
2085
  }
2086
2086
  const s = p.spinner();
2087
2087
  s.start("Opening browser for authentication...");
2088
+ const webOrigin = new URL(getWebUrl()).origin;
2089
+ function corsHeaders() {
2090
+ return {
2091
+ "Access-Control-Allow-Origin": webOrigin,
2092
+ "Access-Control-Allow-Methods": "POST, OPTIONS",
2093
+ "Access-Control-Allow-Headers": "Content-Type"
2094
+ };
2095
+ }
2088
2096
  const server = http.createServer((req, res) => {
2089
2097
  const url = new URL(req.url || "", `http://localhost:${CALLBACK_PORT}`);
2090
- if (url.pathname === "/callback") {
2091
- const apiKey = url.searchParams.get("key");
2092
- if (apiKey) {
2093
- saveConfig({ apiKey });
2094
- res.writeHead(200, { "Content-Type": "text/html" });
2095
- res.end(`
2096
- <!DOCTYPE html>
2097
- <html>
2098
- <head>
2099
- <meta charset="utf-8">
2100
- <title>Calabasas CLI</title>
2101
- <style>
2102
- body { font-family: system-ui; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #0a0a0a; color: white; }
2103
- .container { text-align: center; }
2104
- h1 { color: #22c55e; }
2105
- </style>
2106
- </head>
2107
- <body>
2108
- <div class="container">
2109
- <h1>Authenticated!</h1>
2110
- <p>You can close this window and return to the terminal.</p>
2111
- </div>
2112
- </body>
2113
- </html>
2114
- `);
2115
- s.stop("Authenticated!");
2116
- p.note(`calabasas bot add - Add a Discord bot
2098
+ if (req.method === "OPTIONS") {
2099
+ res.writeHead(204, { ...corsHeaders(), "Access-Control-Max-Age": "600" });
2100
+ res.end();
2101
+ return;
2102
+ }
2103
+ if (url.pathname === "/callback" && req.method === "POST") {
2104
+ const chunks = [];
2105
+ req.on("data", (chunk) => chunks.push(chunk));
2106
+ req.on("end", () => {
2107
+ let apiKey;
2108
+ try {
2109
+ const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
2110
+ if (typeof parsed.key === "string")
2111
+ apiKey = parsed.key;
2112
+ } catch {}
2113
+ if (apiKey) {
2114
+ saveConfig({ apiKey });
2115
+ res.writeHead(204, corsHeaders());
2116
+ res.end();
2117
+ s.stop("Authenticated!");
2118
+ p.note(`calabasas bot add - Add a Discord bot
2117
2119
  calabasas bot list - List your bots
2118
2120
  calabasas generate - Generate event handlers`, "Available commands");
2119
- p.outro("Logged in successfully!");
2120
- setTimeout(() => {
2121
- server.close();
2122
- process.exit(0);
2123
- }, 500);
2124
- } else {
2125
- res.writeHead(400, { "Content-Type": "text/html" });
2126
- res.end(`
2127
- <!DOCTYPE html>
2128
- <html>
2129
- <head><meta charset="utf-8"><title>Error</title></head>
2130
- <body>
2131
- <h1>Authentication failed</h1>
2132
- <p>No API key received.</p>
2133
- </body>
2134
- </html>
2135
- `);
2136
- }
2137
- } else {
2138
- res.writeHead(404);
2139
- res.end("Not found");
2121
+ p.outro("Logged in successfully!");
2122
+ setTimeout(() => {
2123
+ server.close();
2124
+ process.exit(0);
2125
+ }, 500);
2126
+ } else {
2127
+ res.writeHead(400, { ...corsHeaders(), "Content-Type": "application/json" });
2128
+ res.end(JSON.stringify({ error: "Missing or invalid key" }));
2129
+ }
2130
+ });
2131
+ return;
2140
2132
  }
2133
+ res.writeHead(404);
2134
+ res.end("Not found");
2141
2135
  });
2142
2136
  server.listen(CALLBACK_PORT, async () => {
2143
2137
  const authUrl = `${getWebUrl()}/auth/cli?callback=http://localhost:${CALLBACK_PORT}/callback`;
@@ -2268,9 +2262,101 @@ import * as path2 from "path";
2268
2262
  async function parseConfigFile(configPath) {
2269
2263
  const absolutePath = path2.resolve(configPath);
2270
2264
  const source = fs2.readFileSync(absolutePath, "utf-8");
2271
- const stripped = source.replace(/import\s*\{[^}]*\}\s*from\s*["']calabasas["'];?/g, "").replace(/export\s+default\s+\(?defineCalabasas(?:\s+as\s+\w+)?\)?\s*\(/, "(").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "").trim();
2272
- const config = new Function("return " + stripped)();
2273
- return config;
2265
+ return parseConfigText(source);
2266
+ }
2267
+ function parseConfigText(source) {
2268
+ const stripped = source.replace(/import\s*\{[^}]*\}\s*from\s*["'][^"']*["'];?/g, "").replace(/export\s+default\s+\(?defineCalabasas(?:\s+as\s+\w+)?\)?\s*\(/, "(").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "").trim();
2269
+ const start = stripped.indexOf("{");
2270
+ const end = stripped.lastIndexOf("}");
2271
+ if (start === -1 || end === -1 || end <= start) {
2272
+ throw new Error("Could not find defineCalabasas({...}) in config file. Make sure your config uses export default defineCalabasas({...})");
2273
+ }
2274
+ const objectText = stripped.slice(start, end + 1);
2275
+ const json = jsObjectToJson(objectText);
2276
+ try {
2277
+ return JSON.parse(json);
2278
+ } catch (err) {
2279
+ const message = err instanceof Error ? err.message : String(err);
2280
+ throw new Error(`Failed to parse config object: ${message}. Check for syntax errors in your config.`);
2281
+ }
2282
+ }
2283
+ function jsObjectToJson(input) {
2284
+ let result = "";
2285
+ let i = 0;
2286
+ let inString = null;
2287
+ while (i < input.length) {
2288
+ const ch = input[i];
2289
+ if (inString) {
2290
+ if (ch === "\\") {
2291
+ result += ch;
2292
+ i++;
2293
+ if (i < input.length) {
2294
+ result += input[i];
2295
+ i++;
2296
+ }
2297
+ continue;
2298
+ }
2299
+ if (ch === inString) {
2300
+ result += '"';
2301
+ inString = null;
2302
+ i++;
2303
+ continue;
2304
+ }
2305
+ if (inString === "'" && ch === '"') {
2306
+ result += "\\\"";
2307
+ i++;
2308
+ continue;
2309
+ }
2310
+ result += ch;
2311
+ i++;
2312
+ continue;
2313
+ }
2314
+ if (ch === '"' || ch === "'") {
2315
+ inString = ch;
2316
+ result += '"';
2317
+ i++;
2318
+ continue;
2319
+ }
2320
+ if (/[a-zA-Z_$]/.test(ch)) {
2321
+ let ident = "";
2322
+ let j = i;
2323
+ while (j < input.length && /[a-zA-Z_$0-9]/.test(input[j])) {
2324
+ ident += input[j];
2325
+ j++;
2326
+ }
2327
+ let k = j;
2328
+ while (k < input.length && /\s/.test(input[k]))
2329
+ k++;
2330
+ if (k < input.length && input[k] === ":") {
2331
+ result += '"' + ident + '"';
2332
+ i = j;
2333
+ continue;
2334
+ }
2335
+ if (ident === "true" || ident === "false" || ident === "null") {
2336
+ result += ident;
2337
+ i = j;
2338
+ continue;
2339
+ }
2340
+ result += ident;
2341
+ i = j;
2342
+ continue;
2343
+ }
2344
+ if (ch === ",") {
2345
+ let j = i + 1;
2346
+ while (j < input.length && /\s/.test(input[j]))
2347
+ j++;
2348
+ if (j < input.length && (input[j] === "}" || input[j] === "]")) {
2349
+ i++;
2350
+ continue;
2351
+ }
2352
+ result += ch;
2353
+ i++;
2354
+ continue;
2355
+ }
2356
+ result += ch;
2357
+ i++;
2358
+ }
2359
+ return result;
2274
2360
  }
2275
2361
 
2276
2362
  // src/lib/intents.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "calabasas",
3
- "version": "0.24.0",
3
+ "version": "0.24.2",
4
4
  "description": "CLI for Calabasas - Discord Gateway as a Service for Convex",
5
5
  "type": "module",
6
6
  "bin": {