-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: deduplicate execGit, UUID validation, pagination, and remove compat aliases #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * Shared execGit utility — imported by both git.js and worktreeManager.js | ||
| * to avoid a circular dependency (git.js imports worktreeManager.js). | ||
| */ | ||
|
|
||
| import { spawn } from 'child_process'; | ||
|
|
||
| /** | ||
| * Execute a git command safely using spawn (prevents shell injection). | ||
| * @param {string[]} args - Git command arguments | ||
| * @param {string} cwd - Working directory | ||
| * @param {object} options - Additional options | ||
| * @param {number} [options.maxBuffer] - Max output buffer size in bytes (default 10 MB) | ||
| * @param {number} [options.timeout] - Timeout in ms (default 30s) | ||
| * @param {boolean} [options.ignoreExitCode] - Resolve instead of reject on non-zero exit | ||
| * @returns {Promise<{stdout: string, stderr: string, exitCode: number}>} | ||
| */ | ||
| export function execGit(args, cwd, options = {}) { | ||
| return new Promise((resolve, reject) => { | ||
| const maxBuffer = options.maxBuffer || 10 * 1024 * 1024; | ||
| const timeout = options.timeout || 30000; | ||
| const child = spawn('git', args, { | ||
| cwd, | ||
| shell: process.platform === 'win32', | ||
| windowsHide: true | ||
| }); | ||
|
Comment on lines
+8
to
+26
|
||
|
|
||
| let stdout = ''; | ||
| let stderr = ''; | ||
| let killed = false; | ||
|
|
||
| const timer = setTimeout(() => { | ||
| if (!killed) { | ||
| killed = true; | ||
| child.kill(); | ||
| reject(new Error(`git command timed out after ${timeout / 1000}s: git ${args.join(' ')}`)); | ||
| } | ||
| }, timeout); | ||
|
|
||
| child.stdout.on('data', (data) => { | ||
| stdout += data.toString(); | ||
| if (stdout.length + stderr.length > maxBuffer && !killed) { | ||
| killed = true; | ||
| clearTimeout(timer); | ||
| child.kill(); | ||
| reject(new Error(`git output exceeded maxBuffer (${maxBuffer} bytes)`)); | ||
| } | ||
| }); | ||
|
|
||
| child.stderr.on('data', (data) => { | ||
| stderr += data.toString(); | ||
| if (stdout.length + stderr.length > maxBuffer && !killed) { | ||
| killed = true; | ||
| clearTimeout(timer); | ||
| child.kill(); | ||
| reject(new Error(`git output exceeded maxBuffer (${maxBuffer} bytes)`)); | ||
| } | ||
| }); | ||
|
|
||
| child.on('close', (code) => { | ||
| clearTimeout(timer); | ||
| if (killed) return; | ||
| if (code !== 0 && !options.ignoreExitCode) { | ||
| reject(new Error(stderr || `git exited with code ${code}`)); | ||
| } else { | ||
| resolve({ stdout, stderr, exitCode: code }); | ||
| } | ||
| }); | ||
|
|
||
| child.on('error', (err) => { | ||
| clearTimeout(timer); | ||
| reject(err); | ||
| }); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -539,6 +539,24 @@ export function validateRequest(schema, data) { | |
| }); | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // PAGINATION HELPERS | ||
| // ============================================================================= | ||
|
|
||
| /** | ||
| * Parse limit/offset pagination from query params with defaults and clamping. | ||
| * @param {object} query - req.query object | ||
| * @param {object} options - { defaultLimit, maxLimit } | ||
| * @returns {{ limit: number, offset: number }} | ||
| */ | ||
| export function parsePagination(query, { defaultLimit = 50, maxLimit = 200 } = {}) { | ||
| const rawLimit = parseInt(query?.limit, 10); | ||
| const rawOffset = parseInt(query?.offset, 10); | ||
| const limit = Number.isFinite(rawLimit) && rawLimit > 0 ? Math.min(rawLimit, maxLimit) : defaultLimit; | ||
| const offset = Number.isFinite(rawOffset) && rawOffset >= 0 ? rawOffset : 0; | ||
atomantic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { limit, offset }; | ||
atomantic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
Comment on lines
+552
to
+558
|
||
|
|
||
| // ============================================================================= | ||
| // TASK METADATA SANITIZATION | ||
| // ============================================================================= | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.