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 8aa7f63 commit e89bf8f
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 54 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 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="fCmtXbhrizbJN9K3cu3hAX70BvX6I4xCY4ZJxwat0s28QNWyT0A0yoC0jPgC8riX+vlu82DeEc9ZfpwTyyN8B0CXT65Guu3hmdXx76nqiAPeDSNRQbOSMX7uggOIUp+0j7Ue4WcTBP0xQq1+ID7Q/DHNLHqcqP6jyg2cAxHXQ6W7FOfoo2OKMyyCxyWRRQd4N2hEUTo3ByBAYrTxszMBpz7SjwYbcDkP3T9rnOVzXeeiZckWGOpydKvAkaJZQOg8bA0BEip28p31v461MkGGXa08P1c2EX3bCcNnyPOmJal17iRRmzhxDYdGIMJbIhC3FBiocBp/yTdfose7TN6LYm8GSHoze69firTcMHg3ScBhTPiT8s0V2b7Z9dA8jo8J1zD0VJfS20tE76Kt/S6tbcLnqhButsl2my4HMwZYVB4VwxPFqJ0hh8rYgNF12uc3EjwBsqrOM6TI5HItDSMv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I_VERSION=259

# 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
* Last generated using `./settings.mjs` Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
# Last generated Mar 7, 2024 9:03 PM UTC.

