2017-08-31 06:42:10 +00:00
|
|
|
/* eslint-disable no-unused-vars */
|
2018-05-02 13:20:00 +00:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { PIPE_ENCODE } from '../constants';
|
|
|
|
import { kebabCase } from '../utils';
|
2017-08-31 06:42:10 +00:00
|
|
|
|
2017-09-08 02:41:25 +00:00
|
|
|
/**
|
|
|
|
* @lifecycle
|
2018-02-15 03:01:24 +00:00
|
|
|
* static onCheckParams()
|
|
|
|
* static onCache()
|
|
|
|
* constructor()
|
|
|
|
* onInit()
|
|
|
|
* ...
|
|
|
|
* onDestroy()
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* static onCheckParams() and static onCache() are called only once since new Hub().
|
2017-09-08 02:41:25 +00:00
|
|
|
*/
|
2018-05-02 13:20:00 +00:00
|
|
|
export class IPreset extends EventEmitter {
|
2017-02-23 13:41:43 +00:00
|
|
|
|
2018-02-15 03:01:24 +00:00
|
|
|
_config = null;
|
2017-11-24 06:22:37 +00:00
|
|
|
|
2018-05-02 13:20:00 +00:00
|
|
|
_write({ type, buffer, direct, isUdp }, extraArgs) {
|
|
|
|
const postfix = (type === PIPE_ENCODE ? 'Out' : 'In') + (isUdp ? 'Udp' : '');
|
|
|
|
|
|
|
|
// prepare args
|
2018-06-24 05:31:41 +00:00
|
|
|
const fail = (message) => void this.emit('fail', this.name, message);
|
2018-05-02 13:20:00 +00:00
|
|
|
const next = (processed, isReverse = false) => {
|
|
|
|
// oh my nice hack to deal with reverse pipeline if haven't been created
|
|
|
|
const hasListener = this.emit(`next_${isReverse ? -type : type}`, processed);
|
|
|
|
if (!hasListener) {
|
|
|
|
direct(processed, isReverse);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// clientXXX, serverXXX
|
|
|
|
const nextLifeCycleHook = (buf/*, isReverse = false */) => {
|
2018-05-04 14:08:36 +00:00
|
|
|
const args = { buffer: buf, next, fail };
|
2018-05-02 13:20:00 +00:00
|
|
|
const ret = this._config.is_client ? this[`client${postfix}`](args, extraArgs) : this[`server${postfix}`](args, extraArgs);
|
|
|
|
if (ret instanceof Buffer) {
|
|
|
|
next(ret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// beforeXXX
|
|
|
|
// NOTE: next(buf, isReverse) is not available in beforeXXX
|
2018-05-04 14:08:36 +00:00
|
|
|
const args = { buffer, next: nextLifeCycleHook, fail };
|
2018-05-02 13:20:00 +00:00
|
|
|
const ret = this[`before${postfix}`](args, extraArgs);
|
|
|
|
if (ret instanceof Buffer) {
|
|
|
|
nextLifeCycleHook(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return kebabCase(this.constructor.name).replace(/(.*)-preset/i, '$1');
|
|
|
|
}
|
|
|
|
|
2017-08-31 06:42:10 +00:00
|
|
|
/**
|
2017-09-02 14:09:16 +00:00
|
|
|
* check params passed to the preset, if any errors, should throw directly
|
2017-09-08 02:41:25 +00:00
|
|
|
* @param params
|
2017-08-31 06:42:10 +00:00
|
|
|
*/
|
2018-02-15 03:01:24 +00:00
|
|
|
static onCheckParams(params) {
|
2017-08-31 06:42:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:41:25 +00:00
|
|
|
/**
|
2018-02-15 03:01:24 +00:00
|
|
|
* you can make some cache in store or just return something
|
|
|
|
* you want to put in store, then access store later in other
|
|
|
|
* hook functions via this.getStore()
|
2017-09-08 02:41:25 +00:00
|
|
|
* @param params
|
2018-02-15 03:01:24 +00:00
|
|
|
* @param store
|
2017-09-08 02:41:25 +00:00
|
|
|
*/
|
2018-07-28 13:16:25 +00:00
|
|
|
static async onCache(params, store) {
|
2018-02-15 03:01:24 +00:00
|
|
|
// or return something
|
2017-09-08 02:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 03:01:24 +00:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
* @param config
|
|
|
|
* @param params
|
|
|
|
*/
|
2018-02-17 04:19:31 +00:00
|
|
|
constructor({ config, params } = {}) {
|
2018-05-02 13:20:00 +00:00
|
|
|
super();
|
|
|
|
this._config = config;
|
2018-02-15 03:01:24 +00:00
|
|
|
}
|
2017-10-30 14:15:56 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-15 03:01:24 +00:00
|
|
|
* constructor alternative to do initialization
|
|
|
|
* @param params
|
2017-10-30 14:15:56 +00:00
|
|
|
*/
|
2018-02-15 03:01:24 +00:00
|
|
|
onInit(params) {
|
2017-10-30 14:15:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-30 06:25:56 +00:00
|
|
|
/**
|
|
|
|
* you can do something when preset destroyed
|
|
|
|
*/
|
|
|
|
onDestroy() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-26 09:59:09 +00:00
|
|
|
// hooks for tcp
|
2017-03-01 02:50:07 +00:00
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
beforeOut({ buffer, next, fail }) {
|
2017-02-26 08:07:05 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
beforeIn({ buffer, next, fail }) {
|
2017-02-26 08:07:05 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
clientOut({ buffer, next, fail }) {
|
2017-03-23 13:51:25 +00:00
|
|
|
return buffer;
|
2017-02-23 13:41:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
serverIn({ buffer, next, fail }) {
|
2017-03-23 13:51:25 +00:00
|
|
|
return buffer;
|
2017-02-23 13:41:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
serverOut({ buffer, next, fail }) {
|
2017-03-23 13:51:25 +00:00
|
|
|
return buffer;
|
2017-02-23 13:41:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
clientIn({ buffer, next, fail }) {
|
2017-03-23 13:51:25 +00:00
|
|
|
return buffer;
|
2017-02-23 13:41:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 09:59:09 +00:00
|
|
|
// hooks for udp
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
beforeOutUdp({ buffer, next, fail }) {
|
2017-10-26 09:59:09 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
beforeInUdp({ buffer, next, fail }) {
|
2017-10-26 09:59:09 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
clientOutUdp({ buffer, next, fail }) {
|
2017-10-26 09:59:09 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
serverInUdp({ buffer, next, fail }) {
|
2017-10-26 09:59:09 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
serverOutUdp({ buffer, next, fail }) {
|
2017-10-26 09:59:09 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:08:36 +00:00
|
|
|
clientInUdp({ buffer, next, fail }) {
|
2017-10-26 09:59:09 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-05-02 13:20:00 +00:00
|
|
|
// reserved methods, DO NOT overwrite them!
|
2017-09-07 09:11:37 +00:00
|
|
|
|
2017-10-30 14:15:56 +00:00
|
|
|
/**
|
|
|
|
* direct read any property(match non-static then static) of other preset
|
|
|
|
* @param presetName
|
|
|
|
* @param propertyName
|
|
|
|
*/
|
|
|
|
readProperty(presetName, propertyName) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-15 03:01:24 +00:00
|
|
|
/**
|
|
|
|
* return store passed to onCache()
|
|
|
|
*/
|
|
|
|
getStore() {
|
2017-08-31 06:41:11 +00:00
|
|
|
|
2018-02-15 03:01:24 +00:00
|
|
|
}
|
2017-12-25 08:42:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-15 03:01:24 +00:00
|
|
|
* a class which handle addressing
|
2017-12-25 08:42:01 +00:00
|
|
|
*/
|
2018-02-15 03:01:24 +00:00
|
|
|
export class IPresetAddressing extends IPreset {
|
2017-08-31 06:41:11 +00:00
|
|
|
|
2018-04-15 11:59:39 +00:00
|
|
|
/**
|
|
|
|
* triggered once target address resolved on client side
|
|
|
|
* @param host
|
|
|
|
* @param port
|
|
|
|
*/
|
|
|
|
onInitTargetAddress({ host, port }) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 13:20:00 +00:00
|
|
|
// reserved methods, DO NOT overwrite them!
|
|
|
|
|
2018-04-15 12:34:59 +00:00
|
|
|
/**
|
2018-05-02 13:20:00 +00:00
|
|
|
* call it when target address was resolved on server side
|
2018-04-15 12:34:59 +00:00
|
|
|
* @param host
|
|
|
|
* @param port
|
|
|
|
* @param callback
|
|
|
|
*/
|
|
|
|
resolveTargetAddress({ host, port }, callback) {
|
2018-05-04 14:08:36 +00:00
|
|
|
|
2018-04-15 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 06:41:11 +00:00
|
|
|
}
|