Skip to content

Commit

Permalink
fix: ref-napi, ffi-napi optional
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Mar 31, 2022
1 parent 4223c74 commit 4bd8e5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
6 changes: 4 additions & 2 deletions packages/amqp-coffee/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
"bytes": "^3.1.2",
"debug": "^4.3.4",
"fastq": "^1.13.0",
"ffi-napi": "^4.0.3",
"lodash": "^4.17.21",
"read-pkg": "^5.2.0",
"read-pkg": "^5.2.0"
},
"optionalDependencies": {
"ffi-napi": "^4.0.3",
"ref-napi": "^3.0.3"
},
"devDependencies": {
Expand Down
36 changes: 30 additions & 6 deletions packages/amqp-coffee/src/lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
ContentHeader,
ContentHeaderProperties,
} from '@microfleet/amqp-codec'
import { setSocketReadBuffer, setSocketWriteBuffer } from './utils/set-sock-opts'
import { ChannelManager } from './channel-manager'
import { Exchange, ExchangeOptions } from './exchange'
import { Queue, QueueOptions } from './queue'
Expand Down Expand Up @@ -104,13 +103,17 @@ export class Connection extends EventEmitter {
private sendHeartbeatTimer: NodeJS.Timer | null = null
private opening$P: Promise<void> | undefined

// for optional module loading
private readonly setSocketReadBuffer: (fd: number, size: number) => void | (() => void)
private readonly setSocketWriteBuffer: (fd: number, size: number) => void | (() => void)

// connection to work with
public connection!: tls.TLSSocket | net.Socket
public serverProperties: Record<string, any> | null = null
public activeHost!: string
public activePort!: number
public preparedHosts!: { host: string, port: number }[]
public hosti!: number
public hosti!: number

// ###
// host: localhost | [localhost, localhost] | [{host: localhost, port: 5672}, {host: localhost, port: 5673}]
Expand All @@ -136,6 +139,22 @@ export class Connection extends EventEmitter {
this.serializer = new Serializer(this.frameMax)
this.channelManager = new ChannelManager(this)

try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { setSocketReadBuffer, setSocketWriteBuffer } = require('./utils/set-sock-opts')
this.setSocketReadBuffer = setSocketReadBuffer
this.setSocketWriteBuffer = setSocketWriteBuffer
} catch (e: any) {
this.setSocketReadBuffer = () => {/* noop */}
this.setSocketWriteBuffer = () => {/* noop */}
process.emitWarning('could not load set-sock-opts', {
code: 'AMQP_SOCK_OPTS',
detail: e.message,
})
// noop
}


if (!this.connectionOptions.lazyConnect) {
this.connect()
}
Expand Down Expand Up @@ -362,11 +381,16 @@ export class Connection extends EventEmitter {
debug(1, () => `Connected#generic to ${this.connectionOptions.host}:${this.connectionOptions.port}`)

// @ts-expect-error -- it does exist
setSocketReadBuffer(this.connection._handle.fd, bytes('4mb'))
// @ts-expect-error -- it does exist
setSocketWriteBuffer(this.connection._handle.fd, bytes('4mb'))
if (this.connection._handle && typeof this.connection._handle.fd !== 'undefined') {
// @ts-expect-error -- it does exist
const fd = this.connection._handle.fd
this.setSocketReadBuffer(fd, bytes('4mb'))
this.setSocketWriteBuffer(fd, bytes('4mb'))
}

if (this._connectTimeout) clearTimeout(this._connectTimeout)
if (this._connectTimeout) {
clearTimeout(this._connectTimeout)
}

this._resetAllHeartbeatTimers()
this._setupParser(this._reestablishChannels)
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4bd8e5f

Please sign in to comment.