@mherod/get-cookie 2.0.0-rc.11 → 2.0.0-rc.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mherod/get-cookie",
3
- "version": "2.0.0-rc.11",
3
+ "version": "2.0.0-rc.13",
4
4
  "description": "Node.js module for querying a local user's Chrome cookie",
5
5
  "source": "src/index.ts",
6
6
  "bin": "dist/cli.js",
@@ -13,13 +13,11 @@
13
13
  "scripts": {
14
14
  "clean": "git clean -fdX && rm -rf node_modules dist && yarn install",
15
15
  "build": "parcel build",
16
+ "postbuild": "npm run check",
16
17
  "prepare": "npm run build && npm run prettier",
17
18
  "test": "jest",
18
- "postbuild": "npm run check",
19
19
  "prepublish": "npm run build",
20
- "preinstall:global": "npm run build",
21
- "install:global": "npm install --force --global .",
22
- "postinstall:global": "find $NVM_BIN -exec chmod +x {} \\; || true",
20
+ "install:global": "npm run build && npm install --force --global .",
23
21
  "prettier": "prettier --write 'src/{**/*,*}.{js,json,ts}'",
24
22
  "check": "tsc --noEmit"
25
23
  },
@@ -80,7 +78,6 @@
80
78
  "@types/node": "^18.8.2",
81
79
  "@types/tough-cookie": "^4.0.2",
82
80
  "@types/user-agents": "^1.0.2",
83
- "jest": "^29.0.3",
84
81
  "parcel": "^2.7.0",
85
82
  "prettier": "^2.7.1",
86
83
  "ts-node": "^10.9.1",
@@ -1,11 +1,14 @@
1
- import { CookieJar } from "tough-cookie";
2
1
  import { env } from "./global";
2
+ import { parsedArgs } from "./argv";
3
+ import { CookieJar, MemoryCookieStore, Store } from "tough-cookie";
3
4
 
4
5
  const { FileCookieStore } = require("tough-cookie-file-store");
5
6
 
6
- export const cookieStore = new FileCookieStore(
7
- `${env["HOME"]}/cookie-star.json`
8
- );
9
- // export const cookieStore = new MemoryCookieStore();
7
+ export let cookieStore: Store;
8
+ if (parsedArgs["cs"] == "memory") {
9
+ cookieStore = new MemoryCookieStore();
10
+ } else {
11
+ cookieStore = new FileCookieStore(`${env["HOME"]}/cookie-star.json`);
12
+ }
10
13
 
11
14
  export const cookieJar = new CookieJar(cookieStore);
package/src/argv.ts CHANGED
@@ -1,5 +1,20 @@
1
1
  import minimist from "minimist";
2
+ import { merge } from "lodash";
2
3
 
3
- export const argv: string[] = process.argv ?? [];
4
+ export interface MyParsedArgs extends minimist.ParsedArgs {
5
+ verbose: boolean;
6
+ render?: string;
7
+ fetch?: string;
8
+ cs?: string;
9
+ dump?: string;
10
+ "dump-request-headers"?: string;
11
+ "dump-response-headers"?: string;
12
+ "dump-response-body"?: string;
13
+ }
4
14
 
5
- export const parsedArgs: minimist.ParsedArgs = minimist(argv.slice(2));
15
+ export const argv: string[] = process.argv ?? [];
16
+ const minimistArgs: minimist.ParsedArgs = minimist(argv.slice(2));
17
+ const defaultOptions = {
18
+ verbose: false,
19
+ };
20
+ export const parsedArgs: MyParsedArgs = merge(defaultOptions, minimistArgs);
@@ -1,22 +1,67 @@
1
1
  import * as path from "path";
2
2
  import CookieQueryStrategy from "./CookieQueryStrategy";
3
- import { env, HOME } from "../global";
4
- import { existsSync } from "fs";
3
+ import { HOME } from "../global";
4
+ import fs, { existsSync } from "fs";
5
5
  import { findAllFiles } from "../findAllFiles";
6
- import { doSqliteQuery1 } from "../doSqliteQuery1";
7
6
  import ExportedCookie from "../ExportedCookie";
