Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Implement some concurrency limits
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Apr 4, 2016
1 parent e9eb1f2 commit 36e9c8a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"string-template": "^1.0.0",
"strip-bom": "^2.0.0",
"thenify": "^3.1.0",
"throat": "^2.0.2",
"touch": "^1.0.0",
"typescript": "1.8.9",
"xtend": "^4.0.0",
Expand Down
38 changes: 24 additions & 14 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import popsicleStatus = require('popsicle-status')
import popsicleRetry = require('popsicle-retry')
import detectIndent = require('detect-indent')
import sortKeys = require('sort-keys')
import mdp = require('mkdirp')
import Mkdirp = require('mkdirp')
import uniq = require('array-uniq')
import lockfile = require('lockfile')
import rmrf = require('rimraf')
import Rimraf = require('rimraf')
import popsicleProxy = require('popsicle-proxy-agent')
import Throat = require('throat')
import promiseFinally from 'promise-finally'
import tch = require('touch')
import Touch = require('touch')
import { join, dirname } from 'path'
import { parse as parseUrl } from 'url'
import template = require('string-template')
Expand All @@ -28,18 +29,27 @@ import debug from './debug'

const pkg = require('../../package.json')
const registryURL = parseUrl(rc.registryURL)
const throat = Throat(Promise)

export type Stats = fs.Stats

export const touch: (path: string, options?: tch.Options) => Promise<void> = thenify<string, tch.Options, void>(tch)
export const stat: (path: string) => Promise<Stats> = thenify(fs.stat)
export const readFile: (path: string, encoding: string) => Promise<string> = thenify<string, string, string>(fs.readFile)
export const writeFile: (path: string, contents: string | Buffer) => Promise<void> = thenify<string, string | Buffer, void>(fs.writeFile)
export const mkdirp: (path: string) => Promise<string> = thenify<string, string>(mdp)
export const unlink: (path: string) => Promise<void> = thenify<string, void>(fs.unlink)
export const lock: (path: string, options?: lockfile.Options) => Promise<void> = thenify<string, lockfile.Options, void>(lockfile.lock)
export const unlock: (path: string) => Promise<void> = thenify<string, void>(lockfile.unlock)
export const rimraf: (path: string) => Promise<void> = thenify<string, void>(rmrf)
export type LockOp = (path: string, options?: lockfile.Options) => Promise<void>
export type TouchOp = (path: string, options?: Touch.Options) => Promise<void>
export type StatOp = (path: string) => Promise<Stats>
export type ReadFileOp = (path: string, encoding: string) => Promise<string>
export type WriteFileOp = (path: string, contents: string | Buffer) => Promise<void>
export type MkdirpOp = (path: string) => Promise<string>
export type PathOp = (path: string) => Promise<void>

export const touch: TouchOp = throat(10, thenify<string, Touch.Options, void>(Touch))
export const stat: StatOp = throat(10, thenify(fs.stat))
export const readFile: ReadFileOp = throat(10, thenify<string, string, string>(fs.readFile))
export const writeFile: WriteFileOp = thenify<string, string | Buffer, void>(fs.writeFile)
export const mkdirp: MkdirpOp = throat(10, thenify<string, string>(Mkdirp))
export const unlink: PathOp = throat(10, thenify<string, void>(fs.unlink))
export const lock: LockOp = throat(10, thenify<string, lockfile.Options, void>(lockfile.lock))
export const unlock: PathOp = throat(10, thenify<string, void>(lockfile.unlock))
export const rimraf: PathOp = throat(10, thenify<string, void>(Rimraf))

/**
* Verify a path exists and is a file.
Expand Down Expand Up @@ -89,7 +99,7 @@ export function parseConfig (config: ConfigJson, path: string): ConfigJson {
/**
* Read a file over HTTP, using a file cache and status check.
*/
export function readHttp (url: string): Promise<string> {
export const readHttp = throat(5, function readHttp (url: string): Promise<string> {
const { proxy, httpProxy, httpsProxy, noProxy, rejectUnauthorized, ca, key, cert, userAgent } = rc

return popsicle.get({
Expand Down Expand Up @@ -156,7 +166,7 @@ export function readHttp (url: string): Promise<string> {
})
// Return only the response body.
.then(response => response.body)
}
})

/**
* Read a file from anywhere (HTTP or local filesystem).
Expand Down
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"string-template": "github:typings/typed-string-template#4620eee24201636ca45e6dbc2505066703c770da",
"strip-bom": "github:typings/typed-strip-bom#d1d42e7f43dcd9fce1cfd988c40421a1d37fa47b",
"thenify": "github:typings/typed-thenify#edbd46753be5bb34b916787fcede5e9b1281a2e2",
"throat": "github:typed-typings/npm-throat#20e2c3c157d87a90062a8a2f4d99ab4dc475b1fd",
"touch": "github:typings/typed-touch#9b10d0473fb5161c379df57d63d3d59fb5b62fb0",
"typescript": "npm:typescript",
"xtend": "github:typings/typed-xtend#63cccadf3295b3c15561ee45617ac006edcca9e0",
Expand Down

0 comments on commit 36e9c8a

Please sign in to comment.