Add types in local-cache.ts
This commit is contained in:
1 parent
37d8222479
commit
dd907a67b0
3 files changed
+79
-31
No files matched your search
+32
-29
@@ -27,25 +27,34 @@ import * as logger from "./logger";
|
|||||||
import * as scheduling from "./scheduling";
|
import * as scheduling from "./scheduling";
|
||||||
import * as expression from "./common/expression";
|
import * as expression from "./common/expression";
|
||||||
import { parse } from "./common/expression-parser";
|
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 {
|
interface Snapshot {
|
||||||
[role: string]: {
|
presets: Preset[];
|
||||||
[access: number]: {
|
provisions: Provisions;
|
||||||
[resource: string]: {
|
virtualParameters: VirtualParameters;
|
||||||
access: number;
|
files: Files;
|
||||||
filter: Expression;
|
permissions: Permissions;
|
||||||
validate?: Expression;
|
users: Users;
|
||||||
};
|
config: Config;
|
||||||
};
|
ui: UiConfig;
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const REFRESH = 3000;
|
const REFRESH = 3000;
|
||||||
const EVICT_TIMEOUT = 60000;
|
const EVICT_TIMEOUT = 60000;
|
||||||
|
|
||||||
const snapshots = new Map();
|
const snapshots = new Map<string, Snapshot>();
|
||||||
let currentSnapshot = null;
|
let currentSnapshot: string = null;
|
||||||
let nextRefresh = 1;
|
let nextRefresh = 1;
|
||||||
|
|
||||||
function computeHash(snapshot): string {
|
function computeHash(snapshot): string {
|
||||||
@@ -244,7 +253,7 @@ async function fetchPresets(): Promise<Preset[]> {
|
|||||||
return presets;
|
return presets;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchProvisions(): Promise<{}> {
|
async function fetchProvisions(): Promise<Provisions> {
|
||||||
const res = await db.getProvisions();
|
const res = await db.getProvisions();
|
||||||
|
|
||||||
const provisions = {};
|
const provisions = {};
|
||||||
@@ -263,7 +272,7 @@ async function fetchProvisions(): Promise<{}> {
|
|||||||
return provisions;
|
return provisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchVirtualParameters(): Promise<{}> {
|
async function fetchVirtualParameters(): Promise<VirtualParameters> {
|
||||||
const res = await db.getVirtualParameters();
|
const res = await db.getVirtualParameters();
|
||||||
|
|
||||||
const virtualParameters = {};
|
const virtualParameters = {};
|
||||||
@@ -301,7 +310,7 @@ async function fetchPermissions(): Promise<Permissions> {
|
|||||||
return permissions;
|
return permissions;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchFiles(): Promise<{}> {
|
async function fetchFiles(): Promise<Files> {
|
||||||
const res = await db.getFiles();
|
const res = await db.getFiles();
|
||||||
const files = {};
|
const files = {};
|
||||||
|
|
||||||
@@ -316,7 +325,7 @@ async function fetchFiles(): Promise<{}> {
|
|||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchUsers(): Promise<{}> {
|
async function fetchUsers(): Promise<Users> {
|
||||||
const _users = await db.getUsers();
|
const _users = await db.getUsers();
|
||||||
const users = {};
|
const users = {};
|
||||||
|
|
||||||
@@ -331,7 +340,7 @@ async function fetchUsers(): Promise<{}> {
|
|||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchConfig(): Promise<[{}, {}]> {
|
async function fetchConfig(): Promise<[Config, UiConfig]> {
|
||||||
const conf = await db.getConfig();
|
const conf = await db.getConfig();
|
||||||
|
|
||||||
conf.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
|
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;
|
return snapshot.presets;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getProvisions(
|
export function getProvisions(snapshotKey): Provisions {
|
||||||
snapshotKey
|
|
||||||
): { [name: string]: { md5: string; script: vm.Script } } {
|
|
||||||
const snapshot = snapshots.get(snapshotKey);
|
const snapshot = snapshots.get(snapshotKey);
|
||||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||||
return snapshot.provisions;
|
return snapshot.provisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getVirtualParameters(
|
export function getVirtualParameters(snapshotKey): VirtualParameters {
|
||||||
snapshotKey
|
|
||||||
): { [name: string]: { md5: string; script: vm.Script } } {
|
|
||||||
const snapshot = snapshots.get(snapshotKey);
|
const snapshot = snapshots.get(snapshotKey);
|
||||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||||
return snapshot.virtualParameters;
|
return snapshot.virtualParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFiles(
|
export function getFiles(snapshotKey): Files {
|
||||||
snapshotKey
|
|
||||||
): { [name: string]: { length: number; md5: string; contentType: string } } {
|
|
||||||
const snapshot = snapshots.get(snapshotKey);
|
const snapshot = snapshots.get(snapshotKey);
|
||||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||||
return snapshot.files;
|
return snapshot.files;
|
||||||
@@ -520,14 +523,14 @@ export function getUsers(snapshotKey): {} {
|
|||||||
return snapshot.users;
|
return snapshot.users;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPermissions(snapshotKey): {} {
|
export function getPermissions(snapshotKey): Permissions {
|
||||||
const snapshot = snapshots.get(snapshotKey);
|
const snapshot = snapshots.get(snapshotKey);
|
||||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||||
|
|
||||||
return snapshot.permissions;
|
return snapshot.permissions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getUiConfig(snapshotKey): {} {
|
export function getUiConfig(snapshotKey): UiConfig {
|
||||||
const snapshot = snapshots.get(snapshotKey);
|
const snapshot = snapshots.get(snapshotKey);
|
||||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import PathSet from "./common/path-set";
|
|||||||
import VersionedMap from "./versioned-map";
|
import VersionedMap from "./versioned-map";
|
||||||
import InstanceSet from "./instance-set";
|
import InstanceSet from "./instance-set";
|
||||||
import { IncomingMessage, ServerResponse } from "http";
|
import { IncomingMessage, ServerResponse } from "http";
|
||||||
|
import { Script } from "vm";
|
||||||
|
|
||||||
export type Expression = string | number | boolean | null | any[];
|
export type Expression = string | number | boolean | null | any[];
|
||||||
|
|
||||||
@@ -324,6 +325,34 @@ export interface Preset {
|
|||||||
provisions: string[][];
|
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 = {
|
export type PermissionSet = {
|
||||||
[resource: string]: {
|
[resource: string]: {
|
||||||
access: number;
|
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 {
|
export interface SoapMessage {
|
||||||
id: string;
|
id: string;
|
||||||
cwmpVersion: string;
|
cwmpVersion: string;
|
||||||
|
|||||||
@@ -37,13 +37,14 @@ import * as usersPage from "./users-page";
|
|||||||
import Authorizer from "../lib//common/authorizer";
|
import Authorizer from "../lib//common/authorizer";
|
||||||
import * as notifications from "./notifications";
|
import * as notifications from "./notifications";
|
||||||
import { contextifyComponent } from "./components";
|
import { contextifyComponent } from "./components";
|
||||||
|
import { PermissionSet, UiConfig } from "../lib/types";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
authorizer: Authorizer;
|
authorizer: Authorizer;
|
||||||
permissionSets: any;
|
permissionSets: PermissionSet[];
|
||||||
username: string;
|
username: string;
|
||||||
clientConfig: any;
|
clientConfig: { ui: UiConfig };
|
||||||
configSnapshot: string;
|
configSnapshot: string;
|
||||||
genieacsVersion: string;
|
genieacsVersion: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user