camstreamerlib 3.5.2 → 4.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CamOverlayAPI.d.ts +11 -28
  2. package/CamOverlayAPI.js +116 -138
  3. package/CamOverlayDrawingAPI.js +26 -18
  4. package/CamOverlayPainter/Frame.js +167 -182
  5. package/CamOverlayPainter/Painter.js +80 -101
  6. package/CamOverlayPainter/ResourceManager.js +31 -46
  7. package/CamScripterAPI.d.ts +19 -0
  8. package/CamScripterAPI.js +66 -0
  9. package/CamScripterAPICameraEventsGenerator.js +22 -16
  10. package/CamStreamerAPI.d.ts +5 -27
  11. package/CamStreamerAPI.js +45 -71
  12. package/CamSwitcherAPI.d.ts +38 -71
  13. package/CamSwitcherAPI.js +329 -91
  14. package/CamSwitcherEvents.d.ts +15 -33
  15. package/CamSwitcherEvents.js +53 -97
  16. package/CreatePackage.js +5 -7
  17. package/README.md +3 -1
  18. package/VapixAPI.d.ts +66 -0
  19. package/VapixAPI.js +454 -0
  20. package/VapixEvents.js +18 -16
  21. package/errors/errors.d.ts +34 -0
  22. package/errors/errors.js +66 -0
  23. package/events/AxisCameraStationEvents.js +29 -42
  24. package/events/GenetecAgent.d.ts +14 -15
  25. package/events/GenetecAgent.js +81 -100
  26. package/internal/Digest.js +5 -11
  27. package/internal/ProxyClient.d.ts +11 -0
  28. package/internal/ProxyClient.js +40 -0
  29. package/internal/common.d.ts +19 -4
  30. package/internal/common.js +11 -26
  31. package/internal/constants.d.ts +1 -0
  32. package/internal/constants.js +1 -0
  33. package/internal/transformers.d.ts +5 -0
  34. package/internal/transformers.js +25 -0
  35. package/internal/utils.d.ts +11 -0
  36. package/internal/utils.js +34 -0
  37. package/internal/versionCompare.d.ts +6 -0
  38. package/internal/versionCompare.js +44 -0
  39. package/node/DefaultClient.d.ts +15 -0
  40. package/node/DefaultClient.js +50 -0
  41. package/{internal → node}/HttpRequestSender.d.ts +2 -2
  42. package/node/HttpRequestSender.js +85 -0
  43. package/{HttpServer.d.ts → node/HttpServer.d.ts} +1 -1
  44. package/{HttpServer.js → node/HttpServer.js} +22 -24
  45. package/{internal → node}/WsClient.d.ts +1 -1
  46. package/{internal → node}/WsClient.js +32 -39
  47. package/node/WsEventClient.d.ts +13 -0
  48. package/node/WsEventClient.js +18 -0
  49. package/package.json +7 -3
  50. package/types/CamOverlayAPI.d.ts +188 -0
  51. package/types/CamOverlayAPI.js +44 -0
  52. package/types/CamScripterAPI.d.ts +67 -0
  53. package/types/CamScripterAPI.js +17 -0
  54. package/types/CamStreamerAPI.d.ts +139 -0
  55. package/types/CamStreamerAPI.js +25 -0
  56. package/types/CamSwitcherAPI.d.ts +814 -0
  57. package/types/CamSwitcherAPI.js +134 -0
  58. package/types/CamswitcherEvents.d.ts +491 -0
  59. package/types/CamswitcherEvents.js +59 -0
  60. package/types/VapixAPI.d.ts +1704 -0
  61. package/types/VapixAPI.js +129 -0
  62. package/types/common.d.ts +37 -0
  63. package/types/common.js +11 -0
  64. package/web/DefaultClient.d.ts +6 -0
  65. package/web/DefaultClient.js +16 -0
  66. package/web/WsClient.d.ts +13 -0
  67. package/web/WsClient.js +58 -0
  68. package/CameraVapix.d.ts +0 -98
  69. package/CameraVapix.js +0 -441
  70. package/DefaultAgent.d.ts +0 -15
  71. package/DefaultAgent.js +0 -68
  72. package/internal/HttpRequestSender.js +0 -117