# Locals

Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "1.0.281",
"version": "1.0.283",
"license": "GPL-3.0-or-later",
"name": "@clevercanyon/utilities.cfw",
"description": "Utilities for JavaScript apps running in a Cloudflare Worker environment.",
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"dependencies": {},
"peerDependencies": {
"@clevercanyon/utilities": "^1.0.887",
"@clevercanyon/utilities": "^1.0.890",
"@cloudflare/ai": "1.0.53",
"@upstash/ratelimit": "1.0.0",
"@upstash/redis": "1.28.0"
Expand Down
4 changes: 4 additions & 0 deletions src/cfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type StdRequestContextData = Readonly<{

auditLogger: $type.LoggerInterface;
consentLogger: $type.LoggerInterface;
subrequestCounter: { value: number };
}>;

/**
Expand Down Expand Up @@ -139,6 +140,7 @@ export const handleFetchEvent = async (ircData: InitialRequestContextData): Prom

auditLogger,
consentLogger,
subrequestCounter: { value: 0 },
});
let response: Promise<$type.cfw.Response>; // Initialize.

Expand Down Expand Up @@ -327,6 +329,7 @@ const handleFetchCache = async (rcData: RequestContextData, route: Route): Promi
}
// Reads response for this request from HTTP cache.

rcData.subrequestCounter.value++;
if ((cachedResponse = await caches.default.match(keyRequest, { ignoreMethod: true }))) {
return $http.prepareCachedResponse(keyRequest, cachedResponse) as Promise<$type.cfw.Response>;
}
Expand All @@ -350,6 +353,7 @@ const handleFetchCache = async (rcData: RequestContextData, route: Route): Promi
// Cloudflare will not actually cache if headers say not to; {@see https://o5p.me/gMv7W2}.
const responseForCache = (await $http.prepareResponseForCache(keyRequest, response)) as $type.cfw.Response;
await caches.default.put(keyRequest, responseForCache);
rcData.subrequestCounter.value++;
})(),
);
response.headers.set('x-cache-status', 'miss'); // i.e., Cache miss.
Expand Down
3 changes: 3 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const worker = async (rcData: $cfw.StdRequestContextData, requestInfo: $t
} else if (requestInfo instanceof Request) {
requestInfo = new Request($url.addQueryVar('url', requestInfo.url, proxyRoute), requestInfo);
}
rcData.subrequestCounter.value++;
return fetch(requestInfo, requestInit);
};

Expand Down Expand Up @@ -146,6 +147,8 @@ const fetchꓺviaSocket = async (rcData: $cfw.StdRequestContextData, url: $type.
// ---
// Socket setup.

rcData.subrequestCounter.value++;

const socket = sockets.connect({
hostname: opts.proxy.host,
port: opts.proxy.port,
Expand Down
7 changes: 5 additions & 2 deletions src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const instance = $fn.memo(
enableTelemetry: false, // Just for good measure. The environment variable is what’s important.

retry: {
retries: 5,
retries: 5, // Maximum upstash retry attempts.
backoff: (retryAttempts: number) => Math.exp(retryAttempts) * 50,
/**
* Regarding retry attempts and timeouts.
Expand All @@ -110,7 +110,6 @@ export const instance = $fn.memo(
},
signal: undefined, // Not used at this time.
},
// Constructor always reads env variable for this specific setting.
{ UPSTASH_DISABLE_TELEMETRY: '1' }, // Don’t report back to Upstash.
);
},
Expand All @@ -135,9 +134,11 @@ export const rateLimiter = (rcData: $cfw.StdRequestContextData, options?: RateLi
limiter, // RateLimiterCore.

async limit(...args: Parameters<RateLimiterCore['limit']>): ReturnType<RateLimiterCore['limit']> {
rcData.subrequestCounter.value += 2; // Possible read and write requests.
const limiterResponse = await limiter.limit(...args);

if ($is.promise(limiterResponse.pending)) {
rcData.subrequestCounter.value++; // Adds additional pending request to counter.
ctx.waitUntil(limiterResponse.pending); // e.g., Analytics, multiregion sync.
}
if (!limiterResponse.success) {
Expand All @@ -163,9 +164,11 @@ export const rateLimiter = (rcData: $cfw.StdRequestContextData, options?: RateLi
return limiterResponse;
},
async blockUntilReady(...args: Parameters<RateLimiterCore['blockUntilReady']>): ReturnType<RateLimiterCore['blockUntilReady']> {
rcData.subrequestCounter.value += 2; // Possible read and write requests.
const limiterResponse = await limiter.blockUntilReady(...args);

if ($is.promise(limiterResponse.pending)) {
rcData.subrequestCounter.value++; // Adds additional pending request to counter.
ctx.waitUntil(limiterResponse.pending); // e.g., Analytics, multiregion sync.
}
if (!limiterResponse.success) {
Expand Down
31 changes: 7 additions & 24 deletions src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const service = async (rcData: $cfw.StdRequestContextData, requestInfo: $

if (!rt) throw Error('Root service binding unavailable.');

rcData.subrequestCounter.value++;
return rt.fetch(await $cfw.serviceBindingRequest(rcData, requestInfo, requestInit));
};
service.isAvailable = (rcData: $cfw.StdRequestContextData): boolean => {
Expand Down Expand Up @@ -147,6 +148,8 @@ kv.isAvailable = (rcData: $cfw.StdRequestContextData): boolean => {
* @returns Promise of root UA headers.
*/
export const uaHeaders = async (rcData: $cfw.StdRequestContextData, options?: UAHeaderOptions): Promise<UAHeaders> => {
rcData.subrequestCounter.value++;

const opts = $obj.defaults({}, options || {}, { randomIndex: $crypto.randomNumber(1, 100) }) as Required<UAHeaderOptions>,
kvKey = 'ua-headers:' + String($to.integerBetween(opts.randomIndex, 1, 100)),
headers = (await kv(rcData).get(kvKey, { type: 'json' })) as UAHeaders;
Expand All @@ -170,20 +173,8 @@ uaHeaders.isAvailable = kv.isAvailable;
* @returns Promise of root counter value.
*/
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
);
rcData.subrequestCounter.value++;
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; // Powered by root D1 database.

Expand All @@ -194,14 +185,6 @@ counter.isAvailable = d1.isAvailable; // Powered by root D1 database.
* @param by By; default is `1`.
*/
export const bumpCounter = async (rcData: $cfw.StdRequestContextData, key: string, by: number = 1): Promise<void> => {
await d1(rcData)
.prepare(
'INSERT INTO `counters` (`key`, `value`)' +
' VALUES(?1, ?2)' +
//
' ON CONFLICT(`key`) DO UPDATE' +
' SET `value` = `value` + ?2',
)
.bind(key, by)
.run();
rcData.subrequestCounter.value++;
await d1(rcData).prepare('INSERT INTO `counters` (`key`, `value`) VALUES(?1, ?2) ON CONFLICT(`key`) DO UPDATE SET `value` = `value` + ?2').bind(key, by).run();
};
1 change: 1 addition & 0 deletions src/tests/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('$redis', async () => {

auditLogger: mockLoggerInterface,
consentLogger: mockLoggerInterface,
subrequestCounter: { value: 0 },
},
redis = $redis.instance();

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 7, 2024 12:46 PM UTC.
* Last generated using `./tsconfig.mjs` Mar 7, 2024 9:03 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 7, 2024 12:46 PM UTC.
# Last generated using `./wrangler.mjs` Mar 7, 2024 9:03 PM UTC.
##

send_metrics = false
Expand Down

0 comments on commit e89bf8f

Please sign in to comment.