Skip to content

Commit ede6645

Browse files
author
edencoder
committed
amend issues
1 parent 437dc78 commit ede6645

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

bundles/socket/daemons/socket.js renamed to bundles/socket/daemons/socket.ts

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
// Require dependencies
3-
const uuid = require('uuid');
4-
const fetch = require('node-fetch');
5-
const config = require('config');
6-
const Daemon = require('daemon');
7-
const session = require('express-session');
8-
const socketio = require('socket.io');
9-
const SessionStore = require('@edenjs/session-store');
10-
const cookieParser = require('cookie-parser');
3+
import uuid from 'uuid';
4+
import fetch from 'node-fetch';
5+
import config from 'config';
6+
import Daemon from 'daemon';
7+
import session from 'express-session';
8+
import socketio from 'socket.io';
9+
import SessionStore from '@edenjs/session-store';
10+
import cookieParser from 'cookie-parser';
1111

1212
// Require cache dependencies
1313
const calls = cache('calls');
@@ -24,7 +24,7 @@ const aclHelper = helper('user/acl');
2424
* @cluster front
2525
* @cluster socket
2626
*/
27-
class SocketDaemon extends Daemon {
27+
export default class SocketDaemon extends Daemon {
2828
/**
2929
* Construct socket daemon
3030
*/
@@ -76,7 +76,7 @@ class SocketDaemon extends Daemon {
7676
*/
7777
build() {
7878
// Set io
79-
this.__socketIO = socketio(this.eden.router.server);
79+
this.__socketIO = socketio(this.eden.router.app.server);
8080

8181
// Listen for connection
8282
this.__socketIO.on('connection', this.onConnect);
@@ -364,28 +364,53 @@ class SocketDaemon extends Daemon {
364364
if (user) await user.refresh();
365365

366366
// create headers
367-
const headers = Object.assign({}, {
367+
const headers = {
368368
host : socket.request.headers.host,
369369
origin : socket.request.headers.origin,
370370
cookie : socket.request.headers.cookie,
371+
Accept : 'application/json',
371372

372373
'user-agent' : socket.request.headers['user-agent'],
373374
'accept-encoding' : socket.request.headers['accept-encoding'],
374375
'accept-language' : socket.request.headers['accept-language'],
375376
'x-forwarded-for' : socket.request.headers['x-forwarded-for'],
376-
}, {
377-
Accept : 'application/json',
378-
});
377+
};
379378

380-
// get headers
381-
const res = await fetch(`http://${config.get('host')}:${this.eden.port}${data.route}`, {
382-
headers,
379+
// create handlers array
380+
const handlers = this.eden.router.app.find(data.method, data.path).handlers || [];
381+
382+
// create faux request and response
383+
let req = { ...socket.request };
384+
let res = { ...socket.request.res, send : (data, code = 200) => {
385+
console.log('send', data);
386+
// await text
387+
socket.emit(data.id, code, JSON.stringify(data));
388+
}, end : (data, code = 200) => {
389+
// await text
390+
socket.emit(data.id, code, JSON.stringify(data));
391+
}, setHeader : () => {}, getHeader : (key) => headers[key] };
392+
393+
// create next
394+
const wrapAndNext = (i) => {
395+
// add to req
396+
req = Object.assign(req, {
397+
user,
398+
socket,
399+
headers,
400+
body : data.body,
401+
query : data.query,
402+
pathname : data.path,
403+
sessionID : socket.IDs.sessionID,
404+
});
383405

384-
redirect : 'follow',
385-
});
406+
if (!handlers[i]) console.log(handlers[i - 1].toString());
407+
408+
// wrap and next
409+
handlers[i](req, res, () => wrapAndNext(i + 1));
410+
};
386411

387-
// await text
388-
socket.emit(data.id, await res.json());
412+
// wrap and next
413+
wrapAndNext(0);
389414
}
390415

391416
/**
@@ -423,11 +448,4 @@ class SocketDaemon extends Daemon {
423448
// return done
424449
return done;
425450
}
426-
}
427-
428-
/**
429-
* Construct socket daemon
430-
*
431-
* @type {socket}
432-
*/
433-
exports = module.exports = SocketDaemon;
451+
}

bundles/socket/public/js/bootstrap.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,20 @@ class SocketStore {
107107
*
108108
* @return {Promise}
109109
*/
110-
async route(route, opts) {
110+
async route(method, path, opts = {}) {
111+
// check body
112+
const { query, body } = opts;
113+
111114
// Let id
112115
const id = uuid();
113116

114117
// Create emission
115118
const emission = {
116119
id,
117-
opts,
118-
route,
120+
path,
121+
method,
122+
body : body || {},
123+
query : query || {},
119124
};
120125

121126
// Emit to socket

0 commit comments

Comments
 (0)