@networkbase/cli 1.0.0 → 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 +275 -84
  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.0";
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");
@@ -466,16 +466,7 @@ async function apiRequest(endpoint, options = {}) {
466
466
  signal: AbortSignal.timeout(timeoutMs),
467
467
  });
468
468
  } catch (fetchErr) {
469
- if (baseUrl !== "http://localhost:3000" && !options.apiUrl) {
470
- response = await fetch(`http://localhost:3000${endpoint}`, {
471
- method: options.method || "GET",
472
- headers,
473
- body: options.body ? JSON.stringify(options.body) : undefined,
474
- signal: AbortSignal.timeout(timeoutMs),
475
- });
476
- } else {
477
- throw fetchErr;
478
- }
469
+ throw fetchErr;
479
470
  }
480
471
 
481
472
  if (response.status === 304) {
@@ -1342,28 +1333,14 @@ async function tuiTextInput({ title, stepLabel = title, hint = "", placeholder =
1342
1333
  });
1343
1334
  }
1344
1335
 
1345
- async function resolveBestApiUrl(explicitUrl) {
1336
+ async function resolveBestApiUrl(explicitUrl, flags = {}) {
1346
1337
  if (explicitUrl) return explicitUrl.replace(/\/+$/, "");
1338
+ if (flags.dev || flags.local) return "http://localhost:3000";
1347
1339
  if (process.env.NETWORKBASE_API_URL) return process.env.NETWORKBASE_API_URL.replace(/\/+$/, "");
1348
- const localCfg = getLocalConfig();
1349
- if (localCfg?.apiUrl) return localCfg.apiUrl.replace(/\/+$/, "");
1350
1340
  const creds = getCredentials();
1351
- if (creds?.apiUrl) return creds.apiUrl.replace(/\/+$/, "");
1352
-
1353
- // Probe local development server (http://localhost:3000) before defaulting to production
1354
- try {
1355
- const controller = new AbortController();
1356
- const timer = setTimeout(() => controller.abort(), 600);
1357
- const res = await fetch("http://localhost:3000/api/v1/extension/session", {
1358
- signal: controller.signal,
1359
- });
1360
- clearTimeout(timer);
1361
- if (res.status === 200 || res.status === 401) {
1362
- return "http://localhost:3000";
1363
- }
1364
- } catch {
1365
- // Local dev server not running; fall back to production URL
1366
- }
1341
+ if (creds?.apiUrl && creds.apiUrl !== "http://localhost:3000") return creds.apiUrl.replace(/\/+$/, "");
1342
+ const localCfg = getLocalConfig();
1343
+ if (localCfg?.apiUrl && localCfg.apiUrl !== "http://localhost:3000") return localCfg.apiUrl.replace(/\/+$/, "");
1367
1344
  return DEFAULT_API_URL.replace(/\/+$/, "");
1368
1345
  }
1369
1346
 
@@ -1391,9 +1368,28 @@ async function loginWithBrowser(apiUrl, flags = {}) {
1391
1368
  let timeoutHandle;
1392
1369
  const server = http.createServer(async (req, res) => {
1393
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
+
1394
1390
  const reqUrl = new URL(req.url || "/", "http://127.0.0.1");
1395
1391
  if (reqUrl.pathname !== "/auth-callback") {
1396
- res.writeHead(404, { "Content-Type": "text/plain" });
1392
+ res.writeHead(404, { ...corsHeaders, "Content-Type": "text/plain" });
1397
1393
  res.end("Not found");
1398
1394
  return;
1399
1395
  }
@@ -1402,14 +1398,39 @@ async function loginWithBrowser(apiUrl, flags = {}) {
1402
1398
  const message = reqUrl.searchParams.get("message");
1403
1399
  const code = reqUrl.searchParams.get("code");
1404
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"));
1405
1404
 
1406
1405
  if (error) {
1407
- res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" });
1408
- res.end(`<!doctype html><html><body style="font-family:system-ui,sans-serif;padding:40px;text-align:center;">
1409
- <h2>Authorization Cancelled</h2>
1410
- <p>${message || error}</p>
1411
- <p>You can close this window and return to your terminal.</p>
1412
- </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
+ }
1413
1434
  clearTimeout(timeoutHandle);
1414
1435
  server.close();
1415
1436
  reject(new Error(message || `Browser authorization failed: ${error}`));
@@ -1417,25 +1438,223 @@ async function loginWithBrowser(apiUrl, flags = {}) {
1417
1438
  }
1418
1439
 
1419
1440
  if (!code || returnedState !== state) {
1420
- res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" });
1421
- res.end(`<!doctype html><html><body style="font-family:system-ui,sans-serif;padding:40px;text-align:center;">
1422
- <h2>Invalid Callback State</h2>
1423
- <p>Please restart sign-in from your terminal.</p>
1424
- </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
+ }
1425
1469
  clearTimeout(timeoutHandle);
1426
1470
  server.close();
1427
1471
  reject(new Error("Invalid state or missing authorization code from browser callback."));
1428
1472
  return;
1429
1473
  }
1430
1474
 
1431
- res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
1432
- 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;">
1433
- <div style="max-width:420px;margin:0 auto;border:1px solid #262626;border-radius:12px;padding:32px;background:#121212;">
1434
- <div style="font-size:28px;margin-bottom:12px;">✔</div>
1435
- <h2 style="margin:0 0 8px;font-size:18px;font-weight:600;">NetworkBase CLI Connected</h2>
1436
- <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>
1437
- </div>
1438
- </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
+ }
1439
1658
 
1440
1659
  clearTimeout(timeoutHandle);
1441
1660
  server.close();
@@ -1506,41 +1725,13 @@ async function loginWithToken(rawToken, apiUrl, explicitUrl = null) {
1506
1725
  throw new Error("A valid `nb_dev_...` or `nb_pat_...` token is required.");
1507
1726
  }
1508
1727
 
1509
- let resolvedUrl = apiUrl;
1510
- let sessionInfo;
1511
- try {
1512
- sessionInfo = await apiRequest("/api/v1/extension/session", {
1513
- apiUrl: resolvedUrl,
1514
- token,
1515
- });
1516
- } catch (err) {
1517
- if (!explicitUrl && resolvedUrl !== "http://localhost:3000") {
1518
- resolvedUrl = "http://localhost:3000";
1519
- sessionInfo = await apiRequest("/api/v1/extension/session", {
1520
- apiUrl: resolvedUrl,
1521
- token,
1522
- });
1523
- } else {
1524
- throw err;
1525
- }
1526
- }
1527
-
1528
- if (!sessionInfo.active && !explicitUrl && resolvedUrl !== "http://localhost:3000") {
1529
- try {
1530
- const localInfo = await apiRequest("/api/v1/extension/session", {
1531
- apiUrl: "http://localhost:3000",
1532
- token,
1533
- });
1534
- if (localInfo?.active) {
1535
- resolvedUrl = "http://localhost:3000";
1536
- sessionInfo = localInfo;
1537
- }
1538
- } catch {
1539
- // Ignore fallback failure
1540
- }
1541
- }
1728
+ const resolvedUrl = apiUrl;
1729
+ const sessionInfo = await apiRequest("/api/v1/extension/session", {
1730
+ apiUrl: resolvedUrl,
1731
+ token,
1732
+ });
1542
1733
 
1543
- if (!sessionInfo.active) {
1734
+ if (!sessionInfo?.active) {
1544
1735
  throw new Error("This token is expired or has been revoked.");
1545
1736
  }
1546
1737
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@networkbase/cli",
3
- "version": "1.0.0",
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",