|
7 | 7 | [](https://twitter.com/SocketSecurity) |
8 | 8 | [](https://bsky.app/profile/socket.dev) |
9 | 9 |
|
10 | | -Core library for [Socket.dev](https://socket.dev/) tools. |
| 10 | +Core infrastructure library for [Socket.dev](https://socket.dev/) security tools. Provides utilities for file system operations, process spawning, HTTP requests, environment detection, logging, spinners, and more. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +**Node.js 22 or higher** is required. |
11 | 15 |
|
12 | 16 | ## Install |
13 | 17 |
|
14 | 18 | ```bash |
| 19 | +# Using pnpm (recommended) |
15 | 20 | pnpm add @socketsecurity/lib |
| 21 | + |
| 22 | +# Using npm |
| 23 | +npm install @socketsecurity/lib |
| 24 | + |
| 25 | +# Using yarn |
| 26 | +yarn add @socketsecurity/lib |
16 | 27 | ``` |
17 | 28 |
|
18 | | -## Usage |
| 29 | +## Quick Start |
19 | 30 |
|
20 | 31 | ```typescript |
21 | | -// Tree-shakeable exports |
22 | 32 | import { Spinner } from '@socketsecurity/lib/spinner' |
| 33 | +import { getDefaultLogger } from '@socketsecurity/lib/logger' |
23 | 34 | import { readJson } from '@socketsecurity/lib/fs' |
24 | | -import { PACKAGE, LATEST } from '@socketsecurity/lib/constants/packages' |
25 | 35 |
|
26 | | -const spinner = Spinner({ text: 'Loading...' }) |
| 36 | +const logger = getDefaultLogger() |
| 37 | +const spinner = Spinner({ text: 'Loading package.json...' }) |
| 38 | + |
27 | 39 | spinner.start() |
28 | 40 | const pkg = await readJson('./package.json') |
29 | | -spinner.stop() |
| 41 | +spinner.successAndStop('Loaded successfully') |
| 42 | + |
| 43 | +logger.success(`Package: ${pkg.name}@${pkg.version}`) |
30 | 44 | ``` |
31 | 45 |
|
| 46 | +## Documentation |
| 47 | + |
| 48 | +- [Getting Started](./docs/getting-started.md) - Prerequisites, installation, and first examples |
| 49 | +- [Visual Effects](./docs/visual-effects.md) - Spinners, loggers, themes, and progress indicators |
| 50 | +- [File System](./docs/file-system.md) - File operations, globs, paths, and safe deletion |
| 51 | +- [HTTP Utilities](./docs/http-utilities.md) - Making requests, downloading files, and retry logic |
| 52 | +- [Process Utilities](./docs/process-utilities.md) - Spawning processes, IPC, and locks |
| 53 | +- [Package Management](./docs/package-management.md) - npm/pnpm/yarn detection and operations |
| 54 | +- [Environment](./docs/environment.md) - CI detection, env getters, and platform checks |
| 55 | +- [Constants](./docs/constants.md) - Node versions, npm URLs, and platform values |
| 56 | +- [Examples](./docs/examples.md) - Real-world usage patterns |
| 57 | +- [Troubleshooting](./docs/troubleshooting.md) - Common issues and solutions |
| 58 | + |
32 | 59 | ## What's Inside |
33 | 60 |
|
34 | | -- **Visual Effects** → logger, spinner, themes |
35 | | -- **File System** → fs, globs, paths |
36 | | -- **Package Management** → dlx, npm, pnpm, yarn |
37 | | -- **Process & Spawn** → process spawning |
38 | | -- **Environment** → env getters |
39 | | -- **Constants** → node, npm, platform |
40 | | -- **Utilities** → arrays, objects, promises, strings |
| 61 | +### Visual Effects |
| 62 | +Spinners, colored loggers, themes, progress bars, and terminal output formatting. |
| 63 | +- `Spinner` - Animated CLI spinners with progress tracking |
| 64 | +- `getDefaultLogger()` - Colored console logger with symbols |
| 65 | +- `LOG_SYMBOLS` - Colored terminal symbols (✓, ✗, ⚠, ℹ, →) |
| 66 | +- `setTheme()` - Customize colors across the library |
| 67 | + |
| 68 | +### File System |
| 69 | +Cross-platform file operations with safe deletion and convenient wrappers. |
| 70 | +- `readFileUtf8()`, `readFileBinary()` - Read files as text or binary |
| 71 | +- `readJson()`, `writeJson()` - Parse and format JSON files |
| 72 | +- `safeDelete()` - Protected deletion with safety checks |
| 73 | +- `findUp()`, `findUpSync()` - Traverse up to find files |
| 74 | +- `safeMkdir()` - Create directories without EEXIST errors |
| 75 | +- `validateFiles()` - Check file readability (useful for Yarn PnP, pnpm) |
| 76 | + |
| 77 | +### HTTP Utilities |
| 78 | +Native Node.js HTTP/HTTPS requests with retry logic and redirects. |
| 79 | +- `httpJson()` - Fetch and parse JSON from APIs |
| 80 | +- `httpText()` - Fetch text/HTML content |
| 81 | +- `httpDownload()` - Download files with progress callbacks |
| 82 | +- `httpRequest()` - Full control over requests and responses |
| 83 | +- Automatic redirects, exponential backoff retries, timeout support |
| 84 | + |
| 85 | +### Process Management |
| 86 | +Spawn child processes safely with cross-platform support. |
| 87 | +- `spawn()` - Promise-based process spawning with output capture |
| 88 | +- `spawnSync()` - Synchronous version for blocking operations |
| 89 | +- Array-based arguments prevent command injection |
| 90 | +- Automatic Windows `.cmd`/`.bat` handling |
| 91 | +- `ProcessLock` - Ensure only one instance runs at a time |
| 92 | +- `setupIPC()` - Inter-process communication |
| 93 | + |
| 94 | +### Environment Detection |
| 95 | +Type-safe environment variable access and platform detection. |
| 96 | +- `getCI()` - Detect CI environment |
| 97 | +- `getNodeEnv()` - Get NODE_ENV value |
| 98 | +- `isTest()` - Check if running tests |
| 99 | +- `getHome()`, `getUserProfile()` - Cross-platform home directory |
| 100 | +- Test rewiring with `setEnv()`, `resetEnv()` |
| 101 | + |
| 102 | +### Package Management |
| 103 | +Detect and work with npm, pnpm, and yarn. |
| 104 | +- `detectPackageManager()` - Identify package manager from lock files |
| 105 | +- Package manifest operations |
| 106 | +- Lock file management |
| 107 | + |
| 108 | +### Constants |
| 109 | +Pre-defined values for Node.js, npm, and platform detection. |
| 110 | +- `MIN_SUPPORTED_NODE_VERSION` - Minimum Node.js version |
| 111 | +- `MAIN_REGISTRY_URL` - npm registry URL |
| 112 | +- `WIN32`, `DARWIN`, `LINUX` - Platform booleans |
| 113 | +- `getAbortSignal()` - Global abort signal |
| 114 | + |
| 115 | +### Utilities |
| 116 | +Helpers for arrays, objects, strings, promises, sorting, and more. |
| 117 | +- Arrays, objects, strings manipulation |
| 118 | +- Promise utilities and queues |
| 119 | +- Natural sorting |
| 120 | +- Version comparison |
| 121 | +- Error handling with causes |
| 122 | + |
| 123 | +## Features |
| 124 | + |
| 125 | +- **Tree-shakeable exports** - Import only what you need |
| 126 | +- **Cross-platform** - Works on Windows, macOS, and Linux |
| 127 | +- **TypeScript-first** - Full type safety with .d.ts files |
| 128 | +- **Zero dependencies** (for core HTTP - uses Node.js native modules) |
| 129 | +- **Well-tested** - 84% coverage with comprehensive test suite |
| 130 | +- **Security-focused** - Safe defaults, command injection protection |
| 131 | +- **CommonJS output** - Compatible with Node.js tooling |
| 132 | + |
| 133 | +## Common Use Cases |
| 134 | + |
| 135 | +### Running Shell Commands |
| 136 | +```typescript |
| 137 | +import { spawn } from '@socketsecurity/lib/spawn' |
| 138 | + |
| 139 | +const result = await spawn('git', ['status']) |
| 140 | +console.log(result.stdout) |
| 141 | +``` |
| 142 | + |
| 143 | +### Making API Requests |
| 144 | +```typescript |
| 145 | +import { httpJson } from '@socketsecurity/lib/http-request' |
| 146 | + |
| 147 | +const data = await httpJson('https://api.example.com/data') |
| 148 | +``` |
| 149 | + |
| 150 | +### Visual Feedback |
| 151 | +```typescript |
| 152 | +import { Spinner } from '@socketsecurity/lib/spinner' |
| 153 | + |
| 154 | +const spinner = Spinner({ text: 'Processing...' }) |
| 155 | +spinner.start() |
| 156 | +// ... do work ... |
| 157 | +spinner.successAndStop('Complete!') |
| 158 | +``` |
| 159 | + |
| 160 | +### Safe File Deletion |
| 161 | +```typescript |
| 162 | +import { safeDelete } from '@socketsecurity/lib/fs' |
| 163 | + |
| 164 | +// Protected against deleting parent directories |
| 165 | +await safeDelete('./build') |
| 166 | +``` |
| 167 | + |
| 168 | +## Troubleshooting |
| 169 | + |
| 170 | +**Module not found**: Verify you're importing from the correct path: |
| 171 | +```typescript |
| 172 | +// Correct |
| 173 | +import { Spinner } from '@socketsecurity/lib/spinner' |
| 174 | + |
| 175 | +// Wrong |
| 176 | +import { Spinner } from '@socketsecurity/lib' |
| 177 | +``` |
| 178 | + |
| 179 | +**Node version error**: This library requires Node.js 22+. Check your version: |
| 180 | +```bash |
| 181 | +node --version |
| 182 | +``` |
| 183 | + |
| 184 | +For more issues, see the [Troubleshooting Guide](./docs/troubleshooting.md). |
41 | 185 |
|
42 | 186 | ## Development |
43 | 187 |
|
44 | 188 | ```bash |
45 | | -pnpm install # Install |
46 | | -pnpm build # Build |
47 | | -pnpm test # Test |
| 189 | +pnpm install # Install dependencies |
| 190 | +pnpm build # Build the library |
| 191 | +pnpm test # Run tests |
| 192 | +pnpm run cover # Run tests with coverage |
48 | 193 | pnpm dev # Watch mode |
| 194 | +pnpm run lint # Check code style |
| 195 | +pnpm run fix # Fix formatting issues |
49 | 196 | ``` |
50 | 197 |
|
| 198 | +## Contributing |
| 199 | + |
| 200 | +Contributions are welcome! Please read the [CLAUDE.md](./CLAUDE.md) file for development guidelines and coding standards. |
| 201 | + |
51 | 202 | ## License |
52 | 203 |
|
53 | 204 | MIT |
0 commit comments