@debridge-finance/solana-utils 6.0.0 → 6.1.0
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/LICENSE +100 -100
- package/README.md +3 -3
- package/dist/browser/index.js +1697 -70
- package/dist/browser/index.js.map +1 -1
- package/dist/cjs/spl.js +103 -68
- package/dist/cjs/spl.js.map +1 -1
- package/dist/esm/spl.js +106 -71
- package/dist/esm/spl.js.map +1 -1
- package/dist/types/spl.d.ts +17 -7
- package/dist/types/spl.d.ts.map +1 -1
- package/package.json +84 -84
package/dist/browser/index.js
CHANGED
|
@@ -2,6 +2,10 @@ import require$$1, { MessageV0, VersionedTransaction, Transaction, PublicKey, Tr
|
|
|
2
2
|
|
|
3
3
|
var commonjsGlobal$1 = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
4
4
|
|
|
5
|
+
function getDefaultExportFromCjs$1 (x) {
|
|
6
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
7
|
+
}
|
|
8
|
+
|
|
5
9
|
function getAugmentedNamespace(n) {
|
|
6
10
|
var f = n.default;
|
|
7
11
|
if (typeof f == "function") {
|
|
@@ -11343,6 +11347,1595 @@ struct$1([
|
|
|
11343
11347
|
publicKey$4('delegate'),
|
|
11344
11348
|
]);
|
|
11345
11349
|
|
|
11350
|
+
var nodeCache = {exports: {}};
|
|
11351
|
+
|
|
11352
|
+
var node_cache = {exports: {}};
|
|
11353
|
+
|
|
11354
|
+
var clone = {exports: {}};
|
|
11355
|
+
|
|
11356
|
+
var hasRequiredClone;
|
|
11357
|
+
|
|
11358
|
+
function requireClone () {
|
|
11359
|
+
if (hasRequiredClone) return clone.exports;
|
|
11360
|
+
hasRequiredClone = 1;
|
|
11361
|
+
(function (module) {
|
|
11362
|
+
var clone = (function() {
|
|
11363
|
+
|
|
11364
|
+
function _instanceof(obj, type) {
|
|
11365
|
+
return type != null && obj instanceof type;
|
|
11366
|
+
}
|
|
11367
|
+
|
|
11368
|
+
var nativeMap;
|
|
11369
|
+
try {
|
|
11370
|
+
nativeMap = Map;
|
|
11371
|
+
} catch(_) {
|
|
11372
|
+
// maybe a reference error because no `Map`. Give it a dummy value that no
|
|
11373
|
+
// value will ever be an instanceof.
|
|
11374
|
+
nativeMap = function() {};
|
|
11375
|
+
}
|
|
11376
|
+
|
|
11377
|
+
var nativeSet;
|
|
11378
|
+
try {
|
|
11379
|
+
nativeSet = Set;
|
|
11380
|
+
} catch(_) {
|
|
11381
|
+
nativeSet = function() {};
|
|
11382
|
+
}
|
|
11383
|
+
|
|
11384
|
+
var nativePromise;
|
|
11385
|
+
try {
|
|
11386
|
+
nativePromise = Promise;
|
|
11387
|
+
} catch(_) {
|
|
11388
|
+
nativePromise = function() {};
|
|
11389
|
+
}
|
|
11390
|
+
|
|
11391
|
+
/**
|
|
11392
|
+
* Clones (copies) an Object using deep copying.
|
|
11393
|
+
*
|
|
11394
|
+
* This function supports circular references by default, but if you are certain
|
|
11395
|
+
* there are no circular references in your object, you can save some CPU time
|
|
11396
|
+
* by calling clone(obj, false).
|
|
11397
|
+
*
|
|
11398
|
+
* Caution: if `circular` is false and `parent` contains circular references,
|
|
11399
|
+
* your program may enter an infinite loop and crash.
|
|
11400
|
+
*
|
|
11401
|
+
* @param `parent` - the object to be cloned
|
|
11402
|
+
* @param `circular` - set to true if the object to be cloned may contain
|
|
11403
|
+
* circular references. (optional - true by default)
|
|
11404
|
+
* @param `depth` - set to a number if the object is only to be cloned to
|
|
11405
|
+
* a particular depth. (optional - defaults to Infinity)
|
|
11406
|
+
* @param `prototype` - sets the prototype to be used when cloning an object.
|
|
11407
|
+
* (optional - defaults to parent prototype).
|
|
11408
|
+
* @param `includeNonEnumerable` - set to true if the non-enumerable properties
|
|
11409
|
+
* should be cloned as well. Non-enumerable properties on the prototype
|
|
11410
|
+
* chain will be ignored. (optional - false by default)
|
|
11411
|
+
*/
|
|
11412
|
+
function clone(parent, circular, depth, prototype, includeNonEnumerable) {
|
|
11413
|
+
if (typeof circular === 'object') {
|
|
11414
|
+
depth = circular.depth;
|
|
11415
|
+
prototype = circular.prototype;
|
|
11416
|
+
includeNonEnumerable = circular.includeNonEnumerable;
|
|
11417
|
+
circular = circular.circular;
|
|
11418
|
+
}
|
|
11419
|
+
// maintain two arrays for circular references, where corresponding parents
|
|
11420
|
+
// and children have the same index
|
|
11421
|
+
var allParents = [];
|
|
11422
|
+
var allChildren = [];
|
|
11423
|
+
|
|
11424
|
+
var useBuffer = typeof Buffer != 'undefined';
|
|
11425
|
+
|
|
11426
|
+
if (typeof circular == 'undefined')
|
|
11427
|
+
circular = true;
|
|
11428
|
+
|
|
11429
|
+
if (typeof depth == 'undefined')
|
|
11430
|
+
depth = Infinity;
|
|
11431
|
+
|
|
11432
|
+
// recurse this function so we don't reset allParents and allChildren
|
|
11433
|
+
function _clone(parent, depth) {
|
|
11434
|
+
// cloning null always returns null
|
|
11435
|
+
if (parent === null)
|
|
11436
|
+
return null;
|
|
11437
|
+
|
|
11438
|
+
if (depth === 0)
|
|
11439
|
+
return parent;
|
|
11440
|
+
|
|
11441
|
+
var child;
|
|
11442
|
+
var proto;
|
|
11443
|
+
if (typeof parent != 'object') {
|
|
11444
|
+
return parent;
|
|
11445
|
+
}
|
|
11446
|
+
|
|
11447
|
+
if (_instanceof(parent, nativeMap)) {
|
|
11448
|
+
child = new nativeMap();
|
|
11449
|
+
} else if (_instanceof(parent, nativeSet)) {
|
|
11450
|
+
child = new nativeSet();
|
|
11451
|
+
} else if (_instanceof(parent, nativePromise)) {
|
|
11452
|
+
child = new nativePromise(function (resolve, reject) {
|
|
11453
|
+
parent.then(function(value) {
|
|
11454
|
+
resolve(_clone(value, depth - 1));
|
|
11455
|
+
}, function(err) {
|
|
11456
|
+
reject(_clone(err, depth - 1));
|
|
11457
|
+
});
|
|
11458
|
+
});
|
|
11459
|
+
} else if (clone.__isArray(parent)) {
|
|
11460
|
+
child = [];
|
|
11461
|
+
} else if (clone.__isRegExp(parent)) {
|
|
11462
|
+
child = new RegExp(parent.source, __getRegExpFlags(parent));
|
|
11463
|
+
if (parent.lastIndex) child.lastIndex = parent.lastIndex;
|
|
11464
|
+
} else if (clone.__isDate(parent)) {
|
|
11465
|
+
child = new Date(parent.getTime());
|
|
11466
|
+
} else if (useBuffer && Buffer.isBuffer(parent)) {
|
|
11467
|
+
if (Buffer.allocUnsafe) {
|
|
11468
|
+
// Node.js >= 4.5.0
|
|
11469
|
+
child = Buffer.allocUnsafe(parent.length);
|
|
11470
|
+
} else {
|
|
11471
|
+
// Older Node.js versions
|
|
11472
|
+
child = new Buffer(parent.length);
|
|
11473
|
+
}
|
|
11474
|
+
parent.copy(child);
|
|
11475
|
+
return child;
|
|
11476
|
+
} else if (_instanceof(parent, Error)) {
|
|
11477
|
+
child = Object.create(parent);
|
|
11478
|
+
} else {
|
|
11479
|
+
if (typeof prototype == 'undefined') {
|
|
11480
|
+
proto = Object.getPrototypeOf(parent);
|
|
11481
|
+
child = Object.create(proto);
|
|
11482
|
+
}
|
|
11483
|
+
else {
|
|
11484
|
+
child = Object.create(prototype);
|
|
11485
|
+
proto = prototype;
|
|
11486
|
+
}
|
|
11487
|
+
}
|
|
11488
|
+
|
|
11489
|
+
if (circular) {
|
|
11490
|
+
var index = allParents.indexOf(parent);
|
|
11491
|
+
|
|
11492
|
+
if (index != -1) {
|
|
11493
|
+
return allChildren[index];
|
|
11494
|
+
}
|
|
11495
|
+
allParents.push(parent);
|
|
11496
|
+
allChildren.push(child);
|
|
11497
|
+
}
|
|
11498
|
+
|
|
11499
|
+
if (_instanceof(parent, nativeMap)) {
|
|
11500
|
+
parent.forEach(function(value, key) {
|
|
11501
|
+
var keyChild = _clone(key, depth - 1);
|
|
11502
|
+
var valueChild = _clone(value, depth - 1);
|
|
11503
|
+
child.set(keyChild, valueChild);
|
|
11504
|
+
});
|
|
11505
|
+
}
|
|
11506
|
+
if (_instanceof(parent, nativeSet)) {
|
|
11507
|
+
parent.forEach(function(value) {
|
|
11508
|
+
var entryChild = _clone(value, depth - 1);
|
|
11509
|
+
child.add(entryChild);
|
|
11510
|
+
});
|
|
11511
|
+
}
|
|
11512
|
+
|
|
11513
|
+
for (var i in parent) {
|
|
11514
|
+
var attrs;
|
|
11515
|
+
if (proto) {
|
|
11516
|
+
attrs = Object.getOwnPropertyDescriptor(proto, i);
|
|
11517
|
+
}
|
|
11518
|
+
|
|
11519
|
+
if (attrs && attrs.set == null) {
|
|
11520
|
+
continue;
|
|
11521
|
+
}
|
|
11522
|
+
child[i] = _clone(parent[i], depth - 1);
|
|
11523
|
+
}
|
|
11524
|
+
|
|
11525
|
+
if (Object.getOwnPropertySymbols) {
|
|
11526
|
+
var symbols = Object.getOwnPropertySymbols(parent);
|
|
11527
|
+
for (var i = 0; i < symbols.length; i++) {
|
|
11528
|
+
// Don't need to worry about cloning a symbol because it is a primitive,
|
|
11529
|
+
// like a number or string.
|
|
11530
|
+
var symbol = symbols[i];
|
|
11531
|
+
var descriptor = Object.getOwnPropertyDescriptor(parent, symbol);
|
|
11532
|
+
if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
|
|
11533
|
+
continue;
|
|
11534
|
+
}
|
|
11535
|
+
child[symbol] = _clone(parent[symbol], depth - 1);
|
|
11536
|
+
if (!descriptor.enumerable) {
|
|
11537
|
+
Object.defineProperty(child, symbol, {
|
|
11538
|
+
enumerable: false
|
|
11539
|
+
});
|
|
11540
|
+
}
|
|
11541
|
+
}
|
|
11542
|
+
}
|
|
11543
|
+
|
|
11544
|
+
if (includeNonEnumerable) {
|
|
11545
|
+
var allPropertyNames = Object.getOwnPropertyNames(parent);
|
|
11546
|
+
for (var i = 0; i < allPropertyNames.length; i++) {
|
|
11547
|
+
var propertyName = allPropertyNames[i];
|
|
11548
|
+
var descriptor = Object.getOwnPropertyDescriptor(parent, propertyName);
|
|
11549
|
+
if (descriptor && descriptor.enumerable) {
|
|
11550
|
+
continue;
|
|
11551
|
+
}
|
|
11552
|
+
child[propertyName] = _clone(parent[propertyName], depth - 1);
|
|
11553
|
+
Object.defineProperty(child, propertyName, {
|
|
11554
|
+
enumerable: false
|
|
11555
|
+
});
|
|
11556
|
+
}
|
|
11557
|
+
}
|
|
11558
|
+
|
|
11559
|
+
return child;
|
|
11560
|
+
}
|
|
11561
|
+
|
|
11562
|
+
return _clone(parent, depth);
|
|
11563
|
+
}
|
|
11564
|
+
|
|
11565
|
+
/**
|
|
11566
|
+
* Simple flat clone using prototype, accepts only objects, usefull for property
|
|
11567
|
+
* override on FLAT configuration object (no nested props).
|
|
11568
|
+
*
|
|
11569
|
+
* USE WITH CAUTION! This may not behave as you wish if you do not know how this
|
|
11570
|
+
* works.
|
|
11571
|
+
*/
|
|
11572
|
+
clone.clonePrototype = function clonePrototype(parent) {
|
|
11573
|
+
if (parent === null)
|
|
11574
|
+
return null;
|
|
11575
|
+
|
|
11576
|
+
var c = function () {};
|
|
11577
|
+
c.prototype = parent;
|
|
11578
|
+
return new c();
|
|
11579
|
+
};
|
|
11580
|
+
|
|
11581
|
+
// private utility functions
|
|
11582
|
+
|
|
11583
|
+
function __objToStr(o) {
|
|
11584
|
+
return Object.prototype.toString.call(o);
|
|
11585
|
+
}
|
|
11586
|
+
clone.__objToStr = __objToStr;
|
|
11587
|
+
|
|
11588
|
+
function __isDate(o) {
|
|
11589
|
+
return typeof o === 'object' && __objToStr(o) === '[object Date]';
|
|
11590
|
+
}
|
|
11591
|
+
clone.__isDate = __isDate;
|
|
11592
|
+
|
|
11593
|
+
function __isArray(o) {
|
|
11594
|
+
return typeof o === 'object' && __objToStr(o) === '[object Array]';
|
|
11595
|
+
}
|
|
11596
|
+
clone.__isArray = __isArray;
|
|
11597
|
+
|
|
11598
|
+
function __isRegExp(o) {
|
|
11599
|
+
return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
|
|
11600
|
+
}
|
|
11601
|
+
clone.__isRegExp = __isRegExp;
|
|
11602
|
+
|
|
11603
|
+
function __getRegExpFlags(re) {
|
|
11604
|
+
var flags = '';
|
|
11605
|
+
if (re.global) flags += 'g';
|
|
11606
|
+
if (re.ignoreCase) flags += 'i';
|
|
11607
|
+
if (re.multiline) flags += 'm';
|
|
11608
|
+
return flags;
|
|
11609
|
+
}
|
|
11610
|
+
clone.__getRegExpFlags = __getRegExpFlags;
|
|
11611
|
+
|
|
11612
|
+
return clone;
|
|
11613
|
+
})();
|
|
11614
|
+
|
|
11615
|
+
if (module.exports) {
|
|
11616
|
+
module.exports = clone;
|
|
11617
|
+
}
|
|
11618
|
+
} (clone));
|
|
11619
|
+
return clone.exports;
|
|
11620
|
+
}
|
|
11621
|
+
|
|
11622
|
+
var events = {exports: {}};
|
|
11623
|
+
|
|
11624
|
+
var hasRequiredEvents;
|
|
11625
|
+
|
|
11626
|
+
function requireEvents () {
|
|
11627
|
+
if (hasRequiredEvents) return events.exports;
|
|
11628
|
+
hasRequiredEvents = 1;
|
|
11629
|
+
|
|
11630
|
+
var R = typeof Reflect === 'object' ? Reflect : null;
|
|
11631
|
+
var ReflectApply = R && typeof R.apply === 'function'
|
|
11632
|
+
? R.apply
|
|
11633
|
+
: function ReflectApply(target, receiver, args) {
|
|
11634
|
+
return Function.prototype.apply.call(target, receiver, args);
|
|
11635
|
+
};
|
|
11636
|
+
|
|
11637
|
+
var ReflectOwnKeys;
|
|
11638
|
+
if (R && typeof R.ownKeys === 'function') {
|
|
11639
|
+
ReflectOwnKeys = R.ownKeys;
|
|
11640
|
+
} else if (Object.getOwnPropertySymbols) {
|
|
11641
|
+
ReflectOwnKeys = function ReflectOwnKeys(target) {
|
|
11642
|
+
return Object.getOwnPropertyNames(target)
|
|
11643
|
+
.concat(Object.getOwnPropertySymbols(target));
|
|
11644
|
+
};
|
|
11645
|
+
} else {
|
|
11646
|
+
ReflectOwnKeys = function ReflectOwnKeys(target) {
|
|
11647
|
+
return Object.getOwnPropertyNames(target);
|
|
11648
|
+
};
|
|
11649
|
+
}
|
|
11650
|
+
|
|
11651
|
+
function ProcessEmitWarning(warning) {
|
|
11652
|
+
if (console && console.warn) console.warn(warning);
|
|
11653
|
+
}
|
|
11654
|
+
|
|
11655
|
+
var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) {
|
|
11656
|
+
return value !== value;
|
|
11657
|
+
};
|
|
11658
|
+
|
|
11659
|
+
function EventEmitter() {
|
|
11660
|
+
EventEmitter.init.call(this);
|
|
11661
|
+
}
|
|
11662
|
+
events.exports = EventEmitter;
|
|
11663
|
+
events.exports.once = once;
|
|
11664
|
+
|
|
11665
|
+
// Backwards-compat with node 0.10.x
|
|
11666
|
+
EventEmitter.EventEmitter = EventEmitter;
|
|
11667
|
+
|
|
11668
|
+
EventEmitter.prototype._events = undefined;
|
|
11669
|
+
EventEmitter.prototype._eventsCount = 0;
|
|
11670
|
+
EventEmitter.prototype._maxListeners = undefined;
|
|
11671
|
+
|
|
11672
|
+
// By default EventEmitters will print a warning if more than 10 listeners are
|
|
11673
|
+
// added to it. This is a useful default which helps finding memory leaks.
|
|
11674
|
+
var defaultMaxListeners = 10;
|
|
11675
|
+
|
|
11676
|
+
function checkListener(listener) {
|
|
11677
|
+
if (typeof listener !== 'function') {
|
|
11678
|
+
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
|
|
11679
|
+
}
|
|
11680
|
+
}
|
|
11681
|
+
|
|
11682
|
+
Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
|
|
11683
|
+
enumerable: true,
|
|
11684
|
+
get: function() {
|
|
11685
|
+
return defaultMaxListeners;
|
|
11686
|
+
},
|
|
11687
|
+
set: function(arg) {
|
|
11688
|
+
if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
|
|
11689
|
+
throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
|
|
11690
|
+
}
|
|
11691
|
+
defaultMaxListeners = arg;
|
|
11692
|
+
}
|
|
11693
|
+
});
|
|
11694
|
+
|
|
11695
|
+
EventEmitter.init = function() {
|
|
11696
|
+
|
|
11697
|
+
if (this._events === undefined ||
|
|
11698
|
+
this._events === Object.getPrototypeOf(this)._events) {
|
|
11699
|
+
this._events = Object.create(null);
|
|
11700
|
+
this._eventsCount = 0;
|
|
11701
|
+
}
|
|
11702
|
+
|
|
11703
|
+
this._maxListeners = this._maxListeners || undefined;
|
|
11704
|
+
};
|
|
11705
|
+
|
|
11706
|
+
// Obviously not all Emitters should be limited to 10. This function allows
|
|
11707
|
+
// that to be increased. Set to zero for unlimited.
|
|
11708
|
+
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
|
|
11709
|
+
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
|
|
11710
|
+
throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
|
|
11711
|
+
}
|
|
11712
|
+
this._maxListeners = n;
|
|
11713
|
+
return this;
|
|
11714
|
+
};
|
|
11715
|
+
|
|
11716
|
+
function _getMaxListeners(that) {
|
|
11717
|
+
if (that._maxListeners === undefined)
|
|
11718
|
+
return EventEmitter.defaultMaxListeners;
|
|
11719
|
+
return that._maxListeners;
|
|
11720
|
+
}
|
|
11721
|
+
|
|
11722
|
+
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
|
|
11723
|
+
return _getMaxListeners(this);
|
|
11724
|
+
};
|
|
11725
|
+
|
|
11726
|
+
EventEmitter.prototype.emit = function emit(type) {
|
|
11727
|
+
var args = [];
|
|
11728
|
+
for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
|
|
11729
|
+
var doError = (type === 'error');
|
|
11730
|
+
|
|
11731
|
+
var events = this._events;
|
|
11732
|
+
if (events !== undefined)
|
|
11733
|
+
doError = (doError && events.error === undefined);
|
|
11734
|
+
else if (!doError)
|
|
11735
|
+
return false;
|
|
11736
|
+
|
|
11737
|
+
// If there is no 'error' event listener then throw.
|
|
11738
|
+
if (doError) {
|
|
11739
|
+
var er;
|
|
11740
|
+
if (args.length > 0)
|
|
11741
|
+
er = args[0];
|
|
11742
|
+
if (er instanceof Error) {
|
|
11743
|
+
// Note: The comments on the `throw` lines are intentional, they show
|
|
11744
|
+
// up in Node's output if this results in an unhandled exception.
|
|
11745
|
+
throw er; // Unhandled 'error' event
|
|
11746
|
+
}
|
|
11747
|
+
// At least give some kind of context to the user
|
|
11748
|
+
var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
|
|
11749
|
+
err.context = er;
|
|
11750
|
+
throw err; // Unhandled 'error' event
|
|
11751
|
+
}
|
|
11752
|
+
|
|
11753
|
+
var handler = events[type];
|
|
11754
|
+
|
|
11755
|
+
if (handler === undefined)
|
|
11756
|
+
return false;
|
|
11757
|
+
|
|
11758
|
+
if (typeof handler === 'function') {
|
|
11759
|
+
ReflectApply(handler, this, args);
|
|
11760
|
+
} else {
|
|
11761
|
+
var len = handler.length;
|
|
11762
|
+
var listeners = arrayClone(handler, len);
|
|
11763
|
+
for (var i = 0; i < len; ++i)
|
|
11764
|
+
ReflectApply(listeners[i], this, args);
|
|
11765
|
+
}
|
|
11766
|
+
|
|
11767
|
+
return true;
|
|
11768
|
+
};
|
|
11769
|
+
|
|
11770
|
+
function _addListener(target, type, listener, prepend) {
|
|
11771
|
+
var m;
|
|
11772
|
+
var events;
|
|
11773
|
+
var existing;
|
|
11774
|
+
|
|
11775
|
+
checkListener(listener);
|
|
11776
|
+
|
|
11777
|
+
events = target._events;
|
|
11778
|
+
if (events === undefined) {
|
|
11779
|
+
events = target._events = Object.create(null);
|
|
11780
|
+
target._eventsCount = 0;
|
|
11781
|
+
} else {
|
|
11782
|
+
// To avoid recursion in the case that type === "newListener"! Before
|
|
11783
|
+
// adding it to the listeners, first emit "newListener".
|
|
11784
|
+
if (events.newListener !== undefined) {
|
|
11785
|
+
target.emit('newListener', type,
|
|
11786
|
+
listener.listener ? listener.listener : listener);
|
|
11787
|
+
|
|
11788
|
+
// Re-assign `events` because a newListener handler could have caused the
|
|
11789
|
+
// this._events to be assigned to a new object
|
|
11790
|
+
events = target._events;
|
|
11791
|
+
}
|
|
11792
|
+
existing = events[type];
|
|
11793
|
+
}
|
|
11794
|
+
|
|
11795
|
+
if (existing === undefined) {
|
|
11796
|
+
// Optimize the case of one listener. Don't need the extra array object.
|
|
11797
|
+
existing = events[type] = listener;
|
|
11798
|
+
++target._eventsCount;
|
|
11799
|
+
} else {
|
|
11800
|
+
if (typeof existing === 'function') {
|
|
11801
|
+
// Adding the second element, need to change to array.
|
|
11802
|
+
existing = events[type] =
|
|
11803
|
+
prepend ? [listener, existing] : [existing, listener];
|
|
11804
|
+
// If we've already got an array, just append.
|
|
11805
|
+
} else if (prepend) {
|
|
11806
|
+
existing.unshift(listener);
|
|
11807
|
+
} else {
|
|
11808
|
+
existing.push(listener);
|
|
11809
|
+
}
|
|
11810
|
+
|
|
11811
|
+
// Check for listener leak
|
|
11812
|
+
m = _getMaxListeners(target);
|
|
11813
|
+
if (m > 0 && existing.length > m && !existing.warned) {
|
|
11814
|
+
existing.warned = true;
|
|
11815
|
+
// No error code for this since it is a Warning
|
|
11816
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
11817
|
+
var w = new Error('Possible EventEmitter memory leak detected. ' +
|
|
11818
|
+
existing.length + ' ' + String(type) + ' listeners ' +
|
|
11819
|
+
'added. Use emitter.setMaxListeners() to ' +
|
|
11820
|
+
'increase limit');
|
|
11821
|
+
w.name = 'MaxListenersExceededWarning';
|
|
11822
|
+
w.emitter = target;
|
|
11823
|
+
w.type = type;
|
|
11824
|
+
w.count = existing.length;
|
|
11825
|
+
ProcessEmitWarning(w);
|
|
11826
|
+
}
|
|
11827
|
+
}
|
|
11828
|
+
|
|
11829
|
+
return target;
|
|
11830
|
+
}
|
|
11831
|
+
|
|
11832
|
+
EventEmitter.prototype.addListener = function addListener(type, listener) {
|
|
11833
|
+
return _addListener(this, type, listener, false);
|
|
11834
|
+
};
|
|
11835
|
+
|
|
11836
|
+
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
|
11837
|
+
|
|
11838
|
+
EventEmitter.prototype.prependListener =
|
|
11839
|
+
function prependListener(type, listener) {
|
|
11840
|
+
return _addListener(this, type, listener, true);
|
|
11841
|
+
};
|
|
11842
|
+
|
|
11843
|
+
function onceWrapper() {
|
|
11844
|
+
if (!this.fired) {
|
|
11845
|
+
this.target.removeListener(this.type, this.wrapFn);
|
|
11846
|
+
this.fired = true;
|
|
11847
|
+
if (arguments.length === 0)
|
|
11848
|
+
return this.listener.call(this.target);
|
|
11849
|
+
return this.listener.apply(this.target, arguments);
|
|
11850
|
+
}
|
|
11851
|
+
}
|
|
11852
|
+
|
|
11853
|
+
function _onceWrap(target, type, listener) {
|
|
11854
|
+
var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
|
|
11855
|
+
var wrapped = onceWrapper.bind(state);
|
|
11856
|
+
wrapped.listener = listener;
|
|
11857
|
+
state.wrapFn = wrapped;
|
|
11858
|
+
return wrapped;
|
|
11859
|
+
}
|
|
11860
|
+
|
|
11861
|
+
EventEmitter.prototype.once = function once(type, listener) {
|
|
11862
|
+
checkListener(listener);
|
|
11863
|
+
this.on(type, _onceWrap(this, type, listener));
|
|
11864
|
+
return this;
|
|
11865
|
+
};
|
|
11866
|
+
|
|
11867
|
+
EventEmitter.prototype.prependOnceListener =
|
|
11868
|
+
function prependOnceListener(type, listener) {
|
|
11869
|
+
checkListener(listener);
|
|
11870
|
+
this.prependListener(type, _onceWrap(this, type, listener));
|
|
11871
|
+
return this;
|
|
11872
|
+
};
|
|
11873
|
+
|
|
11874
|
+
// Emits a 'removeListener' event if and only if the listener was removed.
|
|
11875
|
+
EventEmitter.prototype.removeListener =
|
|
11876
|
+
function removeListener(type, listener) {
|
|
11877
|
+
var list, events, position, i, originalListener;
|
|
11878
|
+
|
|
11879
|
+
checkListener(listener);
|
|
11880
|
+
|
|
11881
|
+
events = this._events;
|
|
11882
|
+
if (events === undefined)
|
|
11883
|
+
return this;
|
|
11884
|
+
|
|
11885
|
+
list = events[type];
|
|
11886
|
+
if (list === undefined)
|
|
11887
|
+
return this;
|
|
11888
|
+
|
|
11889
|
+
if (list === listener || list.listener === listener) {
|
|
11890
|
+
if (--this._eventsCount === 0)
|
|
11891
|
+
this._events = Object.create(null);
|
|
11892
|
+
else {
|
|
11893
|
+
delete events[type];
|
|
11894
|
+
if (events.removeListener)
|
|
11895
|
+
this.emit('removeListener', type, list.listener || listener);
|
|
11896
|
+
}
|
|
11897
|
+
} else if (typeof list !== 'function') {
|
|
11898
|
+
position = -1;
|
|
11899
|
+
|
|
11900
|
+
for (i = list.length - 1; i >= 0; i--) {
|
|
11901
|
+
if (list[i] === listener || list[i].listener === listener) {
|
|
11902
|
+
originalListener = list[i].listener;
|
|
11903
|
+
position = i;
|
|
11904
|
+
break;
|
|
11905
|
+
}
|
|
11906
|
+
}
|
|
11907
|
+
|
|
11908
|
+
if (position < 0)
|
|
11909
|
+
return this;
|
|
11910
|
+
|
|
11911
|
+
if (position === 0)
|
|
11912
|
+
list.shift();
|
|
11913
|
+
else {
|
|
11914
|
+
spliceOne(list, position);
|
|
11915
|
+
}
|
|
11916
|
+
|
|
11917
|
+
if (list.length === 1)
|
|
11918
|
+
events[type] = list[0];
|
|
11919
|
+
|
|
11920
|
+
if (events.removeListener !== undefined)
|
|
11921
|
+
this.emit('removeListener', type, originalListener || listener);
|
|
11922
|
+
}
|
|
11923
|
+
|
|
11924
|
+
return this;
|
|
11925
|
+
};
|
|
11926
|
+
|
|
11927
|
+
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
11928
|
+
|
|
11929
|
+
EventEmitter.prototype.removeAllListeners =
|
|
11930
|
+
function removeAllListeners(type) {
|
|
11931
|
+
var listeners, events, i;
|
|
11932
|
+
|
|
11933
|
+
events = this._events;
|
|
11934
|
+
if (events === undefined)
|
|
11935
|
+
return this;
|
|
11936
|
+
|
|
11937
|
+
// not listening for removeListener, no need to emit
|
|
11938
|
+
if (events.removeListener === undefined) {
|
|
11939
|
+
if (arguments.length === 0) {
|
|
11940
|
+
this._events = Object.create(null);
|
|
11941
|
+
this._eventsCount = 0;
|
|
11942
|
+
} else if (events[type] !== undefined) {
|
|
11943
|
+
if (--this._eventsCount === 0)
|
|
11944
|
+
this._events = Object.create(null);
|
|
11945
|
+
else
|
|
11946
|
+
delete events[type];
|
|
11947
|
+
}
|
|
11948
|
+
return this;
|
|
11949
|
+
}
|
|
11950
|
+
|
|
11951
|
+
// emit removeListener for all listeners on all events
|
|
11952
|
+
if (arguments.length === 0) {
|
|
11953
|
+
var keys = Object.keys(events);
|
|
11954
|
+
var key;
|
|
11955
|
+
for (i = 0; i < keys.length; ++i) {
|
|
11956
|
+
key = keys[i];
|
|
11957
|
+
if (key === 'removeListener') continue;
|
|
11958
|
+
this.removeAllListeners(key);
|
|
11959
|
+
}
|
|
11960
|
+
this.removeAllListeners('removeListener');
|
|
11961
|
+
this._events = Object.create(null);
|
|
11962
|
+
this._eventsCount = 0;
|
|
11963
|
+
return this;
|
|
11964
|
+
}
|
|
11965
|
+
|
|
11966
|
+
listeners = events[type];
|
|
11967
|
+
|
|
11968
|
+
if (typeof listeners === 'function') {
|
|
11969
|
+
this.removeListener(type, listeners);
|
|
11970
|
+
} else if (listeners !== undefined) {
|
|
11971
|
+
// LIFO order
|
|
11972
|
+
for (i = listeners.length - 1; i >= 0; i--) {
|
|
11973
|
+
this.removeListener(type, listeners[i]);
|
|
11974
|
+
}
|
|
11975
|
+
}
|
|
11976
|
+
|
|
11977
|
+
return this;
|
|
11978
|
+
};
|
|
11979
|
+
|
|
11980
|
+
function _listeners(target, type, unwrap) {
|
|
11981
|
+
var events = target._events;
|
|
11982
|
+
|
|
11983
|
+
if (events === undefined)
|
|
11984
|
+
return [];
|
|
11985
|
+
|
|
11986
|
+
var evlistener = events[type];
|
|
11987
|
+
if (evlistener === undefined)
|
|
11988
|
+
return [];
|
|
11989
|
+
|
|
11990
|
+
if (typeof evlistener === 'function')
|
|
11991
|
+
return unwrap ? [evlistener.listener || evlistener] : [evlistener];
|
|
11992
|
+
|
|
11993
|
+
return unwrap ?
|
|
11994
|
+
unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
|
|
11995
|
+
}
|
|
11996
|
+
|
|
11997
|
+
EventEmitter.prototype.listeners = function listeners(type) {
|
|
11998
|
+
return _listeners(this, type, true);
|
|
11999
|
+
};
|
|
12000
|
+
|
|
12001
|
+
EventEmitter.prototype.rawListeners = function rawListeners(type) {
|
|
12002
|
+
return _listeners(this, type, false);
|
|
12003
|
+
};
|
|
12004
|
+
|
|
12005
|
+
EventEmitter.listenerCount = function(emitter, type) {
|
|
12006
|
+
if (typeof emitter.listenerCount === 'function') {
|
|
12007
|
+
return emitter.listenerCount(type);
|
|
12008
|
+
} else {
|
|
12009
|
+
return listenerCount.call(emitter, type);
|
|
12010
|
+
}
|
|
12011
|
+
};
|
|
12012
|
+
|
|
12013
|
+
EventEmitter.prototype.listenerCount = listenerCount;
|
|
12014
|
+
function listenerCount(type) {
|
|
12015
|
+
var events = this._events;
|
|
12016
|
+
|
|
12017
|
+
if (events !== undefined) {
|
|
12018
|
+
var evlistener = events[type];
|
|
12019
|
+
|
|
12020
|
+
if (typeof evlistener === 'function') {
|
|
12021
|
+
return 1;
|
|
12022
|
+
} else if (evlistener !== undefined) {
|
|
12023
|
+
return evlistener.length;
|
|
12024
|
+
}
|
|
12025
|
+
}
|
|
12026
|
+
|
|
12027
|
+
return 0;
|
|
12028
|
+
}
|
|
12029
|
+
|
|
12030
|
+
EventEmitter.prototype.eventNames = function eventNames() {
|
|
12031
|
+
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
|
|
12032
|
+
};
|
|
12033
|
+
|
|
12034
|
+
function arrayClone(arr, n) {
|
|
12035
|
+
var copy = new Array(n);
|
|
12036
|
+
for (var i = 0; i < n; ++i)
|
|
12037
|
+
copy[i] = arr[i];
|
|
12038
|
+
return copy;
|
|
12039
|
+
}
|
|
12040
|
+
|
|
12041
|
+
function spliceOne(list, index) {
|
|
12042
|
+
for (; index + 1 < list.length; index++)
|
|
12043
|
+
list[index] = list[index + 1];
|
|
12044
|
+
list.pop();
|
|
12045
|
+
}
|
|
12046
|
+
|
|
12047
|
+
function unwrapListeners(arr) {
|
|
12048
|
+
var ret = new Array(arr.length);
|
|
12049
|
+
for (var i = 0; i < ret.length; ++i) {
|
|
12050
|
+
ret[i] = arr[i].listener || arr[i];
|
|
12051
|
+
}
|
|
12052
|
+
return ret;
|
|
12053
|
+
}
|
|
12054
|
+
|
|
12055
|
+
function once(emitter, name) {
|
|
12056
|
+
return new Promise(function (resolve, reject) {
|
|
12057
|
+
function errorListener(err) {
|
|
12058
|
+
emitter.removeListener(name, resolver);
|
|
12059
|
+
reject(err);
|
|
12060
|
+
}
|
|
12061
|
+
|
|
12062
|
+
function resolver() {
|
|
12063
|
+
if (typeof emitter.removeListener === 'function') {
|
|
12064
|
+
emitter.removeListener('error', errorListener);
|
|
12065
|
+
}
|
|
12066
|
+
resolve([].slice.call(arguments));
|
|
12067
|
+
}
|
|
12068
|
+
eventTargetAgnosticAddListener(emitter, name, resolver, { once: true });
|
|
12069
|
+
if (name !== 'error') {
|
|
12070
|
+
addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true });
|
|
12071
|
+
}
|
|
12072
|
+
});
|
|
12073
|
+
}
|
|
12074
|
+
|
|
12075
|
+
function addErrorHandlerIfEventEmitter(emitter, handler, flags) {
|
|
12076
|
+
if (typeof emitter.on === 'function') {
|
|
12077
|
+
eventTargetAgnosticAddListener(emitter, 'error', handler, flags);
|
|
12078
|
+
}
|
|
12079
|
+
}
|
|
12080
|
+
|
|
12081
|
+
function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
|
|
12082
|
+
if (typeof emitter.on === 'function') {
|
|
12083
|
+
if (flags.once) {
|
|
12084
|
+
emitter.once(name, listener);
|
|
12085
|
+
} else {
|
|
12086
|
+
emitter.on(name, listener);
|
|
12087
|
+
}
|
|
12088
|
+
} else if (typeof emitter.addEventListener === 'function') {
|
|
12089
|
+
// EventTarget does not have `error` event semantics like Node
|
|
12090
|
+
// EventEmitters, we do not listen for `error` events here.
|
|
12091
|
+
emitter.addEventListener(name, function wrapListener(arg) {
|
|
12092
|
+
// IE does not have builtin `{ once: true }` support so we
|
|
12093
|
+
// have to do it manually.
|
|
12094
|
+
if (flags.once) {
|
|
12095
|
+
emitter.removeEventListener(name, wrapListener);
|
|
12096
|
+
}
|
|
12097
|
+
listener(arg);
|
|
12098
|
+
});
|
|
12099
|
+
} else {
|
|
12100
|
+
throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter);
|
|
12101
|
+
}
|
|
12102
|
+
}
|
|
12103
|
+
return events.exports;
|
|
12104
|
+
}
|
|
12105
|
+
|
|
12106
|
+
/*
|
|
12107
|
+
* node-cache 5.1.2 ( 2020-07-01 )
|
|
12108
|
+
* https://github.com/node-cache/node-cache
|
|
12109
|
+
*
|
|
12110
|
+
* Released under the MIT license
|
|
12111
|
+
* https://github.com/node-cache/node-cache/blob/master/LICENSE
|
|
12112
|
+
*
|
|
12113
|
+
* Maintained by ( )
|
|
12114
|
+
*/
|
|
12115
|
+
|
|
12116
|
+
var hasRequiredNode_cache;
|
|
12117
|
+
|
|
12118
|
+
function requireNode_cache () {
|
|
12119
|
+
if (hasRequiredNode_cache) return node_cache.exports;
|
|
12120
|
+
hasRequiredNode_cache = 1;
|
|
12121
|
+
(function() {
|
|
12122
|
+
var EventEmitter, clone,
|
|
12123
|
+
splice = [].splice,
|
|
12124
|
+
boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } },
|
|
12125
|
+
indexOf = [].indexOf;
|
|
12126
|
+
|
|
12127
|
+
clone = requireClone();
|
|
12128
|
+
|
|
12129
|
+
EventEmitter = requireEvents().EventEmitter;
|
|
12130
|
+
|
|
12131
|
+
// generate superclass
|
|
12132
|
+
node_cache.exports = (function() {
|
|
12133
|
+
class NodeCache extends EventEmitter {
|
|
12134
|
+
constructor(options = {}) {
|
|
12135
|
+
super();
|
|
12136
|
+
// ## get
|
|
12137
|
+
|
|
12138
|
+
// get a cached key and change the stats
|
|
12139
|
+
|
|
12140
|
+
// **Parameters:**
|
|
12141
|
+
|
|
12142
|
+
// * `key` ( String | Number ): cache key
|
|
12143
|
+
|
|
12144
|
+
// **Example:**
|
|
12145
|
+
|
|
12146
|
+
// myCache.get "myKey", ( err, val )
|
|
12147
|
+
|
|
12148
|
+
this.get = this.get.bind(this);
|
|
12149
|
+
// ## mget
|
|
12150
|
+
|
|
12151
|
+
// get multiple cached keys at once and change the stats
|
|
12152
|
+
|
|
12153
|
+
// **Parameters:**
|
|
12154
|
+
|
|
12155
|
+
// * `keys` ( String|Number[] ): an array of keys
|
|
12156
|
+
|
|
12157
|
+
// **Example:**
|
|
12158
|
+
|
|
12159
|
+
// myCache.mget [ "foo", "bar" ]
|
|
12160
|
+
|
|
12161
|
+
this.mget = this.mget.bind(this);
|
|
12162
|
+
// ## set
|
|
12163
|
+
|
|
12164
|
+
// set a cached key and change the stats
|
|
12165
|
+
|
|
12166
|
+
// **Parameters:**
|
|
12167
|
+
|
|
12168
|
+
// * `key` ( String | Number ): cache key
|
|
12169
|
+
// * `value` ( Any ): A element to cache. If the option `option.forceString` is `true` the module trys to translate it to a serialized JSON
|
|
12170
|
+
// * `[ ttl ]` ( Number | String ): ( optional ) The time to live in seconds.
|
|
12171
|
+
|
|
12172
|
+
// **Example:**
|
|
12173
|
+
|
|
12174
|
+
// myCache.set "myKey", "my_String Value"
|
|
12175
|
+
|
|
12176
|
+
// myCache.set "myKey", "my_String Value", 10
|
|
12177
|
+
|
|
12178
|
+
this.set = this.set.bind(this);
|
|
12179
|
+
|
|
12180
|
+
// ## mset
|
|
12181
|
+
|
|
12182
|
+
// set multiple keys at once
|
|
12183
|
+
|
|
12184
|
+
// **Parameters:**
|
|
12185
|
+
|
|
12186
|
+
// * `keyValueSet` ( Object[] ): an array of object which includes key,value and ttl
|
|
12187
|
+
|
|
12188
|
+
// **Example:**
|
|
12189
|
+
|
|
12190
|
+
// myCache.mset(
|
|
12191
|
+
// [
|
|
12192
|
+
// {
|
|
12193
|
+
// key: "myKey",
|
|
12194
|
+
// val: "myValue",
|
|
12195
|
+
// ttl: [ttl in seconds]
|
|
12196
|
+
// }
|
|
12197
|
+
// ])
|
|
12198
|
+
|
|
12199
|
+
this.mset = this.mset.bind(this);
|
|
12200
|
+
// ## del
|
|
12201
|
+
|
|
12202
|
+
// remove keys
|
|
12203
|
+
|
|
12204
|
+
// **Parameters:**
|
|
12205
|
+
|
|
12206
|
+
// * `keys` ( String | Number | String|Number[] ): cache key to delete or a array of cache keys
|
|
12207
|
+
|
|
12208
|
+
// **Return**
|
|
12209
|
+
|
|
12210
|
+
// ( Number ): Number of deleted keys
|
|
12211
|
+
|
|
12212
|
+
// **Example:**
|
|
12213
|
+
|
|
12214
|
+
// myCache.del( "myKey" )
|
|
12215
|
+
|
|
12216
|
+
this.del = this.del.bind(this);
|
|
12217
|
+
// ## take
|
|
12218
|
+
|
|
12219
|
+
// get the cached value and remove the key from the cache.
|
|
12220
|
+
// Equivalent to calling `get(key)` + `del(key)`.
|
|
12221
|
+
// Useful for implementing `single use` mechanism such as OTP, where once a value is read it will become obsolete.
|
|
12222
|
+
|
|
12223
|
+
// **Parameters:**
|
|
12224
|
+
|
|
12225
|
+
// * `key` ( String | Number ): cache key
|
|
12226
|
+
|
|
12227
|
+
// **Example:**
|
|
12228
|
+
|
|
12229
|
+
// myCache.take "myKey", ( err, val )
|
|
12230
|
+
|
|
12231
|
+
this.take = this.take.bind(this);
|
|
12232
|
+
// ## ttl
|
|
12233
|
+
|
|
12234
|
+
// reset or redefine the ttl of a key. `ttl` = 0 means infinite lifetime.
|
|
12235
|
+
// If `ttl` is not passed the default ttl is used.
|
|
12236
|
+
// If `ttl` < 0 the key will be deleted.
|
|
12237
|
+
|
|
12238
|
+
// **Parameters:**
|
|
12239
|
+
|
|
12240
|
+
// * `key` ( String | Number ): cache key to reset the ttl value
|
|
12241
|
+
// * `ttl` ( Number ): ( optional -> options.stdTTL || 0 ) The time to live in seconds
|
|
12242
|
+
|
|
12243
|
+
// **Return**
|
|
12244
|
+
|
|
12245
|
+
// ( Boolen ): key found and ttl set
|
|
12246
|
+
|
|
12247
|
+
// **Example:**
|
|
12248
|
+
|
|
12249
|
+
// myCache.ttl( "myKey" ) // will set ttl to default ttl
|
|
12250
|
+
|
|
12251
|
+
// myCache.ttl( "myKey", 1000 )
|
|
12252
|
+
|
|
12253
|
+
this.ttl = this.ttl.bind(this);
|
|
12254
|
+
// ## getTtl
|
|
12255
|
+
|
|
12256
|
+
// receive the ttl of a key.
|
|
12257
|
+
|
|
12258
|
+
// **Parameters:**
|
|
12259
|
+
|
|
12260
|
+
// * `key` ( String | Number ): cache key to check the ttl value
|
|
12261
|
+
|
|
12262
|
+
// **Return**
|
|
12263
|
+
|
|
12264
|
+
// ( Number|undefined ): The timestamp in ms when the key will expire, 0 if it will never expire or undefined if it not exists
|
|
12265
|
+
|
|
12266
|
+
// **Example:**
|
|
12267
|
+
|
|
12268
|
+
// myCache.getTtl( "myKey" )
|
|
12269
|
+
|
|
12270
|
+
this.getTtl = this.getTtl.bind(this);
|
|
12271
|
+
// ## keys
|
|
12272
|
+
|
|
12273
|
+
// list all keys within this cache
|
|
12274
|
+
|
|
12275
|
+
// **Return**
|
|
12276
|
+
|
|
12277
|
+
// ( Array ): An array of all keys
|
|
12278
|
+
|
|
12279
|
+
// **Example:**
|
|
12280
|
+
|
|
12281
|
+
// _keys = myCache.keys()
|
|
12282
|
+
|
|
12283
|
+
// # [ "foo", "bar", "fizz", "buzz", "anotherKeys" ]
|
|
12284
|
+
|
|
12285
|
+
this.keys = this.keys.bind(this);
|
|
12286
|
+
// ## has
|
|
12287
|
+
|
|
12288
|
+
// Check if a key is cached
|
|
12289
|
+
|
|
12290
|
+
// **Parameters:**
|
|
12291
|
+
|
|
12292
|
+
// * `key` ( String | Number ): cache key to check the ttl value
|
|
12293
|
+
|
|
12294
|
+
// **Return**
|
|
12295
|
+
|
|
12296
|
+
// ( Boolean ): A boolean that indicates if the key is cached
|
|
12297
|
+
|
|
12298
|
+
// **Example:**
|
|
12299
|
+
|
|
12300
|
+
// _exists = myCache.has('myKey')
|
|
12301
|
+
|
|
12302
|
+
// # true
|
|
12303
|
+
|
|
12304
|
+
this.has = this.has.bind(this);
|
|
12305
|
+
// ## getStats
|
|
12306
|
+
|
|
12307
|
+
// get the stats
|
|
12308
|
+
|
|
12309
|
+
// **Parameters:**
|
|
12310
|
+
|
|
12311
|
+
// -
|
|
12312
|
+
|
|
12313
|
+
// **Return**
|
|
12314
|
+
|
|
12315
|
+
// ( Object ): Stats data
|
|
12316
|
+
|
|
12317
|
+
// **Example:**
|
|
12318
|
+
|
|
12319
|
+
// myCache.getStats()
|
|
12320
|
+
// # {
|
|
12321
|
+
// # hits: 0,
|
|
12322
|
+
// # misses: 0,
|
|
12323
|
+
// # keys: 0,
|
|
12324
|
+
// # ksize: 0,
|
|
12325
|
+
// # vsize: 0
|
|
12326
|
+
// # }
|
|
12327
|
+
|
|
12328
|
+
this.getStats = this.getStats.bind(this);
|
|
12329
|
+
// ## flushAll
|
|
12330
|
+
|
|
12331
|
+
// flush the whole data and reset the stats
|
|
12332
|
+
|
|
12333
|
+
// **Example:**
|
|
12334
|
+
|
|
12335
|
+
// myCache.flushAll()
|
|
12336
|
+
|
|
12337
|
+
// myCache.getStats()
|
|
12338
|
+
// # {
|
|
12339
|
+
// # hits: 0,
|
|
12340
|
+
// # misses: 0,
|
|
12341
|
+
// # keys: 0,
|
|
12342
|
+
// # ksize: 0,
|
|
12343
|
+
// # vsize: 0
|
|
12344
|
+
// # }
|
|
12345
|
+
|
|
12346
|
+
this.flushAll = this.flushAll.bind(this);
|
|
12347
|
+
|
|
12348
|
+
// ## flushStats
|
|
12349
|
+
|
|
12350
|
+
// flush the stats and reset all counters to 0
|
|
12351
|
+
|
|
12352
|
+
// **Example:**
|
|
12353
|
+
|
|
12354
|
+
// myCache.flushStats()
|
|
12355
|
+
|
|
12356
|
+
// myCache.getStats()
|
|
12357
|
+
// # {
|
|
12358
|
+
// # hits: 0,
|
|
12359
|
+
// # misses: 0,
|
|
12360
|
+
// # keys: 0,
|
|
12361
|
+
// # ksize: 0,
|
|
12362
|
+
// # vsize: 0
|
|
12363
|
+
// # }
|
|
12364
|
+
|
|
12365
|
+
this.flushStats = this.flushStats.bind(this);
|
|
12366
|
+
// ## close
|
|
12367
|
+
|
|
12368
|
+
// This will clear the interval timeout which is set on checkperiod option.
|
|
12369
|
+
|
|
12370
|
+
// **Example:**
|
|
12371
|
+
|
|
12372
|
+
// myCache.close()
|
|
12373
|
+
|
|
12374
|
+
this.close = this.close.bind(this);
|
|
12375
|
+
// ## _checkData
|
|
12376
|
+
|
|
12377
|
+
// internal housekeeping method.
|
|
12378
|
+
// Check all the cached data and delete the invalid values
|
|
12379
|
+
this._checkData = this._checkData.bind(this);
|
|
12380
|
+
// ## _check
|
|
12381
|
+
|
|
12382
|
+
// internal method the check the value. If it's not valid any more delete it
|
|
12383
|
+
this._check = this._check.bind(this);
|
|
12384
|
+
// ## _isInvalidKey
|
|
12385
|
+
|
|
12386
|
+
// internal method to check if the type of a key is either `number` or `string`
|
|
12387
|
+
this._isInvalidKey = this._isInvalidKey.bind(this);
|
|
12388
|
+
// ## _wrap
|
|
12389
|
+
|
|
12390
|
+
// internal method to wrap a value in an object with some metadata
|
|
12391
|
+
this._wrap = this._wrap.bind(this);
|
|
12392
|
+
// ## _getValLength
|
|
12393
|
+
|
|
12394
|
+
// internal method to calculate the value length
|
|
12395
|
+
this._getValLength = this._getValLength.bind(this);
|
|
12396
|
+
// ## _error
|
|
12397
|
+
|
|
12398
|
+
// internal method to handle an error message
|
|
12399
|
+
this._error = this._error.bind(this);
|
|
12400
|
+
// ## _initErrors
|
|
12401
|
+
|
|
12402
|
+
// internal method to generate error message templates
|
|
12403
|
+
this._initErrors = this._initErrors.bind(this);
|
|
12404
|
+
this.options = options;
|
|
12405
|
+
this._initErrors();
|
|
12406
|
+
// container for cached data
|
|
12407
|
+
this.data = {};
|
|
12408
|
+
// module options
|
|
12409
|
+
this.options = Object.assign({
|
|
12410
|
+
// convert all elements to string
|
|
12411
|
+
forceString: false,
|
|
12412
|
+
// used standard size for calculating value size
|
|
12413
|
+
objectValueSize: 80,
|
|
12414
|
+
promiseValueSize: 80,
|
|
12415
|
+
arrayValueSize: 40,
|
|
12416
|
+
// standard time to live in seconds. 0 = infinity;
|
|
12417
|
+
stdTTL: 0,
|
|
12418
|
+
// time in seconds to check all data and delete expired keys
|
|
12419
|
+
checkperiod: 600,
|
|
12420
|
+
// 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
|
|
12421
|
+
useClones: true,
|
|
12422
|
+
// whether values should be deleted automatically at expiration
|
|
12423
|
+
deleteOnExpire: true,
|
|
12424
|
+
// enable legacy callbacks
|
|
12425
|
+
enableLegacyCallbacks: false,
|
|
12426
|
+
// max amount of keys that are being stored
|
|
12427
|
+
maxKeys: -1
|
|
12428
|
+
}, this.options);
|
|
12429
|
+
// generate functions with callbacks (legacy)
|
|
12430
|
+
if (this.options.enableLegacyCallbacks) {
|
|
12431
|
+
console.warn("WARNING! node-cache legacy callback support will drop in v6.x");
|
|
12432
|
+
["get", "mget", "set", "del", "ttl", "getTtl", "keys", "has"].forEach((methodKey) => {
|
|
12433
|
+
var oldMethod;
|
|
12434
|
+
// reference real function
|
|
12435
|
+
oldMethod = this[methodKey];
|
|
12436
|
+
this[methodKey] = function(...args) {
|
|
12437
|
+
var cb, err, ref, res;
|
|
12438
|
+
ref = args, [...args] = ref, [cb] = splice.call(args, -1);
|
|
12439
|
+
// return a callback if cb is defined and a function
|
|
12440
|
+
if (typeof cb === "function") {
|
|
12441
|
+
try {
|
|
12442
|
+
res = oldMethod(...args);
|
|
12443
|
+
cb(null, res);
|
|
12444
|
+
} catch (error1) {
|
|
12445
|
+
err = error1;
|
|
12446
|
+
cb(err);
|
|
12447
|
+
}
|
|
12448
|
+
} else {
|
|
12449
|
+
return oldMethod(...args, cb);
|
|
12450
|
+
}
|
|
12451
|
+
};
|
|
12452
|
+
});
|
|
12453
|
+
}
|
|
12454
|
+
// statistics container
|
|
12455
|
+
this.stats = {
|
|
12456
|
+
hits: 0,
|
|
12457
|
+
misses: 0,
|
|
12458
|
+
keys: 0,
|
|
12459
|
+
ksize: 0,
|
|
12460
|
+
vsize: 0
|
|
12461
|
+
};
|
|
12462
|
+
// pre allocate valid keytypes array
|
|
12463
|
+
this.validKeyTypes = ["string", "number"];
|
|
12464
|
+
// initalize checking period
|
|
12465
|
+
this._checkData();
|
|
12466
|
+
return;
|
|
12467
|
+
}
|
|
12468
|
+
|
|
12469
|
+
get(key) {
|
|
12470
|
+
var _ret, err;
|
|
12471
|
+
boundMethodCheck(this, NodeCache);
|
|
12472
|
+
// handle invalid key types
|
|
12473
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12474
|
+
throw err;
|
|
12475
|
+
}
|
|
12476
|
+
// get data and incremet stats
|
|
12477
|
+
if ((this.data[key] != null) && this._check(key, this.data[key])) {
|
|
12478
|
+
this.stats.hits++;
|
|
12479
|
+
_ret = this._unwrap(this.data[key]);
|
|
12480
|
+
// return data
|
|
12481
|
+
return _ret;
|
|
12482
|
+
} else {
|
|
12483
|
+
// if not found return undefined
|
|
12484
|
+
this.stats.misses++;
|
|
12485
|
+
return void 0;
|
|
12486
|
+
}
|
|
12487
|
+
}
|
|
12488
|
+
|
|
12489
|
+
mget(keys) {
|
|
12490
|
+
var _err, err, i, key, len, oRet;
|
|
12491
|
+
boundMethodCheck(this, NodeCache);
|
|
12492
|
+
// convert a string to an array of one key
|
|
12493
|
+
if (!Array.isArray(keys)) {
|
|
12494
|
+
_err = this._error("EKEYSTYPE");
|
|
12495
|
+
throw _err;
|
|
12496
|
+
}
|
|
12497
|
+
// define return
|
|
12498
|
+
oRet = {};
|
|
12499
|
+
for (i = 0, len = keys.length; i < len; i++) {
|
|
12500
|
+
key = keys[i];
|
|
12501
|
+
// handle invalid key types
|
|
12502
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12503
|
+
throw err;
|
|
12504
|
+
}
|
|
12505
|
+
// get data and increment stats
|
|
12506
|
+
if ((this.data[key] != null) && this._check(key, this.data[key])) {
|
|
12507
|
+
this.stats.hits++;
|
|
12508
|
+
oRet[key] = this._unwrap(this.data[key]);
|
|
12509
|
+
} else {
|
|
12510
|
+
// if not found return a error
|
|
12511
|
+
this.stats.misses++;
|
|
12512
|
+
}
|
|
12513
|
+
}
|
|
12514
|
+
// return all found keys
|
|
12515
|
+
return oRet;
|
|
12516
|
+
}
|
|
12517
|
+
|
|
12518
|
+
set(key, value, ttl) {
|
|
12519
|
+
var _err, err, existent;
|
|
12520
|
+
boundMethodCheck(this, NodeCache);
|
|
12521
|
+
// check if cache is overflowing
|
|
12522
|
+
if (this.options.maxKeys > -1 && this.stats.keys >= this.options.maxKeys) {
|
|
12523
|
+
_err = this._error("ECACHEFULL");
|
|
12524
|
+
throw _err;
|
|
12525
|
+
}
|
|
12526
|
+
// force the data to string
|
|
12527
|
+
if (this.options.forceString && !typeof value === "string") {
|
|
12528
|
+
value = JSON.stringify(value);
|
|
12529
|
+
}
|
|
12530
|
+
// set default ttl if not passed
|
|
12531
|
+
if (ttl == null) {
|
|
12532
|
+
ttl = this.options.stdTTL;
|
|
12533
|
+
}
|
|
12534
|
+
// handle invalid key types
|
|
12535
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12536
|
+
throw err;
|
|
12537
|
+
}
|
|
12538
|
+
// internal helper variables
|
|
12539
|
+
existent = false;
|
|
12540
|
+
// remove existing data from stats
|
|
12541
|
+
if (this.data[key]) {
|
|
12542
|
+
existent = true;
|
|
12543
|
+
this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false));
|
|
12544
|
+
}
|
|
12545
|
+
// set the value
|
|
12546
|
+
this.data[key] = this._wrap(value, ttl);
|
|
12547
|
+
this.stats.vsize += this._getValLength(value);
|
|
12548
|
+
// only add the keys and key-size if the key is new
|
|
12549
|
+
if (!existent) {
|
|
12550
|
+
this.stats.ksize += this._getKeyLength(key);
|
|
12551
|
+
this.stats.keys++;
|
|
12552
|
+
}
|
|
12553
|
+
this.emit("set", key, value);
|
|
12554
|
+
// return true
|
|
12555
|
+
return true;
|
|
12556
|
+
}
|
|
12557
|
+
|
|
12558
|
+
mset(keyValueSet) {
|
|
12559
|
+
var _err, err, i, j, key, keyValuePair, len, len1, ttl, val;
|
|
12560
|
+
boundMethodCheck(this, NodeCache);
|
|
12561
|
+
// check if cache is overflowing
|
|
12562
|
+
if (this.options.maxKeys > -1 && this.stats.keys + keyValueSet.length >= this.options.maxKeys) {
|
|
12563
|
+
_err = this._error("ECACHEFULL");
|
|
12564
|
+
throw _err;
|
|
12565
|
+
}
|
|
12566
|
+
|
|
12567
|
+
// loop over keyValueSet to validate key and ttl
|
|
12568
|
+
for (i = 0, len = keyValueSet.length; i < len; i++) {
|
|
12569
|
+
keyValuePair = keyValueSet[i];
|
|
12570
|
+
({key, val, ttl} = keyValuePair);
|
|
12571
|
+
// check if there is ttl and it's a number
|
|
12572
|
+
if (ttl && typeof ttl !== "number") {
|
|
12573
|
+
_err = this._error("ETTLTYPE");
|
|
12574
|
+
throw _err;
|
|
12575
|
+
}
|
|
12576
|
+
// handle invalid key types
|
|
12577
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12578
|
+
throw err;
|
|
12579
|
+
}
|
|
12580
|
+
}
|
|
12581
|
+
for (j = 0, len1 = keyValueSet.length; j < len1; j++) {
|
|
12582
|
+
keyValuePair = keyValueSet[j];
|
|
12583
|
+
({key, val, ttl} = keyValuePair);
|
|
12584
|
+
this.set(key, val, ttl);
|
|
12585
|
+
}
|
|
12586
|
+
return true;
|
|
12587
|
+
}
|
|
12588
|
+
|
|
12589
|
+
del(keys) {
|
|
12590
|
+
var delCount, err, i, key, len, oldVal;
|
|
12591
|
+
boundMethodCheck(this, NodeCache);
|
|
12592
|
+
// convert keys to an array of itself
|
|
12593
|
+
if (!Array.isArray(keys)) {
|
|
12594
|
+
keys = [keys];
|
|
12595
|
+
}
|
|
12596
|
+
delCount = 0;
|
|
12597
|
+
for (i = 0, len = keys.length; i < len; i++) {
|
|
12598
|
+
key = keys[i];
|
|
12599
|
+
// handle invalid key types
|
|
12600
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12601
|
+
throw err;
|
|
12602
|
+
}
|
|
12603
|
+
// only delete if existent
|
|
12604
|
+
if (this.data[key] != null) {
|
|
12605
|
+
// calc the stats
|
|
12606
|
+
this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false));
|
|
12607
|
+
this.stats.ksize -= this._getKeyLength(key);
|
|
12608
|
+
this.stats.keys--;
|
|
12609
|
+
delCount++;
|
|
12610
|
+
// delete the value
|
|
12611
|
+
oldVal = this.data[key];
|
|
12612
|
+
delete this.data[key];
|
|
12613
|
+
// return true
|
|
12614
|
+
this.emit("del", key, oldVal.v);
|
|
12615
|
+
}
|
|
12616
|
+
}
|
|
12617
|
+
return delCount;
|
|
12618
|
+
}
|
|
12619
|
+
|
|
12620
|
+
take(key) {
|
|
12621
|
+
var _ret;
|
|
12622
|
+
boundMethodCheck(this, NodeCache);
|
|
12623
|
+
_ret = this.get(key);
|
|
12624
|
+
if ((_ret != null)) {
|
|
12625
|
+
this.del(key);
|
|
12626
|
+
}
|
|
12627
|
+
return _ret;
|
|
12628
|
+
}
|
|
12629
|
+
|
|
12630
|
+
ttl(key, ttl) {
|
|
12631
|
+
var err;
|
|
12632
|
+
boundMethodCheck(this, NodeCache);
|
|
12633
|
+
ttl || (ttl = this.options.stdTTL);
|
|
12634
|
+
if (!key) {
|
|
12635
|
+
return false;
|
|
12636
|
+
}
|
|
12637
|
+
// handle invalid key types
|
|
12638
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12639
|
+
throw err;
|
|
12640
|
+
}
|
|
12641
|
+
// check for existent data and update the ttl value
|
|
12642
|
+
if ((this.data[key] != null) && this._check(key, this.data[key])) {
|
|
12643
|
+
// if ttl < 0 delete the key. otherwise reset the value
|
|
12644
|
+
if (ttl >= 0) {
|
|
12645
|
+
this.data[key] = this._wrap(this.data[key].v, ttl, false);
|
|
12646
|
+
} else {
|
|
12647
|
+
this.del(key);
|
|
12648
|
+
}
|
|
12649
|
+
return true;
|
|
12650
|
+
} else {
|
|
12651
|
+
// return false if key has not been found
|
|
12652
|
+
return false;
|
|
12653
|
+
}
|
|
12654
|
+
}
|
|
12655
|
+
|
|
12656
|
+
getTtl(key) {
|
|
12657
|
+
var _ttl, err;
|
|
12658
|
+
boundMethodCheck(this, NodeCache);
|
|
12659
|
+
if (!key) {
|
|
12660
|
+
return void 0;
|
|
12661
|
+
}
|
|
12662
|
+
// handle invalid key types
|
|
12663
|
+
if ((err = this._isInvalidKey(key)) != null) {
|
|
12664
|
+
throw err;
|
|
12665
|
+
}
|
|
12666
|
+
// check for existant data and update the ttl value
|
|
12667
|
+
if ((this.data[key] != null) && this._check(key, this.data[key])) {
|
|
12668
|
+
_ttl = this.data[key].t;
|
|
12669
|
+
return _ttl;
|
|
12670
|
+
} else {
|
|
12671
|
+
// return undefined if key has not been found
|
|
12672
|
+
return void 0;
|
|
12673
|
+
}
|
|
12674
|
+
}
|
|
12675
|
+
|
|
12676
|
+
keys() {
|
|
12677
|
+
var _keys;
|
|
12678
|
+
boundMethodCheck(this, NodeCache);
|
|
12679
|
+
_keys = Object.keys(this.data);
|
|
12680
|
+
return _keys;
|
|
12681
|
+
}
|
|
12682
|
+
|
|
12683
|
+
has(key) {
|
|
12684
|
+
var _exists;
|
|
12685
|
+
boundMethodCheck(this, NodeCache);
|
|
12686
|
+
_exists = (this.data[key] != null) && this._check(key, this.data[key]);
|
|
12687
|
+
return _exists;
|
|
12688
|
+
}
|
|
12689
|
+
|
|
12690
|
+
getStats() {
|
|
12691
|
+
boundMethodCheck(this, NodeCache);
|
|
12692
|
+
return this.stats;
|
|
12693
|
+
}
|
|
12694
|
+
|
|
12695
|
+
flushAll(_startPeriod = true) {
|
|
12696
|
+
boundMethodCheck(this, NodeCache);
|
|
12697
|
+
// parameter just for testing
|
|
12698
|
+
|
|
12699
|
+
// set data empty
|
|
12700
|
+
this.data = {};
|
|
12701
|
+
// reset stats
|
|
12702
|
+
this.stats = {
|
|
12703
|
+
hits: 0,
|
|
12704
|
+
misses: 0,
|
|
12705
|
+
keys: 0,
|
|
12706
|
+
ksize: 0,
|
|
12707
|
+
vsize: 0
|
|
12708
|
+
};
|
|
12709
|
+
// reset check period
|
|
12710
|
+
this._killCheckPeriod();
|
|
12711
|
+
this._checkData(_startPeriod);
|
|
12712
|
+
this.emit("flush");
|
|
12713
|
+
}
|
|
12714
|
+
|
|
12715
|
+
flushStats() {
|
|
12716
|
+
boundMethodCheck(this, NodeCache);
|
|
12717
|
+
// reset stats
|
|
12718
|
+
this.stats = {
|
|
12719
|
+
hits: 0,
|
|
12720
|
+
misses: 0,
|
|
12721
|
+
keys: 0,
|
|
12722
|
+
ksize: 0,
|
|
12723
|
+
vsize: 0
|
|
12724
|
+
};
|
|
12725
|
+
this.emit("flush_stats");
|
|
12726
|
+
}
|
|
12727
|
+
|
|
12728
|
+
close() {
|
|
12729
|
+
boundMethodCheck(this, NodeCache);
|
|
12730
|
+
this._killCheckPeriod();
|
|
12731
|
+
}
|
|
12732
|
+
|
|
12733
|
+
_checkData(startPeriod = true) {
|
|
12734
|
+
var key, ref, value;
|
|
12735
|
+
boundMethodCheck(this, NodeCache);
|
|
12736
|
+
ref = this.data;
|
|
12737
|
+
// run the housekeeping method
|
|
12738
|
+
for (key in ref) {
|
|
12739
|
+
value = ref[key];
|
|
12740
|
+
this._check(key, value);
|
|
12741
|
+
}
|
|
12742
|
+
if (startPeriod && this.options.checkperiod > 0) {
|
|
12743
|
+
this.checkTimeout = setTimeout(this._checkData, this.options.checkperiod * 1000, startPeriod);
|
|
12744
|
+
if ((this.checkTimeout != null) && (this.checkTimeout.unref != null)) {
|
|
12745
|
+
this.checkTimeout.unref();
|
|
12746
|
+
}
|
|
12747
|
+
}
|
|
12748
|
+
}
|
|
12749
|
+
|
|
12750
|
+
// ## _killCheckPeriod
|
|
12751
|
+
|
|
12752
|
+
// stop the checkdata period. Only needed to abort the script in testing mode.
|
|
12753
|
+
_killCheckPeriod() {
|
|
12754
|
+
if (this.checkTimeout != null) {
|
|
12755
|
+
return clearTimeout(this.checkTimeout);
|
|
12756
|
+
}
|
|
12757
|
+
}
|
|
12758
|
+
|
|
12759
|
+
_check(key, data) {
|
|
12760
|
+
var _retval;
|
|
12761
|
+
boundMethodCheck(this, NodeCache);
|
|
12762
|
+
_retval = true;
|
|
12763
|
+
// data is invalid if the ttl is too old and is not 0
|
|
12764
|
+
// console.log data.t < Date.now(), data.t, Date.now()
|
|
12765
|
+
if (data.t !== 0 && data.t < Date.now()) {
|
|
12766
|
+
if (this.options.deleteOnExpire) {
|
|
12767
|
+
_retval = false;
|
|
12768
|
+
this.del(key);
|
|
12769
|
+
}
|
|
12770
|
+
this.emit("expired", key, this._unwrap(data));
|
|
12771
|
+
}
|
|
12772
|
+
return _retval;
|
|
12773
|
+
}
|
|
12774
|
+
|
|
12775
|
+
_isInvalidKey(key) {
|
|
12776
|
+
var ref;
|
|
12777
|
+
boundMethodCheck(this, NodeCache);
|
|
12778
|
+
if (ref = typeof key, indexOf.call(this.validKeyTypes, ref) < 0) {
|
|
12779
|
+
return this._error("EKEYTYPE", {
|
|
12780
|
+
type: typeof key
|
|
12781
|
+
});
|
|
12782
|
+
}
|
|
12783
|
+
}
|
|
12784
|
+
|
|
12785
|
+
_wrap(value, ttl, asClone = true) {
|
|
12786
|
+
var livetime, now, ttlMultiplicator;
|
|
12787
|
+
boundMethodCheck(this, NodeCache);
|
|
12788
|
+
if (!this.options.useClones) {
|
|
12789
|
+
asClone = false;
|
|
12790
|
+
}
|
|
12791
|
+
// define the time to live
|
|
12792
|
+
now = Date.now();
|
|
12793
|
+
livetime = 0;
|
|
12794
|
+
ttlMultiplicator = 1000;
|
|
12795
|
+
// use given ttl
|
|
12796
|
+
if (ttl === 0) {
|
|
12797
|
+
livetime = 0;
|
|
12798
|
+
} else if (ttl) {
|
|
12799
|
+
livetime = now + (ttl * ttlMultiplicator);
|
|
12800
|
+
} else {
|
|
12801
|
+
// use standard ttl
|
|
12802
|
+
if (this.options.stdTTL === 0) {
|
|
12803
|
+
livetime = this.options.stdTTL;
|
|
12804
|
+
} else {
|
|
12805
|
+
livetime = now + (this.options.stdTTL * ttlMultiplicator);
|
|
12806
|
+
}
|
|
12807
|
+
}
|
|
12808
|
+
// return the wrapped value
|
|
12809
|
+
return {
|
|
12810
|
+
t: livetime,
|
|
12811
|
+
v: asClone ? clone(value) : value
|
|
12812
|
+
};
|
|
12813
|
+
}
|
|
12814
|
+
|
|
12815
|
+
// ## _unwrap
|
|
12816
|
+
|
|
12817
|
+
// internal method to extract get the value out of the wrapped value
|
|
12818
|
+
_unwrap(value, asClone = true) {
|
|
12819
|
+
if (!this.options.useClones) {
|
|
12820
|
+
asClone = false;
|
|
12821
|
+
}
|
|
12822
|
+
if (value.v != null) {
|
|
12823
|
+
if (asClone) {
|
|
12824
|
+
return clone(value.v);
|
|
12825
|
+
} else {
|
|
12826
|
+
return value.v;
|
|
12827
|
+
}
|
|
12828
|
+
}
|
|
12829
|
+
return null;
|
|
12830
|
+
}
|
|
12831
|
+
|
|
12832
|
+
// ## _getKeyLength
|
|
12833
|
+
|
|
12834
|
+
// internal method the calculate the key length
|
|
12835
|
+
_getKeyLength(key) {
|
|
12836
|
+
return key.toString().length;
|
|
12837
|
+
}
|
|
12838
|
+
|
|
12839
|
+
_getValLength(value) {
|
|
12840
|
+
boundMethodCheck(this, NodeCache);
|
|
12841
|
+
if (typeof value === "string") {
|
|
12842
|
+
// if the value is a String get the real length
|
|
12843
|
+
return value.length;
|
|
12844
|
+
} else if (this.options.forceString) {
|
|
12845
|
+
// force string if it's defined and not passed
|
|
12846
|
+
return JSON.stringify(value).length;
|
|
12847
|
+
} else if (Array.isArray(value)) {
|
|
12848
|
+
// if the data is an Array multiply each element with a defined default length
|
|
12849
|
+
return this.options.arrayValueSize * value.length;
|
|
12850
|
+
} else if (typeof value === "number") {
|
|
12851
|
+
return 8;
|
|
12852
|
+
} else if (typeof (value != null ? value.then : void 0) === "function") {
|
|
12853
|
+
// if the data is a Promise, use defined default
|
|
12854
|
+
// (can't calculate actual/resolved value size synchronously)
|
|
12855
|
+
return this.options.promiseValueSize;
|
|
12856
|
+
} else if (typeof Buffer !== "undefined" && Buffer !== null ? Buffer.isBuffer(value) : void 0) {
|
|
12857
|
+
return value.length;
|
|
12858
|
+
} else if ((value != null) && typeof value === "object") {
|
|
12859
|
+
// if the data is an Object multiply each element with a defined default length
|
|
12860
|
+
return this.options.objectValueSize * Object.keys(value).length;
|
|
12861
|
+
} else if (typeof value === "boolean") {
|
|
12862
|
+
return 8;
|
|
12863
|
+
} else {
|
|
12864
|
+
// default fallback
|
|
12865
|
+
return 0;
|
|
12866
|
+
}
|
|
12867
|
+
}
|
|
12868
|
+
|
|
12869
|
+
_error(type, data = {}) {
|
|
12870
|
+
var error;
|
|
12871
|
+
boundMethodCheck(this, NodeCache);
|
|
12872
|
+
// generate the error object
|
|
12873
|
+
error = new Error();
|
|
12874
|
+
error.name = type;
|
|
12875
|
+
error.errorcode = type;
|
|
12876
|
+
error.message = this.ERRORS[type] != null ? this.ERRORS[type](data) : "-";
|
|
12877
|
+
error.data = data;
|
|
12878
|
+
// return the error object
|
|
12879
|
+
return error;
|
|
12880
|
+
}
|
|
12881
|
+
|
|
12882
|
+
_initErrors() {
|
|
12883
|
+
var _errMsg, _errT, ref;
|
|
12884
|
+
boundMethodCheck(this, NodeCache);
|
|
12885
|
+
this.ERRORS = {};
|
|
12886
|
+
ref = this._ERRORS;
|
|
12887
|
+
for (_errT in ref) {
|
|
12888
|
+
_errMsg = ref[_errT];
|
|
12889
|
+
this.ERRORS[_errT] = this.createErrorMessage(_errMsg);
|
|
12890
|
+
}
|
|
12891
|
+
}
|
|
12892
|
+
|
|
12893
|
+
createErrorMessage(errMsg) {
|
|
12894
|
+
return function(args) {
|
|
12895
|
+
return errMsg.replace("__key", args.type);
|
|
12896
|
+
};
|
|
12897
|
+
}
|
|
12898
|
+
|
|
12899
|
+
}
|
|
12900
|
+
NodeCache.prototype._ERRORS = {
|
|
12901
|
+
"ENOTFOUND": "Key `__key` not found",
|
|
12902
|
+
"ECACHEFULL": "Cache max keys amount exceeded",
|
|
12903
|
+
"EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `__key`",
|
|
12904
|
+
"EKEYSTYPE": "The keys argument has to be an array.",
|
|
12905
|
+
"ETTLTYPE": "The ttl argument has to be a number."
|
|
12906
|
+
};
|
|
12907
|
+
|
|
12908
|
+
return NodeCache;
|
|
12909
|
+
|
|
12910
|
+
}).call(this);
|
|
12911
|
+
|
|
12912
|
+
}).call(commonjsGlobal$1);
|
|
12913
|
+
return node_cache.exports;
|
|
12914
|
+
}
|
|
12915
|
+
|
|
12916
|
+
/*
|
|
12917
|
+
* node-cache 5.1.2 ( 2020-07-01 )
|
|
12918
|
+
* https://github.com/node-cache/node-cache
|
|
12919
|
+
*
|
|
12920
|
+
* Released under the MIT license
|
|
12921
|
+
* https://github.com/node-cache/node-cache/blob/master/LICENSE
|
|
12922
|
+
*
|
|
12923
|
+
* Maintained by ( )
|
|
12924
|
+
*/
|
|
12925
|
+
|
|
12926
|
+
(function (module) {
|
|
12927
|
+
(function() {
|
|
12928
|
+
var exports;
|
|
12929
|
+
|
|
12930
|
+
exports = module.exports = requireNode_cache();
|
|
12931
|
+
|
|
12932
|
+
exports.version = '5.1.2';
|
|
12933
|
+
|
|
12934
|
+
}).call(commonjsGlobal$1);
|
|
12935
|
+
} (nodeCache));
|
|
12936
|
+
|
|
12937
|
+
var NodeCache = /*@__PURE__*/getDefaultExportFromCjs$1(nodeCache.exports);
|
|
12938
|
+
|
|
11346
12939
|
var AccountState;
|
|
11347
12940
|
(function (AccountState) {
|
|
11348
12941
|
AccountState[AccountState["Uninitialized"] = 0] = "Uninitialized";
|
|
@@ -11370,90 +12963,124 @@ var AccountType;
|
|
|
11370
12963
|
AccountType[AccountType["Unknown"] = 3] = "Unknown";
|
|
11371
12964
|
AccountType[AccountType["NotExists"] = 4] = "NotExists";
|
|
11372
12965
|
})(AccountType || (AccountType = {}));
|
|
12966
|
+
const TOKEN_PROGRAM_CACHE = new NodeCache({
|
|
12967
|
+
// cache never expires
|
|
12968
|
+
stdTTL: 0,
|
|
12969
|
+
// this cache stores values (including Promises). Using clones causes node to crash when a race condition occurs
|
|
12970
|
+
useClones: false,
|
|
12971
|
+
});
|
|
12972
|
+
const decodeMetaplexMetadataPartial = (data) => {
|
|
12973
|
+
const rawName = data.subarray(65 + 4, 65 + 36); // omit first 4 bytes, size is already hardcoded
|
|
12974
|
+
const rawSymbol = data.subarray(101 + 4, 115); // omit first 4 bytes, size is already hardcoded
|
|
12975
|
+
const rawUri = data.subarray(115 + 4, 204); // omit first 4 bytes, size is already hardcoded
|
|
12976
|
+
return {
|
|
12977
|
+
uri: rawUri.toString("utf-8").replace(/\u0000/g, ""),
|
|
12978
|
+
symbol: rawSymbol.toString("utf-8").replace(/\u0000/g, ""),
|
|
12979
|
+
name: rawName.toString("utf-8").replace(/\u0000/g, ""),
|
|
12980
|
+
};
|
|
12981
|
+
};
|
|
11373
12982
|
/**
|
|
11374
12983
|
* Get name, symbol and decimals for provided token
|
|
11375
12984
|
* @param connection solana web3 connection
|
|
11376
12985
|
* @param tokenAddress address to get metaplex info for
|
|
11377
12986
|
* @returns
|
|
11378
12987
|
*/
|
|
11379
|
-
function getTokenInfo(
|
|
11380
|
-
return __awaiter(this,
|
|
11381
|
-
|
|
11382
|
-
|
|
11383
|
-
const findMetaplexMetadataPda = (tokenMint) => PublicKey.findProgramAddressSync([
|
|
11384
|
-
buffer.Buffer.from("metadata", "utf-8"),
|
|
11385
|
-
tokenMetaProgramId.toBuffer(),
|
|
11386
|
-
tokenMint.toBuffer(),
|
|
11387
|
-
], tokenMetaProgramId)[0];
|
|
11388
|
-
const decodeMetaplexMetadataPartial = (data) => {
|
|
11389
|
-
const rawName = data.subarray(65 + 4, 65 + 36); // omit first 4 bytes, size is already hardcoded
|
|
11390
|
-
const rawSymbol = data.subarray(101 + 4, 115); // omit first 4 bytes, size is already hardcoded
|
|
11391
|
-
const rawUri = data.subarray(115 + 4, 204); // omit first 4 bytes, size is already hardcoded
|
|
12988
|
+
function getTokenInfo(connection_1, tokenAddress_1) {
|
|
12989
|
+
return __awaiter(this, arguments, void 0, function* (connection, tokenAddress, useCache = false) {
|
|
12990
|
+
const tokenAddressStr = tokenAddress.toBase58();
|
|
12991
|
+
if (tokenAddressStr === "11111111111111111111111111111111") {
|
|
11392
12992
|
return {
|
|
11393
|
-
|
|
11394
|
-
|
|
11395
|
-
name:
|
|
12993
|
+
address: tokenAddress,
|
|
12994
|
+
decimals: 9,
|
|
12995
|
+
name: "Native SOL",
|
|
12996
|
+
symbol: "SOL",
|
|
12997
|
+
json: "",
|
|
12998
|
+
tokenProgram: SystemProgram.programId,
|
|
11396
12999
|
};
|
|
11397
|
-
};
|
|
11398
|
-
const pda = findMetaplexMetadataPda(tokenAddress);
|
|
11399
|
-
const [tokenAccount, pdaAccount] = yield connection.getMultipleAccountsInfo([
|
|
11400
|
-
tokenAddress,
|
|
11401
|
-
pda,
|
|
11402
|
-
]);
|
|
11403
|
-
if (tokenAccount === null) {
|
|
11404
|
-
return null;
|
|
11405
13000
|
}
|
|
11406
|
-
|
|
11407
|
-
|
|
11408
|
-
|
|
11409
|
-
|
|
11410
|
-
|
|
11411
|
-
|
|
11412
|
-
|
|
13001
|
+
// if requested, try returning cached Promise (in-flight or resolved)
|
|
13002
|
+
if (useCache) {
|
|
13003
|
+
const cached = TOKEN_PROGRAM_CACHE.get(tokenAddressStr);
|
|
13004
|
+
if (cached)
|
|
13005
|
+
return cached;
|
|
13006
|
+
}
|
|
13007
|
+
// fetcher encapsulates the actual work and is stored in cache when useCache is true
|
|
13008
|
+
const fetcher = (() => __awaiter(this, void 0, void 0, function* () {
|
|
13009
|
+
var _a;
|
|
13010
|
+
const [pda] = getTokenMetadataAddress(tokenAddress, TOKEN_METADATA_PROGRAM_ID);
|
|
13011
|
+
const [tokenAccount, pdaAccount] = yield connection.getMultipleAccountsInfo([tokenAddress, pda]);
|
|
13012
|
+
if (!tokenAccount || (tokenAccount === null || tokenAccount === void 0 ? void 0 : tokenAccount.lamports) === 0) {
|
|
13013
|
+
return null;
|
|
13014
|
+
}
|
|
13015
|
+
const mint2022 = unpackMint(tokenAddress, tokenAccount, tokenAccount.owner);
|
|
13016
|
+
const extTypes = getExtensionTypes((_a = mint2022.tlvData) !== null && _a !== void 0 ? _a : buffer.Buffer.alloc(0));
|
|
13017
|
+
const tokenMetaBuf = extTypes.includes(ExtensionType.TokenMetadata)
|
|
13018
|
+
? getExtensionData(ExtensionType.TokenMetadata, mint2022.tlvData)
|
|
13019
|
+
: null;
|
|
13020
|
+
if (tokenMetaBuf) {
|
|
13021
|
+
const meta = unpack(tokenMetaBuf);
|
|
13022
|
+
return {
|
|
13023
|
+
address: tokenAddress,
|
|
13024
|
+
decimals: mint2022.decimals,
|
|
13025
|
+
name: meta.name,
|
|
13026
|
+
symbol: meta.symbol,
|
|
13027
|
+
json: meta.uri,
|
|
13028
|
+
tokenProgram: tokenAccount.owner,
|
|
13029
|
+
};
|
|
13030
|
+
}
|
|
13031
|
+
if (extTypes.includes(ExtensionType.MetadataPointer)) {
|
|
13032
|
+
const mpState = getMetadataPointerState(mint2022);
|
|
13033
|
+
if ((mpState === null || mpState === void 0 ? void 0 : mpState.metadataAddress) &&
|
|
13034
|
+
mpState.metadataAddress.equals(tokenAddress)) {
|
|
13035
|
+
const inlineMetaBuf = getExtensionData(ExtensionType.TokenMetadata, mint2022.tlvData);
|
|
13036
|
+
if (inlineMetaBuf) {
|
|
13037
|
+
const meta = unpack(inlineMetaBuf);
|
|
13038
|
+
return {
|
|
13039
|
+
address: tokenAddress,
|
|
13040
|
+
decimals: mint2022.decimals,
|
|
13041
|
+
name: meta.name,
|
|
13042
|
+
symbol: meta.symbol,
|
|
13043
|
+
json: meta.uri,
|
|
13044
|
+
tokenProgram: tokenAccount.owner,
|
|
13045
|
+
};
|
|
13046
|
+
}
|
|
13047
|
+
}
|
|
13048
|
+
}
|
|
13049
|
+
if (!pdaAccount || pdaAccount.lamports === 0) {
|
|
13050
|
+
return null;
|
|
13051
|
+
}
|
|
13052
|
+
// parse token account
|
|
13053
|
+
// mint_authority - coption(pubkey) = 4 or 36
|
|
13054
|
+
// supply - u64 = 8
|
|
13055
|
+
// decimals - u8
|
|
13056
|
+
let decimalsOffset = 12;
|
|
13057
|
+
if (tokenAccount.data[0] === 1)
|
|
13058
|
+
decimalsOffset += 32;
|
|
13059
|
+
const decimals = tokenAccount.data[decimalsOffset];
|
|
13060
|
+
const { name, symbol, uri } = decodeMetaplexMetadataPartial(pdaAccount.data);
|
|
11413
13061
|
return {
|
|
11414
13062
|
address: tokenAddress,
|
|
11415
|
-
decimals
|
|
11416
|
-
name
|
|
11417
|
-
symbol
|
|
11418
|
-
json:
|
|
13063
|
+
decimals,
|
|
13064
|
+
name,
|
|
13065
|
+
symbol,
|
|
13066
|
+
json: uri,
|
|
13067
|
+
tokenProgram: tokenAccount.owner,
|
|
11419
13068
|
};
|
|
11420
|
-
}
|
|
11421
|
-
if (
|
|
11422
|
-
|
|
11423
|
-
|
|
11424
|
-
|
|
11425
|
-
|
|
11426
|
-
if (
|
|
11427
|
-
|
|
11428
|
-
return {
|
|
11429
|
-
address: tokenAddress,
|
|
11430
|
-
decimals: mint2022.decimals,
|
|
11431
|
-
name: meta.name,
|
|
11432
|
-
symbol: meta.symbol,
|
|
11433
|
-
json: meta.uri,
|
|
11434
|
-
};
|
|
13069
|
+
}))();
|
|
13070
|
+
if (useCache) {
|
|
13071
|
+
// store the in-flight Promise so concurrent callers reuse it
|
|
13072
|
+
// but ensure we don't keep null results in the cache: if the promise
|
|
13073
|
+
// resolves to null, remove the cache entry.
|
|
13074
|
+
const wrapped = fetcher.then((res) => {
|
|
13075
|
+
if (res === null) {
|
|
13076
|
+
TOKEN_PROGRAM_CACHE.del(tokenAddressStr);
|
|
11435
13077
|
}
|
|
11436
|
-
|
|
11437
|
-
|
|
11438
|
-
|
|
11439
|
-
return
|
|
13078
|
+
return res;
|
|
13079
|
+
});
|
|
13080
|
+
TOKEN_PROGRAM_CACHE.set(tokenAddressStr, wrapped);
|
|
13081
|
+
return wrapped;
|
|
11440
13082
|
}
|
|
11441
|
-
|
|
11442
|
-
// mint_authority - coption(pubkey) = 4 or 36
|
|
11443
|
-
// supply - u64 = 8
|
|
11444
|
-
// decimals - u8
|
|
11445
|
-
let decimalsOffset = 12;
|
|
11446
|
-
if (tokenAccount.data[0] === 1)
|
|
11447
|
-
decimalsOffset += 32;
|
|
11448
|
-
const decimals = tokenAccount.data[decimalsOffset];
|
|
11449
|
-
const { name, symbol, uri } = decodeMetaplexMetadataPartial(pdaAccount.data);
|
|
11450
|
-
return {
|
|
11451
|
-
address: tokenAddress,
|
|
11452
|
-
decimals,
|
|
11453
|
-
name,
|
|
11454
|
-
symbol,
|
|
11455
|
-
json: uri,
|
|
11456
|
-
};
|
|
13083
|
+
return fetcher;
|
|
11457
13084
|
});
|
|
11458
13085
|
}
|
|
11459
13086
|
/**
|