Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
extract-stream: use subclass instead of one-off transformer (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs authored and zkat committed May 22, 2018
1 parent 7f982cd commit f2a2358
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
45 changes: 28 additions & 17 deletions lib/extract-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,41 @@ const tar = require('tar')
module.exports = extractStream
module.exports._computeMode = computeMode

class Transformer extends Minipass {
constructor (spec, opts) {
super()
this.spec = spec
this.opts = opts
this.str = ''
}
write (data) {
this.str += data
return true
}
end () {
const replaced = this.str.replace(
/}\s*$/,
`\n,"_resolved": ${
JSON.stringify(this.opts.resolved || '')
}\n,"_integrity": ${
JSON.stringify(this.opts.integrity || '')
}\n,"_from": ${
JSON.stringify(this.spec.toString())
}\n}`
)
super.write(replaced)
return super.end()
}
}

function computeMode (fileMode, optMode, umask) {
return (fileMode | optMode) & ~(umask || 0)
}

function pkgJsonTransform (spec, opts) {
return entry => {
if (entry.path === 'package.json') {
const transformed = new Minipass()
let str = ''
entry.once('end', () => {
const replaced = str.replace(
/}\s*$/,
`\n,"_resolved": ${
JSON.stringify(opts.resolved || '')
}\n,"_integrity": ${
JSON.stringify(opts.integrity || '')
}\n,"_from": ${
JSON.stringify(spec.toString())
}\n}`
)
transformed.write(replaced, () => transformed.end())
})
entry.on('error', e => transformed.emit('error'))
entry.on('data', d => { str += d })
const transformed = new Transformer(spec, opts)
return transformed
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/finalize-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ function jsonFromStream (filename, dataStream) {
entry.resume()
} else {
let data = ''
entry.on('data', d => { data += d })
entry.on('error', cb)
finished(entry).then(() => {
try {
Expand All @@ -219,6 +218,7 @@ function jsonFromStream (filename, dataStream) {
}, err => {
cb(err)
})
entry.on('data', d => { data += d })
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/finalize-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ function makeTarball (files) {
})
pack.finalize()
return BB.fromNode(cb => {
pack.on('data', function (d) { tarData += d })
pack.on('error', cb)
pack.on('end', function () { cb(null, tarData) })
pack.on('data', function (d) { tarData += d })
})
}
2 changes: 1 addition & 1 deletion test/pack-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ test('packs a directory into a valid tarball', t => {
const extractor = tar.t()
extractor.on('entry', (entry) => {
let data = ''
entry.on('data', d => { data += d })
entry.on('end', () => {
entries[entry.path] = data
})
entry.on('data', d => { data += d })
})
const pack = packDir(manifest, CACHE, CACHE, target)
return BB.join(pipe(target, extractor), pack, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/registry.manifest.shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ test('tarball setup', t => {
pack.entry({ name: 'package/npm-shrinkwrap.json' }, JSON.stringify(SHRINKWRAP))
pack.entry({ name: 'package/package.json' }, JSON.stringify(PKG))
pack.finalize()
pack.on('data', function (d) { TARBALL += d })
pack.on('error', function (e) { throw e })
pack.on('end', function () { t.end() })
pack.on('data', function (d) { TARBALL += d })
})

test('fetches shrinkwrap data if missing + required', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function mockRepo (opts) {
}).then(() => {
return git._exec(['add', '.'], {cwd})
}).then(() => {
return git._exec(['commit', '-m', '"initial commit"'], {cwd})
return git._exec(['commit', '-m', 'initial commit', '--no-gpg-sign'], {cwd})
}).then(() => {
return daemon(opts)
})
Expand Down

0 comments on commit f2a2358

Please sign in to comment.