Implement custom cookies management mechanism
This commit is contained in:
76
package-lock.json
generated
76
package-lock.json
generated
@@ -15,11 +15,11 @@
|
||||
"dayjs": "^1.11.13",
|
||||
"mqtt": "^5.10.1",
|
||||
"tough-cookie": "^5.0.0",
|
||||
"tough-cookie-file-store": "^2.0.3",
|
||||
"yaml": "^2.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.9.0",
|
||||
"@types/tough-cookie": "^4.0.5",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.6.3"
|
||||
}
|
||||
@@ -441,6 +441,12 @@
|
||||
"safe-buffer": "~5.1.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/tough-cookie": {
|
||||
"version": "4.0.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-4.0.5.tgz",
|
||||
"integrity": "sha512-/Ad8+nIOV7Rl++6f1BdKxFSMgmoqEoYbHRpPcx3JEfv8VRsQe9Z4mCXeJBzxs7mbHY/XOZZuXlRNfhpVPbs6ZA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/ws": {
|
||||
"version": "8.5.13",
|
||||
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.13.tgz",
|
||||
@@ -925,27 +931,6 @@
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||
},
|
||||
"node_modules/psl": {
|
||||
"version": "1.10.0",
|
||||
"resolved": "https://registry.npmjs.org/psl/-/psl-1.10.0.tgz",
|
||||
"integrity": "sha512-KSKHEbjAnpUuAUserOq0FxGXCUrzC3WniuSJhvdbs102rL55266ZcHBqLWOsG30spQMlPdpy7icATiAQehg/iA==",
|
||||
"dependencies": {
|
||||
"punycode": "^2.3.1"
|
||||
}
|
||||
},
|
||||
"node_modules/punycode": {
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
|
||||
"integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/querystringify": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
|
||||
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
|
||||
},
|
||||
"node_modules/readable-stream": {
|
||||
"version": "4.5.2",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-4.5.2.tgz",
|
||||
@@ -971,11 +956,6 @@
|
||||
"resolved": "https://registry.npmjs.org/reinterval/-/reinterval-1.1.0.tgz",
|
||||
"integrity": "sha512-QIRet3SYrGp0HUHO88jVskiG6seqUGC5iAG7AwI/BV4ypGcuqk9Du6YQBUOUqm9c8pw1eyLoIaONifRua1lsEQ=="
|
||||
},
|
||||
"node_modules/requires-port": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
|
||||
"integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ=="
|
||||
},
|
||||
"node_modules/resolve-pkg-maps": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
|
||||
@@ -1057,31 +1037,6 @@
|
||||
"node": ">=16"
|
||||
}
|
||||
},
|
||||
"node_modules/tough-cookie-file-store": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/tough-cookie-file-store/-/tough-cookie-file-store-2.0.3.tgz",
|
||||
"integrity": "sha512-sMpZVcmFf6EYFHFFl+SYH4W1/OnXBYMGDsv2IlbQ2caHyFElW/UR/gpj/KYU1JwmP4dE9xqwv2+vWcmlXHojSw==",
|
||||
"dependencies": {
|
||||
"tough-cookie": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/tough-cookie-file-store/node_modules/tough-cookie": {
|
||||
"version": "4.1.4",
|
||||
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.4.tgz",
|
||||
"integrity": "sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==",
|
||||
"dependencies": {
|
||||
"psl": "^1.1.33",
|
||||
"punycode": "^2.1.1",
|
||||
"universalify": "^0.2.0",
|
||||
"url-parse": "^1.5.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/tslib": {
|
||||
"version": "2.8.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
|
||||
@@ -1129,23 +1084,6 @@
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
|
||||
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw=="
|
||||
},
|
||||
"node_modules/universalify": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz",
|
||||
"integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==",
|
||||
"engines": {
|
||||
"node": ">= 4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/url-parse": {
|
||||
"version": "1.5.10",
|
||||
"resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz",
|
||||
"integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==",
|
||||
"dependencies": {
|
||||
"querystringify": "^2.1.1",
|
||||
"requires-port": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/util-deprecate": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.9.0",
|
||||
"@types/tough-cookie": "^4.0.5",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.6.3"
|
||||
},
|
||||
@@ -20,7 +21,6 @@
|
||||
"dayjs": "^1.11.13",
|
||||
"mqtt": "^5.10.1",
|
||||
"tough-cookie": "^5.0.0",
|
||||
"tough-cookie-file-store": "^2.0.3",
|
||||
"yaml": "^2.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
24
src/tauron/cookie.ts
Normal file
24
src/tauron/cookie.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import fs from "fs";
|
||||
import { CookieJar } from "tough-cookie";
|
||||
import { wrapper } from 'axios-cookiejar-support';
|
||||
import { AxiosStatic, AxiosInstance, AxiosResponse } from "axios";
|
||||
|
||||
export const withCookies = <T extends AxiosStatic | AxiosInstance>(filePath: string) => {
|
||||
const jar = fs.existsSync(filePath)
|
||||
? CookieJar.deserializeSync(fs.readFileSync(filePath, 'utf8'))
|
||||
: new CookieJar();
|
||||
|
||||
const serializeCookiesJar = (r: AxiosResponse<unknown, unknown>) => {
|
||||
const cookies = JSON.stringify(jar.serializeSync());
|
||||
fs.writeFileSync(filePath, cookies);
|
||||
return r;
|
||||
};
|
||||
|
||||
return (axios: T): T => {
|
||||
const http = wrapper(axios);
|
||||
http.defaults.jar = jar;
|
||||
http.interceptors.response.use(serializeCookiesJar);
|
||||
|
||||
return http;
|
||||
};
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
import fs from 'fs';
|
||||
import axios, { Axios, AxiosResponse } from "axios";
|
||||
import { wrapper } from 'axios-cookiejar-support';
|
||||
import { CookieJar } from 'tough-cookie';
|
||||
import { FileCookieStore } from 'tough-cookie-file-store';
|
||||
import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types";
|
||||
import { TauronConfig } from '../config';
|
||||
import { Dayjs } from 'dayjs';
|
||||
import { withCookies } from './cookie';
|
||||
|
||||
export * from './types';
|
||||
|
||||
@@ -26,12 +24,12 @@ export class Tauron {
|
||||
#config: TauronConfig;
|
||||
|
||||
constructor(config: TauronConfig) {
|
||||
// TODO: It should be paths instead of raw credentials
|
||||
this.#config = config;
|
||||
|
||||
const wrapper = withCookies(config.cookiesJarPath || './cookies.json');
|
||||
|
||||
this.#http = wrapper(axios.create({
|
||||
baseURL: BASE_URL,
|
||||
jar: new CookieJar(new FileCookieStore(config.cookiesJarPath || './cookies.json')),
|
||||
headers: {
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/jxl,image/webp,image/png,image/svg+xml,*/*;q=0.8',
|
||||
'Accept-Language': 'en-US,en;q=0.5',
|
||||
|
||||
Reference in New Issue
Block a user