利用kappa搭建私有NPM倉庫

NPM

寫在前面

採用Node.js開發私有或者商業項目的時候,咱們須要建立一些內部使用的module供多個項目之間共用,這些module顯然你不想發佈到社區,默認狀況下npm publishnpm install都是對registry.npmjs.org進行操做,而咱們須要的是這樣一種倉庫:對於一個module,首先操做私有倉庫,若是私有倉庫中不存在此module,則操做官方的倉庫,架構大體以下:html

+--+
+------------+            |p |
| client     |   'foo'?   |r |          +------------+
|            |  --------> |o |  'foo'?  |            |
|            |            |x |  ------> | private    |
|            |            |y | <------  | registry   |
|            |            |  |    404   +------------+
|            |            |  |
|            |            |  |          'foo'?     +---------------+
|            |            |  |  -----------------> |               |
|            |            |  | <-----------------  |  public       |
|            | <--------  |  |         'foo'       |  registry     |
+------------+    'foo'   +--+                     +---------------+

本文采用eBay開源的kappa代理和NPM官方registry來實現以上架構。node

CouchDB

CouchDB是專門爲Web而生的數據庫,它以JSON的格式對數據進行存儲,數據庫的操做是經過HTTP REST方法進行。CouchDB須要的版本爲1.5.0及以上。(如下是在Mac上安裝CouchDB的方法,其餘系統安裝方法見官方文檔git

$ brew install automake libtool erlang icu4c spidermonkey 
$ brew link icu4c
$ brew link erlang
$ brew install couchdb

編輯配置文件local.ini(/usr/local/etc/couchdb/local.ini),添加以下內容:github

[couch_httpd_auth]
public_fields = appdotnet, avatar, avatarMedium, avatarLarge, date, email, fields, freenode, fullname, github, homepage, name, roles, twitter, type, _id, _rev
users_db_public = true

[httpd]
secure_rewrites = false

[couchdb]
delayed_commits = false

以後,部署自啓動腳本並啓動CouchDBmongodb

$ ln -sfv /usr/local/opt/couchdb/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.couchdb.plist

建立數據庫registry數據庫

$ curl -X PUT http://localhost:5984/registry

添加管理員admin,密碼爲adminnpm

$ HOST="http://127.0.0.1:5984"
$ curl -X PUT $HOST/_config/admins/admin -d '"admin"'

NPM Registry

接下來咱們來安裝NPM Registry應用json

$ git clone git://github.com/npm/npm-registry-couchapp
$ cd npm-registry-couchapp
$ npm install

以後,同步ddoc_design/scratchsegmentfault

$ echo "_npm-registry-couchapp:couch=http://admin:admin@localhost:5984/registry" >> ~/.npmrc
$ npm start

確認views已經load:api

$ npm run load
LOADING: fieldsInUse
HTTP/1.1 200 OK
Server: CouchDB/1.5.0 (Erlang OTP/R16B03)
ETag: "DPBK9T7XVY4LAM7S6CS2G2JLZ"
Date: Thu, 01 May 2014 06:06:01 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 0
Cache-Control: must-revalidate

把ddoc從_design/scratchcopy到_design/app

$ npm run copy
> npm-registry-couchapp@2.1.4 copy /Users/weizhifeng/dev/node/npm-registry-couchapp
> bash ./copy.sh

Did you already run the load-views.sh script? (type 'yes')
yes
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    41  100    41    0     0  10408      0 --:--:-- --:--:-- --:--:-- 13666
{"ok":true,"id":"_design/app","rev":"1-36f4a9b735467b7817ae26b547524fd2"}
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4869  100  4869    0     0  1051k      0 --:--:-- --:--:-- --:--:-- 1188k
{"ok":true,"id":"_design/_auth","rev":"2-ebde50b9ecf5a06240c0e3c4166c847f"}

以上完成以後,咱們就能夠把npm的倉庫指向本身剛搭建的倉庫:

$ echo "registry = http://admin:admin@localhost:5984/registry/_design/app/_rewrite" >> ~/.npmrc

爲了能發佈module,須要添加npm用戶:

$ npm adduser

用戶添加完成以後,能夠把私有module進行發佈:

$ cd your-own-package
$ npm publish

發佈完成以後,能夠在CouchDB的管理界面中找到module的信息。

這樣,咱們就搭建好了私有的npm倉庫,而且已經能夠向其中發佈module了。那麼接下來咱們要實現架構圖中的Proxy

Kappa

Kappa是由eBay建立的一個基於npm-delegatehapi的npm代理,經過kappa咱們不須要複製整個公共的倉庫數據就能建立本身的私有倉庫。

安裝kappa

$ npm install -g kappa

下載npm-proxy應用:

$ git clone https://github.com/JeremyWei/npm-proxy.git
$ cd npm-proxy && npm install

修改config.json文件:

{
    "servers": [
        {
            "host": "localhost",
            "port": 8100
        },
        {
            "host": "localhost",
            "port": 8143,
            "tls": {
                "requestCert": true,
                "rejectUnauthorized": true,
                "key": "file:./key.pem",
                "cert": "file:./cert.pem"
            }
        }
    ],
    "plugins": {
        "kappa": {
            "vhost": "npm.myorg.com",
            "paths": [
                "http://admin:admin@localhost:5984/registry/_design/app/_rewrite/",
                "https://registry.npmjs.org/"
            ]
        }
    }
}
  • vhost是虛擬主機,能夠修改爲本身須要的,可是要注意與下面的內容保持一致。
  • paths數組第一個元素是本地私有倉庫,第二個元素是公共倉庫。

以後添加npm.myorg.com到hosts

$ echo "127.0.0.1 npm.myorg.com" >> /etc/hosts

在啓動proxy以前須要npm cache clean清除一下緩存,不然頗有可能在npm start時候出現如下錯誤:

2013-12-02T23:19:33.688Z HAPI,HANDLER,ERROR {"msec":0.15241800248622894}
2013-12-02T23:19:33.689Z HAPI,RESPONSE,ERROR unspecified

啓動proxy:

$ npm start
> kappa -c config.json

Kappa listening on localhost:8100
Kappa listening on localhost:8143

啓動成功以後就能夠把registry地址換成代理地址:

echo "registry = http://npm.myorg.com:8100" >> ~/.npmrc

完成以後,咱們在本身的項目中測試代理是否工做正常:

$ npm install 
npm http GET http://npm.myorg.com:8100/mongodb
npm http GET http://npm.myorg.com:8100/mocha

若是以上沒有問題,那麼恭喜你終於有了本身的私有npm倉庫。

參考

相關文章
相關標籤/搜索