mongodb-livedata-server 0.1.3 → 0.1.4

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.
Files changed (91) hide show
  1. package/dist/livedata_server.d.ts +4 -4
  2. package/dist/livedata_server.js +11 -11
  3. package/dist/meteor/binary-heap/max_heap.d.ts +31 -31
  4. package/dist/meteor/binary-heap/max_heap.js +186 -186
  5. package/dist/meteor/binary-heap/min_heap.d.ts +6 -6
  6. package/dist/meteor/binary-heap/min_heap.js +17 -17
  7. package/dist/meteor/binary-heap/min_max_heap.d.ts +11 -11
  8. package/dist/meteor/binary-heap/min_max_heap.js +48 -48
  9. package/dist/meteor/callback-hook/hook.d.ts +11 -11
  10. package/dist/meteor/callback-hook/hook.js +78 -78
  11. package/dist/meteor/ddp/crossbar.d.ts +15 -15
  12. package/dist/meteor/ddp/crossbar.js +136 -136
  13. package/dist/meteor/ddp/heartbeat.d.ts +19 -19
  14. package/dist/meteor/ddp/heartbeat.js +77 -77
  15. package/dist/meteor/ddp/livedata_server.d.ts +141 -142
  16. package/dist/meteor/ddp/livedata_server.js +403 -403
  17. package/dist/meteor/ddp/method-invocation.d.ts +35 -35
  18. package/dist/meteor/ddp/method-invocation.js +72 -72
  19. package/dist/meteor/ddp/random-stream.d.ts +8 -8
  20. package/dist/meteor/ddp/random-stream.js +100 -100
  21. package/dist/meteor/ddp/session-collection-view.d.ts +20 -20
  22. package/dist/meteor/ddp/session-collection-view.js +106 -106
  23. package/dist/meteor/ddp/session-document-view.d.ts +8 -8
  24. package/dist/meteor/ddp/session-document-view.js +82 -82
  25. package/dist/meteor/ddp/session.d.ts +75 -75
  26. package/dist/meteor/ddp/session.js +590 -590
  27. package/dist/meteor/ddp/stream_server.d.ts +20 -21
  28. package/dist/meteor/ddp/stream_server.js +181 -181
  29. package/dist/meteor/ddp/subscription.d.ts +94 -94
  30. package/dist/meteor/ddp/subscription.js +370 -370
  31. package/dist/meteor/ddp/utils.d.ts +8 -8
  32. package/dist/meteor/ddp/utils.js +104 -104
  33. package/dist/meteor/ddp/writefence.d.ts +20 -20
  34. package/dist/meteor/ddp/writefence.js +111 -111
  35. package/dist/meteor/diff-sequence/diff.d.ts +17 -17
  36. package/dist/meteor/diff-sequence/diff.js +257 -257
  37. package/dist/meteor/ejson/ejson.d.ts +82 -82
  38. package/dist/meteor/ejson/ejson.js +568 -569
  39. package/dist/meteor/ejson/stringify.d.ts +2 -2
  40. package/dist/meteor/ejson/stringify.js +119 -119
  41. package/dist/meteor/ejson/utils.d.ts +12 -12
  42. package/dist/meteor/ejson/utils.js +42 -42
  43. package/dist/meteor/mongo/caching_change_observer.d.ts +16 -16
  44. package/dist/meteor/mongo/caching_change_observer.js +63 -63
  45. package/dist/meteor/mongo/doc_fetcher.d.ts +7 -7
  46. package/dist/meteor/mongo/doc_fetcher.js +53 -53
  47. package/dist/meteor/mongo/geojson_utils.d.ts +3 -3
  48. package/dist/meteor/mongo/geojson_utils.js +40 -41
  49. package/dist/meteor/mongo/live_connection.d.ts +28 -28
  50. package/dist/meteor/mongo/live_connection.js +264 -264
  51. package/dist/meteor/mongo/live_cursor.d.ts +25 -25
  52. package/dist/meteor/mongo/live_cursor.js +60 -60
  53. package/dist/meteor/mongo/minimongo_common.d.ts +84 -84
  54. package/dist/meteor/mongo/minimongo_common.js +1998 -1998
  55. package/dist/meteor/mongo/minimongo_matcher.d.ts +23 -23
  56. package/dist/meteor/mongo/minimongo_matcher.js +283 -283
  57. package/dist/meteor/mongo/minimongo_sorter.d.ts +16 -16
  58. package/dist/meteor/mongo/minimongo_sorter.js +268 -268
  59. package/dist/meteor/mongo/observe_driver_utils.d.ts +9 -9
  60. package/dist/meteor/mongo/observe_driver_utils.js +72 -73
  61. package/dist/meteor/mongo/observe_multiplexer.d.ts +46 -46
  62. package/dist/meteor/mongo/observe_multiplexer.js +203 -203
  63. package/dist/meteor/mongo/oplog-observe-driver.d.ts +68 -68
  64. package/dist/meteor/mongo/oplog-observe-driver.js +918 -918
  65. package/dist/meteor/mongo/oplog_tailing.d.ts +35 -35
  66. package/dist/meteor/mongo/oplog_tailing.js +352 -352
  67. package/dist/meteor/mongo/oplog_v2_converter.d.ts +1 -1
  68. package/dist/meteor/mongo/oplog_v2_converter.js +125 -126
  69. package/dist/meteor/mongo/polling_observe_driver.d.ts +30 -30
  70. package/dist/meteor/mongo/polling_observe_driver.js +216 -221
  71. package/dist/meteor/mongo/synchronous-cursor.d.ts +17 -17
  72. package/dist/meteor/mongo/synchronous-cursor.js +261 -261
  73. package/dist/meteor/mongo/synchronous-queue.d.ts +13 -13
  74. package/dist/meteor/mongo/synchronous-queue.js +110 -110
  75. package/dist/meteor/ordered-dict/ordered_dict.d.ts +31 -31
  76. package/dist/meteor/ordered-dict/ordered_dict.js +198 -198
  77. package/dist/meteor/random/AbstractRandomGenerator.d.ts +42 -42
  78. package/dist/meteor/random/AbstractRandomGenerator.js +92 -92
  79. package/dist/meteor/random/AleaRandomGenerator.d.ts +13 -13
  80. package/dist/meteor/random/AleaRandomGenerator.js +90 -90
  81. package/dist/meteor/random/NodeRandomGenerator.d.ts +16 -16
  82. package/dist/meteor/random/NodeRandomGenerator.js +42 -42
  83. package/dist/meteor/random/createAleaGenerator.d.ts +2 -2
  84. package/dist/meteor/random/createAleaGenerator.js +32 -32
  85. package/dist/meteor/random/createRandom.d.ts +1 -1
  86. package/dist/meteor/random/createRandom.js +22 -22
  87. package/dist/meteor/random/main.d.ts +1 -1
  88. package/dist/meteor/random/main.js +12 -12
  89. package/dist/meteor/types.d.ts +1 -1
  90. package/dist/meteor/types.js +2 -2
  91. package/package.json +5 -5
