All files / src/lib/modules storage.ts

20.69% Statements 6/29
0% Branches 0/4
0% Functions 0/8
22.22% Lines 6/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 501x 1x 1x 1x     1x   1x                                                                                  
import {Module} from "../module";
import {Util} from "../util";
import {on} from "../decorators";
import {EVENT} from "../enums";
 
 
export class Storage extends Module {
    @on(EVENT.ON_PAGE_LOAD)
    static async end() {
        const applicationCacheStorageList = await Storage.printApplicationCacheInfo();
 
        Util.wrapGroup('PuzzleJs', 'Debug Mode - Storage', () => {
            if (window.caches) {
                Util.wrapGroup('PuzzleJs', 'Application Cache', () => {
                    Util.table(applicationCacheStorageList);
                });
            }
        });
    }
 
    static async printApplicationCacheInfo() {
        const cacheNames = await caches.keys();
        let storageList: { [key: string]: any } = {
            total: 0
        };
 
        const sizePromises = cacheNames.map(async cacheName => {
            const cache = await caches.open(cacheName);
            const keys = await cache.keys();
            let cacheSize = 0;
 
            await Promise.all(keys.map(async key => {
                const response = await cache.match(key);
                if (response) {
                    const blob = await response.blob();
                    storageList.total += blob.size;
                    cacheSize += blob.size;
                }
            }));
 
            storageList[cacheName] = `${cacheSize} bytes`;
        });
 
        await Promise.all(sizePromises);
 
        storageList.total = `${storageList.total} bytes`;
        return storageList;
    }
}