Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@types/loader-utils": "^1.1.3",
"@types/minimist": "^1.2.0",
"@types/node": "8.10.10",
"@types/npm": "2.0.29",
"@types/request": "^2.47.1",
"@types/semver": "^5.5.0",
"@types/source-map": "0.5.2",
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/update/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@angular-devkit/core": "0.0.0",
"@angular-devkit/schematics": "0.0.0",
"npm": "6.4.1",
"npm-registry-client": "8.6.0",
"semver": "5.5.1",
"semver-intersect": "1.4.0",
Expand Down
236 changes: 47 additions & 189 deletions packages/schematics/update/update/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,120 +6,30 @@
* found in the LICENSE file at https://angular.io/license
*/
import { logging } from '@angular-devkit/core';
import { exec } from 'child_process';
import { readFileSync } from 'fs';
import { Observable, ReplaySubject, concat, of } from 'rxjs';
import {
catchError,
concatMap,
defaultIfEmpty,
filter,
first,
map,
shareReplay,
toArray,
} from 'rxjs/operators';
import * as npm from 'npm';
import { Observable, ReplaySubject } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import * as url from 'url';
import { NpmRepositoryPackageJson } from './npm-package-json';

const RegistryClient = require('npm-registry-client');

const npmPackageJsonCache = new Map<string, Observable<NpmRepositoryPackageJson>>();
const npmConfigOptionCache = new Map<string, Observable<string | undefined>>();


function _readNpmRc(): Observable<{ [key: string]: string }> {
return new Observable<{ [key: string]: string }>(subject => {
// TODO: have a way to read options without using fs directly.
const path = require('path');
const fs = require('fs');

let npmrc = '';
if (process.platform === 'win32') {
if (process.env.LOCALAPPDATA) {
npmrc = fs.readFileSync(path.join(process.env.LOCALAPPDATA, '.npmrc')).toString('utf-8');
}
} else {
if (process.env.HOME) {
npmrc = fs.readFileSync(path.join(process.env.HOME, '.npmrc')).toString('utf-8');
}
}

const allOptionsArr = npmrc.split(/\r?\n/).map(x => x.trim());
const allOptions: { [key: string]: string } = {};

allOptionsArr.forEach(x => {
const [key, ...value] = x.split('=');
allOptions[key] = value.join('=');
});

subject.next(allOptions);
const ensuredNpm = new Observable(subject => {
npm.load(() => {
subject.next();
subject.complete();
}).pipe(
catchError(() => of({})),
shareReplay(),
);
}

});
}).pipe(shareReplay());

function getOptionFromNpmRc(option: string): Observable<string | undefined> {
return _readNpmRc().pipe(
map(options => options[option]),
);
}

function getOptionFromNpmCli(option: string): Observable<string | undefined> {
return new Observable<string | undefined>(subject => {
exec(`npm get ${option}`, (error, data) => {
if (error) {
throw error;
} else {
data = data.trim();
if (!data || data === 'undefined' || data === 'null') {
subject.next();
} else {
subject.next(data);
}
}

subject.complete();
});
}).pipe(
catchError(() => of(undefined)),
shareReplay(),
);
}

