Skip to content

Commit

Permalink
perf: unpack and download the same stream
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jun 15, 2017
1 parent 1d0c2b3 commit 1554ce4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/install/fetchResolution.ts
Expand Up @@ -118,7 +118,8 @@ export async function fetchFromRemoteTarball (dir: string, dist: PackageDist, op
if (opts.offline) {
throw new PnpmError('NO_OFFLINE_TARBALL', `Could not find ${localTarballPath} in local registry mirror ${opts.storePath}`)
}
await opts.got.download(dist.tarball, localTarballPath, {
return await opts.got.download(dist.tarball, localTarballPath, {
unpackTo: dir,
registry: dist.registry,
integrity: dist.integrity,
onStart: () => logStatus({status: 'fetching', pkgId: opts.pkgId}),
Expand Down
21 changes: 11 additions & 10 deletions src/network/got.ts
Expand Up @@ -7,6 +7,7 @@ import mkdirp = require('mkdirp-promise')
import path = require('path')
import createWriteStreamAtomic = require('fs-write-stream-atomic')
import ssri = require('ssri')
import unpackStream = require('unpack-stream')

export type AuthInfo = {
alwaysAuth: boolean,
Expand All @@ -23,11 +24,12 @@ export type HttpResponse = {

export type Got = {
download(url: string, saveto: string, opts: {
unpackTo: string,
registry?: string,
onStart?: () => void,
onProgress?: (downloaded: number, totalSize: number) => void,
integrity?: string
}): Promise<void>,
}): Promise<{}>,
getJSON<T>(url: string): Promise<T>,
}

Expand Down Expand Up @@ -60,11 +62,12 @@ export default (
}

function download (url: string, saveto: string, opts: {
unpackTo: string,
registry?: string,
onStart?: () => void,
onProgress?: (downloaded: number, totalSize: number) => void,
integrity?: string
}): Promise<void> {
}): Promise<{}> {
return limit(async () => {
await mkdirp(path.dirname(saveto))

Expand All @@ -81,14 +84,12 @@ export default (
.pipe(writeStream)
.on('error', reject)

if (opts.integrity) {
try {
await ssri.checkStream(res, opts.integrity)
} catch (err) {
reject(err)
}
}
stream.on('finish', resolve)
Promise.all([
opts.integrity && ssri.checkStream(res, opts.integrity),
unpackStream.local(res, opts.unpackTo)
])
.then(vals => resolve(vals[1]))
.catch(reject)

function start (res: IncomingMessage) {
if (res.statusCode !== 200) {
Expand Down

0 comments on commit 1554ce4

Please sign in to comment.