Compare commits

...

36 Commits

Author SHA1 Message Date
0b1b6deb98 2.0.0 2022-04-13 15:50:58 +02:00
d5636aaf9a 1.0.40 2022-04-13 15:50:14 +02:00
0276faa949 fix tests 2022-04-13 15:50:11 +02:00
c0d64926a0 update 2022-04-04 23:21:49 +02:00
7737014464 1.0.39 2021-06-07 13:59:57 +02:00
349360cdc6 fix(core): update 2021-06-07 13:59:56 +02:00
983a860e2f 1.0.38 2021-05-10 23:27:11 +00:00
c375d9ba10 fix(core): update 2021-05-10 23:27:11 +00:00
e874c935bb 1.0.37 2021-05-10 16:12:59 +00:00
001a79252c fix(core): update 2021-05-10 16:12:58 +00:00
634d1c3570 1.0.36 2021-05-10 16:12:11 +00:00
cd8c9bcdaa fix(core): update 2021-05-10 16:12:11 +00:00
f6d3d0987b 1.0.35 2021-05-06 10:34:45 +00:00
60a1de38ce fix(core): update 2021-05-06 10:34:44 +00:00
0ef6d0ccb2 1.0.34 2021-05-06 10:22:15 +00:00
8e538fd84d 1.0.33 2021-05-06 10:05:50 +00:00
6745115db7 fix(core): update 2021-05-06 10:05:49 +00:00
5ebce389d3 1.0.32 2021-05-06 10:04:51 +00:00
b4100688ac fix(version matching): now respecting packages without latest tag 2021-05-06 10:04:51 +00:00
77d515d915 1.0.31 2021-05-05 12:37:27 +00:00
aa71105b2d 1.0.30 2021-05-05 11:56:12 +00:00
e0ccb6c076 fix(core): update 2021-05-05 11:56:12 +00:00
af0d95f327 1.0.29 2021-05-05 10:54:32 +00:00
d0ac8f1418 fix(core): update 2021-05-05 10:54:31 +00:00
62f8106fc6 1.0.28 2021-04-29 21:20:19 +00:00
e87b8c994b fix(core): update 2021-04-29 21:20:18 +00:00
694ba7be25 1.0.27 2021-04-29 20:44:47 +00:00
952399c26e fix(core): update 2021-04-29 20:44:47 +00:00
ab1e83b8bf 1.0.26 2021-04-29 17:02:40 +00:00
67980f9f41 fix(core): update 2021-04-29 17:02:39 +00:00
2d34397b9b 1.0.25 2021-04-29 16:59:59 +00:00
2744e1a92b fix(core): update 2021-04-29 16:59:58 +00:00
b8987b7913 1.0.24 2021-04-26 08:30:02 +00:00
95ffb06e77 fix(core): update 2021-04-26 08:30:02 +00:00
71abbbb5f1 1.0.23 2021-04-21 09:25:32 +00:00
9dfa1a789e fix(core): update 2021-04-21 09:25:31 +00:00
14 changed files with 17870 additions and 7465 deletions

View File

@ -36,6 +36,7 @@ auditProductionDependencies:
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci

