Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module refactor 2 #58

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"root": true,
"extends": ["standard", "plugin:n/recommended"],
"extends": ["standard", "plugin:n/recommended", "plugin:@typescript-eslint/stylistic"],
"rules": {
"curly": ["error", "all"],
"brace-style": ["error", "1tbs", { "allowSingleLine": false }],
Expand All @@ -20,6 +20,8 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 2020
}
"ecmaVersion": 2020,
"sourceType": "module"
},
"ignorePatterns": ["dist/", "*.d.ts"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules/
local_tests/
test/config/TestConfig.js
dist/

.npm
.eslintcache
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ addons:
language: node_js

node_js:
- 16
- 18
- 20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mi miatt kell a 18?


git:
depth: false
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ A RabbitMQ wrapper for node
yarn add @techteamer/mq
```

## Build

```
yarn run build
```

## Tests

To run tests you need to rename `test/config/TestConfig.js.config` to `TestConfig.js` and provide valid configurations.
Expand All @@ -22,3 +28,9 @@ Then run:
```
yarn test
```

## Publish

**Before publish always run the build process!** This will create the `dist` folder, which will be needed in the published package.


11 changes: 11 additions & 0 deletions fixup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >dist/mjs/package.json <<!EOF
{
"type": "module"
}
!EOF
61 changes: 30 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
const QueueClient = require('./src/QueueClient')
const QueueConfig = require('./src/QueueConfig')
const QueueConnection = require('./src/QueueConnection')
const ConnectionPool = require('./src/ConnectionPool')
const QueueMessage = require('./src/QueueMessage')
const ProtoQueueMessage = require('./src/ProtoQueueMessage')
const QueueServer = require('./src/QueueServer')
const QueueManager = require('./src/QueueManager')
const RPCClient = require('./src/RPCClient')
const RPCError = require('./src/RPCError')
const RPCServer = require('./src/RPCServer')
const Publisher = require('./src/Publisher')
const Subscriber = require('./src/Subscriber')
const GatheringClient = require('./src/GatheringClient')
const GatheringServer = require('./src/GatheringServer')

module.exports.QueueClient = QueueClient
module.exports.QueueConfig = QueueConfig
module.exports.QueueConnection = QueueConnection
module.exports.ConnectionPool = ConnectionPool
module.exports.QueueMessage = QueueMessage
module.exports.ProtoQueueMessage = ProtoQueueMessage
module.exports.QueueServer = QueueServer
module.exports.QueueManager = QueueManager
module.exports.RPCClient = RPCClient
module.exports.RPCError = RPCError
module.exports.RPCServer = RPCServer
module.exports.Publisher = Publisher
module.exports.Subscriber = Subscriber
module.exports.GatheringClient = GatheringClient
module.exports.GatheringServer = GatheringServer
import QueueClient from './src/QueueClient.js'
import QueueConfig from './src/QueueConfig.js'
import QueueConnection from './src/QueueConnection.js'
import ConnectionPool from './src/ConnectionPool.js'
import QueueMessage from './src/QueueMessage.js'
import ProtoQueueMessage from './src/ProtoQueueMessage.js'
import QueueServer from './src/QueueServer.js'
import QueueManager from './src/QueueManager.js'
import RPCClient from './src/RPCClient.js'
import RPCError from './src/RPCError.js'
import RPCServer from './src/RPCServer.js'
import Publisher from './src/Publisher.js'
import Subscriber from './src/Subscriber.js'
import GatheringClient from './src/GatheringClient.js'
import GatheringServer from './src/GatheringServer.js'
export { QueueClient }
export { QueueConfig }
export { QueueConnection }
export { ConnectionPool }
export { QueueMessage }
export { ProtoQueueMessage }
export { QueueServer }
export { QueueManager }
export { RPCClient }
export { RPCError }
export { RPCServer }
export { Publisher }
export { Subscriber }
export { GatheringClient }
export { GatheringServer }
23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "@techteamer/mq",
"version": "6.3.2",
"version": "6.3.3-alpha",
"description": "A RabbitMQ wrapper for node",
"main": "index.js",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},
"author": "TechTeamer",
"contributors": [
"Gábor Tóth <gabor@techteamer.com>",
Expand All @@ -17,27 +24,31 @@
"node": ">=16"
},
"scripts": {
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./fixup.sh",
"lint": "eslint .",
"unit": "mocha --exit",
"test": "eslint . && nyc mocha --temp-directory=./test/coverage/ --exit && nyc report --reporter=text-lcov --report-dir=./test/coverage/ --reporter=lcovonly | coveralls"
"test": "eslint . && c8 --temp-directory=./test/coverage/tmp mocha --exit && c8 report --reporter=text-lcov --report-dir=./test/coverage/ --reporter=lcovonly | coveralls"
},
"type": "module",
"dependencies": {
"amqplib": "^0.10.3",
"uuid": "^9.0.0"
},
"devDependencies": {
"c8": "^9.1.0",
"chai": "^4.3.7",
"coveralls": "^3.1.1",
"eslint": "^8.38.0",
"eslint": "^8.57.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-standard": "^5.0.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"protobufjs": "^7.2.3",
"seed-random": "^2.2.0"
"seed-random": "^2.2.0",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
},
"peerDependencies": {
"protobufjs": "^7.2.3"
Expand Down
36 changes: 13 additions & 23 deletions src/ConnectionPool.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const QueueManager = require('./QueueManager')
const QueueConfig = require('./QueueConfig')

import QueueManager from './QueueManager.js'
import QueueConfig from './QueueConfig.js'
/**
* @class ConnectionPool
* */
class ConnectionPool {
/**
* @param {Object} poolConfig
* @param {{}} [poolConfig]
* @param {String} [poolConfig.defaultConnectionName]
*/
* @param {Object} poolConfig
* @param {{}} [poolConfig]
* @param {String} [poolConfig.defaultConnectionName]
*/
constructor (poolConfig) {
const { defaultConnectionName } = poolConfig || {}

this._logger = null
/**
* @type {Map<string, QueueManager>}
*/
* @type {Map<string, QueueManager>}
*/
this.connections = new Map()
this.defaultConnection = null
this.defaultConnectionName = defaultConnectionName || 'default'
Expand All @@ -26,7 +24,6 @@ class ConnectionPool {
const defaultConnectionName = this.defaultConnectionName
let defaultConnectionConfig = null
let restConnections = null

if (QueueConfig.isValidConfig(connectionConfigs)) {
// single connection config (backwards compatible)
defaultConnectionConfig = connectionConfigs
Expand All @@ -44,13 +41,11 @@ class ConnectionPool {
throw new Error('Invalid default connections config')
}
}

if (defaultConnectionConfig) {
const connection = this.createConnection(defaultConnectionConfig)
this.registerConnection(defaultConnectionName, connection)
this.setDefaultConnection(connection)
}

if (restConnections) {
Object.keys(restConnections).forEach((connectionName) => {
const connectionConfig = restConnections[connectionName]
Expand All @@ -70,11 +65,9 @@ class ConnectionPool {

createConnection (connectionConfig) {
const connection = new QueueManager(connectionConfig)

if (this._logger) {
connection.setLogger(this._logger)
}

return connection
}

Expand All @@ -95,26 +88,23 @@ class ConnectionPool {
}

/**
* @return {Promise}
*/
* @return {Promise}
*/
async connect () {
const connections = [...this.connections.values()]

for (const connection of connections) {
await connection.connect()
}
}

/**
* @return {Promise}
*/
* @return {Promise}
*/
async reconnect () {
const connections = [...this.connections.values()]

for (const connection of connections) {
await connection.reconnect()
}
}
}

module.exports = ConnectionPool
export default ConnectionPool
Loading