smartcontext-proxy 0.1.0 → 0.2.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.
Files changed (48) hide show
  1. package/PLAN-v2.md +390 -0
  2. package/dist/src/context/ab-test.d.ts +32 -0
  3. package/dist/src/context/ab-test.js +133 -0
  4. package/dist/src/index.js +99 -78
  5. package/dist/src/proxy/classifier.d.ts +14 -0
  6. package/dist/src/proxy/classifier.js +63 -0
  7. package/dist/src/proxy/connect-proxy.d.ts +34 -0
  8. package/dist/src/proxy/connect-proxy.js +167 -0
  9. package/dist/src/proxy/tls-interceptor.d.ts +23 -0
  10. package/dist/src/proxy/tls-interceptor.js +211 -0
  11. package/dist/src/proxy/tunnel.d.ts +7 -0
  12. package/dist/src/proxy/tunnel.js +33 -0
  13. package/dist/src/system/installer.d.ts +25 -0
  14. package/dist/src/system/installer.js +180 -0
  15. package/dist/src/system/linux.d.ts +11 -0
  16. package/dist/src/system/linux.js +60 -0
  17. package/dist/src/system/macos.d.ts +24 -0
  18. package/dist/src/system/macos.js +98 -0
  19. package/dist/src/system/watchdog.d.ts +7 -0
  20. package/dist/src/system/watchdog.js +115 -0
  21. package/dist/src/test/connect-proxy.test.d.ts +1 -0
  22. package/dist/src/test/connect-proxy.test.js +147 -0
  23. package/dist/src/tls/ca-manager.d.ts +9 -0
  24. package/dist/src/tls/ca-manager.js +117 -0
  25. package/dist/src/tls/trust-store.d.ts +11 -0
  26. package/dist/src/tls/trust-store.js +121 -0
  27. package/dist/src/tray/bridge.d.ts +8 -0
  28. package/dist/src/tray/bridge.js +66 -0
  29. package/dist/src/ui/ws-feed.d.ts +8 -0
  30. package/dist/src/ui/ws-feed.js +30 -0
  31. package/native/macos/SmartContextTray/Package.swift +13 -0
  32. package/native/macos/SmartContextTray/Sources/main.swift +206 -0
  33. package/package.json +6 -2
  34. package/src/context/ab-test.ts +172 -0
  35. package/src/index.ts +104 -74
  36. package/src/proxy/classifier.ts +71 -0
  37. package/src/proxy/connect-proxy.ts +187 -0
  38. package/src/proxy/tls-interceptor.ts +261 -0
  39. package/src/proxy/tunnel.ts +32 -0
  40. package/src/system/installer.ts +148 -0
  41. package/src/system/linux.ts +57 -0
  42. package/src/system/macos.ts +89 -0
  43. package/src/system/watchdog.ts +76 -0
  44. package/src/test/connect-proxy.test.ts +170 -0
  45. package/src/tls/ca-manager.ts +140 -0
  46. package/src/tls/trust-store.ts +123 -0
  47. package/src/tray/bridge.ts +61 -0
  48. package/src/ui/ws-feed.ts +32 -0
@@ -0,0 +1,61 @@
1
+ import { execFile, type ChildProcess } from 'node:child_process';
2
+ import path from 'node:path';
3
+ import fs from 'node:fs';
4
+
5
+ let trayProcess: ChildProcess | null = null;
6
+
7
+ const TRAY_BINARY_DIR = path.join(__dirname, '..', '..', 'native', 'macos', 'SmartContextTray');
8
+ const TRAY_BUILD_DIR = path.join(TRAY_BINARY_DIR, '.build', 'release');
9
+ const TRAY_BINARY = path.join(TRAY_BUILD_DIR, 'SmartContextTray');
10
+
11
+ /** Build the native tray app if not already built */
12
+ export function buildTray(): boolean {
13
+ if (process.platform !== 'darwin') return false;
14
+
15
+ if (fs.existsSync(TRAY_BINARY)) return true;
16
+
17
+ try {
18
+ const { execFileSync } = require('node:child_process');
19
+ execFileSync('swift', ['build', '-c', 'release'], {
20
+ cwd: TRAY_BINARY_DIR,
21
+ stdio: 'pipe',
22
+ timeout: 60000,
23
+ });
24
+ return fs.existsSync(TRAY_BINARY);
25
+ } catch (err) {
26
+ console.log(`Tray build failed: ${err}`);
27
+ return false;
28
+ }
29
+ }
30
+
31
+ /** Launch the tray app */
32
+ export function startTray(): boolean {
33
+ if (process.platform !== 'darwin') return false;
34
+ if (trayProcess) return true;
35
+
36
+ if (!buildTray()) {
37
+ console.log('Tray app not available (build failed or not macOS)');
38
+ return false;
39
+ }
40
+
41
+ trayProcess = execFile(TRAY_BINARY, [], {}, (err) => {
42
+ if (err) console.log(`Tray exited: ${err.message}`);
43
+ trayProcess = null;
44
+ });
45
+
46
+ trayProcess.unref();
47
+ return true;
48
+ }
49
+
50
+ /** Stop the tray app */
51
+ export function stopTray(): void {
52
+ if (trayProcess) {
53
+ trayProcess.kill('SIGTERM');
54
+ trayProcess = null;
55
+ }
56
+ }
57
+
58
+ /** Check if tray is running */
59
+ export function isTrayRunning(): boolean {
60
+ return trayProcess !== null && !trayProcess.killed;
61
+ }
@@ -0,0 +1,32 @@
1
+ import { WebSocketServer, WebSocket } from 'ws';
2
+ import type { Server } from 'node:http';
3
+ import type { RequestMetric } from '../metrics/collector.js';
4
+
5
+ let wss: WebSocketServer | null = null;
6
+
7
+ /** Attach WebSocket server to existing HTTP server */
8
+ export function attachWebSocketFeed(server: Server): void {
9
+ wss = new WebSocketServer({ server, path: '/_sc/ws' });
10
+
11
+ wss.on('connection', (ws) => {
12
+ ws.send(JSON.stringify({ type: 'connected', timestamp: Date.now() }));
13
+ });
14
+ }
15
+
16
+ /** Broadcast a new request metric to all connected clients */
17
+ export function broadcastMetric(metric: RequestMetric): void {
18
+ if (!wss) return;
19
+
20
+ const data = JSON.stringify({ type: 'request', data: metric });
21
+
22
+ for (const client of wss.clients) {
23
+ if (client.readyState === WebSocket.OPEN) {
24
+ client.send(data);
25
+ }
26
+ }
27
+ }
28
+
29
+ /** Get count of connected WebSocket clients */
30
+ export function getConnectedClients(): number {
31
+ return wss?.clients.size || 0;
32
+ }