mirror of https://github.com/keeweb/keeweb
extracted protocolimpl
parent
b4f93adf05
commit
00159d26dc
|
@ -1,6 +1,6 @@
|
|||
import { Launcher } from 'comp/launcher';
|
||||
import { Logger } from 'util/logger';
|
||||
import { ProtocolHandlers, ProtocolImpl } from './protocol-impl';
|
||||
import { ProtocolImpl } from './protocol-impl';
|
||||
import { RuntimeInfo } from 'const/runtime-info';
|
||||
import { AppSettingsModel } from 'models/app-settings-model';
|
||||
import { Features } from 'util/features';
|
||||
|
@ -132,16 +132,7 @@ const BrowserExtensionConnector = {
|
|||
|
||||
const request = pendingBrowserMessages.shift();
|
||||
|
||||
let response;
|
||||
try {
|
||||
const handler = ProtocolHandlers[request.action];
|
||||
if (!handler) {
|
||||
throw new Error(`Handler not found: ${request.action}`);
|
||||
}
|
||||
response = await handler(request, WebConnectionInfo);
|
||||
} catch (e) {
|
||||
response = this.errorToResponse(e, request);
|
||||
}
|
||||
const response = await ProtocolImpl.handleRequest(request, WebConnectionInfo);
|
||||
|
||||
processingBrowserMessage = false;
|
||||
|
||||
|
@ -152,14 +143,6 @@ const BrowserExtensionConnector = {
|
|||
this.processBrowserMessages();
|
||||
},
|
||||
|
||||
errorToResponse(e, request) {
|
||||
return {
|
||||
action: request?.action,
|
||||
error: e.message || 'Unknown error',
|
||||
errorCode: e.code || 0
|
||||
};
|
||||
},
|
||||
|
||||
sendWebResponse(response) {
|
||||
logger.debug('KeeWeb -> Extension', response);
|
||||
response.kwConnect = 'response';
|
||||
|
@ -198,19 +181,15 @@ const BrowserExtensionConnector = {
|
|||
|
||||
async socketRequest(socketId, request) {
|
||||
let result;
|
||||
try {
|
||||
const connectionInfo = connections.get(socketId);
|
||||
if (!connectionInfo) {
|
||||
throw new Error(`Connection not found: ${socketId}`);
|
||||
}
|
||||
const handler = ProtocolHandlers[request.action];
|
||||
if (!handler) {
|
||||
throw new Error(`Handler not found: ${request.action}`);
|
||||
}
|
||||
result = await handler(request, connectionInfo);
|
||||
} catch (e) {
|
||||
result = this.errorToResponse(e, request);
|
||||
|
||||
const connectionInfo = connections.get(socketId);
|
||||
if (connectionInfo) {
|
||||
result = await ProtocolImpl.handleRequest(request, connectionInfo);
|
||||
} else {
|
||||
const message = `Connection not found: ${socketId}`;
|
||||
result = ProtocolImpl.errorToResponse({ message }, request);
|
||||
}
|
||||
|
||||
this.sendSocketResult(socketId, result);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -29,28 +29,6 @@ let logger;
|
|||
let appModel;
|
||||
let sendEvent;
|
||||
|
||||
const ProtocolImpl = {
|
||||
init(vars) {
|
||||
appModel = vars.appModel;
|
||||
logger = vars.logger;
|
||||
sendEvent = vars.sendEvent;
|
||||
|
||||
setupListeners();
|
||||
},
|
||||
|
||||
cleanup() {
|
||||
connectedClients.clear();
|
||||
},
|
||||
|
||||
deleteConnection(connectionId) {
|
||||
for (const client of connectedClients.values()) {
|
||||
if (client.connection.connectionId === connectionId) {
|
||||
connectedClients.delete(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function setupListeners() {
|
||||
Events.on('file-opened', () => {
|
||||
sendEvent({ action: 'database-unlocked' });
|
||||
|
@ -408,4 +386,46 @@ const ProtocolHandlers = {
|
|||
}
|
||||
};
|
||||
|
||||
export { ProtocolHandlers, ProtocolImpl };
|
||||
const ProtocolImpl = {
|
||||
init(vars) {
|
||||
appModel = vars.appModel;
|
||||
logger = vars.logger;
|
||||
sendEvent = vars.sendEvent;
|
||||
|
||||
setupListeners();
|
||||
},
|
||||
|
||||
cleanup() {
|
||||
connectedClients.clear();
|
||||
},
|
||||
|
||||
deleteConnection(connectionId) {
|
||||
for (const client of connectedClients.values()) {
|
||||
if (client.connection.connectionId === connectionId) {
|
||||
connectedClients.delete(client);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
errorToResponse(e, request) {
|
||||
return {
|
||||
action: request?.action,
|
||||
error: e.message || 'Unknown error',
|
||||
errorCode: e.code || 0
|
||||
};
|
||||
},
|
||||
|
||||
async handleRequest(request, connectionInfo) {
|
||||
try {
|
||||
const handler = ProtocolHandlers[request.action];
|
||||
if (!handler) {
|
||||
throw new Error(`Handler not found: ${request.action}`);
|
||||
}
|
||||
return await handler(request, connectionInfo);
|
||||
} catch (e) {
|
||||
return this.errorToResponse(e, request);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export { ProtocolImpl };
|
||||
|
|
Loading…
Reference in New Issue