From 9349fe51fb84b8d145372a4031a44939168941b2 Mon Sep 17 00:00:00 2001 From: Brayden Wilmoth Date: Sun, 1 Dec 2024 13:30:39 -0500 Subject: [PATCH 1/2] CORS Preflight --- src/index.ts | 7 +++++-- src/utils.ts | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0c477b5..ce9718d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { createResponse } from './utils'; import handleStudioRequest from "./studio"; import { Handler } from "./handler"; import { DatabaseStub, DataSource, RegionLocationHint, Source } from './types'; +import { corsHeaders, corsPreflight } from './cors'; export { DatabaseDurableObject } from './do'; const DURABLE_OBJECT_ID = 'sql-durable-object'; @@ -52,6 +53,9 @@ export default { const pathname = url.pathname; const isWebSocket = request.headers.get("Upgrade") === "websocket"; + // Authorize the request with CORS rules before proceeding. + corsPreflight(request); + /** * If the request is a GET request to the /studio endpoint, we can handle the request * directly in the Worker to avoid the need to deploy a separate Worker for the Studio. @@ -105,8 +109,7 @@ export default { } }; - const response = await new Handler().handle(request, dataSource, env); - return response; + return await new Handler().handle(request, dataSource, env); } catch (error) { // Return error response to client return createResponse( diff --git a/src/utils.ts b/src/utils.ts index 1b0d63a..035b97b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import { corsHeaders } from "./cors"; + export type QueryTransactionRequest = { transaction?: QueryRequest[]; } @@ -21,6 +23,7 @@ export function createJSONResponse(data: ServerResponse): Response { status: data.status, headers: { 'Content-Type': 'application/json', + ...corsHeaders }, }); } From 7266a9485dd253c20a78780eaaeb4644f96aa877 Mon Sep 17 00:00:00 2001 From: Brayden Wilmoth Date: Sun, 1 Dec 2024 13:32:50 -0500 Subject: [PATCH 2/2] Remove unused import and function --- src/index.ts | 2 +- src/utils.ts | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index ce9718d..7ef2441 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { createResponse } from './utils'; import handleStudioRequest from "./studio"; import { Handler } from "./handler"; import { DatabaseStub, DataSource, RegionLocationHint, Source } from './types'; -import { corsHeaders, corsPreflight } from './cors'; +import { corsPreflight } from './cors'; export { DatabaseDurableObject } from './do'; const DURABLE_OBJECT_ID = 'sql-durable-object'; diff --git a/src/utils.ts b/src/utils.ts index 035b97b..b554180 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -35,7 +35,3 @@ export function createResponse(result: any, error: string | undefined, status: n status, }); }; - -export function createResponseFromOperationResponse(response: { result?: any, error?: string | undefined, status: number }): Response { - return createResponse(response.result, response.error, response.status); -} \ No newline at end of file