8
7
  import CookieRow from "../CookieRow";
9
8
  import CookieSpec from "../CookieSpec";
10
9
  import { specialCases } from "../SpecialCases";
10
+ import { parsedArgs } from "../argv";
11
+ import { DoSqliteQuery1Params } from "../doSqliteQuery1Params";
12
+ import * as sqlite3 from "sqlite3";
13
+ import { merge } from "lodash";
14
+
15
+ export async function doSqliteQuery1({
16
+ file,
17
+ sql,
18
+ rowTransform,
19
+ }: DoSqliteQuery1Params): Promise<CookieRow[]> {
20
+ if (!file || (file && !fs.existsSync(file))) {
21
+ throw new Error(`doSqliteQuery1: file ${file} does not exist`);
22
+ }
23
+ const db = new sqlite3.Database(file);
24
+ return new Promise((resolve, reject) => {
25
+ db.all(sql, (err: Error, rows: any[]) => {
26
+ if (err) {
27
+ reject(err);
28
+ return;
29
+ }
30
+ const rows1: any[] = rows;
31
+ if (rows1 == null || rows1.length === 0) {
32
+ resolve([]);
33
+ return;
34
+ }
35
+ if (Array.isArray(rows1)) {
36
+ const cookieRows: CookieRow[] = rows1.map((row: any) => {
37
+ const newVar = {
38
+ meta: {
39
+ file: file,
40
+ },
41
+ };
42
+ const cookieRow: CookieRow = rowTransform(row);
43
+ return merge(newVar, cookieRow);
44
+ });
45
+ resolve(cookieRows);
46
+ return;
47
+ }
48
+ if (parsedArgs.verbose) {
49
+ console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
50
+ }
51
+ resolve([rows1]);
52
+ });
53
+ });
54
+ }
11
55
 
