UNPKG

homebridge-hikvision-local

Version:

Homebridge plugin that connects to your HikVision DVR via a local connection and exposes your cameras in Homebridge. The plugin is heavily based on excellent [homebridge-camera-ffmpeg](https://github.com/Sunoo/homebridge-camera-ffmpeg) and the various hom

60 lines 2.08 kB
import { EventEmitter } from 'events'; /** * Parses multipart-style stream parts that include `Content-Length` headers. * Emits `message` events with { headers, body }. */ export class MultipartXmlStreamParser extends EventEmitter { constructor() { super(...arguments); this.buffer = ''; this.processing = false; } write(chunk) { this.buffer += chunk.toString('utf8'); if (!this.processing) { this.processing = true; this.processBuffer(); } } processBuffer() { while (true) { const headerEnd = this.buffer.indexOf('\r\n\r\n'); if (headerEnd === -1) { break; } const rawHeaders = this.buffer.slice(0, headerEnd); const headers = this.parseHeaders(rawHeaders); const contentLength = parseInt(headers['content-length'] || '', 10); if (isNaN(contentLength)) { this.emit('error', new Error('Missing or invalid Content-Length header')); this.buffer = this.buffer.slice(headerEnd + 4); continue; } const totalLength = headerEnd + 4 + contentLength; if (this.buffer.length < totalLength) { break; } const body = this.buffer.slice(headerEnd + 4, totalLength); this.emit('message', { headers, body: body.trim(), }); this.buffer = this.buffer.slice(totalLength); } this.processing = false; } parseHeaders(headerText) { const headers = {}; const lines = headerText.split('\r\n'); for (const line of lines) { const sep = line.indexOf(':'); if (sep !== -1) { const key = line.slice(0, sep).trim().toLowerCase(); const value = line.slice(sep + 1).trim(); headers[key] = value; } } return headers; } } //# sourceMappingURL=MultiPartXMLStreamParser.js.map