|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Candidate Claude Code binding. It never resolves `codeguard` from PATH or falls back to Python. |
| 5 | +const fs = require('node:fs'); |
| 6 | +const path = require('node:path'); |
| 7 | +const { spawnSync } = require('node:child_process'); |
| 8 | +const pluginRoot = fs.existsSync(path.join(__dirname, '..', 'runtime', 'codeguard_runtime.cjs')) |
| 9 | + ? path.resolve(__dirname, '..') : path.resolve(__dirname, '..', '..'); |
| 10 | +const { activeBinary } = require(path.join(pluginRoot, 'runtime', 'codeguard_runtime.cjs')); |
| 11 | + |
| 12 | +const EVENTS = new Map([ |
| 13 | + ['session-start', 'SessionStart'], |
| 14 | + ['post-tool-use', 'PostToolUse'], |
| 15 | + ['post-tool-use-failure', 'PostToolUseFailure'], |
| 16 | + ['stop', 'Stop'], |
| 17 | +]); |
| 18 | + |
| 19 | +function incomplete(event, reason) { |
| 20 | + const safe = /^[a-z_]+$/.test(reason) ? reason : 'runtime_unavailable'; |
| 21 | + const message = `CodeGuard Rust 运行时未完成(${safe});本次源码检查未运行,交付未评估。请执行插件 runtime/codeguard_runtime.cjs verify 或修复安装。`; |
| 22 | + return event === 'stop' |
| 23 | + ? { systemMessage: message } |
| 24 | + : { hookSpecificOutput: { hookEventName: EVENTS.get(event), additionalContext: message } }; |
| 25 | +} |
| 26 | + |
| 27 | +function readInput() { |
| 28 | + const pieces = []; |
| 29 | + const buffer = Buffer.allocUnsafe(64 * 1024); |
| 30 | + let total = 0; |
| 31 | + for (;;) { |
| 32 | + const length = fs.readSync(0, buffer, 0, buffer.length, null); |
| 33 | + if (length === 0) break; |
| 34 | + total += length; |
| 35 | + if (total > 1024 * 1024) throw new Error('host_input_too_large'); |
| 36 | + pieces.push(Buffer.from(buffer.subarray(0, length))); |
| 37 | + } |
| 38 | + return Buffer.concat(pieces); |
| 39 | +} |
| 40 | + |
| 41 | +function projectRoot() { |
| 42 | + const configured = process.env.CLAUDE_PROJECT_DIR; |
| 43 | + const value = configured && path.isAbsolute(configured) ? configured : process.cwd(); |
| 44 | + return fs.realpathSync(value); |
| 45 | +} |
| 46 | + |
| 47 | +function run(event) { |
| 48 | + if (!EVENTS.has(event)) throw new Error('event_unsupported'); |
| 49 | + const input = readInput(); |
| 50 | + const binary = activeBinary(); |
| 51 | + const root = projectRoot(); |
| 52 | + const result = spawnSync(binary, ['hook', 'claude', event, root, '--timeout=5s', '--format=json'], { |
| 53 | + input, encoding: 'utf8', shell: false, timeout: 8000, maxBuffer: 16 * 1024, |
| 54 | + env: process.env, |
| 55 | + }); |
| 56 | + if (result.error || result.signal || result.status !== 0) throw new Error('runtime_execution_failed'); |
| 57 | + let output; |
| 58 | + try { output = JSON.parse(result.stdout); } catch { throw new Error('runtime_response_invalid'); } |
| 59 | + if (event === 'stop') { |
| 60 | + if (typeof output.systemMessage !== 'string' |
| 61 | + && output.hookSpecificOutput?.hookEventName !== 'Stop') { |
| 62 | + throw new Error('runtime_response_invalid'); |
| 63 | + } |
| 64 | + } else if (output.hookSpecificOutput?.hookEventName !== EVENTS.get(event) |
| 65 | + || typeof output.hookSpecificOutput.additionalContext !== 'string') { |
| 66 | + throw new Error('runtime_response_invalid'); |
| 67 | + } |
| 68 | + return output; |
| 69 | +} |
| 70 | + |
| 71 | +if (require.main === module) { |
| 72 | + const event = process.argv[2]; |
| 73 | + try { |
| 74 | + process.stdout.write(JSON.stringify(run(event)) + '\n'); |
| 75 | + } catch (error) { |
| 76 | + const reason = typeof error.message === 'string' && error.message.startsWith('codeguard runtime incomplete: ') |
| 77 | + ? error.message.slice('codeguard runtime incomplete: '.length) |
| 78 | + : error.message; |
| 79 | + if (EVENTS.has(event)) process.stdout.write(JSON.stringify(incomplete(event, reason)) + '\n'); |
| 80 | + else { |
| 81 | + process.stderr.write('CodeGuard Rust 宿主事件无效。\n'); |
| 82 | + process.exitCode = 2; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = { run }; |
0 commit comments