mongodb-livedata-server 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{livedata_server.ts → dist/livedata_server.d.ts} +1 -1
- package/dist/livedata_server.js +3 -1
- package/dist/meteor/binary-heap/max_heap.d.ts +31 -0
- package/dist/meteor/binary-heap/min_heap.d.ts +6 -0
- package/dist/meteor/binary-heap/min_max_heap.d.ts +11 -0
- package/dist/meteor/callback-hook/hook.d.ts +11 -0
- package/dist/meteor/ddp/crossbar.d.ts +15 -0
- package/dist/meteor/ddp/heartbeat.d.ts +19 -0
- package/dist/meteor/ddp/livedata_server.d.ts +141 -0
- package/dist/meteor/ddp/method-invocation.d.ts +25 -0
- package/dist/meteor/ddp/random-stream.d.ts +8 -0
- package/dist/meteor/ddp/session-collection-view.d.ts +27 -0
- package/dist/meteor/ddp/session-document-view.d.ts +8 -0
- package/dist/meteor/ddp/session.d.ts +69 -0
- package/dist/meteor/ddp/stream_server.d.ts +21 -0
- package/dist/meteor/ddp/subscription.d.ts +89 -0
- package/dist/meteor/ddp/utils.d.ts +8 -0
- package/dist/meteor/ddp/writefence.d.ts +20 -0
- package/dist/meteor/diff-sequence/diff.d.ts +13 -0
- package/dist/meteor/ejson/ejson.d.ts +82 -0
- package/dist/meteor/ejson/stringify.d.ts +2 -0
- package/dist/meteor/ejson/utils.d.ts +12 -0
- package/dist/meteor/id-map/id_map.d.ts +16 -0
- package/dist/meteor/mongo/caching_change_observer.d.ts +16 -0
- package/dist/meteor/mongo/doc_fetcher.d.ts +7 -0
- package/dist/meteor/mongo/geojson_utils.d.ts +3 -0
- package/dist/meteor/mongo/live_connection.d.ts +27 -0
- package/dist/meteor/mongo/live_cursor.d.ts +25 -0
- package/dist/meteor/mongo/minimongo_common.d.ts +84 -0
- package/dist/meteor/mongo/minimongo_matcher.d.ts +22 -0
- package/dist/meteor/mongo/minimongo_sorter.d.ts +16 -0
- package/dist/meteor/mongo/observe_driver_utils.d.ts +9 -0
- package/dist/meteor/mongo/observe_multiplexer.d.ts +36 -0
- package/dist/meteor/mongo/oplog-observe-driver.d.ts +67 -0
- package/dist/meteor/mongo/oplog_tailing.d.ts +35 -0
- package/dist/meteor/mongo/oplog_v2_converter.d.ts +1 -0
- package/dist/meteor/mongo/polling_observe_driver.d.ts +30 -0
- package/dist/meteor/mongo/synchronous-cursor.d.ts +17 -0
- package/dist/meteor/mongo/synchronous-queue.d.ts +14 -0
- package/dist/meteor/ordered-dict/ordered_dict.d.ts +31 -0
- package/dist/meteor/random/AbstractRandomGenerator.d.ts +42 -0
- package/dist/meteor/random/AleaRandomGenerator.d.ts +13 -0
- package/dist/meteor/random/NodeRandomGenerator.d.ts +16 -0
- package/dist/meteor/random/createAleaGenerator.d.ts +2 -0
- package/dist/meteor/random/createRandom.d.ts +1 -0
- package/dist/meteor/random/main.d.ts +1 -0
- package/package.json +2 -2
- package/meteor/LICENSE +0 -28
- package/meteor/binary-heap/max_heap.ts +0 -225
- package/meteor/binary-heap/min_heap.ts +0 -15
- package/meteor/binary-heap/min_max_heap.ts +0 -53
- package/meteor/callback-hook/hook.ts +0 -85
- package/meteor/ddp/crossbar.ts +0 -148
- package/meteor/ddp/heartbeat.ts +0 -97
- package/meteor/ddp/livedata_server.ts +0 -474
- package/meteor/ddp/method-invocation.ts +0 -86
- package/meteor/ddp/random-stream.ts +0 -102
- package/meteor/ddp/session-collection-view.ts +0 -119
- package/meteor/ddp/session-document-view.ts +0 -92
- package/meteor/ddp/session.ts +0 -708
- package/meteor/ddp/stream_server.ts +0 -204
- package/meteor/ddp/subscription.ts +0 -392
- package/meteor/ddp/utils.ts +0 -119
- package/meteor/ddp/writefence.ts +0 -130
- package/meteor/diff-sequence/diff.ts +0 -295
- package/meteor/ejson/ejson.ts +0 -601
- package/meteor/ejson/stringify.ts +0 -122
- package/meteor/ejson/utils.ts +0 -38
- package/meteor/id-map/id_map.ts +0 -84
- package/meteor/mongo/caching_change_observer.ts +0 -120
- package/meteor/mongo/doc_fetcher.ts +0 -52
- package/meteor/mongo/geojson_utils.ts +0 -42
- package/meteor/mongo/live_connection.ts +0 -302
- package/meteor/mongo/live_cursor.ts +0 -79
- package/meteor/mongo/minimongo_common.ts +0 -2440
- package/meteor/mongo/minimongo_matcher.ts +0 -275
- package/meteor/mongo/minimongo_sorter.ts +0 -331
- package/meteor/mongo/observe_driver_utils.ts +0 -79
- package/meteor/mongo/observe_multiplexer.ts +0 -256
- package/meteor/mongo/oplog-observe-driver.ts +0 -1049
- package/meteor/mongo/oplog_tailing.ts +0 -414
- package/meteor/mongo/oplog_v2_converter.ts +0 -124
- package/meteor/mongo/polling_observe_driver.ts +0 -247
- package/meteor/mongo/synchronous-cursor.ts +0 -293
- package/meteor/mongo/synchronous-queue.ts +0 -119
- package/meteor/ordered-dict/ordered_dict.ts +0 -229
- package/meteor/random/AbstractRandomGenerator.ts +0 -99
- package/meteor/random/AleaRandomGenerator.ts +0 -96
- package/meteor/random/NodeRandomGenerator.ts +0 -37
- package/meteor/random/createAleaGenerator.ts +0 -31
- package/meteor/random/createRandom.ts +0 -19
- package/meteor/random/main.ts +0 -8
- package/tsconfig.json +0 -10
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
// This file defines an ordered dictionary abstraction that is useful for
|
|
2
|
-
// maintaining a dataset backed by observeChanges. It supports ordering items
|
|
3
|
-
// by specifying the item they now come before.
|
|
4
|
-
|
|
5
|
-
// The implementation is a dictionary that contains nodes of a doubly-linked
|
|
6
|
-
// list as its values.
|
|
7
|
-
|
|
8
|
-
// constructs a new element struct
|
|
9
|
-
// next and prev are whole elements, not keys.
|
|
10
|
-
function element(key, value, next, prev?) {
|
|
11
|
-
return {
|
|
12
|
-
key: key,
|
|
13
|
-
value: value,
|
|
14
|
-
next: next,
|
|
15
|
-
prev: prev
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class OrderedDict {
|
|
20
|
-
public static BREAK = { "break": true };
|
|
21
|
-
|
|
22
|
-
private _dict: Record<string, any>;
|
|
23
|
-
private _first: any;
|
|
24
|
-
private _last: any;
|
|
25
|
-
private _size: number;
|
|
26
|
-
private _stringify: Function;
|
|
27
|
-
|
|
28
|
-
constructor(...args) {
|
|
29
|
-
this._dict = Object.create(null);
|
|
30
|
-
this._first = null;
|
|
31
|
-
this._last = null;
|
|
32
|
-
this._size = 0;
|
|
33
|
-
|
|
34
|
-
if (typeof args[0] === 'function') {
|
|
35
|
-
this._stringify = args.shift();
|
|
36
|
-
} else {
|
|
37
|
-
this._stringify = function (x) { return x; };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
args.forEach(kv => this.putBefore(kv[0], kv[1], null));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// the "prefix keys with a space" thing comes from here
|
|
44
|
-
// https://github.com/documentcloud/underscore/issues/376#issuecomment-2815649
|
|
45
|
-
_k(key: any) {
|
|
46
|
-
return " " + this._stringify(key);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
empty() {
|
|
50
|
-
return !this._first;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
size() {
|
|
54
|
-
return this._size;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
_linkEltIn(elt) {
|
|
58
|
-
if (!elt.next) {
|
|
59
|
-
elt.prev = this._last;
|
|
60
|
-
if (this._last)
|
|
61
|
-
this._last.next = elt;
|
|
62
|
-
this._last = elt;
|
|
63
|
-
} else {
|
|
64
|
-
elt.prev = elt.next.prev;
|
|
65
|
-
elt.next.prev = elt;
|
|
66
|
-
if (elt.prev)
|
|
67
|
-
elt.prev.next = elt;
|
|
68
|
-
}
|
|
69
|
-
if (this._first === null || this._first === elt.next)
|
|
70
|
-
this._first = elt;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
_linkEltOut(elt) {
|
|
74
|
-
if (elt.next)
|
|
75
|
-
elt.next.prev = elt.prev;
|
|
76
|
-
if (elt.prev)
|
|
77
|
-
elt.prev.next = elt.next;
|
|
78
|
-
if (elt === this._last)
|
|
79
|
-
this._last = elt.prev;
|
|
80
|
-
if (elt === this._first)
|
|
81
|
-
this._first = elt.next;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
putBefore(key, item, before) {
|
|
85
|
-
if (this._dict[this._k(key)])
|
|
86
|
-
throw new Error("Item " + key + " already present in OrderedDict");
|
|
87
|
-
var elt = before ?
|
|
88
|
-
element(key, item, this._dict[this._k(before)]) :
|
|
89
|
-
element(key, item, null);
|
|
90
|
-
if (typeof elt.next === "undefined")
|
|
91
|
-
throw new Error("could not find item to put this one before");
|
|
92
|
-
this._linkEltIn(elt);
|
|
93
|
-
this._dict[this._k(key)] = elt;
|
|
94
|
-
this._size++;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
append(key, item) {
|
|
98
|
-
this.putBefore(key, item, null);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
remove(key) {
|
|
102
|
-
var elt = this._dict[this._k(key)];
|
|
103
|
-
if (typeof elt === "undefined")
|
|
104
|
-
throw new Error("Item " + key + " not present in OrderedDict");
|
|
105
|
-
this._linkEltOut(elt);
|
|
106
|
-
this._size--;
|
|
107
|
-
delete this._dict[this._k(key)];
|
|
108
|
-
return elt.value;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
get(key) {
|
|
112
|
-
if (this.has(key)) {
|
|
113
|
-
return this._dict[this._k(key)].value;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
has(key) {
|
|
118
|
-
return Object.prototype.hasOwnProperty.call(
|
|
119
|
-
this._dict,
|
|
120
|
-
this._k(key)
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Iterate through the items in this dictionary in order, calling
|
|
125
|
-
// iter(value, key, index) on each one.
|
|
126
|
-
|
|
127
|
-
// Stops whenever iter returns OrderedDict.BREAK, or after the last element.
|
|
128
|
-
forEach(iter, context = null) {
|
|
129
|
-
var i = 0;
|
|
130
|
-
var elt = this._first;
|
|
131
|
-
while (elt !== null) {
|
|
132
|
-
var b = iter.call(context, elt.value, elt.key, i);
|
|
133
|
-
if (b === OrderedDict.BREAK) return;
|
|
134
|
-
elt = elt.next;
|
|
135
|
-
i++;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
first() {
|
|
140
|
-
if (this.empty()) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
return this._first.key;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
firstValue() {
|
|
147
|
-
if (this.empty()) {
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
return this._first.value;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
last() {
|
|
154
|
-
if (this.empty()) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
return this._last.key;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
lastValue() {
|
|
161
|
-
if (this.empty()) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
return this._last.value;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
prev(key) {
|
|
168
|
-
if (this.has(key)) {
|
|
169
|
-
var elt = this._dict[this._k(key)];
|
|
170
|
-
if (elt.prev)
|
|
171
|
-
return elt.prev.key;
|
|
172
|
-
}
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
next(key) {
|
|
177
|
-
if (this.has(key)) {
|
|
178
|
-
var elt = this._dict[this._k(key)];
|
|
179
|
-
if (elt.next)
|
|
180
|
-
return elt.next.key;
|
|
181
|
-
}
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
moveBefore(key, before) {
|
|
186
|
-
var elt = this._dict[this._k(key)];
|
|
187
|
-
var eltBefore = before ? this._dict[this._k(before)] : null;
|
|
188
|
-
if (typeof elt === "undefined") {
|
|
189
|
-
throw new Error("Item to move is not present");
|
|
190
|
-
}
|
|
191
|
-
if (typeof eltBefore === "undefined") {
|
|
192
|
-
throw new Error("Could not find element to move this one before");
|
|
193
|
-
}
|
|
194
|
-
if (eltBefore === elt.next) // no moving necessary
|
|
195
|
-
return;
|
|
196
|
-
// remove from its old place
|
|
197
|
-
this._linkEltOut(elt);
|
|
198
|
-
// patch into its new place
|
|
199
|
-
elt.next = eltBefore;
|
|
200
|
-
this._linkEltIn(elt);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Linear, sadly.
|
|
204
|
-
indexOf(key) {
|
|
205
|
-
var ret = null;
|
|
206
|
-
this.forEach((v, k, i) => {
|
|
207
|
-
if (this._k(k) === this._k(key)) {
|
|
208
|
-
ret = i;
|
|
209
|
-
return OrderedDict.BREAK;
|
|
210
|
-
}
|
|
211
|
-
return;
|
|
212
|
-
});
|
|
213
|
-
return ret;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
_checkRep() {
|
|
217
|
-
Object.keys(this._dict).forEach(k => {
|
|
218
|
-
const v = this._dict[k];
|
|
219
|
-
if (v.next === v) {
|
|
220
|
-
throw new Error("Next is a loop");
|
|
221
|
-
}
|
|
222
|
-
if (v.prev === v) {
|
|
223
|
-
throw new Error("Prev is a loop");
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
// We use cryptographically strong PRNGs (crypto.getRandomBytes() on the server,
|
|
2
|
-
// window.crypto.getRandomValues() in the browser) when available. If these
|
|
3
|
-
// PRNGs fail, we fall back to the Alea PRNG, which is not cryptographically
|
|
4
|
-
// strong, and we seed it with various sources such as the date, Math.random,
|
|
5
|
-
// and window size on the client. When using crypto.getRandomValues(), our
|
|
6
|
-
// primitive is hexString(), from which we construct fraction(). When using
|
|
7
|
-
// window.crypto.getRandomValues() or alea, the primitive is fraction and we use
|
|
8
|
-
// that to construct hex string.
|
|
9
|
-
|
|
10
|
-
const UNMISTAKABLE_CHARS = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz';
|
|
11
|
-
const BASE64_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +
|
|
12
|
-
'0123456789-_';
|
|
13
|
-
|
|
14
|
-
// `type` is one of `RandomGenerator.Type` as defined below.
|
|
15
|
-
//
|
|
16
|
-
// options:
|
|
17
|
-
// - seeds: (required, only for RandomGenerator.Type.ALEA) an array
|
|
18
|
-
// whose items will be `toString`ed and used as the seed to the Alea
|
|
19
|
-
// algorithm
|
|
20
|
-
export default class RandomGenerator {
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @name Random.fraction
|
|
24
|
-
* @summary Return a number between 0 and 1, like `Math.random`.
|
|
25
|
-
* @locus Anywhere
|
|
26
|
-
*/
|
|
27
|
-
fraction (): number {
|
|
28
|
-
throw new Error(`Unknown random generator type`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @name Random.hexString
|
|
33
|
-
* @summary Return a random string of `n` hexadecimal digits.
|
|
34
|
-
* @locus Anywhere
|
|
35
|
-
* @param {Number} n Length of the string
|
|
36
|
-
*/
|
|
37
|
-
hexString (digits) {
|
|
38
|
-
return this._randomString(digits, '0123456789abcdef');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
_randomString (charsCount, alphabet) {
|
|
42
|
-
let result = '';
|
|
43
|
-
for (let i = 0; i < charsCount; i++) {
|
|
44
|
-
result += this.choice(alphabet);
|
|
45
|
-
}
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @name Random.id
|
|
51
|
-
* @summary Return a unique identifier, such as `"Jjwjg6gouWLXhMGKW"`, that is
|
|
52
|
-
* likely to be unique in the whole world.
|
|
53
|
-
* @locus Anywhere
|
|
54
|
-
* @param {Number} [n] Optional length of the identifier in characters
|
|
55
|
-
* (defaults to 17)
|
|
56
|
-
*/
|
|
57
|
-
id (charsCount) {
|
|
58
|
-
// 17 characters is around 96 bits of entropy, which is the amount of
|
|
59
|
-
// state in the Alea PRNG.
|
|
60
|
-
if (charsCount === undefined) {
|
|
61
|
-
charsCount = 17;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return this._randomString(charsCount, UNMISTAKABLE_CHARS);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @name Random.secret
|
|
69
|
-
* @summary Return a random string of printable characters with 6 bits of
|
|
70
|
-
* entropy per character. Use `Random.secret` for security-critical secrets
|
|
71
|
-
* that are intended for machine, rather than human, consumption.
|
|
72
|
-
* @locus Anywhere
|
|
73
|
-
* @param {Number} [n] Optional length of the secret string (defaults to 43
|
|
74
|
-
* characters, or 256 bits of entropy)
|
|
75
|
-
*/
|
|
76
|
-
secret (charsCount) {
|
|
77
|
-
// Default to 256 bits of entropy, or 43 characters at 6 bits per
|
|
78
|
-
// character.
|
|
79
|
-
if (charsCount === undefined) {
|
|
80
|
-
charsCount = 43;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return this._randomString(charsCount, BASE64_CHARS);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @name Random.choice
|
|
88
|
-
* @summary Return a random element of the given array or string.
|
|
89
|
-
* @locus Anywhere
|
|
90
|
-
* @param {Array|String} arrayOrString Array or string to choose from
|
|
91
|
-
*/
|
|
92
|
-
choice (arrayOrString) {
|
|
93
|
-
const index = Math.floor(this.fraction() * arrayOrString.length);
|
|
94
|
-
if (typeof arrayOrString === 'string') {
|
|
95
|
-
return arrayOrString.substr(index, 1);
|
|
96
|
-
}
|
|
97
|
-
return arrayOrString[index];
|
|
98
|
-
}
|
|
99
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import RandomGenerator from './AbstractRandomGenerator';
|
|
2
|
-
|
|
3
|
-
// Alea PRNG, which is not cryptographically strong
|
|
4
|
-
// see http://baagoe.org/en/wiki/Better_random_numbers_for_javascript
|
|
5
|
-
// for a full discussion and Alea implementation.
|
|
6
|
-
function Alea(seeds) {
|
|
7
|
-
function Mash() {
|
|
8
|
-
let n = 0xefc8249d;
|
|
9
|
-
|
|
10
|
-
const mash = (data) => {
|
|
11
|
-
data = data.toString();
|
|
12
|
-
for (let i = 0; i < data.length; i++) {
|
|
13
|
-
n += data.charCodeAt(i);
|
|
14
|
-
let h = 0.02519603282416938 * n;
|
|
15
|
-
n = h >>> 0;
|
|
16
|
-
h -= n;
|
|
17
|
-
h *= n;
|
|
18
|
-
n = h >>> 0;
|
|
19
|
-
h -= n;
|
|
20
|
-
n += h * 0x100000000; // 2^32
|
|
21
|
-
}
|
|
22
|
-
return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
mash.version = 'Mash 0.9';
|
|
26
|
-
return mash;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let s0 = 0;
|
|
30
|
-
let s1 = 0;
|
|
31
|
-
let s2 = 0;
|
|
32
|
-
let c = 1;
|
|
33
|
-
if (seeds.length === 0) {
|
|
34
|
-
seeds = [+new Date];
|
|
35
|
-
}
|
|
36
|
-
let mash = Mash();
|
|
37
|
-
s0 = mash(' ');
|
|
38
|
-
s1 = mash(' ');
|
|
39
|
-
s2 = mash(' ');
|
|
40
|
-
|
|
41
|
-
for (let i = 0; i < seeds.length; i++) {
|
|
42
|
-
s0 -= mash(seeds[i]);
|
|
43
|
-
if (s0 < 0) {
|
|
44
|
-
s0 += 1;
|
|
45
|
-
}
|
|
46
|
-
s1 -= mash(seeds[i]);
|
|
47
|
-
if (s1 < 0) {
|
|
48
|
-
s1 += 1;
|
|
49
|
-
}
|
|
50
|
-
s2 -= mash(seeds[i]);
|
|
51
|
-
if (s2 < 0) {
|
|
52
|
-
s2 += 1;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
mash = null;
|
|
56
|
-
|
|
57
|
-
const random = () => {
|
|
58
|
-
const t = (2091639 * s0) + (c * 2.3283064365386963e-10); // 2^-32
|
|
59
|
-
s0 = s1;
|
|
60
|
-
s1 = s2;
|
|
61
|
-
return s2 = t - (c = t | 0);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
random.uint32 = () => random() * 0x100000000; // 2^32
|
|
65
|
-
random.fract53 = () => random() +
|
|
66
|
-
((random() * 0x200000 | 0) * 1.1102230246251565e-16); // 2^-53
|
|
67
|
-
|
|
68
|
-
random.version = 'Alea 0.9';
|
|
69
|
-
random.args = seeds;
|
|
70
|
-
return random;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// options:
|
|
74
|
-
// - seeds: an array
|
|
75
|
-
// whose items will be `toString`ed and used as the seed to the Alea
|
|
76
|
-
// algorithm
|
|
77
|
-
export default class AleaRandomGenerator extends RandomGenerator {
|
|
78
|
-
private alea: ReturnType<typeof Alea>;
|
|
79
|
-
|
|
80
|
-
constructor ({ seeds = [] } = {}) {
|
|
81
|
-
super();
|
|
82
|
-
if (!seeds) {
|
|
83
|
-
throw new Error('No seeds were provided for Alea PRNG');
|
|
84
|
-
}
|
|
85
|
-
this.alea = Alea(seeds);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* @name Random.fraction
|
|
90
|
-
* @summary Return a number between 0 and 1, like `Math.random`.
|
|
91
|
-
* @locus Anywhere
|
|
92
|
-
*/
|
|
93
|
-
fraction () {
|
|
94
|
-
return this.alea();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto';
|
|
2
|
-
import RandomGenerator from './AbstractRandomGenerator';
|
|
3
|
-
|
|
4
|
-
export default class NodeRandomGenerator extends RandomGenerator {
|
|
5
|
-
/**
|
|
6
|
-
* @name Random.fraction
|
|
7
|
-
* @summary Return a number between 0 and 1, like `Math.random`.
|
|
8
|
-
* @locus Anywhere
|
|
9
|
-
*/
|
|
10
|
-
fraction () {
|
|
11
|
-
const numerator = Number.parseInt(this.hexString(8), 16);
|
|
12
|
-
return numerator * 2.3283064365386963e-10; // 2^-3;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @name Random.hexString
|
|
17
|
-
* @summary Return a random string of `n` hexadecimal digits.
|
|
18
|
-
* @locus Anywhere
|
|
19
|
-
* @param {Number} n Length of the string
|
|
20
|
-
*/
|
|
21
|
-
hexString (digits) {
|
|
22
|
-
const numBytes = Math.ceil(digits / 2);
|
|
23
|
-
let bytes;
|
|
24
|
-
// Try to get cryptographically strong randomness. Fall back to
|
|
25
|
-
// non-cryptographically strong if not available.
|
|
26
|
-
try {
|
|
27
|
-
bytes = crypto.randomBytes(numBytes);
|
|
28
|
-
} catch (e) {
|
|
29
|
-
// XXX should re-throw any error except insufficient entropy
|
|
30
|
-
bytes = crypto.pseudoRandomBytes(numBytes);
|
|
31
|
-
}
|
|
32
|
-
const result = bytes.toString('hex');
|
|
33
|
-
// If the number of digits is odd, we'll have generated an extra 4 bits
|
|
34
|
-
// of randomness, so we need to trim the last digit.
|
|
35
|
-
return result.substring(0, digits);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import AleaRandomGenerator from './AleaRandomGenerator';
|
|
2
|
-
|
|
3
|
-
// instantiate RNG. Heuristically collect entropy from various sources when a
|
|
4
|
-
// cryptographic PRNG isn't available.
|
|
5
|
-
|
|
6
|
-
// client sources
|
|
7
|
-
const height = (typeof window !== 'undefined' && window.innerHeight) ||
|
|
8
|
-
(typeof document !== 'undefined'
|
|
9
|
-
&& document.documentElement
|
|
10
|
-
&& document.documentElement.clientHeight) ||
|
|
11
|
-
(typeof document !== 'undefined'
|
|
12
|
-
&& document.body
|
|
13
|
-
&& document.body.clientHeight) ||
|
|
14
|
-
1;
|
|
15
|
-
|
|
16
|
-
const width = (typeof window !== 'undefined' && window.innerWidth) ||
|
|
17
|
-
(typeof document !== 'undefined'
|
|
18
|
-
&& document.documentElement
|
|
19
|
-
&& document.documentElement.clientWidth) ||
|
|
20
|
-
(typeof document !== 'undefined'
|
|
21
|
-
&& document.body
|
|
22
|
-
&& document.body.clientWidth) ||
|
|
23
|
-
1;
|
|
24
|
-
|
|
25
|
-
const agent = (typeof navigator !== 'undefined' && navigator.userAgent) || '';
|
|
26
|
-
|
|
27
|
-
export default function createAleaGenerator() {
|
|
28
|
-
return new AleaRandomGenerator({
|
|
29
|
-
seeds: [new Date, height, width, agent, Math.random()],
|
|
30
|
-
});
|
|
31
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import AleaRandomGenerator from './AleaRandomGenerator'
|
|
2
|
-
import createAleaGeneratorWithGeneratedSeed from './createAleaGenerator';
|
|
3
|
-
|
|
4
|
-
export default function createRandom(generator) {
|
|
5
|
-
// Create a non-cryptographically secure PRNG with a given seed (using
|
|
6
|
-
// the Alea algorithm)
|
|
7
|
-
generator.createWithSeeds = (...seeds) => {
|
|
8
|
-
if (seeds.length === 0) {
|
|
9
|
-
throw new Error('No seeds were provided');
|
|
10
|
-
}
|
|
11
|
-
return new AleaRandomGenerator({ seeds });
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
// Used like `Random`, but much faster and not cryptographically
|
|
15
|
-
// secure
|
|
16
|
-
generator.insecure = createAleaGeneratorWithGeneratedSeed();
|
|
17
|
-
|
|
18
|
-
return generator;
|
|
19
|
-
}
|
package/meteor/random/main.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// We use cryptographically strong PRNGs (crypto.getRandomBytes())
|
|
2
|
-
// When using crypto.getRandomValues(), our primitive is hexString(),
|
|
3
|
-
// from which we construct fraction().
|
|
4
|
-
|
|
5
|
-
import NodeRandomGenerator from './NodeRandomGenerator';
|
|
6
|
-
import createRandom from './createRandom';
|
|
7
|
-
|
|
8
|
-
export const Random = createRandom(new NodeRandomGenerator());
|