@interncom/diplomatic 0.1.1 → 0.1.2

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/dist/client.d.ts CHANGED
@@ -2,9 +2,9 @@ import DiplomaticClientAPI from "./shared/client";
2
2
  import { IClock } from "./shared/clock";
3
3
  import { Status } from "./shared/consts";
4
4
  import { EncodedMessage } from "./shared/message";
5
- import { EntityID, HostHandle, ICrypto, IHostConnectionInfo, IInsertParams, IMessageHead, ITransport, IUpsertParams, MasterSeed } from "./shared/types";
5
+ import { EntityID, HostHandle, ICrypto, IHostConnectionInfo, IInsertParams, IMessageHead, IStateManager, ITransport, IUpsertParams, MasterSeed } from "./shared/types";
6
6
  import { ValStat } from "./shared/valstat";
7
- import { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IStateEmitter, IStateManager, IStore } from "./types";
7
+ import { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IStateEmitter, IStore } from "./types";
8
8
  export declare class SyncClient<Handle extends HostHandle> implements IClient<Handle> {
9
9
  private clock;
10
10
  private state;
package/dist/events.d.ts CHANGED
@@ -1,11 +1,5 @@
1
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
- }
2
+ import { TypedEventEmitter } from "./shared/events";
9
3
  export declare class StateEmitter<T> implements IStateEmitter<T> {
10
4
  private getter;
11
5
  static eventName: string;
@@ -15,4 +9,3 @@ export declare class StateEmitter<T> implements IStateEmitter<T> {
15
9
  emit(): Promise<void>;
16
10
  listen(func: (state: T) => void): () => void;
17
11
  }
18
- export {};
package/dist/index.d.ts CHANGED
@@ -7,17 +7,20 @@ import { useClient, useClientState, useClientXferState, useSyncOnResume } from "
7
7
  import useStateWatcher, { useStateWatcherSuspense } from "./react/useStateWatcher";
8
8
  import { btoh, htob } from "./shared/binary";
9
9
  import { Clock } from "./shared/clock";
10
- import { genSingletonEID } from "./shared/codecs/eid";
10
+ import { Decoder, Encoder } from "./shared/codec";
11
+ import { eidCodec, genSingletonEID } from "./shared/codecs/eid";
11
12
  import { Status } from "./shared/consts";
12
13
  import { HTTPTransport } from "./shared/http";
13
- import { EntityID, GroupID, type IOp, MasterSeed } from "./shared/types";
14
+ import { TypedEventEmitter } from "./shared/events";
15
+ import { EntityID, GroupID, IMessage, IMutateOp, type IOp, IStateManager, MasterSeed } from "./shared/types";
14
16
  import { nullStateManager, StateManager } from "./state";
15
17
  import { IDBStore, openIDBStore } from "./stores/idb/store";
16
18
  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
+ import { SingletonStateManager } from "./shared/singleton";
20
+ import type { Applier, IDiplomaticClientState, IStore, IStoredMessage, IStoredMessageData } from "./types";
21
+ export declare function genWebClient(stateMgr: IStateManager, url: URL): Promise<{
19
22
  client: SyncClient<URL>;
20
23
  setSeed: (seedHex: string) => Promise<void>;
21
24
  }>;
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 };
25
+ export { btoh, Clock, Decoder, eidCodec, Encoder, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, htob, HTTPTransport, IDBStore, IEntDB, IEntity, IStore, libsodiumCrypto, MasterSeed, MemoryStore, nullEntDB, nullStateManager, openEntIDB, openIDBStore, SingletonStateManager, StateManager, Status, SyncClient, TypedEventEmitter, useClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, };
26
+ export type { Applier, IDiplomaticClientState, IMessage, IMutateOp, IOp, IStoredMessage, IStoredMessageData, };
package/dist/index.mjs CHANGED
@@ -7851,7 +7851,7 @@ var LibsodiumCrypto = class {
7851
7851
  var libsodiumCrypto = new LibsodiumCrypto(libsodium_esm_wrappers_exports);
7852
7852
  var crypto_default = libsodiumCrypto;
7853
7853
 
7854
- // src/events.ts
7854
+ // ../shared/events.ts
7855
7855
  var TypedEventEmitter = class {
7856
7856
  listeners = /* @__PURE__ */ new Map();
7857
7857
  // Add an event listener for a specific event type
@@ -7887,6 +7887,8 @@ var TypedEventEmitter = class {
7887
7887
  }
7888
7888
  }
7889
7889
  };
