📄 src/models/interfaces.ts
D-OPEN SOVEREIGN

Documentation & Insights

Key used in per-language value maps when a value is language-agnostic
(patches with langId 0).
One embedded instance (e.g. a PDF variant of a Book). Keys are the
instanceField names from the schema; `isDefault` is appended by the
sidecar writer and is not an instanceField.
V2 in-memory ORM tuple. All value maps are per-language (perLang defaults
to true in the V2 schema); language-agnostic values use LANG_AGNOSTIC ("").
JSON-compatible serialization of CommonOrmJson (worker <-> main thread).
Anything the patcher queue can hold: EVM-bound V2 patches (`AnyPatch`,
discriminated by `operation`) or local analytics ops (discriminated by `action`).
Flattened sidecar URLs for all selected languages.
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 * 
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 * 
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15import { ActionTypes, Rating, WorkerCommands } from "./enums.ts";
16import type { AnyPatch } from "./binary.ts";
17
18export type Lang = string;
19export type uid = number;
20
21export interface RecordIdentityCluster {
22    sfcuid: string;
23    native: string;
24    aliases: string[];
25    wireId: number;
26}
27
28/**
29 * Key used in per-language value maps when a value is language-agnostic
30 * (patches with langId 0).
31 */
32export const LANG_AGNOSTIC: Lang = "";
33
34/**
35 * One embedded instance (e.g. a PDF variant of a Book). Keys are the
36 * instanceField names from the schema; `isDefault` is appended by the
37 * sidecar writer and is not an instanceField.
38 */
39export type InstanceData = Record<string, string | number | boolean | null>;
40
41/**
42 * V2 in-memory ORM tuple. All value maps are per-language (perLang defaults
43 * to true in the V2 schema); language-agnostic values use LANG_AGNOSTIC ("").
44 */
45export type CommonOrmJson = [
46    uid,
47    Map<number, Set<number>>,             // _has   (relationId -> target uids)
48    Map<number, Set<number>>,             // _in    (relationId -> parent uids)
49    Map<number, Map<Lang, number>>,       // _numberValues (propId -> lang -> value)
50    Map<number, Map<Lang, boolean>>,      // _boolValues
51    Map<number, Map<Lang, string>>,       // _stringValues
52    Map<Lang, InstanceData[]>,            // _instances (lang -> instance array)
53];
54
55/** JSON-compatible serialization of CommonOrmJson (worker <-> main thread). */
56export type SerializedCommonOrmJson = [
57    uid,
58    Array<[number, Array<number>]>,
59    Array<[number, Array<number>]>,
60    Array<[number, Array<[Lang, number]>]>,
61    Array<[number, Array<[Lang, boolean]>]>,
62    Array<[number, Array<[Lang, string]>]>,
63    Array<[Lang, InstanceData[]]>,
64];
65
66export type PropertyType = 'string' | 'number' | 'boolean' | 'enum';
67
68// ─── Local-only analytics operations (never sent to the EVM) ─────────────────
69
70export interface BaseLocalOp {
71    ts: number,
72    action: ActionTypes,
73    objectType: number,
74    objectId: number,
75}
76export interface RateOp extends BaseLocalOp {
77    rate: Rating
78}
79export interface PartOp extends BaseLocalOp {
80    part: number,
81}
82export interface RatePartOp extends PartOp {
83    rate: Rating
84}
85export interface ShareOp extends BaseLocalOp {
86    socialMediaId: number
87}
88export interface SharePartOp extends PartOp {
89    socialMediaId: number
90}
91
92export type LocalOp = RateOp | RatePartOp | PartOp | ShareOp | SharePartOp | BaseLocalOp;
93
94/**
95 * Anything the patcher queue can hold: EVM-bound V2 patches (`AnyPatch`,
96 * discriminated by `operation`) or local analytics ops (discriminated by `action`).
97 */
98export type QueueItem = AnyPatch | LocalOp;
99
100export const isEvmPatch = (item: QueueItem): item is AnyPatch =>
101    (item as AnyPatch).operation !== undefined;
102
103// ─── Worker protocol ─────────────────────────────────────────────────────────
104
105export type SidecarUrls = {
106    objectType: number,
107    lang: Lang,
108    slimUrl?: string,
109    extendedUrl?: string,
110    provUrl?: string,
111}
112
113export type InitPayload = {
114    cmd: WorkerCommands.Init,
115    payload: {
116        /** Flattened sidecar URLs for all selected languages. */
117        sidecars: Array<SidecarUrls>,
118        selectedLangs: Lang[],
119    }
120}
121
122export type SavePatchesPayload = {
123    cmd: WorkerCommands.SavePatches,
124    payload: { username: string, patches: Array<QueueItem> }
125}
126
127export type GetRecordPayload = {
128    cmd: WorkerCommands.GetRecord,
129    payload: { objectType: number, id: uid }
130}
131
132export type LoadLanguagePayload = {
133    cmd: WorkerCommands.LoadLanguage,
134    payload: { lang: Lang, sidecars: Array<SidecarUrls> }
135}
136
137export type WorkerPayload = InitPayload | SavePatchesPayload | GetRecordPayload | LoadLanguagePayload;
138
139export interface IThread {
140    sendRequest(payload: WorkerPayload, transferables?: Array<any>): Promise<any>
141}
142
143// ─── Stats ───────────────────────────────────────────────────────────────────
144
145export type StatSummary = {
146    visits: Array<number>,
147    rating: Rating,
148    socialMediaId: Array<number>,
149    downloads: Array<number>,
150    parts: {
151        [partId: string]: {
152            rating: Rating,
153            socialMediaId: Array<number>,
154            visits: Array<number>
155        }
156    }
157}
158
159// ─── Misc storage records (IndexedDB) ────────────────────────────────────────
160
161export interface BookData {
162    fileId: string,
163    parsed: boolean,
164    author: string,
165    title: string,
166    dimensions: Array<{ width: number, height: number } | null>
167}
168
169export interface FileRecord {
170    id: string;
171    creationTime: number;
172    sizeKb: number;
173    content: Uint8Array;
174    local: boolean
175}
176