@mherod/get-cookie 2.0.0-beta.1 → 2.0.0-beta.10

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 (45) hide show
  1. package/.idea/codeStyles/Project.xml +59 -0
  2. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  3. package/.idea/get-cookie.iml +1 -0
  4. package/.prettierrc.json +1 -0
  5. package/dist/cli.js +1 -1
  6. package/dist/cli.js.map +1 -1
  7. package/dist/index.js +563 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/module.js +533 -0
  10. package/dist/module.js.map +1 -0
  11. package/dist/types.d.ts +35 -0
  12. package/dist/types.d.ts.map +1 -0
  13. package/package.json +37 -13
  14. package/src/CookieRequest.ts +4 -0
  15. package/src/CookieRow.ts +6 -0
  16. package/src/ExportedCookie.ts +6 -0
  17. package/src/IsCookieRow.ts +5 -0
  18. package/src/IsExportedCookie.ts +5 -0
  19. package/src/argv.ts +1 -0
  20. package/src/browsers/ChromeCookieQueryStrategy.ts +259 -0
  21. package/src/browsers/{CompositeCookieQueryStrategy.js → CompositeCookieQueryStrategy.ts} +7 -5
  22. package/src/browsers/CookieQueryStrategy.ts +5 -0
  23. package/src/browsers/{FirefoxCookieQueryStrategy.js → FirefoxCookieQueryStrategy.ts} +30 -15
  24. package/src/browsers/SafariCookieQueryStrategy.ts +8 -0
  25. package/src/cli.ts +82 -0
  26. package/src/doSqliteQuery1.ts +41 -0
  27. package/src/fetchResponse.ts +11 -0
  28. package/src/fetchWithCookies.ts +62 -0
  29. package/src/{findAllFiles.js → findAllFiles.ts} +15 -20
  30. package/src/getGroupedRenderedCookies.ts +32 -0
  31. package/src/{global.js → global.ts} +2 -2
  32. package/src/{index.js → index.ts} +16 -18
  33. package/src/{isValidJwt.js → isValidJwt.ts} +1 -4
  34. package/src/queryCookies.ts +27 -0
  35. package/src/resultsRendered.ts +8 -0
  36. package/src/utils.ts +56 -0
  37. package/tsconfig.json +23 -0
  38. package/dist/main.js +0 -499
  39. package/dist/main.js.map +0 -1
  40. package/src/browsers/AbstractCookieQueryStrategy.js +0 -8
  41. package/src/browsers/ChromeCookieQueryStrategy.js +0 -263
  42. package/src/browsers/SafariCookieQueryStrategy.js +0 -3
  43. package/src/cli.js +0 -70
  44. package/src/queryCookies.js +0 -28
  45. package/src/utils.js +0 -181
@@ -0,0 +1,62 @@
1
+ // noinspection JSUnusedGlobalSymbols
2
+
3
+ import { fetch } from "cross-fetch";
4
+ import { merge } from "lodash";
5
+ // noinspection SpellCheckingInspection
6
+ import destr from "destr";
7
+ import FetchResponse from "./fetchResponse";
8
+ import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
9
+
10
+ export async function fetchWithCookies(
11
+ url: RequestInfo | URL,
12
+ options: RequestInit | undefined = {}
13
+ ): Promise<FetchResponse> {
14
+ const defaultOptions: RequestInit = {
15
+ headers: {
16
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
17
+ },
18
+ redirect: "manual"
19
+ };
20
+ const url2: string = `${url}`;
21
+ const url1: URL = new URL(url2);
22
+ const domain = url1.hostname.replace(/^.*(\.\w+\.\w+)$/, (match, p1) => {
23
+ return `%${p1}`;
24
+ });
25
+ const cookies: string[] = await getGroupedRenderedCookies({
26
+ name: "%",
27
+ domain: domain
28
+ });
29
+ const cookie = cookies.pop();
30
+ const newOptions1: RequestInit = merge(defaultOptions, options, {
31
+ headers: {
32
+ Cookie: cookie
33
+ }
34
+ });
35
+ try {
36
+ const res = await fetch(url2, newOptions1);
37
+ const newUrl = res.headers.get("location") as string;
38
+ if (res.redirected || newUrl && newUrl !== url2) {
39
+ return fetchWithCookies(newUrl, newOptions1);
40
+ }
41
+ const arrayBuffer1 = res.arrayBuffer();
42
+ const arrayBuffer = async () => arrayBuffer1;
43
+ const buffer = async () => arrayBuffer().then(Buffer.from);
44
+ const text = async () => buffer().then((buffer) => buffer.toString("utf8"));
45
+ const json = async () => text().then(destr);
46
+ const formData = async () => text().then((text) => new URLSearchParams(text));
47
+ const source2 = {
48
+ status: res.status,
49
+ statusText: res.statusText,
50
+ headers: res.headers,
51
+ arrayBuffer,
52
+ buffer,
53
+ text,
54
+ json,
55
+ formData
56
+ //
57
+ };
58
+ return merge({}, res, source2);
59
+ } catch (e) {
60
+ throw e;
61
+ }
62
+ }
@@ -1,28 +1,24 @@
1
1
  import { env } from "./global";
