Skip to content

Commit

Permalink
test: update tap, standard, standard-version, travis
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop support for Node.js v6.

We knew this was coming, and the Stream changes are breaking anyway.
May as well do this now.
  • Loading branch information
isaacs committed Sep 18, 2019
1 parent 34a7c74 commit 2e54956
Show file tree
Hide file tree
Showing 11 changed files with 4,904 additions and 3,417 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
sudo: false
node_js:
- "9"
- "12"
- "10"
- "8"
- "6"
65 changes: 38 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ const STRICT_SRI_REGEX = /^([^-]+)-([A-Za-z0-9+/=]{44,88})(\?[\x21-\x7E]*)*$/
const VCHAR_REGEX = /^[\x21-\x7E]+$/

const SsriOpts = figgyPudding({
algorithms: {default: ['sha512']},
error: {default: false},
algorithms: { default: ['sha512'] },
error: { default: false },
integrity: {},
options: {default: []},
pickAlgorithm: {default: () => getPrioritizedHash},
Promise: {default: () => Promise},
sep: {default: ' '},
single: {default: false},
options: { default: [] },
pickAlgorithm: { default: () => getPrioritizedHash },
Promise: { default: () => Promise },
sep: { default: ' ' },
single: { default: false },
size: {},
strict: {default: false}
strict: { default: false }
})

class IntegrityStream extends MiniPass {
Expand All @@ -41,19 +41,22 @@ class IntegrityStream extends MiniPass {
this.hashes = this.algorithms.map(crypto.createHash)
this.onEnd = this.onEnd.bind(this)
}

emit (ev, data) {
if (ev === 'end') this.onEnd()
return super.emit(ev, data)
}

write (data) {
this.size += data.length
this.hashes.forEach(h => h.update(data))
super.write(data)
}

onEnd () {
const optString = (this.opts.options && this.opts.options.length)
? `?${this.opts.options.join('?')}`
: ''
? `?${this.opts.options.join('?')}`
: ''
const newSri = parse(this.hashes.map((h, i) => {
return `${this.algorithms[i]}-${h.digest('base64')}${optString}`
}).join(' '), this.opts)
Expand Down Expand Up @@ -92,8 +95,8 @@ class Hash {
// https://w3c.github.io/webappsec-subresource-integrity/#integrity-metadata-description
const match = this.source.match(
strict
? STRICT_SRI_REGEX
: SRI_REGEX
? STRICT_SRI_REGEX
: SRI_REGEX
)
if (!match) { return }
if (strict && !SPEC_ALGORITHMS.some(a => a === match[1])) { return }
Expand All @@ -103,12 +106,15 @@ class Hash {
const rawOpts = match[3]
this.options = rawOpts ? rawOpts.slice(1).split('?') : []
}

hexDigest () {
return this.digest && Buffer.from(this.digest, 'base64').toString('hex')
}

toJSON () {
return this.toString()
}

toString (opts) {
opts = SsriOpts(opts)
if (opts.strict) {
Expand All @@ -132,8 +138,8 @@ class Hash {
}
}
const options = this.options && this.options.length
? `?${this.options.join('?')}`
: ''
? `?${this.options.join('?')}`
: ''
return `${this.algorithm}-${this.digest}${options}`
}
}
Expand All @@ -143,6 +149,7 @@ class Integrity {
toJSON () {
return this.toString()
}

toString (opts) {
opts = SsriOpts(opts)
let sep = opts.sep || ' '
Expand All @@ -156,16 +163,19 @@ class Integrity {
}).filter(x => x.length).join(sep)
}).filter(x => x.length).join(sep)
}

concat (integrity, opts) {
opts = SsriOpts(opts)
const other = typeof integrity === 'string'
? integrity
: stringify(integrity, opts)
? integrity
: stringify(integrity, opts)
return parse(`${this.toString(opts)} ${other}`, opts)
}

hexDigest () {
return parse(this, {single: true}).hexDigest()
return parse(this, { single: true }).hexDigest()
}

match (integrity, opts) {
opts = SsriOpts(opts)
const other = parse(integrity, opts)
Expand All @@ -180,6 +190,7 @@ class Integrity {
)
) || false
}

pickAlgorithm (opts) {
opts = SsriOpts(opts)
const pickAlgorithm = opts.pickAlgorithm
Expand Down Expand Up @@ -242,8 +253,8 @@ module.exports.fromHex = fromHex
function fromHex (hexDigest, algorithm, opts) {
opts = SsriOpts(opts)
const optString = opts.options && opts.options.length
? `?${opts.options.join('?')}`
: ''
? `?${opts.options.join('?')}`
: ''
return parse(
`${algorithm}-${
Buffer.from(hexDigest, 'hex').toString('base64')
Expand All @@ -256,13 +267,13 @@ function fromData (data, opts) {
opts = SsriOpts(opts)
const algorithms = opts.algorithms
const optString = opts.options && opts.options.length
? `?${opts.options.join('?')}`
: ''
? `?${opts.options.join('?')}`
: ''
return algorithms.reduce((acc, algo) => {
const digest = crypto.createHash(algo).update(data).digest('base64')
const hash = new Hash(
`${algo}-${digest}${optString}`,
opts
opts
)
if (hash.algorithm && hash.digest) {
const algo = hash.algorithm
Expand Down Expand Up @@ -306,7 +317,7 @@ function checkData (data, sri, opts) {
}
const algorithm = sri.pickAlgorithm(opts)
const digest = crypto.createHash(algorithm).update(data).digest('base64')
const newSri = parse({algorithm, digest})
const newSri = parse({ algorithm, digest })
const match = newSri.match(sri, opts)
if (match || !opts.error) {
return match
Expand Down Expand Up @@ -356,8 +367,8 @@ function createIntegrity (opts) {
opts = SsriOpts(opts)
const algorithms = opts.algorithms
const optString = opts.options.length
? `?${opts.options.join('?')}`
: ''
? `?${opts.options.join('?')}`
: ''

const hashes = algorithms.map(crypto.createHash)

Expand Down Expand Up @@ -400,6 +411,6 @@ const DEFAULT_PRIORITY = [

function getPrioritizedHash (algo1, algo2) {
return DEFAULT_PRIORITY.indexOf(algo1.toLowerCase()) >= DEFAULT_PRIORITY.indexOf(algo2.toLowerCase())
? algo1
: algo2
? algo1
: algo2
}
Loading

0 comments on commit 2e54956

Please sign in to comment.