Comparing version 8.1.0 to 8.2.0
Changelog | ||
========= | ||
# 8.2.0 | ||
- introduce `SmqpError(message, code)` inherited from Error, it is thrown when package specific errors occur. It is also exported so that instance can be checked | ||
- no more general errors, either it is a `TypeError` or `SmqpError` | ||
- fix inconsistent cancel consumer implementations in queue, add requeue argument when applicable | ||
# 8.1.0 | ||
@@ -5,0 +11,0 @@ |
@@ -11,2 +11,3 @@ "use strict"; | ||
var _shared = require("./shared.js"); | ||
var _Errors = require("./Errors.js"); | ||
const kEntities = Symbol.for('entities'); | ||
@@ -48,3 +49,3 @@ const kEventHandler = Symbol.for('eventHandler'); | ||
}) { | ||
if (!exchangeName || !pattern || typeof onMessage !== 'function') throw new Error('exchange name, pattern, and message callback are required'); | ||
if (!exchangeName || !pattern || typeof onMessage !== 'function') throw new TypeError('exchange name, pattern, and message callback are required'); | ||
if (options && options.consumerTag) this.validateConsumerTag(options.consumerTag); | ||
@@ -63,3 +64,3 @@ const exchange = this.assertExchange(exchangeName); | ||
Broker.prototype.subscribeOnce = function subscribeOnce(exchangeName, pattern, onMessage, options = {}) { | ||
if (typeof onMessage !== 'function') throw new Error('message callback is required'); | ||
if (typeof onMessage !== 'function') throw new TypeError('message callback is required'); | ||
if (options && options.consumerTag) this.validateConsumerTag(options.consumerTag); | ||
@@ -91,3 +92,3 @@ const exchange = this.assertExchange(exchangeName); | ||
if (exchange) { | ||
if (type && exchange.type !== type) throw new Error('Type doesn\'t match'); | ||
if (type && exchange.type !== type) throw new _Errors.SmqpError('Type doesn\'t match', _Errors.ERR_EXCHANGE_TYPE_MISMATCH); | ||
return exchange; | ||
@@ -114,3 +115,3 @@ } | ||
const queue = this.getQueue(queueName); | ||
if (!queue) throw new Error(`Queue with name <${queueName}> was not found`); | ||
if (!queue) throw new _Errors.SmqpError(`Queue with name <${queueName}> was not found`, _Errors.ERR_QUEUE_NOT_FOUND); | ||
if (options) this.validateConsumerTag(options.consumerTag); | ||
@@ -255,3 +256,3 @@ return queue.consume(onMessage, options, this.owner); | ||
const queue = this.getQueue(queueName); | ||
if (!queue) throw new Error(`Queue named ${queueName} doesn't exists`); | ||
if (!queue) throw new _Errors.SmqpError(`Queue with name <${queueName}> was not found`, _Errors.ERR_QUEUE_NOT_FOUND); | ||
return queue.queueMessage(null, content, options); | ||
@@ -280,3 +281,3 @@ }; | ||
Broker.prototype.createQueue = function createQueue(queueName, options) { | ||
if (queueName && typeof queueName !== 'string') throw new TypeError('queue name must be a string');else if (!queueName) queueName = `smq.qname-${(0, _shared.generateId)()}`;else if (this.getQueue(queueName)) throw new Error(`Queue named ${queueName} already exists`); | ||
if (queueName && typeof queueName !== 'string') throw new TypeError('queue name must be a string');else if (!queueName) queueName = `smq.qname-${(0, _shared.generateId)()}`;else if (this.getQueue(queueName)) throw new _Errors.SmqpError(`Queue named ${queueName} already exists`, _Errors.ERR_QUEUE_NAME_CONFLICT); | ||
const queueEmitter = new _Exchange.EventExchange(`${queueName}__events`); | ||
@@ -302,3 +303,3 @@ this[kEventHandler].listen(queueEmitter); | ||
if (!queue) return this.createQueue(queueName, options); | ||
if (queue.options.durable !== options.durable) throw new Error('Durable doesn\'t match'); | ||
if (queue.options.durable !== options.durable) throw new _Errors.SmqpError('Durable doesn\'t match', _Errors.ERR_QUEUE_DURABLE_MISMATCH); | ||
return queue; | ||
@@ -338,3 +339,3 @@ }; | ||
if (this.getConsumer(consumerTag)) { | ||
throw new Error(`Consumer tag must be unique, ${consumerTag} is occupied`); | ||
throw new _Errors.SmqpError(`Consumer tag must be unique, ${consumerTag} is occupied`, _Errors.ERR_CONSUMER_TAG_CONFLICT); | ||
} | ||
@@ -345,3 +346,3 @@ return true; | ||
const shovels = this[kEntities].shovels; | ||
if (this.getShovel(name)) throw new Error(`Shovel name must be unique, ${name} is occupied`); | ||
if (this.getShovel(name)) throw new _Errors.SmqpError(`Shovel name must be unique, ${name} is occupied`, _Errors.ERR_SHOVEL_NAME_CONFLICT); | ||
const shovel = new _Shovel.Shovel(name, { | ||
@@ -348,0 +349,0 @@ ...source, |
@@ -18,3 +18,3 @@ "use strict"; | ||
if (!name || typeof name !== 'string') throw new TypeError('Exchange name is required and must be a string'); | ||
if (exchangeTypes.indexOf(type) === -1) throw Error('Exchange type must be one of topic or direct'); | ||
if (exchangeTypes.indexOf(type) === -1) throw new TypeError('Exchange type must be one of topic or direct'); | ||
const eventExchange = new EventExchange(`${name}__events`); | ||
@@ -21,0 +21,0 @@ return new ExchangeBase(name, type, options, eventExchange); |
@@ -18,2 +18,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "SmqpError", { | ||
enumerable: true, | ||
get: function () { | ||
return _Errors.SmqpError; | ||
} | ||
}); | ||
exports.default = void 0; | ||
@@ -29,3 +35,4 @@ Object.defineProperty(exports, "getRoutingKeyPattern", { | ||
var _shared = require("./shared.js"); | ||
var _Errors = require("./Errors.js"); | ||
var _default = _Broker.Broker; | ||
exports.default = _default; |
@@ -10,2 +10,3 @@ "use strict"; | ||
var _Message = require("./Message.js"); | ||
var _Errors = require("./Errors.js"); | ||
const kConsumers = Symbol.for('consumers'); | ||
@@ -112,4 +113,4 @@ const kConsuming = Symbol.for('consuming'); | ||
if (noOfConsumers) { | ||
if (this[kExclusive]) throw new Error(`Queue ${this.name} is exclusively consumed by ${consumers[0].consumerTag}`); | ||
if (consumeOptions.exclusive) throw new Error(`Queue ${this.name} already has consumers and cannot be exclusively consumed`); | ||
if (this[kExclusive]) throw new _Errors.SmqpError(`Queue ${this.name} is exclusively consumed by ${consumers[0].consumerTag}`, _Errors.ERR_EXCLUSIVE_CONFLICT); | ||
if (consumeOptions.exclusive) throw new _Errors.SmqpError(`Queue ${this.name} already has consumers and cannot be exclusively consumed`, _Errors.ERR_EXCLUSIVE_NOT_ALLOWED); | ||
} | ||
@@ -270,3 +271,3 @@ const consumer = new Consumer(this, onMessage, consumeOptions, owner, new ConsumerEmitter(this)); | ||
}; | ||
Queue.prototype.cancel = function cancel(consumerTag) { | ||
Queue.prototype.cancel = function cancel(consumerTag, requeue) { | ||
const consumers = this[kConsumers]; | ||
@@ -276,11 +277,11 @@ const idx = consumers.findIndex(c => c.consumerTag === consumerTag); | ||
const consumer = consumers[idx]; | ||
this.unbindConsumer(consumer); | ||
this.unbindConsumer(consumer, requeue); | ||
}; | ||
Queue.prototype.dismiss = function dismiss(onMessage) { | ||
Queue.prototype.dismiss = function dismiss(onMessage, requeue) { | ||
const consumers = this[kConsumers]; | ||
const consumer = consumers.find(c => c.onMessage === onMessage); | ||
if (!consumer) return; | ||
this.unbindConsumer(consumer); | ||
this.unbindConsumer(consumer, requeue); | ||
}; | ||
Queue.prototype.unbindConsumer = function unbindConsumer(consumer) { | ||
Queue.prototype.unbindConsumer = function unbindConsumer(consumer, requeue = true) { | ||
const consumers = this[kConsumers]; | ||
@@ -292,3 +293,3 @@ const idx = consumers.indexOf(consumer); | ||
consumer.stop(); | ||
consumer.nackAll(true); | ||
consumer.nackAll(requeue); | ||
this.emit('consumer.cancel', consumer); | ||
@@ -422,3 +423,3 @@ if (!consumers.length && this.options.autoDelete) return this.emit('delete', this); | ||
function Consumer(queue, onMessage, options, owner, eventEmitter) { | ||
if (typeof onMessage !== 'function') throw new Error('message callback is required and must be a function'); | ||
if (typeof onMessage !== 'function') throw new TypeError('message callback is required and must be a function'); | ||
const { | ||
@@ -425,0 +426,0 @@ consumerTag |
@@ -9,2 +9,3 @@ "use strict"; | ||
var _Exchange = require("./Exchange.js"); | ||
var _Errors = require("./Errors.js"); | ||
const kBrokerInternal = Symbol.for('brokerInternal'); | ||
@@ -34,7 +35,7 @@ const kCloneMessage = Symbol.for('cloneMessage'); | ||
if (!sourceExchange) { | ||
throw new Error(`shovel ${name} source exchange <${sourceExchangeName}> not found`); | ||
throw new _Errors.SmqpError(`shovel ${name} source exchange <${sourceExchangeName}> not found`, _Errors.ERR_SHOVEL_SOURCE_EXCHANGE_NOT_FOUND); | ||
} | ||
const destinationExchange = destinationBroker.getExchange(destinationExchangeName); | ||
if (!destinationExchange) { | ||
throw new Error(`shovel ${name} destination exchange <${destinationExchangeName}> not found`); | ||
throw new _Errors.SmqpError(`shovel ${name} destination exchange <${destinationExchangeName}> not found`, _Errors.ERR_SHOVEL_DESTINATION_EXCHANGE_NOT_FOUND); | ||
} | ||
@@ -41,0 +42,0 @@ if (!(this instanceof Shovel)) { |
{ | ||
"name": "smqp", | ||
"version": "8.1.0", | ||
"version": "8.2.0", | ||
"type": "module", | ||
@@ -30,3 +30,3 @@ "description": "Synchronous message queueing package", | ||
"src", | ||
"dist/*.js" | ||
"dist" | ||
], | ||
@@ -63,3 +63,3 @@ "scripts": { | ||
"@babel/core": "^7.22.11", | ||
"@babel/preset-env": "^7.22.10", | ||
"@babel/preset-env": "^7.22.14", | ||
"c8": "^8.0.1", | ||
@@ -66,0 +66,0 @@ "chai": "^4.3.8", |
@@ -5,2 +5,3 @@ import { Exchange, EventExchange } from './Exchange.js'; | ||
import { generateId } from './shared.js'; | ||
import { SmqpError, ERR_EXCHANGE_TYPE_MISMATCH, ERR_QUEUE_DURABLE_MISMATCH, ERR_CONSUMER_TAG_CONFLICT, ERR_QUEUE_NAME_CONFLICT, ERR_SHOVEL_NAME_CONFLICT, ERR_QUEUE_NOT_FOUND } from './Errors.js'; | ||
@@ -44,3 +45,3 @@ const kEntities = Symbol.for('entities'); | ||
Broker.prototype.subscribe = function subscribe(exchangeName, pattern, queueName, onMessage, options = { durable: true }) { | ||
if (!exchangeName || !pattern || typeof onMessage !== 'function') throw new Error('exchange name, pattern, and message callback are required'); | ||
if (!exchangeName || !pattern || typeof onMessage !== 'function') throw new TypeError('exchange name, pattern, and message callback are required'); | ||
if (options && options.consumerTag) this.validateConsumerTag(options.consumerTag); | ||
@@ -61,3 +62,3 @@ | ||
Broker.prototype.subscribeOnce = function subscribeOnce(exchangeName, pattern, onMessage, options = {}) { | ||
if (typeof onMessage !== 'function') throw new Error('message callback is required'); | ||
if (typeof onMessage !== 'function') throw new TypeError('message callback is required'); | ||
if (options && options.consumerTag) this.validateConsumerTag(options.consumerTag); | ||
@@ -88,3 +89,3 @@ | ||
if (exchange) { | ||
if (type && exchange.type !== type) throw new Error('Type doesn\'t match'); | ||
if (type && exchange.type !== type) throw new SmqpError('Type doesn\'t match', ERR_EXCHANGE_TYPE_MISMATCH); | ||
return exchange; | ||
@@ -116,3 +117,3 @@ } | ||
const queue = this.getQueue(queueName); | ||
if (!queue) throw new Error(`Queue with name <${queueName}> was not found`); | ||
if (!queue) throw new SmqpError(`Queue with name <${queueName}> was not found`, ERR_QUEUE_NOT_FOUND); | ||
@@ -255,3 +256,3 @@ if (options) this.validateConsumerTag(options.consumerTag); | ||
const queue = this.getQueue(queueName); | ||
if (!queue) throw new Error(`Queue named ${queueName} doesn't exists`); | ||
if (!queue) throw new SmqpError(`Queue with name <${queueName}> was not found`, ERR_QUEUE_NOT_FOUND); | ||
return queue.queueMessage(null, content, options); | ||
@@ -285,3 +286,3 @@ }; | ||
else if (!queueName) queueName = `smq.qname-${generateId()}`; | ||
else if (this.getQueue(queueName)) throw new Error(`Queue named ${queueName} already exists`); | ||
else if (this.getQueue(queueName)) throw new SmqpError(`Queue named ${queueName} already exists`, ERR_QUEUE_NAME_CONFLICT); | ||
@@ -311,3 +312,3 @@ const queueEmitter = new EventExchange(`${queueName}__events`); | ||
if (queue.options.durable !== options.durable) throw new Error('Durable doesn\'t match'); | ||
if (queue.options.durable !== options.durable) throw new SmqpError('Durable doesn\'t match', ERR_QUEUE_DURABLE_MISMATCH); | ||
return queue; | ||
@@ -353,3 +354,3 @@ }; | ||
if (this.getConsumer(consumerTag)) { | ||
throw new Error(`Consumer tag must be unique, ${consumerTag} is occupied`); | ||
throw new SmqpError(`Consumer tag must be unique, ${consumerTag} is occupied`, ERR_CONSUMER_TAG_CONFLICT); | ||
} | ||
@@ -362,3 +363,3 @@ | ||
const shovels = this[kEntities].shovels; | ||
if (this.getShovel(name)) throw new Error(`Shovel name must be unique, ${name} is occupied`); | ||
if (this.getShovel(name)) throw new SmqpError(`Shovel name must be unique, ${name} is occupied`, ERR_SHOVEL_NAME_CONFLICT); | ||
const shovel = new Shovel(name, { ...source, broker: this }, destination, options); | ||
@@ -365,0 +366,0 @@ this[kEventHandler].listen(shovel.events); |
@@ -15,3 +15,3 @@ import { Message } from './Message.js'; | ||
if (exchangeTypes.indexOf(type) === -1) throw Error('Exchange type must be one of topic or direct'); | ||
if (exchangeTypes.indexOf(type) === -1) throw new TypeError('Exchange type must be one of topic or direct'); | ||
const eventExchange = new EventExchange(`${name}__events`); | ||
@@ -18,0 +18,0 @@ return new ExchangeBase(name, type, options, eventExchange); |
import { Broker } from './Broker.js'; | ||
import { Shovel } from './Shovel.js'; | ||
import { getRoutingKeyPattern } from './shared.js'; | ||
import { SmqpError } from './Errors.js'; | ||
export default Broker; | ||
export { Broker, Shovel, getRoutingKeyPattern }; | ||
export { Broker, Shovel, getRoutingKeyPattern, SmqpError }; |
import { generateId, sortByPriority } from './shared.js'; | ||
import { Message } from './Message.js'; | ||
import { SmqpError, ERR_EXCLUSIVE_CONFLICT, ERR_EXCLUSIVE_NOT_ALLOWED } from './Errors.js'; | ||
@@ -119,4 +120,4 @@ const kConsumers = Symbol.for('consumers'); | ||
if (noOfConsumers) { | ||
if (this[kExclusive]) throw new Error(`Queue ${this.name} is exclusively consumed by ${consumers[0].consumerTag}`); | ||
if (consumeOptions.exclusive) throw new Error(`Queue ${this.name} already has consumers and cannot be exclusively consumed`); | ||
if (this[kExclusive]) throw new SmqpError(`Queue ${this.name} is exclusively consumed by ${consumers[0].consumerTag}`, ERR_EXCLUSIVE_CONFLICT); | ||
if (consumeOptions.exclusive) throw new SmqpError(`Queue ${this.name} already has consumers and cannot be exclusively consumed`, ERR_EXCLUSIVE_NOT_ALLOWED); | ||
} | ||
@@ -299,3 +300,3 @@ | ||
Queue.prototype.cancel = function cancel(consumerTag) { | ||
Queue.prototype.cancel = function cancel(consumerTag, requeue) { | ||
const consumers = this[kConsumers]; | ||
@@ -306,13 +307,13 @@ const idx = consumers.findIndex((c) => c.consumerTag === consumerTag); | ||
const consumer = consumers[idx]; | ||
this.unbindConsumer(consumer); | ||
this.unbindConsumer(consumer, requeue); | ||
}; | ||
Queue.prototype.dismiss = function dismiss(onMessage) { | ||
Queue.prototype.dismiss = function dismiss(onMessage, requeue) { | ||
const consumers = this[kConsumers]; | ||
const consumer = consumers.find((c) => c.onMessage === onMessage); | ||
if (!consumer) return; | ||
this.unbindConsumer(consumer); | ||
this.unbindConsumer(consumer, requeue); | ||
}; | ||
Queue.prototype.unbindConsumer = function unbindConsumer(consumer) { | ||
Queue.prototype.unbindConsumer = function unbindConsumer(consumer, requeue = true) { | ||
const consumers = this[kConsumers]; | ||
@@ -327,3 +328,3 @@ const idx = consumers.indexOf(consumer); | ||
consumer.stop(); | ||
consumer.nackAll(true); | ||
consumer.nackAll(requeue); | ||
@@ -467,3 +468,3 @@ this.emit('consumer.cancel', consumer); | ||
export function Consumer(queue, onMessage, options, owner, eventEmitter) { | ||
if (typeof onMessage !== 'function') throw new Error('message callback is required and must be a function'); | ||
if (typeof onMessage !== 'function') throw new TypeError('message callback is required and must be a function'); | ||
@@ -470,0 +471,0 @@ const { consumerTag } = this.options = { prefetch: 1, priority: 0, noAck: false, ...options }; |
import { EventExchange } from './Exchange.js'; | ||
import { SmqpError, ERR_SHOVEL_DESTINATION_EXCHANGE_NOT_FOUND, ERR_SHOVEL_SOURCE_EXCHANGE_NOT_FOUND } from './Errors.js'; | ||
@@ -21,3 +22,3 @@ const kBrokerInternal = Symbol.for('brokerInternal'); | ||
if (!sourceExchange) { | ||
throw new Error(`shovel ${name} source exchange <${sourceExchangeName}> not found`); | ||
throw new SmqpError(`shovel ${name} source exchange <${sourceExchangeName}> not found`, ERR_SHOVEL_SOURCE_EXCHANGE_NOT_FOUND); | ||
} | ||
@@ -27,3 +28,3 @@ | ||
if (!destinationExchange) { | ||
throw new Error(`shovel ${name} destination exchange <${destinationExchangeName}> not found`); | ||
throw new SmqpError(`shovel ${name} destination exchange <${destinationExchangeName}> not found`, ERR_SHOVEL_DESTINATION_EXCHANGE_NOT_FOUND); | ||
} | ||
@@ -30,0 +31,0 @@ |
@@ -1,6 +0,7 @@ | ||
import { Broker } from "./Broker.js"; | ||
import { Shovel } from "./Shovel.js"; | ||
import { getRoutingKeyPattern } from "./shared.js"; | ||
import { Broker } from './Broker.js'; | ||
import { Shovel } from './Shovel.js'; | ||
import { getRoutingKeyPattern } from './shared.js'; | ||
import { SmqpError } from './Errors.js'; | ||
export default Broker; | ||
export { Broker, Shovel, getRoutingKeyPattern }; | ||
export { Broker, Shovel, getRoutingKeyPattern, SmqpError }; |
@@ -58,5 +58,5 @@ import { Message, MessageFields, MessageProperties, MessageMessage } from './Message.js'; | ||
peek(ignoreDelivered?: boolean): Message; | ||
cancel(consumerTag: string): void; | ||
dismiss(onMessage: onMessage): void; | ||
unbindConsumer(consumer: any): void; | ||
cancel(consumerTag: string, requeue?: boolean): void; | ||
dismiss(onMessage: onMessage, requeue?: boolean): void; | ||
unbindConsumer(consumer: Consumer, requeue?: boolean): void; | ||
emit(eventName: string, content?: any): void; | ||
@@ -63,0 +63,0 @@ on(eventName: string, handler: CallableFunction): Consumer; |
128251
29
3423