partly repair gun types (#1154)

* partly repair gun types

* added types tests
This commit is contained in:
MaciejDot 2021-10-30 08:15:02 +02:00 committed by GitHub
parent 17af355ac1
commit 395c2b8f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 1060 additions and 638 deletions

1
.gitignore vendored
View File

@ -16,5 +16,6 @@ isolate*.log
.localStorage .localStorage
/types/**/*.ts /types/**/*.ts
!/types/**/*.d.ts !/types/**/*.d.ts
!/types/**/*.test-d.ts
/gun.ts /gun.ts
/temp/ /temp/

4
global.d.ts vendored
View File

@ -1,4 +1,4 @@
import { IGunStatic } from './types/static'; import { IGun } from './types/gun/IGun';
declare global { declare global {
var Gun: IGunStatic; var Gun: IGun;
} }

4
gun.d.ts vendored
View File

@ -1,3 +1,3 @@
import { IGunStatic } from './types/static'; import { IGun } from './types/gun/IGun';
declare const Gun: IGunStatic; declare const Gun: IGun;
export = Gun; export = Gun;

4
index.d.ts vendored
View File

@ -1,3 +1,3 @@
import { IGunStatic } from './types/static'; import { IGun } from './types/gun/IGun';
declare const Gun: IGunStatic; declare const Gun: IGun;
export = Gun; export = Gun;

308
types/chain.d.ts vendored
View File

@ -1,308 +0,0 @@
import { AlwaysDisallowedType, DisallowPrimitives, AckCallback, Saveable, IGunCryptoKeyPair } from './types';
import { IGunConstructorOptions } from './options';
declare type ITSResolvable<R> = R | PromiseLike<R>;
export interface IGunChainReference<DataType = Record<string, any>, ReferenceKey = any, IsTop extends 'pre_root' | 'root' | false = false> {
/**
* Save data into gun, syncing it with your connected peers.
*
* * You cannot save primitive values at the root level.
*
* @param data You do not need to re-save the entire object every time,
* gun will automatically merge your data into what already exists as a "partial" update.
*
* * `undefined`, `NaN`, `Infinity`, `array`, will be rejected.
* * Traditional arrays are dangerous in real-time apps. Use `gun.set` instead.
*
* @param callback invoked on each acknowledgment
* @param options additional options (used for specifying certs)
*/
put(data: Partial<AlwaysDisallowedType<DisallowPrimitives<IsTop, DataType>>>, callback?: AckCallback | null, options?: { opt?: { cert?: string } }): IGunChainReference<DataType, ReferenceKey, IsTop>;
/**
* **.set does not mean 'set data', it means a Mathematical Set**
*
* Add a unique item to an unordered list.
* `gun.set` works like a mathematical set, where each item in the list is unique.
* If the item is added twice, it will be merged.
*
* **This means only objects, for now, are supported.**
* @param data the object to add to the set
* @param callback optional function to invoke when the operation is complete
* @param options additional options (used for specifying certs)
*/
set<K extends keyof DataType>(data: Partial<AlwaysDisallowedType<DisallowPrimitives<IsTop, DataType[K]>>>, callback?: AckCallback | null, options?: { opt?: { cert?: string } }): IGunChainReference<DataType, ReferenceKey, IsTop>;
/**
* Where to read data from.
* @param key The key is the ID or property name of the data that you saved from earlier
* (or that will be saved later).
* * Note that if you use .put at any depth after a get it first reads the data and then writes, merging the data as a partial update.
* @param callback You will usually be using gun.on or gun.once to actually retrieve your data,
* not this callback (it is intended for more low-level control, for module and extensions).
*
* **Avoid use callback. The type in the document may be wrong.**
*
* **Here the type of callback respect to the actual behavior**
*/
get<K extends keyof DataType>(key: Exclude<K, Array<any>>, callback?: (
/** the raw data. Internal node of gun. Will not typed here. */
paramA: Record<'gun' | '$' | 'root' | 'id' | 'back' | 'on' | 'tag' | 'get' | 'soul' | 'ack' | 'put', any>,
/** the key, ID, or property name of the data. */
paramB: Record<'off' | 'to' | 'next' | 'the' | 'on' | 'as' | 'back' | 'rid' | 'id', any>) => void): IGunChainReference<DataType[K], K, IsTop extends 'pre_root' ? 'root' : false>;
/**
* Change the configuration of the gun database instance.
* @param options The options argument is the same object you pass to the constructor.
*
* The options's properties replace those in the instance's configuration but options.peers are **added** to peers known to the gun instance.
* @returns No mention in the document, behavior as `ChainReference<DataType, ReferenceKey>`
*/
opt(options: IGunConstructorOptions): IGunChainReference<DataType, ReferenceKey>;
/**
* Move up to the parent context on the chain.
*
* Every time a new chain is created, a reference to the old context is kept to go back to.
* @param amount The number of times you want to go back up the chain.
* `-1` or `Infinity` will take you to the root.
* @returns Impossible to determinate final type. You must cast it by yourself.
*/
back(amount?: number): IGunChainReference;
/**
* Subscribe to updates and changes on a node or property in real-time.
* @param option Currently, the only option is to filter out old data, and just be given the changes.
* If you're listening to a node with 100 fields, and just one changes,
* you'll instead be passed a node with a single property representing that change rather than the full node every time.
* @param callback
* Once initially and whenever the property or node you're focused on changes, this callback is immediately fired with the data as it is at that point in time.
*
* Since gun streams data, the callback will probably be called multiple times as new chunks come in.
* To remove a listener call .off() on the same property or node.
*/
on(callback: (data: DisallowPrimitives<IsTop, AlwaysDisallowedType<DataType>>, key: ReferenceKey) => void, option?: {
change: boolean;
} | boolean): IGunChainReference<DataType, ReferenceKey>;
/**
* Get the current data without subscribing to updates. Or `undefined` if it cannot be found.
* @returns In the document, it said the return value may change in the future. Don't rely on it.
*/
once(callback?: (data: (DisallowPrimitives<IsTop, AlwaysDisallowedType<Record<string, DataType>>>) | undefined, key: ReferenceKey) => void, option?: {
wait: number;
}): IGunChainReference<DataType, ReferenceKey>;
/**
* Map iterates over each property and item on a node, passing it down the chain,
* behaving like a forEach on your data.
* It also subscribes to every item as well and listens for newly inserted items.
*/
map(callback?: (value: DataType, key: string) => DataType | undefined): IGunChainReference<DataType, ReferenceKey>;
/**
* Undocumented, but extremely useful and mentioned in the document
*
* Remove **all** listener on this node.
*/
off(): void;
/**
*
* Path does the same thing as `.get` but has some conveniences built in.
* @deprecated This is not friendly with type system.
*
* **Warning**: This extension was removed from core, you probably shouldn't be using it!
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/path.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/path.js"></script>`!
*/
path?(path: string | string[]): IGunChainReference;
/**
* Handle cases where data can't be found.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/not.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/not.js"></script>`!
*/
not?(callback: (key: ReferenceKey) => void): IGunChainReference<DataType, ReferenceKey>;
/**
* Open behaves very similarly to gun.on, except it gives you the **full depth of a document** on every update.
* It also works with graphs, tables, or other data structures. Think of it as opening up a live connection to a document.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/open.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/open.js"></script>`!
*/
open?(callback: (data: DataType) => void): IGunChainReference<DataType, ReferenceKey>;
/**
* Loads the full object once. It is the same as `open` but with the behavior of `once`.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/load.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/load.js"></script>`!
*/
load?(callback: (data: DataType) => void): IGunChainReference<DataType, ReferenceKey>;
/**
* Returns a promise for you to use.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/then.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/then.js"></script>`!
*/
// then?<R, TResult1 = DataType>(onfulfilled: (value: TResult1) => ITSResolvable<R>): Promise<R>;
// then?<TResult1 = DataType>(): Promise<TResult1>;
/**
* Returns a promise for you to use.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/then.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/then.js"></script>`!
*/
promise?<R, TResult1 = {
put: Record<string, DataType>;
key: ReferenceKey;
gun: IGunChainReference<DataType, ReferenceKey>;
}>(onfulfilled: (value: TResult1) => ITSResolvable<R>): Promise<R>;
promise?<TResult1 = {
put: Record<string, DataType>;
key: ReferenceKey;
gun: IGunChainReference<DataType, ReferenceKey>;
}>(): Promise<TResult1>;
/**
* bye lets you change data after that browser peer disconnects.
* This is useful for games and status messages,
* that if a player leaves you can remove them from the game or set a user's status to "away".
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/bye.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/bye.js"></script>`!
*/
bye?(): {
put(data: Saveable<DataType>): void;
};
/**
* Say you save some data, but want to do something with it later, like expire it or refresh it.
* Well, then `later` is for you! You could use this to easily implement a TTL or similar behavior.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/later.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/later.js"></script>`!
*/
later?(callback: (this: IGunChainReference<DataType, ReferenceKey>, data: Record<string, DataType>, key: ReferenceKey) => void, seconds: number): IGunChainReference<DataType, ReferenceKey>;
/**
* After you save some data in an unordered list, you may need to remove it.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/unset.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/unset.js"></script>`!
*/
unset?<K extends keyof DataType>(data: K): IGunChainReference<DataType, ReferenceKey>;
/**
* Subscribes to all future events that occur on the Timegraph and retrieve a specified number of old events
*
* **Warning**: The Timegraph extension isn't required by default, you would need to include at "gun/lib/time.js"
*/
time?<K extends keyof DataType>(callback: (data: DataType[K], key: ReferenceKey, time: number) => void, alsoReceiveNOldEvents?: number): IGunChainReference<DataType, ReferenceKey>;
/** Pushes data to a Timegraph with it's time set to Gun.state()'s time */
time?<K extends keyof DataType>(data: DataType[K]): void;
/**
* Creates a new user and calls callback upon completion.
* @param alias Username or Alias which can be used to find a user.
* @param pass Passphrase that will be extended with PBKDF2 to make it a secure way to login.
* @param cb Callback that is to be called upon creation of the user.
* @param opt Option Object containing options for creation. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
create(alias: string, pass: string, cb?: (ack: {
ok: 0;
pub: string;
} | {
err: string;
}) => void, opt?: {}): IGunChainReference;
/**
* Authenticates a user, previously created via User.create.
* @param alias Username or Alias which can be used to find a user.
* @param pass Passphrase for the user
* @param cb Callback that is to be called upon authentication of the user.
* @param opt Option Object containing options for authentication. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
auth(alias: string, pass: string, cb?: (ack: {
ack: 2;
get: string;
on: (...args: [unknown, unknown, unknown]) => unknown;
put: {
alias: string;
auth: any;
epub: string;
pub: string;
};
sea: IGunCryptoKeyPair;
soul: string;
} | {
err: string;
}) => void, opt?: {}): IGunChainReference;
/**
* Authenticates a user, previously created via User.create.
* @param pair Public/Private Key Pair
* @param cb Callback that is to be called upon authentication of the user.
* @param opt Option Object containing options for authentication. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
auth(pair: CryptoKeyPair, cb?: (ack: {
ack: 2;
get: string;
on: (...args: [unknown, unknown, unknown]) => unknown;
put: {
alias: string;
auth: any;
epub: string;
pub: string;
};
sea: IGunCryptoKeyPair;
soul: string;
} | {
err: string;
}) => void, opt?: {}): IGunChainReference;
/**
* Returns the key pair in the form of an object as below.
*/
pair(): IGunCryptoKeyPair;
/**
* Log out currently authenticated user. Parameters are unused in the current implementation.
* @param opt unused in current implementation.
* @param cb unused in current implementation.
*/
leave(opt?: never, cb?: never): IGunChainReference;
/**
* Deletes a user from the current gun instance and propagates the delete to other peers.
* @param alias Username or alias.
* @param pass Passphrase for the user.
* @param cb Callback that is called when the user was successfully deleted.
*/
delete(alias: string, pass: string, cb?: (ack: {
ok: 0;
}) => void): Promise<void>;
/**
* Recall saves a users credentials in sessionStorage of the browser. As long as the tab of your app is not closed the user stays logged in, even through page refreshes and reloads.
* @param opt option object If you want to use browser sessionStorage to allow users to stay logged in as long as the session is open, set opt.sessionStorage to true
* @param cb internally the callback is passed on to the user.auth function to log the user back in. Refer to user.auth for callback documentation.
*/
recall(opt?: {
sessionStorage: boolean;
}, cb?: Parameters<IGunChainReference['auth']>[2]): IGunChainReference;
/**
* @param publicKey If you know a users publicKey you can get their user graph and see any unencrypted data they may have stored there.
*/
user(publicKey?: string): IGunChainReference;
}

7
types/gun/AckCallback.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
export declare type AckCallback = (ack: {
err: string;
ok: undefined;
} | {
err: undefined;
ok: string;
}) => void;

20
types/gun/AuthCallback.d.ts vendored Normal file
View File

@ -0,0 +1,20 @@
import { ISEAPair } from "../sea/ISEAPair";
export type AuthCallback = (ack: {
ack: 2;
ing: false,
id: number,
get: string;
on: (tag:unknown , args:unknown, as: unknown) => unknown;
put: {
alias: string;
auth: string;
epub: string;
pub: string;
};
sea: ISEAPair;
err?: undefined;
soul: string;
} | {
err: string;
}) => void

9
types/gun/CreateCallback.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
export type CreateCallback = (ack: {
ok: 0;
pub: string;
err?:undefined
} | {
ok?: 0;
pub?: string;
err: string;
}) => any

45
types/gun/IGun.d.ts vendored Normal file
View File

@ -0,0 +1,45 @@
import { ISEA } from '../sea/ISEA';
import { AckCallback } from './AckCallback';
import { IGunConstructorOptions } from './IGunConstructorOptions';
import { IGunDataType } from './IGunDataType';
import { IGunInstance } from './IGunInstance';
import { IGunReturnObject } from './IGunReturnObject';
export interface IGun {
/**
* @description
* no parameters creates a local datastore using the default persistence layer, either localStorage or Radisk.
* @param options
* passing a URL creates the above local datastore that also tries to sync with the URL.
*
* or you can pass in an array of URLs to sync with multiple peers.
* DataType must be type not interface
*/
<DataType extends IGunDataType = IGunDataType>(options?: IGunConstructorOptions): IGunInstance<DataType,undefined>;
new <DataType extends IGunDataType= IGunDataType>(options?: IGunConstructorOptions): IGunInstance<DataType,undefined>;
readonly node: IGun;
/** @see https://gun.eco/docs/SEA */
readonly SEA: ISEA;
readonly version: string;
readonly chain: IGunInstance<IGunDataType,undefined>;
readonly log: {
(...argv: any[]): void;
once(...argv: any[]): void;
off: boolean;
};
/** Returns true if data is a gun node, otherwise false. */
is(anything: any): anything is IGunInstance<IGunDataType, undefined>;
/**
* Returns data's gun ID (instead of manually grabbing its metadata i.e. data["_"]["#"], which is faster but could change in the future)
*
* Returns undefined if data is not correct gun data.
*/
soul(data: IGunReturnObject<any, any>): string | undefined;
/** Returns a "gun-ified" variant of the json input by injecting a new gun ID into the metadata field. */
ify(json: any): any;
state():number
}

View File

@ -1,9 +1,4 @@
/** export type IGunConstructorOptions = Partial<{
* options['module name'] allows you to pass options to a 3rd party module.
* Their project README will likely list the exposed options
* https://github.com/amark/gun/wiki/Modules
*/
export interface IGunConstructorOptions extends Partial<{
/** Undocumented but mentioned. Write data to a JSON. */ /** Undocumented but mentioned. Write data to a JSON. */
file: string; file: string;
@ -28,12 +23,11 @@ export interface IGunConstructorOptions extends Partial<{
localStorage: boolean; localStorage: boolean;
/** uuid allows you to override the default 24 random alphanumeric soul generator with your own function. */ /** uuid allows you to override the default 24 random alphanumeric soul generator with your own function. */
uuid(): string; uuid(): string | number;
/** /**
* allows you to pass options to a 3rd party module. Their project README will likely list the exposed options * allows you to pass options to a 3rd party module. Their project README will likely list the exposed options
* @see https://github.com/amark/gun/wiki/Modules * @see https://github.com/amark/gun/wiki/Modules
*/ */
[key: string]: any; [key: string]: any;
}> { }> | string | string[];
}

8
types/gun/IGunDataType.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
export type IGunDataType = {
[P in string]: IGunDataType | string | number | boolean | null
}&
{
[T in number]: IGunDataType | string | number | boolean | null
}
export type IGunNodeDataType = IGunDataType | string | number | boolean | null

86
types/gun/IGunFinalTreeMethods.d.ts vendored Normal file
View File

@ -0,0 +1,86 @@
import { AckCallback } from "./AckCallback";
import { AuthCallback } from "./AuthCallback";
import { IGunDataType } from "./IGunDataType";
import { IGunInstance } from "./IGunInstance";
import { IGunReturnObject } from "./IGunReturnObject";
export interface IGunFinalTreeMethods<TValue, TKey, TSoul> {
not?(callback: (key: TKey) => void): IGunFinalTreeMethods<TValue, TKey, TSoul> ;
/**
* Say you save some data, but want to do something with it later, like expire it or refresh it.
* Well, then `later` is for you! You could use this to easily implement a TTL or similar behavior.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/later.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/later.js"></script>`!
*/
later?(callback: (data: TValue, key: TKey) => void, seconds: number): IGunFinalTreeMethods<TValue, TKey, TSoul> ;
off(): IGunFinalTreeMethods<TValue, TKey, TSoul> ;
/* /**
* Save data into gun, syncing it with your connected peers.
*
* * You cannot save primitive values at the root level.
*
* @param data You do not need to re-save the entire object every time,
* gun will automatically merge your data into what already exists as a "partial" update.
*
* * `undefined`, `NaN`, `Infinity`, `array`, will be rejected.
* * Traditional arrays are dangerous in real-time apps. Use `gun.set` instead.
*
* @param callback invoked on each acknowledgment
* @param options additional options (used for specifying certs)
*/
put(data: Partial<TValue>, callback?: AckCallback | null, options?: { opt?: { cert?: string } }): IGunFinalTreeMethods<TValue, TKey, TSoul>
/**
* Subscribe to updates and changes on a node or property in real-time.
* @param option Currently, the only option is to filter out old data, and just be given the changes.
* If you're listening to a node with 100 fields, and just one changes,
* you'll instead be passed a node with a single property representing that change rather than the full node every time.
* @param callback
* Once initially and whenever the property or node you're focused on changes, this callback is immediately fired with the data as it is at that point in time.
*
* Since gun streams data, the callback will probably be called multiple times as new chunks come in.
* To remove a listener call .off() on the same property or node.
*/
on(callback: (data: IGunReturnObject<TValue, TSoul>, key: TKey, _msg:any, _ev:any) => void, option?: {
change: boolean;
} | boolean, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any): IGunFinalTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>;
/**
* Subscribe to database event.
* @param eventName event name that you want listen to (currently only 'auth')
* @param callback once event fire callback
*/
on(eventName: 'auth', cb: AuthCallback, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any): IGunFinalTreeMethods<TValue, TKey, TSoul>
/**
* Get the current data without subscribing to updates. Or `undefined` if it cannot be found.
* @returns In the document, it said the return value may change in the future. Don't rely on it.
*/
once(callback?: (data: IGunReturnObject<TValue, TSoul>, key: TKey) => void, option?: {
wait: number;
}): IGunFinalTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>;
/**
* Open behaves very similarly to gun.on, except it gives you the **full depth of a document** on every update.
* It also works with graphs, tables, or other data structures. Think of it as opening up a live connection to a document.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/open.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/open.js"></script>`!
*/
open?(callback: (data: IGunReturnObject<TValue, TSoul>) => any, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<TValue>): IGunFinalTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>;
/**
* Loads the full object once. It is the same as `open` but with the behavior of `once`.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/load.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/load.js"></script>`!
*/
load?(callback: (data: IGunReturnObject<TValue, TSoul>) => void, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<TValue>): IGunFinalTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>
/**goes back user chain */
back(amount?:number) : IGunInstance<IGunDataType, string | undefined>
}

90
types/gun/IGunFinalUserTreeMethods.d.ts vendored Normal file
View File

@ -0,0 +1,90 @@
import { AckCallback } from "./AckCallback";
import { AuthCallback } from "./AuthCallback";
import { IGunDataType } from "./IGunDataType";
import { IGunReturnObject } from "./IGunReturnObject";
import { IGunUserInstance } from "./IGunUserInstance";
export interface IGunFinalUserTreeMethods<TValue, TKey, TSoul> {
/**
* check out https://gun.eco/docs/User#user-secret
* save secret that only trusted users can read
*
*/
not?(callback: (key: TKey) => void): IGunFinalUserTreeMethods<TValue, TKey, TSoul> ;
/**
* Say you save some data, but want to do something with it later, like expire it or refresh it.
* Well, then `later` is for you! You could use this to easily implement a TTL or similar behavior.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/later.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/later.js"></script>`!
*/
later?(callback: (data: TValue, key: TKey) => void, seconds: number): IGunFinalUserTreeMethods<TValue, TKey, TSoul>;
off(): IGunFinalUserTreeMethods<TValue, TKey, TSoul> ;
/* /**
* Save data into gun, syncing it with your connected peers.
*
* * You cannot save primitive values at the root level.
*
* @param data You do not need to re-save the entire object every time,
* gun will automatically merge your data into what already exists as a "partial" update.
*
* * `undefined`, `NaN`, `Infinity`, `array`, will be rejected.
* * Traditional arrays are dangerous in real-time apps. Use `gun.set` instead.
*
* @param callback invoked on each acknowledgment
* @param options additional options (used for specifying certs)
*/
put(data: Partial<TValue>, callback?: AckCallback | null, options?: { opt?: { cert?: string } }): IGunFinalUserTreeMethods<TValue, TKey, TSoul>
/**
* Subscribe to updates and changes on a node or property in real-time.
* @param option Currently, the only option is to filter out old data, and just be given the changes.
* If you're listening to a node with 100 fields, and just one changes,
* you'll instead be passed a node with a single property representing that change rather than the full node every time.
* @param callback
* Once initially and whenever the property or node you're focused on changes, this callback is immediately fired with the data as it is at that point in time.
*
* Since gun streams data, the callback will probably be called multiple times as new chunks come in.
* To remove a listener call .off() on the same property or node.
*/
on(callback: (data: IGunReturnObject<TValue, TSoul>, key: TKey, _msg:any, _ev:any) => void, option?: {
change: boolean;
} | boolean, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any): IGunFinalUserTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>;
/**
* Subscribe to database event.
* @param eventName event name that you want listen to (currently only 'auth')
* @param callback once event fire callback
*/
on(eventName: 'auth', cb: AuthCallback, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any):IGunFinalUserTreeMethods<TValue, TKey, TSoul>
/**
* Get the current data without subscribing to updates. Or `undefined` if it cannot be found.
* @returns In the document, it said the return value may change in the future. Don't rely on it.
*/
once(callback?: (data: IGunReturnObject<TValue, TSoul>, key: TKey) => void, option?: {
wait: number;
}): IGunFinalUserTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>;
/**
* Open behaves very similarly to gun.on, except it gives you the **full depth of a document** on every update.
* It also works with graphs, tables, or other data structures. Think of it as opening up a live connection to a document.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/open.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/open.js"></script>`!
*/
open?(callback: (data: IGunReturnObject<TValue, TSoul>) => any, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<TValue>): IGunFinalUserTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>;
/**
* Loads the full object once. It is the same as `open` but with the behavior of `once`.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/load.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/load.js"></script>`!
*/
load?(callback: (data: IGunReturnObject<TValue, TSoul>) => void, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<TValue>): IGunFinalUserTreeMethods<TValue, TKey, TSoul> | Promise<IGunReturnObject<TValue, TSoul>>
/**goes back user chain */
back(amount?:number) : IGunUserInstance<IGunDataType, string | undefined>
secret(string: string, callback : (...args:unknown[])=> any): IGunFinalUserTreeMethods<TValue, TKey, TSoul>
}

181
types/gun/IGunInstance.d.ts vendored Normal file
View File

@ -0,0 +1,181 @@
import { And } from "../shared/And";
import { AckCallback } from "./AckCallback";
import { AuthCallback } from "./AuthCallback";
import { IGunConstructorOptions } from "./IGunConstructorOptions";
import { IGunDataType, IGunNodeDataType } from "./IGunDataType";
import { IGunFinalTreeMethods } from "./IGunFinalTreeMethods";
import { IGunReturnObject } from "./IGunReturnObject";
import { IGunTree } from "./IGunTree";
import { IGunUserInstance } from "./IGunUserInstance";
export interface IGunInstance<
CurrentTreeDataType extends IGunNodeDataType,
TSoul extends string | undefined
>{
not?(callback: (key: TSoul) => void): IGunInstance<CurrentTreeDataType, TSoul> ;
/**
* Say you save some data, but want to do something with it later, like expire it or refresh it.
* Well, then `later` is for you! You could use this to easily implement a TTL or similar behavior.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/later.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/later.js"></script>`!
*/
later?(callback: (data: CurrentTreeDataType, key: TSoul) => void, seconds: number): IGunInstance<CurrentTreeDataType, TSoul> ;
off(): IGunInstance<CurrentTreeDataType, TSoul> ;
/* /**
* Save data into gun, syncing it with your connected peers.
*
* * You cannot save primitive values at the root level.
*
* @param data You do not need to re-save the entire object every time,
* gun will automatically merge your data into what already exists as a "partial" update.
*
* * `undefined`, `NaN`, `Infinity`, `array`, will be rejected.
* * Traditional arrays are dangerous in real-time apps. Use `gun.set` instead.
*
* @param callback invoked on each acknowledgment
* @param options additional options (used for specifying certs)
*/
put(data: Partial<CurrentTreeDataType>, callback?: AckCallback | null, options?: { opt?: { cert?: string } }): IGunInstance<CurrentTreeDataType, TSoul>
/**
* Subscribe to updates and changes on a node or property in real-time.
* @param option Currently, the only option is to filter out old data, and just be given the changes.
* If you're listening to a node with 100 fields, and just one changes,
* you'll instead be passed a node with a single property representing that change rather than the full node every time.
* @param callback
* Once initially and whenever the property or node you're focused on changes, this callback is immediately fired with the data as it is at that point in time.
*
* Since gun streams data, the callback will probably be called multiple times as new chunks come in.
* To remove a listener call .off() on the same property or node.
*/
on(callback: (data: IGunReturnObject<CurrentTreeDataType, TSoul>, key: TSoul, _msg:any, _ev:any) => void, option?: {
change: boolean;
} | boolean, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any): IGunInstance<CurrentTreeDataType, TSoul> | Promise<IGunReturnObject<CurrentTreeDataType, TSoul>>;
/**
* Subscribe to database event.
* @param eventName event name that you want listen to (currently only 'auth')
* @param callback once event fire callback
*/
on(eventName: 'auth', cb: AuthCallback, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any): IGunInstance<CurrentTreeDataType, TSoul>
/**
* Get the current data without subscribing to updates. Or `undefined` if it cannot be found.
* @returns In the document, it said the return value may change in the future. Don't rely on it.
*/
once(callback?: (data: IGunReturnObject<CurrentTreeDataType, TSoul>, key: TSoul) => void, option?: {
wait: number;
}): IGunInstance<CurrentTreeDataType, TSoul>| Promise<IGunReturnObject<CurrentTreeDataType, TSoul>>;
/**
* Open behaves very similarly to gun.on, except it gives you the **full depth of a document** on every update.
* It also works with graphs, tables, or other data structures. Think of it as opening up a live connection to a document.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/open.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/open.js"></script>`!
*/
open?(callback: (data: IGunReturnObject<CurrentTreeDataType, TSoul>) => any, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<CurrentTreeDataType>): IGunInstance<CurrentTreeDataType, TSoul> | Promise<IGunReturnObject<CurrentTreeDataType, TSoul>>;
/**
* Loads the full object once. It is the same as `open` but with the behavior of `once`.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/load.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/load.js"></script>`!
*/
load?(callback: (data: IGunReturnObject<CurrentTreeDataType, TSoul>) => void, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<CurrentTreeDataType>): IGunInstance<CurrentTreeDataType, TSoul> | Promise<IGunReturnObject<CurrentTreeDataType, TSoul>>
/**goes back user chain */
back(amount?:number) : IGunInstance<IGunDataType, string | undefined>
/**
* **.set does not mean 'set data', it means a Mathematical Set**
*
* Add a unique item to an unordered list.
* `gun.set` works like a mathematical set, where each item in the list is unique.
* If the item is added twice, it will be merged.
*
* **This means only objects, for now, are supported.**
* @param data the object to add to the set
* @param callback optional function to invoke when the operation is complete
* @param options additional options (used for specifying certs)
*/
set<K extends keyof CurrentTreeDataType>(data: CurrentTreeDataType[K], callback?: AckCallback | null, options?: { opt?: { cert?: string } }): CurrentTreeDataType[K] extends IGunDataType ? IGunInstance<CurrentTreeDataType[K], string>: IGunFinalTreeMethods<CurrentTreeDataType[K], K, string>;
/**
* Where to read data from.
* @param key The key is the ID or property name of the data that you saved from earlier
* (or that will be saved later).
* * Note that if you use .put at any depth after a get it first reads the data and then writes, merging the data as a partial update.
* @param callback You will usually be using gun.on or gun.once to actually retrieve your data,
* not this callback (it is intended for more low-level control, for module and extensions).
*
* **Avoid use callback. The type in the document may be wrong.**
*
* **Here the type of callback respect to the actual behavior**
*/
get<K extends keyof CurrentTreeDataType>(key: K, callback?: (
data: IGunReturnObject<CurrentTreeDataType[K], string>,
key: K) => any):
And< Promise<CurrentTreeDataType[K]>,
CurrentTreeDataType[K] extends IGunDataType ?
IGunInstance<CurrentTreeDataType[K], string> & IGunFinalTreeMethods<CurrentTreeDataType[K], K, string> :
IGunDataType extends CurrentTreeDataType[K] ?
IGunFinalTreeMethods<CurrentTreeDataType[K] , K, string> & IGunInstance<IGunDataType, string>
: IGunFinalTreeMethods<CurrentTreeDataType[K], K, string>>
map<T>(match: (data: CurrentTreeDataType) => T ): IGunFinalTreeMethods<T, keyof CurrentTreeDataType, string>
/**
* After you save some data in an unordered list, you may need to remove it.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/unset.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/unset.js"></script>`!
*/
unset?<K extends keyof CurrentTreeDataType>(data: K): CurrentTreeDataType[K] extends IGunDataType ? IGunInstance<CurrentTreeDataType[K], string>: IGunFinalTreeMethods<CurrentTreeDataType[K], K, string>;
/**
* Map iterates over each property and item on a node, passing it down the chain,
* behaving like a forEach on your data.
* It also subscribes to every item as well and listens for newly inserted items.
*/
map(match: IGunTree): CurrentTreeDataType[keyof CurrentTreeDataType] extends IGunDataType? IGunInstance<CurrentTreeDataType[keyof CurrentTreeDataType], string> : IGunFinalTreeMethods<CurrentTreeDataType[keyof CurrentTreeDataType], keyof CurrentTreeDataType, string>
opt(opt: IGunConstructorOptions): unknown
/**
*
* Path does the same thing as `.get` but has some conveniences built in.
* @deprecated This is not friendly with type system.
*
* **Warning**: This extension was removed from core, you probably shouldn't be using it!
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/path.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/path.js"></script>`!
*/
path?(path: string | string[]): unknown;
/**
* Subscribes to all future events that occur on the Timegraph and retrieve a specified number of old events
*
* **Warning**: The Timegraph extension isn't required by default, you would need to include at "gun/lib/time.js"
*/
time?<K extends keyof CurrentTreeDataType>(callback: (data: CurrentTreeDataType[K], key: K, time: number) => void, alsoReceiveNOldEvents?: number): CurrentTreeDataType[K] extends IGunDataType ? IGunInstance<CurrentTreeDataType[K], string>: IGunFinalTreeMethods<CurrentTreeDataType[K], K, string>;
/** Pushes data to a Timegraph with it's time set to Gun.state()'s time */
time?<K extends keyof CurrentTreeDataType>(data: CurrentTreeDataType[K]): CurrentTreeDataType[K] extends IGunDataType ? IGunInstance<CurrentTreeDataType[K], string>: IGunFinalTreeMethods<CurrentTreeDataType[K], K, string>;
/**
* @param publicKey If you know a users publicKey you can get their user graph and see any unencrypted data they may have stored there.
*/
user<TUserGraph extends IGunDataType>(): IGunUserInstance<TUserGraph, undefined>
user(publicKey: string): IGunUserInstance<CurrentTreeDataType, undefined>
}

6
types/gun/IGunReturnObject.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
export declare type IGunReturnObject<TObject, TSoul> = (TObject & {
_:{
'#': TSoul,
'>': TObject extends number |string|boolean?unknown : Record<keyof TObject, number>
}
})|undefined

10
types/gun/IGunTree.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
export interface IGunTree{
"+"?:string|IGunTree,
"#"?:string|IGunTree,
"."?:string|IGunTree,
"="?:string|IGunTree,
"*"?:string|IGunTree,
">"?:string|IGunTree,
"<"?:string|IGunTree,
'-'?:string|number|IGunTree
}

232
types/gun/IGunUserInstance.d.ts vendored Normal file
View File

@ -0,0 +1,232 @@
import { ISEAPair } from "../sea/ISEAPair";
import { And } from "../shared/And";
import { AckCallback } from "./AckCallback";
import { AuthCallback } from "./AuthCallback";
import { CreateCallback } from "./CreateCallback";
import { IGunConstructorOptions } from "./IGunConstructorOptions";
import { IGunDataType, IGunNodeDataType } from "./IGunDataType";
import { IGunFinalUserTreeMethods } from "./IGunFinalUserTreeMethods";
import { IGunReturnObject } from "./IGunReturnObject";
import { IGunTree } from "./IGunTree";
export interface IGunUserInstance<CurrentDataType extends IGunNodeDataType, TKey extends string | undefined> {
/**
* check out https://gun.eco/docs/User#user-secret
* save secret that only trusted users can read
*
*/
not?(callback: (key: TKey) => void): IGunUserInstance<CurrentDataType, TKey> ;
/**
* Say you save some data, but want to do something with it later, like expire it or refresh it.
* Well, then `later` is for you! You could use this to easily implement a TTL or similar behavior.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/later.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/later.js"></script>`!
*/
later?(callback: (data: CurrentDataType, key: TKey) => void, seconds: number): IGunUserInstance<CurrentDataType, TKey> ;
off(): IGunUserInstance<CurrentDataType, TKey> ;
/* /**
* Save data into gun, syncing it with your connected peers.
*
* * You cannot save primitive values at the root level.
*
* @param data You do not need to re-save the entire object every time,
* gun will automatically merge your data into what already exists as a "partial" update.
*
* * `undefined`, `NaN`, `Infinity`, `array`, will be rejected.
* * Traditional arrays are dangerous in real-time apps. Use `gun.set` instead.
*
* @param callback invoked on each acknowledgment
* @param options additional options (used for specifying certs)
*/
put(data: Partial<CurrentDataType>, callback?: AckCallback | null, options?: { opt?: { cert?: string } }):IGunUserInstance<CurrentDataType, TKey>
/**
* Subscribe to updates and changes on a node or property in real-time.
* @param option Currently, the only option is to filter out old data, and just be given the changes.
* If you're listening to a node with 100 fields, and just one changes,
* you'll instead be passed a node with a single property representing that change rather than the full node every time.
* @param callback
* Once initially and whenever the property or node you're focused on changes, this callback is immediately fired with the data as it is at that point in time.
*
* Since gun streams data, the callback will probably be called multiple times as new chunks come in.
* To remove a listener call .off() on the same property or node.
*/
on(callback: (data: IGunReturnObject<CurrentDataType, TKey>, key: TKey, _msg:any, _ev:any) => void, option?: {
change: boolean;
} | boolean, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any): IGunUserInstance<CurrentDataType, TKey> | Promise<IGunReturnObject<CurrentDataType, TKey>>;
/**
* Subscribe to database event.
* @param eventName event name that you want listen to (currently only 'auth')
* @param callback once event fire callback
*/
on(eventName: 'auth', cb: AuthCallback, eas?:{$?:any, subs?: unknown[] | { push(arg: unknown) }}, as?:any):IGunUserInstance<CurrentDataType, TKey>
/**
* Get the current data without subscribing to updates. Or `undefined` if it cannot be found.
* @returns In the document, it said the return value may change in the future. Don't rely on it.
*/
once(callback?: (data: IGunReturnObject<CurrentDataType, TKey>, key: TKey) => void, option?: {
wait: number;
}): IGunUserInstance<CurrentDataType, TKey> | Promise<IGunReturnObject<CurrentDataType, TKey>>;
/**
* Open behaves very similarly to gun.on, except it gives you the **full depth of a document** on every update.
* It also works with graphs, tables, or other data structures. Think of it as opening up a live connection to a document.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/open.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/open.js"></script>`!
*/
open?(callback: (data: IGunReturnObject<CurrentDataType, TKey>) => any, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<CurrentDataType>): IGunUserInstance<CurrentDataType, TKey> | Promise<IGunReturnObject<CurrentDataType, TKey>>;
/**
* Loads the full object once. It is the same as `open` but with the behavior of `once`.
*
* **Warning**: Not included by default! You must include it yourself via `require('gun/lib/load.js')` or
* `<script src="https://cdn.jsdelivr.net/npm/gun/lib/load.js"></script>`!
*/
load?(callback: (data: IGunReturnObject<CurrentDataType, TKey>) => void, opt?: { at?: any, key?: any, doc?: any, ids?: any, any?: any, meta?: any, ev?: { off?: () => {} } }, at?: Partial<CurrentDataType>): IGunUserInstance<CurrentDataType, TKey> | Promise<IGunReturnObject<CurrentDataType, TKey>>
/**goes back user chain */
back(amount?:number) : IGunUserInstance<CurrentDataType, string>
secret(string: string, callback : (...args:unknown[])=> any): IGunUserInstance<CurrentDataType, TKey>
is?: {
alias: string | ISEAPair
epub: string
pub: string
}
/**
* Creates a new user and calls callback upon completion.
* @param alias Username or Alias which can be used to find a user.
* @param pass Passphrase that will be extended with PBKDF2 to make it a secure way to login.
* @param cb Callback that is to be called upon creation of the user.
* @param opt Option Object containing options for creation. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
create(alias: string, pass: string, cb?: CreateCallback, opt?: {}): unknown;
/**
* Creates a new user and calls callback upon completion.
* @param pair User cryptographic pair
* @param cb Callback that is to be called upon creation of the user.
* @param opt Option Object containing options for creation. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
create(pair: ISEAPair, cb?: AuthCallback, opt?: {}): unknown;
/**
* Authenticates a user, previously created via User.create.
* @param alias Username or Alias which can be used to find a user.
* @param pass Passphrase for the user
* @param cb Callback that is to be called upon authentication of the user.
* @param opt Option Object containing options for authentication. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
auth(alias: string, pass: string, cb?:AuthCallback, opt?: {}): unknown
/**
* Authenticates a user, previously created via User.create.
* @param pair Public/Private Key Pair
* @param cb Callback that is to be called upon authentication of the user.
* @param opt Option Object containing options for authentication. (In gun options are added at end of syntax. opt is rarely used, hence is added at the end.)
*/
auth(pair: ISEAPair, cb?: AuthCallback, opt?: {}): unknown;
/**
* Log out currently authenticated user. Parameters are unused in the current implementation.
* @param opt unused in current implementation.
* @param cb unused in current implementation.
*/
leave(opt?: never, cb?: never): unknown;
/**
* Deletes a user from the current gun instance and propagates the delete to other peers.
* @param alias Username or alias.
* @param pass Passphrase for the user.
* @param cb Callback that is called when the user was successfully deleted.
*/
delete(alias: string, pass: string, cb?: (ack: {
ok: 0;
}) => void): Promise<void>;
/**
* Where to read data from.
* @param key The key is the ID or property name of the data that you saved from earlier
* (or that will be saved later).
* * Note that if you use .put at any depth after a get it first reads the data and then writes, merging the data as a partial update.
* @param callback You will usually be using gun.on or gun.once to actually retrieve your data,
* not this callback (it is intended for more low-level control, for module and extensions).
*
* **Avoid use callback. The type in the document may be wrong.**
*
* **Here the type of callback respect to the actual behavior**
*/
get<K extends keyof CurrentDataType>(key: K, callback?: (
data: IGunReturnObject<CurrentDataType[K], string>,
key: K) => any):
And< Promise<CurrentDataType[K]>,
CurrentDataType[K] extends IGunDataType ?
IGunUserInstance<CurrentDataType[K], string> & IGunFinalUserTreeMethods<CurrentDataType[K], K, string> :
IGunDataType extends CurrentDataType[K] ?
IGunFinalUserTreeMethods<CurrentDataType[K], K, string> & IGunUserInstance< IGunDataType , string>
: IGunFinalUserTreeMethods<CurrentDataType[K], K, string>>
/**
* **.set does not mean 'set data', it means a Mathematical Set**
*
* Add a unique item to an unordered list.
* `gun.set` works like a mathematical set, where each item in the list is unique.
* If the item is added twice, it will be merged.
*
* **This means only objects, for now, are supported.**
* @param data the object to add to the set
* @param callback optional function to invoke when the operation is complete
* @param options additional options (used for specifying certs)
*/
set<K extends keyof CurrentDataType>(data: CurrentDataType[K], callback?: AckCallback | null, options?: { opt?: { cert?: string } }): CurrentDataType[K] extends IGunDataType? IGunUserInstance<CurrentDataType[K], string>: IGunFinalUserTreeMethods<CurrentDataType[K], K, string> ;
opt(opt: IGunConstructorOptions): unknown
/**
* Recall saves a users credentials in sessionStorage of the browser. As long as the tab of your app is not closed the user stays logged in, even through page refreshes and reloads.
* @param opt option object If you want to use browser sessionStorage to allow users to stay logged in as long as the session is open, set opt.sessionStorage to true
* @param cb internally the callback is passed on to the user.auth function to log the user back in. Refer to user.auth for callback documentation.
*/
recall(opt?: {
sessionStorage: boolean;
}, cb?: AuthCallback): IGunUserInstance<CurrentDataType, TKey>;
map(match: IGunTree ): CurrentDataType[keyof CurrentDataType] extends IGunDataType? IGunUserInstance<CurrentDataType[keyof CurrentDataType], string> : IGunFinalUserTreeMethods<CurrentDataType[keyof CurrentDataType], keyof CurrentDataType, string>
map<T>(match: (data: CurrentDataType) => T ): IGunFinalUserTreeMethods<T, keyof CurrentDataType, string>
/**
* Subscribes to all future events that occur on the Timegraph and retrieve a specified number of old events
*
* **Warning**: The Timegraph extension isn't required by default, you would need to include at "gun/lib/time.js"
*/
time?<K extends keyof CurrentDataType>(callback: (data: CurrentDataType[K], key: K, time: number) => void, alsoReceiveNOldEvents?: number): CurrentDataType[K] extends IGunDataType ? IGunUserInstance<CurrentDataType[K], string>: IGunFinalUserTreeMethods<CurrentDataType[K], K, string>;
/** Pushes data to a Timegraph with it's time set to Gun.state()'s time */
time?<K extends keyof CurrentDataType>(data: CurrentDataType[K]): CurrentDataType[K] extends IGunDataType ? IGunUserInstance<CurrentDataType[K], string>: IGunFinalUserTreeMethods<CurrentDataType[K], K, string>;
/**
* @param publicKey If you know a users publicKey you can get their user graph and see any unencrypted data they may have stored there.
*/
user<TUserGraph extends IGunDataType>(): IGunUserInstance<TUserGraph, undefined>
user(publicKey: string): IGunUserInstance<CurrentDataType, undefined>
}

View File

@ -1,121 +0,0 @@
import { expectError } from 'tsd';
import Gun = require('../index');
Gun(['http://server1.com/gun', 'http://server2.com/gun']);
Gun({
s3: {
key: '',
secret: '',
bucket: ''
},
file: 'file/path.json',
uuid() {
return 'xxxxxx';
}
});
interface AppState {
object: {
num: number;
str: string;
/** Comment test */
bool: boolean;
specstr: 'a' | 'b';
obj: {
arr2: Record<string, { foo: number; bar: string }>;
};
};
chatRoom: Record<string, { by: string; message: string }>;
}
const app = new Gun<AppState>();
// Put and get something that was previously put
app.get('object')
.get('bool')
.put(true);
app.get('object')
.get('num')
.put(1);
app.get('object').put({
bool: true
});
// Set and get something that was inserted using `set`.
const appSet = app.get('object')
.get('obj')
.get('arr2');
appSet.set({ foo: 1, bar: '2' });
// getting an auto-generated key may return an undefined value.
appSet.get('stringIdentifier').once(a => a?.foo);
expectError(
app.get('object')
.get('bool')
.put(1));
app.get('object').on(data => {
data.bool;
});
app.get('object').off();
app.get('object').once(data => {
if (data) data.bool;
});
async function name() {
const data = await app.get('object').promise!();
data.put.bool;
}
app.get('chatRoom').time!({ by: 'A', message: 'Hello' });
app.get('chatRoom').time!(msg => {
msg.by;
}, 20);
expectError(
app.get('object').time!({ a: 1 }));
class X {
val: string = 'someString';
b() { }
}
interface BadState {
// Top level primitives
a: 1;
b: {
// Ban functions
c: () => void;
// Ban class
d: typeof X;
// Recursive check for banned types
e: {
f: () => void;
};
};
// Filter, remove functions on prototype.
c: X;
}
const bad = new Gun<BadState>();
expectError(
bad.get('a').put(1));
expectError(bad.get('b')
.get('c')
.put(() => { }));
expectError(bad.get('b')
.get('d')
.put(X));
expectError(
bad.get('b').put({ c: () => { }, d: X, e: { f: () => { } } }));
expectError(
bad.get('c').put(new X()));

View File

@ -1,13 +1,13 @@
import { IGunCryptoKeyPair } from "../types"; import { ISEAPair } from "./ISEAPair";
import { ISEAPolicy } from "./ISEAPolicy";
/** @see https://gun.eco/docs/SEA */ /** @see https://gun.eco/docs/SEA */
export interface IGunStaticSEA { export interface ISEA {
/** If you want SEA to throw while in development, turn SEA.throw = true on, but please do not use this in production. */ /** If you want SEA to throw while in development, turn SEA.throw = true on, but please do not use this in production. */
throw?: boolean; throw?: boolean;
/** Last known error */ /** Last known error */
err?: Error; err?: string;
/** /**
* This gives you a Proof of Work (POW) / Hashing of Data * This gives you a Proof of Work (POW) / Hashing of Data
@ -34,7 +34,7 @@ export interface IGunStaticSEA {
* You will need this for most of SEA's API, see those method's examples. * You will need this for most of SEA's API, see those method's examples.
* The default cryptographic primitives for the asymmetric keys are ECDSA for signing and ECDH for encryption. * The default cryptographic primitives for the asymmetric keys are ECDSA for signing and ECDH for encryption.
*/ */
pair(cb?: (data: IGunCryptoKeyPair) => void, opt?: {}): Promise<IGunCryptoKeyPair | undefined>; pair(cb?: (data: ISEAPair) => void, opt?: {}): Promise<ISEAPair | undefined>;
/** /**
* Adds a signature to a message, for data that you want to prevent attackers tampering with. * Adds a signature to a message, for data that you want to prevent attackers tampering with.
@ -48,7 +48,7 @@ export interface IGunStaticSEA {
* @param message is what comes from .sign. * @param message is what comes from .sign.
* @param pair from .pair or its public key text (pair.pub). * @param pair from .pair or its public key text (pair.pub).
*/ */
verify(message: any, pair: { pub: string } | string): Promise<unknown>; verify<TMessage = unknown>(message: string, pair: { pub: string } | string): Promise<TMessage | undefined>;
/** /**
* Takes some data that you want to keep secret and encrypts it so nobody else can read it. * Takes some data that you want to keep secret and encrypts it so nobody else can read it.
@ -62,5 +62,23 @@ export interface IGunStaticSEA {
* @param message is what comes from .encrypt. * @param message is what comes from .encrypt.
* @param pair from .pair or the passphrase to decypher the message. * @param pair from .pair or the passphrase to decypher the message.
*/ */
decrypt(message: any, pair: { epriv: string } | string): Promise<unknown>; decrypt<TMessage=unknown>(message: any, pair: { epriv: string } | string): Promise<TMessage|undefined>;
/**
* determine secret between users.
* @param key public key of first user.
* @param pair from .pair or the passphrase to decypher the message
*/
secret(key: string|{epub:string}, pair:{epriv:string, epub:string }, cb?: (arg: string|undefined)=>any, opt?:{ why?:string}) :Promise<string|undefined>
/**
* Certify other users to use your graph.
* @param certificants users for certification.
* @param policy policises for certificants permissions
* @param authority user that gives rights
* @param cb callback
* @param opt options
*/
certify (certificants: string | string[] | { pub: string } | { pub: string }[], policy: ISEAPolicy, authority: { pub: string; priv: string }, cb?: (cert: string) => any | null, opt?:
{ blacklist?: string, expiry?: number }): Promise<string |undefined>
} }

7
types/sea/ISEACertifyOptions.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
export interface ISEACertifyOptions{
blacklist?: string | {
read: string|{'#': string}
write: string|{'#': string}
}
expiry?: number
}

6
types/sea/ISEAPair.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
export declare interface ISEAPair {
pub: string
priv:string
epub:string
epriv:string
}

17
types/sea/ISEAPolicy.d.ts vendored Normal file
View File

@ -0,0 +1,17 @@
interface ISEAPolicyTree{
"+"?:string|ISEAPolicyTree,
"#"?:string|ISEAPolicyTree,
"."?:string|ISEAPolicyTree,
"="?:string|ISEAPolicyTree,
"*"?:string|ISEAPolicyTree,
">"?:string|ISEAPolicyTree,
"<"?:string|ISEAPolicyTree
}
interface ISEAPolicySingle extends ISEAPolicyTree{
read?:string |ISEAPolicyTree,
write?:string|ISEAPolicyTree,
}
export declare type ISEAPolicy = ISEAPolicySingle |string | string[] | ISEAPolicySingle[]

1
types/shared/And.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export type And<A,B> = A & B

28
types/static.d.ts vendored
View File

@ -1,28 +0,0 @@
import { IGunChainReference } from './chain';
import { IGunConstructorOptions } from './options';
import { IGunStaticNode } from './static/node';
import { IGunStaticSEA } from './static/sea';
export interface IGunStatic {
/**
* @description
* no parameters creates a local datastore using the default persistence layer, either localStorage or Radisk.
* @param options
* passing a URL creates the above local datastore that also tries to sync with the URL.
*
* or you can pass in an array of URLs to sync with multiple peers.
*/
<DataType = any>(options?: string | string[] | IGunConstructorOptions): IGunChainReference<DataType, any, 'pre_root'>;
new <DataType = any>(options?: string | string[] | IGunConstructorOptions): IGunChainReference<DataType, any, 'pre_root'>;
readonly node: IGunStaticNode;
/** @see https://gun.eco/docs/SEA */
readonly SEA: IGunStaticSEA;
readonly version: string;
readonly chain: IGunChainReference;
readonly log: {
(...argv: any[]): void;
once(...argv: any[]): void;
off: boolean;
};
}

View File

@ -1,17 +0,0 @@
import { IGunChainReference } from '../chain';
export interface IGunStaticNode {
/** Returns true if data is a gun node, otherwise false. */
is(anything: any): anything is IGunChainReference;
/**
* Returns data's gun ID (instead of manually grabbing its metadata i.e. data["_"]["#"], which is faster but could change in the future)
*
* Returns undefined if data is not correct gun data.
*/
soul(data: IGunChainReference): string;
/** Returns a "gun-ified" variant of the json input by injecting a new gun ID into the metadata field. */
ify(json: any): any;
}

View File

@ -0,0 +1,6 @@
import Gun = require('../../index');
Gun().get('users')
/* now change the context to alice */
.get('alice')
.put({})
.back().map(x=>x)

View File

@ -0,0 +1,14 @@
//import { expectError } from 'tsd';
import Gun = require('../../index');
//Documentation should work
async function get(){
const gun = new Gun();
const alice = await gun.get('user').get('alice')
const gun2 = new Gun<{user:{alice:string}}>();
const alice2 = (await gun2.get('user')).alice
}

View File

@ -0,0 +1,15 @@
import Gun = require('../../index');
Gun()
Gun(['http://server1.com/gun', 'http://server2.com/gun']);
Gun({
s3: {
key: '',
secret: '',
bucket: ''
},
file: 'file/path.json',
uuid() {
return 'xxxxxx';
}
});

View File

@ -0,0 +1,3 @@
import Gun = require('../../index');
Gun().get('users').map(user => user.name === 'Mark'? user : undefined)

View File

@ -0,0 +1,23 @@
import Gun = require('../../index');
const gun = Gun()
var listenerHandler = (value, key, _msg, _ev) => {
}
Gun().on(listenerHandler)
// add listener to foo
gun.get('foo').on(listenerHandler, true)
// remove listener to foo
gun.get('foo').off()
gun.get('users').get('username').on(function(user : any){
// update in real-time
if (user.online) {
} else {
}
})
gun.get('home').get('lights').on(listenerHandler,true);

View File

@ -0,0 +1,17 @@
import Gun = require('../../index');
const gun= Gun()
let view;
gun.get('peer').get('userID').get('profile').once(function(profile){
// render it, but only once. No updates.
view.show.user(profile)
})
gun.get('IoT').get('temperature').once(function(number){
view.show.temp(number)
})
gun.once(function(data, key) {
gun.get('something').put('something')
})

View File

@ -0,0 +1,6 @@
import Gun = require('../../index');
Gun().opt({
uuid: function () {
return Math.floor(Math.random() * 4294967296);
}
})

View File

@ -0,0 +1,12 @@
//import { expectError } from 'tsd';
import Gun = require('../../index');
//Documentation should work
const gun = new Gun();
gun.get('user').put('alice')
const gun2 = new Gun<{user:{alice:string}}>();
gun2.get('user').put({alice:"asd"})

View File

@ -0,0 +1,4 @@
import Gun = require('../../index');
const gun = Gun()
var user = gun.get('alice').put({name: "Alice"})
gun.get('users').set("sa");

View File

@ -0,0 +1,14 @@
import Gun = require('../../index');
const gun = Gun<ExampleState>()
type ExampleState={
a:{
b:{
c:{
d: Record<string,string>
}
}
}
}
gun.get("a").get("b").get("c").get("d").get("anystring").on(x=>x.startsWith("some"))

View File

@ -0,0 +1,13 @@
import Gun = require('../../index');
//Documentation should work
const gun = Gun()
gun.on('auth', data => {
})
gun.user().auth("a","b")
async () => gun.user().auth(await Gun.SEA.pair())

View File

@ -0,0 +1,8 @@
import Gun = require('../../index');
//Documentation should work
const gun = Gun()
gun.user().delete('alias', 'pass', data => data)

View File

@ -0,0 +1,8 @@
import Gun = require('../../index');
//Documentation should work
const gun = Gun()
gun.user().leave()

View File

@ -0,0 +1,3 @@
import Gun = require('../../index');
var gun = Gun();
var user = gun.user().recall({sessionStorage: true});

View File

@ -0,0 +1,4 @@
import Gun = require('../../index');
var gun = Gun();
var user = gun.user().recall({sessionStorage: true});
user.get('mysecrets').secret('string', data => data)

View File

@ -0,0 +1,3 @@
import Gun = require('../../index');
var gun = Gun();
gun.user("publicKey").once(console.log)

View File

@ -0,0 +1,29 @@
import Gun = require('../../index');
/*Documentation example*/
async function certify(){
const SEA = Gun.SEA;
const gun = Gun();
const user = gun.user();
var Alice = await SEA.pair()
var AliceHusband = await SEA.pair()
var Bob = await SEA.pair()
var Dave = await SEA.pair()
// Alice wants to allow Bob and Dave to use write to her "inbox" and "stories" UNTIL TOMORROW
// On Alice's side:
var certificate = await SEA.certify([Bob.pub, Dave.pub], [{"*": "inbox", "+": "*"}, {"*": "stories"}], Alice, null, {expiry: Gun.state()+(60*60*24*1000), blacklist: 'blacklist'})
// Now on Bob/Dave's side, they can write to Alice's graph using gun.put:
gun.get('~'+Alice.pub).get('inbox').get('deeper'+Bob.pub).put('hello world', null, {opt: {cert: certificate}}) // {opt: {cert: certificate}} is how you use Certificate in gun.put
// Now Alice wants to revoke access of Bob. She has TWO OPTIONS. OPTION 1 is to manage the blacklist by herself.
user.get('blacklist').get(Bob.pub).put(true) // OPTION 1: She directly manages her blacklist, in her graph.
// OPTION 2: Alice could point the blacklist to her husband's graph:
user.get('blacklist').put({'#': '~'+AliceHusband.pub+'/blacklist'})
// Now on AliceHusband's side, HE can add Bob to his blacklist:
user.get('blacklist').get(Bob.pub).put(true)
}

28
types/test/sea.test-d.ts Normal file
View File

@ -0,0 +1,28 @@
import Gun = require('../../index');
const SEA = Gun.SEA
;(async () => {
var pair = await SEA.pair();
var enc = await SEA.encrypt('hello self', pair);
var data = await SEA.sign(enc, pair);
console.log(data);
var msg = await SEA.verify(data, pair.pub);
var dec = await SEA.decrypt(msg, pair);
var proof = await SEA.work(dec, pair);
var check = await SEA.work('hello self', pair);
console.log(dec);
console.log(proof === check);
// now let's share private data with someone:
var alice = await SEA.pair();
var bob = await SEA.pair();
var enc = await SEA.encrypt('shared data', await SEA.secret(bob.epub, alice));
await SEA.decrypt(enc, await SEA.secret(alice.epub, bob));
// `.secret` is Elliptic-curve DiffieHellman
// Bob allows Alice to write to part of his graph, he creates a certificate for Alice
var certificate = await SEA.certify(alice.pub, ["^AliceOnly.*"], bob)
// Alice logs in
const gun = Gun();
await gun.user().auth(alice);
// and uses the certificate
await gun.get('~'+bob.pub).get('AliceOnly').get('do-not-tell-anyone').put(enc, null, {opt: {cert: certificate}})
await gun.get('~'+bob.pub).get('AliceOnly').get('do-not-tell-anyone').once(console.log) // return 'enc'
})();

52
types/types.d.ts vendored
View File

@ -1,52 +0,0 @@
import { IGunChainReference } from './chain';
export declare type ArrayOf<T> = T extends Array<infer U> ? U : never;
export declare type DisallowArray<T> = Exclude<T, Array<any>>;
/** These types cannot be stored on Gun (functions and classes) */
export declare type AlwaysDisallowedType<T> = T extends (...args: any[]) => void ? never
: T extends { new(...args: any[]): any; } ? never
: AccessObject<T>;
export declare type AccessObject<T> = T extends object ? {
[key in keyof T]: (AlwaysDisallowedType<T[key]> extends never ? never : AccessObject<T[key]>);
} : T;
/** These types cannot be stored on Gun's root level */
export declare type DisallowPrimitives<Open, T> = Open extends false ? T
: T extends string ? never
: T extends number ? never
: T extends boolean ? never
: T extends null ? never
: T extends undefined ? never
: T;
export declare type Saveable<DataType> = Partial<DataType> | string | number | boolean | null | IGunChainReference<DataType>;
export declare type AckCallback = (ack: {
err: Error;
ok: any;
} | {
err: undefined;
ok: string;
}) => void;
export declare type IGunCryptoKeyPair = Record<'pub' | 'priv' | 'epub' | 'epriv', string>;
export interface IGunRecordNodeRawBase {
'#': string;
}
export interface IGunRecordNodeRawExtra<DataType> extends IGunRecordNodeRawBase {
'>': Record<keyof DataType, number>;
}
export interface IGunRecordNodeRaw<DataType> {
'_': IGunRecordNodeRawExtra<DataType>;
}
export declare type IGunRecordNode<DataType> = {
[K in keyof DataType]: IGunRecordNodeRawBase;
} & IGunRecordNodeRaw<DataType>;
export declare type IGunRecordData<DataType> = DataType & IGunRecordNodeRaw<DataType>;