@mherod/get-cookie 2.0.0-beta.8 → 2.0.0-rc.1

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.
@@ -1,9 +1,20 @@
1
1
  import * as fs from "fs";
2
2
  import * as sqlite3 from "sqlite3";
3
- import CookieRow from "./CookieRow";
3
+ import { CookieRow } from "./CookieRow";
4
+ import { merge } from "lodash";
4
5
 
5
- export async function doSqliteQuery1(file: string, sql: string): Promise<CookieRow[]> {
6
- if (!fs.existsSync(file)) {
6
+ interface DoSqliteQuery1Params {
7
+ file: string;
8
+ sql: string;
9
+ rowTransform: (row: any) => CookieRow;
10
+ }
11
+
12
+ export async function doSqliteQuery1({
13
+ file,
14
+ sql,
15
+ rowTransform,
16
+ }: DoSqliteQuery1Params): Promise<CookieRow[]> {
17
+ if (!file || (file && !fs.existsSync(file))) {
7
18
  throw new Error(`doSqliteQuery1: file ${file} does not exist`);
8
19
  }
9
20
  const db = new sqlite3.Database(file);
@@ -19,15 +30,14 @@ export async function doSqliteQuery1(file: string, sql: string): Promise<CookieR
19
30
  return;
20
31
  }
21
32
  if (Array.isArray(rows1)) {
22
- const cookieRows: CookieRow[] = rows1.map((row) => {
23
- return {
24
- domain: row["host_key"],
25
- name: row["name"],
26
- value: row["encrypted_value"],
33
+ const cookieRows: CookieRow[] = rows1.map((row: any) => {
34
+ const newVar = {
27
35
  meta: {
28
- file: file
29
- }
36
+ file: file,
37
+ },
30
38
  };
39
+ const cookieRow: CookieRow = rowTransform(row);
40
+ return merge(newVar, cookieRow);
31
41
  });
32
42
  resolve(cookieRows);
33
43
  return;
@@ -4,32 +4,48 @@ import { fetch } from "cross-fetch";
4
4
  import { merge } from "lodash";
5
5
  // noinspection SpellCheckingInspection
6
6
  import destr from "destr";
7
- import FetchResponse from "./fetchResponse";
7
+ import FetchResponse from "./FetchResponse";
8
8
  import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
9
9
 
10
10
  export async function fetchWithCookies(
11
11
  url: RequestInfo | URL,
12
12
  options: RequestInit | undefined = {}
13
13
  ): Promise<FetchResponse> {
14
- const url1 = new URL(`${url}`);
15
- const cookie: string[] = await getGroupedRenderedCookies({
14
+ const defaultOptions: RequestInit = {
15
+ headers: {
16
+ "User-Agent":
17
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
18
+ },
19
+ redirect: "manual",
20
+ };
21
+ const url2: string = `${url}`;
22
+ const url1: URL = new URL(url2);
23
+ const domain = url1.hostname.replace(/^.*(\.\w+\.\w+)$/, (match, p1) => {
24
+ return `%${p1}`;
25
+ });
26
+ const cookies: string[] = await getGroupedRenderedCookies({
16
27
  name: "%",
17
- domain: url1.hostname
28
+ domain: domain,
18
29
  });
19
- merge(options, {
20
- credentials: "include",
30
+ const cookie = cookies.pop();
31
+ const newOptions1: RequestInit = merge(defaultOptions, options, {
21
32
  headers: {
22
- Cookie: cookie.pop()
23
- }
33
+ Cookie: cookie,
34
+ },
24
35
  });
25
36
  try {
26
- const res = await fetch(url, options);
37
+ const res: Response = await fetch(url2, newOptions1);
38
+ const newUrl = res.headers.get("location") as string;
39
+ if (res.redirected || (newUrl && newUrl !== url2)) {
40
+ return fetchWithCookies(newUrl, newOptions1);
41
+ }
27
42
  const arrayBuffer1 = res.arrayBuffer();
28
43
  const arrayBuffer = async () => arrayBuffer1;
29
44
  const buffer = async () => arrayBuffer().then(Buffer.from);
30
45
  const text = async () => buffer().then((buffer) => buffer.toString("utf8"));
31
46
  const json = async () => text().then(destr);
32
- const formData = async () => text().then((text) => new URLSearchParams(text));
47
+ const formData = async () =>
48
+ text().then((text) => new URLSearchParams(text));
33
49
  const source2 = {
34
50
  status: res.status,
35
51
  statusText: res.statusText,
@@ -38,7 +54,7 @@ export async function fetchWithCookies(
38
54
  buffer,
39
55
  text,
40
56
  json,
41
- formData
57
+ formData,
42
58
  //
43
59
  };
44
60
  return merge({}, res, source2);
@@ -1,18 +1,16 @@
1
1
  import { env } from "./global";
2
2
  import * as fs from "fs";
3
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[]> {
4
+ export async function findAllFiles({
5
+ path,
6
+ name,
7
+ maxDepth = 2,
8
+ }: //
9
+ {
10
+ path: string;
11
+ name: string;
12
+ maxDepth?: number;
13
+ }): Promise<string[]> {
16
14
  const rootSegments = path.split("/").length;
17
15
 
18
16
  if (env.VERBOSE) {
@@ -45,7 +43,7 @@ export async function findAllFiles(
45
43
  const subFiles = await findAllFiles({
46
44
  path: filePath,
47
45
  name: name,
48
- maxDepth: 2
46
+ maxDepth: 2,
49
47
  });
50
48
  files.push(...subFiles);
51
49
  } catch (e) {
@@ -1,19 +1,19 @@
1
- import ExportedCookie from "./ExportedCookie";
2
1
  import { queryCookies } from "./queryCookies";
3
2
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
4
3
  import { groupBy } from "lodash";
5
4
  import { resultsRendered } from "./resultsRendered";
6
- import CookieRequest from "./CookieRequest";
5
+ import { CookieSpec } from "./CookieSpec";
6
+ import { ExportedCookie } from "./ExportedCookie";
7
7
 
8
8
  export async function getGroupedRenderedCookies(
9
9
  //
10
10
  {
11
11
  name,
12
- domain
13
- //
14
- }: CookieRequest
15
- //
16
- ): Promise<string[]> {
12
+ domain,
13
+ }: //
14
+ CookieSpec
15
+ ): //
16
+ Promise<string[]> {
17
17
  const cookies: ExportedCookie[] = await queryCookies(
18
18
  { name, domain },
19
19
  new CompositeCookieQueryStrategy()
package/src/global.ts CHANGED
@@ -1,4 +1,5 @@
1
- const { merge } = require("lodash");
1
+ import { merge } from "lodash";
2
+
2
3
  export const env: any = {};
3
4
  merge(env, process.env);
4
5
  export const HOME: string = env["HOME"];
package/src/index.ts CHANGED
@@ -5,10 +5,14 @@ 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 { CookieSpec } from "./CookieSpec";
9
+ import { ExportedCookie } from "./ExportedCookie";
8
10
  import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
9
11
  import { fetchWithCookies } from "./fetchWithCookies";
10
12
 
11
- export async function getCookie(params: { name: string; domain: string; }) {
13
+ export async function getCookie(
14
+ params: CookieSpec
15
+ ): Promise<ExportedCookie | undefined> {
12
16
  const cookies = await queryCookies(
13
17
  params,
14
18
  new CompositeCookieQueryStrategy()
@@ -21,12 +25,9 @@ export async function getCookie(params: { name: string; domain: string; }) {
21
25
  }
22
26
  }
23
27
 
24
- /**
25
- *
26
- * @param params
27
- * @returns {Promise<*>}
28
- */
29
- export async function getFirefoxCookie(params: { name: string; domain: string; }) {
28
+ export async function getFirefoxCookie(
29
+ params: CookieSpec
30
+ ): Promise<ExportedCookie | undefined> {
30
31
  const cookies = await queryCookies(
31
32
  params,
32
33
  new FirefoxCookieQueryStrategy()
@@ -39,12 +40,9 @@ export async function getFirefoxCookie(params: { name: string; domain: string; }
39
40
  }
40
41
  }
41
42
 
42
- /**
43
- *
44
- * @param params
45
- * @returns {Promise<*>}
46
- */
47
- export async function getChromeCookie(params: { name: string; domain: string; }) {
43
+ export async function getChromeCookie(
44
+ params: CookieSpec
45
+ ): Promise<ExportedCookie | undefined> {
48
46
  const cookies = await queryCookies(
49
47
  params,
50
48
  new ChromeCookieQueryStrategy()
@@ -57,10 +55,8 @@ export async function getChromeCookie(params: { name: string; domain: string; })
57
55
  }
58
56
  }
59
57
 
60
- export default {
61
- getCookie,
62
- getFirefoxCookie,
63
- getChromeCookie,
64
- getGroupedRenderedCookies,
65
- fetchWithCookies
66
- }
58
+ export { getGroupedRenderedCookies, fetchWithCookies };
59
+
60
+ export * from "./CookieSpec";
61
+ export * from "./CookieRow";
62
+ export * from "./ExportedCookie";
package/src/isValidJwt.ts CHANGED
@@ -1,4 +1,4 @@
1
- import jsonwebtoken from "jsonwebtoken";
1
+ import jsonwebtoken, { JwtPayload } from "jsonwebtoken";
2
2
  import { env } from "./global";
3
3
 
4
4
  export default function isValidJwt(token: any) {
@@ -7,6 +7,16 @@ export default function isValidJwt(token: any) {
7
7
  if (env.VERBOSE) {
8
8
  console.log(result);
9
9
  }
10
+ const payload: JwtPayload = result?.payload as JwtPayload;
11
+ if (payload) {
12
+ const exp = payload.exp;
13
+ if (exp) {
14
+ const now = new Date().getTime() / 1000;
15
+ if (now > exp) {
16
+ return false;
17
+ }
18
+ }
19
+ }
10
20
  return true;
11
21
  } catch (err) {
12
22
  return false;
@@ -3,14 +3,11 @@ import { uniqBy } from "lodash";
3
3
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
4
4
  import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
5
5
  import isValidJwt from "./isValidJwt";
6
- import ExportedCookie from "./ExportedCookie";
7
- import CookieRequest from "./CookieRequest";
6
+ import { CookieSpec } from "./CookieSpec";
7
+ import { ExportedCookie } from "./ExportedCookie";
8
8
 
9
9
  export async function queryCookies(
10
- {
11
- name,
12
- domain
13
- }: CookieRequest,
10
+ { name, domain }: CookieSpec,
14
11
  strategy: CookieQueryStrategy = new CompositeCookieQueryStrategy()
15
12
  ) {
16
13
  const results: ExportedCookie[] = await strategy.queryCookies(name, domain);
@@ -1,5 +1,5 @@
1
- import ExportedCookie from "./ExportedCookie";
2
1
  import { uniqBy } from "lodash";
2
+ import { ExportedCookie } from "./ExportedCookie";
3
3
 
4
4
  export function resultsRendered(results: ExportedCookie[]) {
5
5
  return uniqBy(results, (r) => r.name)
@@ -0,0 +1,13 @@
1
+ export function unpackHeaders(headerArgs: string[] | string) {
2
+ const headers: any = {};
3
+ if (Array.isArray(headerArgs)) {
4
+ for (const h of headerArgs) {
5
+ const [key, value] = h.split("=");
6
+ headers[key] = value;
7
+ }
8
+ } else {
9
+ const [key, value] = headerArgs.split("=");
10
+ headers[key] = value;
11
+ }
12
+ return headers;
13
+ }
package/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // noinspection JSUnusedGlobalSymbols
2
2
 
3
- import {exec, ExecException} from "child_process";
3
+ import { exec, ExecException } from "child_process";
4
4
 
5
5
  export async function execSimple(command: string): Promise<string> {
6
6
  return new Promise((resolve, reject) => {
@@ -24,11 +24,6 @@ export async function execSimple(command: string): Promise<string> {
24
24
  });
25
25
  }
26
26
 
27
- /**
28
- *
29
- * @param result
30
- * @returns {string|null}
31
- */
32
27
  export function toStringOrNull(result: any) {
33
28
  if (result == null) {
34
29
  return null;
@@ -50,7 +45,6 @@ export function invalidString(input: any): boolean {
50
45
  return typeof input !== "string" || input.length === 0;
51
46
  }
52
47
 
53
-
54
48
  export function validString(input: any): input is string {
55
49
  return typeof input == "string" && input.length > 0;
56
50
  }