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 11, 2024
1 parent eaa9b6f commit 393c193
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 95 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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 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="7GW98NfL4hM4N6HbfeDXCKreVxKvcVPupFRnZ8XJ+pomaQ=="
DOTENV_VAULT_MAIN_VERSION=1

# dev
DOTENV_VAULT_DEV="agBG5NRzDWVFI1SrYr85UUMPhYItNgsc1sRbT2appVFCX8rAzpuLvfY16XLda8ebuNPowwEEQ9bW7EwjsWnP20TTueHFPleY/CcjT92tI9frJpO2s6Jr1THyJGdlc1xDa2/E7s5uOFuqsy/cgL/74jnOjuyV+rWDrJCj/FVWvYi1SqoZ4J3canIPWdZ+KOHu8TVQltxubhaSbgfFFS/8T5MVQJswz36cEnCs/DRYRoUoUWMtWaEwSHlT5y2eFovCWw=="
DOTENV_VAULT_DEV_VERSION=411
DOTENV_VAULT_DEV="sJHSex1TRN1IboD+NQAPf9BsODnwCzJCLuLfP8e8qEOWFSBnquZmiaCWSikoyS5uscOph9rvR5YegVPi3RKwefopISxJOwIPJ5Qq2Y/1pmDozSntvuWBiIzKL6yHVQN4Y9R+YxR5Kv2tuBonZEAE3iPCrcM240NgcAK+l+iuRAieyJbCJABT8735p+UPPqrVwFt3sy7DHuzrm0VWZgI6BydXJYw6YTcA2c89VRcMLy9VedxNItmDEFSweSyTXsJDWw=="
DOTENV_VAULT_DEV_VERSION=413

# ci
DOTENV_VAULT_CI="SzgMS+Bvs+JBj1eUqDes/Rr8pxPIf/euhSKuTzjYatouHnjzvHYZrdQy4blh5d1EqX6HQ8tzVtdaMVbQRqCCR99qxE0P15POnMlLyhc3Bku0pePU6LI4WrSbFdMAi2T/P0f4XW0lAxEaO3k0e+SErSRqfaYesAr/Uxg4QQ4FKw=="
DOTENV_VAULT_CI_VERSION=411
DOTENV_VAULT_CI="uTZTlhT6bA+QV/F4p/ZD9SPRiYW+olRYL3cPp06NyozlkfkqnlX+Gtiw/fnyq6yIq0vMSjo+whnonluyABjXGcbppwewnVjx21t2iIi3xCOMuRTUPPeWa/pcp1t0fc1TaO4O+hV+ZRYK0XQso6BrTCI6FgIaPBPhLjv8SQjm4A=="
DOTENV_VAULT_CI_VERSION=413

# stage
DOTENV_VAULT_STAGE="aRP8su2YV4jZu3w1HZ/SLaots0IwJDFw75TCpvXEFeNp7tw="
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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 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 8, 2024 11:06 PM UTC.
* Last generated using `./settings.mjs` Mar 11, 2024 5:40 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 8, 2024 11:06 PM UTC.
# Last generated Mar 11, 2024 5:40 PM UTC.

# Locals

Expand Down
80 changes: 40 additions & 40 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.892",
"version": "1.0.893",
"license": "GPL-3.0-or-later",
"name": "@clevercanyon/utilities",
"description": "Utilities for JavaScript apps running in any environment.",
Expand Down
36 changes: 5 additions & 31 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import '#@initialize.ts';

import { $fnꓺmemo } from '#@standalone/index.ts';
import { $app, $crypto, $env, $fn, $gzip, $is, $mime, $obj, $path, $str, $symbol, $time, $to, $url, $user, type $type } from '#index.ts';
import { $app, $crypto, $env, $fn, $gzip, $is, $mime, $obj, $path, $str, $symbol, $time, $to, $url, type $type } from '#index.ts';

