保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
 * node-cache 4.2.1 ( 2019-07-24 )
 * https://github.com/mpneuried/nodecache
 *
 * Released under the MIT license
 * https://github.com/mpneuried/nodecache/blob/master/LICENSE
 *
 * Maintained by  (  )
*/
(function() {
  // lodash requires
  var EventEmitter, NodeCache, _assignIn, _isArray, _isFunction, _isNumber, _isObject, _isString, _size, _template, clone,
    boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } },
    indexOf = [].indexOf;
 
  _assignIn = require("lodash/assignIn");
 
  _isArray = require("lodash/isArray");
 
  _isString = require("lodash/isString");
 
  _isFunction = require("lodash/isFunction");
 
  _isNumber = require("lodash/isNumber");
 
  _isObject = require("lodash/isObject");
 
  _size = require("lodash/size");
 
  _template = require("lodash/template");
 
  clone = require("clone");
 
  EventEmitter = require('events').EventEmitter;
 
  // generate superclass
  module.exports = NodeCache = (function() {
    class NodeCache extends EventEmitter {
      constructor(options = {}) {
        super();
        // ## get
 
        // get a cached key and change the stats
 
        // **Parameters:**
 
        // * `key` ( String | Number ): cache key
        // * `[cb]` ( Function ): Callback function
        // * `[errorOnMissing=false]` ( Boolean ) return a error to the `cb` or throw it if no `cb` is used. Otherwise the get will return `undefined` on a miss.
 
        // **Example:**
 
        //     myCache.get "myKey", ( err, val )->
        //       console.log( err, val )
        //       return
 
        this.get = this.get.bind(this);
        // ## mget
 
        // get multiple cached keys at once and change the stats
 
        // **Parameters:**
 
        // * `keys` ( String|Number[] ): an array of keys
        // * `[cb]` ( Function ): Callback function
 
        // **Example:**
 
        //     myCache.mget [ "foo", "bar" ], ( err, val )->
        //       console.log( err, val )
        //       return
 
        this.mget = this.mget.bind(this);
        // ## set
 
        // set a cached key and change the stats
 
        // **Parameters:**
 
        // * `key` ( String | Number ): cache key
        // * `value` ( Any ): A element to cache. If the option `option.forceString` is `true` the module trys to translate it to a serialized JSON
        // * `[ ttl ]` ( Number | String ): ( optional ) The time to live in seconds.
        // * `[cb]` ( Function ): Callback function
 
        // **Example:**
 
        //     myCache.set "myKey", "my_String Value", ( err, success )->
        //       console.log( err, success )
 
        //     myCache.set "myKey", "my_String Value", "10", ( err, success )->
        //       console.log( err, success )
 
        this.set = this.set.bind(this);
        // ## del
 
        // remove keys
 
        // **Parameters:**
 
        // * `keys` ( String | Number | String|Number[] ): cache key to delete or a array of cache keys
        // * `[cb]` ( Function ): Callback function
 
        // **Return**
 
        // ( Number ): Number of deleted keys
 
        // **Example:**
 
        //     myCache.del( "myKey" )
 
        //     myCache.del( "myKey", ( err, delCount )->
        //       console.log( err, delCount )
        //       return
        this.del = this.del.bind(this);
        // ## ttl
 
        // reset or redefine the ttl of a key. `ttl` = 0 means infinite lifetime.
        // If `ttl` is not passed the default ttl is used.
        // If `ttl` < 0 the key will be deleted.
 
        // **Parameters:**
 
        // * `key` ( String | Number ): cache key to reset the ttl value
        // * `ttl` ( Number ): ( optional -> options.stdTTL || 0 ) The time to live in seconds
        // * `[cb]` ( Function ): Callback function
 
        // **Return**
 
        // ( Boolen ): key found and ttl set
 
        // **Example:**
 
        //     myCache.ttl( "myKey" ) // will set ttl to default ttl
 
        //     myCache.ttl( "myKey", 1000, ( err, keyFound )->
        //       console.log( err, success )
 
        this.ttl = this.ttl.bind(this);
        // ## getTtl
 
        // receive the ttl of a key.
 
        // **Parameters:**
 
        // * `key` ( String | Number ): cache key to check the ttl value
        // * `[cb]` ( Function ): Callback function
 
        // **Return**
 
        // ( Number|undefined ): The timestamp in ms when the key will expire, 0 if it will never expire or undefined if it not exists
 
        // **Example:**
 
        //     ts = myCache.getTtl( "myKey" )
 
        //     myCache.getTtl( "myKey",( err, ttl )->
        //       console.log( err, ttl )
        //       return
 
        this.getTtl = this.getTtl.bind(this);
        // ## keys
 
        // list all keys within this cache
 
        // **Parameters:**
 
        // * `[cb]` ( Function ): Callback function
 
        // **Return**
 
        // ( Array ): An array of all keys
 
        // **Example:**
 
        //     _keys = myCache.keys()
 
        //     # [ "foo", "bar", "fizz", "buzz", "anotherKeys" ]
 
        this.keys = this.keys.bind(this);
        // ## getStats
 
        // get the stats
 
        // **Parameters:**
 
        // -
 
        // **Return**
 
        // ( Object ): Stats data
 
        // **Example:**
 
        //     myCache.getStats()
        //     # {
        //     # hits: 0,
        //     # misses: 0,
        //     # keys: 0,
        //     # ksize: 0,
        //     # vsize: 0
        //     # }
 
        this.getStats = this.getStats.bind(this);
        // ## flushAll
 
        // flush the whole data and reset the stats
 
        // **Example:**
 
        //     myCache.flushAll()
 
        //     myCache.getStats()
        //     # {
        //     # hits: 0,
        //     # misses: 0,
        //     # keys: 0,
        //     # ksize: 0,
        //     # vsize: 0
        //     # }
 
        this.flushAll = this.flushAll.bind(this);
        // ## close
 
        // This will clear the interval timeout which is set on checkperiod option.
 
        // **Example:**
 
        //     myCache.close()
 
        this.close = this.close.bind(this);
        // ## _checkData
 
        // internal housekeeping method.
        // Check all the cached data and delete the invalid values
        this._checkData = this._checkData.bind(this);
        // ## _check
 
        // internal method the check the value. If it's not valid any more delete it
        this._check = this._check.bind(this);
        // ## _isInvalidKey
 
        // internal method to check if the type of a key is either `number` or `string`
        this._isInvalidKey = this._isInvalidKey.bind(this);
        // ## _wrap
 
        // internal method to wrap a value in an object with some metadata
        this._wrap = this._wrap.bind(this);
        // ## _getValLength
 
        // internal method to calculate the value length
        this._getValLength = this._getValLength.bind(this);
        // ## _error
 
        // internal method to handle an error message
        this._error = this._error.bind(this);
        // ## _initErrors
 
        // internal method to generate error message templates
        this._initErrors = this._initErrors.bind(this);
        this.options = options;
        this._initErrors();
        // container for cached data
        this.data = {};
        // module options
        this.options = _assignIn({
          // convert all elements to string
          forceString: false,
          // used standard size for calculating value size
          objectValueSize: 80,
          promiseValueSize: 80,
          arrayValueSize: 40,
          // standard time to live in seconds. 0 = infinity;
          stdTTL: 0,
          // time in seconds to check all data and delete expired keys
          checkperiod: 600,
          // en/disable cloning of variables. If `true` you'll get a copy of the cached variable. If `false` you'll save and get just the reference
          useClones: true,
          // en/disable throwing errors when trying to `.get` missing or expired values.
          errorOnMissing: false,
          // whether values should be deleted automatically at expiration
          deleteOnExpire: true
        }, this.options);
        // statistics container
        this.stats = {
          hits: 0,
          misses: 0,
          keys: 0,
          ksize: 0,
          vsize: 0
        };
        // pre allocate valid keytypes array
        this.validKeyTypes = ["string", "number"];
        // initalize checking period
        this._checkData();
        return;
      }
 
      get(key, cb, errorOnMissing) {
        var _err, _ret, err;
        boundMethodCheck(this, NodeCache);
        // handle passing in errorOnMissing without cb
        if (typeof cb === "boolean" && arguments.length === 2) {
          errorOnMissing = cb;
          cb = void 0;
        }
        // handle invalid key types
        if ((err = this._isInvalidKey(key)) != null) {
          if (cb != null) {
            cb(err);
            return;
          } else {
            throw err;
          }
        }
        // get data and incremet stats
        if ((this.data[key] != null) && this._check(key, this.data[key])) {
          this.stats.hits++;
          _ret = this._unwrap(this.data[key]);
          if (cb != null) {
            // return data
            cb(null, _ret);
          }
          return _ret;
        } else {
          // if not found return a error
          this.stats.misses++;
          if (this.options.errorOnMissing || errorOnMissing) {
            _err = this._error("ENOTFOUND", {
              key: key
            }, cb);
            if (_err != null) {
              throw _err;
            }
            return;
          } else {
            if (cb != null) {
              cb(null, void 0);
            }
          }
          return void 0;
        }
      }
 
      mget(keys, cb) {
        var _err, err, i, key, len, oRet;
        boundMethodCheck(this, NodeCache);
        // convert a string to an array of one key
        if (!_isArray(keys)) {
          _err = this._error("EKEYSTYPE");
          if (cb != null) {
            cb(_err);
          }
          return _err;
        }
        // define return
        oRet = {};
        for (i = 0, len = keys.length; i < len; i++) {
          key = keys[i];
          // handle invalid key types
          if ((err = this._isInvalidKey(key)) != null) {
            if (cb != null) {
              cb(err);
              return;
            } else {
              throw err;
            }
          }
          // get data and increment stats
          if ((this.data[key] != null) && this._check(key, this.data[key])) {
            this.stats.hits++;
            oRet[key] = this._unwrap(this.data[key]);
          } else {
            // if not found return a error
            this.stats.misses++;
          }
        }
        if (cb != null) {
          // return all found keys
          cb(null, oRet);
        }
        return oRet;
      }
 
      set(key, value, ttl, cb) {
        var err, existent;
        boundMethodCheck(this, NodeCache);
        // force the data to string
        if (this.options.forceString && !_isString(value)) {
          value = JSON.stringify(value);
        }
        // remap the arguments if `ttl` is not passed
        if (arguments.length === 3 && _isFunction(ttl)) {
          cb = ttl;
          ttl = this.options.stdTTL;
        }
        // handle invalid key types
        if ((err = this._isInvalidKey(key)) != null) {
          if (cb != null) {
            cb(err);
            return;
          } else {
            throw err;
          }
        }
        // internal helper variables
        existent = false;
        // remove existing data from stats
        if (this.data[key]) {
          existent = true;
          this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false));
        }
        // set the value
        this.data[key] = this._wrap(value, ttl);
        this.stats.vsize += this._getValLength(value);
        // only add the keys and key-size if the key is new
        if (!existent) {
          this.stats.ksize += this._getKeyLength(key);
          this.stats.keys++;
        }
        this.emit("set", key, value);
        if (cb != null) {
          // return true
          cb(null, true);
        }
        return true;
      }
 
      del(keys, cb) {
        var delCount, err, i, key, len, oldVal;
        boundMethodCheck(this, NodeCache);
        // convert keys to an array of itself
        if (!_isArray(keys)) {
          keys = [keys];
        }
        delCount = 0;
        for (i = 0, len = keys.length; i < len; i++) {
          key = keys[i];
          // handle invalid key types
          if ((err = this._isInvalidKey(key)) != null) {
            if (cb != null) {
              cb(err);
              return;
            } else {
              throw err;
            }
          }
          // only delete if existent
          if (this.data[key] != null) {
            // calc the stats
            this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false));
            this.stats.ksize -= this._getKeyLength(key);
            this.stats.keys--;
            delCount++;
            // delete the value
            oldVal = this.data[key];
            delete this.data[key];
            // return true
            this.emit("del", key, oldVal.v);
          } else {
            // if the key has not been found return an error
            this.stats.misses++;
          }
        }
        if (cb != null) {
          cb(null, delCount);
        }
        return delCount;
      }
 
      ttl() {
        var arg, args, cb, err, i, key, len, ttl;
        boundMethodCheck(this, NodeCache);
        // change args if only key and callback are passed
        [key, ...args] = arguments;
        for (i = 0, len = args.length; i < len; i++) {
          arg = args[i];
          switch (typeof arg) {
            case "number":
              ttl = arg;
              break;
            case "function":
              cb = arg;
          }
        }
        ttl || (ttl = this.options.stdTTL);
        if (!key) {
          if (cb != null) {
            cb(null, false);
          }
          return false;
        }
        // handle invalid key types
        if ((err = this._isInvalidKey(key)) != null) {
          if (cb != null) {
            cb(err);
            return;
          } else {
            throw err;
          }
        }
        // check for existant data and update the ttl value
        if ((this.data[key] != null) && this._check(key, this.data[key])) {
          // if ttl < 0 delete the key. otherwise reset the value
          if (ttl >= 0) {
            this.data[key] = this._wrap(this.data[key].v, ttl, false);
          } else {
            this.del(key);
          }
          if (cb != null) {
            cb(null, true);
          }
          return true;
        } else {
          if (cb != null) {
            // return false if key has not been found
            cb(null, false);
          }
          return false;
        }
      }
 
      getTtl(key, cb) {
        var _ttl, err;
        boundMethodCheck(this, NodeCache);
        if (!key) {
          if (cb != null) {
            cb(null, void 0);
          }
          return void 0;
        }
        // handle invalid key types
        if ((err = this._isInvalidKey(key)) != null) {
          if (cb != null) {
            cb(err);
            return;
          } else {
            throw err;
          }
        }
        // check for existant data and update the ttl value
        if ((this.data[key] != null) && this._check(key, this.data[key])) {
          _ttl = this.data[key].t;
          if (cb != null) {
            cb(null, _ttl);
          }
          return _ttl;
        } else {
          if (cb != null) {
            // return undefined if key has not been found
            cb(null, void 0);
          }
          return void 0;
        }
      }
 
      keys(cb) {
        var _keys;
        boundMethodCheck(this, NodeCache);
        _keys = Object.keys(this.data);
        if (cb != null) {
          cb(null, _keys);
        }
        return _keys;
      }
 
      getStats() {
        boundMethodCheck(this, NodeCache);
        return this.stats;
      }
 
      flushAll(_startPeriod = true) {
        boundMethodCheck(this, NodeCache);
        // parameter just for testing
 
        // set data empty
        this.data = {};
        // reset stats
        this.stats = {
          hits: 0,
          misses: 0,
          keys: 0,
          ksize: 0,
          vsize: 0
        };
        // reset check period
        this._killCheckPeriod();
        this._checkData(_startPeriod);
        this.emit("flush");
      }
 
      close() {
        boundMethodCheck(this, NodeCache);
        this._killCheckPeriod();
      }
 
      _checkData(startPeriod = true) {
        var key, ref, value;
        boundMethodCheck(this, NodeCache);
        ref = this.data;
        // run the housekeeping method
        for (key in ref) {
          value = ref[key];
          this._check(key, value);
        }
        if (startPeriod && this.options.checkperiod > 0) {
          this.checkTimeout = setTimeout(this._checkData, this.options.checkperiod * 1000, startPeriod);
          if (this.checkTimeout.unref != null) {
            this.checkTimeout.unref();
          }
        }
      }
 
      // ## _killCheckPeriod
 
      // stop the checkdata period. Only needed to abort the script in testing mode.
      _killCheckPeriod() {
        if (this.checkTimeout != null) {
          return clearTimeout(this.checkTimeout);
        }
      }
 
      _check(key, data) {
        var _retval;
        boundMethodCheck(this, NodeCache);
        _retval = true;
        // data is invalid if the ttl is too old and is not 0
        // console.log data.t < Date.now(), data.t, Date.now()
        if (data.t !== 0 && data.t < Date.now()) {
          if (this.options.deleteOnExpire) {
            _retval = false;
            this.del(key);
          }
          this.emit("expired", key, this._unwrap(data));
        }
        return _retval;
      }
 
      _isInvalidKey(key) {
        var ref;
        boundMethodCheck(this, NodeCache);
        if (ref = typeof key, indexOf.call(this.validKeyTypes, ref) < 0) {
          return this._error("EKEYTYPE", {
            type: typeof key
          });
        }
      }
 
      _wrap(value, ttl, asClone = true) {
        var livetime, now, oReturn, ttlMultiplicator;
        boundMethodCheck(this, NodeCache);
        if (!this.options.useClones) {
          asClone = false;
        }
        // define the time to live
        now = Date.now();
        livetime = 0;
        ttlMultiplicator = 1000;
        // use given ttl
        if (ttl === 0) {
          livetime = 0;
        } else if (ttl) {
          livetime = now + (ttl * ttlMultiplicator);
        } else {
          // use standard ttl
          if (this.options.stdTTL === 0) {
            livetime = this.options.stdTTL;
          } else {
            livetime = now + (this.options.stdTTL * ttlMultiplicator);
          }
        }
        // return the wrapped value
        return oReturn = {
          t: livetime,
          v: asClone ? clone(value) : value
        };
      }
 
      // ## _unwrap
 
      // internal method to extract get the value out of the wrapped value
      _unwrap(value, asClone = true) {
        if (!this.options.useClones) {
          asClone = false;
        }
        if (value.v != null) {
          if (asClone) {
            return clone(value.v);
          } else {
            return value.v;
          }
        }
        return null;
      }
 
      // ## _getKeyLength
 
      // internal method the calculate the key length
      _getKeyLength(key) {
        return key.length;
      }
 
      _getValLength(value) {
        boundMethodCheck(this, NodeCache);
        if (_isString(value)) {
          // if the value is a String get the real length
          return value.length;
        } else if (this.options.forceString) {
          // force string if it's defined and not passed
          return JSON.stringify(value).length;
        } else if (_isArray(value)) {
          // if the data is an Array multiply each element with a defined default length
          return this.options.arrayValueSize * value.length;
        } else if (_isNumber(value)) {
          return 8;
        } else if (typeof (value != null ? value.then : void 0) === "function") {
          // if the data is a Promise, use defined default
          // (can't calculate actual/resolved value size synchronously)
          return this.options.promiseValueSize;
        } else if (_isObject(value)) {
          // if the data is an Object multiply each element with a defined default length
          return this.options.objectValueSize * _size(value);
        } else {
          // default fallback
          return 0;
        }
      }
 
      _error(type, data = {}, cb) {
        var error;
        boundMethodCheck(this, NodeCache);
        // generate the error object
        error = new Error();
        error.name = type;
        error.errorcode = type;
        error.message = this.ERRORS[type] != null ? this.ERRORS[type](data) : "-";
        error.data = data;
        if (cb && _isFunction(cb)) {
          // return the error
          cb(error, null);
        } else {
          // if no callback is defined return the error object
          return error;
        }
      }
 
      _initErrors() {
        var _errMsg, _errT, ref;
        boundMethodCheck(this, NodeCache);
        this.ERRORS = {};
        ref = this._ERRORS;
        for (_errT in ref) {
          _errMsg = ref[_errT];
          this.ERRORS[_errT] = _template(_errMsg);
        }
      }
 
    };
 
    NodeCache.prototype._ERRORS = {
      "ENOTFOUND": "Key `<%= key %>` not found",
      "EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `<%= type %>`",
      "EKEYSTYPE": "The keys argument has to be an array."
    };
 
    return NodeCache;
 
  }).call(this);
 
}).call(this);