@mherod/get-cookie 2.0.0-rc.8 → 2.1.0

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 (77) hide show
  1. package/.idea/runConfigurations/build.xml +12 -0
  2. package/.parcelrc +12 -0
  3. package/.prettierignore +5 -0
  4. package/.prettierrc +12 -0
  5. package/.terserrc +26 -0
  6. package/README.md +2 -2
  7. package/build-indexes.ts +108 -0
  8. package/dist/cli.js +2 -0
  9. package/dist/index.js +1 -823
  10. package/dist/index.js.map +1 -0
  11. package/dist/module.js +1446 -0
  12. package/dist/module.js.map +1 -0
  13. package/dist/prompt.2b8c61c0.js +40 -0
  14. package/dist/prompt.536a2c51.js +40 -0
  15. package/dist/prompt.fb2c7dad.js.map +1 -0
  16. package/dist/types.d.ts +30 -0
  17. package/dist/types.d.ts.map +1 -0
  18. package/jest.config.ts +14 -0
  19. package/package-lock.json +3862 -8098
  20. package/package.json +49 -35
  21. package/src/CookieRow.ts +8 -0
  22. package/src/CookieSpec.ts +20 -0
  23. package/src/CookieStore.ts +24 -7
  24. package/src/ExportedCookie.ts +12 -0
  25. package/src/FetchResponse.ts +1 -3
  26. package/src/FileCookieStore.ts +178 -0
  27. package/src/StringToRegex.ts +10 -0
  28. package/src/argv.ts +27 -2
  29. package/src/browsers/CompositeCookieQueryStrategy.ts +36 -38
  30. package/src/browsers/CookieQueryStrategy.ts +1 -0
  31. package/src/browsers/CookieStoreQueryStrategy.ts +29 -22
  32. package/src/browsers/QuerySqliteThenTransform.ts +85 -0
  33. package/src/browsers/chrome/ChromeApplicationSupport.ts +10 -0
  34. package/src/browsers/chrome/ChromeCookieQueryStrategy.ts +131 -0
  35. package/src/browsers/chrome/decrypt.ts +118 -0
  36. package/src/browsers/chrome/getChromePassword.ts +11 -0
  37. package/src/browsers/{FirefoxCookieQueryStrategy.ts → firefox/FirefoxCookieQueryStrategy.ts} +19 -16
  38. package/src/browsers/getEncryptedChromeCookie.ts +87 -0
  39. package/src/browsers/index.ts +12 -0
  40. package/src/browsers/mock/MockCookieQueryStrategy.ts +18 -0
  41. package/src/browsers/{SafariCookieQueryStrategy.ts → safari/SafariCookieQueryStrategy.ts} +3 -2
  42. package/src/cli.ts +43 -67
  43. package/src/cliQueryCookies.ts +55 -0
  44. package/src/comboQueryCookieSpec.test.ts +92 -0
  45. package/src/comboQueryCookieSpec.ts +29 -0
  46. package/src/cookieQueryOptions.ts +20 -0
  47. package/src/cookieSpecsFromUrl.test.ts +65 -0
  48. package/src/cookieSpecsFromUrl.ts +27 -0
  49. package/src/execSimple.ts +13 -0
  50. package/src/fetchWithCookies.test.ts +32 -0
  51. package/src/fetchWithCookies.ts +124 -47
  52. package/src/findAllFiles.ts +25 -49
  53. package/src/getChromeCookie.ts +6 -4
  54. package/src/getCookie.ts +6 -3
  55. package/src/getFirefoxCookie.ts +6 -4
  56. package/src/getGroupedRenderedCookies.ts +7 -17
  57. package/src/getMergedRenderedCookies.test.ts +43 -0
  58. package/src/getMergedRenderedCookies.ts +17 -0
  59. package/src/global.ts +1 -1
  60. package/src/index.ts +2 -7
  61. package/src/isValidJwt.ts +2 -2
  62. package/src/listChromeProfiles.ts +45 -0
  63. package/src/logger.ts +13 -0
  64. package/src/processBeforeReturn.ts +26 -0
  65. package/src/queryCookies.ts +11 -3
  66. package/src/util/flatMapAsync.test.ts +55 -0
  67. package/src/util/flatMapAsync.ts +37 -0
  68. package/src/util/index.ts +1 -0
  69. package/tsconfig.json +14 -11
  70. package/.github/dependabot.yml +0 -6
  71. package/.prettierrc.json +0 -1
  72. package/src/IsCookieRow.ts +0 -9
  73. package/src/IsExportedCookie.ts +0 -9
  74. package/src/browsers/ChromeCookieQueryStrategy.ts +0 -343
  75. package/src/doSqliteQuery1.ts +0 -47
  76. package/src/doSqliteQuery1Params.ts +0 -7
  77. package/src/utils.ts +0 -25
