BREAKING CHANGE(core): switch to esm

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

View File

@ -12,6 +12,9 @@ stages:
- release - release
- metadata - metadata
before_script:
- npm install -g @shipzone/npmci
# ==================== # ====================
# security stage # security stage
# ==================== # ====================
@ -36,6 +39,7 @@ auditProductionDependencies:
- npmci command npm audit --audit-level=high --only=prod --production - npmci command npm audit --audit-level=high --only=prod --production
tags: tags:
- docker - docker
allow_failure: true
auditDevDependencies: auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
@ -96,10 +100,9 @@ codequality:
only: only:
- tags - tags
script: script:
- npmci command npm install -g tslint typescript - npmci command npm install -g typescript
- npmci npm prepare - npmci npm prepare
- npmci npm install - npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags: tags:
- lossless - lossless
- docker - docker

24
.vscode/launch.json vendored
View File

@ -2,28 +2,10 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "current file", "command": "npm test",
"type": "node", "name": "Run npm test",
"request": "launch", "request": "launch",
"args": [ "type": "node-terminal"
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
} }
] ]
} }

View File

@ -5,7 +5,7 @@
"githost": "gitlab.com", "githost": "gitlab.com",
"gitscope": "pushrocks", "gitscope": "pushrocks",
"gitrepo": "levelcache", "gitrepo": "levelcache",
"shortDescription": "a cache that uses memory/disk/s3 as backup", "description": "a cache that uses memory/disk/s3 as backup",
"npmPackagename": "@pushrocks/levelcache", "npmPackagename": "@pushrocks/levelcache",
"license": "MIT", "license": "MIT",
"projectDomain": "push.rocks" "projectDomain": "push.rocks"

24570
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
"description": "a cache that uses memory/disk/s3 as backup", "description": "a cache that uses memory/disk/s3 as backup",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Lossless GmbH", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
@ -12,24 +13,24 @@
"build": "(tsbuild --web)" "build": "(tsbuild --web)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.60",
"@gitzone/tstest": "^1.0.52", "@gitzone/tstest": "^1.0.69",
"@pushrocks/tapbundle": "^3.2.14", "@pushrocks/tapbundle": "^5.0.2",
"@types/node": "^14.14.41", "@types/node": "^17.0.22"
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/lik": "^4.0.20", "@pushrocks/lik": "^5.0.4",
"@pushrocks/smartbucket": "^1.0.39", "@pushrocks/smartbucket": "^1.0.44",
"@pushrocks/smartcache": "^1.0.13", "@pushrocks/smartcache": "^1.0.13",
"@pushrocks/smartexit": "^1.0.15", "@pushrocks/smartenv": "^5.0.0",
"@pushrocks/smartfile": "^8.0.9", "@pushrocks/smartexit": "^1.0.20",
"@pushrocks/smartfile": "^9.0.6",
"@pushrocks/smartjson": "^4.0.6", "@pushrocks/smartjson": "^4.0.6",
"@pushrocks/smartpromise": "^3.1.5", "@pushrocks/smartpath": "^5.0.5",
"@pushrocks/smartstring": "^3.0.24", "@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartstring": "^4.0.1",
"@pushrocks/smartunique": "^3.0.3", "@pushrocks/smartunique": "^3.0.3",
"@pushrocks/taskbuffer": "^2.1.13" "@pushrocks/taskbuffer": "^2.1.17"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

View File

