Skip to content

Commit

Permalink
Expose keepAliveMs (#30)
Browse files Browse the repository at this point in the history
* Expose keepAliveMs

* Rename _keepAliveMs to keepAliveMs (get rid of getter)

* Rename keepAliveMs to keepAlive
  • Loading branch information
HDegroote authored Apr 2, 2024
1 parent 7f6737b commit 8997b57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Populated after `open` is emitted.
Get the unique hash of this handshake.
Populated after `open` is emitted.

#### `s.keepAlive`

Get the interval (in milliseconds) at which keep-alive messages are sent (0 means none are sent).

#### `s.on('connect', onconnect)`

Emitted when the handshake is fully done.
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = class NoiseSecretStream extends Duplex {
this._timeout = null
this._timeoutMs = 0
this._keepAlive = null
this._keepAliveMs = 0
this.keepAlive = 0

if (opts.autoStart !== false) this.start(rawStream, opts)

Expand Down Expand Up @@ -93,7 +93,7 @@ module.exports = class NoiseSecretStream extends Duplex {
setKeepAlive (ms) {
if (!ms) ms = 0

this._keepAliveMs = ms
this.keepAlive = ms

if (!ms || this.rawStream === null) return

Expand Down Expand Up @@ -124,8 +124,8 @@ module.exports = class NoiseSecretStream extends Duplex {
if (opts.data) this._onrawdata(opts.data)
if (opts.ended) this._onrawend()

if (this._keepAliveMs > 0 && this._keepAlive === null) {
this.setKeepAlive(this._keepAliveMs)
if (this.keepAlive > 0 && this._keepAlive === null) {
this.setKeepAlive(this.keepAlive)
}

if (this._timeoutMs > 0 && this._timeout === null) {
Expand Down Expand Up @@ -320,7 +320,7 @@ module.exports = class NoiseSecretStream extends Duplex {
}

// If keep alive is selective, eat the empty buffers (ie assume the other side has it enabled also)
if (plain.byteLength === 0 && this._keepAliveMs !== 0) return
if (plain.byteLength === 0 && this.keepAlive !== 0) return

if (this.push(plain) === false) {
this.rawStream.pause()
Expand Down Expand Up @@ -472,7 +472,7 @@ module.exports = class NoiseSecretStream extends Duplex {
if (this._keepAlive === null) return
this._keepAlive.destroy()
this._keepAlive = null
this._keepAliveMs = 0
this.keepAlive = 0
}

_destroy (cb) {
Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ test('can timeout', function (t) {
})

test('keep alive', function (t) {
t.plan(1)
t.plan(2)

const a = new NoiseStream(true)
const b = new NoiseStream(false)
Expand All @@ -459,6 +459,7 @@ test('keep alive', function (t) {

a.setKeepAlive(500)
b.setKeepAlive(500)
t.is(a.keepAlive, 500)

a.resume()

Expand Down

0 comments on commit 8997b57

Please sign in to comment.