@@ -1,93 +1,170 @@
1
- // noinspection JSUnusedGlobalSymbols
1
+ // noinspection JSUnusedGlobalSymbols,ExceptionCaughtLocallyJS
2
2
 
3
3
  import { fetch as fetchImpl } from "cross-fetch";
4
4
  import { merge } from "lodash";
5
5
  // noinspection SpellCheckingInspection
6
6
  import destr from "destr";
7
- import CookieSpec from "./CookieSpec";
8
- import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
9
- import { cookieJar } from "./CookieStore";
10
- import UserAgent from "user-agents";
11
7
  import { parsedArgs } from "./argv";
12
- import { blue, yellow } from "colorette";
8
+ import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
9
+ import { cookieSpecsFromUrl } from "./cookieSpecsFromUrl";
10
+ import CookieSpec from "./CookieSpec";
11
+ import consola from "consola";
12
+
13
+ if (typeof fetchImpl !== "function") {
14
+ throw new Error("fetch is not a function");
15
+ }
16
+
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(" ");
24
+ }
25
+
26
+ const userAgent = constructUserAgent();
27
+
28
+ interface FetchRequestInit {
29
+ url: RequestInfo | URL | string;
30
+ options?: RequestInit;
31
+ }
32
+
33
+ export type FetchFn =
34
+ | typeof fetchImpl
35
+ | ((url: URL, options?: RequestInit) => Promise<Response>);
13
36
 
14
37
  export async function fetchWithCookies(
15
- url: RequestInfo | URL,
38
+ url: RequestInfo | URL | string,
16
39
  options: RequestInit | undefined = {},
17
- fetch: Function = fetchImpl
40
+ fetch: FetchFn = fetchImpl as FetchFn,
41
+ originalRequest?: FetchRequestInit,
18
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
+ };
19
52
  const defaultOptions: RequestInit = {
20
- headers: {
21
- "User-Agent": new UserAgent().toString(),
22
- },
53
+ headers,
23
54
  redirect: "manual",
24
55
  };
25
56
  const url2: string = `${url}`;
26
57
  const url1: URL = new URL(url2);
27
- const domain = url1.hostname.replace(/^.*(\.\w+\.\w+)$/, (match, p1) => {
28
- return `%${p1}`;
29
- });
30
- const cookieSpec: CookieSpec = {
31
- name: "%",
32
- domain: domain,
33
- };
34
- const cookies: string[] = await getGroupedRenderedCookies(cookieSpec).catch(
35
- () => []
36
- );
37
- const cookie = cookies.pop();
38
- const newOptions1: RequestInit = merge(defaultOptions, options, {
39
- headers: {
40
- Cookie: cookie,
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 "";
41
64
  },
42
- });
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);
43
76
  try {
44
- const res: Response = await fetch(url2, newOptions1);
77
+ const res: Response = await fetch(url1, newOptions1);
78
+ // noinspection JSMismatchedCollectionQueryUpdate
45
79
  const headers: [string, string][] = [];
46
80
  res.headers.forEach((value, key) => {
47
81
  headers.push([key, value]);
48
82
  });
49
- for (const [key, value] of headers) {
50
- if (key === "set-cookie") {
51
- await cookieJar.setCookie(value, url2);
52
- if (parsedArgs.verbose) {
53
- console.log(blue(`Set-Cookie: ${yellow(value)} ${yellow(url2)}`));
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
+ // }
92
+
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);
54
100
  }
55
- // const cookie = tough.parse(value);
56
- // if (cookie instanceof Cookie) {
57
- // await memoryCookieStore.putCookie(cookie);
58
- // }
101
+ return fetchWithCookies(
102
+ //
103
+ newUrl,
104
+ newOptions1,
105
+ fetch,
106
+ originalRequest1,
107
+ //
108
+ );
59
109
  }
60
110
  }
