This commit is contained in:
Mark Nadal 2021-12-16 01:21:04 -08:00
commit 852b77f49a
62 changed files with 1938 additions and 3765 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/

View File

@ -2,7 +2,7 @@
FROM node:14-alpine as builder FROM node:14-alpine as builder
RUN mkdir /work RUN mkdir /work
WORKDIR /work WORKDIR /work
RUN apk add --no-cache alpine-sdk python RUN apk add --no-cache alpine-sdk python3
COPY package*.json ./ COPY package*.json ./
RUN mkdir -p node_modules RUN mkdir -p node_modules
RUN npm ci --only=production RUN npm ci --only=production

View File

@ -121,6 +121,8 @@ Thanks to:
<td vlign="center"><a href="https://mozilla.org/builders"><img height="100" src="https://user-images.githubusercontent.com/1423657/81992335-85346480-9643-11ea-8754-8275e98e06bc.png"></a></td> <td vlign="center"><a href="https://mozilla.org/builders"><img height="100" src="https://user-images.githubusercontent.com/1423657/81992335-85346480-9643-11ea-8754-8275e98e06bc.png"></a></td>
<td vlign="center"><a href="http://unstoppabledomains.com/"><img src="https://gun.eco/img/unstoppable.png"></a></td> <td vlign="center"><a href="http://unstoppabledomains.com/"><img src="https://gun.eco/img/unstoppable.png"></a></td>
<td vlign="center"><a href="https://www.ajar.org/"><img src="https://www.ajar.org/logo.png" width="200"></a></td> <td vlign="center"><a href="https://www.ajar.org/"><img src="https://www.ajar.org/logo.png" width="200"></a></td>
<td vlign="center"><a href="https://ghostdrive.com/"><img src="https://gun.eco/img/ghostdrive.png" width="250"></a></td>
<td vlign="center"><a href="https://mask.io/"><img src="https://dimensiondev.github.io/Mask-VI/assets/Logo/MB--Logo--CombH-Circle--Blue.svg" width="250"></a></td>
</tr></table> </tr></table>
<a href="https://github.com/robertheessels">Robert Heessels</a>, <a href="https://github.com/robertheessels">Robert Heessels</a>,
@ -143,7 +145,9 @@ Thanks to:
<a href="https://github.com/sjuxax">Jeff Cook</a>, <a href="https://github.com/sjuxax">Jeff Cook</a>,
<a href="https://github.com/nmauersberg">Nico</a>, <a href="https://github.com/nmauersberg">Nico</a>,
<a href="https://github.com/ajartille">Aaron Artille</a>, <a href="https://github.com/ajartille">Aaron Artille</a>,
<a href="https://github.com/timjrobinson">Tim Robinson</a> <a href="https://github.com/timjrobinson">Tim Robinson</a>,
<a href="https://github.com/hibas123">Fabian Stamm</a>,
<a href="https://twitter.com/mikestaub">Mike Staub</a>
</p> </p>
- Join others in sponsoring code: https://www.patreon.com/gunDB ! - Join others in sponsoring code: https://www.patreon.com/gunDB !

6
axe.js
View File

@ -42,14 +42,14 @@
tmp = peers[id = 'http://localhost:8765/gun'] = peers[id] || {}; tmp = peers[id = 'http://localhost:8765/gun'] = peers[id] || {};
tmp.id = tmp.url = id; tmp.id = tmp.url = id;
tmp.retry = tmp.retry || 0; // BUG: Check 0? tmp.retry = tmp.retry || 0; // BUG: Check 0?
console.log("AXE enabled: Trying to find network via (1) local peer (2) last used peers (3) hard coded peers."); Gun.log.once("AXE", "AXE enabled: Trying to find network via (1) local peer (2) last used peers (3) hard coded peers.");
console.log("Warning: AXE alpha became super slow & laggy, now in testing only mode!"); Gun.log.once("AXEWarn", "Warning: AXE alpha became super slow & laggy, now in testing only mode!");
var last = JSON.parse((localStorage||'')[(opt.file||'')+'axe/']||null) || {}; var last = JSON.parse((localStorage||'')[(opt.file||'')+'axe/']||null) || {};
Object.keys(last.peers||'').forEach(function(key){ Object.keys(last.peers||'').forEach(function(key){
tmp = peers[id = key] = peers[id] || {}; tmp = peers[id = key] = peers[id] || {};
tmp.id = tmp.url = id; tmp.id = tmp.url = id;
}); });
tmp = peers[id = 'https://ovh.era.eco/gun'] = peers[id] || {}; tmp = peers[id = 'https://guntest.herokuapp.com/gun'] = peers[id] || {};
tmp.id = tmp.url = id; tmp.id = tmp.url = id;
var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); // DAM! var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); // DAM!

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;
} }

6
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;

2
gun.js
View File

