Skip to content

Commit 210ed10

Browse files
zknprclaude
andauthored
docs: update CLAUDE.md with stale fixes and missing patterns (#110)
* fix: address code review suggestions for PR #108 1. cellEditBehavior precedence: Move VS Code env config read after state restoration so extension settings override restored webview state. If the user changes the setting while a tab is hidden, the new value takes effect on re-show. 2. Unit tests: Add tests for getNodeFs() (returns fs module in Node.js, returns same reference on repeated calls) and LogEnvelope processing (onLog callback invoked with correct args, silent drop without callback). 3. JSDoc cleanup: Remove duplicate processProtocolMessage JSDoc block in rpc.ts and consolidate into single comment with onLog parameter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: update CLAUDE.md with 8 stale fixes and missing patterns Stale fixes: - Uint8Array serialization: document Base64 format as preferred, array as legacy - saveCellEdit: fix reference from backendApi.exec() to backendApi.updateCell() - Web demo paths: website/public/demo/ → website/public/sqlite-viewer/ - Worker loading: webWorker.ts → workerFactory.ts (file doesn't exist) - Worker logging: document RPC LogEnvelope routing to output channel - retainContextWhenHidden: now false, document setState/getState flow Missing content added: - Key files: json-utils, cancellation-utils, serialization, documentRegistry, webviewMessageHandler, webview-collection - Patterns: webview state persistence, getNodeFs(), json_patch() optimization with runtime fallback, queryBatch for batched IPC Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a1affda commit 210ed10

1 file changed

Lines changed: 45 additions & 9 deletions

File tree

CLAUDE.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@ The extension uses a three-layer communication architecture:
5151
| `src/core/sqlite-db.ts` | Database engine wrapper |
5252
| `src/core/query-builder.ts` | Safe SQL query construction |
5353
| `src/core/sql-utils.ts` | SQL utilities and escaping |
54+
| `src/core/json-utils.ts` | JSON Merge Patch (RFC 7396) generate and apply |
55+
| `src/core/cancellation-utils.ts` | CancellationToken to AbortSignal bridge |
56+
| `src/core/serialization.ts` | Serialization utilities |
5457
| `src/core/undo-history.ts` | ModificationTracker for undo/redo |
58+
| `src/documentRegistry.ts` | Global registry of open DatabaseDocument instances |
59+
| `src/webviewMessageHandler.ts` | Webview → Extension Host message routing |
60+
| `src/webview-collection.ts` | Tracks active webview panels per document |
5561
| `src/virtualFileSystem.ts` | Virtual FS provider for editing cells in tabs |
5662
| `src/loggingDatabaseOperations.ts` | Decorator for logging SQL queries |
5763
| `core/ui/modules/settings.js` | UI logic for database settings/pragma editor |
5864
| `core/ui/modules/web-api.js` | Web demo API module (parent window communication) |
5965
| `core/ui/viewer.html` | Standalone webview UI |
6066
| `core/ui/web-viewer.js` | Web demo entry point |
6167
| `website/app/demo/page.tsx` | Web demo React page |
62-
| `website/public/demo/worker.js` | Web demo SQLite worker |
63-
| `website/public/demo/viewer.html` | Web demo bundled viewer |
68+
| `website/public/sqlite-viewer/worker.js` | Web demo SQLite worker |
69+
| `website/public/sqlite-viewer/viewer.html` | Web demo bundled viewer |
6470
| `assets/sqlite3.wasm` | SQLite WebAssembly binary |
6571

6672
### RPC Protocol
@@ -127,16 +133,26 @@ workerProxy.method(new Transfer(data, [data.buffer]));
127133
```
128134

129135
**Uint8Array Serialization:**
130-
`Uint8Array` cannot be directly serialized via `postMessage` (becomes `{}`). The RPC layer uses a marker format:
136+
`Uint8Array` cannot be directly serialized via `postMessage` (becomes `{}`). The RPC layer uses Base64 encoding:
131137
```javascript
132-
// Serialized format (safe for JSON)
138+
// Serialized format (preferred, compact)
139+
{ __type: 'Uint8Array', base64: 'SGVsbG8=' }
140+
141+
// Legacy array format (supported for backward-compatible deserialization only)
133142
{ __type: 'Uint8Array', data: [72, 101, 108, 108, 111] }
134143

135-
// Security: Marker must have exactly 2 keys (__type, data) to prevent collision with user data
144+
// Security: Marker must have exactly 2 keys to prevent collision with user data
136145
```
137146
- Webview serializes in requests, deserializes in responses (`core/ui/modules/api.js`)
138147
- Extension host deserializes in requests, serializes in responses (`src/editorController.ts`)
139148

149+
**Worker Log Forwarding:**
150+
Workers route logs through RPC using `LogEnvelope` instead of `console.*`:
151+
```javascript
152+
{ kind: 'log', level: 'warn', args: ['message', 42] }
153+
```
154+
The host routes these to the VS Code "SQLite Explorer" output channel via `GlobalOutputChannel`.
155+
140156
## Security Standards
141157

142158
### Content Security Policy (CSP)
@@ -184,7 +200,7 @@ npm test
184200
- `out/worker.js` - Node.js worker
185201
- `out/worker-browser.js` - Browser worker
186202
- `core/ui/viewer.html` - Webview UI
187-
- `website/public/demo/viewer.html` - Web demo viewer (bundled)
203+
- `website/public/sqlite-viewer/viewer.html` - Web demo viewer (bundled)
188204
- `assets/sqlite3.wasm` - SQLite WASM binary
189205

190206
### Web Demo
@@ -262,7 +278,7 @@ The build uses esbuild with these targets:
262278

263279
The webview handles inline editing:
264280
1. Double-click cell → Creates input overlay
265-
2. Enter key → `saveCellEdit()` sends UPDATE via `backendApi.exec()`
281+
2. Enter key → `saveCellEdit()` sends UPDATE via `backendApi.updateCell()`
266282
3. Escape key → `cancelCellEdit()` discards changes
267283

268284
### Virtual File System
@@ -280,6 +296,26 @@ Database operations are wrapped in `LoggingDatabaseOperations` which writes all
280296

281297
The webview provides a UI to configure SQLite PRAGMAs (e.g., WAL mode, Foreign Keys) directly via `hostBridge.setPragma()`.
282298

299+
### Webview State Persistence
300+
301+
`retainContextWhenHidden` is `false` — webviews are destroyed when hidden to save memory. State survives via `vscodeApi.setState()`/`getState()`:
302+
- `persistState()` in `state.js` debounces (500ms) and serializes user-facing state (selected table, scroll position, filters, pins, settings)
303+
- `viewer.js` restores state on re-initialization, including scroll position after grid render
304+
- VS Code extension settings (e.g., `cellEditBehavior`) take precedence over restored state
305+
- Web demo (`web-api.js`) provides no-op stubs since there is no VS Code API
306+
307+
### Platform Helpers
308+
309+
`getNodeFs()` in `sqlite-db.ts` safely requires the Node.js `fs` module, returning `undefined` in browser environments. Used by `sqlite-db.ts` (file reading, writing) and `tableExporter.ts` (streaming export).
310+
311+
### JSON Patch Optimization
312+
313+
`updateCell` and `updateCellBatch` in `sqlite-db.ts` probe for SQLite's `json_patch()` at engine construction time (`hasJsonPatch` flag). When available, uses `json_patch(COALESCE(col, '{}'), ?)` in a single UPDATE (no SELECT round-trip). Falls back to JS-side `applyMergePatch()` from `json-utils.ts` when JSON1 extension is unavailable.
314+
315+
### Batched IPC
316+
317+
`queryBatch` in `nativeWorker.ts` sends multiple SQL queries in a single IPC round-trip. Used for schema fetching (3 queries → 1 call) and pragma reads.
318+
283319
### Blob Inspector
284320

285321
The Blob Inspector (`core/ui/modules/blob-inspector.js`) provides preview and editing for BLOB data:
@@ -337,13 +373,13 @@ ConfigurationSection = 'sqliteExplorer'
337373

338374
1. **RPC timeout**: Check that message format matches expected protocol
339375
2. **CSP errors**: Verify Content-Security-Policy in `editorController.ts`
340-
3. **Worker not loading**: Check worker path resolution in `webWorker.ts`
376+
3. **Worker not loading**: Check worker path resolution in `workerFactory.ts`
341377
4. **WASM not found**: Ensure `assets/sqlite3.wasm` exists after build
342378

343379
### Logging
344380

345381
- Extension Host: `console.log()` appears in VS Code Developer Tools
346-
- Worker: `console.log()` appears in Extension Host output
382+
- Worker: Logs route via RPC `LogEnvelope` to "SQLite Explorer" output channel (View → Output → SQLite Explorer)
347383
- Webview: Use browser DevTools (Cmd+Shift+P → "Developer: Open Webview Developer Tools")
348384

349385
## Development Workflow

0 commit comments

Comments
 (0)