function getNpmConfigOption(
option: string,
scope?: string,
tryWithoutScope?: boolean,
): Observable<string | undefined> {
if (scope && tryWithoutScope) {
return concat(
getNpmConfigOption(option, scope),
getNpmConfigOption(option),
).pipe(
filter(result => !!result),
defaultIfEmpty(),
first(),
);
function getNpmConfigOption(option: string, scope?: string): string {
if (scope) {
return npm.config.get(`${scope}:${option}`) || getNpmConfigOption(option);
}

const fullOption = `${scope ? scope + ':' : ''}${option}`;

let value = npmConfigOptionCache.get(fullOption);
if (value) {
return value;
}

value = option.startsWith('_')
? getOptionFromNpmRc(fullOption)
: getOptionFromNpmCli(fullOption);

npmConfigOptionCache.set(fullOption, value);

return value;
return npm.config.get(option);
}

function getNpmClientSslOptions(strictSsl?: string, cafile?: string) {
Expand All @@ -143,7 +53,7 @@ function getNpmClientSslOptions(strictSsl?: string, cafile?: string) {
* @param {string} packageName The package name to fetch.
* @param {string} registryUrl The NPM Registry URL to use.
* @param {LoggerApi} logger A logger instance to log debug information.
* @returns An observable that will put the pacakge.json content.
* @returns An observable that will put the package.json content.
* @private
*/
export function getNpmPackageJson(
Expand All @@ -153,114 +63,62 @@ export function getNpmPackageJson(
): Observable<Partial<NpmRepositoryPackageJson>> {
const scope = packageName.startsWith('@') ? packageName.split('/')[0] : undefined;

return (
registryUrl ? of(registryUrl) : getNpmConfigOption('registry', scope, true)
).pipe(
map(partialUrl => {
if (!partialUrl) {
partialUrl = 'https://registry.npmjs.org/';
}
return ensuredNpm.pipe(
map(() => {
const partialUrl = registryUrl
|| getNpmConfigOption('registry', scope)
|| 'https://registry.npmjs.org/';

const partial = url.parse(partialUrl);
let fullUrl = new url.URL(`http://${partial.host}/${packageName.replace(/\//g, '%2F')}`);
try {
const registry = new url.URL(partialUrl);
registry.pathname = (registry.pathname || '')
.replace(/\/?$/, '/' + packageName.replace(/\//g, '%2F'));
.replace(/\/?$/, '/' + packageName.replace(/\//g, '%2F'));
fullUrl = new url.URL(url.format(registry));
} catch {}
} catch { }

logger.debug(
`Getting package.json from '${packageName}' (url: ${JSON.stringify(fullUrl)})...`,
);

return fullUrl;
}),
concatMap(fullUrl => {
switchMap(fullUrl => {
let maybeRequest = npmPackageJsonCache.get(fullUrl.toString());

if (maybeRequest) {
return maybeRequest;
}

const registryKey = `//${fullUrl.host}/`;

return concat(
getNpmConfigOption('proxy'),
getNpmConfigOption('https-proxy'),
getNpmConfigOption('strict-ssl'),
getNpmConfigOption('cafile'),
getNpmConfigOption('_auth'),
getNpmConfigOption('_authToken', registryKey),
getNpmConfigOption('username', registryKey, true),
getNpmConfigOption('password', registryKey, true),
getNpmConfigOption('alwaysAuth', registryKey, true),
).pipe(
toArray(),
concatMap(options => {
const [
http,
https,
strictSsl,
cafile,
token,
authToken,
username,
password,
alwaysAuth,
] = options;

const subject = new ReplaySubject<NpmRepositoryPackageJson>(1);

const sslOptions = getNpmClientSslOptions(strictSsl, cafile);

const auth: {
token?: string,
alwaysAuth?: boolean;
username?: string;
password?: string
} = {};

if (alwaysAuth !== undefined) {
auth.alwaysAuth = alwaysAuth === 'false' ? false : !!alwaysAuth;
const subject = new ReplaySubject<NpmRepositoryPackageJson>(1);
const auth = npm.config.getCredentialsByURI(fullUrl.toString());
const client = new RegistryClient({
proxy: {
http: getNpmConfigOption('proxy'),
https: getNpmConfigOption('https-proxy'),
},
ssl: getNpmClientSslOptions(getNpmConfigOption('strict-ssl'), getNpmConfigOption('cafile')),
});

client.log.level = 'silent';

client.get(
fullUrl.toString(),
{ auth, timeout: 30000 },
(error: object, data: NpmRepositoryPackageJson) => {
if (error) {
subject.error(error);
}

if (authToken) {
auth.token = authToken;
} else if (token) {
auth.token = token;
} else if (username) {
auth.username = username;
auth.password = password;
}

const client = new RegistryClient({
proxy: { http, https },
ssl: sslOptions,
});
client.log.level = 'silent';
const params = {
timeout: 30000,
auth,
};

client.get(
fullUrl.toString(),
params,
(error: object, data: NpmRepositoryPackageJson) => {
if (error) {
subject.error(error);
}

subject.next(data);
subject.complete();
});
subject.next(data);
subject.complete();
});

maybeRequest = subject.asObservable();
npmPackageJsonCache.set(fullUrl.toString(), maybeRequest);
maybeRequest = subject.asObservable();
npmPackageJsonCache.set(fullUrl.toString(), maybeRequest);

return maybeRequest;
}),
);
return maybeRequest;
}),
);

}
Loading