Skip to content

Commit

Permalink
Added TypeScript packet
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Feb 9, 2025
1 parent 9e21c8f commit 65bbd1e
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ It is suggested to use [socket class](https://github.com/ioBroker/socket-client)
-->

## Changelog
### 3.0.0 (2025-02-08)
### **WORK IN PROGRESS**
* (@GermanBluefox) Adapter was rewritten in TypeScript

### 2.7.0 (2024-11-17)
Expand Down
1 change: 1 addition & 0 deletions dist/lib/socket.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
39 changes: 39 additions & 0 deletions dist/lib/socket.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/lib/socket.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export declare class WsAdapter extends Adapter {
constructor(options?: Partial<AdapterOptions>);
onUnload(callback: () => void): void;
onMessage(obj: ioBroker.Message): void;
checkUser(username: string, password: string, cb: (error: null | Error, result?: {
checkUser: (username: string, password: string, cb: (error: null | Error, result?: {
logged_in: boolean;
}) => void): void;
}) => void) => void;
initWebServer(): void;
main(): Promise<void>;
}
5 changes: 3 additions & 2 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main.js.map

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { SocketWS } from './lib/socketWS';

export interface WsAdapterConfig {
port: number | string;
auth: boolean;
secure: boolean;
bind: string;
ttl: number | string;
certPublic: string;
certPrivate: string;
certChained: string;
defaultUser: string;
leEnabled: boolean;
leUpdate: boolean;
language: ioBroker.Languages;
leCheckPort: number | string;
}

export declare class IOSocketClass {
public ioServer: SocketWS | null;

constructor(server: Server, settings: SocketSettings, adapter: ioBroker.Adapter, store: Store);

getWhiteListIpForAddress(
remoteIp: string,
whiteListSettings: {
[address: string]: WhiteListSettings;
},
): string | null;
publishAll(type: SocketSubscribeTypes, id: string, obj: ioBroker.Object | ioBroker.State | null | undefined): void;
publishFileAll(id: string, fileName: string, size: number | null): void;
publishInstanceMessageAll(sourceInstance: string, messageType: string, sid: string, data: any): void;
sendLog(obj: ioBroker.LogMessage): void;
close(): void;
}
13 changes: 0 additions & 13 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@
"zh-cn": "该适配器允许与ioBroker通信不同的Web应用程序"
},
"news": {
"3.0.0": {
"en": "Adapter was rewritten in TypeScript",
"de": "Adapter wurde in TypeScript neu geschrieben",
"ru": "Адаптер был переписан в TypeScript",
"pt": "Adapter foi reescrito no TypeScript",
"nl": "Adapter is herschreven in TypeScript",
"fr": "Adaptateur a été réécrit dans TypeScript",
"it": "L'adattatore è stato riscritto in TypeScript",
"es": "Adaptador fue reescrito en TipoScript",
"pl": "Adapter został przepisany w TypeScript",
"uk": "Адаптер був записаний у TypeScript",
"zh-cn": "适配器在类型脚本中重写"
},
"2.7.0": {
"en": "Update ws-server library",
"de": "Ws-Server-Bibliothek aktualisieren",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"@iobroker/adapter-core": "^3.2.3",
"@iobroker/socket-classes": "^2.0.5",
"@iobroker/socket-classes": "^2.0.7",
"@iobroker/webserver": "^1.0.8",
"@iobroker/ws-server": "^4.2.4",
"express-session": "^1.18.1"
Expand Down Expand Up @@ -49,6 +49,7 @@
"url": "https://github.com/ioBroker/ioBroker.ws/issues"
},
"main": "dist/main.js",
"types": "dist/types.d.ts",
"files": [
"admin/",
"dist/",
Expand Down
75 changes: 75 additions & 0 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { Server as HttpServer } from 'node:http';
import type { Server as HttpsServer } from 'node:https';

import { SocketIO as WebSocketServer } from '@iobroker/ws-server';
import { SocketWS } from './socketWS';
import {
type Store,
SocketCommon,
type WhiteListSettings,
type SocketSubscribeTypes,
type SocketSettings,
} from '@iobroker/socket-classes';

type Server = HttpServer | HttpsServer;

class Socket {
public ioServer: SocketWS | null;
constructor(
server: Server,
settings: SocketSettings,
adapter: ioBroker.Adapter,
store: Store,
checkUser?: (
user: string,
pass: string,
cb: (
error: Error | null,
result?: {
logged_in: boolean;
},
) => void,
) => void,
) {
this.ioServer = new SocketWS(settings, adapter);
this.ioServer.start(server, WebSocketServer, {
checkUser,
store,
secret: settings.secret,
});
}

getWhiteListIpForAddress(
remoteIp: string,
whiteListSettings: {
[address: string]: WhiteListSettings;
},
): string | null {
return SocketCommon.getWhiteListIpForAddress(remoteIp, whiteListSettings);
}

publishAll(type: SocketSubscribeTypes, id: string, obj: ioBroker.Object | ioBroker.State | null | undefined): void {
return this.ioServer?.publishAll(type, id, obj);
}

publishFileAll(id: string, fileName: string, size: number | null): void {
return this.ioServer?.publishFileAll(id, fileName, size);
}

publishInstanceMessageAll(sourceInstance: string, messageType: string, sid: string, data: any): void {
return this.ioServer?.publishInstanceMessageAll(sourceInstance, messageType, sid, data);
}

sendLog(obj: ioBroker.LogMessage): void {
this.ioServer?.sendLog(obj);
}

close(): void {
if (this.ioServer) {
this.ioServer.close();
this.ioServer = null;
}
}
}

module.exports = Socket;
4 changes: 2 additions & 2 deletions src/lib/socketWS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class SocketWS extends SocketCommon {
sessionId,
(
_err: Error | null,
obj: {
obj?: {
cookie: {
originalMaxAge: number;
expires: string;
Expand Down Expand Up @@ -210,7 +210,7 @@ export class SocketWS extends SocketCommon {
sessionId,
(
_err: Error | null,
obj: {
obj?: {
cookie: {
originalMaxAge: number;
expires: string;
Expand Down
20 changes: 5 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as session from 'express-session';
import { Adapter, type AdapterOptions, commonTools, EXIT_CODES } from '@iobroker/adapter-core'; // Get common adapter utils
import { WebServer } from '@iobroker/webserver';
import { SocketIO, type Socket as WebSocketClient } from '@iobroker/ws-server';
import type { Store } from '@iobroker/socket-classes';
import type {SocketSettings, Store} from '@iobroker/socket-classes';
import type { WsAdapterConfig } from './types';
import { SocketWS } from './lib/socketWS';
import { readFileSync } from 'node:fs';
Expand Down Expand Up @@ -79,7 +79,7 @@ export class WsAdapter extends Adapter {
this.server?.io?.publishInstanceMessageAll(obj.from, obj.message.m, obj.message.s, obj.message.d);
}

checkUser(
checkUser = (
username: string,
password: string,
cb: (
Expand All @@ -88,7 +88,7 @@ export class WsAdapter extends Adapter {
logged_in: boolean;
},
) => void,
): void {
): void => {
username = (username || '')
.toString()
.replace(this.FORBIDDEN_CHARS, '_')
Expand Down Expand Up @@ -237,18 +237,7 @@ export class WsAdapter extends Adapter {
},
);

const settings: {
language?: ioBroker.Languages;
defaultUser?: string;
ttl?: number;
secure?: boolean;
auth?: boolean;
crossDomain?: boolean;
extensions?: (socket: WebSocketClient) => void;
port?: number;
compatibilityV2?: boolean;
forceWebSockets?: boolean;
} = {
const settings: SocketSettings = {
ttl: this.wsConfig.ttl as number,
port: this.wsConfig.port,
secure: this.wsConfig.secure,
Expand All @@ -257,6 +246,7 @@ export class WsAdapter extends Adapter {
forceWebSockets: true, // this is irrelevant for ws
defaultUser: this.wsConfig.defaultUser,
language: this.wsConfig.language,
secret: this.secret,
};

this.server.io = new SocketWS(settings, this);
Expand Down
20 changes: 20 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SocketWS } from './lib/socketWS';

export interface WsAdapterConfig {
port: number | string;
auth: boolean;
Expand All @@ -13,3 +15,21 @@ export interface WsAdapterConfig {
language: ioBroker.Languages;
leCheckPort: number | string;
}

export declare class IOSocketClass {
public ioServer: SocketWS | null;

constructor(server: Server, settings: SocketSettings, adapter: ioBroker.Adapter, store: Store);

getWhiteListIpForAddress(
remoteIp: string,
whiteListSettings: {
[address: string]: WhiteListSettings;
},
): string | null;
publishAll(type: SocketSubscribeTypes, id: string, obj: ioBroker.Object | ioBroker.State | null | undefined): void;
publishFileAll(id: string, fileName: string, size: number | null): void;
publishInstanceMessageAll(sourceInstance: string, messageType: string, sid: string, data: any): void;
sendLog(obj: ioBroker.LogMessage): void;
close(): void;
}
4 changes: 3 additions & 1 deletion tasks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { writeFileSync, readFileSync } = require('node:fs');
const { writeFileSync, readFileSync, copyFileSync} = require('node:fs');

const socket = require.resolve('@iobroker/ws').replace(/\\/g, '/');
writeFileSync(`${__dirname}/dist/lib/socket.io.js`, readFileSync(socket));

copyFileSync(`${__dirname}/src/types.d.ts`, `${__dirname}/dist/types.d.ts`);

0 comments on commit 65bbd1e

Please sign in to comment.