Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createResponse } from './utils';
import handleStudioRequest from "./studio";
import { Handler } from "./handler";
import { DatabaseStub, DataSource, RegionLocationHint, Source } from './types';
import { corsPreflight } from './cors';
export { DatabaseDurableObject } from './do';

const DURABLE_OBJECT_ID = 'sql-durable-object';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { corsHeaders } from "./cors";

export type QueryTransactionRequest = {
transaction?: QueryRequest[];
}
Expand All @@ -21,6 +23,7 @@ export function createJSONResponse(data: ServerResponse): Response {
status: data.status,
headers: {
'Content-Type': 'application/json',
...corsHeaders
},
});
}
Expand All @@ -32,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);
}