fix: prevent asset conflicts between React and Grid.js versions

Add coexistence checks to all enqueue methods to prevent loading
both React and Grid.js assets simultaneously.

Changes:
- ReactAdmin.php: Only enqueue React assets when ?react=1
- Init.php: Skip Grid.js when React active on admin pages
- Form.php, Coupon.php, Access.php: Restore classic assets when ?react=0
- Customer.php, Product.php, License.php: Add coexistence checks

Now the toggle between Classic and React versions works correctly.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-04-18 17:02:14 +07:00
parent bd9cdac02e
commit e8fbfb14c1
74973 changed files with 6658406 additions and 71 deletions

5
node_modules/lru_map/.npmignore generated vendored Normal file
View File

@@ -0,0 +1,5 @@
.DS_Store
_local
tstest.js
*-test.js
node_modules

6
node_modules/lru_map/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,6 @@
language: node_js
node_js:
- node # latest stable
- 6
script:
- npm test

214
node_modules/lru_map/README.md generated vendored Normal file
View File

@@ -0,0 +1,214 @@
# Least Recently Used (LRU) cache algorithm
A finite key-value map using the [Least Recently Used (LRU)](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) algorithm, where the most recently-used items are "kept alive" while older, less-recently used items are evicted to make room for newer items.
Useful when you want to limit use of memory to only hold commonly-used things.
[![Build status](https://travis-ci.org/rsms/js-lru.svg?branch=master)](https://travis-ci.org/rsms/js-lru)
## Terminology & design
- Based on a doubly-linked list for low complexity random shuffling of entries.
- The cache object iself has a "head" (least recently used entry) and a
"tail" (most recently used entry).
- The "oldest" and "newest" are list entries -- an entry might have a "newer" and
an "older" entry (doubly-linked, "older" being close to "head" and "newer"
being closer to "tail").
- Key lookup is done through a key-entry mapping native object, which on most
platforms mean `O(1)` complexity. This comes at a very low memory cost (for
storing two extra pointers for each entry).
Fancy ASCII art illustration of the general design:
```txt
entry entry entry entry
______ ______ ______ ______
| head |.newer => | |.newer => | |.newer => | tail |
.oldest = | A | | B | | C | | D | = .newest
|______| <= older.|______| <= older.|______| <= older.|______|
removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
```
## Example
```js
let c = new LRUMap(3)
c.set('adam', 29)
c.set('john', 26)
c.set('angela', 24)
c.toString() // -> "adam:29 < john:26 < angela:24"
c.get('john') // -> 26
// Now 'john' is the most recently used entry, since we just requested it
c.toString() // -> "adam:29 < angela:24 < john:26"
c.set('zorro', 141) // -> {key:adam, value:29}
// Because we only have room for 3 entries, adding 'zorro' caused 'adam'
// to be removed in order to make room for the new entry
c.toString() // -> "angela:24 < john:26 < zorro:141"
```
# Usage
**Recommended:** Copy the code in lru.js or copy the lru.js and lru.d.ts files into your source directory. For minimal functionality, you only need the lines up until the comment that says "Following code is optional".
**Using NPM:** [`yarn add lru_map`](https://www.npmjs.com/package/lru_map) (note that because NPM is one large flat namespace, you need to import the module as "lru_map" rather than simply "lru".)
**Using AMD:** An [AMD](https://github.com/amdjs/amdjs-api/blob/master/AMD.md#amd) module loader like [`amdld`](https://github.com/rsms/js-amdld) can be used to load this module as well. There should be nothing to configure.
**Testing**:
- Run tests with `npm test`
- Run benchmarks with `npm run benchmark`
**ES compatibility:** This implementation is compatible with modern JavaScript environments and depend on the following features not found in ES5:
- `const` and `let` keywords
- `Symbol` including `Symbol.iterator`
- `Map`
> Note: If you need ES5 compatibility e.g. to use with older browsers, [please use version 2](https://github.com/rsms/js-lru/tree/v2) which has a slightly less feature-full API but is well-tested and about as fast as this implementation.
**Using with TypeScript**
This module comes with complete typing coverage for use with TypeScript. If you copied the code or files rather than using a module loader, make sure to include `lru.d.ts` into the same location where you put `lru.js`.
```ts
import {LRUMap} from './lru'
// import {LRUMap} from 'lru' // when using via AMD
// import {LRUMap} from 'lru_map' // when using from NPM
console.log('LRUMap:', LRUMap)
```
# API
The API imitates that of [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), which means that in most cases you can use `LRUMap` as a drop-in replacement for `Map`.
```ts
export class LRUMap<K,V> {
// Construct a new cache object which will hold up to limit entries.
// When the size == limit, a `put` operation will evict the oldest entry.
//
// If `entries` is provided, all entries are added to the new map.
// `entries` should be an Array or other iterable object whose elements are
// key-value pairs (2-element Arrays). Each key-value pair is added to the new Map.
// null is treated as undefined.
constructor(limit :number, entries? :Iterable<[K,V]>);
// Convenience constructor equivalent to `new LRUMap(count(entries), entries)`
constructor(entries :Iterable<[K,V]>);
// Current number of items
size :number;
// Maximum number of items this map can hold
limit :number;
// Least recently-used entry. Invalidated when map is modified.
oldest :Entry<K,V>;
// Most recently-used entry. Invalidated when map is modified.
newest :Entry<K,V>;
// Replace all values in this map with key-value pairs (2-element Arrays) from
// provided iterable.
assign(entries :Iterable<[K,V]>) : void;
// Put <value> into the cache associated with <key>. Replaces any existing entry
// with the same key. Returns `this`.
set(key :K, value :V) : LRUMap<K,V>;
// Purge the least recently used (oldest) entry from the cache.
// Returns the removed entry or undefined if the cache was empty.
shift() : [K,V] | undefined;
// Get and register recent use of <key>.
// Returns the value associated with <key> or undefined if not in cache.
get(key :K) : V | undefined;
// Check if there's a value for key in the cache without registering recent use.
has(key :K) : boolean;
// Access value for <key> without registering recent use. Useful if you do not
// want to chage the state of the map, but only "peek" at it.
// Returns the value associated with <key> if found, or undefined if not found.
find(key :K) : V | undefined;
// Remove entry <key> from cache and return its value.
// Returns the removed value, or undefined if not found.
delete(key :K) : V | undefined;
// Removes all entries
clear() : void;
// Returns an iterator over all keys, starting with the oldest.
keys() : Iterator<K>;
// Returns an iterator over all values, starting with the oldest.
values() : Iterator<V>;
// Returns an iterator over all entries, starting with the oldest.
entries() : Iterator<[K,V]>;
// Returns an iterator over all entries, starting with the oldest.
[Symbol.iterator]() : Iterator<[K,V]>;
// Call `fun` for each entry, starting with the oldest entry.
forEach(fun :(value :V, key :K, m :LRUMap<K,V>)=>void, thisArg? :any) : void;
// Returns an object suitable for JSON encoding
toJSON() : Array<{key :K, value :V}>;
// Returns a human-readable text representation
toString() : string;
}
// An entry holds the key and value, and pointers to any older and newer entries.
// Entries might hold references to adjacent entries in the internal linked-list.
// Therefore you should never store or modify Entry objects. Instead, reference the
// key and value of an entry when needed.
interface Entry<K,V> {
key :K;
value :V;
}
```
If you need to perform any form of finalization of items as they are evicted from the cache, wrapping the `shift` method is a good way to do it:
```js
let c = new LRUMap(123);
c.shift = function() {
let entry = LRUMap.prototype.shift.call(this);
doSomethingWith(entry);
return entry;
}
```
The internals calls `shift` as entries need to be evicted, so this method is guaranteed to be called for any item that's removed from the cache. The returned entry must not include any strong references to other entries. See note in the documentation of `LRUMap.prototype.set()`.
# MIT license
Copyright (c) 2010-2016 Rasmus Andersson <https://rsms.me/>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

155
node_modules/lru_map/benchmark.js generated vendored Normal file
View File

@@ -0,0 +1,155 @@
// This is a benchmark suite which will run with nodejs.
// $ node --expose-gc benchmark.js
var assert = require('assert'),
util = require('util'),
LRUMap = require('./lru').LRUMap;
// Create a cache with N entries
var N = 10000;
// Run each measurement Iterations times
var Iterations = 1000;
var has_gc_access = typeof gc === 'function';
var gc_collect = has_gc_access ? gc : function(){};
Number.prototype.toHuman = function(divisor) {
var N = Math.round(divisor ? this/divisor : this);
var n = N.toString().split('.');
n[0] = n[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
return n.join('.');
}
Number.prototype.toSignedHuman = function(divisor) {
var n = this.toHuman(divisor);
if (this > -1) n = '+'+n;
return n;
}
function measure(block) {
gc_collect();
var elapsed = 0, start, usec, n;
var sm = process.memoryUsage();
if (Iterations && Iterations > 0) {
start = new Date();
for (n = 0; n < Iterations; n++) {
block();
}
usec = ((new Date() - start) / Iterations) * 1000;
} else {
start = new Date();
block();
usec = (new Date() - start) * 1000;
}
var msg = '\n----------\n ' + block.toString().replace(/\n/g, "\n ") + '\n';
if (has_gc_access) {
gc_collect();
var em = process.memoryUsage();
var mrssd = em.rss - sm.rss;
var mhtotd = em.heapTotal - sm.heapTotal;
var mhusedd = em.heapUsed - sm.heapUsed;
msg += ' rss: ' + mrssd.toSignedHuman(1024) + ' kB -- (' +
sm.rss.toHuman(1024) + ' kB -> ' + em.rss.toHuman(1024) + ' kB)\n';
if (typeof sm.vsize === 'number') {
var mvsized = em.vsize - sm.vsize;
msg +=
' vsize: ' + mvsized.toSignedHuman(1024) + ' kB -- (' +
sm.vsize.toHuman(1024) + ' kB -> ' + em.vsize.toHuman(1024) + ' kB)\n';
}
msg += ' heap total: ' + mhtotd.toSignedHuman(1024) + ' kB -- (' +
sm.heapTotal.toHuman(1024) + ' kB -> ' + em.heapTotal.toHuman(1024) + ' kB)\n' +
' heap used: ' + mhusedd.toSignedHuman(1024) + ' kB -- (' +
sm.heapUsed.toHuman(1024) + ' kB -> ' + em.heapUsed.toHuman(1024) + ' kB)\n\n';
}
var call_avg = usec;
msg += ' -- ' + (call_avg / 1000) + ' ms avg per iteration --\n';
process.stdout.write(msg);
}
var c = new LRUMap(N);
console.log('N = ' + N + ', Iterations = ' + Iterations);
// We should probably spin up the system in some way, or repeat the benchmarks a
// few times, since initial heap resizing takes considerable time.
measure(function(){
// 1. put
// Simply append a new entry.
// There will be no reordering since we simply append to the tail.
for (var i=N; --i;)
c.set('key'+i, i);
});
measure(function(){
// 2. get recent -> old
// Get entries starting with newest, effectively reversing the list.
//
// a. For each get, a find is first executed implemented as a native object with
// keys mapping to entries, so this should be reasonably fast as most native
// objects are implemented as hash maps.
//
// b. For each get, the aquired item will be moved to tail which includes a
// maximum of 7 assignment operations (minimum 3).
for (var i=1,L=N+1; i<L; ++i)
c.get('key'+i, i);
});
measure(function(){
// 3. get old -> recent
// Get entries starting with oldest, effectively reversing the list.
//
// - Same conditions apply as for test 2.
for (var i=1,L=N+1; i<L; ++i)
c.get('key'+i);
});
measure(function(){
// 4. get missing
// Get try to get entries not in the cache.
// - Same conditions apply as for test 2, section a.
for (var i=1,L=N+1; i<L; ++i)
c.get('xkey'+i);
});
measure(function(){
// 5. put overflow
// Overflow the cache with N more items than it can hold.
// a. The complexity of put in this case should be:
// ( <get whith enough space> + <shift> )
for (var i=N; --i;)
c.set('key2_'+i, i);
});
measure(function(){
// 6. shift head -> tail
// Remove all entries going from head to tail
for (var i=1,L=N+1; i<L; ++i)
c.shift();
});
measure(function(){
// 7. put
// Simply put N new items into an empty cache with exactly N space.
for (var i=N; --i;)
c.set('key'+i, i);
});
// pre-build random key array
var shuffledKeys = Array.from(c.keys());
shuffledKeys.sort(function (){return Math.random()-0.5; });
measure(function(){
// 8. delete random
// a. Most operations (which are not entries at head or tail) will cause closes
// siblings to be relinked.
for (var i=shuffledKeys.length, key; key = shuffledKeys[--i]; ) {
c.delete('key'+i, i);
}
});

123
node_modules/lru_map/benchmark.out.txt generated vendored Normal file
View File

@@ -0,0 +1,123 @@
> lru_map@0.3.0 benchmark /src/js-lru
> node --expose-gc benchmark.js
N = 10000, Iterations = 1000
----------
function (){
// 1. put
// Simply append a new entry.
// There will be no reordering since we simply append to the tail.
for (var i=N; --i;)
c.set('key'+i, i);
}
rss: +9,380 kB -- (19,564 kB -> 28,944 kB)
heap total: +6,144 kB -- (11,260 kB -> 17,404 kB)
heap used: +1,640 kB -- (3,819 kB -> 5,459 kB)
-- 1.192 ms avg per iteration --
----------
function (){
// 2. get recent -> old
// Get entries starting with newest, effectively reversing the list.
//
// a. For each get, a find is first executed implemented as a native object with
// keys mapping to entries, so this should be reasonably fast as most native
// objects are implemented as hash maps.
//
// b. For each get, the aquired item will be moved to tail which includes a
// maximum of 7 assignment operations (minimum 3).
for (var i=1,L=N+1; i<L; ++i)
c.get('key'+i, i);
}
rss: +340 kB -- (29,112 kB -> 29,452 kB)
heap total: +0 kB -- (18,428 kB -> 18,428 kB)
heap used: -255 kB -- (5,479 kB -> 5,224 kB)
-- 1.197 ms avg per iteration --
----------
function (){
// 3. get old -> recent
// Get entries starting with oldest, effectively reversing the list.
//
// - Same conditions apply as for test 2.
for (var i=1,L=N+1; i<L; ++i)
c.get('key'+i);
}
rss: -344 kB -- (29,476 kB -> 29,132 kB)
heap total: +0 kB -- (18,428 kB -> 18,428 kB)
heap used: +3 kB -- (5,211 kB -> 5,215 kB)
-- 1.161 ms avg per iteration --
----------
function (){
// 4. get missing
// Get try to get entries not in the cache.
// - Same conditions apply as for test 2, section a.
for (var i=1,L=N+1; i<L; ++i)
c.get('xkey'+i);
}
rss: +0 kB -- (29,132 kB -> 29,132 kB)
heap total: +0 kB -- (18,428 kB -> 18,428 kB)
heap used: +5 kB -- (5,210 kB -> 5,215 kB)
-- 1.035 ms avg per iteration --
----------
function (){
// 5. put overflow
// Overflow the cache with N more items than it can hold.
// a. The complexity of put in this case should be:
// ( <get whith enough space> + <shift> )
for (var i=N; --i;)
c.set('key2_'+i, i);
}
rss: +2,036 kB -- (29,132 kB -> 31,168 kB)
heap total: +916 kB -- (18,428 kB -> 19,344 kB)
heap used: +542 kB -- (5,211 kB -> 5,752 kB)
-- 1.185 ms avg per iteration --
----------
function (){
// 6. shift head -> tail
// Remove all entries going from head to tail
for (var i=1,L=N+1; i<L; ++i)
c.shift();
}
rss: -1,528 kB -- (31,288 kB -> 29,760 kB)
heap total: +108 kB -- (18,320 kB -> 18,428 kB)
heap used: -1,819 kB -- (5,748 kB -> 3,929 kB)
-- 0.023 ms avg per iteration --
----------
function (){
// 7. put
// Simply put N new items into an empty cache with exactly N space.
for (var i=N; --i;)
c.set('key'+i, i);
}
rss: +8,064 kB -- (29,876 kB -> 37,940 kB)
heap total: +8,192 kB -- (17,404 kB -> 25,596 kB)
heap used: +1,327 kB -- (3,904 kB -> 5,232 kB)
-- 1.264 ms avg per iteration --
----------
function (){
// 8. delete random
// a. Most operations (which are not entries at head or tail) will cause closes
// siblings to be relinked.
for (var i=shuffledKeys.length, key; key = shuffledKeys[--i]; ) {
c.delete('key'+i, i);
}
}
rss: +132 kB -- (38,088 kB -> 38,220 kB)
heap total: +0 kB -- (25,596 kB -> 25,596 kB)
heap used: +410 kB -- (5,380 kB -> 5,789 kB)
-- 1.914 ms avg per iteration --

94
node_modules/lru_map/example.html generated vendored Executable file
View File

@@ -0,0 +1,94 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>lru</title>
<style type="text/css">
body {
font-family:
-apple-system,BlinkMacSystemFont,
"Segoe UI",
Roboto,
Oxygen,
Ubuntu,
Cantarell,
"Open Sans",
"Helvetica Neue",
sans-serif;
letter-spacing:0;
font-weight:400;
font-style:normal;
text-rendering:optimizeLegibility;
-moz-font-feature-settings:"liga" on;
background: white;
color:rgba(0,0,0,.8);
font-size:13px;
line-height:1.4;
margin:2em;
}
a { text-decoration:inherit; color: inherit; }
a:hover { color:#30C2FF; }
b { font-weight: 500; }
ul { list-style:none; }
li { line-height:1.4; }
h2 { font-size: 16px; font-weight:600; }
.log {
background: rgba(0,0,0,0.05);
white-space: pre-line;
display: inline-block;
}
#out {
}
</style>
<!-- <script src="https://unpkg.com/amdld/amdld.min.js"></script> -->
<script src="lru.js"></script>
</head>
<body>
<h2>Expected results:</h2>
<div class="log">adam:29 &lt; john:26 &lt; angela:24
adam:29 &lt; angela:24 &lt; john:26
angela:24 &lt; john:26 &lt; zorro:141
</div>
<h2>Actual results:</h2>
<div class="log" id="out"></div>
<script>
const out = document.querySelector('#out')
function log(s) { out.innerText += s + '\n' }
let c = new LRUMap(3)
c.set('adam', 29)
c.set('john', 26)
c.set('angela', 24)
log(c.toString()) // -> "adam:29 < john:26 < angela:24"
c.get('john') // -> 26
// Now 'john' is the most recently used entry, since we just requested it
log(c.toString()) // -> "adam:29 < angela:24 < john:26"
c.set('zorro', 141) // -> {key:adam, value:29}
// Because we only have room for 3 entries, adding 'zorro' caused 'adam'
// to be removed in order to make room for the new entry
log(c.toString()) // -> "angela:24 < john:26 < zorro:141"
// With AMDLD:
// define(['lru'], function(lru) {
// let c = new lru.LRUMap(3)
// // ...
// })
</script>
</body>
</html>

83
node_modules/lru_map/lru.d.ts generated vendored Normal file
View File

@@ -0,0 +1,83 @@
// An entry holds the key and value, and pointers to any older and newer entries.
interface Entry<K,V> {
key :K;
value :V;
}
export class LRUMap<K,V> {
// Construct a new cache object which will hold up to limit entries.
// When the size == limit, a `put` operation will evict the oldest entry.
//
// If `entries` is provided, all entries are added to the new map.
// `entries` should be an Array or other iterable object whose elements are
// key-value pairs (2-element Arrays). Each key-value pair is added to the new Map.
// null is treated as undefined.
constructor(limit :number, entries? :Iterable<[K,V]>);
// Convenience constructor equivalent to `new LRUMap(count(entries), entries)`
constructor(entries :Iterable<[K,V]>);
// Current number of items
size :number;
// Maximum number of items this map can hold
limit :number;
// Least recently-used entry. Invalidated when map is modified.
oldest :Entry<K,V>;
// Most recently-used entry. Invalidated when map is modified.
newest :Entry<K,V>;
// Replace all values in this map with key-value pairs (2-element Arrays) from
// provided iterable.
assign(entries :Iterable<[K,V]>) : void;
// Put <value> into the cache associated with <key>. Replaces any existing entry
// with the same key. Returns `this`.
set(key :K, value :V) : LRUMap<K,V>;
// Purge the least recently used (oldest) entry from the cache.
// Returns the removed entry or undefined if the cache was empty.
shift() : [K,V] | undefined;
// Get and register recent use of <key>.
// Returns the value associated with <key> or undefined if not in cache.
get(key :K) : V | undefined;
// Check if there's a value for key in the cache without registering recent use.
has(key :K) : boolean;
// Access value for <key> without registering recent use. Useful if you do not
// want to chage the state of the map, but only "peek" at it.
// Returns the value associated with <key> if found, or undefined if not found.
find(key :K) : V | undefined;
// Remove entry <key> from cache and return its value.
// Returns the removed value, or undefined if not found.
delete(key :K) : V | undefined;
// Removes all entries
clear() : void;
// Returns an iterator over all keys, starting with the oldest.
keys() : Iterator<K>;
// Returns an iterator over all values, starting with the oldest.
values() : Iterator<V>;
// Returns an iterator over all entries, starting with the oldest.
entries() : Iterator<[K,V]>;
// Returns an iterator over all entries, starting with the oldest.
[Symbol.iterator]() : Iterator<[K,V]>;
// Call `fun` for each entry, starting with the oldest entry.
forEach(fun :(value :V, key :K, m :LRUMap<K,V>)=>void, thisArg? :any) : void;
// Returns an object suitable for JSON encoding
toJSON() : Array<{key :K, value :V}>;
// Returns a human-readable text representation
toString() : string;
}

305
node_modules/lru_map/lru.js generated vendored Normal file
View File

@@ -0,0 +1,305 @@
/**
* A doubly linked list-based Least Recently Used (LRU) cache. Will keep most
* recently used items while discarding least recently used items when its limit
* is reached.
*
* Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
* See README.md for details.
*
* Illustration of the design:
*
* entry entry entry entry
* ______ ______ ______ ______
* | head |.newer => | |.newer => | |.newer => | tail |
* | A | | B | | C | | D |
* |______| <= older.|______| <= older.|______| <= older.|______|
*
* removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
*/
(function(g,f){
const e = typeof exports == 'object' ? exports : typeof g == 'object' ? g : {};
f(e);
if (typeof define == 'function' && define.amd) { define('lru', e); }
})(this, function(exports) {
const NEWER = Symbol('newer');
const OLDER = Symbol('older');
function LRUMap(limit, entries) {
if (typeof limit !== 'number') {
// called as (entries)
entries = limit;
limit = 0;
}
this.size = 0;
this.limit = limit;
this.oldest = this.newest = undefined;
this._keymap = new Map();
if (entries) {
this.assign(entries);
if (limit < 1) {
this.limit = this.size;
}
}
}
exports.LRUMap = LRUMap;
function Entry(key, value) {
this.key = key;
this.value = value;
this[NEWER] = undefined;
this[OLDER] = undefined;
}
LRUMap.prototype._markEntryAsUsed = function(entry) {
if (entry === this.newest) {
// Already the most recenlty used entry, so no need to update the list
return;
}
// HEAD--------------TAIL
// <.older .newer>
// <--- add direction --
// A B C <D> E
if (entry[NEWER]) {
if (entry === this.oldest) {
this.oldest = entry[NEWER];
}
entry[NEWER][OLDER] = entry[OLDER]; // C <-- E.
}
if (entry[OLDER]) {
entry[OLDER][NEWER] = entry[NEWER]; // C. --> E
}
entry[NEWER] = undefined; // D --x
entry[OLDER] = this.newest; // D. --> E
if (this.newest) {
this.newest[NEWER] = entry; // E. <-- D
}
this.newest = entry;
};
LRUMap.prototype.assign = function(entries) {
let entry, limit = this.limit || Number.MAX_VALUE;
this._keymap.clear();
let it = entries[Symbol.iterator]();
for (let itv = it.next(); !itv.done; itv = it.next()) {
let e = new Entry(itv.value[0], itv.value[1]);
this._keymap.set(e.key, e);
if (!entry) {
this.oldest = e;
} else {
entry[NEWER] = e;
e[OLDER] = entry;
}
entry = e;
if (limit-- == 0) {
throw new Error('overflow');
}
}
this.newest = entry;
this.size = this._keymap.size;
};
LRUMap.prototype.get = function(key) {
// First, find our cache entry
var entry = this._keymap.get(key);
if (!entry) return; // Not cached. Sorry.
// As <key> was found in the cache, register it as being requested recently
this._markEntryAsUsed(entry);
return entry.value;
};
LRUMap.prototype.set = function(key, value) {
var entry = this._keymap.get(key);
if (entry) {
// update existing
entry.value = value;
this._markEntryAsUsed(entry);
return this;
}
// new entry
this._keymap.set(key, (entry = new Entry(key, value)));
if (this.newest) {
// link previous tail to the new tail (entry)
this.newest[NEWER] = entry;
entry[OLDER] = this.newest;
} else {
// we're first in -- yay
this.oldest = entry;
}
// add new entry to the end of the linked list -- it's now the freshest entry.
this.newest = entry;
++this.size;
if (this.size > this.limit) {
// we hit the limit -- remove the head
this.shift();
}
return this;
};
LRUMap.prototype.shift = function() {
// todo: handle special case when limit == 1
var entry = this.oldest;
if (entry) {
if (this.oldest[NEWER]) {
// advance the list
this.oldest = this.oldest[NEWER];
this.oldest[OLDER] = undefined;
} else {
// the cache is exhausted
this.oldest = undefined;
this.newest = undefined;
}
// Remove last strong reference to <entry> and remove links from the purged
// entry being returned:
entry[NEWER] = entry[OLDER] = undefined;
this._keymap.delete(entry.key);
--this.size;
return [entry.key, entry.value];
}
};
// ----------------------------------------------------------------------------
// Following code is optional and can be removed without breaking the core
// functionality.
LRUMap.prototype.find = function(key) {
let e = this._keymap.get(key);
return e ? e.value : undefined;
};
LRUMap.prototype.has = function(key) {
return this._keymap.has(key);
};
LRUMap.prototype['delete'] = function(key) {
var entry = this._keymap.get(key);
if (!entry) return;
this._keymap.delete(entry.key);
if (entry[NEWER] && entry[OLDER]) {
// relink the older entry with the newer entry
entry[OLDER][NEWER] = entry[NEWER];
entry[NEWER][OLDER] = entry[OLDER];
} else if (entry[NEWER]) {
// remove the link to us
entry[NEWER][OLDER] = undefined;
// link the newer entry to head
this.oldest = entry[NEWER];
} else if (entry[OLDER]) {
// remove the link to us
entry[OLDER][NEWER] = undefined;
// link the newer entry to head
this.newest = entry[OLDER];
} else {// if(entry[OLDER] === undefined && entry.newer === undefined) {
this.oldest = this.newest = undefined;
}
this.size--;
return entry.value;
};
LRUMap.prototype.clear = function() {
// Not clearing links should be safe, as we don't expose live links to user
this.oldest = this.newest = undefined;
this.size = 0;
this._keymap.clear();
};
function EntryIterator(oldestEntry) { this.entry = oldestEntry; }
EntryIterator.prototype[Symbol.iterator] = function() { return this; }
EntryIterator.prototype.next = function() {
let ent = this.entry;
if (ent) {
this.entry = ent[NEWER];
return { done: false, value: [ent.key, ent.value] };
} else {
return { done: true, value: undefined };
}
};
function KeyIterator(oldestEntry) { this.entry = oldestEntry; }
KeyIterator.prototype[Symbol.iterator] = function() { return this; }
KeyIterator.prototype.next = function() {
let ent = this.entry;
if (ent) {
this.entry = ent[NEWER];
return { done: false, value: ent.key };
} else {
return { done: true, value: undefined };
}
};
function ValueIterator(oldestEntry) { this.entry = oldestEntry; }
ValueIterator.prototype[Symbol.iterator] = function() { return this; }
ValueIterator.prototype.next = function() {
let ent = this.entry;
if (ent) {
this.entry = ent[NEWER];
return { done: false, value: ent.value };
} else {
return { done: true, value: undefined };
}
};
LRUMap.prototype.keys = function() {
return new KeyIterator(this.oldest);
};
LRUMap.prototype.values = function() {
return new ValueIterator(this.oldest);
};
LRUMap.prototype.entries = function() {
return this;
};
LRUMap.prototype[Symbol.iterator] = function() {
return new EntryIterator(this.oldest);
};
LRUMap.prototype.forEach = function(fun, thisObj) {
if (typeof thisObj !== 'object') {
thisObj = this;
}
let entry = this.oldest;
while (entry) {
fun.call(thisObj, entry.value, entry.key, this);
entry = entry[NEWER];
}
};
/** Returns a JSON (array) representation */
LRUMap.prototype.toJSON = function() {
var s = new Array(this.size), i = 0, entry = this.oldest;
while (entry) {
s[i++] = { key: entry.key, value: entry.value };
entry = entry[NEWER];
}
return s;
};
/** Returns a String representation */
LRUMap.prototype.toString = function() {
var s = '', entry = this.oldest;
while (entry) {
s += String(entry.key)+':'+entry.value;
entry = entry[NEWER];
if (entry) {
s += ' < ';
}
}
return s;
};
});

31
node_modules/lru_map/package.json generated vendored Normal file
View File

@@ -0,0 +1,31 @@
{
"name": "lru_map",
"version": "0.3.3",
"description": "Finite key-value map using the Least Recently Used (LRU) algorithm where the most recently used objects are keept in the map while less recently used items are evicted to make room for new ones.",
"main": "lru.js",
"typings": "lru.d.ts",
"scripts": {
"test": "node test.js && echo 'Verifying TypeScript definition...' && tsc --noEmit",
"prepublish": "npm test",
"benchmark": "node --expose-gc benchmark.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rsms/js-lru.git"
},
"keywords": [
"cache",
"lru",
"buffer",
"map"
],
"author": "Rasmus Andersson <me@rsms.me>",
"license": "MIT",
"bugs": {
"url": "https://github.com/rsms/js-lru/issues"
},
"homepage": "https://github.com/rsms/js-lru#readme",
"devDependencies": {
"typescript": "^2.0.10"
}
}

336
node_modules/lru_map/test.js generated vendored Normal file
View File

@@ -0,0 +1,336 @@
// Test which will run in nodejs
// $ node test.js
// (Might work with other CommonJS-compatible environments)
const assert = require('assert');
const LRUMap = require('./lru').LRUMap;
const asserteq = assert.equal;
const tests = {
['set and get']() {
let c = new LRUMap(4);
asserteq(c.size, 0);
asserteq(c.limit, 4);
asserteq(c.oldest, undefined);
asserteq(c.newest, undefined);
c.set('adam', 29)
.set('john', 26)
.set('angela', 24)
.set('bob', 48);
asserteq(c.toString(), 'adam:29 < john:26 < angela:24 < bob:48');
asserteq(c.size, 4);
asserteq(c.get('adam'), 29);
asserteq(c.get('john'), 26);
asserteq(c.get('angela'), 24);
asserteq(c.get('bob'), 48);
asserteq(c.toString(), 'adam:29 < john:26 < angela:24 < bob:48');
asserteq(c.get('angela'), 24);
asserteq(c.toString(), 'adam:29 < john:26 < bob:48 < angela:24');
c.set('ygwie', 81);
asserteq(c.toString(), 'john:26 < bob:48 < angela:24 < ygwie:81');
asserteq(c.size, 4);
asserteq(c.get('adam'), undefined);
c.set('john', 11);
asserteq(c.toString(), 'bob:48 < angela:24 < ygwie:81 < john:11');
asserteq(c.get('john'), 11);
let expectedKeys = ['bob', 'angela', 'ygwie', 'john'];
c.forEach(function(v, k) {
//sys.sets(k+': '+v);
asserteq(k, expectedKeys.shift());
})
// removing one item decrements size by one
let currentSize = c.size;
assert(c.delete('john') !== undefined);
asserteq(currentSize - 1, c.size);
},
['construct with iterator']() {
let verifyEntries = function(c) {
asserteq(c.size, 4);
asserteq(c.limit, 4);
asserteq(c.oldest.key, 'adam');
asserteq(c.newest.key, 'bob');
asserteq(c.get('adam'), 29);
asserteq(c.get('john'), 26);
asserteq(c.get('angela'), 24);
asserteq(c.get('bob'), 48);
};
// with explicit limit
verifyEntries(new LRUMap(4, [
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]));
// with inferred limit
verifyEntries(new LRUMap([
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]));
},
assign() {
let c = new LRUMap([
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]);
let newEntries = [
['mimi', 1],
['patrick', 2],
['jane', 3],
['fred', 4],
];
c.assign(newEntries);
asserteq(c.size, 4);
asserteq(c.limit, 4);
asserteq(c.oldest.key, newEntries[0][0]);
asserteq(c.newest.key, newEntries[newEntries.length-1][0]);
let i = 0;
c.forEach(function(v, k) {
asserteq(k, newEntries[i][0]);
asserteq(v, newEntries[i][1]);
i++;
});
// assigning too many items should throw an exception
assert.throws(() => {
c.assign([
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
['ken', 30],
]);
}, /overflow/);
// assigning less than limit should not affect limit but adjust size
c.assign([
['adam', 29],
['john', 26],
['angela', 24],
]);
asserteq(c.size, 3);
asserteq(c.limit, 4);
},
delete() {
let c = new LRUMap([
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]);
c.delete('adam');
asserteq(c.size, 3);
c.delete('angela');
asserteq(c.size, 2);
c.delete('bob');
asserteq(c.size, 1);
c.delete('john');
asserteq(c.size, 0);
asserteq(c.oldest, undefined);
asserteq(c.newest, undefined);
},
clear() {
let c = new LRUMap(4);
c.set('adam', 29);
c.set('john', 26);
asserteq(c.size, 2);
c.clear();
asserteq(c.size, 0);
asserteq(c.oldest, undefined);
asserteq(c.newest, undefined);
},
shift() {
let c2 = new LRUMap(4);
asserteq(c2.size, 0);
c2.set('a', 1)
c2.set('b', 2)
c2.set('c', 3)
asserteq(c2.size, 3);
let e = c2.shift();
asserteq(e[0], 'a');
asserteq(e[1], 1);
e = c2.shift();
asserteq(e[0], 'b');
asserteq(e[1], 2);
e = c2.shift();
asserteq(e[0], 'c');
asserteq(e[1], 3);
// c2 should be empty
c2.forEach(function () { assert(false); });
asserteq(c2.size, 0);
},
set() {
// Note: v0.1 allows putting same key multiple times. v0.2 does not.
c = new LRUMap(4);
c.set('a', 1);
c.set('a', 2);
c.set('a', 3);
c.set('a', 4);
asserteq(c.size, 1);
asserteq(c.newest, c.oldest);
assert.deepEqual(c.newest, {key:'a', value:4 });
c.set('a', 5);
asserteq(c.size, 1);
asserteq(c.newest, c.oldest);
assert.deepEqual(c.newest, {key:'a', value:5 });
c.set('b', 6);
asserteq(c.size, 2);
assert(c.newest !== c.oldest);
assert.deepEqual(c.newest, { key:'b', value:6 });
assert.deepEqual(c.oldest, { key:'a', value:5 });
c.shift();
asserteq(c.size, 1);
c.shift();
asserteq(c.size, 0);
c.forEach(function(){ assert(false) });
},
['entry iterator']() {
let c = new LRUMap(4, [
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]);
let verifyEntries = function(iterable) {
asserteq(typeof iterable[Symbol.iterator], 'function');
let it = iterable[Symbol.iterator]();
assert.deepEqual(it.next().value, ['adam', 29]);
assert.deepEqual(it.next().value, ['john', 26]);
assert.deepEqual(it.next().value, ['angela', 24]);
assert.deepEqual(it.next().value, ['bob', 48]);
assert(it.next().done);
};
verifyEntries(c);
verifyEntries(c.entries());
},
['key iterator']() {
let c = new LRUMap(4, [
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]);
let kit = c.keys();
asserteq(kit.next().value, 'adam');
asserteq(kit.next().value, 'john');
asserteq(kit.next().value, 'angela');
asserteq(kit.next().value, 'bob');
assert(kit.next().done);
},
['value iterator']() {
let c = new LRUMap(4, [
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]);
let kit = c.values();
asserteq(kit.next().value, 29);
asserteq(kit.next().value, 26);
asserteq(kit.next().value, 24);
asserteq(kit.next().value, 48);
assert(kit.next().done);
},
toJSON() {
let c = new LRUMap(4, [
['adam', 29],
['john', 26],
['angela', 24],
['bob', 48],
]);
let json = c.toJSON();
assert(json.length == 4);
assert.deepEqual(json, [
{key:'adam', value:29},
{key:'john', value:26},
{key:'angela', value:24},
{key:'bob', value:48},
]);
},
}; // tests
function fmttime(t) {
return (Math.round((t)*10)/10)+'ms';
}
function die(err) {
console.error('\n' + (err.stack || err));
process.exit(1);
}
function runNextTest(tests, testNames, allDoneCallback) {
let testName = testNames[0];
if (!testName) {
return allDoneCallback();
}
process.stdout.write(testName+' ... ');
let t1 = Date.now();
let next = function() {
t1 = Date.now() - t1;
if (t1 > 10) {
process.stdout.write('ok ('+fmttime(t1)+')\n');
} else {
process.stdout.write('ok\n');
}
runNextTest(tests, testNames.slice(1), allDoneCallback);
};
try {
let p = tests[testName]();
if (p && p instanceof Promise) {
p.then(next).catch(die);
} else {
next();
}
} catch (err) {
die(err);
}
}
let t = Date.now();
runNextTest(tests, Object.keys(tests), function() {
t = Date.now() - t;
let timestr = '';
if (t > 10) {
timestr = '(' + fmttime(t) + ')';
}
console.log(`${Object.keys(tests).length} tests passed ${timestr}`);
});

16
node_modules/lru_map/tsconfig.json generated vendored Normal file
View File

@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": false,
"lib": [
"es6",
"dom"
],
"pretty": true
},
"files": [
"tstest.ts"
]
}

6
node_modules/lru_map/tstest.ts generated vendored Normal file
View File

@@ -0,0 +1,6 @@
import {LRUMap} from './lru'
let m = new LRUMap<string, number>(3);
let entit = m.entries();
let k : string = entit.next().value[0];
let v : number = entit.next().value[1];