61
-
62
- const newUrl: string = res.headers.get("location") as string;
63
- if (res.redirected || (newUrl && newUrl !== url2)) {
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
+ }
64
130
  if (parsedArgs.verbose) {
65
- console.log(blue(`Redirected to `), yellow(newUrl));
131
+ console.log(`Redirected to `, newUrl);
66
132
  }
67
- return fetchWithCookies(newUrl, newOptions1);
133
+ return fetchWithCookies(
134
+ //
135
+ newUrl,
136
+ newOptions2,
137
+ fetch,
138
+ originalRequest1,
139
+ //
140
+ );
68
141
  }
69
142
 
70
- const arrayBuffer1: Promise<ArrayBuffer> = res.arrayBuffer();
143
+ // all deserialize functions will use the arraybuffer
144
+ const arrayBufferPromise: Promise<ArrayBuffer> = res.arrayBuffer();
145
+ const bufferPromise = arrayBufferPromise.then(Buffer.from);
71
146
 
72
147
  async function arrayBuffer(): Promise<ArrayBuffer> {
73
- return arrayBuffer1;
148
+ return arrayBufferPromise;
74
149
  }
75
150
 
76
151
  async function buffer(): Promise<Buffer> {
77
- return arrayBuffer().then(Buffer.from);
152
+ return bufferPromise;
78
153
  }
79
154
 
80
155
  async function text(): Promise<string> {
81
- return buffer().then((buffer) => buffer.toString("utf8"));
156
+ const buffer1 = await bufferPromise;
157
+ return buffer1.toString("utf8");
82
158
  }
83
159
 
84
160
  async function json(): Promise<any> {
85
- return text().then((text) => destr(text));
161
+ const text1 = await text();
162
+ return destr(text1);
86
163
  }
87
164
 
