|
| 1 | +# Socket CLI Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Socket CLI is a TypeScript-based command-line tool for security analysis and package management. It's designed with modularity and maintainability in mind, using a clear separation of concerns. |
| 6 | + |
| 7 | +## Directory Structure |
| 8 | + |
| 9 | +``` |
| 10 | +socket-cli/ |
| 11 | +├── src/ # Source code |
| 12 | +│ ├── cli.mts # Main CLI entry point |
| 13 | +│ ├── commands.mts # Command registry |
| 14 | +│ ├── constants.mts # Shared constants |
| 15 | +│ ├── types.mts # Type definitions |
| 16 | +│ ├── commands/ # Command implementations |
| 17 | +│ ├── utils/ # Shared utilities |
| 18 | +│ ├── shadow/ # Package manager shadows |
| 19 | +│ ├── sea/ # Single Executable Application |
| 20 | +│ └── external/ # External dependencies |
| 21 | +├── bin/ # Binary entry points |
| 22 | +├── dist/ # Compiled output |
| 23 | +├── scripts/ # Build and utility scripts |
| 24 | +├── test/ # Test files |
| 25 | +└── .config/ # Configuration files |
| 26 | +``` |
| 27 | + |
| 28 | +## Core Concepts |
| 29 | + |
| 30 | +### 1. Command Pattern |
| 31 | +Each command follows a consistent pattern: |
| 32 | +- `cmd-*.mts` - CLI interface and flag parsing |
| 33 | +- `handle-*.mts` - Business logic implementation |
| 34 | +- `output-*.mts` - Output formatting |
| 35 | +- `fetch-*.mts` - API data fetching |
| 36 | + |
| 37 | +### 2. Shadow Binaries |
| 38 | +The CLI can act as a shadow for npm, npx, and pnpm, intercepting and enhancing their functionality: |
| 39 | +- Located in `src/shadow/` |
| 40 | +- Provides security scanning during package operations |
| 41 | +- Transparent to the end user |
| 42 | + |
| 43 | +### 3. Utils Organization |
| 44 | +Utilities are grouped by function to reduce cognitive load: |
| 45 | +- **API & Network** - API client and HTTP utilities |
| 46 | +- **Error Handling** - Centralized error types and handlers |
| 47 | +- **Output & Formatting** - Consistent output formatting |
| 48 | +- **File System** - File operations and path handling |
| 49 | +- **Package Management** - Package manager detection and operations |
| 50 | + |
| 51 | +### 4. Constants Management |
| 52 | +Constants are imported from `@socketsecurity/registry` and extended with CLI-specific values: |
| 53 | +- Shared constants from registry |
| 54 | +- CLI-specific constants |
| 55 | +- Environment variables |
| 56 | +- Configuration keys |
| 57 | + |
| 58 | +## Key Design Patterns |
| 59 | + |
| 60 | +### Result/Either Pattern |
| 61 | +Used throughout for error handling: |
| 62 | +```typescript |
| 63 | +type CResult<T> = |
| 64 | + | { ok: true; data: T; message?: string } |
| 65 | + | { ok: false; message: string; code?: number } |
| 66 | +``` |
| 67 | +
|
| 68 | +### Lazy Loading |
| 69 | +Heavy dependencies are loaded only when needed to improve startup time. |
| 70 | +
|
| 71 | +### Configuration Cascade |
| 72 | +Configuration is resolved in order: |
| 73 | +1. Command-line flags |
| 74 | +2. Environment variables |
| 75 | +3. Local config file (.socketrc) |
| 76 | +4. Global config file |
| 77 | +5. Default values |
| 78 | +
|
| 79 | +## Build System |
| 80 | +
|
| 81 | +### Rollup Configuration |
| 82 | +- Main build uses Rollup for tree-shaking and optimization |
| 83 | +- Separate configs for different build targets |
| 84 | +- JSON files are inlined during build |
| 85 | +
|
| 86 | +### TypeScript Compilation |
| 87 | +- Uses `.mts` extensions for ES modules |
| 88 | +- Compiled with `tsgo` for better performance |
| 89 | +- Type definitions generated separately |
| 90 | +
|
| 91 | +## Testing Strategy |
| 92 | +
|
| 93 | +### Unit Tests |
| 94 | +- Located alongside source files as `*.test.mts` |
| 95 | +- Focus on individual function behavior |
| 96 | +- Run with Vitest |
| 97 | +
|
| 98 | +### Integration Tests |
| 99 | +- Located in `test/integration/` |
| 100 | +- Test command flows end-to-end |
| 101 | +- Mock external API calls |
| 102 | +
|
| 103 | +## Performance Considerations |
| 104 | +
|
| 105 | +### Startup Time |
| 106 | +- Minimal dependencies loaded at startup |
| 107 | +- Commands lazy-loaded on demand |
| 108 | +- Constants use lazy getters |
| 109 | +
|
| 110 | +### Memory Usage |
| 111 | +- Configurable memory limits via flags |
| 112 | +- Streaming for large data sets |
| 113 | +- Efficient caching strategies |
| 114 | +
|
| 115 | +## Security |
| 116 | +
|
| 117 | +### API Token Management |
| 118 | +- Tokens never logged or displayed |
| 119 | +- Stored securely in system keychain when possible |
| 120 | +- Validated before use |
| 121 | +
|
| 122 | +### Package Scanning |
| 123 | +- Real-time scanning during installs |
| 124 | +- Comprehensive vulnerability database |
| 125 | +- Configurable risk policies |
| 126 | +
|
| 127 | +## Extension Points |
| 128 | +
|
| 129 | +### Custom Commands |
| 130 | +Commands can be added by: |
| 131 | +1. Creating files in `src/commands/` |
| 132 | +2. Following the command pattern |
| 133 | +3. Registering in `src/commands.mts` |
| 134 | +
|
| 135 | +### Plugins |
| 136 | +Future support planned for plugins via: |
| 137 | +- Standard plugin interface |
| 138 | +- Dynamic loading |
| 139 | +- Isolated execution context |
| 140 | +
|
| 141 | +## Best Practices |
| 142 | +
|
| 143 | +1. **Keep files focused** - Single responsibility per file |
| 144 | +2. **Use TypeScript strictly** - No `any` types without good reason |
| 145 | +3. **Document exports** - JSDoc for all public APIs |
| 146 | +4. **Test thoroughly** - Aim for high coverage |
| 147 | +5. **Handle errors gracefully** - Use Result pattern |
| 148 | +6. **Optimize imports** - Import only what's needed |
| 149 | +7. **Follow patterns** - Consistency reduces cognitive load |
| 150 | +
|
| 151 | +## Common Tasks |
| 152 | +
|
| 153 | +### Adding a New Command |
| 154 | +1. Create `src/commands/[name]/cmd-[name].mts` |
| 155 | +2. Implement command logic in `handle-[name].mts` |
| 156 | +3. Add output formatting in `output-[name].mts` |
| 157 | +4. Register in `src/commands.mts` |
| 158 | +5. Add tests |
| 159 | +
|
| 160 | +### Adding a Utility |
| 161 | +1. Choose appropriate category in `src/utils/` |
| 162 | +2. Create focused utility file |
| 163 | +3. Export from category index if applicable |
| 164 | +4. Document with JSDoc |
| 165 | +5. Add unit tests |
| 166 | +
|
| 167 | +### Updating Constants |
| 168 | +1. Check if constant exists in `@socketsecurity/registry` |
| 169 | +2. If shared, add to registry |
| 170 | +3. If CLI-specific, add to `src/constants.mts` |
| 171 | +4. Use descriptive names |
| 172 | +5. Group related constants |
0 commit comments