Comparing version 1.0.0 to 1.1.0
101
app.js
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
const Message = require('ali-ons').Message; | ||
const Consumer = require('ali-ons').Consumer; | ||
const Producer = require('ali-ons').Producer; | ||
const ONS = require('./lib/ons'); | ||
module.exports = app => { | ||
const logger = app.getLogger('onsLogger'); | ||
const { sub, pub } = app.config.ons; | ||
const defaultOptions = app.config.ons.default; | ||
const httpclient = app.httpclient; | ||
const consumerMap = new Map(); | ||
const producerMap = new Map(); | ||
const topic2Producer = new Map(); | ||
let appReady = false; | ||
app.ready(() => { | ||
appReady = true; | ||
app.ons = new ONS(app); | ||
app.beforeStart(function* () { | ||
yield app.ons.init(); | ||
}); | ||
function errorHandler(err) { | ||
// 应用启动前避免错误输出到标准输出 | ||
if (appReady) { | ||
logger.error(err); | ||
} else { | ||
logger.warn(err); | ||
} | ||
} | ||
for (const options of sub) { | ||
const consumer = new Consumer(Object.assign({ | ||
httpclient, | ||
logger, | ||
}, defaultOptions, options)); | ||
consumer.on('error', errorHandler); | ||
const key = `${consumer.consumerGroup}-${consumer.clientId}`; | ||
assert(!consumerMap.has(key), `[egg-ons] duplicate consumer, consumerGroup=${consumer.consumerGroup}, clientId=${consumer.clientId}`); | ||
consumerMap.set(key, consumer); | ||
app.beforeStart(function* () { | ||
yield consumer.ready(); | ||
logger.info('[egg-ons] consumer: %s is ready, messageModel: %s', consumer.consumerGroup, consumer.messageModel); | ||
}); | ||
app.beforeClose(function* () { | ||
yield consumer.close(); | ||
logger.info('[egg-ons] consumer: %s is closed, messageModel: %s', consumer.consumerGroup, consumer.messageModel); | ||
}); | ||
const topics = options.topics || []; | ||
for (const topic of topics) { | ||
const filepath = path.join(app.config.baseDir, 'app/ons', topic + '.js'); | ||
if (!fs.existsSync(filepath)) { | ||
app.coreLogger.warn('[egg-ons] CANNOT find the subscription logic in file:`%s` for topic=%s', filepath, topic); | ||
continue; | ||
} | ||
const Subscriber = require(filepath); | ||
consumer.subscribe(topic, Subscriber.subExpression || '*', function* (msg) { | ||
const ctx = app.createAnonymousContext(); | ||
const subscriber = new Subscriber(ctx); | ||
yield subscriber.subscribe(msg); | ||
}); | ||
} | ||
} | ||
for (const options of pub) { | ||
const producer = new Producer(Object.assign({ | ||
httpclient: app.httpclient, | ||
logger, | ||
}, defaultOptions, options)); | ||
producer.on('error', errorHandler); | ||
assert(!producerMap.has(producer.producerGroup), `[egg-ons] duplicate producer, producerGroup=${producer.producerGroup}`); | ||
producerMap.set(producer.producerGroup, producer); | ||
const topics = options.topics || []; | ||
for (const topic of topics) { | ||
topic2Producer.set(topic, producer); | ||
} | ||
app.beforeStart(function* () { | ||
yield producer.ready(); | ||
logger.info('[egg-ons] producer: %s is ready', producer.producerGroup); | ||
}); | ||
app.beforeClose(function* () { | ||
yield producer.close(); | ||
logger.info('[egg-ons] producer: %s is closed', producer.producerGroup); | ||
}); | ||
} | ||
app.ons = { | ||
consumerMap, | ||
producerMap, | ||
Message, | ||
* send(msg) { | ||
assert(msg && msg.topic, '[egg-ons] send(msg) msg.topic is required'); | ||
const producer = topic2Producer.get(msg.topic); | ||
assert(producer, `[egg-ons] CANNOT find producer for topic=${msg.topic}`); | ||
return yield producer.send(msg); | ||
}, | ||
}; | ||
}; |
1.1.0 / 2018-05-03 | ||
================== | ||
**features** | ||
* [[`097d576`](http://github.com/eggjs/egg-ons/commit/097d5765352e7f177170ec8f2b782d1034815a0d)] - feat: support dynamic create comsumer and producer (dead-horse <<dead_horse@qq.com>>) | ||
**others** | ||
* [[`9b46e9c`](http://github.com/eggjs/egg-ons/commit/9b46e9c4029d8cc9509ce1bf7afee9f98c083ebe)] - f (dead-horse <<dead_horse@qq.com>>) | ||
* [[`d86ed69`](http://github.com/eggjs/egg-ons/commit/d86ed69333093bdf3c0f637310f9d8b1e6c9d3d6)] - f (dead-horse <<dead_horse@qq.com>>) | ||
* [[`ebf5adc`](http://github.com/eggjs/egg-ons/commit/ebf5adc3087111a0a92dc6eeada6700a67be3f55)] - f (dead-horse <<dead_horse@qq.com>>) | ||
* [[`9a66754`](http://github.com/eggjs/egg-ons/commit/9a667549f4cb48ce7487c14039a441ae0956d2d0)] - doc: fix wrong plural config key (Hongcai Deng <<admin@dhchouse.com>>) | ||
* [[`9b97d3b`](http://github.com/eggjs/egg-ons/commit/9b97d3b6358053b003dd01be3fe91f9a688cd9db)] - chore: release 1.0.0 (xiaochen.gaoxc <<xiaochen.gaoxc@alibaba-inc.com>>), | ||
1.0.0 / 2017-10-19 | ||
@@ -3,0 +16,0 @@ ================== |
{ | ||
"name": "egg-ons", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "aliyun ons plugin for egg", | ||
@@ -40,4 +40,5 @@ "eggPlugin": { | ||
"app", | ||
"config", | ||
"app.js", | ||
"config" | ||
"lib" | ||
], | ||
@@ -44,0 +45,0 @@ "repository": { |
@@ -53,3 +53,3 @@ # egg-ons | ||
}, | ||
subs: [{ | ||
sub: [{ | ||
consumerGroup: 'your-consumer-group', | ||
@@ -60,3 +60,3 @@ topics: [ | ||
}], | ||
pubs: [{ | ||
pub: [{ | ||
producerGroup: 'your-producer-group', | ||
@@ -63,0 +63,0 @@ topics: [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13030
9
165
0