Skip to content

Commit 6b0cd20

Browse files
committed
query param replica to use remote storage
localhost:5173/?replica=foo will default to remote + replica foo without a query param you default to memory or vite config
1 parent f37ebcf commit 6b0cd20

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

typescript/packages/common-charm/src/charm.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import { Module, NAME, Recipe, TYPE, UI } from "@commontools/builder";
2-
import { getDoc, type DocLink, DocImpl, EntityId, idle, createRef, getRecipe, isDoc, isDocLink, run } from "@commontools/runner";
2+
import {
3+
getDoc,
4+
type DocLink,
5+
DocImpl,
6+
EntityId,
7+
idle,
8+
createRef,
9+
getRecipe,
10+
isDoc,
11+
isDocLink,
12+
run,
13+
} from "@commontools/runner";
314
import { createStorage } from "./storage.js";
415

516
export type Charm = {
@@ -9,7 +20,15 @@ export type Charm = {
920
[key: string]: any;
1021
};
1122

12-
export const storage = createStorage((import.meta as any).env.VITE_STORAGE_TYPE ?? "memory");
23+
// FIXME(ja): we shouldn't assume we are in a browser environment here.
24+
const defaultReplica = "common-knowledge";
25+
const urlParams = new URLSearchParams(window.location.search);
26+
const replica = urlParams.get("replica") ?? defaultReplica;
27+
const storageType = urlParams.get("replica")
28+
? "remote"
29+
: ((import.meta as any).env.VITE_STORAGE_TYPE ?? "memory");
30+
31+
export const storage = createStorage(storageType, replica);
1332
export const charms = getDoc<DocLink[]>([], "charms");
1433
(window as any).charms = charms;
1534

typescript/packages/common-charm/src/storage-providers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ export class RemoteStorageProvider implements StorageProvider {
370370
if (revision) {
371371
return revision;
372372
}
373+
return;
373374
}
374375

375376
async perform(command: Command) {

typescript/packages/common-charm/src/storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class StorageImpl implements Storage {
576576
}
577577
}
578578

579-
export function createStorage(type: "local" | "memory" | "remote"): Storage {
579+
export function createStorage(type: "local" | "memory" | "remote", replica: string = "common-knowledge"): Storage {
580580
let storageProvider: StorageProvider;
581581

582582
if (type === "local") {
@@ -586,6 +586,7 @@ export function createStorage(type: "local" | "memory" | "remote"): Storage {
586586
} else if (type === "remote") {
587587
storageProvider = new RemoteStorageProvider({
588588
address: new URL("/api/storage/memory", new URL(location.href)),
589+
replica,
589590
});
590591
} else {
591592
throw new Error("Invalid storage type");

0 commit comments

Comments
 (0)