最近因工作需要,想要在内网开发环境访问到npm包的内部文件。
我的第一反应当然是使用大名鼎鼎的unpkg啦,它可以方便地访问到任意版本的任意文件,甚至还支持dist-tag自动跳转。
| export NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist | |
| # 注意这是个临时方案 |
| #!/usr/bin/env bash | |
| # Extract the host where the server is running, and add the URL to the APIs | |
| [[ $CI_PROJECT_URL =~ ^https?://[^/]+ ]] && CI_PROJECT_URL="${BASH_REMATCH[0]}/api/v4/projects/" | |
| # Look which is the default branch | |
| #TARGET_BRANCH=`curl --silent "${CI_PROJECT_URL}${CI_PROJECT_ID}" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" | python3 -c "import sys, json; print(json.load(sys.stdin)['default_branch'])"`; | |
| TARGET_BRANCH="develop" | |
| # The description of our new MR, we want to remove the branch after the MR has | |
| # been closed |
| const app = require('express')(); | |
| const path = require('path') | |
| const fs = require('fs') | |
| const { createProxyMiddleware } = require('http-proxy-middleware'); | |
| const mcache = require('memory-cache') | |
| const getCacheFilePath = req => path.join('cache', Buffer.from(req.originalUrl || req.url).toString('base64') ) | |
| app.use('/unpkg', (req, res, next) => { | |
| req.cacheFilePath = getCacheFilePath(req) |
| const _ = require('lodash') | |
| const protectCache = new Set() | |
| const protect = (val) => { | |
| return new Proxy(val, { | |
| get: function (obj, prop) { | |
| // recursive | |
| const value = obj[prop] | |
| if ((_.isPlainObject(value) || Array.isArray(value)) && !protectCache.has(value)) { | |
| const result = protect(value) | |
| protectCache.add(result) |