Alessio Biancalana Grab The Blaster di Alessio Biancalana

Registry npm privato: in cloud con Nodejitsu oppure in-house

Node.js

Proprio in questi giorni, ho letto che Nodejitsu ha cominciato a implementare il proprio modello per i registry privati di pacchetti npm in modo che le aziende possano mantenere i propri package privati su un server in cloud a prezzi comunque sostenibili. Il tutto si basa sull'utilizzo del modulo di Node smart-private-npm; di seguito, un esempio preso dal loro blog dove sfruttano Node.js per fornire un registry privato insieme alla loro libreria, da poco (immagino) resa open source per chi comunque non può permettersi un costo così elevato (perché in startup o per qualche altro motivo) e decide di sostituire al costo della cloud qualche ora di effort e una macchina - o una VPS. Ovviamente io consiglio DigitalOcean che permette l'hosting di droplet su SSD :D

var smartPrivateNpm = require("smart-private-npm"),
    url = require("url");

//
// Configure your private npm. You could load this in from a file
// somewhere.
//
var config = {
  rewrites: require("./config/rewrites"),
  proxy: {
    //
    // Location of the target public npm registry. 
    //
    npm: url.parse("http://user:[email protected]"),
    //
    // Private npm options.
    //
    policy: {
      npm: url.parse("http://user:[email protected]"),
      private: {
        //
        // This is the list of 'known private modules'
        // that will always be proxied to the private npm.
        // It is built over time by remembering 'publish' requests.
        //
      },
      blacklist: {
        //
        // This is the list of modules that will ALWAYS be proxies
        // to the private npm, no matter what.
        //
      },
      whitelist: {
        //
        // If enabled: only requests for these modules will be served
        // by the proxy (unless they are 'known private modules').
        //
      },
      //
      // In 'transparent mode' the proxy will always forward to
      // the public registry.
      //
      transparent: false
    }
  },
  //
  // Server options (from 'create-servers')
  //
  http: 80
  https: {
    port: 443,
    root: "/path/to/your/ssl/files",
    key: "your-ssl.key",  // or .pem
    key: "your-ssl.cert", // or .pem
  }
};

smartPrivateNpm.createServer(config, function (err, servers) {
  if (err) {
    console.log("Error starting private npm: %j", servers);
    return process.exit(1);
  }

  console.log("Private npm running on %j servers.", Object.keys(servers));
});

Per l'operazione di pubblicazione dei pacchetti npm su un registry diverso, va semplicemente riadattato poi il comando che andiamo a dare nella shell:

npm publish some-private-code --reg http://localhost/

Ho trovato con cosa giocare stasera dopo il lavoro. In bocca al lupo a chiunque si voglia cimentare, e come sempre grazie a Nodejitsu per tutto l'open source che ci regala.

Photo courtesy of Trygve Lie

comments powered by Disqus

Member of

Previous Random Next