-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
561 lines (469 loc) · 13.3 KB
/
helpers.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
const crypto = require("crypto");
/**
*
* @param {Number} durationMilliSeconds
*
* @returns {Promise}
*
* @see https://davidwalsh.name/waitfortime/
*/
const sleepFor = (durationMilliSeconds = 10) => {
return new Promise((resolve) => setTimeout(resolve, durationMilliSeconds));
};
/**
*
* @param {Function} conditionCallback
* @param {Number} pollIntervalMilliSeconds
* @param {Number} timeoutAfterMilliSeconds
*
* @returns {Promise}
*
* @see https://davidwalsh.name/waitfor/
*/
const waitFor = async (
conditionCallback,
pollIntervalMilliSeconds = 50,
timeoutAfterMilliSeconds = 3000
) => {
const startTimeMilliSeconds = Date.now();
const timeoutThresholdMilliseconds =
startTimeMilliSeconds +
(typeof timeoutAfterMilliSeconds === "number"
? timeoutAfterMilliSeconds
: 0);
let timedOut = false;
let error = new Error("Condition not met before timeout");
while (true) {
if (Date.now() >= timeoutThresholdMilliseconds) {
timedOut = true;
break;
}
const result = conditionCallback();
if (result) {
return result;
}
await sleepFor(pollIntervalMilliSeconds);
}
if (timedOut) {
throw error;
}
return {
orThrow(newError) {
error = newError;
}
};
};
/**
*
* @param {Uint8Array} bytes
*
* @return {String}
*
* @see https://stegriff.co.uk/upblog/bit-packing-binary-in-javascript-and-json/
*/
const pack = (bytes = []) => {
const characters = [];
for (let count = 0, bytesLength = bytes.length; count < bytesLength; ) {
characters.push(((bytes[count++] & 0xff) << 8) | (bytes[count++] & 0xff));
}
String.fromCharCode(...characters);
};
/**
*
* @param {String} utf16EncodedString
*
* @return {Array}
*
* @see https://stegriff.co.uk/upblog/bit-packing-binary-in-javascript-and-json/
*/
const unpack = (utf16EncodedString) => {
const bytes = [];
for (
let count = 0, binaryStringLength = utf16EncodedString.length;
count < binaryStringLength;
count++
) {
const character = utf16EncodedString.charCodeAt(count);
bytes.push(character >>> 8, character & 0xff);
}
return bytes;
};
/**
*
* @param {String} text
* @param {String} delimeter
*
* @returns {String}
*/
const camelCaseify = (text) => {
return text.replace(/\W+(.)/g, function(_, chr) {
return chr.toUpperCase();
});
};
/**
*
* @param {*} objectValue
*
* @returns {Boolean}
*/
const isEmpty = (objectValue) => {
if (!objectValue || typeof objectValue !== "object") {
return true;
}
for (const prop in objectValue) {
if (Object.prototype.hasOwnProperty.call(objectValue, prop)) {
return false;
}
}
return JSON.stringify(objectValue) === JSON.stringify({});
};
/**
* @class
* @see https://medium.com/@vincentcorbee/utf-16-to-utf-8-in-javascript-18b4b11b6e1e
*/
class StringReader {
constructor(source) {
this.position = 0;
this.source = source;
}
isInRange(position) {
if (position > -1 && position < this.source.length) return true;
return false;
}
next() {
if (!this.isInRange(this.position)) return null;
return this.source[this.position++].charCodeAt(0);
}
previous() {
if (!this.isInRange(this.position)) return null;
return this.source[this.position--].charCodeAt(0);
}
peak() {
const oldPosition = this.position;
const character = this.next();
this.position = oldPosition;
return character;
}
seek(position) {
if (!this.isInRange(position)) {
throw RangeError(`Offset: ${position} is out of range`);
}
this.position = position;
return this;
}
advance() {
const position = this.position + 1;
if (!this.isInRange(position)) this.position = -1;
else this.position++;
return this;
}
getPosition() {
return this.position;
}
}
/**
* @callback isLeadingSurrogate
* @see https://medium.com/@vincentcorbee/utf-16-to-utf-8-in-javascript-18b4b11b6e1e
*/
const isLeadingSurrogate = (charCode) => {
return charCode >= 0xd800 && charCode <= 0xdbff;
};
/**
* @callback isTrailingSurrogate
* @see https://medium.com/@vincentcorbee/utf-16-to-utf-8-in-javascript-18b4b11b6e1e
*/
const isTrailingSurrogate = (charCode) => {
return charCode >= 0xdc00 && charCode <= 0xdfff;
};
/**
* @class StringEncoder
* @see https://medium.com/@vincentcorbee/utf-16-to-utf-8-in-javascript-18b4b11b6e1e
*/
class StringEncoder {
static UTF16SurrogatePairToCodePoint(leading, trailing) {
return (leading - 0xd800) * 0x400 + (trailing - 0xdc00) + 0x10000;
}
static stringToUtf8(source) {
const utf8codes = [];
const stringReader = new StringReader(source);
let charCode = null;
/* eslint-disable-next-line no-cond-assign */
while ((charCode = stringReader.next()) !== null) {
/* Character takes one byte in UTF-8 (US-ASCII range) */
if (charCode >= 0x0000 && charCode <= 0x007f) {
utf8codes.push(charCode);
/* Character takes two bytes in UTF-8 */
} else if (charCode >= 0x0080 && charCode <= 0x07ff) {
let firstByte = 0xc0 | (charCode >>> 6);
let secondByte = 0x80 | (charCode & 0x3f);
utf8codes.push(firstByte, secondByte);
} else if (isLeadingSurrogate(charCode)) {
/* High surrogate */
const leading = charCode;
/* Low surrogate */
const trailing = stringReader.peak();
if (trailing && isTrailingSurrogate(trailing)) {
/* Surrogate pairs takes four bytes in UTF-8 */
const codePoint = StringEncoder.UTF16SurrogatePairToCodePoint(
leading,
trailing
);
let firstByte = 0xf0 | (codePoint >>> 18);
let secondByte = 0x80 | ((codePoint >>> 12) & 0x3f);
let thirdByte = 0x80 | ((codePoint >>> 6) & 0x3f);
let fourthByte = 0x80 | (codePoint & 0x3f);
utf8codes.push(firstByte, secondByte, thirdByte, fourthByte);
stringReader.advance();
} else {
/* Isolated high surrogate */
}
} else if (isTrailingSurrogate(charCode)) {
/* Low surrogate */
/* Isolated low surrogate */
} else if (charCode >= 0x0800 && charCode <= 0xffff) {
/* Character takes three bytes in UTF-8 */
let firstByte = 0xe0 | (charCode >>> 12);
let secondByte = 0x80 | ((charCode >>> 6) & 0x3f);
let thirdByte = 0x80 | (charCode & 0x3f);
utf8codes.push(firstByte, secondByte, thirdByte);
}
}
return new Uint8Array(utf8codes);
}
}
/**
* @class StringDecoder
* @see https://medium.com/@vincentcorbee/utf-16-to-utf-8-in-javascript-18b4b11b6e1e
*/
class StringDecoder {
static stringFromUTF16CharCode(charCodes) {
return String.fromCharCode(...charCodes);
}
static UTF8ToString(uint8Array) {
const charCodes = [];
for (
let index = 0, length = uint8Array.byteLength;
index < length;
index++
) {
const charCode = uint8Array[index];
/* Character takes one byte */
if (charCode < 0xc0) charCodes.push(charCode);
/* Character takes two bytes */ else if (charCode < 0xe0)
charCodes.push(((charCode & 0x1f) << 6) | (uint8Array[++index] & 0x3f));
/* Character takes three bytes */ else if (charCode < 0xef)
charCodes.push(
((charCode & 0xf) << 12) |
((uint8Array[++index] & 0x3f) << 6) |
(uint8Array[++index] & 0x3f)
);
/* Character takes four bytes */ else {
/* Character consists of high and low surrogate pair */
const codePoint =
(((charCode & 0x7) << 18) |
((uint8Array[++index] & 0x3f) << 12) |
((uint8Array[++index] & 0x3f) << 6) |
(uint8Array[++index] & 0x3f)) -
0x10000;
charCodes.push(
(codePoint >>> 10) + 0xd800,
(codePoint & 0x3ff) + 0xdc00
);
}
}
return StringDecoder.stringFromUTF16CharCode(charCodes);
}
}
/**
*
* @param {Uint8Array} bytesArray
*
* @returns {String}
*/
const convertBytesArrayToBinaryString = (bytesArray) => {
const typedArrayUTF8 = bytesArray;
return StringDecoder.UTF8ToString(typedArrayUTF8);
};
/**
*
* @param {String} rawBinaryString
*
* @returns {Uint8Array}
*/
const convertBinaryStringToBytesArray = (rawBinaryString) => {
const javaScriptStringUTF16 = rawBinaryString;
return StringEncoder.stringToUtf8(javaScriptStringUTF16);
};
const encryptionKey = crypto
.createHash("sha512")
.update("Shcxb573jbci9nbdk")
.digest("hex")
.substring(0, 32);
const encryptionIV = crypto
.createHash("sha512")
.update("92hFAywb579MNmjh86")
.digest("hex")
.substring(0, 16);
/**
*
* @param {String} plainData
* @param {String} sourceEncoding
*
* @returns {String}
*/
const encryptData = (plainData, sourceEncoding = "utf8") => {
const cipherIV = crypto.createCipheriv(
"aes-256-ocb",
encryptionKey,
encryptionIV
);
return Buffer.from(
cipherIV.update(plainData, sourceEncoding, "hex") + cipherIV.final("hex")
).toString("base64");
};
/**
*
* @param {Buffer | String} encryptedData
* @param {String} sourceEncoding
*
* @returns {String}
*/
const decryptData = (encryptedData, sourceEncoding = "utf8") => {
const buffer =
encryptedData instanceof Buffer && encryptedData.isEncoding("base64")
? encryptedData
: Buffer.from(encryptedData, "base64");
const decipherIV = crypto.createDecipheriv(
"aes-256-ocb",
encryptionKey,
encryptionIV
);
return (
decipherIV.update(buffer.toString(sourceEncoding), "hex", sourceEncoding) +
decipherIV.final(sourceEncoding)
);
};
/*
* The MurmurHash3 algorithm was created by Austin Appleby. This JavaScript port was authored
* by whitequark (based on Java port by Yonik Seeley) and is placed into the public domain.
* The author hereby disclaims copyright to this source code.
*
* This produces exactly the same hash values as the final C++ version of MurmurHash3 and
* is thus suitable for producing the same hash values across platforms.
*
* There are two versions of this hash implementation. First interprets the string as a
* sequence of bytes, ignoring most significant byte of each codepoint. The second one
* interprets the string as a UTF-16 codepoint sequence, and appends each 16-bit codepoint
* to the hash independently. The latter mode was not written to be compatible with
* any other implementation, but it should offer better performance for JavaScript-only
* applications.
*
* See http://github.com/whitequark/murmurhash3-js for future updates to this file.
*/
const MurmurHash3 = {
mul32: function (m, n) {
var nlo = n & 0xffff;
var nhi = n - nlo;
return (((nhi * m) | 0) + ((nlo * m) | 0)) | 0;
},
hashBytes: function (data, len, seed) {
var c1 = 0xcc9e2d51;
var c2 = 0x1b873593;
var h1 = seed;
var roundedEnd = len & ~0x3;
for (var i = 0; i < roundedEnd; i += 4) {
var k1 =
(data.charCodeAt(i) & 0xff) |
((data.charCodeAt(i + 1) & 0xff) << 8) |
((data.charCodeAt(i + 2) & 0xff) << 16) |
((data.charCodeAt(i + 3) & 0xff) << 24);
k1 = this.mul32(k1, c1);
k1 = ((k1 & 0x1ffff) << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 = this.mul32(k1, c2);
h1 ^= k1;
h1 = ((h1 & 0x7ffff) << 13) | (h1 >>> 19); // ROTL32(h1,13);
h1 = (h1 * 5 + 0xe6546b64) | 0;
}
k1 = 0;
switch (len % 4) {
case 3:
k1 = (data.charCodeAt(roundedEnd + 2) & 0xff) << 16;
// fallthrough
case 2:
k1 |= (data.charCodeAt(roundedEnd + 1) & 0xff) << 8;
// fallthrough
case 1:
k1 |= data.charCodeAt(roundedEnd) & 0xff;
k1 = this.mul32(k1, c1);
k1 = ((k1 & 0x1ffff) << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 = this.mul32(k1, c2);
h1 ^= k1;
break;
default:
k1 = 0;
}
// finalization
h1 ^= len;
// fmix(h1);
h1 ^= h1 >>> 16;
h1 = this.mul32(h1, 0x85ebca6b);
h1 ^= h1 >>> 13;
h1 = this.mul32(h1, 0xc2b2ae35);
h1 ^= h1 >>> 16;
return h1;
},
hashString: function (data, len, seed) {
var c1 = 0xcc9e2d51;
var c2 = 0x1b873593;
var h1 = seed;
var roundedEnd = len & ~0x1;
for (var i = 0; i < roundedEnd; i += 2) {
var k1 = data.charCodeAt(i) | (data.charCodeAt(i + 1) << 16);
k1 = this.mul32(k1, c1);
k1 = ((k1 & 0x1ffff) << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 = this.mul32(k1, c2);
h1 ^= k1;
h1 = ((h1 & 0x7ffff) << 13) | (h1 >>> 19); // ROTL32(h1,13);
h1 = (h1 * 5 + 0xe6546b64) | 0;
}
if (len % 2 === 1) {
k1 = data.charCodeAt(roundedEnd);
k1 = this.mul32(k1, c1);
k1 = ((k1 & 0x1ffff) << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 = this.mul32(k1, c2);
h1 ^= k1;
}
// finalization
h1 ^= len << 1;
// fmix(h1);
h1 ^= h1 >>> 16;
h1 = this.mul32(h1, 0x85ebca6b);
h1 ^= h1 >>> 13;
h1 = this.mul32(h1, 0xc2b2ae35);
h1 ^= h1 >>> 16;
return h1;
}
};
/**
*
* @param {String} data
*
* @returns {String}
*/
const murmurHash = (data) => {
const hash = MurmurHash3.hashBytes(data, data.length, 150);
return Number(Math.abs(hash) % 262144263494052048758001).toString(16);
};
module.exports = {
encryptData,
decryptData,
camelCaseify,
pack,
unpack,
isEmpty,
waitFor,
sleepFor,
murmurHash,
convertBytesArrayToBinaryString,
convertBinaryStringToBytesArray
};