88
165
  async function formData(): Promise<FormData> {
89
166
  const urlSearchParams: URLSearchParams = await text().then(
90
- (text) => new URLSearchParams(text)
167
+ (text) => new URLSearchParams(text),
91
168
  );
92
169
  const formData = new FormData();
93
170
  for (const [key, value] of urlSearchParams.entries()) {
@@ -1,62 +1,38 @@
1
- import * as fs from "fs";
1
+ import { existsSync } from "fs";
2
2
  import { parsedArgs } from "./argv";
3
+ import consola from "consola";
4
+ import { sync } from "fast-glob";
3
5
 
4
- export async function findAllFiles({
5
- path,
6
- name,
7
- maxDepth = 2,
8
- }: //
9
- {
6
+ type FindFilesOptions = {
10
7
  path: string;
11
8
  name: string;
12
9
  maxDepth?: number;
13
- }): Promise<string[]> {
14
- const rootSegments = path.split("/").length;
15
- const files: string[] = [];
16
- let readdirSync;
17
- try {
18
- readdirSync = fs.readdirSync(path);
19
- } catch (e) {
20
- if (parsedArgs.verbose) {
21
- console.log(`Error reading ${path}`, e);
22
- }
23
- return [];
10
+ };
11
+
12
+ export function findAllFiles({
13
+ path,
14
+ name,
15
+ maxDepth = 2,
16
+ }: FindFilesOptions): string[] {
17
+ if (!existsSync(path)) {
18
+ throw new Error(`Path ${path} does not exist`);
24
19
  }
25
- for (const file of readdirSync) {
26
- const filePath = path + "/" + file;
27
- let stat;
28
- try {
29
- stat = fs.statSync(filePath);
30
- } catch (e) {
31
- if (parsedArgs.verbose) {
32
- console.error(`Error getting stat for ${filePath}`, e);
33
- }
34
- continue;
35
- }
36
- if (stat.isDirectory()) {
37
- if (filePath.split("/").length < rootSegments + maxDepth) {
38
- try {
39
- const subFiles = await findAllFiles({
40
- path: filePath,
41
- name: name,
42
- maxDepth: 2,
43
- });
44
- files.push(...subFiles);
45
- } catch (e) {
46
- if (parsedArgs.verbose) {
47
- console.error(e);
48
- }
49
- }
50
- }
51
- } else if (file === name) {
52
- files.push(filePath);
53
- }
20
+
21
+ if (parsedArgs.verbose) {
22
+ consola.start(`Searching for ${name} files in ${path}`);
54
23
  }
24
+
25
+ const files: string[] = sync(`${path}/**/${name}`, {
26
+ onlyFiles: true,
27
+ deep: maxDepth,
28
+ });
29
+
55
30
  if (parsedArgs.verbose) {
56
31
  if (files.length > 0) {
57
- console.log(`Found ${files.length} ${name} files`);
58
- console.log(files);
32
+ consola.success(`Found ${files.length} ${name} files`);
33
+ consola.info(files);
59
34
  }
60
35
  }
36
+
61
37
  return files;
62
38
  }
@@ -1,15 +1,17 @@
1
1
  import CookieSpec from "./CookieSpec";
2
2
  import ExportedCookie from "./ExportedCookie";
3
3
  import { queryCookies } from "./queryCookies";
4
- import ChromeCookieQueryStrategy from "./browsers/ChromeCookieQueryStrategy";
5
- import { isExportedCookie } from "./IsExportedCookie";
4
+ import ChromeCookieQueryStrategy from "./browsers/chrome/ChromeCookieQueryStrategy";
5
+ import { isExportedCookie } from "./ExportedCookie";
6
6
 
7
7
  export async function getChromeCookie(
8
- params: CookieSpec
8
+ params: CookieSpec,
9
9
  ): Promise<ExportedCookie | undefined> {
10
10
  const cookies = await queryCookies(
11
11
  params,
12
- new ChromeCookieQueryStrategy()
12
+ {
13
+ strategy: new ChromeCookieQueryStrategy(),
14
+ },
13
15
  //
14
16
  );
15
17
  if (cookies.length == 0) {
package/src/getCookie.ts CHANGED
@@ -4,11 +4,14 @@ import { queryCookies } from "./queryCookies";
4
4
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
5
5
 
6
6
  export async function getCookie(
7
- params: CookieSpec
7
+ params: CookieSpec,
8
8
  ): Promise<ExportedCookie | undefined> {
9
- const cookies = await queryCookies(
9
+ //
10
+ const cookies: ExportedCookie[] = await queryCookies(
10
11
  params,
11
- new CompositeCookieQueryStrategy()
12
+ {
13
+ strategy: new CompositeCookieQueryStrategy(),
14
+ },
12
15
  //
13
16
  );
14
17
  if (Array.isArray(cookies) && cookies.length > 0) {
@@ -1,14 +1,16 @@
1
1
  import CookieSpec from "./CookieSpec";
2
2
  import ExportedCookie from "./ExportedCookie";
3
3
  import { queryCookies } from "./queryCookies";
4
- import FirefoxCookieQueryStrategy from "./browsers/FirefoxCookieQueryStrategy";
4
+ import FirefoxCookieQueryStrategy from "./browsers/firefox/FirefoxCookieQueryStrategy";
5
5
 
6
6
  export async function getFirefoxCookie(
7
- params: CookieSpec
7
+ params: CookieSpec,
8
8
  ): Promise<ExportedCookie | undefined> {
9
- const cookies = await queryCookies(
9
+ const cookies: ExportedCookie[] = await queryCookies(
10
10
  params,
11
- new FirefoxCookieQueryStrategy()
11
+ {
12
+ strategy: new FirefoxCookieQueryStrategy(),
13
+ },
12
14
  //
13
15
  );
14
16
  if (Array.isArray(cookies) && cookies.length > 0) {
@@ -1,27 +1,17 @@
1
1
  import { groupBy } from "lodash";
2
- import { queryCookies } from "./queryCookies";
3
2
  import { resultsRendered } from "./resultsRendered";
4
- import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
5
- import CookieSpec from "./CookieSpec";
3
+ import { MultiCookieSpec } from "./CookieSpec";
6
4
  import ExportedCookie from "./ExportedCookie";
5
+ import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
7
6
 
8
- export async function getGroupedRenderedCookies({
9
- name,
10
- domain,
11
- }: CookieSpec): Promise<string[]> {
12
- const cookies: ExportedCookie[] = await queryCookies(
13
- {
14
- name,
15
- domain,
16
- },
17
- new CompositeCookieQueryStrategy()
18
- //
19
- );
7
+ export async function getGroupedRenderedCookies(
8
+ cookieSpec: MultiCookieSpec,
9
+ ): Promise<string[]> {
10
+ const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec);
20
11
  if (cookies.length == 0) {
21
12
  throw new Error("Cookie not found");
22
13
  }
23
- const results: ExportedCookie[] = await queryCookies({ name, domain });
24
- const groupedByFile = groupBy(results, (r: ExportedCookie) => r.meta?.file);
14
+ const groupedByFile = groupBy(cookies, (r: ExportedCookie) => r.meta?.file);
25
15
  return Object.keys(groupedByFile).map((file: string) => {
26
16
  const results: ExportedCookie[] = groupedByFile[file];
27
17
  return resultsRendered(results);
@@ -0,0 +1,43 @@
1
+ import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
2
+ import { MultiCookieSpec } from "./CookieSpec";
3
+ import MockCookieQueryStrategy from "./browsers/mock/MockCookieQueryStrategy";
4
+
5
+ describe("getMergedRenderedCookies", () => {
6
+ it("should return empty string if no cookies are present", async () => {
7
+ const cookieSpec: MultiCookieSpec = [];
8
+ const result = await getMergedRenderedCookies(cookieSpec);
9
+ expect(result).toBe("");
10
+ });
11
+
12
+ it("should return rendered cookies if cookies are present", async () => {
13
+ const cookieSpec: MultiCookieSpec = [
14
+ {
15
+ name: "sessionCookie",
16
+ domain: "www.example.com",
17
+ },
18
+ {
19
+ name: "persistentCookie",
20
+ domain: "www.example.com",
21
+ },
22
+ ];
23
+ const result = await getMergedRenderedCookies(
24
+ cookieSpec,
25
+ new MockCookieQueryStrategy([
26
+ {
27
+ name: "sessionCookie",
28
+ value: "sessionCookieValue",
29
+ domain: "www.example.com",
30
+ },
31
+ {
32
+ name: "persistentCookie",
33
+ value: "persistentCookieValue",
34
+ domain: "www.example.com",
35
+ },
36
+ ]),
37
+ //
38
+ );
39
+ expect(result).not.toBe("");
40
+ expect(result).toContain("sessionCookie=sessionCookieValue");
41
+ expect(result).toContain("persistentCookie=persistentCookieValue");
42
+ });
43
+ });
@@ -0,0 +1,17 @@
1
+ import { resultsRendered } from "./resultsRendered";
2
+ import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
3
+ import { MultiCookieSpec } from "./CookieSpec";
4
+ import ExportedCookie from "./ExportedCookie";
5
+ import consola from "consola";
6
+ import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
7
+ import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
8
+
9
+ export async function getMergedRenderedCookies(
10
+ cookieSpec: MultiCookieSpec,
11
+ strategy: CookieQueryStrategy = new CompositeCookieQueryStrategy(),
12
+ ): Promise<string> {
13
+ const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec, {
14
+ strategy,
15
+ });
16
+ return cookies.length > 0 ? resultsRendered(cookies) : "";
17
+ }
package/src/global.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { merge } from "lodash";
2
2
 
3
3
  export const env: any = {};
4
- merge(env, process.env);
4
+ merge(env, process?.env ?? {});
5
5
  export const HOME: string = env["HOME"];
6
6
  if (!HOME) {
7
7
  throw new Error("HOME environment variable is not set");
package/src/index.ts CHANGED
@@ -1,21 +1,16 @@
1
- #!/usr/bin/env node
2
- // noinspection JSUnusedGlobalSymbols
3
-
4
1
  import { getCookie } from "./getCookie";
5
2
  import { getChromeCookie } from "./getChromeCookie";
6
3
  import { getFirefoxCookie } from "./getFirefoxCookie";
7
4
  import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
5
+ import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
8
6
  import { fetchWithCookies } from "./fetchWithCookies";
9
7
 
10
8
  export {
11
9
  getCookie,
12
10
  getChromeCookie,
13
11
  getFirefoxCookie,
12
+ getMergedRenderedCookies,
14
13
  getGroupedRenderedCookies,
15
14
  fetchWithCookies,
16
15
  //
17
16
  };
18
-
19
- export * from "./CookieSpec";
20
- export * from "./CookieRow";
21
- export * from "./ExportedCookie";
package/src/isValidJwt.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import jsonwebtoken, { JwtPayload } from "jsonwebtoken";
2
2
  import { parsedArgs } from "./argv";
3
3
 
4
- export default function isValidJwt(token: any) {
4
+ export default function isValidJwt(token: string) {
5
5
  try {
6
6
  const result = jsonwebtoken.decode(token, { complete: true });
7
7
  if (parsedArgs.verbose && result) {
8
- console.log(result);
8
+ console.debug(result);
9
9
  }
10
10
  const payload: JwtPayload = result?.payload as JwtPayload;
11
11
  if (payload) {
@@ -0,0 +1,45 @@
1
+ import { sync } from "fast-glob";
2
+ import { chromeApplicationSupport } from "./browsers/chrome/ChromeApplicationSupport";
3
+ import { dirname } from "path";
4
+ import { readFile } from "fs/promises";
5
+ import { flatMapAsync } from "./util/flatMapAsync";
6
+ import destr from "destr";
7
+
8
+ export async function listChromeProfilePaths(): Promise<string[]> {
9
+ return sync(`./**/Cookies`, {
10
+ cwd: chromeApplicationSupport,
11
+ absolute: true,
12
+ onlyFiles: true,
13
+ deep: 2,
14
+ }).map((f) => {
15
+ // parent dir
16
+ return dirname(f);
17
+ });
18
+ }
19
+
20
+ 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
+ });
26
+ }
27
+
28
+ export type ChromeProfileAccountInfo = {
29
+ account_id: string;
30
+ accountcapabilities: any;
31
+ email: string;
32
+ full_name: string;
33
+ gaia: string;
34
+ given_name: string;
35
+ hd: string;
36
+ is_supervised_child: number;
37
+ is_under_advanced_protection: boolean;
38
+ last_downloaded_image_url_with_size: string;
39
+ locale: string;
40
+ picture_url: string;
41
+ };
42
+
43
+ export type ChromeProfile = {
44
+ account_info: ChromeProfileAccountInfo[];
45
+ };
package/src/logger.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { createConsola } from "consola";
2
+
3
+ const consola = createConsola({
4
+ fancy: true,
5
+ formatOptions: {
6
+ showLogLevel: true,
7
+ colors: true,
8
+ date: false,
9
+ },
10
+ });
11
+ consola.wrapConsole();
12
+
13
+ export default consola;
@@ -0,0 +1,26 @@
1
+ import ExportedCookie from "./ExportedCookie";
2
+ import { uniqBy } from "lodash";
3
+ import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
4
+ import { CookieQueryOptions } from "./cookieQueryOptions";
5
+
6
+ export function processBeforeReturn<T extends CookieQueryStrategy>(
7
+ cookies: ExportedCookie[],
8
+ options?: CookieQueryOptions<T>,
9
+ ): ExportedCookie[] {
10
+ if (options?.removeExpired) {
11
+ return cookies.filter((c: ExportedCookie) => {
12
+ 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
+ }
20
+ });
21
+ }
22
+ if (options?.limit) {
23
+ return cookies.slice(0, options.limit);
24
+ }
25
+ return uniqBy(cookies, JSON.stringify);
26
+ }
@@ -3,13 +3,21 @@ import { parsedArgs } from "./argv";
3
3
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
4
4
  import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
5
5
  import isValidJwt from "./isValidJwt";
6
- import CookieSpec from "./CookieSpec";
6
+ import CookieSpec, { MultiCookieSpec } from "./CookieSpec";
7
7
  import ExportedCookie from "./ExportedCookie";
8
+ import { CookieQueryOptions } from "./cookieQueryOptions";
8
9
 
9
10
  export async function queryCookies(
10
- { name, domain }: CookieSpec,
11
- strategy: CookieQueryStrategy = new CompositeCookieQueryStrategy()
11
+ {
12
+ name,
13
+ domain,
14
+ }: //
15
+ CookieSpec,
16
+ options?: CookieQueryOptions<CookieQueryStrategy>,
12
17
  ): Promise<ExportedCookie[]> {
18
+ const strategy: CookieQueryStrategy =
19
+ options?.strategy || new CompositeCookieQueryStrategy();
20
+ //
13
21
  const results: ExportedCookie[] = await strategy.queryCookies(name, domain);
14
22
  const allCookies: ExportedCookie[] = uniqBy(results, JSON.stringify);
15
23