From 4bd8e5f759b6637a52aaf39fd4cd39ce1ccd843f Mon Sep 17 00:00:00 2001 From: AVVS Date: Thu, 31 Mar 2022 10:25:18 -0700 Subject: [PATCH] fix: ref-napi, ffi-napi optional --- packages/amqp-coffee/package.json | 6 ++-- packages/amqp-coffee/src/lib/connection.ts | 36 ++++++++++++++++++---- pnpm-lock.yaml | 10 +++++- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/packages/amqp-coffee/package.json b/packages/amqp-coffee/package.json index 26da2b8..458d357 100644 --- a/packages/amqp-coffee/package.json +++ b/packages/amqp-coffee/package.json @@ -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": { diff --git a/packages/amqp-coffee/src/lib/connection.ts b/packages/amqp-coffee/src/lib/connection.ts index a93d46b..d17b1ed 100644 --- a/packages/amqp-coffee/src/lib/connection.ts +++ b/packages/amqp-coffee/src/lib/connection.ts @@ -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' @@ -104,13 +103,17 @@ export class Connection extends EventEmitter { private sendHeartbeatTimer: NodeJS.Timer | null = null private opening$P: Promise | 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 | 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}] @@ -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() } @@ -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) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e460810..1f3690d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -107,9 +107,10 @@ importers: 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 + optionalDependencies: + ffi-napi: 4.0.3 ref-napi: 3.0.3 devDependencies: '@makeomatic/deploy': 12.6.3 @@ -4933,6 +4934,7 @@ packages: transitivePeerDependencies: - supports-color dev: false + optional: true /figures/2.0.0: resolution: {integrity: sha1-OrGi0qYsi/tDGgyUy3l6L84nyWI=} @@ -5205,12 +5207,14 @@ packages: /get-symbol-from-current-process-h/1.0.2: resolution: {integrity: sha512-syloC6fsCt62ELLrr1VKBM1ggOpMdetX9hTrdW77UQdcApPHLmf7CI7OKcN1c9kYuNxKcDe4iJ4FY9sX3aw2xw==} dev: false + optional: true /get-uv-event-loop-napi-h/1.0.6: resolution: {integrity: sha512-t5c9VNR84nRoF+eLiz6wFrEp1SE2Acg0wS+Ysa2zF0eROes+LzOfuTaVHxGy8AbS8rq7FHEJzjnCZo1BupwdJg==} dependencies: get-symbol-from-current-process-h: 1.0.2 dev: false + optional: true /getpass/0.1.7: resolution: {integrity: sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=} @@ -7553,6 +7557,7 @@ packages: /node-addon-api/3.2.1: resolution: {integrity: sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==} dev: false + optional: true /node-emoji/1.11.0: resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==} @@ -7580,6 +7585,7 @@ packages: resolution: {integrity: sha512-iWjXZvmboq0ja1pUGULQBexmxq8CV4xBhX7VDOTbL7ZR4FOowwY/VOtRxBN/yKxmdGoIp4j5ysNT4u3S2pDQ3Q==} hasBin: true dev: false + optional: true /node-int64/0.4.0: resolution: {integrity: sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs=} @@ -8631,12 +8637,14 @@ packages: transitivePeerDependencies: - supports-color dev: false + optional: true /ref-struct-di/1.1.1: resolution: {integrity: sha512-2Xyn/0Qgz89VT+++WP0sTosdm9oeowLP23wRJYhG4BFdMUrLj3jhwHZNEytYNYgtPKLNTP3KJX4HEgBvM1/Y2g==} dependencies: debug: 3.2.7 dev: false + optional: true /regenerator-runtime/0.13.9: resolution: {integrity: sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==}