@mherod/get-cookie 2.0.0-rc.29 → 2.0.0-rc.30

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.
@@ -0,0 +1,45 @@
1
+ import { sync } from "fast-glob";
2
+ import { chromeApplicationSupport } from "./browsers/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
+ };
@@ -0,0 +1,55 @@
1
+ import { flatMapAsync } from "./flatMapAsync";
2
+
3
+ describe("flatMapAsync", () => {
4
+ it("should apply async callback to each element, wait for all promises to fulfill and flatten the resulting array", async () => {
5
+ const array = [1, 2, 3];
6
+ const callback = async (value: number) => [value, value * 2];
7
+ const result = await flatMapAsync(array, callback);
8
+ expect(result).toEqual([1, 2, 2, 4, 3, 6]);
9
+ });
10
+
11
+ it("should handle errors with the provided error callback", async () => {
12
+ const array = [1, 2, 3];
13
+ const callback = async (value: number) => {
14
+ if (value === 2) {
15
+ throw new Error("Test error");
16
+ }
17
+ return [value, value * 2];
18
+ };
19
+ const errorCallback = (error: any) => [0];
20
+ const result = await flatMapAsync(array, callback, errorCallback);
21
+ expect(result).toEqual([1, 2, 0, 3, 6]);
22
+ });
23
+
24
+ it("should return an empty array when input array is empty", async () => {
25
+ const array: number[] = [];
26
+ const callback = async (value: number) => [value, value * 2];
27
+ const result = await flatMapAsync(array, callback);
28
+ expect(result).toEqual([]);
29
+ });
30
+
31
+ it("should return an array with single element when input array has one element", async () => {
32
+ const array = [1];
33
+ const callback = async (value: number) => [value, value * 2];
34
+ const result = await flatMapAsync(array, callback);
35
+ expect(result).toEqual([1, 2]);
36
+ });
37
+
38
+ it("should handle null values in the array", async () => {
39
+ const array = [1, null, 3];
40
+ const callback = async (value: number) =>
41
+ value ? [value, value * 2] : [0];
42
+ // @ts-expect-error
43
+ const result = await flatMapAsync(array, callback);
44
+ expect(result).toEqual([1, 2, 0, 3, 6]);
45
+ });
46
+
47
+ it("should handle undefined values in the array", async () => {
48
+ const array = [1, undefined, 3];
49
+ const callback = async (value: number) =>
50
+ value ? [value, value * 2] : [0];
51
+ // @ts-expect-error
52
+ const result = await flatMapAsync(array, callback);
53
+ expect(result).toEqual([1, 2, 0, 3, 6]);
54
+ });
55
+ });
@@ -0,0 +1,37 @@
1
+ // This utility function applies an async callback to each element of the input array (just like Array.prototype.map),
2
+ // waits for all resulting promises to fulfill (similar to Promise.all), and then flattens the resulting array (like Array.prototype.flat).
3
+
4
+ export async function flatMapAsync<T, O>(
5
+ array: T[], // The input array to transform.
6
+ callback: (value: T, index: number, array: T[]) => Promise<O[]>, // The async function to apply to each input array element.,
7
+ or?: O[] | ((error: any) => O[] | Promise<O[]>) // An optional callback to handle errors.
8
+ ): Promise<O[]> {
9
+ if (or) {
10
+ return flatMapAsync(
11
+ array,
12
+ async (value: T, index: number, array: T[]) => {
13
+ try {
14
+ return await callback(value, index, array);
15
+ } catch (error) {
16
+ return typeof or == "function" ? or(error) : or;
17
+ }
18
+ }
19
+ //
20
+ );
21
+ }
22
+
23
+ // Step 1: Use Array.prototype.map to transform each element of the input array with the provided callback.
24
+ // This will result in an array of promises.
25
+ const ps: Promise<O>[] = array.map(callback) as Promise<O>[];
26
+
27
+ // Step 2: Use Promise.all to wait for all promises in the array to fulfill.
28
+ // This will give us an array of fulfilled promise values.
29
+ const awaitedAll: Awaited<O>[] = await Promise.all(ps);
30
+
31
+ // Step 3: Flatten the array of fulfilled promise values into a single array.
32
+ // Note: The '@ts-ignore' comment is used to suppress TypeScript compiler warnings, as .flat() method might not be recognized
33
+ // as a valid method for an array of Awaited<O> instances. However, this code assumes that the result of Promise.all
34
+ // is indeed an array that can be flattened.
35
+ // @ts-ignore
36
+ return awaitedAll.flat() as O[];
37
+ }
@@ -1,9 +0,0 @@
1
- import ExportedCookie from "./ExportedCookie";
2
-
3
- export function isExportedCookie(obj: any): obj is ExportedCookie {
4
- return (
5
- obj.domain !== undefined &&
6
- obj.name !== undefined &&
7
- obj.value !== undefined
8
- );
9
- }
@@ -1,8 +0,0 @@
1
- import CookieRow from "./CookieRow";
2
-
3
- export interface DoSqliteQuery1Params {
4
- file: string;
5
- sql: string;
6
- rowFilter?: (row: any) => boolean;
7
- rowTransform: (row: any) => CookieRow;
8
- }
package/src/utils.ts DELETED
@@ -1,25 +0,0 @@
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
- }