Skip to content

Commit

Permalink
Merge pull request #29 from kalm/v3.2.3
Browse files Browse the repository at this point in the history
v3.2.3
  • Loading branch information
fed135 authored Jan 14, 2020
2 parents 7616b80 + ce0bf2f commit c188225
Show file tree
Hide file tree
Showing 22 changed files with 292 additions and 26 deletions.
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,3 @@ packages/ipc/bin
packages/tcp/bin
packages/udp/bin
packages/ws/bin

# built types reference
packages/kalm/types.d.ts
packages/ipc/types.d.ts
packages/tcp/types.d.ts
packages/udp/types.d.ts
packages/ws/types.d.ts
2 changes: 1 addition & 1 deletion examples/chat_websocket/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Server = kalm.listen({
label: 'server',
port: 8800,
transport: ws(),
routine: kalm.routines.tick(5), // Hz
routine: kalm.routines.tick({ hz: 5 }),
host: '0.0.0.0',
});

Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_pub_sub/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const providers = [
label: 'external',
transport: ws(),
port: 3938,
routine: kalm.routines.tick(120, tickSeed),
routine: kalm.routines.tick({ hz: 120, seed: tickSeed }),
host: '0.0.0.0', // Apply local ip
}),
];
Expand Down
6 changes: 3 additions & 3 deletions examples/typescript/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import kalm from 'kalm';
import {connect, Routines} from 'kalm';
import ws from '@kalm/ws';

