This commit is contained in:
cha0s 2021-01-14 01:22:40 -06:00
parent 6f16de6887
commit 03424b4db1
13 changed files with 48 additions and 30 deletions

View File

@ -6,10 +6,18 @@ const capitalize = (token) => token.substring(0, 1).toUpperCase() + token.substr
const debug = D('@latus/core/gather'); const debug = D('@latus/core/gather');
export const gatherWithLatus = (context) => (latus) => ( export const gatherWithLatus = (context) => (latus) => (
Object.fromEntries(context.keys().map((path) => [ Object.fromEntries(context.keys().map((path) => {
basename(path, extname(path)).split('-').map(capitalize).join(''), const M = context(path).default;
context(path).default(latus), 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 = () => {}) => { export default (latus, type, idAttribute, typeAttribute, check = () => {}) => {

View File

@ -3,6 +3,10 @@ import {render} from 'react-dom';
import LatusContext from './context'; 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 { export default {
hooks: { hooks: {
'@latus/http/client/up': async (latus) => { '@latus/http/client/up': async (latus) => {

View File

@ -0,0 +1,5 @@
import {useContext} from 'react';
import LatusContext from '../context';
export default () => useContext(LatusContext);

View File

@ -1,7 +1,5 @@
export {default as React} from 'react'; export {default as React} from 'react';
export {default as LatusContext} from './context';
export default { export default {
hooks: { hooks: {
'@latus/core/config': () => ({ '@latus/core/config': () => ({

View File

@ -75,10 +75,10 @@ export default class SocketClient extends decorate(Class) {
for (let i = 0; i < Packets.length; i++) { for (let i = 0; i < Packets.length; i++) {
const Packet = Packets[i]; const Packet = Packets[i];
const {id} = Packet; 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) => { this.socket.on(id, (data, fn) => {
const packet = new Packet(data); 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); this.emit('packet', packet, fn);
}); });
} }
@ -94,7 +94,7 @@ export default class SocketClient extends decorate(Class) {
static send(latus, socket, packetOrDehydrated, method) { static send(latus, socket, packetOrDehydrated, method) {
const packet = normalize(latus, packetOrDehydrated); const packet = normalize(latus, packetOrDehydrated);
debug('sending packet %o', packet); debug('sending packet %s(%j)', packet.constructor.type, packet.data);
const {id} = packet.constructor; const {id} = packet.constructor;
return socket[method](id, packet.data); return socket[method](id, packet.data);
} }

View File

@ -4,7 +4,7 @@ import useSocket from './use-socket';
const {useEffect} = React; const {useEffect} = React;
export default function useSocketPacket(fn, deps) { export default function useSocketPacket(fn, deps = []) {
const socket = useSocket(); const socket = useSocket();
useEffect(() => { useEffect(() => {
socket.on('packet', fn); socket.on('packet', fn);

View File

@ -1,4 +1,4 @@
import {LatusContext, React} from '@latus/react'; import {LatusContext, React} from '@latus/react/client';
import {socket} from '../client/socket'; import {socket} from '../client/socket';

View File

@ -4,6 +4,7 @@ import badPacketsCheck from './packet/bad-packets-check';
import Bundle from './packet/bundle'; import Bundle from './packet/bundle';
import Refresh from './packet/refresh'; import Refresh from './packet/refresh';
export * from './hooks';
export {default as normalize} from './normalize'; export {default as normalize} from './normalize';
export {default as Packet, ValidationError} from './packet'; export {default as Packet, ValidationError} from './packet';
export {default as packets} from './packet/packets'; export {default as packets} from './packet/packets';

View File

@ -12,7 +12,7 @@ export default (socket) => async (packet, fn) => {
if (error instanceof Error) { if (error instanceof Error) {
fn({ fn({
code: error.code || 500, 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}), ...('production' === process.env.NODE_ENV ? {} : {stack: error.stack}),
}); });
return; return;

View File

@ -1,4 +1,6 @@
import normalize from '../normalize';
import Packet from './packet'; import Packet from './packet';
import packets from './packets';
export default (latus) => class BundlePacket extends Packet { export default (latus) => class BundlePacket extends Packet {
@ -16,7 +18,7 @@ export default (latus) => class BundlePacket extends Packet {
// Pack up all the packets. // Pack up all the packets.
const packedPackets = new Array(packets.length); const packedPackets = new Array(packets.length);
for (let i = 0; i < packets.length; i++) { for (let i = 0; i < packets.length; i++) {
const packet = packets[i]; const packet = normalize(latus, packets[i]);
const Packet_ = packet.constructor; const Packet_ = packet.constructor;
const {id} = Packet_; const {id} = Packet_;
packedPackets[i] = Packet_.pack({ packedPackets[i] = Packet_.pack({
@ -47,32 +49,32 @@ export default (latus) => class BundlePacket extends Packet {
return this.unpack(buffer).data; return this.unpack(buffer).data;
} }
static unpackData(data) { static unpackData(buffer) {
const packets = []; const res = [];
let caret = 0; let caret = 0;
while (caret < data.length) { while (caret < buffer.length) {
// Read packed length. // Read packed length.
const length = data.readUInt32LE(caret); const length = buffer.readUInt32LE(caret);
caret += 4; caret += 4;
// Read packed data. TODO: manual blitting sucks... // Read packed data. TODO: manual blitting sucks...
const packedPacket = Buffer.allocUnsafe(length); const packedPacket = Buffer.allocUnsafe(length);
let i = 0; let i = 0;
while (i < length) { while (i < length) {
packedPacket.writeUInt8(data.readUInt8(caret++), i++); packedPacket.writeUInt8(buffer.readUInt8(caret++), i++);
} }
// Lookup packet. // Lookup packet.
const packetId = packedPacket.readUInt8(0); const packetId = packedPacket.readUInt8(0);
const {default: Packet} = packets(latus).fromId[packetId]; const {fromId: {[packetId]: Packet_}} = packets(latus);
// Unpack and instantiate the packet. // Unpack and instantiate the packet.
const unpacked = Packet.unpack(packedPacket); const unpacked = Packet_.unpack(packedPacket);
packets.push(new Packet(unpacked.data)); res.push(new Packet_(unpacked.data));
} }
return packets; return res;
} }
static respond({data: packets}, socket) { static respond({data: packets}, socket) {
for (let i = 0; i < packets.length; i++) { for (let i = 0; i < packets.length; i++) {
socket.emit('packet', packets[i]); socket.emit('packet', packets[i], () => {});
} }
} }

View File

@ -44,7 +44,7 @@ export default class Packet {
static unpack(buffer) { static unpack(buffer) {
const unpacked = this.builder.decode(buffer); const unpacked = this.builder.decode(buffer);
unpacked.data = Packet.unpackData(unpacked.data); unpacked.data = this.unpackData(unpacked.data);
return unpacked; return unpacked;
} }

View File

@ -47,7 +47,7 @@ const Encoder = (latus) => class Encoder {
} }
catch (error) { catch (error) {
const next = new Error(`Couldn't pack ${ const next = new Error(`Couldn't pack ${
Packet.name Packet.type
}(${ }(${
JSON.stringify(packet.data[1], null, 2) JSON.stringify(packet.data[1], null, 2)
}): ${ }): ${
@ -95,7 +95,7 @@ const Decoder = (latus) => class Decoder extends decorate(class {}) {
}); });
} }
catch (error) { 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 // eslint-disable-next-line no-console
console.error(next); console.error(next);
throw next; throw next;

View File

@ -24,12 +24,12 @@ export default class ServerSocket extends decorate(Class) {
this.socket.leavePromise = promisify(this.socket.leave.bind(this.socket)); this.socket.leavePromise = promisify(this.socket.leave.bind(this.socket));
const Packets = Object.entries(packets(latus).fromType); const Packets = Object.entries(packets(latus).fromType);
for (let i = 0; i < Packets.length; i++) { for (let i = 0; i < Packets.length; i++) {
const [name, Packet] = Packets[i]; const [type, Packet] = Packets[i];
const {id} = Packet; 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) => { this.socket.on(id, (data, fn) => {
const packet = new Packet(data); const packet = new Packet(data);
debug('recieved packet %s(%o)', name, data); debug('recieved packet %s(%j)', type, data);
this.emit('packet', packet, fn); this.emit('packet', packet, fn);
}); });
} }
@ -67,7 +67,7 @@ export default class ServerSocket extends decorate(Class) {
static send(latus, socket, packetOrDehydrated, method) { static send(latus, socket, packetOrDehydrated, method) {
const packet = normalize(latus, packetOrDehydrated); 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; const {id} = packet.constructor;
return socket[method](id, packet.data); return socket[method](id, packet.data);
} }