-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
npm-registry.js
272 lines (224 loc) · 8.81 KB
/
npm-registry.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* @flow */
import type Reporter from '../reporters/base-reporter.js';
import type RequestManager from '../util/request-manager.js';
import type {RegistryRequestOptions, CheckOutdatedReturn} from './base-registry.js';
import type Config from '../config.js';
import type {ConfigRegistries} from './index.js';
import {YARN_REGISTRY} from '../constants.js';
import * as fs from '../util/fs.js';
import NpmResolver from '../resolvers/registries/npm-resolver.js';
import envReplace from '../util/env-replace.js';
import Registry from './base-registry.js';
import {addSuffix} from '../util/misc';
import {getPosixPath, resolveWithHome} from '../util/path';
const userHome = require('../util/user-home-dir').default;
const path = require('path');
const url = require('url');
const ini = require('ini');
const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
const REGEX_REGISTRY_PREFIX = /^https?:/;
const REGEX_REGISTRY_SUFFIX = /registry\/?$/;
function getGlobalPrefix(): string {
if (process.env.PREFIX) {
return process.env.PREFIX;
} else if (process.platform === 'win32') {
// c:\node\node.exe --> prefix=c:\node\
return path.dirname(process.execPath);
} else {
// /usr/local/bin/node --> prefix=/usr/local
let prefix = path.dirname(path.dirname(process.execPath));
// destdir only is respected on Unix
if (process.env.DESTDIR) {
prefix = path.join(process.env.DESTDIR, prefix);
}
return prefix;
}
}
const PATH_CONFIG_OPTIONS = ['cache', 'cafile', 'prefix', 'userconfig'];
function isPathConfigOption(key: string): boolean {
return PATH_CONFIG_OPTIONS.indexOf(key) >= 0;
}
function normalizePath(val: mixed): ?string {
if (val === undefined) {
return undefined;
}
if (typeof val !== 'string') {
val = '' + (val: any);
}
return resolveWithHome(val);
}
export default class NpmRegistry extends Registry {
constructor(cwd: string, registries: ConfigRegistries, requestManager: RequestManager, reporter: Reporter) {
super(cwd, registries, requestManager, reporter);
this.folder = 'node_modules';
}
static filename = 'package.json';
static escapeName(name: string): string {
// scoped packages contain slashes and the npm registry expects them to be escaped
return name.replace('/', '%2f');
}
request(pathname: string, opts?: RegistryRequestOptions = {}, packageName: ?string): Promise<*> {
const registry = this.getRegistry(packageName || pathname);
const requestUrl = url.resolve(registry, pathname);
const alwaysAuth = this.getRegistryOrGlobalOption(registry, 'always-auth');
const headers = Object.assign(
{
Accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
},
opts.headers,
);
const packageIdent = packageName || pathname;
const isScoppedPackage = packageIdent.match(/^@|\/@/);
// this.token must be checked to account for publish requests on non-scopped packages
if (this.token || alwaysAuth || isScoppedPackage) {
const authorization = this.getAuth(packageName || pathname);
if (authorization) {
headers.authorization = authorization;
}
}
return this.requestManager.request({
url: requestUrl,
method: opts.method,
body: opts.body,
auth: opts.auth,
headers,
json: !opts.buffer,
buffer: opts.buffer,
process: opts.process,
gzip: true,
});
}
async checkOutdated(config: Config, name: string, range: string): CheckOutdatedReturn {
const req = await this.request(NpmRegistry.escapeName(name), {
headers: {Accept: 'application/json'},
});
if (!req) {
throw new Error('couldnt find ' + name);
}
const {repository, homepage} = req;
const url = homepage || (repository && repository.url) || '';
return {
latest: req['dist-tags'].latest,
wanted: (await NpmResolver.findVersionInRegistryResponse(config, range, req)).version,
url,
};
}
async getPossibleConfigLocations(filename: string, reporter: Reporter): Promise<Array<[boolean, string, string]>> {
const possibles = [
[false, path.join(this.cwd, filename)],
[true, this.config.userconfig || path.join(userHome, filename)],
[false, path.join(getGlobalPrefix(), filename)],
];
const foldersFromRootToCwd = getPosixPath(this.cwd).split('/');
while (foldersFromRootToCwd.length > 1) {
possibles.push([false, path.join(foldersFromRootToCwd.join(path.sep), filename)]);
foldersFromRootToCwd.pop();
}
const actuals = [];
for (const [isHome, loc] of possibles) {
reporter.verbose(reporter.lang('configPossibleFile', loc));
if (await fs.exists(loc)) {
reporter.verbose(reporter.lang('configFileFound', loc));
actuals.push([isHome, loc, await fs.readFile(loc)]);
}
}
return actuals;
}
static normalizeConfig(config: Object): Object {
config = Registry.normalizeConfig(config);
for (const key: string in config) {
config[key] = envReplace(config[key]);
if (isPathConfigOption(key)) {
config[key] = normalizePath(config[key]);
}
}
return config;
}
async loadConfig(): Promise<void> {
// docs: https://docs.npmjs.com/misc/config
this.mergeEnv('npm_config_');
for (const [, loc, file] of await this.getPossibleConfigLocations('.npmrc', this.reporter)) {
const config = NpmRegistry.normalizeConfig(ini.parse(file));
// normalize offline mirror path relative to the current npmrc
const offlineLoc = config['yarn-offline-mirror'];
// don't normalize if we already have a mirror path
if (!this.config['yarn-offline-mirror'] && offlineLoc) {
const mirrorLoc = (config['yarn-offline-mirror'] = path.resolve(path.dirname(loc), offlineLoc));
await fs.mkdirp(mirrorLoc);
}
this.config = Object.assign({}, config, this.config);
}
}
getScope(packageName: string): string {
return !packageName || packageName[0] !== '@' ? '' : packageName.split(/\/|%2f/)[0];
}
getRegistry(packageName: string): string {
// Try extracting registry from the url, then scoped registry, and default registry
if (packageName.match(/^https?:/)) {
const availableRegistries = this.getAvailableRegistries();
const registry = availableRegistries.find(registry => packageName.startsWith(registry));
if (registry) {
return addSuffix(registry, '/');
}
}
for (const scope of [this.getScope(packageName), '']) {
const registry =
this.getScopedOption(scope, 'registry') || this.registries.yarn.getScopedOption(scope, 'registry');
if (registry) {
return addSuffix(String(registry), '/');
}
}
return DEFAULT_REGISTRY;
}
getAuth(packageName: string): string {
if (this.token) {
return this.token;
}
const baseRegistry = this.getRegistry(packageName);
const registries = [baseRegistry];
// If sending a request to the Yarn registry, we must also send it the auth token for the npm registry
if (baseRegistry === YARN_REGISTRY) {
registries.push(DEFAULT_REGISTRY);
}
for (const registry of registries) {
// Check for bearer token.
const authToken = this.getRegistryOrGlobalOption(registry, '_authToken');
if (authToken) {
return `Bearer ${String(authToken)}`;
}
// Check for basic auth token.
const auth = this.getRegistryOrGlobalOption(registry, '_auth');
if (auth) {
return `Basic ${String(auth)}`;
}
// Check for basic username/password auth.
const username = this.getRegistryOrGlobalOption(registry, 'username');
const password = this.getRegistryOrGlobalOption(registry, '_password');
if (username && password) {
const pw = new Buffer(String(password), 'base64').toString();
return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64');
}
}
return '';
}
getScopedOption(scope: string, option: string): mixed {
return this.getOption(scope + (scope ? ':' : '') + option);
}
getRegistryOption(registry: string, option: string): mixed {
const pre = REGEX_REGISTRY_PREFIX;
const suf = REGEX_REGISTRY_SUFFIX;
// When registry is used config scope, the trailing '/' is required
const reg = addSuffix(registry, '/');
// 1st attempt, try to get option for the given registry URL
// 2nd attempt, remove the 'https?:' prefix of the registry URL
// 3nd attempt, remove the 'registry/?' suffix of the registry URL
return (
this.getScopedOption(reg, option) ||
(reg.match(pre) && this.getRegistryOption(reg.replace(pre, ''), option)) ||
(reg.match(suf) && this.getRegistryOption(reg.replace(suf, ''), option))
);
}
getRegistryOrGlobalOption(registry: string, option: string): mixed {
return this.getRegistryOption(registry, option) || this.getOption(option);
}
}