@@ -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
0 commit comments