refactor: route raw Bun.file/write/hash/which/spawn through runtime shims

Eliminates 19 unguarded `Bun.*` runtime call sites in the plugin bundle
that crashed with `ReferenceError: Bun is not defined` under Electron.

Per-tool-call hot paths (executed on every Read/Edit):
- src/tools/hashline-edit/hash-computation.ts: Bun.hash.xxHash32 → bunHashXxh32
- src/tools/hashline-edit/hashline-edit-executor.ts: 8 sites via bunFile/bunWrite
- src/hooks/hashline-read-enhancer/hook.ts: Bun.file → bunFile
- src/hooks/hashline-edit-diff-enhancer/hook.ts: 2 sites via bunFile

Plugin-load paths:
- src/hooks/claude-code-hooks/config.ts and config-loader.ts: Bun.file → bunFile
- src/features/claude-code-mcp-loader/loader.ts: Bun.file → bunFile
- src/features/claude-code-plugin-loader/mcp-server-loader.ts: Bun.file → bunFile
- src/features/team-mode/deps.ts: Bun.spawn → spawn shim
- src/hooks/session-notification-utils.ts: Bun.which → bunWhich, also drops
  the bare `declare const Bun` ambient declaration
- src/shared/binary-downloader.ts: Bun.write → bunWrite

Pure mechanical API swaps. No control-flow or signature changes.
This commit is contained in:
YeonGyu-Kim
2026-05-12 12:46:31 +09:00
parent 4394f34225
commit 0aafe20a85
11 changed files with 30 additions and 23 deletions
+2 -1
View File
@@ -1,12 +1,13 @@
import { HASHLINE_DICT } from "./constants"
import { createHashlineChunkFormatter } from "./hashline-chunk-formatter"
import { bunHashXxh32 } from "../../shared/bun-hash-shim"
const RE_SIGNIFICANT = /[\p{L}\p{N}]/u
function computeNormalizedLineHash(lineNumber: number, normalizedContent: string): string {
const stripped = normalizedContent
const seed = RE_SIGNIFICANT.test(stripped) ? 0 : lineNumber
const hash = Bun.hash.xxHash32(stripped, seed)
const hash = bunHashXxh32(stripped, seed)
const index = hash % 256
return HASHLINE_DICT[index]
}
@@ -1,5 +1,6 @@
import type { ToolContext } from "@opencode-ai/plugin/tool"
import { publishToolMetadata } from "../../features/tool-metadata-store"
import { bunFile, bunWrite } from "../../shared/bun-file-shim"
import { applyHashlineEditsWithReport } from "./edit-operations"
import { countLineDiffs, generateUnifiedDiff } from "./diff-utils"
import { canonicalizeFileText, restoreFileText } from "./file-text-canonicalization"
@@ -94,7 +95,7 @@ export async function executeHashlineEditTool(args: HashlineEditArgs, context: T
const edits = deleteMode ? [] : normalizeHashlineEdits(args.edits)
const file = Bun.file(filePath)
const file = bunFile(filePath)
const exists = await file.exists()
if (!exists && !deleteMode && !canCreateFromMissingFile(edits)) {
return `Error: File not found: ${filePath}`
@@ -102,7 +103,7 @@ export async function executeHashlineEditTool(args: HashlineEditArgs, context: T
if (deleteMode) {
if (!exists) return `Error: File not found: ${filePath}`
await Bun.file(filePath).delete()
await bunFile(filePath).delete()
return `Successfully deleted ${filePath}`
}
@@ -122,11 +123,11 @@ export async function executeHashlineEditTool(args: HashlineEditArgs, context: T
const writeContent = restoreFileText(canonicalNewContent, oldEnvelope)
await Bun.write(filePath, writeContent)
await bunWrite(filePath, writeContent)
if (pluginCtx?.client) {
await runFormattersForFile(pluginCtx.client as FormatterClient, context.directory, filePath)
const formattedContent = Buffer.from(await Bun.file(filePath).arrayBuffer()).toString("utf8")
const formattedContent = Buffer.from(await bunFile(filePath).arrayBuffer()).toString("utf8")
if (formattedContent !== writeContent) {
const formattedEnvelope = canonicalizeFileText(formattedContent)
const formattedMeta = buildSuccessMeta(
@@ -138,8 +139,8 @@ export async function executeHashlineEditTool(args: HashlineEditArgs, context: T
)
await publishToolMetadata(metadataContext, formattedMeta)
if (rename && rename !== filePath) {
await Bun.write(rename, formattedContent)
await Bun.file(filePath).delete()
await bunWrite(rename, formattedContent)
await bunFile(filePath).delete()
return `Moved ${filePath} to ${rename}`
}
return `Updated ${filePath}`
@@ -147,8 +148,8 @@ export async function executeHashlineEditTool(args: HashlineEditArgs, context: T
}
if (rename && rename !== filePath) {
await Bun.write(rename, writeContent)
await Bun.file(filePath).delete()
await bunWrite(rename, writeContent)
await bunFile(filePath).delete()
}
const effectivePath = rename && rename !== filePath ? rename : filePath