12
56
  export default class FirefoxCookieQueryStrategy implements CookieQueryStrategy {
13
57
  browserName = "Firefox";
14
58
 
15
59
  async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
16
60
  if (process.platform !== "darwin") {
17
- throw new Error("This only works on macOS");
61
+ // TODO: implement
62
+ return [];
18
63
  }
19
- if (env.CHROME_ONLY) {
64
+ if (parsedArgs.browser !== "firefox") {
20
65
  return [];
21
66
  }
22
67
  const cookies = await this.#getFirefoxCookie({ name, domain });
@@ -49,7 +94,8 @@ export default class FirefoxCookieQueryStrategy implements CookieQueryStrategy {
49
94
  const fn: (file: string) => Promise<CookieRow[]> = async (file: string) => {
50
95
  return await this.#queryCookiesDb(file, name, domain);
51
96
  };
52
- const all: Awaited<CookieRow[]>[] = await Promise.all(files.map(fn));
97
+ const all: CookieRow[][] = await Promise.all(files.map(fn));
98
+ // flatten
53
99
  return all.flat();
54
100
  }
55
101
 
@@ -5,6 +5,7 @@ export default class SafariCookieQueryStrategy implements CookieQueryStrategy {
5
5
  browserName = "Safari";
6
6
 
7
7
  async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
8
+ // TODO: implement
8
9
  return [];
9
10
  }
10
11
  }
@@ -8,13 +8,16 @@ export async function comboQueryCookieSpec(
8
8
  ): Promise<ExportedCookie[]> {
9
9
  const cookies: ExportedCookie[] = [];
10
10
  if (Array.isArray(cookieSpec)) {
11
- const cookieSpecs = <CookieSpec[]>cookieSpec;
12
- for (const cookieSpec1 of cookieSpecs) {
13
- const cookies1: ExportedCookie[] = await queryCookies(cookieSpec1);
14
- cookies.push(...cookies1);
11
+ const results: Awaited<ExportedCookie[]>[] = await Promise.all(
12
+ cookieSpec.map((cs) => {
13
+ return queryCookies(cs);
14
+ })
15
+ );
16
+ for (const exportedCookie of results.flat()) {
17
+ cookies.push(exportedCookie);
15
18
  }
16
19
  } else {
17
- const singleQuery = await queryCookies(cookieSpec);
20
+ const singleQuery: ExportedCookie[] = await queryCookies(cookieSpec);
18
21
  cookies.push(...singleQuery);
19
22
  }
20
23
  return uniqBy(cookies, JSON.stringify);
@@ -1,4 +1,4 @@
1
- // noinspection JSUnusedGlobalSymbols
1
+ // noinspection JSUnusedGlobalSymbols,ExceptionCaughtLocallyJS
2
2
 
3
3
  import { fetch as fetchImpl } from "cross-fetch";
4
4
  import { merge } from "lodash";
@@ -12,13 +12,15 @@ import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
12
12
  import { cookieSpecsFromUrl } from "./cookieSpecsFromUrl";
13
13
  import CookieSpec from "./CookieSpec";
14
14
 
15
+ const userAgent = new UserAgent().toString();
16
+
15
17
  export async function fetchWithCookies(
16
18
  url: RequestInfo | URL,
17
19
  options: RequestInit | undefined = {},
18
20
  fetch: Function = fetchImpl
19
21
  ): Promise<Response> {
20
22
  const headers = {
21
- "User-Agent": new UserAgent().toString(),
23
+ "User-Agent": userAgent,
22
24
  };
23
25
  const defaultOptions: RequestInit = {
24
26
  headers,
@@ -57,11 +59,21 @@ export async function fetchWithCookies(
57
59
  }
58
60
 
59
61
  const newUrl: string = res.headers.get("location") as string;
60
- if (res.redirected || (newUrl && newUrl !== url2)) {
61
- if (parsedArgs.verbose) {
62
- console.log(blue(`Redirected to `), yellow(newUrl));
62
+ if (res.status >= 300 && res.status < 400) {
63
+ if (newUrl && newUrl !== url2) {
64
+ if (parsedArgs.verbose) {
65
+ console.log(blue(`Redirected to `), yellow(newUrl));
66
+ }
67
+ return fetchWithCookies(
68
+ //
69
+ newUrl,
70
+ newOptions1,
71
+ fetch
72
+ //
73
+ );
74
+ } else {
75
+ throw new Error("Redirected but no new location");
63
76
  }
64
- return fetchWithCookies(newUrl, newOptions1);
65
77
  }
66
78
 
67
79
  const arrayBuffer1: Promise<ArrayBuffer> = res.arrayBuffer();
@@ -1,47 +0,0 @@
1
- import * as fs from "fs";
2
- import * as sqlite3 from "sqlite3";
3
- import CookieRow from "./CookieRow";
4
- import { merge } from "lodash";
5
- import { parsedArgs } from "./argv";
6
- import { DoSqliteQuery1Params } from "./doSqliteQuery1Params";
7
-
8
- export async function doSqliteQuery1({
9
- file,
10
- sql,
11
- rowTransform,
12
- }: DoSqliteQuery1Params): Promise<CookieRow[]> {
13
- if (!file || (file && !fs.existsSync(file))) {
14
- throw new Error(`doSqliteQuery1: file ${file} does not exist`);
15
- }
16
- const db = new sqlite3.Database(file);
17
- return new Promise((resolve, reject) => {
18
- db.all(sql, (err: Error, rows: any[]) => {
19
- if (err) {
20
- reject(err);
21
- return;
22
- }
23
- const rows1: any[] = rows;
24
- if (rows1 == null || rows1.length === 0) {
25
- resolve([]);
26
- return;
27
- }
28
- if (Array.isArray(rows1)) {
29
- const cookieRows: CookieRow[] = rows1.map((row: any) => {
30
- const newVar = {
31
- meta: {
32
- file: file,
33
- },
34
- };
35
- const cookieRow: CookieRow = rowTransform(row);
36
- return merge(newVar, cookieRow);
37
- });
38
- resolve(cookieRows);
39
- return;
40
- }
41
- if (parsedArgs.verbose) {
42
- console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
43
- }
44
- resolve([rows1]);
45
- });
46
- });
47
- }