From 6e8b4a983df17196cf678be2cdef8897b63c4ea3 Mon Sep 17 00:00:00 2001 From: mgreminger Date: Fri, 27 Jan 2023 11:01:05 -0600 Subject: [PATCH] refactor: no longer hard code spaUrl for sheet URLS 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 --- src/database/_worker.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/database/_worker.ts b/src/database/_worker.ts index 598e3c00..270bbb02 100644 --- a/src/database/_worker.ts +++ b/src/database/_worker.ts @@ -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 { @@ -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") || "", @@ -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, @@ -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 }); @@ -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 }));