@mherod/get-cookie 1.3.0 → 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 (43) 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 +38 -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.ts +30 -0
  22. package/src/browsers/CookieQueryStrategy.ts +5 -0
  23. package/src/browsers/FirefoxCookieQueryStrategy.ts +92 -0
  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.ts +68 -0
  30. package/src/getGroupedRenderedCookies.ts +32 -0
  31. package/src/global.ts +7 -0
  32. package/src/index.ts +59 -0
  33. package/src/isValidJwt.ts +14 -0
  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 -430
  39. package/dist/main.js.map +0 -1
  40. package/src/cli.js +0 -58
  41. package/src/global.js +0 -3
  42. package/src/index.js +0 -462
  43. package/src/utils.js +0 -185
@@ -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
+ }
@@ -0,0 +1,68 @@
1
+ import { env } from "./global";
2
+ import * as fs from "fs";
3
+
4
+ export async function findAllFiles(
5
+ {
6
+ path,
7
+ name,
8
+ maxDepth = 2
9
+ //
10
+ }: {
11
+ path: string;
12
+ name: string;
13
+ maxDepth?: number;
14
+ }
15
+ ): Promise<string[]> {
16
+ const rootSegments = path.split("/").length;
17
+
18
+ if (env.VERBOSE) {
19
+ console.log(`Searching for ${name} in ${path}`);
20
+ }
21
+ const files: string[] = [];
22
+ let readdirSync;
23
+ try {
24
+ readdirSync = fs.readdirSync(path);
25
+ } catch (e) {
26
+ if (env.VERBOSE) {
27
+ console.log(`Error reading ${path}`, e);
28
+ }
29
+ return [];
30
+ }
31
+ for (const file of readdirSync) {
32
+ const filePath = path + "/" + file;
33
+ let stat;
34
+ try {
35
+ stat = fs.statSync(filePath);
36
+ } catch (e) {
37
+ if (env.VERBOSE) {
38
+ console.error(`Error getting stat for ${filePath}`, e);
39
+ }
40
+ continue;
41
+ }
42
+ if (stat.isDirectory()) {
43
+ if (filePath.split("/").length < rootSegments + maxDepth) {
44
+ try {
45
+ const subFiles = await findAllFiles({
46
+ path: filePath,
47
+ name: name,
48
+ maxDepth: 2
49
+ });
50
+ files.push(...subFiles);
51
+ } catch (e) {
52
+ if (env.VERBOSE) {
53
+ console.error(e);
54
+ }
55
+ }
56
+ }
57
+ } else if (file === name) {
58
+ files.push(filePath);
59
+ }
60
+ }
61
+ if (env.VERBOSE) {
62
+ if (files.length > 0) {
63
+ console.log(`Found ${files.length} ${name} files`);
64
+ console.log(files);
65
+ }
66
+ }
67
+ return files;
68
+ }
@@ -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
+ }
package/src/global.ts ADDED
@@ -0,0 +1,7 @@
1
+ const { merge } = require("lodash");
2
+ export const env: any = {};
3
+ merge(env, process.env);
4
+ export const HOME: string = env["HOME"];
5
+ if (!HOME) {
6
+ throw new Error("HOME environment variable is not set");
7
+ }
package/src/index.ts ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env node
2
+ // noinspection JSUnusedGlobalSymbols
3
+
4
+ import { queryCookies } from "./queryCookies";
5
+ import FirefoxCookieQueryStrategy from "./browsers/FirefoxCookieQueryStrategy";
6
+ import ChromeCookieQueryStrategy from "./browsers/ChromeCookieQueryStrategy";
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";
12
+
13
+ export async function getCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
14
+ const cookies = await queryCookies(
15
+ params,
16
+ new CompositeCookieQueryStrategy()
17
+ //
18
+ );
19
+ if (Array.isArray(cookies) && cookies.length > 0) {
20
+ return cookies.find((cookie) => cookie != null);
21
+ } else {
22
+ throw new Error("Cookie not found");
23
+ }
24
+ }
25
+
26
+ export async function getFirefoxCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
27
+ const cookies = await queryCookies(
28
+ params,
29
+ new FirefoxCookieQueryStrategy()
30
+ //
31
+ );
32
+ if (Array.isArray(cookies) && cookies.length > 0) {
33
+ return cookies.find((cookie) => cookie != null);
34
+ } else {
35
+ throw new Error("Cookie not found");
36
+ }
37
+ }
38
+
39
+ export async function getChromeCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
40
+ const cookies = await queryCookies(
41
+ params,
42
+ new ChromeCookieQueryStrategy()
43
+ //
44
+ );
45
+ if (Array.isArray(cookies) && cookies.length > 0) {
46
+ return cookies.find((cookie) => cookie != null);
47
+ } else {
48
+ throw new Error("Cookie not found");
49
+ }
50
+ }
51
+
52
+ export {
53
+ getGroupedRenderedCookies,
54
+ fetchWithCookies,
55
+ };
56
+
57
+ export * from "./CookieRequest";
58
+ export * from "./CookieRow";
59
+ export * from "./ExportedCookie";
@@ -0,0 +1,14 @@
1
+ import jsonwebtoken from "jsonwebtoken";
2
+ import { env } from "./global";
3
+
4
+ export default function isValidJwt(token: any) {
5
+ try {
6
+ const result = jsonwebtoken.decode(token, { complete: true });
7
+ if (env.VERBOSE) {
8
+ console.log(result);
9
+ }
10
+ return true;
11
+ } catch (err) {
12
+ return false;
13
+ }
14
+ }
@@ -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
+ }