homebridge-melcloud-control 4.4.1-beta.25 → 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.25",
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
 
@@ -576,54 +578,6 @@ 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