UNPKG

@x5e/gink

Version:

an eventually consistent database

150 lines (149 loc) 7.23 kB
/** * Herein lay a bunch of utility functions, mostly for creating and * manipulating the types defined in typedefs.ts. */ import { Muid, Medallion, Value, MuidTuple, ScalarKey, EdgeData, Entry, ActorId, Timestamp, Bytes, KeyPair, StorageKey } from "./typedefs"; import { MuidBuilder, ValueBuilder, KeyBuilder } from "./builders"; import { TreeMap, MapIterator } from "jstreemap"; export declare const emptyBytes: Uint8Array<ArrayBuffer>; export declare function getShortHashKey(): Bytes; export declare const safeMask: bigint; export declare function shorterHash(data: Bytes): number; export declare const digest: (data: Bytes) => Uint8Array<ArrayBufferLike>; export declare const librariesReady: Promise<void>; export declare const signingBundles = true; export declare function noOp(..._args: any[]): void; export declare function toLastWithPrefixBeforeSuffix<V>(map: TreeMap<string, V>, prefix: string, suffix?: string): MapIterator<string, V> | undefined; export declare function dumpTree<V>(map: TreeMap<string, V>): void; export declare const inspectSymbol: unique symbol; export declare function ensure(x: any, msg?: string): any; export declare function generateTimestamp(): number; /** * Converts a storage key (which is the key used in EntryBuilders) to a * key usable by addEntry, etc. * @param storageKey * @returns */ export declare function fromStorageKey(storageKey: StorageKey): ScalarKey | Muid | [Muid, Muid]; /** * Randomly selects a number that can be used as a medallion. * Note that this doesn't actually have to be cryptographically secure; * as long as it's unique within an organization there won't be problems. * This is unlikely to cause collisions as long as an organization * has fewer than a million instances, after that some tracking is warranted. * https://en.wikipedia.org/wiki/Birthday_problem#Probability_table */ export declare function generateMedallion(): any; export declare function muidToBuilder(address: Muid, relativeTo?: Medallion): MuidBuilder; export declare function builderToMuid(muidBuilder: MuidBuilder, relativeTo?: Muid): Muid; /** * Converts from a KeyType (number or string) to a Gink Proto * @param key * @returns */ export declare function wrapKey(key: number | string | Uint8Array): KeyBuilder; /** * Convert from a Gink Proto known to contain a string or number * into the equiv Javascript object. * @param keyBuilder * @returns */ export declare function unwrapKey(keyBuilder: KeyBuilder): ScalarKey; /** * Convert from a Gink Proto (Builder) for a Value to the corresponding JS object. * @param valueBuilder Gink Proto for Value * @returns */ export declare function unwrapValue(valueBuilder: ValueBuilder): Value; /** * Converts a hex string (presumably encoded previously) to * an authentication token, prefixed with 'token ' * @param {string} hex hexadecimal string to convert * @returns a string 'token {token}' */ export declare function decodeToken(hex: string): string; /** * Encodes an authentication token as hexadecimal, prefixed by '0x'. * @param {string} token the token to encode * @returns an encoded hexadecimal string */ export declare function encodeToken(token: string): string; /** * Converts from any javascript value Gink can store into the corresponding proto builder. * @param arg Any Javascript value Gink can store * @returns */ export declare function wrapValue(arg: Value): ValueBuilder; export declare function isDate(value: any): boolean; export declare function matches(a: any[], b: any[]): boolean; export declare function pairKeyToArray(storageKey: String): Array<Muid>; /** * Converts a Muid object to its canonical string representation * Refer to docs/muid.md * @param muid * @returns a string of the canonical string representation */ export declare function muidToString(muid: Muid): string; export declare function muidTupleToString(muidTuple: MuidTuple): string; export declare function strToMuidTuple(value: string): MuidTuple; export declare function strToMuid(value: string): Muid; /** * Converts a number to its hexadecimal equivalent. * @param value * @param padding maximum size of hex string, padded by 0s. * @returns a hexadecimal string */ export declare function intToHex(value: number, padding?: number): string; export declare const oneByteToHex: (byte: number) => string; export declare const bytesToHex: (bytes: Uint8Array) => string; export declare const parseByte: (twoHexDigits: string) => number; export declare const hexToBytes: (hex: string) => Uint8Array<ArrayBuffer>; export declare function timestampToString(timestamp: Timestamp): string; export declare function valueToJson(value: Value): string; export declare function muidToTuple(muid: Muid): MuidTuple; export declare function muidTupleToMuid(tuple: MuidTuple): Muid; /** * Checks the resource path to ensure that it will resolve to a sensible file. * Specifically, it will require that each path component start with [a-zA-Z0-9_], * and only allow [a-zA-Z0-9_.@-] for following characters. This is to prevent * users from accessing hidden files with a dot prefix and traversing up with dot-dot * @param path resource requested * @returns True if the path doesn't look like something we should let users access. */ export declare function isPathDangerous(path: string): boolean; /** * Uses `console.error` to log messages to stderr in a form like: * [04:07:03.227Z CommandLineInterface.ts:51] got chain manager, using medallion=383316229311328 * That is to say, it's: * [<Timestamp> <SourceFileName>:<SourceLine>] <Message> * @param msg message to log */ export declare function logToStdErr(msg: string): void; export declare function sameData(key1: any, key2: any): boolean; export declare function entryToEdgeData(entry: Entry): EdgeData; export declare const dehydrate: typeof muidToTuple; export declare const rehydrate: typeof muidTupleToMuid; export declare function getActorId(): ActorId; /** * Used to (attempt to) identify the user who starts a gink chain. * @returns either the 'username@hostname' of the process running gink, * or a generic 'browser-client' if gink is running in a browser. */ export declare function getIdentity(): string; /** * This function exists to determine if the process or window that previously wrote to a chain is still around. * If not, then it's safe to append to that chain (to reduce the number of chain starts). If the creator of a * chain is still active, then you can't assume that the chain is free for reuse. * @param actorId * @returns */ export declare function isAlive(actorId: ActorId): Promise<boolean>; export declare function getType(extension: string): any; export declare function mergeBytes(arrayOne: Bytes, arrayTwo: Bytes): Bytes; export declare function signBundle(message: Bytes, secretKey: Bytes): Bytes; export declare function verifyBundle(signedBundle: Bytes, verifyKey: Bytes): void; export declare function createKeyPair(): KeyPair; export declare function getSig(bytes: Bytes): number; export declare function encryptMessage(message: string | Bytes, key: Bytes): Bytes; export declare function decryptMessage(message: Bytes, key: Bytes): Bytes; export declare function concatenate(a: Bytes, b: Bytes): Bytes;