Skip to content

Commit

Permalink
initial structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbdk committed Jun 22, 2019
0 parents commit ed90442
Show file tree
Hide file tree
Showing 8 changed files with 3,950 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Empty file added index.js
Empty file.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "vargr-table",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jest": "^24.8.0",
"koa": "^2.7.0",
"koa-router": "^7.4.0",
"koa-websocket": "^5.0.1"
}
}
59 changes: 59 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function dispatchMessage(msg, collectionName, subscriptions, ignore = []) {
if (subscriptions[collectionName]) {
subscriptions[collectionName].forEach((sock) => {
if (ignore && ignore.indexOf(sock) === -1) {
sock.send(JSON.stringify(msg));
}
});
}
}

function handleDisconnect(collectionName, dbi, sock, message) {}

const actions = {
addAction: async (collectionName, dbi, sock, req, subs) => {
const resData = await dbi.create(collectionName, req);
dispatchMessage({
action: req.action,
data: resData
}, collectionName, subs);
},
updateAction: async (collectionName, dbi, sock, req, subs) => {
const method = Array.isArray(req.data) ? 'updateMany' : 'updateOne';
const resData = await dbi[method](collectionName, req);
dispatchMessage({
action: req.action,
data: resData
}, collectionName, subs);
},
deleteAction: async (collectionName, dbi, sock, req, subs) => {
const resData = await dbi.delete(collectionName, req);
dispatchMessage({
action: req.action,
data: {
where: req.where
}
}, collectionName, subs);
},
pingAction: async (collectionName, dbi, sock, req, subs) => {
sock.send(JSON.stringify({
action: req.action,
data: {
servertime: performance.now(),
clienttime: req.clienttime
}
}));
},
subAction: (collectionName, dbi, sock, message, subs) => {
if (!subs[collectionName]) {
subs[collectionName] = [];
}
subs[collectionName].push(sock);
},
unsubAction: (collectionName, dbi, sock, message, subs) => {
if (!subs[collectionName]) {
subs[collectionName] = [];
}
subs[collectionName] = subs[collectionName].filter((s) => s != sock);
}
}
5 changes: 5 additions & 0 deletions src/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class {
constructor(url, protocols = []) {
this.socket = new WebSocket(url, protocols);
}
}
32 changes: 32 additions & 0 deletions src/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Koa = require('koa');
const Router = require('koa-router');
const websockify = require('koa-websocket');

const actions = require('./actions');

module.exports = function sock(dbi) {
const subscriptions = {};
const router = new Router();
const app = websockify(new Koa());

router.get(`/:collectionName`, ctx => {
const collectionName = ctx.params.collectionName;

actions.subAction(collectionName, dbi, ctx.websocket, null, subscriptions)

ctx.websocket.on('message', (rawReq) => {
const requests = rawReq[0] === "[" ? JSON.parse(rawReq) : [JSON.parse(rawReq)];

requests.forEach((req) => {
actions[req.action + 'Action'](collectionName, dbi, ctx.websocket, req, subscriptions);
});
});

ctx.websocket.on('close', () => {
actions.unsubAction(collectionName, dbi, ctx.websocket, null, subscriptions)
});
});

app.ws.use(router.routes());
return app;
}
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Agent = require('./src/agent');
const Table = require('./src/table');




const agent01 = new Agent("wss://www.example.com/socketserver", ["protocolOne", "protocolTwo"])
Loading

0 comments on commit ed90442

Please sign in to comment.