/npm-adapter

Turn your files to an NPM repository

Primary LanguageJavaMIT LicenseMIT

EO principles respected here DevOps By Rultor.com We recommend IntelliJ IDEA

Maven Build Javadoc License Hits-of-Code Maven Central PDD status

Similar solutions:

References:

This is the dependency you need:

<dependency>
  <groupId>com.artipie</groupId>
  <artifactId>npm-adapter</artifactId>
  <version>[...]</version>
</dependency>

How it works?

First, you upload a json file generated by npm publish. Then, you call Npm#publish, which triggers meta.json file update/generation and .tgz source code archives creation.

This gives anything you need to respond on a series of requests generated by npm intall <package_name> command.

An example of meta.json can be found here

How NPM commands work with NPMSlice.

  1. Let's say we use npm publish in the following way:
npm publish --registry=http://localhost:8080

By invoking such command npm sends following http request:

method: "PUT"
url: "/@hello%2fsimple-npm-project"
headers: { 
  "connection": "keep-alive",
  "user-agent": "npm/6.13.6 node/v10.15.1 darwin x64",
  "npm-in-ci": "false",
  "npm-scope": "@hello",
  "npm-session": "52b4a33613bae565",
  "referer": "publish",
  "content-type": "application/json",
  "accept": "*/*",
  "content-length": "1331",
  "accept-encoding": "gzip,deflate",
  "host": "localhost:8080"
} 

With request body which contains data field which contains base64 content of the package. Then we generate the meta data and .tar package and save it into asto.

  1. Let's say now we want to install our package via command:
npm install --registry=http://localhost:8080  @hello/simple-npm-project

First NPM sends following request to get the meta data:

method: "GET"
url: "/@hello%2fsimple-npm-project"
headers: { 
  "connection": "keep-alive",
  "user-agent": "npm/6.13.6 node/v10.15.1 darwin x64",
  "npm-in-ci": "false",
  "npm-scope": "@hello",
  "npm-session": "ee324ef71224a7ee",
  "referer": "install [REDACTED]",
  "pacote-req-type": "packument",
  "pacote-pkg-id": "registry:@hello/simple-npm-project",
  "accept":
   "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*",
  "accept-encoding": "gzip,deflate",
  "host": "localhost:8080"
} 

and expect following response body with meta info from asto like:

{
  "name": "@hello/simple-npm-project",
  "_id": "@hello/simple-npm-project",
  "readme": "ERROR: No README data found!",
  "time": {
    "created": "2020-03-12T14:51:34.44"
  },
  "users": {},
  "versions": {
    "1.0.1": {
      "name": "@hello/simple-npm-project",
      "version": "1.0.1",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "readme": "ERROR: No README data found!",
      "_id": "@hello/simple-npm-project@1.0.1",
      "_nodeVersion": "10.15.1",
      "_npmVersion": "6.13.6",
      "maintainers": [
        {}
      ],
      "dist": {
        "integrity": "sha512-uTxRHajE8jAoeUMI32YjujfWTxD6D2Ng2hZoeR8jd8Wvx+3cJsda8mh64Cq4pFYvl65Za8OkzLAo2/vU/ibq9A==",
        "shasum": "d20a235fc4fe4f68b02649f349bfeb324178d9b1",
        "tarball": "http://127.0.0.1:8080/@hello/simple-npm-project/-/@hello/simple-npm-project-1.0.1.tgz"
      }
    }
  },
  "_attachments": {},
  "dist-tags": {
    "latest": "1.0.1"
  }
}

Then NPM using such meta info sends another request to get a package:

method: "GET"
url: "/@hello/simple-npm-project/-/@hello/simple-npm-project-1.0.1.tgz"
headers: {
  "connection": "keep-alive",
  "user-agent": "npm/6.13.6 node/v10.15.1 darwin x64",
  "npm-in-ci": "false",
  "npm-scope": "@hello",
  "npm-session": "ee324ef71224a7ee",
  "referer": "install [REDACTED]",
  "pacote-req-type": "tarball",
  "pacote-pkg-id": "registry:@hello/simple-npm-project@http://127.0.0.1:8080/@hello/simple-npm-project/-/@hello/simple-npm-project-1.0.1.tgz",
  "accept": "*/*",
  "host": "127.0.0.1:8080" 
}

NPM Proxy

NPM proxy repository could be created using NpmProxySlice. Proxy repository forwards HTTP requests to specified URL and adapter processes them. After receiving artifact from the remote server, it caches received data and transforms it before sending it back to client and saves to local storage. Similar request received afterwords will not require remote repository call - the cached data will be used.

Detailed explanation about used algorithms and protocols used for proxy implementation can be found here.

How to contribute

Fork repository, make changes, send us a pull request. We will review your changes and apply them to the master branch shortly, provided they don't violate our quality standards. To avoid frustration, before sending us your pull request please run full Maven build:

$ mvn clean install -Pqulice

To avoid build errors use Maven 3.2+.