Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .npmignore

This file was deleted.

11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [v8.1.1] - 2025-10-28

commit [#](https://github.com/kalm/kalm.js/commits)

### Minor changes (QOL)

- added warning on misnamed config parameters
- added a client `disconnect()` method that is an alias for `destroy()`
- added a `secure` property on ws transport config for simpler wss
- throws an error on channel subscription with non-function handler

## [v8.1.0] - 2025-10-27

commit [#](https://github.com/kalm/kalm.js/commits)
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kalm.js",
"private": true,
"version": "8.1.0",
"version": "8.1.1",
"description": "The socket optimizer",
"main": "packages/kalm/dist/kalm.js",
"scripts": {
Expand Down Expand Up @@ -53,26 +53,26 @@
}
},
"devDependencies": {
"@rollup/plugin-commonjs": "^28.0.6",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-sucrase": "^5.0.2",
"@rollup/plugin-typescript": "^12.1.4",
"@stylistic/eslint-plugin": "^5.2.0",
"@rollup/plugin-commonjs": "^28.0.0",
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-sucrase": "^5.0.0",
"@rollup/plugin-typescript": "^12.3.0",
"@stylistic/eslint-plugin": "^5.5.0",
"@types/jest": "^30.0.0",
"@types/node": "^24.1.0",
"eslint": "^9.32.0",
"@types/node": "^24.9.0",
"eslint": "^9.38.0",
"eslint-plugin-jest": "^29.0.0",
"husky": "^9.1.0",
"jest": "^30.0.0",
"rollup": "^4.46.0",
"jest": "^30.2.0",
"rollup": "^4.52.0",
"rollup-plugin-polyfill-node": "^0.13.0",
"snappy": "^7.3.0",
"socket.io": "^4.8.0",
"socket.io-client": "^4.8.0",
"ts-jest": "^29.4.0",
"tsx": "^4.20.3",
"typescript": "^5.8.0",
"typescript-eslint": "^8.38.0",
"typescript": "^5.9.0",
"typescript-eslint": "^8.46.0",
"ws": "^8.18.3"
}
}
6 changes: 5 additions & 1 deletion packages/ipc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ An IPC transport for the [Kalm](https://github.com/kalm/kalm.js) framework.

```
{
/** The maximum idle time for the connection before it hangs up (default: 30000) */
socketTimeout: 30000,
path: '/tmp/app.socket'
/** The prefix to use for file handler location. Final handler is ${path + port}
Ex: '/tmp/app.socket-9001' for Mac and Linux or 'C:\Windows\Temp\app.socket-9001' on Windows
*/
path: '/tmp/app.socket-'
}
```

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": "8.0.0",
"version": "8.1.1",
"description": "IPC transport for Kalm",
"main": "dist/ipc.js",
"scripts": {
Expand Down
13 changes: 13 additions & 0 deletions packages/ipc/src/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net from 'node:net';
import path from 'node:path';

interface IPCSocket extends net.Socket {
server?: {
Expand All @@ -17,12 +18,24 @@ type IPCConfig = {
path?: string
};

function isValidPathSyntax(filePath) {
try {
path.parse(filePath);
return true;
}
catch (e) {
return e;
}
}

export default function ipc({ socketTimeout = 30000, path = '/tmp/app.socket-' }: IPCConfig = {}): KalmTransport {
if (typeof window !== 'undefined') throw new Error('Cannot use IPC from the browser');

return function socket(params: ClientConfig, emitter: NodeJS.EventEmitter): Socket {
let listener: net.Server;

if (isValidPathSyntax(path + params.port) !== true) throw new Error(`Invalid IPC location, path is not resolvable: ${path + params.port}`);

function bind(): void {
listener = net.createServer(soc => emitter.emit('socket', soc));
listener.on('error', err => emitter.emit('error', err));
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": "8.1.0",
"version": "8.1.1",
"description": "The socket optimizer",
"main": "dist/kalm.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions packages/kalm/src/components/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function Client(params: ClientConfig, emitter: EventEmitter, socket?: any
}

function subscribe(channelName: string, handler: (msg: any, context: Context) => void): void {
if (!handler || typeof handler !== 'function') throw new Error(`Subscribe handler is not a function: ${handler.toString()}`);
_resolveChannel(channelName).handlers.push(handler);
}

Expand All @@ -126,6 +127,7 @@ export function Client(params: ClientConfig, emitter: EventEmitter, socket?: any
instance = Object.assign(emitter, {
write,
destroy,
disconnect: destroy,
subscribe,
unsubscribe,
remote,
Expand Down
5 changes: 5 additions & 0 deletions packages/kalm/src/kalm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function validateOptions(options: ServerConfig): void {
throw new Error('Routine is not valid, it may not have been invoked, see: https://github.com/kalm/kalm.js#documentation');
}
}

// Common misnamed parameters
if (options['hostname'] && !options.host) {
console.warn('Kalm: Option "hostname" does not exist, did you mean "host" ?');
}
}

export function listen(options: ServerConfig): Server {
Expand Down
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": "8.0.0",
"version": "8.1.1",
"description": "UDP transport for Kalm",
"main": "dist/udp.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions packages/udp/src/udp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ type UDPSocketHandle = {
socket: dgram.Socket
port: number
host: string
_timer?: number
};

type UDPConfig = {
type?: dgram.SocketType
localAddr?: string
reuseAddr?: boolean
socketTimeout?: number
socketTimeout?: NodeJS.Timeout
};

export default function udp({ type = 'udp4', localAddr = '0.0.0.0', reuseAddr = false, socketTimeout = 30000 }: UDPConfig = {}): KalmTransport {
Expand Down Expand Up @@ -72,6 +73,7 @@ export default function udp({ type = 'udp4', localAddr = '0.0.0.0', reuseAddr =
}

function disconnect(handle?: UDPSocketHandle): void {
if (handle && handle._timer) clearTimeout(handle._timer);
if (handle && handle.socket) handle.socket = null;
setTimeout(() => emitter.emit('disconnected'), 1);
}
Expand Down Expand Up @@ -124,7 +126,7 @@ export default function udp({ type = 'udp4', localAddr = '0.0.0.0', reuseAddr =
}
}

function resetTimeout(handle) {
function resetTimeout(handle: UDPSocketHandle) {
clearTimeout(handle._timer);
handle._timer = setTimeout(() => {
disconnect(handle);
Expand Down
2 changes: 2 additions & 0 deletions packages/ws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ A websocket transport for the [Kalm](https://github.com/kalm/kalm.js) framework.
cert?: string
/** The key file content for a secure socket connection, both this and `cert` must be set */
key?: string
/** Indicates wether a server or client should use wss:// protocol. Will throw an error if set without cert or key on the server */
secure?: boolean
/** The maximum idle time for the connection before it hangs up (default: 30000) */
socketTimeout: number
}
Expand Down
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": "8.0.0",
"version": "8.1.1",
"description": "WebSocket transport for Kalm",
"main": "dist/ws.js",
"scripts": {
Expand Down
9 changes: 7 additions & 2 deletions packages/ws/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type WSConfig = {
cert?: string
key?: string
socketTimeout?: number
secure?: boolean
};

type WSHandle = WebSocket & {
Expand All @@ -16,12 +17,16 @@ type WSHandle = WebSocket & {
_socket?: any
};

export default function ws({ cert, key, socketTimeout = 30000 }: WSConfig = {}): KalmTransport {
export default function ws({ cert, key, secure, socketTimeout = 30000 }: WSConfig = {}): KalmTransport {
return function socket(params: ClientConfig, emitter: NodeJS.EventEmitter): Socket {
let listener;

async function bind(): Promise<void> {
if (typeof window !== 'undefined') throw new Error('Cannot create a websocket server from the browser');
if (secure) {
if (!cert) throw new Error('Missing cert to create a secure websocket server');
if (!key) throw new Error('Missing key to create a secure websocket server');
}

if (cert && key) {
const https = await import('https');
Expand All @@ -47,7 +52,7 @@ export default function ws({ cert, key, socketTimeout = 30000 }: WSConfig = {}):
}

function connect(handle?: WSHandle): WSHandle {
const protocol: string = (!!cert && !!key) === true ? 'wss' : 'ws';
const protocol: string = ((!!secure) || (!!cert && !!key)) === true ? 'wss' : 'ws';
const connection: WSHandle = handle || new (nativeAPIExists ? WebSocket : WSClient)(`${protocol}://${params.host}:${params.port}`);
connection.binaryType = 'arraybuffer';
const evtType: string = nativeAPIExists ? 'addEventListener' : 'on';
Expand Down
2 changes: 2 additions & 0 deletions packages/ws/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare module '@kalm/ws' {
cert?: string
/** The key file content for a secure socket connection, both this and `cert` must be set */
key?: string
/** Indicates wether a server or client should use wss:// protocol. Will throw an error if set without cert or key on the server */
secure?: boolean
/** The maximum idle time for the connection before it hangs up (default: 30000) */
socketTimeout?: number
}
Expand Down