7890
+
7891
+ // src/events.ts
7890
7892
  var StateEmitter = class _StateEmitter {
7891
7893
  constructor(getter) {
7892
7894
  this.getter = getter;
@@ -11208,6 +11210,40 @@ var MemoryStore = class {
11208
11210
  }
11209
11211
  };
11210
11212
 
11213
+ // ../shared/singleton.ts
11214
+ var SingletonStateManager = class {
11215
+ constructor(singletonType) {
11216
+ this.singletonType = singletonType;
11217
+ }
11218
+ latest;
11219
+ latestOff = -1;
11220
+ emitter = new TypedEventEmitter();
11221
+ async apply(messages) {
11222
+ for (const msg of messages) {
11223
+ const dec = new Decoder2(msg.eid);
11224
+ const [eidDec, stat] = dec.readStruct(eidCodec);
11225
+ if (stat !== 0 /* Success */) continue;
11226
+ const encType = new Encoder2();
11227
+ encType.writeVarString(this.singletonType);
11228
+ const typeBytes = encType.result();
11229
+ if (eidDec.id.length === typeBytes.length && eidDec.id.every((b, i) => b === typeBytes[i])) {
11230
+ if (this.latest === void 0 || msg.off > this.latestOff) {
11231
+ this.latestOff = msg.off;
11232
+ this.latest = msg.bod;
11233
+ this.emitter.emit(this.singletonType, null);
11234
+ }
11235
+ }
11236
+ }
11237
+ return messages.map(() => 0 /* Success */);
11238
+ }
11239
+ on = (event, listener) => {
11240
+ this.emitter.addEventListener(event, listener);
11241
+ };
11242
+ off = (event, listener) => {
11243
+ this.emitter.removeEventListener(event, listener);
11244
+ };
11245
+ };
11246
+
11211
11247
  // src/index.ts
11212
11248
  async function genWebClient(stateMgr, url) {
11213
11249
  const idbStore = await openIDBStore(crypto_default);
@@ -11227,15 +11263,20 @@ async function genWebClient(stateMgr, url) {
11227
11263
  }
11228
11264
  export {
11229
11265
  Clock,
11266
+ Decoder2 as Decoder,
11267
+ Encoder2 as Encoder,
11230
11268
  EntDBMemory,
11231
11269
  EntIDB,
11232
11270
  HTTPTransport,
11233
11271
  IDBStore,
11234
11272
  MemoryStore,
11273
+ SingletonStateManager,
11235
11274
  StateManager,
11236
11275
  Status,
11237
11276
  SyncClient,
11277
+ TypedEventEmitter,
11238
11278
  btoh,
11279
+ eidCodec,
11239
11280
  entStateManager,
11240
11281
  genSingletonEID,
11241
11282
  genWebClient,
@@ -1,6 +1,6 @@
1
1
  import { SyncClient } from "../client";
2
- import { HostHandle, IHostConnectionInfo, MasterSeed } from "../shared/types";
3
- import type { IDiplomaticClientState, IDiplomaticClientXferState, IStateManager } from "../types";
2
+ import { HostHandle, IHostConnectionInfo, IStateManager, MasterSeed } from "../shared/types";
3
+ import type { IDiplomaticClientState, IDiplomaticClientXferState } from "../types";
4
4
  import { IEntDB } from "../entdb/entdb";
5
5
  import { IClock } from "../shared/clock";
6
6
  export declare function useClientState<Handle extends HostHandle>(client: SyncClient<Handle>): IDiplomaticClientState | undefined;
@@ -1,3 +1,3 @@
1
- import { IStateManager } from "../types";
1
+ import { IStateManager } from "../shared/types";
2
2
  export default function useStateWatcher<T>(mgr: IStateManager, opType: string, callback: () => Promise<T>): T | undefined;
3
3
  export declare function useStateWatcherSuspense<T>(mgr: IStateManager, opType: string, callback: () => Promise<T>): T;
@@ -0,0 +1,8 @@
1
+ type TypedListener<T> = (data: T) => void;
2
+ export declare class TypedEventEmitter<T> {
3
+ private listeners;
4
+ addEventListener(eventType: string, listener: TypedListener<T>): () => void;
5
+ removeEventListener(eventType: string, listener: TypedListener<T>): void;
6
+ emit(eventType: string, data: T): void;
7
+ }
8
+ export {};
@@ -1,11 +1,14 @@
1
1
  import { IClock } from "../clock.ts";
2
- import { type IHostCrypto, type IProtoHost, type IStorage, type IWebSocketPushNotifier } from "../types.ts";
2
+ import { IAuthTimestamp } from "../codecs/authTimestamp.ts";
3
+ import { type IHostCrypto, type IProtoHost, type IPushNotifier, type IStorage } from "../types.ts";
4
+ import { ValStat } from "../valstat.ts";
3
5
  export declare class DiplomaticHTTPServer implements IProtoHost {
4
6
  storage: IStorage;
5
7
  crypto: IHostCrypto;
6
- notifier: IWebSocketPushNotifier;
8
+ notifier: IPushNotifier;
7
9
  clock: IClock;
8
- constructor(storage: IStorage, crypto: IHostCrypto, notifier: IWebSocketPushNotifier, clock: IClock);
10
+ constructor(storage: IStorage, crypto: IHostCrypto, notifier: IPushNotifier, clock: IClock);
9
11
  corsHandler: (request: Request) => Promise<Response>;
10
12
  handler: (request: Request) => Promise<Response>;
11
13
  }
14
+ export declare function validateWebSocketAuth(request: Request, host: IProtoHost): Promise<ValStat<IAuthTimestamp>>;
@@ -1,12 +1,5 @@
1
- import { IClock } from "./clock.ts";
2
- import type { ICrypto, IMessage, IMessageHead, SerializedContent } from "./types.ts";
1
+ import type { ICrypto, IDeleteMessage, IMessage, IMessageHead, IUpsertMessage, SerializedContent } from "./types.ts";
3
2
  import { ValStat } from "./valstat.ts";
4
- interface IUpsertMessage extends IMessage {
5
- bod: SerializedContent;
6
- }
7
- interface IDeleteMessage extends Omit<IMessage, "bod"> {
8
- len: 0;
9
- }
10
3
  export type EncodedMessage = Uint8Array;
11
4
  interface IInsertParams {
12
5
  now: Date;
@@ -26,5 +19,4 @@ interface IDeleteParams extends Omit<IMessage, "off" | "len" | "hsh" | "bod"> {
26
19
  export declare function genDeleteHead({ now, eid, ctr, crypto }: IDeleteParams): Promise<ValStat<IMessageHead>>;
27
20
  export declare function genInsert({ now, bod, crypto }: IInsertParams): Promise<ValStat<IUpsertMessage>>;
28
21
  export declare function genDelete(params: IDeleteParams): Promise<ValStat<IDeleteMessage>>;
29
- export declare function genSingletonUpsert(type: string, clk: IClock, content: Uint8Array, ctr?: number): Promise<ValStat<IUpsertMessage>>;
30
22
  export {};
@@ -0,0 +1,15 @@
1
+ import { IClock } from "./clock.ts";
2
+ import { Status } from "./consts.ts";
3
+ import { IMessage, IStateManager, IUpsertMessage, SerializedContent } from "./types.ts";
4
+ import { ValStat } from "./valstat.ts";
5
+ export declare function genSingletonUpsert(type: string, clk: IClock, content: Uint8Array, ctr?: number): Promise<ValStat<IUpsertMessage>>;
6
+ export declare class SingletonStateManager implements IStateManager {
7
+ private singletonType;
8
+ latest: SerializedContent | undefined;
9
+ private latestOff;
10
+ private emitter;
11
+ constructor(singletonType: string);
12
+ apply(messages: IMessage[]): Promise<Status[]>;
13
+ on: (event: string, listener: () => void) => void;
14
+ off: (event: string, listener: () => void) => void;
15
+ }
@@ -115,9 +115,6 @@ export interface IPushNotifier {
115
115
  open(authTS: IAuthTimestamp, recv: PushReceiver, crypto: IHostCrypto, clock: IClock): Promise<IPushOpenResponse>;
116
116
  push(pubKey: PublicKey, data: Uint8Array): void;
117
117
  }
118
- export interface IWebSocketPushNotifier extends IPushNotifier {
119
- handle(host: IProtoHost, req: Request): Promise<Response>;
120
- }
121
118
  export interface IPushListener {
122
119
  connect(authTS: IAuthTimestamp, recv: PushReceiver, onDisconnect: () => void): Promise<Status>;
123
120
  connected(): boolean;
@@ -153,4 +150,15 @@ export interface ITransport {
153
150
  call: (name: APICallName, enc: Encoder) => Promise<ValStat<Decoder>>;
154
151
  listener: IPushListener;
155
152
  }
153
+ export interface IStateManager {
154
+ apply: (msgs: IMessage[]) => Promise<Status[]>;
155
+ on: (type: string, listener: () => void) => void;
156
+ off: (type: string, listener: () => void) => void;
157
+ }
158
+ export interface IUpsertMessage extends IMessage {
159
+ bod: SerializedContent;
160
+ }
161
+ export interface IDeleteMessage extends Omit<IMessage, "bod"> {
162
+ len: 0;
163
+ }
156
164
  export {};
package/dist/state.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Status } from "./shared/consts";
2
- import { IMessage, IMsgEntBody, IOp } from "./shared/types";
2
+ import { IMessage, IMsgEntBody, IOp, IStateManager } from "./shared/types";
3
3
  import { ValStat } from "./shared/valstat";
4
- import type { Applier, IStateManager } from "./types";
4
+ import type { Applier } from "./types";
5
5
  export declare function isMsgEntBody(bodDec: unknown): bodDec is IMsgEntBody;
6
6
  export declare function msgToOp(msg: IMessage): ValStat<IOp>;
7
7
  export declare class StateManager implements IStateManager {
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Status } from "./shared/consts";
2
2
  import type { Enclave } from "./shared/enclave";
3
3
  import type { EncodedMessage } from "./shared/message";
4
- import type { EntityID, Hash, HostHandle, IHostConnectionInfo, IHostMetadata, IInsertParams, IMessage, IMessageHead, IOp, IUpsertParams, MasterSeed, SerializedContent } from "./shared/types";
4
+ import type { EntityID, Hash, HostHandle, IHostConnectionInfo, IHostMetadata, IInsertParams, IMessageHead, IOp, IUpsertParams, MasterSeed, SerializedContent } from "./shared/types";
5
5
  import { ValStat } from "./shared/valstat";
6
6
  import { ICrypto } from "./shared/types";
7
7
  export interface IMsgParts {
@@ -115,9 +115,4 @@ export interface IClient<Handle extends HostHandle> {
115
115
  clientState: IStateEmitter<IDiplomaticClientState>;
116
116
  xferState: IStateEmitter<IDiplomaticClientXferState>;
117
117
  }
118
- export interface IStateManager {
119
- apply: (msgs: IMessage[]) => Promise<Status[]>;
120
- on: (type: string, listener: () => void) => void;
121
- off: (type: string, listener: () => void) => void;
122
- }
123
118
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interncom/diplomatic",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",