mirror of
https://github.com/logancyang/obsidian-copilot.git
synced 2026-07-22 07:50:24 +00:00
Mobile Obsidian runs on Capacitor + WebView (no Node.js runtime), so the bare `Buffer` global is undefined. On iOS WebKit this surfaces as the runtime error "Can't find variable: Buffer" the moment the image processing path fires. Desktop is unaffected because Electron exposes Node globals. Two PRs in 3.3.0 introduced direct `Buffer.from(...)` calls in image-handling paths assuming "Buffer is global": - #2403 rewrote src/utils/base64.ts from btoa/atob to bare Buffer.from - #2401 did the same in src/utils.ts safeFetch arrayBuffer branch and src/LLMProviders/BedrockChatModel.ts decodeBase64ToUint8Array Result: any image attachment on mobile fails with "failed to send image, please try again". Confirmed via user log: ERROR Error sending message: Can't find variable: Buffer The fix is an explicit `import { Buffer } from "buffer"` in each affected file. The `buffer` npm package (already in dependencies) is a browser-compatible polyfill that esbuild bundles into main.js, so the same Buffer code path works on desktop (where it's effectively the Electron-provided Buffer) and on mobile (where it's the polyfill). Two lint rules complicate this: - Obsidian's official scorecard flags btoa/atob as deprecated and asks for Buffer.from. That rules out reverting to btoa/atob. - The repo's own `import/no-nodejs-modules` rule blocks `import from "buffer"`. Suppressed inline at each import site with justification, because `buffer` here is the npm polyfill (in package.json dependencies), not Node's built-in being used at runtime. Files touched: - src/utils/base64.ts (arrayBufferToBase64 / base64ToArrayBuffer) This is the call site the image processor hits and the path encryptionService.ts uses for WEBCRYPTO_PREFIX (mobile encryption), so mobile encryption is also fixed transitively. - src/utils.ts (safeFetch arrayBuffer fallback) - src/LLMProviders/BedrockChatModel.ts (decodeBase64ToUint8Array) Regression test: - src/utils/base64.test.ts deletes globalThis.Buffer before exercising the helpers to confirm the imported polyfill binding is what gets used (not a bare global reference). Catches any future regression where someone replaces the import with a bare global usage. Out of scope: - src/encryptionService.ts has Buffer.from references but they sit inside desktop-only branches (`getSafeStorage().isEncryptionAvailable()` / DESKTOP_PREFIX) that mobile never enters. Touching them from a mobile hotfix would expand blast radius unnecessarily. - src/LLMProviders/BedrockChatModel.ts:835 is already guarded with `typeof Buffer !== "undefined"` so mobile skips it; left alone. Bundle size unchanged at 3.3 MB (buffer polyfill is small). Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| __tests__ | ||
| cache | ||
| commands | ||
| components | ||
| context | ||
| contexts | ||
| core | ||
| editor | ||
| hooks | ||
| imageProcessing | ||
| integration_tests | ||
| lib | ||
| LLMProviders | ||
| memory | ||
| mentions | ||
| miyo | ||
| projects | ||
| search | ||
| services | ||
| settings | ||
| state | ||
| styles | ||
| system-prompts | ||
| tests | ||
| tools | ||
| types | ||
| utils | ||
| aiParams.ts | ||
| chainFactory.ts | ||
| chainType.ts | ||
| chainUtils.ts | ||
| chatUtils.toolMarkers.test.ts | ||
| chatUtils.ts | ||
| composerUtils.ts | ||
| constants.ts | ||
| context.ts | ||
| contextProcessor.dataview.test.ts | ||
| contextProcessor.embeds.test.ts | ||
| contextProcessor.selectedText.test.ts | ||
| contextProcessor.ts | ||
| encryptionService.test.ts | ||
| encryptionService.ts | ||
| error.ts | ||
| errorFormat.ts | ||
| langchainStream.test.ts | ||
| langchainStream.ts | ||
| logFileManager.ts | ||
| logger.ts | ||
| main.ts | ||
| noteUtils.ts | ||
| plusUtils.ts | ||
| rateLimiter.ts | ||
| types.ts | ||
| utils.cleanMessageForCopy.test.ts | ||
| utils.test.ts | ||
| utils.ts | ||