chrome-remote-cache
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "chrome-remote-cache", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Caching system for chrome devtools protocol", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -19,3 +19,3 @@ #chrome-remote-cache | ||
const page = await devtools.connectNewPage(); | ||
const cm = new CacheManager(); | ||
const cm = new CacheManager(new Redis()); | ||
const remoteCache = new ChromeRemoteCache(cm); | ||
@@ -27,6 +27,7 @@ remoteCache.cache('www.google.com/maps/dir///'); | ||
remoteCache.cache('www.google.com/maps/preview/pwa/manifest'); | ||
// www.gstatic.com, maps.gstatic.com, fonts.gstatic.com | ||
// www.gstatic.com, maps.gstatic.com, fonts.gstatic.com | ||
remoteCache.cache('*.gstatic.com'); | ||
remoteCache.cache('fonts.googleapis.com'); | ||
remoteCache.cache('lh3.googleusercontent.com'); | ||
remoteCache.ignore('ogs.google.com'); | ||
@@ -40,11 +41,13 @@ remoteCache.ignore('play.google.com'); | ||
remoteCache.ignore('www.google.com/maps/preview/log204'); | ||
await remoteCache.register(page); | ||
await page.Page.navigate({ url: 'https://www.google.com/maps/'}); | ||
await delay(10000); | ||
await delay(7000); | ||
console.log(); | ||
// display cache usage | ||
const {cache, pt} = remoteCache.getStats(); | ||
console.log('cache:', cache); | ||
console.log('passt:', pt); | ||
console.log('cache:', cache.toString(true)); | ||
console.log('passt:', pt.toString(true)); | ||
console.log(); | ||
console.log(); | ||
console.log(`cache efficency: ${(remoteCache.efficency*100).toFixed(1)}%`); | ||
} | ||
@@ -51,0 +54,0 @@ |
@@ -5,8 +5,12 @@ /** | ||
export declare class CacheStat { | ||
query: number; | ||
transfert: number; | ||
perDom: { | ||
[key: string]: number; | ||
}; | ||
#private; | ||
/** | ||
* get number of query | ||
*/ | ||
get query(): number; | ||
/** | ||
* get total bandwidh | ||
*/ | ||
get transfert(): number; | ||
/** | ||
* increate data usage from a particular url. | ||
@@ -13,0 +17,0 @@ * @param url |
@@ -13,6 +13,18 @@ "use strict"; | ||
class CacheStat { | ||
query = 0; | ||
transfert = 0; | ||
perDom = {}; | ||
#query = 0; | ||
#transfert = 0; | ||
#perDom = {}; | ||
/** | ||
* get number of query | ||
*/ | ||
get query() { | ||
return this.#query; | ||
} | ||
/** | ||
* get total bandwidh | ||
*/ | ||
get transfert() { | ||
return this.#transfert; | ||
} | ||
/** | ||
* increate data usage from a particular url. | ||
@@ -26,10 +38,10 @@ * @param url | ||
return; | ||
this.query++; | ||
this.#query++; | ||
let size = meta.length; | ||
if (length) | ||
size += length; | ||
this.transfert += size; | ||
this.#transfert += size; | ||
const [dom] = (0, CacheUtils_1.splitUrl)(url); | ||
let old = this.perDom[dom] || 0; | ||
this.perDom[dom] = old + size; | ||
let old = this.#perDom[dom] || 0; | ||
this.#perDom[dom] = old + size; | ||
} | ||
@@ -42,5 +54,5 @@ /** | ||
toString(full) { | ||
let out = `${picocolors_1.default.green((0, CacheUtils_1.formatSize)(this.transfert))} in ${picocolors_1.default.green(this.query.toString().padStart(3, ' '))} Query`; | ||
let out = `${picocolors_1.default.green((0, CacheUtils_1.formatSize)(this.#transfert))} in ${picocolors_1.default.green(this.#query.toString().padStart(3, ' '))} Query`; | ||
if (full) { | ||
const asArray = Object.entries(this.perDom); | ||
const asArray = Object.entries(this.#perDom); | ||
asArray.sort((a, b) => b[1] - a[1]); | ||
@@ -47,0 +59,0 @@ for (const [dom, size] of asArray) { |
import { CacheManager } from './CacheManager'; | ||
import { CacheStat } from './CacheStat'; | ||
import { Chrome } from "@u4/chrome-remote-interface"; | ||
@@ -16,2 +17,7 @@ export declare class ChromeRemoteCache { | ||
cache(...doms: string[]): void; | ||
/** | ||
* alias for cache(doms), ignore(doms) | ||
* @param doms url selections | ||
*/ | ||
cacheIgnore(...doms: string[]): void; | ||
cacheRemap(dom: string, mapping: (url: string) => string): void; | ||
@@ -31,6 +37,10 @@ /** | ||
getStats(): { | ||
cache: string; | ||
pt: string; | ||
cache: CacheStat; | ||
pt: CacheStat; | ||
}; | ||
/** | ||
* return cache | ||
*/ | ||
get efficency(): number; | ||
} | ||
export default ChromeRemoteCache; |
@@ -13,2 +13,9 @@ "use strict"; | ||
const dummy = (url) => url; | ||
const PREFIXS = { | ||
'cached': picocolors_1.default.bold(picocolors_1.default.green('cached')), | ||
'missing': picocolors_1.default.magenta('missing'), | ||
'blocked': picocolors_1.default.red('blocked'), | ||
'pass': picocolors_1.default.bold(picocolors_1.default.white('pass')), | ||
'addcache': picocolors_1.default.bgMagenta('ADD Cache'), | ||
}; | ||
// cache https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkliZ0tt-7UEKoB_KBN7v3D121PhkkA1JZRlvvV4Rv&s=10 | ||
@@ -40,2 +47,10 @@ class ChromeRemoteCache { | ||
} | ||
/** | ||
* alias for cache(doms), ignore(doms) | ||
* @param doms url selections | ||
*/ | ||
cacheIgnore(...doms) { | ||
this.cache(...doms); | ||
this.ignore(...doms); | ||
} | ||
cacheRemap(dom, mapping) { | ||
@@ -55,20 +70,3 @@ this.cacheDomain.add(dom, mapping); | ||
return; | ||
let prefix = ''; | ||
switch (type) { | ||
case 'cached': | ||
prefix = picocolors_1.default.bold(picocolors_1.default.green('cached')); | ||
break; | ||
case 'missing': | ||
prefix = picocolors_1.default.magenta('missing'); | ||
break; | ||
case 'blocked': | ||
prefix = picocolors_1.default.red('blocked'); | ||
break; | ||
case 'pass': | ||
prefix = picocolors_1.default.white('pass'); | ||
break; | ||
case 'addcache': | ||
prefix = picocolors_1.default.bgMagenta('ADD Cache'); | ||
break; | ||
} | ||
let prefix = PREFIXS[type]; | ||
this.#logfnc(`${prefix}: ${textUrl}`); | ||
@@ -200,4 +198,5 @@ } | ||
if (!resp) { | ||
console.log('No body for url:', url); | ||
return; | ||
resp = { body: '', base64Encoded: false }; | ||
// console.log(`No body (${status}) for url: ${url}`, ); | ||
// return; | ||
} | ||
@@ -234,8 +233,14 @@ const cacheKey = this.getCacheKey(url); | ||
return { | ||
cache: this.statCache.toString(), | ||
pt: this.statPassthrough.toString(), | ||
cache: this.statCache, | ||
pt: this.statPassthrough, | ||
}; | ||
} | ||
/** | ||
* return cache | ||
*/ | ||
get efficency() { | ||
return this.statCache.transfert / (this.statCache.transfert + this.statPassthrough.transfert); | ||
} | ||
} | ||
exports.ChromeRemoteCache = ChromeRemoteCache; | ||
exports.default = ChromeRemoteCache; |
28056
743
53