@ -1,6 +1,6 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as levelcache from '../ts/index'; import * as levelcache from '../ts/index.js';
import { CacheEntry } from '../ts/index'; import { CacheEntry } from '../ts/index.js';
let testLevelCache: levelcache.LevelCache; let testLevelCache: levelcache.LevelCache;
@ -8,7 +8,7 @@ tap.test('should create a new levelcache instance', async () => {
testLevelCache = new levelcache.LevelCache({ testLevelCache = new levelcache.LevelCache({
cacheId: 'myCache', cacheId: 'myCache',
}); });
expect(testLevelCache).to.be.instanceOf(levelcache.LevelCache); expect(testLevelCache).toBeInstanceOf(levelcache.LevelCache);
}); });
tap.test('should cache a value', async () => { tap.test('should cache a value', async () => {
@ -21,7 +21,7 @@ tap.test('should cache a value', async () => {
}) })
); );
const result = await testLevelCache.retrieveCacheEntryByKey('mykey'); const result = await testLevelCache.retrieveCacheEntryByKey('mykey');
expect(result.contents.toString()).to.equal('heythere'); expect(result.contents.toString()).toEqual('heythere');
}); });
tap.test('should respect ttl', async (tools) => { tap.test('should respect ttl', async (tools) => {
@ -34,10 +34,10 @@ tap.test('should respect ttl', async (tools) => {
}) })
); );
const result = await testLevelCache.retrieveCacheEntryByKey('mykey'); const result = await testLevelCache.retrieveCacheEntryByKey('mykey');
expect(result.contents.toString()).to.equal('heythere'); expect(result.contents.toString()).toEqual('heythere');
await tools.delayFor(1100); await tools.delayFor(1100);
const result2 = await testLevelCache.retrieveCacheEntryByKey('mykey'); const result2 = await testLevelCache.retrieveCacheEntryByKey('mykey');
expect(result2).to.be.null; expect(result2).toBeNull();
}); });
tap.start(); tap.start();

View File