@ -1779,7 +1779,7 @@
;(function(){ ;(function(){
var u; var u;
if(''+u == typeof Gun){ return } if(''+u == typeof Gun){ return }
var DEP = function(n){ console.log("Warning! Deprecated internal utility will break in next version:", n) } var DEP = function(n){ console.warn("Warning! Deprecated internal utility will break in next version:", n) }
// Generic javascript utilities. // Generic javascript utilities.
var Type = Gun; var Type = Gun;
//Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }} //Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }}

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;

View File

@ -10,7 +10,7 @@ function start(root){
var opt = root.opt, peers = opt.peers; var opt = root.opt, peers = opt.peers;
if(false === opt.axe){ return } if(false === opt.axe){ return }
if((typeof process !== "undefined") && 'false' === ''+(process.env||'').AXE){ return } if((typeof process !== "undefined") && 'false' === ''+(process.env||'').AXE){ return }
console.log("AXE relay enabled!"); Gun.log.once("AXE relay enabled!");
var axe = root.axe = {}, tmp, id; var axe = root.axe = {}, tmp, id;
var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); // DAM! var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); // DAM!
var dup = root.dup; var dup = root.dup;

View File

@ -15,13 +15,14 @@ try { chokidar = require('chokidar') } catch (error) {
* @param {Object} options - https://gun.eco/docs/hub.js#options * @param {Object} options - https://gun.eco/docs/hub.js#options
*/ */
function watch(what, options) { function watch(what, options) {
options = options ?? { msg: true, hubignore: false } options = options ?? { msg: true, hubignore: false, alias: require('os').userInfo().username }
options.msg = options.msg ?? true; options.msg = options.msg ?? true;
options.hubignore = options.hubignore ?? false; options.hubignore = options.hubignore ?? false;
options.alias = options.alias ?? require('os').userInfo().username
let modifiedPath = (options.file ?? "");
let modifiedPath = options.alias;
let watcher; let watcher;
try { try {
@ -57,9 +58,9 @@ function watch(what, options) {
if (options.msg) log(`File ${path} has been added`); if (options.msg) log(`File ${path} has been added`);
if(path[path.search(/^./gm)] === "/" || ".") { if(path[path.search(/^./gm)] === "/" || ".") {
gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) gun.get('hub').get(modifiedPath + path.split(require('os').userInfo().username)[1]).put(fs.readFileSync(path, 'utf-8'))
} else { } else {
gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) gun.get('hub').get(modifiedPath + '/' + path.split(require('os').userInfo().username)[1]).put(fs.readFileSync(path, 'utf-8'))
} }
} else { } else {
@ -78,7 +79,11 @@ function watch(what, options) {
} else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf('/') + 1))) { } else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf('/') + 1))) {
if (options.msg) log(`File ${path} has been changed`); if (options.msg) log(`File ${path} has been changed`);
gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) if(path[path.search(/^./gm)] === "/" || ".") {
gun.get('hub').get(modifiedPath + path.split(require('os').userInfo().username)[1]).put(fs.readFileSync(path, 'utf-8'))
} else {
gun.get('hub').get(modifiedPath + '/' + path.split(require('os').userInfo().username)[1]).put(fs.readFileSync(path, 'utf-8'))
}
} else { } else {
@ -96,7 +101,12 @@ function watch(what, options) {
} else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf('/') + 1))) { } else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf('/') + 1))) {
if(options.msg) log(`File ${path} has been removed`); if(options.msg) log(`File ${path} has been removed`);
gun.get('hub').get(modifiedPath + '/' + path).put(null) if(path[path.search(/^./gm)] === "/" || ".") {
gun.get('hub').get(modifiedPath + path.split(require('os').userInfo().username)[1]).put(null)
} else {
gun.get('hub').get(modifiedPath + '/' + path.split(require('os').userInfo().username)[1]).put(null)
}
} else { } else {

703
lib/ison.js Normal file
View File

@ -0,0 +1,703 @@
/* **************************************************************************
* A modified version of yieldable-json package that's backwards compatible
* with GunDB's YSON implementation
*
* (c) Copyright IBM Corp. 2017
*
* This program and the accompanying materials are made available
* under the terms of the Apache License v2.0 which accompanies
* this distribution.
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* Contributors:
* Multiple authors (IBM Corp.) - initial implementation and documentation
* **************************************************************************/
;(function () {
var yson = {}, u;
let counter = 0;
let objStack = [];
let temp = '';
const limit = 100000;
function StringifyError(m) {
this.name = 'Error';
this.message = m;
}
/**
* Checking for unicode and backslash characters and replaces if any.
* @param { string }
* @return { string }
*/
let normalize = (string, flagN) => {
let retStr = '';
let transform = '';
let uc =
'/[\\\'\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4' +
'\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g';
let unicode = new RegExp(uc);
// Taking '\\' out of the loop to avoid change in
// order of execution of object entries resulting
// in unwanted side effect
string = string.replace(/\\/gi, '\\\\');
let escape = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"': '\\"',
};
// Escape is implemented globally
for(var pattern in escape) {
var regex = new RegExp(pattern,'gi')
string = string.replace(regex, escape[pattern])
}
unicode.lastIndex = 0;
if (unicode.test(string)) {
// Unicode logic here
transform = string.replace(unicode, (a) => {
return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
});
if (flagN === 1) {
transform += temp;
transform += transform;
temp = '';
return '"' + transform + '"';
} else if (flagN === 2) {
return '"' + transform + '"';
} else {
temp += transform;
}
} else {
if (flagN === 1) {
retStr += temp;
retStr += string;
temp = '';
return '"' + retStr + '"';
} else if (flagN === 2) {
return '"' + string + '"';
} else {
temp += string;
return;
}
}
};
/**
* Obtain stringified value by yielding at required intensity
* @param { string} field
* @param { primitive data type } container
* @param { function or array } replacer
* @param { number or string } space
* @param { number } intensity
* @return { function } yieldCPU
*/
function * stringifyYield(field, container, replacer, space, intensity) {
let itr = 0;
let key = '';
let val = '';
let length = 0;
let tempVal = '';
let result = '';
let value = container[field];
// Made scope local handling async issues
let flag1 = 0;
let returnStr = '';
let subStr = '';
let len = 0;
// Yield the stringification at definite intervals
if (++counter > 512 * intensity) {
counter = 0;
yield val;
}
// Call replacer if one is present (SPEC)
if (typeof replacer === 'function') {
value = replacer.call(container, field, value);
}
switch (typeof value) {
case 'string':
if (value.length > limit) {
for (let l = 0; l < value.length; l += limit) {
flag1 = 0;
yield value;
subStr = value.substr(l, limit);
len += subStr.length;
if (len === value.length)
flag1 = 1;
returnStr = normalize(subStr, flag1);
}
} else
returnStr = normalize(value, 2);
return returnStr;
case 'number':
return isFinite(value)
? String(value)
: 'null';
case 'boolean':
case 'null':
return String(value);
case 'undefined':
return;
case 'function':
return 'null';
case 'object':
if (!value)
return 'null';
// Manage special cases of Arrays and Objects
let getResult = (decision) => {
if (result.length === 0)
if (decision)
return '{}';
else
return '[]';
else
if (decision)
if (space)
return '{\n' + space + result.join(',\n' + space) + '\n' + '}';
else
return '{' + result.join(',') + '}';
else
if (space)
return '[\n' + space + result.join(',\n' + space) + '\n' + ']';
else
return '[' + result.join(',') + ']';
};
result = [];
// If toJSON is present, invoke it (SPEC)
if (value && typeof value.toJSON === 'function') {
const response = value.toJSON(field);
if (response === undefined) {
return undefined;
}
if (typeof response === "number") {
result.push(value.toJSON(field));
} else {
result.push('"' + value.toJSON(field) + '"');
}
if (result.length === 0)
return '{}';
else
if (space)
return space + result.join(',\n' + space) + '\n';
else
return result.join(',');
}
// Array case
if (value && value.constructor === Array) {
length = value.length;
for (itr = 0; itr < length; itr += 1) {
tempVal =
yield *stringifyYield(itr, value, replacer, space, intensity) ||
'null';
if (tempVal !== undefined)
result.push(tempVal);
}
return getResult(false);
}
// Manage replacing object scenario (SPEC)
if (replacer && typeof replacer === 'object') {
length = replacer.length;
for (itr = 0; itr < length; itr += 1) {
if (typeof replacer[itr] === 'string') {
key = replacer[itr];
val = yield *stringifyYield(key, value, replacer, space, intensity);
if (val !== undefined)
result.push(normalize(key, 2) + (space
? ': '
: ':') + val);
}
}
} else {
// Object case
objStack.push(value);
for (key in value) {
if (typeof value[key] === 'object' && value[key] !== null &&
value[key] !== undefined) {
if (objStack.indexOf(value[key]) !== -1) {
return new StringifyError('Circular Structure Detected');
} else
objStack.push(value[key]);
}
if (Object.hasOwnProperty.call(value, key)) {
val = yield *stringifyYield(key, value, replacer, space, intensity);
if (val !== undefined)
result.push(normalize(key, 2) + (space
? ': '
: ':') + val);
}
objStack = objStack.filter((v, i, a) => { return v !== value[key] });
}
objStack = objStack.filter((v, i, a) => { return v !== value });
}
return getResult(true);
default:
return new StringifyError('Unexpected Character');
}
}
/**
* Calling appropriate functions each time.
* @param { primitive data types } value
* @param { function or array } replacer
* @param { number or string } space
* @param { number } intensity
* @param { function } callback
* @return { function } yieldCPU
*/
let stringifyWrapper = (value, replacer, space, intensity, callback) => {
let indent = '';
if (typeof space === 'number') {
indent = ' '.repeat(space);
} else if (typeof space === 'string') {
indent = space;
}
let yielding;
// To hold 'stringifyYield' genarator function
function * yieldBridge() {
yielding = yield *stringifyYield('', {'': value}, replacer, indent, 1);
}
let rs = yieldBridge();
let g = rs.next();
let yieldCPU = () => {
setImmediate(() => {
g = rs.next();
if (g && g.done === true) {
// Reinitializing the values at the end of API call
counter = 0;
temp = ''
objStack = [];
if (typeof yielding === 'object')
return callback(yielding, null);
else
return callback(null, yielding);
}
yieldCPU();
});
};
return yieldCPU();
};
/**
* This method parses a JSON text to produce an object or array.
* It can throw a SyntaxError exception, if the string is malformed.
* @param { string } text
* @param { function or array } reviver
* @param { number } intensity
* @param { function } cb
* @return { function } yieldCPU
*/
let parseWrapper = (text, reviver, intensity, cb) => {
let counter = 0;
let keyN = 0;
let parseStr = text;
let at = 0;
let ch = ' ';
let word = '';
function ParseError(m) {
this.name = 'ParseError';
this.message = m;
this.text = parseStr;
}
// Seek to the next character, after skipping white spaces, if any.
let seek = () => {
ch = parseStr.charAt && parseStr.charAt(at);
at++;
while (ch && ch <= ' ') {
seek();
}
return ch;
};
// Seek to the previous character, required in some special cases.
let unseek = () => {
ch = parseStr.charAt(--at);
};
// Match 'true', 'false' and 'null' built-ins.
let wordCheck = () => {
word = '';
do {
word += ch;
seek();
} while (ch.match(/[a-z]/i));
parseStr = parseStr.slice(at - 1);
at = 0;
return word;
};
// Process strings specially.
let normalizeUnicodedString = () => {
let inQuotes = ' ';
let tempIndex = at;
let index = 0;
let slash = 0;
let c = '"';
while (c) {
index = parseStr.indexOf('"', tempIndex + 1);
tempIndex = index;
ch = parseStr.charAt(tempIndex - 1);
while (ch === '\\') {
slash++;
ch = parseStr.charAt(tempIndex - (slash + 1));
}
if (slash % 2 === 0) {
inQuotes = parseStr.substring(at, index);
parseStr = parseStr.slice(++index);
slash = 0;
break;
} else
slash = 0;
}
// When parsing string values, look for " and \ characters.
index = inQuotes.indexOf('\\');
while (index >= 0) {
let escapee = {
'"': '"',
'\'': '\'',
'/': '/',
'\\': '\\',
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t',
};
let hex = 0;
let i = 0;
let uffff = 0;
at = index;
ch = inQuotes.charAt(++at);
if (ch === 'u') {
uffff = 0;
for (i = 0; i < 4; i += 1) {
hex = parseInt(ch = inQuotes.charAt(++at), 16);
if (!isFinite(hex)) {
break;
}
uffff = uffff * 16 + hex;
}
inQuotes = inQuotes.slice(0, index) +
String.fromCharCode(uffff) + inQuotes.slice(index + 6);
at = index;
} else if (typeof escapee[ch] === 'string') {
inQuotes = inQuotes.slice(0, index) +
escapee[ch] + inQuotes.slice(index + 2);
at = index + 1;
} else
break;
index = inQuotes.indexOf('\\', at);
}
at = 0;
return inQuotes;
};
/**
* This function parses the current string and returns the JavaScript
* Object, through recursive method, and yielding back occasionally
* based on the intensity parameter.
* @return { object } returnObj
*/
function * parseYield() {
let key = '';
let returnObj = {};
let returnArr = [];
let v = '';
let inQuotes = '';
let num = 0;
let numHolder = '';
let addup = () => {
numHolder += ch;
seek();
};
// Handle premitive types. eg: JSON.parse(21)
if (typeof parseStr === 'number' || typeof parseStr === 'boolean' || typeof parseStr === "function" ||
parseStr === null) {
parseStr = '';
return text;
} else if (typeof parseStr === 'undefined') {
parseStr = undefined;
return text;
} else if (parseStr.charAt && parseStr.charAt(0) === '[' && parseStr.charAt(1) === ']') {
parseStr = '';
return [];
} else if (parseStr.charAt && parseStr.charAt(0) === '{' && parseStr.charAt(1) === '}') {
parseStr = '';
return {};
} else {
// Yield the parsing work at specified intervals.
if (++counter > 512 * intensity) {
counter = 0;
yield;
}
// Common case: non-premitive types.
if (keyN !== 1)
seek();
switch (ch) {
case '{':
// Object case
seek();
if (ch === '}') {
parseStr = parseStr.slice(at);
at = 0;
return returnObj;
}
do {
if (ch !== '"')
seek();
keyN = 1;
key = yield *parseYield();
keyN = 0;
seek();
returnObj[key] = yield *parseYield();
seek();
if (ch === '}') {
parseStr = parseStr.slice(at);
at = 0;
return returnObj;
}
} while (ch === ',');
return new ParseError('Bad object');
case '[':
// Array case
seek();
if (ch === ']') {
parseStr = parseStr.slice(at);
at = 0;
return returnArr;
}
unseek();
do {
v = yield *parseYield();
returnArr.push(v);
seek();
if (ch === ']') {
parseStr = parseStr.slice(at);
at = 0;
return returnArr;
}
} while (ch === ',');
return new ParseError('Bad array');
case '"':
parseStr = parseStr.slice(at - 1);
at = 0;
if (parseStr.charAt(0) === '"' && parseStr.charAt(1) === '"') {
parseStr = parseStr.slice(2);
at = 0;
return inQuotes;
} else {
seek();
return normalizeUnicodedString();
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
if (ch === '-') addup();
do {
addup();
if (ch === '.' || ch === 'e' || ch === 'E' ||
ch === '-' || ch === '+' ||
(ch >= String.fromCharCode(65) &&
ch <= String.fromCharCode(70)))
addup();
} while (ch === '-' || ch === '+' || (isFinite(ch) && ch !== ''));
num = Number(numHolder);
parseStr = parseStr.slice(at - 1);
at = 0;
return num;
case 't':
word = wordCheck();
if (word === 'true')
return true;
else return new ParseError('Unexpected character');
case 'f':
word = wordCheck();
if (word === 'false')
return false;
else return new ParseError('Unexpected character');
case 'n':
word = wordCheck();
if (word === 'null')
return null;
else return new ParseError('Unexpected character');
default:
return new ParseError('Unexpected character');
}
}
}
/**
* If there is a reviver function, we recursively walk the new structure,
* passing each name/value pair to the reviver function for possible
* transformation, starting with a temporary root object that holds the result
* in an empty key. If there is not a reviver function, we simply return the
* result.
* @param { object } yieldedObject
* @param { string } key
* @return { function } reviver
*/
let revive = (yieldedObject, key) => {
let k = '';
let v = '';
let val = yieldedObject[key];
if (val && typeof val === 'object') {
for (k in val) {
if (Object.prototype.hasOwnProperty.call(val, k)) {
v = revive(val, k);
if (v !== undefined)
val[k] = v;
else
delete val[k];
}
}
}
return reviver.call(yieldedObject, key, val);
};
let yielding = '';
// To hold 'parseYield' genarator function
function * yieldBridge() {
yielding = yield* parseYield();
}
let rs = yieldBridge();
let gen = rs.next();
// Main yield control logic.
let yieldCPU = () => {
setImmediate(() => {
gen = rs.next();
if (gen && gen.done === true) {
let isEmpty = (value) => {
if (value.charAt(0) === '}' || value.charAt(0) === ']')
value = value.substring(1, value.length);
return typeof value === 'string' && !value.trim();
};
if (typeof yielding === 'undefined')
return cb(new ParseError('Unexpected Character'), null);
else if (yielding instanceof ParseError)
return cb(yielding, null);
else if (!isEmpty(parseStr))
return cb(new ParseError('Unexpected Character'), null);
else {
if (reviver !== null) {
if (typeof reviver === 'function') {
let result = revive({'': yielding}, '');
return cb(null, result);
}
} else
return cb(null, yielding);
}
}
yieldCPU();
});
};
return yieldCPU();
};
/**
* Checks whether the provided space
* @param { string or number } space
* @return { string or number }
*/
let validateSpace = (space) => {
if (typeof space === 'number') {
space = Math.round(space);
if (space >= 1 && space <= 10)
return space;
else if (space < 1)
return 0;
else
return 10;
} else {
if (space.length <= 10)
return space;
else
return space.substr(0, 9);
}
};
/**
* Checks whether the provided intensity
* @param { number } intensity
* @return { number }
*/
let validateIntensity = (intensity) => {
intensity = Math.round(intensity);
if (intensity > 0 && intensity <= 32)
return intensity;
else if (intensity <= 0)
return 1;
else
return 32;
};
yson.parseAsync = function (data, callback, reviver = null, intensity = 1) {
//Bring parity with the in-built parser, that takes both string and buffer
if (Buffer.isBuffer(data))
data = data.toString();
if (!callback)
throw new Error('Missing Callback');
intensity = validateIntensity(intensity);
return parseWrapper(data, reviver, intensity, callback);
};
/**
* Error checking and call of appropriate functions for JSON stringify API
* @param { primitive data types } data
* @param { function or array } replacer
* @param { number or string } space
* @param { number } intensity
* @param { function } callback
* @return { function } stringifyWrapper
*/
yson.stringifyAsync = function(data, callback, replacer = null, space, intensity = 1) {
if (typeof callback !== 'function') {
throw new TypeError('Callback is not a function');
}
if (typeof space === 'number' || typeof space === 'string')
space = validateSpace(space);
if (typeof intensity === 'number')
intensity = validateIntensity(intensity);
return stringifyWrapper(data, replacer, space, intensity, callback);
}
if(typeof window != ''+u){ window.YSON = yson }
try{ if(typeof module != ''+u){ module.exports = yson } }catch(e){}
if(typeof JSON != ''+u){
JSON.parseAsync = yson.parseAsync;
JSON.stringifyAsync = yson.stringifyAsync;
}
}());

5
lib/mobile.js Normal file
View File

@ -0,0 +1,5 @@
import Buffer from "buffer";
import { TextEncoder, TextDecoder } from "text-encoding";
global.Buffer = global.Buffer || Buffer.Buffer;
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

View File

@ -36,7 +36,7 @@ Gun.on('create', function(root){
socket.send(buf, 0, buf.length, udp.port, udp.address, noop); socket.send(buf, 0, buf.length, udp.port, udp.address, noop);
} }
console.log('Multicast on', udp.peer.id); Gun.log.once('Multicast on', udp.peer.id);
return; // below code only needed for when WebSocket connections desired! return; // below code only needed for when WebSocket connections desired!
setInterval(function broadcast(){ setInterval(function broadcast(){
port = port || (opt.web && opt.web.address()||{}).port; port = port || (opt.web && opt.web.address()||{}).port;

View File

@ -37,7 +37,7 @@
//opt.log("WARNING: `store.list` interface might be needed!"); //opt.log("WARNING: `store.list` interface might be needed!");
} }
if(''+u != typeof require){ require('./yson') } if(''+u != typeof require){ require('./ison') }
var parse = JSON.parseAsync || function(t,cb,r){ var u; try{ cb(u, JSON.parse(t,r)) }catch(e){ cb(e) } } var parse = JSON.parseAsync || function(t,cb,r){ var u; try{ cb(u, JSON.parse(t,r)) }catch(e){ cb(e) } }
var json = JSON.stringifyAsync || function(v,cb,r,s){ var u; try{ cb(u, JSON.stringify(v,r,s)) }catch(e){ cb(e) } } var json = JSON.stringifyAsync || function(v,cb,r,s){ var u; try{ cb(u, JSON.stringify(v,r,s)) }catch(e){ cb(e) } }
/* /*

View File

@ -74,7 +74,7 @@
var start = opt.start, end = opt.end, END = '\uffff'; var start = opt.start, end = opt.end, END = '\uffff';
var i = 0, l = keys.length; var i = 0, l = keys.length;
for(;i < l; i++){ var key = keys[i], tree = t[key], tmp, p, pt; for(;i < l; i++){ var key = keys[i], tree = t[key], tmp, p, pt;
if(!tree || '' === key || _ === key){ continue } if(!tree || '' === key || _ === key || 'undefined' === key){ continue }
p = pre.slice(0); p.push(key); p = pre.slice(0); p.push(key);
pt = p.join(''); pt = p.join('');
if(u !== start && pt < (start||'').slice(0,pt.length)){ continue } if(u !== start && pt < (start||'').slice(0,pt.length)){ continue }
@ -118,4 +118,4 @@
}, no = {}, u; }, no = {}, u;
var _ = String.fromCharCode(24); var _ = String.fromCharCode(24);
}()); }());

View File

@ -1,5 +1,5 @@
;(function(){ ;(function(){
require('./yson'); require('./ison');
var Gun = require('../gun'), u; var Gun = require('../gun'), u;
Gun.serve = require('./serve'); Gun.serve = require('./serve');
//process.env.GUN_ENV = process.env.GUN_ENV || 'debug'; //process.env.GUN_ENV = process.env.GUN_ENV || 'debug';

View File

@ -6,11 +6,12 @@ Gun.on('opt', function(root){
if(typeof process === 'undefined'){ return } if(typeof process === 'undefined'){ return }
if(typeof require === 'undefined'){ return } if(typeof require === 'undefined'){ return }
if(false === root.opt.stats){ return } if(false === root.opt.stats){ return }
var file = root.opt.file || 'radata'; var path = require('path') || {};
var file = root.opt.file ? path.resolve(root.opt.file).split(path.sep).slice(-1)[0] : 'radata';
var noop = function(){}; var noop = function(){};
var os = require('os') || {}; var os = require('os') || {};
var fs = require('fs') || {}; var fs = require('fs') || {};
fs.existsSync = fs.existsSync || require('path').existsSync; fs.existsSync = fs.existsSync || path.existsSync;
if(!fs.existsSync){ return } if(!fs.existsSync){ return }
if(!process){ return } if(!process){ return }
process.uptime = process.uptime || noop; process.uptime = process.uptime || noop;
@ -23,7 +24,7 @@ Gun.on('opt', function(root){
var S = +new Date, W; var S = +new Date, W;
var obj_ify = function(o){try{o = JSON.parse(o)}catch(e){o={}};return o;} var obj_ify = function(o){try{o = JSON.parse(o)}catch(e){o={}};return o;}
setTimeout(function(){ setTimeout(function(){
root.stats = obj_ify((fs.existsSync(__dirname+'/../stats.'+(root.opt.file||file)) && fs.readFileSync(__dirname+'/../stats.'+(root.opt.file||file)).toString())) || {}; root.stats = obj_ify((fs.existsSync(__dirname+'/../stats.'+file) && fs.readFileSync(__dirname+'/../stats.'+file).toString())) || {};
root.stats.up = root.stats.up || {}; root.stats.up = root.stats.up || {};
root.stats.up.start = root.stats.up.start || +(new Date); root.stats.up.start = root.stats.up.start || +(new Date);
root.stats.up.count = (root.stats.up.count || 0) + 1; root.stats.up.count = (root.stats.up.count || 0) + 1;
@ -62,16 +63,16 @@ Gun.on('opt', function(root){
root.opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // reset root.opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // reset
} }
JSON.stringifyAsync(stats, function(err, raw){ if(err){ return } W = true; JSON.stringifyAsync(stats, function(err, raw){ if(err){ return } W = true;
fs.writeFile(__dirname+'/../stats.'+(root.opt.file||file), raw, function(err){ W = false; err && console.log(console.STAT.err = err); console.STAT && console.STAT(S, +new Date - S, 'stats stash') }); fs.writeFile(__dirname+'/../stats.'+file, raw, function(err){ W = false; err && console.log(console.STAT.err = err); console.STAT && console.STAT(S, +new Date - S, 'stats stash') });
}); });
//exec("top -b -n 1", function(err, out){ out && fs.writeFile(__dirname+'/../stats.top.'+(root.opt.file||file), out, noop) }); // was it really seriously actually this? //exec("top -b -n 1", function(err, out){ out && fs.writeFile(__dirname+'/../stats.top.'+file, out, noop) }); // was it really seriously actually this?
//}, 1000 * 15); //}, 1000 * 15);
}, 1000 * 5); }, 1000 * 5);
}); });
var exec = require("child_process").exec, noop = function(){}; var exec = require("child_process").exec, noop = function(){};
require('./yson'); require('./ison');
var log = Gun.log, all = {}, max = 1000; var log = Gun.log, all = {}, max = 1000;
Gun.log = console.STAT = function(a,b,c,d){ Gun.log = console.STAT = function(a,b,c,d){

View File

@ -1,10 +1,11 @@
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun'); var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
const rel_ = Gun.val.link._; // '#' const rel_ = '#'; // '#'
const node_ = Gun.node._; // '_' const node_ = '_'; // '_'
Gun.chain.unset = function(node){ Gun.chain.unset = function(node){
if( this && node && node[node_] && node[node_].put && node[node_].put[node_] && node[node_].put[node_][rel_] ) if( this && node && node[node_] && node[node_].put && node[node_].put[node_] && node[node_].put[node_][rel_] ){
this.put( { [node[node_].put[node_][rel_]]:null} ); this.put( { [node[node_].put[node_][rel_]]:null} );
}
return this; return this;
} }

3205
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@ describe('Gun', function(){
root.Gun = root.Gun; root.Gun = root.Gun;
root.Gun.TESTING = true; root.Gun.TESTING = true;
} else { } else {
require('../lib/yson'); require('../lib/ison');
root.Gun = require('../gun'); root.Gun = require('../gun');
root.Gun.TESTING = true; root.Gun.TESTING = true;
require('../lib/store'); require('../lib/store');
@ -82,10 +82,10 @@ describe('Gun', function(){
function Foo(){}; Foo.prototype.toJSON = function(){}; function Foo(){}; Foo.prototype.toJSON = function(){};
//var obj = {"what\"lol": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}}; //var obj = {"what\"lol": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}};
var obj = {"what": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}}; var obj = {"what": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}};
obj = [{x:"test",a:true,b: new Foo,c:3,y:"yes","get":{"#":"chat"},wow:undefined,foo:[1,function(){}, function(){}, 'go'],blah:{a:5,toJSON:function(){ return 9 }}}]; obj = [{x:"test 😎\\😄🔥",a:true,b: new Foo,c:3,y:"yes","get":{"#":"chat"},wow:undefined,foo:[1,function(){}, function(){}, 'go'],blah:{a:5,toJSON:function(){ return 9 }}}];
JSON.stringifyAsync(obj, function(err, text){ JSON.stringifyAsync(obj, function(err, text){
JSON.parseAsync(text, function(err, data){ JSON.parseAsync(text, function(err, data){
expect(data).to.be.eql([{x:"test",a:true,c:3,y:"yes","get":{"#":"chat"},foo:[1,null,null,'go'],blah:9}]); expect(data).to.be.eql([{x:"test 😎\\😄🔥",a:true,c:3,y:"yes","get":{"#":"chat"},foo:[1,null,null,'go'],blah:9}]);
var obj = {a: [], b: [""], c: ["", 1], d: [1, ""], e: {"":[]}, "a\"b": {0: 1}, wow: {'': {cool: 1}}};obj.lol = {0: {sweet: 9}};obj.wat = {"": 'cool'};obj.oh = {phew: {}, "": {}}; var obj = {a: [], b: [""], c: ["", 1], d: [1, ""], e: {"":[]}, "a\"b": {0: 1}, wow: {'': {cool: 1}}};obj.lol = {0: {sweet: 9}};obj.wat = {"": 'cool'};obj.oh = {phew: {}, "": {}};
JSON.stringifyAsync(obj, function(err, text2){ JSON.stringifyAsync(obj, function(err, text2){

View File

@ -1,9 +1,9 @@
// const Gun = require('../..'); const Gun = require('../..');
// const gun = Gun(); const gun = Gun();
// gun.get('hub').on(data => { gun.get('hub').on(data => {
// console.log(data[`${__dirname}/index.html`]) console.log(data)
// }) })
const hub = require('../../lib/hub'); const hub = require('../../lib/hub');
hub.watch(__dirname, {msg: true, hubignore: true}) hub.watch(__dirname, {msg: true, hubignore: true, alias:require('os').userInfo().username})

View File

@ -7,6 +7,6 @@
<title>Document</title> <title>Document</title>
</head> </head>
<body> <body>
<div>Hi!</div>
</body> </body>
</html> </html>

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,39 +1,33 @@
/** 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 /** Undocumented but mentioned. Write data to a JSON. */
* https://github.com/amark/gun/wiki/Modules file: string;
*/
export interface IGunConstructorOptions extends Partial<{ /** Undocumented but mentioned. Create a websocket server */
web: any;
/** Undocumented but mentioned. Write data to a JSON. */
file: string; /** Undocumented but mentioned. Amazon S3 */
s3: {
/** Undocumented but mentioned. Create a websocket server */ key: any;
web: any; secret: any;
bucket: any;
/** Undocumented but mentioned. Amazon S3 */ };
s3: {
key: any; /** the URLs are properties, and the value is an empty object. */
secret: any; peers: string[] | Record<string, {}>;
bucket: any;
}; /** default: true, creates and persists local (nodejs) data using Radisk. */
radisk: boolean;
/** the URLs are properties, and the value is an empty object. */
peers: string[] | Record<string, {}>; /** default: true, persists local (browser) data to localStorage. */
localStorage: boolean;
/** default: true, creates and persists local (nodejs) data using Radisk. */
radisk: boolean; /** uuid allows you to override the default 24 random alphanumeric soul generator with your own function. */
uuid(): string | number;
/** default: true, persists local (browser) data to localStorage. */
localStorage: boolean; /**
* allows you to pass options to a 3rd party module. Their project README will likely list the exposed options
/** uuid allows you to override the default 24 random alphanumeric soul generator with your own function. */ * @see https://github.com/amark/gun/wiki/Modules
uuid(): string; */
[key: string]: any;
/** }> | string | string[];
* 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
*/
[key: string]: any;
}> {
}

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,66 +1,84 @@
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
* @param data The data to be hashed, work to be performed on. * @param data The data to be hashed, work to be performed on.
* @param pair (salt) You can pass pair of keys to use as salt. Salt will prevent others to pre-compute the work, * @param pair (salt) You can pass pair of keys to use as salt. Salt will prevent others to pre-compute the work,
* so using your public key is not a good idea. If it is not specified, it will be random, * so using your public key is not a good idea. If it is not specified, it will be random,
* which ruins your chance of ever being able to re-derive the work deterministically * which ruins your chance of ever being able to re-derive the work deterministically
* @param callback function to executed upon execution of proof * @param callback function to executed upon execution of proof
* @param opt default: {name: 'PBKDF2', encode: 'base64'} * @param opt default: {name: 'PBKDF2', encode: 'base64'}
*/ */
work(data: any, pair?: any, callback?: (data: string | undefined) => void, opt?: Partial<{ work(data: any, pair?: any, callback?: (data: string | undefined) => void, opt?: Partial<{
name: 'SHA-256' | 'PBKDF2'; name: 'SHA-256' | 'PBKDF2';
encode: 'base64' | 'base32' | 'base16'; encode: 'base64' | 'base32' | 'base16';
/** iterations to use on subtle.deriveBits */ /** iterations to use on subtle.deriveBits */
iterations: number; iterations: number;
salt: any; salt: any;
hash: string; hash: string;
length: any; length: any;
}>): Promise<string | undefined>; }>): Promise<string | undefined>;
/** /**
* This generates a cryptographically secure public/private key pair - be careful not to leak the private keys! * This generates a cryptographically secure public/private key pair - be careful not to leak the private keys!
* Note: API subject to change we may change the parameters to accept data and work, in addition to generation. * Note: API subject to change we may change the parameters to accept data and work, in addition to generation.
* 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.
* @param data is the content that you want to prove is authorized. * @param data is the content that you want to prove is authorized.
* @param pair is from .pair. * @param pair is from .pair.
*/ */
sign(data: any, pair: { pub: string; priv: string }): Promise<string | undefined>; sign(data: any, pair: { pub: string; priv: string }): Promise<string | undefined>;
/** /**
* Gets the data if and only if the message can be verified as coming from the person you expect. * Gets the data if and only if the message can be verified as coming from the person you expect.
* @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.
* @param data is the content that you want to encrypt. * @param data is the content that you want to encrypt.
* @param pair from .pair or a passphrase you want to use as a cypher to encrypt with. * @param pair from .pair or a passphrase you want to use as a cypher to encrypt with.
*/ */
encrypt(data: any, pair: { epriv: string } | string): Promise<string>; encrypt(data: any, pair: { epriv: string } | string): Promise<string>;
/** /**
* Read the secret data, if and only if you are allowed to. * Read the secret data, if and only if you are allowed to.
* @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>;