Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e8375a925 | |||
| 0dedf79fa7 | |||
| 3bb68776fb | |||
| 925f5c7097 |
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"@git.zone/cli": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartnpm",
|
||||
"description": "A library to interface with npm for retrieving package information and manipulation.",
|
||||
"npmPackagename": "@push.rocks/smartnpm",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"npm",
|
||||
"package",
|
||||
"information",
|
||||
"registry",
|
||||
"search",
|
||||
"metadata",
|
||||
"version",
|
||||
"dependencies"
|
||||
]
|
||||
},
|
||||
"release": {
|
||||
"registries": [
|
||||
"https://verdaccio.lossless.digital",
|
||||
"https://registry.npmjs.org"
|
||||
],
|
||||
"accessLevel": "public"
|
||||
}
|
||||
},
|
||||
"@git.zone/tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
},
|
||||
"@ship.zone/szci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmRegistryUrl": "registry.npmjs.org"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,18 @@
|
||||
# Changelog
|
||||
|
||||
## 2026-05-01 - 2.1.0 - feat(registry)
|
||||
modernize npm registry file handling and package extraction APIs
|
||||
|
||||
- replace legacy smartarchive and smartfile filesystem usage with SmartArchive create() chaining, SmartFs, and SmartFileFactory node adapters
|
||||
- improve null safety and type annotations across registry, package, and cache classes to handle missing metadata, versions, tarballs, and search results more robustly
|
||||
- update package metadata, tooling configuration, test imports, and dependency versions to align with the newer build and CI setup
|
||||
|
||||
## 2025-08-18 - 2.0.6 - fix(readme)
|
||||
Expand README with detailed usage examples, API reference and features; add local assistant settings
|
||||
|
||||
- Expanded README.md: added badges, features list, installation instructions (including pnpm), quick start, detailed usage examples (package info, search, download, extraction, version management, virtual directory), caching info, API reference, and common use cases.
|
||||
- Added .claude/settings.local.json to define local assistant permissions
|
||||
|
||||
## 2025-08-18 - 2.0.5 - fix(smartnpm)
|
||||
Fix file extraction & streaming, types and caching; update deps and CI; skip flaky tests
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2026 Task Venture Capital GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+13
-6
@@ -1,9 +1,5 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"@git.zone/cli": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
@@ -22,9 +18,20 @@
|
||||
"version",
|
||||
"dependencies"
|
||||
]
|
||||
},
|
||||
"release": {
|
||||
"registries": [
|
||||
"https://verdaccio.lossless.digital",
|
||||
"https://registry.npmjs.org"
|
||||
],
|
||||
"accessLevel": "public"
|
||||
}
|
||||
},
|
||||
"tsdoc": {
|
||||
"@git.zone/tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
},
|
||||
"@ship.zone/szci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmRegistryUrl": "registry.npmjs.org"
|
||||
}
|
||||
}
|
||||
+49
-42
@@ -1,52 +1,23 @@
|
||||
{
|
||||
"name": "@push.rocks/smartnpm",
|
||||
"version": "2.0.5",
|
||||
"version": "2.1.0",
|
||||
"private": false,
|
||||
"description": "A library to interface with npm for retrieving package information and manipulation.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"author": "Task Venture Capital GmbH <hello@task.vc>",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --verbose --logfile --timeout 120)",
|
||||
"build": "(tsbuild --web --allowimplicitany)",
|
||||
"test": "tstest test/ --verbose --timeout 120",
|
||||
"build": "tsbuild --web",
|
||||
"format": "gitzone format",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.6.6",
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tstest": "^2.3.4",
|
||||
"@push.rocks/tapbundle": "^6.0.3",
|
||||
"@types/node": "^20.4.4"
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartnpm.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/consolecolor": "^2.0.1",
|
||||
"@push.rocks/levelcache": "^3.0.6",
|
||||
"@push.rocks/smartarchive": "^4.2.1",
|
||||
"@push.rocks/smartfile": "^11.2.7",
|
||||
"@push.rocks/smartpath": "^6.0.0",
|
||||
"@push.rocks/smartpromise": "^4.0.3",
|
||||
"@push.rocks/smartrequest": "^4.2.2",
|
||||
"@push.rocks/smarttime": "^4.0.4",
|
||||
"@push.rocks/smartversion": "^3.0.2",
|
||||
"package-json": "^8.1.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"keywords": [
|
||||
"npm",
|
||||
"package",
|
||||
@@ -57,10 +28,46 @@
|
||||
"version",
|
||||
"dependencies"
|
||||
],
|
||||
"homepage": "https://code.foss.global/push.rocks/smartnpm",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartnpm.git"
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/push.rocks/smartnpm/issues"
|
||||
},
|
||||
"packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748"
|
||||
"homepage": "https://code.foss.global/push.rocks/smartnpm",
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^4.4.0",
|
||||
"@git.zone/tsrun": "^2.0.3",
|
||||
"@git.zone/tstest": "^3.6.3",
|
||||
"@types/lodash.clonedeep": "^4.5.9",
|
||||
"@types/node": "^25.6.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/consolecolor": "^2.0.3",
|
||||
"@push.rocks/levelcache": "^3.2.2",
|
||||
"@push.rocks/smartarchive": "^5.2.2",
|
||||
"@push.rocks/smartfile": "^13.1.3",
|
||||
"@push.rocks/smartfs": "^1.5.1",
|
||||
"@push.rocks/smartpath": "^6.0.0",
|
||||
"@push.rocks/smartpromise": "^4.2.3",
|
||||
"@push.rocks/smartrequest": "^5.0.1",
|
||||
"@push.rocks/smarttime": "^4.2.3",
|
||||
"@push.rocks/smartversion": "^3.1.0",
|
||||
"package-json": "^10.0.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
".smartconfig.json",
|
||||
"license",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"packageManager": "pnpm@10.28.2"
|
||||
}
|
||||
|
||||
Generated
+3498
-4433
File diff suppressed because it is too large
Load Diff
@@ -1,93 +1,337 @@
|
||||
# @push.rocks/smartnpm
|
||||
interface with npm to retrieve package information
|
||||
**Smart npm interface for Node.js 🚀**
|
||||
|
||||
## Install
|
||||
To install `@push.rocks/smartnpm`, open your terminal and run the following command:
|
||||
```sh
|
||||
[](https://www.npmjs.com/package/@push.rocks/smartnpm)
|
||||
[](https://www.typescriptlang.org/)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
|
||||
> A powerful TypeScript library to programmatically interact with npm registries, retrieve package information, search packages, and handle package downloads with full caching support.
|
||||
|
||||
## 🎯 Features
|
||||
|
||||
- 📦 **Package Information Retrieval** - Get comprehensive metadata about any npm package
|
||||
- 🔍 **Advanced Package Search** - Search npm registry with multiple filter options
|
||||
- 💾 **Package Downloads** - Download and extract packages to disk programmatically
|
||||
- 📁 **File Extraction** - Extract specific files or directories from packages without full download
|
||||
- 🏷️ **Version & Tag Management** - Work with specific versions, dist-tags, and version ranges
|
||||
- ⚡ **Smart Caching** - Built-in caching system for improved performance
|
||||
- 🌐 **Custom Registry Support** - Use with npm, Verdaccio, or any npm-compatible registry
|
||||
- 🗂️ **Virtual Directory Creation** - Load packages as virtual file systems in memory
|
||||
|
||||
## 📥 Installation
|
||||
|
||||
```bash
|
||||
npm install @push.rocks/smartnpm --save
|
||||
```
|
||||
This will add `@push.rocks/smartnpm` as a dependency to your project and you're ready to start using it.
|
||||
|
||||
## Usage
|
||||
To use `@push.rocks/smartnpm` in your project, you first need to import it in your TypeScript files. `@push.rocks/smartnpm` provides a powerful interface to interact with npm to retrieve package information, handle package downloads, and more. Below are examples showcasing how to leverage some of its features in real-world scenarios.
|
||||
Or using pnpm (recommended):
|
||||
|
||||
### Initialize the NpmRegistry
|
||||
Before you can retrieve any package information or perform actions such as downloading packages, you need to create an instance of `NpmRegistry`. This acts as your starting point.
|
||||
```bash
|
||||
pnpm add @push.rocks/smartnpm
|
||||
```
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
```typescript
|
||||
import { NpmRegistry } from '@push.rocks/smartnpm';
|
||||
|
||||
// Initialize with default npm registry
|
||||
const npmRegistry = new NpmRegistry();
|
||||
|
||||
// Or use a custom registry
|
||||
const customRegistry = new NpmRegistry({
|
||||
npmRegistryUrl: 'https://your-registry.example.com'
|
||||
});
|
||||
```
|
||||
|
||||
## 📘 Usage Examples
|
||||
|
||||
### Package Information Retrieval
|
||||
|
||||
Get detailed information about any npm package:
|
||||
|
||||
```typescript
|
||||
import { NpmRegistry } from '@push.rocks/smartnpm';
|
||||
|
||||
const npmRegistry = new NpmRegistry();
|
||||
```
|
||||
|
||||
Optionally, you can provide a custom npm registry URL if you're not using the default npm registry:
|
||||
async function getPackageDetails() {
|
||||
const packageInfo = await npmRegistry.getPackageInfo('@angular/core');
|
||||
|
||||
```typescript
|
||||
const customRegistry = new NpmRegistry({
|
||||
npmRegistryUrl: 'https://custom.registry.url'
|
||||
});
|
||||
```
|
||||
console.log(`Package: ${packageInfo.name}`);
|
||||
console.log(`Latest Version: ${packageInfo.version}`);
|
||||
console.log(`Description: ${packageInfo.description}`);
|
||||
console.log(`License: ${packageInfo.license}`);
|
||||
|
||||
### Retrieve Package Information
|
||||
`@push.rocks/smartnpm` allows you to easily get detailed information about a package, including its versions, dist tags, and metadata. Here's how you can get information about a specific package:
|
||||
// Access all versions
|
||||
packageInfo.allVersions.forEach(version => {
|
||||
console.log(`- ${version.version}: ${version.date}`);
|
||||
});
|
||||
|
||||
```typescript
|
||||
async function getPackageInfo() {
|
||||
const packageName = 'your-package-name';
|
||||
const packageInfo = await npmRegistry.getPackageInfo(packageName);
|
||||
console.log(packageInfo);
|
||||
// Access dist tags
|
||||
packageInfo.allDistTags.forEach(tag => {
|
||||
console.log(`Tag ${tag.name}: ${tag.targetVersion}`);
|
||||
});
|
||||
}
|
||||
|
||||
getPackageInfo();
|
||||
```
|
||||
|
||||
### Search for Packages
|
||||
You can search for packages using a variety of filters such as keywords, author, maintainer, etc. Here's an example of searching for packages with specific criteria:
|
||||
### 🔍 Advanced Package Search
|
||||
|
||||
Search the npm registry with powerful filters:
|
||||
|
||||
```typescript
|
||||
async function searchPackages() {
|
||||
// Search with multiple criteria
|
||||
const searchResults = await npmRegistry.searchOnNpm({
|
||||
keywords: ['webpack-plugin'],
|
||||
author: 'webpack'
|
||||
name: 'webpack-plugin',
|
||||
keywords: ['webpack', 'plugin', 'build'],
|
||||
author: 'webpack-contrib',
|
||||
maintainer: 'sokra',
|
||||
scope: '@webpack',
|
||||
deprecated: false,
|
||||
unstable: false,
|
||||
insecure: false,
|
||||
boostExact: true,
|
||||
scoreEffect: 15.3,
|
||||
qualityWeight: 1.95,
|
||||
popularityWeight: 3.3,
|
||||
maintenanceWeight: 2.05
|
||||
});
|
||||
console.log(searchResults);
|
||||
}
|
||||
|
||||
searchPackages();
|
||||
console.log(`Found ${searchResults.length} packages`);
|
||||
|
||||
searchResults.forEach(pkg => {
|
||||
console.log(`📦 ${pkg.name}@${pkg.version}`);
|
||||
console.log(` Score: ${pkg.searchScore}`);
|
||||
console.log(` Quality: ${pkg.score.detail.quality}`);
|
||||
console.log(` Popularity: ${pkg.score.detail.popularity}`);
|
||||
console.log(` Maintenance: ${pkg.score.detail.maintenance}`);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### Downloading Packages
|
||||
`@push.rocks/smartnpm` provides an easy way to download npm packages and extract them to a specific directory. This could be useful for creating tools that need to programmatically handle packages.
|
||||
### 💾 Download Packages
|
||||
|
||||
Download and extract npm packages to your filesystem:
|
||||
|
||||
```typescript
|
||||
async function downloadPackage() {
|
||||
const packageName = 'some-package';
|
||||
const targetDirectory = './path/to/targetDir';
|
||||
// Download latest version
|
||||
await npmRegistry.savePackageToDisk('express', './downloads/express');
|
||||
|
||||
await npmRegistry.savePackageToDisk(packageName, targetDirectory);
|
||||
console.log(`${packageName} has been downloaded to ${targetDirectory}`);
|
||||
// Download specific version
|
||||
const packageInfo = await npmRegistry.getPackageInfo('express');
|
||||
const specificVersion = packageInfo.allVersions.find(v => v.version === '4.18.0');
|
||||
if (specificVersion) {
|
||||
await specificVersion.saveToDisk('./downloads/express-4.18.0');
|
||||
}
|
||||
}
|
||||
|
||||
downloadPackage();
|
||||
```
|
||||
|
||||
### Working with Package Versions and Dist Tags
|
||||
You can easily retrieve detailed information about specific package versions or distribution tags. This is particularly useful for automation scripts that need to work with specific versions of a package.
|
||||
### 📁 Extract Specific Files
|
||||
|
||||
Extract individual files or directories from packages without downloading the entire package:
|
||||
|
||||
```typescript
|
||||
async function getPackageVersionDetails() {
|
||||
const packageName = 'some-package';
|
||||
const version = '1.0.0'; // You can also use dist tags like 'latest'
|
||||
async function extractSpecificFiles() {
|
||||
// Get a single file
|
||||
const readmeFile = await npmRegistry.getFileFromPackage(
|
||||
'typescript',
|
||||
'README.md'
|
||||
);
|
||||
|
||||
const packageInfo = await npmRegistry.getPackageInfo(packageName);
|
||||
const versionInfo = packageInfo.allVersions.find(v => v.version === version);
|
||||
console.log(versionInfo);
|
||||
if (readmeFile) {
|
||||
console.log('README Contents:', readmeFile.contentBuffer.toString());
|
||||
}
|
||||
|
||||
// Get a file from specific version
|
||||
const packageJson = await npmRegistry.getFileFromPackage(
|
||||
'react',
|
||||
'package.json',
|
||||
{ version: '18.0.0' }
|
||||
);
|
||||
|
||||
// Get all files from a directory
|
||||
const sourceFiles = await npmRegistry.getFilesFromPackage(
|
||||
'@angular/core',
|
||||
'src/',
|
||||
{ distTag: 'latest' }
|
||||
);
|
||||
|
||||
sourceFiles.forEach(file => {
|
||||
console.log(`📄 ${file.path} (${file.contentBuffer.length} bytes)`);
|
||||
});
|
||||
}
|
||||
|
||||
getPackageVersionDetails();
|
||||
```
|
||||
|
||||
These examples only scratch the surface of what you can achieve with `@push.rocks/smartnpm`. By integrating this library, you have a powerful tool at your disposal for interacting with npm in a programmatic way, enabling a wide range of possibilities for automation, CI/CD, and tooling around npm packages.
|
||||
### 🏷️ Version Management
|
||||
|
||||
Work with specific versions and dist tags:
|
||||
|
||||
```typescript
|
||||
async function versionManagement() {
|
||||
const pkg = await npmRegistry.getPackageInfo('vue');
|
||||
|
||||
// Get best matching version for a range
|
||||
const bestVersion = pkg.getBestMatchingVersion('^3.0.0');
|
||||
console.log(`Best matching version: ${bestVersion}`);
|
||||
|
||||
// Work with dist tags
|
||||
const latestTag = pkg.allDistTags.find(t => t.name === 'latest');
|
||||
const nextTag = pkg.allDistTags.find(t => t.name === 'next');
|
||||
|
||||
console.log(`Latest: ${latestTag?.targetVersion}`);
|
||||
console.log(`Next: ${nextTag?.targetVersion}`);
|
||||
|
||||
// Get files from specific dist tag
|
||||
const files = await npmRegistry.getFilesFromPackage(
|
||||
'vue',
|
||||
'dist/',
|
||||
{ distTag: 'next' }
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### 🗂️ Virtual Directory
|
||||
|
||||
Load packages as virtual file systems in memory:
|
||||
|
||||
```typescript
|
||||
async function virtualDirectory() {
|
||||
// Create virtual directory from package
|
||||
const virtualDir = await npmRegistry.getPackageAsSmartfileVirtualDir('@angular/cli');
|
||||
|
||||
// Work with files in memory
|
||||
const allFiles = virtualDir.getFileArray();
|
||||
|
||||
allFiles.forEach(file => {
|
||||
console.log(`Virtual file: ${file.path}`);
|
||||
});
|
||||
|
||||
// Export virtual directory to disk if needed
|
||||
await virtualDir.saveToDisk('./output/angular-cli');
|
||||
}
|
||||
```
|
||||
|
||||
### ⚡ Caching
|
||||
|
||||
The library includes built-in intelligent caching:
|
||||
|
||||
```typescript
|
||||
// Files are automatically cached
|
||||
const file1 = await npmRegistry.getFileFromPackage('lodash', 'package.json');
|
||||
// This will be served from cache
|
||||
const file2 = await npmRegistry.getFileFromPackage('lodash', 'package.json');
|
||||
|
||||
// Cache is registry-specific and version-aware
|
||||
const specificVersion = await npmRegistry.getFileFromPackage(
|
||||
'lodash',
|
||||
'README.md',
|
||||
{ version: '4.17.21' }
|
||||
);
|
||||
```
|
||||
|
||||
## 🏗️ API Reference
|
||||
|
||||
### NpmRegistry Class
|
||||
|
||||
#### Constructor
|
||||
```typescript
|
||||
new NpmRegistry(options?: INpmRegistryConstructorOptions)
|
||||
```
|
||||
|
||||
Options:
|
||||
- `npmRegistryUrl`: Custom registry URL (default: `https://registry.npmjs.org`)
|
||||
|
||||
#### Methods
|
||||
|
||||
##### `getPackageInfo(packageName: string): Promise<NpmPackage>`
|
||||
Retrieves comprehensive information about a package.
|
||||
|
||||
##### `searchOnNpm(searchObject: ISearchObject): Promise<NpmPackage[]>`
|
||||
Searches the npm registry with advanced filters.
|
||||
|
||||
##### `savePackageToDisk(packageName: string, targetDir: string): Promise<void>`
|
||||
Downloads and extracts a package to the filesystem.
|
||||
|
||||
##### `getFileFromPackage(packageName: string, filePath: string, options?): Promise<SmartFile>`
|
||||
Extracts a single file from a package.
|
||||
|
||||
##### `getFilesFromPackage(packageName: string, filePath: string, options?): Promise<SmartFile[]>`
|
||||
Extracts multiple files from a package directory.
|
||||
|
||||
##### `getPackageAsSmartfileVirtualDir(packageName: string): Promise<VirtualDirectory>`
|
||||
Creates an in-memory virtual directory from a package.
|
||||
|
||||
### NpmPackage Class
|
||||
|
||||
#### Properties
|
||||
- `name`: Package name
|
||||
- `version`: Current version
|
||||
- `description`: Package description
|
||||
- `license`: License type
|
||||
- `allVersions`: Array of all available versions
|
||||
- `allDistTags`: Array of all dist tags
|
||||
- `dependencies`: Package dependencies
|
||||
- `keywords`: Package keywords
|
||||
- `maintainers`: Package maintainers
|
||||
- `dist`: Distribution information
|
||||
|
||||
#### Methods
|
||||
|
||||
##### `getBestMatchingVersion(versionRange: string): string`
|
||||
Finds the best matching version for a semver range.
|
||||
|
||||
##### `saveToDisk(targetDir: string): Promise<void>`
|
||||
Saves the package to disk.
|
||||
|
||||
##### `getFileFromPackage(filePath: string, options?): Promise<SmartFile>`
|
||||
Gets a file from the package.
|
||||
|
||||
##### `getFilesFromPackage(filePath: string, options?): Promise<SmartFile[]>`
|
||||
Gets multiple files from the package.
|
||||
|
||||
## 🔧 Advanced Configuration
|
||||
|
||||
### Custom Registry with Authentication
|
||||
|
||||
```typescript
|
||||
const privateRegistry = new NpmRegistry({
|
||||
npmRegistryUrl: 'https://private-registry.company.com'
|
||||
});
|
||||
|
||||
// Note: Authentication should be configured via .npmrc or npm config
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
```typescript
|
||||
try {
|
||||
const pkg = await npmRegistry.getPackageInfo('non-existent-package');
|
||||
} catch (error) {
|
||||
console.error('Package not found:', error.message);
|
||||
}
|
||||
|
||||
// Safe file extraction
|
||||
const file = await npmRegistry.getFileFromPackage('express', 'README.md');
|
||||
if (file) {
|
||||
// File exists
|
||||
console.log('File size:', file.contentBuffer.length);
|
||||
} else {
|
||||
// File doesn't exist
|
||||
console.log('File not found');
|
||||
}
|
||||
```
|
||||
|
||||
## 🎯 Common Use Cases
|
||||
|
||||
- **CI/CD Pipelines**: Automatically download and verify package contents
|
||||
- **Security Scanning**: Extract and analyze package files without installation
|
||||
- **Documentation Generation**: Pull README files and docs from packages
|
||||
- **Dependency Analysis**: Analyze package structures and dependencies
|
||||
- **Registry Mirroring**: Sync packages between registries
|
||||
- **Package Validation**: Verify package contents before deployment
|
||||
- **Automated Updates**: Check for new versions and update notifications
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import * as smartnpm from '../ts/index.js';
|
||||
import { NpmRegistry } from '../ts/index.js';
|
||||
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartnpm',
|
||||
version: '2.0.5',
|
||||
version: '2.1.0',
|
||||
description: 'A library to interface with npm for retrieving package information and manipulation.'
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { PackageVersion, type IVersionData } from './smartnpm.classes.packagever
|
||||
export class NpmPackage {
|
||||
public static async createFromFullMetadataAndVersionData(
|
||||
npmRegistryArg: NpmRegistry,
|
||||
fullMetadataArg: plugins.packageJson.FullMetadata,
|
||||
fullMetadataArg: Record<string, unknown>,
|
||||
versionsDataArg: {
|
||||
name: string;
|
||||
'dist-tags': { [key: string]: string };
|
||||
@@ -34,30 +34,30 @@ export class NpmPackage {
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
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;
|
||||
public license: string;
|
||||
public links: {
|
||||
public name: string | null = null;
|
||||
public scope: string | null = null;
|
||||
public version: string | null = null;
|
||||
public allVersions: PackageVersion[] = [];
|
||||
public allDistTags: PackageDisttag[] = [];
|
||||
public description: string | null = null;
|
||||
public keywords: string[] | null = null;
|
||||
public date!: string;
|
||||
public license!: string;
|
||||
public links!: {
|
||||
npm: string;
|
||||
homepage: string;
|
||||
repository: string;
|
||||
bugs: string;
|
||||
};
|
||||
public author: {
|
||||
public author!: {
|
||||
name: 'Lossless GmbH';
|
||||
};
|
||||
public publisher: {
|
||||
public publisher!: {
|
||||
username: 'gitzone';
|
||||
email: 'npm@git.zone';
|
||||
};
|
||||
public maintainers: any = null;
|
||||
public dist: {
|
||||
public maintainers: unknown = null;
|
||||
public dist!: {
|
||||
integrity: string;
|
||||
shasum: string;
|
||||
tarball: string;
|
||||
@@ -69,8 +69,8 @@ export class NpmPackage {
|
||||
popularity: number;
|
||||
maintenance: number;
|
||||
};
|
||||
} = null;
|
||||
public searchScore: number = null;
|
||||
} | null = null;
|
||||
public searchScore: number | null = null;
|
||||
|
||||
public npmRegistryRef: NpmRegistry;
|
||||
constructor(npmRegistryArg: NpmRegistry) {
|
||||
@@ -81,9 +81,7 @@ export class NpmPackage {
|
||||
* saves the package to disk
|
||||
*/
|
||||
public async saveToDisk(targetDir: string) {
|
||||
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||
const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(this.dist.tarball);
|
||||
await archive.exportToFs(targetDir);
|
||||
await plugins.smartarchive.SmartArchive.create().url(this.dist.tarball).extract(targetDir);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,17 +97,15 @@ export class NpmPackage {
|
||||
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();
|
||||
let tarballUrl = this.dist?.tarball;
|
||||
): Promise<plugins.smartfile.SmartFile[] | null> {
|
||||
let tarballUrl: string | undefined = 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;
|
||||
let targetVersionString: string | undefined;
|
||||
if (optionsArg.distTag) {
|
||||
const targetDistTag = this.allDistTags.find((distTag) => {
|
||||
return distTag.name === optionsArg.distTag;
|
||||
@@ -120,6 +116,9 @@ export class NpmPackage {
|
||||
} else {
|
||||
targetVersionString = optionsArg.version;
|
||||
}
|
||||
if (!targetVersionString) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// lets find the best matching release
|
||||
const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
|
||||
@@ -128,59 +127,20 @@ export class NpmPackage {
|
||||
}
|
||||
tarballUrl = this.allVersions.find(
|
||||
(packageVersion) => packageVersion.version === bestMatchingVersion
|
||||
).dist.tarball;
|
||||
)?.dist.tarball;
|
||||
}
|
||||
if (!tarballUrl) {
|
||||
return null;
|
||||
}
|
||||
const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(tarballUrl);
|
||||
const streamOfFiles = await archive.exportToStreamOfStreamFiles();
|
||||
const wantedFilePath = plugins.path.join('package', filePath);
|
||||
|
||||
// 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);
|
||||
}
|
||||
const allFiles = await plugins.smartarchive.SmartArchive.create().url(tarballUrl).toSmartFiles();
|
||||
const allMatchingFiles = allFiles.filter((fileArg) => {
|
||||
if (returnOnFirstArg) {
|
||||
return fileArg.path === wantedFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
done.resolve(allMatchingFiles);
|
||||
return done.promise;
|
||||
return fileArg.path.startsWith(wantedFilePath);
|
||||
});
|
||||
return returnOnFirstArg ? allMatchingFiles.slice(0, 1) : allMatchingFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,9 +152,9 @@ export class NpmPackage {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.SmartFile> {
|
||||
): Promise<plugins.smartfile.SmartFile | null> {
|
||||
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
|
||||
return result[0] || null;
|
||||
return result?.[0] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,7 +163,7 @@ export class NpmPackage {
|
||||
update() {}
|
||||
|
||||
/** */
|
||||
public getBestMatchingVersion(versionArg: string): string {
|
||||
public getBestMatchingVersion(versionArg: string): string | null {
|
||||
// lets find the best matching release
|
||||
const targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(versionArg);
|
||||
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
|
||||
|
||||
@@ -13,12 +13,14 @@ export interface INpmRegistryConstructorOptions {
|
||||
}
|
||||
|
||||
export class NpmRegistry {
|
||||
public options: INpmRegistryConstructorOptions;
|
||||
public options: Required<INpmRegistryConstructorOptions>;
|
||||
public registryCache: RegistryCache;
|
||||
public smartFs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
|
||||
public smartFileFactory = plugins.smartfile.SmartFileFactory.nodeFs();
|
||||
private searchDomain = 'https://api.npms.io/v2/search?q=';
|
||||
|
||||
constructor(optionsArg: INpmRegistryConstructorOptions = {}) {
|
||||
const defaultOptions: INpmRegistryConstructorOptions = {
|
||||
const defaultOptions: Required<INpmRegistryConstructorOptions> = {
|
||||
npmRegistryUrl: 'https://registry.npmjs.org',
|
||||
};
|
||||
this.options = {
|
||||
@@ -46,10 +48,17 @@ export class NpmRegistry {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
allVersions: true,
|
||||
});
|
||||
if (!fullMetadata) {
|
||||
throw new Error(`Could not retrieve metadata for package ${packageName}.`);
|
||||
}
|
||||
const npmPackage = await NpmPackage.createFromFullMetadataAndVersionData(
|
||||
this,
|
||||
fullMetadata,
|
||||
versionData as any
|
||||
versionData as {
|
||||
name: string;
|
||||
'dist-tags': { [key: string]: string };
|
||||
versions: { [key: string]: import('./smartnpm.classes.packageversion.js').IVersionData };
|
||||
}
|
||||
);
|
||||
return npmPackage;
|
||||
}
|
||||
@@ -60,7 +69,7 @@ export class NpmRegistry {
|
||||
* @param targetDir
|
||||
*/
|
||||
public async savePackageToDisk(packageName: string, targetDir: string): Promise<void> {
|
||||
plugins.smartfile.fs.ensureDirSync(paths.nogitDir);
|
||||
await this.smartFs.directory(paths.nogitDir).create();
|
||||
const npmPackage = await this.getPackageInfo(packageName);
|
||||
await npmPackage.saveToDisk(targetDir);
|
||||
}
|
||||
@@ -75,7 +84,7 @@ export class NpmRegistry {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.SmartFile> {
|
||||
): Promise<plugins.smartfile.SmartFile | null> {
|
||||
// lets create a cache descriptor
|
||||
const cacheDescriptor: ICacheDescriptor = {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
@@ -86,7 +95,7 @@ export class NpmRegistry {
|
||||
};
|
||||
|
||||
// lets see if we have something cached
|
||||
const cachedFile: plugins.smartfile.SmartFile = await this.registryCache.getCachedFile(
|
||||
const cachedFile = await this.registryCache.getCachedFile(
|
||||
cacheDescriptor
|
||||
);
|
||||
|
||||
@@ -98,9 +107,8 @@ export class NpmRegistry {
|
||||
(packageArg) => packageArg.name === 'latest'
|
||||
);
|
||||
if (!latestAvailable) {
|
||||
optionsArg = {
|
||||
version: npmPackage.getBestMatchingVersion('*'),
|
||||
};
|
||||
const version = npmPackage.getBestMatchingVersion('*');
|
||||
optionsArg = version ? { version } : undefined;
|
||||
}
|
||||
}
|
||||
const fileResult = await npmPackage.getFileFromPackage(filePathArg, optionsArg);
|
||||
@@ -120,16 +128,15 @@ export class NpmRegistry {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.SmartFile[]> {
|
||||
): Promise<plugins.smartfile.SmartFile[] | null> {
|
||||
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('*'),
|
||||
};
|
||||
const version = npmPackage.getBestMatchingVersion('*');
|
||||
optionsArg = version ? { version } : undefined;
|
||||
}
|
||||
}
|
||||
return npmPackage.getFilesFromPackage(filePath, optionsArg);
|
||||
@@ -142,10 +149,10 @@ export class NpmRegistry {
|
||||
* TODO: rewrite as memory only
|
||||
*/
|
||||
const baseDir = plugins.path.join(paths.nogitDir, packageNameArg.replace('/', '__'));
|
||||
await plugins.smartfile.fs.ensureDir(baseDir);
|
||||
await this.smartFs.directory(baseDir).create();
|
||||
await this.savePackageToDisk(packageNameArg, baseDir);
|
||||
const virtualDir = await plugins.smartfile.VirtualDirectory.fromFsDirPath(baseDir);
|
||||
await plugins.smartfile.fs.remove(baseDir);
|
||||
const virtualDir = await this.smartFileFactory.virtualDirectoryFromPath(baseDir);
|
||||
await this.smartFs.directory(baseDir).recursive().delete();
|
||||
return virtualDir;
|
||||
}
|
||||
|
||||
@@ -224,7 +231,13 @@ export class NpmRegistry {
|
||||
`info: Search on npm for ${plugins.consolecolor.coloredString(searchString, 'pink')}`
|
||||
);
|
||||
|
||||
let body: any;
|
||||
let body: {
|
||||
results?: Array<{
|
||||
package: {
|
||||
name: string;
|
||||
};
|
||||
}>;
|
||||
} | string | undefined;
|
||||
try {
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(this.searchDomain + searchString)
|
||||
@@ -238,7 +251,7 @@ export class NpmRegistry {
|
||||
const packageArray: NpmPackage[] = [];
|
||||
|
||||
// if request failed just return it empty
|
||||
if (!body || typeof body === 'string') {
|
||||
if (!body || typeof body === 'string' || !Array.isArray(body.results)) {
|
||||
return packageArray;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,11 @@ export class PackageVersion implements IVersionData {
|
||||
return packageVersion;
|
||||
}
|
||||
|
||||
name: string;
|
||||
version: string;
|
||||
dependencies: { [key: string]: string };
|
||||
devDependencies: { [key: string]: string };
|
||||
dist: {
|
||||
name!: string;
|
||||
version!: string;
|
||||
dependencies!: { [key: string]: string };
|
||||
devDependencies!: { [key: string]: string };
|
||||
dist!: {
|
||||
integrity: string;
|
||||
shasum: string;
|
||||
tarball: string;
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface ICacheDescriptor {
|
||||
export class RegistryCache {
|
||||
npmregistryRef: NpmRegistry;
|
||||
public levelCache: plugins.levelcache.LevelCache;
|
||||
public smartFileFactory = plugins.smartfile.SmartFileFactory.nodeFs();
|
||||
|
||||
constructor(npmRegistryRefArg: NpmRegistry) {
|
||||
this.npmregistryRef = npmRegistryRefArg;
|
||||
@@ -22,12 +23,12 @@ export class RegistryCache {
|
||||
|
||||
public async getCachedFile(
|
||||
cacheDescriptorArg: ICacheDescriptor
|
||||
): Promise<plugins.smartfile.SmartFile> {
|
||||
): Promise<plugins.smartfile.SmartFile | null> {
|
||||
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(
|
||||
this.getCacheDescriptorAsString(cacheDescriptorArg)
|
||||
);
|
||||
if (cacheEntry) {
|
||||
return plugins.smartfile.SmartFile.fromFoldedJson(cacheEntry.contents.toString());
|
||||
return this.smartFileFactory.fromFoldedJson(cacheEntry.contents.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -55,7 +56,7 @@ export class RegistryCache {
|
||||
}
|
||||
}
|
||||
|
||||
public getCacheDescriptorAsString(cacheDescriptorArg?: ICacheDescriptor) {
|
||||
public getCacheDescriptorAsString(cacheDescriptorArg: ICacheDescriptor) {
|
||||
return `${cacheDescriptorArg.registryUrl}//+//${cacheDescriptorArg.packageName}//+//${
|
||||
cacheDescriptorArg.filePath
|
||||
}//+//${cacheDescriptorArg.distTag || cacheDescriptorArg.version}`;
|
||||
|
||||
@@ -8,6 +8,7 @@ import * as consolecolor from '@push.rocks/consolecolor';
|
||||
import * as levelcache from '@push.rocks/levelcache';
|
||||
import * as smartarchive from '@push.rocks/smartarchive';
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
import * as smartfs from '@push.rocks/smartfs';
|
||||
import * as smartpath from '@push.rocks/smartpath';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
import * as smartrequest from '@push.rocks/smartrequest';
|
||||
@@ -19,6 +20,7 @@ export {
|
||||
levelcache,
|
||||
smartarchive,
|
||||
smartfile,
|
||||
smartfs,
|
||||
smartpath,
|
||||
smartpromise,
|
||||
smartrequest,
|
||||
|
||||
+4
-4
@@ -5,10 +5,10 @@
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"noImplicitAny": true,
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
"verbatimModuleSyntax": true,
|
||||
"types": ["node"]
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
"exclude": ["dist_*/**/*.d.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user