@ -1,2 +1,2 @@
export * from './levelcache.classes.levelcache'; export * from './levelcache.classes.levelcache.js';
export * from './levelcache.classes.cacheentry'; 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 { export abstract class AbstractCache {
public abstract ready: Promise<void>; public abstract ready: Promise<void>;

View File

@ -1,8 +1,8 @@
import * as plugins from './levelcache.plugins'; import * as plugins from './levelcache.plugins.js';
import * as paths from './levelcache.paths'; import * as paths from './levelcache.paths.js';
import { AbstractCache } from './levelcache.abstract.classes.cache'; import { AbstractCache } from './levelcache.abstract.classes.cache.js';
import { ILevelCacheConstructorOptions, LevelCache } from './levelcache.classes.levelcache'; import { ILevelCacheConstructorOptions, LevelCache } from './levelcache.classes.levelcache.js';
import { CacheEntry } from './levelcache.classes.cacheentry'; 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))); await plugins.smartfile.fs.remove(plugins.path.join(this.fsPath, encodeURIComponent(keyArg)));
} }
public async cleanOutdated() {} public async cleanOutdated() {}
public async cleanAll() { public async cleanAll() {

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
import * as plugins from './levelcache.plugins'; import * as plugins from './levelcache.plugins.js';
import { LevelCache } from './levelcache.classes.levelcache'; import { LevelCache } from './levelcache.classes.levelcache.js';
import { AbstractCache } from './levelcache.abstract.classes.cache'; import { AbstractCache } from './levelcache.abstract.classes.cache.js';
import { CacheEntry } from './levelcache.classes.cacheentry'; import { CacheEntry } from './levelcache.classes.cacheentry.js';
export class CacheRouter { export class CacheRouter {
public levelCacheRef: LevelCache; public levelCacheRef: LevelCache;
@ -16,22 +16,29 @@ export class CacheRouter {
*/ */
async getCacheForStoreAction (keyArg: string, cacheEntry: CacheEntry): Promise<AbstractCache> { async getCacheForStoreAction (keyArg: string, cacheEntry: CacheEntry): Promise<AbstractCache> {
let returnCache: 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) { switch (true) {
case cacheEntry.contents.byteLength <= 500: case cacheEntry.contents.byteLength <= maxMemoryBytes &&
this.levelCacheRef.cacheMemoryManager.status === 'active':
returnCache = this.levelCacheRef.cacheMemoryManager; returnCache = this.levelCacheRef.cacheMemoryManager;
break; break;
case this.levelCacheRef.cacheDiskManager.status === 'active' && case this.levelCacheRef.cacheDiskManager.status === 'active' &&
cacheEntry.contents.byteLength >= 500 && cacheEntry.contents.byteLength > maxMemoryBytes &&
(cacheEntry.contents.byteLength < 10000 || cacheEntry.contents.byteLength <= maxDiskBytes &&
this.levelCacheRef.cacheS3Manager.status === 'inactive'): this.levelCacheRef.cacheDiskManager.status === 'active':
returnCache = this.levelCacheRef.cacheDiskManager; returnCache = this.levelCacheRef.cacheDiskManager;
break; break;
case cacheEntry.contents.byteLength >= 10000 && case cacheEntry.contents.byteLength > maxDiskBytes &&
cacheEntry.contents.byteLength < maxS3Bytes &&
this.levelCacheRef.cacheS3Manager.status === 'active': this.levelCacheRef.cacheS3Manager.status === 'active':
returnCache = this.levelCacheRef.cacheS3Manager; returnCache = this.levelCacheRef.cacheS3Manager;
break; break;
default: default:
returnCache = this.levelCacheRef.cacheMemoryManager; returnCache = null;
} }
this.cacheKeyMap.addToMap(keyArg, returnCache); this.cacheKeyMap.addToMap(keyArg, returnCache);
return returnCache; return returnCache;

View File

@ -1,10 +1,10 @@
import * as plugins from './levelcache.plugins'; import * as plugins from './levelcache.plugins.js';
import { CacheDiskManager } from './levelcache.classes.cache.diskmanager'; import { CacheDiskManager } from './levelcache.classes.cache.diskmanager.js';
import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager'; import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager.js';
import { CacheS3Manager } from './levelcache.classes.cache.s3manager'; import { CacheS3Manager } from './levelcache.classes.cache.s3manager.js';
import { CacheEntry } from './levelcache.classes.cacheentry'; import { CacheEntry } from './levelcache.classes.cacheentry.js';
import { CacheRouter } from './levelcache.classes.cacherouter'; import { CacheRouter } from './levelcache.classes.cacherouter.js';
import { AbstractCache } from './levelcache.abstract.classes.cache'; import { AbstractCache } from './levelcache.abstract.classes.cache.js';
export interface ILevelCacheConstructorOptions { export interface ILevelCacheConstructorOptions {
/** /**
@ -42,13 +42,18 @@ export class LevelCache extends AbstractCache {
constructor(optionsArg: ILevelCacheConstructorOptions) { constructor(optionsArg: ILevelCacheConstructorOptions) {
super(); super();
this.options = optionsArg; this.options = {
maxMemoryStorageInMB: 0.5,
maxDiskStorageInMB: 10,
maxS3StorageInMB: 50,
...optionsArg
};
this.init(); this.init();
} }
public async init() { public async init() {
this.cacheDiskManager = new CacheDiskManager(this);
this.cacheMemoryManager = new CacheMemoryManager(this); this.cacheMemoryManager = new CacheMemoryManager(this);
this.cacheDiskManager = new CacheDiskManager(this);
this.cacheS3Manager = new CacheS3Manager(this); this.cacheS3Manager = new CacheS3Manager(this);
await Promise.all([ await Promise.all([
this.cacheDiskManager.ready, 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/'); 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 smartcache from '@pushrocks/smartcache';
import * as smartfile from '@pushrocks/smartfile'; import * as smartfile from '@pushrocks/smartfile';
import * as smartjson from '@pushrocks/smartjson'; import * as smartjson from '@pushrocks/smartjson';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as smartstring from '@pushrocks/smartstring'; import * as smartstring from '@pushrocks/smartstring';
import * as smartunique from '@pushrocks/smartunique'; import * as smartunique from '@pushrocks/smartunique';
@ -20,6 +21,7 @@ export {
smartcache, smartcache,
smartfile, smartfile,
smartjson, smartjson,
smartpath,
smartpromise, smartpromise,
smartstring, smartstring,
smartunique, smartunique,

View File

@ -1,7 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"experimentalDecorators": true, "experimentalDecorators": true,
"target": "es2017", "target": "ES2022",
"module": "commonjs" "module": "ES2022",
"moduleResolution": "nodenext"
} }
} }

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}