# Zero-Dependency Brainstorm Server Implementation Plan > **For agentic workers:** REQUIRED: Use superpowers:subagent-driven-development (if subagents available) or superpowers:executing-plans to implement this plan. Steps use checkbox (`- [ ]`) syntax for tracking. **Goal:** Replace the brainstorm server's vendored node_modules with a single zero-dependency `server.js` using Node built-ins. **Architecture:** Single file with WebSocket protocol (RFC 6455 text frames), HTTP server (`http` module), and file watching (`fs.watch`). Exports protocol functions for unit testing when required as a module. **Tech Stack:** Node.js built-ins only: `http`, `crypto`, `fs`, `path` **Spec:** `docs/superpowers/specs/2026-03-11-zero-dep-brainstorm-server-design.md` **Existing tests:** `tests/brainstorm-server/ws-protocol.test.js` (unit), `tests/brainstorm-server/server.test.js` (integration) --- ## File Map - **Create:** `skills/brainstorming/scripts/server.js` — the zero-dep replacement - **Modify:** `skills/brainstorming/scripts/start-server.sh:94,100` — change `index.js` to `server.js` - **Modify:** `.gitignore:6` — remove the `!skills/brainstorming/scripts/node_modules/` exception - **Delete:** `skills/brainstorming/scripts/index.js` - **Delete:** `skills/brainstorming/scripts/package.json` - **Delete:** `skills/brainstorming/scripts/package-lock.json` - **Delete:** `skills/brainstorming/scripts/node_modules/` (714 files) - **No changes:** `skills/brainstorming/scripts/helper.js`, `skills/brainstorming/scripts/frame-template.html`, `skills/brainstorming/scripts/stop-server.sh` --- ## Chunk 1: WebSocket Protocol Layer ### Task 1: Implement WebSocket protocol exports **Files:** - Create: `skills/brainstorming/scripts/server.js` - Test: `tests/brainstorm-server/ws-protocol.test.js` (already exists) - [ ] **Step 1: Create server.js with OPCODES constant and computeAcceptKey** ```js const crypto = require('crypto'); const OPCODES = { TEXT: 0x01, CLOSE: 0x08, PING: 0x09, PONG: 0x0A }; const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; function computeAcceptKey(clientKey) { return crypto.createHash('sha1').update(clientKey + WS_MAGIC).digest('base64'); } ``` - [ ] **Step 2: Implement encodeFrame** Server frames are never masked. Three length encodings: - payload < 126: 2-byte header (FIN+opcode, length) - 126-65535: 4-byte header (FIN+opcode, 126, 16-bit length) - > 65535: 10-byte header (FIN+opcode, 127, 64-bit length) ```js function encodeFrame(opcode, payload) { const fin = 0x80; const len = payload.length; let header; if (len < 126) { header = Buffer.alloc(2); header[0] = fin | opcode; header[1] = len; } else if (len < 65536) { header = Buffer.alloc(4); header[0] = fin | opcode; header[1] = 126; header.writeUInt16BE(len, 2); } else { header = Buffer.alloc(10); header[0] = fin | opcode; header[1] = 127; header.writeBigUInt64BE(BigInt(len), 2); } return Buffer.concat([header, payload]); } ``` - [ ] **Step 3: Implement decodeFrame** Client frames are always masked. Returns `{ opcode, payload, bytesConsumed }` or `null` for incomplete. Throws on unmasked frames. ```js function decodeFrame(buffer) { if (buffer.length < 2) return null; const firstByte = buffer[0]; const secondByte = buffer[1]; const opcode = firstByte & 0x0F; const masked = (secondByte & 0x80) !== 0; let payloadLen = secondByte & 0x7F; let offset = 2; if (!masked) throw new Error('Client frames must be masked'); if (payloadLen === 126) { if (buffer.length < 4) return null; payloadLen = buffer.readUInt16BE(2); offset = 4; } else if (payloadLen === 127) { if (buffer.length < 10) return null; payloadLen = Number(buffer.readBigUInt64BE(2)); offset = 10; } const maskOffset = offset; const dataOffset = offset + 4; const totalLen = dataOffset + payloadLen; if (buffer.length < totalLen) return null; const mask = buffer.slice(maskOffset, dataOffset); const data = Buffer.alloc(payloadLen); for (let i = 0; i < payloadLen; i++) { data[i] = buffer[dataOffset + i] ^ mask[i % 4]; } return { opcode, payload: data, bytesConsumed: totalLen }; } ``` - [ ] **Step 4: Add module exports at the bottom of the file** ```js module.exports = { computeAcceptKey, encodeFrame, decodeFrame, OPCODES }; ``` - [ ] **Step 5: Run unit tests** Run: `cd tests/brainstorm-server && node ws-protocol.test.js` Expected: All tests pass (handshake, encoding, decoding, boundaries, edge cases) - [ ] **Step 6: Commit** ```bash git add skills/brainstorming/scripts/server.js git commit -m "Add WebSocket protocol layer for zero-dep brainstorm server" ``` --- ## Chunk 2: HTTP Server and Application Logic ### Task 2: Add HTTP server, file watching, and WebSocket connection handling **Files:** - Modify: `skills/brainstorming/scripts/server.js` - Test: `tests/brainstorm-server/server.test.js` (already exists) - [ ] **Step 1: Add configuration and constants at top of server.js (after requires)** ```js const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = process.env.BRAINSTORM_PORT || (49152 + Math.floor(Math.random() * 16383)); const HOST = process.env.BRAINSTORM_HOST || '127.0.0.1'; const URL_HOST = process.env.BRAINSTORM_URL_HOST || (HOST === '127.0.0.1' ? 'localhost' : HOST); const SCREEN_DIR = process.env.BRAINSTORM_DIR || '/tmp/brainstorm'; const MIME_TYPES = { '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml' }; ``` - [ ] **Step 2: Add WAITING_PAGE, template loading at module scope, and helper functions** Load `frameTemplate` and `helperInjection` at module scope so they're accessible to `wrapInFrame` and `handleRequest`. They only read files from `__dirname` (the scripts directory), which is valid whether the module is required or run directly. ```js const WAITING_PAGE = `
Waiting for Claude to push a screen...
`; const frameTemplate = fs.readFileSync(path.join(__dirname, 'frame-template.html'), 'utf-8'); const helperScript = fs.readFileSync(path.join(__dirname, 'helper.js'), 'utf-8'); const helperInjection = ''; function isFullDocument(html) { const trimmed = html.trimStart().toLowerCase(); return trimmed.startsWith('', content); } function getNewestScreen() { const files = fs.readdirSync(SCREEN_DIR) .filter(f => f.endsWith('.html')) .map(f => { const fp = path.join(SCREEN_DIR, f); return { path: fp, mtime: fs.statSync(fp).mtime.getTime() }; }) .sort((a, b) => b.mtime - a.mtime); return files.length > 0 ? files[0].path : null; } ``` - [ ] **Step 3: Add HTTP request handler** ```js function handleRequest(req, res) { if (req.method === 'GET' && req.url === '/') { const screenFile = getNewestScreen(); let html = screenFile ? (raw => isFullDocument(raw) ? raw : wrapInFrame(raw))(fs.readFileSync(screenFile, 'utf-8')) : WAITING_PAGE; if (html.includes('