From b2c28d4162e8d9debc6e8979b52db84a27fa0005 Mon Sep 17 00:00:00 2001 From: Jason Hyde Date: Tue, 3 Sep 2024 07:14:41 +0200 Subject: [PATCH] [ts] add url to request options and fix date time request typings --- source/client/http.ts | 5 +++-- source/discovery.ts | 1 + source/onvif.ts | 10 +++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/source/client/http.ts b/source/client/http.ts index 3d19426..61e2489 100644 --- a/source/client/http.ts +++ b/source/client/http.ts @@ -13,6 +13,7 @@ export type HttpClientOptions = { export type RequestOptions = { body: string + url?: URL service?: string raw?: boolean } @@ -31,8 +32,8 @@ export class HttpClient { } public async request(options: RequestOptions): Promise<[T, string]> { - const { body, service } = options - const url = this.createUrl(service) + const { body, service, url: requestUrl } = options + const url = requestUrl ?? this.createUrl(service) const requestOptions: RequestInit = { method: 'POST', diff --git a/source/discovery.ts b/source/discovery.ts index 6cd76b9..4f5f47a 100644 --- a/source/discovery.ts +++ b/source/discovery.ts @@ -128,6 +128,7 @@ export class DiscoverySingleton extends EventEmitter { const listener = async (msg: Buffer, rinfo: RemoteInfo): Promise => { try { const [data, xml] = await parseSOAPString(msg.toString()) + // @ts-expect-error TODO fix later if (!data['probeMatches']) { throw new Error(`Wrong SOAP message from ${rinfo.address}:${rinfo.port}\n${xml}`) } diff --git a/source/onvif.ts b/source/onvif.ts index cd6a025..eb4a01d 100644 --- a/source/onvif.ts +++ b/source/onvif.ts @@ -252,7 +252,7 @@ export class Onvif extends EventEmitter { } private setupSystemDateAndTime(data: GetSystemDateAndTimeResponse): Date { - const systemDateAndTime = data?.getSystemDateAndTimeResponse?.systemDateAndTime + const systemDateAndTime = data?.systemDateAndTime if (!systemDateAndTime) { throw new Error('Invalid data structure: systemDateAndTime not found') } @@ -332,7 +332,7 @@ export class Onvif extends EventEmitter { // authenticated getSystemDateAndTime. So for these devices we need to do an authenticated getSystemDateAndTime. // As 'timeShift' is not set, the local clock MUST be set to the correct time AND the NVT/Camera MUST be set // to the correct time if the camera implements Replay Attack Protection (e.g. Axis) - const [data, xml] = await this.request({ + const [data, xml] = await this.request<{ getSystemDateAndTimeResponse: GetSystemDateAndTimeResponse }>({ // Try the Unauthenticated Request first. Do not use this._envelopeHeader() as we don't have timeShift yet. raw: true, body: @@ -343,14 +343,14 @@ export class Onvif extends EventEmitter { '' }) try { - return this.setupSystemDateAndTime(data) + return this.setupSystemDateAndTime(data.getSystemDateAndTimeResponse) } catch (error) { if (xml?.toLowerCase().includes('sender not authorized')) { // Try again with a Username and Password - const [data] = await this.request({ + const [data] = await this.request<{ getSystemDateAndTimeResponse: GetSystemDateAndTimeResponse }>({ body: '}' }) - return this.setupSystemDateAndTime(data) + return this.setupSystemDateAndTime(data.getSystemDateAndTimeResponse) } throw error }