cryptpad-server 0.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.
@@ -0,0 +1,993 @@
1
+ "use strict";
2
+
3
+ var require$$0 = require("tweetnacl-util");
4
+
5
+ var require$$0$1 = require("tweetnacl/nacl-fast");
6
+
7
+ function getDefaultExportFromCjs(x) {
8
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
9
+ }
10
+
11
+ var worker$1 = {};
12
+
13
+ var commonUtil$1 = {
14
+ exports: {}
15
+ };
16
+
17
+ var commonUtil = commonUtil$1.exports;
18
+
19
+ var hasRequiredCommonUtil;
20
+
21
+ function requireCommonUtil() {
22
+ if (hasRequiredCommonUtil) return commonUtil$1.exports;
23
+ hasRequiredCommonUtil = 1;
24
+ (function(module) {
25
+ (function(window) {
26
+ const factory = NaclUtil => {
27
+ var Util = window.CryptPad_Util = {};
28
+ window.atob = window.atob || function(str) {
29
+ return Buffer.from(str, "base64").toString("binary");
30
+ };
31
+ window.btoa = window.btoa || function(str) {
32
+ return Buffer.from(str, "binary").toString("base64");
33
+ };
34
+ Util.encodeBase64 = NaclUtil.encodeBase64;
35
+ Util.decodeBase64 = str => {
36
+ let i = str.length % 4;
37
+ if (i) {
38
+ str += "=".repeat(4 - i);
39
+ }
40
+ return NaclUtil.decodeBase64(str);
41
+ };
42
+ Util.encodeUTF8 = NaclUtil.encodeUTF8;
43
+ Util.decodeUTF8 = NaclUtil.decodeUTF8;
44
+ Util.slice = function(A, start, end) {
45
+ return Array.prototype.slice.call(A, start, end);
46
+ };
47
+ Util.u8ToBase64 = (u8, cb) => {
48
+ const reader = new FileReader;
49
+ reader.onload = () => {
50
+ let res = reader.result;
51
+ let trim = res.slice(res.indexOf(",") + 1);
52
+ cb(trim);
53
+ };
54
+ reader.readAsDataURL(new Blob([ u8 ]));
55
+ };
56
+ Util.shuffleArray = function(a) {
57
+ for (var i = a.length - 1; i > 0; i--) {
58
+ var j = Math.floor(Math.random() * (i + 1));
59
+ var tmp = a[i];
60
+ a[i] = a[j];
61
+ a[j] = tmp;
62
+ }
63
+ };
64
+ Util.bake = function(f, args) {
65
+ if (typeof args === "undefined") {
66
+ args = [];
67
+ }
68
+ if (!Array.isArray(args)) {
69
+ args = [ args ];
70
+ }
71
+ return function() {
72
+ return f.apply(null, args);
73
+ };
74
+ };
75
+ Util.both = function(pre, post) {
76
+ if (typeof pre !== "function") {
77
+ throw new Error("INVALID_USAGE");
78
+ }
79
+ if (typeof post !== "function") {
80
+ post = function(x) {
81
+ return x;
82
+ };
83
+ }
84
+ return function() {
85
+ pre.apply(null, arguments);
86
+ return post.apply(null, arguments);
87
+ };
88
+ };
89
+ Util.clone = function(o) {
90
+ if (o === undefined || o === null) {
91
+ return o;
92
+ }
93
+ return JSON.parse(JSON.stringify(o));
94
+ };
95
+ Util.serializeError = function(err) {
96
+ if (!(err instanceof Error)) {
97
+ return err;
98
+ }
99
+ var ser = {};
100
+ Object.getOwnPropertyNames(err).forEach(function(key) {
101
+ ser[key] = err[key];
102
+ });
103
+ return ser;
104
+ };
105
+ Util.tryParse = function(s) {
106
+ try {
107
+ return JSON.parse(s);
108
+ } catch (e) {
109
+ return;
110
+ }
111
+ };
112
+ Util.mkAsync = function(f, ms) {
113
+ if (typeof f !== "function") {
114
+ throw new Error("EXPECTED_FUNCTION");
115
+ }
116
+ return function() {
117
+ var args = Array.prototype.slice.call(arguments);
118
+ setTimeout(function() {
119
+ f.apply(null, args);
120
+ }, ms);
121
+ };
122
+ };
123
+ Util.mkEvent = function(once) {
124
+ var handlers = [];
125
+ var fired = false;
126
+ let promiseResolve;
127
+ const promise = new Promise(resolve => {
128
+ promiseResolve = resolve;
129
+ });
130
+ return {
131
+ reg: function(cb) {
132
+ if (once && fired) {
133
+ return void setTimeout(cb);
134
+ }
135
+ handlers.push(cb);
136
+ },
137
+ unreg: function(cb) {
138
+ if (handlers.indexOf(cb) === -1) {
139
+ return void console.log("event handler was already unregistered");
140
+ }
141
+ handlers.splice(handlers.indexOf(cb), 1);
142
+ },
143
+ fire: function() {
144
+ if (once && fired) {
145
+ return;
146
+ }
147
+ var args = Array.prototype.slice.call(arguments);
148
+ if (!fired) {
149
+ promiseResolve.apply(null, args);
150
+ }
151
+ fired = true;
152
+ handlers.forEach(function(h) {
153
+ h.apply(null, args);
154
+ });
155
+ },
156
+ promise
157
+ };
158
+ };
159
+ Util.mkTimeout = function(_f, ms) {
160
+ ms = ms || 0;
161
+ var f = Util.once(_f);
162
+ var timeout = setTimeout(function() {
163
+ f("TIMEOUT");
164
+ }, ms);
165
+ return Util.both(f, function() {
166
+ clearTimeout(timeout);
167
+ });
168
+ };
169
+ Util.onClickEnter = function($element, handler, cfg) {
170
+ $element.on("click keydown", function(e) {
171
+ var isClick = e.type === "click";
172
+ var isEnter = e.type === "keydown" && e.which === 13;
173
+ var isSpace = e.type === "keydown" && e.which === 32 && cfg && cfg.space;
174
+ if (!isClick && !isEnter && !isSpace) {
175
+ return;
176
+ }
177
+ if (e.type === "keydown") {
178
+ e.preventDefault();
179
+ }
180
+ handler(e);
181
+ });
182
+ };
183
+ Util.response = function(errorHandler) {
184
+ var pending = {};
185
+ var timeouts = {};
186
+ if (typeof errorHandler !== "function") {
187
+ errorHandler = function(label) {
188
+ throw new Error(label);
189
+ };
190
+ }
191
+ var clear = function(id) {
192
+ clearTimeout(timeouts[id]);
193
+ delete timeouts[id];
194
+ delete pending[id];
195
+ };
196
+ var expect = function(id, fn, ms) {
197
+ if (typeof id !== "string") {
198
+ errorHandler("EXPECTED_STRING");
199
+ }
200
+ if (typeof fn !== "function") {
201
+ errorHandler("EXPECTED_CALLBACK");
202
+ }
203
+ pending[id] = fn;
204
+ if (typeof ms === "number" && ms) {
205
+ timeouts[id] = setTimeout(function() {
206
+ if (typeof pending[id] === "function") {
207
+ pending[id]("TIMEOUT");
208
+ }
209
+ clear(id);
210
+ }, ms);
211
+ }
212
+ };
213
+ var handle = function(id, args) {
214
+ var fn = pending[id];
215
+ if (typeof fn !== "function") {
216
+ return void errorHandler("MISSING_CALLBACK", {
217
+ id,
218
+ args
219
+ });
220
+ }
221
+ try {
222
+ fn.apply(null, Array.isArray(args) ? args : [ args ]);
223
+ } catch (err) {
224
+ console.trace(err);
225
+ errorHandler("HANDLER_ERROR", {
226
+ error: err,
227
+ id,
228
+ args
229
+ });
230
+ }
231
+ clear(id);
232
+ };
233
+ return {
234
+ clear,
235
+ expected: function(id) {
236
+ return Boolean(pending[id]);
237
+ },
238
+ expectation: function(id) {
239
+ return pending[id];
240
+ },
241
+ expect,
242
+ handle,
243
+ _pending: pending
244
+ };
245
+ };
246
+ Util.inc = function(map, key, val) {
247
+ map[key] = (map[key] || 0) + (typeof val === "number" ? val : 1);
248
+ };
249
+ Util.values = function(obj) {
250
+ return Object.keys(obj).map(function(k) {
251
+ return obj[k];
252
+ });
253
+ };
254
+ Util.find = function(map, path) {
255
+ var l = path.length;
256
+ for (var i = 0; i < l; i++) {
257
+ if (typeof map[path[i]] === "undefined") {
258
+ return;
259
+ }
260
+ map = map[path[i]];
261
+ }
262
+ return map;
263
+ };
264
+ Util.uid = function() {
265
+ return Number(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString(32).replace(/\./g, "");
266
+ };
267
+ Util.guid = function(map) {
268
+ var id = Util.uid();
269
+ if (typeof map[id] === "undefined") {
270
+ return id;
271
+ }
272
+ return Util.guid(map);
273
+ };
274
+ Util.fixHTML = function(str) {
275
+ if (!str) {
276
+ return "";
277
+ }
278
+ return str.replace(/[<>&"']/g, function(x) {
279
+ return {
280
+ "<": "&lt;",
281
+ ">": "&gt",
282
+ "&": "&amp;",
283
+ '"': "&#34;",
284
+ "'": "&#39;"
285
+ }[x];
286
+ });
287
+ };
288
+ Util.hexToBase64 = function(hex) {
289
+ var hexArray = hex.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ");
290
+ var byteString = String.fromCharCode.apply(null, hexArray);
291
+ return window.btoa(byteString).replace(/\//g, "-").replace(/=+$/, "");
292
+ };
293
+ Util.base64ToHex = function(b64String) {
294
+ var hexArray = [];
295
+ window.atob(b64String.replace(/-/g, "/")).split("").forEach(function(e) {
296
+ var h = e.charCodeAt(0).toString(16);
297
+ if (h.length === 1) {
298
+ h = "0" + h;
299
+ }
300
+ hexArray.push(h);
301
+ });
302
+ return hexArray.join("");
303
+ };
304
+ Util.uint8ArrayToHex = function(bytes) {
305
+ var hexString = "";
306
+ for (var i = 0; i < bytes.length; i++) {
307
+ if (bytes[i] < 16) {
308
+ hexString += "0";
309
+ }
310
+ hexString += bytes[i].toString(16);
311
+ }
312
+ return hexString;
313
+ };
314
+ Util.hexToUint8Array = function(hexString) {
315
+ var bytes = new Uint8Array(Math.ceil(hexString.length / 2));
316
+ for (var i = 0; i < bytes.length; i++) {
317
+ bytes[i] = parseInt(hexString.substr(i * 2, 2), 16);
318
+ }
319
+ return bytes;
320
+ };
321
+ Util.uint8ArrayJoin = function(AA) {
322
+ var l = 0;
323
+ var i = 0;
324
+ for (;i < AA.length; i++) {
325
+ l += AA[i].length;
326
+ }
327
+ var C = new Uint8Array(l);
328
+ i = 0;
329
+ for (var offset = 0; i < AA.length; i++) {
330
+ C.set(AA[i], offset);
331
+ offset += AA[i].length;
332
+ }
333
+ return C;
334
+ };
335
+ Util.escapeKeyCharacters = function(key) {
336
+ return key && key.replace && key.replace(/\//g, "-");
337
+ };
338
+ Util.unescapeKeyCharacters = function(key) {
339
+ return key.replace(/\-/g, "/");
340
+ };
341
+ Util.deduplicateString = function(array) {
342
+ return Array.from(new Set(array));
343
+ };
344
+ Util.fixFileName = function(filename) {
345
+ return filename.replace(/ /g, "-").replace(/[\/\?]/g, "_").replace(/_+/g, "_");
346
+ };
347
+ var oneKilobyte = 1024;
348
+ var oneMegabyte = 1024 * oneKilobyte;
349
+ var oneGigabyte = 1024 * oneMegabyte;
350
+ Util.bytesToGigabytes = function(bytes) {
351
+ return Math.ceil(bytes / oneGigabyte * 100) / 100;
352
+ };
353
+ Util.bytesToMegabytes = function(bytes) {
354
+ return Math.ceil(bytes / oneMegabyte * 100) / 100;
355
+ };
356
+ Util.bytesToKilobytes = function(bytes) {
357
+ return Math.ceil(bytes / oneKilobyte * 100) / 100;
358
+ };
359
+ Util.magnitudeOfBytes = function(bytes) {
360
+ if (bytes >= oneGigabyte) {
361
+ return "GB";
362
+ } else if (bytes >= oneMegabyte) {
363
+ return "MB";
364
+ } else {
365
+ return "KB";
366
+ }
367
+ };
368
+ var getCacheKey = function(src) {
369
+ var _src = src.replace(/(\/)*$/, "");
370
+ var idx = _src.lastIndexOf("/");
371
+ var cacheKey = _src.slice(idx + 1);
372
+ if (!/^[a-f0-9]{48}$/.test(cacheKey)) {
373
+ cacheKey = undefined;
374
+ }
375
+ return cacheKey;
376
+ };
377
+ Util.getBlock = function(src, opt, cb) {
378
+ var CB = Util.once(Util.mkAsync(cb));
379
+ var headers = {};
380
+ if (typeof opt.bearer === "string" && opt.bearer) {
381
+ headers.authorization = `Bearer ${opt.bearer}`;
382
+ }
383
+ fetch(src, {
384
+ method: "GET",
385
+ credentials: "include",
386
+ headers
387
+ }).then(response => {
388
+ if (response.ok) {
389
+ return void CB(void 0, response);
390
+ }
391
+ if (response.status === 401 || response.status === 404) {
392
+ response.json().then(data => {
393
+ CB(response.status, data);
394
+ }).catch(() => {
395
+ CB(response.status);
396
+ });
397
+ return;
398
+ }
399
+ CB(response.status, response);
400
+ }).catch(error => {
401
+ CB(error);
402
+ });
403
+ };
404
+ Util.fetchApi = function(origin, type, ignoreCache, cb) {
405
+ const url = new URL(origin);
406
+ url.pathname = `api/${type}`;
407
+ let href = url.href + (ignoreCache ? "?" + +new Date : "");
408
+ if (typeof self !== "undefined" && self.crypto) {
409
+ fetch(href).then(res => {
410
+ if (!res.ok) {
411
+ throw new Error(`Fetch error: ${res.status}`);
412
+ }
413
+ return res.text();
414
+ }).then(body => {
415
+ cb(JSON.parse(body.slice(27, -5)));
416
+ }).catch(err => {
417
+ console.error(err.message);
418
+ cb({});
419
+ });
420
+ } else if (typeof require !== "undefined") {
421
+ const H = url.protocol === "http:" ? require("node:http") : require("node:https");
422
+ H.get(url.href, res => {
423
+ let body = "";
424
+ res.on("data", data => {
425
+ body += data;
426
+ });
427
+ res.on("end", () => {
428
+ try {
429
+ cb(JSON.parse(body.slice(27, -5)));
430
+ } catch (e) {
431
+ console.error(e);
432
+ cb({});
433
+ }
434
+ });
435
+ });
436
+ }
437
+ };
438
+ Util.fetch = function(src, cb, progress, cache) {
439
+ var CB = Util.once(Util.mkAsync(cb));
440
+ var cacheKey = getCacheKey(src);
441
+ var getBlobCache = function(id, cb) {
442
+ if (!cache || typeof cache.getBlobCache !== "function") {
443
+ return void cb("EINVAL");
444
+ }
445
+ cache.getBlobCache(id, cb);
446
+ };
447
+ var setBlobCache = function(id, u8, cb) {
448
+ if (!cache || typeof cache.setBlobCache !== "function") {
449
+ return void cb("EINVAL");
450
+ }
451
+ cache.setBlobCache(id, u8, cb);
452
+ };
453
+ var xhr;
454
+ var fetch = function() {
455
+ xhr = new XMLHttpRequest;
456
+ xhr.open("GET", src, true);
457
+ if (progress) {
458
+ xhr.addEventListener("progress", function(evt) {
459
+ if (evt.lengthComputable) {
460
+ var percentComplete = evt.loaded / evt.total;
461
+ progress(percentComplete);
462
+ }
463
+ }, false);
464
+ }
465
+ xhr.responseType = "arraybuffer";
466
+ xhr.onerror = function(err) {
467
+ CB(err);
468
+ };
469
+ xhr.onload = function() {
470
+ if (/^4/.test("" + this.status)) {
471
+ return CB("XHR_ERROR");
472
+ }
473
+ var arrayBuffer = xhr.response;
474
+ if (arrayBuffer) {
475
+ var u8 = new Uint8Array(arrayBuffer);
476
+ if (cacheKey) {
477
+ return void setBlobCache(cacheKey, u8, function() {
478
+ CB(null, u8);
479
+ });
480
+ }
481
+ return void CB(void 0, u8);
482
+ }
483
+ CB("ENOENT");
484
+ };
485
+ xhr.send(null);
486
+ };
487
+ if (!cacheKey) {
488
+ return void fetch();
489
+ }
490
+ getBlobCache(cacheKey, function(err, u8) {
491
+ if (err || !u8) {
492
+ return void fetch();
493
+ }
494
+ CB(void 0, u8);
495
+ });
496
+ return {
497
+ cancel: function() {
498
+ if (xhr && xhr.abort) {
499
+ xhr.abort();
500
+ }
501
+ }
502
+ };
503
+ };
504
+ Util.dataURIToBlob = function(dataURI) {
505
+ var byteString = atob(dataURI.split(",")[1]);
506
+ var mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0];
507
+ var ab = new ArrayBuffer(byteString.length);
508
+ var ia = new Uint8Array(ab);
509
+ for (var i = 0; i < byteString.length; i++) {
510
+ ia[i] = byteString.charCodeAt(i);
511
+ }
512
+ var bb = new Blob([ ab ], {
513
+ type: mimeString
514
+ });
515
+ return bb;
516
+ };
517
+ Util.throttle = function(f, ms) {
518
+ var last = 0;
519
+ var to;
520
+ var args;
521
+ var defer = function(delay) {
522
+ to = setTimeout(function() {
523
+ to = undefined;
524
+ var now = +new Date;
525
+ var diff = now - last;
526
+ if (diff < ms) {
527
+ return void defer(ms - diff);
528
+ }
529
+ f.apply(null, args);
530
+ }, delay);
531
+ };
532
+ var g = function() {
533
+ last = +new Date;
534
+ args = Util.slice(arguments);
535
+ if (to) {
536
+ return;
537
+ }
538
+ defer(ms);
539
+ };
540
+ g.clear = function() {
541
+ clearTimeout(to);
542
+ to = undefined;
543
+ };
544
+ return g;
545
+ };
546
+ Util.notAgainForAnother = function(f, t) {
547
+ if (typeof f !== "function" || typeof t !== "number") {
548
+ throw new Error("invalid inputs");
549
+ }
550
+ var last = null;
551
+ return function() {
552
+ var now = +new Date;
553
+ if (last && now <= last + t) {
554
+ return t - (now - last);
555
+ }
556
+ last = now;
557
+ f.apply(null, Util.slice(arguments));
558
+ return null;
559
+ };
560
+ };
561
+ Util.createRandomInteger = function() {
562
+ return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
563
+ };
564
+ Util.noop = function() {};
565
+ Util.once = function(f, g) {
566
+ return function() {
567
+ if (!f) {
568
+ return;
569
+ }
570
+ f.apply(this, Array.prototype.slice.call(arguments));
571
+ f = g;
572
+ };
573
+ };
574
+ Util.blobToImage = function(blob, cb) {
575
+ var reader = new FileReader;
576
+ reader.onloadend = function() {
577
+ cb(reader.result);
578
+ };
579
+ reader.readAsDataURL(blob);
580
+ };
581
+ Util.blobURLToImage = function(url, cb) {
582
+ var xhr = new XMLHttpRequest;
583
+ xhr.onload = function() {
584
+ var reader = new FileReader;
585
+ reader.onloadend = function() {
586
+ cb(reader.result);
587
+ };
588
+ reader.readAsDataURL(xhr.response);
589
+ };
590
+ xhr.open("GET", url);
591
+ xhr.responseType = "blob";
592
+ xhr.send();
593
+ };
594
+ Util.isObject = function(o) {
595
+ return typeof o === "object" && Object.prototype.toString.call(o) === "[object Object]";
596
+ };
597
+ Util.isCircular = function(o) {
598
+ try {
599
+ JSON.stringify(o);
600
+ return false;
601
+ } catch (e) {
602
+ return true;
603
+ }
604
+ };
605
+ Util.extend = function(a, b) {
606
+ if (!Util.isObject(a) || !Util.isObject(b)) {
607
+ return void console.log("Extend only works with 2 objects");
608
+ }
609
+ if (Util.isCircular(b)) {
610
+ return void console.log("Extend doesn't accept circular objects");
611
+ }
612
+ for (var k in b) {
613
+ if (Util.isObject(b[k])) {
614
+ a[k] = Util.isObject(a[k]) ? a[k] : {};
615
+ Util.extend(a[k], b[k]);
616
+ continue;
617
+ }
618
+ if (Array.isArray(b[k])) {
619
+ a[k] = b[k].slice();
620
+ continue;
621
+ }
622
+ a[k] = b[k];
623
+ }
624
+ };
625
+ Util.isChecked = function(el) {
626
+ if (!el) {
627
+ return false;
628
+ }
629
+ if (typeof el.tagName !== "undefined") {
630
+ return Boolean(el.checked);
631
+ }
632
+ if (typeof el.prop === "function") {
633
+ return Boolean(el.prop("checked"));
634
+ }
635
+ return false;
636
+ };
637
+ Util.hexToRGB = function(hex) {
638
+ var h = hex.replace(/^#/, "");
639
+ return [ parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16) ];
640
+ };
641
+ Util.rgbToHex = function(rgb) {
642
+ return `#${rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/).slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, "0")).join("")}`;
643
+ };
644
+ Util.isSmallScreen = function() {
645
+ return window.innerHeight < 800 || window.innerWidth < 800;
646
+ };
647
+ Util.stripTags = function(text) {
648
+ var div = document.createElement("div");
649
+ div.innerHTML = text;
650
+ return div.innerText;
651
+ };
652
+ Util.parseFilename = function(filename) {
653
+ if (!filename || !filename.trim()) {
654
+ return {};
655
+ }
656
+ var parsedName = /^(\.?.+?)(\.[^.]+)?$/.exec(filename) || [];
657
+ return {
658
+ name: parsedName[1],
659
+ ext: parsedName[2]
660
+ };
661
+ };
662
+ Util.isPlainTextFile = function(type, name) {
663
+ if (type && type.indexOf("text/") === 0) {
664
+ return true;
665
+ }
666
+ var parsedName = Util.parseFilename(name);
667
+ if (!type && name && !parsedName.ext) {
668
+ return true;
669
+ }
670
+ if (type === "application/x-javascript") {
671
+ return true;
672
+ }
673
+ if (type === "application/xml") {
674
+ return true;
675
+ }
676
+ return false;
677
+ };
678
+ Util.isSpreadsheet = function(type, name) {
679
+ return type && (type === "application/vnd.oasis.opendocument.spreadsheet" || type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") || name && (name.endsWith(".xlsx") || name.endsWith(".ods"));
680
+ };
681
+ Util.isOfficeDoc = function(type, name) {
682
+ return type && (type === "application/vnd.oasis.opendocument.text" || type === "application/vnd.openxmlformats-officedocument.wordprocessingml.document") || name && (name.endsWith(".docx") || name.endsWith(".odt"));
683
+ };
684
+ Util.isPresentation = function(type, name) {
685
+ return type && (type === "application/vnd.oasis.opendocument.presentation" || type === "application/vnd.openxmlformats-officedocument.presentationml.presentation") || name && (name.endsWith(".pptx") || name.endsWith(".odp"));
686
+ };
687
+ Util.isValidURL = function(str) {
688
+ var pattern = new RegExp("^(https?:\\/\\/)" + "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + "((\\d{1,3}\\.){3}\\d{1,3}))" + "(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + "(\\?[;&a-z\\d%_.~+=-]*)?");
689
+ return !!pattern.test(str);
690
+ };
691
+ var emoji_patt = /([\uD800-\uDBFF][\uDC00-\uDFFF])/;
692
+ var isEmoji = function(str) {
693
+ return emoji_patt.test(str);
694
+ };
695
+ var emojiStringToArray = function(str) {
696
+ var split = str.split(emoji_patt);
697
+ var arr = [];
698
+ for (var i = 0; i < split.length; i++) {
699
+ var char = split[i];
700
+ if (char !== "") {
701
+ arr.push(char);
702
+ }
703
+ }
704
+ return arr;
705
+ };
706
+ Util.getFirstCharacter = function(str) {
707
+ if (!str || !str.trim()) {
708
+ return "?";
709
+ }
710
+ var emojis = emojiStringToArray(str);
711
+ return isEmoji(emojis[0]) ? emojis[0] : str[0];
712
+ };
713
+ Util.getRandomColor = function(light) {
714
+ var getColor = function() {
715
+ if (light) {
716
+ return Math.floor(Math.random() * 156) + 70;
717
+ }
718
+ return Math.floor(Math.random() * 200) + 25;
719
+ };
720
+ return "#" + getColor().toString(16) + getColor().toString(16) + getColor().toString(16);
721
+ };
722
+ Util.checkRestrictedApp = function(app, AppConfig, earlyTypes, plan, loggedIn) {
723
+ if (Array.isArray(earlyTypes) && earlyTypes.includes(app) && !AppConfig.enableEarlyAccess) {
724
+ return -2;
725
+ }
726
+ var premiumTypes = AppConfig.premiumTypes;
727
+ if (!Array.isArray(premiumTypes) || !premiumTypes.includes(app)) {
728
+ return 2;
729
+ }
730
+ if (!loggedIn) {
731
+ return -1;
732
+ }
733
+ return plan ? 1 : 0;
734
+ };
735
+ var supportsSharedArrayBuffers = function() {
736
+ try {
737
+ return Object.prototype.toString.call(new window.WebAssembly.Memory({
738
+ shared: true,
739
+ initial: 0,
740
+ maximum: 0
741
+ }).buffer) === "[object SharedArrayBuffer]";
742
+ } catch (err) {
743
+ console.error(err);
744
+ }
745
+ return false;
746
+ };
747
+ Util.supportsWasm = function() {
748
+ return !(typeof Atomics === "undefined" || !supportsSharedArrayBuffers() || typeof WebAssembly === "undefined");
749
+ };
750
+ Util.getKeysArray = function(length) {
751
+ return [ ...Array(length).keys() ];
752
+ };
753
+ Util.getVersionFromUrlArgs = urlArgs => {
754
+ let arr = /ver=([0-9.]+)(-[0-9]*)?/.exec(urlArgs);
755
+ let ver = Array.isArray(arr) && arr[1];
756
+ return ver || undefined;
757
+ };
758
+ Util.Saferphore = {
759
+ create: resourceCount => {
760
+ var queue = [];
761
+ var check;
762
+ var mkRa = function() {
763
+ var outerCalled = 0;
764
+ return function(func) {
765
+ if (outerCalled++) {
766
+ throw new Error("returnAfter() called multiple times");
767
+ }
768
+ var called = 0;
769
+ return function() {
770
+ if (called++) {
771
+ throw new Error("returnAfter wrapped callback called multiple times");
772
+ }
773
+ if (func) {
774
+ func.apply(null, arguments);
775
+ }
776
+ resourceCount++;
777
+ check();
778
+ };
779
+ };
780
+ };
781
+ check = function() {
782
+ if (resourceCount < 0) {
783
+ throw new Error("(resourceCount < 0) should never happen");
784
+ }
785
+ if (resourceCount === 0 || queue.length === 0) {
786
+ return;
787
+ }
788
+ resourceCount--;
789
+ queue.shift()(mkRa());
790
+ };
791
+ return {
792
+ take: function(func) {
793
+ queue.push(func);
794
+ check();
795
+ }
796
+ };
797
+ }
798
+ };
799
+ return Util;
800
+ };
801
+ if (module.exports) {
802
+ module.exports = factory(require$$0);
803
+ }
804
+ })(typeof self !== "undefined" ? self : commonUtil);
805
+ })(commonUtil$1);
806
+ return commonUtil$1.exports;
807
+ }
808
+
809
+ var crypto;
810
+
811
+ var hasRequiredCrypto;
812
+
813
+ function requireCrypto() {
814
+ if (hasRequiredCrypto) return crypto;
815
+ hasRequiredCrypto = 1;
816
+ crypto = cryptoLib => {
817
+ let exports = {};
818
+ exports.decodeBase64 = msg => Buffer.from(msg, "base64");
819
+ exports.encodeBase64 = msg => Buffer.from(msg).toString("base64");
820
+ exports.decodeUTF8 = msg => Buffer.from(msg, "utf8");
821
+ exports.encodeUTF8 = msg => Buffer.from(msg).toString("utf8");
822
+ let SodiumNative, NaCl;
823
+ switch (cryptoLib) {
824
+ case "sodiumnative":
825
+ SodiumNative = require("sodium-native");
826
+ exports.sigVerify = (signedMessage, validateKey) => {
827
+ let msg = signedMessage.subarray(64);
828
+ let ok = SodiumNative.crypto_sign_open(msg, signedMessage, validateKey);
829
+ if (!ok) {
830
+ return false;
831
+ }
832
+ return msg;
833
+ };
834
+ exports.detachedVerify = (signedBuffer, signatureBuffer, validateKey) => SodiumNative.crypto_sign_verify_detached(signatureBuffer, signedBuffer, validateKey);
835
+ exports.secretbox = (message, nonce, secretKey) => {
836
+ let secretBox = Buffer.alloc(message.length + SodiumNative.crypto_box_MACBYTES);
837
+ SodiumNative.crypto_secretbox_easy(secretBox, message, nonce, secretKey);
838
+ return secretBox;
839
+ };
840
+ exports.secretboxOpen = (secretBox, nonce, secretKey) => {
841
+ let msg = Buffer.alloc(secretBox.length - SodiumNative.crypto_secretbox_MACBYTES);
842
+ if (SodiumNative.crypto_secretbox_open_easy(msg, secretBox, nonce, secretKey)) {
843
+ return msg;
844
+ } else {
845
+ return void 0;
846
+ }
847
+ };
848
+ exports.publicKeyFromSecretKey = secretKey => {
849
+ let pk = new Uint8Array(SodiumNative.crypto_sign_PUBLICKEYBYTES);
850
+ SodiumNative.crypto_sign_ed25519_sk_to_pk(pk, secretKey);
851
+ return pk;
852
+ };
853
+ exports.hash = message => {
854
+ let out = Buffer.alloc(SodiumNative.crypto_hash_sha512_BYTES);
855
+ SodiumNative.crypto_hash_sha512(out, message);
856
+ return out;
857
+ };
858
+ break;
859
+
860
+ default:
861
+ NaCl = require$$0$1;
862
+ exports.sigVerify = NaCl.sign.open;
863
+ exports.detachedVerify = NaCl.sign.detached.verify;
864
+ exports.secretbox = NaCl.secretbox;
865
+ exports.secretboxOpen = (secretBox, nonce, secretKey) => Buffer.from(NaCl.secretbox.open(secretBox, nonce, secretKey));
866
+ exports.publicKeyFromSecretKey = secretKey => NaCl.sign?.keyPair?.fromSecretKey(secretKey)?.publicKey;
867
+ exports.hash = msg => Buffer.from(NaCl.hash(msg));
868
+ break;
869
+ }
870
+ return exports;
871
+ };
872
+ return crypto;
873
+ }
874
+
875
+ var hasRequiredWorker;
876
+
877
+ function requireWorker() {
878
+ if (hasRequiredWorker) return worker$1;
879
+ hasRequiredWorker = 1;
880
+ const Util = requireCommonUtil();
881
+ const Crypto = requireCrypto()("sodiumnative");
882
+ const COMMANDS = {};
883
+ const init = (config, cb) => {
884
+ cb();
885
+ };
886
+ let onValidateMessage = (msg, vk, cb) => {
887
+ let signedMsg;
888
+ try {
889
+ signedMsg = Crypto.decodeBase64(msg);
890
+ } catch {
891
+ return void cb("E_BAD_MESSAGE");
892
+ }
893
+ let validateKey;
894
+ try {
895
+ validateKey = Crypto.decodeBase64(vk);
896
+ } catch {
897
+ return void cb("E_BADKEY");
898
+ }
899
+ const validated = Crypto.sigVerify(signedMsg, validateKey);
900
+ if (!validated) {
901
+ return void cb("FAILED");
902
+ }
903
+ cb();
904
+ };
905
+ const onValidateRpc = (signedMsg, signature, publicKey) => {
906
+ if (!(signedMsg && publicKey)) {
907
+ throw new Error("INVALID_ARGS");
908
+ }
909
+ let signedBuffer;
910
+ let pubBuffer;
911
+ let signatureBuffer;
912
+ try {
913
+ signedBuffer = Util.decodeUTF8(signedMsg);
914
+ } catch (e) {
915
+ throw new Error("INVALID_SIGNED_BUFFER");
916
+ }
917
+ try {
918
+ pubBuffer = Util.decodeBase64(publicKey);
919
+ } catch (e) {
920
+ throw new Error("INVALID_PUBLIC_KEY");
921
+ }
922
+ try {
923
+ signatureBuffer = Util.decodeBase64(signature);
924
+ } catch (e) {
925
+ throw new Error("INVALID_SIGNATURE");
926
+ }
927
+ if (pubBuffer.length !== 32) {
928
+ throw new Error("INVALID_PUBLIC_KEY_LENGTH");
929
+ }
930
+ if (signatureBuffer.length !== 64) {
931
+ throw new Error("INVALID_SIGNATURE_LENGTH");
932
+ }
933
+ if (Crypto.detachedVerify(signedBuffer, signatureBuffer, pubBuffer) !== true) {
934
+ throw new Error("FAILED");
935
+ }
936
+ };
937
+ COMMANDS.VALIDATE_MESSAGE = (data, cb) => {
938
+ onValidateMessage(data.signedMsg, data.validateKey, cb);
939
+ };
940
+ COMMANDS.VALIDATE_RPC = (data, cb) => {
941
+ try {
942
+ onValidateRpc(data.msg, data.sig, data.key);
943
+ } catch (err) {
944
+ return void cb(err && err.message);
945
+ }
946
+ cb();
947
+ };
948
+ let ready = false;
949
+ process.on("message", function(obj) {
950
+ if (!obj || !obj.txid || !obj.pid) {
951
+ return void process.send({
952
+ error: "E_INVAL",
953
+ data: obj
954
+ });
955
+ }
956
+ const command = COMMANDS[obj.command];
957
+ const data = obj.data;
958
+ const cb = function(err, value) {
959
+ process.send({
960
+ error: Util.serializeError(err),
961
+ txid: obj.txid,
962
+ pid: obj.pid,
963
+ value
964
+ });
965
+ };
966
+ if (!ready) {
967
+ return void init(obj.config, function(err) {
968
+ if (err) {
969
+ return void cb(Util.serializeError(err));
970
+ }
971
+ ready = true;
972
+ cb();
973
+ });
974
+ }
975
+ if (typeof command !== "function") {
976
+ return void cb("E_BAD_COMMAND");
977
+ }
978
+ command(data, cb);
979
+ });
980
+ process.on("uncaughtException", function(err) {
981
+ console.error("[%s] UNCAUGHT EXCEPTION IN DB WORKER", new Date);
982
+ console.error(err);
983
+ console.error("TERMINATING");
984
+ process.exit(1);
985
+ });
986
+ return worker$1;
987
+ }
988
+
989
+ var workerExports = requireWorker();
990
+
991
+ var worker = getDefaultExportFromCjs(workerExports);
992
+
993
+ module.exports = worker;