v2.1.88 反编译源码

从 npm 包 @anthropic-ai/claude-code 2.1.88 版本提取的反编译源码
包含 src/ 目录下的 TypeScript 源文件及 vendor/ 原生模块源码

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
sanbuphy
2026-03-31 17:08:54 +08:00
commit a988ee13cb
1907 changed files with 513677 additions and 0 deletions

151
vendor/audio-capture-src/index.ts vendored Normal file
View File

@@ -0,0 +1,151 @@
type AudioCaptureNapi = {
startRecording(
onData: (data: Buffer) => void,
onEnd: () => void,
): boolean
stopRecording(): void
isRecording(): boolean
startPlayback(sampleRate: number, channels: number): boolean
writePlaybackData(data: Buffer): void
stopPlayback(): void
isPlaying(): boolean
// TCC microphone authorization status (macOS only):
// 0 = notDetermined, 1 = restricted, 2 = denied, 3 = authorized.
// Linux: always returns 3 (authorized) — no system-level microphone permission API.
// Windows: returns 3 (authorized) if registry key absent or allowed,
// 2 (denied) if microphone access is explicitly denied.
microphoneAuthorizationStatus?(): number
}
let cachedModule: AudioCaptureNapi | null = null
let loadAttempted = false
function loadModule(): AudioCaptureNapi | null {
if (loadAttempted) {
return cachedModule
}
loadAttempted = true
// Supported platforms: macOS (darwin), Linux, Windows (win32)
const platform = process.platform
if (platform !== 'darwin' && platform !== 'linux' && platform !== 'win32') {
return null
}
// Candidate 1: native-embed path (bun compile). AUDIO_CAPTURE_NODE_PATH is
// defined at build time in build-with-plugins.ts for native builds only — the
// define resolves it to the static literal "../../audio-capture.node" so bun
// compile can rewrite it to /$bunfs/root/audio-capture.node. MUST stay a
// direct require(env var) — bun cannot analyze require(variable) from a loop.
if (process.env.AUDIO_CAPTURE_NODE_PATH) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = require(
process.env.AUDIO_CAPTURE_NODE_PATH,
) as AudioCaptureNapi
return cachedModule
} catch {
// fall through to runtime fallbacks below
}
}
// Candidates 2/3: npm-install and dev/source layouts. Dynamic require is
// fine here — in bundled output (node --target build) require() resolves at
// runtime relative to cli.js at the package root; in dev it resolves
// relative to this file (vendor/audio-capture-src/index.ts).
const platformDir = `${process.arch}-${platform}`
const fallbacks = [
`./vendor/audio-capture/${platformDir}/audio-capture.node`,
`../audio-capture/${platformDir}/audio-capture.node`,
]
for (const p of fallbacks) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = require(p) as AudioCaptureNapi
return cachedModule
} catch {
// try next
}
}
return null
}
export function isNativeAudioAvailable(): boolean {
return loadModule() !== null
}
export function startNativeRecording(
onData: (data: Buffer) => void,
onEnd: () => void,
): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.startRecording(onData, onEnd)
}
export function stopNativeRecording(): void {
const mod = loadModule()
if (!mod) {
return
}
mod.stopRecording()
}
export function isNativeRecordingActive(): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.isRecording()
}
export function startNativePlayback(
sampleRate: number,
channels: number,
): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.startPlayback(sampleRate, channels)
}
export function writeNativePlaybackData(data: Buffer): void {
const mod = loadModule()
if (!mod) {
return
}
mod.writePlaybackData(data)
}
export function stopNativePlayback(): void {
const mod = loadModule()
if (!mod) {
return
}
mod.stopPlayback()
}
export function isNativePlaying(): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.isPlaying()
}
// Returns the microphone authorization status.
// On macOS, returns the TCC status: 0=notDetermined, 1=restricted, 2=denied, 3=authorized.
// On Linux, always returns 3 (authorized) — no system-level mic permission API.
// On Windows, returns 3 (authorized) if registry key absent or allowed, 2 (denied) if explicitly denied.
// Returns 0 (notDetermined) if the native module is unavailable.
export function microphoneAuthorizationStatus(): number {
const mod = loadModule()
if (!mod || !mod.microphoneAuthorizationStatus) {
return 0
}
return mod.microphoneAuthorizationStatus()
}

163
vendor/image-processor-src/index.ts vendored Normal file
View File