/**
* Defines types.
Expand Down Expand Up @@ -144,10 +144,12 @@ export const prepareRequest = async (request: $type.Request, config?: RequestCon
// This Miniflare behavior; i.e., `http:`, began in Wrangler 3.19.0.
// We assume the original request URL was `https:` and Miniflare is acting as a proxy.
// It’s worth noting that all our local test configurations make `https:` requests only.
url.protocol = 'https:'; // Rewrites to assumed original request URL w/ `https:`.
url.protocol = 'https:';
}
url.searchParams.delete('utx_audit_log'); // Not to be seen by any other handlers.
request = new Request(url.toString(), request as RequestInit); // Mutatable.

// Rewrites to a mutable request with revised URL.
request = new Request(url.toString(), request as RequestInit);

if (cfg.cspNonce) {
request.headers.set('x-csp-nonce', cfg.cspNonce); // Internal header.
Expand Down Expand Up @@ -1007,34 +1009,6 @@ export const parseHeaders = (parseable: $type.RawHeadersInit): $type.Headers =>
return headers;
};

/**
* Verifies a Cloudflare turnstile response.
*
* @param request HTTP request to verify.
* @param turnstile Turnstile response token.
*
* @returns True if turnstile can be verified by Cloudflare.
*
* @requiredEnv cfw
*/
export const verifyTurnstile = async (request: $type.Request, turnstile: string): Promise<boolean> => {
if (!$env.isCFW()) throw Error('SqRkpZAB');

const formData = new FormData();
formData.append('secret', $env.get('SSR_APP_TURNSTILE_SECRET_KEY', { type: 'string' }) || $env.get('APP_TURNSTILE_SECRET_KEY', { type: 'string' }));
formData.append('remoteip', await $user.ip(request));
formData.append('response', turnstile);

const verificationEndpointURL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';

return await fetch(verificationEndpointURL, { method: 'POST', body: formData })
.then(async (response): Promise<$type.Object> => {
return $to.plainObject(await response.json());
})
.then((response) => Boolean(response.success))
.catch((): boolean => false);
};

/**
* Logs a heartbeat for monitoring purposes.
*
Expand Down
22 changes: 13 additions & 9 deletions src/resources/classes/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ type ConfigMinutia = {
maxRetryFailuresExpiresAfter: number; // In milliseconds.
};
type CFW = {
subrequestCounter: { value: number };
subrequestCounter: $type.SubrequestCounter;
ctx: Readonly<Pick<$type.cfw.ExecutionContext | Parameters<$type.cfw.PagesFunction>[0], 'waitUntil'>>;
};
type WithContextOptions = Partial<{ request?: $type.Request }>;

type WithContextOptions = Partial<{
request?: $type.Request;
subrequestCounter?: $type.SubrequestCounter;
}>;
type WithContextInterface = {
withContext(subcontext?: object, subcontextOptions?: WithContextOptions): WithContextInterface;
log(message: string, subcontext?: object, level?: string): Promise<boolean>;
Expand Down Expand Up @@ -360,9 +362,12 @@ export const getClass = (): Constructor => {
/**
* Gets `request` context data.
*
* @returns `request` context data promise.
* @param request Request.
* @param subrequestCounter Subrequest counter.
*
* @returns `request` context data promise.
*/
protected async requestContext(request: $type.Request): Promise<$type.Object> {
protected async requestContext(request: $type.Request, subrequestCounter?: $type.SubrequestCounter): Promise<$type.Object> {
return jsonCloneObjectDeep({
_: {
env: {
Expand Down Expand Up @@ -391,6 +396,7 @@ export const getClass = (): Constructor => {
},
$set: {
request, // Request objects are partially redacted by our JSON middlware.
subrequestCounter, // i.e. `{ value: number }`, which counts subrequests.
},
},
});
Expand Down Expand Up @@ -442,7 +448,7 @@ export const getClass = (): Constructor => {

const withContext = $obj.mergeDeep(
jsonCloneObjectDeep(context), // Inherits current context data/opts.
contextOpts.request ? await this.requestContext(contextOpts.request) : {},
contextOpts.request ? await this.requestContext(contextOpts.request, contextOpts.subrequestCounter) : {},
jsonCloneObjectDeep(subcontext || {}), // Optionally, subcontext data.
);
return this.log(message, withContext, level);
Expand Down Expand Up @@ -622,9 +628,6 @@ export const getClass = (): Constructor => {

const httpPost = (): Promise<boolean> => {
return new Promise((resolve): void => {
if (this.cfw?.subrequestCounter) {
this.cfw.subrequestCounter.value++;
}
void fetch(this.endpoint, {
keepalive: true,
redirect: 'manual',
Expand Down Expand Up @@ -677,6 +680,7 @@ export const getClass = (): Constructor => {
);
}
});
if (this.cfw) this.cfw.subrequestCounter.value++;
});
};
return httpPost();
Expand Down
Loading

0 comments on commit 393c193

Please sign in to comment.