forked from atjn/usable-gun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
90 lines (85 loc) · 1.9 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import seaPlugin from './sea.js'
import gunPlugin from '../gun.js'
let __usable_isActivated = false
const __usable_module = {}
/**
*
* @param __usable_environment
* @param __usable_MODULE
*/
export default function (__usable_environment) {
if (__usable_isActivated) return __usable_module.exports
__usable_isActivated = true
/* BEGIN WRAPPED GUN CODE */
const SEA = seaPlugin(__usable_environment)
let Gun
if (SEA.window) {
Gun = SEA.window.GUN || {
chain: {}
}
} else {
Gun = gunPlugin(__usable_environment)
}
SEA.GUN = Gun
/**
*
* @param root
*/
function User (root) {
this._ = {
$: this
}
}
User.prototype = (() => {
/**
*
*/
function F () {}
F.prototype = Gun.chain
return new F()
})() // Object.create polyfill
User.prototype.constructor = User
// let's extend the gun chain with a `user` function.
// only one user can be logged in at a time, per gun instance.
Gun.chain.user = function (pub) {
const gun = this
var root = gun.back(-1)
let user
if (pub) {
pub = SEA.opt.pub((pub._ || '')['#']) || pub
return root.get(`~${pub}`)
}
if ((user = root.back('user'))) {
return user
}
var root = root._
let at = root
const uuid = at.opt.uuid || lex;
(at = (user = at.user = gun.chain(new User()))._).opt = {}
at.opt.uuid = (cb) => {
let id = uuid()
let pub = root.user
if (!pub || !(pub = pub.is) || !(pub = pub.pub)) {
return id
}
id = `~${pub}/${id}`
if (cb && cb.call) {
cb(null, id)
}
return id
}
return user
}
/**
*
*/
function lex () {
return Gun.state().toString(36).replace('.', '')
}
Gun.User = User
User.GUN = Gun
User.SEA = Gun.SEA = SEA
__usable_module.exports = User
__usable_environment.exports.sea.user = __usable_module.exports
return __usable_module.exports
}