This commit is contained in:
2026-01-04 20:47:43 +00:00
parent a8b07e1dfd
commit abed903b06
15 changed files with 9186 additions and 181 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env node
process.env.CLI_CALL = 'true';
require('@gitzone/tsrun');
const cliTool = require('./ts/index');
import '@git.zone/tsrun';
const cliTool = await import('./ts/index.js');
cliTool.runCli();

View File

@@ -2,10 +2,11 @@
"name": "npmcdn",
"version": "1.0.3",
"description": "a cdn using npm as source",
"type": "module",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH",
"license": "UNLICENSED",
"author": "Task Venture Capital GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"start": "(node --max_old_space_size=100 ./cli.js)",
@@ -13,38 +14,29 @@
"build": "(tsbuild --web)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.28",
"@gitzone/tswatch": "^1.0.30",
"@pushrocks/tapbundle": "^3.0.13",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0"
"@git.zone/tsbuild": "^4.1.0",
"@git.zone/tsrun": "^2.0.1",
"@git.zone/tstest": "^3.1.4",
"@push.rocks/smartnetwork": "^4.4.0",
"@push.rocks/smartrequest": "^5.0.1"
},
"dependencies": {
"@losslessone_private/lole-serviceserver": "^1.0.24",
"@pushrocks/smartstate": "^1.0.14",
"@pushrocks/qenv": "^4.0.10",
"@pushrocks/smartdata": "^4.0.12",
"@pushrocks/projectinfo": "^4.0.5",
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartfile": "^8.0.10",
"@pushrocks/smartmarkdown": "^2.0.6",
"@pushrocks/smartmime": "^1.0.5",
"@pushrocks/smartnpm": "^1.0.39",
"@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/smarttime": "^3.0.38",
"@pushrocks/tapbundle": "^3.2.14",
"@types/autocannon": "^4.1.1",
"@types/compression": "^1.7.2",
"@types/express": "^4.17.13",
"@types/splunk-logging": "^0.9.2",
"compression": "^1.7.4",
"express": "^4.17.1",
"lit-ntml": "^2.20.0",
"prom-client": "^13.2.0",
"splunk-logging": "^0.11.1",
"tty-table": "^4.1.3"
"@losslessone_private/lole-serviceserver": "^1.0.54",
"@push.rocks/projectinfo": "^5.0.2",
"@push.rocks/qenv": "^6.1.0",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartfile": "^11.1.3",
"@push.rocks/smartmarkdown": "^3.0.3",
"@push.rocks/smartmime": "^2.0.4",
"@push.rocks/smartnpm": "^2.0.6",
"@push.rocks/smartpromise": "^4.2.3",
"@push.rocks/smarttime": "^4.1.1",
"@types/compression": "^1.8.1",
"@types/express": "^5.0.6",
"compression": "^1.8.1",
"express": "^4.22.1",
"lit-ntml": "^4.0.2",
"prom-client": "^15.1.3"
},
"private": true,
"browserslist": [

8906
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

184
readme.md
View File

@@ -1,34 +1,164 @@
# @losslessone/services/servezone/npmcdn
undefined variable
# npmcdn
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/npmcdn)
* [gitlab.com (source)](https://gitlab.com/losslessone/services/servezone/npmcdn)
* [github.com (source mirror)](https://github.com/losslessone/services/servezone/npmcdn)
* [docs (typedoc)](https://losslessone/services/servezone.gitlab.io/npmcdn/)
> 🚀 A blazing-fast CDN that serves files directly from npm packages
## Status for master
`npmcdn` is a lightweight, TypeScript-powered CDN server that allows you to serve specific files from npm packages. Think of it as your own private unpkg/jsdelivr, but with fine-grained control over which packages are accessible.
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/losslessone/services/servezone/npmcdn/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/losslessone/services/servezone/npmcdn/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/npmcdn)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/losslessone/services/servezone/npmcdn)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/npmcdn)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/npmcdn)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/npmcdn)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
## Issue Reporting and Security
## Usage
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
Use TypeScript for best in class intellisense
For further information read the linked docs at the top of this readme.
## ✨ Features
> UNLICENSED licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
- **📦 Package Allowlisting** - Only serve files from packages you explicitly approve
- **🔢 Version Support** - Request specific versions, version ranges, or dist-tags
- **⚡ Smart Caching** - Built-in cache headers for optimal performance
- **🗜️ Compression** - Automatic gzip compression for smaller payloads
- **🔍 Peek Mode** - Dev mode includes a UI to browse available packages and files
- **📄 MIME Type Detection** - Automatic content-type detection for served files
- **🌐 CORS Ready** - Cross-origin requests supported out of the box
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
## 📦 Installation
```bash
pnpm add npmcdn
```
## 🚀 Quick Start
```typescript
import { UiPublicServer } from 'npmcdn';
const server = new UiPublicServer({
port: 3000,
npmRegistryUrl: 'https://registry.npmjs.org/',
allowedPackages: [
'@push.rocks/smartfile',
'@push.rocks/smartpromise',
// Add more packages as needed
],
packageBaseDirectory: './', // Path within packages to serve from
mode: 'dev', // 'dev' or 'prod'
});
await server.startServer();
```
## 📡 API Endpoints
Once running, access files using this URL pattern:
```
http://localhost:3000/@scope/package-name/path/to/file.js?version=1.2.3
```
### Query Parameters
| Parameter | Description | Example |
|-----------|-------------|---------|
| `version` | Semver version or range | `1.2.3`, `^1.0.0`, `2.x.x` |
| `disttag` | Dist tag name | `latest`, `beta`, `next` |
### Examples
```bash
# Get the readme from latest version
curl http://localhost:3000/@push.rocks/smartfile/readme.md
# Get package.json from specific version
curl http://localhost:3000/@push.rocks/smartfile/package.json?version=10.0.0
# Get a file using version range
curl http://localhost:3000/@push.rocks/smartfile/dist_ts/index.js?version=11.x.x
```
## ⚙️ Configuration Options
```typescript
interface IPublicServerOptions {
port?: number; // Server port (default: 8080)
npmRegistryUrl?: string; // npm registry URL (default: https://registry.npmjs.org)
allowedPackages?: string[]; // Array of allowed package names
packageBaseDirectory?: string; // Base path within packages (default: './')
mode: 'dev' | 'prod'; // Server mode
log?: boolean; // Enable request logging (default: true)
}
```
## 🔧 Dev Mode Features
When running in `dev` mode, you get access to additional endpoints:
- **`/peek/`** - Browse all allowlisted packages and their files
- **`/readme/`** - View the server's readme as HTML
These endpoints help you explore what's available without needing to know exact file paths.
## 🏗️ Architecture
```
┌─────────────────────────────────────────────────────────┐
│ Client Request │
│ GET /@scope/package/path/to/file.js?version=1.2.3 │
└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────┐
│ UiPublicServer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────┐ │
│ │ Allowlist │ │ Cache │ │ GZIP │ │
│ │ Check │──│ Layer │──│ Compression │ │
│ └─────────────┘ └─────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────┐
│ npm Registry │
│ (fetch package tarball) │
└─────────────────────────────────────────────────────────┘
```
## 🔒 Security
- **Allowlisting** - Only explicitly allowed packages can be served
- **Path Validation** - Requests are validated against the allowed package base directory
- **No Arbitrary Execution** - The server only serves static files, no code execution
## 🛠️ Use Cases
- **Microservices** - Serve shared frontend assets across multiple services
- **Internal CDN** - Host internal packages for your organization
- **Development** - Quick access to npm package files during development
- **Documentation** - Serve package documentation directly
## 📊 Response Headers
The server sets appropriate headers for caching and content delivery:
| Header | Value |
|--------|-------|
| `content-type` | Detected MIME type |
| `content-length` | File size in bytes |
| `cache-control` | `max-age` based on version specificity |
| `access-control-allow-origin` | `*` (configurable) |
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
**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.
### Trademarks
This 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 or third parties, 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 or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
### Company Information
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or further information, please contact us via email at hello@task.vc.
By 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.

