@cpu-utils/headless 1.0.0

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,20 @@
1
+ /**
2
+ * Default headers that mimic a Chrome browser request.
3
+ */
4
+ declare const CHROME_DEFAULT_HEADERS: Record<string, string>;
5
+ export { CHROME_DEFAULT_HEADERS };
6
+ /**
7
+ * Fetch a URL using Chrome cookies and default Chrome headers.
8
+ *
9
+ * This function reads cookies from the local Chrome cookie store for the given URL,
10
+ * then makes a fetch request with those cookies and default Chrome browser headers.
11
+ * Any additional options or headers provided will be merged, with user-provided values
12
+ * taking precedence over the defaults.
13
+ *
14
+ * @param url - The URL to fetch.
15
+ * @param init - Optional fetch init options (headers, method, body, etc.).
16
+ * @param profile - Chrome profile to use for cookies. Defaults to 'Default'.
17
+ * @returns A promise that resolves to the fetch Response.
18
+ */
19
+ export declare function chromeFetch(url: string, init?: RequestInit, profile?: string): Promise<Response>;
20
+ //# sourceMappingURL=chromeFetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chromeFetch.d.ts","sourceRoot":"","sources":["../src/chromeFetch.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,QAAA,MAAM,sBAAsB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAYlD,CAAC;AAEF,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAElC;;;;;;;;;;;;GAYG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,WAAW,EAClB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,QAAQ,CAAC,CAanB"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CHROME_DEFAULT_HEADERS = void 0;
4
+ exports.chromeFetch = chromeFetch;
5
+ const getChromeCookie_1 = require("./getChromeCookie");
6
+ /**
7
+ * Default headers that mimic a Chrome browser request.
8
+ */
9
+ const CHROME_DEFAULT_HEADERS = {
10
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
11
+ Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
12
+ 'Accept-Language': 'en-US,en;q=0.9',
13
+ 'Accept-Encoding': 'gzip, deflate, br',
14
+ Connection: 'keep-alive',
15
+ 'Upgrade-Insecure-Requests': '1',
16
+ 'Sec-Fetch-Dest': 'document',
17
+ 'Sec-Fetch-Mode': 'navigate',
18
+ 'Sec-Fetch-Site': 'none',
19
+ 'Sec-Fetch-User': '?1',
20
+ };
21
+ exports.CHROME_DEFAULT_HEADERS = CHROME_DEFAULT_HEADERS;
22
+ /**
23
+ * Fetch a URL using Chrome cookies and default Chrome headers.
24
+ *
25
+ * This function reads cookies from the local Chrome cookie store for the given URL,
26
+ * then makes a fetch request with those cookies and default Chrome browser headers.
27
+ * Any additional options or headers provided will be merged, with user-provided values
28
+ * taking precedence over the defaults.
29
+ *
30
+ * @param url - The URL to fetch.
31
+ * @param init - Optional fetch init options (headers, method, body, etc.).
32
+ * @param profile - Chrome profile to use for cookies. Defaults to 'Default'.
33
+ * @returns A promise that resolves to the fetch Response.
34
+ */
35
+ async function chromeFetch(url, init, profile) {
36
+ const cookie = await (0, getChromeCookie_1.getChromeCookie)(url, profile);
37
+ const headers = {
38
+ ...CHROME_DEFAULT_HEADERS,
39
+ ...(cookie ? { Cookie: cookie } : {}),
40
+ ...toHeaderRecord(init?.headers),
41
+ };
42
+ return fetch(url, {
43
+ ...init,
44
+ headers,
45
+ });
46
+ }
47
+ /**
48
+ * Convert various header formats to a plain record.
49
+ */
50
+ function toHeaderRecord(headers) {
51
+ if (!headers)
52
+ return {};
53
+ if (headers instanceof Headers) {
54
+ const record = {};
55
+ headers.forEach((value, key) => {
56
+ record[key] = value;
57
+ });
58
+ return record;
59
+ }
60
+ if (Array.isArray(headers)) {
61
+ return Object.fromEntries(headers);
62
+ }
63
+ return headers;
64
+ }
65
+ //# sourceMappingURL=chromeFetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chromeFetch.js","sourceRoot":"","sources":["../src/chromeFetch.ts"],"names":[],"mappings":";;;AAkCA,kCAiBC;AAnDD,uDAAoD;AAEpD;;GAEG;AACH,MAAM,sBAAsB,GAA2B;IACrD,YAAY,EACV,iHAAiH;IACnH,MAAM,EAAE,uFAAuF;IAC/F,iBAAiB,EAAE,gBAAgB;IACnC,iBAAiB,EAAE,mBAAmB;IACtC,UAAU,EAAE,YAAY;IACxB,2BAA2B,EAAE,GAAG;IAChC,gBAAgB,EAAE,UAAU;IAC5B,gBAAgB,EAAE,UAAU;IAC5B,gBAAgB,EAAE,MAAM;IACxB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAEO,wDAAsB;AAE/B;;;;;;;;;;;;GAYG;AACI,KAAK,UAAU,WAAW,CAC/B,GAAW,EACX,IAAkB,EAClB,OAAgB;IAEhB,MAAM,MAAM,GAAG,MAAM,IAAA,iCAAe,EAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAEnD,MAAM,OAAO,GAA2B;QACtC,GAAG,sBAAsB;QACzB,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrC,GAAG,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC;KACjC,CAAC;IAEF,OAAO,KAAK,CAAC,GAAG,EAAE;QAChB,GAAG,IAAI;QACP,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,SAAS,cAAc,CAAC,OAAqB;IAC3C,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IACxB,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;QAC/B,MAAM,MAAM,GAA2B,EAAE,CAAC;QAC1C,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YAC7B,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACtB,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,OAAiC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Get Chrome cookies for a given URL from the local Chrome database.
3
+ *
4
+ * This reads the current user's Chrome cookie store and returns the cookies
5
+ * associated with the given URL as a header string (e.g. "name=value; name2=value2").
6
+ *
7
+ * @param url - The target URL to retrieve cookies for (e.g. "https://example.com")
8
+ * @param profile - Chrome profile to use. Defaults to 'Default'.
9
+ * Use 'Profile 1', 'Profile 2', etc. for additional Chrome profiles.
10
+ * @returns A promise that resolves to the cookie header string.
11
+ */
12
+ export declare function getChromeCookie(url: string, profile?: string): Promise<string>;
13
+ //# sourceMappingURL=getChromeCookie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getChromeCookie.d.ts","sourceRoot":"","sources":["../src/getChromeCookie.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAEvF"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getChromeCookie = getChromeCookie;
4
+ const chrome_cookies_secure_1 = require("chrome-cookies-secure");
5
+ /**
6
+ * Get Chrome cookies for a given URL from the local Chrome database.
7
+ *
8
+ * This reads the current user's Chrome cookie store and returns the cookies
9
+ * associated with the given URL as a header string (e.g. "name=value; name2=value2").
10
+ *
11
+ * @param url - The target URL to retrieve cookies for (e.g. "https://example.com")
12
+ * @param profile - Chrome profile to use. Defaults to 'Default'.
13
+ * Use 'Profile 1', 'Profile 2', etc. for additional Chrome profiles.
14
+ * @returns A promise that resolves to the cookie header string.
15
+ */
16
+ async function getChromeCookie(url, profile = 'Default') {
17
+ return (0, chrome_cookies_secure_1.getCookiesPromised)(url, 'header', profile);
18
+ }
19
+ //# sourceMappingURL=getChromeCookie.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getChromeCookie.js","sourceRoot":"","sources":["../src/getChromeCookie.ts"],"names":[],"mappings":";;AAaA,0CAEC;AAfD,iEAA2D;AAE3D;;;;;;;;;;GAUG;AACI,KAAK,UAAU,eAAe,CAAC,GAAW,EAAE,OAAO,GAAG,SAAS;IACpE,OAAO,IAAA,0CAAkB,EAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { getChromeCookie } from './getChromeCookie';
2
+ export { chromeFetch, CHROME_DEFAULT_HEADERS } from './chromeFetch';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CHROME_DEFAULT_HEADERS = exports.chromeFetch = exports.getChromeCookie = void 0;
4
+ var getChromeCookie_1 = require("./getChromeCookie");
5
+ Object.defineProperty(exports, "getChromeCookie", { enumerable: true, get: function () { return getChromeCookie_1.getChromeCookie; } });
6
+ var chromeFetch_1 = require("./chromeFetch");
7
+ Object.defineProperty(exports, "chromeFetch", { enumerable: true, get: function () { return chromeFetch_1.chromeFetch; } });
8
+ Object.defineProperty(exports, "CHROME_DEFAULT_HEADERS", { enumerable: true, get: function () { return chromeFetch_1.CHROME_DEFAULT_HEADERS; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAoD;AAA3C,kHAAA,eAAe,OAAA;AACxB,6CAAoE;AAA3D,0GAAA,WAAW,OAAA;AAAE,qHAAA,sBAAsB,OAAA"}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@cpu-utils/headless",
3
+ "version": "1.0.0",
4
+ "description": "Headless browser utilities for the monorepo",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": ["dist"],
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "build:watch": "tsc --watch",
11
+ "test": "node --test dist/**/*.test.js",
12
+ "typecheck": "tsc --noEmit"
13
+ },
14
+ "dependencies": {
15
+ "chrome-cookies-secure": "^2.1.1"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^20.0.0",
22
+ "typescript": "*"
23
+ }
24
+ }