Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created March 13, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/2650b74c43637a5892d945cfc551d2d0 to your computer and use it in GitHub Desktop.
Save isaacs/2650b74c43637a5892d945cfc551d2d0 to your computer and use it in GitHub Desktop.
diff --git a/lib/dir.js b/lib/dir.js
index 44dadaa..70a62ee 100644
--- a/lib/dir.js
+++ b/lib/dir.js
@@ -41,18 +41,14 @@ class DirFetcher extends Fetcher {
}
[_tarballFromResolved] () {
- const stream = new Minipass()
- stream.resolved = this.resolved
- stream.integrity = this.integrity
-
// run the prepare script, get the list of files, and tar it up
// pipe to the stream, and proxy errors the chain.
- this[_prepareDir]()
+ return this[_prepareDir]()
.then(() => packlist({ path: this.resolved }))
- .then(files => tar.c(this[_tarcOpts](), files)
- .on('error', er => stream.emit('error', er)).pipe(stream))
- .catch(er => stream.emit('error', er))
- return stream
+ .then(files => Object.assign(tar.c(this[_tarcOpts](), files), {
+ resolved: this.resolved,
+ integrity: this.integrity,
+ }))
}
[_tarcOpts] () {
diff --git a/lib/fetcher.js b/lib/fetcher.js
index 4c5efdc..22701df 100644
--- a/lib/fetcher.js
+++ b/lib/fetcher.js
@@ -271,7 +271,8 @@ class FetcherBase {
}. Extracting by manifest.`)
}
return this.resolve().then(() => retry(tryAgain =>
- streamHandler(this[_istream](this[_tarballFromResolved]()))
+ this[_tarballFromResolved]()
+ .then(stream => streamHandler(this[_istream](stream)))
.catch(er => {
// Most likely data integrity. A cache ENOENT error is unlikely
// here, since we're definitely not reading from the cache, but it
diff --git a/lib/file.js b/lib/file.js
index d5c601a..1510b58 100644
--- a/lib/file.js
+++ b/lib/file.js
@@ -67,7 +67,10 @@ class FileFetcher extends Fetcher {
[_tarballFromResolved] () {
// create a read stream and return it
- return new fsm.ReadStream(this.resolved)
+ return Promise.resovle(Object.assign(new fsm.ReadStream(this.resolved), {
+ resolved: this.resolved,
+ integrity: this.integrity,
+ })
}
packument () {
diff --git a/lib/git.js b/lib/git.js
index 81f7ca2..82a803f 100644
--- a/lib/git.js
+++ b/lib/git.js
@@ -160,28 +160,17 @@ class GitFetcher extends Fetcher {
}
[_tarballFromResolved] () {
- const stream = new Minipass()
- stream.resolved = this.resolved
- stream.integrity = this.integrity
- stream.from = this.from
-
// check it out and then shell out to the DirFetcher tarball packer
- this[_clone](dir => this[_prepareDir](dir)
- .then(() => new Promise((res, rej) => {
- const df = new DirFetcher(`file:${dir}`, {
- ...this.opts,
- resolved: null,
- integrity: null,
- })
- const dirStream = df[_tarballFromResolved]()
- dirStream.on('error', rej)
- dirStream.on('end', res)
- dirStream.pipe(stream)
- }))).catch(
- /* istanbul ignore next: very unlikely and hard to test */
- er => stream.emit('error', er)
- )
- return stream
+ return this[_clone](dir => this[_prepareDir](dir)
+ .then(() => new DirFetcher(`file:${dir}`, {
+ ...this.opts,
+ resolved: null,
+ integrity: null,
+ })[_tarballFromResolved])
+ .then(stream => Object.assign(stream, {
+ integrity: this.integrity,
+ resolved: this.resolved,
+ }))
}
// clone a git repo into a temp folder (or fetch and unpack if possible)
diff --git a/lib/registry.js b/lib/registry.js
index b9df036..566e6bf 100644
--- a/lib/registry.js
+++ b/lib/registry.js
@@ -144,6 +144,7 @@ class RegistryFetcher extends Fetcher {
return new RemoteFetcher(this.resolved, {
...this.opts,
resolved: this.resolved,
+ integrity: this.integrity,
pkgid: `registry:${this.spec.name}@${this.resolved}`,
})[_tarballFromResolved]()
}
diff --git a/lib/remote.js b/lib/remote.js
index 81f14ef..53f07aa 100644
--- a/lib/remote.js
+++ b/lib/remote.js
@@ -17,7 +17,6 @@ class RemoteFetcher extends Fetcher {
}
[_tarballFromResolved] () {
- const stream = new Minipass()
const fetchOpts = {
...this.opts,
headers: this[_headers](),
@@ -25,19 +24,17 @@ class RemoteFetcher extends Fetcher {
integrity: this.integrity,
algorithms: [ this.pickIntegrityAlgorithm() ],
}
- fetch(this.resolved, fetchOpts).then(res => {
+ return fetch(this.resolved, fetchOpts).then(res => {
const hash = res.headers.get('x-local-cache-hash')
if (hash) {
this.integrity = decodeURIComponent(hash)
}
- res.body.on('error',
- /* istanbul ignore next - exceedingly rare and hard to simulate */
- er => stream.emit('error', er)
- ).pipe(stream)
- }).catch(er => stream.emit('error', er))
-
- return stream
+ return Object.assign(res.body, {
+ integrity: this.integrity,
+ resolved: this.resolved,
+ })
+ })
}
[_headers] () {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment