Refactor db and local-cache code
Separated cwmp and ui code into distinct files to enhance clarity and maintainability with varied internal data representations.
This commit is contained in:
1 parent
493f1d2f37
commit
c3954c277f
31 files changed
+1542
-1530
No files matched your search
+28
-42
@@ -22,7 +22,7 @@ import * as crypto from "crypto";
|
||||
import { Socket } from "net";
|
||||
import * as auth from "./auth";
|
||||
import * as config from "./config";
|
||||
import * as common from "./common";
|
||||
import { generateDeviceId, once, setTimeoutPromise } from "./util";
|
||||
import * as soap from "./soap";
|
||||
import * as session from "./session";
|
||||
import {
|
||||
@@ -31,8 +31,19 @@ import {
|
||||
extractParams,
|
||||
} from "./common/expression/util";
|
||||
import * as cache from "./cache";
|
||||
import * as localCache from "./local-cache";
|
||||
import * as db from "./db";
|
||||
import * as localCache from "./cwmp/local-cache";
|
||||
import {
|
||||
clearTasks,
|
||||
deleteFault,
|
||||
deleteOperation,
|
||||
fetchDevice,
|
||||
getDueTasks,
|
||||
getFaults,
|
||||
getOperations,
|
||||
saveDevice,
|
||||
saveFault,
|
||||
saveOperation,
|
||||
} from "./cwmp/db";
|
||||
import * as logger from "./logger";
|
||||
import * as scheduling from "./scheduling";
|
||||
import Path from "./common/path";
|
||||
@@ -59,7 +70,6 @@ import { parseXmlDeclaration } from "./xml-parser";
|
||||
import * as debug from "./debug";
|
||||
import { getRequestOrigin } from "./forwarded";
|
||||
import { getSocketEndpoints } from "./server";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
const gzipPromisified = promisify(zlib.gzip);
|
||||
const deflatePromisified = promisify(zlib.deflate);
|
||||
@@ -773,7 +783,7 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
|
||||
const promises = [];
|
||||
|
||||
promises.push(
|
||||
db.saveDevice(
|
||||
saveDevice(
|
||||
sessionContext.deviceId,
|
||||
sessionContext.deviceData,
|
||||
sessionContext.new,
|
||||
@@ -786,14 +796,14 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
|
||||
saveCache = true;
|
||||
if (sessionContext.operations[k]) {
|
||||
promises.push(
|
||||
db.saveOperation(
|
||||
saveOperation(
|
||||
sessionContext.deviceId,
|
||||
k,
|
||||
sessionContext.operations[k]
|
||||
)
|
||||
);
|
||||
} else {
|
||||
promises.push(db.deleteOperation(sessionContext.deviceId, k));
|
||||
promises.push(deleteOperation(sessionContext.deviceId, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -801,7 +811,7 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
|
||||
if (sessionContext.doneTasks?.length) {
|
||||
saveCache = true;
|
||||
promises.push(
|
||||
db.clearTasks(sessionContext.deviceId, sessionContext.doneTasks)
|
||||
clearTasks(sessionContext.deviceId, sessionContext.doneTasks)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -811,10 +821,10 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
|
||||
if (sessionContext.faults[k]) {
|
||||
sessionContext.faults[k].retries = sessionContext.retries[k];
|
||||
promises.push(
|
||||
db.saveFault(sessionContext.deviceId, k, sessionContext.faults[k])
|
||||
saveFault(sessionContext.deviceId, k, sessionContext.faults[k])
|
||||
);
|
||||
} else {
|
||||
promises.push(db.deleteFault(sessionContext.deviceId, k));
|
||||
promises.push(deleteFault(sessionContext.deviceId, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -890,30 +900,6 @@ async function sendAcsRequest(
|
||||
return writeResponse(sessionContext, res);
|
||||
}
|
||||
|
||||
function once(
|
||||
emitter: EventEmitter,
|
||||
event: string,
|
||||
timeout: number
|
||||
): Promise<unknown[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
reject(new Error(`Event ${event} timed out after ${timeout} ms`));
|
||||
}, timeout);
|
||||
|
||||
emitter.once(event, (...args: unknown[]) => {
|
||||
clearTimeout(timer);
|
||||
resolve(args);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function setTimeoutPromise(delay: number, ref = true): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const timerId = setTimeout(resolve, delay);
|
||||
if (!ref) timerId.unref();
|
||||
});
|
||||
}
|
||||
|
||||
// When socket closes, store active sessions in cache
|
||||
export async function onConnection(socket: Socket): Promise<void> {
|
||||
await once(socket, "close", 300000);
|
||||
@@ -966,7 +952,7 @@ export async function onConnection(socket: Socket): Promise<void> {
|
||||
|
||||
export async function onClientError(err: Error, socket: Socket): Promise<void> {
|
||||
const remoteAddress = getSocketEndpoints(socket).remoteAddress;
|
||||
const cacheSnapshot = await localCache.getCurrentSnapshot();
|
||||
const cacheSnapshot = await localCache.getRevision();
|
||||
const debugEnabled = !!localCache.getConfig(
|
||||
cacheSnapshot,
|
||||
"cwmp.debug",
|
||||
@@ -1021,9 +1007,9 @@ async function getDueTasksAndFaultsAndOperations(
|
||||
}
|
||||
|
||||
const res2 = await Promise.all([
|
||||
db.getDueTasks(deviceId, timestamp),
|
||||
db.getFaults(deviceId),
|
||||
db.getOperations(deviceId),
|
||||
getDueTasks(deviceId, timestamp),
|
||||
getFaults(deviceId),
|
||||
getOperations(deviceId),
|
||||
]);
|
||||
return {
|
||||
tasks: res2[0][0],
|
||||
@@ -1383,7 +1369,7 @@ async function clientError(
|
||||
debugEnabled = sessionContext.debug;
|
||||
deviceId = sessionContext.deviceId;
|
||||
} else {
|
||||
const cacheSnapshot = await localCache.getCurrentSnapshot();
|
||||
const cacheSnapshot = await localCache.getRevision();
|
||||
debugEnabled = !!localCache.getConfig(
|
||||
cacheSnapshot,
|
||||
"cwmp.debug",
|
||||
@@ -1637,9 +1623,9 @@ async function listenerAsync(
|
||||
}
|
||||
|
||||
stats.initiatedSessions += 1;
|
||||
const deviceId = common.generateDeviceId(rpc.cpeRequest.deviceId);
|
||||
const deviceId = generateDeviceId(rpc.cpeRequest.deviceId);
|
||||
|
||||
const cacheSnapshot = await localCache.getCurrentSnapshot();
|
||||
const cacheSnapshot = await localCache.getRevision();
|
||||
|
||||
const _sessionContext = session.init(
|
||||
deviceId,
|
||||
@@ -1679,7 +1665,7 @@ async function listenerAsync(
|
||||
}
|
||||
}
|
||||
|
||||
const parameters = await db.fetchDevice(
|
||||
const parameters = await fetchDevice(
|
||||
_sessionContext.deviceId,
|
||||
_sessionContext.timestamp
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user