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 Dec 18, 2023
1 parent bbbc990 commit da84a9b
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 105 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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM 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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM UTC.

# Locals

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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM 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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM 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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM 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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM 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` Dec 18, 2023 2:30 AM UTC.
* Last generated using `./settings.mjs` Dec 18, 2023 6:09 AM 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 Dec 18, 2023 2:30 AM UTC.
# Last generated Dec 18, 2023 6:09 AM UTC.

# Locals

Expand Down
150 changes: 75 additions & 75 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 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.143",
"version": "1.0.144",
"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 @@ -54,14 +54,14 @@
},
"dependencies": {},
"peerDependencies": {
"@clevercanyon/utilities": "^1.0.622",
"@clevercanyon/utilities": "^1.0.624",
"@cloudflare/kv-asset-handler": "0.2.0"
},
"peerDependenciesMeta": {},
"optionalDependencies": {},
"bundleDependencies": [],
"devDependencies": {
"@clevercanyon/dev-deps": "^1.0.304"
"@clevercanyon/dev-deps": "^1.0.305"
},
"overrides": {},
"cpu": ["x64", "arm64"],
Expand Down
46 changes: 29 additions & 17 deletions src/cfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import '#@initialize.ts';

import { $env, $error, $http, $json, $mime, $mm, $str, $url, type $type } from '@clevercanyon/utilities';
import { $app, $env, $error, $http, $json, $mime, $mm, $str, $url, type $type } from '@clevercanyon/utilities';
import * as cfKVA from '@cloudflare/kv-asset-handler';

/**
Expand Down Expand Up @@ -113,35 +113,47 @@ export const handleFetchEvent = async (ifeData: InitialFetchEventData): Promise<
* @returns Response promise.
*/
export const handleFetchCache = async (route: Route, feData: FetchEventData): Promise<$type.cf.Response> => {
const { request, ctx } = feData;
let cachedResponse; // Initialize.
let key, cachedResponse; // Initialize.
const { request, ctx, url } = feData;

// Populates cache key.

if (!$http.requestHasCacheableMethod(request)) {
return route(feData); // Not applicable.
key = 'v=' + $app.buildTime().unix().toString();
if (request.headers.has('origin') /* Possibly empty. */) {
key += '&origin=' + (request.headers.get('origin') || '');
}
if ((cachedResponse = await cache.match(request, { ignoreMethod: true }))) {
if (!$http.requestNeedsContentBody(request, cachedResponse.status)) {
cachedResponse = new Response(null /* No response body. */, {
status: cachedResponse.status,
statusText: cachedResponse.statusText,
headers: cachedResponse.headers,
}) as unknown as $type.cf.Response;
const keyURL = $url.removeCSOQueryVars(url); // e.g., `ut[mx]_`, `_ck`, etc.
keyURL.searchParams.set('_ck', key), keyURL.searchParams.sort(); // Optimizes cache.
const keyRequest = new Request(keyURL.toString(), request as unknown as Request) as unknown as $type.cf.Request;

// Checks if request is cacheable.

if (!['HEAD', 'GET'].includes(keyRequest.method) || !$http.requestHasCacheableMethod(keyRequest)) {
return route(feData); // Not cacheable; use async route.
}
// Reads response for this request from HTTP cache.

if ((cachedResponse = await cache.match(keyRequest, { ignoreMethod: true }))) {
if (!$http.requestNeedsContentBody(keyRequest, cachedResponse.status)) {
cachedResponse = new Response(null, cachedResponse) as unknown as $type.cf.Response;
}
return cachedResponse;
}
const response = await route(feData);
// Routes request and writes response to HTTP cache.

const response = await route(feData); // Awaits response so we can cache.

if ('GET' === request.method && 206 !== response.status && '*' !== response.headers.get('vary') && !response.webSocket) {
if ('GET' === keyRequest.method && 206 !== response.status && '*' !== response.headers.get('vary') && !response.webSocket) {
if ($env.isCFWViaMiniflare() && 'no-store' === response.headers.get('cdn-cache-control')) {
// Miniflare doesn’t currently support `cdn-cache-control`, so we implement basic support for it here.
response.headers.set('cf-cache-status', 'c10n.miniflare.cdn-cache-control.BYPASS');
} else {
// Cloudflare will not actually cache if response headers say not to cache.
// For further details regarding `cache.put()`; {@see https://o5p.me/gMv7W2}.
ctx.waitUntil(cache.put(request, response.clone()));
ctx.waitUntil(cache.put(keyRequest, response.clone()));
}
}
return response;
return response; // Potentially cached async.
};

/**
Expand All @@ -165,7 +177,7 @@ export const handleFetchDynamics = async (feData: FetchEventData): Promise<$type
$env.get('__STATIC_CONTENT' /* Worker site? */) && //
$mm.test(url.pathname, $url.pathFromAppBase('./assets/') + '**')
) {
return handleFetchCache(handleFetchStaticAssets, feData);
return handleFetchStaticAssets(feData);
}
return $http.prepareResponse(request, { status: 404 }) as $type.cf.Response;
};
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` Dec 18, 2023 2:30 AM UTC.
* Last generated using `./tsconfig.mjs` Dec 18, 2023 6:09 AM 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` Dec 18, 2023 2:30 AM UTC.
# Last generated using `./wrangler.mjs` Dec 18, 2023 6:09 AM UTC.
##

send_metrics = false
Expand Down

0 comments on commit da84a9b

Please sign in to comment.