Skip to content

Commit

Permalink
fix: use Buffer.byteLength rather than new Buffer().length
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkpunkd committed Oct 18, 2019
1 parent ab00ffc commit 903cc23
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/peptalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,18 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
let last = split[split.length - 1]
let reres = re.exec(last)
let leftovers: string | null = null
// console.log('SBF >>>', split)
while (reres !== null) {
if (new Buffer(last, 'utf8').length - (reres.index + reres[0].length + (+reres[1])) < 0) {
if (Buffer.byteLength(last, 'utf8') - (reres.index + reres[0].length + (+reres[1])) < 0) {
leftovers = last
split = split.slice(0, -1)
break
}
reres = re.exec(last)
}
// console.log('SAF >>>', split)
if (this.leftovers) {
console.log(this.leftovers.slice(-40), ' @@@ ', split[0].slice(0, 40))
split[0] = this.leftovers + split[0]
this.leftovers = null
}
Expand All @@ -382,7 +385,9 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
return
}
this.leftovers = leftovers ? leftovers : this.leftovers
if (split.length === 0) return
m = split[0]
console.log('LEN', m.length)
let firstSpace = m.indexOf(' ')
if (firstSpace <= 0) return
let c = +m.slice(0, firstSpace)
Expand Down Expand Up @@ -472,7 +477,7 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
}

/** Escape a string using plaintalk representation. */
private esc = (s: string) => `{${new Buffer(s, 'utf8').length}}${s}`
private esc = (s: string) => `{${Buffer.byteLength(s, 'utf8')}}${s}`

/** Remove all plaintalk escaping from a string. */
private unesc = (s: string) => s.replace(/\{\d+\}/g, '')
Expand Down

0 comments on commit 903cc23

Please sign in to comment.