This repository was archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathproxy_to_npm.js
84 lines (75 loc) · 2.11 KB
/
proxy_to_npm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
var debug = require('debug')('cnpmjs.org:middleware:proxy_to_npm');
var config = require('../config');
module.exports = function (options) {
var redirectUrl = config.sourceNpmRegistry;
var proxyUrls = [
// /:pkg, dont contains scoped package
// /:pkg/:versionOrTag
/^\/[\w\-\.]+(?:\/[\w\-\.]+)?$/,
// /-/package/:pkg/dist-tags
/^\/\-\/package\/[\w\-\.]+\/dist-tags/,
];
var scopedUrls = [
// scoped package
/^\/(@[\w\-\.]+)\/[\w\-\.]+(?:\/[\w\-\.]+)?$/,
/^\/\-\/package\/(@[\w\-\.]+)\/[\w\-\.]+\/dist\-tags/,
];
if (options && options.isWeb) {
redirectUrl = config.sourceNpmWeb || redirectUrl.replace('//registry.', '//');
proxyUrls = [
// /package/:pkg
/^\/package\/[\w\-\.]+/,
];
scopedUrls = [
// scoped package
/^\/package\/(@[\w\-\.]+)\/[\w\-\.]+/,
];
}
return function* proxyToNpm(next) {
if (config.syncModel !== 'none') {
return yield next;
}
// syncModel === none
// only proxy read requests
if (this.method !== 'GET' && this.method !== 'HEAD') {
return yield next;
}
var pathname = decodeURIComponent(this.path);
var isScoped = false;
var isPublichScoped = false;
// check scoped name
if (config.scopes && config.scopes.length > 0) {
for (var i = 0; i < scopedUrls.length; i++) {
const m = scopedUrls[i].exec(pathname);
if (m) {
isScoped = true;
if (config.scopes.indexOf(m[1]) !== -1) {
// internal scoped
isPublichScoped = false;
} else {
isPublichScoped = true;
}
break;
}
}
}
var isPublich = false;
if (!isScoped) {
for (var i = 0; i < proxyUrls.length; i++) {
isPublich = proxyUrls[i].test(pathname);
if (isPublich) {
break;
}
}
}
if (isPublich || isPublichScoped) {
var url = redirectUrl + this.url;
debug('proxy isPublich: %s, isPublichScoped: %s, package to %s',
isPublich, isPublichScoped, url);
this.redirect(url);
return;
}
yield next;
};
};