@@ -22,30 +22,37 @@ import { handleOptions, responseWithAllowOrigin } from "./cors";
22
22
* @returns
23
23
*/
24
24
async function handleConversionLog ( request : Request < unknown , IncomingRequestCfProperties < unknown > > , env : Env , ctx : ExecutionContext , url : URL ) : Promise < Response > {
25
- const requestID = new Date ( ) . toISOString ( ) . substring ( 0 , 19 ) . replaceAll ( '-' , '/' ) . replaceAll ( 'T' , '/' ) . replaceAll ( ':' , '' ) + '--' + crypto . randomUUID ( ) ;
26
- const loggingEnabled = Math . random ( ) < env . LZ_LOG_SAMPLE_RATE ;
25
+ try {
26
+ const requestID = new Date ( ) . toISOString ( ) . substring ( 0 , 19 ) . replaceAll ( '-' , '/' ) . replaceAll ( 'T' , '/' ) . replaceAll ( ':' , '' ) + '--' + crypto . randomUUID ( ) ;
27
+ const loggingEnabled = Math . random ( ) < env . LZ_LOG_SAMPLE_RATE ;
28
+
29
+ // Example path: [0]/[1]api/[2]v2/[3]convert/[4]zpl/[5]to/[6]pdf
30
+ const conversionPathParts = url . pathname . split ( '/' ) ;
31
+ const sourceFormat = conversionPathParts [ 4 ] . toLowerCase ( ) ;
32
+ const targetFormat = conversionPathParts [ 6 ] . toLowerCase ( ) ;
27
33
28
- // Example path: [0]/[1]api/[2]v2/[3]convert/[4]zpl/[5]to/[6]pdf
29
- const conversionPathParts = url . pathname . split ( '/' ) ;
30
- const sourceFormat = conversionPathParts [ 4 ] ;
31
- const targetFormat = conversionPathParts [ 6 ] ;
32
-
33
- // Clone and log request asynchronously
34
- if ( loggingEnabled ) ctx . waitUntil ( Promise . all ( [
35
- env . LZ_R2_BUCKET . put ( requestID + `/in.${ sourceFormat } ` , request . clone ( ) . body ) ,
36
- env . LZ_R2_BUCKET . put ( requestID + '/params.json' , url . searchParams . get ( 'params' ) )
37
- ] ) ) ;
38
-
39
- // Generate response
40
- const response = await proxyRequestToBackend ( request , url , env , requestID ) ;
41
-
42
- // Clone and log response asynchronously
43
- if ( loggingEnabled ) ctx . waitUntil (
44
- env . LZ_R2_BUCKET . put ( requestID + `/out.${ targetFormat } ` , response . clone ( ) . body )
45
- ) ;
46
-
47
- // Return response to client
48
- return response ;
34
+ // TODO: Unwrap Base64 (if applicable) before storing in R2
35
+
36
+ // Clone and log request asynchronously
37
+ if ( loggingEnabled ) ctx . waitUntil ( Promise . all ( [
38
+ env . LZ_R2_BUCKET . put ( requestID + `/in.${ sourceFormat } ` , request . clone ( ) . body ) , // TODO: Set content type
39
+ env . LZ_R2_BUCKET . put ( requestID + '/params.json' , url . searchParams . get ( 'params' ) , { httpMetadata :{ contentType :'application/json' } } )
40
+ ] ) ) ;
41
+
42
+ // Generate response
43
+ const response = await proxyRequestToBackend ( request , url , env , requestID ) ;
44
+
45
+ // Clone and log response asynchronously
46
+ if ( loggingEnabled ) ctx . waitUntil (
47
+ env . LZ_R2_BUCKET . put ( requestID + `/out.${ targetFormat } ` , response . clone ( ) . body ) // TODO: Set content type
48
+ ) ;
49
+
50
+ // Return response to client
51
+ return response ;
52
+ } catch ( error ) {
53
+ console . error ( "error logging conversion data" , error ) ;
54
+ return await proxyRequestToBackend ( request , url , env ) ;
55
+ }
49
56
}
50
57
51
58
/**
0 commit comments