NPM 私库从搭建到数据迁移最后容灾备份的一些解决方案

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按照国际惯例,正文开始之前,我们先简单介绍下目前市面上的 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM 私库开源框架"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Verdaccio"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Verdaccio 是 Sinopia 开源框架的一个分支。它提供了自己的小数据库,以及代理其他注册中心的能力(例如:npmjs.org 网站),配置以及部署相对简单,一步到\"胃\"。如果公司的私包比较少的话或者你想偷懒,可以考虑一下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Cnpmjs.org"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大名鼎鼎的 CNPM,想必各位早就感受到了它的速度之“快”,没错,它的 Register 服务就是"},{"type":"text","marks":[{"type":"strong"}],"text":"淘宝镜像"},{"type":"text","text":" (https:\/\/registry.npm.taobao.org\/)。主要是基于 Koa、MySQL 和简单存储服务的企业专用 NPM 注册和 WEB 服务,其中最强大的功能就是它的同步模块机制(定时同步所有源 Registry 的模块、只同步已经存在于数据库的模块、只同步 Popular 模块)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Nexus"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"后端开发的小伙伴应该比较熟悉。Nexus2 主要是用于 Maven\/Gralde 仓库的统一管理,而 Nexus3 则添加了 NPM 插件,可以对 NPM 提供支持,其中 NPM 仓库有三种类型,分别是 Hosted(私有仓库)、Proxy(代理仓库)、Group(组合仓库)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"总体来讲,抛开 Nexus,虽然 "},{"type":"text","marks":[{"type":"strong"}],"text":"Cnpmjs.org"},{"type":"text","text":" 在部署过程以及总体设计方案上相对于 Verdaccio 复杂的多,但是它提供更高的拓展性,定制性,可以支持多种业务使用场景。接下来,我们分别从 "},{"type":"text","marks":[{"type":"strong"}],"text":"Cnpmjs.org 容器化部署"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"数据迁移"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"OSS 容灾备份"},{"type":"text","text":"等内容,层层展开。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Cnpmjs.org 容器化部署"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前,公司的应用部署都是容器化部署,内部搭建了 "},{"type":"text","marks":[{"type":"strong"}],"text":"Ipaas"},{"type":"text","text":" 平台,应用流程化部署以及一键发布。而 "},{"type":"text","marks":[{"type":"strong"}],"text":"Cnpmjs.org"},{"type":"text","text":" 也附带了 "},{"type":"text","marks":[{"type":"strong"}],"text":"Dockerfile"},{"type":"text","text":" 以及 "},{"type":"text","marks":[{"type":"strong"}],"text":"docker-compose.yml"},{"type":"text","text":" 文件,所以,这里大致讲解下怎么用 "},{"type":"text","marks":[{"type":"strong"}],"text":"Docker"},{"type":"text","text":" 部署吧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先让我们看看 "},{"type":"text","marks":[{"type":"strong"}],"text":"Dockerfile"},{"type":"text","text":" 文件"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"FROM node:12\nMAINTAINER zian [email protected]\n\n# Working enviroment\nENV \\\n    CNPM_DIR=\"\/var\/app\/cnpmjs.org\" \\\n    CNPM_DATA_DIR=\"\/var\/data\/cnpm_data\" \n\n# Shell 格式\n# 在 Docker Build 时运行\nRUN mkdir -p ${CNPM_DIR}\n\n# 指定工作目录:用 WORKDIR 指定的工作目录,会在构建镜像的每一层中都存在\nWORKDIR ${CNPM_DIR}\n\n# 复制指令:从上下文目录中复制目录或文件到容器里指定的路径\nCOPY package.json ${CNPM_DIR}\n\nRUN npm set registry https:\/\/registry.npm.taobao.org\n\nRUN npm install --production\n\nCOPY .  ${CNPM_DIR}\nCOPY docs\/dockerize\/config.js  ${CNPM_DIR}\/config\/\n\n# 声明端口(7001 为 Register 服务、7002 为 web 服务)\nEXPOSE 7001\/tcp 7002\/tcp\n\n# 匿名数据卷:在启动容器时忘记挂载数据卷,会自动挂载到匿名卷。\nVOLUME [\"\/var\/data\/cnpm_data\"]\n\nRUN chmod +x ${CNPM_DIR}\/docker-entrypoint_prod.sh\n\n# Entrypoint \n# Exec 格式\n# 在 Docker Run 时运行\n# Dockerfile 存在多个 CMD 命令,仅最后一个生效\n# CMD [\"node\", \"dispatch.js\"]\nCMD [\"npm\", \"run\", \"prod\"]\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里把 "},{"type":"text","marks":[{"type":"strong"}],"text":"CMD"},{"type":"text","text":" 命令修改为 "},{"type":"codeinline","content":[{"type":"text","text":"[\"npm\", \"run\", \"prod\"]"}]},{"type":"text","text":",因为增加了一层不同环境的 "},{"type":"text","marks":[{"type":"strong"}],"text":"shell"},{"type":"text","text":" 脚本,目前全局变量全都存放在这里。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:docker-entrypoint_env.sh"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"export DB='db_cnpmjs'\nexport DB_USRNAME='root'\nexport DB_PASSWORD='123456'\nexport DB_HOST='127.0.0.1'\n\nexport BINDING_HOST='0.0.0.0'\n\nDEBUG=cnpm* node dispatch.js \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再修改下 "},{"type":"text","marks":[{"type":"strong"}],"text":"docker-compose.yml"},{"type":"text","text":" 文件,这里把 "},{"type":"text","marks":[{"type":"strong"}],"text":"mysql-db"},{"type":"text","text":" 这个服务删掉了,原因是可通过 "},{"type":"text","marks":[{"type":"strong"}],"text":"\/docs\/dockerize\/config.js"},{"type":"text","text":" 下的配置文件去连接公司测试环境的 "},{"type":"text","marks":[{"type":"strong"}],"text":"MySQL"},{"type":"text","text":" 数据库,则不需要构建生成 "},{"type":"text","marks":[{"type":"strong"}],"text":"mysql-db 镜像"},{"type":"text","text":"。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"version: '3' # docker版本\nservices: # 配置的容器列表\n  web: # 自定义,服务名称\n    build: # 基于 Dockerfile 构建镜像(可增加 args )\n      context: .\n      dockerfile: Dockerfile ## 依赖的 Dockerfile 文件\n    image: cnpmjs.org # 镜像名称或 id\n    volumes:\n      - cnpm-files-volume:\/var\/data\/cnpm_data\n    ports:\n      - \"7001:7001\"\n      - \"7002:7002\" \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意点:1、全局配置文件路径: \/docs\/dockerize\/config.js;2、bindingHost 为 0.0.0.0。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最后,在控制台敲下 "},{"type":"codeinline","content":[{"type":"text","text":"docker-compose up -d"}]},{"type":"text","text":",即以守护进程模式形式启动应用,然后打开浏览器入 "},{"type":"codeinline","content":[{"type":"text","text":"http:\/\/127.0.0.1:7002"}]},{"type":"text","text":",就会看到 WEB 页面。执行 "},{"type":"codeinline","content":[{"type":"text","text":"npm config set registry http:\/\/127.0.0.1:7001"}]},{"type":"text","text":" 可设置为搭建的私库的镜像源地址,这里推荐使用 "},{"type":"codeinline","content":[{"type":"text","text":"nrm"}]},{"type":"text","text":",可自由切换 NPM 源。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"展示站点如下图:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/43\/43b4bb20d2dbb7fc86514d5e238d2c5e.webp","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意点:1、当你改变本地代码之后,先执行 docker-compose build 构建新的镜像,然后执行 docker-compose up -d 取代运行中的容器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"数据迁移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于公司之前用的 "},{"type":"text","marks":[{"type":"strong"}],"text":"Verdaccio"},{"type":"text","text":" 搭建的私库,要切换使用新的 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM 私库"},{"type":"text","text":",意味着要把之前发布过的私包全部迁移过来。大概统计了下,有 400 多个 "},{"type":"text","marks":[{"type":"strong"}],"text":"Package"},{"type":"text","text":",总共有  7000 多个版本,按照正常逻辑,做数据迁移首先会从数据库下手,但是 "},{"type":"text","marks":[{"type":"strong"}],"text":"Verdaccio"},{"type":"text","text":" 并不依赖数据库。刚开始没有一点头绪,大概看了下 "},{"type":"text","marks":[{"type":"strong"}],"text":"Cnpmjs.org"},{"type":"text","text":" 的源码,分析了当我们 "},{"type":"text","marks":[{"type":"strong"}],"text":"publish"},{"type":"text","text":" 模块时,它是怎么把 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM 模块"},{"type":"text","text":" 的元数据存储到数据库。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过路由文件("},{"type":"codeinline","content":[{"type":"text","text":"\/routes\/registry.js"}]},{"type":"text","text":")我们很容易找到 "},{"type":"codeinline","content":[{"type":"text","text":"\/controllers\/registry\/package\/save.js"}]},{"type":"text","text":",这个文件便是我们想要的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"核心代码:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\nvar pkg = this.request.body; \/\/ 这里拿到 npm 模块元数据,即 package.json 文件经过 libnpmpublish模块处理过的 Json 数据\nvar username = this.user.name; \/\/ 当前用户名\nvar name = this.params.name || this.params[0]; \/\/ NPM 模块名\nvar filename = Object.keys(pkg._attachments || {})[0]; \/\/ NPM 模块的压缩后的文件名\nvar version = Object.keys(pkg.versions || {})[0]; \/\/ NPM 模块的最新版本\n\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ Upload Attachment\n\n\/\/ Base64 解码,获取模块文件二进制数据。从 libnpmpublish 模块了解到 tardata.toString('base64'),即NPM 模块文件流转 Base64 字符串\nvar tarballBuffer = Buffer.from(attachment.data, 'base64'); \n\/\/ 默认使用 fs-cnpm,将 NPM 模块文件保存到本地,默认保存路径:path.join(process.env.HOME, '.cnpmjs.org', 'nfs')\nvar uploadResult = yield nfs.uploadBuffer(tarballBuffer, options);\n\nvar versionPackage = pkg.versions[version];\nvar dist = {\n  shasum: shasum,\n  size: attachment.length\n};\n\n\/\/ If nfs upload return a key, record it\nif (uploadResult.url) {\n  dist.tarball = uploadResult.url;\n} else if (uploadResult.key) {\n  dist.key = uploadResult.key;\n  dist.tarball = uploadResult.key;\n}\nvar mod = {\n  name: name,\n  version: version,\n  author: username,\n  package: versionPackage\n};\n\nmod.package.dist = dist;\n\n\/\/ 模块数据保存到数据库\nvar addResult = yield packageService.saveModule(mod);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即只要我们能够拿到 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM"},{"type":"text","text":" 模块的元数据(即 package.json 被处理过的 JSON 数据),就能把模块文件上传到文件系统或者 "},{"type":"text","marks":[{"type":"strong"}],"text":"OSS"},{"type":"text","text":" 服务,同时数据落库。"},{"type":"text","marks":[{"type":"strong"}],"text":"Verdaccio"},{"type":"text","text":" 有两个 API 可以拿到其私库 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM"},{"type":"text","text":" 模块全量数据和当前 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM"},{"type":"text","text":" 模块的 "},{"type":"text","marks":[{"type":"strong"}],"text":"JSON"},{"type":"text","text":" 数据,路径分别是 "},{"type":"codeinline","content":[{"type":"text","text":"\/-\/verdaccio\/packages"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"\/-\/verdaccio\/sidebar\/$PKG$"}]},{"type":"text","text":",其中有 "},{"type":"text","marks":[{"type":"strong"}],"text":"scope"},{"type":"text","text":" 的模块的请求路径是 "},{"type":"codeinline","content":[{"type":"text","text":"\/-\/verdaccio\/sidebar\/$SCOPE$\/$PKG$"}]},{"type":"text","text":"。思路已经很明确了,开始动起来吧!新增 save_zcy.js 文件,基于原来的 "},{"type":"codeinline","content":[{"type":"text","text":"\/controllers\/registry\/package\/save.js"}]},{"type":"text","text":" 稍加改造下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"核心代码:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 请求远程文件,并返回二进制流\nconst handleFiles = function (url) {\n  return new Promise((resolve, reject) => {\n    try {\n      http.get(url, res => {\n        res.setEncoding('binary') \/\/ 二进制\n        let files = ''\n        res.on('data', chunk => { \/\/ 加载到内存\n          files += chunk\n        }).on('end', () => { \/\/ 加载完\n          resolve(files)\n        })\n      }) \n    } catch (error) {\n      reject(error)\n    }\n  })\n};\n\n\/\/ 获取远程模块文件的二进制数据\nyield handleFiles(dist.tarball).then(res => {\n  \/\/ 利用 Buffer 转为对象\n  const tardata = Buffer.from(res, 'binary')\n  pkg._attachments = {};\n  pkg._attachments[filename] = {\n    'content_type': 'application\/octet-stream',\n    'data': tardata.toString('base64'), \/\/ 从缓冲区读取数据,使用 base64 编码并转换成字符串\n    'length': tardata.length,\n  };\n}, error => {\n  this.status = 400;\n  this.body = {\n    error,\n    reason: error,\n  };\n  return;\n});\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下来我们把控制器 save_zcy.js 接入到 Registry 服务的 APP 路由上。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\/\/ 新增 fetchPackageZcy、savePackageZcy 控制器\napp.get('\/:name\/:version', syncByInstall, fetchPackageZcy, savePackageZcy, getOneVersion);\napp.get('\/:name', syncByInstall, fetchPackageZcy, savePackageZcy, listAllVersions);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"控制器 fetchPackageZcy 作用是请求上面的 API(\/-\/verdaccio\/sidebar\/\/ 或 \/-\/verdaccio\/sidebar\/)来拉取对应模块的 JSON 数据。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/8d\/8d99790bd63ec7bb72357a1cb7335b45.webp","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"OK,接下来我们写一个定时任务,每隔一段时间执行 "},{"type":"codeinline","content":[{"type":"text","text":"npm install [name]"}]},{"type":"text","text":",这样原来私库的 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM"},{"type":"text","text":" 包都能够 "},{"type":"text","marks":[{"type":"strong"}],"text":"install"},{"type":"text","text":" 并进入到上面的控制器逻辑,大功告成!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"OSS 容灾备份"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,简单说明下为什么要做 "},{"type":"text","marks":[{"type":"strong"}],"text":"OSS 容灾备份"},{"type":"text","text":",有以下几点。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果服务器上磁盘损坏,易丢失文件,有一定的风险"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"若服务器磁盘爆满,可自动降级上传模块文件到 "},{"type":"text","marks":[{"type":"strong"}],"text":"OSS"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于以上几点,我们整理了下容灾备份方案:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"package publish"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/30\/30d4735575fb9cfadd536f856d8b6c3e.png","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即发布模块文件时本地存储,同时上传到 "},{"type":"text","marks":[{"type":"strong"}],"text":"OSS"},{"type":"text","text":" 作为备份,用到的插件分别是 "},{"type":"text","marks":[{"type":"strong"}],"text":"fs-cnpm"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"oss-cnpm"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"package install"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/75\/7565ba2ebec66c0b62f065c6dd49e7b8.webp","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即下载模块文件时,先判断是否是私包(即包名是否有带  "},{"type":"text","marks":[{"type":"strong"}],"text":"scope"},{"type":"text","text":" ),如果不是私包代理到上游 "},{"type":"text","marks":[{"type":"strong"}],"text":"Registry"},{"type":"text","text":",若是私包先判断服务器本地是否有该私包文件,如果不存在先去 "},{"type":"text","marks":[{"type":"strong"}],"text":"OSS"},{"type":"text","text":" 下载到本地 "},{"type":"text","marks":[{"type":"strong"}],"text":"nfs"},{"type":"text","text":" 目录下,如果存在则直接从 "},{"type":"text","marks":[{"type":"strong"}],"text":"nfs"},{"type":"text","text":" 目录找到模块文件,然后读取并写到 "},{"type":"text","marks":[{"type":"strong"}],"text":"downloads"},{"type":"text","text":" 目录下,最后调用 "},{"type":"text","marks":[{"type":"strong"}],"text":"fs.createReadStream"},{"type":"text","text":" 方法流读取该文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"isEnsureFileExists"},{"type":"text","text":" 即判断模块文件本地是否存在,代码如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"typescript"},"content":[{"type":"text","text":"const mkdirp = require('mkdirp');\nconst fs = require('fs');\n\nfunction ensureFileExists(filepath) {\n  return function (callback) {\n    fs.access(filepath, fs.constants.F_OK, callback);\n  };\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意,在 "},{"type":"text","marks":[{"type":"strong"}],"text":"OSS"},{"type":"text","text":" 下载模块文件到 "},{"type":"text","marks":[{"type":"strong"}],"text":"nfs"},{"type":"text","text":" 之前,一定要先创建模块文件目录,方法如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const mkdirp = require('mkdirp');\n\nfunction ensureDirExists(filepath) {\n  return function (callback) {\n    mkdirp(path.dirname(filepath), callback);\n  };\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"邮件通知"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Cnpmjs.org"},{"type":"text","text":" 本来就带有邮件通知的功能,但只应用错误日志上报。由于我们的私包大部分都是业务组件、工具等,有时候发布正式版本的业务组件需要通知到业务组件的使用方。目前,我们采用 "},{"type":"text","marks":[{"type":"strong"}],"text":"Maintainers"},{"type":"text","text":" 来维护,包含模块的维护者及使用者。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\"maintainers\": [\n  {\n    \"name\": \"yuanzhian\",\n    \"email\": \"[email protected]\"\n  }\n]\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"邮箱配置如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"mail: {\n  enable: true,\n  appname: 'cnpmjs.org',\n  from: process.env.EMAIL_HOST,\n  host: 'smtp.mxhichina.com',\n  service: 'qiye.aliyun', \/\/ 使用了内置传输发送邮件,查看支持列表:https:\/\/nodemailer.com\/smtp\/well-known\/\n  port: 465, \/\/ SMTP 端口\n  secureConnection: true, \/\/ 使用了 SSL\n  auth: {\n     user: process.env.EMAIL_HOST,\n     pass: process.env.EMAIL_PSD, \/\/ \n   }\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"写在文末"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"未来,我们还可以在 "},{"type":"text","marks":[{"type":"strong"}],"text":"Cnpmjs.org"},{"type":"text","text":" 上做很多定制化开发,比如"},{"type":"text","marks":[{"type":"strong"}],"text":"接入公司内部权限系统"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"WEB 页面重构"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"对接业务组件在线文档"},{"type":"text","text":"等等。如果你正好也需要搭建 "},{"type":"text","marks":[{"type":"strong"}],"text":"NPM"},{"type":"text","text":" 私有库,希望这篇文章对你有所帮助。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"头图:Unsplash"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:梓安"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文:https:\/\/mp.weixin.qq.com\/s\/NUYooqqTklsSs77VDRLwKA"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文:NPM 私库从搭建到数据迁移最后容灾备份的一些解决方案"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"来源:政采云前端团队 - 微信公众号 [ID:Zoo-Team]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"转载:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章