-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
159 lines (116 loc) · 2.65 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
const hash = require('./lib/hash');
const scope = '\u2316';
let preset = '';
let Gun;
try {
Gun = window.Gun;
} catch (err) {
Gun = require('gun/gun');
}
function find (chain, cb) {
let val;
if (!Gun.is(chain)) {
return undefined;
}
while (Gun.is(chain)) {
if ((val = cb(chain)) !== undefined) {
return val;
}
if (chain === chain.back) {
break;
}
chain = chain.back;
}
return undefined;
}
function findScope (gun) {
let found = find(gun, function (chain) {
return chain._[scope];
});
found = typeof found === 'string' ? found : gun.__[scope];
return found || '';
}
function prefix (gun, name) {
// if name is a string, prefix it with a scope
if (typeof name === 'string') {
// find the prefix
const scope = findScope(gun);
const match = new RegExp(`^${scope}`);
// only prefix once
if (!name.match(match)) {
name = scope + name;
}
} else if (Gun.obj.is(name)) {
if (!name['#']) {
return name;
}
name['#'] = prefix(gun, name['#']);
return name;
}
return name;
}
Gun.scope = function (name) {
if (typeof name === 'string') {
preset = hash(name);
}
return Gun;
};
// each new instance, set a starting scope
Gun.on('opt').event(function (gun) {
gun.__[scope] = gun.__[scope] || preset;
});
// add the `scope` method
Gun.chain.scope = function (name) {
const gun = this.chain();
if (typeof name === 'string') {
/*
Hash the name and keep it as a string.
This is used in `.get`.
*/
gun._[scope] = hash(name);
}
if (name === null) {
gun._[scope] = '';
}
return gun;
};
/*
* wrap the `.key`
* and `.get` methods
* to prefix the keys
*/
Gun.chain.get = (function () {
// keep a reference to the real `get` method
const get = Gun.chain.get;
return function (name, cb, opt) {
// apply a scope
name = prefix(this, name);
// invoke the original `get` method
return get.call(this, name, cb, opt);
};
}());
Gun.chain.key = (function () {
// keep a reference to the original `key` method
const key = Gun.chain.key;
return function (name, cb, opt) {
name = prefix(this, name);
return key.call(this, name, cb, opt);
};
}());
Gun.chain.put = (function () {
const put = Gun.chain.put;
return function () {
Gun.text.random.scope = findScope(this);
return put.apply(this, arguments);
};
}());
Gun.text.random = (function () {
const random = Gun.text.random;
return function (length, chars) {
const scope = Gun.text.random.scope;
return scope + random.call(this, length, chars);
};
}());
Gun.text.random.scope = '';
module.exports = Gun;