Skip to content

Commit 106a8df

Browse files
committed
Update claude.md
1 parent 73d1a7b commit 106a8df

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

CLAUDE.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ You are a **Principal Software Engineer** responsible for:
4949
- **Update snapshots**: `pnpm test:unit -u` or `pnpm testu`
5050
- **Coverage report**: `pnpm test:unit:coverage`
5151

52+
#### Vitest Memory Optimization (CRITICAL)
53+
- **Pool configuration**: Use `pool: 'forks'` with `singleFork: true`, `maxForks: 1`, `isolate: true`
54+
- **Memory limits**: Set `NODE_OPTIONS="--max-old-space-size=4096 --max-semi-space-size=512"` in `.env.test`
55+
- **Timeout settings**: Use `testTimeout: 60000, hookTimeout: 60000` for stability
56+
- **Thread limits**: Use `singleThread: true, maxThreads: 1` to prevent RegExp compiler exhaustion
57+
- **Test cleanup**: 🚨 MANDATORY - Use `await trash([paths])` in test scripts/utilities only. For cleanup within `/src/` test files, use `fs.rm()` with proper error handling
58+
5259
### Cross-Platform Compatibility - CRITICAL: Windows and POSIX
5360
- **🚨 MANDATORY**: Tests and functionality MUST work on both POSIX (macOS/Linux) and Windows systems
5461
- **Path handling**: ALWAYS use `path.join()`, `path.resolve()`, `path.sep` for file paths
@@ -59,6 +66,9 @@ You are a **Principal Software Engineer** responsible for:
5966
- **Temp directories**: Use `os.tmpdir()` for temporary file paths in tests
6067
- ❌ WRONG: `'/tmp/test-project'` (POSIX-specific)
6168
- ✅ CORRECT: `path.join(os.tmpdir(), 'test-project')` (cross-platform)
69+
- **Unique temp dirs**: Use `fs.mkdtemp()` or `fs.mkdtempSync()` for collision-free directories
70+
- ✅ PREFERRED: `await fs.mkdtemp(path.join(os.tmpdir(), 'socket-test-'))` (async)
71+
- ✅ ACCEPTABLE: `fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-'))` (sync)
6272
- **Path separators**: Never hard-code `/` or `\` in paths
6373
- Use `path.sep` when you need the separator character
6474
- Use `path.join()` to construct paths correctly
@@ -122,41 +132,48 @@ You are a **Principal Software Engineer** responsible for:
122132

123133
## Architecture
124134

125-
This is a JavaScript implementation of the Package URL (purl) specification for parsing and constructing package URLs.
135+
This is a TypeScript implementation of the Package URL (purl) specification for parsing and constructing package URLs, compiled to CommonJS for deployment.
126136

127137
### Core Structure
128-
- **Main entry**: `src/index.js` - Main exports and API
138+
- **Main entry**: `src/package-url.ts` - Main exports and API (TypeScript)
129139
- **Parser**: Core parsing logic for purl strings
130140
- **Normalizer**: Normalization logic for different package types
131141
- **Validator**: Input validation and sanitization
132142
- **Types**: Type-specific handling for npm, pypi, maven, etc.
133143
- **Scripts**: `scripts/` - Build and utility scripts
134144
- **Tests**: `test/` - Comprehensive test suite
145+
- **Build output**: `dist/cjs/` - CommonJS TypeScript compilation output
135146

136147
### Key Features
137148
- Full purl specification compliance
138-
- High-performance parsing
149+
- High-performance parsing with TypeScript type safety
139150
- Type-specific normalization
140151
- Comprehensive validation
141152
- Extensive test coverage
153+
- CommonJS-only deployment for maximum compatibility
142154

143155
## 🔧 Code Style (MANDATORY)
144156

145157
### 📁 File Organization
146-
- **File extensions**: Use `.js` for JavaScript files, `.mjs` for ES modules
158+
- **File extensions**: Use `.ts` for TypeScript files, `.js` for JavaScript files, `.mjs` for ES modules
147159
- **Import order**: Node.js built-ins first, then third-party packages, then local imports
148160
- **Import grouping**: Group imports by source (Node.js, external packages, local modules)
149-
- **Type imports**: When using JSDoc, keep type imports organized
161+
- **Type imports**: 🚨 ALWAYS use separate `import type` statements for TypeScript types, NEVER mix runtime imports with type imports in the same statement
162+
- ✅ CORRECT: `import { readPackageJson } from '@socketsecurity/registry/lib/packages'` followed by `import type { PackageJson } from '@socketsecurity/registry/lib/packages'`
163+
- ❌ FORBIDDEN: `import { readPackageJson, type PackageJson } from '@socketsecurity/registry/lib/packages'`
150164

151165
### Naming Conventions
152-
- **Constants**: Use `UPPER_SNAKE_CASE` for constants
153-
- **Files**: Use kebab-case for filenames
166+
- **Constants**: Use `UPPER_SNAKE_CASE` for constants (e.g., `CMD_NAME`, `REPORT_LEVEL`)
167+
- **Files**: Use kebab-case for filenames (e.g., `package-url.ts`, `purl-type.ts`)
154168
- **Variables**: Use camelCase for variables and functions
155169
- **Classes**: Use PascalCase for classes
170+
- **Types/Interfaces**: Use PascalCase for TypeScript types and interfaces
156171

157172
### 🏗️ Code Structure (CRITICAL PATTERNS)
173+
- **Type definitions**: 🚨 ALWAYS use `import type` for better tree-shaking and TypeScript optimization
158174
- **Error handling**: 🚨 REQUIRED - Use try-catch blocks and handle errors gracefully
159175
- **Array destructuring**: Use object notation `{ 0: key, 1: data }` instead of array destructuring `[key, data]`
176+
- **Dynamic imports**: 🚨 FORBIDDEN - Never use dynamic imports (`await import()`). Always use static imports at the top of the file
160177
- **Comment formatting**: 🚨 MANDATORY - ALL comments MUST follow these rules:
161178
- **Periods required**: Every comment MUST end with a period, except ESLint disable comments and URLs which are directives/references. This includes single-line, multi-line, inline, and c8 ignore comments.
162179
- **Sentence structure**: Comments should be complete sentences with proper capitalization and grammar.
@@ -177,18 +194,30 @@ This is a JavaScript implementation of the Package URL (purl) specification for
177194
- **Existence checks**: Perform simple existence checks first before complex operations
178195
- **Destructuring order**: Sort destructured properties alphabetically in const declarations
179196
- **Function ordering**: Place functions in alphabetical order, with private functions first, then exported functions
180-
- **Object mappings**: Use objects with `__proto__: null` for static mappings to prevent prototype pollution
181-
- **Array length checks**: Use `!array.length` instead of `array.length === 0`
197+
- **Object mappings**: Use objects with `__proto__: null` (not `undefined`) for static string-to-string mappings and lookup tables to prevent prototype pollution; use `Map` for dynamic collections that will be mutated
198+
- **Mapping constants**: Move static mapping objects outside functions as module-level constants with descriptive UPPER_SNAKE_CASE names
199+
- **Array length checks**: Use `!array.length` instead of `array.length === 0`. For `array.length > 0`, use `!!array.length` when function must return boolean, or `array.length` when used in conditional contexts
182200
- **Catch parameter naming**: Use `catch (e)` instead of `catch (error)` for consistency
183-
- **Number formatting**: 🚨 REQUIRED - Use underscore separators (e.g., `20_000`) for large numeric literals
201+
- **Number formatting**: 🚨 REQUIRED - Use underscore separators (e.g., `20_000`) for large numeric literals. 🚨 FORBIDDEN - Do NOT modify number values inside strings
202+
- **Node.js fs imports**: 🚨 MANDATORY pattern - `import { someSyncThing, promises as fs } from 'node:fs'`
203+
- **Process spawning**: 🚨 FORBIDDEN to use Node.js built-in `child_process.spawn` - MUST use `spawn` from `@socketsecurity/registry/lib/spawn`
184204

185205
### 🗑️ Safe File Operations (SECURITY CRITICAL)
186-
- **File deletion**: 🚨 ABSOLUTELY FORBIDDEN - NEVER use `rm -rf`. 🚨 MANDATORY - ALWAYS use `pnpm dlx trash-cli`
206+
- **Script usage only**: Use `trash` package ONLY in scripts, build files, and utilities - NOT in `/src/` files
207+
- **Import and use `trash` package**: `import { trash } from 'trash'` then `await trash(paths)` (scripts only)
208+
- **Source code deletion**: In `/src/` files, use `fs.rm()` with proper error handling when deletion is required
209+
- **Script deletion operations**: Use `await trash()` for scripts, build processes, and development utilities
210+
- **Array optimization**: `trash` accepts arrays - collect paths and pass as array
211+
- **Async requirement**: Always `await trash()` - it's an async operation
212+
- **NO rmSync**: 🚨 ABSOLUTELY FORBIDDEN - NEVER use `fs.rmSync()` or `rm -rf` commands
187213
- **Examples**:
188214
- ❌ CATASTROPHIC: `rm -rf directory` (permanent deletion - DATA LOSS RISK)
189215
- ❌ REPOSITORY DESTROYER: `rm -rf "$(pwd)"` (deletes entire repository)
190-
- ✅ SAFE: `pnpm dlx trash-cli directory` (recoverable deletion)
191-
- **Why this matters**: trash-cli enables recovery from accidental deletions via system trash/recycle bin
216+
- ❌ FORBIDDEN: `fs.rmSync(tmpDir, { recursive: true, force: true })` (dangerous)
217+
- ✅ SCRIPTS: `await trash([tmpDir])` (recoverable deletion in build scripts)
218+
- ✅ SOURCE CODE: `await fs.rm(tmpDir, { recursive: true, force: true })` (when needed in /src/)
219+
- **Why scripts use trash**: Enables recovery from accidental deletions during development and build processes
220+
- **Why source avoids trash**: Bundling complications and dependency management issues in production code
192221

193222
### 🔧 Formatting Rules
194223
- **Indentation**: 2 spaces (no tabs)

0 commit comments

Comments
 (0)