Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move from symbols to private methods #74

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ const VCHAR_REGEX = /^[\x21-\x7E]+$/

const getOptString = options => options?.length ? `?${options.join('?')}` : ''

const _onEnd = Symbol('_onEnd')
const _getOptions = Symbol('_getOptions')
const _emittedSize = Symbol('_emittedSize')
const _emittedIntegrity = Symbol('_emittedIntegrity')
const _emittedVerified = Symbol('_emittedVerified')

class IntegrityStream extends MiniPass {
#emittedIntegrity
#emittedSize
#emittedVerified

constructor (opts) {
super()
this.size = 0
this.opts = opts

// may be overridden later, but set now for class consistency
this[_getOptions]()
this.#getOptions()

// options used for calculating stream. can't be changed.
const algorithms = opts?.algorithms || DEFAULT_ALGORITHMS
Expand All @@ -38,7 +36,7 @@ class IntegrityStream extends MiniPass {
this.hashes = this.algorithms.map(crypto.createHash)
}

[_getOptions] () {
#getOptions () {
// For verification
this.sri = this.opts?.integrity ? parse(this.opts?.integrity, this.opts) : null
this.expectedSize = this.opts?.size
Expand All @@ -49,24 +47,24 @@ class IntegrityStream extends MiniPass {
}

on (ev, handler) {
if (ev === 'size' && this[_emittedSize]) {
return handler(this[_emittedSize])
if (ev === 'size' && this.#emittedSize) {
return handler(this.#emittedSize)
}

if (ev === 'integrity' && this[_emittedIntegrity]) {
return handler(this[_emittedIntegrity])
if (ev === 'integrity' && this.#emittedIntegrity) {
return handler(this.#emittedIntegrity)
}

if (ev === 'verified' && this[_emittedVerified]) {
return handler(this[_emittedVerified])
if (ev === 'verified' && this.#emittedVerified) {
return handler(this.#emittedVerified)
}

return super.on(ev, handler)
}

emit (ev, data) {
if (ev === 'end') {
this[_onEnd]()
this.#onEnd()
}
return super.emit(ev, data)
}
Expand All @@ -77,9 +75,9 @@ class IntegrityStream extends MiniPass {
return super.write(data)
}

[_onEnd] () {
#onEnd () {
if (!this.goodSri) {
this[_getOptions]()
this.#getOptions()
}
const newSri = parse(this.hashes.map((h, i) => {
return `${this.algorithms[i]}-${h.digest('base64')}${this.optString}`
Expand All @@ -104,12 +102,12 @@ class IntegrityStream extends MiniPass {
err.sri = this.sri
this.emit('error', err)
} else {
this[_emittedSize] = this.size
this.#emittedSize = this.size
this.emit('size', this.size)
this[_emittedIntegrity] = newSri
this.#emittedIntegrity = newSri
this.emit('integrity', newSri)
if (match) {
this[_emittedVerified] = match
this.#emittedVerified = match
this.emit('verified', match)
}
}
Expand Down Expand Up @@ -381,7 +379,7 @@ function checkData (data, sri, opts) {
const digest = crypto.createHash(algorithm).update(data).digest('base64')
const newSri = parse({ algorithm, digest })
const match = newSri.match(sri, opts)
opts = opts || Object.create(null)
opts = opts || {}
if (match || !(opts.error)) {
return match
} else if (typeof opts.size === 'number' && (data.length !== opts.size)) {
Expand Down