@interncom/diplomatic 0.0.75 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -6
- package/dist/client.d.ts +44 -56
- package/dist/entdb/entdb.d.ts +42 -0
- package/dist/entdb/idb.d.ts +28 -0
- package/dist/entdb/memory.d.ts +30 -0
- package/dist/events.d.ts +18 -0
- package/dist/exim.d.ts +0 -0
- package/dist/index.d.ts +21 -13
- package/dist/index.mjs +6359 -7052
- package/dist/lib.d.ts +2 -0
- package/dist/react/useClient.d.ts +17 -5
- package/dist/react/useEntities.d.ts +0 -0
- package/dist/react/useStateWatcher.d.ts +3 -3
- package/dist/shared/api/peek.d.ts +3 -0
- package/dist/shared/api/pull.d.ts +3 -0
- package/dist/shared/api/push.d.ts +4 -0
- package/dist/shared/api/user.d.ts +2 -0
- package/dist/shared/auth.d.ts +8 -0
- package/dist/shared/bag.d.ts +15 -0
- package/dist/shared/binary.d.ts +8 -0
- package/dist/shared/client.d.ts +20 -9
- package/dist/shared/clock.d.ts +13 -0
- package/dist/shared/codec.d.ts +43 -0
- package/dist/shared/codecs/authTimestamp.d.ts +8 -0
- package/dist/shared/codecs/bag.d.ts +3 -0
- package/dist/shared/codecs/eid.d.ts +10 -0
- package/dist/shared/codecs/file.d.ts +8 -0
- package/dist/shared/codecs/fileHead.d.ts +10 -0
- package/dist/shared/codecs/fileIndexItem.d.ts +8 -0
- package/dist/shared/codecs/messageHead.d.ts +16 -0
- package/dist/shared/codecs/notifItem.d.ts +7 -0
- package/dist/shared/codecs/peekItem.d.ts +6 -0
- package/dist/shared/codecs/peekItemHead.d.ts +7 -0
- package/dist/shared/codecs/pullItem.d.ts +6 -0
- package/dist/shared/codecs/pushItem.d.ts +11 -0
- package/dist/shared/codecs/respHead.d.ts +10 -0
- package/dist/shared/codecs/usageQuota.d.ts +6 -0
- package/dist/shared/consts.d.ts +50 -0
- package/dist/shared/crypto/libsodium.d.ts +5 -5
- package/dist/shared/enclave.d.ts +8 -0
- package/dist/shared/endpoint.d.ts +23 -0
- package/dist/shared/exim.d.ts +12 -0
- package/dist/shared/http/listener.d.ts +11 -0
- package/dist/shared/http/server.d.ts +11 -0
- package/dist/shared/http.d.ts +82 -0
- package/dist/shared/lpc/listener.d.ts +16 -0
- package/dist/shared/lpc/pusher.d.ts +9 -0
- package/dist/shared/lpc/server.d.ts +19 -0
- package/dist/shared/message.d.ts +30 -0
- package/dist/shared/storage/memory.d.ts +10 -0
- package/dist/shared/sync.d.ts +9 -0
- package/dist/shared/types.d.ts +127 -49
- package/dist/shared/valstat.d.ts +6 -0
- package/dist/state.d.ts +12 -17
- package/dist/stores/idb/dnlds.d.ts +10 -0
- package/dist/stores/idb/hosts.d.ts +15 -0
- package/dist/stores/idb/msgs.d.ts +19 -0
- package/dist/stores/idb/seed.d.ts +11 -0
- package/dist/stores/idb/store.d.ts +24 -0
- package/dist/stores/idb/uplds.d.ts +11 -0
- package/dist/stores/memory/dnlds.d.ts +9 -0
- package/dist/stores/memory/hosts.d.ts +13 -0
- package/dist/stores/memory/msgs.d.ts +19 -0
- package/dist/stores/memory/seed.d.ts +9 -0
- package/dist/stores/memory/store.d.ts +16 -0
- package/dist/stores/memory/uplds.d.ts +10 -0
- package/dist/sync.d.ts +25 -2
- package/dist/types.d.ts +113 -29
- package/package.json +10 -7
- package/dist/api.d.ts +0 -3
- package/dist/entityDB.d.ts +0 -34
- package/dist/eventEmitter.d.ts +0 -8
- package/dist/idbStore.d.ts +0 -38
- package/dist/localStorageStore.d.ts +0 -36
- package/dist/memoryStore.d.ts +0 -40
- package/dist/queue.d.ts +0 -10
- package/dist/react/initSeedView.d.ts +0 -7
- package/dist/react/statusBar.d.ts +0 -7
- package/dist/shared/lib.d.ts +0 -2
- package/dist/shared/ops.d.ts +0 -4
- package/dist/shared/server.d.ts +0 -12
- package/dist/typedEventEmitter.d.ts +0 -8
- package/dist/useClient.d.ts +0 -4
package/README.md
CHANGED
|
@@ -5,14 +5,19 @@ Secure sync layer for web apps.
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
7
|
```tsx
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
DiplomaticClient,
|
|
10
|
+
localStorageStore,
|
|
11
|
+
StateManager,
|
|
12
|
+
useStateWatcher,
|
|
13
|
+
} from "@interncom/diplomatic";
|
|
9
14
|
|
|
10
15
|
const appState = { count: 0 };
|
|
11
16
|
const stateMgr = new StateManager(async (op) => {
|
|
12
17
|
if (op.type === "count" && typeof op.body === "number") {
|
|
13
18
|
appState.count = op.body;
|
|
14
19
|
}
|
|
15
|
-
})
|
|
20
|
+
});
|
|
16
21
|
|
|
17
22
|
const client = new DiplomaticClient({
|
|
18
23
|
seed: "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF",
|
|
@@ -22,8 +27,8 @@ const client = new DiplomaticClient({
|
|
|
22
27
|
});
|
|
23
28
|
|
|
24
29
|
export default function App() {
|
|
25
|
-
const count = useStateWatcher(stateMgr, "count", () => appState.count)
|
|
26
|
-
const inc = () => client.upsert("count", count + 1)
|
|
30
|
+
const count = useStateWatcher(stateMgr, "count", () => appState.count);
|
|
31
|
+
const inc = () => client.upsert("count", count + 1);
|
|
27
32
|
|
|
28
33
|
return (
|
|
29
34
|
<div style={{ width: "100vw", textAlign: "center" }}>
|
|
@@ -31,10 +36,11 @@ export default function App() {
|
|
|
31
36
|
<h2>{count}</h2>
|
|
32
37
|
<button onClick={inc}>+1</button>
|
|
33
38
|
</div>
|
|
34
|
-
)
|
|
39
|
+
);
|
|
35
40
|
}
|
|
36
41
|
```
|
|
37
42
|
|
|
38
43
|
## Development
|
|
39
44
|
|
|
40
|
-
To deploy npm package, from the `web` dir, run:
|
|
45
|
+
To deploy npm package, from the `web` dir, run:
|
|
46
|
+
`npm run build && npm run tsc && npm publish --public`.
|
package/dist/client.d.ts
CHANGED
|
@@ -1,57 +1,45 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
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
|
-
private
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
apply(op: IOp): Promise<void>;
|
|
46
|
-
export(filename: string, extension?: string): Promise<void>;
|
|
47
|
-
import: (file: File) => Promise<void>;
|
|
48
|
-
upsert<T>({ type, body, eid, gid, pid, version }: {
|
|
49
|
-
type: string;
|
|
50
|
-
body: T;
|
|
51
|
-
eid?: EntityID;
|
|
52
|
-
gid?: GroupID;
|
|
53
|
-
pid?: EntityID;
|
|
54
|
-
version?: number;
|
|
55
|
-
}): Promise<void>;
|
|
56
|
-
delete<T>(type: string, eid: Uint8Array, version?: number): Promise<void>;
|
|
1
|
+
import DiplomaticClientAPI from "./shared/client";
|
|
2
|
+
import { IClock } from "./shared/clock";
|
|
3
|
+
import { Status } from "./shared/consts";
|
|
4
|
+
import { EncodedMessage } from "./shared/message";
|
|
5
|
+
import { EntityID, HostHandle, ICrypto, IHostConnectionInfo, IInsertParams, IMessageHead, ITransport, IUpsertParams, MasterSeed } from "./shared/types";
|
|
6
|
+
import { ValStat } from "./shared/valstat";
|
|
7
|
+
import { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IStateEmitter, IStateManager, IStore } from "./types";
|
|
8
|
+
export declare class SyncClient<Handle extends HostHandle> implements IClient<Handle> {
|
|
9
|
+
private clock;
|
|
10
|
+
private state;
|
|
11
|
+
private store;
|
|
12
|
+
private transport;
|
|
13
|
+
private crypto;
|
|
14
|
+
private forceSkewHandlingByDefault;
|
|
15
|
+
connections: Map<string, DiplomaticClientAPI<Handle>>;
|
|
16
|
+
private currentSync;
|
|
17
|
+
clientState: IStateEmitter<IDiplomaticClientState>;
|
|
18
|
+
xferState: IStateEmitter<IDiplomaticClientXferState>;
|
|
19
|
+
constructor(clock: IClock, state: IStateManager, store: IStore<Handle>, transport: (host: IHostConnectionInfo<Handle>) => ITransport, crypto?: ICrypto, forceSkewHandlingByDefault?: boolean);
|
|
20
|
+
private readonly SYNC_DEBOUNCE_DELAY_MS;
|
|
21
|
+
private syncTimeout;
|
|
22
|
+
setSeed(seed: MasterSeed): Promise<void>;
|
|
23
|
+
private getClientState;
|
|
24
|
+
private getXferState;
|
|
25
|
+
private apply;
|
|
26
|
+
insertRaw(bod: EncodedMessage): Promise<ValStat<IMessageHead>>;
|
|
27
|
+
upsertRaw(eid: EntityID, bod: EncodedMessage | undefined, force?: boolean): Promise<ValStat<IMessageHead>>;
|
|
28
|
+
insert<T = unknown>(op: IInsertParams<T>): Promise<ValStat<IMessageHead>>;
|
|
29
|
+
upsert<T = unknown>(op: IUpsertParams<T>, force?: boolean): Promise<ValStat<IMessageHead>>;
|
|
30
|
+
delete(eid: EntityID): Promise<ValStat<IMessageHead>>;
|
|
31
|
+
genEID(id?: Uint8Array): Promise<ValStat<EntityID>>;
|
|
32
|
+
sync(): Promise<Status>;
|
|
33
|
+
private doSync;
|
|
34
|
+
wipe(): Promise<void>;
|
|
35
|
+
import: (file: File, options?: {
|
|
36
|
+
onProgress?: (index: number, total: number, status: Status) => void;
|
|
37
|
+
}) => Promise<Status>;
|
|
38
|
+
private scheduleSync;
|
|
39
|
+
export(filename: string): Promise<Status.Success | import("./shared/valstat").StatusNOK>;
|
|
40
|
+
link(host: IHostConnectionInfo<Handle>, connect?: boolean): Promise<void>;
|
|
41
|
+
unlink(label: string): Promise<void>;
|
|
42
|
+
private connectToHost;
|
|
43
|
+
connect(listen?: boolean, sync?: boolean): Promise<void>;
|
|
44
|
+
disconnect: () => Promise<void>;
|
|
57
45
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Status } from "../shared/consts";
|
|
2
|
+
import { ValStat } from "../shared/valstat";
|
|
3
|
+
import { EntityID, GroupID, IMsgEntBody, IOp } from "../shared/types";
|
|
4
|
+
import { StateManager } from "../state.ts";
|
|
5
|
+
export interface IEntity<T = unknown> extends Omit<IMsgEntBody<T>, "body"> {
|
|
6
|
+
eid: EntityID;
|
|
7
|
+
updatedAt: Date;
|
|
8
|
+
createdAt: Date;
|
|
9
|
+
ctr: number;
|
|
10
|
+
body: T;
|
|
11
|
+
}
|
|
12
|
+
export interface IDateRange {
|
|
13
|
+
start: Date;
|
|
14
|
+
end: Date;
|
|
15
|
+
}
|
|
16
|
+
export type EntitiesQuery = {
|
|
17
|
+
type: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: string;
|
|
20
|
+
gid: GroupID;
|
|
21
|
+
} | {
|
|
22
|
+
type: string;
|
|
23
|
+
pid: EntityID;
|
|
24
|
+
} | {
|
|
25
|
+
type: string;
|
|
26
|
+
updatedBetween: IDateRange;
|
|
27
|
+
};
|
|
28
|
+
export interface IEntDB {
|
|
29
|
+
apply: (ops: IOp[]) => Promise<{
|
|
30
|
+
stats: Status[];
|
|
31
|
+
types: Set<string>;
|
|
32
|
+
}>;
|
|
33
|
+
clear: () => Promise<Status>;
|
|
34
|
+
getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
|
|
35
|
+
getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
|
|
36
|
+
countEntities({ type }: {
|
|
37
|
+
type: string;
|
|
38
|
+
}): Promise<ValStat<number>>;
|
|
39
|
+
}
|
|
40
|
+
export declare function entStateManager(edb: IEntDB): StateManager;
|
|
41
|
+
export declare function applyOp(curr: IEntity | undefined, op: IOp): ValStat<IEntity | undefined>;
|
|
42
|
+
export declare const nullEntDB: IEntDB;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Status } from "../shared/consts";
|
|
2
|
+
import { EntityID, GroupID, IOp } from "../shared/types";
|
|
3
|
+
import { ValStat } from "../shared/valstat.ts";
|
|
4
|
+
import { EntitiesQuery, IEntDB, IEntity } from "./entdb";
|
|
5
|
+
export declare const entityTableName = "entities";
|
|
6
|
+
export declare const typeIndexName = "entity_type_created_at";
|
|
7
|
+
export declare const typeUpdatedAtIndexName = "entity_type_updated_at";
|
|
8
|
+
export declare const typeGroupIndexName = "entity_type_group_id";
|
|
9
|
+
export declare const typeParentIndexName = "entity_type_parent_id";
|
|
10
|
+
export declare class EntIDB implements IEntDB {
|
|
11
|
+
db: IDBDatabase | undefined;
|
|
12
|
+
init(): Promise<void>;
|
|
13
|
+
apply: (ops: IOp[]) => Promise<{
|
|
14
|
+
stats: Status[];
|
|
15
|
+
types: Set<string>;
|
|
16
|
+
}>;
|
|
17
|
+
clear(): Promise<Status>;
|
|
18
|
+
getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
|
|
19
|
+
getAllOfTypeUpdatedBetween<T>(opType: string, start: Date, end: Date): Promise<ValStat<IEntity<T>[]>>;
|
|
20
|
+
getGroupMembers<T>(opType: string, gid: GroupID): Promise<ValStat<IEntity<T>[]>>;
|
|
21
|
+
getAllOfType<T>(opType: string): Promise<ValStat<IEntity<T>[]>>;
|
|
22
|
+
private getAllEntities;
|
|
23
|
+
getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
|
|
24
|
+
countEntities({ type }: {
|
|
25
|
+
type: string;
|
|
26
|
+
}): Promise<ValStat<number>>;
|
|
27
|
+
}
|
|
28
|
+
export declare function openEntIDB(): Promise<EntIDB>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { IEntDB, IEntity } from "./entdb";
|
|
2
|
+
import { Status } from "../shared/consts";
|
|
3
|
+
import { EntityID, GroupID, IOp } from "../shared/types";
|
|
4
|
+
import { ValStat } from "../shared/valstat.ts";
|
|
5
|
+
interface IDateRange {
|
|
6
|
+
start: Date;
|
|
7
|
+
end: Date;
|
|
8
|
+
}
|
|
9
|
+
type EntitiesQuery = {
|
|
10
|
+
type: string;
|
|
11
|
+
gid?: GroupID;
|
|
12
|
+
pid?: EntityID;
|
|
13
|
+
updatedBetween?: IDateRange;
|
|
14
|
+
};
|
|
15
|
+
export declare class EntDBMemory implements IEntDB {
|
|
16
|
+
ents: Map<string, IEntity>;
|
|
17
|
+
constructor(initEnts?: IEntity[]);
|
|
18
|
+
apply(ops: IOp[]): Promise<{
|
|
19
|
+
stats: Status[];
|
|
20
|
+
types: Set<string>;
|
|
21
|
+
}>;
|
|
22
|
+
clear(): Promise<Status>;
|
|
23
|
+
getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
|
|
24
|
+
private getAllEntities;
|
|
25
|
+
getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
|
|
26
|
+
countEntities({ type }: {
|
|
27
|
+
type: string;
|
|
28
|
+
}): Promise<ValStat<number>>;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IStateEmitter } from "./types";
|
|
2
|
+
type TypedListener<T> = (data: T) => void;
|
|
3
|
+
export declare class TypedEventEmitter<T> {
|
|
4
|
+
private listeners;
|
|
5
|
+
addEventListener(eventType: string, listener: TypedListener<T>): () => void;
|
|
6
|
+
removeEventListener(eventType: string, listener: TypedListener<T>): void;
|
|
7
|
+
emit(eventType: string, data: T): void;
|
|
8
|
+
}
|
|
9
|
+
export declare class StateEmitter<T> implements IStateEmitter<T> {
|
|
10
|
+
private getter;
|
|
11
|
+
static eventName: string;
|
|
12
|
+
emitter: TypedEventEmitter<T>;
|
|
13
|
+
constructor(getter: () => Promise<T>);
|
|
14
|
+
get: () => Promise<T>;
|
|
15
|
+
emit(): Promise<void>;
|
|
16
|
+
listen(func: (state: T) => void): () => void;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/exim.d.ts
ADDED
|
File without changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { opMapApplier, StateManager } from "./state";
|
|
3
|
-
import { localStorageStore } from "./localStorageStore";
|
|
4
|
-
import { idbStore } from "./idbStore";
|
|
5
|
-
import type { IDiplomaticClientState } from "./types";
|
|
6
|
-
import DiplomaticClient from "./client";
|
|
1
|
+
import { SyncClient } from "./client";
|
|
7
2
|
import libsodiumCrypto from "./crypto";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
3
|
+
import { EntitiesQuery, entStateManager, IEntDB, IEntity, nullEntDB } from "./entdb/entdb";
|
|
4
|
+
import { EntIDB, openEntIDB } from "./entdb/idb";
|
|
5
|
+
import { EntDBMemory } from "./entdb/memory";
|
|
6
|
+
import { useClient, useClientState, useClientXferState, useSyncOnResume } from "./react/useClient";
|
|
10
7
|
import useStateWatcher, { useStateWatcherSuspense } from "./react/useStateWatcher";
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import { btoh, htob } from "./shared/binary";
|
|
9
|
+
import { Clock } from "./shared/clock";
|
|
10
|
+
import { genSingletonEID } from "./shared/codecs/eid";
|
|
11
|
+
import { Status } from "./shared/consts";
|
|
12
|
+
import { HTTPTransport } from "./shared/http";
|
|
13
|
+
import { EntityID, GroupID, type IOp, MasterSeed } from "./shared/types";
|
|
14
|
+
import { nullStateManager, StateManager } from "./state";
|
|
15
|
+
import { IDBStore, openIDBStore } from "./stores/idb/store";
|
|
16
|
+
import { MemoryStore } from "./stores/memory/store";
|
|
17
|
+
import type { IDiplomaticClientState, IStateManager, IStore } from "./types";
|
|
18
|
+
export declare function genWebClient(stateMgr: StateManager, url: URL): Promise<{
|
|
19
|
+
client: SyncClient<URL>;
|
|
20
|
+
setSeed: (seedHex: string) => Promise<void>;
|
|
21
|
+
}>;
|
|
22
|
+
export { btoh, Clock, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, htob, HTTPTransport, IDBStore, IEntDB, IEntity, IStateManager, IStore, libsodiumCrypto, MasterSeed, MemoryStore, nullEntDB, nullStateManager, openEntIDB, openIDBStore, StateManager, Status, SyncClient, useClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, };
|
|
23
|
+
export type { IDiplomaticClientState, IOp };
|