@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.
Files changed (57) hide show
  1. package/.parcelrc +2 -7
  2. package/bun.lockb +0 -0
  3. package/dist/cli.js +18319 -2
  4. package/dist/index.js +24754 -1300
  5. package/dist/types.d.ts +361 -30
  6. package/package-lock.json +3997 -6448
  7. package/package.json +8 -52
  8. package/src/CookieRow.ts +35 -7
  9. package/src/CookieSpec.ts +32 -14
  10. package/src/SpecialCases.ts +6 -5
  11. package/src/StringToRegex.ts +4 -4
  12. package/src/browsers/CompositeCookieQueryStrategy.ts +34 -28
  13. package/src/browsers/CookieQueryStrategy.ts +1 -1
  14. package/src/browsers/CookieStoreQueryStrategy.ts +71 -55
  15. package/src/browsers/QuerySqliteThenTransform.ts +27 -64
  16. package/src/browsers/chrome/ChromeApplicationSupport.ts +4 -0
  17. package/src/browsers/chrome/ChromeCookieQueryStrategy.ts +118 -91
  18. package/src/browsers/chrome/decrypt.ts +133 -97
  19. package/src/browsers/chrome/getChromePassword.ts +22 -5
  20. package/src/browsers/firefox/FirefoxCookieQueryStrategy.ts +19 -13
  21. package/src/browsers/getEncryptedChromeCookie.ts +101 -66
  22. package/src/browsers/mock/MockCookieQueryStrategy.ts +8 -4
  23. package/src/browsers/safari/SafariCookieQueryStrategy.ts +34 -3
  24. package/src/cli.ts +36 -29
  25. package/src/cliQueryCookies.ts +14 -4
  26. package/src/comboQueryCookieSpec.ts +7 -10
  27. package/src/cookieQueryOptions.ts +8 -1
  28. package/src/cookieSpecsFromUrl.ts +53 -7
  29. package/src/decodeBinaryCookies.ts +72 -0
  30. package/src/execSimple.ts +1 -2
  31. package/src/fetchWithCookies.ts +172 -144
  32. package/src/findAllFiles.ts +10 -0
  33. package/src/getChromeCookie.ts +6 -8
  34. package/src/getCookie.ts +8 -8
  35. package/src/getFirefoxCookie.ts +13 -9
  36. package/src/getGroupedRenderedCookies.ts +29 -6
  37. package/src/getMergedRenderedCookies.ts +4 -2
  38. package/src/global.ts +2 -2
  39. package/src/index.ts +16 -6
  40. package/src/isValidJwt.ts +6 -4
  41. package/src/listChromeProfiles.ts +18 -10
  42. package/src/logger.ts +0 -1
  43. package/src/processBeforeReturn.ts +15 -12
  44. package/src/queryCookies.ts +22 -17
  45. package/src/resultsRendered.ts +20 -7
  46. package/src/unpackHeaders.ts +10 -8
  47. package/src/util/flatMapAsync.ts +20 -16
  48. package/tsconfig.json +4 -4
  49. package/dist/index.js.map +0 -1
  50. package/dist/module.js +0 -1446
  51. package/dist/module.js.map +0 -1
  52. package/dist/prompt.2b8c61c0.js +0 -40
  53. package/dist/prompt.536a2c51.js +0 -40
  54. package/dist/prompt.fb2c7dad.js.map +0 -1
  55. package/dist/types.d.ts.map +0 -1
  56. package/src/CookieStore.ts +0 -27
  57. package/src/FileCookieStore.ts +0 -178
