Add browser support with Socket.io transport, authentication, performance-optimized IPC, and CI/CD (#925)
This commit is contained in:
@@ -2,14 +2,17 @@ import FileAsync from 'lowdb/adapters/FileAsync'
|
||||
import fs from 'fs-extra'
|
||||
import lowdb from 'lowdb'
|
||||
import path from 'path'
|
||||
import { backendRpc } from '../../events'
|
||||
import { Rpc } from '../../events/EventSystem/Rpc'
|
||||
import { storageClearEvent, storageLoadEvent, storageStoreEvent } from '../../events/StorageEvents'
|
||||
|
||||
export default class ConfigStorage {
|
||||
private file: string
|
||||
private database: any
|
||||
constructor(file: string) {
|
||||
private rpc: Rpc
|
||||
|
||||
constructor(file: string, rpc: Rpc) {
|
||||
this.file = file
|
||||
this.rpc = rpc
|
||||
}
|
||||
|
||||
private async getDb() {
|
||||
@@ -26,13 +29,13 @@ export default class ConfigStorage {
|
||||
}
|
||||
|
||||
public async init() {
|
||||
backendRpc.on(storageStoreEvent, async event => {
|
||||
this.rpc.on(storageStoreEvent, async event => {
|
||||
const db = await this.getDb()
|
||||
await db.set(event.store, event.data).write()
|
||||
return
|
||||
})
|
||||
|
||||
backendRpc.on(storageLoadEvent, async event => {
|
||||
this.rpc.on(storageLoadEvent, async event => {
|
||||
const db = await this.getDb()
|
||||
const data = await db.get(event.store).value()
|
||||
return {
|
||||
@@ -41,7 +44,7 @@ export default class ConfigStorage {
|
||||
}
|
||||
})
|
||||
|
||||
backendRpc.on(storageClearEvent, async event => {
|
||||
this.rpc.on(storageClearEvent, async event => {
|
||||
const db = await this.getDb()
|
||||
const keys = await db.keys().value()
|
||||
for (const key of keys) {
|
||||
|
||||
@@ -4,15 +4,20 @@ import {
|
||||
AddMqttConnection,
|
||||
MqttMessage,
|
||||
addMqttConnectionEvent,
|
||||
backendEvents,
|
||||
makeConnectionMessageEvent,
|
||||
makeConnectionStateEvent,
|
||||
makePublishEvent,
|
||||
removeConnection,
|
||||
} from '../../events'
|
||||
import { EventBusInterface } from '../../events/EventSystem/EventBusInterface'
|
||||
|
||||
export class ConnectionManager {
|
||||
private connections: { [s: string]: DataSource<any> } = {}
|
||||
private backendEvents: EventBusInterface
|
||||
|
||||
constructor(backendEvents: EventBusInterface) {
|
||||
this.backendEvents = backendEvents
|
||||
}
|
||||
|
||||
private handleConnectionRequest = (event: AddMqttConnection) => {
|
||||
const connectionId = event.id
|
||||
@@ -28,12 +33,12 @@ export class ConnectionManager {
|
||||
|
||||
const connectionStateEvent = makeConnectionStateEvent(connectionId)
|
||||
connection.stateMachine.onUpdate.subscribe(state => {
|
||||
backendEvents.emit(connectionStateEvent, state)
|
||||
this.backendEvents.emit(connectionStateEvent, state)
|
||||
})
|
||||
|
||||
connection.connect(options)
|
||||
this.handleNewMessagesForConnection(connectionId, connection)
|
||||
backendEvents.subscribe(makePublishEvent(connectionId), (msg: MqttMessage) => {
|
||||
this.backendEvents.subscribe(makePublishEvent(connectionId), (msg: MqttMessage) => {
|
||||
this.connections[connectionId].publish(msg)
|
||||
})
|
||||
}
|
||||
@@ -49,7 +54,7 @@ export class ConnectionManager {
|
||||
let decoded_payload = null
|
||||
decoded_payload = Base64Message.fromBuffer(buffer)
|
||||
|
||||
backendEvents.emit(messageEvent, {
|
||||
this.backendEvents.emit(messageEvent, {
|
||||
topic,
|
||||
payload: decoded_payload,
|
||||
qos: packet.qos,
|
||||
@@ -60,8 +65,8 @@ export class ConnectionManager {
|
||||
}
|
||||
|
||||
public manageConnections() {
|
||||
backendEvents.subscribe(addMqttConnectionEvent, this.handleConnectionRequest)
|
||||
backendEvents.subscribe(removeConnection, (connectionId: string) => {
|
||||
this.backendEvents.subscribe(addMqttConnectionEvent, this.handleConnectionRequest)
|
||||
this.backendEvents.subscribe(removeConnection, (connectionId: string) => {
|
||||
this.removeConnection(connectionId)
|
||||
})
|
||||
}
|
||||
@@ -69,7 +74,7 @@ export class ConnectionManager {
|
||||
public removeConnection(connectionId: string) {
|
||||
const connection = this.connections[connectionId]
|
||||
if (connection) {
|
||||
backendEvents.unsubscribeAll(makePublishEvent(connectionId))
|
||||
this.backendEvents.unsubscribeAll(makePublishEvent(connectionId))
|
||||
connection.disconnect()
|
||||
delete this.connections[connectionId]
|
||||
connection.stateMachine.onUpdate.removeAllListeners()
|
||||
|
||||
Reference in New Issue
Block a user