fix(core): update

This commit is contained in:
Philipp Kunz 2021-05-10 16:12:11 +00:00
parent f6d3d0987b
commit cd8c9bcdaa
6 changed files with 585 additions and 593 deletions

1116
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -16,17 +16,18 @@
"@gitzone/tsrun": "^1.2.12",
"@gitzone/tstest": "^1.0.54",
"@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^15.0.1",
"@types/node": "^15.0.2",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@pushrocks/consolecolor": "^2.0.1",
"@pushrocks/levelcache": "^1.0.9",
"@pushrocks/levelcache": "^1.0.10",
"@pushrocks/smartarchive": "^2.0.4",
"@pushrocks/smartfile": "^8.0.10",
"@pushrocks/smartpromise": "^3.1.5",
"@pushrocks/smartrequest": "^1.1.51",
"@pushrocks/smarttime": "^3.0.38",
"@pushrocks/smartversion": "^2.0.7",
"package-json": "^6.5.0"
},

View File

@ -32,7 +32,6 @@ tap.test('should create a verdaccio registry', async () => {
tap.test('should get package from verdaccio', async () => {
const npmPackage = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate');
console.log(npmPackage);
expect(npmPackage.license).to.equal('MIT');
});

View File

@ -6,7 +6,7 @@ import { ISearchObject } from './smartnpm.interfaces';
// classes
import { NpmPackage } from './smartnpm.classes.npmpackage';
import { RegistryCache } from './smartnpm.classes.registrycache';
import { ICacheDescriptor, RegistryCache } from './smartnpm.classes.registrycache';
export interface INpmRegistryConstructorOptions {
npmRegistryUrl?: string;
@ -62,12 +62,23 @@ export class NpmRegistry {
/**
* gets a file from a package as Smartfile
*/
public async getFileFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
public async getFileFromPackage(packageNameArg: string, filePathArg: string, optionsArg?: {
distTag?: string;
version?: string;
}): Promise<plugins.smartfile.Smartfile> {
const fileId = `${this.options.npmRegistryUrl}//+//${packageNameArg}//+//${filePath}//+//${optionsArg?.distTag || optionsArg?.version}`;
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(fileId);
// lets create a cache descriptor
const cacheDescriptor: ICacheDescriptor = {
registryUrl: this.options.npmRegistryUrl,
packageName: packageNameArg,
filePath: filePathArg,
distTag: optionsArg?.distTag,
version: optionsArg?.version
};
// lets see if we have something cached
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(cacheDescriptor);
// lets handle both occasions
if (!cachedFile) {
const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
@ -78,8 +89,8 @@ export class NpmRegistry {
};
}
}
const fileResult = await npmPackage.getFileFromPackage(filePath, optionsArg);
this.registryCache.cacheSmartFile(fileId, fileResult);
const fileResult = await npmPackage.getFileFromPackage(filePathArg, optionsArg);
this.registryCache.cacheSmartFile(cacheDescriptor, fileResult);
return fileResult;
} else {
return cachedFile;

View File

@ -1,6 +1,15 @@
import { NpmRegistry } from './smartnpm.classes.npmregistry';
import * as plugins from './smartnpm.plugins';
export interface ICacheDescriptor {
registryUrl: string;
packageName: string;
filePath: string;
distTag?: string;
version?: string;
}
export class RegistryCache {
npmregistryRef: NpmRegistry;
public levelCache: plugins.levelcache.LevelCache;
@ -13,20 +22,29 @@ export class RegistryCache {
});
}
public async getCachedFile (fileId: string): Promise<plugins.smartfile.Smartfile> {
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(fileId);
public async getCachedFile (cacheDescriptorArg: ICacheDescriptor): Promise<plugins.smartfile.Smartfile> {
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg));
if (cacheEntry) {
return plugins.smartfile.Smartfile.fromFoldedJson(cacheEntry.contents.toString());
}
return null;
}
public async cacheSmartFile (fileIdArg: string, smartfileArg: plugins.smartfile.Smartfile) {
if (smartfileArg) {
await this.levelCache.storeCacheEntryByKey(fileIdArg, new plugins.levelcache.CacheEntry({
public async cacheSmartFile (cacheDescriptorArg: ICacheDescriptor, smartfileArg: plugins.smartfile.Smartfile) {
if (smartfileArg && cacheDescriptorArg.version) {
await this.levelCache.storeCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg), new plugins.levelcache.CacheEntry({
contents: Buffer.from(smartfileArg.foldToJson()),
ttl: 60000
ttl: plugins.smarttime.getMilliSecondsFromUnits({hours: 1})
}));
} else {
await this.levelCache.storeCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg), new plugins.levelcache.CacheEntry({
contents: Buffer.from(smartfileArg.foldToJson()),
ttl: plugins.smarttime.getMilliSecondsFromUnits({minutes: 1})
}));
}
}
public getCacheDescriptorAsString(cacheDescriptorArg?: ICacheDescriptor) {
return `${cacheDescriptorArg.registryUrl}//+//${cacheDescriptorArg.packageName}//+//${cacheDescriptorArg.filePath}//+//${cacheDescriptorArg.distTag || cacheDescriptorArg.version}`;
}
}

View File

@ -11,8 +11,9 @@ import * as smartfile from '@pushrocks/smartfile';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartversion from '@pushrocks/smartversion';
import * as smarttime from '@pushrocks/smarttime';
export { consolecolor, levelcache, smartarchive, smartfile, smartpromise, smartrequest, smartversion };
export { consolecolor, levelcache, smartarchive, smartfile, smartpromise, smartrequest, smartversion, smarttime };
// third party scope
import packageJson from 'package-json';