View File

@@ -1,4 +1,4 @@
import { UiPublicServer } from '../ts';
import { UiPublicServer } from '../ts/index.js';
export const defaultPublicServer = new UiPublicServer({
port: 3000,

View File

@@ -1,12 +1,11 @@
import { tap, expect } from '@pushrocks/tapbundle';
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as uiPublicServer from '../ts/npm-publicserver.classes.uipublicserver';
import * as smartnetwork from '@pushrocks/smartnetwork';
import * as smartrequest from '@pushrocks/smartrequest';
import * as uiPublicServer from '../ts/npm-publicserver.classes.uipublicserver.js';
import * as smartnetwork from '@push.rocks/smartnetwork';
import { SmartRequest } from '@push.rocks/smartrequest';
const plugins = {
smartnetwork,
smartrequest,
};
let testserverInstance: uiPublicServer.UiPublicServer;
@@ -18,7 +17,7 @@ tap.test('should create an instance of PublicServer', async () => {
allowedPackages: ['@pushrocks/smartfile'],
mode: 'prod',
});
expect(testserverInstance).to.be.instanceOf(uiPublicServer.UiPublicServer);
expect(testserverInstance).toBeInstanceOf(uiPublicServer.UiPublicServer);
});
tap.test('should start the server', async () => {
@@ -26,8 +25,8 @@ tap.test('should start the server', async () => {
const result1 = await smartnetworkInstance.isLocalPortUnused(3000);
await testserverInstance.startServer();
const result2 = await smartnetworkInstance.isLocalPortUnused(3000);
expect(result1).to.be.true;
expect(result2).to.be.false;
expect(result1).toBeTrue();
expect(result2).toBeFalse();
});
tap.skip.test('optional manual testing', async (toolsArg) => {
@@ -35,65 +34,53 @@ tap.skip.test('optional manual testing', async (toolsArg) => {
});
tap.test('should NOT deliver a file for a malformed org', async () => {
const response = await plugins.smartrequest.request('http://localhost:3000/someorg/somemodule', {
method: 'GET',
});
console.log(response.body);
expect(response.body).to.equal('npmorg "someorg" must start with @');
const response = await SmartRequest.create()
.url('http://localhost:3000/someorg/somemodule')
.get();
const body = await response.text();
console.log(body);
expect(body).toEqual('npmorg "someorg" must start with @');
});
tap.test('should NOT deliver a file for a nonexisting file', async () => {
const response = await plugins.smartrequest.request(
'http://localhost:3000/@pushrocks/smartfile/readme2.md',
{
method: 'GET',
}
);
console.log(response.body);
expect(response.body.includes('@pushrocks/smartfile@ does not have a file at')).to.be.true;
const response = await SmartRequest.create()
.url('http://localhost:3000/@pushrocks/smartfile/readme2.md')
.get();
const body = await response.text();
console.log(body);
expect(body.includes('@pushrocks/smartfile@ does not have a file at')).toBeTrue();
});
tap.test('should deliver a file for an existing file', async () => {
const response = await plugins.smartrequest.request(
'http://localhost:3000/@pushrocks/smartfile/readme.md',
{
method: 'GET',
}
);
expect(response.body.startsWith('# @pushrocks/smartfile')).to.be.true;
const response = await SmartRequest.create()
.url('http://localhost:3000/@pushrocks/smartfile/readme.md')
.get();
const body = await response.text();
expect(body.startsWith('# @pushrocks/smartfile')).toBeTrue();
});
tap.test('should deliver different versions', async () => {
const response = await plugins.smartrequest.request(
'http://localhost:3000/@pushrocks/smartfile/package.json?version=7',
{
method: 'GET',
}
);
const packageJson = response.body;
expect(packageJson.version.startsWith('7')).to.be.true;
const response = await SmartRequest.create()
.url('http://localhost:3000/@pushrocks/smartfile/package.json?version=7')
.get();
const packageJson = await response.json();
expect(packageJson.version.startsWith('7')).toBeTrue();
const response2 = await plugins.smartrequest.request(
'http://localhost:3000/@pushrocks/smartfile/package.json?version=8.x.x',
{
method: 'GET',
}
);
const packageJson2 = response2.body;
expect(packageJson2.version.startsWith('8')).to.be.true;
const response2 = await SmartRequest.create()
.url('http://localhost:3000/@pushrocks/smartfile/package.json?version=8.x.x')
.get();
const packageJson2 = await response2.json();
expect(packageJson2.version.startsWith('8')).toBeTrue();
const response3 = await plugins.smartrequest.request(
'http://localhost:3000/@pushrocks/smartfile/package.json?version=6.0.6',
{
method: 'GET',
}
);
const packageJson3 = response3.body;
expect(packageJson3.version).to.equal('6.0.6');
const response3 = await SmartRequest.create()
.url('http://localhost:3000/@pushrocks/smartfile/package.json?version=6.0.6')
.get();
const packageJson3 = await response3.json();
expect(packageJson3.version).toEqual('6.0.6');
});
tap.test('should stop the server', async () => {
await testserverInstance.stopServer();
});
tap.start();
export default tap.start();

View File

@@ -1,8 +1,8 @@
export * from './npm-publicserver.classes.uipublicserver';
export * from './npm-publicserver.classes.uipublicserver.js';
import { UiPublicServer } from '.';
import { UiPublicServer } from './npm-publicserver.classes.uipublicserver.js';
process.env.UIP_ENV = process.env.BACKEND_URL.includes('develop-backend') ? 'dev' : 'prod';
process.env.UIP_ENV = process.env.BACKEND_URL?.includes('develop-backend') ? 'dev' : 'prod';
export const defaultPublicServer = new UiPublicServer({
port: 3000,

View File

@@ -1,20 +0,0 @@
import * as plugins from './plugins';
export const logger = new plugins.splunkLogging.Logger({
token: '',
});
logger.send({
message: {
package: '',
subFolder: '',
filePath: '',
status: 200,
},
severity: 'info',
metadata: {
host: 'ui-publicserver',
source: 'nodejs',
sourcetype: 'process',
},
});

View File

@@ -1,7 +1,7 @@
import * as interfaces from './interfaces';
import * as plugins from './plugins';
import * as paths from './paths';
import * as ntml from './ntml';
import * as interfaces from './interfaces.js';
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import * as ntml from './ntml/index.js';
export interface IPublicServerOptions {
packageBaseDirectory?: string;
@@ -17,9 +17,7 @@ export interface IPublicServerOptions {
*/
export class UiPublicServer {
public projectinfo = new plugins.projectinfo.ProjectinfoNpm(paths.packageDir);
public readme = new plugins.smartmarkdown.SmartMarkdown().markdownToHtml(
plugins.smartfile.fs.toStringSync(plugins.path.join(paths.packageDir, 'readme.md'))
);
public readme: string;
public startedAt: string;
private server: plugins.http.Server;
private npmRegistry: plugins.smartnpm.NpmRegistry;
@@ -53,11 +51,24 @@ export class UiPublicServer {
});
}
/**
* initializes the readme content
*/
private async initReadme(): Promise<void> {
const readmePath = plugins.path.join(paths.packageDir, 'readme.md');
const readmeContent = await plugins.smartfile.fs.toStringSync(readmePath);
this.readme = await plugins.smartmarkdown.SmartMarkdown.easyMarkdownToHtml(readmeContent);
}
/**
* starts the server
*/
public async startServer() {
console.log('starting the uipublicserver');
// Initialize readme
await this.initReadme();
const done = plugins.smartpromise.defer();
const expressApplication = plugins.express();
@@ -235,7 +246,7 @@ export class UiPublicServer {
const requestDescriptor = `${packageName}/${filePath}/${distTag}/${version}`;
let smartfile: plugins.smartfile.Smartfile;
let smartfile: plugins.smartfile.SmartFile;
// protect against parallel requests
if (this.requestMap[requestDescriptor]) {
@@ -268,6 +279,10 @@ export class UiPublicServer {
};
}
// Detect mime type from buffer
const mimeResult = await plugins.smartmime.detectMimeType({ buffer: smartfile.contentBuffer });
const contentType = mimeResult?.mime || 'application/octet-stream';
return {
headers: {
'cache-control': `max-age=${
@@ -276,7 +291,7 @@ export class UiPublicServer {
: plugins.smarttime.getMilliSecondsFromUnits({ days: 1 })) / 1000
}`,
'content-length': smartfile.contentBuffer.length.toString(),
'content-type': plugins.smartmime.detectMimeType(smartfile.path),
'content-type': contentType,
},
status: 200,
body: smartfile.contentBuffer,

View File

@@ -1,5 +1,6 @@
import { UiPublicServer } from '../npm-publicserver.classes.uipublicserver';
import * as plugins from '../plugins';
import type { UiPublicServer } from '../npm-publicserver.classes.uipublicserver.js';
import * as plugins from '../plugins.js';
export const getBody = async (uipublicServerArg: UiPublicServer, contentArg: string | string[]) => {
return await plugins.litNtml.html`
<head></head>
@@ -50,7 +51,7 @@ export const getBody = async (uipublicServerArg: UiPublicServer, contentArg: str
</div>
${contentArg}
<div class="footer">
UiPublicServer v${uipublicServerArg.projectinfo.version} |
UiPublicServer v${uipublicServerArg.projectinfo.version} |
running since ${uipublicServerArg.startedAt} |
<a href="https://lossless.gmbh" target="_blank">Legal Info</a></div>
</div>

View File

@@ -1 +1 @@
export * from './body';
export * from './body.js';

View File

@@ -1,3 +1,3 @@
import * as plugins from './plugins';
import * as plugins from './plugins.js';
export const packageDir = plugins.path.join(__dirname, '../');
export const packageDir = plugins.path.join(import.meta.dirname, '../');

View File

@@ -5,15 +5,15 @@ import * as path from 'path';
export { http, url, path };
// @pushrocks scope (maintained by Lossless GmbH)
import * as projectinfo from '@pushrocks/projectinfo';
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartfile from '@pushrocks/smartfile';
import * as smartmime from '@pushrocks/smartmime';
import * as smartmarkdown from '@pushrocks/smartmarkdown';
import * as smartnpm from '@pushrocks/smartnpm';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smarttime from '@pushrocks/smarttime';
// @push.rocks scope
import * as projectinfo from '@push.rocks/projectinfo';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartfile from '@push.rocks/smartfile';
import * as smartmime from '@push.rocks/smartmime';
import * as smartmarkdown from '@push.rocks/smartmarkdown';
import * as smartnpm from '@push.rocks/smartnpm';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smarttime from '@push.rocks/smarttime';
export {
projectinfo,
@@ -31,6 +31,5 @@ import compression from 'compression';
import express from 'express';
import * as litNtml from 'lit-ntml';
import * as promClient from 'prom-client';
import splunkLogging from 'splunk-logging';
export { compression, express, litNtml, promClient, splunkLogging };
export { compression, express, litNtml, promClient };

12
tsconfig.json Normal file
View File

@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

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"
}