Skip to content

Commit

Permalink
refactor: no longer hard code spaUrl for sheet URLS
Browse files Browse the repository at this point in the history
Use origin instead. Doesn't really impact anything except for the URL's stored in the history since front end was using getHash for everything anyway
  • Loading branch information
mgreminger committed Jan 27, 2023
1 parent 5bf4a89 commit 6e8b4a9
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/database/_worker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { getHash, API_GET_PATH, API_SAVE_PATH } from "./utility";

const spaUrl = "https://engineeringpaper.xyz";
const maxSize = 2000000; // max length of byte string that represents sheet

export const API_MANUAL_SAVE_PATH = "/documents/manual-save";

const cspHeaderValue = "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:;";

// local dev mode requires some extra exceptions for live reload
const devCspHeaderValue = cspHeaderValue + " script-src 'self' http://localhost:35729; connect-src 'self' ws://localhost:35729;";

export const API_MANUAL_SAVE_PATH = "/documents/manual-save";


type Flag = "0" | "1" | 0 | 1 | undefined;

interface Env {
Expand Down Expand Up @@ -57,6 +56,7 @@ export default {
if (path.startsWith(API_SAVE_PATH) && request.method === "POST") {
// Store sheet
return await postSheet({
origin: url.origin,
requestHash: path.replace(API_SAVE_PATH, ''),
requestBody: await request.json(),
requestIp: request.headers.get("CF-Connecting-IP") || "",
Expand Down Expand Up @@ -160,8 +160,9 @@ function getNewId(): string {
return crypto.randomUUID().replaceAll('-', '').slice(0, 22);
}

async function postSheet({ requestHash, requestBody, requestIp, kv, d1, useD1 }:
async function postSheet({ origin, requestHash, requestBody, requestIp, kv, d1, useD1 }:
{
origin: string,
requestHash: string,
requestBody: SheetPostBody,
requestIp: string,
Expand Down Expand Up @@ -204,7 +205,7 @@ async function postSheet({ requestHash, requestBody, requestIp, kv, d1, useD1 }:
if (createNewDocument) {
// update document history to include latest version
dbEntry.history.unshift({
url: `${spaUrl}/#${id}`,
url: `${origin}/${id}`,
hash: id,
creation: dbEntry.creation
});
Expand All @@ -223,7 +224,7 @@ async function postSheet({ requestHash, requestBody, requestIp, kv, d1, useD1 }:
}

return new Response(JSON.stringify({
url: `${spaUrl}/#${id}`,
url: `${origin}/${id}`,
hash: id,
history: dbEntry.history
}));
Expand Down

0 comments on commit 6e8b4a9

Please sign in to comment.