Skip to content

Commit

Permalink
chore(streams): replace transform streams with minipass
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this replaces the Node.js stream with a Minipass
stream.  See http://npm.im/minipass for documentation.
  • Loading branch information
claudiahdz authored and isaacs committed Sep 18, 2019
1 parent a4337cd commit 363995e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const crypto = require('crypto')
const figgyPudding = require('figgy-pudding')
const Transform = require('stream').Transform
const MiniPass = require('minipass')

const SPEC_ALGORITHMS = ['sha256', 'sha384', 'sha512']

Expand All @@ -24,6 +24,19 @@ const SsriOpts = figgyPudding({
strict: {default: false}
})

class Transform extends MiniPass {
constructor (opts) {
super()
this.size = 0
this.transform = opts.transform
}
write (data) {
this.size += data.length
this.transform(data)
super.write(data)
}
}

class Hash {
get isHash () { return true }
constructor (hash, opts) {
Expand Down Expand Up @@ -301,14 +314,11 @@ function integrityStream (opts) {
new Set(opts.algorithms.concat(algorithm ? [algorithm] : []))
)
const hashes = algorithms.map(crypto.createHash)
let streamSize = 0
const stream = new Transform({
transform (chunk, enc, cb) {
streamSize += chunk.length
hashes.forEach(h => h.update(chunk, enc))
cb(null, chunk, enc)
}
}).on('end', () => {
transform (chunk) { hashes.forEach(h => h.update(chunk)) }
})

stream.on('end', () => {
const optString = (opts.options && opts.options.length)
? `?${opts.options.join('?')}`
: ''
Expand All @@ -317,6 +327,7 @@ function integrityStream (opts) {
}).join(' '), opts)
// Integrity verification mode
const match = goodSri && newSri.match(sri, opts)
const streamSize = stream.size
if (typeof opts.size === 'number' && streamSize !== opts.size) {
const err = new Error(`stream size mismatch when checking ${sri}.\n Wanted: ${opts.size}\n Found: ${streamSize}`)
err.code = 'EBADSIZE'
Expand Down
35 changes: 23 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"license": "ISC",
"dependencies": {
"figgy-pudding": "^3.5.1"
"figgy-pudding": "^3.5.1",
"minipass": "^2.5.1"
},
"devDependencies": {
"nyc": "^11.4.1",
Expand Down

0 comments on commit 363995e

Please sign in to comment.