@networkbase/cli 1.0.1 → 1.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 (2) hide show
  1. package/bin/nb.mjs +263 -21
  2. package/package.json +1 -1
package/bin/nb.mjs CHANGED
@@ -11,7 +11,7 @@ import { fileURLToPath } from "node:url";
11
11
 
12
12
  const CLI_SCRIPT_PATH = fileURLToPath(import.meta.url);
13
13
 
14
- const CLI_VERSION = "1.0.1";
14
+ const CLI_VERSION = "1.0.2";
15
15
  const DEFAULT_API_URL = process.env.NETWORKBASE_API_URL || "https://networkbase.in";
16
16
  const GLOBAL_CONFIG_DIR = path.join(os.homedir(), ".networkbase");
17
17
  const GLOBAL_CREDENTIALS_PATH = path.join(GLOBAL_CONFIG_DIR, "credentials.json");
@@ -1368,9 +1368,28 @@ async function loginWithBrowser(apiUrl, flags = {}) {
1368
1368
  let timeoutHandle;
1369
1369
  const server = http.createServer(async (req, res) => {
1370
1370
  try {
1371
+ const origin = req.headers.origin || "";
1372
+ const isAllowedOrigin =
1373
+ origin.startsWith("https://networkbase.in") ||
1374
+ origin.startsWith("https://networkbase.app") ||
1375
+ origin.startsWith("http://localhost:3000") ||
1376
+ origin.startsWith("http://127.0.0.1");
1377
+
1378
+ const corsHeaders = {
1379
+ "Access-Control-Allow-Origin": isAllowedOrigin ? origin : "*",
1380
+ "Access-Control-Allow-Methods": "GET, OPTIONS",
1381
+ "Access-Control-Allow-Headers": "Content-Type, Authorization, Accept, X-Requested-With",
1382
+ };
1383
+
1384
+ if (req.method === "OPTIONS") {
1385
+ res.writeHead(204, corsHeaders);
1386
+ res.end();
1387
+ return;
1388
+ }
1389
+
1371
1390
  const reqUrl = new URL(req.url || "/", "http://127.0.0.1");
1372
1391
  if (reqUrl.pathname !== "/auth-callback") {
1373
- res.writeHead(404, { "Content-Type": "text/plain" });
1392
+ res.writeHead(404, { ...corsHeaders, "Content-Type": "text/plain" });
1374
1393
  res.end("Not found");
1375
1394
  return;
1376
1395
  }
@@ -1379,14 +1398,39 @@ async function loginWithBrowser(apiUrl, flags = {}) {
1379
1398
  const message = reqUrl.searchParams.get("message");
1380
1399
  const code = reqUrl.searchParams.get("code");
1381
1400
  const returnedState = reqUrl.searchParams.get("state");
1401
+ const isAjax =
1402
+ req.headers["sec-fetch-mode"] === "cors" ||
1403
+ (req.headers.accept && req.headers.accept.includes("application/json"));
1382
1404
 
1383
1405
  if (error) {
1384
- res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" });
1385
- res.end(`<!doctype html><html><body style="font-family:system-ui,sans-serif;padding:40px;text-align:center;">
1386
- <h2>Authorization Cancelled</h2>
1387
- <p>${message || error}</p>
1388
- <p>You can close this window and return to your terminal.</p>
1389
- </body></html>`);
1406
+ if (isAjax) {
1407
+ res.writeHead(400, { ...corsHeaders, "Content-Type": "application/json; charset=utf-8" });
1408
+ res.end(JSON.stringify({ ok: false, error: message || error }));
1409
+ } else {
1410
+ res.writeHead(400, { ...corsHeaders, "Content-Type": "text/html; charset=utf-8" });
1411
+ res.end(`<!doctype html>
1412
+ <html lang="en">
1413
+ <head>
1414
+ <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
1415
+ <title>Authorization Cancelled · NetworkBase</title>
1416
+ <link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='6' fill='%23c05638'/%3E%3C/svg%3E" />
1417
+ <style>
1418
+ body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #09090b; color: #fafafa; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; padding: 24px; }
1419
+ .card { width: 100%; max-width: 420px; background: #121215; border: 1px solid #27272a; border-radius: 12px; padding: 32px 28px; text-align: center; }
1420
+ h2 { font-size: 18px; margin: 0 0 8px; font-weight: 600; }
1421
+ p { font-size: 13px; color: #a1a1aa; line-height: 1.5; margin: 0 0 20px; }
1422
+ .btn { display: block; width: 100%; height: 40px; line-height: 40px; background: #fafafa; color: #000; font-size: 13px; font-weight: 500; border: none; border-radius: 6px; cursor: pointer; }
1423
+ </style>
1424
+ </head>
1425
+ <body>
1426
+ <div class="card">
1427
+ <h2>Authorization Cancelled</h2>
1428
+ <p>${message || error || "Please restart sign-in from your terminal."}</p>
1429
+ <button class="btn" onclick="window.close()">Close Window</button>
1430
+ </div>
1431
+ </body>
1432
+ </html>`);
1433
+ }
1390
1434
  clearTimeout(timeoutHandle);
1391
1435
  server.close();
1392
1436
  reject(new Error(message || `Browser authorization failed: ${error}`));
@@ -1394,25 +1438,223 @@ async function loginWithBrowser(apiUrl, flags = {}) {
1394
1438
  }
1395
1439
 
1396
1440
  if (!code || returnedState !== state) {
1397
- res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" });
1398
- res.end(`<!doctype html><html><body style="font-family:system-ui,sans-serif;padding:40px;text-align:center;">
1399
- <h2>Invalid Callback State</h2>
1400
- <p>Please restart sign-in from your terminal.</p>
1401
- </body></html>`);
1441
+ if (isAjax) {
1442
+ res.writeHead(400, { ...corsHeaders, "Content-Type": "application/json; charset=utf-8" });
1443
+ res.end(JSON.stringify({ ok: false, error: "Invalid state or missing authorization code." }));
1444
+ } else {
1445
+ res.writeHead(400, { ...corsHeaders, "Content-Type": "text/html; charset=utf-8" });
1446
+ res.end(`<!doctype html>
1447
+ <html lang="en">
1448
+ <head>
1449
+ <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
1450
+ <title>Invalid Callback State · NetworkBase</title>
1451
+ <link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='6' fill='%23c05638'/%3E%3C/svg%3E" />
1452
+ <style>
1453
+ body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #09090b; color: #fafafa; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; padding: 24px; }
1454
+ .card { width: 100%; max-width: 420px; background: #121215; border: 1px solid #27272a; border-radius: 12px; padding: 32px 28px; text-align: center; }
1455
+ h2 { font-size: 18px; margin: 0 0 8px; font-weight: 600; }
1456
+ p { font-size: 13px; color: #a1a1aa; line-height: 1.5; margin: 0 0 20px; }
1457
+ .btn { display: block; width: 100%; height: 40px; line-height: 40px; background: #fafafa; color: #000; font-size: 13px; font-weight: 500; border: none; border-radius: 6px; cursor: pointer; }
1458
+ </style>
1459
+ </head>
1460
+ <body>
1461
+ <div class="card">
1462
+ <h2>Invalid Callback State</h2>
1463
+ <p>Please restart sign-in from your terminal.</p>
1464
+ <button class="btn" onclick="window.close()">Close Window</button>
1465
+ </div>
1466
+ </body>
1467
+ </html>`);
1468
+ }
1402
1469
  clearTimeout(timeoutHandle);
1403
1470
  server.close();
1404
1471
  reject(new Error("Invalid state or missing authorization code from browser callback."));
1405
1472
  return;
1406
1473
  }
1407
1474
 
1408
- res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
1409
- res.end(`<!doctype html><html><body style="font-family:system-ui,-apple-system,sans-serif;background:#0a0a0a;color:#fafafa;padding:64px 24px;text-align:center;">
1410
- <div style="max-width:420px;margin:0 auto;border:1px solid #262626;border-radius:12px;padding:32px;background:#121212;">
1411
- <div style="font-size:28px;margin-bottom:12px;">✔</div>
1412
- <h2 style="margin:0 0 8px;font-size:18px;font-weight:600;">NetworkBase CLI Connected</h2>
1413
- <p style="margin:0;color:#a1a1aa;font-size:14px;line-height:1.5;">Your terminal is now authenticated. You can close this tab and return to your terminal.</p>
1414
- </div>
1415
- </body></html>`);
1475
+ if (isAjax) {
1476
+ res.writeHead(200, {
1477
+ ...corsHeaders,
1478
+ "Content-Type": "application/json; charset=utf-8",
1479
+ });
1480
+ res.end(JSON.stringify({ ok: true, status: "connected", message: "NetworkBase CLI Connected" }));
1481
+ } else {
1482
+ res.writeHead(200, {
1483
+ ...corsHeaders,
1484
+ "Content-Type": "text/html; charset=utf-8",
1485
+ });
1486
+ res.end(`<!doctype html>
1487
+ <html lang="en">
1488
+ <head>
1489
+ <meta charset="utf-8" />
1490
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
1491
+ <title>NetworkBase CLI Connected · NetworkBase</title>
1492
+ <link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='6' fill='%23c05638'/%3E%3Cpath d='M9 16.5l4.5 4.5 9.5-9.5' fill='none' stroke='%23ffffff' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E" />
1493
+ <style>
1494
+ :root {
1495
+ --background: #09090b;
1496
+ --card: #121215;
1497
+ --border: #27272a;
1498
+ --foreground: #fafafa;
1499
+ --muted: #a1a1aa;
1500
+ --accent: #c05638;
1501
+ --emerald: #10b981;
1502
+ }
1503
+ * { box-sizing: border-box; margin: 0; padding: 0; }
1504
+ body {
1505
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
1506
+ background-color: var(--background);
1507
+ color: var(--foreground);
1508
+ min-height: 100vh;
1509
+ display: flex;
1510
+ align-items: center;
1511
+ justify-content: center;
1512
+ padding: 24px;
1513
+ -webkit-font-smoothing: antialiased;
1514
+ }
1515
+ .card {
1516
+ width: 100%;
1517
+ max-width: 440px;
1518
+ background-color: var(--card);
1519
+ border: 1px solid var(--border);
1520
+ border-radius: 12px;
1521
+ padding: 32px 28px;
1522
+ text-align: center;
1523
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
1524
+ }
1525
+ .brand {
1526
+ display: inline-flex;
1527
+ align-items: center;
1528
+ gap: 8px;
1529
+ font-size: 13px;
1530
+ font-weight: 600;
1531
+ letter-spacing: -0.01em;
1532
+ color: var(--foreground);
1533
+ margin-bottom: 24px;
1534
+ }
1535
+ .brand-dot {
1536
+ width: 10px;
1537
+ height: 10px;
1538
+ background-color: var(--accent);
1539
+ border-radius: 2px;
1540
+ }
1541
+ .icon-container {
1542
+ width: 52px;
1543
+ height: 52px;
1544
+ margin: 0 auto 16px;
1545
+ display: flex;
1546
+ align-items: center;
1547
+ justify-content: center;
1548
+ border-radius: 50%;
1549
+ background: rgba(16, 185, 129, 0.12);
1550
+ border: 1px solid rgba(16, 185, 129, 0.25);
1551
+ color: var(--emerald);
1552
+ }
1553
+ .title {
1554
+ font-size: 18px;
1555
+ font-weight: 600;
1556
+ letter-spacing: -0.02em;
1557
+ margin-bottom: 8px;
1558
+ }
1559
+ .subtitle {
1560
+ font-size: 13px;
1561
+ color: var(--muted);
1562
+ line-height: 1.5;
1563
+ margin-bottom: 24px;
1564
+ }
1565
+ .details {
1566
+ background: rgba(255, 255, 255, 0.02);
1567
+ border: 1px solid var(--border);
1568
+ border-radius: 8px;
1569
+ padding: 12px 14px;
1570
+ margin-bottom: 24px;
1571
+ text-align: left;
1572
+ font-size: 12px;
1573
+ }
1574
+ .details-row {
1575
+ display: flex;
1576
+ justify-content: space-between;
1577
+ align-items: center;
1578
+ padding: 4px 0;
1579
+ }
1580
+ .details-label { color: var(--muted); }
1581
+ .details-value { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; }
1582
+ .badge-connected {
1583
+ color: var(--emerald);
1584
+ background: rgba(16, 185, 129, 0.15);
1585
+ border: 1px solid rgba(16, 185, 129, 0.3);
1586
+ padding: 2px 6px;
1587
+ border-radius: 4px;
1588
+ font-weight: 500;
1589
+ }
1590
+ .btn {
1591
+ display: block;
1592
+ width: 100%;
1593
+ height: 40px;
1594
+ line-height: 40px;
1595
+ background: var(--foreground);
1596
+ color: #000;
1597
+ font-size: 13px;
1598
+ font-weight: 500;
1599
+ border: none;
1600
+ border-radius: 6px;
1601
+ cursor: pointer;
1602
+ text-decoration: none;
1603
+ transition: opacity 0.15s ease;
1604
+ }
1605
+ .btn:hover { opacity: 0.9; }
1606
+ .countdown {
1607
+ margin-top: 14px;
1608
+ font-size: 11px;
1609
+ color: var(--muted);
1610
+ }
1611
+ </style>
1612
+ </head>
1613
+ <body>
1614
+ <div class="card">
1615
+ <div class="brand">
1616
+ <span class="brand-dot"></span>
1617
+ <span>NetworkBase</span>
1618
+ </div>
1619
+ <div class="icon-container">
1620
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
1621
+ <path d="M20 6L9 17l-5-5"/>
1622
+ </svg>
1623
+ </div>
1624
+ <h1 class="title">NetworkBase CLI Connected</h1>
1625
+ <p class="subtitle">Your terminal session is now authenticated. You can safely close this window.</p>
1626
+ <div class="details">
1627
+ <div class="details-row">
1628
+ <span class="details-label">Client</span>
1629
+ <span class="details-value">NetworkBase CLI</span>
1630
+ </div>
1631
+ <div class="details-row">
1632
+ <span class="details-label">Server</span>
1633
+ <span class="details-value">networkbase.in</span>
1634
+ </div>
1635
+ <div class="details-row">
1636
+ <span class="details-label">Status</span>
1637
+ <span class="badge-connected">● CONNECTED</span>
1638
+ </div>
1639
+ </div>
1640
+ <button class="btn" onclick="window.close()">Close Window</button>
1641
+ <p class="countdown">Closing automatically in <span id="sec">3</span>s…</p>
1642
+ </div>
1643
+ <script>
1644
+ let s = 3;
1645
+ const el = document.getElementById('sec');
1646
+ const t = setInterval(() => {
1647
+ s--;
1648
+ if (el) el.textContent = s;
1649
+ if (s <= 0) {
1650
+ clearInterval(t);
1651
+ window.close();
1652
+ }
1653
+ }, 1000);
1654
+ </script>
1655
+ </body>
1656
+ </html>`);
1657
+ }
1416
1658
 
1417
1659
  clearTimeout(timeoutHandle);
1418
1660
  server.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@networkbase/cli",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "NetworkBase CLI, Local Mirror Sync, Git Credential Helper & AI MCP Server",
5
5
  "type": "module",
6
6
  "homepage": "https://networkbase.in",