Skip to content

Commit

Permalink
feat(koa): simpleMiddleWare for koa
Browse files Browse the repository at this point in the history
affects: @tao.js/koa

ISSUES CLOSED: #23
  • Loading branch information
eudaimos committed Feb 24, 2021
1 parent f6e5959 commit 92674d9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/koa-tao/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import taoHttpMiddleware from './tao-http-middleware';
import simpleMiddleware from './simple-middleware';

export default taoHttpMiddleware;

export { taoHttpMiddleware, simpleMiddleware };
79 changes: 79 additions & 0 deletions packages/koa-tao/src/simple-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// import { AppCtx } from '@tao.js/core';
import cartesian from 'cartesian';
import { Channel, Transponder } from '@tao.js/utils';
import { noop, normalizeAC, cleanInput } from './helpers';

const DEFAULT_CHANNEL_NAME = 'koa-simple-middleware';
const CHANNEL_NAME_TYPE = 'channel';
const TRANSPONDER_NAME_TYPE = 'transponder';
const DEFAULT_TIMEOUT = 3000;

function getNameId(type, name) {
return newId => {
return `${name}-${type}-${newId}`;
};
}

function buildCtxTao(transponder) {
return {
setCtx({ t, term, a, action, o, orient }, data) {
return transponder.setCtx({ t, term, a, action, o, orient }, data);
},
setAppCtx(ac) {
return transponder.setAppCtx(ac);
}
};
}

/**
* Use the `simpleMiddleware` to build koa apps that work with your TAO Signal Network on the
* koa app server.
*
* @export
* @param {Kernel} TAO
* @param {Object} [opt={}]
* @param {string} [opt.name] - name to prepend to the TAO Channel and Transponder names that will be generated
* @param {number} [opt.timeout=3000] - timeout in Milliseconds to wait on completing a TAO chain before the Transponder rejects the Promise
* @param {Promise} [opt.promise=Promise] - Promise constructor used to create promises returned by `setCtx` and `setAppCtx`
* @returns {Object} - an Object instantiated to attach a `middleware` to a koa app and add/remove response handlers
*/
export default function simpleMiddleware(TAO, opt = {}) {
const namer = getNameId(CHANNEL_NAME_TYPE, opt.name || DEFAULT_CHANNEL_NAME);
const channel = new Channel(TAO, namer);
return {
middleware() {
return (ctx, next) => {
const transponder = new Transponder(
channel,
getNameId(TRANSPONDER_NAME_TYPE, opt.name),
opt.timeout || DEFAULT_TIMEOUT,
opt.promise
);
ctx.tao = buildCtxTao(transponder);
next();
transponder.detach();
ctx.tao = null;
transponder = null;
};
},
addResponseHandler({ t, term, a, action, o, orient }, handler = noop) {
const trigrams = cleanInput(
normalizeAC({ t, term, a, action, o, orient })
);
const permutations = cartesian(trigrams);
for (let trigram of permutations) {
console.log('@tao.js/koa::addResponseHandler::trigram:', trigram);
channel.addInlineHandler(trigram, handler);
}
},
removeResponseHandler({ t, term, a, action, o, orient }, handler = noop) {
const trigrams = cleanInput(
normalizeAC({ t, term, a, action, o, orient })
);
const permutations = cartesian(trigrams);
for (let trigram of permutations) {
channel.removeInlineHandler(trigram, handler);
}
}
};
}
2 changes: 1 addition & 1 deletion packages/tao-utils/src/Transceiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function transceiverControl(transceiverId, resolve, reject) {
}

/**
* Like a Transponder, a Transceiver converts Signnals on a Network to Promises.
* Like a Transponder, a Transceiver converts Signals on a Network to Promises.
* Unlike a Transponder, a Transceiver allows the handlers attached to it to control
* the behavior of the Promise.
*
Expand Down

0 comments on commit 92674d9

Please sign in to comment.