Skip to content

Commit 32b14f7

Browse files
committed
Refine room ID generation for Cloudflare sync in Tldraw components
- Updated `getSyncRoomId` to encode the room ID as a base64url token, enhancing security and compatibility. - Clarified comments in `Tldraw.tsx` regarding canvas identity handling in Roam.
1 parent b9e5f91 commit 32b14f7

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

apps/roam/src/components/canvas/Tldraw.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const isPageUid = (uid: string) =>
150150

151151
const TldrawCanvas = ({ title }: { title: string }) => {
152152
// In Roam, canvas identity is currently keyed by the page UID.
153-
// Room sync is graphName/uid
153+
// Room sync is graph/page encoded as an opaque base64url token.
154154
const pageUid = useMemo(() => getPageUidByPageTitle(title), [title]);
155155
const useCloudflareSync =
156156
TLDRAW_CLOUDFLARE_SYNC_ENABLED &&

apps/roam/src/components/canvas/TldrawCanvasCloudflareSync.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ export type CloudflareCanvasStoreAdapterResult = {
2424

2525
const getSyncRoomId = ({ pageUid }: { pageUid: string }): string => {
2626
const graphName = window.roamAlphaAPI.graph.name;
27-
return `${graphName}/${pageUid}`;
27+
const payload = JSON.stringify({ graphName, pageUid });
28+
const bytes = new TextEncoder().encode(payload);
29+
let binary = "";
30+
for (const byte of bytes) {
31+
binary += String.fromCharCode(byte);
32+
}
33+
34+
return btoa(binary)
35+
.replace(/\+/g, "-")
36+
.replace(/\//g, "_")
37+
.replace(/=+$/g, "");
2838
};
2939

3040
const parseRoamUploadResponse = (value: string): string => {
@@ -68,7 +78,7 @@ export const useCloudflareSyncStore = ({
6878
);
6979

7080
const uri = useMemo(() => {
71-
const roomId = encodeURIComponent(getSyncRoomId({ pageUid }));
81+
const roomId = getSyncRoomId({ pageUid });
7282
const query = new URLSearchParams();
7383
for (const shapeType of customShapeTypes) {
7484
query.append("shapeType", shapeType);

0 commit comments

Comments
 (0)