@x5e/gink
Version:
an eventually consistent database
168 lines (167 loc) • 7.98 kB
TypeScript
import { Peer } from "./Peer";
import { BundleListener, CallBack, BundleInfo, Muid, AsOf } from "./typedefs";
import { ChainTracker } from "./ChainTracker";
import { Bundler } from "./Bundler";
import { PairSet } from "./PairSet";
import { PairMap } from "./PairMap";
import { KeySet } from "./KeySet";
import { Directory } from "./Directory";
import { Box } from "./Box";
import { Sequence } from "./Sequence";
import { Group } from "./Group";
import { Store } from "./Store";
import { Behavior, ContainerBuilder } from "./builders";
import { Property } from "./Property";
import { Vertex } from "./Vertex";
import { EdgeType } from "./EdgeType";
/**
* This is an instance of the Gink database that can be run inside a web browser or via
* ts-node on a server. Because of the need to work within a browser it doesn't do any port
* listening (see SimpleServer for that capability).
*/
export declare class Database {
readonly store: Store;
readonly logger: CallBack;
ready: Promise<any>;
readonly peers: Map<number, Peer>;
static readonly PROTOCOL = "gink";
private listeners;
private countConnections;
private lastLinkToExtend;
private keyPair;
private identity;
private chainGetter?;
protected iHave: ChainTracker;
private static W3cWebSocket;
constructor(store?: Store, identity?: string, logger?: CallBack);
private initialize;
/**
* Starts a chain or finds one to reuse, then sets myChain.
*/
getChain(): Promise<BundleInfo>;
private getChainHelper;
/**
* Reset all containers in the database to a previous time.
* @param toTime optional timestamp to reset to. If not provided, each container
* will be cleared.
* @param bundlerOrComment optional bundler to add this change to, or a string to
* add a comment to a new bundle.
*/
reset(toTime?: AsOf, bundlerOrComment?: Bundler | string): Promise<void>;
getGlobalDirectory(): Directory;
getGlobalProperty(): Property;
getMedallionDirectory(): Directory;
/**
* Creates a new box container.
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the Box container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createBox(change?: Bundler | string): Promise<Box>;
/**
* Creates a new List container.
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the List container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createSequence(change?: Bundler | string): Promise<Sequence>;
/**
* Creates a new Key Set container.
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the Key Set container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createKeySet(change?: Bundler | string): Promise<KeySet>;
/**
* Creates a new Group container.
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the Group container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createGroup(change?: Bundler | string): Promise<Group>;
/**
* Creates a new PairSet container.
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the PairSet container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createPairSet(change?: Bundler | string): Promise<PairSet>;
/**
* Creates a new PairMap container.
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the PairMap container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createPairMap(change?: Bundler | string): Promise<PairMap>;
/**
* Creates a new Directory container (like a javascript map or a python dict).
* @param change either the bundler to add this box creation to, or a comment for an immediate change
* @returns promise that resolves to the Directory container (immediately if a bundler is passed in, otherwise after the bundle)
*/
createDirectory(change?: Bundler | string): Promise<Directory>;
createVertex(change?: Bundler | string): Promise<Vertex>;
createEdgeType(change?: Bundler | string): Promise<EdgeType>;
createProperty(bundlerOrComment?: Bundler | string): Promise<Property>;
protected createContainer(behavior: Behavior, change?: Bundler | string): Promise<[Muid, ContainerBuilder]>;
/**
* Returns an array of Muids of containers that have the provided name.
* @param name
* @param asOf optional timestamp to look back to.
* @returns an array of Muids.
*/
getContainersWithName(name: string, asOf?: AsOf): Promise<Muid[]>;
/**
* Adds a listener that will be called every time a bundle is received with the
* BundleInfo (which contains chain information, timestamp, and bundle comment).
* @param listener a callback to be invoked when a change occurs in the database or container
* @param containerMuid the Muid of a container to subscribe to. If left out, subscribe to all containers.
*/
addListener(listener: BundleListener, containerMuid?: Muid, remoteOnly?: boolean): void;
/**
* Gets a list of bundle listeners per container, listening to all bundles or just remote.
* @param remoteOnly true if looking for listeners only subscribed to remote bundles.
* @param containerMuid optional container muid to find listeners subscribed to a specific container.
*/
private getListeners;
/**
* Adds a bundle to a chain, setting the medallion and timestamps on the bundle in the process.
*
* @param bundler a PendingBundle ready to be sealed
* @returns A promise that will resolve to the bundle timestamp once it's persisted/sent.
*/
addBundler(bundler: Bundler): Promise<BundleInfo>;
/**
* Closes connections to peers and closes the store.
*/
close(): Promise<void>;
/**
* @returns a truthy number that can be used to identify connections
*/
protected createConnectionId(): number;
/**
* Tries to add a bundle to the local store. If successful (i.e. it hasn't seen it before)
* then it will also publish that bundle to the connected peers.
*
* This is called both from addPendingBundle (for locally produced bundles) and
* being called by receiveMessage.
*
* @param bundleBytes The bytes that correspond to this transaction.
* @param fromConnectionId The (truthy) connectionId if it came from a peer.
* @returns
*/
private receiveBundle;
/**
* @param messageBytes Bytes received from a peer.
* @param fromConnectionId Local name of the peer the data was received from.
* @returns
*/
protected receiveMessage(messageBytes: Uint8Array, fromConnectionId: number): Promise<void>;
/**
* Initiates a websocket connection to a peer.
* @param target a websocket uri, e.g. "ws://127.0.0.1:8080/"
* @param onClose optional callback to invoke when the connection is closed
* @param resolveOnOpen if true, resolve when the connection is established, otherwise wait for greeting
* @param retryOnDisconnect if true, try to reconnect (with backoff) if the server closes the connection
* @returns a promise to the peer
*/
connectTo(target: string, options?: {
onClose?: CallBack;
resolveOnOpen?: boolean;
retryOnDisconnect?: boolean;
authToken?: string;
}): Promise<Peer>;
}