Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Apr 23, 2024
1 parent e12f373 commit b724d28
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* global Bare */
const { Duplex } = require('bare-stream')
const binding = require('./binding')
const constants = require('./lib/constants')
const errors = require('./lib/errors')

const DEFAULT_READ_BUFFER = 65536

const constants = exports.constants = require('./lib/constants')
const defaultReadBufferSize = 65536

const context = binding.initContext()

Expand All @@ -16,22 +16,23 @@ exports.Socket = class TLSSocket extends Duplex {
key = null,
eagerOpen = true,
allowHalfOpen = true,
readBufferSize = DEFAULT_READ_BUFFER
readBufferSize = defaultReadBufferSize
} = opts

super({ mapWritable, eagerOpen })

this._pendingOpen = null

this._state = 0
this._buffer = Buffer.alloc(readBufferSize)
this._reading = null

this._socket = socket
this._key = key
this._cert = cert
this._allowHalfOpen = allowHalfOpen

this._pendingRead = null
this._pendingOpen = null

this._buffer = Buffer.alloc(readBufferSize)

this._handle = binding.init(context, isServer, cert, key, this,
this._onread,
this._onwrite
Expand Down Expand Up @@ -59,7 +60,7 @@ exports.Socket = class TLSSocket extends Duplex {
}

_ondata (data) {
this._reading = data
this._pendingRead = data

if (this._state & constants.state.HANDSHAKE) {
const length = binding.read(this._handle, this._buffer)
Expand All @@ -85,15 +86,15 @@ exports.Socket = class TLSSocket extends Duplex {
}

_onread (data) {
let buffer = this._reading
let buffer = this._pendingRead
if (buffer === null) return 0

if (buffer.byteLength > data.byteLength) {
const rest = buffer.subarray(data.byteLength)
buffer = buffer.subarray(0, data.byteLength)
this._reading = rest
this._pendingRead = rest
} else {
this._reading = null
this._pendingRead = null
}

data.set(buffer)
Expand Down Expand Up @@ -147,6 +148,9 @@ exports.Socket = class TLSSocket extends Duplex {

exports.TLSSocket = exports.Socket // For Node.js compatibility

exports.constants = constants
exports.errors = errors

function mapWritable (data) {
return typeof data === 'string' ? Buffer.from(data) : data
}
Expand Down

0 comments on commit b724d28

Please sign in to comment.