From bf0e93f61702ddb27a9decfb800e469577fa1f87 Mon Sep 17 00:00:00 2001 From: Jason Caldwell Date: Tue, 5 Mar 2024 14:54:21 -0500 Subject: [PATCH] Project update. [p][robotic] --- .browserslistrc | 2 +- .dockerignore | 2 +- .env.vault | 8 +- .gitattributes | 2 +- .gitignore | 2 +- .npmignore | 2 +- .prettierignore | 2 +- .vscode/settings.json | 2 +- .vscodeignore | 2 +- package-lock.json | 10 +-- package.json | 2 +- src/proxy.ts | 187 ++++++++++++++++++++++++------------------ src/root.ts | 8 +- tsconfig.json | 2 +- wrangler.toml | 2 +- 15 files changed, 129 insertions(+), 106 deletions(-) diff --git a/.browserslistrc b/.browserslistrc index f597acb..826de9d 100644 --- a/.browserslistrc +++ b/.browserslistrc @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. [production] node >= 20.9.0 diff --git a/.dockerignore b/.dockerignore index 50267c5..72174e5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. # Locals diff --git a/.env.vault b/.env.vault index b62e472..adc9600 100644 --- a/.env.vault +++ b/.env.vault @@ -8,12 +8,12 @@ DOTENV_VAULT_MAIN="3rfi+ClkONvfHPiU9FTBcC1+Um/L8QeM9dRw1jFzEqzZXw==" DOTENV_VAULT_MAIN_VERSION=1 # dev -DOTENV_VAULT_DEV="5iUNJilhSTYkuldheih4Lejvy/oSQa2LgZCHlU5rEk10sCJ1JlLXxHKlXiWXRJ/BexNBxPM1WgO8qRoGjLVogQDV2WaoPzBH9h6MWmOaNa4+Ly4B8cJ24QD6UxEit0+/EKLLXmAmNCV3R0bPeP+NRtkKHaXjpFplOAwsOLnFMCVxMazQXWNXRceN+Sv+u3sHxgz+C2khvtlMW48Ipz/NrqRz6Pl8WaCIG7JlDzWSyAjslUoc4bMb3PfJ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0JXj1dz8EsOE1NtjV+od1T89zDBHdpvzk9ZUlH0z8UQgk8B5PELrzGryBvfkkkL/zxjduM6ZSG/V1TuDWEeK0qWZWHQof5pNLUdT32hTYc4MvX3o6BPkTxTblNbykWaYu8dFM8rk7pE0Pe4QnFflLg4/CeE268bVJMRyWbk3iox7GdUGKlGA0R1l3S/UvteVTAs" +DOTENV_VAULT_CI_VERSION=239 # stage DOTENV_VAULT_STAGE="bfqZYD8oVLWMZ0JR5LICV+sB/btaXavszxZLxTfwSH8U5fg=" diff --git a/.gitattributes b/.gitattributes index aae5fe4..7e61892 100644 --- a/.gitattributes +++ b/.gitattributes @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. # Default diff --git a/.gitignore b/.gitignore index dff38e8..b907b1e 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. # Locals diff --git a/.npmignore b/.npmignore index 87cc05a..c0694f4 100644 --- a/.npmignore +++ b/.npmignore @@ -25,7 +25,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. # Locals diff --git a/.prettierignore b/.prettierignore index f5613da..5d0cd5c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. # Packages diff --git a/.vscode/settings.json b/.vscode/settings.json index ff58d5d..03218dc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ * @note This entire file will be updated automatically. * @note Instead of editing here, please review `./settings.mjs`. * - * Last generated using `./settings.mjs` Mar 5, 2024 7:09 PM UTC. + * Last generated using `./settings.mjs` Mar 5, 2024 7:54 PM UTC. */ { "editor.formatOnType": false, diff --git a/.vscodeignore b/.vscodeignore index 3686a7e..86722d8 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 5, 2024 7:09 PM UTC. +# Last generated Mar 5, 2024 7:54 PM UTC. # Locals diff --git a/package-lock.json b/package-lock.json index 592ac36..51a6bd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@clevercanyon/utilities.cfw", - "version": "1.0.271", + "version": "1.0.272", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@clevercanyon/utilities.cfw", - "version": "1.0.271", + "version": "1.0.272", "cpu": [ "x64", "arm64" @@ -1103,9 +1103,9 @@ } }, "node_modules/@clevercanyon/utilities.cfw": { - "version": "1.0.271", - "resolved": "https://registry.npmjs.org/@clevercanyon/utilities.cfw/-/utilities.cfw-1.0.271.tgz", - "integrity": "sha512-TPYg1eBTSZ1PU4cI0dbK/gr+1hywczNyyL2e4fnx+LHdJb9BVxIaSZxmyPtbH8SMaFLj81Etr/jT8ACDK8IccQ==", + "version": "1.0.272", + "resolved": "https://registry.npmjs.org/@clevercanyon/utilities.cfw/-/utilities.cfw-1.0.272.tgz", + "integrity": "sha512-eekwbUbI3J4CXaAJMj9UtB3cxpd4lc0Tkp31VarFa0OR6UK7Yb16c2gwzmfwP1OPhCENWRT9OKR8Qvbfsa8jiA==", "cpu": [ "x64", "arm64" diff --git a/package.json b/package.json index e8dae04..4137216 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "publishConfig": { "access": "public" }, - "version": "1.0.272", + "version": "1.0.273", "license": "GPL-3.0-or-later", "name": "@clevercanyon/utilities.cfw", "description": "Utilities for JavaScript apps running in a Cloudflare Worker environment.", diff --git a/src/proxy.ts b/src/proxy.ts index 591a136..1345efd 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -102,6 +102,113 @@ export const fetch = async (rcData: $cfw.StdRequestContextData, parseable: $type return await Promise.race([fetchꓺwaitTimeout(rcData, opts), fetchꓺviaSocket(rcData, url, opts)]); }; +/** + * Fetches worker using another worker as a proxy. + * + * @param rcData Request context data; {@see $cfw.StdRequestContextData}. + * @param requestInfo New request info; {@see $type.cfw.RequestInfo}. + * @param requestInit New request init; {@see $type.cfw.RequestInit}. + * + * @returns Promise of response from worker using another worker as a proxy. + */ +export const fetchWorker = async (rcData: $cfw.StdRequestContextData, requestInfo: $type.cfw.RequestInfo, requestInit?: $type.cfw.RequestInit): Promise<$type.cfw.Response> => { + const { fetch, Request } = cfw; + + if ($is.string(requestInfo) || $is.url(requestInfo)) { + requestInfo = 'https://worker-proxy.c10n.workers.dev/?url=' + $url.encode(requestInfo.toString()); + // + } else if (requestInfo instanceof Request) { + requestInfo = new Request('https://worker-proxy.c10n.workers.dev/?url=' + $url.encode(requestInfo.url), requestInfo); + } + return fetch(requestInfo, requestInit); +}; + +// --- +// Misc exports. + +/** + * Fetches UA headers. + * + * @param rcData Request context data; {@see $cfw.StdRequestContextData}. + * @param options All optional; {@see UAHeaderOptions}. + * + * @returns Promise of UA headers. + */ +export const uaHeaders = async (rcData: $cfw.StdRequestContextData, options?: UAHeaderOptions): Promise => { + const { fetch } = cfw, + { url, auditLogger } = rcData, + // + opts = $obj.defaults({}, options || {}, { + randomIndex: $crypto.randomNumber(1, 100), + }) as Required, + // + apiRoute = $url.addQueryVars( + { random_index: String(opts.randomIndex) }, // + new URL('https://workers.hop.gdn/api/ua-headers/v1'), + ), + defaultHeaders = { + 'upgrade-insecure-requests': '1', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', + 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'sec-ch-ua': 'Google Chrome;v="80", "Chromium";v="80", ";Not A Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': 'Windows', + 'sec-fetch-site': 'none', + 'sec-fetch-mod': '', + 'sec-fetch-user': '?1', + 'accept-encoding': 'gzip, deflate', + 'accept-language': 'en-US,en;q=0.9', + }; + if ($root.kv.isAvailable(rcData)) { + const kvKey = 'ua-headers:' + String(opts.randomIndex), + headers = (await $root.kv(rcData).get(kvKey, { type: 'json' })) as UAHeaders; + + if (!$is.plainObject(headers)) { + void auditLogger.warn('UA headers failure.', { headers, error: Error('q9UTub4N') }); + return defaultHeaders; + } + return headers; + } + if ($root.fetch.isAvailable(rcData)) { + let thrown: unknown; + + const payload = await $root + .fetch(rcData, apiRoute) + .then(async (response): Promise => { + return $to.plainObject(await response.json()) as UAHeadersResponsePayload; + }) + .catch((unknownThrown: unknown): void => { + thrown = unknownThrown; + }); + if (!payload?.ok || !$is.plainObject(payload.data) || thrown) { + void auditLogger.warn('UA headers failure.', { payload, error: Error('DkkbNUJr'), thrown }); + return defaultHeaders; + } + return payload.data; + } + if (url.hostname !== apiRoute.hostname) { + let thrown: unknown; + + const payload = await fetch(apiRoute) + .then(async (response): Promise => { + return $to.plainObject(await response.json()) as UAHeadersResponsePayload; + }) + .catch((unknownThrown: unknown): void => { + thrown = unknownThrown; + }); + if (!payload?.ok || !$is.plainObject(payload.data) || thrown) { + void auditLogger.warn('UA headers failure.', { payload, error: Error('hMG9q7P5'), thrown }); + return defaultHeaders; + } + return payload.data; + } + return defaultHeaders; +}; +uaHeaders.urlSafeOptionKeys = ['randomIndex'] as string[]; + +// --- +// Misc utilities. + /** * Creates a timeout promise. * @@ -260,83 +367,3 @@ const fetchꓺviaSocket = async (rcData: $cfw.StdRequestContextData, url: $type. }); } }; - -/** - * Fetches UA headers. - * - * @param rcData Request context data; {@see $cfw.StdRequestContextData}. - * @param options All optional; {@see UAHeaderOptions}. - * - * @returns Promise of UA headers. - */ -export const uaHeaders = async (rcData: $cfw.StdRequestContextData, options?: UAHeaderOptions): Promise => { - const { fetch } = cfw, - { url, auditLogger } = rcData, - // - opts = $obj.defaults({}, options || {}, { - randomIndex: $crypto.randomNumber(1, 100), - }) as Required, - // - apiRoute = $url.addQueryVars( - { random_index: String(opts.randomIndex) }, // - new URL('https://workers.hop.gdn/api/ua-headers/v1'), - ), - defaultHeaders = { - 'upgrade-insecure-requests': '1', - 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', - 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', - 'sec-ch-ua': 'Google Chrome;v="80", "Chromium";v="80", ";Not A Brand";v="99"', - 'sec-ch-ua-mobile': '?0', - 'sec-ch-ua-platform': 'Windows', - 'sec-fetch-site': 'none', - 'sec-fetch-mod': '', - 'sec-fetch-user': '?1', - 'accept-encoding': 'gzip, deflate', - 'accept-language': 'en-US,en;q=0.9', - }; - if ($root.kv.isAvailable(rcData)) { - const kvKey = 'ua-headers:' + String(opts.randomIndex), - headers = (await $root.kv(rcData).get(kvKey, { type: 'json' })) as UAHeaders; - - if (!$is.plainObject(headers)) { - void auditLogger.warn('UA headers failure.', { headers, error: Error('q9UTub4N') }); - return defaultHeaders; - } - return headers; - } - if ($root.fetch.isAvailable(rcData)) { - let thrown: unknown; - - const payload = await $root - .fetch(rcData, apiRoute) - .then(async (response): Promise => { - return $to.plainObject(await response.json()) as UAHeadersResponsePayload; - }) - .catch((unknownThrown: unknown): void => { - thrown = unknownThrown; - }); - if (!payload?.ok || !$is.plainObject(payload.data) || thrown) { - void auditLogger.warn('UA headers failure.', { payload, error: Error('DkkbNUJr'), thrown }); - return defaultHeaders; - } - return payload.data; - } - if (url.hostname !== apiRoute.hostname) { - let thrown: unknown; - - const payload = await fetch(apiRoute) - .then(async (response): Promise => { - return $to.plainObject(await response.json()) as UAHeadersResponsePayload; - }) - .catch((unknownThrown: unknown): void => { - thrown = unknownThrown; - }); - if (!payload?.ok || !$is.plainObject(payload.data) || thrown) { - void auditLogger.warn('UA headers failure.', { payload, error: Error('hMG9q7P5'), thrown }); - return defaultHeaders; - } - return payload.data; - } - return defaultHeaders; -}; -uaHeaders.urlSafeOptionKeys = ['randomIndex'] as string[]; diff --git a/src/root.ts b/src/root.ts index a7f6e15..6471d40 100644 --- a/src/root.ts +++ b/src/root.ts @@ -21,11 +21,7 @@ const rootPkgName = '@clevercanyon/workers.hop.gdn'; * * @returns Promise of response from root service binding. */ -export const fetch = async ( - rcData: $cfw.StdRequestContextData, // - requestInfo: $type.cfw.RequestInfo, - requestInit?: $type.cfw.RequestInit, -): Promise<$type.cfw.Response> => { +export const fetch = async (rcData: $cfw.StdRequestContextData, requestInfo: $type.cfw.RequestInfo, requestInit?: $type.cfw.RequestInit): Promise<$type.cfw.Response> => { const { env } = rcData, rt = env.RT; @@ -122,7 +118,7 @@ kv.isAvailable = (rcData: $cfw.StdRequestContextData): boolean => { export const counter = async (rcData: $cfw.StdRequestContextData, key: string): Promise => { return ((await d1(rcData).prepare('SELECT `value` FROM `counters` WHERE `key` = ?1 LIMIT 1').bind(key).first('value')) as number) || 0; }; -counter.isAvailable = d1.isAvailable; +counter.isAvailable = d1.isAvailable; // Powered by root D1 database. /** * Bumps counter value. diff --git a/tsconfig.json b/tsconfig.json index 1fd02a6..f0a9abb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,7 @@ * @note This entire file will be updated automatically. * @note Instead of editing here, please review `./tsconfig.mjs`. * - * Last generated using `./tsconfig.mjs` Mar 5, 2024 7:09 PM UTC. + * Last generated using `./tsconfig.mjs` Mar 5, 2024 7:54 PM UTC. */ { "include": ["./src/**/*", "./dev-types.d.ts"], diff --git a/wrangler.toml b/wrangler.toml index 3cdd051..9e62455 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -7,7 +7,7 @@ # @note This entire file will be updated automatically. # @note Instead of editing here, please review `./wrangler.mjs`. # -# Last generated using `./wrangler.mjs` Mar 5, 2024 7:09 PM UTC. +# Last generated using `./wrangler.mjs` Mar 5, 2024 7:54 PM UTC. ## send_metrics = false