2
- import fs from "fs";
2
+ import * as fs from "fs";
3
3
 
4
- /**
5
- *
6
- * @param path
7
- * @param name
8
- * @param rootSegments
9
- * @param maxDepth
10
- * @returns {Promise<[string]>}
11
- */
12
- export async function findAllFiles({ path, name, maxDepth = 2 }) {
13
- if (typeof path !== "string") {
14
- throw new Error("path must be a string");
4
+ export async function findAllFiles(
5
+ {
6
+ path,
7
+ name,
8
+ maxDepth = 2
9
+ //
10
+ }: {
11
+ path: string;
12
+ name: string;
13
+ maxDepth?: number;
15
14
  }
16
- if (typeof name !== "string") {
17
- throw new Error("name must be a string");
18
- }
19
-
15
+ ): Promise<string[]> {
20
16
  const rootSegments = path.split("/").length;
21
17
 
22
18
  if (env.VERBOSE) {
23
19
  console.log(`Searching for ${name} in ${path}`);
24
20
  }
25
- const files = [];
21
+ const files: string[] = [];
26
22
  let readdirSync;
27
23
  try {
28
24
  readdirSync = fs.readdirSync(path);
@@ -30,7 +26,7 @@ export async function findAllFiles({ path, name, maxDepth = 2 }) {
30
26
  if (env.VERBOSE) {
31
27
  console.log(`Error reading ${path}`, e);
32
28
  }
33
- return files;
29
+ return [];
34
30
  }
35
31
  for (const file of readdirSync) {
36
32
  const filePath = path + "/" + file;
@@ -49,8 +45,7 @@ export async function findAllFiles({ path, name, maxDepth = 2 }) {
49
45
  const subFiles = await findAllFiles({
50
46
  path: filePath,
51
47
  name: name,
52
- rootSegments: rootSegments,
53
- maxDepth: 2,
48
+ maxDepth: 2
54
49
  });
55
50
  files.push(...subFiles);
56
51
  } catch (e) {
@@ -0,0 +1,32 @@
1
+ import { queryCookies } from "./queryCookies";
2
+ import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
3
+ import { groupBy } from "lodash";
4
+ import { resultsRendered } from "./resultsRendered";
5
+ import { CookieRequest } from "./CookieRequest";
6
+ import { ExportedCookie } from "./ExportedCookie";
7
+
8
+ export async function getGroupedRenderedCookies(
9
+ //
10
+ {
11
+ name,
12
+ domain
13
+ //
14
+ }: CookieRequest
15
+ //
16
+ ): Promise<string[]> {
17
+ const cookies: ExportedCookie[] = await queryCookies(
18
+ { name, domain },
19
+ new CompositeCookieQueryStrategy()
20
+ //
21
+ );
22
+ if (Array.isArray(cookies) && cookies.length > 0) {
23
+ const results: ExportedCookie[] = await queryCookies({ name, domain });
24
+ const groupedByFile = groupBy(results, (r) => r.meta?.file);
25
+ return Object.keys(groupedByFile).map((file: string) => {
26
+ const results: ExportedCookie[] = groupedByFile[file];
27
+ return resultsRendered(results);
28
+ });
29
+ } else {
30
+ throw new Error("Cookie not found");
31
+ }
32
+ }
@@ -1,7 +1,7 @@
1
1
  const { merge } = require("lodash");
2
- export const env = {};
2
+ export const env: any = {};
3
3
  merge(env, process.env);
4
- export const HOME = env["HOME"];
4
+ export const HOME: string = env["HOME"];
5
5
  if (!HOME) {
6
6
  throw new Error("HOME environment variable is not set");
7
7
  }
@@ -5,13 +5,12 @@ import { queryCookies } from "./queryCookies";
5
5
  import FirefoxCookieQueryStrategy from "./browsers/FirefoxCookieQueryStrategy";
6
6
  import ChromeCookieQueryStrategy from "./browsers/ChromeCookieQueryStrategy";
7
7
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
8
+ import { CookieRequest } from "./CookieRequest";
9
+ import { ExportedCookie } from "./ExportedCookie";
10
+ import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
11
+ import { fetchWithCookies } from "./fetchWithCookies";
8
12
 
9
- /**
10
- *
11
- * @param params
12
- * @returns {Promise<string>}
13
- */
14
- export async function getCookie(params) {
13
+ export async function getCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
15
14
  const cookies = await queryCookies(
16
15
  params,
17
16
  new CompositeCookieQueryStrategy()
@@ -24,12 +23,7 @@ export async function getCookie(params) {
24
23
  }
25
24
  }
26
25
 
27
- /**
28
- *
29
- * @param params
30
- * @returns {Promise<*>}
31
- */
32
- export async function getFirefoxCookie(params) {
26
+ export async function getFirefoxCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
33
27
  const cookies = await queryCookies(
34
28
  params,
35
29
  new FirefoxCookieQueryStrategy()
@@ -42,12 +36,7 @@ export async function getFirefoxCookie(params) {
42
36
  }
43
37
  }
44
38
 
45
- /**
46
- *
47
- * @param params
48
- * @returns {Promise<*>}
49
- */
50
- export async function getChromeCookie(params) {
39
+ export async function getChromeCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
51
40
  const cookies = await queryCookies(
52
41
  params,
53
42
  new ChromeCookieQueryStrategy()
@@ -59,3 +48,12 @@ export async function getChromeCookie(params) {
59
48
  throw new Error("Cookie not found");
60
49
  }
61
50
  }
51
+
52
+ export {
53
+ getGroupedRenderedCookies,
54
+ fetchWithCookies,
55
+ };
56
+
57
+ export * from "./CookieRequest";
58
+ export * from "./CookieRow";
59
+ export * from "./ExportedCookie";
@@ -1,10 +1,7 @@
1
1
  import jsonwebtoken from "jsonwebtoken";
2
2
  import { env } from "./global";
3
3
 
4
- export default function isValidJwt(token) {
5
- if (typeof token !== "string") {
6
- return false;
7
- }
4
+ export default function isValidJwt(token: any) {
8
5
  try {
9
6
  const result = jsonwebtoken.decode(token, { complete: true });
10
7
  if (env.VERBOSE) {
@@ -0,0 +1,27 @@
1
+ import { env } from "./global";
2
+ import { uniqBy } from "lodash";
3
+ import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
4
+ import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
5
+ import isValidJwt from "./isValidJwt";
6
+ import { CookieRequest } from "./CookieRequest";
7
+ import { ExportedCookie } from "./ExportedCookie";
8
+
9
+ export async function queryCookies(
10
+ {
11
+ name,
12
+ domain
13
+ }: CookieRequest,
14
+ strategy: CookieQueryStrategy = new CompositeCookieQueryStrategy()
15
+ ) {
16
+ const results: ExportedCookie[] = await strategy.queryCookies(name, domain);
17
+ const results1: ExportedCookie[] = uniqBy(results, JSON.stringify);
18
+ const jwtCookies = [];
19
+ for (const result of results1) {
20
+ const value = result.value;
21
+ if (isValidJwt(value)) {
22
+ jwtCookies.push(result);
23
+ }
24
+ }
25
+ const resultsUniq = env.REQUIRE_JWT ? jwtCookies : results1;
26
+ return env.SINGLE ? [resultsUniq[0]] : resultsUniq;
27
+ }
@@ -0,0 +1,8 @@
1
+ import { uniqBy } from "lodash";
2
+ import { ExportedCookie } from "./ExportedCookie";
3
+
4
+ export function resultsRendered(results: ExportedCookie[]) {
5
+ return uniqBy(results, (r) => r.name)
6
+ .map((r) => r.name + "=" + r.value)
7
+ .join("; ");
8
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,56 @@
1
+ // noinspection JSUnusedGlobalSymbols
2
+
3
+ import {exec, ExecException} from "child_process";
4
+
5
+ export async function execSimple(command: string): Promise<string> {
6
+ return new Promise((resolve, reject) => {
7
+ exec(
8
+ command,
9
+ { encoding: "binary", maxBuffer: 5 * 1024 },
10
+ (error: ExecException | null, stdout: string, stderr: string) => {
11
+ if (error) {
12
+ reject(error);
13
+ return;
14
+ }
15
+ if (stderr) {
16
+ reject(error);
17
+ return;
18
+ }
19
+ if (stdout) {
20
+ resolve(stdout.trim());
21
+ }
22
+ }
23
+ );
24
+ });
25
+ }
26
+
27
+ /**
28
+ *
29
+ * @param result
30
+ * @returns {string|null}
31
+ */
32
+ export function toStringOrNull(result: any) {
33
+ if (result == null) {
34
+ return null;
35
+ }
36
+ if (process.env.VERBOSE) {
37
+ console.log("result", result);
38
+ }
39
+ if (typeof result === "string" && result.length > 0) {
40
+ return result;
41
+ }
42
+ if (result.slice && result.toString) {
43
+ // noinspection JSCheckFunctionSignatures
44
+ return result.toString("utf8");
45
+ }
46
+ return null;
47
+ }
48
+
49
+ export function invalidString(input: any): boolean {
50
+ return typeof input !== "string" || input.length === 0;
51
+ }
52
+
53
+
54
+ export function validString(input: any): input is string {
55
+ return typeof input == "string" && input.length > 0;
56
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "include": [
3
+ "src/**.ts",
4
+ "src/**.tsx",
5
+ "src/**.js",
6
+ "src/**.jsx"
7
+ ],
8
+ "compilerOptions": {
9
+ "moduleResolution": "node",
10
+ "target": "es2021",
11
+ "types": [
12
+ "node"
13
+ ],
14
+ "lib": [
15
+ "es2015",
16
+ "es2017",
17
+ "es2021",
18
+ "dom"
19
+ ],
20
+ "strict": true,
21
+ "allowSyntheticDefaultImports": true,
22
+ }
23
+ }