/ sync / src / topic-schemas.ts
topic-schemas.ts
 1  import { z } from 'zod';
 2  
 3  // Topic structure
 4  export const topicSchema = z.object({
 5      did: z.string(),
 6      path: z.string(),
 7      full: z.string() // {did}/{path}
 8  });
 9  
10  // Topic message
11  export const topicMessageSchema = z.object({
12      type: z.enum(['update', 'delete', 'snapshot']),
13      cid: z.string(),
14      timestamp: z.number(),
15      signature: z.instanceof(Uint8Array)
16  });
17  
18  // Topic subscription options
19  export const subscriptionOptionsSchema = z.object({
20      proof: z.string().optional(), // UCAN token for access
21      cache: z.boolean().optional().default(true),
22      verifySignature: z.boolean().optional().default(true)
23  });
24  
25  // Replication policy
26  export const replicationPolicySchema = z.object({
27      type: z.enum(['always', 'collaborators', 'popular', 'never']),
28      priority: z.enum(['high', 'medium', 'low']).default('medium'),
29      retention: z.union([
30          z.literal('permanent'),
31          z.number() // milliseconds
32      ]).default('permanent'),
33      announce: z.boolean().default(true)
34  });
35  
36  // Storage quota
37  export const storageQuotaSchema = z.object({
38      maxTotalSize: z.number().default(1_000_000_000), // 1 GB
39      maxDocuments: z.number().default(1000),
40      maxDocumentSize: z.number().default(10_000_000), // 10 MB
41      maxMediaSize: z.number().default(5_000_000_000), // 5 GB
42      maxMediaFileSize: z.number().default(100_000_000), // 100 MB
43      unpinnedRetention: z.number().default(30 * 24 * 60 * 60 * 1000), // 30 days
44      cacheRetention: z.number().default(7 * 24 * 60 * 60 * 1000) // 7 days
45  });
46  
47  // Replication stats
48  export const replicationStatsSchema = z.object({
49      replicated: z.number(),
50      replicatedSize: z.number(),
51      cached: z.number(),
52      cachedSize: z.number(),
53      announced: z.number()
54  });
55  
56  // Export inferred types
57  export type Topic = z.infer<typeof topicSchema>;
58  export type TopicMessage = z.infer<typeof topicMessageSchema>;
59  export type SubscriptionOptions = z.infer<typeof subscriptionOptionsSchema>;
60  export type ReplicationPolicy = z.infer<typeof replicationPolicySchema>;
61  export type StorageQuota = z.infer<typeof storageQuotaSchema>;
62  export type ReplicationStats = z.infer<typeof replicationStatsSchema>;