diff --git a/packages/socket/src/client/index.js b/packages/socket/src/client/index.js index 58cb048..1bfb83b 100644 --- a/packages/socket/src/client/index.js +++ b/packages/socket/src/client/index.js @@ -7,7 +7,13 @@ export default { hooks: { '@latus/http/client/up': (latus) => { // eslint-disable-next-line no-param-reassign - latus.config['%packets'] = gather(latus, '@latus/socket/packets', 'id', badPacketsCheck); + latus.config['%packets'] = gather( + latus, + '@latus/socket/packets', + 'id', + 'name', + badPacketsCheck, + ); const socket = new SocketClient(latus); socket.on('packet', (packet) => { if ('Refresh' === packet.constructor.name) { diff --git a/packages/socket/src/client/socket.js b/packages/socket/src/client/socket.js index 4561727..dafa892 100644 --- a/packages/socket/src/client/socket.js +++ b/packages/socket/src/client/socket.js @@ -5,6 +5,7 @@ import io from 'socket.io-client'; import {Class, compose, EventEmitter} from '@latus/core'; +import normalize from '../normalize'; import {fromName} from '../packet/packets'; import SocketIoParser from '../packet/socket.io-parser'; @@ -85,22 +86,12 @@ export default class SocketClient extends decorate(Class) { this.socket.disconnect(); } - static hydrate(latus, [type, data]) { - const Packets = fromName(latus); - if (!Packets[type]) { - throw new TypeError(`No packet of type '${type}'`); - } - return new Packets[type](data); - } - get id() { return this.socket ? this.socket.id : undefined; } static send(latus, socket, packetOrDehydrated, method) { - const packet = Array.isArray(packetOrDehydrated) - ? this.hydrate(latus, packetOrDehydrated) - : packetOrDehydrated; + const packet = normalize(latus, packetOrDehydrated); debug('sending packet %o', packet); const {id} = packet.constructor; return socket[method](id, packet.data); diff --git a/packages/socket/src/index.js b/packages/socket/src/index.js index 0027a83..2149700 100644 --- a/packages/socket/src/index.js +++ b/packages/socket/src/index.js @@ -1,8 +1,11 @@ +import Bundle from './packet/bundle'; import Refresh from './packet/refresh'; export * from './hooks'; +export {default as normalize} from './normalize'; export {default as Packet, ValidationError} from './packet'; +export {fromId as packetFromId, fromName as packetFromName} from './packet/packets'; export default { hooks: { @@ -11,7 +14,8 @@ export default { connect: [], }), '@latus/socket/packets': (latus) => ({ - Refresh: Refresh(latus), + Bundle: Bundle(latus), + Refresh, }), }, }; diff --git a/packages/socket/src/normalize.js b/packages/socket/src/normalize.js new file mode 100644 index 0000000..76a4526 --- /dev/null +++ b/packages/socket/src/normalize.js @@ -0,0 +1,15 @@ +import {fromName} from './packet/packets'; + +const hydrate = (latus, [type, data]) => { + const Packets = fromName(latus); + if (!Packets[type]) { + throw new TypeError(`No packet of type '${type}'`); + } + return new Packets[type](data); +}; + +export default (latus, packetOrDehydrated) => ( + Array.isArray(packetOrDehydrated) + ? hydrate(latus, packetOrDehydrated) + : packetOrDehydrated +); diff --git a/packages/socket/src/packet/refresh.js b/packages/socket/src/packet/refresh.js index 6e98bc7..f491095 100644 --- a/packages/socket/src/packet/refresh.js +++ b/packages/socket/src/packet/refresh.js @@ -1,3 +1,3 @@ import Packet from './packet'; -export default () => class Refresh extends Packet {}; +export default class Refresh extends Packet {} diff --git a/packages/socket/src/server.js b/packages/socket/src/server.js index fa6b33f..ba7a37f 100644 --- a/packages/socket/src/server.js +++ b/packages/socket/src/server.js @@ -12,7 +12,13 @@ export default { hooks: { '@latus/http/server/up': async (httpServer, latus) => { // eslint-disable-next-line no-param-reassign - latus.config['%packets'] = gather(latus, '@latus/socket/packets', 'id', badPacketsCheck); + latus.config['%packets'] = gather( + latus, + '@latus/socket/packets', + 'id', + 'name', + badPacketsCheck, + ); createSocketServer(httpServer, latus); }, '@latus/repl/context': (latus) => ({ diff --git a/packages/socket/src/socket.js b/packages/socket/src/socket.js index fa50c6a..44a786e 100644 --- a/packages/socket/src/socket.js +++ b/packages/socket/src/socket.js @@ -3,6 +3,7 @@ import {promisify} from 'util'; import {Class, compose, EventEmitter} from '@latus/core'; import D from 'debug'; +import normalize from './normalize'; import {fromName} from './packet/packets'; const debug = D('@latus/socket/server/socket'); @@ -44,14 +45,6 @@ export default class ServerSocket extends decorate(Class) { this.socket.disconnect(true); } - static hydrate(latus, [type, data]) { - const Packets = fromName(latus); - if (!Packets[type]) { - throw new TypeError(`No packet of type '${type}'`); - } - return new Packets[type](data); - } - get id() { return this.socket.id; } @@ -73,9 +66,7 @@ export default class ServerSocket extends decorate(Class) { } static send(latus, socket, packetOrDehydrated, method) { - const packet = Array.isArray(packetOrDehydrated) - ? this.hydrate(latus, packetOrDehydrated) - : packetOrDehydrated; + const packet = normalize(latus, packetOrDehydrated); debug('sending packet %s(%o)', packet.constructor.name, packet.data); const {id} = packet.constructor; return socket[method](id, packet.data);