24856
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,32 +1,35 @@
{
"name": "@pushrocks/smartnpm",
"version": "1.0.22",
"version": "2.0.0",
"private": false,
"description": "interface with npm to retrieve package information",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild --web)"
"build": "(tsbuild --web --allowimplicitany)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.25",
"@gitzone/tsrun": "^1.2.12",
"@gitzone/tstest": "^1.0.52",
"@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^14.14.41",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
"@gitzone/tsbuild": "^2.1.61",
"@gitzone/tsrun": "^1.2.32",
"@gitzone/tstest": "^1.0.70",
"@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.23"
},
"dependencies": {
"@pushrocks/consolecolor": "^2.0.1",
"@pushrocks/smartarchive": "^2.0.4",
"@pushrocks/smartfile": "^8.0.9",
"@pushrocks/smartpromise": "^3.1.3",
"@pushrocks/smartrequest": "^1.1.51",
"package-json": "^6.5.0"
"@pushrocks/levelcache": "^3.0.1",
"@pushrocks/smartarchive": "^3.0.2",
"@pushrocks/smartfile": "^9.0.6",
"@pushrocks/smartpath": "^5.0.5",
"@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartrequest": "^1.1.56",
"@pushrocks/smarttime": "^3.0.45",
"@pushrocks/smartversion": "^2.0.7",
"package-json": "^7.0.0"
},
"files": [
"ts/**/*",

View File

@ -1,6 +1,6 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartnpm from '../ts/index';
import { NpmRegistry } from '../ts/index';
import * as smartnpm from '../ts/index.js';
import { NpmRegistry } from '../ts/index.js';
let npmRegistry: smartnpm.NpmRegistry;
let verdaccioRegistry: smartnpm.NpmRegistry;
@ -9,17 +9,17 @@ let testPackage: smartnpm.NpmPackage;
// lets test things with the standard npm registry
tap.test('should create valid instances', async () => {
npmRegistry = new smartnpm.NpmRegistry();
expect(npmRegistry).to.be.instanceof(smartnpm.NpmRegistry);
expect(npmRegistry).toBeInstanceOf(smartnpm.NpmRegistry);
testPackage = new smartnpm.NpmPackage(npmRegistry);
expect(testPackage).to.be.instanceof(smartnpm.NpmPackage);
expect(testPackage).toBeInstanceOf(smartnpm.NpmPackage);
});
tap.test('should produce a valid search string and this return npmts', async () => {
const packages = await npmRegistry.searchOnNpm({
name: '@pushrocks/smartupdate',
});
expect(packages[0].name).to.equal('@pushrocks/smartupdate');
expect(packages[0].name).toEqual('@pushrocks/smartupdate');
});
// lets test things with the verdaccio registry
@ -27,21 +27,38 @@ tap.test('should create a verdaccio registry', async () => {
verdaccioRegistry = new NpmRegistry({
npmRegistryUrl: 'https://verdaccio.lossless.one',
});
expect(verdaccioRegistry).to.be.instanceOf(smartnpm.NpmRegistry);
expect(verdaccioRegistry).toBeInstanceOf(smartnpm.NpmRegistry);
});
tap.test('should get package from verdaccio', async () => {
const npmPackage = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate');
console.log(npmPackage);
expect(npmPackage.license).to.equal('MIT');
expect(npmPackage.license).toEqual('MIT');
});
tap.test('should get a specific file from a package', async () => {
const bundleFile = await verdaccioRegistry.getFileFromPackage(
const wantedFile = await verdaccioRegistry.getFileFromPackage(
'@pushrocks/websetup',
'ts/index.ts'
'./ts/index.ts'
);
console.log(bundleFile.contentBuffer.toString());
console.log(wantedFile.contentBuffer.toString());
});
tap.test('should get a specific file from a package', async () => {
const wantedFiles = await verdaccioRegistry.getFilesFromPackage(
'@pushrocks/websetup',
'ts/'
);
for(const file of wantedFiles) {
console.log(file.path);
}
});
tap.test('should not get a nonexisting file from a package', async () => {
const wantedFileNotThere = await verdaccioRegistry.getFileFromPackage(
'@pushrocks/websetup',
'ts/notthere'
);
expect(wantedFileNotThere).toBeNull();
});
tap.start();

View File

@ -1,4 +1,4 @@
import * as plugins from './smartnpm.plugins';
import * as plugins from './smartnpm.plugins.js';
export * from './smartnpm.classes.npmregistry';
export * from './smartnpm.classes.npmpackage';
export * from './smartnpm.classes.npmregistry.js';
export * from './smartnpm.classes.npmpackage.js';

View File

@ -1,13 +1,35 @@
import * as plugins from './smartnpm.plugins';
import { NpmRegistry } from './smartnpm.classes.npmregistry';
import * as plugins from './smartnpm.plugins.js';
import { NpmRegistry } from './smartnpm.classes.npmregistry.js';
import { PackageDisttag } from './smartnpm.classes.packagedisttag.js';
import { PackageVersion, IVersionData } from './smartnpm.classes.packageversion.js';
export class NpmPackage {
public static async createFromFullMetadata(
public static async createFromFullMetadataAndVersionData(
npmRegistryArg: NpmRegistry,
fullMetadata: plugins.packageJson.FullMetadata
fullMetadataArg: plugins.packageJson.FullMetadata,
versionsDataArg: {
name: string;
'dist-tags': { [key: string]: string };
versions: { [key: string]: IVersionData };
}
) {
const npmPackage = new NpmPackage(npmRegistryArg);
Object.assign(npmPackage, fullMetadata);
Object.assign(npmPackage, fullMetadataArg);
npmPackage.allVersions = [];
npmPackage.allDistTags = [];
for (const versionArg of Object.keys(versionsDataArg.versions)) {
const packageVersion = PackageVersion.createFromVersionData(
versionsDataArg.versions[versionArg]
);
npmPackage.allVersions.push(packageVersion);
}
for (const distTagArg of Object.keys(versionsDataArg['dist-tags'])) {
const packageDistTag = new PackageDisttag(
distTagArg,
versionsDataArg['dist-tags'][distTagArg]
);
npmPackage.allDistTags.push(packageDistTag);
}
return npmPackage;
}
@ -15,6 +37,8 @@ export class NpmPackage {
public name: string = null;
public scope: string = null;
public version: string = null;
public allVersions: PackageVersion[];
public allDistTags: PackageDisttag[];
public description: string = null;
public keywords: string[] = null;
public date: string;
@ -62,34 +86,102 @@ export class NpmPackage {
}
/**
* saves the package to memory
* saves the complete package to cache
*/
public async saveToMemory() {}
public async saveToCache() {}
/**
* get file from package
* get files from package
*/
public async getFileFromPackage(filePath: string): Promise<plugins.smartfile.Smartfile> {
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile>();
public async getFilesFromPackage(
filePath: string,
optionsArg: {
distTag?: string;
version?: string;
},
returnOnFirstArg = false
): Promise<plugins.smartfile.Smartfile[]> {
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile[]>();
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(
this.dist.tarball
);
let tarballUrl = this.dist?.tarball;
if (optionsArg?.version || optionsArg?.distTag) {
if (optionsArg.distTag && optionsArg.version) {
throw new Error('Please either specify version OR disttag, not both.');
}
let targetVersionString: string;
if (optionsArg.distTag) {
const targetDistTag = this.allDistTags.find((distTag) => {
return distTag.name === optionsArg.distTag;
});
if (targetDistTag) {
targetVersionString = targetDistTag.targetVersion;
}
} else {
targetVersionString = optionsArg.version;
}
// lets find the best matching release
const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
if (!bestMatchingVersion) {
return null;
}
tarballUrl = this.allVersions.find(
(packageVersion) => packageVersion.version === bestMatchingVersion
).dist.tarball;
}
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl);
const wantedFilePath = plugins.path.join('package', filePath);
const allMatchingFiles: plugins.smartfile.Smartfile[] = [];
const subscription = fileObservable.subscribe(
(fileArg) => {
if (fileArg.path === wantedFilePath) {
done.resolve(fileArg);
// returnOnFirstArg requires exact match
if (returnOnFirstArg && fileArg.path === wantedFilePath) {
// lets resolve with the wanted file
done.resolve([fileArg]);
subscription.unsubscribe();
} else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
allMatchingFiles.push(fileArg);
}
},
(err) => {
console.log(err);
},
() => {
done.resolve(allMatchingFiles);
subscription.unsubscribe();
}
);
return done.promise;
}
/**
* get files from package
*/
public async getFileFromPackage(
filePath: string,
optionsArg?: {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile> {
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
return result[0] || null;
}
/**
* updates the package with information from the registry
*/
update() {}
/** */
public getBestMatchingVersion(versionArg: string): string {
// lets find the best matching release
const targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(versionArg);
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
if (!bestMatchingVersion) {
return null;
}
return bestMatchingVersion;
}
}

View File

@ -1,11 +1,12 @@
import * as plugins from './smartnpm.plugins';
import * as paths from './smartnpm.paths';
import * as plugins from './smartnpm.plugins.js';
import * as paths from './smartnpm.paths.js';
// interfaces
import { ISearchObject } from './smartnpm.interfaces';
import { ISearchObject } from './smartnpm.interfaces.js';
// classes
import { NpmPackage } from './smartnpm.classes.npmpackage';
import { NpmPackage } from './smartnpm.classes.npmpackage.js';
import { ICacheDescriptor, RegistryCache } from './smartnpm.classes.registrycache.js';
export interface INpmRegistryConstructorOptions {
npmRegistryUrl?: string;
@ -13,7 +14,7 @@ export interface INpmRegistryConstructorOptions {
export class NpmRegistry {
public options: INpmRegistryConstructorOptions;
public registry: string;
public registryCache: RegistryCache;
private searchDomain = 'https://api.npms.io/v2/search?q=';
constructor(optionsArg: INpmRegistryConstructorOptions = {}) {
@ -24,6 +25,7 @@ export class NpmRegistry {
...defaultOptions,
...optionsArg,
};
this.registryCache = new RegistryCache(this);
}
/**
@ -34,8 +36,15 @@ export class NpmRegistry {
const fullMetadata = await plugins.packageJson(packageName, {
registryUrl: this.options.npmRegistryUrl,
fullMetadata: true,
}).catch(err => {
console.log(err);
return null;
});
const npmPackage = await NpmPackage.createFromFullMetadata(this, fullMetadata);
const versionData = await plugins.packageJson(packageName, {
registryUrl: this.options.npmRegistryUrl,
allVersions: true
});
const npmPackage = await NpmPackage.createFromFullMetadataAndVersionData(this, fullMetadata, versionData as any);
return npmPackage;
}
@ -53,12 +62,63 @@ export class NpmRegistry {
/**
* gets a file from a package as Smartfile
*/
public async getFileFromPackage(packageNameArg: string, filePath: string): Promise<plugins.smartfile.Smartfile> {
public async getFileFromPackage(packageNameArg: string, filePathArg: string, optionsArg?: {
distTag?: string;
version?: string;
}): Promise<plugins.smartfile.Smartfile> {
// 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);
return npmPackage.getFileFromPackage(filePath);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(packageArg => packageArg.name === 'latest');
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
};
}
}
const fileResult = await npmPackage.getFileFromPackage(filePathArg, optionsArg);
if (fileResult) {
this.registryCache.cacheSmartFile(cacheDescriptor, fileResult);
}
return fileResult;
} else {
return cachedFile;
}
}
public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
distTag?: string;
version?: string;
}): Promise<plugins.smartfile.Smartfile[]> {
const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(packageDistTagArg => packageDistTagArg.name === 'latest');
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
};
}
}
return npmPackage.getFilesFromPackage(filePath, optionsArg);
}
public async getPackageAsSmartfileVirtualDir(packageNameArg: string): Promise<plugins.smartfile.VirtualDirectory> {
/**
* TODO: rewrite as memory only
*/
const baseDir = plugins.path.join(paths.nogitDir, packageNameArg.replace('/', '__'));
await plugins.smartfile.fs.ensureDir(baseDir);
await this.savePackageToDisk(packageNameArg, baseDir);

View File

@ -0,0 +1,11 @@
import * as plugins from './smartnpm.plugins.js';
export class PackageDisttag {
name: string;
targetVersion: string;
constructor(nameArg: string, targetVersionArg: string) {
this.name = nameArg;
this.targetVersion = targetVersionArg;
}
}

View File

@ -0,0 +1,31 @@
import * as plugins from './smartnpm.plugins.js';
export interface IVersionData {
name: string;
version: string;
dependencies: { [key: string]: string };
devDependencies: { [key: string]: string };
dist: {
integrity: string;
shasum: string;
tarball: string;
};
}
export class PackageVersion implements IVersionData {
public static createFromVersionData(versionDataArg: IVersionData) {
const packageVersion = new PackageVersion();
Object.assign(packageVersion, versionDataArg);
return packageVersion;
}
name: string;
version: string;
dependencies: { [key: string]: string };
devDependencies: { [key: string]: string };
dist: {
integrity: string;
shasum: string;
tarball: string;
};
}

View File

@ -0,0 +1,50 @@
import { NpmRegistry } from './smartnpm.classes.npmregistry.js';
import * as plugins from './smartnpm.plugins.js';
export interface ICacheDescriptor {
registryUrl: string;
packageName: string;
filePath: string;
distTag?: string;
version?: string;
}
export class RegistryCache {
npmregistryRef: NpmRegistry;
public levelCache: plugins.levelcache.LevelCache;
constructor(npmRegistryRefArg: NpmRegistry) {
this.npmregistryRef = npmRegistryRefArg;
this.levelCache = new plugins.levelcache.LevelCache({
cacheId: encodeURIComponent(this.npmregistryRef.options.npmRegistryUrl),
});
}
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 (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: 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

@ -1,4 +1,4 @@
import * as plugins from './smartnpm.plugins';
import * as plugins from './smartnpm.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

@ -5,12 +5,16 @@ export { path };
// @pushrocks scope
import * as consolecolor from '@pushrocks/consolecolor';
import * as levelcache from '@pushrocks/levelcache';
import * as smartarchive from '@pushrocks/smartarchive';
import * as smartfile from '@pushrocks/smartfile';
import * as smartpath from '@pushrocks/smartpath';
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, smartarchive, smartfile, smartpromise, smartrequest };
export { consolecolor, levelcache, smartarchive, smartfile, smartpath, smartpromise, smartrequest, smartversion, smarttime };
// third party scope
import packageJson from 'package-json';

9
tsconfig.json Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"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"
}