@@ -0,0 +1,5 @@
1
+ import type { CamelCasedProperties, CamelCasedPropertiesDeep, SnakeCasedProperties, SnakeCasedPropertiesDeep } from 'type-fest';
2
+ export declare const toCamelCase: <T extends object>(o: T) => CamelCasedProperties<T>;
3
+ export declare const toCamelCaseDeep: <T extends object>(o: T) => CamelCasedPropertiesDeep<T>;
4
+ export declare const toSnakeCase: <T extends object>(o: T) => SnakeCasedProperties<T>;
5
+ export declare const toSnakeCaseDeep: <T extends object>(o: T) => SnakeCasedPropertiesDeep<T>;
@@ -0,0 +1,25 @@
1
+ import { camelCase, snakeCase, isPlainObject, mapKeys, mapValues } from 'lodash';
2
+ export const toCamelCase = (o) => mapKeys(o, camelCaseKey);
3
+ export const toCamelCaseDeep = (o) => {
4
+ return mapKeysDeep(o, camelCaseKey);
5
+ };
6
+ export const toSnakeCase = (o) => mapKeys(o, snakeCaseKey);
7
+ export const toSnakeCaseDeep = (o) => {
8
+ return mapKeysDeep(o, snakeCaseKey);
9
+ };
10
+ const camelCaseKey = (_, key) => camelCase(key);
11
+ const snakeCaseKey = (_, key) => snakeCase(key);
12
+ const mapKeysDeep = (obj, cb) => {
13
+ if (Array.isArray(obj)) {
14
+ return obj.map((item) => {
15
+ return mapKeysDeep(item, cb);
16
+ });
17
+ }
18
+ if (typeof obj !== 'object' || isPlainObject(obj) === false) {
19
+ return obj;
20
+ }
21
+ const result = mapKeys(obj, cb);
22
+ return mapValues(result, (value) => {
23
+ return mapKeysDeep(value, cb);
24
+ });
25
+ };
@@ -0,0 +1,11 @@
1
+ import { TPlaylistPlayType } from '../types/CamSwitcherAPI';
2
+ import { TParameters } from './common';
3
+ export declare const addParametersToPath: (path: string, params?: TParameters) => string;
4
+ export declare const paramToUrl: (params?: TParameters) => string;
5
+ export declare const arrayToUrl: (arr: string | string[]) => string;
6
+ export declare const isCamera: (id?: string) => boolean;
7
+ export declare const isStream: (id?: string) => boolean;
8
+ export declare const isClip: (id?: string) => boolean;
9
+ export declare const isTracker: (id?: string) => boolean;
10
+ export declare const isPlaylist: (id?: string) => boolean;
11
+ export declare const isLoopPlayType: (playType: TPlaylistPlayType) => boolean;
@@ -0,0 +1,34 @@
1
+ import { isNullish } from './common';
2
+ export const addParametersToPath = (path, params) => {
3
+ if (params === undefined || Object.keys(params).length === 0) {
4
+ return path;
5
+ }
6
+ const joinChar = path.indexOf('?') === -1 ? '?' : '&';
7
+ return `${path}${joinChar}${paramToUrl(params)}`;
8
+ };
9
+ export const paramToUrl = (params) => {
10
+ if (params === undefined) {
11
+ return '';
12
+ }
13
+ let output = '';
14
+ for (const key in params) {
15
+ const value = params[key];
16
+ if (isNullish(value)) {
17
+ continue;
18
+ }
19
+ output += `${encodeURIComponent(key)}=${encodeURIComponent(value)}&`;
20
+ }
21
+ return output.slice(0, output.length - 1);
22
+ };
23
+ export const arrayToUrl = (arr) => {
24
+ if (Array.isArray(arr)) {
25
+ return arr.join(',');
26
+ }
27
+ return arr;
28
+ };
29
+ export const isCamera = (id) => id?.charAt(0) === 'c';
30
+ export const isStream = (id) => id?.charAt(0) === 'c' || id?.charAt(0) === 'a';
31
+ export const isClip = (id) => id?.charAt(0) === 's';
32
+ export const isTracker = (id) => id?.charAt(0) === 't';
33
+ export const isPlaylist = (id) => id?.charAt(0) === 'p';
34
+ export const isLoopPlayType = (playType) => playType.includes('LOOP');
@@ -0,0 +1,6 @@
1
+ export declare const assertVersionString: (s: string, msg?: string) => void;
2
+ export declare const isFirmwareVersionAtLeast: (version: string, compareVersion: string) => boolean;
3
+ export declare const isVersionAtLeast: (version: string, compareVersion: string) => boolean;
4
+ export declare const firmwareVersionCompare: (a: string, b: string) => 1 | -1 | 0;
5
+ export declare const versionCompare: (a: string, b: string) => 1 | -1 | 0;
6
+ export declare const fixVersionToDots: (version: string) => string;
@@ -0,0 +1,44 @@
1
+ export const assertVersionString = (s, msg) => {
2
+ if (!s.match(/^[0-9]+(\.[0-9]+){1,3}$/)) {
3
+ throw new Error(msg ?? `${s} is not a version`);
4
+ }
5
+ };
6
+ export const isFirmwareVersionAtLeast = (version, compareVersion) => {
7
+ return firmwareVersionCompare(version, compareVersion) >= 0;
8
+ };
9
+ export const isVersionAtLeast = (version, compareVersion) => {
10
+ return versionCompare(version, compareVersion) >= 0;
11
+ };
12
+ export const firmwareVersionCompare = (a, b) => {
13
+ const versions = [a, b];
14
+ const matchBetaFirmwareVersion = (x) => /^CVP-/.test(x) || /^[0-9]+.*beta/.test(x);
15
+ if (versions.every(matchBetaFirmwareVersion)) {
16
+ return 0;
17
+ }
18
+ if (matchBetaFirmwareVersion(a)) {
19
+ return -1;
20
+ }
21
+ if (matchBetaFirmwareVersion(b)) {
22
+ return 1;
23
+ }
24
+ return versionCompare(a, b);
25
+ };
26
+ export const versionCompare = (a, b) => {
27
+ assertVersionString(a);
28
+ assertVersionString(b);
29
+ const aSplit = parseVersion(a);
30
+ const bSplit = parseVersion(b);
31
+ for (let i = 0; i < aSplit.length; i++) {
32
+ if (aSplit[i] !== bSplit[i]) {
33
+ return aSplit[i] < bSplit[i] ? -1 : 1;
34
+ }
35
+ }
36
+ return 0;
37
+ };
38
+ export const fixVersionToDots = (version) => version.replaceAll('-', '.');
39
+ const parseVersion = (version) => {
40
+ assertVersionString(version);
41
+ const parsed = version.split('.').map((s) => parseInt(s));
42
+ parsed.push(...Array(4 - parsed.length).fill(0));
43
+ return parsed;
44
+ };
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { IClient, HttpOptions, TParameters } from '../internal/common';
3
+ export declare class DefaultClient implements IClient {
4
+ private tls;
5
+ private ip;
6
+ private port;
7
+ private user;
8
+ private pass;
9
+ private httpRequestSender;
10
+ constructor(opt?: HttpOptions);
11
+ get url(): string;
12
+ get(path: string, parameters?: TParameters, headers?: Record<string, string>): Promise<import("undici").Response>;
13
+ post(path: string, data: string | FormData | Buffer, parameters?: TParameters, headers?: Record<string, string>): Promise<import("undici").Response>;
14
+ private getBaseConnectionParams;
15
+ }
@@ -0,0 +1,50 @@
1
+ import { addParametersToPath } from '../internal/utils';
2
+ import { HttpRequestSender } from './HttpRequestSender';
3
+ export class DefaultClient {
4
+ tls;
5
+ ip;
6
+ port;
7
+ user;
8
+ pass;
9
+ httpRequestSender;
10
+ constructor(opt = {}) {
11
+ this.tls = opt.tls ?? false;
12
+ this.ip = opt.ip ?? '127.0.0.1';
13
+ this.port = opt.port ?? (this.tls ? 443 : 80);
14
+ this.user = opt.user ?? '';
15
+ this.pass = opt.pass ?? '';
16
+ let agentOptions;
17
+ if (opt.tlsInsecure !== undefined || opt.keepAlive !== undefined) {
18
+ agentOptions = {
19
+ rejectUnaurhorized: !opt.tlsInsecure,
20
+ keepAlive: opt.keepAlive,
21
+ };
22
+ }
23
+ this.httpRequestSender = new HttpRequestSender(agentOptions);
24
+ }
25
+ get url() {
26
+ return `${this.tls ? 'https' : 'http'}://${this.user}:${this.pass}@${this.ip}:${this.port}`;
27
+ }
28
+ async get(path, parameters = {}, headers) {
29
+ const options = this.getBaseConnectionParams('GET', path, parameters);
30
+ options.headers = headers;
31
+ return this.httpRequestSender.sendRequest(options);
32
+ }
33
+ async post(path, data, parameters = {}, headers) {
34
+ const options = this.getBaseConnectionParams('POST', path, parameters);
35
+ options.headers = headers;
36
+ return this.httpRequestSender.sendRequest(options, data);
37
+ }
38
+ getBaseConnectionParams(method, path, params) {
39
+ let pathName = addParametersToPath(path, params);
40
+ return {
41
+ method: method,
42
+ protocol: this.tls ? 'https:' : 'http:',
43
+ host: this.ip,
44
+ port: this.port,
45
+ path: pathName,
46
+ user: this.user,
47
+ pass: this.pass,
48
+ };
49
+ }
50
+ }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
2
+ import { FormData as UndiciFormData, Response as UndiciResponse } from 'undici';
3
3
  export type HttpRequestOptions = {
4
4
  method?: string;
5
5
  protocol: string;
@@ -19,7 +19,7 @@ export declare class HttpRequestSender {
19
19
  private agent?;
20
20
  private authData?;
21
21
  constructor(agentOptions?: AgentOptions);
22
- sendRequest(options: HttpRequestOptions, postData?: Buffer | string | FormData): Promise<Response>;
22
+ sendRequest(options: HttpRequestOptions, postData?: Buffer | string | UndiciFormData): Promise<UndiciResponse>;
23
23
  private sendRequestWithAuth;
24
24
  private static getURL;
25
25
  private getAuthorization;
@@ -0,0 +1,85 @@
1
+ import { Digest } from '../internal/Digest';
2
+ import { Agent, fetch as undiciFetch, Request as UndiciRequest, } from 'undici';
3
+ export class HttpRequestSender {
4
+ agent;
5
+ authData;
6
+ constructor(agentOptions) {
7
+ this.agent = new Agent({
8
+ connect: { rejectUnauthorized: agentOptions?.rejectUnaurhorized, keepAlive: agentOptions?.keepAlive },
9
+ });
10
+ }
11
+ sendRequest(options, postData) {
12
+ return this.sendRequestWithAuth(options, postData);
13
+ }
14
+ async sendRequestWithAuth(options, postData, wwwAuthenticateHeader) {
15
+ options.timeout ??= 10000;
16
+ const url = HttpRequestSender.getURL(options);
17
+ const authorization = this.getAuthorization(options, wwwAuthenticateHeader);
18
+ if (authorization !== undefined) {
19
+ options.headers ??= {};
20
+ options.headers['Authorization'] = authorization;
21
+ }
22
+ const req = new UndiciRequest(url, { body: postData, method: options.method, headers: options.headers });
23
+ const res = await undiciFetch(req, { signal: AbortSignal.timeout(options.timeout), dispatcher: this.agent });
24
+ if (!res.ok) {
25
+ this.invalidateAuthorization();
26
+ }
27
+ if (res.status === 401) {
28
+ const authenticateHeader = res.headers.get('www-authenticate');
29
+ if (authenticateHeader !== null &&
30
+ authenticateHeader.indexOf('Digest') !== -1 &&
31
+ wwwAuthenticateHeader === undefined) {
32
+ return this.sendRequestWithAuth(options, postData, authenticateHeader);
33
+ }
34
+ else {
35
+ return res;
36
+ }
37
+ }
38
+ else {
39
+ return res;
40
+ }
41
+ }
42
+ static getURL(options) {
43
+ const url = new URL(`${options.protocol}//${options.host}:${options.port}${options.path}`);
44
+ return url.toString();
45
+ }
46
+ getAuthorization(options, wwwAuthenticateHeader) {
47
+ if (options.user === undefined || options.pass === undefined) {
48
+ this.authData = undefined;
49
+ return;
50
+ }
51
+ if (this.authData &&
52
+ (this.authData.host !== options.host ||
53
+ this.authData.port !== options.port ||
54
+ this.authData.user !== options.user ||
55
+ this.authData.pass !== options.pass ||
56
+ (wwwAuthenticateHeader !== undefined && this.authData.wwwAuthenticateHeader !== wwwAuthenticateHeader))) {
57
+ this.authData = undefined;
58
+ }
59
+ if (this.authData === undefined) {
60
+ this.authData = {
61
+ host: options.host,
62
+ port: options.port,
63
+ user: options.user,
64
+ pass: options.pass,
65
+ wwwAuthenticateHeader,
66
+ digest: new Digest(),
67
+ };
68
+ }
69
+ return HttpRequestSender.getAuthHeader(options, this.authData);
70
+ }
71
+ invalidateAuthorization() {
72
+ this.authData = undefined;
73
+ }
74
+ static getAuthHeader(options, authData) {
75
+ if (options.user === undefined || options.pass === undefined) {
76
+ throw new Error('No credentials found');
77
+ }
78
+ if (authData.wwwAuthenticateHeader !== undefined && authData.wwwAuthenticateHeader.indexOf('Digest') !== -1) {
79
+ return authData.digest.getAuthHeader(options.user, options.pass, options.method ?? 'GET', options.path, authData.wwwAuthenticateHeader);
80
+ }
81
+ else {
82
+ return `Basic ${btoa(options.user + ':' + options.pass)}`;
83
+ }
84
+ }
85
+ }
@@ -15,7 +15,7 @@ export declare class HttpServer extends EventEmitter {
15
15
  private sockets;
16
16
  constructor(options?: HttpServerOptions);
17
17
  getServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
18
- onRequest(path: string, callback: TOnRequestCallback): void;
18
+ onRequest(pathName: string, callback: TOnRequestCallback): void;
19
19
  close(): void;
20
20
  }
21
21
  export {};
@@ -1,23 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpServer = void 0;
4
- const http = require("http");
5
- const url = require("url");
6
- const fs = require("fs");
7
- const path = require("path");
8
- const EventEmitter = require("events");
9
- class HttpServer extends EventEmitter {
1
+ import * as http from 'http';
2
+ import * as url from 'url';
3
+ import * as fs from 'fs';
4
+ import * as path from 'path';
5
+ import * as EventEmitter from 'events';
6
+ export class HttpServer extends EventEmitter {
7
+ host;
8
+ port;
9
+ registeredPaths;
10
+ server;
11
+ sockets;
10
12
  constructor(options) {
11
- var _a, _b, _c, _d;
12
13
  super();
13
- this.host = (_b = (_a = options === null || options === void 0 ? void 0 : options.host) !== null && _a !== void 0 ? _a : process.env.HTTP_HOST) !== null && _b !== void 0 ? _b : '0.0.0.0';
14
- this.port = (_c = options === null || options === void 0 ? void 0 : options.port) !== null && _c !== void 0 ? _c : parseInt((_d = process.env.HTTP_PORT) !== null && _d !== void 0 ? _d : '80');
14
+ this.host = options?.host ?? process.env.HTTP_HOST ?? '0.0.0.0';
15
+ this.port = options?.port ?? parseInt(process.env.HTTP_PORT ?? '80');
15
16
  this.registeredPaths = new Map();
16
17
  this.server = http.createServer((req, res) => {
17
- var _a, _b;
18
18
  this.emit('access', req.method + ' ' + req.url);
19
- const parsedUrl = url.parse((_a = req.url) !== null && _a !== void 0 ? _a : '');
20
- (_b = parsedUrl.pathname) !== null && _b !== void 0 ? _b : (parsedUrl.pathname = '');
19
+ const parsedUrl = url.parse(req.url ?? '');
20
+ parsedUrl.pathname ??= '';
21
21
  const requestCallback = this.registeredPaths.get(parsedUrl.pathname);
22
22
  if (requestCallback) {
23
23
  requestCallback(req, res);
@@ -49,15 +49,14 @@ class HttpServer extends EventEmitter {
49
49
  if (fs.statSync(pathname).isDirectory()) {
50
50
  pathname += `/index${ext}`;
51
51
  }
52
- fs.readFile(pathname, (err, data) => {
53
- var _a;
54
- if (err) {
52
+ fs.readFile(pathname, (error, data) => {
53
+ if (error) {
55
54
  res.statusCode = 500;
56
- res.end(`Error getting the file: ${err}`);
57
- this.emit('error', `Error getting the file: ${err}`);
55
+ res.end(`Error getting the file: ${error}`);
56
+ this.emit('error', `Error getting the file: ${error}`);
58
57
  }
59
58
  else {
60
- res.setHeader('Content-type', (_a = map[ext]) !== null && _a !== void 0 ? _a : 'text/plain');
59
+ res.setHeader('Content-type', map[ext] ?? 'text/plain');
61
60
  res.setHeader('Access-Control-Allow-Origin', '*');
62
61
  res.end(data);
63
62
  }
@@ -81,8 +80,8 @@ class HttpServer extends EventEmitter {
81
80
  getServer() {
82
81
  return this.server;
83
82
  }
84
- onRequest(path, callback) {
85
- this.registeredPaths.set(path, callback);
83
+ onRequest(pathName, callback) {
84
+ this.registeredPaths.set(pathName, callback);
86
85
  }
87
86
  close() {
88
87
  this.server.close();
@@ -91,4 +90,3 @@ class HttpServer extends EventEmitter {
91
90
  }
92
91
  }
93
92
  }
94
- exports.HttpServer = HttpServer;
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import * as EventEmitter from 'events';
4
- import { WsOptions } from './common';
4
+ import { WsOptions } from '../internal/common';
5
5
  export type WsClientOptions = WsOptions & {
6
6
  address: string;
7
7
  headers?: Record<string, string>;
@@ -1,39 +1,35 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.WsClient = void 0;
13
- const EventEmitter = require("events");
14
- const WebSocket = require("ws");
15
- const Digest_1 = require("./Digest");
16
- class WsClient extends EventEmitter {
1
+ import * as EventEmitter from 'events';
2
+ import * as WebSocket from 'ws';
3
+ import { Digest } from '../internal/Digest';
4
+ export class WsClient extends EventEmitter {
5
+ user;
6
+ pass;
7
+ address;
8
+ protocol;
9
+ pingInterval;
10
+ wsOptions;
11
+ digestAddress;
12
+ isAlive = true;
13
+ pingTimer;
14
+ ws;
15
+ isClosed = false;
17
16
  constructor(options) {
18
- var _a, _b, _c, _d, _e, _f, _g, _h;
19
17
  super();
20
- this.isAlive = true;
21
- this.isClosed = false;
22
- const tls = (_a = options === null || options === void 0 ? void 0 : options.tls) !== null && _a !== void 0 ? _a : false;
23
- const tlsInsecure = (_b = options === null || options === void 0 ? void 0 : options.tlsInsecure) !== null && _b !== void 0 ? _b : false;
24
- const ip = (_c = options === null || options === void 0 ? void 0 : options.ip) !== null && _c !== void 0 ? _c : '127.0.0.1';
25
- const port = (_d = options === null || options === void 0 ? void 0 : options.port) !== null && _d !== void 0 ? _d : (tls ? 443 : 80);
26
- this.user = (_e = options === null || options === void 0 ? void 0 : options.user) !== null && _e !== void 0 ? _e : '';
27
- this.pass = (_f = options === null || options === void 0 ? void 0 : options.pass) !== null && _f !== void 0 ? _f : '';
18
+ const tls = options.tls ?? false;
19
+ const tlsInsecure = options.tlsInsecure ?? false;
20
+ const ip = options.ip ?? '127.0.0.1';
21
+ const port = options.port ?? (tls ? 443 : 80);
22
+ this.user = options.user ?? '';
23
+ this.pass = options.pass ?? '';
28
24
  const protocol = tls ? 'wss' : 'ws';
29
25
  this.address = `${protocol}://${ip}:${port}${options.address}`;
30
26
  this.digestAddress = options.address;
31
- this.pingInterval = (_g = options.pingInterval) !== null && _g !== void 0 ? _g : 30000;
27
+ this.pingInterval = options.pingInterval ?? 30000;
32
28
  this.protocol = options.protocol;
33
29
  this.wsOptions = {
34
30
  auth: `${this.user}:${this.pass}`,
35
31
  rejectUnauthorized: !tlsInsecure,
36
- headers: (_h = options.headers) !== null && _h !== void 0 ? _h : {},
32
+ headers: options.headers ?? {},
37
33
  };
38
34
  }
39
35
  open(wwwAuthenticateHeader) {
@@ -50,38 +46,36 @@ class WsClient extends EventEmitter {
50
46
  }
51
47
  this.ws.binaryType = 'arraybuffer';
52
48
  this.isAlive = true;
53
- this.pingTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
54
- var _a;
49
+ this.pingTimer = setInterval(async () => {
55
50
  if ((this.ws && this.ws.readyState !== WebSocket.OPEN) || this.isAlive === false) {
56
51
  this.emit('error', new Error('Connection timeout'));
57
- yield this.closeWsConnection();
52
+ await this.closeWsConnection();
58
53
  }
59
54
  else {
60
55
  this.isAlive = false;
61
- (_a = this.ws) === null || _a === void 0 ? void 0 : _a.ping();
56
+ this.ws?.ping();
62
57
  }
63
- }), this.pingInterval);
58
+ }, this.pingInterval);
64
59
  this.ws.on('pong', () => {
65
60
  this.isAlive = true;
66
61
  });
67
62
  if (wwwAuthenticateHeader !== undefined) {
68
- this.wsOptions.headers['Authorization'] = new Digest_1.Digest().getAuthHeader(this.user, this.pass, 'GET', this.digestAddress, wwwAuthenticateHeader);
63
+ this.wsOptions.headers['Authorization'] = new Digest().getAuthHeader(this.user, this.pass, 'GET', this.digestAddress, wwwAuthenticateHeader);
69
64
  }
70
- this.ws.on('unexpected-response', (req, res) => __awaiter(this, void 0, void 0, function* () {
71
- var _b;
65
+ this.ws.on('unexpected-response', (req, res) => {
72
66
  if (res.statusCode === 401 && res.headers['www-authenticate'] !== undefined) {
73
67
  if (this.pingTimer) {
74
68
  clearInterval(this.pingTimer);
75
69
  }
76
- (_b = this.ws) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
70
+ this.ws?.removeAllListeners();
77
71
  this.ws = undefined;
78
72
  this.open(res.headers['www-authenticate']);
79
73
  }
80
74
  else {
81
75
  this.emit('error', new Error('Status code: ' + res.statusCode));
82
- yield this.closeWsConnection();
76
+ this.closeWsConnection();
83
77
  }
84
- }));
78
+ });
85
79
  this.ws.on('open', () => this.emit('open'));
86
80
  this.ws.on('message', (data) => this.emit('message', data));
87
81
  this.ws.on('error', (error) => {
@@ -149,4 +143,3 @@ class WsClient extends EventEmitter {
149
143
  }
150
144
  }
151
145
  }
152
- exports.WsClient = WsClient;
@@ -0,0 +1,13 @@
1
+ import { IWebsocket } from '../internal/common';
2
+ import { WsClientOptions } from './WsClient';
3
+ type TEvent = {
4
+ data: string;
5
+ };
6
+ export declare class WsEventClient implements IWebsocket<TEvent> {
7
+ private wsClient;
8
+ constructor(options: WsClientOptions);
9
+ send: (data: string) => void;
10
+ destroy: () => void;
11
+ onmessage: ((event: TEvent) => void) | null;
12
+ }
13
+ export {};
@@ -0,0 +1,18 @@
1
+ import { WsClient } from './WsClient';
2
+ export class WsEventClient {
3
+ wsClient;
4
+ constructor(options) {
5
+ this.wsClient = new WsClient(options);
6
+ this.wsClient.on('message', (data) => {
7
+ this.onmessage?.({ data: data.toString() });
8
+ });
9
+ }
10
+ send = (data) => {
11
+ this.wsClient.send(data);
12
+ };
13
+ destroy = () => {
14
+ this.wsClient.close();
15
+ this.onmessage = null;
16
+ };
17
+ onmessage = null;
18
+ }
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "camstreamerlib",
3
- "version": "3.5.2",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "Helper library for CamStreamer ACAP applications.",
5
5
  "prettier": "@camstreamer/prettier-config",
6
+ "type": "module",
6
7
  "dependencies": {
7
8
  "adm-zip": "^0.5.9",
8
9
  "eventemitter2": "^5.0.1",
10
+ "lodash": "^4.17.21",
9
11
  "prettify-xml": "^1.2.0",
10
- "undici": "6.13.0",
12
+ "type-fest": "^4.41.0",
13
+ "undici": "^6.21.3",
11
14
  "ws": "^8.18.0",
12
15
  "xml2js": "^0.5.0",
13
16
  "zod": "^3.24.3"
@@ -16,10 +19,11 @@
16
19
  "node": ">=18.0.0"
17
20
  },
18
21
  "devDependencies": {
19
- "@camstreamer/eslint-config": "^1.0.0",
22
+ "@camstreamer/eslint-config": "^1.0.2",
20
23
  "@camstreamer/prettier-config": "^2.0.4",
21
24
  "@types/adm-zip": "^0.5.5",
22
25
  "@types/jest": "^28.0.0",
26
+ "@types/lodash": "^4.17.18",
23
27
  "@types/node": "^18.19.39",
24
28
  "@types/ws": "^8.5.10",
25
29
  "@types/xml2js": "^0.4.14",