BREAKING CHANGE(core): switch to esm

This commit is contained in:
2022-03-22 22:45:12 +01:00
parent 9feaa921bf
commit 716d805723
18 changed files with 16306 additions and 8513 deletions

View File

@ -1,2 +1,2 @@
export * from './levelcache.classes.levelcache';
export * from './levelcache.classes.cacheentry';
export * from './levelcache.classes.levelcache.js';
export * from './levelcache.classes.cacheentry.js';

View File

@ -1,4 +1,4 @@
import { CacheEntry } from './levelcache.classes.cacheentry';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
export abstract class AbstractCache {
public abstract ready: Promise<void>;
@ -24,7 +24,7 @@ export abstract class AbstractCache {
/**
* delete a key
*/
public abstract deleteCacheEntryByKey(keyArg: string): Promise<void>;
public abstract deleteCacheEntryByKey(keyArg: string): Promise<void>;
/**
* clean the cache

View File

@ -1,8 +1,8 @@
import * as plugins from './levelcache.plugins';
import * as paths from './levelcache.paths';
import { AbstractCache } from './levelcache.abstract.classes.cache';
import { ILevelCacheConstructorOptions, LevelCache } from './levelcache.classes.levelcache';
import { CacheEntry } from './levelcache.classes.cacheentry';
import * as plugins from './levelcache.plugins.js';
import * as paths from './levelcache.paths.js';
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
import { ILevelCacheConstructorOptions, LevelCache } from './levelcache.classes.levelcache.js';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
/**
*
@ -59,7 +59,6 @@ export class CacheDiskManager extends AbstractCache {
await plugins.smartfile.fs.remove(plugins.path.join(this.fsPath, encodeURIComponent(keyArg)));
}
public async cleanOutdated() {}
public async cleanAll() {

View File

@ -1,7 +1,7 @@
import * as plugins from './levelcache.plugins';
import { AbstractCache } from './levelcache.abstract.classes.cache';
import { CacheEntry } from './levelcache.classes.cacheentry';
import { ILevelCacheConstructorOptions, LevelCache } from './levelcache.classes.levelcache';
import * as plugins from './levelcache.plugins.js';
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
import { ILevelCacheConstructorOptions, LevelCache } from './levelcache.classes.levelcache.js';
export class CacheMemoryManager extends AbstractCache {
private levelCacheRef: LevelCache;

View File

@ -1,7 +1,7 @@
import * as plugins from './levelcache.plugins';
import { AbstractCache } from './levelcache.abstract.classes.cache';
import { LevelCache } from './levelcache.classes.levelcache';
import { CacheEntry } from './levelcache.classes.cacheentry';
import * as plugins from './levelcache.plugins.js';
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
import { LevelCache } from './levelcache.classes.levelcache.js';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
/**
*
@ -26,9 +26,9 @@ export class CacheS3Manager extends AbstractCache {
if (this.levelCacheRef.options.s3Config) {
this.smartbucket = new plugins.smartbucket.SmartBucket(this.levelCacheRef.options.s3Config);
this.s3CacheBucket = await this.smartbucket.getBucketByName('');
this.s3CacheDir = await (await this.s3CacheBucket.getBaseDirectory()).getSubDirectoryByName(
this.levelCacheRef.options.cacheId
);
this.s3CacheDir = await (
await this.s3CacheBucket.getBaseDirectory()
).getSubDirectoryByName(this.levelCacheRef.options.cacheId);
if (this.levelCacheRef.options.maxS3StorageInMB) {
console.log(`cache level S3 activated with ${this.levelCacheRef.options.maxS3StorageInMB}`);
} else {
@ -65,7 +65,7 @@ export class CacheS3Manager extends AbstractCache {
}
public async deleteCacheEntryByKey(keyArg: string) {
if(this.status === 'active') {
if (this.status === 'active') {
await this.s3CacheDir.fastRemove(encodeURIComponent(keyArg));
}
}

View File

@ -1,4 +1,4 @@
import * as plugins from './levelcache.plugins';
import * as plugins from './levelcache.plugins.js';
export interface ICacheEntryConstructorOptions {
key?: string;
@ -12,7 +12,8 @@ export interface ICacheEntryConstructorOptions {
*/
export class CacheEntry
extends plugins.smartjson.Smartjson
implements ICacheEntryConstructorOptions {
implements ICacheEntryConstructorOptions
{
public static fromStorageJsonString(storageJsonString: string) {
return new CacheEntry(plugins.smartjson.parse(storageJsonString));
}
@ -23,15 +24,15 @@ export class CacheEntry
@plugins.smartjson.foldDec()
public ttl: number;
@plugins.smartjson.foldDec()
public createdAt: number;
@plugins.smartjson.foldDec()
public typeInfo: string;
@plugins.smartjson.foldDec()
contents: Buffer;
@plugins.smartjson.foldDec()
public createdAt: number;
public toStorageJsonString(): string {
return this.foldToJson();
}

View File

@ -1,7 +1,7 @@
import * as plugins from './levelcache.plugins';
import { LevelCache } from './levelcache.classes.levelcache';
import { AbstractCache } from './levelcache.abstract.classes.cache';
import { CacheEntry } from './levelcache.classes.cacheentry';
import * as plugins from './levelcache.plugins.js';
import { LevelCache } from './levelcache.classes.levelcache.js';
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
export class CacheRouter {
public levelCacheRef: LevelCache;
@ -14,24 +14,31 @@ export class CacheRouter {
/**
* gets the relevant cache to perform a store action on
*/
async getCacheForStoreAction(keyArg: string, cacheEntry: CacheEntry): Promise<AbstractCache> {
async getCacheForStoreAction (keyArg: string, cacheEntry: CacheEntry): Promise<AbstractCache> {
let returnCache: AbstractCache;
const mbToBytesMultiplier = 1000 * 1000;
const maxMemoryBytes = this.levelCacheRef.options.maxMemoryStorageInMB * mbToBytesMultiplier;
const maxDiskBytes = this.levelCacheRef.options.maxDiskStorageInMB * mbToBytesMultiplier;
const maxS3Bytes = this.levelCacheRef.options.maxS3StorageInMB * mbToBytesMultiplier;
switch (true) {
case cacheEntry.contents.byteLength <= 500:
case cacheEntry.contents.byteLength <= maxMemoryBytes &&
this.levelCacheRef.cacheMemoryManager.status === 'active':
returnCache = this.levelCacheRef.cacheMemoryManager;
break;
case this.levelCacheRef.cacheDiskManager.status === 'active' &&
cacheEntry.contents.byteLength >= 500 &&
(cacheEntry.contents.byteLength < 10000 ||
this.levelCacheRef.cacheS3Manager.status === 'inactive'):
cacheEntry.contents.byteLength > maxMemoryBytes &&
cacheEntry.contents.byteLength <= maxDiskBytes &&
this.levelCacheRef.cacheDiskManager.status === 'active':
returnCache = this.levelCacheRef.cacheDiskManager;
break;
case cacheEntry.contents.byteLength >= 10000 &&
case cacheEntry.contents.byteLength > maxDiskBytes &&
cacheEntry.contents.byteLength < maxS3Bytes &&
this.levelCacheRef.cacheS3Manager.status === 'active':
returnCache = this.levelCacheRef.cacheS3Manager;
break;
default:
returnCache = this.levelCacheRef.cacheMemoryManager;
returnCache = null;
}
this.cacheKeyMap.addToMap(keyArg, returnCache);
return returnCache;
@ -40,7 +47,7 @@ export class CacheRouter {
/**
* gets the relevant cache to perform a retrieval action on
*/
async getCacheForRetrieveAction(keyArg: string): Promise<AbstractCache> {
async getCacheForRetrieveAction (keyArg: string): Promise<AbstractCache> {
const done = plugins.smartpromise.defer<AbstractCache>();
const returnCache = this.cacheKeyMap.getByKey(keyArg);
if (!returnCache && this.levelCacheRef.options.persistentCache) {

View File

@ -1,10 +1,10 @@
import * as plugins from './levelcache.plugins';
import { CacheDiskManager } from './levelcache.classes.cache.diskmanager';
import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager';
import { CacheS3Manager } from './levelcache.classes.cache.s3manager';
import { CacheEntry } from './levelcache.classes.cacheentry';
import { CacheRouter } from './levelcache.classes.cacherouter';
import { AbstractCache } from './levelcache.abstract.classes.cache';
import * as plugins from './levelcache.plugins.js';
import { CacheDiskManager } from './levelcache.classes.cache.diskmanager.js';
import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager.js';
import { CacheS3Manager } from './levelcache.classes.cache.s3manager.js';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
import { CacheRouter } from './levelcache.classes.cacherouter.js';
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
export interface ILevelCacheConstructorOptions {
/**
@ -42,13 +42,18 @@ export class LevelCache extends AbstractCache {
constructor(optionsArg: ILevelCacheConstructorOptions) {
super();
this.options = optionsArg;
this.options = {
maxMemoryStorageInMB: 0.5,
maxDiskStorageInMB: 10,
maxS3StorageInMB: 50,
...optionsArg
};
this.init();
}
public async init() {
this.cacheDiskManager = new CacheDiskManager(this);
this.cacheMemoryManager = new CacheMemoryManager(this);
this.cacheDiskManager = new CacheDiskManager(this);
this.cacheS3Manager = new CacheS3Manager(this);
await Promise.all([
this.cacheDiskManager.ready,

View File

@ -1,4 +1,7 @@
import * as plugins from './levelcache.plugins';
import * as plugins from './levelcache.plugins.js';
export const packageDir = plugins.path.join(__dirname, '../');
export const packageDir = plugins.path.join(
plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
'../'
);
export const nogitDir = plugins.path.join(packageDir, '.nogit/');

View File

@ -9,6 +9,7 @@ import * as smartbucket from '@pushrocks/smartbucket';
import * as smartcache from '@pushrocks/smartcache';
import * as smartfile from '@pushrocks/smartfile';
import * as smartjson from '@pushrocks/smartjson';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartstring from '@pushrocks/smartstring';
import * as smartunique from '@pushrocks/smartunique';
@ -20,6 +21,7 @@ export {
smartcache,
smartfile,
smartjson,
smartpath,
smartpromise,
smartstring,
smartunique,