MCPcopy Create free account
hub / github.com/DeepNotesApp/DeepNotes / getAllPageUpdates

Function getAllPageUpdates

packages/@deeplib/data/src/page-updates.ts:8–66  ·  view source on GitHub ↗
(
  pageId: string,
  redis: Redis | Cluster,
)

Source from the content-addressed store, hash-verified

6import { pack, unpack } from 'msgpackr';
7
8export async function getAllPageUpdates(
9 pageId: string,
10 redis: Redis | Cluster,
11): Promise<[number, Uint8Array][]> {
12 const [updateCacheItems, updateBufferItems] = await Promise.all([
13 redis.lrangeBuffer(`page-update-cache:{${pageId}}`, 0, -1),
14 redis.lrangeBuffer(`page-update-buffer:{${pageId}}`, 0, -1),
15
16 redis.expire(`page-update-cache:{${pageId}}`, DEFAULT_REMOTE_TTL),
17 ]);
18
19 const cachePageUpdates = updateCacheItems
20 .map<[number, string]>((msgpackPageUpdate) => unpack(msgpackPageUpdate))
21 .map<[number, Uint8Array]>((redisPageUpdate) => [
22 redisPageUpdate[0],
23 base64ToBytes(redisPageUpdate[1]),
24 ]);
25
26 const bufferPageUpdates = updateBufferItems
27 .map<[number, string]>((msgpackPageUpdate) => unpack(msgpackPageUpdate))
28 .map<[number, Uint8Array]>((redisPageUpdate) => [
29 redisPageUpdate[0],
30 base64ToBytes(redisPageUpdate[1]),
31 ]);
32
33 if (updateCacheItems.length > 0) {
34 return cachePageUpdates
35 .concat(bufferPageUpdates)
36 .sort(([updateIndexA], [updateIndexB]) => updateIndexA - updateIndexB);
37 }
38
39 const dbPageUpdates = (
40 await PageUpdateModel.query()
41 .where('page_id', pageId)
42 .orderBy('index')
43 .select('index', 'encrypted_data')
44 ).map<[number, Uint8Array]>((pageUpdate) => [
45 parseInt(pageUpdate.index as any),
46 pageUpdate.encrypted_data,
47 ]);
48
49 if (dbPageUpdates.length > 0) {
50 await redis
51 .multi()
52 .del(`page-update-cache:{${pageId}}`)
53 .rpush(
54 `page-update-cache:{${pageId}}`,
55 ...dbPageUpdates.map((pageUpdate) =>
56 pack([pageUpdate[0], bytesToBase64(pageUpdate[1])]),
57 ),
58 )
59 .expire(`page-update-cache:{${pageId}}`, DEFAULT_REMOTE_TTL)
60 .exec();
61 }
62
63 return dbPageUpdates
64 .concat(bufferPageUpdates)
65 .sort(([updateIndexA], [updateIndexB]) => updateIndexA - updateIndexB);

Callers 2

moveStep1Function · 0.90

Calls 3

base64ToBytesFunction · 0.90
bytesToBase64Function · 0.90
delMethod · 0.80

Tested by

no test coverage detected