TypeScript!

This commit is contained in:
Zaid Abdulla committed 2019-02-20 03:36:52 -08:00
1 parent ee8c43747e
commit 5d3c81cb23
105 files changed
+12054 -1901

No files matched your search

+20 -19
View File
@@ -16,19 +16,18 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import crypto from "crypto";
import dgram from "dgram";
import URL from "url";
import http from "http";
import * as crypto from "crypto";
import * as dgram from "dgram";
import * as URL from "url";
import * as http from "http";
import * as config from "./config";
import * as db from "./db";
import * as common from "./common";
import * as auth from "./auth";
import * as cache from "./cache";
function udpConReq(address, un, key, callback) {
function udpConReq(address, un, key, callback): boolean {
if (!address) return false;
let [host, port] = address.split(":", 2);
@@ -53,7 +52,9 @@ function udpConReq(address, un, key, callback) {
);
const client = dgram.createSocket({ type: "udp4", reuseAddr: true });
const UDP_CONNECTION_REQUEST_PORT = config.get("UDP_CONNECTION_REQUEST_PORT");
const UDP_CONNECTION_REQUEST_PORT = +config.get(
"UDP_CONNECTION_REQUEST_PORT"
);
if (UDP_CONNECTION_REQUEST_PORT)
// When a device is NAT'ed, the UDP Connection Request must originate from
@@ -65,7 +66,7 @@ function udpConReq(address, un, key, callback) {
client.bind({ port: UDP_CONNECTION_REQUEST_PORT, exclusive: true });
let count = 3;
const func = err => {
const func = (err: Error): void => {
if (err || --count <= 0) {
client.close();
return void callback(err);
@@ -83,8 +84,8 @@ function httpConReq(
allowBasicAuth,
timeout,
callback
) {
const options = URL.parse(url);
): void {
const options: http.RequestOptions = URL.parse(url);
if (options.protocol !== "http:") {
return void callback(
new Error("Invalid connection request URL or protocol")
@@ -92,7 +93,7 @@ function httpConReq(
}
options.agent = new http.Agent({ maxSockets: 1 });
function statusToError(statusCode) {
function statusToError(statusCode): Error {
switch (statusCode) {
case 200:
case 204:
@@ -112,7 +113,7 @@ function httpConReq(
const authHeader = auth.parseAuthHeader(
response.headers["www-authenticate"]
);
if (authHeader.method === "Basic") {
if (authHeader["method"] === "Basic") {
if (!allowBasicAuth) {
request.abort();
return void callback(
@@ -122,7 +123,7 @@ function httpConReq(
options.headers = {
Authorization: auth.basic(username || "", password || "")
};
} else if (authHeader.method === "Digest") {
} else if (authHeader["method"] === "Digest") {
options.headers = {
Authorization: auth.digest(
username || "",
@@ -187,7 +188,7 @@ function httpConReq(
});
}
export function connectionRequest(deviceId, callback) {
export function connectionRequest(deviceId, callback): void {
const CONNECTION_REQUEST_TIMEOUT = config.get(
"CONNECTION_REQUEST_TIMEOUT",
deviceId
@@ -235,7 +236,7 @@ export function connectionRequest(deviceId, callback) {
if (managementServer.ConnectionRequestPassword)
password = managementServer.ConnectionRequestPassword._value;
const conReq = () => {
const conReq = (): void => {
const udpSent = udpConReq(
udpConnectionRequestAddress,
username,
@@ -284,7 +285,7 @@ export function connectionRequest(deviceId, callback) {
});
}
export function watchTask(deviceId, taskId, timeout, callback) {
export function watchTask(deviceId, taskId, timeout, callback): void {
setTimeout(() => {
db.tasksCollection.findOne(
{ _id: taskId },
@@ -313,7 +314,7 @@ export function watchTask(deviceId, taskId, timeout, callback) {
}, 500);
}
function sanitizeTask(task, callback) {
function sanitizeTask(task, callback): void {
task.timestamp = new Date(task.timestamp || Date.now());
if (task.expiry) {
if (common.typeOf(task.expiry) === common.DATE_TYPE || isNaN(task.expiry))
@@ -324,7 +325,7 @@ function sanitizeTask(task, callback) {
callback(task);
}
export function insertTasks(tasks, callback) {
export function insertTasks(tasks, callback): void {
if (tasks && common.typeOf(tasks) !== common.ARRAY_TYPE) tasks = [tasks];
else if (!tasks || tasks.length === 0)
return void callback(null, tasks || []);
@@ -344,7 +345,7 @@ export function insertTasks(tasks, callback) {
}
}
export function deleteDevice(deviceId, callback) {
export function deleteDevice(deviceId, callback): void {
db.tasksCollection.deleteMany({ device: deviceId }, err => {
if (err) return void callback(err);
db.devicesCollection.deleteOne({ _id: deviceId }, err => {
+16 -10
View File
@@ -16,11 +16,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { createHash } from "crypto";
export function parseAuthHeader(authHeader) {
export function parseAuthHeader(authHeader): {} {
authHeader = authHeader.trim();
const method = authHeader.split(" ", 1)[0];
const res = { method: method };
@@ -53,11 +52,18 @@ export function parseAuthHeader(authHeader) {
return res;
}
export function basic(username, password) {
export function basic(username, password): string {
return "Basic " + Buffer.from(`${username}:${password}`).toString("base64");
}
export function digest(username, password, uri, httpMethod, body, authHeader) {
export function digest(
username,
password,
uri,
httpMethod,
body,
authHeader
): string {
const cnonce = "0a4f113b";
const nc = "00000001";
let qop;
@@ -68,7 +74,7 @@ export function digest(username, password, uri, httpMethod, body, authHeader) {
else qop = authHeader.qop;
}
let ha1 = createHash("md5");
const ha1 = createHash("md5");
ha1
.update(username)
.update(":")
@@ -76,19 +82,19 @@ export function digest(username, password, uri, httpMethod, body, authHeader) {
.update(":")
.update(password);
// TODO support "MD5-sess" algorithm directive
ha1 = ha1.digest("hex");
const ha1d = ha1.digest("hex");
let ha2 = createHash("md5");
const ha2 = createHash("md5");
ha2
.update(httpMethod)
.update(":")
.update(uri);
if (qop === "auth-int") ha2.update(":").update(body);
ha2 = ha2.digest("hex");
const ha2d = ha2.digest("hex");
const hash = createHash("md5");
hash
.update(ha1)
.update(ha1d)
.update(":")
.update(authHeader.nonce);
if (qop) {
@@ -100,7 +106,7 @@ export function digest(username, password, uri, httpMethod, body, authHeader) {
.update(":")
.update(qop);
}
hash.update(":").update(ha2);
hash.update(":").update(ha2d);
let authString = `Digest username="${username}"`;
authString += `,realm="${authHeader.realm}"`;
+31 -14
View File
@@ -16,19 +16,18 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { MongoClient } from "mongodb";
import * as config from "./config";
const MAX_CACHE_TTL = config.get("MAX_CACHE_TTL");
const MAX_CACHE_TTL = +config.get("MAX_CACHE_TTL");
let mongoClient;
let mongoCollection;
let mongoTimeOffset = 0;
export function connect(callback) {
const MONGODB_CONNECTION_URL = config.get("MONGODB_CONNECTION_URL");
export function connect(callback): void {
const MONGODB_CONNECTION_URL = "" + config.get("MONGODB_CONNECTION_URL");
MongoClient.connect(
MONGODB_CONNECTION_URL,
@@ -51,11 +50,11 @@ export function connect(callback) {
);
}
export function disconnect() {
export function disconnect(): void {
if (mongoClient) mongoClient.close();
}
export function get(key, callback) {
export function get(key, callback): void {
const expire = new Date(Date.now() - mongoTimeOffset);
if (Array.isArray(key)) {
mongoCollection.find({ _id: { $in: key } }).toArray((err, res) => {
@@ -83,21 +82,39 @@ export function get(key, callback) {
}
}
export function del(key, callback) {
export function del(key, callback): void {
if (Array.isArray(key))
mongoCollection.deleteMany({ _id: { $in: key } }, callback);
else mongoCollection.deleteOne({ _id: key }, callback);
}
export function set(key, value, ttl, callback) {
if (!callback && typeof v === "function") {
export function set(
key: string,
value: string | number,
callback: (err?: Error) => void
): void;
export function set(
key: string,
value: string | number,
ttl: number,
callback: (err?: Error) => void
): void;
export function set(
key: string,
value: string | number,
ttl: number | ((err?: Error) => void),
callback?: (err?: Error) => void
): void {
if (!callback && typeof ttl === "function") {
callback = ttl;
ttl = null;
}
if (!ttl) ttl = MAX_CACHE_TTL;
const expire = new Date(Date.now() - mongoTimeOffset + ttl * 1000);
const expire = new Date(
Date.now() - mongoTimeOffset + (ttl as number) * 1000
);
mongoCollection.replaceOne(
{ _id: key },
{ _id: key, value: value, expire: expire },
@@ -106,7 +123,7 @@ export function set(key, value, ttl, callback) {
);
}
export function pop(key, callback) {
export function pop(key, callback): void {
mongoCollection.findOneAndDelete({ _id: key }, (err, res) => {
if (err || !res["value"]) return void callback(err);
@@ -117,12 +134,12 @@ export function pop(key, callback) {
});
}
export function lock(lockName, ttl, callback) {
export function lock(lockName, ttl, callback): void {
const token = Math.random()
.toString(36)
.slice(2);
function unlockOrExtend(extendTtl) {
function unlockOrExtend(extendTtl): void {
if (!extendTtl) {
mongoCollection.deleteOne({ _id: lockName, value: token }, (err, res) => {
if (err || res["result"]["n"] !== 1)
@@ -135,7 +152,7 @@ export function lock(lockName, ttl, callback) {
{ expire: expire },
(err, res) => {
if (err || res["result"]["n"] !== 1)
throw err | new Error("Lock expired");
throw err || new Error("Lock expired");
}
);
}
+6 -5
View File
@@ -16,7 +16,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import cluster from "cluster";
import { cpus } from "os";
@@ -25,10 +24,12 @@ import * as logger from "./logger";
let respawnTimestamp = 0;
let tooManyCrashesTimestamp = 0;
function restartWorker(worker, code, signal) {
function restartWorker(worker, code, signal): void {
const msg = {
message: "Worker died",
pid: worker.process.pid
pid: worker.process.pid,
exitCode: null,
signal: null
};
if (code != null) msg.exitCode = code;
@@ -63,7 +64,7 @@ function restartWorker(worker, code, signal) {
}, respawnTimestamp - now);
}
export function start(workerCount, servicePort, serviceAddress) {
export function start(workerCount, servicePort, serviceAddress): void {
cluster.on("listening", (worker, address) => {
if (
(address.addressType === 4 || address.addressType === 6) &&
@@ -86,7 +87,7 @@ export function start(workerCount, servicePort, serviceAddress) {
for (let i = 0; i < workerCount; ++i) cluster.fork();
}
export function stop() {
export function stop(): void {
cluster.removeListener("exit", restartWorker);
for (const pid in cluster.workers) cluster.workers[pid].kill();
}
+15 -10
View File
@@ -16,7 +16,8 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { ParamPath } from "./types";
export const UNDEFINED_TYPE = "[object Undefined]";
export const NULL_TYPE = "[object Null]";
@@ -28,11 +29,11 @@ export const OBJECT_TYPE = "[object Object]";
export const REGEXP_TYPE = "[object RegExp]";
export const DATE_TYPE = "[object Date]";
export const typeOf = obj => Object.prototype.toString.call(obj);
export const typeOf = (obj): string => Object.prototype.toString.call(obj);
export function generateDeviceId(deviceIdStruct) {
export function generateDeviceId(deviceIdStruct): string {
// Escapes everything except alphanumerics and underscore
function esc(str) {
function esc(str): string {
return str.replace(/[^A-Za-z0-9_]/g, chr => {
const buf = Buffer.from(chr, "utf8");
let rep = "";
@@ -54,11 +55,12 @@ export function generateDeviceId(deviceIdStruct) {
return esc(deviceIdStruct["OUI"]) + "-" + esc(deviceIdStruct["SerialNumber"]);
}
export function parseAlias(pattern, start, res) {
export function parseAlias(pattern, start, res): number {
const aliases = [];
let i = start;
while (i < pattern.length && pattern[i] !== "]") {
const alias = [];
/* eslint-disable-next-line @typescript-eslint/no-use-before-define */
let j = (i = parsePath(pattern, i, alias) + 1);
while (pattern[j] !== "]" && pattern[j] !== ",") {
if (pattern[j] === '"' && i === j) {
@@ -87,7 +89,7 @@ export function parseAlias(pattern, start, res) {
}
// Sort to ensure identical expressions have idential string representation
function srt(a, b) {
function srt(a, b): number {
const jMax = Math.min(a.length, b.length);
for (let j = 0; j < jMax; j += 2) {
const kMax = Math.min(a[j].length, b[j].length);
@@ -126,8 +128,11 @@ export function parseAlias(pattern, start, res) {
return i;
}
export function parsePath(pattern, start, res) {
const path = [];
/* eslint-disable @typescript-eslint/no-use-before-define */
export function parsePath(pattern, start, res): number;
export function parsePath(pattern, start?): ParamPath;
export function parsePath(pattern, start?, res?): number | ParamPath {
const path = [] as ParamPath;
path.wildcard = 0;
path.alias = 0;
let i = start || 0;
@@ -163,7 +168,7 @@ export function parsePath(pattern, start, res) {
return i;
}
export function addPathMeta(path) {
export function addPathMeta(path): ParamPath {
if (path.alias != null && path.wildcard != null) return path;
path.alias = 0;
@@ -183,6 +188,6 @@ export function addPathMeta(path) {
}
// Source: http://stackoverflow.com/a/6969486
export function escapeRegExp(str) {
export function escapeRegExp(str): string {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
}
@@ -1,20 +1,27 @@
"use strict";
import pass from "./validators/pass";
import test from "./validators/test";
import { PermissionSet } from "../types";
const validators = { pass, test };
export default class Authorizer {
constructor(permissionSets) {
private permissionSets: PermissionSet[];
private validatorCache: WeakMap<
object,
(mutationType, mutation, any) => boolean
>;
private hasAccessCache: Map<string, boolean>;
public constructor(permissionSets) {
this.permissionSets = permissionSets;
this.validatorCache = new WeakMap();
this.hasAccessCache = {};
this.hasAccessCache = new Map();
}
hasAccess(resourceType, access) {
public hasAccess(resourceType, access): boolean {
const cacheKey = `${resourceType}-${access}`;
if (cacheKey in this.hasAccessCache) return this.hasAccessCache[cacheKey];
if (this.hasAccessCache.has(cacheKey))
return this.hasAccessCache.get(cacheKey);
let has = false;
for (const permissionSet of this.permissionSets) {
@@ -28,11 +35,14 @@ export default class Authorizer {
}
}
this.hasAccessCache[cacheKey] = has;
this.hasAccessCache.set(cacheKey, has);
return has;
}
getValidator(resourceType, resource) {
public getValidator(
resourceType,
resource
): (mutationType: string, mutation: any, args: any) => boolean {
if (this.validatorCache.has(resource))
return this.validatorCache.get(resource);
@@ -53,7 +63,11 @@ export default class Authorizer {
}
}
const validator = (mutationType, mutation, any) => {
const validator = (
mutationType: string,
mutation: any,
any: any
): boolean => {
let valid = false;
for (const [k, v] of Object.entries(funcs)) {
if (v) {
@@ -1,8 +1,9 @@
"use strict";
import { map, parseLikePattern } from "./expression-parser";
import { Expression } from "../types";
function or(cnf1, cnf2) {
type CNF = number[][];
function or(cnf1: CNF, cnf2: CNF): CNF {
if (cnf1.length === 0) return cnf2;
else if (cnf2.length === 0) return cnf1;
@@ -19,7 +20,30 @@ function or(cnf1, cnf2) {
return res;
}
export function booleanCnf(exp) {
function likePatternIncludes(pat1, pat2, idx1 = 0, idx2 = 0): boolean {
while (idx1 < pat1.length && idx2 < pat2.length) {
if (pat1[idx1] === "\\%") {
for (let i = idx2; i <= pat2.length; ++i)
if (likePatternIncludes(pat1, pat2, idx1 + 1, i)) return true;
return false;
} else if (pat2[idx2] === "\\%") {
if (pat1[idx1] !== "\\%") return false;
} else if (pat1[idx1] === "\\_") {
// Ignore
} else if (pat2[idx2] === "\\_") {
return false;
} else if (pat2[idx2] !== pat1[idx1]) {
return false;
}
++idx1;
++idx2;
}
if (idx1 === pat1.length && idx2 === pat2.length) return true;
return false;
}
export function booleanCnf(exp: Expression): { vars: number; clauses: CNF } {
const ranges = new Map();
const likePatterns = new Map();
map(exp, e => {
@@ -51,15 +75,20 @@ export function booleanCnf(exp) {
});
for (const [k, v] of ranges) {
if (k.endsWith(":number"))
ranges.set(k, Array.from(v).sort((a, b) => a - b));
else ranges.set(k, Array.from(v).sort());
if (k.endsWith(":number")) {
ranges.set(
k,
Array.from(v as number[]).sort((a: number, b: number) => a - b)
);
} else {
ranges.set(k, Array.from(v).sort());
}
}
const expressions = new Map();
const mutuallyExclusive = new Map();
const expressions: Map<string, number> = new Map();
const mutuallyExclusive: Map<string, Set<number>> = new Map();
function getVariable(lhs, op, rhs = "") {
function getVariable(lhs, op, rhs = ""): number {
const key = `${lhs}:${op}:${rhs}`;
let f = expressions.get(key);
if (!f) {
@@ -72,7 +101,13 @@ export function booleanCnf(exp) {
return f;
}
function getComparisonVariables(lhs, from, fromInclusive, to, toInclusive) {
function getComparisonVariables(
lhs,
from,
fromInclusive,
to,
toInclusive
): number[] {
const type = from != null ? typeof from : typeof to;
const range = ranges.get(`${lhs}:${type}`);
const vars = [];
@@ -109,7 +144,7 @@ export function booleanCnf(exp) {
});
}
function getLikeVariables(lhs, pat, esc) {
function getLikeVariables(lhs, pat, esc): number[] {
const vars = [];
const pats = likePatterns.get(lhs);
pat = pats.get(`${esc || ""}:${pat}`);
@@ -125,7 +160,11 @@ export function booleanCnf(exp) {
});
}
const comparisonOperators = {
interface ComparisonOperators {
[name: string]: (v: string) => [string, boolean, string, boolean];
}
const comparisonOperators: ComparisonOperators = {
"=": v => [v, true, v, true],
">": v => [v, false, null, null],
">=": v => [v, true, null, null],
@@ -133,7 +172,7 @@ export function booleanCnf(exp) {
"<=": v => [null, null, v, true]
};
function recursive(clause, negate) {
function recursive(clause, negate): CNF {
let op = clause[0];
if ((op === "AND" && !negate) || (op === "OR" && negate)) {
@@ -218,33 +257,10 @@ export function booleanCnf(exp) {
const cnf = recursive(exp, false);
for (const m of mutuallyExclusive.values()) {
const ar = Array.from(m);
const ar: number[] = Array.from(m);
for (let i = 0; i < ar.length; ++i)
for (let j = i + 1; j < ar.length; ++j) cnf.push([0 - ar[i], 0 - ar[j]]);
}
return { vars: expressions.size, clauses: cnf };
}
function likePatternIncludes(pat1, pat2, idx1 = 0, idx2 = 0) {
while (idx1 < pat1.length && idx2 < pat2.length) {
if (pat1[idx1] === "\\%") {
for (let i = idx2; i <= pat2.length; ++i)
if (likePatternIncludes(pat1, pat2, idx1 + 1, i)) return true;
return false;
} else if (pat2[idx2] === "\\%") {
if (pat1[idx1] !== "\\%") return false;
} else if (pat1[idx1] === "\\_") {
// Ignore
} else if (pat2[idx2] === "\\_") {
return false;
} else if (pat2[idx2] !== pat1[idx1]) {
return false;
}
++idx1;
++idx2;
}
if (idx1 === pat1.length && idx2 === pat2.length) return true;
return false;
}
@@ -1,9 +1,8 @@
"use strict";
import parsimmon from "parsimmon";
import * as parsimmon from "parsimmon";
import { Expression } from "../types";
// Turn escaped characters into real ones (e.g. "\\n" becomes "\n").
function interpretEscapes(str) {
function interpretEscapes(str): string {
const escapes = {
b: "\b",
f: "\f",
@@ -22,7 +21,7 @@ function interpretEscapes(str) {
});
}
export function map(exp, callback) {
export function map(exp, callback): Expression {
if (!Array.isArray(exp)) return callback(exp);
let clone;
@@ -37,7 +36,7 @@ export function map(exp, callback) {
return callback(clone || exp);
}
function binaryLeft(operatorsParser, nextParser) {
function binaryLeft(operatorsParser, nextParser): parsimmon.Parser<{}> {
return parsimmon.seqMap(
nextParser,
parsimmon.seq(operatorsParser, nextParser).many(),
@@ -257,7 +256,7 @@ const lang = parsimmon.createLanguage({
);
},
Expression: function(r) {
function unary(operatorsParser, nextParser) {
function unary(operatorsParser, nextParser): parsimmon.Parser<{}> {
return parsimmon.seq(operatorsParser, nextParser).or(nextParser);
}
@@ -271,12 +270,12 @@ const lang = parsimmon.createLanguage({
}
});
export function parse(str) {
export function parse(str): Expression {
if (!str) return null;
return lang.Expression.tryParse(str);
}
export function stringify(exp, level = 0) {
export function stringify(exp, level = 0): string {
if (!Array.isArray(exp)) return JSON.stringify(exp);
const opLevels = {
@@ -302,7 +301,7 @@ export function stringify(exp, level = 0) {
const op = exp[0].toUpperCase();
function wrap(e) {
function wrap(e): string {
if (opLevels[op] <= level) return `(${e})`;
else return e;
}
@@ -360,7 +359,7 @@ export function stringify(exp, level = 0) {
}
}
export function parseLikePattern(pat, esc) {
export function parseLikePattern(pat, esc): string[] {
const chars = pat.split("");
for (let i = 0; i < chars.length; ++i) {
@@ -1,15 +1,14 @@
"use strict";
import { map, parseLikePattern } from "./expression-parser";
import { booleanCnf } from "./expression-cnf";
import { naiveDpll } from "./sat-solver";
import { Expression } from "../types";
const isArray = Array.isArray;
const regExpCache = new WeakMap();
const REDUCE_SKIP = {};
function reduce(exp, callback) {
function reduce(exp, callback): Expression {
let loop = true;
while (loop) {
loop = false;
@@ -26,8 +25,45 @@ function reduce(exp, callback) {
return exp;
}
export function evaluate(exp, obj, now, cb) {
function getRegExp(pat, esc) {
export function likePatternToRegExp(pat, esc = "", flags = ""): RegExp {
const convChars = {
"-": "\\-",
"/": "\\/",
"\\": "\\/",
"^": "\\^",
$: "\\$",
"*": "\\*",
"+": "\\+",
"?": "\\?",
".": "\\.",
"(": "\\(",
")": "\\)",
"|": "\\|",
"[": "\\[",
"]": "\\]",
"{": "\\{",
"}": "\\}",
"\\%": ".*",
"\\_": "."
};
let chars = parseLikePattern(pat, esc);
if (!chars.length) return new RegExp("^$", flags);
chars = chars.map(c => convChars[c] || c);
chars[0] = chars[0] === ".*" ? "" : "^" + chars[0];
const l = chars.length - 1;
chars[l] = [".*", ""].includes(chars[l]) ? "" : chars[l] + "$";
return new RegExp(chars.join(""), flags);
}
export function evaluate(
exp,
obj,
now: number,
cb?: Function
): string | number | boolean | null;
export function evaluate(exp, obj?, now?: number, cb?: Function): Expression;
export function evaluate(exp, obj?, now?: number, cb?: Function): Expression {
function getRegExp(pat, esc): RegExp {
const k = `${esc || ""}:${pat}`;
let c = regExpCache.get(exp);
if (!c) regExpCache.set(exp, (c = {}));
@@ -162,11 +198,11 @@ export function evaluate(exp, obj, now, cb) {
});
}
export function and(exp1, exp2) {
export function and(exp1, exp2): Expression {
if (!isArray(exp1)) return exp1 ? exp2 : exp1;
if (!isArray(exp2)) return exp2 ? exp1 : exp2;
let res = ["AND"];
let res: Expression = ["AND"];
if (exp1[0] === "AND") res = res.concat(exp1.slice(1));
else res.push(exp1);
@@ -177,11 +213,11 @@ export function and(exp1, exp2) {
return res;
}
export function or(exp1, exp2) {
export function or(exp1, exp2): Expression {
if (!isArray(exp1)) return exp1 ? exp1 : exp2;
if (!isArray(exp2)) return exp2 ? exp2 : exp1;
let res = ["OR"];
let res: Expression = ["OR"];
if (exp1[0] === "OR") res = res.concat(exp1.slice(1));
else res.push(exp1);
@@ -192,49 +228,19 @@ export function or(exp1, exp2) {
return res;
}
export function not(exp) {
export function not(exp): Expression {
if (isArray(exp) && exp[0] === "NOT") return exp[1];
return ["NOT", exp];
}
export function subset(exp1, exp2) {
export function subset(exp1, exp2): boolean {
const e = evaluate(["NOT", ["OR", ["NOT", exp1], exp2]]);
if (!isArray(e)) return !e;
const { vars, clauses } = booleanCnf(e);
return !naiveDpll(clauses, vars);
}
export function likePatternToRegExp(pat, esc = "", flags = "") {
const convChars = {
"-": "\\-",
"/": "\\/",
"\\": "\\/",
"^": "\\^",
$: "\\$",
"*": "\\*",
"+": "\\+",
"?": "\\?",
".": "\\.",
"(": "\\(",
")": "\\)",
"|": "\\|",
"[": "\\[",
"]": "\\]",
"{": "\\{",
"}": "\\}",
"\\%": ".*",
"\\_": "."
};
let chars = parseLikePattern(pat, esc);
if (!chars.length) return new RegExp("^$", flags);
chars = chars.map(c => convChars[c] || c);
chars[0] = chars[0] === ".*" ? "" : "^" + chars[0];
const l = chars.length - 1;
chars[l] = [".*", ""].includes(chars[l]) ? "" : chars[l] + "$";
return new RegExp(chars.join(""), flags);
}
export function extractParams(exp) {
export function extractParams(exp): string[] {
const params = new Set();
map(exp, e => {
if (isArray(e) && e[0] === "PARAM") params.add(e[1]);
@@ -1,10 +1,8 @@
"use strict";
let cache1 = new Map();
let cache2 = new Map();
const keys = new WeakMap();
function getKey(obj) {
function getKey(obj): string {
if (obj === null) return "null";
else if (obj === undefined) return "undefined";
@@ -22,7 +20,7 @@ function getKey(obj) {
return k;
}
export default function memoize(func) {
export default function memoize(func): Function {
return (...args) => {
const key = JSON.stringify(args.map(getKey)) + getKey(func);
@@ -14,9 +14,10 @@
* The code is very similar to the pure table solver, except we use a
* unit-propagating, derived-varlist-returing satisfiableAt.
*/
"use strict";
function satisfiableAt(clauses, varVals, varnr, val) {
/* eslint-disable @typescript-eslint/no-use-before-define */
function satisfiableAt(clauses, varVals, varnr, val): boolean {
if (varnr !== 0) varVals[varnr] = val;
const propRes = unitPropagate(clauses, varVals);
@@ -87,7 +88,7 @@ function satisfiableAt(clauses, varVals, varnr, val) {
* undetermined. The values of variables on the list will be restored (set to
* 0) during backtracking.
*/
function unitPropagate(clauses, varVals) {
function unitPropagate(clauses, varVals): number[] | 1 | -1 {
// all vars with a value derived in the forthcoming main loop
const derivedVars = [];
@@ -167,7 +168,7 @@ function unitPropagate(clauses, varVals) {
return derivedVars;
}
export function naiveDpll(clauses, maxvarnr) {
export function naiveDpll(clauses, maxvarnr): boolean {
// variable values are 0 if not set, 1 if positive, -1 if negative
const varVals = new Int32Array(maxvarnr + 1);
return satisfiableAt(clauses, varVals, 0, 0);
-3
View File
@@ -1,3 +0,0 @@
export default function pass() {
return 1;
}
+3
View File
@@ -0,0 +1,3 @@
export default function pass(): number {
return 1;
}
@@ -1,4 +1,4 @@
export default function test(resourceType, resource) {
export default function test(resourceType, resource): number {
if (resourceType === "devices") {
if (resource["Tags.test"] && resource["Tags.test"].value[0]) return 1;
} else if (resourceType === "presets") {
+12 -8
View File
@@ -16,7 +16,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { resolve } from "path";
import { readFileSync } from "fs";
@@ -90,9 +89,9 @@ const options = {
DEVICE_ONLINE_THRESHOLD: { type: "int", default: 4000 }
};
const allConfig = {};
const allConfig: { [name: string]: string | number } = {};
function setConfig(name, value, commandLineArgument) {
function setConfig(name, value, commandLineArgument = false): boolean {
if (allConfig[name] != null) return true;
// For compatibility with v1.0
@@ -119,7 +118,7 @@ function setConfig(name, value, commandLineArgument) {
if (name === "FS_IP" || name === "fs-ip") setConfig("FS_HOSTNAME", value);
function cast(val, type) {
function cast(val, type): string | number | boolean {
switch (type) {
case "int":
return Number(val);
@@ -182,7 +181,9 @@ setConfig("CONFIG_DIR", options["CONFIG_DIR"]["default"]);
// Configuration file
const configFile = JSON.parse(
readFileSync(resolve(allConfig.CONFIG_DIR, "config.json"))
readFileSync(
resolve(allConfig.CONFIG_DIR as string, "config.json")
).toString()
);
for (const [k, v] of Object.entries(configFile)) {
if (!setConfig(k, v))
@@ -194,7 +195,10 @@ for (const [k, v] of Object.entries(configFile)) {
for (const [k, v] of Object.entries(options))
if (v["default"] != null) setConfig(k, v["default"]);
export function get(optionName, deviceId) {
export function get(
optionName: string,
deviceId?: string
): string | number | boolean {
if (!deviceId) return allConfig[optionName];
optionName = `${optionName}-${deviceId}`;
@@ -222,7 +226,7 @@ export function get(optionName, deviceId) {
return null;
}
export function getDefault(optionName) {
export function getDefault(optionName): string | number | boolean {
const option = options[optionName];
if (!option) return null;
@@ -235,7 +239,7 @@ export function getDefault(optionName) {
export let auth;
// Load authentication scripts
try {
auth = require(resolve(allConfig.CONFIG_DIR, "auth.js"));
auth = require(resolve(allConfig.CONFIG_DIR as string, "auth.js"));
} catch (error) {
// No auth.js exists
}
+94 -40
View File
@@ -16,11 +16,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import zlib from "zlib";
import crypto from "crypto";
import fs from "fs";
import * as zlib from "zlib";
import * as crypto from "crypto";
import * as fs from "fs";
import { Socket } from "net";
import * as config from "./config";
import * as common from "./common";
import * as soap from "./soap";
@@ -32,9 +32,19 @@ import * as localCache from "./local-cache";
import * as db from "./db";
import * as logger from "./logger";
import * as scheduling from "./scheduling";
import {
SessionContext,
ParamPath,
AcsRequest,
SetAcsRequest,
SessionFault,
Operation
} from "./types";
const MAX_CYCLES = 4;
const MAX_CONCURRENT_REQUESTS = config.get("MAX_CONCURRENT_REQUESTS");
const MAX_CONCURRENT_REQUESTS = +config.get("MAX_CONCURRENT_REQUESTS");
const currentSessions = new WeakMap<Socket, SessionContext>();
const stats = {
concurrentRequests: 0,
@@ -43,7 +53,7 @@ const stats = {
initiatedSessions: 0
};
function throwError(err, httpResponse) {
function throwError(err, httpResponse?): never {
if (httpResponse) {
currentSessions.delete(httpResponse.connection);
httpResponse.writeHead(500, { Connection: "close" });
@@ -53,7 +63,11 @@ function throwError(err, httpResponse) {
throw err;
}
function writeResponse(sessionContext, res, close) {
function writeResponse(
sessionContext: SessionContext,
res,
close = false
): void {
// Close connection after last request in session
if (close) res.headers["Connection"] = "close";
@@ -69,7 +83,7 @@ function writeResponse(sessionContext, res, close) {
});
}
function finish() {
function finish(): void {
stats.concurrentRequests -= 1;
if (sessionContext.httpRequest.connection.destroyed) {
logger.accessError({
@@ -130,7 +144,9 @@ function writeResponse(sessionContext, res, close) {
}
}
function recordFault(sessionContext, fault, provisions, channels) {
function recordFault(sessionContext, fault, provisions, channels): void;
function recordFault(sessionContext, fault): void;
function recordFault(sessionContext, fault, provisions?, channels?): void {
if (!provisions) {
provisions = sessionContext.provisions;
channels = sessionContext.channels;
@@ -185,7 +201,7 @@ function recordFault(sessionContext, fault, provisions, channels) {
session.clearProvisions(sessionContext);
}
function inform(sessionContext, rpc) {
function inform(sessionContext, rpc): void {
session.inform(sessionContext, rpc.cpeRequest, (err, acsResponse) => {
if (err) return void throwError(err, sessionContext.httpResponse);
@@ -213,7 +229,7 @@ function inform(sessionContext, rpc) {
});
}
function transferComplete(sessionContext, rpc) {
function transferComplete(sessionContext, rpc): void {
session.transferComplete(
sessionContext,
rpc.cpeRequest,
@@ -250,7 +266,7 @@ function transferComplete(sessionContext, rpc) {
}
// Append providions and remove duplicates
function appendProvisions(original, toAppend) {
function appendProvisions(original, toAppend): boolean {
let modified = false;
const stringified = new WeakMap();
@@ -281,7 +297,7 @@ function appendProvisions(original, toAppend) {
return modified;
}
function applyPresets(sessionContext) {
function applyPresets(sessionContext: SessionContext): void {
const deviceData = sessionContext.deviceData;
const presets = localCache.getPresets(sessionContext.cacheSnapshot);
@@ -289,7 +305,7 @@ function applyPresets(sessionContext) {
const blackList = {};
let whiteList = null;
let whiteListProvisions = null;
const RETRY_DELAY = localCache.getConfig(
const RETRY_DELAY = +localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.retryDelay",
sessionContext.configContext
@@ -318,13 +334,17 @@ function applyPresets(sessionContext) {
deviceData.attributes.revision = 1;
const deviceEvents = {};
for (const p of deviceData.paths.find(["Events", "*"], false, true)) {
for (const p of deviceData.paths.find(
["Events", "*"] as ParamPath,
false,
true
)) {
const attrs = deviceData.attributes.get(p);
if (attrs && attrs.value && attrs.value[1][0] >= sessionContext.timestamp)
deviceEvents[p[1]] = true;
}
const parameters = {};
const parameters: { [name: string]: ParamPath } = {};
const filteredPresets = [];
for (const preset of presets) {
@@ -478,7 +498,7 @@ function applyPresets(sessionContext) {
);
}
function nextRpc(sessionContext) {
function nextRpc(sessionContext: SessionContext): void {
session.rpcRequest(sessionContext, null, (err, fault, id, acsRequest) => {
if (err) return void throwError(err, sessionContext.httpResponse);
@@ -604,7 +624,7 @@ function nextRpc(sessionContext) {
});
}
function endSession(sessionContext, callback) {
function endSession(sessionContext: SessionContext, callback): void {
let saveCache = sessionContext.cacheUntil != null;
let counter = 3;
@@ -714,24 +734,29 @@ function endSession(sessionContext, callback) {
if ((counter -= 2) === 1) callback(null, sessionContext.new);
}
function sendAcsRequest(sessionContext, id, acsRequest) {
function sendAcsRequest(
sessionContext: SessionContext,
id?: string,
acsRequest?: AcsRequest
): void {
if (!acsRequest)
return void writeResponse(sessionContext, soap.response(null), true);
if (acsRequest.name === "Download") {
acsRequest.fileSize = 0;
if (!acsRequest.url) {
const downloadRequest = acsRequest as SetAcsRequest;
downloadRequest.fileSize = 0;
if (!downloadRequest.url) {
const FS_PORT = config.get("FS_PORT");
const FS_HOSTNAME = config.get("FS_HOSTNAME");
const FS_SSL = config.get("FS_SSL");
acsRequest.url = FS_SSL ? "https://" : "http://";
acsRequest.url += FS_HOSTNAME;
if (FS_PORT !== 80) acsRequest.url += ":" + FS_PORT;
acsRequest.url += "/" + encodeURI(acsRequest.fileName);
downloadRequest.url = FS_SSL ? "https://" : "http://";
downloadRequest.url += FS_HOSTNAME;
if (FS_PORT !== 80) downloadRequest.url += ":" + FS_PORT;
downloadRequest.url += "/" + encodeURI(downloadRequest.fileName);
const files = localCache.getFiles(sessionContext.cacheSnapshot);
if (files[acsRequest.fileName])
acsRequest.fileSize = files[acsRequest.fileName].length;
if (files[downloadRequest.fileName])
downloadRequest.fileSize = files[downloadRequest.fileName].length;
}
}
@@ -751,7 +776,11 @@ function sendAcsRequest(sessionContext, id, acsRequest) {
writeResponse(sessionContext, res);
}
function getSession(connection, sessionId, callback) {
function getSession(
connection,
sessionId,
callback: (err?: Error, sessionContext?: SessionContext) => void
): void {
const sessionContext = currentSessions.get(connection);
if (sessionContext) {
currentSessions.delete(connection);
@@ -768,10 +797,8 @@ function getSession(connection, sessionId, callback) {
}, 100);
}
const currentSessions = new WeakMap();
// When socket closes, store active sessions in cache
export function onConnection(socket) {
export function onConnection(socket): void {
// The property remoteAddress may be undefined after the connection is
// closed, unless we read it at least once (caching?)
socket.remoteAddress;
@@ -840,7 +867,29 @@ setInterval(() => {
stats.initiatedSessions = 0;
}, 10000).unref();
function getDueTasksAndFaultsAndOperations(deviceId, timestamp, callback) {
interface GetDueTasksAndFaultsAndOperationsCallback {
(
err: null,
tasks: any[],
faults: { [channel: string]: SessionFault },
operations: { [commandKey: string]: Operation },
ttl: number
): void;
(
err: Error,
tasks?: null,
faults?: null,
operations?: null,
ttl?: null
): void;
}
function getDueTasksAndFaultsAndOperations(
deviceId,
timestamp,
callback: GetDueTasksAndFaultsAndOperationsCallback
): void {
cache.get(`${deviceId}_tasks_faults_operations`, (err, res) => {
if (err) return void callback(err);
@@ -850,7 +899,8 @@ function getDueTasksAndFaultsAndOperations(deviceId, timestamp, callback) {
null,
res.tasks || [],
res.faults || {},
res.operations || {}
res.operations || {},
0
);
}
@@ -899,8 +949,12 @@ function cacheDueTasksAndFaultsAndOperations(
operations,
cacheUntil,
callback
) {
const v = {};
): void {
const v = {
tasks: null,
faults: null,
operations: null
};
if (tasks.length) v.tasks = tasks;
if (Object.keys(faults).length) v.faults = faults;
if (Object.keys(operations).length) v.operations = operations;
@@ -917,7 +971,7 @@ function cacheDueTasksAndFaultsAndOperations(
);
}
function processRequest(sessionContext, rpc) {
function processRequest(sessionContext, rpc): void {
if (rpc.cpeRequest) {
if (rpc.cpeRequest.name === "Inform") {
logger.accessInfo({
@@ -996,7 +1050,7 @@ function processRequest(sessionContext, rpc) {
}
}
export function listener(httpRequest, httpResponse) {
export function listener(httpRequest, httpResponse): void {
stats.totalRequests += 1;
if (httpRequest.method !== "POST") {
@@ -1064,7 +1118,7 @@ export function listener(httpRequest, httpResponse) {
offset += chunk.length;
}
function parsedRpc(sessionContext, rpc, parseWarnings) {
function parsedRpc(sessionContext, rpc, parseWarnings): void {
for (const w of parseWarnings) {
w.sessionContext = sessionContext;
w.rpc = rpc;
@@ -1143,7 +1197,7 @@ export function listener(httpRequest, httpResponse) {
if (sessionContext) {
if (
(rpc.cpeRequest && rpc.cpeRequest.name === "Inform") ||
!sessionContext.rpcRequest ^ !(rpc.cpeResponse || rpc.cpeFault)
!sessionContext.rpcRequest !== !(rpc.cpeResponse || rpc.cpeFault)
) {
logger.accessError({
message: "Bad session state",
@@ -1201,7 +1255,7 @@ export function listener(httpRequest, httpResponse) {
_sessionContext.cacheSnapshot = cacheSnapshot;
_sessionContext.configContext = configContext;
_sessionContext.debug = localCache.getConfig(
_sessionContext.debug = !!localCache.getConfig(
cacheSnapshot,
"cwmp.debug",
configContext
+34 -33
View File
@@ -16,7 +16,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { MongoClient, ObjectID } from "mongodb";
import { get } from "./config";
@@ -36,9 +35,9 @@ export let tasksCollection,
export let client;
export function connect(callback) {
export function connect(callback): void {
MongoClient.connect(
get("MONGODB_CONNECTION_URL"),
"" + get("MONGODB_CONNECTION_URL"),
{ useNewUrlParser: true },
(err, _client) => {
if (err) return void callback(err);
@@ -63,13 +62,13 @@ export function connect(callback) {
);
}
export function disconnect() {
export function disconnect(): void {
if (client) client.close();
}
// Optimize projection by removing overlaps
// This can modify the object
function optimizeProjection(obj) {
function optimizeProjection(obj: {}): {} {
if (obj[""]) return { "": obj[""] };
const keys = Object.keys(obj).sort();
@@ -88,14 +87,14 @@ function optimizeProjection(obj) {
return obj;
}
export function fetchDevice(id, timestamp, patterns, callback) {
const MAX_DEPTH = get("MAX_DEPTH", id);
export function fetchDevice(id, timestamp, patterns, callback): void {
const MAX_DEPTH = +get("MAX_DEPTH", id);
if (!patterns || !patterns.length) patterns = [[[], (1 << MAX_DEPTH) - 1]];
let projection = {};
const projectionTree = {};
function func(path, pats, projTree) {
function func(path, pats, projTree): void {
const children = {};
for (const pat of pats) {
const fragment = pat[0][path.length] || "*";
@@ -232,16 +231,16 @@ export function fetchDevice(id, timestamp, patterns, callback) {
}
}
const options = {};
let options;
if (!projection[""]) {
if (!Object.keys(projection).length) options.projection = { _id: 1 };
else options.projection = projection;
if (!Object.keys(projection).length) options = { projection: { _id: 1 } };
else options = { projection: projection };
}
devicesCollection.findOne({ _id: id }, options, (err, device) => {
if (err || !device) return void callback(err);
function storeParams(obj, path, ts, descendantsFetched, projTree) {
function storeParams(obj, path, ts, descendantsFetched, projTree): void {
let thisFetched = false;
if (descendantsFetched) {
thisFetched = true;
@@ -278,7 +277,11 @@ export function fetchDevice(id, timestamp, patterns, callback) {
if (obj["_instance"] && obj["_object"] == null) obj["_object"] = true;
if (thisFetched) {
const attrs = {};
const attrs: {
object?: [number, number];
writable?: [number, number];
value?: [number, [string | number | boolean, string]];
} = {};
let t = obj["_timestamp"] || 1;
if (ts > t) t = ts;
@@ -392,7 +395,7 @@ export function fetchDevice(id, timestamp, patterns, callback) {
delete device[k];
break;
case "_tags":
if (v.length) {
if ((v as string[]).length) {
res.push([
["Tags"],
timestamp,
@@ -400,7 +403,7 @@ export function fetchDevice(id, timestamp, patterns, callback) {
]);
}
for (let t of v) {
for (let t of v as string[]) {
t = t.replace(/[^a-zA-Z0-9-]+/g, "_");
res.push([
["Tags", t],
@@ -478,7 +481,7 @@ export function saveDevice(
isNew,
sessionTimestamp,
callback
) {
): void {
const update = { $set: {}, $unset: {}, $addToSet: {}, $pull: {} };
for (const diff of deviceData.timestamps.diff()) {
@@ -501,7 +504,7 @@ export function saveDevice(
.concat("_timestamp")
.join(".")
] = 1;
} else if (diff[2] !== diff[1]) {
} else {
const parent = deviceData.paths.get(diff[0].slice(0, -1));
if (parent && (!parent.length || deviceData.attributes.has(parent))) {
update["$set"][
@@ -515,8 +518,6 @@ export function saveDevice(
}
for (const diff of deviceData.attributes.diff()) {
if (diff[1] === diff[2]) continue;
const path = diff[0];
const value1 = (((diff[1] || {}).value || [])[1] || [])[0];
const value2 = (((diff[2] || {}).value || [])[1] || [])[0];
@@ -726,7 +727,7 @@ export function saveDevice(
);
}
export function getFaults(deviceId, callback) {
export function getFaults(deviceId, callback): void {
faultsCollection
.find({ _id: { $regex: `^${escapeRegExp(deviceId)}\\:` } })
.toArray((err, res) => {
@@ -747,7 +748,7 @@ export function getFaults(deviceId, callback) {
});
}
export function saveFault(deviceId, channel, fault, callback) {
export function saveFault(deviceId, channel, fault, callback): void {
const id = `${deviceId}:${channel}`;
fault = Object.assign({}, fault);
fault._id = id;
@@ -758,11 +759,11 @@ export function saveFault(deviceId, channel, fault, callback) {
faultsCollection.replaceOne({ _id: id }, fault, { upsert: true }, callback);
}
export function deleteFault(deviceId, channel, callback) {
export function deleteFault(deviceId, channel, callback): void {
faultsCollection.deleteOne({ _id: `${deviceId}:${channel}` }, callback);
}
export function getDueTasks(deviceId, timestamp, callback) {
export function getDueTasks(deviceId, timestamp, callback): void {
const cur = tasksCollection.find({ device: deviceId }).sort(["timestamp"]);
const tasks = [];
@@ -807,14 +808,14 @@ export function getDueTasks(deviceId, timestamp, callback) {
);
}
export function clearTasks(deviceId, taskIds, callback) {
export function clearTasks(deviceId, taskIds, callback): void {
tasksCollection.deleteMany(
{ _id: { $in: taskIds.map(id => new ObjectID(id)) } },
callback
);
}
export function getOperations(deviceId, callback) {
export function getOperations(deviceId, callback): void {
operationsCollection
.find({ _id: { $regex: `^${escapeRegExp(deviceId)}\\:` } })
.toArray((err, res) => {
@@ -835,7 +836,7 @@ export function getOperations(deviceId, callback) {
});
}
export function saveOperation(deviceId, commandKey, operation, callback) {
export function saveOperation(deviceId, commandKey, operation, callback): void {
const id = `${deviceId}:${commandKey}`;
operation = Object.assign({}, operation);
operation._id = id;
@@ -851,34 +852,34 @@ export function saveOperation(deviceId, commandKey, operation, callback) {
);
}
export function deleteOperation(deviceId, commandKey, callback) {
export function deleteOperation(deviceId, commandKey, callback): void {
operationsCollection.deleteOne(
{ _id: `${deviceId}:${commandKey}` },
callback
);
}
export function getPresets(callback) {
export function getPresets(callback): void {
presetsCollection.find().toArray(callback);
}
export function getObjects(callback) {
export function getObjects(callback): void {
objectsCollection.find().toArray(callback);
}
export function getProvisions(callback) {
export function getProvisions(callback): void {
provisionsCollection.find().toArray(callback);
}
export function getVirtualParameters(callback) {
export function getVirtualParameters(callback): void {
virtualParametersCollection.find().toArray(callback);
}
export function getFiles(callback) {
export function getFiles(callback): void {
filesCollection.find().toArray(callback);
}
export function getConfig(callback) {
export function getConfig(callback): void {
configCollection.find().toArray((err, res) => {
if (err) return void callback(err);
@@ -16,16 +16,15 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import * as config from "./config";
import * as common from "./common";
import * as device from "./device";
import * as scheduling from "./scheduling";
const MAX_DEPTH = config.get("MAX_DEPTH");
const MAX_DEPTH = +config.get("MAX_DEPTH");
export function refresh(sessionContext, provision, declarations) {
export function refresh(sessionContext, provision, declarations): boolean {
const path = common.parsePath(provision[1]).slice();
const l = path.length;
path.length = MAX_DEPTH;
@@ -48,7 +47,7 @@ export function refresh(sessionContext, provision, declarations) {
return true;
}
export function value(sessionContext, provision, declarations) {
export function value(sessionContext, provision, declarations): boolean {
const path = common.parsePath(provision[1]);
declarations.push({
path: path,
@@ -62,7 +61,7 @@ export function value(sessionContext, provision, declarations) {
return true;
}
export function tag(sessionContext, provision, declarations) {
export function tag(sessionContext, provision, declarations): boolean {
const path = ["Tags", provision[1]];
declarations.push({
@@ -77,7 +76,7 @@ export function tag(sessionContext, provision, declarations) {
return true;
}
export function reboot(sessionContext, provision, declarations) {
export function reboot(sessionContext, provision, declarations): boolean {
declarations.push({
path: ["Reboot"],
pathGet: 1,
@@ -90,7 +89,7 @@ export function reboot(sessionContext, provision, declarations) {
return true;
}
export function reset(sessionContext, provision, declarations) {
export function reset(sessionContext, provision, declarations): boolean {
declarations.push({
path: ["FactoryReset"],
pathGet: 1,
@@ -103,7 +102,7 @@ export function reset(sessionContext, provision, declarations) {
return true;
}
export function download(sessionContext, provision, declarations) {
export function download(sessionContext, provision, declarations): boolean {
const alias = [
["FileType"],
provision[1] || "",
@@ -140,7 +139,7 @@ export function instances(
declarations,
startRevision,
endRevision
) {
): boolean {
let count = Number(provision[2]);
if (Number.isNaN(count)) return true;
+62 -42
View File
@@ -16,7 +16,8 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { ParamPath, DeviceData, Declaration, Clear } from "./types";
const CHANGE_FLAGS = {
object: 2,
@@ -24,7 +25,7 @@ const CHANGE_FLAGS = {
value: 8
};
function parseBool(v) {
function parseBool(v): boolean {
v = "" + v;
if (v === "true" || v === "TRUE" || v === "True" || v === "1") return true;
else if (v === "false" || v === "FALSE" || v === "False" || v === "0")
@@ -32,14 +33,16 @@ function parseBool(v) {
else return null;
}
export function sanitizeParameterValue(parameterValue) {
export function sanitizeParameterValue(
parameterValue
): [string | number | boolean, string?] {
if (parameterValue[0] != null) {
switch (parameterValue[1]) {
case "xsd:boolean":
if (typeof parameterValue[0] !== "boolean") {
let v = parseBool(parameterValue[0]);
if (v == null) v = "" + parameterValue[0];
parameterValue[0] = v;
const b = parseBool(parameterValue[0]);
if (b == null) parameterValue[0] = "" + parameterValue[0];
else parameterValue[0] = b;
}
break;
case "xsd:string":
@@ -52,20 +55,22 @@ export function sanitizeParameterValue(parameterValue) {
case "xsd:int":
case "xsd:unsignedInt":
if (typeof parameterValue[0] !== "number") {
let v = parseInt(parameterValue[0]);
if (isNaN(v)) v = "" + parameterValue[0];
parameterValue[0] = v;
const i = parseInt(parameterValue[0]);
if (isNaN(i)) parameterValue[0] = "" + parameterValue[0];
else parameterValue[0] = i;
}
break;
case "xsd:dateTime":
if (typeof parameterValue[0] !== "number") {
// Don't use parseInt because it reads date string as a number
let v = +parameterValue[0];
if (isNaN(v)) {
v = Date.parse(parameterValue[0]);
if (isNaN(v)) v = "" + parameterValue[0];
let i = +parameterValue[0];
if (isNaN(i)) {
i = Date.parse(parameterValue[0]);
if (isNaN(i)) parameterValue[0] = "" + parameterValue[0];
else parameterValue[0] = i;
} else {
parameterValue[0] = i;
}
parameterValue[0] = v;
}
break;
default:
@@ -83,10 +88,10 @@ export function sanitizeParameterValue(parameterValue) {
return parameterValue;
}
export function getAliasDeclarations(path, timestamp) {
const decs = [];
export function getAliasDeclarations(path, timestamp): Declaration[] {
const decs = [] as Declaration[];
function recursive(p, prefix) {
function recursive(p, prefix): void {
const pattern = prefix.concat(p);
for (let i = prefix.length; i < pattern.length; ++i) {
@@ -113,13 +118,17 @@ export function getAliasDeclarations(path, timestamp) {
return decs;
}
export function unpack(deviceData, path, revision) {
let allMatches = [];
export function unpack(
deviceData,
path: ParamPath,
revision?: number
): ParamPath[] {
let allMatches = [] as ParamPath[];
if (!path.alias) {
for (const p of deviceData.paths.find(path, false, true))
if (deviceData.attributes.has(p, revision)) allMatches.push(p);
} else {
const wildcardPath = path.slice();
const wildcardPath = path.slice() as ParamPath;
wildcardPath.wildcard = path.wildcard;
for (let i = 0; i < wildcardPath.length; ++i) {
if (Array.isArray(wildcardPath[i])) {
@@ -134,7 +143,9 @@ export function unpack(deviceData, path, revision) {
for (let i = path.length - 1; i >= 0; --i) {
if (Array.isArray(path[i])) {
for (let j = 0; j < path[i].length; j += 2) {
const p = wildcardPath.slice(0, i + 1).concat(path[i][j]);
const p = wildcardPath
.slice(0, i + 1)
.concat(path[i][j]) as ParamPath;
p.alias =
((wildcardPath.alias & ((1 << (i + 1)) - 1)) + path[i][j].alias) <<
(i + 1);
@@ -143,7 +154,7 @@ export function unpack(deviceData, path, revision) {
path[i][j].wildcard) <<
(i + 1);
const unpacked = unpack(deviceData, p, revision);
const filtered = [];
const filtered = [] as ParamPath[];
for (const up of unpacked) {
const attributes = deviceData.attributes.get(up, revision);
if (
@@ -161,7 +172,7 @@ export function unpack(deviceData, path, revision) {
if (k < 0) {
filtered.push(allMatches[m]);
allMatches[m] = [];
allMatches[m] = null;
}
}
}
@@ -172,21 +183,19 @@ export function unpack(deviceData, path, revision) {
}
}
allMatches.sort((a, b) => {
for (let i = 0; i < a.length; ++i) {
if (a === b) continue;
allMatches.sort((p1, p2) => {
for (let i = 0; i < p1.length; ++i) {
const a = p1[i];
const b = p2[i];
if (a !== b) {
// Use numeric sorting for numbers
const ia = parseInt(a);
const ib = parseInt(b);
// Use numeric sorting for numbers
const ia = parseInt(a);
const ib = parseInt(b);
if (ia.toString() === a && ib.toString() === b) {
a = ia;
b = ib;
if (ia === +a && ib === +b) return ia - ib;
else if (a < b) return -1;
else return 1;
}
if (a < b) return -1;
else return 1;
}
return 0;
});
@@ -200,7 +209,7 @@ export function clear(
timestamp,
attributes,
changeFlags = 0
) {
): void {
const changeTrackers = {};
timestamp = timestamp || 0;
@@ -259,7 +268,7 @@ export function clear(
if (changeTrackers[k] & changeFlags) deviceData.changes.add(k);
}
function compareEquality(a, b) {
function compareEquality(a, b): boolean {
const t = typeof a;
if (
a === null ||
@@ -274,7 +283,13 @@ function compareEquality(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
export function set(deviceData, path, timestamp, attributes, toClear) {
export function set(
deviceData: DeviceData,
path: ParamPath,
timestamp,
attributes,
toClear?: Clear[]
): Clear[] {
path = deviceData.paths.add(path);
const currentTimestamp = deviceData.timestamps.get(path);
@@ -330,7 +345,7 @@ export function set(deviceData, path, timestamp, attributes, toClear) {
if (path.length > 1) {
toClear = set(
deviceData,
path.slice(0, path.length - 1),
path.slice(0, path.length - 1) as ParamPath,
timestamp,
{ object: [timestamp, 1] },
toClear
@@ -372,7 +387,12 @@ export function set(deviceData, path, timestamp, attributes, toClear) {
return toClear;
}
export function track(deviceData, path, marker, attributes) {
export function track(
deviceData: DeviceData,
path: ParamPath,
marker: string,
attributes?
): void {
path = deviceData.paths.add(path);
let f = 1;
@@ -388,7 +408,7 @@ export function track(deviceData, path, marker, attributes) {
cur[marker] |= f;
}
export function clearTrackers(deviceData, tracker) {
export function clearTrackers(deviceData, tracker): void {
if (Array.isArray(tracker)) {
for (const v of deviceData.trackers.values())
for (const t of tracker) delete v[t];
+6 -7
View File
@@ -16,18 +16,17 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { spawn } from "child_process";
import crypto from "crypto";
import * as crypto from "crypto";
import * as config from "./config";
const TIMEOUT = config.get("EXT_TIMEOUT");
const TIMEOUT = +config.get("EXT_TIMEOUT");
const processes = {};
const jobs = new Map();
function messageHandler(message) {
function messageHandler(message): void {
const func = jobs.get(message[0]);
if (func) {
jobs.delete(message[0]);
@@ -35,7 +34,7 @@ function messageHandler(message) {
}
}
export function run(args, callback) {
export function run(args, callback): boolean {
const scriptName = args[0];
const id = crypto.randomBytes(8).toString("hex");
@@ -77,7 +76,7 @@ export function run(args, callback) {
return processes[scriptName].send([id, args.slice(1)]);
}
function kill(process, callback) {
function kill(process, callback?: () => void): void {
if (process.signalCode !== null || process.exitCode !== null) return;
const timeToKill = Date.now() + 5000;
@@ -96,7 +95,7 @@ function kill(process, callback) {
}, 100);
}
export function killAll(callback) {
export function killAll(callback?): void {
let counter = 1;
for (const n in processes) {
++counter;
+4 -5
View File
@@ -16,14 +16,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import url from "url";
import querystring from "querystring";
import * as url from "url";
import * as querystring from "querystring";
import { GridFSBucket } from "mongodb";
import * as db from "./db";
export function listener(request, response) {
export function listener(request, response): void {
const urlParts = url.parse(request.url, true);
if (request.method === "GET") {
const filename = querystring.unescape(urlParts.pathname.substring(1));
@@ -46,7 +45,7 @@ export function listener(request, response) {
});
const bucket = new GridFSBucket(db.client.db());
const downloadStream = bucket.openDownloadStream(filename);
const downloadStream = bucket.openDownloadStreamByName(filename);
downloadStream.pipe(response);
});
} else {
@@ -16,7 +16,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const WILDCARD_MULTIPLIER = 2;
const UNDISCOVERED_DEPTH = 7;
@@ -28,7 +27,7 @@ const UNDISCOVERED_DEPTH = 7;
// flags is an int where its bits mark the segments in the pattern that
// need refreshing. Leading 0s indicate that the pattern up to that
// point has been discovered.
export function estimateGpnCount(gpnPatterns, depth = 0) {
export function estimateGpnCount(gpnPatterns, depth = 0): number {
const children = {};
const wildcardChildren = [];
let wildcardDiscovered = false;
+17 -12
View File
@@ -16,22 +16,27 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
interface Instance {
[name: string]: string;
}
export default class InstanceSet {
constructor() {
private set: Set<Instance>;
public constructor() {
this.set = new Set();
}
add(instance) {
public add(instance: Instance): void {
this.set.add(instance);
}
delete(instance) {
public delete(instance: Instance): void {
this.set.delete(instance);
}
superset(instance) {
public superset(instance: Instance): Instance[] {
const res = [];
for (const inst of this.set) {
let match = true;
@@ -67,7 +72,7 @@ export default class InstanceSet {
return res;
}
subset(instance) {
public subset(instance: Instance): Instance[] {
const res = [];
for (const inst of this.set) {
@@ -104,23 +109,23 @@ export default class InstanceSet {
return res;
}
[Symbol.iterator]() {
public [Symbol.iterator](): IterableIterator<Instance> {
return this.set.values();
}
forEach(callback) {
return this.set.forEach(callback);
public forEach(callback: (instance: Instance) => void): void {
this.set.forEach(callback);
}
values() {
public values(): IterableIterator<Instance> {
return this.set.values();
}
clear() {
public clear(): void {
this.set.clear();
}
get size() {
public get size(): number {
return this.set.size;
}
}
+37 -18
View File
@@ -16,11 +16,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import vm from "vm";
import crypto from "crypto";
import * as vm from "vm";
import * as crypto from "crypto";
import * as config from "./config";
import * as db from "./db";
import * as cache from "./cache";
@@ -28,6 +26,7 @@ import * as query from "./query";
import * as logger from "./logger";
import * as scheduling from "./scheduling";
import * as expression from "./common/expression";
import { Preset } from "./types";
const REFRESH = 3000;
const EVICT_TIMEOUT = 60000;
@@ -36,7 +35,7 @@ const snapshots = new Map();
let currentSnapshot = null;
let nextRefresh = 1;
function computeHash(snapshot) {
function computeHash(snapshot): string {
// MD5 hash for presets, provisions, virtual parameters for detecting changes
const h = crypto.createHash("md5");
for (const p of snapshot.presets) {
@@ -65,7 +64,7 @@ function computeHash(snapshot) {
return h.digest("hex");
}
function flattenObject(src, prefix = "", dst = {}) {
function flattenObject(src, prefix = "", dst = {}): {} {
for (const k of Object.keys(src)) {
const v = src[k];
if (typeof v === "object" && !Array.isArray(v))
@@ -75,7 +74,7 @@ function flattenObject(src, prefix = "", dst = {}) {
return dst;
}
function refresh(callback) {
function refresh(callback): void {
if (!nextRefresh) {
return void setTimeout(() => {
refresh(callback);
@@ -126,7 +125,7 @@ function refresh(callback) {
else return a.weight - b.weight;
});
const presets = [];
const presets = [] as Preset[];
for (const preset of res) {
let schedule = null;
if (preset.schedule) {
@@ -369,7 +368,16 @@ function refresh(callback) {
});
}
export function getCurrentSnapshot(callback) {
interface GetCurrentSnapshotCallback {
(err: null, currentSnapshot: string): void;
(err: Error, currentSnapshot?: null): void;
}
export function getCurrentSnapshot(callback: GetCurrentSnapshotCallback);
export function getCurrentSnapshot(): Promise<string>;
export function getCurrentSnapshot(
callback?: GetCurrentSnapshotCallback
): Promise<string> | void {
if (Date.now() < nextRefresh) {
if (callback) return callback(null, currentSnapshot);
return Promise.resolve(currentSnapshot);
@@ -389,35 +397,46 @@ export function getCurrentSnapshot(callback) {
});
}
export function hasSnapshot(hash) {
export function hasSnapshot(hash): boolean {
return snapshots.has(hash);
}
export function getPresets(snapshotKey) {
export function getPresets(snapshotKey): Preset[] {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.presets;
}
export function getProvisions(snapshotKey) {
export function getProvisions(
snapshotKey
): { [name: string]: { md5: string; script: vm.Script } } {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.provisions;
}
export function getVirtualParameters(snapshotKey) {
export function getVirtualParameters(
snapshotKey
): { [name: string]: { md5: string; script: vm.Script } } {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.virtualParameters;
}
export function getFiles(snapshotKey) {
export function getFiles(
snapshotKey
): { [name: string]: { length: number; md5: string; contentType: string } } {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.files;
}
export function getConfig(snapshotKey, key, context, now) {
export function getConfig(
snapshotKey,
key,
context?,
now?
): string | number | boolean | null {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
@@ -446,21 +465,21 @@ export function getConfig(snapshotKey, key, context, now) {
return expression.evaluate(snapshot.config[key], context, now || Date.now());
}
export function getAuthConfig(snapshotKey) {
export function getAuthConfig(snapshotKey): {} {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.config.auth;
}
export function getPermissionsConfig(snapshotKey) {
export function getPermissionsConfig(snapshotKey): {} {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.config.permissions;
}
export function getUiConfig(snapshotKey) {
export function getUiConfig(snapshotKey): {} {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
+33 -21
View File
@@ -16,10 +16,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import fs from "fs";
import os from "os";
import * as fs from "fs";
import * as os from "os";
import * as config from "./config";
@@ -28,20 +27,35 @@ const REOPEN_EVERY = 60000;
const LOG_FORMAT = config.get("LOG_FORMAT");
const ACCESS_LOG_FORMAT = config.get("ACCESS_LOG_FORMAT") || LOG_FORMAT;
const defaultMeta = {};
const defaultMeta: { [name: string]: any } = {};
let LOG_SYSTEMD = false;
let ACCESS_LOG_SYSTEMD = false;
let LOG_FILE, ACCESS_LOG_FILE;
declare global {
/* eslint-disable-next-line @typescript-eslint/no-namespace */
namespace NodeJS {
export interface WritableStream {
fd?: number;
}
}
}
declare module "fs" {
interface WriteStream {
fd?: number;
}
}
let logStream = fs.createWriteStream(null, { fd: process.stderr.fd });
let logStat = fs.fstatSync(logStream.fd);
let accessLogStream = fs.createWriteStream(null, { fd: process.stdout.fd });
let accessLogStat = fs.fstatSync(accessLogStream.fd);
// Reopen if original files have been moved (e.g. logrotate)
function reopen() {
function reopen(): void {
let counter = 1;
if (LOG_FILE) {
@@ -90,7 +104,7 @@ function reopen() {
setTimeout(reopen, REOPEN_EVERY - (Date.now() % REOPEN_EVERY)).unref();
}
export function init(service, version) {
export function init(service, version): void {
defaultMeta.hostname = os.hostname();
defaultMeta.pid = process.pid;
defaultMeta.name = `genieacs-${service}`;
@@ -115,9 +129,7 @@ export function init(service, version) {
const JOURNAL_STREAM = process.env["JOURNAL_STREAM"];
if (JOURNAL_STREAM) {
let [dev, inode] = JOURNAL_STREAM.split(":");
dev = parseInt(dev);
inode = parseInt(inode);
const [dev, inode] = JOURNAL_STREAM.split(":").map(parseInt);
LOG_SYSTEMD = logStat.dev === dev && logStat.ino === inode;
ACCESS_LOG_SYSTEMD =
@@ -129,12 +141,12 @@ export function init(service, version) {
setTimeout(reopen, REOPEN_EVERY - (Date.now() % REOPEN_EVERY)).unref();
}
export function close() {
export function close(): void {
accessLogStream.end();
logStream.end();
}
export function flatten(details) {
export function flatten(details): {} {
if (details.sessionContext) {
details.deviceId = details.sessionContext.deviceId;
details.remoteAddress =
@@ -198,7 +210,7 @@ export function flatten(details) {
return details;
}
function formatJson(details, systemd) {
function formatJson(details, systemd): string {
if (systemd) {
let severity = "";
if (details.severity === "info") severity = "<6>";
@@ -211,7 +223,7 @@ function formatJson(details, systemd) {
return `${JSON.stringify(flatten(details))}${os.EOL}`;
}
function formatSimple(details, systemd) {
function formatSimple(details, systemd): string {
flatten(details);
let remote = "";
@@ -255,7 +267,7 @@ function formatSimple(details, systemd) {
}${meta}${os.EOL}`;
}
function log(details) {
function log(details): void {
details.timestamp = new Date().toISOString();
if (LOG_FORMAT === "json") {
details = Object.assign({}, defaultMeta, details);
@@ -265,22 +277,22 @@ function log(details) {
}
}
export function info(details) {
export function info(details): void {
details.severity = "info";
log(details);
}
export function warn(details) {
export function warn(details): void {
details.severity = "warn";
log(details);
}
export function error(details) {
export function error(details): void {
details.severity = "error";
log(details);
}
export function accessLog(details) {
export function accessLog(details): void {
details.timestamp = new Date().toISOString();
if (ACCESS_LOG_FORMAT === "json") {
Object.assign(details, defaultMeta);
@@ -290,17 +302,17 @@ export function accessLog(details) {
}
}
export function accessInfo(details) {
export function accessInfo(details): void {
details.severity = "info";
accessLog(details);
}
export function accessWarn(details) {
export function accessWarn(details): void {
details.severity = "warn";
accessLog(details);
}
export function accessError(details) {
export function accessError(details): void {
details.severity = "error";
accessLog(details);
}
+31 -24
View File
@@ -16,12 +16,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import url from "url";
import mongodb from "mongodb";
import querystring from "querystring";
import vm from "vm";
import * as url from "url";
import * as mongodb from "mongodb";
import * as querystring from "querystring";
import * as vm from "vm";
import { exec } from "child_process";
import * as config from "./config";
import * as db from "./db";
@@ -43,7 +42,7 @@ const PROVISIONS_REGEX = /^\/provisions\/([a-zA-Z0-9\-_%]+)\/?$/;
const VIRTUAL_PARAMETERS_REGEX = /^\/virtual_parameters\/([a-zA-Z0-9\-_%]+)\/?$/;
const FAULTS_REGEX = /^\/faults\/([a-zA-Z0-9\-_%:]+)\/?$/;
function throwError(err, httpResponse) {
function throwError(err, httpResponse?): never {
if (httpResponse) {
httpResponse.writeHead(500, { Connection: "close" });
httpResponse.end(`${err.name}: ${err.message}`);
@@ -51,17 +50,20 @@ function throwError(err, httpResponse) {
throw err;
}
export function listener(request, response) {
export function listener(request, response): void {
const chunks = [];
let bytes = 0;
response.setHeader("GenieACS-Version", VERSION);
request.addListener("data", chunk => {
chunks.push(chunk);
bytes += chunk.length;
});
request.addListener(
"data",
(chunk): void => {
chunks.push(chunk);
bytes += chunk.length;
}
);
function getBody() {
function getBody(): Buffer {
// Write all chunks into a Buffer
const body = Buffer.allocUnsafe(bytes);
let offset = 0;
@@ -80,7 +82,7 @@ export function listener(request, response) {
PRESETS_REGEX.exec(urlParts.pathname)[1]
);
if (request.method === "PUT") {
const preset = JSON.parse(body);
const preset = JSON.parse(body.toString());
preset._id = presetName;
db.presetsCollection.replaceOne(
{ _id: presetName },
@@ -117,7 +119,7 @@ export function listener(request, response) {
OBJECTS_REGEX.exec(urlParts.pathname)[1]
);
if (request.method === "PUT") {
const object = JSON.parse(body);
const object = JSON.parse(body.toString());
object._id = objectName;
db.objectsCollection.replaceOne(
{ _id: objectName },
@@ -320,7 +322,7 @@ export function listener(request, response) {
DEVICE_TASKS_REGEX.exec(urlParts.pathname)[1]
);
if (body.length) {
const task = JSON.parse(body);
const task = JSON.parse(body.toString());
task.device = deviceId;
apiFunctions.insertTasks(task, err => {
if (err) return void throwError(err, response);
@@ -340,7 +342,7 @@ export function listener(request, response) {
const taskTimeout =
(urlParts.query.timeout &&
parseInt(urlParts.query.timeout)) ||
parseInt(urlParts.query.timeout as string)) ||
config.get("DEVICE_ONLINE_THRESHOLD", deviceId);
apiFunctions.watchTask(
@@ -499,15 +501,18 @@ export function listener(request, response) {
const uploadStream = bucket.openUploadStreamWithId(filename, filename, {
metadata: metadata
});
uploadStream.end(body, err => {
if (err) return void throwError(err, response);
uploadStream.on("error", err => {
throwError(err, response);
});
uploadStream.end(body, () => {
response.writeHead(201);
response.end();
});
} else if (request.method === "DELETE") {
const bucket = new mongodb.GridFSBucket(db.client.db());
bucket.delete(filename, err => {
bucket.delete((filename as unknown) as mongodb.ObjectId, err => {
if (err) return void throwError(err, response);
response.writeHead(200);
@@ -577,7 +582,7 @@ export function listener(request, response) {
let q = {};
if (urlParts.query.query) {
try {
q = JSON.parse(urlParts.query.query);
q = JSON.parse(urlParts.query.query as string);
} catch (err) {
response.writeHead(400);
response.end(`${err.name}: ${err.message}`);
@@ -606,14 +611,14 @@ export function listener(request, response) {
let projection = null;
if (urlParts.query.projection) {
projection = {};
for (const p of urlParts.query.projection.split(","))
for (const p of (urlParts.query.projection as string).split(","))
projection[p.trim()] = 1;
}
const cur = collection.find(q, { projection: projection });
if (urlParts.query.sort) {
const s = JSON.parse(urlParts.query.sort);
const s = JSON.parse(urlParts.query.sort as string);
const sort = {};
for (const [k, v] of Object.entries(s)) {
if (k[k.lastIndexOf(".") + 1] !== "_" && collectionName === "devices")
@@ -624,9 +629,11 @@ export function listener(request, response) {
cur.sort(sort);
}
if (urlParts.query.skip) cur.skip(parseInt(urlParts.query.skip));
if (urlParts.query.skip)
cur.skip(parseInt(urlParts.query.skip as string));
if (urlParts.query.limit) cur.limit(parseInt(urlParts.query.limit));
if (urlParts.query.limit)
cur.limit(parseInt(urlParts.query.limit as string));
cur.count(false, (err, total) => {
if (err) return void throwError(err);
+16 -7
View File
@@ -16,19 +16,23 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import { ParamPath } from "./types";
export default class PathSet {
constructor() {
private lengthIndex: Set<ParamPath>[];
private fragmentIndex: Map<string, Set<ParamPath>>[];
public constructor() {
this.lengthIndex = [];
this.fragmentIndex = [];
}
get depth() {
public get depth(): number {
return this.lengthIndex.length;
}
add(path) {
public add(path: ParamPath): ParamPath {
const p = this.get(path);
if (p) return p;
@@ -50,7 +54,7 @@ export default class PathSet {
let fragmentIndexSet = fragmentIndex.get(fragment);
if (!fragmentIndexSet) {
fragmentIndexSet = new Set();
fragmentIndexSet = new Set<ParamPath>();
fragmentIndex.set(fragment, fragmentIndexSet);
}
@@ -65,7 +69,7 @@ export default class PathSet {
return path;
}
get(pattern) {
public get(pattern: ParamPath): ParamPath {
const lengthIndex = this.lengthIndex[pattern.length];
if (!lengthIndex || !lengthIndex.size) return null;
@@ -102,7 +106,12 @@ export default class PathSet {
return null;
}
find(pattern, superset = false, subset = false, depth = pattern.length) {
public find(
pattern: ParamPath,
superset: boolean = false,
subset: boolean = false,
depth: number = pattern.length
): ParamPath[] {
const res = [];
const groups = [[]];
+18 -19
View File
@@ -16,11 +16,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import * as common from "./common";
function stringToRegexp(input, flags) {
function stringToRegexp(input, flags?): RegExp | false {
if (input.indexOf("*") === -1) return false;
let output = input.replace(/[[\]\\^$.|?+()]/, "\\$&");
@@ -34,7 +33,7 @@ function stringToRegexp(input, flags) {
return new RegExp(output, flags);
}
function normalize(input) {
function normalize(input): any {
if (common.typeOf(input) === common.STRING_TYPE) {
const vals = [input];
const m = /^\/(.*?)\/(g?i?m?y?)$/.exec(input);
@@ -53,10 +52,10 @@ function normalize(input) {
return input;
}
export function expandValue(value) {
export function expandValue(value): any[] {
if (common.typeOf(value) === common.ARRAY_TYPE) {
let a = [];
for (const j of value) a = a.conact(expandValue(j));
for (const j of value) a = a.concat(expandValue(j));
return [a];
} else if (common.typeOf(value) !== common.OBJECT_TYPE) {
const n = normalize(value);
@@ -89,7 +88,7 @@ export function expandValue(value) {
return objs;
}
function permute(param, val) {
function permute(param, val): any[] {
const conditions = [];
const values = expandValue(val);
@@ -104,12 +103,12 @@ function permute(param, val) {
return conditions;
}
export function expand(query) {
export function expand(query): {} {
const newQuery = {};
for (const [k, v] of Object.entries(query)) {
if (k[0] === "$") {
// Operator
newQuery[k] = v.map(e => expand(e));
newQuery[k] = (v as any[]).map(e => expand(e));
} else {
const conditions = permute(k, v);
if (conditions.length > 1) {
@@ -130,7 +129,7 @@ export function expand(query) {
return newQuery;
}
function testExpressions(params, expressions, lop) {
function testExpressions(params, expressions, lop): boolean {
for (const f of expressions) {
const res = test(params, f);
switch (lop) {
@@ -157,7 +156,7 @@ function testExpressions(params, expressions, lop) {
}
}
export function test(params, query) {
export function test(params, query): boolean {
let res;
for (const [k, v] of Object.entries(query)) {
if (k.charAt(0) === "$") {
@@ -170,7 +169,8 @@ export function test(params, query) {
// TODO comparing array to regex, array to array, and object to object
res = value.indexOf(v) !== -1;
} else {
if (common.typeOf(v) === common.REGEXP_TYPE) res = v.test(value);
if (common.typeOf(v) === common.REGEXP_TYPE)
res = (v as RegExp).test(value);
else res = v === value;
}
} else {
@@ -216,7 +216,7 @@ export function test(params, query) {
return true;
}
function matchType(src, dst) {
function matchType(src, dst): string | number | boolean {
const type = typeof src;
if (type === "string") {
return String(dst);
@@ -233,7 +233,7 @@ function matchType(src, dst) {
return dst;
}
export function testFilter(obj, filter) {
export function testFilter(obj, filter): boolean {
for (const [k, v] of Object.entries(filter)) {
const [param, op] = k.split(/([^a-zA-Z0-9\-_.].*)/, 2);
const val = matchType(obj[param], v);
@@ -264,12 +264,11 @@ export function testFilter(obj, filter) {
return true;
}
export function convertMongoQueryToFilters(query, filters) {
filters = filters || {};
export function convertMongoQueryToFilters(query, filters = {}): {} {
for (let [k, v] of Object.entries(query)) {
if (k[0] === "$") {
if (k === "$and")
for (const vv of v) convertMongoQueryToFilters(vv, filters);
for (const vv of v as any[]) convertMongoQueryToFilters(vv, filters);
else throw new Error(`Operator ${k} not supported`);
} else if (k === "_tags") {
if (common.typeOf(v) === common.OBJECT_TYPE) {
@@ -279,7 +278,7 @@ export function convertMongoQueryToFilters(query, filters) {
else throw new Error(`Operator ${kk} not supported`);
}
} else {
v = v.replace(/[^a-zA-Z0-9-]+/g, "_");
v = (v as string).replace(/[^a-zA-Z0-9-]+/g, "_");
filters[`Tags.${v}`] = true;
}
} else {
@@ -346,11 +345,11 @@ export function convertMongoQueryToFilters(query, filters) {
return filters;
}
export function sanitizeQueryTypes(query, types) {
export function sanitizeQueryTypes(query, types): {} {
for (const [k, v] of Object.entries(query)) {
if (k[0] === "$") {
// Logical operator
for (const vv of v) sanitizeQueryTypes(vv, types);
for (const vv of v as any[]) sanitizeQueryTypes(vv, types);
} else if (k in types) {
if (common.typeOf(v) === common.OBJECT_TYPE) {
for (const [kk, vv] of Object.entries(v)) {
+30 -18
View File
@@ -16,17 +16,15 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import vm from "vm";
import * as vm from "vm";
import seedrandom from "seedrandom";
import * as common from "./common";
import * as device from "./device";
import * as extensions from "./extensions";
import * as logger from "./logger";
import * as scheduling from "./scheduling";
import { ParamPath, Fault } from "./types";
// Used for throwing to exit user script and commit
const COMMIT = Symbol();
@@ -41,7 +39,7 @@ const context = vm.createContext();
let state;
const runningExtensions = new WeakMap();
function runExtension(sessionContext, key, extCall, callback) {
function runExtension(sessionContext, key, extCall, callback): void {
let re = runningExtensions.get(sessionContext);
if (!re) {
re = {};
@@ -62,13 +60,23 @@ function runExtension(sessionContext, key, extCall, callback) {
}
class SandboxDate {
constructor() {
if (arguments.length) return new Date(...arguments);
public constructor(
...argumentList: [
number?,
number?,
number?,
number?,
number?,
number?,
number?
]
) {
if (argumentList.length) return new Date(...argumentList);
return new Date(state.sessionContext.timestamp);
}
static now(intervalOrCron, variance) {
public static now(intervalOrCron, variance): number {
let t = state.sessionContext.timestamp;
if (typeof intervalOrCron === "number") {
@@ -93,7 +101,7 @@ class SandboxDate {
}
}
function random() {
function random(): number {
if (!state.rng) state.rng = seedrandom(state.sessionContext.deviceId);
return state.rng();
@@ -104,7 +112,7 @@ random.seed = function(s) {
};
class ParameterWrapper {
constructor(path, attributes, unpacked, unpackedRevision) {
public constructor(path, attributes, unpacked?, unpackedRevision?) {
for (const attrName of attributes) {
Object.defineProperty(this, attrName, {
get: function() {
@@ -189,7 +197,11 @@ class ParameterWrapper {
}
}
function declare(path, timestamps, values) {
function declare(
path: ParamPath,
timestamps: { [attr: string]: number },
values: { [attr: string]: any }
): ParameterWrapper {
state.uncommitted = true;
if (!timestamps) timestamps = {};
@@ -237,14 +249,14 @@ function declare(path, timestamps, values) {
return new ParameterWrapper(parsedPath, attrs);
}
function clear(path, timestamp, attributes) {
function clear(path, timestamp, attributes): void {
state.uncommitted = true;
if (state.revision === state.maxRevision)
state.clear.push([common.parsePath(path), timestamp, attributes]);
}
function commit() {
function commit(): void {
++state.revision;
state.uncommitted = false;
@@ -258,7 +270,7 @@ function commit() {
}
}
function ext() {
function ext(): any {
++state.extCounter;
const extCall = Array.from(arguments).map(String);
const key = `${state.revision}: ${JSON.stringify(extCall)}`;
@@ -270,7 +282,7 @@ function ext() {
throw EXT;
}
function log(msg, meta) {
function log(msg, meta): void {
if (state.revision === state.maxRevision && state.extCounter >= 0) {
const details = Object.assign({}, meta, {
sessionContext: state.sessionContext,
@@ -300,12 +312,12 @@ context.random = random;
vm.runInContext("Math.random = random;", context);
delete context.random;
function errorToFault(err) {
function errorToFault(err): Fault {
if (!err) return null;
if (!err.name) return { code: "script", message: `${err}` };
const fault = {
const fault: Fault = {
code: `script.${err.name}`,
message: err.message,
detail: {
@@ -335,7 +347,7 @@ export function run(
maxRevision,
callback,
extCounter = 0
) {
): void {
state = {
sessionContext: sessionContext,
revision: startRevision,
+7 -9
View File
@@ -16,13 +16,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import crypto from "crypto";
import * as crypto from "crypto";
import * as later from "later";
import later from "later";
function md532(str) {
function md532(str): number {
const digest = crypto
.createHash("md5")
.update(str)
@@ -35,22 +33,22 @@ function md532(str) {
);
}
export function variance(deviceId, vrnc) {
export function variance(deviceId, vrnc): number {
return (md532(deviceId) >>> 0) % vrnc;
}
export function interval(timestamp, intrvl, offset = 0) {
export function interval(timestamp, intrvl, offset = 0): number {
return Math.trunc((timestamp + offset) / intrvl) * intrvl - offset;
}
export function parseCron(cronExp) {
export function parseCron(cronExp): any {
const parts = cronExp.trim().split(/\s+/);
if (parts.length === 5) parts.unshift("*");
return later.schedule(later.parse.cron(parts.join(" "), true));
}
export function cron(timestamp, schedule, offset = 0) {
export function cron(timestamp, schedule, offset = 0): number[] {
// TODO later.js doesn't throw erorr if expression is invalid!
const ret = [0, 0];
+16 -9
View File
@@ -16,15 +16,15 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import fs from "fs";
import http from "http";
import https from "https";
import * as fs from "fs";
import * as http from "http";
import * as https from "https";
let server, listener;
let server: http.Server | https.Server;
let listener: (...args) => void;
function closeServer(timeout, callback) {
function closeServer(timeout, callback): void {
if (!server) return void callback();
setTimeout(() => {
@@ -48,13 +48,20 @@ function closeServer(timeout, callback) {
});
}
export function start(port, networkInterface, ssl, _listener, onConnection) {
export function start(
port,
networkInterface,
ssl,
_listener,
onConnection?
): void {
listener = _listener;
if (ssl) {
const options = {
key: fs.readFileSync(ssl.key),
cert: fs.readFileSync(ssl.cert)
cert: fs.readFileSync(ssl.cert),
ca: null
};
try {
@@ -76,7 +83,7 @@ export function start(port, networkInterface, ssl, _listener, onConnection) {
server.listen(port, networkInterface);
}
export function stop() {
export function stop(): Promise<void> {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error("Could not close server in a timely manner"));
+203 -96
View File
@@ -16,7 +16,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import * as config from "./config";
import * as common from "./common";
@@ -29,8 +28,20 @@ import { estimateGpnCount } from "./gpn-heuristic";
import PathSet from "./path-set";
import VersionedMap from "./versioned-map";
import InstanceSet from "./instance-set";
import {
Attributes,
SessionContext,
ParamPath,
DeviceData,
VirtualParameterDeclaration,
GetAcsRequest,
SetAcsRequest,
AttributeTimestamps,
AttributeValues,
Fault
} from "./types";
function initDeviceData() {
function initDeviceData(): DeviceData {
return {
paths: new PathSet(),
timestamps: new VersionedMap(),
@@ -41,9 +52,14 @@ function initDeviceData() {
};
}
export function init(deviceId, cwmpVersion, timeout, callback) {
export function init(
deviceId: string,
cwmpVersion: string,
timeout: number,
callback: (err: Error, sessionContext: SessionContext) => void
): void {
const timestamp = Date.now();
const sessionContext = {
const sessionContext: SessionContext = {
timestamp: timestamp,
deviceId: deviceId,
deviceData: initDeviceData(),
@@ -63,7 +79,7 @@ export function init(deviceId, cwmpVersion, timeout, callback) {
callback(null, sessionContext);
}
function loadParameters(sessionContext, callback) {
function loadParameters(sessionContext: SessionContext, callback): void {
if (!sessionContext.toLoad || !sessionContext.toLoad.size)
return void callback();
@@ -79,7 +95,7 @@ function loadParameters(sessionContext, callback) {
// Device not available in database, mark as new
sessionContext.new = true;
loaded = [
[[], (1 << config.get("MAX_DEPTH", sessionContext.deviceId)) - 1]
[[], (1 << +config.get("MAX_DEPTH", sessionContext.deviceId)) - 1]
];
parameters = [];
}
@@ -104,7 +120,7 @@ function loadParameters(sessionContext, callback) {
);
}
function generateRpcId(sessionContext) {
function generateRpcId(sessionContext: SessionContext): string {
return (
sessionContext.timestamp.toString(16) +
("0" + sessionContext.cycle.toString(16)).slice(-2) +
@@ -112,11 +128,11 @@ function generateRpcId(sessionContext) {
);
}
export function inform(sessionContext, rpcReq, callback) {
export function inform(sessionContext: SessionContext, rpcReq, callback): void {
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
const params = [
const params: [ParamPath, number, Attributes][] = [
[
["DeviceID", "Manufacturer"],
["DeviceID", "Manufacturer"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -126,7 +142,7 @@ export function inform(sessionContext, rpcReq, callback) {
],
[
["DeviceID", "OUI"],
["DeviceID", "OUI"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -136,7 +152,7 @@ export function inform(sessionContext, rpcReq, callback) {
],
[
["DeviceID", "ProductClass"],
["DeviceID", "ProductClass"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -146,7 +162,7 @@ export function inform(sessionContext, rpcReq, callback) {
],
[
["DeviceID", "SerialNumber"],
["DeviceID", "SerialNumber"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -169,7 +185,7 @@ export function inform(sessionContext, rpcReq, callback) {
}
params.push([
["Events", "Inform"],
["Events", "Inform"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -180,7 +196,7 @@ export function inform(sessionContext, rpcReq, callback) {
for (const e of rpcReq.event) {
params.push([
["Events", e.replace(/\s+/g, "_")],
["Events", e.replace(/\s+/g, "_")] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -191,7 +207,7 @@ export function inform(sessionContext, rpcReq, callback) {
}
// Preload DeviceID params
loadPath(sessionContext, ["DeviceID", "*"]);
loadPath(sessionContext, ["DeviceID", "*"] as ParamPath);
for (const p of params) loadPath(sessionContext, p[0]);
@@ -200,7 +216,7 @@ export function inform(sessionContext, rpcReq, callback) {
if (sessionContext.new) {
params.push([
["DeviceID", "ID"],
["DeviceID", "ID"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -209,7 +225,7 @@ export function inform(sessionContext, rpcReq, callback) {
}
]);
params.push([
["Events", "Registered"],
["Events", "Registered"] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -244,7 +260,11 @@ export function inform(sessionContext, rpcReq, callback) {
});
}
export function transferComplete(sessionContext, rpcReq, callback) {
export function transferComplete(
sessionContext: SessionContext,
rpcReq,
callback
): void {
const revision =
(sessionContext.revisions[sessionContext.revisions.length - 1] || 0) + 1;
sessionContext.deviceData.timestamps.revision = revision;
@@ -278,7 +298,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
);
}
loadPath(sessionContext, ["Downloads", instance, "*"]);
loadPath(sessionContext, ["Downloads", instance, "*"] as ParamPath);
loadParameters(sessionContext, err => {
if (err) return void callback(err);
@@ -292,7 +312,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
"Downloads",
instance,
"LastDownload"
]);
] as ParamPath);
toClear = device.set(
sessionContext.deviceData,
p,
@@ -305,7 +325,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
"Downloads",
instance,
"LastFileType"
]);
] as ParamPath);
toClear = device.set(
sessionContext.deviceData,
p,
@@ -318,7 +338,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
"Downloads",
instance,
"LastFileName"
]);
] as ParamPath);
toClear = device.set(
sessionContext.deviceData,
p,
@@ -331,7 +351,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
"Downloads",
instance,
"LastTargetFileName"
]);
] as ParamPath);
toClear = device.set(
sessionContext.deviceData,
p,
@@ -344,7 +364,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
"Downloads",
instance,
"StartTime"
]);
] as ParamPath);
toClear = device.set(
sessionContext.deviceData,
p,
@@ -357,7 +377,7 @@ export function transferComplete(sessionContext, rpcReq, callback) {
"Downloads",
instance,
"CompleteTime"
]);
] as ParamPath);
toClear = device.set(
sessionContext.deviceData,
p,
@@ -372,8 +392,12 @@ export function transferComplete(sessionContext, rpcReq, callback) {
});
}
function revertDownloadParameters(sessionContext, instance, callback) {
loadPath(sessionContext, ["Downloads", instance, "*"]);
function revertDownloadParameters(
sessionContext: SessionContext,
instance,
callback
): void {
loadPath(sessionContext, ["Downloads", instance, "*"] as ParamPath);
loadParameters(sessionContext, err => {
if (err) return void callback(err);
@@ -386,7 +410,7 @@ function revertDownloadParameters(sessionContext, instance, callback) {
"Downloads",
instance,
"LastDownload"
]);
] as ParamPath);
const lastDownload = sessionContext.deviceData.attributes.get(p);
@@ -394,7 +418,7 @@ function revertDownloadParameters(sessionContext, instance, callback) {
"Downloads",
instance,
"Download"
]);
] as ParamPath);
const toClear = device.set(sessionContext.deviceData, p, timestamp, {
value: [
@@ -410,7 +434,10 @@ function revertDownloadParameters(sessionContext, instance, callback) {
});
}
export function timeoutOperations(sessionContext, callback) {
export function timeoutOperations(
sessionContext: SessionContext,
callback
): void {
const revision =
(sessionContext.revisions[sessionContext.revisions.length - 1] || 0) + 1;
sessionContext.deviceData.timestamps.revision = revision;
@@ -427,7 +454,7 @@ export function timeoutOperations(sessionContext, callback) {
}
const DOWNLOAD_TIMEOUT =
localCache.getConfig(
+localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.downloadTimeout",
sessionContext.configContext
@@ -460,11 +487,15 @@ export function timeoutOperations(sessionContext, callback) {
if ((counter -= 2) === 1) callback(null, faults, operations);
}
export function addProvisions(sessionContext, channel, provisions) {
export function addProvisions(
sessionContext: SessionContext,
channel,
provisions
): void {
// Multiply by two because every iteration is two
// phases: read and update
const MAX_ITERATIONS =
localCache.getConfig(
+localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.maxCommitIterations",
sessionContext.configContext
@@ -513,11 +544,11 @@ export function addProvisions(sessionContext, channel, provisions) {
}
}
export function clearProvisions(sessionContext) {
export function clearProvisions(sessionContext: SessionContext): void {
// Multiply by two because every iteration is two
// phases: read and update
const MAX_ITERATIONS =
localCache.getConfig(
+localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.maxCommitIterations",
sessionContext.configContext
@@ -551,7 +582,7 @@ function runProvisions(
startRevision,
endRevision,
callback
) {
): void {
let done = true;
let allDeclarations = [];
let allClear = [];
@@ -616,7 +647,7 @@ function runVirtualParameters(
startRevision,
endRevision,
callback
) {
): void {
let done = true;
const virtualParameterUpdates = [];
let allDeclarations = [];
@@ -660,7 +691,11 @@ function runVirtualParameters(
return void (counter = 0);
}
const ret = {};
const ret: {
writable?: number;
value?: [string | number | boolean, string?];
} = {};
if (_returnValue.writable != null) {
ret.writable = +!!_returnValue.writable;
} else if (
@@ -758,7 +793,10 @@ function runVirtualParameters(
}
}
function runDeclarations(sessionContext, declarations) {
function runDeclarations(
sessionContext: SessionContext,
declarations
): VirtualParameterDeclaration[] {
if (!sessionContext.syncState) {
sessionContext.syncState = {
refreshAttributes: {
@@ -768,7 +806,7 @@ function runDeclarations(sessionContext, declarations) {
value: new Set()
},
spv: new Map(),
gpn: new Set(),
gpn: new Set<ParamPath>(),
gpnPatterns: new Map(),
tags: new Map(),
virtualParameterDeclarations: [],
@@ -782,14 +820,20 @@ function runDeclarations(sessionContext, declarations) {
}
const allDeclareTimestamps = new Map();
const allDeclareAttributeTimestamps = new Map();
const allDeclareAttributeValues = new Map();
const allDeclareAttributeTimestamps = new Map<
ParamPath,
AttributeTimestamps
>();
const allDeclareAttributeValues = new Map<ParamPath, AttributeValues>();
const allVirtualParameters = localCache.getVirtualParameters(
sessionContext.cacheSnapshot
);
function mergeAttributeTimestamps(p, attrs) {
function mergeAttributeTimestamps(
p: ParamPath,
attrs: AttributeTimestamps
): void {
let cur = allDeclareAttributeTimestamps.get(p);
if (!cur) {
allDeclareAttributeTimestamps.set(p, attrs);
@@ -801,7 +845,11 @@ function runDeclarations(sessionContext, declarations) {
}
}
function mergeAttributeValues(p, attrs, defer) {
function mergeAttributeValues(
p: ParamPath,
attrs: AttributeValues,
defer: boolean
): void {
let cur = allDeclareAttributeValues.get(p);
if (!cur) {
if (!defer) allDeclareAttributeValues.set(p, attrs);
@@ -816,19 +864,26 @@ function runDeclarations(sessionContext, declarations) {
let unpacked;
if ((path.alias | path.wildcard) & 1 || path[0] === "VirtualParameters") {
sessionContext.deviceData.paths.add(["VirtualParameters"]);
sessionContext.deviceData.paths.add(["VirtualParameters"] as ParamPath);
if ((path.alias | path.wildcard) & 2) {
sessionContext.deviceData.paths.add(["VirtualParameters", "*"]);
for (const k of Object.keys(allVirtualParameters))
sessionContext.deviceData.paths.add(["VirtualParameters", k]);
sessionContext.deviceData.paths.add([
"VirtualParameters",
"*"
] as ParamPath);
for (const k of Object.keys(allVirtualParameters)) {
sessionContext.deviceData.paths.add([
"VirtualParameters",
k
] as ParamPath);
}
}
}
if ((path.alias | path.wildcard) & 1 || path[0] === "Reboot")
sessionContext.deviceData.paths.add(["Reboot"]);
sessionContext.deviceData.paths.add(["Reboot"] as ParamPath);
if ((path.alias | path.wildcard) & 1 || path[0] === "FactoryReset")
sessionContext.deviceData.paths.add(["FactoryReset"]);
sessionContext.deviceData.paths.add(["FactoryReset"] as ParamPath);
if (path.alias) {
const aliasDecs = device.getAliasDeclarations(
@@ -958,7 +1013,11 @@ function runDeclarations(sessionContext, declarations) {
);
}
export function rpcRequest(sessionContext, _declarations, callback) {
export function rpcRequest(
sessionContext: SessionContext,
_declarations,
callback
): void {
if (sessionContext.rpcRequest != null) {
return void callback(
null,
@@ -1097,7 +1156,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
// Multiply by two because every iteration is two
// phases: read and update
const MAX_ITERATIONS =
localCache.getConfig(
+localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.maxCommitIterations",
sessionContext.configContext
@@ -1162,7 +1221,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
for (const k of Object.keys(allVirtualParameters)) {
toClear = device.set(
sessionContext.deviceData,
["VirtualParameters", k],
["VirtualParameters", k] as ParamPath,
timestamp,
{
object: [timestamp, 0]
@@ -1272,7 +1331,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
if (index == null) {
index = 0;
for (const p of sessionContext.deviceData.paths.find(
["Downloads", "*"],
["Downloads", "*"] as ParamPath,
false,
true
)) {
@@ -1285,7 +1344,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads"],
["Downloads"] as ParamPath,
timestamp,
{ object: [timestamp, 1], writable: [timestamp, 1] },
toClear
@@ -1293,7 +1352,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", "" + index],
["Downloads", "" + index] as ParamPath,
timestamp,
{ object: [timestamp, 1], writable: [timestamp, 1] },
toClear
@@ -1327,7 +1386,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
for (const [k, v] of Object.entries(params)) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", `${index}`, k],
["Downloads", `${index}`, k] as ParamPath,
timestamp,
{
object: [timestamp, 0],
@@ -1340,7 +1399,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", `${index}`, "*"],
["Downloads", `${index}`, "*"] as ParamPath,
timestamp,
null,
toClear
@@ -1445,7 +1504,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
toClear = device.set(
sessionContext.deviceData,
["VirtualParameters", vparams[i][0]],
["VirtualParameters", vparams[i][0]] as ParamPath,
timestamp,
vpu,
toClear
@@ -1458,7 +1517,7 @@ export function rpcRequest(sessionContext, _declarations, callback) {
});
}
function generateGetRpcRequest(sessionContext) {
function generateGetRpcRequest(sessionContext: SessionContext): GetAcsRequest {
const syncState = sessionContext.syncState;
if (!syncState) return null;
@@ -1482,7 +1541,10 @@ function generateGetRpcRequest(sessionContext) {
}
if (!found) {
const p = sessionContext.deviceData.paths.add(path.slice(0, -1));
const p = sessionContext.deviceData.paths.add(path.slice(
0,
-1
) as ParamPath);
syncState.gpn.add(p);
const f = 1 << p.length;
syncState.gpnPatterns.set(p, f | syncState.gpnPatterns.get(p));
@@ -1510,7 +1572,10 @@ function generateGetRpcRequest(sessionContext) {
}
if (!found) {
const p = sessionContext.deviceData.paths.add(path.slice(0, -1));
const p = sessionContext.deviceData.paths.add(path.slice(
0,
-1
) as ParamPath);
syncState.gpn.add(p);
const f = 1 << p.length;
syncState.gpnPatterns.set(p, f | syncState.gpnPatterns.get(p));
@@ -1519,7 +1584,10 @@ function generateGetRpcRequest(sessionContext) {
syncState.refreshAttributes.object.clear();
for (const path of syncState.refreshAttributes.writable) {
const p = sessionContext.deviceData.paths.add(path.slice(0, -1));
const p = sessionContext.deviceData.paths.add(path.slice(
0,
-1
) as ParamPath);
syncState.gpn.add(p);
const f = 1 << p.length;
syncState.gpnPatterns.set(p, f | syncState.gpnPatterns.get(p));
@@ -1613,7 +1681,7 @@ function generateGetRpcRequest(sessionContext) {
return null;
}
function generateSetRpcRequest(sessionContext) {
function generateSetRpcRequest(sessionContext): SetAcsRequest {
const syncState = sessionContext.syncState;
if (!syncState) return null;
@@ -1653,19 +1721,23 @@ function generateSetRpcRequest(sessionContext) {
sessionContext.configContext
);
const DATETIME_MILLISECONDS = localCache.getConfig(
const DATETIME_MILLISECONDS = !!localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.datetimeMilliseconds",
sessionContext.configContext
);
const BOOLEAN_LITERAL = localCache.getConfig(
const BOOLEAN_LITERAL = !!localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.booleanLiteral",
sessionContext.configContext
);
const parameterValues = [];
const parameterValues = [] as [
ParamPath,
string | number | boolean,
string
][];
for (const [k, v] of syncState.spv) {
syncState.spv.delete(k);
const attrs = sessionContext.deviceData.attributes.get(k);
@@ -1693,7 +1765,14 @@ function generateSetRpcRequest(sessionContext) {
if (parameterValues.length) {
return {
name: "SetParameterValues",
parameterList: parameterValues.map(p => [p[0].join("."), p[1], p[2]]),
parameterList: parameterValues.map(
p =>
[p[0].join("."), p[1], p[2]] as [
string,
string | number | boolean,
string
]
),
DATETIME_MILLISECONDS: DATETIME_MILLISECONDS,
BOOLEAN_LITERAL: BOOLEAN_LITERAL
};
@@ -1764,7 +1843,13 @@ function generateSetRpcRequest(sessionContext) {
function generateGetVirtualParameterProvisions(
sessionContext,
virtualParameterDeclarations
) {
): [
ParamPath,
AttributeTimestamps,
AttributeValues,
AttributeTimestamps,
AttributeValues
][] {
let provisions;
if (virtualParameterDeclarations) {
for (const declaration of virtualParameterDeclarations) {
@@ -1804,7 +1889,13 @@ function generateGetVirtualParameterProvisions(
function generateSetVirtualParameterProvisions(
sessionContext,
virtualParameterDeclarations
) {
): [
ParamPath,
AttributeTimestamps,
AttributeValues,
AttributeTimestamps,
AttributeValues
][] {
let provisions;
if (virtualParameterDeclarations) {
for (const declaration of virtualParameterDeclarations) {
@@ -1850,21 +1941,27 @@ function generateSetVirtualParameterProvisions(
function processDeclarations(
sessionContext,
allDeclareTimestamps,
allDeclareAttributeTimestamps,
allDeclareAttributeValues
) {
allDeclareAttributeTimestamps: Map<ParamPath, AttributeTimestamps>,
allDeclareAttributeValues: Map<ParamPath, AttributeValues>
): VirtualParameterDeclaration[] {
const deviceData = sessionContext.deviceData;
const syncState = sessionContext.syncState;
const root = sessionContext.deviceData.paths.add([]);
const paths = deviceData.paths.find([], false, true, 99);
paths.sort((a, b) =>
a.wildcard === b.wildcard ? a.length - b.length : a.wildcard - b.wildcard
paths.sort(
(a, b): number =>
a.wildcard === b.wildcard ? a.length - b.length : a.wildcard - b.wildcard
);
const virtualParameterDeclarations = [];
const virtualParameterDeclarations = [] as VirtualParameterDeclaration[];
function func(leafParam, leafIsObject, leafTimestamp, _paths) {
function func(
leafParam: ParamPath,
leafIsObject: number,
leafTimestamp: number,
_paths: ParamPath[]
): void {
const currentPath = _paths[0];
const children = new Map();
let declareTimestamp = 0;
@@ -2122,7 +2219,11 @@ function processDeclarations(
return virtualParameterDeclarations;
}
function loadPath(sessionContext, path, depth) {
function loadPath(
sessionContext: SessionContext,
path: ParamPath,
depth?: number
): boolean {
depth = depth || (1 << path.length) - 1;
if (sessionContext.new || !depth) return true;
@@ -2132,12 +2233,13 @@ function loadPath(sessionContext, path, depth) {
let trimWildcard = path.length;
while (trimWildcard && path[trimWildcard - 1] === "*") --trimWildcard;
if (trimWildcard < path.length) path = path.slice(0, trimWildcard);
if (trimWildcard < path.length)
path = path.slice(0, trimWildcard) as ParamPath;
for (let i = 0; i <= path.length; ++i) {
const d = i === path.length ? 99 : i;
for (const sup of sessionContext.deviceData.paths.find(
path.slice(0, i),
path.slice(0, i) as ParamPath,
true,
false,
d
@@ -2165,7 +2267,7 @@ function processInstances(
minInstances,
maxInstances,
defer
) {
): void {
let instancesToCreate, instancesToDelete;
if (parent[0] === "Downloads") {
if (parent.length !== 1) return;
@@ -2217,10 +2319,10 @@ function processInstances(
}
}
function clear(sessionContext, toClear, callback) {
function clear(sessionContext, toClear, callback): void {
if (!toClear || !toClear.length) return void callback();
const MAX_DEPTH = config.get("MAX_DEPTH", sessionContext.deviceId);
const MAX_DEPTH = +config.get("MAX_DEPTH", sessionContext.deviceId);
for (const c of toClear) {
if (c[1]) {
@@ -2250,7 +2352,7 @@ function clear(sessionContext, toClear, callback) {
});
}
export function rpcResponse(sessionContext, id, rpcRes, callback) {
export function rpcResponse(sessionContext, id, rpcRes, callback): void {
if (id !== generateRpcId(sessionContext))
return void callback(new Error("Request ID not recognized"));
@@ -2398,7 +2500,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
if (rpcReq.nextLevel) {
loadPath(sessionContext, root, (1 << (root.length + 1)) - 1);
} else {
const MAX_DEPTH = config.get("MAX_DEPTH", sessionContext.deviceId);
const MAX_DEPTH = +config.get("MAX_DEPTH", sessionContext.deviceId);
loadPath(sessionContext, root, (1 << MAX_DEPTH) - 1);
}
@@ -2495,7 +2597,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
case "DownloadResponse":
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "Download"],
["Downloads", rpcReq.instance, "Download"] as ParamPath,
timestamp + 1,
{ value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]] },
toClear
@@ -2504,7 +2606,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
if (rpcRes.status === 0) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "LastDownload"],
["Downloads", rpcReq.instance, "LastDownload"] as ParamPath,
timestamp + 1,
{
value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]]
@@ -2514,7 +2616,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "LastFileType"],
["Downloads", rpcReq.instance, "LastFileType"] as ParamPath,
timestamp + 1,
{ value: [timestamp + 1, [rpcReq.fileType, "xsd:string"]] },
toClear
@@ -2522,7 +2624,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "LastFileName"],
["Downloads", rpcReq.instance, "LastFileName"] as ParamPath,
timestamp + 1,
{ value: [timestamp + 1, [rpcReq.fileType, "xsd:string"]] },
toClear
@@ -2530,7 +2632,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "LastTargetFileName"],
["Downloads", rpcReq.instance, "LastTargetFileName"] as ParamPath,
timestamp + 1,
{ value: [timestamp + 1, [rpcReq.fileType, "xsd:string"]] },
toClear
@@ -2538,7 +2640,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "StartTime"],
["Downloads", rpcReq.instance, "StartTime"] as ParamPath,
timestamp + 1,
{ value: [timestamp + 1, [+rpcRes.startTime, "xsd:dateTime"]] },
toClear
@@ -2546,7 +2648,7 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
toClear = device.set(
sessionContext.deviceData,
["Downloads", rpcReq.instance, "CompleteTime"],
["Downloads", rpcReq.instance, "CompleteTime"] as ParamPath,
timestamp + 1,
{ value: [timestamp + 1, [+rpcRes.completeTime, "xsd:dateTime"]] },
toClear
@@ -2583,7 +2685,12 @@ export function rpcResponse(sessionContext, id, rpcRes, callback) {
clear(sessionContext, toClear, callback);
}
export function rpcFault(sessionContext, id, faultResponse, callback) {
export function rpcFault(
sessionContext,
id,
faultResponse,
callback: (err: Error, fault?: Fault) => void
): void {
const rpcReq = sessionContext.rpcRequest;
delete sessionContext.syncState;
delete sessionContext.rpcRequest;
@@ -2627,7 +2734,7 @@ export function rpcFault(sessionContext, id, faultResponse, callback) {
callback(null, fault);
}
export function deserialize(sessionContextString, callback) {
export function deserialize(sessionContextString, callback): void {
const sessionContext = JSON.parse(sessionContextString);
for (const decs of sessionContext.declarations)
@@ -2654,7 +2761,7 @@ export function deserialize(sessionContextString, callback) {
});
}
export function serialize(sessionContext, callback) {
export function serialize(sessionContext, callback): void {
const deviceData = [];
for (const path of sessionContext.deviceData.paths.find(
+93 -54
View File
@@ -16,11 +16,21 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
import libxmljs from "libxmljs";
import { parseXml, Document, Element, ParserOptions } from "libxmljs";
import * as config from "./config";
import { version as VERSION } from "../package.json";
import {
CpeResponse,
CpeGetResponse,
CpeSetResponse,
InformRequest,
AcsResponse,
CpeRequest,
FaultStruct,
SpvFault,
CpeFault
} from "./types";
const SERVER_NAME = `GenieACS/${VERSION}`;
@@ -62,21 +72,36 @@ const NAMESPACES = {
}
};
const LIBXMLJS_PARSE_OPTIONS = {
const LIBXMLJS_PARSE_OPTIONS: ParserOptions = {
nocdata: true,
recover: config.get("XML_RECOVER"),
ignore_enc: config.get("XML_IGNORE_ENC")
recover: !!config.get("XML_RECOVER"),
/* eslint-disable-next-line @typescript-eslint/camelcase */
ignore_enc: !!config.get("XML_IGNORE_ENC")
};
const LIBXMLJS_SAVE_OPTIONS = {
format: config.get("XML_FORMAT"),
interface WriterOptions {
declaration?: boolean;
format?: boolean;
selfCloseEmpty?: boolean;
whitespace?: boolean;
type?: "XML" | "HTML" | "XHTML";
}
declare module "libxmljs" {
interface Document {
toString(formatting: boolean | WriterOptions): string;
}
}
const LIBXMLJS_SAVE_OPTIONS: WriterOptions = {
format: !!config.get("XML_FORMAT"),
declaration: !config.get("XML_NO_DECL"),
selfCloseEmpty: !config.get("XML_NO_EMPTY")
};
let warnings;
function parseBool(v) {
function parseBool(v): boolean {
v = "" + v;
if (v === "true" || v === "TRUE" || v === "True" || v === "1") return true;
else if (v === "false" || v === "FALSE" || v === "False" || v === "0")
@@ -84,7 +109,7 @@ function parseBool(v) {
else return null;
}
function event(xml) {
function event(xml): string[] {
return xml
.childNodes()
.filter(n => n.name() === "EventStruct")
@@ -97,7 +122,7 @@ function event(xml) {
);
}
function parameterInfoList(xml) {
function parameterInfoList(xml): [string, boolean][] {
return xml
.childNodes()
.filter(e => e.name() === "ParameterInfoStruct")
@@ -128,7 +153,9 @@ function parameterInfoList(xml) {
});
}
function parameterValueList(xml) {
function parameterValueList(
xml
): [string, string | number | boolean, string][] {
return xml
.childNodes()
.filter(e => e.name() === "ParameterValueStruct")
@@ -181,13 +208,13 @@ function parameterValueList(xml) {
});
}
function GetParameterNames(xml, methodRequest) {
function GetParameterNames(xml, methodRequest): void {
const el = xml.node("cwmp:GetParameterNames");
el.node("ParameterPath").text(methodRequest.parameterPath);
el.node("NextLevel").text(+methodRequest.nextLevel);
}
function GetParameterNamesResponse(xml) {
function GetParameterNamesResponse(xml): CpeGetResponse {
return {
name: "GetParameterNamesResponse",
parameterList: parameterInfoList(
@@ -196,7 +223,7 @@ function GetParameterNamesResponse(xml) {
};
}
function GetParameterValues(xml, methodRequest) {
function GetParameterValues(xml, methodRequest): void {
const el = xml.node("cwmp:GetParameterValues").node("ParameterNames");
el.attr({
"soap-enc:arrayType": `xsd:string[${methodRequest.parameterNames.length}]`
@@ -205,7 +232,7 @@ function GetParameterValues(xml, methodRequest) {
for (const p of methodRequest.parameterNames) el.node("string").text(p);
}
function GetParameterValuesResponse(xml) {
function GetParameterValuesResponse(xml): CpeGetResponse {
return {
name: "GetParameterValuesResponse",
parameterList: parameterValueList(
@@ -214,7 +241,7 @@ function GetParameterValuesResponse(xml) {
};
}
function SetParameterValues(xml, methodRequest) {
function SetParameterValues(xml, methodRequest): void {
const el = xml.node("cwmp:SetParameterValues");
const paramList = el.node("ParameterList");
paramList.attr({
@@ -244,7 +271,7 @@ function SetParameterValues(xml, methodRequest) {
el.node("ParameterKey").text(methodRequest.parameterKey || "");
}
function SetParameterValuesResponse(xml) {
function SetParameterValuesResponse(xml): CpeSetResponse {
return {
name: "SetParameterValuesResponse",
status: parseInt(
@@ -256,13 +283,13 @@ function SetParameterValuesResponse(xml) {
};
}
function AddObject(xml, methodRequest) {
function AddObject(xml, methodRequest): void {
const el = xml.node("cwmp:AddObject");
el.node("ObjectName").text(methodRequest.objectName);
el.node("ParameterKey").text(methodRequest.parameterKey || "");
}
function AddObjectResponse(xml) {
function AddObjectResponse(xml): CpeSetResponse {
let instanceNumber, status;
for (const c of xml.childNodes()) {
switch (c.name()) {
@@ -282,13 +309,13 @@ function AddObjectResponse(xml) {
};
}
function DeleteObject(xml, methodRequest) {
function DeleteObject(xml, methodRequest): void {
const el = xml.node("cwmp:DeleteObject");
el.node("ObjectName").text(methodRequest.objectName);
el.node("ParameterKey").text(methodRequest.parameterKey || "");
}
function DeleteObjectResponse(xml) {
function DeleteObjectResponse(xml): CpeSetResponse {
return {
name: "DeleteObjectResponse",
status: parseInt(
@@ -300,28 +327,28 @@ function DeleteObjectResponse(xml) {
};
}
function Reboot(xml, methodRequest) {
function Reboot(xml, methodRequest): void {
const el = xml.node("cwmp:Reboot");
el.node("CommandKey").text(methodRequest.commandKey || "");
}
function RebootResponse() {
function RebootResponse(): CpeSetResponse {
return {
name: "RebootResponse"
};
}
function FactoryReset(xml) {
function FactoryReset(xml): void {
xml.node("cwmp:FactoryReset");
}
function FactoryResetResponse() {
function FactoryResetResponse(): CpeSetResponse {
return {
name: "FactoryResetResponse"
};
}
function Download(xml, methodRequest) {
function Download(xml, methodRequest): void {
const el = xml.node("cwmp:Download");
el.node("CommandKey").text(methodRequest.commandKey || "");
el.node("FileType").text(methodRequest.fileType);
@@ -335,7 +362,7 @@ function Download(xml, methodRequest) {
el.node("FailureURL").text(methodRequest.failureUrl || "");
}
function DownloadResponse(xml) {
function DownloadResponse(xml): CpeSetResponse {
let status, startTime, completeTime;
for (const c of xml.childNodes()) {
switch (c.name()) {
@@ -359,7 +386,7 @@ function DownloadResponse(xml) {
};
}
function Inform(xml) {
function Inform(xml): InformRequest {
let retryCount, evnt, parameterList;
const deviceId = {
Manufacturer: null,
@@ -397,18 +424,18 @@ function Inform(xml) {
};
}
function InformResponse(xml) {
function InformResponse(xml): void {
xml
.node("cwmp:InformResponse")
.node("MaxEnvelopes")
.text(1);
}
function GetRPCMethods() {
function GetRPCMethods(): AcsResponse {
return { name: "GetRPCMethods" };
}
function GetRPCMethodsResponse(xml, methodResponse) {
function GetRPCMethodsResponse(xml, methodResponse): void {
const el = xml.node("cwmp:GetRPCMethodsResponse").node("MethodList");
el.attr({
"soap-enc:arrayType": `xsd:string[${methodResponse.methodList.length}]`
@@ -417,7 +444,7 @@ function GetRPCMethodsResponse(xml, methodResponse) {
for (const m of methodResponse.methodList) el.node("string").text(m);
}
function TransferComplete(xml) {
function TransferComplete(xml): AcsResponse {
let commandKey, _faultStruct, startTime, completeTime;
for (const c of xml.childNodes()) {
switch (c.name()) {
@@ -445,11 +472,11 @@ function TransferComplete(xml) {
};
}
function TransferCompleteResponse(xml) {
function TransferCompleteResponse(xml): void {
xml.node("cwmp:TransferCompleteResponse").text("");
}
function RequestDownload(xml) {
function RequestDownload(xml): CpeRequest {
return {
name: "RequestDownload",
fileType: xml
@@ -459,12 +486,12 @@ function RequestDownload(xml) {
};
}
function RequestDownloadResponse(xml) {
function RequestDownloadResponse(xml): void {
xml.node("cwmp:RequestDownloadResponse").text("");
}
function faultStruct(xml) {
let faultCode, faultString, setParameterValuesFault, pn, fc, fs;
function faultStruct(xml): FaultStruct {
let faultCode, faultString, setParameterValuesFault: SpvFault[], pn, fc, fs;
for (const c of xml.childNodes()) {
switch (c.name()) {
case "FaultCode":
@@ -499,7 +526,7 @@ function faultStruct(xml) {
return { faultCode, faultString, setParameterValuesFault };
}
function fault(xml) {
function fault(xml): CpeFault {
let faultCode, faultString, detail;
for (const c of xml.childNodes()) {
switch (c.name()) {
@@ -518,7 +545,18 @@ function fault(xml) {
return { faultCode, faultString, detail };
}
export function request(data, cwmpVersion, warn) {
export function request(
data,
cwmpVersion,
warn
): {
id: string;
cwmpVersion: string;
sessionTimeout: number;
cpeRequest?: CpeRequest;
cpeFault?: CpeFault;
cpeResponse?: CpeResponse;
} {
warnings = warn;
const rpc = {
@@ -526,12 +564,13 @@ export function request(data, cwmpVersion, warn) {
cwmpVersion: cwmpVersion,
sessionTimeout: null,
cpeRequest: null,
cpeFault: null
cpeFault: null,
cpeResponse: null
};
if (!data.length) return rpc;
const xml = libxmljs.parseXml(data, LIBXMLJS_PARSE_OPTIONS);
const xml = parseXml(data, LIBXMLJS_PARSE_OPTIONS);
let headerElement, bodyElement;
@@ -589,7 +628,7 @@ export function request(data, cwmpVersion, warn) {
rpc.cpeRequest = Inform(methodElement);
break;
case "GetRPCMethods":
rpc.cpeRequest = GetRPCMethods(methodElement);
rpc.cpeRequest = GetRPCMethods();
break;
case "TransferComplete":
rpc.cpeRequest = TransferComplete(methodElement);
@@ -613,16 +652,16 @@ export function request(data, cwmpVersion, warn) {
rpc.cpeResponse = DeleteObjectResponse(methodElement);
break;
case "RebootResponse":
rpc.cpeResponse = RebootResponse(methodElement);
rpc.cpeResponse = RebootResponse();
break;
case "FactoryResetResponse":
rpc.cpeResponse = FactoryResetResponse(methodElement);
rpc.cpeResponse = FactoryResetResponse();
break;
case "DownloadResponse":
rpc.cpeResponse = DownloadResponse(methodElement);
break;
case "Fault":
rpc.cpeFault = fault(methodElement, rpc.cwmpVersion);
rpc.cpeFault = fault(methodElement);
break;
default:
throw new Error(`8000 Method not supported ${methodName}`);
@@ -632,8 +671,8 @@ export function request(data, cwmpVersion, warn) {
return rpc;
}
function createSoapEnv(cwmpVersion) {
const xml = libxmljs.Document();
function createSoapEnv(cwmpVersion: string): Element {
const xml = new Document();
const env = xml.node("soap-env:Envelope");
for (const prefix of Object.keys(NAMESPACES[cwmpVersion]))
@@ -642,20 +681,20 @@ function createSoapEnv(cwmpVersion) {
return env;
}
export function response(rpc) {
export function response(rpc): { code: number; headers: {}; data: string } {
const headers = {
Server: SERVER_NAME,
SOAPServer: SERVER_NAME
};
if (!rpc) return { code: 204, headers: headers, data: Buffer.allocUnsafe(0) };
if (!rpc) return { code: 204, headers: headers, data: "" };
const env = createSoapEnv(rpc.cwmpVersion);
const header = env.node("soap-env:Header");
header
.node("cwmp:ID")
.attr({
"soap-env:mustUnderstand": 1
"soap-env:mustUnderstand": "1"
})
.text(rpc.id);
@@ -664,16 +703,16 @@ export function response(rpc) {
if (rpc.acsResponse) {
switch (rpc.acsResponse.name) {
case "InformResponse":
InformResponse(body, rpc.acsResponse);
InformResponse(body);
break;
case "GetRPCMethodsResponse":
GetRPCMethodsResponse(body, rpc.acsResponse);
break;
case "TransferCompleteResponse":
TransferCompleteResponse(body, rpc.acsResponse);
TransferCompleteResponse(body);
break;
case "RequestDownloadResponse":
RequestDownloadResponse(body, rpc.acsResponse);
RequestDownloadResponse(body);
break;
default:
throw new Error(`Unknown method response type ${rpc.acsResponse.name}`);
@@ -699,7 +738,7 @@ export function response(rpc) {
Reboot(body, rpc.acsRequest);
break;
case "FactoryReset":
FactoryReset(body, rpc.acsRequest);
FactoryReset(body);
break;
case "Download":
Download(body, rpc.acsRequest);
+305
View File
@@ -0,0 +1,305 @@
import PathSet from "./path-set";
import VersionedMap from "./versioned-map";
import InstanceSet from "./instance-set";
import { IncomingMessage, ServerResponse } from "http";
export interface ParamPath extends Array<any> {
wildcard: number;
alias: number;
}
export type Expression = string | number | boolean | null | any[];
export interface Fault {
code: string;
message: string;
detail?: {
name: string;
message: string;
stack?: string;
};
}
export interface SessionFault extends Fault {
channel: string;
timestamp: number;
provisions: string[][];
retryNow?: boolean;
precondition?: boolean;
retries?: number;
expiry?: number;
}
export interface Attributes {
object?: [number, 1 | 0];
writable?: [number, 1 | 0];
value?: [number, [string | number | boolean, string]];
}
export interface AttributeTimestamps {
object?: number;
writable?: number;
value?: number;
}
export interface AttributeValues {
object?: boolean;
writable?: boolean;
value?: [string | number | boolean, string?];
}
export interface DeviceData {
paths: PathSet;
timestamps: VersionedMap<ParamPath, number>;
attributes: VersionedMap<ParamPath, Attributes>;
loaded: Map<ParamPath, number>;
trackers: Map<ParamPath, { [name: string]: number }>;
changes: Set<string>;
}
export type VirtualParameterDeclaration = [
ParamPath,
{ path?: number; object?: number; writable?: number; value?: number }?,
{
path?: [number, number];
object?: boolean;
writable?: boolean;
value?: [string | number | boolean, string?];
}?
];
export interface SyncState {
refreshAttributes: {
exist: Set<ParamPath>;
object: Set<ParamPath>;
writable: Set<ParamPath>;
value: Set<ParamPath>;
};
spv: Map<ParamPath, [string | number | boolean, string]>;
gpn: Set<ParamPath>;
gpnPatterns: Map<ParamPath, number>;
tags: Map<ParamPath, [string | number | boolean, string]>;
virtualParameterDeclarations: VirtualParameterDeclaration[][];
instancesToDelete: Map<ParamPath, Set<ParamPath>>;
instancesToCreate: Map<ParamPath, InstanceSet>;
downloadsToDelete: Set<ParamPath>;
downloadsToCreate: InstanceSet;
downloadsValues: Map<ParamPath, string | number>;
downloadsDownload: Map<ParamPath, number>;
}
export interface SessionContext {
sessionId?: string;
timestamp: number;
deviceId: string;
deviceData: DeviceData;
cwmpVersion: string;
timeout: number;
provisions: any[];
channels: { [channel: string]: number };
virtualParameters: any[];
revisions: number[];
rpcCount: number;
iteration: number;
cycle: number;
extensionsCache: any;
declarations: any[];
faults?: { [channel: string]: SessionFault };
retries?: { [channel: string]: number };
cacheSnapshot?: string;
configContext?: { [name: string]: any };
httpResponse?: ServerResponse;
httpRequest?: IncomingMessage;
faultsTouched?: { [channel: string]: boolean };
presetCycles?: number;
toLoad?: Map<ParamPath, number>;
new?: boolean;
debug?: boolean;
tasks?: Task[];
operations?: { [commandKey: string]: Operation };
cacheUntil?: number;
syncState?: SyncState;
lastActivity?: number;
rpcRequest?: AcsRequest;
operationsTouched?: { [commandKey: string]: 1 | 0 };
provisionsRet?: any[];
doneTasks?: string[];
}
export interface Task {
_id: string;
name: string;
parameterNames?: string[];
parameterValues?: [string, string | number | boolean, string?][];
objectName?: string;
fileType?: string;
fileName?: string;
targetFileName?: string;
expiry?: number;
}
export interface Operation {
name: string;
timestamp: number;
provisions: string[][];
channels: { [channel: string]: number };
retries: { [channel: string]: number };
args: {
instance: string;
fileType: string;
fileName: string;
targetFileName: string;
};
}
export interface AcsRequest {
name: string;
}
export interface GetAcsRequest extends AcsRequest {
name: "GetParameterNames" | "GetParameterValues";
objectName?: string;
parameterNames?: string[];
parameterPath?: string;
nextLevel?: boolean;
}
export interface SetAcsRequest extends AcsRequest {
name:
| "SetParameterValues"
| "AddObject"
| "DeleteObject"
| "FactoryReset"
| "Reboot"
| "Download";
parameterList?: [string, string | number | boolean, string][];
instanceValues?: { [param: string]: string | number | boolean };
objectName?: string;
next?: string;
DATETIME_MILLISECONDS?: boolean;
BOOLEAN_LITERAL?: boolean;
commandKey?: string;
instance?: string;
fileType?: string;
fileSize?: number;
url?: string;
fileName?: string;
targetFileName?: string;
}
export interface CpeResponse {
name: string;
}
export interface SpvFault {
parameterName: string;
faultCode: string;
faultString: string;
}
export interface FaultStruct {
faultCode: string;
faultString: string;
setParameterValuesFault?: SpvFault[];
}
export interface CpeFault {
faultCode: string;
faultString: string;
detail?: FaultStruct;
}
export interface CpeGetResponse extends CpeResponse {
name: "GetParameterNamesResponse" | "GetParameterValuesResponse";
parameterList?:
| [string, boolean][]
| [string, string | number | boolean, string][];
}
export interface CpeSetResponse extends CpeResponse {
name:
| "SetParameterValuesResponse"
| "AddObjectResponse"
| "DeleteObjectResponse"
| "RebootResponse"
| "FactoryResetResponse"
| "DownloadResponse";
status?: number;
instanceNumber?: string;
startTime?: number;
completeTime?: number;
}
export interface CpeRequest {
name: string;
fileType?: string;
}
export interface InformRequest extends CpeRequest {
name: "Inform";
deviceId: {
Manufacturer: string;
OUI: string;
ProductClass?: string;
SerialNumber: string;
};
event: string[];
retryCount: number;
parameterList: [string, string | number | boolean, string][];
}
export interface AcsResponse {
name: string;
commandKey?: string;
faultStruct?: FaultStruct;
startTime?: number;
completeTime?: number;
}
export interface QueryOptions {
projection?: any;
skip?: number;
limit?: number;
sort?: {
[param: string]: number;
};
}
export interface Declaration {
path: ParamPath;
pathGet: number;
pathSet?: [number, number];
attrGet?: { object?: number; writable?: number; value?: number };
attrSet?: {
object?: boolean;
writable?: boolean;
value?: [string | number | boolean, string?];
};
defer: boolean;
}
export type Clear = [
ParamPath,
number,
{ object?: number; writable?: number; value?: number }?,
number?
];
export interface Preset {
name: string;
channel: string;
schedule?: { md5: string; duration: number; schedule: any };
events?: { [event: string]: boolean };
precondition?: {};
provisions: string[][];
}
export type PermissionSet = {
[resource: string]: {
access: number;
validate?: {
[validator: string]: boolean;
};
filter: Expression;
};
}[];
+15 -9
View File
@@ -1,5 +1,3 @@
"use strict";
import { Z_SYNC_FLUSH } from "zlib";
import Koa from "koa";
import Router from "koa-router";
@@ -14,14 +12,21 @@ import Authorizer from "./common/authorizer";
import { parse } from "./common/expression-parser";
import * as logger from "./logger";
import * as localCache from "./local-cache";
import { PermissionSet } from "./types";
declare module "koa" {
interface Request {
body: any;
}
}
const koa = new Koa();
const router = new Router();
const JWT_SECRET = config.get("UI_JWT_SECRET");
const JWT_SECRET = "" + config.get("UI_JWT_SECRET");
const JWT_COOKIE = "genieacs-ui-jwt";
function getPermissionSets(ctx) {
function getPermissionSets(ctx): PermissionSet[] {
const allPermissions = localCache.getPermissionsConfig(
ctx.state.configSnapshot
);
@@ -50,9 +55,9 @@ function getPermissionSets(ctx) {
return permissionSets;
}
function authSimple(ctx, username, password) {
function authSimple(ctx, username, password): Promise<string[]> {
const authConfig = localCache.getAuthConfig(ctx.state.configSnapshot);
const user = authConfig.simple.users[username];
const user = authConfig["simple"].users[username];
if (user && user.password === password) {
const roles = user.roles.split(",").map(s => s.trim());
return Promise.resolve(roles);
@@ -100,10 +105,11 @@ router.post("/login", async ctx => {
const log = {
message: "Log in",
context: ctx,
username: username
username: username,
method: null
};
function success(roles, method) {
function success(roles, method): void {
log.method = method;
const token = jwt.sign({ username, roles }, JWT_SECRET);
ctx.cookies.set(JWT_COOKIE, token);
@@ -111,7 +117,7 @@ router.post("/login", async ctx => {
logger.accessInfo(log);
}
function failure() {
function failure(): void {
ctx.status = 400;
ctx.body = "Incorrect username or password";
log.message += " failed";
@@ -1,9 +1,7 @@
"use strict";
import { exec } from "child_process";
import { ObjectID } from "mongodb";
import * as db from "./db";
import { del } from "../cache";
import { ObjectID } from "mongodb";
import { exec } from "child_process";
import {
insertTasks,
watchTask,
@@ -11,7 +9,7 @@ import {
deleteDevice
} from "../api-functions";
function deleteFault(id) {
function deleteFault(id): Promise<void> {
const deviceId = id.split(":", 1)[0];
const channel = id.slice(deviceId.length + 1);
return new Promise((resolve, reject) => {
@@ -30,7 +28,7 @@ function deleteFault(id) {
});
}
export function deleteResource(resource, id) {
export function deleteResource(resource, id): Promise<void> {
return new Promise((resolve, reject) => {
let promise;
@@ -80,7 +78,11 @@ export function deleteResource(resource, id) {
});
}
export function postTasks(deviceId, tasks, timeout) {
export function postTasks(
deviceId,
tasks,
timeout
): Promise<{ connectionRequest: string; tasks: any[] }> {
return new Promise((resolve, reject) => {
for (const task of tasks) task.device = deviceId;
@@ -139,7 +141,17 @@ export function postTasks(deviceId, tasks, timeout) {
});
}
export function ping(host) {
interface PingResponse {
packetsTransmitted: number;
packetsReceived: number;
packetLoss: string;
min: number;
avg: number;
max: number;
mdev: number;
}
export function ping(host): Promise<PingResponse> {
return new Promise((resolve, reject) => {
exec(`ping -w 1 -i 0.2 -c 3 ${host}`, (err, stdout) => {
if (err) return void reject(err);
@@ -162,7 +174,7 @@ export function ping(host) {
});
}
export function putResource(resource, id, data) {
export function putResource(resource, id, data): Promise<void> {
return new Promise((resolve, reject) => {
let promise;
+18 -16
View File
@@ -1,6 +1,4 @@
"use strict";
import stream from "stream";
import * as stream from "stream";
import Router from "koa-router";
import * as db from "./db";
import * as apiFunctions from "./api-functions";
@@ -8,11 +6,12 @@ import { evaluate, and, extractParams } from "../common/expression";
import { parse } from "../common/expression-parser";
import * as logger from "../logger";
import { getConfig } from "../local-cache";
import { QueryOptions, Expression } from "../types";
const router = new Router();
export default router;
function logUnauthorizedWarning(log) {
function logUnauthorizedWarning(log): void {
log.message += " not authorized";
logger.accessWarn(log);
}
@@ -91,13 +90,14 @@ router.get(`/devices/:id.csv`, async ctx => {
for (const [resource, flags] of Object.entries(resources)) {
router.head(`/${resource}`, async (ctx, next) => {
let filter = true;
let filter: Expression = true;
if (ctx.request.query.filter) filter = parse(ctx.request.query.filter);
const log = {
message: `Count ${resource}`,
context: ctx,
filter: ctx.request.query.filter
filter: ctx.request.query.filter,
count: null
};
if (!ctx.state.authorizer.hasAccess(resource, 1)) {
@@ -115,7 +115,7 @@ for (const [resource, flags] of Object.entries(resources)) {
const count = await db.count(resource, filter);
ctx.set("X-Total-Count", count);
ctx.set("X-Total-Count", `${count}`);
ctx.body = "";
log.count = count;
@@ -123,8 +123,8 @@ for (const [resource, flags] of Object.entries(resources)) {
});
router.get(`/${resource}`, async (ctx, next) => {
const options = {};
let filter = true;
const options: QueryOptions = {};
let filter: Expression = true;
if (ctx.request.query.filter) filter = parse(ctx.request.query.filter);
if (ctx.request.query.limit) options.limit = +ctx.request.query.limit;
if (ctx.request.query.skip) options.skip = +ctx.request.query.skip;
@@ -174,8 +174,8 @@ for (const [resource, flags] of Object.entries(resources)) {
// CSV download
router.get(`/${resource}.csv`, async (ctx, next) => {
const options = { projection: {} };
let filter = true;
const options: QueryOptions = { projection: {} };
let filter: Expression = true;
if (ctx.request.query.filter) filter = parse(ctx.request.query.filter);
if (ctx.request.query.limit) options.limit = +ctx.request.query.limit;
if (ctx.request.query.skip) options.skip = +ctx.request.query.skip;
@@ -356,9 +356,9 @@ for (const [resource, flags] of Object.entries(resources)) {
return void (ctx.status = 403);
}
const err = await apiFunctions.putResource(resource, id, obj);
if (err) {
try {
await apiFunctions.putResource(resource, id, obj);
} catch (err) {
log.message += " failed";
logger.accessWarn(log);
ctx.body = err;
@@ -379,7 +379,8 @@ router.put("/files/:id", async (ctx, next) => {
const log = {
message: `Upload ${resource}`,
context: ctx,
id: id
id: id,
metadata: null
};
const authorizer = ctx.state.authorizer;
@@ -412,7 +413,8 @@ router.post("/devices/:id/tasks", async (ctx, next) => {
const log = {
message: "Commit tasks",
context: ctx,
deviceId: ctx.params.id
deviceId: ctx.params.id,
tasks: null
};
const authorizer = ctx.state.authorizer;
+29 -25
View File
@@ -1,10 +1,9 @@
"use strict";
import { MongoClient, GridFSBucket } from "mongodb";
import * as config from "../config";
import * as mongodbFunctions from "./mongodb-functions";
import * as expression from "../common/expression";
import { preProcessPreset } from "./mongodb-functions";
import { QueryOptions } from "../types";
const CACHE_TTL = 300000;
@@ -14,17 +13,17 @@ const RESOURCE_COLLECTION = {
files: "fs.files"
};
function ensureIndexes(client) {
function ensureIndexes(client): void {
client
.db()
.collection("cache")
.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
}
function getClient() {
function getClient(): Promise<MongoClient> {
if (!_clientPromise) {
_clientPromise = new Promise((resolve, reject) => {
const CONNECTION_URL = config.get("MONGODB_CONNECTION_URL");
const CONNECTION_URL = "" + config.get("MONGODB_CONNECTION_URL");
MongoClient.connect(
CONNECTION_URL,
{ useNewUrlParser: true },
@@ -40,14 +39,14 @@ function getClient() {
return _clientPromise;
}
export function cache(key, valueGetter, ttl) {
export function cache<T>(key, valueGetter: () => Promise<T>, ttl): Promise<T> {
return new Promise((resolve, reject) => {
getClient()
.then(client => {
const collection = client.db().collection("cache");
collection.findOne({ _id: key }, (err, doc) => {
if (err) return void reject(err);
if (doc != null) return void resolve(JSON.parse(doc.value));
if (doc != null) return void resolve(JSON.parse(doc.value) as T);
valueGetter()
.then(res => {
const expire = Date.now() + (ttl || CACHE_TTL);
@@ -73,7 +72,12 @@ export function cache(key, valueGetter, ttl) {
});
}
export function query(resource, filter, options, callback) {
export function query(
resource,
filter,
options?: QueryOptions,
callback?: (doc: any) => void
): Promise<any[]> {
options = options || {};
let q;
filter = expression.evaluate(filter, null, Date.now());
@@ -159,7 +163,7 @@ export function query(resource, filter, options, callback) {
});
}
export function count(resource, filter) {
export function count(resource, filter): Promise<number> {
let q;
filter = expression.evaluate(filter, null, Date.now());
@@ -188,7 +192,7 @@ export function count(resource, filter) {
});
}
export function updateDeviceTags(deviceId, tags) {
export function updateDeviceTags(deviceId, tags): Promise<void> {
return new Promise((resolve, reject) => {
const add = [];
const pull = [];
@@ -213,7 +217,7 @@ export function updateDeviceTags(deviceId, tags) {
});
}
function putResource(resource, id, object) {
function putResource(resource, id, object): Promise<void> {
return new Promise((resolve, reject) => {
getClient()
.then(client => {
@@ -229,7 +233,7 @@ function putResource(resource, id, object) {
});
}
function deleteResource(resource, id) {
function deleteResource(resource, id): Promise<void> {
return new Promise((resolve, reject) => {
getClient()
.then(client => {
@@ -245,40 +249,40 @@ function deleteResource(resource, id) {
});
}
export function putPreset(id, object) {
export function putPreset(id, object): Promise<void> {
object = preProcessPreset(object);
return putResource("presets", id, object);
}
export function deletePreset(id) {
export function deletePreset(id): Promise<void> {
return deleteResource("presets", id);
}
export function putProvision(id, object) {
export function putProvision(id, object): Promise<void> {
return putResource("provisions", id, object);
}
export function deleteProvision(id) {
export function deleteProvision(id): Promise<void> {
return deleteResource("provisions", id);
}
export function putVirtualParameter(id, object) {
export function putVirtualParameter(id, object): Promise<void> {
return putResource("virtualParameters", id, object);
}
export function deleteVirtualParameter(id) {
export function deleteVirtualParameter(id): Promise<void> {
return deleteResource("virtualParameters", id);
}
export function putConfig(id, object) {
export function putConfig(id, object): Promise<void> {
return putResource("config", id, object);
}
export function deleteConfig(id) {
export function deleteConfig(id): Promise<void> {
return deleteResource("config", id);
}
export function putFile(filename, metadata, contentStream) {
export function putFile(filename, metadata, contentStream): Promise<void> {
return new Promise((resolve, reject) => {
getClient()
.then(client => {
@@ -295,7 +299,7 @@ export function putFile(filename, metadata, contentStream) {
});
}
export function deleteFile(filename) {
export function deleteFile(filename): Promise<void> {
return new Promise((resolve, reject) => {
getClient()
.then(client => {
@@ -309,15 +313,15 @@ export function deleteFile(filename) {
});
}
export function deleteFault(id) {
export function deleteFault(id): Promise<void> {
return deleteResource("faults", id);
}
export function deleteTask(id) {
export function deleteTask(id): Promise<void> {
return deleteResource("tasks", id);
}
export function disconnect() {
export function disconnect(): void {
if (_clientPromise) {
_clientPromise.then(client => {
client.close();
@@ -1,11 +1,11 @@
"use strict";
import { ObjectID } from "mongodb";
import { map, parse, stringify } from "../common/expression-parser";
import { likePatternToRegExp, evaluate } from "../common/expression";
import { Expression, Fault, Task } from "../types";
const isArray = Array.isArray;
export function processDeviceFilter(filter) {
export function processDeviceFilter(filter): Expression {
return map(filter, exp => {
if (!isArray(exp)) return exp;
@@ -46,7 +46,7 @@ export function processDeviceFilter(filter) {
});
if (typeof e[2] === "number") {
const alt = e.slice();
const alt = (e as any[]).slice();
alt[2] = new Date(e[2]);
e = ["OR", e, alt];
}
@@ -56,7 +56,7 @@ export function processDeviceFilter(filter) {
});
}
export function processTasksFilter(filter) {
export function processTasksFilter(filter): Expression {
return map(filter, exp => {
if (!isArray(exp)) return exp;
if (["=", "<>", ">", ">=", "<", "<="].includes(exp[0])) {
@@ -72,7 +72,7 @@ export function processTasksFilter(filter) {
});
}
export function processFaultsFilter(filter) {
export function processFaultsFilter(filter): Expression {
return map(filter, exp => {
if (!isArray(exp)) return exp;
if (["=", "<>", ">", ">=", "<", "<="].includes(exp[0])) {
@@ -86,7 +86,7 @@ export function processFaultsFilter(filter) {
});
}
export function filterToMongoQuery(exp) {
export function filterToMongoQuery(exp: Expression): {} {
const ops = {
OR: 0,
AND: 0,
@@ -103,7 +103,7 @@ export function filterToMongoQuery(exp) {
"IS NOT NULL": 1
};
function recursive(filter, negate, res = {}) {
function recursive(filter, negate, res = {}): {} {
const op = filter[0];
if (ops[op] === 0) {
@@ -219,7 +219,7 @@ export function filterToMongoQuery(exp) {
return recursive(_exp, false);
}
export function processDeviceProjection(projection) {
export function processDeviceProjection(projection: {}): {} {
if (!projection) return projection;
const p = {};
for (const [k, v] of Object.entries(projection)) {
@@ -245,7 +245,7 @@ export function processDeviceProjection(projection) {
return p;
}
export function processDeviceSort(sort) {
export function processDeviceSort(sort: {}): {} {
if (!sort) return sort;
const s = {};
for (const [k, v] of Object.entries(sort)) {
@@ -261,13 +261,24 @@ export function processDeviceSort(sort) {
return s;
}
function parseDate(d) {
function parseDate(d): number | string {
const n = +d;
return isNaN(n) ? "" + d : n;
}
export function flattenDevice(device) {
function recursive(input, root, output, timestamp) {
interface FlatDevice {
[param: string]: {
object: boolean;
objectTimestamp: number;
writable: boolean;
writableTimestamp: number;
value: [string | number | boolean, string];
valueTimestamp: number;
};
}
export function flattenDevice(device): FlatDevice {
function recursive(input, root, output, timestamp): void {
for (const [name, tree] of Object.entries(input)) {
if (!root) {
if (name === "_lastInform") {
@@ -356,7 +367,7 @@ export function flattenDevice(device) {
objectTimestamp: timestamp
};
for (const t of tree) {
for (const t of tree as string[]) {
output[`Tags.${t}`] = {
value: [true, "xsd:boolean"],
valueTimestamp: timestamp,
@@ -410,14 +421,14 @@ export function flattenDevice(device) {
return newDevice;
}
export function flattenFault(fault) {
export function flattenFault(fault): Fault {
const f = Object.assign({}, fault);
if (f.timestamp) f.timestamp = +f.timestamp;
if (f.expiry) f.expiry = +f.expiry;
return f;
}
export function flattenTask(task) {
export function flattenTask(task): Task {
const t = Object.assign({}, task);
t._id = "" + t._id;
if (t.timestamp) t.timestamp = +t.timestamp;
@@ -425,9 +436,9 @@ export function flattenTask(task) {
return t;
}
function mongoQueryToFilter(query) {
function mongoQueryToFilter(query): Expression {
const expressions = [];
function recursive(_query) {
function recursive(_query): void {
for (const [k, v] of Object.entries(_query)) {
if (k[0] === "$") {
if (k === "$and") for (const vv of Object.values(v)) recursive(vv);
@@ -492,7 +503,7 @@ function mongoQueryToFilter(query) {
return expressions;
}
export function flattenPreset(preset) {
export function flattenPreset(preset): {} {
const p = Object.assign({}, preset);
if (p.precondition) {
p.precondition = mongoQueryToFilter(JSON.parse(p.precondition));
@@ -521,7 +532,7 @@ export function flattenPreset(preset) {
return p;
}
export function flattenFile(file) {
export function flattenFile(file): {} {
const f = {};
f["_id"] = file["_id"];
if (file.metadata) {
@@ -533,7 +544,7 @@ export function flattenFile(file) {
return f;
}
export function preProcessPreset(data) {
export function preProcessPreset(data): {} {
const preset = Object.assign({}, data);
preset.precondition = preset.precondition
? filterToMongoQuery(parse(preset.precondition))
@@ -561,7 +572,8 @@ export function preProcessPreset(data) {
const configuration = {
type: "provision",
name: preset.provision
name: preset.provision,
args: null
};
if (preset.provisionArgs)
+35 -26
View File
@@ -16,44 +16,53 @@
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const NONEXISTENT = new Object();
const NONEXISTENT = Symbol();
const UNDEFINED = undefined;
export default class VersionedMap {
constructor() {
interface Revisions<V> {
[rev: number]: V;
delete?: number;
}
export default class VersionedMap<K, V> {
private _sizeDiff: number[];
private _revision: number;
private map: Map<K, (V | symbol)[]>;
public dirty: number;
public constructor() {
this._sizeDiff = [0];
this._revision = 0;
this.map = new Map();
this.dirty = 0;
}
get size() {
public get size(): number {
return this.map.size + this._sizeDiff[this.revision];
}
get revision() {
public get revision(): number {
return this._revision;
}
set revision(rev) {
public set revision(rev) {
for (let i = this._sizeDiff.length; i <= rev; ++i)
this._sizeDiff[i] = this._sizeDiff[i - 1];
this._revision = rev;
}
get(key, rev = this._revision) {
public get(key: K, rev = this._revision): V {
const revisions = this.map.get(key);
if (!revisions) return UNDEFINED;
const v = revisions[Math.min(revisions.length - 1, rev)];
if (v === NONEXISTENT) return UNDEFINED;
return v;
return v as V;
}
has(key, rev = this._revision) {
public has(key: K, rev = this._revision): boolean {
const revisions = this.map.get(key);
if (!revisions) return false;
@@ -62,7 +71,7 @@ export default class VersionedMap {
return true;
}
set(key, value, rev = this._revision) {
public set(key: K, value: V, rev = this._revision): this {
let revisions = this.map.get(key);
if (!revisions) {
this.dirty |= 1 << rev;
@@ -72,7 +81,7 @@ export default class VersionedMap {
for (let i = 0; i < rev; ++i) revisions[i] = NONEXISTENT;
revisions[rev] = value;
this.map.set(key, revisions);
return true;
return this;
}
// Can't modify old revisions
@@ -86,10 +95,10 @@ export default class VersionedMap {
for (let i = revisions.length; i < rev; ++i) revisions[i] = old;
revisions[rev] = value;
return true;
return this;
}
delete(key, rev = this._revision) {
public delete(key: K, rev = this._revision): boolean {
const revisions = this.map.get(key);
if (!revisions) return false;
@@ -108,24 +117,24 @@ export default class VersionedMap {
return true;
}
getRevisions(key) {
public getRevisions(key: K): Revisions<V> {
const revisions = this.map.get(key);
if (!revisions) return null;
const res = {};
const res: Revisions<V> = {};
let prev = NONEXISTENT;
let prev: V | symbol = NONEXISTENT;
for (const [i, v] of revisions.entries()) {
if (v === prev) continue;
if (v === NONEXISTENT) res.delete |= 1 << i;
else res[i] = v;
else res[i] = v as V;
prev = v;
}
return res;
}
setRevisions(key, revisionsObj) {
public setRevisions(key: K, revisionsObj: Revisions<V>): void {
const del = revisionsObj.delete || 0;
const maxRev = Object.keys(revisionsObj).reduce(
(acc, cur) => (+cur > acc ? +cur : acc),
@@ -134,7 +143,7 @@ export default class VersionedMap {
const revisions = [];
let prev = NONEXISTENT;
let prev: V | symbol = NONEXISTENT;
for (let i = 0; i <= maxRev; ++i) {
let v = prev;
if (del & (1 << i)) v = NONEXISTENT;
@@ -147,28 +156,28 @@ export default class VersionedMap {
this.map.set(key, revisions);
}
getDiff(key) {
public getDiff(key: K): [V, V] {
const revisions = this.map.get(key);
if (!revisions) return [UNDEFINED, UNDEFINED];
let first = revisions[0];
if (first === NONEXISTENT) first = UNDEFINED;
let last = revisions[revisions.length - 1];
if (last === NONEXISTENT) last = UNDEFINED;
return [first, last];
return [first as V, last as V];
}
*diff() {
public *diff(): IterableIterator<[K, V, V]> {
for (const [key, revisions] of this.map) {
let first = revisions[0];
let last = revisions[revisions.length - 1];
if ((first === last) === NONEXISTENT) continue;
if (first === NONEXISTENT && last === NONEXISTENT) continue;
if (first === NONEXISTENT) first = UNDEFINED;
if (last === NONEXISTENT) last = UNDEFINED;
yield [key, first, last];
yield [key, first as V, last as V];
}
}
collapse(revision) {
public collapse(revision: number): void {
if (this._sizeDiff.length <= revision) return;
this._sizeDiff[revision] = this._sizeDiff[this._sizeDiff.length - 1];