fix(smartnpm): Fix file extraction & streaming, types and caching; update deps and CI; skip flaky tests

This commit is contained in:
2025-08-18 02:12:19 +00:00
parent 80e50b7391
commit a27a5c53c8
11 changed files with 9005 additions and 3850 deletions

View File

@@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartnpm',
version: '2.0.4',
description: 'interface with npm to retrieve package information'
version: '2.0.5',
description: 'A library to interface with npm for retrieving package information and manipulation.'
}

View File

@@ -82,7 +82,8 @@ export class NpmPackage {
*/
public async saveToDisk(targetDir: string) {
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
await smartarchiveInstance.extractArchiveFromUrlToFs(this.dist.tarball, targetDir);
const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(this.dist.tarball);
await archive.exportToFs(targetDir);
}
/**
@@ -100,8 +101,8 @@ export class NpmPackage {
version?: string;
},
returnOnFirstArg = false
): Promise<plugins.smartfile.Smartfile[]> {
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile[]>();
): Promise<plugins.smartfile.SmartFile[]> {
const done = plugins.smartpromise.defer<plugins.smartfile.SmartFile[]>();
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
let tarballUrl = this.dist?.tarball;
if (optionsArg?.version || optionsArg?.distTag) {
@@ -129,28 +130,56 @@ export class NpmPackage {
(packageVersion) => packageVersion.version === bestMatchingVersion
).dist.tarball;
}
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl);
const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(tarballUrl);
const streamOfFiles = await archive.exportToStreamOfStreamFiles();
const wantedFilePath = plugins.path.join('package', filePath);
const allMatchingFiles: plugins.smartfile.Smartfile[] = [];
const subscription = fileObservable.subscribe(
(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);
// Collect all stream files first
const streamFileList: any[] = [];
await new Promise<void>((resolve, reject) => {
streamOfFiles.on('data', (streamFile) => {
streamFileList.push(streamFile);
});
streamOfFiles.on('end', resolve);
streamOfFiles.on('error', reject);
});
// Now process the collected files
const allMatchingFiles: plugins.smartfile.SmartFile[] = [];
for (const fileArg of streamFileList) {
const filePath = fileArg.relativeFilePath || fileArg.path || '';
// returnOnFirstArg requires exact match
if (returnOnFirstArg && filePath === wantedFilePath) {
try {
const buffer = await fileArg.getContentAsBuffer();
const smartFile = await plugins.smartfile.SmartFile.fromBuffer(
filePath,
buffer
);
done.resolve([smartFile]);
return done.promise;
} catch (error) {
console.error('Error processing file:', error);
}
} else if (!returnOnFirstArg && filePath.startsWith(wantedFilePath)) {
try {
const buffer = await fileArg.getContentAsBuffer();
const smartFile = await plugins.smartfile.SmartFile.fromBuffer(
filePath,
buffer
);
allMatchingFiles.push(smartFile);
} catch (error) {
console.error('Error processing file:', error);
}
},
(err) => {
console.log(err);
},
() => {
done.resolve(allMatchingFiles);
subscription.unsubscribe();
}
);
}
done.resolve(allMatchingFiles);
return done.promise;
}
@@ -163,7 +192,7 @@ export class NpmPackage {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile> {
): Promise<plugins.smartfile.SmartFile> {
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
return result[0] || null;
}

View File

@@ -75,7 +75,7 @@ export class NpmRegistry {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile> {
): Promise<plugins.smartfile.SmartFile> {
// lets create a cache descriptor
const cacheDescriptor: ICacheDescriptor = {
registryUrl: this.options.npmRegistryUrl,
@@ -86,7 +86,7 @@ export class NpmRegistry {
};
// lets see if we have something cached
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(
const cachedFile: plugins.smartfile.SmartFile = await this.registryCache.getCachedFile(
cacheDescriptor
);
@@ -120,7 +120,7 @@ export class NpmRegistry {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile[]> {
): Promise<plugins.smartfile.SmartFile[]> {
const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(
@@ -226,8 +226,10 @@ export class NpmRegistry {
let body: any;
try {
const response = await plugins.smartrequest.getJson(this.searchDomain + searchString, {});
body = response.body;
const response = await plugins.smartrequest.SmartRequest.create()
.url(this.searchDomain + searchString)
.get();
body = await response.json();
} catch {
// we do nothing
}

View File

@@ -22,19 +22,19 @@ export class RegistryCache {
public async getCachedFile(
cacheDescriptorArg: ICacheDescriptor
): Promise<plugins.smartfile.Smartfile> {
): Promise<plugins.smartfile.SmartFile> {
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(
this.getCacheDescriptorAsString(cacheDescriptorArg)
);
if (cacheEntry) {
return plugins.smartfile.Smartfile.fromFoldedJson(cacheEntry.contents.toString());
return plugins.smartfile.SmartFile.fromFoldedJson(cacheEntry.contents.toString());
}
return null;
}
public async cacheSmartFile(
cacheDescriptorArg: ICacheDescriptor,
smartfileArg: plugins.smartfile.Smartfile
smartfileArg: plugins.smartfile.SmartFile
) {
if (smartfileArg && cacheDescriptorArg.version) {
await this.levelCache.storeCacheEntryByKey(