NPM本地缓存方案

NPM本地缓存方案

如果你被前一篇《centos下搭建npm镜像》搞晕了搞挫败了搞得萎了,毕竟完整镜像npm几十G的内容需要大量的时间,期间可能遇到各种网络问题(由于某些大家都知道的原因),或者像这位哥们吐槽官方教程不给力(https://github.com/npm/npmjs.org/issues/106),你只是想简单架个本地服务而不具备太多系统或程序相关的知识,无法一下子处理各种奇葩的异常,那么你应该继续看下去。

NPM本地缓存?

在天朝网络环境下做nodejs相关开发,在我看来最痛的点就是使用npm的时候各种依赖包下载超级慢和各种异常,所以我关注的几个点:

1,下载依赖速度要快
2,不会因为npm官方镜像挂掉而影响开发
3,私有模块管理

当然,你可以常年挂着VPN,你可以找几个国内靠谱的镜像出现问题的时候切换过去,你可以内部管理模块然后npm link,就可以解决上面的问题,在这之外不妨动手自己架设一个本地服务。

找了一圈,我发现了两个cache模块:https://github.com/mixu/npm_lazyhttps://github.com/rlidwka/sinopia,这两个模块的实现彻底消除了之前完整镜像npm官方的痛,几乎是零配置,他们基本的思路基本一致:在本地运行一个服务器实例,初始化一个空的“仓库”,我们无需关心它是什么样的仓库,当用户向本地服务器发起请求时,先检查本地是否有现成的已更新的包,有则从本地仓库返回,无则从官方或指定的镜像下载请求所需要的包缓存到本地并返回给用户。从某种程度上,该逻辑解决了我们前面提到的两个问题大多数情况,因为如果你使用一个从来没使用过的模块就还是有问题。关于私有模块,sinopia提供了自己的解决方案,私有模块都建议以“local-”开头进行发布,npm_lazy则没有提及。

npm_lazy的使用

作为演示,我们选择npm_lazy快速的架设一个本地npm cache服务。

测试环境:

Shell

[root@localhost ~]# node -vv0.10.21[root@localhost ~]# npm -v1.3.11[root@localhost ~]# lsb_release -aLSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarchDistributor ID: RedHatEnterpriseServerDescription:    Red Hat Enterprise Linux Server release 5.8 (Tikanga)Release:        5.8Codename:       Tikanga

和其他npm模块一样,你只需一条命令,全局安装即可:

Shell

npm install -g npm_lazy

然后在终端里执行npm_lazy服务就跑起来了,是不是很简单→_→,有一点需要注意,如果你是要在内网供大家使用的,而不是本地自己用,需要修改一下配置,让它监听所有ip的请求。

Shell

[root@localhost ~]# cd /usr/local/lib/node_modules/npm_lazy[root@localhost npm_lazy]# lltotal 64drwxr-xr-x   2 nobody root 4096 Jan 20 14:02 bin-rw-r–r–   1 nobody root 1130 Jan 20 17:47 config.js-rw-r–r–   1 root   root 2435 Jan 20 15:50 config.yaml-rw-r–r–   1 nobody root  171 Jan 17 07:43 index.jsdrwxr-xr-x   2 nobody root 4096 Jan 20 14:02 lib-rw-r–r–   1 nobody root  622 Jan 16 11:05 Makefiledrwxr-xr-x  12 nobody root 4096 Jan 20 14:02 node_modules-rw-r–r–   1 nobody root 8242 Jan 20 14:02 package.json-rw-r–r–   1 nobody root 7097 Jan 17 07:51 readme.md-rw-r–r–   1 nobody root 1397 Jan 17 07:43 server.jsdrwxr-xr-x 147 root   root 4096 Jan 20 16:32 storagedrwxr-xr-x   6 nobody root 4096 Jan 20 14:02 test-rw-r–r–   1 nobody root 1593 Jan 16 10:29 verify.js

修改后的配置文件config.js如下:

JavaScript

var path = require(‘path’),    homePath = path.normalize(process.env[(process.platform == ‘win32’) ? ‘USERPROFILE’ : ‘HOME’]);module.exports = {  // Cache config  // Directory to store cached packages.  // Since any relative path is resolved relative to the current working  // directory when the server is started, you should use a full path.  cacheDirectory: homePath + ‘/.npm_lazy’,  // maximum age before an index is refreshed from remoteUrl  // negative value means no refresh  cacheAge: 60 * 60 * 1000,  // Request config  // max milliseconds to wait for each HTTP response  httpTimeout: 10000,  // maximum number of retries per HTTP resource to get  maxRetries: 5,  // whether or not HTTPS requests are checked against Node’s list of CAs  // set false if you are using your own npm mirror with a self-signed SSL cert  rejectUnauthorized: true,  // Remote and local URL  // external url to npm_lazy, no trailing /  externalUrl: ‘http://x.x.x.x:8888’,  // registry url with trailing /  remoteUrl: ‘http://registry.npmjs.org/’,  // bind port and host  port: 8888,  host: ‘x.x.x.x’};

你需要修改的配置就是把我示例里是x.x.x.x换成你的服务器的内网ip即可,注意externalUrl和port的端口保持一致,externalUrl最后不要以/结尾,其他字段根据自己的需求调整,比如缓存时间,失败重连次数和外网镜像等。服务跑起来后,你需要设置一下你工作机的npm,将它的镜像地址改成你自己的内网服务器就可以开始使用了。

Shell

1npm config set registry http://x.x.x.x:8888/

正常使用npm intall安装

npm install

sinopia的安装过程也是一样的,顺便把配置晒一下:

Shell

[root@localhost lib]# cd /usr/local/lib/node_modules/sinopia/lib[root@localhost lib]# lltotal 124-rw-r–r–  1 nobody root  4245 Jan 19 00:27 cli.js-rw-r–r–  1 nobody root  2407 Jan 19 03:02 config_def.yaml-rw-r–r–  1 nobody root   441 Oct 26 20:12 config_gen.js-rw-r–r–  1 nobody root  4910 Jan 14 00:01 config.js-rw-r–r–  1 root   root  2436 Jan 21 09:40 config.yaml-rw-r–r–  1 nobody root  1394 Oct 26 19:58 error.js-rw-r–r–  1 nobody root 10948 Jan 19 03:01 index.js-rw-r–r–  1 nobody root  4526 Jan 14 00:47 local-fs.js-rw-r–r–  1 nobody root 13305 Jan 14 02:53 local-storage.js-rw-r–r–  1 nobody root  3523 Dec 23 00:29 logger.js-rw-r–r–  1 nobody root  4463 Dec 29 14:26 middleware.js-rw-r–r–  1 nobody root  4422 Jun 20  2013 npmsslkeys.js-rw-r–r–  1 nobody root  3467 Nov 11 07:37 status-cats.jsdrwxr-xr-x 10 root   root  4096 Jan 21 09:54 storage-rw-r–r–  1 nobody root 10621 Dec 29 13:44 storage.js-rw-r–r–  1 nobody root  1337 Dec 23 00:09 streams.js-rw-r–r–  1 nobody root  7698 Dec 27 21:07 up-storage.js-rw-r–r–  1 nobody root  3550 Jan 19 02:23 utils.js[root@localhost lib]# cat config.yaml# path to a directory with all packagesstorage: ./storage# a list of usersusers:  admin:    # crypto.createHash(‘sha1’).update(pass).digest(‘hex’)    password: ‘这里的密码hash是通过上面一行的命令在node控制台获得,将pass改成你的密码明文字符串,执行得到hash’# a list of other known repositories we can talk touplinks:  npmjs:    url: https://registry.npmjs.org/    # amount of time (in milliseconds) to wait for repository to respond    # before giving up and use the local cached copy    #timeout: 30000    # maximum time (in seconds) in which data is considered up to date    #    # default is 2 minutes, so server won’t request the same data from    # uplink if a similar request was made less than 2 minutes ago    maxage: 6 * 60 * 60packages:  # uncomment this for packages with « local- » prefix to be available  # for admin only, it’s a recommended way of handling private packages  ‘local-*’:    allow_access: admin    allow_publish: admin    # you can override storage directory for a group of packages this way:    storage: ‘local_storage’  ‘*’:    # allow all users to read packages (‘all’ is a keyword)    # this includes non-authenticated users    allow_access: all    # allow ‘admin’ to publish packages    allow_publish: admin    # if package is not available locally, proxy requests to ‘npmjs’ registry    proxy: npmjs###################################################################### Advanced settings###################################################################### if you use nginx with custom path, use this to override links#url_prefix: https://dev.company.local/sinopia/# you can specify listen address (or simply a port)listen: 0.0.0.0:4873# type: file | stdout | stderr# level: trace | debug | info | http (default) | warn | error | fatal## parameters for file: name is filename#  {type: ‘file’, path: ‘sinopia.log’, level: ‘debug’},## parameters for stdout and stderr: format: json | pretty#  {type: ‘stdout’, format: ‘pretty’, level: ‘debug’},logs:  – {type: stdout, format: pretty, level: http}  #- {type: file, path: sinopia.log, level: info}# you can specify proxy used with all requests in wget-like manner here# (or set up ENV variables with the same name)#http_proxy: http://something.local/#https_proxy: https://something.local/#no_proxy: localhost,127.0.0.1# maximum size of uploaded json document# increase it if you have « request entity too large » errors#max_body_size: 1mb

上述配置config.yaml是通过config_def.yaml复制改名得到的,listen的值改为0.0.0.0:4873用于监听所有ip的请求,其他配置根据需求自己定制。

npm_lazy功能相对简单,关于sinopia更多的配置信息,注意事项,以及其他类似功能的包可以看https://github.com/rlidwka/sinopia

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *