diff --git a/lib/local-cache.ts b/lib/local-cache.ts index 783db79..29ff665 100644 --- a/lib/local-cache.ts +++ b/lib/local-cache.ts @@ -27,25 +27,34 @@ import * as logger from "./logger"; import * as scheduling from "./scheduling"; import * as expression from "./common/expression"; import { parse } from "./common/expression-parser"; -import { Preset, Expression } from "./types"; +import { + Preset, + Expression, + Provisions, + VirtualParameters, + Files, + Users, + Permissions, + Config, + UiConfig +} from "./types"; -interface Permissions { - [role: string]: { - [access: number]: { - [resource: string]: { - access: number; - filter: Expression; - validate?: Expression; - }; - }; - }; +interface Snapshot { + presets: Preset[]; + provisions: Provisions; + virtualParameters: VirtualParameters; + files: Files; + permissions: Permissions; + users: Users; + config: Config; + ui: UiConfig; } const REFRESH = 3000; const EVICT_TIMEOUT = 60000; -const snapshots = new Map(); -let currentSnapshot = null; +const snapshots = new Map(); +let currentSnapshot: string = null; let nextRefresh = 1; function computeHash(snapshot): string { @@ -244,7 +253,7 @@ async function fetchPresets(): Promise { return presets; } -async function fetchProvisions(): Promise<{}> { +async function fetchProvisions(): Promise { const res = await db.getProvisions(); const provisions = {}; @@ -263,7 +272,7 @@ async function fetchProvisions(): Promise<{}> { return provisions; } -async function fetchVirtualParameters(): Promise<{}> { +async function fetchVirtualParameters(): Promise { const res = await db.getVirtualParameters(); const virtualParameters = {}; @@ -301,7 +310,7 @@ async function fetchPermissions(): Promise { return permissions; } -async function fetchFiles(): Promise<{}> { +async function fetchFiles(): Promise { const res = await db.getFiles(); const files = {}; @@ -316,7 +325,7 @@ async function fetchFiles(): Promise<{}> { return files; } -async function fetchUsers(): Promise<{}> { +async function fetchUsers(): Promise { const _users = await db.getUsers(); const users = {}; @@ -331,7 +340,7 @@ async function fetchUsers(): Promise<{}> { return users; } -async function fetchConfig(): Promise<[{}, {}]> { +async function fetchConfig(): Promise<[Config, UiConfig]> { const conf = await db.getConfig(); conf.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0)); @@ -436,25 +445,19 @@ export function getPresets(snapshotKey): Preset[] { return snapshot.presets; } -export function getProvisions( - snapshotKey -): { [name: string]: { md5: string; script: vm.Script } } { +export function getProvisions(snapshotKey): Provisions { const snapshot = snapshots.get(snapshotKey); if (!snapshot) throw new Error("Cache snapshot does not exist"); return snapshot.provisions; } -export function getVirtualParameters( - snapshotKey -): { [name: string]: { md5: string; script: vm.Script } } { +export function getVirtualParameters(snapshotKey): VirtualParameters { const snapshot = snapshots.get(snapshotKey); if (!snapshot) throw new Error("Cache snapshot does not exist"); return snapshot.virtualParameters; } -export function getFiles( - snapshotKey -): { [name: string]: { length: number; md5: string; contentType: string } } { +export function getFiles(snapshotKey): Files { const snapshot = snapshots.get(snapshotKey); if (!snapshot) throw new Error("Cache snapshot does not exist"); return snapshot.files; @@ -520,14 +523,14 @@ export function getUsers(snapshotKey): {} { return snapshot.users; } -export function getPermissions(snapshotKey): {} { +export function getPermissions(snapshotKey): Permissions { const snapshot = snapshots.get(snapshotKey); if (!snapshot) throw new Error("Cache snapshot does not exist"); return snapshot.permissions; } -export function getUiConfig(snapshotKey): {} { +export function getUiConfig(snapshotKey): UiConfig { const snapshot = snapshots.get(snapshotKey); if (!snapshot) throw new Error("Cache snapshot does not exist"); diff --git a/lib/types.ts b/lib/types.ts index ff2c3d8..b9748c1 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -22,6 +22,7 @@ import PathSet from "./common/path-set"; import VersionedMap from "./versioned-map"; import InstanceSet from "./instance-set"; import { IncomingMessage, ServerResponse } from "http"; +import { Script } from "vm"; export type Expression = string | number | boolean | null | any[]; @@ -324,6 +325,34 @@ export interface Preset { provisions: string[][]; } +export interface Provisions { + [name: string]: { md5: string; script: Script }; +} + +export interface VirtualParameters { + [name: string]: { md5: string; script: Script }; +} + +export interface Files { + [name: string]: { length: number; md5: string; contentType: string }; +} + +export interface Users { + [name: string]: { password: string; salt: string; roles: string[] }; +} + +export interface Permissions { + [role: string]: { + [access: number]: { + [resource: string]: { + access: number; + filter: Expression; + validate?: Expression; + }; + }; + }; +} + export type PermissionSet = { [resource: string]: { access: number; @@ -332,6 +361,21 @@ export type PermissionSet = { }; }[]; +export interface Config { + [name: string]: Expression; +} + +export interface UiConfig { + filters: {}; + device: {}; + index: {}; + overview: { + charts?: {}; + groups?: {}; + }; + pageSize?: Expression; +} + export interface SoapMessage { id: string; cwmpVersion: string; diff --git a/ui/app.ts b/ui/app.ts index 5aee77c..78b915b 100644 --- a/ui/app.ts +++ b/ui/app.ts @@ -37,13 +37,14 @@ import * as usersPage from "./users-page"; import Authorizer from "../lib//common/authorizer"; import * as notifications from "./notifications"; import { contextifyComponent } from "./components"; +import { PermissionSet, UiConfig } from "../lib/types"; declare global { interface Window { authorizer: Authorizer; - permissionSets: any; + permissionSets: PermissionSet[]; username: string; - clientConfig: any; + clientConfig: { ui: UiConfig }; configSnapshot: string; genieacsVersion: string; }