Skip to content

Commit

Permalink
Project update. [p][robotic]
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswrks committed Mar 7, 2024
1 parent b7af04e commit 8aa7f63
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .browserslistrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

[production]
node >= 20.9.0
Expand Down
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

# Locals

Expand Down
8 changes: 4 additions & 4 deletions .env.vault
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ DOTENV_VAULT_MAIN="3rfi+ClkONvfHPiU9FTBcC1+Um/L8QeM9dRw1jFzEqzZXw=="
DOTENV_VAULT_MAIN_VERSION=1

# dev
DOTENV_VAULT_DEV="jQzPzFCg4gSEeYluy2ieuSH+VNEgn+/dJhdAwwB7rpybk7pz11snctZc/CiINsDW/RfKWelwXMtIUB5OU8AURGn6jaj189M7N0WPb0A1WELdaw67LCEspJLsZ4dVUJz0rUcUWXogSAFrV8/LBRlC3ImAO+PGnIh+8eR+Dy4l2+5VuvFaUfcI4Qc4GlsjB40AL7N84ONFT4IqvMfBhwptK7Y+1/bOte13MZFzgDCp7qDQaQxTEvbWqAZNTL+M+E9ithPL6FDN92WseDHdT72xPe1f5kT89VN5yq5gcen7ztqZprCKVWOE2kpBMcxK57DyUZMw7c5Hzgy0I0asZ0Sc6YS926iQMCABQSMQQPHX7E3cz3IgBp+l4MohC5S6vGlfUzE0tNkitkTM7f+yzbROGwa2gi7VYY3EXrTAP+xb2cJWV+SAVyyPDALwEayFGXO5xZYWfbKa6oP/dWWxK0HIFJtGI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

# stage
DOTENV_VAULT_STAGE="bfqZYD8oVLWMZ0JR5LICV+sB/btaXavszxZLxTfwSH8U5fg="
Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

# Default

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

# Locals

Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

# Locals

Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

# Packages

Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 6, 2024 9:04 PM UTC.
* Last generated using `./settings.mjs` Mar 7, 2024 12:46 PM UTC.
*/
{
"editor.formatOnType": false,
Expand Down
2 changes: 1 addition & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Mar 6, 2024 9:04 PM UTC.
# Last generated Mar 7, 2024 12:46 PM UTC.

# Locals

Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "1.0.280",
"version": "1.0.281",
"license": "GPL-3.0-or-later",
"name": "@clevercanyon/utilities.cfw",
"description": "Utilities for JavaScript apps running in a Cloudflare Worker environment.",
Expand Down
115 changes: 4 additions & 111 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import '#@initialize.ts';

import { $cfw, $root, cfw } from '#index.ts';
import { $arr, $crypto, $env, $gzip, $http, $is, $mime, $obj, $str, $time, $to, $url, type $type } from '@clevercanyon/utilities';
import { $arr, $crypto, $env, $gzip, $http, $is, $mime, $obj, $str, $time, $url, type $type } from '@clevercanyon/utilities';

/**
* Defines types.
Expand All @@ -28,30 +28,6 @@ type RequiredFetchOptions = Required<FetchOptions> & {
proxy: Required<FetchOptions['proxy']>;
headers: $type.cfw.Headers;
};
export type UAHeaderOptions = {
randomIndex?: number;
};
export type UAHeaders = $type.ReadonlyDeep<{
'user-agent': string;

'accept': string;
'accept-encoding': string;
'accept-language': string;

'sec-ch-ua': string;
'sec-ch-ua-mobile': string;
'sec-ch-ua-platform': string;
'sec-fetch-site': string;
'sec-fetch-mod': string;
'sec-fetch-user': string;

'upgrade-insecure-requests': string;
}>;
type UAHeadersResponsePayload = $type.ReadonlyDeep<{
ok: boolean;
error?: { message: string };
data?: UAHeaders;
}>;

/**
* Performs an HTTP fetch using a proxy.
Expand Down Expand Up @@ -86,7 +62,7 @@ export const fetch = async (rcData: $cfw.StdRequestContextData, parseable: $type
opts.headers = $http.parseHeaders(opts.headers) as $type.cfw.Headers;

if (!opts.headers.has('user-agent'))
for (const [name, value] of Object.entries(await uaHeaders(rcData))) {
for (const [name, value] of Object.entries(await $root.uaHeaders(rcData))) {
opts.headers.set(name, value);
}
if (opts.uaBotAppend /* e.g., `SomeCoolBot/1.0.0` */) {
Expand All @@ -111,9 +87,9 @@ export const fetch = async (rcData: $cfw.StdRequestContextData, parseable: $type
*
* @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> => {
export const worker = async (rcData: $cfw.StdRequestContextData, requestInfo: $type.cfw.RequestInfo, requestInit?: $type.cfw.RequestInit): Promise<$type.cfw.Response> => {
const { fetch, Request } = cfw,
proxyRoute = 'https://workers.o5p.me/worker-proxy/';
proxyRoute = 'https://proxy.c11n.workers.dev/';

if ($is.string(requestInfo) || $is.url(requestInfo)) {
requestInfo = $url.addQueryVar('url', requestInfo.toString(), proxyRoute);
Expand All @@ -124,89 +100,6 @@ export const fetchWorker = async (rcData: $cfw.StdRequestContextData, requestInf
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<UAHeaders> => {
const { fetch } = cfw,
{ url, auditLogger } = rcData,
//
opts = $obj.defaults({}, options || {}, {
randomIndex: $crypto.randomNumber(1, 100),
}) as Required<UAHeaderOptions>,
//
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<UAHeadersResponsePayload> => {
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<UAHeadersResponsePayload> => {
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.

Expand Down
Loading

0 comments on commit 8aa7f63

Please sign in to comment.