Skip to content

Commit 8e80eca

Browse files
authored
fix: move from symbols to private methods (#74)
1 parent a316b12 commit 8e80eca

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

lib/index.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@ const VCHAR_REGEX = /^[\x21-\x7E]+$/
1515

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

18-
const _onEnd = Symbol('_onEnd')
19-
const _getOptions = Symbol('_getOptions')
20-
const _emittedSize = Symbol('_emittedSize')
21-
const _emittedIntegrity = Symbol('_emittedIntegrity')
22-
const _emittedVerified = Symbol('_emittedVerified')
23-
2418
class IntegrityStream extends MiniPass {
19+
#emittedIntegrity
20+
#emittedSize
21+
#emittedVerified
22+
2523
constructor (opts) {
2624
super()
2725
this.size = 0
2826
this.opts = opts
2927

3028
// may be overridden later, but set now for class consistency
31-
this[_getOptions]()
29+
this.#getOptions()
3230

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

41-
[_getOptions] () {
39+
#getOptions () {
4240
// For verification
4341
this.sri = this.opts?.integrity ? parse(this.opts?.integrity, this.opts) : null
4442
this.expectedSize = this.opts?.size
@@ -49,24 +47,24 @@ class IntegrityStream extends MiniPass {
4947
}
5048

5149
on (ev, handler) {
52-
if (ev === 'size' && this[_emittedSize]) {
53-
return handler(this[_emittedSize])
50+
if (ev === 'size' && this.#emittedSize) {
51+
return handler(this.#emittedSize)
5452
}
5553

56-
if (ev === 'integrity' && this[_emittedIntegrity]) {
57-
return handler(this[_emittedIntegrity])
54+
if (ev === 'integrity' && this.#emittedIntegrity) {
55+
return handler(this.#emittedIntegrity)
5856
}
5957

60-
if (ev === 'verified' && this[_emittedVerified]) {
61-
return handler(this[_emittedVerified])
58+
if (ev === 'verified' && this.#emittedVerified) {
59+
return handler(this.#emittedVerified)
6260
}
6361

6462
return super.on(ev, handler)
6563
}
6664

6765
emit (ev, data) {
6866
if (ev === 'end') {
69-
this[_onEnd]()
67+
this.#onEnd()
7068
}
7169
return super.emit(ev, data)
7270
}
@@ -77,9 +75,9 @@ class IntegrityStream extends MiniPass {
7775
return super.write(data)
7876
}
7977

80-
[_onEnd] () {
78+
#onEnd () {
8179
if (!this.goodSri) {
82-
this[_getOptions]()
80+
this.#getOptions()
8381
}
8482
const newSri = parse(this.hashes.map((h, i) => {
8583
return `${this.algorithms[i]}-${h.digest('base64')}${this.optString}`
@@ -104,12 +102,12 @@ class IntegrityStream extends MiniPass {
104102
err.sri = this.sri
105103
this.emit('error', err)
106104
} else {
107-
this[_emittedSize] = this.size
105+
this.#emittedSize = this.size
108106
this.emit('size', this.size)
109-
this[_emittedIntegrity] = newSri
107+
this.#emittedIntegrity = newSri
110108
this.emit('integrity', newSri)
111109
if (match) {
112-
this[_emittedVerified] = match
110+
this.#emittedVerified = match
113111
this.emit('verified', match)
114112
}
115113
}
@@ -423,7 +421,7 @@ function checkData (data, sri, opts) {
423421
const digest = crypto.createHash(algorithm).update(data).digest('base64')
424422
const newSri = parse({ algorithm, digest })
425423
const match = newSri.match(sri, opts)
426-
opts = opts || Object.create(null)
424+
opts = opts || {}
427425
if (match || !(opts.error)) {
428426
return match
429427
} else if (typeof opts.size === 'number' && (data.length !== opts.size)) {

0 commit comments

Comments
 (0)