@@ -0,0 +1,163 @@
export type ClipboardImageResult = {
png: Buffer
originalWidth: number
originalHeight: number
width: number
height: number
}
// Clipboard functions are macOS-only and only present in darwin binaries;
// older/non-darwin binaries built before this addition won't export them.
// Typed as optional so callers can guard. These property names appear only
// in type-space here; all runtime property access lives in src/ behind
// feature() so they tree-shake out of builds that don't want them.
export type NativeModule = {
processImage: (input: Buffer) => Promise<ImageProcessor>
readClipboardImage?: (maxWidth: number, maxHeight: number) => ClipboardImageResult | null
hasClipboardImage?: () => boolean
}
// Lazy: defers dlopen until first call. The .node binary links against
// CoreGraphics/ImageIO on darwin; resolving that at module-eval time blocks
// startup because imagePaste.ts pulls this into the REPL chunk via static
// import. Same pattern as audio-capture-src/index.ts.
let cachedModule: NativeModule | null = null
let loadAttempted = false
// Raw binding accessor. Callers that need optional exports (e.g. clipboard
// functions) reach through this; keeping the wrappers on the caller side lets
// feature() tree-shake the property access strings out of external builds.
export function getNativeModule(): NativeModule | null {
if (loadAttempted) return cachedModule
loadAttempted = true
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = require('../../image-processor.node')
} catch {
cachedModule = null
}
return cachedModule
}
interface ImageProcessor {
metadata(): { width: number; height: number; format: string }
resize(
width: number,
height: number,
options?: { fit?: string; withoutEnlargement?: boolean },
): ImageProcessor
jpeg(quality?: number): ImageProcessor
png(options?: {
compressionLevel?: number
palette?: boolean
colors?: number
}): ImageProcessor
webp(quality?: number): ImageProcessor
toBuffer(): Promise<Buffer>
}
interface SharpInstance {
metadata(): Promise<{ width: number; height: number; format: string }>
resize(
width: number,
height: number,
options?: { fit?: string; withoutEnlargement?: boolean },
): SharpInstance
jpeg(options?: { quality?: number }): SharpInstance
png(options?: {
compressionLevel?: number
palette?: boolean
colors?: number
}): SharpInstance
webp(options?: { quality?: number }): SharpInstance
toBuffer(): Promise<Buffer>
}
// Factory function that matches sharp's API
export function sharp(input: Buffer): SharpInstance {
let processorPromise: Promise<ImageProcessor> | null = null
// Create a chain of operations
const operations: Array<(proc: ImageProcessor) => void> = []
// Track how many operations have been applied to avoid re-applying
let appliedOperationsCount = 0
// Get or create the processor (without applying operations)
async function ensureProcessor(): Promise<ImageProcessor> {
if (!processorPromise) {
processorPromise = (async () => {
const mod = getNativeModule()
if (!mod) {
throw new Error('Native image processor module not available')
}
return mod.processImage(input)
})()
}
return processorPromise
}
// Apply any pending operations to the processor
function applyPendingOperations(proc: ImageProcessor): void {
for (let i = appliedOperationsCount; i < operations.length; i++) {
const op = operations[i]
if (op) {
op(proc)
}
}
appliedOperationsCount = operations.length
}
const instance: SharpInstance = {
async metadata() {
const proc = await ensureProcessor()
return proc.metadata()
},
resize(
width: number,
height: number,
options?: { fit?: string; withoutEnlargement?: boolean },
) {
operations.push(proc => {
proc.resize(width, height, options)
})
return instance
},
jpeg(options?: { quality?: number }) {
operations.push(proc => {
proc.jpeg(options?.quality)
})
return instance
},
png(options?: {
compressionLevel?: number
palette?: boolean
colors?: number
}) {
operations.push(proc => {
proc.png(options)
})
return instance
},
webp(options?: { quality?: number }) {
operations.push(proc => {
proc.webp(options?.quality)
})
return instance
},
async toBuffer() {
const proc = await ensureProcessor()
applyPendingOperations(proc)
return proc.toBuffer()
},
}
return instance
}
export default sharp

67
vendor/modifiers-napi-src/index.ts vendored Normal file
View File

@@ -0,0 +1,67 @@
import { createRequire } from 'module'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
type ModifiersNapi = {
getModifiers(): string[]
isModifierPressed(modifier: string): boolean
}
let cachedModule: ModifiersNapi | null = null
function loadModule(): ModifiersNapi | null {
if (cachedModule) {
return cachedModule
}
// Only works on macOS
if (process.platform !== 'darwin') {
return null
}
try {
if (process.env.MODIFIERS_NODE_PATH) {
// Bundled mode - use the env var path
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = require(process.env.MODIFIERS_NODE_PATH) as ModifiersNapi
} else {
// Dev mode - load from vendor directory
const modulePath = join(
dirname(fileURLToPath(import.meta.url)),
'..',
'modifiers-napi',
`${process.arch}-darwin`,
'modifiers.node',
)
cachedModule = createRequire(import.meta.url)(modulePath) as ModifiersNapi
}
return cachedModule
} catch {
return null
}
}
export function getModifiers(): string[] {
const mod = loadModule()
if (!mod) {
return []
}
return mod.getModifiers()
}
export function isModifierPressed(modifier: string): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.isModifierPressed(modifier)
}
/**
* Pre-warm the native module by loading it in advance.
* Call this early (e.g., at startup) to avoid delay on first use.
*/
export function prewarm(): void {
// Just call loadModule to cache it
loadModule()
}

58
vendor/url-handler-src/index.ts vendored Normal file
View File

@@ -0,0 +1,58 @@
import { createRequire } from 'module'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
type UrlHandlerNapi = {
waitForUrlEvent(timeoutMs: number): string | null
}
let cachedModule: UrlHandlerNapi | null = null
function loadModule(): UrlHandlerNapi | null {
if (cachedModule) {
return cachedModule
}
// Only works on macOS
if (process.platform !== 'darwin') {
return null
}
try {
if (process.env.URL_HANDLER_NODE_PATH) {
// Bundled mode - use the env var path
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = require(process.env.URL_HANDLER_NODE_PATH) as UrlHandlerNapi
} else {
// Dev mode - load from vendor directory
const modulePath = join(
dirname(fileURLToPath(import.meta.url)),
'..',
'url-handler',
`${process.arch}-darwin`,
'url-handler.node',
)
cachedModule = createRequire(import.meta.url)(modulePath) as UrlHandlerNapi
}
return cachedModule
} catch {
return null
}
}
/**
* Wait for a macOS URL event (Apple Event kAEGetURL).
*
* Initializes NSApplication, registers for the URL event, and pumps
* the event loop for up to `timeoutMs` milliseconds.
*
* Returns the URL string if one was received, or null.
* Only functional on macOS — returns null on other platforms.
*/
export function waitForUrlEvent(timeoutMs: number): string | null {
const mod = loadModule()
if (!mod) {
return null
}
return mod.waitForUrlEvent(timeoutMs)
}