From dabdfc56a509a32b3d7f56475ef5973ed83c15c5 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sun, 8 Sep 2019 12:25:09 -0400 Subject: [PATCH] More cleanup --- src/HapClient/eventedHttpClient/httpParser.ts | 51 ++++++++++--------- src/HapClient/eventedHttpClient/index.ts | 5 +- src/HapClient/hapMonitor.ts | 6 ++- src/HapClient/index.ts | 5 ++ src/HapClient/interfaces.ts | 8 +-- src/Util/Callbackify.ts | 7 +-- src/Util/Sleep.ts | 4 +- src/services/monitor.ts | 2 +- 8 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 src/HapClient/index.ts diff --git a/src/HapClient/eventedHttpClient/httpParser.ts b/src/HapClient/eventedHttpClient/httpParser.ts index 8ed59ac..eba85b4 100755 --- a/src/HapClient/eventedHttpClient/httpParser.ts +++ b/src/HapClient/eventedHttpClient/httpParser.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + /** * The contents in this file were taken from NorthernMan54/Hap-Node-Client * https://raw.githubusercontent.com/NorthernMan54/Hap-Node-Client/master/lib/httpParser.js @@ -16,26 +18,21 @@ interface IHttpResult { method?: string; url?: string; headers?: Dictionary; - //eslint-disable-next-line body?: any; - //eslint-disable-next-line boundary?: any; - //eslint-disable-next-line multipart?: any; - //eslint-disable-next-line additional?: any; - //eslint-disable-next-line meta?: any; } export default class HttpMessageParser { - private static requestLineRegex = /(HTTP|EVENT)\/(1\.0|1\.1|2\.0)\s+(\d+)\s+([\w\s-_]+)/i; - private static responseLineRegex = /(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD|TRACE|CONNECT)\s+(.*)\s+HTTP\/(1\.0|1\.1|2\.0)/i; - private static headerNewlineRegex = /^[\r\n]+/gim; - private static boundaryRegex = /(\n|\r\n)+--[\w-]+(\n|\r\n)+/g; + private requestLineRegex = /(HTTP|EVENT)\/(1\.0|1\.1|2\.0)\s+(\d+)\s+([\w\s-_]+)/i; + private responseLineRegex = /(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD|TRACE|CONNECT)\s+(.*)\s+HTTP\/(1\.0|1\.1|2\.0)/i; + private headerNewlineRegex = /^[\r\n]+/gim; + private boundaryRegex = /(\n|\r\n)+--[\w-]+(\n|\r\n)+/g; //eslint-disable-next-line - public static parse(message: any): IHttpResult { + public parse(message: any): IHttpResult { const result: IHttpResult = {}; let messageString = ''; @@ -119,7 +116,11 @@ export default class HttpMessageParser { /* Parse body */ let start = headerNewlineIndex; - const contentLength: number = result.headers!['Content-Length'] as number; + + let contentLength = 0; + if (result.headers) { + contentLength = result.headers['Content-Length'] as number; + } let end = (result.headers && result.headers['Content-Length'] && contentLength ? contentLength + start : messageString.length); @@ -163,7 +164,7 @@ export default class HttpMessageParser { const splitRegex = new RegExp('^' + fullBoundary + '.*[\n\r]?$', 'gm'); const parts = multipartBody.split(splitRegex); - result.multipart = parts.filter(this.isTruthy).map(function (part, i) { + result.multipart = parts.filter(this.isTruthy).map((part, index) => { // tslint:disable-next-line: no-shadowed-variable const result: IHttpResult = {}; @@ -188,7 +189,7 @@ export default class HttpMessageParser { let endOffset = null; if (newlineIndex > -1) { - const headers = HttpMessageParser.parseHeaders(possibleHeadersString); + const headers = this.parseHeaders(possibleHeadersString); if (Object.keys(headers).length > 0) { result.headers = headers; @@ -211,8 +212,8 @@ export default class HttpMessageParser { boundaryNewlineIndexes.push(headerNewlineIndex); }); - startOffset = boundaryNewlineIndexes[i]; - endOffset = boundaryIndexes[i + 1]; + startOffset = boundaryNewlineIndexes[index]; + endOffset = boundaryIndexes[index + 1]; body = message.slice(startOffset, endOffset); } else { body = part; @@ -233,7 +234,7 @@ export default class HttpMessageParser { } - private static trim(str: string): string { + private trim(str: string): string { const firstNonWhitespaceRegex = /[\w-]+/gim; const firstNonWhitespaceIndex = str.search(firstNonWhitespaceRegex); let tmpStr = ""; @@ -245,11 +246,11 @@ export default class HttpMessageParser { return str; } - private static isTruthy = function _isTruthy(value: any): boolean { + private isTruthy = function _isTruthy(value: any): boolean { return !!value; }; - private static isNumeric = function _isNumeric(value: any): boolean { + private isNumeric = function _isNumeric(value: any): boolean { if (typeof value === 'number' && !isNaN(value)) { return true; } @@ -263,35 +264,35 @@ export default class HttpMessageParser { return !isNaN(value); }; - private static isBuffer = (item: any): boolean => { - return ((HttpMessageParser.isNodeBufferSupported() && + private isBuffer = (item: any): boolean => { + return ((this.isNodeBufferSupported() && typeof global === 'object' && global.Buffer.isBuffer(item)) || (item instanceof Object && item._isBuffer)); }; - private static isNodeBufferSupported = (): boolean => { + private isNodeBufferSupported = (): boolean => { return (typeof global === 'object' && typeof global.Buffer === 'function' && typeof global.Buffer.isBuffer === 'function'); }; - private static parseHeaders = (body: any): Dictionary => { + private parseHeaders = (body: any): Dictionary => { const headers: Dictionary = {}; if (typeof body !== 'string') { return headers; } - body.split(/[\r\n]/).forEach(function (string) { - const match = string.match(/([\w-]+):\s*(.*)/i); + body.split(/[\r\n]/).forEach((str: string) => { + const match = str.match(/([\w-]+):\s*(.*)/i); if (Array.isArray(match) && match.length === 3) { const key = match[1]; const value = match[2]; - headers[key] = HttpMessageParser.isNumeric(value) ? Number(value) : value; + headers[key] = this.isNumeric(value) ? Number(value) : value; } }); diff --git a/src/HapClient/eventedHttpClient/index.ts b/src/HapClient/eventedHttpClient/index.ts index ea4058f..feba019 100755 --- a/src/HapClient/eventedHttpClient/index.ts +++ b/src/HapClient/eventedHttpClient/index.ts @@ -5,8 +5,8 @@ import * as net from 'net'; import * as url from 'url'; -import HttpMessageParser from './httpParser'; import { Dictionary } from '../../Types/types'; +import HttpMessageParser from './httpParser'; interface IRequest { method: 'PUT' | 'POST' | 'GET' | 'DELETE' | 'PATCH'; @@ -16,7 +16,6 @@ interface IRequest { body: string; } -export const parseMessage = HttpMessageParser.parse; function _headersToString(headers: Dictionary): string { let response = ''; @@ -71,3 +70,5 @@ export function createConnection(instance: { ipAddress: string; port: number }, return client; } + +export { HttpMessageParser }; diff --git a/src/HapClient/hapMonitor.ts b/src/HapClient/hapMonitor.ts index 0b56a95..d31971b 100755 --- a/src/HapClient/hapMonitor.ts +++ b/src/HapClient/hapMonitor.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; import { IServiceType, IHapEvInstance } from './interfaces'; -import { createConnection, parseMessage } from './eventedHttpClient'; +import { createConnection, HttpMessageParser } from './eventedHttpClient'; import { CharacteristicType } from './hapClient'; import { log } from '../Types/types'; @@ -10,6 +10,7 @@ export class HapMonitor extends EventEmitter { private services: IServiceType[]; private logger: log; private debug: log; + private parser: HttpMessageParser constructor(logger: log, debug: log, pin: string, services: IServiceType[]) { super(); @@ -18,6 +19,7 @@ export class HapMonitor extends EventEmitter { this.pin = pin; this.services = services; this.evInstances = []; + this.parser = new HttpMessageParser(); // get a list of characteristics we can watch for each instance this.parseServices(); @@ -36,7 +38,7 @@ export class HapMonitor extends EventEmitter { this.debug(`[HapClient] [${instance.ipAddress}:${instance.port} (${instance.username})] Connected`); instance.socket.on('data', (data) => { - const message = parseMessage(data); + const message = this.parser.parse(data); if (message.statusCode === 401) { diff --git a/src/HapClient/index.ts b/src/HapClient/index.ts new file mode 100644 index 0000000..d6e287a --- /dev/null +++ b/src/HapClient/index.ts @@ -0,0 +1,5 @@ +import { HapClient } from './hapClient'; +import { HapMonitor } from './hapMonitor'; + +export { HapClient }; +export { HapMonitor }; diff --git a/src/HapClient/interfaces.ts b/src/HapClient/interfaces.ts index 8e6e5ff..622deaa 100755 --- a/src/HapClient/interfaces.ts +++ b/src/HapClient/interfaces.ts @@ -1,7 +1,7 @@ import { Socket } from 'net'; export interface IHapInstance { - displayName: string + displayName: string; name: string; ipAddress: string; port: number; @@ -13,7 +13,7 @@ export interface IHapEvInstance { ipAddress: string; port: number; username: string; - evCharacteristics?: { aid: number, iid: number, ev: boolean }[]; + evCharacteristics?: { aid: number; iid: number; ev: boolean }[]; socket?: Socket; } @@ -47,7 +47,7 @@ export interface IServiceResp { } export interface IAccessoryResp { - instance: IInstanceResp + instance: IInstanceResp; aid: number; services: Array; } @@ -121,4 +121,4 @@ export const createDefaultCharacteristicType = (): ICharacteristicType => { canWrite: false, ev: false, }; -} +}; diff --git a/src/Util/Callbackify.ts b/src/Util/Callbackify.ts index 4bf521f..b3e73d6 100644 --- a/src/Util/Callbackify.ts +++ b/src/Util/Callbackify.ts @@ -1,10 +1,11 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Helper function to convert callbacks into promises * @param func */ export default function callbackify(func: (...args: any[]) => Promise): Function { - return (...args: any[]) => { + return (...args: any[]): void => { const onlyArgs: any[] = []; let maybeCallback: Function | null = null; @@ -25,6 +26,6 @@ export default function callbackify(func: (...args: any[]) => Promise): Fun func(...onlyArgs) .then((data: any) => callback(null, data)) - .catch((err: any) => callback(err)) - } + .catch((err: any) => callback(err)); + }; } \ No newline at end of file diff --git a/src/Util/Sleep.ts b/src/Util/Sleep.ts index 9721cc4..c864756 100644 --- a/src/Util/Sleep.ts +++ b/src/Util/Sleep.ts @@ -1,3 +1,3 @@ -export function sleep(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); +export function sleep(ms: number): Promise { + return new Promise((resolve): number => setTimeout(resolve, ms)); } \ No newline at end of file diff --git a/src/services/monitor.ts b/src/services/monitor.ts index 19a3667..fc862f6 100644 --- a/src/services/monitor.ts +++ b/src/services/monitor.ts @@ -1,4 +1,4 @@ -import { HapClient } from "../HapClient/hapClient"; +import { HapClient } from "../HapClient"; import { log } from '../Types/types'; import { singleton } from 'tsyringe';