Skip to content

Commit d40b127

Browse files
committed
Fix worker script to handle cache
1 parent 9aa0156 commit d40b127

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

worker/worker.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async function redirectionio_fetch(request, event) {
1111
add_rule_ids_header: REDIRECTIONIO_ADD_HEADER_RULE_IDS === 'true',
1212
version: REDIRECTIONIO_VERSION || 'redirection-io-cloudflare/dev',
1313
instance_name: REDIRECTIONIO_INSTANCE_NAME || 'undefined',
14+
cache_time: REDIRECTIONIO_CACHE_TIME || 0,
1415
}
1516

1617
if (options.token === null) {
@@ -20,20 +21,23 @@ async function redirectionio_fetch(request, event) {
2021
const libredirectionio = wasm_bindgen;
2122
await wasm_bindgen(wasm);
2223

23-
const redirectionioRequest = create_redirectionio_request(request);
24-
const [action, registerCachePromise] = await get_action(request, redirectionioRequest);
25-
const response = await proxy(request, redirectionioRequest, action, libredirectionio, options);
24+
const redirectionioRequest = create_redirectionio_request(request, libredirectionio);
25+
const [action, registerCachePromise] = await get_action(request, redirectionioRequest, options, libredirectionio);
26+
const response = await proxy(request, redirectionioRequest, action, options, libredirectionio);
2627
const clientIP = request.headers.get("CF-Connecting-IP");
2728

2829
event.waitUntil(async function () {
29-
await registerCachePromise;
30-
await log(response, redirectionioRequest, action, libredirectionio, options, clientIP);
30+
if (registerCachePromise !== null) {
31+
await registerCachePromise;
32+
}
33+
34+
await log(response, redirectionioRequest, action, libredirectionio, options, clientIP || "");
3135
}());
3236

3337
return response;
3438
}
3539

36-
function create_redirectionio_request(request) {
40+
function create_redirectionio_request(request, libredirectionio) {
3741
const urlObject = new URL(request.url);
3842
const redirectionioRequest = new libredirectionio.Request(urlObject.pathname + urlObject.search, urlObject.host, urlObject.protocol.includes('https') ? 'https' : 'http', request.method);
3943

@@ -44,7 +48,7 @@ function create_redirectionio_request(request) {
4448
return redirectionioRequest;
4549
}
4650

47-
async function get_action(request, redirectionioRequest) {
51+
async function get_action(request, redirectionioRequest, options, libredirectionio) {
4852
const cache = caches.default;
4953
const cacheUrl = new URL(request.url)
5054
cacheUrl.pathname = "/get-action/" + redirectionioRequest.get_hash().toString()
@@ -55,10 +59,12 @@ async function get_action(request, redirectionioRequest) {
5559
method: "GET",
5660
});
5761

58-
let actionStr = await cache.match(cacheKey);
62+
let response = await cache.match(cacheKey);
63+
let registerCachePromise = null;
64+
let actionStr = '';
5965

60-
if (!actionStr) {
61-
const response = await Promise.race([
66+
if (!response) {
67+
response = await Promise.race([
6268
fetch('https://agent.redirection.io/' + options.token + '/action', {
6369
method: 'POST',
6470
body: redirectionioRequest.serialize().toString(),
@@ -73,9 +79,16 @@ async function get_action(request, redirectionioRequest) {
7379
]);
7480

7581
actionStr = await response.text();
76-
}
7782

78-
const registerCachePromise = cache.put(cacheKey, actionStr);
83+
if (options.cache_time > 0) {
84+
const cacheResponse = new Response(new Blob([actionStr], { type: "application/json" }), response);
85+
cacheResponse.headers.append("Cache-Control", `public, max-age=${options.cache_time}`);
86+
87+
registerCachePromise = cache.put(cacheKey, cacheResponse);
88+
}
89+
} else {
90+
actionStr = await response.text();
91+
}
7992

8093
if (actionStr === "") {
8194
return [libredirectionio.Action.empty(), registerCachePromise]
@@ -86,13 +99,12 @@ async function get_action(request, redirectionioRequest) {
8699
} catch (e) {
87100
console.error(e);
88101

89-
return [new libredirectionio.Action.empty(), registerCachePromise];
102+
return [libredirectionio.Action.empty(), registerCachePromise];
90103
}
91104
}
92105

93106
/* Redirection.io logic */
94-
async function proxy(request, redirectionioRequest, action, libredirectionio, options) {
95-
107+
async function proxy(request, redirectionioRequest, action, options, libredirectionio) {
96108
try {
97109
const statusCodeBeforeResponse = action.get_status_code(0);
98110

wrangler.toml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ vars.REDIRECTIONIO_ADD_HEADER_RULE_IDS = false
99
vars.REDIRECTIONIO_INSTANCE_NAME = "instance_name_on_redirectionio"
1010
vars.REDIRECTIONIO_TIMEOUT = 5000
1111
vars.REDIRECTIONIO_VERSION = "redirection-io-cloudflare/dev"
12+
vars.REDIRECTIONIO_CACHE_TIME = 0

0 commit comments

Comments
 (0)