@@ -22,37 +22,37 @@ import { handleOptions, responseWithAllowOrigin } from "./cors";
2222 * @returns
2323 */
2424async function handleConversionLog ( request : Request < unknown , IncomingRequestCfProperties < unknown > > , env : Env , ctx : ExecutionContext , url : URL ) : Promise < Response > {
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 ( ) ;
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 ( ) ;
3333
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- }
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+ }
5656}
5757
5858/**
@@ -62,20 +62,20 @@ async function handleConversionLog(request: Request<unknown, IncomingRequestCfPr
6262 * @returns
6363 */
6464async function proxyRequestToBackend ( request : Request , url : URL , env : Env , requestID = '' ) : Promise < Response > {
65- const backendUrl = env . LZ_PROD_API_BASE_URL + url . pathname + url . search ;
66- const newRequest = new Request ( backendUrl , request ) ;
67- let ip = request . headers . get ( "X-Forwarded-For" ) ?? '' ;
68- if ( ! ip ) {
69- ip = request . headers . get ( "Cf-Connecting-Ip" ) ?? '' ;
70- }
71- newRequest . headers . set ( 'X-LZ-IP' , ip ) ;
72- newRequest . headers . set ( 'X-LZ-Secret-Key' , env . LZ_PROD_API_SECRET_KEY )
73- if ( requestID ) newRequest . headers . set ( "X-LZ-Request-ID" , requestID ) ;
74- const response = await fetch ( newRequest ) ;
65+ const backendUrl = env . LZ_PROD_API_BASE_URL + url . pathname + url . search ;
66+ const newRequest = new Request ( backendUrl , request ) ;
67+ let ip = request . headers . get ( "X-Forwarded-For" ) ?? '' ;
68+ if ( ! ip ) {
69+ ip = request . headers . get ( "Cf-Connecting-Ip" ) ?? '' ;
70+ }
71+ newRequest . headers . set ( 'X-LZ-IP' , ip ) ;
72+ newRequest . headers . set ( 'X-LZ-Secret-Key' , env . LZ_PROD_API_SECRET_KEY )
73+ if ( requestID ) newRequest . headers . set ( "X-LZ-Request-ID" , requestID ) ;
74+ const response = await fetch ( newRequest ) ;
7575
7676 // Force redirects to be relative because I couldn't get it to work in Spring Boot
7777 if ( response . status === 301 || response . status === 302 ) {
78- const locationHeader = response . headers . get ( 'Location' ) ?? '' ;
78+ const locationHeader = response . headers . get ( 'Location' ) ?? '' ;
7979 if ( locationHeader . includes ( 'labelzoom.net/' ) ) {
8080 const url = new URL ( locationHeader ) ;
8181
@@ -85,44 +85,44 @@ async function proxyRequestToBackend(request: Request, url: URL, env: Env, reque
8585 }
8686 }
8787
88- return response ;
88+ return response ;
8989}
9090
9191export default {
92- async fetch ( request , env , ctx ) : Promise < Response > {
93- const url = new URL ( request . url ) ;
92+ async fetch ( request , env , ctx ) : Promise < Response > {
93+ const url = new URL ( request . url ) ;
9494
95- // API modifiers
96- if ( url . pathname . startsWith ( '/api/' ) ) {
97- if ( request . method === "OPTIONS" ) {
98- // Handle CORS preflight requests
99- return handleOptions ( request , env ) ;
100- }
101- if ( url . pathname . startsWith ( '/api/v2/convert/' ) ) {
102- return responseWithAllowOrigin (
103- await handleConversionLog ( request , env , ctx , url ) ,
104- request . headers . get ( 'Origin' ) ?? '*'
105- ) ;
106- }
95+ // API modifiers
96+ if ( url . pathname . startsWith ( '/api/' ) ) {
97+ if ( request . method === "OPTIONS" ) {
98+ // Handle CORS preflight requests
99+ return handleOptions ( request , env ) ;
100+ }
101+ if ( url . pathname . startsWith ( '/api/v2/convert/' ) ) {
102+ return responseWithAllowOrigin (
103+ await handleConversionLog ( request , env , ctx , url ) ,
104+ request . headers . get ( 'Origin' ) ?? '*'
105+ ) ;
106+ }
107107
108- // Fallthrough behavior, proxy request to Spring Web
109- return responseWithAllowOrigin (
110- await proxyRequestToBackend ( request , url , env ) ,
111- request . headers . get ( 'Origin' ) ?? '*'
112- ) ;
113- } else if ( url . pathname === '/api' ) {
114- // Force trailing slash after /api
115- url . pathname = '/api/' ;
116- return Response . redirect ( url . toString ( ) ) ;
117- }
108+ // Fallthrough behavior, proxy request to Spring Web
109+ return responseWithAllowOrigin (
110+ await proxyRequestToBackend ( request , url , env ) ,
111+ request . headers . get ( 'Origin' ) ?? '*'
112+ ) ;
113+ } else if ( url . pathname === '/api' ) {
114+ // Force trailing slash after /api
115+ url . pathname = '/api/' ;
116+ return Response . redirect ( url . toString ( ) ) ;
117+ }
118118
119- // TODO: Eventually we'll wall off access to the rest of the endpoints once everything has been fully migrated
120- // return new Response(`Not found`, { status: 404 });
119+ // TODO: Eventually we'll wall off access to the rest of the endpoints once everything has been fully migrated
120+ // return new Response(`Not found`, { status: 404 });
121121
122- // Fallthrough behavior, proxy request to Spring Web
123- return responseWithAllowOrigin (
124- await proxyRequestToBackend ( request , url , env ) ,
125- request . headers . get ( 'Origin' ) ?? '*'
126- ) ;
127- } ,
122+ // Fallthrough behavior, proxy request to Spring Web
123+ return responseWithAllowOrigin (
124+ await proxyRequestToBackend ( request , url , env ) ,
125+ request . headers . get ( 'Origin' ) ?? '*'
126+ ) ;
127+ } ,
128128} satisfies ExportedHandler < Env > ;
0 commit comments