feat: limit network concurrency

This commit is contained in:
zkochan
2017-02-02 22:08:19 +02:00
committed by Zoltan Kochan
parent 5f07768d2d
commit 400f6bed21
7 changed files with 18 additions and 7 deletions

View File

@@ -15,7 +15,7 @@ Install packages.
* `options.cwd` - *String* - the directory in which the installation will be performed. By default the `process.cwd()` value is used.
* `options.quiet` - *Boolean* - `false` by default. No output to the console.
* `options.metaCache` - *Map* - a cache for package meta info.
* `options.flatTree` - *Boolean* - `false` by default. Can be use only on Node.js >= 6.3.0. Emulates a flat dependency tree similar to the one created by npm v3 ([npm v3 Dependency Resolution](https://docs.npmjs.com/how-npm-works/npm3)).
* `options.networkConcurrency` - *Number* - `16` by default. Max amount of network requests to perform concurrently.
**Returns:** a Promise

View File

@@ -56,6 +56,7 @@
"npm-package-arg": "4.2.0",
"npm-registry-client": "7.4.5",
"os-homedir": "1.0.2",
"p-limit": "1.1.0",
"path-name": "1.0.0",
"pause-stream": "0.0.11",
"pnpm-default-reporter": "0.1.0",

View File

@@ -23,6 +23,7 @@ const defaults = () => (<StrictPnpmOptions>{
depth: 0,
engineStrict: false,
metaCache: new Map(),
networkConcurrency: 16,
})
export default (opts?: PnpmOptions): StrictPnpmOptions => {

View File

@@ -87,7 +87,7 @@ async function installInContext (installType: string, packagesToInstall: Depende
tag: opts.tag,
engineStrict: opts.engineStrict,
nodeVersion: opts.nodeVersion,
got: createGot(client),
got: createGot(client, {networkConcurrency: opts.networkConcurrency}),
fetchingFiles: Promise.resolve(),
baseNodeModules: nodeModulesPath,
metaCache: opts.metaCache,

View File

@@ -2,6 +2,7 @@ import {IncomingMessage} from 'http'
import pauseStream = require('pause-stream')
import getRegistryAuthInfo = require('registry-auth-token')
import memoize = require('lodash.memoize')
import pLimit = require('p-limit')
export type RequestParams = {
auth?: {
@@ -26,19 +27,20 @@ export type NpmRegistryClient = {
fetch: Function
}
export default (client: NpmRegistryClient): Got => {
export default (client: NpmRegistryClient, opts: {networkConcurrency: number}): Got => {
const limit = pLimit(opts.networkConcurrency)
async function getJSON (url: string) {
return new Promise((resolve, reject) => {
return limit(() => new Promise((resolve, reject) => {
client.get(url, createOptions(url), (err: Error, data: Object, raw: Object, res: HttpResponse) => {
if (err) return reject(err)
resolve(data)
})
})
}))
}
const getStream = function (url: string): Promise<IncomingMessage> {
return new Promise((resolve, reject) => {
return limit(() => new Promise((resolve, reject) => {
client.fetch(url, createOptions(url), (err: Error, res: IncomingMessage) => {
if (err) return reject(err)
const ps = pauseStream()
@@ -46,7 +48,7 @@ export default (client: NpmRegistryClient): Got => {
res.pipe(ps.pause())
resolve(ps)
})
})
}))
}
function createOptions (url: string): RequestParams {

View File

@@ -21,6 +21,7 @@ export type PnpmOptions = {
depth?: number,
engineStrict?: boolean,
nodeVersion?: string,
networkConcurrency?: number,
// proxy
proxy?: string,
@@ -59,6 +60,7 @@ export type StrictPnpmOptions = {
depth: number,
engineStrict: boolean,
nodeVersion: string,
networkConcurrency: number,
// proxy
proxy?: string,

5
typings/local.d.ts vendored
View File

@@ -227,3 +227,8 @@ declare module 'module' {
const anything: any;
export = anything;
}
declare module 'p-limit' {
const anything: any;
export = anything;
}