homebridge-melcloud-control 4.4.1-beta.24 → 4.4.1-beta.26

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.4.1-beta.24",
4
+ "version": "4.4.1-beta.26",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/helper.js ADDED
@@ -0,0 +1,70 @@
1
+ class CookieJar {
2
+ constructor() {
3
+ this.cookies = [];
4
+ }
5
+
6
+ addFromHeader(setCookie, url) {
7
+ if (!setCookie) return;
8
+
9
+ const urlObj = new URL(url);
10
+ const headers = Array.isArray(setCookie) ? setCookie : [setCookie];
11
+
12
+ for (const header of headers) {
13
+ const parts = header.split(';').map(p => p.trim());
14
+ const [name, value] = parts[0].split('=');
15
+
16
+ const cookie = {
17
+ name,
18
+ value,
19
+ domain: urlObj.hostname,
20
+ path: '/',
21
+ expires: null,
22
+ };
23
+
24
+ for (const part of parts.slice(1)) {
25
+ const [key, val] = part.split('=').map(v => v?.trim());
26
+ if (!key) continue;
27
+
28
+ switch (key.toLowerCase()) {
29
+ case 'domain':
30
+ cookie.domain = val;
31
+ break;
32
+ case 'path':
33
+ cookie.path = val || '/';
34
+ break;
35
+ case 'expires':
36
+ cookie.expires = new Date(val);
37
+ break;
38
+ case 'max-age':
39
+ cookie.expires = new Date(Date.now() + Number(val) * 1000);
40
+ break;
41
+ }
42
+ }
43
+
44
+ this.cookies = this.cookies.filter(
45
+ c => !(c.name === cookie.name && c.domain === cookie.domain && c.path === cookie.path),
46
+ );
47
+
48
+ this.cookies.push(cookie);
49
+ }
50
+ }
51
+
52
+ headerFor(url) {
53
+ const urlObj = new URL(url);
54
+ const now = Date.now();
55
+
56
+ const validCookies = this.cookies.filter(cookie => {
57
+ if (cookie.expires && cookie.expires.getTime() <= now) return false;
58
+ if (!urlObj.hostname.endsWith(cookie.domain)) return false;
59
+ if (!urlObj.pathname.startsWith(cookie.path)) return false;
60
+ return true;
61
+ });
62
+
63
+ return validCookies.length
64
+ ? validCookies.map(c => `${c.name}=${c.value}`).join('; ')
65
+ : null;
66
+ }
67
+ }
68
+
69
+
70
+ export default CookieJar
@@ -9,6 +9,8 @@ import Functions from './functions.js';
9
9
  import { ApiUrls, LanguageLocaleMap } from './constants.js';
10
10
  const execPromise = promisify(exec);
11
11
 
12
+ import CookieJar from './helper.js';
13
+
12
14
  import crypto from 'crypto';
13
15
  import { URL } from 'url';
14
16
 
@@ -513,7 +515,7 @@ class MelCloudHome extends EventEmitter {
513
515
  }).toString();
514
516
 
515
517
  // STEP 1 – get login page
516
- const loginPage = await axiosRequest(authUrl, {
518
+ const loginPage = await this.axiosRequest(authUrl, {
517
519
  headers: { 'User-Agent': userAgent },
518
520
  }, jar);
519
521
 
@@ -530,7 +532,7 @@ class MelCloudHome extends EventEmitter {
530
532
  password,
531
533
  }).toString();
532
534
 
533
- const loginResult = await axiosRequest(loginPage.url, {
535
+ const loginResult = await this.axiosRequest(loginPage.url, {
534
536
  method: 'POST',
535
537
  headers: {
536
538
  'User-Agent': userAgent,
@@ -576,61 +578,13 @@ class MelCloudHome extends EventEmitter {
576
578
  };
577
579
  }
578
580
 
579
- addFromHeader(setCookie, url) {
580
- if (!setCookie) return;
581
- const urlObj = new URL(url);
582
- const cookies = Array.isArray(setCookie) ? setCookie : [setCookie];
583
-
584
- for (const header of cookies) {
585
- const parts = header.split(';').map(p => p.trim());
586
- const [name, value] = parts[0].split('=');
587
-
588
- const cookie = {
589
- name,
590
- value,
591
- domain: urlObj.hostname,
592
- path: '/',
593
- expires: null,
594
- };
595
-
596
- for (const part of parts.slice(1)) {
597
- const [k, v] = part.split('=');
598
- if (!k) continue;
599
- const key = k.toLowerCase();
600
- if (key === 'domain') cookie.domain = v;
601
- if (key === 'path') cookie.path = v;
602
- if (key === 'expires') cookie.expires = new Date(v);
603
- if (key === 'max-age') cookie.expires = new Date(Date.now() + Number(v) * 1000);
604
- }
605
-
606
- this.cookies = this.cookies.filter(
607
- c => !(c.name === cookie.name && c.domain === cookie.domain && c.path === cookie.path),
608
- );
609
- this.cookies.push(cookie);
610
- }
611
- }
612
-
613
- headerFor(url) {
614
- const urlObj = new URL(url);
615
- const now = Date.now();
616
-
617
- return this.cookies
618
- .filter(c =>
619
- (!c.expires || c.expires.getTime() > now) &&
620
- urlObj.hostname.endsWith(c.domain) &&
621
- urlObj.pathname.startsWith(c.path),
622
- )
623
- .map(c => `${c.name}=${c.value}`)
624
- .join('; ');
625
- }
626
-
627
581
  async connect() {
628
582
  if (this.logDebug) this.emit('debug', 'Connecting to MELCloud Home');
629
583
 
630
584
  try {
631
585
  const accountInfo = { State: false, Info: '', Account: {}, UseFahrenheit: false };
632
586
 
633
- const tokens = await loginWithCredentials(this.user, this.passwd);
587
+ const tokens = await this.loginWithCredentials(this.user, this.passwd);
634
588
  this.emit('debug', `Tokens: ${JSON.stringify(tokens, null, 2)}`);
635
589
  return accountInfo
636
590
  } catch (error) {