diff --git a/packages/core/src/gather.js b/packages/core/src/gather.js index b1c4d97..ffba7d2 100644 --- a/packages/core/src/gather.js +++ b/packages/core/src/gather.js @@ -6,10 +6,18 @@ const capitalize = (token) => token.substring(0, 1).toUpperCase() + token.substr const debug = D('@latus/core/gather'); export const gatherWithLatus = (context) => (latus) => ( - Object.fromEntries(context.keys().map((path) => [ - basename(path, extname(path)).split('-').map(capitalize).join(''), - context(path).default(latus), - ])) + Object.fromEntries(context.keys().map((path) => { + const M = context(path).default; + if ('function' !== typeof M) { + throw new ReferenceError( + `gatherWithLatus: require(${path}).default is not a function (from: ${context.id})`, + ); + } + return [ + basename(path, extname(path)).split('-').map(capitalize).join(''), + M(latus), + ]; + })) ); export default (latus, type, idAttribute, typeAttribute, check = () => {}) => { diff --git a/packages/react/src/client.js b/packages/react/src/client.js index c66200b..912697f 100644 --- a/packages/react/src/client.js +++ b/packages/react/src/client.js @@ -3,6 +3,10 @@ import {render} from 'react-dom'; import LatusContext from './context'; +export {default as React} from 'react'; +export {default as LatusContext} from './context'; +export {default as useLatus} from './hooks/use-latus'; + export default { hooks: { '@latus/http/client/up': async (latus) => { diff --git a/packages/react/src/hooks/use-latus.js b/packages/react/src/hooks/use-latus.js new file mode 100644 index 0000000..d0780d7 --- /dev/null +++ b/packages/react/src/hooks/use-latus.js @@ -0,0 +1,5 @@ +import {useContext} from 'react'; + +import LatusContext from '../context'; + +export default () => useContext(LatusContext); diff --git a/packages/react/src/index.js b/packages/react/src/index.js index a400cb1..52babe5 100644 --- a/packages/react/src/index.js +++ b/packages/react/src/index.js @@ -1,7 +1,5 @@ export {default as React} from 'react'; -export {default as LatusContext} from './context'; - export default { hooks: { '@latus/core/config': () => ({ diff --git a/packages/socket/src/client/socket.js b/packages/socket/src/client/socket.js index e8337ef..c49d007 100644 --- a/packages/socket/src/client/socket.js +++ b/packages/socket/src/client/socket.js @@ -75,10 +75,10 @@ export default class SocketClient extends decorate(Class) { for (let i = 0; i < Packets.length; i++) { const Packet = Packets[i]; const {id} = Packet; - debug('Registering packet %s(id: %s)', Packet.name, id); + debug('Registering packet %s(id: %s)', Packet.type, id); this.socket.on(id, (data, fn) => { const packet = new Packet(data); - debug('recieved packet %s(%o)', Packet.name, packet.data); + debug('recieved packet %s(%j)', Packet.type, packet.data); this.emit('packet', packet, fn); }); } @@ -94,7 +94,7 @@ export default class SocketClient extends decorate(Class) { static send(latus, socket, packetOrDehydrated, method) { const packet = normalize(latus, packetOrDehydrated); - debug('sending packet %o', packet); + debug('sending packet %s(%j)', packet.constructor.type, packet.data); const {id} = packet.constructor; return socket[method](id, packet.data); } diff --git a/packages/socket/src/hooks/use-socket-packet.js b/packages/socket/src/hooks/use-socket-packet.js index ad1fdee..a3f3a2f 100644 --- a/packages/socket/src/hooks/use-socket-packet.js +++ b/packages/socket/src/hooks/use-socket-packet.js @@ -4,7 +4,7 @@ import useSocket from './use-socket'; const {useEffect} = React; -export default function useSocketPacket(fn, deps) { +export default function useSocketPacket(fn, deps = []) { const socket = useSocket(); useEffect(() => { socket.on('packet', fn); diff --git a/packages/socket/src/hooks/use-socket.js b/packages/socket/src/hooks/use-socket.js index 8b159cb..1ee6d4f 100644 --- a/packages/socket/src/hooks/use-socket.js +++ b/packages/socket/src/hooks/use-socket.js @@ -1,4 +1,4 @@ -import {LatusContext, React} from '@latus/react'; +import {LatusContext, React} from '@latus/react/client'; import {socket} from '../client/socket'; diff --git a/packages/socket/src/index.js b/packages/socket/src/index.js index 99e0a01..1dfc089 100644 --- a/packages/socket/src/index.js +++ b/packages/socket/src/index.js @@ -4,6 +4,7 @@ import badPacketsCheck from './packet/bad-packets-check'; 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 {default as packets} from './packet/packets'; diff --git a/packages/socket/src/packet/acceptor.js b/packages/socket/src/packet/acceptor.js index ebf2f13..d7d549b 100644 --- a/packages/socket/src/packet/acceptor.js +++ b/packages/socket/src/packet/acceptor.js @@ -12,7 +12,7 @@ export default (socket) => async (packet, fn) => { if (error instanceof Error) { fn({ code: error.code || 500, - reason: 'production' === process.env.NODE_ENV ? 'Error' : error.message, + reason: 'production' === process.env.NODE_ENV ? 'error' : error.message, ...('production' === process.env.NODE_ENV ? {} : {stack: error.stack}), }); return; diff --git a/packages/socket/src/packet/bundle.js b/packages/socket/src/packet/bundle.js index ed4ae50..720f4e7 100644 --- a/packages/socket/src/packet/bundle.js +++ b/packages/socket/src/packet/bundle.js @@ -1,4 +1,6 @@ +import normalize from '../normalize'; import Packet from './packet'; +import packets from './packets'; export default (latus) => class BundlePacket extends Packet { @@ -16,7 +18,7 @@ export default (latus) => class BundlePacket extends Packet { // Pack up all the packets. const packedPackets = new Array(packets.length); for (let i = 0; i < packets.length; i++) { - const packet = packets[i]; + const packet = normalize(latus, packets[i]); const Packet_ = packet.constructor; const {id} = Packet_; packedPackets[i] = Packet_.pack({ @@ -47,32 +49,32 @@ export default (latus) => class BundlePacket extends Packet { return this.unpack(buffer).data; } - static unpackData(data) { - const packets = []; + static unpackData(buffer) { + const res = []; let caret = 0; - while (caret < data.length) { + while (caret < buffer.length) { // Read packed length. - const length = data.readUInt32LE(caret); + const length = buffer.readUInt32LE(caret); caret += 4; // Read packed data. TODO: manual blitting sucks... const packedPacket = Buffer.allocUnsafe(length); let i = 0; while (i < length) { - packedPacket.writeUInt8(data.readUInt8(caret++), i++); + packedPacket.writeUInt8(buffer.readUInt8(caret++), i++); } // Lookup packet. const packetId = packedPacket.readUInt8(0); - const {default: Packet} = packets(latus).fromId[packetId]; + const {fromId: {[packetId]: Packet_}} = packets(latus); // Unpack and instantiate the packet. - const unpacked = Packet.unpack(packedPacket); - packets.push(new Packet(unpacked.data)); + const unpacked = Packet_.unpack(packedPacket); + res.push(new Packet_(unpacked.data)); } - return packets; + return res; } static respond({data: packets}, socket) { for (let i = 0; i < packets.length; i++) { - socket.emit('packet', packets[i]); + socket.emit('packet', packets[i], () => {}); } } diff --git a/packages/socket/src/packet/packet.js b/packages/socket/src/packet/packet.js index e9f8d1d..204b57e 100644 --- a/packages/socket/src/packet/packet.js +++ b/packages/socket/src/packet/packet.js @@ -44,7 +44,7 @@ export default class Packet { static unpack(buffer) { const unpacked = this.builder.decode(buffer); - unpacked.data = Packet.unpackData(unpacked.data); + unpacked.data = this.unpackData(unpacked.data); return unpacked; } diff --git a/packages/socket/src/packet/socket.io-parser.js b/packages/socket/src/packet/socket.io-parser.js index 3f5cc99..31531b8 100644 --- a/packages/socket/src/packet/socket.io-parser.js +++ b/packages/socket/src/packet/socket.io-parser.js @@ -47,7 +47,7 @@ const Encoder = (latus) => class Encoder { } catch (error) { const next = new Error(`Couldn't pack ${ - Packet.name + Packet.type }(${ JSON.stringify(packet.data[1], null, 2) }): ${ @@ -95,7 +95,7 @@ const Decoder = (latus) => class Decoder extends decorate(class {}) { }); } catch (error) { - const next = new Error(`Couldn't unpack ${Packet.name}(${view}): ${error.message}`); + const next = new Error(`Couldn't unpack ${Packet.type}(${view}): ${error.message}`); // eslint-disable-next-line no-console console.error(next); throw next; diff --git a/packages/socket/src/socket.js b/packages/socket/src/socket.js index 399c0f0..3a67b52 100644 --- a/packages/socket/src/socket.js +++ b/packages/socket/src/socket.js @@ -24,12 +24,12 @@ export default class ServerSocket extends decorate(Class) { this.socket.leavePromise = promisify(this.socket.leave.bind(this.socket)); const Packets = Object.entries(packets(latus).fromType); for (let i = 0; i < Packets.length; i++) { - const [name, Packet] = Packets[i]; + const [type, Packet] = Packets[i]; const {id} = Packet; - debug('Registering packet %s(id: %s)', name, id); + debug('Registering packet %s(id: %s)', type, id); this.socket.on(id, (data, fn) => { const packet = new Packet(data); - debug('recieved packet %s(%o)', name, data); + debug('recieved packet %s(%j)', type, data); this.emit('packet', packet, fn); }); } @@ -67,7 +67,7 @@ export default class ServerSocket extends decorate(Class) { static send(latus, socket, packetOrDehydrated, method) { const packet = normalize(latus, packetOrDehydrated); - debug('sending packet %s(%o)', packet.constructor.type, packet.data); + debug('sending packet %s(%j)', packet.constructor.type, packet.data); const {id} = packet.constructor; return socket[method](id, packet.data); }