UNPKG

@homebridge/hap-client

Version:
47 lines 1.59 kB
import { Buffer } from 'node:buffer'; import { createConnection as netCreateConnection } from 'node:net'; import httpMessageParser from './httpParser.js'; export const parseMessage = httpMessageParser; export function createConnection(instance, pin, body) { const client = netCreateConnection({ host: instance.ipAddress, port: instance.port, }); client.write(_buildMessage({ method: 'PUT', url: `http://${instance.ipAddress}:${instance.port}/characteristics`, maxAttempts: 1, headers: { 'Content-Type': 'Application/json', 'authorization': pin, 'connection': 'keep-alive', }, body: JSON.stringify(body), })); return client; } function _headersToString(headers) { let response = ''; for (const header of Object.keys(headers)) { response = `${response + header}: ${headers[header]}\r\n`; } return (response); } function _buildMessage(request) { const context = new URL(request.url); let message; message = `${request.method} ${context.pathname}`; if (context.search) { message = message + context.search; } message = `${message} HTTP/1.1\r\nHost: ${context.host}\r\n${_headersToString(request.headers)}`; if (request.body) { const contentLength = Buffer.byteLength(request.body, 'utf8'); message = `${message}Content-Length: ${contentLength}\r\n\r\n${request.body}\r\n\r\n`; } else { message = `${message}\r\n\r\n`; } return (message); } //# sourceMappingURL=index.js.map