const client = kalm.connect({
const client = connect({
transport: ws(),
port: 3938,
routine: kalm.routines.realtime(),
routine: Routines.realtime(),
});

type MyCustomPayload = {
Expand Down
6 changes: 3 additions & 3 deletions examples/typescript/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import kalm from 'kalm';
import {listen, Routines} from 'kalm';
import ws from '@kalm/ws';

const provider = kalm.listen({
const provider = listen({
transport: ws(),
port: 3938,
routine: kalm.routines.tick(5),
routine: Routines.tick({ hz: 5 }),
host: '0.0.0.0',
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kalm",
"private": true,
"version": "3.2.2",
"version": "3.2.3",
"description": "The socket optimizer",
"main": "packages/kalm/bin/kalm.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kalm/ipc",
"version": "3.2.2",
"version": "3.2.3",
"description": "IPC transport for Kalm",
"main": "bin/ipc.js",
"scripts": {
Expand Down
28 changes: 28 additions & 0 deletions packages/ipc/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
declare module '@kalm/ipc' {
interface IPCConfig {
socketTimeout?: number
path?: string
}

interface KalmTransport {
(params: any, emitter: NodeJS.EventEmitter): Socket
}

type Remote = {
host: string
port: number
}

type SocketHandle = NodeJS.Socket

interface Socket {
bind: () => void
remote: (handle: SocketHandle) => Remote
connect: (handle?: SocketHandle) => SocketHandle
stop: () => void
send: (handle: SocketHandle, message: number[] | Buffer) => void
disconnect: (handle: SocketHandle) => void
}

export default function ipc(config?: IPCConfig): (config?: IPCConfig) => Transport;
}
2 changes: 1 addition & 1 deletion packages/kalm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const ws = require('@kalm/ws');
const Server = kalm.listen({
port: 8800,
transport: ws(),
routine: kalm.routines.tick(5), // Hz
routine: kalm.routines.tick({ hz: 5 }), // Sends packets at a frequency of 5 Hz (200ms)
host: '0.0.0.0',
});
```
Expand Down
2 changes: 1 addition & 1 deletion packages/kalm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kalm",
"version": "3.2.2",
"version": "3.2.3",
"description": "The socket optimizer",
"main": "bin/kalm.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion packages/kalm/src/routines/dynamic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Methods -------------------------------------------------------------------*/

export function dynamic(hz: number): KalmRoutine {
export function dynamic({ hz, maxPackets = Infinity }: { hz: number, maxPackets?: number }): KalmRoutine {
if (hz <= 0 || hz > 1000) {
throw new Error(`Unable to set Hertz value of ${hz}. Must be between 0.1e13 and 1000`);
}
Expand All @@ -21,6 +21,11 @@ export function dynamic(hz: number): KalmRoutine {

function add(packet: Buffer): void {
clientEmitter.emit(`${channel}.queueAdd`, { frameId: i, packet: packets.length });
if (packets.length > maxPackets - 1) {
packets.push(packet);
_step();
return;
}
if (timer === null) {
timer = setTimeout(_step, Math.round(1000 / hz));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/kalm/src/routines/tick.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* Methods -------------------------------------------------------------------*/

export function tick(hz: number, seed: number = Date.now()): KalmRoutine {
export function tick({ hz, seed = Date.now() }: { hz: number, seed?: number }): KalmRoutine {
if (hz <= 0 || hz > 1000) {
throw new Error(`Unable to set Hertz value of ${hz}. Must be between 0.1e13 and 1000`);
}

let i: number = 0;

function _delta(): number {
Expand Down
139 changes: 139 additions & 0 deletions packages/kalm/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
declare module 'kalm' {
interface ClientConfig {
label?: string
routine?: KalmRoutine
json?: Boolean
transport?: KalmTransport
port?: number
host?: string
isServer?: boolean
provider?: any
}

interface ProviderConfig {
label?: string
routine?: KalmRoutine
json?: Boolean
transport?: KalmTransport
port?: number
host?: string
}

type Remote = {
host: string
port: number
}

interface Provider extends NodeJS.EventEmitter {
broadcast: (channel: string, message: Serializable) => void
label: string
stop: () => void
connections: Client[]
}

interface Client extends NodeJS.EventEmitter {
write: (channel: string, message: Serializable) => void
destroy: () => void
subscribe: (channel: string, handler: (body: any, frame: Frame) => any) => void
unsubscribe: (channel: string, handler: (body: any, frame: Frame) => any) => void
local: () => Remote
remote: () => Remote
}

type Channel = {
queue: Queue
emitter: NodeJS.EventEmitter
}

type ChannelList = {
[key: string]: Channel
}

type Serializable = Buffer | object | string | null

type UDPSocketHandle = {
socket: any
port: number
host: string
}
type UDPClient = {
client: Client
timeout: NodeJS.Timeout
data: Buffer[]
}
type UDPClientList = {
[key: string]: UDPClient
}

type SocketHandle = NodeJS.Socket | UDPSocketHandle | WebSocket

interface KalmRoutine {
(channel: string, params: any, channelEmitter: NodeJS.EventEmitter, clientEmitter: NodeJS.EventEmitter): Queue
}
interface Queue {
add: (packet: Buffer) => void
size: () => number
flush: () => void
}

interface KalmTransport {
(params: any, emitter: NodeJS.EventEmitter): Socket
}
interface Socket {
bind: () => void
remote: (handle: SocketHandle) => Remote
connect: (handle?: SocketHandle) => SocketHandle
stop: () => void
send: (handle: SocketHandle, message: number[] | Buffer) => void
disconnect: (handle: SocketHandle) => void
}

interface IPCConfig {
socketTimeout?: number
path?: string
}

interface TCPConfig {
socketTimeout?: number
}

interface UDPConfig {
type?: string
localAddr?: string
reuseAddr?: boolean
socketTimeout?: number
connectTimeout?: number
}

interface WSConfig {
cert?: string
key?: string
secure?: boolean
}

type RawFrame = {
frameId: number
channel: string
packets: Buffer[]
payloadBytes: number
}

type Frame = {
client: Client
channel: string
frame: {
id: number
messageIndex: number
payloadBytes: number
payloadMessages: number
}
}

export const listen: (config: ProviderConfig) => Provider;
export const connect: (config: ClientConfig) => Client;
export const Routines: {
tick: (config: { hz: number, seed?: number }) => KalmRoutine
dynamic: (config: { hz: number, maxPackets?: number }) => KalmRoutine
realtime: () => KalmRoutine
};
}
2 changes: 1 addition & 1 deletion packages/tcp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kalm/tcp",
"version": "3.2.2",
"version": "3.2.3",
"description": "TCP transport for Kalm",
"main": "bin/tcp.js",
"scripts": {
Expand Down
27 changes: 27 additions & 0 deletions packages/tcp/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
declare module '@kalm/tcp' {
interface TCPConfig {
socketTimeout?: number
}

interface KalmTransport {
(params: any, emitter: NodeJS.EventEmitter): Socket
}

type Remote = {
host: string
port: number
}

type SocketHandle = NodeJS.Socket

interface Socket {
bind: () => void
remote: (handle: SocketHandle) => Remote
connect: (handle?: SocketHandle) => SocketHandle
stop: () => void
send: (handle: SocketHandle, message: number[] | Buffer) => void
disconnect: (handle: SocketHandle) => void
}

export default function tcp(config?: TCPConfig): (config?: TCPConfig) => Transport;
}
2 changes: 1 addition & 1 deletion packages/udp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kalm/udp",
"version": "3.2.2",
"version": "3.2.3",
"description": "UDP transport for Kalm",
"main": "bin/udp.js",
"scripts": {
Expand Down
35 changes: 35 additions & 0 deletions packages/udp/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
declare module '@kalm/udp' {
interface UDPConfig {
type?: string
localAddr?: string
reuseAddr?: boolean
socketTimeout?: number
connectTimeout?: number
}

interface KalmTransport {
(params: any, emitter: NodeJS.EventEmitter): Socket
}

type Remote = {
host: string
port: number
}

type SocketHandle = {
socket: any
port: number
host: string
}

interface Socket {
bind: () => void
remote: (handle: SocketHandle) => Remote
connect: (handle?: SocketHandle) => SocketHandle
stop: () => void
send: (handle: SocketHandle, message: number[] | Buffer) => void
disconnect: (handle: SocketHandle) => void
}

export default function udp(config?: UDPConfig): (config?: UDPConfig) => Transport;
}
2 changes: 1 addition & 1 deletion packages/ws/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kalm/ws",
"version": "3.2.2",
"version": "3.2.3",
"description": "WebSocket transport for Kalm",
"main": "bin/ws.js",
"scripts": {
Expand Down
Loading

0 comments on commit c188225

Please sign in to comment.