@mherod/get-cookie 2.1.1 → 2.1.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.
- package/.parcelrc +2 -7
- package/bun.lockb +0 -0
- package/dist/cli.js +18319 -2
- package/dist/index.js +24754 -1300
- package/dist/types.d.ts +361 -30
- package/package-lock.json +3997 -6448
- package/package.json +8 -52
- package/src/CookieRow.ts +35 -7
- package/src/CookieSpec.ts +32 -14
- package/src/SpecialCases.ts +6 -5
- package/src/StringToRegex.ts +4 -4
- package/src/browsers/CompositeCookieQueryStrategy.ts +34 -28
- package/src/browsers/CookieQueryStrategy.ts +1 -1
- package/src/browsers/CookieStoreQueryStrategy.ts +71 -55
- package/src/browsers/QuerySqliteThenTransform.ts +27 -64
- package/src/browsers/chrome/ChromeApplicationSupport.ts +4 -0
- package/src/browsers/chrome/ChromeCookieQueryStrategy.ts +118 -91
- package/src/browsers/chrome/decrypt.ts +133 -97
- package/src/browsers/chrome/getChromePassword.ts +22 -5
- package/src/browsers/firefox/FirefoxCookieQueryStrategy.ts +19 -13
- package/src/browsers/getEncryptedChromeCookie.ts +101 -66
- package/src/browsers/mock/MockCookieQueryStrategy.ts +8 -4
- package/src/browsers/safari/SafariCookieQueryStrategy.ts +34 -3
- package/src/cli.ts +36 -29
- package/src/cliQueryCookies.ts +14 -4
- package/src/comboQueryCookieSpec.ts +7 -10
- package/src/cookieQueryOptions.ts +8 -1
- package/src/cookieSpecsFromUrl.ts +53 -7
- package/src/decodeBinaryCookies.ts +72 -0
- package/src/execSimple.ts +1 -2
- package/src/fetchWithCookies.ts +172 -144
- package/src/findAllFiles.ts +10 -0
- package/src/getChromeCookie.ts +6 -8
- package/src/getCookie.ts +8 -8
- package/src/getFirefoxCookie.ts +13 -9
- package/src/getGroupedRenderedCookies.ts +29 -6
- package/src/getMergedRenderedCookies.ts +4 -2
- package/src/global.ts +2 -2
- package/src/index.ts +16 -6
- package/src/isValidJwt.ts +6 -4
- package/src/listChromeProfiles.ts +18 -10
- package/src/logger.ts +0 -1
- package/src/processBeforeReturn.ts +15 -12
- package/src/queryCookies.ts +22 -17
- package/src/resultsRendered.ts +20 -7
- package/src/unpackHeaders.ts +10 -8
- package/src/util/flatMapAsync.ts +20 -16
- package/tsconfig.json +4 -4
- package/dist/index.js.map +0 -1
- package/dist/module.js +0 -1446
- package/dist/module.js.map +0 -1
- package/dist/prompt.2b8c61c0.js +0 -40
- package/dist/prompt.536a2c51.js +0 -40
- package/dist/prompt.fb2c7dad.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/src/CookieStore.ts +0 -27
- package/src/FileCookieStore.ts +0 -178
package/src/fetchWithCookies.ts
CHANGED
|
@@ -2,28 +2,53 @@
|
|
|
2
2
|
|
|
3
3
|
import { fetch as fetchImpl } from "cross-fetch";
|
|
4
4
|
import { merge } from "lodash";
|
|
5
|
-
// noinspection SpellCheckingInspection
|
|
6
5
|
import destr from "destr";
|
|
7
|
-
import { parsedArgs } from "./argv";
|
|
8
6
|
import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
|
|
9
7
|
import { cookieSpecsFromUrl } from "./cookieSpecsFromUrl";
|
|
10
8
|
import CookieSpec from "./CookieSpec";
|
|
11
|
-
import consola from "consola";
|
|
12
9
|
|
|
13
10
|
if (typeof fetchImpl !== "function") {
|
|
14
11
|
throw new Error("fetch is not a function");
|
|
15
12
|
}
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Class to build a User-Agent string.
|
|
16
|
+
*/
|
|
17
|
+
class UserAgentBuilder {
|
|
18
|
+
private platform: string;
|
|
19
|
+
private engine: string;
|
|
20
|
+
private browser: string;
|
|
21
|
+
private layout: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Constructs a UserAgentBuilder instance.
|
|
25
|
+
* @param platform - The platform information.
|
|
26
|
+
* @param engine - The engine information.
|
|
27
|
+
* @param browser - The browser information.
|
|
28
|
+
* @param layout - The layout information.
|
|
29
|
+
*/
|
|
30
|
+
constructor(
|
|
31
|
+
platform: string = "Macintosh; Intel Mac OS X 10_15_7",
|
|
32
|
+
engine: string = "AppleWebKit/537.36 (KHTML, like Gecko)",
|
|
33
|
+
browser: string = "Chrome/118.0.0.0",
|
|
34
|
+
layout: string = "Safari/537.36",
|
|
35
|
+
) {
|
|
36
|
+
this.platform = platform;
|
|
37
|
+
this.engine = engine;
|
|
38
|
+
this.browser = browser;
|
|
39
|
+
this.layout = layout;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Builds the User-Agent string.
|
|
44
|
+
* @returns The User-Agent string.
|
|
45
|
+
*/
|
|
46
|
+
build(): string {
|
|
47
|
+
return `${this.platform} ${this.engine} ${this.browser} ${this.layout}`;
|
|
48
|
+
}
|
|
24
49
|
}
|
|
25
50
|
|
|
26
|
-
const userAgent =
|
|
51
|
+
const userAgent: string = new UserAgentBuilder().build();
|
|
27
52
|
|
|
28
53
|
interface FetchRequestInit {
|
|
29
54
|
url: RequestInfo | URL | string;
|
|
@@ -34,156 +59,159 @@ export type FetchFn =
|
|
|
34
59
|
| typeof fetchImpl
|
|
35
60
|
| ((url: URL, options?: RequestInit) => Promise<Response>);
|
|
36
61
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (typeof fetch !== "function") {
|
|
44
|
-
const message = "fetch is not a function";
|
|
45
|
-
consola.error(message);
|
|
46
|
-
throw new Error(message);
|
|
47
|
-
}
|
|
48
|
-
const originalRequest1 = originalRequest || { url, options };
|
|
49
|
-
const headers: HeadersInit = {
|
|
50
|
-
"User-Agent": userAgent,
|
|
51
|
-
};
|
|
52
|
-
const defaultOptions: RequestInit = {
|
|
53
|
-
headers,
|
|
54
|
-
redirect: "manual",
|
|
55
|
-
};
|
|
56
|
-
const url2: string = `${url}`;
|
|
57
|
-
const url1: URL = new URL(url2);
|
|
58
|
-
consola.start("fetchWithCookies", url2);
|
|
59
|
-
const cookieSpecs: CookieSpec[] = cookieSpecsFromUrl(url1);
|
|
60
|
-
const renderedCookie = await getMergedRenderedCookies(cookieSpecs).catch(
|
|
61
|
-
(err) => {
|
|
62
|
-
consola.error(err);
|
|
63
|
-
return "";
|
|
64
|
-
},
|
|
65
|
-
);
|
|
66
|
-
if (renderedCookie) {
|
|
67
|
-
headers["Cookie"] = renderedCookie;
|
|
68
|
-
} else {
|
|
69
|
-
consola.info("No cookies found for this request");
|
|
70
|
-
}
|
|
71
|
-
if (parsedArgs["dump-request-headers"]) {
|
|
72
|
-
consola.info("Request URL:", url1.href);
|
|
73
|
-
consola.info("Request headers:", headers);
|
|
74
|
-
}
|
|
75
|
-
const newOptions1: RequestInit = merge(defaultOptions, { headers }, options);
|
|
76
|
-
try {
|
|
77
|
-
const res: Response = await fetch(url1, newOptions1);
|
|
78
|
-
// noinspection JSMismatchedCollectionQueryUpdate
|
|
79
|
-
const headers: [string, string][] = [];
|
|
80
|
-
res.headers.forEach((value, key) => {
|
|
81
|
-
headers.push([key, value]);
|
|
82
|
-
});
|
|
83
|
-
// for (const [key, value] of headers) {
|
|
84
|
-
// if (key === "set-cookie") {
|
|
85
|
-
// const cookieJar1 = await cookieJarPromise;
|
|
86
|
-
// await cookieJar1.setCookie(value, url2);
|
|
87
|
-
// if (parsedArgs.verbose) {
|
|
88
|
-
// console.log(blue(`Set-Cookie:`), yellow(value), yellow(url2));
|
|
89
|
-
// }
|
|
90
|
-
// }
|
|
91
|
-
// }
|
|
62
|
+
/**
|
|
63
|
+
* Class to handle fetch requests with cookies.
|
|
64
|
+
*/
|
|
65
|
+
class FetchWithCookies {
|
|
66
|
+
private fetch: FetchFn;
|
|
67
|
+
private userAgent: string;
|
|
92
68
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return fetchWithCookies(
|
|
102
|
-
//
|
|
103
|
-
newUrl,
|
|
104
|
-
newOptions1,
|
|
105
|
-
fetch,
|
|
106
|
-
originalRequest1,
|
|
107
|
-
//
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
if (res.status == 303 && newUrl && newUrl !== url2) {
|
|
112
|
-
// follow the redirect with GET
|
|
113
|
-
let newOptions2: RequestInit = {};
|
|
114
|
-
switch (newOptions1.method) {
|
|
115
|
-
case "POST":
|
|
116
|
-
case "PUT":
|
|
117
|
-
case "DELETE":
|
|
118
|
-
merge(newOptions2, newOptions1, { method: "GET" });
|
|
119
|
-
newOptions2.body = undefined; // TODO: is this needed?
|
|
120
|
-
break;
|
|
121
|
-
case "HEAD":
|
|
122
|
-
case "GET":
|
|
123
|
-
merge(newOptions2, newOptions1);
|
|
124
|
-
newOptions2.body = undefined; // TODO: is this needed?
|
|
125
|
-
break;
|
|
126
|
-
default:
|
|
127
|
-
merge(newOptions2, newOptions1);
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
if (parsedArgs.verbose) {
|
|
131
|
-
console.log(`Redirected to `, newUrl);
|
|
132
|
-
}
|
|
133
|
-
return fetchWithCookies(
|
|
134
|
-
//
|
|
135
|
-
newUrl,
|
|
136
|
-
newOptions2,
|
|
137
|
-
fetch,
|
|
138
|
-
originalRequest1,
|
|
139
|
-
//
|
|
140
|
-
);
|
|
69
|
+
/**
|
|
70
|
+
* Constructs a FetchWithCookies instance.
|
|
71
|
+
* @param fetch - The fetch function to use.
|
|
72
|
+
* @param userAgent - The User-Agent string to use.
|
|
73
|
+
*/
|
|
74
|
+
constructor(fetch: FetchFn, userAgent: string) {
|
|
75
|
+
if (typeof fetch !== "function") {
|
|
76
|
+
throw new Error("fetch is not a function");
|
|
141
77
|
}
|
|
78
|
+
this.fetch = fetch;
|
|
79
|
+
this.userAgent = userAgent;
|
|
80
|
+
}
|
|
142
81
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Gets the headers for a given URL.
|
|
84
|
+
* @param url - The URL to get headers for.
|
|
85
|
+
* @returns A promise that resolves to the headers.
|
|
86
|
+
*/
|
|
87
|
+
private async getHeaders(url: URL): Promise<HeadersInit> {
|
|
88
|
+
const headers: HeadersInit = { "User-Agent": this.userAgent };
|
|
89
|
+
const cookieSpecs: CookieSpec[] = cookieSpecsFromUrl(url);
|
|
90
|
+
const renderedCookie: string = await getMergedRenderedCookies(
|
|
91
|
+
cookieSpecs,
|
|
92
|
+
).catch(() => "");
|
|
146
93
|
|
|
147
|
-
|
|
148
|
-
|
|
94
|
+
if (renderedCookie) {
|
|
95
|
+
headers["Cookie"] = renderedCookie;
|
|
149
96
|
}
|
|
150
97
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
98
|
+
return headers;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Handles redirects for a given response.
|
|
103
|
+
* @param res - The response to handle redirects for.
|
|
104
|
+
* @param url - The original URL.
|
|
105
|
+
* @param options - The request options.
|
|
106
|
+
* @param originalRequest - The original request information.
|
|
107
|
+
* @returns A promise that resolves to the final response.
|
|
108
|
+
*/
|
|
109
|
+
private async handleRedirects(
|
|
110
|
+
res: Response,
|
|
111
|
+
url: URL,
|
|
112
|
+
options: RequestInit,
|
|
113
|
+
originalRequest: FetchRequestInit,
|
|
114
|
+
): Promise<Response> {
|
|
115
|
+
const newUrl: string = res.headers.get("location") ?? res.url;
|
|
154
116
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
117
|
+
if (
|
|
118
|
+
[301, 302].includes(res.status) &&
|
|
119
|
+
newUrl &&
|
|
120
|
+
newUrl !== url.toString()
|
|
121
|
+
) {
|
|
122
|
+
return this.fetchWithCookies(newUrl, options, originalRequest);
|
|
158
123
|
}
|
|
159
124
|
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
125
|
+
if (res.status === 303 && newUrl && newUrl !== url.toString()) {
|
|
126
|
+
const newOptions: RequestInit = {
|
|
127
|
+
...options,
|
|
128
|
+
method: "GET",
|
|
129
|
+
body: undefined,
|
|
130
|
+
};
|
|
131
|
+
return this.fetchWithCookies(newUrl, newOptions, originalRequest);
|
|
163
132
|
}
|
|
164
133
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
134
|
+
return res;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Enhances the response with additional methods.
|
|
139
|
+
* @param res - The response to enhance.
|
|
140
|
+
* @returns A promise that resolves to the enhanced response.
|
|
141
|
+
*/
|
|
142
|
+
private async enhanceResponse(res: Response): Promise<Response> {
|
|
143
|
+
const originalArrayBuffer: ArrayBuffer = await res.arrayBuffer();
|
|
144
|
+
const originalBuffer: Buffer = Buffer.from(originalArrayBuffer);
|
|
145
|
+
const originalText: string = originalBuffer.toString("utf8");
|
|
146
|
+
|
|
147
|
+
const arrayBuffer = async (): Promise<ArrayBuffer> => originalArrayBuffer;
|
|
148
|
+
const buffer = async (): Promise<Buffer> => originalBuffer;
|
|
149
|
+
const text = async (): Promise<string> => originalText;
|
|
150
|
+
const json = async (): Promise<any> => destr(originalText);
|
|
151
|
+
const formData = async (): Promise<FormData> => {
|
|
152
|
+
const urlSearchParams: URLSearchParams = new URLSearchParams(
|
|
153
|
+
originalText,
|
|
168
154
|
);
|
|
169
|
-
const formData = new FormData();
|
|
155
|
+
const formData: FormData = new FormData();
|
|
170
156
|
for (const [key, value] of urlSearchParams.entries()) {
|
|
171
157
|
formData.append(key, value);
|
|
172
158
|
}
|
|
173
159
|
return formData;
|
|
174
|
-
}
|
|
160
|
+
};
|
|
175
161
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
162
|
+
return merge(res, { arrayBuffer, text, json, buffer, formData });
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Fetches a URL with cookies.
|
|
167
|
+
* @param url - The URL to fetch.
|
|
168
|
+
* @param options - The request options.
|
|
169
|
+
* @param originalRequest - The original request information.
|
|
170
|
+
* @returns A promise that resolves to the response.
|
|
171
|
+
*/
|
|
172
|
+
public async fetchWithCookies(
|
|
173
|
+
url: RequestInfo | URL | string,
|
|
174
|
+
options: RequestInit | undefined = {},
|
|
175
|
+
originalRequest?: FetchRequestInit,
|
|
176
|
+
): Promise<Response> {
|
|
177
|
+
const originalRequest1: FetchRequestInit = originalRequest || {
|
|
178
|
+
url,
|
|
179
|
+
options,
|
|
184
180
|
};
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
181
|
+
const url1: URL = new URL(`${url}`);
|
|
182
|
+
const headers: HeadersInit = await this.getHeaders(url1);
|
|
183
|
+
const defaultOptions: RequestInit = { headers, redirect: "manual" };
|
|
184
|
+
const newOptions: RequestInit = merge(defaultOptions, { headers }, options);
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
const res: Response = await this.fetch(url1, newOptions);
|
|
188
|
+
const redirectedRes: Response = await this.handleRedirects(
|
|
189
|
+
res,
|
|
190
|
+
url1,
|
|
191
|
+
newOptions,
|
|
192
|
+
originalRequest1,
|
|
193
|
+
);
|
|
194
|
+
return this.enhanceResponse(redirectedRes);
|
|
195
|
+
} catch (e) {
|
|
196
|
+
throw e;
|
|
197
|
+
}
|
|
188
198
|
}
|
|
189
199
|
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Fetches a URL with cookies.
|
|
203
|
+
* @param url - The URL to fetch.
|
|
204
|
+
* @param options - The request options.
|
|
205
|
+
* @param fetch - The fetch function to use.
|
|
206
|
+
* @param originalRequest - The original request information.
|
|
207
|
+
* @returns A promise that resolves to the response.
|
|
208
|
+
*/
|
|
209
|
+
export async function fetchWithCookies(
|
|
210
|
+
url: RequestInfo | URL | string,
|
|
211
|
+
options: RequestInit | undefined = {},
|
|
212
|
+
fetch: FetchFn = fetchImpl as FetchFn,
|
|
213
|
+
originalRequest?: FetchRequestInit,
|
|
214
|
+
): Promise<Response> {
|
|
215
|
+
const fetcher = new FetchWithCookies(fetch, userAgent);
|
|
216
|
+
return fetcher.fetchWithCookies(url, options, originalRequest);
|
|
217
|
+
}
|
package/src/findAllFiles.ts
CHANGED
|
@@ -9,6 +9,16 @@ type FindFilesOptions = {
|
|
|
9
9
|
maxDepth?: number;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Finds all files matching the specified name within a given path and depth.
|
|
14
|
+
*
|
|
15
|
+
* @param {FindFilesOptions} options - The options for finding files.
|
|
16
|
+
* @param {string} options.path - The path to search within.
|
|
17
|
+
* @param {string} options.name - The name of the files to search for.
|
|
18
|
+
* @param {number} [options.maxDepth=2] - The maximum depth to search within.
|
|
19
|
+
* @returns {string[]} An array of file paths that match the search criteria.
|
|
20
|
+
* @throws Will throw an error if the specified path does not exist.
|
|
21
|
+
*/
|
|
12
22
|
export function findAllFiles({
|
|
13
23
|
path,
|
|
14
24
|
name,
|
package/src/getChromeCookie.ts
CHANGED
|
@@ -7,15 +7,13 @@ import { isExportedCookie } from "./ExportedCookie";
|
|
|
7
7
|
export async function getChromeCookie(
|
|
8
8
|
params: CookieSpec,
|
|
9
9
|
): Promise<ExportedCookie | undefined> {
|
|
10
|
-
const cookies = await queryCookies(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
);
|
|
17
|
-
if (cookies.length == 0) {
|
|
10
|
+
const cookies: ExportedCookie[] = await queryCookies(params, {
|
|
11
|
+
strategy: new ChromeCookieQueryStrategy(),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (cookies.length === 0) {
|
|
18
15
|
throw new Error("Cookie not found");
|
|
19
16
|
}
|
|
17
|
+
|
|
20
18
|
return cookies.find(isExportedCookie);
|
|
21
19
|
}
|
package/src/getCookie.ts
CHANGED
|
@@ -3,20 +3,20 @@ import ExportedCookie from "./ExportedCookie";
|
|
|
3
3
|
import { queryCookies } from "./queryCookies";
|
|
4
4
|
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
5
5
|
|
|
6
|
+
const queryStrategy: CompositeCookieQueryStrategy =
|
|
7
|
+
new CompositeCookieQueryStrategy();
|
|
8
|
+
|
|
6
9
|
export async function getCookie(
|
|
7
10
|
params: CookieSpec,
|
|
8
11
|
): Promise<ExportedCookie | undefined> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
{
|
|
13
|
-
strategy: new CompositeCookieQueryStrategy(),
|
|
14
|
-
},
|
|
15
|
-
//
|
|
16
|
-
);
|
|
12
|
+
const cookies: ExportedCookie[] = await queryCookies(params, {
|
|
13
|
+
strategy: queryStrategy,
|
|
14
|
+
});
|
|
17
15
|
if (Array.isArray(cookies) && cookies.length > 0) {
|
|
18
16
|
return cookies.find((cookie) => cookie != null);
|
|
19
17
|
} else {
|
|
20
18
|
throw new Error("Cookie not found");
|
|
21
19
|
}
|
|
22
20
|
}
|
|
21
|
+
|
|
22
|
+
export default getCookie;
|
package/src/getFirefoxCookie.ts
CHANGED
|
@@ -6,16 +6,20 @@ import FirefoxCookieQueryStrategy from "./browsers/firefox/FirefoxCookieQueryStr
|
|
|
6
6
|
export async function getFirefoxCookie(
|
|
7
7
|
params: CookieSpec,
|
|
8
8
|
): Promise<ExportedCookie | undefined> {
|
|
9
|
-
const cookies: ExportedCookie[] = await queryCookies(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
const cookies: ExportedCookie[] = await queryCookies(params, {
|
|
10
|
+
strategy: new FirefoxCookieQueryStrategy(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
if (!Array.isArray(cookies) || cookies.length === 0) {
|
|
14
|
+
throw new Error("Cookie not found");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const validCookie: ExportedCookie | undefined = cookies.find(
|
|
18
|
+
(cookie) => cookie != null,
|
|
15
19
|
);
|
|
16
|
-
if (
|
|
17
|
-
return cookies.find((cookie) => cookie != null);
|
|
18
|
-
} else {
|
|
20
|
+
if (!validCookie) {
|
|
19
21
|
throw new Error("Cookie not found");
|
|
20
22
|
}
|
|
23
|
+
|
|
24
|
+
return validCookie;
|
|
21
25
|
}
|
|
@@ -7,13 +7,36 @@ import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
|
7
7
|
export async function getGroupedRenderedCookies(
|
|
8
8
|
cookieSpec: MultiCookieSpec,
|
|
9
9
|
): Promise<string[]> {
|
|
10
|
+
const cookies: ExportedCookie[] = await fetchCookies(cookieSpec);
|
|
11
|
+
const groupedByFile = groupCookiesByFile(cookies);
|
|
12
|
+
return renderGroupedCookies(groupedByFile);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function fetchCookies(
|
|
16
|
+
cookieSpec: MultiCookieSpec,
|
|
17
|
+
): Promise<ExportedCookie[]> {
|
|
10
18
|
const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec);
|
|
11
|
-
if (cookies.length
|
|
19
|
+
if (cookies.length === 0) {
|
|
12
20
|
throw new Error("Cookie not found");
|
|
13
21
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
return cookies;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function groupCookiesByFile(
|
|
26
|
+
cookies: ExportedCookie[],
|
|
27
|
+
): Record<string, ExportedCookie[]> {
|
|
28
|
+
return groupBy(cookies, (r: ExportedCookie) => r.meta?.file);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function renderGroupedCookies(
|
|
32
|
+
groupedByFile: Record<string, ExportedCookie[]>,
|
|
33
|
+
): string[] {
|
|
34
|
+
const renderedResults: string[] = [];
|
|
35
|
+
for (const file in groupedByFile) {
|
|
36
|
+
if (groupedByFile.hasOwnProperty(file)) {
|
|
37
|
+
const results: ExportedCookie[] = groupedByFile[file];
|
|
38
|
+
renderedResults.push(resultsRendered(results));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return renderedResults;
|
|
19
42
|
}
|
|
@@ -2,7 +2,6 @@ import { resultsRendered } from "./resultsRendered";
|
|
|
2
2
|
import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
3
3
|
import { MultiCookieSpec } from "./CookieSpec";
|
|
4
4
|
import ExportedCookie from "./ExportedCookie";
|
|
5
|
-
import consola from "consola";
|
|
6
5
|
import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
|
|
7
6
|
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
8
7
|
|
|
@@ -13,5 +12,8 @@ export async function getMergedRenderedCookies(
|
|
|
13
12
|
const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec, {
|
|
14
13
|
strategy,
|
|
15
14
|
});
|
|
16
|
-
|
|
15
|
+
if (cookies.length > 0) {
|
|
16
|
+
return resultsRendered(cookies);
|
|
17
|
+
}
|
|
18
|
+
return "";
|
|
17
19
|
}
|
package/src/global.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { merge } from "lodash";
|
|
2
2
|
|
|
3
|
-
export const env:
|
|
3
|
+
export const env: { [key: string]: string | undefined } = {};
|
|
4
4
|
merge(env, process?.env ?? {});
|
|
5
|
-
export const HOME: string = env["HOME"];
|
|
5
|
+
export const HOME: string | undefined = env["HOME"];
|
|
6
6
|
if (!HOME) {
|
|
7
7
|
throw new Error("HOME environment variable is not set");
|
|
8
8
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
import
|
|
1
|
+
const getCookie = () =>
|
|
2
|
+
import("./getCookie").then((module) => module.getCookie);
|
|
3
|
+
const getChromeCookie = () =>
|
|
4
|
+
import("./getChromeCookie").then((module) => module.getChromeCookie);
|
|
5
|
+
const getFirefoxCookie = () =>
|
|
6
|
+
import("./getFirefoxCookie").then((module) => module.getFirefoxCookie);
|
|
7
|
+
const getGroupedRenderedCookies = () =>
|
|
8
|
+
import("./getGroupedRenderedCookies").then(
|
|
9
|
+
(module) => module.getGroupedRenderedCookies,
|
|
10
|
+
);
|
|
11
|
+
const getMergedRenderedCookies = () =>
|
|
12
|
+
import("./getMergedRenderedCookies").then(
|
|
13
|
+
(module) => module.getMergedRenderedCookies,
|
|
14
|
+
);
|
|
15
|
+
const fetchWithCookies = () =>
|
|
16
|
+
import("./fetchWithCookies").then((module) => module.fetchWithCookies);
|
|
7
17
|
|
|
8
18
|
export {
|
|
9
19
|
getCookie,
|
package/src/isValidJwt.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import jsonwebtoken, { JwtPayload } from "jsonwebtoken";
|
|
2
2
|
import { parsedArgs } from "./argv";
|
|
3
3
|
|
|
4
|
-
export default function isValidJwt(token: string) {
|
|
4
|
+
export default function isValidJwt(token: string): boolean {
|
|
5
5
|
try {
|
|
6
6
|
const result = jsonwebtoken.decode(token, { complete: true });
|
|
7
7
|
if (parsedArgs.verbose && result) {
|
|
8
8
|
console.debug(result);
|
|
9
9
|
}
|
|
10
|
-
const payload: JwtPayload = result?.payload as
|
|
10
|
+
const payload: JwtPayload | undefined = result?.payload as
|
|
11
|
+
| JwtPayload
|
|
12
|
+
| undefined;
|
|
11
13
|
if (payload) {
|
|
12
|
-
const exp = payload.exp;
|
|
14
|
+
const exp: number | undefined = payload.exp;
|
|
13
15
|
if (exp) {
|
|
14
|
-
const now = new Date().getTime() / 1000;
|
|
16
|
+
const now: number = new Date().getTime() / 1000;
|
|
15
17
|
if (now > exp) {
|
|
16
18
|
return false;
|
|
17
19
|
}
|
|
@@ -2,27 +2,35 @@ import { sync } from "fast-glob";
|
|
|
2
2
|
import { chromeApplicationSupport } from "./browsers/chrome/ChromeApplicationSupport";
|
|
3
3
|
import { dirname } from "path";
|
|
4
4
|
import { readFile } from "fs/promises";
|
|
5
|
-
import { flatMapAsync } from "./util/flatMapAsync";
|
|
6
5
|
import destr from "destr";
|
|
7
6
|
|
|
8
7
|
export async function listChromeProfilePaths(): Promise<string[]> {
|
|
9
|
-
|
|
8
|
+
const files: string[] = sync(`./**/Cookies`, {
|
|
10
9
|
cwd: chromeApplicationSupport,
|
|
11
10
|
absolute: true,
|
|
12
11
|
onlyFiles: true,
|
|
13
12
|
deep: 2,
|
|
14
|
-
}).map((f) => {
|
|
15
|
-
// parent dir
|
|
16
|
-
return dirname(f);
|
|
17
13
|
});
|
|
14
|
+
|
|
15
|
+
const directories: string[] = [];
|
|
16
|
+
for (const file of files) {
|
|
17
|
+
directories.push(dirname(file));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return directories;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
export async function listChromeProfiles(): Promise<ChromeProfile[]> {
|
|
21
|
-
const paths = await listChromeProfilePaths();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const paths: string[] = await listChromeProfilePaths();
|
|
25
|
+
const profiles: ChromeProfile[] = [];
|
|
26
|
+
|
|
27
|
+
for (const path of paths) {
|
|
28
|
+
const content: string = await readFile(`${path}/Preferences`, "utf-8");
|
|
29
|
+
const profile: ChromeProfile = await destr(content);
|
|
30
|
+
profiles.push(profile);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return profiles;
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
export type ChromeProfileAccountInfo = {
|
package/src/logger.ts
CHANGED
|
@@ -7,20 +7,23 @@ export function processBeforeReturn<T extends CookieQueryStrategy>(
|
|
|
7
7
|
cookies: ExportedCookie[],
|
|
8
8
|
options?: CookieQueryOptions<T>,
|
|
9
9
|
): ExportedCookie[] {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
let processedCookies: ExportedCookie[] = cookies;
|
|
11
|
+
|
|
12
|
+
if (options && options.removeExpired) {
|
|
13
|
+
const now: number = Date.now();
|
|
14
|
+
processedCookies = processedCookies.filter((c: ExportedCookie) => {
|
|
12
15
|
const expiry: Date | "Infinity" | undefined = c.expiry;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return true;
|
|
17
|
-
} else {
|
|
18
|
-
return expiry.getTime() > Date.now();
|
|
19
|
-
}
|
|
16
|
+
return (
|
|
17
|
+
expiry === undefined || expiry === "Infinity" || expiry.getTime() > now
|
|
18
|
+
);
|
|
20
19
|
});
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
|
|
22
|
+
if (options && options.limit) {
|
|
23
|
+
processedCookies = processedCookies.slice(0, options.limit);
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
return uniqBy(processedCookies, (cookie: ExportedCookie) =>
|
|
27
|
+
JSON.stringify(cookie),
|
|
28
|
+
);
|
|
26
29
|
}
|