@@ -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
- function constructUserAgent(
18
- platform = "Macintosh; Intel Mac OS X 10_15_7",
19
- engine = "AppleWebKit/537.36 (KHTML, like Gecko)",
20
- browser = "Chrome/118.0.0.0",
21
- layout = "Safari/537.36",
22
- ): string {
23
- return [platform, engine, browser, layout].join(" ");
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 = constructUserAgent();
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
- export async function fetchWithCookies(
38
- url: RequestInfo | URL | string,
39
- options: RequestInit | undefined = {},
40
- fetch: FetchFn = fetchImpl as FetchFn,
41
- originalRequest?: FetchRequestInit,
42
- ): Promise<Response> {
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
- const newUrl: string = res.headers.get("location") ?? res.url;
94
- // const sameHost = new URL(newUrl).host === url1.host;
95
- if (res.status == 301 || res.status == 302) {
96
- // follow the redirect
97
- if (newUrl && newUrl !== url2) {
98
- if (parsedArgs.verbose || parsedArgs["dump-response-headers"]) {
99
- consola.info(`Redirected to `, newUrl);
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
- // all deserialize functions will use the arraybuffer
144
- const arrayBufferPromise: Promise<ArrayBuffer> = res.arrayBuffer();
145
- const bufferPromise = arrayBufferPromise.then(Buffer.from);
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
- async function arrayBuffer(): Promise<ArrayBuffer> {
148
- return arrayBufferPromise;
94
+ if (renderedCookie) {
95
+ headers["Cookie"] = renderedCookie;
149
96
  }
150
97
 
151
- async function buffer(): Promise<Buffer> {
152
- return bufferPromise;
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
- async function text(): Promise<string> {
156
- const buffer1 = await bufferPromise;
157
- return buffer1.toString("utf8");
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
- async function json(): Promise<any> {
161
- const text1 = await text();
162
- return destr(text1);
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
- async function formData(): Promise<FormData> {
166
- const urlSearchParams: URLSearchParams = await text().then(
167
- (text) => new URLSearchParams(text),
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
- const res1: Response = res;
177
- const source2 = {
178
- arrayBuffer,
179
- text,
180
- json,
181
- buffer,
182
- formData,
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
- return merge(res1, source2);
186
- } catch (e) {
187
- throw e;
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
+ }
@@ -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,
@@ -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
- params,
12
- {
13
- strategy: new ChromeCookieQueryStrategy(),
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
- const cookies: ExportedCookie[] = await queryCookies(
11
- params,
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;
@@ -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
- params,
11
- {
12
- strategy: new FirefoxCookieQueryStrategy(),
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 (Array.isArray(cookies) && cookies.length > 0) {
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 == 0) {
19
+ if (cookies.length === 0) {
12
20
  throw new Error("Cookie not found");
13
21
  }
14
- const groupedByFile = groupBy(cookies, (r: ExportedCookie) => r.meta?.file);
15
- return Object.keys(groupedByFile).map((file: string) => {
16
- const results: ExportedCookie[] = groupedByFile[file];
17
- return resultsRendered(results);
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
- return cookies.length > 0 ? resultsRendered(cookies) : "";
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: any = {};
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
- import { getCookie } from "./getCookie";
2
- import { getChromeCookie } from "./getChromeCookie";
3
- import { getFirefoxCookie } from "./getFirefoxCookie";
4
- import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
5
- import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
6
- import { fetchWithCookies } from "./fetchWithCookies";
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 JwtPayload;
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
- return sync(`./**/Cookies`, {
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
- return await flatMapAsync(paths, async (p) => {
23
- const content = await readFile(`${p}/Preferences`, "utf-8");
24
- return await destr(content);
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
@@ -8,6 +8,5 @@ const consola = createConsola({
8
8
  date: false,
9
9
  },
10
10
  });
11
- consola.wrapConsole();
12
11
 
13
12
  export default consola;
@@ -7,20 +7,23 @@ export function processBeforeReturn<T extends CookieQueryStrategy>(
7
7
  cookies: ExportedCookie[],
8
8
  options?: CookieQueryOptions<T>,
9
9
  ): ExportedCookie[] {
10
- if (options?.removeExpired) {
11
- return cookies.filter((c: ExportedCookie) => {
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
- if (expiry === undefined) {
14
- return true;
15
- } else if (expiry === "Infinity") {
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
- if (options?.limit) {
23
- return cookies.slice(0, options.limit);
21
+
22
+ if (options && options.limit) {
23
+ processedCookies = processedCookies.slice(0, options.limit);
24
24
  }
25
- return uniqBy(cookies, JSON.stringify);
25
+
26
+ return uniqBy(processedCookies, (cookie: ExportedCookie) =>
27
+ JSON.stringify(cookie),
28
+ );
26
29
  }