From 393c1930cdbdd96168f954dbebb098a3050b6e9f Mon Sep 17 00:00:00 2001 From: Jason Caldwell Date: Mon, 11 Mar 2024 13:41:10 -0400 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 | 80 ++++++++++++++++----------------- package.json | 2 +- src/http.ts | 36 +++------------ src/resources/classes/logger.ts | 22 +++++---- src/type.ts | 2 + tsconfig.json | 2 +- wrangler.toml | 2 +- 16 files changed, 75 insertions(+), 95 deletions(-) diff --git a/.browserslistrc b/.browserslistrc index 6d1af228..73bd2719 100644 --- a/.browserslistrc +++ b/.browserslistrc @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. [production] node >= 20.9.0 diff --git a/.dockerignore b/.dockerignore index c049eee2..028a81c9 100644 --- a/.dockerignore +++ b/.dockerignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. # Locals diff --git a/.env.vault b/.env.vault index 1f9df1a8..e186c599 100644 --- a/.env.vault +++ b/.env.vault @@ -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=" diff --git a/.gitattributes b/.gitattributes index c3f98a81..4bf685f1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. # Default diff --git a/.gitignore b/.gitignore index 70c2f6b7..e6b37c06 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. # Locals diff --git a/.npmignore b/.npmignore index 9698eaa8..9f0e4701 100644 --- a/.npmignore +++ b/.npmignore @@ -25,7 +25,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. # Locals diff --git a/.prettierignore b/.prettierignore index 3ce55d6f..b097397a 100644 --- a/.prettierignore +++ b/.prettierignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. # Packages diff --git a/.vscode/settings.json b/.vscode/settings.json index a0354607..477b2d69 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 8, 2024 11:06 PM UTC. + * Last generated using `./settings.mjs` Mar 11, 2024 5:40 PM UTC. */ { "editor.formatOnType": false, diff --git a/.vscodeignore b/.vscodeignore index 005ccc80..ddd56a39 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -17,7 +17,7 @@ # Generated data. # -# Last generated Mar 8, 2024 11:06 PM UTC. +# Last generated Mar 11, 2024 5:40 PM UTC. # Locals diff --git a/package-lock.json b/package-lock.json index 50be1c83..175c401b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@clevercanyon/utilities", - "version": "1.0.891", + "version": "1.0.892", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@clevercanyon/utilities", - "version": "1.0.891", + "version": "1.0.892", "cpu": [ "x64", "arm64" @@ -1026,9 +1026,9 @@ } }, "node_modules/@clevercanyon/utilities": { - "version": "1.0.891", - "resolved": "https://registry.npmjs.org/@clevercanyon/utilities/-/utilities-1.0.891.tgz", - "integrity": "sha512-Ge1X63WiH8tHDov10o1RHJgdPErbmTridrgJvYsiinIbgA3y+PIhT+bCED1Ph/FtQPxyJzQ6zRbQmy3jh1LQFQ==", + "version": "1.0.892", + "resolved": "https://registry.npmjs.org/@clevercanyon/utilities/-/utilities-1.0.892.tgz", + "integrity": "sha512-CqmZDoEjUD0Kzyv19as5lAN3ABZNXqVhxjyg/UZs6hKBrocJC2Mt2p9T3UhGOJh2qBMKLTz5Mxrqo56Q8zI8eg==", "cpu": [ "x64", "arm64" @@ -1087,9 +1087,9 @@ } }, "node_modules/@clevercanyon/utilities.cfp": { - "version": "1.0.234", - "resolved": "https://registry.npmjs.org/@clevercanyon/utilities.cfp/-/utilities.cfp-1.0.234.tgz", - "integrity": "sha512-k+xsloKiqf4Pr+EgTmmqeDKVBG4H0qRYxq4Gaf/JZJgZjUfh6m57wWyQ8iByTDncAhyLHGvzV3JZu+tyPh+OLw==", + "version": "1.0.235", + "resolved": "https://registry.npmjs.org/@clevercanyon/utilities.cfp/-/utilities.cfp-1.0.235.tgz", + "integrity": "sha512-j56PjgTbjM9rspr4Qfd+K2iQdV989jQRU57/2Hb/5bYolOyTwDEFV8u9cs82sqW4REuNUbBEMoQaqA3wwR/vFQ==", "cpu": [ "x64", "arm64" @@ -1107,15 +1107,15 @@ "url": "https://github.com/sponsors/clevercanyon" }, "peerDependencies": { - "@clevercanyon/utilities": "^1.0.891", - "@clevercanyon/utilities.cfw": "^1.0.286", + "@clevercanyon/utilities": "^1.0.892", + "@clevercanyon/utilities.cfw": "^1.0.287", "@clevercanyon/utilities.web": "^1.0.152" } }, "node_modules/@clevercanyon/utilities.cfw": { - "version": "1.0.286", - "resolved": "https://registry.npmjs.org/@clevercanyon/utilities.cfw/-/utilities.cfw-1.0.286.tgz", - "integrity": "sha512-k5sehN2nAHFkc1k6q5aHKeBJZ4ASaELvvsaiD4xaZqbDM9sYcTd4mY/XOKheubHicFhD1yOAa04UQ0lLUxHp2A==", + "version": "1.0.289", + "resolved": "https://registry.npmjs.org/@clevercanyon/utilities.cfw/-/utilities.cfw-1.0.289.tgz", + "integrity": "sha512-VYihYGXFMTD5ITkK3IBUNqUyBGxjb153Snx3T3Dz1dGhi+zP/5ar+9083+NsL3WonSbhSwuMqSHCqlLmShyorA==", "cpu": [ "x64", "arm64" @@ -1134,7 +1134,7 @@ "url": "https://github.com/sponsors/clevercanyon" }, "peerDependencies": { - "@clevercanyon/utilities": "^1.0.891", + "@clevercanyon/utilities": "^1.0.892", "@cloudflare/ai": "1.0.53", "@upstash/ratelimit": "1.0.0", "@upstash/redis": "1.28.0" @@ -8154,9 +8154,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001596", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001596.tgz", - "integrity": "sha512-zpkZ+kEr6We7w63ORkoJ2pOfBwBkY/bJrG/UZ90qNb45Isblu8wzDgevEOrRL1r9dWayHjYiiyCMEXPn4DweGQ==", + "version": "1.0.30001597", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001597.tgz", + "integrity": "sha512-7LjJvmQU6Sj7bL0j5b5WY/3n7utXUJvAe1lxhsHDbLmwX9mdL86Yjtr+5SRCyf8qME4M7pU2hswj0FpyBVCv9w==", "dev": true, "funding": [ { @@ -9014,9 +9014,9 @@ } }, "node_modules/cssdb": { - "version": "7.11.1", - "resolved": "https://registry.npmjs.org/cssdb/-/cssdb-7.11.1.tgz", - "integrity": "sha512-F0nEoX/Rv8ENTHsjMPGHd9opdjGfXkgRBafSUGnQKPzGZFB7Lm0BbT10x21TMOCrKLbVsJ0NoCDMk6AfKqw8/A==", + "version": "7.11.2", + "resolved": "https://registry.npmjs.org/cssdb/-/cssdb-7.11.2.tgz", + "integrity": "sha512-lhQ32TFkc1X4eTefGfYPvgovRSzIMofHkigfH8nWtyRL4XJLsRhJFreRvEgKzept7x1rjBuy3J/MurXLaFxW/A==", "dev": true, "funding": [ { @@ -13334,9 +13334,9 @@ } }, "node_modules/hasown": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.1.tgz", - "integrity": "sha512-1/th4MHjnwncwXsIW6QMzlvYL9kG5e/CpVvLRZe4XPa8TOUNbCELqmvhDmnkNsAjwaG4+I8gJJL0JBvTTLO9qA==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", "dependencies": { "function-bind": "^1.1.2" }, @@ -26824,13 +26824,13 @@ } }, "node_modules/safe-array-concat": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/safe-array-concat/-/safe-array-concat-1.1.0.tgz", - "integrity": "sha512-ZdQ0Jeb9Ofti4hbt5lX3T2JcAamT9hfzYU1MNB+z/jaEbB6wfFfPIR/zEORmZqobkCCJhSjodobH6WHNmJ97dg==", + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/safe-array-concat/-/safe-array-concat-1.1.2.tgz", + "integrity": "sha512-vj6RsCsWBCf19jIeHEfkRMw8DPiBb+DMXklQ/1SGDHOMlHdPUkZXFQ2YdplS23zESTijAcurb1aSgJA3AgMu1Q==", "dev": true, "dependencies": { - "call-bind": "^1.0.5", - "get-intrinsic": "^1.2.2", + "call-bind": "^1.0.7", + "get-intrinsic": "^1.2.4", "has-symbols": "^1.0.3", "isarray": "^2.0.5" }, @@ -26996,17 +26996,17 @@ "dev": true }, "node_modules/set-function-length": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.1.tgz", - "integrity": "sha512-j4t6ccc+VsKwYHso+kElc5neZpjtq9EnRICFZtWyBsLojhmeF/ZBd/elqm22WJh/BziDe/SBiOeAt0m2mfLD0g==", + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.2.tgz", + "integrity": "sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==", "dev": true, "dependencies": { - "define-data-property": "^1.1.2", + "define-data-property": "^1.1.4", "es-errors": "^1.3.0", "function-bind": "^1.1.2", - "get-intrinsic": "^1.2.3", + "get-intrinsic": "^1.2.4", "gopd": "^1.0.1", - "has-property-descriptors": "^1.0.1" + "has-property-descriptors": "^1.0.2" }, "engines": { "node": ">= 0.4" @@ -31408,16 +31408,16 @@ } }, "node_modules/which-typed-array": { - "version": "1.1.14", - "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.14.tgz", - "integrity": "sha512-VnXFiIW8yNn9kIHN88xvZ4yOWchftKDsRJ8fEPacX/wl1lOvBrhsJ/OeJCXq7B0AaijRuqgzSKalJoPk+D8MPg==", + "version": "1.1.15", + "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.15.tgz", + "integrity": "sha512-oV0jmFtUky6CXfkqehVvBP/LSWJ2sy4vWMioiENyJLePrBO/yKyV9OyJySfAKosh+RYkIl5zJCNZ8/4JncrpdA==", "dev": true, "dependencies": { - "available-typed-arrays": "^1.0.6", - "call-bind": "^1.0.5", + "available-typed-arrays": "^1.0.7", + "call-bind": "^1.0.7", "for-each": "^0.3.3", "gopd": "^1.0.1", - "has-tostringtag": "^1.0.1" + "has-tostringtag": "^1.0.2" }, "engines": { "node": ">= 0.4" diff --git a/package.json b/package.json index 58e526e7..dc7734b1 100644 --- a/package.json +++ b/package.json @@ -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.", diff --git a/src/http.ts b/src/http.ts index 7c1ca422..f4c3e965 100644 --- a/src/http.ts +++ b/src/http.ts @@ -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. @@ -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. @@ -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 => { - 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. * diff --git a/src/resources/classes/logger.ts b/src/resources/classes/logger.ts index 44d93ec4..e38abb36 100644 --- a/src/resources/classes/logger.ts +++ b/src/resources/classes/logger.ts @@ -61,11 +61,13 @@ type ConfigMinutia = { maxRetryFailuresExpiresAfter: number; // In milliseconds. }; type CFW = { - subrequestCounter: { value: number }; + subrequestCounter: $type.SubrequestCounter; ctx: Readonly[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; @@ -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: { @@ -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. }, }, }); @@ -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); @@ -622,9 +628,6 @@ export const getClass = (): Constructor => { const httpPost = (): Promise => { return new Promise((resolve): void => { - if (this.cfw?.subrequestCounter) { - this.cfw.subrequestCounter.value++; - } void fetch(this.endpoint, { keepalive: true, redirect: 'manual', @@ -677,6 +680,7 @@ export const getClass = (): Constructor => { ); } }); + if (this.cfw) this.cfw.subrequestCounter.value++; }); }; return httpPost(); diff --git a/src/type.ts b/src/type.ts index adf22d7d..915b764d 100644 --- a/src/type.ts +++ b/src/type.ts @@ -54,6 +54,7 @@ export type { $RawHeadersInit as RawHeadersInit }; export type { $Request as Request }; export type { $RequestInit as RequestInit }; +export type { $SubrequestCounter as SubrequestCounter }; export type { $Response as Response }; export type { $ResponseInit as ResponseInit }; @@ -445,6 +446,7 @@ type $RawHeadersInit = HeadersInit | cfw.HeadersInit | $StrKeyable<{ [x: string] type $Request = Request | cfw.Request; type $RequestInit = (RequestInit | cfw.RequestInit) & { cache?: string }; +type $SubrequestCounter = { value: number }; type $Response = Response | cfw.Response; type $ResponseInit = ResponseInit | cfw.ResponseInit; diff --git a/tsconfig.json b/tsconfig.json index b68be214..dd12109a 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 8, 2024 11:06 PM UTC. + * Last generated using `./tsconfig.mjs` Mar 11, 2024 5:40 PM UTC. */ { "include": ["./src/**/*", "./dev-types.d.ts"], diff --git a/wrangler.toml b/wrangler.toml index 1492889c..1df8c428 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 8, 2024 11:06 PM UTC. +# Last generated using `./wrangler.mjs` Mar 11, 2024 5:40 PM UTC. ## send_metrics = false