@@ -1,198 +1,198 @@
1
- "use strict";
2
- // This file defines an ordered dictionary abstraction that is useful for
3
- // maintaining a dataset backed by observeChanges. It supports ordering items
4
- // by specifying the item they now come before.
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.OrderedDict = void 0;
7
- // The implementation is a dictionary that contains nodes of a doubly-linked
8
- // list as its values.
9
- // constructs a new element struct
10
- // next and prev are whole elements, not keys.
11
- function element(key, value, next, prev) {
12
- return {
13
- key: key,
14
- value: value,
15
- next: next,
16
- prev: prev
17
- };
18
- }
19
- class OrderedDict {
20
- constructor(...args) {
21
- this._dict = Object.create(null);
22
- this._first = null;
23
- this._last = null;
24
- this._size = 0;
25
- if (typeof args[0] === 'function') {
26
- this._stringify = args.shift();
27
- }
28
- else {
29
- this._stringify = function (x) { return x; };
30
- }
31
- args.forEach(kv => this.putBefore(kv[0], kv[1], null));
32
- }
33
- // the "prefix keys with a space" thing comes from here
34
- // https://github.com/documentcloud/underscore/issues/376#issuecomment-2815649
35
- _k(key) {
36
- return " " + this._stringify(key);
37
- }
38
- empty() {
39
- return !this._first;
40
- }
41
- get size() {
42
- return this._size;
43
- }
44
- _linkEltIn(elt) {
45
- if (!elt.next) {
46
- elt.prev = this._last;
47
- if (this._last)
48
- this._last.next = elt;
49
- this._last = elt;
50
- }
51
- else {
52
- elt.prev = elt.next.prev;
53
- elt.next.prev = elt;
54
- if (elt.prev)
55
- elt.prev.next = elt;
56
- }
57
- if (this._first === null || this._first === elt.next)
58
- this._first = elt;
59
- }
60
- _linkEltOut(elt) {
61
- if (elt.next)
62
- elt.next.prev = elt.prev;
63
- if (elt.prev)
64
- elt.prev.next = elt.next;
65
- if (elt === this._last)
66
- this._last = elt.prev;
67
- if (elt === this._first)
68
- this._first = elt.next;
69
- }
70
- putBefore(key, item, before) {
71
- if (this._dict[this._k(key)])
72
- throw new Error("Item " + key + " already present in OrderedDict");
73
- var elt = before ?
74
- element(key, item, this._dict[this._k(before)]) :
75
- element(key, item, null);
76
- if (typeof elt.next === "undefined")
77
- throw new Error("could not find item to put this one before");
78
- this._linkEltIn(elt);
79
- this._dict[this._k(key)] = elt;
80
- this._size++;
81
- }
82
- append(key, item) {
83
- this.putBefore(key, item, null);
84
- }
85
- delete(key) {
86
- var elt = this._dict[this._k(key)];
87
- if (typeof elt === "undefined")
88
- throw new Error("Item " + key + " not present in OrderedDict");
89
- this._linkEltOut(elt);
90
- this._size--;
91
- delete this._dict[this._k(key)];
92
- return elt.value;
93
- }
94
- get(key) {
95
- if (this.has(key)) {
96
- return this._dict[this._k(key)].value;
97
- }
98
- }
99
- has(key) {
100
- return Object.prototype.hasOwnProperty.call(this._dict, this._k(key));
101
- }
102
- // Iterate through the items in this dictionary in order, calling
103
- // iter(value, key, index) on each one.
104
- // Stops whenever iter returns OrderedDict.BREAK, or after the last element.
105
- forEach(iter, context = null) {
106
- var i = 0;
107
- var elt = this._first;
108
- while (elt !== null) {
109
- var b = iter.call(context, elt.value, elt.key, i);
110
- if (b === OrderedDict.BREAK)
111
- return;
112
- elt = elt.next;
113
- i++;
114
- }
115
- }
116
- first() {
117
- if (this.empty()) {
118
- return;
119
- }
120
- return this._first.key;
121
- }
122
- firstValue() {
123
- if (this.empty()) {
124
- return;
125
- }
126
- return this._first.value;
127
- }
128
- last() {
129
- if (this.empty()) {
130
- return;
131
- }
132
- return this._last.key;
133
- }
134
- lastValue() {
135
- if (this.empty()) {
136
- return;
137
- }
138
- return this._last.value;
139
- }
140
- prev(key) {
141
- if (this.has(key)) {
142
- var elt = this._dict[this._k(key)];
143
- if (elt.prev)
144
- return elt.prev.key;
145
- }
146
- return null;
147
- }
148
- next(key) {
149
- if (this.has(key)) {
150
- var elt = this._dict[this._k(key)];
151
- if (elt.next)
152
- return elt.next.key;
153
- }
154
- return null;
155
- }
156
- moveBefore(key, before) {
157
- var elt = this._dict[this._k(key)];
158
- var eltBefore = before ? this._dict[this._k(before)] : null;
159
- if (typeof elt === "undefined") {
160
- throw new Error("Item to move is not present");
161
- }
162
- if (typeof eltBefore === "undefined") {
163
- throw new Error("Could not find element to move this one before");
164
- }
165
- if (eltBefore === elt.next) // no moving necessary
166
- return;
167
- // remove from its old place
168
- this._linkEltOut(elt);
169
- // patch into its new place
170
- elt.next = eltBefore;
171
- this._linkEltIn(elt);
172
- }
173
- // Linear, sadly.
174
- indexOf(key) {
175
- var ret = null;
176
- this.forEach((v, k, i) => {
177
- if (this._k(k) === this._k(key)) {
178
- ret = i;
179
- return OrderedDict.BREAK;
180
- }
181
- return;
182
- });
183
- return ret;
184
- }
185
- _checkRep() {
186
- Object.keys(this._dict).forEach(k => {
187
- const v = this._dict[k];
188
- if (v.next === v) {
189
- throw new Error("Next is a loop");
190
- }
191
- if (v.prev === v) {
192
- throw new Error("Prev is a loop");
193
- }
194
- });
195
- }
196
- }
197
- exports.OrderedDict = OrderedDict;
198
- OrderedDict.BREAK = { "break": true };
1
+ "use strict";
2
+ // This file defines an ordered dictionary abstraction that is useful for
3
+ // maintaining a dataset backed by observeChanges. It supports ordering items
4
+ // by specifying the item they now come before.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.OrderedDict = void 0;
7
+ // The implementation is a dictionary that contains nodes of a doubly-linked
8
+ // list as its values.
9
+ // constructs a new element struct
10
+ // next and prev are whole elements, not keys.
11
+ function element(key, value, next, prev) {
12
+ return {
13
+ key: key,
14
+ value: value,
15
+ next: next,
16
+ prev: prev
17
+ };
18
+ }
19
+ class OrderedDict {
20
+ constructor(...args) {
21
+ this._dict = Object.create(null);
22
+ this._first = null;
23
+ this._last = null;
24
+ this._size = 0;
25
+ if (typeof args[0] === 'function') {
26
+ this._stringify = args.shift();
27
+ }
28
+ else {
29
+ this._stringify = function (x) { return x; };
30
+ }
31
+ args.forEach(kv => this.putBefore(kv[0], kv[1], null));
32
+ }
33
+ // the "prefix keys with a space" thing comes from here
34
+ // https://github.com/documentcloud/underscore/issues/376#issuecomment-2815649
35
+ _k(key) {
36
+ return " " + this._stringify(key);
37
+ }
38
+ empty() {
39
+ return !this._first;
40
+ }
41
+ get size() {
42
+ return this._size;
43
+ }
44
+ _linkEltIn(elt) {
45
+ if (!elt.next) {
46
+ elt.prev = this._last;
47
+ if (this._last)
48
+ this._last.next = elt;
49
+ this._last = elt;
50
+ }
51
+ else {
52
+ elt.prev = elt.next.prev;
53
+ elt.next.prev = elt;
54
+ if (elt.prev)
55
+ elt.prev.next = elt;
56
+ }
57
+ if (this._first === null || this._first === elt.next)
58
+ this._first = elt;
59
+ }
60
+ _linkEltOut(elt) {
61
+ if (elt.next)
62
+ elt.next.prev = elt.prev;
63
+ if (elt.prev)
64
+ elt.prev.next = elt.next;
65
+ if (elt === this._last)
66
+ this._last = elt.prev;
67
+ if (elt === this._first)
68
+ this._first = elt.next;
69
+ }
70
+ putBefore(key, item, before) {
71
+ if (this._dict[this._k(key)])
72
+ throw new Error("Item " + key + " already present in OrderedDict");
73
+ var elt = before ?
74
+ element(key, item, this._dict[this._k(before)]) :
75
+ element(key, item, null);
76
+ if (typeof elt.next === "undefined")
77
+ throw new Error("could not find item to put this one before");
78
+ this._linkEltIn(elt);
79
+ this._dict[this._k(key)] = elt;
80
+ this._size++;
81
+ }
82
+ append(key, item) {
83
+ this.putBefore(key, item, null);
84
+ }
85
+ delete(key) {
86
+ var elt = this._dict[this._k(key)];
87
+ if (typeof elt === "undefined")
88
+ throw new Error("Item " + key + " not present in OrderedDict");
89
+ this._linkEltOut(elt);
90
+ this._size--;
91
+ delete this._dict[this._k(key)];
92
+ return elt.value;
93
+ }
94
+ get(key) {
95
+ if (this.has(key)) {
96
+ return this._dict[this._k(key)].value;
97
+ }
98
+ }
99
+ has(key) {
100
+ return Object.prototype.hasOwnProperty.call(this._dict, this._k(key));
101
+ }
102
+ // Iterate through the items in this dictionary in order, calling
103
+ // iter(value, key, index) on each one.
104
+ // Stops whenever iter returns OrderedDict.BREAK, or after the last element.
105
+ forEach(iter, context = null) {
106
+ var i = 0;
107
+ var elt = this._first;
108
+ while (elt !== null) {
109
+ var b = iter.call(context, elt.value, elt.key, i);
110
+ if (b === OrderedDict.BREAK)
111
+ return;
112
+ elt = elt.next;
113
+ i++;
114
+ }
115
+ }
116
+ first() {
117
+ if (this.empty()) {
118
+ return;
119
+ }
120
+ return this._first.key;
121
+ }
122
+ firstValue() {
123
+ if (this.empty()) {
124
+ return;
125
+ }
126
+ return this._first.value;
127
+ }
128
+ last() {
129
+ if (this.empty()) {
130
+ return;
131
+ }
132
+ return this._last.key;
133
+ }
134
+ lastValue() {
135
+ if (this.empty()) {
136
+ return;
137
+ }
138
+ return this._last.value;
139
+ }
140
+ prev(key) {
141
+ if (this.has(key)) {
142
+ var elt = this._dict[this._k(key)];
143
+ if (elt.prev)
144
+ return elt.prev.key;
145
+ }
146
+ return null;
147
+ }
148
+ next(key) {
149
+ if (this.has(key)) {
150
+ var elt = this._dict[this._k(key)];
151
+ if (elt.next)
152
+ return elt.next.key;
153
+ }
154
+ return null;
155
+ }
156
+ moveBefore(key, before) {
157
+ var elt = this._dict[this._k(key)];
158
+ var eltBefore = before ? this._dict[this._k(before)] : null;
159
+ if (typeof elt === "undefined") {
160
+ throw new Error("Item to move is not present");
161
+ }
162
+ if (typeof eltBefore === "undefined") {
163
+ throw new Error("Could not find element to move this one before");
164
+ }
165
+ if (eltBefore === elt.next) // no moving necessary
166
+ return;
167
+ // remove from its old place
168
+ this._linkEltOut(elt);
169
+ // patch into its new place
170
+ elt.next = eltBefore;
171
+ this._linkEltIn(elt);
172
+ }
173
+ // Linear, sadly.
174
+ indexOf(key) {
175
+ var ret = null;
176
+ this.forEach((v, k, i) => {
177
+ if (this._k(k) === this._k(key)) {
178
+ ret = i;
179
+ return OrderedDict.BREAK;
180
+ }
181
+ return;
182
+ });
183
+ return ret;
184
+ }
185
+ _checkRep() {
186
+ Object.keys(this._dict).forEach(k => {
187
+ const v = this._dict[k];
188
+ if (v.next === v) {
189
+ throw new Error("Next is a loop");
190
+ }
191
+ if (v.prev === v) {
192
+ throw new Error("Prev is a loop");
193
+ }
194
+ });
195
+ }
196
+ }
197
+ exports.OrderedDict = OrderedDict;
198
+ OrderedDict.BREAK = { "break": true };
@@ -1,42 +1,42 @@
1
- export default class RandomGenerator {
2
- /**
3
- * @name Random.fraction
4
- * @summary Return a number between 0 and 1, like `Math.random`.
5
- * @locus Anywhere
6
- */
7
- fraction(): number;
8
- /**
9
- * @name Random.hexString
10
- * @summary Return a random string of `n` hexadecimal digits.
11
- * @locus Anywhere
12
- * @param {Number} n Length of the string
13
- */
14
- hexString(digits: any): string;
15
- _randomString(charsCount: any, alphabet: any): string;
16
- /**
17
- * @name Random.id
18
- * @summary Return a unique identifier, such as `"Jjwjg6gouWLXhMGKW"`, that is
19
- * likely to be unique in the whole world.
20
- * @locus Anywhere
21
- * @param {Number} [n] Optional length of the identifier in characters
22
- * (defaults to 17)
23
- */
24
- id(charsCount: any): string;
25
- /**
26
- * @name Random.secret
27
- * @summary Return a random string of printable characters with 6 bits of
28
- * entropy per character. Use `Random.secret` for security-critical secrets
29
- * that are intended for machine, rather than human, consumption.
30
- * @locus Anywhere
31
- * @param {Number} [n] Optional length of the secret string (defaults to 43
32
- * characters, or 256 bits of entropy)
33
- */
34
- secret(charsCount: any): string;
35
- /**
36
- * @name Random.choice
37
- * @summary Return a random element of the given array or string.
38
- * @locus Anywhere
39
- * @param {Array|String} arrayOrString Array or string to choose from
40
- */
41
- choice(arrayOrString: any): any;
42
- }
1
+ export default class RandomGenerator {
2
+ /**
3
+ * @name Random.fraction
4
+ * @summary Return a number between 0 and 1, like `Math.random`.
5
+ * @locus Anywhere
6
+ */
7
+ fraction(): number;
8
+ /**
9
+ * @name Random.hexString
10
+ * @summary Return a random string of `n` hexadecimal digits.
11
+ * @locus Anywhere
12
+ * @param {Number} n Length of the string
13
+ */
14
+ hexString(digits: any): string;
15
+ _randomString(charsCount: any, alphabet: any): string;
16
+ /**
17
+ * @name Random.id
18
+ * @summary Return a unique identifier, such as `"Jjwjg6gouWLXhMGKW"`, that is
19
+ * likely to be unique in the whole world.
20
+ * @locus Anywhere
21
+ * @param {Number} [n] Optional length of the identifier in characters
22
+ * (defaults to 17)
23
+ */
24
+ id(charsCount: any): string;
25
+ /**
26
+ * @name Random.secret
27
+ * @summary Return a random string of printable characters with 6 bits of
28
+ * entropy per character. Use `Random.secret` for security-critical secrets
29
+ * that are intended for machine, rather than human, consumption.
30
+ * @locus Anywhere
31
+ * @param {Number} [n] Optional length of the secret string (defaults to 43
32
+ * characters, or 256 bits of entropy)
33
+ */
34
+ secret(charsCount: any): string;
35
+ /**
36
+ * @name Random.choice
37
+ * @summary Return a random element of the given array or string.
38
+ * @locus Anywhere
39
+ * @param {Array|String} arrayOrString Array or string to choose from
40
+ */
41
+ choice(arrayOrString: any): any;
42
+ }
@@ -1,92 +1,92 @@
1
- "use strict";
2
- // We use cryptographically strong PRNGs (crypto.getRandomBytes() on the server,
3
- // window.crypto.getRandomValues() in the browser) when available. If these
4
- // PRNGs fail, we fall back to the Alea PRNG, which is not cryptographically
5
- // strong, and we seed it with various sources such as the date, Math.random,
6
- // and window size on the client. When using crypto.getRandomValues(), our
7
- // primitive is hexString(), from which we construct fraction(). When using
8
- // window.crypto.getRandomValues() or alea, the primitive is fraction and we use
9
- // that to construct hex string.
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- const UNMISTAKABLE_CHARS = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz';
12
- const BASE64_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +
13
- '0123456789-_';
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
- class RandomGenerator {
21
- /**
22
- * @name Random.fraction
23
- * @summary Return a number between 0 and 1, like `Math.random`.
24
- * @locus Anywhere
25
- */
26
- fraction() {
27
- throw new Error(`Unknown random generator type`);
28
- }
29
- /**
30
- * @name Random.hexString
31
- * @summary Return a random string of `n` hexadecimal digits.
32
- * @locus Anywhere
33
- * @param {Number} n Length of the string
34
- */
35
- hexString(digits) {
36
- return this._randomString(digits, '0123456789abcdef');
37
- }
38
- _randomString(charsCount, alphabet) {
39
- let result = '';
40
- for (let i = 0; i < charsCount; i++) {
41
- result += this.choice(alphabet);
42
- }
43
- return result;
44
- }
45
- /**
46
- * @name Random.id
47
- * @summary Return a unique identifier, such as `"Jjwjg6gouWLXhMGKW"`, that is
48
- * likely to be unique in the whole world.
49
- * @locus Anywhere
50
- * @param {Number} [n] Optional length of the identifier in characters
51
- * (defaults to 17)
52
- */
53
- id(charsCount) {
54
- // 17 characters is around 96 bits of entropy, which is the amount of
55
- // state in the Alea PRNG.
56
- if (charsCount === undefined) {
57
- charsCount = 17;
58
- }
59
- return this._randomString(charsCount, UNMISTAKABLE_CHARS);
60
- }
61
- /**
62
- * @name Random.secret
63
- * @summary Return a random string of printable characters with 6 bits of
64
- * entropy per character. Use `Random.secret` for security-critical secrets
65
- * that are intended for machine, rather than human, consumption.
66
- * @locus Anywhere
67
- * @param {Number} [n] Optional length of the secret string (defaults to 43
68
- * characters, or 256 bits of entropy)
69
- */
70
- secret(charsCount) {
71
- // Default to 256 bits of entropy, or 43 characters at 6 bits per
72
- // character.
73
- if (charsCount === undefined) {
74
- charsCount = 43;
75
- }
76
- return this._randomString(charsCount, BASE64_CHARS);
77
- }
78
- /**
79
- * @name Random.choice
80
- * @summary Return a random element of the given array or string.
81
- * @locus Anywhere
82
- * @param {Array|String} arrayOrString Array or string to choose from
83
- */
84
- choice(arrayOrString) {
85
- const index = Math.floor(this.fraction() * arrayOrString.length);
86
- if (typeof arrayOrString === 'string') {
87
- return arrayOrString.substr(index, 1);
88
- }
89
- return arrayOrString[index];
90
- }
91
- }
92
- exports.default = RandomGenerator;
1
+ "use strict";
2
+ // We use cryptographically strong PRNGs (crypto.getRandomBytes() on the server,
3
+ // window.crypto.getRandomValues() in the browser) when available. If these
4
+ // PRNGs fail, we fall back to the Alea PRNG, which is not cryptographically
5
+ // strong, and we seed it with various sources such as the date, Math.random,
6
+ // and window size on the client. When using crypto.getRandomValues(), our
7
+ // primitive is hexString(), from which we construct fraction(). When using
8
+ // window.crypto.getRandomValues() or alea, the primitive is fraction and we use
9
+ // that to construct hex string.
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ const UNMISTAKABLE_CHARS = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz';
12
+ const BASE64_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +
13
+ '0123456789-_';
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
+ class RandomGenerator {
21
+ /**
22
+ * @name Random.fraction
23
+ * @summary Return a number between 0 and 1, like `Math.random`.
24
+ * @locus Anywhere
25
+ */
26
+ fraction() {
27
+ throw new Error(`Unknown random generator type`);
28
+ }
29
+ /**
30
+ * @name Random.hexString
31
+ * @summary Return a random string of `n` hexadecimal digits.
32
+ * @locus Anywhere
33
+ * @param {Number} n Length of the string
34
+ */
35
+ hexString(digits) {
36
+ return this._randomString(digits, '0123456789abcdef');
37
+ }
38
+ _randomString(charsCount, alphabet) {
39
+ let result = '';
40
+ for (let i = 0; i < charsCount; i++) {
41
+ result += this.choice(alphabet);
42
+ }
43
+ return result;
44
+ }
45
+ /**
46
+ * @name Random.id
47
+ * @summary Return a unique identifier, such as `"Jjwjg6gouWLXhMGKW"`, that is
48
+ * likely to be unique in the whole world.
49
+ * @locus Anywhere
50
+ * @param {Number} [n] Optional length of the identifier in characters
51
+ * (defaults to 17)
52
+ */
53
+ id(charsCount) {
54
+ // 17 characters is around 96 bits of entropy, which is the amount of
55
+ // state in the Alea PRNG.
56
+ if (charsCount === undefined) {
57
+ charsCount = 17;
58
+ }
59
+ return this._randomString(charsCount, UNMISTAKABLE_CHARS);
60
+ }
61
+ /**
62
+ * @name Random.secret
63
+ * @summary Return a random string of printable characters with 6 bits of
64
+ * entropy per character. Use `Random.secret` for security-critical secrets
65
+ * that are intended for machine, rather than human, consumption.
66
+ * @locus Anywhere
67
+ * @param {Number} [n] Optional length of the secret string (defaults to 43
68
+ * characters, or 256 bits of entropy)
69
+ */
70
+ secret(charsCount) {
71
+ // Default to 256 bits of entropy, or 43 characters at 6 bits per
72
+ // character.
73
+ if (charsCount === undefined) {
74
+ charsCount = 43;
75
+ }
76
+ return this._randomString(charsCount, BASE64_CHARS);
77
+ }
78
+ /**
79
+ * @name Random.choice
80
+ * @summary Return a random element of the given array or string.
81
+ * @locus Anywhere
82
+ * @param {Array|String} arrayOrString Array or string to choose from
83
+ */
84
+ choice(arrayOrString) {
85
+ const index = Math.floor(this.fraction() * arrayOrString.length);
86
+ if (typeof arrayOrString === 'string') {
87
+ return arrayOrString.substr(index, 1);
88
+ }
89
+ return arrayOrString[index];
90
+ }
91
+ }
92
+ exports.default = RandomGenerator;