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 5, 2024
1 parent 9bcfb44 commit bf0e93f
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 106 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 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="5iUNJilhSTYkuldheih4Lejvy/oSQa2LgZCHlU5rEk10sCJ1JlLXxHKlXiWXRJ/BexNBxPM1WgO8qRoGjLVogQDV2WaoPzBH9h6MWmOaNa4+Ly4B8cJ24QD6UxEit0+/EKLLXmAmNCV3R0bPeP+NRtkKHaXjpFplOAwsOLnFMCVxMazQXWNXRceN+Sv+u3sHxgz+C2khvtlMW48Ipz/NrqRz6Pl8WaCIG7JlDzWSyAjslUoc4bMb3PfJ0ZpowzzMC9LKgTjQvfsIw4mP2GhXkVyghCIc58LUnqT+JZmVYrzHcjJ8Mp8HMRr1/5Um1m0Q6ulDahOewRGujhlZPdOe3SoLf0LNsjTg+nhTJw0H+FX97sfMyZ6TcaGQOOO3Ig8wMlnnBxq4e7JJvleG0TVEufnIf2opJ62lg5HclPkF47uf8jXwSe5xDPXfxITPUQM56gevF86fxt9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=239

# 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 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 5, 2024 7:09 PM UTC.
* Last generated using `./settings.mjs` Mar 5, 2024 7:54 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 5, 2024 7:09 PM UTC.
# Last generated Mar 5, 2024 7:54 PM UTC.

# Locals

Expand Down
10 changes: 5 additions & 5 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.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.",
Expand Down
187 changes: 107 additions & 80 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.

/**
* Creates a timeout promise.
*
Expand Down Expand Up @@ -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<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[];
8 changes: 2 additions & 6 deletions src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -122,7 +118,7 @@ kv.isAvailable = (rcData: $cfw.StdRequestContextData): boolean => {
export const counter = async (rcData: $cfw.StdRequestContextData, key: string): Promise<number> => {
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.
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.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 `./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"],
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
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 `./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
Expand Down

0 comments on commit bf0e93f

Please sign in to comment.