generated from edmundhung/remix-cloudflare-template
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathutils.ts
111 lines (95 loc) · 2.55 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export function configureStore<Env, T extends Record<string, any>>(
handlersCreator: (state: DurableObjectState, env: Env) => Promise<T>,
) {
class Store {
env: Env;
state: DurableObjectState;
handlers: T | null;
constructor(state: DurableObjectState, env: Env) {
this.state = state;
this.env = env;
this.handlers = null;
state.blockConcurrencyWhile(async () => {
this.handlers = await handlersCreator(state, env);
});
}
async fetch(request: Request): Promise<Response> {
const { method, args } = await request.json<{
method: string;
args: any[];
}>();
try {
if (!this.handlers) {
throw new Error(
'The handlers are not initialised; Please check if the store is setup properly',
);
}
const handler = this.handlers[method];
if (typeof handler === 'function') {
const result = JSON.stringify(await handler(...args)) ?? null;
return new Response(result, {
status: result !== null ? 200 : 204,
});
} else {
return new Response('Not Found', { status: 404 });
}
} catch (e) {
console.error(e);
return new Response('Internal Server Error', { status: 500 });
}
}
}
function createClient(namespace: DurableObjectNamespace, name: string): T {
const id = namespace.idFromName(name);
const stub = namespace.get(id);
const client = new Proxy(
{},
{
get(_, method) {
return async (...args: any[]) => {
const response = await stub.fetch('http://store', {
headers: {
'content-type': 'application/json',
},
method: 'POST',
body: JSON.stringify({ method, args }),
});
switch (response.status) {
case 200:
return await response.json();
case 204:
return;
case 404:
throw new Error(`Method ${method.toString()} is not available`);
default:
throw new Error(
`Unknown error caught; Received a ${response.status} response`,
);
}
};
},
},
);
return client as T;
}
return {
Store,
createClient,
};
}
export async function restoreStoreData(
storage: DurableObjectStorage,
data: Record<string, any>,
): Promise<void> {
const batches = [];
const keys = Object.keys(data);
for (let i = 0; i * 128 < keys.length; i++) {
const entires = keys.slice(i * 128, (i + 1) * 128).reduce((result, key) => {
result[key] = data[key];
return result;
}, {} as Record<string, any>);
batches.push(entires);
}
await storage.deleteAll();
await Promise.all(batches.map((entries) => storage.put(entries)));
}