Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4e1b297ae | ||
|
|
d445aa03cf | ||
|
|
f39e2ce7cf | ||
|
|
6f3ce6f7c8 | ||
|
|
75908e624f | ||
|
|
4a191afdec | ||
|
|
a7256620e0 | ||
|
|
bef840ce0a | ||
|
|
cc723c0b50 | ||
|
|
a2a6fd82f3 | ||
|
|
729beba0ed | ||
|
|
1b14387aa2 | ||
|
|
49cdd39617 |
No files matched your search
@@ -0,0 +1,28 @@
|
||||
# Dependências e artefatos de build
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
# Arquivos de ambiente (contêm segredos)
|
||||
.env
|
||||
|
||||
# Logs e debug
|
||||
logs/
|
||||
*.log
|
||||
|
||||
# Artefatos de teste
|
||||
test/*.js
|
||||
|
||||
# Docs geradas
|
||||
docs/_build/
|
||||
|
||||
# Dados de desenvolvimento
|
||||
.git/
|
||||
.github/
|
||||
|
||||
# IDEs
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,69 @@
|
||||
# ============================================================
|
||||
# GenieACS — variáveis de ambiente
|
||||
# Copie este arquivo para .env e ajuste os valores
|
||||
# NUNCA faça commit do .env com dados reais em repositórios
|
||||
# públicos.
|
||||
# ============================================================
|
||||
|
||||
# ── MongoDB ─────────────────────────────────────────────────
|
||||
# Quando usando docker-compose o host é o nome do serviço "mongo"
|
||||
GENIEACS_MONGODB_CONNECTION_URL=mongodb://mongo:27017/genieacs
|
||||
|
||||
# ── Extensões ───────────────────────────────────────────────
|
||||
GENIEACS_EXT_DIR=/opt/genieacs/config/ext
|
||||
GENIEACS_EXT_TIMEOUT=3000
|
||||
|
||||
# ── Logs ────────────────────────────────────────────────────
|
||||
# json | simple
|
||||
GENIEACS_LOG_FORMAT=json
|
||||
GENIEACS_ACCESS_LOG_FORMAT=json
|
||||
|
||||
# Para gravar logs em arquivo (opcional, senão vai para stdout/stderr)
|
||||
# GENIEACS_CWMP_LOG_FILE=/opt/genieacs/logs/cwmp.log
|
||||
# GENIEACS_CWMP_ACCESS_LOG_FILE=/opt/genieacs/logs/cwmp-access.log
|
||||
# GENIEACS_NBI_LOG_FILE=/opt/genieacs/logs/nbi.log
|
||||
# GENIEACS_NBI_ACCESS_LOG_FILE=/opt/genieacs/logs/nbi-access.log
|
||||
# GENIEACS_FS_LOG_FILE=/opt/genieacs/logs/fs.log
|
||||
# GENIEACS_FS_ACCESS_LOG_FILE=/opt/genieacs/logs/fs-access.log
|
||||
# GENIEACS_UI_LOG_FILE=/opt/genieacs/logs/ui.log
|
||||
# GENIEACS_UI_ACCESS_LOG_FILE=/opt/genieacs/logs/ui-access.log
|
||||
|
||||
# ── Debug (opcional) ────────────────────────────────────────
|
||||
# GENIEACS_DEBUG_FILE=/opt/genieacs/logs/debug.yaml
|
||||
# GENIEACS_DEBUG_FORMAT=yaml
|
||||
|
||||
# ── Worker processes (0 = 1 por CPU core) ───────────────────
|
||||
GENIEACS_CWMP_WORKER_PROCESSES=1
|
||||
GENIEACS_NBI_WORKER_PROCESSES=1
|
||||
GENIEACS_FS_WORKER_PROCESSES=1
|
||||
GENIEACS_UI_WORKER_PROCESSES=1
|
||||
|
||||
# ── Portas ──────────────────────────────────────────────────
|
||||
GENIEACS_CWMP_PORT=7547
|
||||
GENIEACS_NBI_PORT=7557
|
||||
GENIEACS_FS_PORT=7567
|
||||
GENIEACS_UI_PORT=3000
|
||||
|
||||
# ── Interface de bind (:: = todas as interfaces) ────────────
|
||||
GENIEACS_CWMP_INTERFACE=::
|
||||
GENIEACS_NBI_INTERFACE=::
|
||||
GENIEACS_FS_INTERFACE=::
|
||||
GENIEACS_UI_INTERFACE=::
|
||||
|
||||
# ── UI — JWT Secret (OBRIGATÓRIO trocar em produção!) ───────
|
||||
# Mínimo 32 chars, máximo 64 chars
|
||||
GENIEACS_UI_JWT_SECRET=mude-isto-em-producao-use-uma-string-aleatoria-longa
|
||||
|
||||
# ── FS — URL prefix (configure se CWMP e FS estiverem em ────
|
||||
# hosts/IPs diferentes ou atrás de proxy)
|
||||
# GENIEACS_FS_URL_PREFIX=http://seu-ip-publico:7567/
|
||||
|
||||
# ── SSL (opcional — passe o caminho para cert/key PEM) ──────
|
||||
# GENIEACS_CWMP_SSL_CERT=/opt/genieacs/certs/cwmp.crt
|
||||
# GENIEACS_CWMP_SSL_KEY=/opt/genieacs/certs/cwmp.key
|
||||
# GENIEACS_NBI_SSL_CERT=/opt/genieacs/certs/nbi.crt
|
||||
# GENIEACS_NBI_SSL_KEY=/opt/genieacs/certs/nbi.key
|
||||
# GENIEACS_FS_SSL_CERT=/opt/genieacs/certs/fs.crt
|
||||
# GENIEACS_FS_SSL_KEY=/opt/genieacs/certs/fs.key
|
||||
# GENIEACS_UI_SSL_CERT=/opt/genieacs/certs/ui.crt
|
||||
# GENIEACS_UI_SSL_KEY=/opt/genieacs/certs/ui.key
|
||||
@@ -1,36 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"es2022": true,
|
||||
"node": true
|
||||
},
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
},
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"prettier"
|
||||
],
|
||||
"rules": {
|
||||
"no-shadow": ["error", { "allow": ["err"] }],
|
||||
"handle-callback-err": "error",
|
||||
"prefer-arrow-callback": "error",
|
||||
"no-buffer-constructor": "error",
|
||||
"prefer-const": ["error", { "destructuring": "all" }],
|
||||
"eqeqeq": ["error", "always", { "null": "ignore" }],
|
||||
"@typescript-eslint/no-explicit-any": 0,
|
||||
"@typescript-eslint/no-use-before-define": [
|
||||
"error",
|
||||
{ "functions": false }
|
||||
],
|
||||
"@typescript-eslint/explicit-function-return-type": [
|
||||
"error",
|
||||
{ "allowExpressions": true }
|
||||
],
|
||||
"@typescript-eslint/no-floating-promises": "error",
|
||||
"@typescript-eslint/no-misused-promises": "error",
|
||||
"no-prototype-builtins": 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
GitHub Copilot Instructions: GenieACS -> UniFi UI Theme
|
||||
You are an expert senior software engineer and frontend architect focused on UI/UX modernization, maintainability, and clean architecture. Your primary goal is to help modify the GenieACS user interface to closely resemble the UniFi Controller, while strictly ensuring the codebase remains easy to synchronize (via merge or rebase) with the original GenieACS upstream repository for bug fixes and core updates.
|
||||
|
||||
1. Core Principles (Non-Negotiable)
|
||||
Sync-Friendly Architecture (Crucial): Write code defensively to prevent future merge conflicts with the original GenieACS project. Prefer extending, overriding, or wrapping components rather than heavily modifying deeply ingrained original core files.
|
||||
|
||||
Visual Fidelity (UniFi): Implement the clean, dashboard-centric, modern aesthetic of the UniFi Controller. Use comparable color palettes, typography, spacing, and network topology layouts.
|
||||
|
||||
Safety First: Never modify the underlying TR-069 communication logic, backend models, or database schemas unless explicitly requested. The focus is strictly on the presentation layer (View).
|
||||
|
||||
Minimal Footprint: When generating files, aim to isolate custom UI assets. Keep custom CSS/SCSS and new UI components in distinct directories or files whenever possible.
|
||||
|
||||
2. GenieACS Customization & Upstream Integration
|
||||
CRITICAL: Whenever the user requests a modification to a page or component, you MUST prioritize the following strategies to keep the fork maintainable:
|
||||
|
||||
Style Overrides: Prefer creating custom stylesheets (e.g., unifi-theme.scss) to override existing GenieACS classes instead of changing inline styles or the original CSS files directly.
|
||||
|
||||
Component Wrapping: If an existing UI component needs significant structural changes, create a new "UniFi-styled" wrapper component that consumes the original GenieACS state/props, rather than gutting the original component.
|
||||
|
||||
Clear Demarcation: When modifying an original GenieACS file is unavoidable, clearly mark the beginning and end of the custom code with comments (e.g., // --- BEGIN UNIFI THEME --- and // --- END UNIFI THEME ---). This makes resolving Git merge conflicts significantly easier.
|
||||
|
||||
3. Code Generation Rules
|
||||
A. Refactoring & Modification
|
||||
Context Awareness: Read the surrounding code to understand the existing state management and API calls. Do not break the data flow between the GenieACS backend and the frontend.
|
||||
|
||||
Preserve Logic: When changing how data is displayed (e.g., changing a simple table to a UniFi-style card grid), ensure all original data points remain accessible or are intentionally hidden via CSS, rather than deleting the data-fetching logic.
|
||||
|
||||
B. New Features (UI Components)
|
||||
Modularity: Build reusable UI components (Buttons, Cards, Navbars) that match the UniFi design system, so they can be applied globally across the GenieACS interface.
|
||||
|
||||
Responsiveness: Ensure all new UI elements are fully responsive, as network management dashboards are often viewed on various screen sizes.
|
||||
|
||||
C. Testing
|
||||
Prioritize visual regression checks and ensure that frontend unit tests (if applicable) are updated to reflect structural DOM changes. Ensure the connection to the GenieACS API remains mocked and functional in tests.
|
||||
|
||||
4. Response Format
|
||||
Plan First: Before generating code, briefly outline your strategy. Specifically state which files will be modified and how you plan to implement the change while minimizing upstream merge conflicts.
|
||||
|
||||
Complete Code: Output the full, functional block of code needed for the file. Do not use placeholders like // ... rest of code unless the file is massive and the change is highly localized.
|
||||
|
||||
Git Context: If a change requires creating a new file to override an old one, explain how to integrate it into the project's build system.
|
||||
|
||||
5. "Do No Harm" Checklist
|
||||
Before outputting the final response, ask yourself:
|
||||
|
||||
Does this modification break the core GenieACS device management logic?
|
||||
|
||||
Will this change cause a massive merge conflict if the upstream repository updates this specific component? (If yes, rethink the approach).
|
||||
|
||||
Does the resulting UI genuinely reflect the UniFi Controller design language?
|
||||
@@ -2,3 +2,4 @@
|
||||
node_modules
|
||||
dist
|
||||
docs/_build
|
||||
.env
|
||||
@@ -0,0 +1,82 @@
|
||||
# AGENTS.md — GenieACS
|
||||
|
||||
GenieACS is a TR-069 Auto Configuration Server for remote management of CPE
|
||||
devices (routers, modems, gateways). TypeScript codebase compiled with esbuild,
|
||||
backed by MongoDB.
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
Four services share a single MongoDB instance:
|
||||
|
||||
- **CWMP** (port 7547) — TR-069 protocol handler; manages device sessions
|
||||
- **NBI** (port 7557) — Northbound REST API for external consumers
|
||||
- **FS** (port 7567) — File server for firmware/config (GridFS-backed)
|
||||
- **UI** (port 3000) — Web interface (Koa backend + Mithril.js SPA frontend)
|
||||
|
||||
Key subsystems: expression engine (`lib/common/expression/`) compiles a
|
||||
Lisp-like DSL used for queries, config, and authorization; session engine
|
||||
(`lib/session.ts`) drives CWMP interactions via declarations rather than
|
||||
imperative RPCs; sandbox (`lib/sandbox.ts`) runs user-defined provision scripts
|
||||
in `vm.Script` with deterministic replay.
|
||||
|
||||
Read `ARCHITECTURE.md` for a full map of the codebase when working on unfamiliar
|
||||
areas. It covers service boundaries, the expression pipeline, the path system,
|
||||
the CWMP session state machine, the database layer, and architectural
|
||||
invariants.
|
||||
|
||||
## Project Structure
|
||||
|
||||
- `lib/` — Core server-side logic
|
||||
- `lib/common/` — Shared code (runs in both Node.js and browser)
|
||||
- `lib/db/` — MongoDB database layer
|
||||
- `lib/ui/` — UI backend helpers
|
||||
- `ui/` — Frontend SPA (Mithril.js)
|
||||
- `bin/` — Service entry points (5 executables)
|
||||
- `build/` — Build scripts (esbuild pipeline)
|
||||
- `test/` — Unit tests (node:test)
|
||||
- `docs/` — User docs (Sphinx/reStructuredText)
|
||||
- `public/` — Static assets (favicon, logo)
|
||||
|
||||
## Build / Lint / Test Commands
|
||||
|
||||
```bash
|
||||
npm run build # Production build (esbuild pipeline -> dist/)
|
||||
NODE_ENV=development npm run build # Dev build (no minification)
|
||||
npm run lint # Prettier + ESLint + tsc --noEmit in parallel
|
||||
npm test # Compile tests with esbuild, run with node --test
|
||||
```
|
||||
|
||||
### Running a Single Test File
|
||||
|
||||
```bash
|
||||
esbuild --log-level=warning --bundle --platform=node --target=node18 \
|
||||
--packages=external --sourcemap=inline --outdir=test test/path.ts \
|
||||
&& node --test --enable-source-maps test/path.js \
|
||||
&& rm test/path.js
|
||||
```
|
||||
|
||||
### Running a Single Test Case
|
||||
|
||||
```bash
|
||||
esbuild --log-level=warning --bundle --platform=node --target=node18 \
|
||||
--packages=external --sourcemap=inline --outdir=test test/path.ts \
|
||||
&& node --test --enable-source-maps --test-name-pattern="^parse$" test/path.js \
|
||||
&& rm test/path.js
|
||||
```
|
||||
|
||||
### Lint Sub-commands
|
||||
|
||||
```bash
|
||||
prettier --prose-wrap always --write .
|
||||
eslint 'bin/*.ts' 'lib/**/*.ts' 'ui/**/*.ts' 'test/**/*.ts' 'build/**/*.ts'
|
||||
tsc --noEmit
|
||||
```
|
||||
|
||||
## Before Committing
|
||||
|
||||
Read `CONTRIBUTING.md` and ensure your changes comply with it. In particular:
|
||||
|
||||
- Run `npm run lint` and `npm test` and fix any failures.
|
||||
- Follow the code style, naming, import, and comment conventions documented
|
||||
there.
|
||||
- Use the Conventional Commits format for commit messages.
|
||||
+479
@@ -0,0 +1,479 @@
|
||||
# Architecture
|
||||
|
||||
This document describes the high-level architecture of GenieACS. If you want to
|
||||
familiarize yourself with the codebase, you are in the right place.
|
||||
|
||||
## Bird's Eye View
|
||||
|
||||
GenieACS is a TR-069 Auto Configuration Server (ACS). It manages CPE devices
|
||||
(routers, modems, gateways) using the CWMP protocol (TR-069). The system
|
||||
consists of four network-facing services that share a MongoDB database:
|
||||
|
||||
```
|
||||
+------------+
|
||||
CPE Devices --->| CWMP (7547)|-+
|
||||
+------------+ |
|
||||
+-----------+ | +---------+
|
||||
CPE Devices --->| FS (7567) |--+---->| MongoDB |
|
||||
+-----------+ | +---------+
|
||||
+-----------+ |
|
||||
OSS / Scripts ->| NBI (7557)|--+
|
||||
+-----------+ |
|
||||
+-----------+ |
|
||||
Administrators->| UI (3000) |--+
|
||||
+-----------+
|
||||
```
|
||||
|
||||
- **CWMP** -- The core TR-069 protocol server. CPE devices connect here to
|
||||
report their state and receive configuration instructions via SOAP/XML over
|
||||
HTTP.
|
||||
- **NBI** -- Northbound Interface. A REST API for external systems (OSS/BSS,
|
||||
scripts, automation) to manage devices, tasks, presets, and configuration
|
||||
programmatically.
|
||||
- **FS** -- File Server. Serves firmware images and configuration files to CPE
|
||||
devices during download operations.
|
||||
- **UI** -- Web interface. A Koa-based backend serving a Mithril.js single-page
|
||||
application for administrators to browse devices, manage configuration, and
|
||||
trigger operations.
|
||||
|
||||
All four services follow the same process model: a primary process forks
|
||||
configurable worker processes via Node.js `cluster` (see `cluster.ts`). Workers
|
||||
connect to MongoDB and start an HTTP(S) server.
|
||||
|
||||
## Code Map
|
||||
|
||||
```
|
||||
bin/ Service entry points (5 executables)
|
||||
lib/ All backend logic
|
||||
common/ Shared utilities (expression engine, Path, errors)
|
||||
expression.ts Expression class hierarchy (base + subclasses)
|
||||
expression/ Expression parser, evaluator, normalizer, minimizer
|
||||
cwmp/ CWMP-service-specific DB and caching
|
||||
db/ Database layer (MongoDB collections, query synthesis)
|
||||
ui/ UI-service-specific API, DB, and caching
|
||||
types/ (empty, reserved)
|
||||
ui/ Frontend SPA (Mithril.js)
|
||||
components/ Reusable UI components (parameter, tags, ping, etc.)
|
||||
css/ Stylesheets (vanilla CSS)
|
||||
icons/ SVG icons (compiled into a sprite)
|
||||
test/ Unit tests (Node.js native test runner)
|
||||
build/ Build scripts (esbuild-based)
|
||||
public/ Static assets (logo, favicon)
|
||||
```
|
||||
|
||||
### Entry Points (`bin/`)
|
||||
|
||||
Each file in `bin/` bootstraps one service. They are structurally identical:
|
||||
initialize logging, read config, fork workers in the primary process, connect to
|
||||
MongoDB and start the HTTP server in each worker.
|
||||
|
||||
- `genieacs-cwmp.ts` -- CWMP service. Unique in that it disables HTTP keep-alive
|
||||
(`keepAliveTimeout: 0`) and provides custom `onConnection` / `onClientError`
|
||||
hooks for TR-069 session lifecycle management.
|
||||
- `genieacs-nbi.ts` -- NBI service. Straightforward REST API server.
|
||||
- `genieacs-fs.ts` -- File server. The leanest service; does not use the
|
||||
extensions subsystem.
|
||||
- `genieacs-ui.ts` -- UI service. Wraps the Koa application as the HTTP
|
||||
listener.
|
||||
- `genieacs-ext.ts` -- **Not a service.** This is a child process worker spawned
|
||||
by the extensions subsystem. It communicates with its parent via IPC,
|
||||
executing user-defined extension scripts in isolation.
|
||||
|
||||
### The Expression System (`lib/common/expression.ts`, `lib/common/expression/`)
|
||||
|
||||
The `Expression` class (defined in `lib/common/expression.ts`) is the most
|
||||
important abstraction in the codebase. It uses a typed class hierarchy:
|
||||
`Expression.Literal` (wraps `string | number | boolean | null`),
|
||||
`Expression.Parameter` (wraps a `Path`), `Expression.Binary` (operator +
|
||||
left/right), `Expression.Unary` (operator + operand), `Expression.FunctionCall`
|
||||
(name + args), and `Expression.Conditional` (condition/then/otherwise). For
|
||||
example, in the SQL-like text syntax:
|
||||
|
||||
```
|
||||
Device.ModelName = "BrandX" AND Events.Inform > 1000
|
||||
```
|
||||
|
||||
Expressions are used pervasively:
|
||||
|
||||
- **Query/filter language** -- Database queries for devices, faults, presets,
|
||||
etc. are all represented as expressions and compiled to MongoDB filters.
|
||||
- **Configuration values** -- Config entries and preset preconditions are
|
||||
expressions, enabling dynamic evaluation.
|
||||
- **Authorization** -- Permission filters and validators are expressions.
|
||||
- **Pagination cursors** -- Keyset pagination boundaries are expressed as filter
|
||||
expressions.
|
||||
|
||||
The expression pipeline flows through four modules:
|
||||
|
||||
1. `parser.ts` -- Parses a SQL-like text syntax into the AST using a hand-rolled
|
||||
recursive descent parser with a `Cursor`-based scanner. Also provides
|
||||
`stringifyExpression()` for serialization. The `map()` / `mapAsync()`
|
||||
tree-walking primitives are abstract methods on the `Expression` class, and
|
||||
`stringify()` is now `Expression.toString()`.
|
||||
2. `normalize.ts` -- Algebraic normalization using exact rational polynomial
|
||||
arithmetic over native `bigint` values. The `Polynomial` class extends
|
||||
`Expression` as an intermediate representation. Ensures equivalent
|
||||
expressions have the same canonical form (e.g., `a + 2 > b` and `a > b - 2`
|
||||
normalize identically).
|
||||
3. `synth.ts` -- Boolean logic minimization via the Espresso algorithm
|
||||
(espresso-iisojs). Converts expressions to minimal sum-of-products form with
|
||||
three-valued logic (true/false/null). Handles domain-specific constraints
|
||||
like comparison ordering and LIKE pattern relationships.
|
||||
4. `evaluate.ts` -- Runtime evaluation. The `reduce()` function evaluates
|
||||
operators on literal values and supports partial evaluation (returns a
|
||||
reduced expression if some values are unknown). Parameter resolution is
|
||||
handled by the `Expression.evaluate()` method (in `lib/common/expression.ts`)
|
||||
which calls `reduce()` after mapping children via a user-supplied callback.
|
||||
|
||||
`pagination.ts` implements cursor-based pagination by generating filter
|
||||
expressions from sort-key bookmarks.
|
||||
|
||||
### The Path System (`lib/common/path.ts`, `lib/common/path-set.ts`)
|
||||
|
||||
`Path` represents a TR-069 parameter path (e.g., `Device.WiFi.SSID.1.Name`).
|
||||
Paths can contain wildcards (`*`) and alias expressions (`[key:value]`) for
|
||||
query-based addressing.
|
||||
|
||||
Key design decisions:
|
||||
|
||||
- **Cached** -- `Path.parse()` caches instances in a two-generation LRU cache
|
||||
rotated every 120 seconds. The constructor is public (used directly by the
|
||||
parser and by methods like `slice()`, `concat()`, and `stripAlias()`).
|
||||
- **Bitmask encoding** -- The `wildcard` and `alias` fields are bitfields for
|
||||
O(1) segment-type checking. This limits paths to 32 segments. A `colon` field
|
||||
tracks the number of attribute path segments (after a `:` separator), enabling
|
||||
`paramLength` and `attrLength` accessors.
|
||||
- **Immutable** -- Segment arrays are `Object.freeze()`-d.
|
||||
|
||||
`PathSet` is a multi-indexed collection of paths supporting pattern-matching
|
||||
queries. It maintains separate `paramSegmentIndex` and `attrSegmentIndex` arrays
|
||||
(one `Map<string, Set<Path>>` per position), plus a `stringIndex` map. The
|
||||
`find()` method takes bitmasks to control which segments require exact matches
|
||||
vs. wildcard compatibility, then uses set intersection across the smallest index
|
||||
sets. A higher-level `findCompat()` method computes the appropriate bitmasks for
|
||||
superset/subset matching.
|
||||
|
||||
### CWMP Protocol Layer (`lib/cwmp.ts`, `lib/soap.ts`, `lib/xml-parser.ts`)
|
||||
|
||||
`xml-parser.ts` is a custom single-pass XML parser (no DOM). It scans
|
||||
character-by-character with bitwise state flags, building a tree of elements
|
||||
with namespace support. Does not support CDATA.
|
||||
|
||||
`soap.ts` handles both parsing CPE SOAP messages and generating ACS SOAP
|
||||
responses. It dispatches on the SOAP body's method name to type-specific parsers
|
||||
for Inform, TransferComplete, GetParameterNamesResponse, etc. Supports CWMP
|
||||
versions 1.0 through 1.4.
|
||||
|
||||
`cwmp.ts` is the HTTP-level CWMP request handler. It manages the session state
|
||||
machine:
|
||||
|
||||
1. **State 0** -- Expects an Inform. Authenticates the device (Basic or Digest
|
||||
auth, configurable via expression). Acquires a distributed lock. Loads device
|
||||
data from MongoDB. Sends InformResponse.
|
||||
2. **State 1** -- Waits for the CPE to send an empty POST (ready for ACS RPCs).
|
||||
Processes any TransferComplete messages.
|
||||
3. **State 2** -- The ACS drives RPCs (GetParameterNames, GetParameterValues,
|
||||
SetParameterValues, AddObject, DeleteObject, Download, Reboot, FactoryReset).
|
||||
The CPE responds to each.
|
||||
|
||||
Session persistence across TCP disconnects: when a socket closes mid-session,
|
||||
the entire `SessionContext` is serialized to Redis (the MongoDB `cache`
|
||||
collection) and restored when the CPE reconnects (identified by a session
|
||||
cookie).
|
||||
|
||||
### Session Engine (`lib/session.ts`)
|
||||
|
||||
The session engine implements the **declaration-driven data fetching** pattern.
|
||||
Rather than issuing RPCs imperatively, provisions create `Declaration` objects
|
||||
stating what paths and attributes they need to read or write. The engine then:
|
||||
|
||||
1. Processes all declarations into a `SyncState` -- a structured plan of which
|
||||
parameters to refresh, which values to set, which instances to create/delete,
|
||||
etc.
|
||||
2. Generates the minimal set of CWMP RPCs needed to fulfill the plan.
|
||||
3. After each RPC response, updates `DeviceData` and re-evaluates.
|
||||
4. Iterates until all declarations are satisfied.
|
||||
|
||||
The preset system (`applyPresets` in `cwmp.ts`) implements a policy engine:
|
||||
presets are rules with precondition expressions that, when matched, contribute
|
||||
provisions to the session. After provisions execute, if device data changed,
|
||||
presets are re-evaluated (up to 4 cycles to prevent infinite loops).
|
||||
|
||||
### Provisions and Virtual Parameters
|
||||
|
||||
**Provisions** are the unit of configuration intent. Built-in provisions
|
||||
(`default-provisions.ts`) include `refresh`, `value`, `tag`, `reboot`, `reset`,
|
||||
`download`, and `instances`. Custom provisions are user-defined JavaScript
|
||||
scripts stored in the `provisions` MongoDB collection.
|
||||
|
||||
**Virtual parameters** are scripts that present computed/derived values as if
|
||||
they were real device parameters under the `VirtualParameters.*` namespace. They
|
||||
run in two phases: a "get" phase reads real parameters and returns a computed
|
||||
value; a "set" phase translates a desired value into real parameter changes.
|
||||
Virtual parameters can reference other virtual parameters (up to depth 8).
|
||||
|
||||
### Sandbox (`lib/sandbox.ts`)
|
||||
|
||||
The sandbox provides a secure execution environment for provision and virtual
|
||||
parameter scripts using `vm.Script` with a 50ms timeout. It uses a
|
||||
**replay-based execution model**:
|
||||
|
||||
1. A script runs and calls `declare()` to request data.
|
||||
2. When `commit()` is called, the script throws a sentinel symbol and exits.
|
||||
3. The engine fetches the requested data via CWMP RPCs.
|
||||
4. The script is **re-run from the beginning** with the fetched data available.
|
||||
5. Earlier `declare()` calls return cached results; the script progresses
|
||||
further.
|
||||
6. This repeats until the script completes without throwing.
|
||||
|
||||
The sandbox API: `declare(path, timestamps, values)` returns a
|
||||
`ParameterWrapper` proxy; `clear(path, timestamp, attributes)` invalidates
|
||||
cached data; `ext(...args)` calls external extensions (results are cached per
|
||||
revision to survive replays); `commit()` explicitly triggers a fetch cycle.
|
||||
`Math.random()` is replaced with a seeded PRNG for determinism.
|
||||
|
||||
### Device Data Model (`lib/types.ts`, `lib/device.ts`)
|
||||
|
||||
`DeviceData` is the in-memory working copy of a device's parameter tree during a
|
||||
session:
|
||||
|
||||
- `paths: PathSet` -- All known parameter paths.
|
||||
- `timestamps: VersionedMap<Path, number>` -- When each path was last refreshed.
|
||||
- `attributes: VersionedMap<Path, Attributes>` -- Per-path attributes (object,
|
||||
writable, value, notification, accessList), each paired with a timestamp.
|
||||
- `trackers` / `changes` -- Change tracking for re-evaluation.
|
||||
|
||||
`VersionedMap` (in `versioned-map.ts`) provides multi-revision snapshots,
|
||||
enabling the sandbox replay model where scripts may be re-run at different
|
||||
revision levels.
|
||||
|
||||
`device.ts` handles setting and clearing parameter data with invariant
|
||||
enforcement (e.g., if `value` is set, `object` is forced to 0; parent paths are
|
||||
ensured to exist). The `unpack()` function resolves wildcards and alias paths
|
||||
against concrete device data.
|
||||
|
||||
### NBI (`lib/nbi.ts`)
|
||||
|
||||
A raw Node.js HTTP listener (no framework) with regex-based URL routing.
|
||||
Endpoints include CRUD for presets, provisions, virtual parameters, objects, and
|
||||
files; device task management (with optional synchronous execution via
|
||||
connection request); fault management; generic collection querying; and ping.
|
||||
|
||||
The NBI uses MongoDB-style JSON queries directly. For the `devices` collection,
|
||||
`query.ts` expands user-friendly queries by auto-appending `._value` to
|
||||
parameter paths and generating multi-type interpretations (string, number, date,
|
||||
regex) for filter values.
|
||||
|
||||
### File Server (`lib/fs.ts`)
|
||||
|
||||
A minimal HTTP file server reading from MongoDB GridFS. Supports GET/HEAD with
|
||||
full HTTP caching (ETag, Last-Modified, If-None-Match, If-Modified-Since) and
|
||||
Range requests (HTTP 206) for partial content downloads. Files are cached
|
||||
in-memory via memoization.
|
||||
|
||||
### UI Backend (`lib/ui.ts`, `lib/ui/`)
|
||||
|
||||
A Koa application with JWT authentication, role-based authorization, and a rich
|
||||
CRUD API under `/api/`. The root route serves an HTML shell that bootstraps the
|
||||
Mithril.js SPA with injected config, user info, and hashed asset filenames.
|
||||
|
||||
`lib/ui/api.ts` defines generic CRUD endpoints for all resource types (devices,
|
||||
presets, provisions, files, config, users, permissions, faults, tasks) with
|
||||
authorization checks at every level. Specialized endpoints handle file
|
||||
upload/download, synchronous task execution, tag management, password changes,
|
||||
and CSV export.
|
||||
|
||||
`lib/ui/db.ts` translates between the UI's flat parameter representation and
|
||||
MongoDB's nested document structure. The `flattenDevice()` function is the key
|
||||
transformation: it recursively walks the nested device document and produces a
|
||||
flat key-value map with colon-delimited attribute keys (e.g.,
|
||||
`"Device.WiFi.SSID.1.Name" -> value`,
|
||||
`"Device.WiFi.SSID.1.Name:type" -> "xsd:string"`,
|
||||
`"Device.WiFi.SSID.1.Name:writable" -> true`). The `FlatDevice` type is
|
||||
`Record<string, Value>` where `Value = string | number | boolean | null`.
|
||||
|
||||
`lib/ui/local-cache.ts` caches permissions, users, and config in-process with
|
||||
hash-based revision tracking. The `getConfig()` function uses typed overloads --
|
||||
it takes a typed default value (`string`, `number`, or `boolean`) and an
|
||||
expression evaluation callback, returning a value of the same type as the
|
||||
default.
|
||||
|
||||
### Database Layer (`lib/db/`)
|
||||
|
||||
`db/db.ts` is the single entry point to MongoDB. It manages 14 collections:
|
||||
|
||||
| Collection | Purpose |
|
||||
| ------------------- | -------------------------------- |
|
||||
| `devices` | CPE device parameter trees |
|
||||
| `presets` | Configuration rules |
|
||||
| `provisions` | Provision scripts |
|
||||
| `virtualParameters` | Virtual parameter scripts |
|
||||
| `objects` | Generic objects |
|
||||
| `tasks` | Queued device management tasks |
|
||||
| `faults` | Error records with retry state |
|
||||
| `operations` | In-flight async operations |
|
||||
| `files` (GridFS) | Firmware images and config files |
|
||||
| `permissions` | RBAC rules |
|
||||
| `users` | User accounts |
|
||||
| `config` | Key-value configuration |
|
||||
| `cache` | Distributed cache (TTL index) |
|
||||
| `locks` | Distributed locks (TTL index) |
|
||||
|
||||
`db/synth.ts` is the sophisticated expression-to-MongoDB query compiler. It
|
||||
normalizes expressions, converts them to a Boolean satisfiability representation
|
||||
using the `Clause` hierarchy, minimizes via Espresso, and emits MongoDB
|
||||
`$and`/`$or`/`$not` filter objects. This is used by the UI backend.
|
||||
|
||||
`cwmp/db.ts` handles CWMP-specific persistence: loading the nested device
|
||||
document into `DeviceData` (`fetchDevice`), diffing changes back into MongoDB
|
||||
update operations (`saveDevice`), and managing faults, tasks, and operations.
|
||||
|
||||
### Query Systems
|
||||
|
||||
There are two separate query paths:
|
||||
|
||||
1. **NBI queries** (`lib/query.ts`) -- Processes MongoDB-style JSON queries from
|
||||
external API consumers. Expands parameter names, generates multi-type value
|
||||
interpretations, and passes through to MongoDB directly.
|
||||
|
||||
2. **UI/Expression queries** (`lib/db/synth.ts`) -- Compiles the internal
|
||||
expression language into optimized MongoDB filters via Boolean minimization.
|
||||
This is the more sophisticated path, used by the UI backend.
|
||||
|
||||
### Frontend (`ui/`)
|
||||
|
||||
A Mithril.js SPA with hash-based routing (`#!/`). Key modules:
|
||||
|
||||
- `app.ts` -- Route definitions. The `pagify()` function wraps each page into a
|
||||
`RouteResolver` that handles initialization, error boundaries, and data
|
||||
fulfillment.
|
||||
- `layout.ts` -- Top-level layout (header, navigation, content, overlay).
|
||||
- `store.ts` -- Centralized data store. Implements a query-based reactive cache
|
||||
with deduplication and incremental fetching. The `fulfill()` method (called
|
||||
after every render) batches pending queries, computes filter diffs via
|
||||
`unionDiff()`, and fetches only missing data. Connection monitoring polls
|
||||
every 3 seconds for server health, clock skew, and config changes.
|
||||
- `components.ts` -- Component registry with a context propagation system. The
|
||||
proxied `m()` function resolves string component names and the `m.context()`
|
||||
API passes data (like the current device object) down the component tree
|
||||
without explicit prop threading.
|
||||
- `smart-query.ts` -- Translates user-friendly `Label: value` searches into
|
||||
filter expressions with type-aware matching (string, number, timestamp, MAC
|
||||
address, tag).
|
||||
- `task-queue.ts` -- Two-stage task pipeline: staging (user configures tasks)
|
||||
then queue (tasks are committed and executed via the backend).
|
||||
- `dynamic-loader.ts` -- Lazy loading of heavy libraries (CodeMirror, YAML) via
|
||||
dynamic `import()`.
|
||||
|
||||
### Build System (`build/`)
|
||||
|
||||
The build is a self-bootstrapping esbuild pipeline (`npm run build` pipes
|
||||
`build/build.ts` through esbuild then node). It produces:
|
||||
|
||||
- **Backend binaries** -- 5 entry points bundled for Node.js 12+ with shebang
|
||||
banners, executable permissions, and `.js` extension stripped.
|
||||
- **Frontend bundle** -- `ui/app.ts` bundled for browsers (ESM, code-split) with
|
||||
content-hashed filenames.
|
||||
- **CSS** -- `ui/css/app.css` bundled and minified with content-hashed output.
|
||||
- **SVG sprite** -- All icons in `ui/icons/` optimized via SVGO and combined
|
||||
into a single sprite.
|
||||
|
||||
`build/assets.ts` is a compile-time bridge: at rest it contains placeholder
|
||||
filenames; during build, the `assetsPlugin` replaces them with actual
|
||||
content-hashed names so both backend and frontend reference the correct assets.
|
||||
|
||||
Build metadata includes the date and a hash derived from the git state, appended
|
||||
to the package version.
|
||||
|
||||
## Cross-Cutting Concerns
|
||||
|
||||
### Multi-Level Caching
|
||||
|
||||
```
|
||||
memoize.ts In-process function cache (2-4 min, two-generation rotation)
|
||||
|
|
||||
local-cache.ts In-process snapshot cache (5s refresh, hash-based revisions)
|
||||
|
|
||||
cache.ts MongoDB-backed distributed cache (configurable TTL)
|
||||
|
|
||||
MongoDB TTL index auto-expiration
|
||||
```
|
||||
|
||||
Each service has its own `local-cache` that periodically checks the distributed
|
||||
cache for staleness. Distributed locks (`lock.ts`) coordinate rebuilds so only
|
||||
one worker does the expensive computation.
|
||||
|
||||
### Configuration (`lib/config.ts`)
|
||||
|
||||
Three-tier priority: CLI args > environment variables (`GENIEACS_*`) > config
|
||||
file (`config.json`) > defaults. Supports per-device overrides by appending
|
||||
`-OUI-ProductClass-SerialNumber` to option names.
|
||||
|
||||
### Distributed Locking (`lib/lock.ts`)
|
||||
|
||||
MongoDB-based mutual exclusion using upsert + duplicate key detection. TTL
|
||||
indexes prevent deadlocks from crashed processes. Clock skew tolerance of 30
|
||||
seconds.
|
||||
|
||||
### Authentication
|
||||
|
||||
- **CWMP devices** -- HTTP Basic or Digest auth, configurable per-device via
|
||||
expressions (`auth.ts`).
|
||||
- **UI users** -- JWT tokens in cookies. Passwords hashed with PBKDF2-SHA512
|
||||
(10000 iterations). Role-based authorization via `Authorizer` with
|
||||
expression-based filters and validators.
|
||||
|
||||
### Connection Requests (`lib/connection-request.ts`)
|
||||
|
||||
Three methods to ask a CPE device to initiate a CWMP session:
|
||||
|
||||
- **HTTP** -- Standard TR-069 connection request with Digest/Basic auth.
|
||||
- **UDP** -- For NAT traversal (STUN-based) with HMAC-SHA1 signed messages.
|
||||
- **XMPP** -- TR-069 Annex K via a full XMPP client (`xmpp-client.ts`).
|
||||
|
||||
### Extensions (`lib/extensions.ts`)
|
||||
|
||||
User-defined scripts executed in long-lived child processes (`genieacs-ext.ts`)
|
||||
communicating via IPC. Processes are lazily spawned per script name and reused.
|
||||
Each request gets a unique ID; responses are matched by ID with a configurable
|
||||
timeout.
|
||||
|
||||
### Logging (`lib/logger.ts`)
|
||||
|
||||
Dual-stream structured logging (application + access logs). Supports simple and
|
||||
JSON formats, systemd journal integration, and automatic log rotation detection.
|
||||
Protocol traces can be written to a debug file in YAML or JSON format
|
||||
(`debug.ts`).
|
||||
|
||||
### Three-Valued Logic
|
||||
|
||||
The system consistently implements SQL-style three-valued logic
|
||||
(true/false/null). This is visible in expression evaluation, the `Clause`
|
||||
hierarchy's separate true/false/null methods, and the 2-bit minterm encoding in
|
||||
the Boolean minimizer. NULL means "unknown" and propagates through operations
|
||||
following SQL semantics.
|
||||
|
||||
## Architectural Invariants
|
||||
|
||||
- **The expression system does not depend on any service-specific code.** The
|
||||
`lib/common/expression/` modules are pure and shared across all services.
|
||||
|
||||
- **The sandbox is deterministic across replays.** `Math.random()` is seeded
|
||||
from the device ID, `Date.now()` is controlled, and extension results are
|
||||
cached. A script re-run with the same inputs produces the same outputs.
|
||||
|
||||
- **Services share no in-process state.** All cross-process coordination goes
|
||||
through MongoDB (the `cache` and `locks` collections). Each worker process is
|
||||
independent.
|
||||
|
||||
- **Device data is never mutated in place during a session.** `VersionedMap`
|
||||
provides revision-based snapshots. The sandbox writes to new revisions;
|
||||
earlier revisions remain readable for re-evaluation.
|
||||
|
||||
- **CWMP session exclusivity.** A distributed lock (`cwmp_session_<deviceId>`)
|
||||
ensures only one session exists per device at a time. The lock is refreshed
|
||||
periodically and released at session end.
|
||||
|
||||
- **The UI backend never queries MongoDB with raw user input.** All queries go
|
||||
through the expression-to-MongoDB compiler (`db/synth.ts`), which validates
|
||||
and normalizes expressions before generating filters.
|
||||
@@ -1,5 +1,12 @@
|
||||
# Change Log
|
||||
|
||||
## 1.2.14 (2026-03-12)
|
||||
|
||||
- Prevent UI crash when a malformed URL is sent to the server.
|
||||
|
||||
- Fix potential edge-case bugs in expression evaluation and session
|
||||
serialization.
|
||||
|
||||
## 1.2.13 (2024-06-06)
|
||||
|
||||
- Increase connection timeout for UI and NBI from 30 to 120 seconds to avoid
|
||||
|
||||
+334
-21
@@ -1,31 +1,348 @@
|
||||
# Have a question?
|
||||
# Contributing to GenieACS
|
||||
|
||||
Please ask in the [forum] instead of opening a Github Issue. The Github issue
|
||||
tracker is strictly for bug reports.
|
||||
## Questions and Support
|
||||
|
||||
# Issues
|
||||
Please use the [forum](https://forum.genieacs.com) for questions, help requests,
|
||||
and general discussion. GitHub Issues are reserved for confirmed bug reports.
|
||||
|
||||
We use GitHub issues to track bugs. Please ensure your bug description is clear
|
||||
and has sufficient instructions to be able to reproduce the issue.
|
||||
## Issues
|
||||
|
||||
If you face interoperability issues with your CPE, more often than not it's a
|
||||
device related issue. Please consult the [forum] before submitting an Issue.
|
||||
We use GitHub Issues to track bugs. When filing a bug report:
|
||||
|
||||
# Pull Requests
|
||||
- Provide a clear description of the problem.
|
||||
- Include steps to reproduce the issue.
|
||||
- Note the GenieACS version, Node.js version, and MongoDB version.
|
||||
- Include relevant log output if applicable.
|
||||
|
||||
Pull requests are welcome. If you have a fix for a bug, go ahead and create a
|
||||
pull request. For other kinds of enhancements, please discuss in the [forum]
|
||||
first.
|
||||
If you face interoperability issues with a CPE, it is more often than not a
|
||||
device-specific issue. Please consult the [forum](https://forum.genieacs.com)
|
||||
before opening an Issue.
|
||||
|
||||
Please follow the guidelines [here][commit-guidelines] when committing your
|
||||
changes.
|
||||
## Pull Requests
|
||||
|
||||
Pull requests are welcome. For bug fixes, go ahead and open a PR directly. For
|
||||
new features or significant changes, please discuss in the
|
||||
[forum](https://forum.genieacs.com) first to ensure alignment with the project's
|
||||
direction.
|
||||
|
||||
When submitting a PR:
|
||||
|
||||
- Keep changes focused. One PR should address one concern.
|
||||
- Run `npm run lint` and `npm test` before submitting.
|
||||
- Update documentation in `docs/` if your change affects user-facing behavior.
|
||||
- Add tests where appropriate (see [Testing](#testing)).
|
||||
- Write a clear PR description explaining what the change does and why.
|
||||
|
||||
## Code Style
|
||||
|
||||
### Naming Conventions
|
||||
|
||||
- **Variables and functions**: `camelCase`
|
||||
- **Classes**: `PascalCase`
|
||||
- **Interfaces and type aliases**: `PascalCase`, without an `I` or `T` prefix
|
||||
- **Module-level constants**: `UPPER_SNAKE_CASE`
|
||||
- **Private class members**: prefixed with underscore (`_name`, `_cache`)
|
||||
|
||||
Choose descriptive, meaningful names. A longer clear name is better than a short
|
||||
ambiguous one.
|
||||
|
||||
### Imports
|
||||
|
||||
Organize imports in three groups, in this order:
|
||||
|
||||
1. Node.js built-in modules, using the `node:` prefix
|
||||
2. External packages
|
||||
3. Local project modules
|
||||
|
||||
Use `.ts` extensions for all local imports.
|
||||
|
||||
```typescript
|
||||
import { readFileSync } from "node:fs";
|
||||
import * as http from "node:http";
|
||||
|
||||
import { Collection } from "mongodb";
|
||||
|
||||
import { connect, disconnect } from "./db.ts";
|
||||
import Path from "./common/path.ts";
|
||||
```
|
||||
|
||||
### Functions and Exports
|
||||
|
||||
Use the `function` keyword for all named and exported function declarations.
|
||||
Arrow functions should only be used for callbacks and short inline expressions.
|
||||
|
||||
```typescript
|
||||
// Named/exported functions use function keyword
|
||||
export function processRequest(req: Request): Response {
|
||||
// ...
|
||||
}
|
||||
|
||||
// Arrow functions for callbacks
|
||||
items.filter((item) => item.active);
|
||||
promise.then((result) => {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
- All function declarations must have explicit return types. This is enforced by
|
||||
ESLint.
|
||||
- Using `any` is permitted where full typing would be impractical, but prefer
|
||||
more specific types when reasonable.
|
||||
- Prefer `Record<string, T>` over `{ [key: string]: T }` for mapped types.
|
||||
- Use type assertions (`as`) sparingly and only when you have stronger type
|
||||
knowledge than the compiler.
|
||||
|
||||
### Comments
|
||||
|
||||
Code should be self-documenting. Use comments sparingly and only when the "why"
|
||||
is not obvious from the code itself. When you do comment, use inline `//` style.
|
||||
|
||||
Do not use JSDoc (`/** */`) or block comments (`/* */`).
|
||||
|
||||
```typescript
|
||||
// Good: explains a non-obvious reason
|
||||
// Escapes everything except alphanumerics and underscore
|
||||
function escapeRegExp(str: string): string {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
// Good: links to an external reference
|
||||
// Source: http://stackoverflow.com/a/6969486
|
||||
|
||||
// Good: flags a known limitation
|
||||
// TODO support "MD5-sess" algorithm directive
|
||||
|
||||
// Bad: restates what the code does
|
||||
// Loop through each item and increment the counter
|
||||
for (const item of items) {
|
||||
counter++;
|
||||
}
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
Use `== null` (not `=== null || === undefined`) for null/undefined checks.
|
||||
ESLint is configured to allow this.
|
||||
|
||||
## Testing
|
||||
|
||||
### When to Write Tests
|
||||
|
||||
Tests are most valuable for pure logic: parsers, data transformations,
|
||||
algorithms, and utility functions where inputs and outputs are well defined. A
|
||||
regression test accompanying a bug fix is also worthwhile to prevent the same
|
||||
issue from resurfacing.
|
||||
|
||||
Not every change needs a test. Use judgment and consider the likelihood and cost
|
||||
of breakage. Avoid writing tests that duplicate coverage already provided by
|
||||
existing tests, and resist the urge to test trivial code just for the sake of
|
||||
coverage numbers.
|
||||
|
||||
### Conventions
|
||||
|
||||
Tests use the Node.js built-in test runner (`node:test`) and assertion module
|
||||
(`node:assert`). No external test libraries or mocking frameworks are used.
|
||||
|
||||
```typescript
|
||||
import test from "node:test";
|
||||
import assert from "node:assert";
|
||||
|
||||
void test("parseValue returns correct type for integer strings", () => {
|
||||
const result = parseValue("42");
|
||||
assert.strictEqual(result, 42);
|
||||
});
|
||||
|
||||
void test("parseValue throws on invalid input", () => {
|
||||
assert.throws(() => parseValue(""), new Error("empty value"));
|
||||
});
|
||||
```
|
||||
|
||||
Key conventions:
|
||||
|
||||
- Prefix `test()` calls with `void` to satisfy the `no-floating-promises` lint
|
||||
rule.
|
||||
- Keep tests flat. Do not nest `describe` blocks.
|
||||
- Use descriptive test names that state what is being tested and the expected
|
||||
outcome.
|
||||
- Use `assert.strictEqual()` for value comparisons and
|
||||
`assert.deepStrictEqual()` for objects and arrays.
|
||||
|
||||
Test files live in the `test/` directory and are named after the module they
|
||||
test (e.g. `test/path.ts` tests `lib/common/path.ts`).
|
||||
|
||||
## Dependencies
|
||||
|
||||
This project deliberately keeps its dependency footprint small. Before adding a
|
||||
new dependency:
|
||||
|
||||
- Prefer Node.js built-in APIs when they can do the job.
|
||||
- Consider whether the functionality is simple enough to implement directly.
|
||||
- Justify the addition in your PR description.
|
||||
|
||||
Do not add development tool dependencies (linter plugins, editor integrations,
|
||||
etc.) without prior discussion.
|
||||
|
||||
## File Organization
|
||||
|
||||
| Directory | Contents |
|
||||
| ------------- | -------------------------------------------- |
|
||||
| `lib/` | Core server-side application code |
|
||||
| `lib/common/` | Code shared between server and browser |
|
||||
| `lib/db/` | Database layer (MongoDB) |
|
||||
| `lib/ui/` | UI backend helpers |
|
||||
| `ui/` | Frontend code (Mithril.js SPA) |
|
||||
| `bin/` | Service entry points |
|
||||
| `build/` | Build tooling |
|
||||
| `test/` | Test files |
|
||||
| `docs/` | User documentation (Sphinx/reStructuredText) |
|
||||
| `public/` | Static assets (favicon, logo) |
|
||||
|
||||
Place new code in the directory that matches its purpose. Server-side logic
|
||||
belongs in `lib/`, code that must run in both Node.js and the browser belongs in
|
||||
`lib/common/`, and frontend-only code belongs in `ui/`.
|
||||
|
||||
## Documentation
|
||||
|
||||
User documentation lives in the `docs/` directory as reStructuredText files
|
||||
built with Sphinx and published to
|
||||
[docs.genieacs.com](https://docs.genieacs.com).
|
||||
|
||||
When your change affects user-facing behavior:
|
||||
|
||||
- Update the relevant documentation in `docs/`.
|
||||
- If adding a new feature, consider whether it warrants a new page or a section
|
||||
in an existing page.
|
||||
- Keep documentation concise and practical. Match the existing tone: direct,
|
||||
factual, no filler.
|
||||
|
||||
Documentation changes should be included in the same PR as the code change, not
|
||||
submitted separately.
|
||||
|
||||
### ARCHITECTURE.md
|
||||
|
||||
`ARCHITECTURE.md` describes the high-level architecture of the project: the
|
||||
service boundaries, major subsystems, data flow, and key invariants. It is aimed
|
||||
at contributors who need a mental map of the codebase.
|
||||
|
||||
This file has a different update cadence than `docs/`. It should be revisited a
|
||||
few times a year rather than kept in lockstep with every code change. When you
|
||||
do update it, follow these principles:
|
||||
|
||||
- **Only describe things that are unlikely to change frequently.** Module
|
||||
responsibilities, service boundaries, key data structures, and architectural
|
||||
invariants belong here. Implementation details, function signatures, and
|
||||
config option lists do not.
|
||||
- **Name important files, modules, and types but do not link them.** Links go
|
||||
stale. Encourage the reader to use symbol search to find named entities; this
|
||||
also helps them discover related, similarly named things.
|
||||
- **Keep it short.** Every recurring contributor will read it. A shorter
|
||||
document is less likely to become stale and more likely to be maintained.
|
||||
- **Describe the "what" and "where", not the "how".** This is a map of the
|
||||
country, not an atlas of its states. Pull detailed explanations into inline
|
||||
code comments or separate documents.
|
||||
- **Call out architectural invariants explicitly.** Important invariants are
|
||||
often expressed as the _absence_ of something (e.g., "the expression system
|
||||
does not depend on service-specific code") and are hard to discover by reading
|
||||
code alone.
|
||||
|
||||
## Git Workflow
|
||||
|
||||
### Commit Messages
|
||||
|
||||
This project follows the
|
||||
[Conventional Commits](https://www.conventionalcommits.org/) format:
|
||||
|
||||
<type>(<scope>): <subject>
|
||||
|
||||
[optional body]
|
||||
|
||||
- Use the imperative mood, present tense: "Fix bug", not "Fixed bug" or "Fixes
|
||||
bug".
|
||||
- Do not capitalize the first letter of the subject (the type prefix handles
|
||||
visual structure).
|
||||
- Do not end the subject line with a period.
|
||||
- Keep the subject line under 72 characters.
|
||||
- When more context is helpful, add a body separated from the subject by a blank
|
||||
line, wrapped at 72 characters. The goal is to provide enough information for
|
||||
someone scanning the commit history to find a specific change (e.g. for
|
||||
troubleshooting or rebasing) or to draft changelog entries for a release.
|
||||
Don't be verbose, but don't be cryptic either.
|
||||
|
||||
#### Types
|
||||
|
||||
| Type | When to use |
|
||||
| ---------- | --------------------------------------------------------------------------- |
|
||||
| `fix` | Bug fixes |
|
||||
| `feat` | New features or capabilities |
|
||||
| `refactor` | Code restructuring with no behavior change |
|
||||
| `test` | Adding or updating tests |
|
||||
| `docs` | Documentation changes (`docs/`, `CONTRIBUTING.md`, `README.md`) |
|
||||
| `build` | Build system, dependencies, esbuild config, `package.json` scripts |
|
||||
| `chore` | Maintenance tasks that don't fit above (`.gitignore`, tooling config, etc.) |
|
||||
|
||||
#### Scopes
|
||||
|
||||
Scope is optional. Use it when it adds useful context; omit it when the change
|
||||
is cross-cutting or the subject already makes it obvious.
|
||||
|
||||
| Scope | Covers |
|
||||
| ------ | ----------------------------------- |
|
||||
| `cwmp` | CWMP service (including extensions) |
|
||||
| `nbi` | Northbound REST API service |
|
||||
| `fs` | File service |
|
||||
| `ui` | Web UI (frontend and backend) |
|
||||
| `db` | Database layer |
|
||||
|
||||
If a change touches multiple scopes, either pick the primary one or omit the
|
||||
scope entirely.
|
||||
|
||||
#### Examples
|
||||
|
||||
feat(nbi): add bulk device delete endpoint
|
||||
fix(cwmp): handle missing ParameterKey in InformResponse
|
||||
refactor(db): replace raw queries with parameterized calls
|
||||
fix(ui): correct parameter table sort order
|
||||
test: add XML parser edge case coverage
|
||||
docs: update provisioning guide for new API
|
||||
build: upgrade esbuild to v0.20
|
||||
|
||||
fix(cwmp): increase server timeout to 2 mins
|
||||
|
||||
To allow enough time for running unindexed queries in large
|
||||
deployments.
|
||||
|
||||
### Branches
|
||||
|
||||
- `master` is the main development branch.
|
||||
- Create a feature or fix branch for your work and open a PR against `master`.
|
||||
- Use concise, descriptive branch names in lowercase with hyphens:
|
||||
`fix-race-condition`, `support-xmpp-requests`.
|
||||
|
||||
## Changelog
|
||||
|
||||
The changelog (`CHANGELOG.md`) is maintained for each release and is written for
|
||||
users and system administrators, not developers.
|
||||
|
||||
- Write entries as clear, user-facing prose. Do not copy commit messages
|
||||
verbatim.
|
||||
- Each entry should describe what changed and, when helpful, why it matters.
|
||||
- Group entries under a version heading with the release date:
|
||||
`## 1.2.13 (2024-06-06)`.
|
||||
- Start each entry with a verb: "Fix", "Add", "Improve", "Remove", etc.
|
||||
- Include enough context that a user can understand the impact without reading
|
||||
the code.
|
||||
|
||||
You do not need to update the changelog in your PR. The maintainer will add
|
||||
changelog entries during the release process.
|
||||
|
||||
## Contributor License Agreement
|
||||
|
||||
By submitting a pull request to this repository, you acknowledge that, while
|
||||
maintaining copyright, you grant Zaid Abdulla a perpetual, worldwide, non-
|
||||
exclusive, no-charge, royalty-free, irrevocable license to reproduce, prepare
|
||||
derivative works of, publicly display, publicly perform, sublicense, and
|
||||
maintaining copyright, you grant GenieACS Inc. a perpetual, worldwide,
|
||||
non-exclusive, no-charge, royalty-free, irrevocable license to reproduce,
|
||||
prepare derivative works of, publicly display, publicly perform, sublicense, and
|
||||
distribute your contributions and such derivative works under the AGPLv3 license
|
||||
or any other license terms, including, but not limited to, proprietary or
|
||||
commercial license terms.
|
||||
@@ -33,7 +350,3 @@ commercial license terms.
|
||||
You confirm that you own or have rights to distribute and sublicense the source
|
||||
code contained therein, and that your content does not infringe upon the
|
||||
intellectual property rights of a third party.
|
||||
|
||||
[commit-guidelines]:
|
||||
http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
|
||||
[forum]: https://forum.genieacs.com
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
# =========================================================
|
||||
# GenieACS — UniFi Theme Fork
|
||||
# Multi-stage build:
|
||||
# Stage 1 (builder): instala devDeps + compila o projeto
|
||||
# Stage 2 (runtime): imagem mínima só com dist/ + prodDeps
|
||||
# =========================================================
|
||||
|
||||
# ── Stage 1: builder ─────────────────────────────────────
|
||||
FROM node:20-alpine AS builder
|
||||
|
||||
# Dependências de sistema necessárias para o build
|
||||
# (git é exigido pelo build.ts para calcular o metadata de versão)
|
||||
RUN apk add --no-cache git python3 make g++
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Copiar manifests primeiro para cache de camadas do npm install
|
||||
COPY package.json npm-shrinkwrap.json ./
|
||||
|
||||
# Instalar TODAS as dependências (incluindo devDependencies)
|
||||
RUN npm ci --fund=false --audit=false
|
||||
|
||||
# Copiar o restante do fonte
|
||||
COPY . .
|
||||
|
||||
# Compilar o projeto (gera dist/)
|
||||
RUN npm run build
|
||||
|
||||
# ── Stage 2: runtime ─────────────────────────────────────
|
||||
FROM node:20-alpine AS runtime
|
||||
|
||||
# Timezone e utilitários mínimos
|
||||
RUN apk add --no-cache tzdata dumb-init
|
||||
|
||||
WORKDIR /opt/genieacs
|
||||
|
||||
# Copiar artefatos compilados
|
||||
COPY --from=builder /build/dist ./
|
||||
|
||||
# Instalar apenas dependências de produção a partir do dist/
|
||||
RUN npm ci --omit=dev --fund=false --audit=false
|
||||
|
||||
# Criar diretórios de configuração e extensões
|
||||
RUN mkdir -p \
|
||||
/opt/genieacs/config/ext \
|
||||
/opt/genieacs/logs \
|
||||
&& addgroup -S genieacs \
|
||||
&& adduser -S -G genieacs genieacs \
|
||||
&& chown -R genieacs:genieacs /opt/genieacs
|
||||
|
||||
USER genieacs
|
||||
|
||||
# Variáveis de ambiente padrão (sobrescreva no compose ou .env)
|
||||
ENV GENIEACS_MONGODB_CONNECTION_URL=mongodb://mongo:27017/genieacs \
|
||||
GENIEACS_EXT_DIR=/opt/genieacs/config/ext \
|
||||
GENIEACS_LOG_FORMAT=json \
|
||||
GENIEACS_ACCESS_LOG_FORMAT=json \
|
||||
GENIEACS_UI_JWT_SECRET=change-me-in-production \
|
||||
GENIEACS_CWMP_WORKER_PROCESSES=1 \
|
||||
GENIEACS_NBI_WORKER_PROCESSES=1 \
|
||||
GENIEACS_FS_WORKER_PROCESSES=1 \
|
||||
GENIEACS_UI_WORKER_PROCESSES=1
|
||||
|
||||
# Expor todas as portas dos serviços
|
||||
EXPOSE 3000 7547 7557 7567
|
||||
|
||||
# dumb-init garante propagação correta de sinais
|
||||
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
|
||||
@@ -1,5 +1,9 @@
|
||||
# GenieACS
|
||||
|
||||
**This is the development branch for GenieACS v1.3. It is unstable and not ready
|
||||
for production use. For the latest stable release, see the
|
||||
[v1.2 branch](https://github.com/genieacs/genieacs/tree/v1.2).**
|
||||
|
||||
GenieACS is a high performance Auto Configuration Server (ACS) for remote
|
||||
management of TR-069 enabled devices. It utilizes a declarative and fault
|
||||
tolerant configuration engine for automating complex provisioning scenarios at
|
||||
@@ -72,5 +76,5 @@ For commercial support options, please visit
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2013-2021 GenieACS Inc. GenieACS is released under the
|
||||
Copyright 2013-2026 GenieACS Inc. GenieACS is released under the
|
||||
[AGPLv3 license terms](https://raw.githubusercontent.com/genieacs/genieacs/master/LICENSE).
|
||||
@@ -55,6 +55,7 @@ process.on("message", (message) => {
|
||||
if (!script) {
|
||||
const cwd = process.env["GENIEACS_EXT_DIR"];
|
||||
process.chdir(cwd);
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
script = require(`${cwd}/${fileName}`);
|
||||
}
|
||||
|
||||
|
||||
+48
-11
@@ -92,6 +92,19 @@ const assetsPlugin = {
|
||||
},
|
||||
} as esbuild.Plugin;
|
||||
|
||||
const seedPlugin = {
|
||||
name: "seed",
|
||||
setup(build) {
|
||||
build.onLoad({ filter: /\/seed\// }, (args) => {
|
||||
if (args.with?.["type"] !== "text") return undefined;
|
||||
let contents = fs.readFileSync(args.path, "utf8");
|
||||
// Strip TypeScript directives that are only needed for type-checking
|
||||
contents = contents.replace(/^\s*\/\/\s*@ts-.*\n/gm, "");
|
||||
return { contents, loader: "text" };
|
||||
});
|
||||
},
|
||||
} as esbuild.Plugin;
|
||||
|
||||
const packageDotJsonPlugin = {
|
||||
name: "packageDotJson",
|
||||
setup(build) {
|
||||
@@ -107,15 +120,25 @@ const packageDotJsonPlugin = {
|
||||
const inlineDepsPlugin = {
|
||||
name: "inlineDeps",
|
||||
setup(build) {
|
||||
const deps = [
|
||||
"parsimmon",
|
||||
"espresso-iisojs",
|
||||
"codemirror",
|
||||
"mithril",
|
||||
"yaml",
|
||||
];
|
||||
const deps = ["espresso-iisojs", "@codemirror", "mithril", "yaml"];
|
||||
const depFiles = new Set();
|
||||
build.onResolve({ filter: /^[^.]/ }, async (args) => {
|
||||
if (deps.some((d) => args.path.startsWith(d))) return undefined;
|
||||
if (args.pluginData === "inlineDeps") return undefined;
|
||||
if (
|
||||
depFiles.has(args.importer) ||
|
||||
deps.some((d) => args.path.startsWith(d))
|
||||
) {
|
||||
const res = await build.resolve(args.path, {
|
||||
importer: args.importer,
|
||||
namespace: args.namespace,
|
||||
resolveDir: args.resolveDir,
|
||||
kind: args.kind,
|
||||
with: args.with,
|
||||
pluginData: "inlineDeps",
|
||||
});
|
||||
depFiles.add(res.path);
|
||||
return res;
|
||||
}
|
||||
return { sideEffects: false, external: true };
|
||||
});
|
||||
},
|
||||
@@ -226,6 +249,16 @@ async function copyStatic(): Promise<void> {
|
||||
}
|
||||
|
||||
async function generateCss(): Promise<void> {
|
||||
const tailwindPlugin = {
|
||||
name: "tailwind",
|
||||
setup(build) {
|
||||
build.onLoad({ filter: /\/ui\/css\/app.css$/ }, async (args) => {
|
||||
const res = await execAsync(`npx @tailwindcss/cli -i ${args.path}`);
|
||||
return { loader: "css", contents: res.stdout };
|
||||
});
|
||||
},
|
||||
} as esbuild.Plugin;
|
||||
|
||||
const res = await esbuild.build({
|
||||
bundle: true,
|
||||
absWorkingDir: INPUT_DIR,
|
||||
@@ -235,7 +268,11 @@ async function generateCss(): Promise<void> {
|
||||
entryPoints: ["ui/css/app.css"],
|
||||
entryNames: "[dir]/[name]-[hash]",
|
||||
outfile: path.join(OUTPUT_DIR, "public/app.css"),
|
||||
target: ["chrome109", "safari15.6", "firefox115", "opera102", "edge118"],
|
||||
plugins: [tailwindPlugin],
|
||||
loader: {
|
||||
".woff2": "dataurl",
|
||||
},
|
||||
target: ["chrome111", "safari16.4", "firefox128"],
|
||||
metafile: true,
|
||||
});
|
||||
|
||||
@@ -271,7 +308,7 @@ async function generateBackendJs(): Promise<void> {
|
||||
banner: { js: "#!/usr/bin/env node" },
|
||||
entryPoints: services.map((s) => `bin/${s}.ts`),
|
||||
outdir: path.join(OUTPUT_DIR, "bin"),
|
||||
plugins: [packageDotJsonPlugin, assetsPlugin],
|
||||
plugins: [packageDotJsonPlugin, assetsPlugin, seedPlugin],
|
||||
});
|
||||
|
||||
for (const bin of services) {
|
||||
@@ -293,7 +330,7 @@ async function generateFrontendJs(): Promise<void> {
|
||||
sourcesContent: false,
|
||||
platform: "browser",
|
||||
format: "esm",
|
||||
target: ["chrome109", "safari15.6", "firefox115", "opera102", "edge118"],
|
||||
target: ["chrome111", "safari16.4", "firefox128"],
|
||||
entryPoints: ["ui/app.ts"],
|
||||
entryNames: "[dir]/[name]-[hash]",
|
||||
outdir: path.join(OUTPUT_DIR, "public"),
|
||||
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
declare -A FONTS=(
|
||||
[InterVariable]=https://rsms.me/inter/font-files/InterVariable.woff2
|
||||
[InterVariable-Italic]=https://rsms.me/inter/font-files/InterVariable-Italic.woff2
|
||||
[RobotoMono]=https://raw.githubusercontent.com/googlefonts/RobotoMono/main/fonts/variable/RobotoMono%5Bwght%5D.ttf
|
||||
)
|
||||
|
||||
for name in "${!FONTS[@]}"
|
||||
do
|
||||
url="${FONTS[$name]}"
|
||||
echo "Downloading $url"
|
||||
TMP=$(mktemp)
|
||||
curl "$url" -s --output "$TMP"
|
||||
pyftsubset "$TMP" --output-file="../ui/css/$name.woff2" --flavor=woff2 --unicodes="U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD" --layout-features+="tnum"
|
||||
done
|
||||
+2
-2
@@ -5,7 +5,7 @@ const execPromise = promisify(exec);
|
||||
|
||||
async function runEslint(): Promise<string> {
|
||||
const CMD =
|
||||
"eslint 'bin/*.ts' 'lib/**/*.ts' 'ui/**/*.ts' 'test/**/*.ts' 'build/**/*.ts'";
|
||||
"eslint 'bin/*.ts' 'lib/**/*.ts' 'ui/**/*.ts' 'test/**/*.ts' 'build/**/*.ts' 'seed/*'";
|
||||
const env = {
|
||||
...(process.stdout.isTTY && { FORCE_COLOR: "1" }),
|
||||
...process.env,
|
||||
@@ -21,7 +21,7 @@ async function runEslint(): Promise<string> {
|
||||
}
|
||||
|
||||
async function runTsc(): Promise<string> {
|
||||
const CMD = "tsc --noEmit";
|
||||
const CMD = "tsc --noEmit && tsc -p seed/tsconfig.json";
|
||||
const env = {
|
||||
...(process.stdout.isTTY && { FORCE_COLOR: "1" }),
|
||||
...process.env,
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
services:
|
||||
|
||||
# ── MongoDB ──────────────────────────────────────────────────────────────
|
||||
mongo:
|
||||
image: mongo:6
|
||||
container_name: genieacs-mongo
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- mongo_data:/data/db
|
||||
- mongo_config:/data/configdb
|
||||
networks:
|
||||
- genieacs
|
||||
healthcheck:
|
||||
test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping').ok"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 20s
|
||||
|
||||
# ── CWMP — TR-069 protocol handler (porta 7547) ──────────────────────────
|
||||
cwmp:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
target: runtime
|
||||
image: genieacs-unifi:latest
|
||||
container_name: genieacs-cwmp
|
||||
restart: unless-stopped
|
||||
command: ["node", "bin/genieacs-cwmp"]
|
||||
ports:
|
||||
- "7547:7547"
|
||||
env_file: .env
|
||||
environment:
|
||||
GENIEACS_MONGODB_CONNECTION_URL: mongodb://mongo:27017/genieacs
|
||||
depends_on:
|
||||
mongo:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ext_dir:/opt/genieacs/config/ext
|
||||
- logs:/opt/genieacs/logs
|
||||
networks:
|
||||
- genieacs
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -qO- http://localhost:7547/ || true"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 15s
|
||||
|
||||
# ── NBI — Northbound REST API (porta 7557) ───────────────────────────────
|
||||
nbi:
|
||||
image: genieacs-unifi:latest
|
||||
container_name: genieacs-nbi
|
||||
restart: unless-stopped
|
||||
command: ["node", "bin/genieacs-nbi"]
|
||||
ports:
|
||||
- "7557:7557"
|
||||
env_file: .env
|
||||
environment:
|
||||
GENIEACS_MONGODB_CONNECTION_URL: mongodb://mongo:27017/genieacs
|
||||
depends_on:
|
||||
mongo:
|
||||
condition: service_healthy
|
||||
cwmp:
|
||||
condition: service_started
|
||||
volumes:
|
||||
- ext_dir:/opt/genieacs/config/ext
|
||||
- logs:/opt/genieacs/logs
|
||||
networks:
|
||||
- genieacs
|
||||
|
||||
# ── FS — File Server / GridFS (porta 7567) ───────────────────────────────
|
||||
fs:
|
||||
image: genieacs-unifi:latest
|
||||
container_name: genieacs-fs
|
||||
restart: unless-stopped
|
||||
command: ["node", "bin/genieacs-fs"]
|
||||
ports:
|
||||
- "7567:7567"
|
||||
env_file: .env
|
||||
environment:
|
||||
GENIEACS_MONGODB_CONNECTION_URL: mongodb://mongo:27017/genieacs
|
||||
depends_on:
|
||||
mongo:
|
||||
condition: service_healthy
|
||||
cwmp:
|
||||
condition: service_started
|
||||
volumes:
|
||||
- ext_dir:/opt/genieacs/config/ext
|
||||
- logs:/opt/genieacs/logs
|
||||
networks:
|
||||
- genieacs
|
||||
|
||||
# ── UI — Web Interface (porta 3000) ──────────────────────────────────────
|
||||
ui:
|
||||
image: genieacs-unifi:latest
|
||||
container_name: genieacs-ui
|
||||
restart: unless-stopped
|
||||
command: ["node", "bin/genieacs-ui"]
|
||||
ports:
|
||||
- "3000:3000"
|
||||
env_file: .env
|
||||
environment:
|
||||
GENIEACS_MONGODB_CONNECTION_URL: mongodb://mongo:27017/genieacs
|
||||
depends_on:
|
||||
mongo:
|
||||
condition: service_healthy
|
||||
nbi:
|
||||
condition: service_started
|
||||
volumes:
|
||||
- ext_dir:/opt/genieacs/config/ext
|
||||
- logs:/opt/genieacs/logs
|
||||
networks:
|
||||
- genieacs
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -qO- http://localhost:3000/ || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 20s
|
||||
|
||||
volumes:
|
||||
mongo_data:
|
||||
driver: local
|
||||
mongo_config:
|
||||
driver: local
|
||||
ext_dir:
|
||||
driver: local
|
||||
logs:
|
||||
driver: local
|
||||
|
||||
networks:
|
||||
genieacs:
|
||||
driver: bridge
|
||||
@@ -0,0 +1,77 @@
|
||||
import { defineConfig } from "@eslint/config-helpers";
|
||||
import eslint from "@eslint/js";
|
||||
import tseslint from "typescript-eslint";
|
||||
import eslintConfigPrettier from "eslint-config-prettier";
|
||||
import globals from "globals";
|
||||
|
||||
export default defineConfig(
|
||||
{
|
||||
ignores: ["dist/", "test/*.js"],
|
||||
},
|
||||
eslint.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
eslintConfigPrettier,
|
||||
{
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.es2022,
|
||||
...globals.node,
|
||||
},
|
||||
parserOptions: {
|
||||
projectService: true,
|
||||
tsconfigRootDir: import.meta.dirname,
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
"@typescript-eslint/no-shadow": ["error", { allow: ["err"] }],
|
||||
"handle-callback-err": "error",
|
||||
"prefer-arrow-callback": "error",
|
||||
"no-buffer-constructor": "error",
|
||||
"prefer-const": ["error", { destructuring: "all" }],
|
||||
eqeqeq: ["error", "always", { null: "ignore" }],
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"@typescript-eslint/no-use-before-define": [
|
||||
"error",
|
||||
{ functions: false },
|
||||
],
|
||||
"@typescript-eslint/explicit-function-return-type": [
|
||||
"error",
|
||||
{ allowExpressions: true },
|
||||
],
|
||||
"@typescript-eslint/no-floating-promises": "error",
|
||||
"@typescript-eslint/no-misused-promises": "error",
|
||||
"no-prototype-builtins": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ["ui/**/*.ts", "ui/**/*.tsx"],
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.browser,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ["seed/*.js"],
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
project: null,
|
||||
},
|
||||
globals: {
|
||||
declare: "readonly",
|
||||
clear: "readonly",
|
||||
commit: "readonly",
|
||||
ext: "readonly",
|
||||
log: "readonly",
|
||||
args: "readonly",
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
"@typescript-eslint/explicit-function-return-type": "off",
|
||||
"@typescript-eslint/no-floating-promises": "off",
|
||||
"@typescript-eslint/no-misused-promises": "off",
|
||||
"@typescript-eslint/no-shadow": "off",
|
||||
"no-shadow": ["error", { allow: ["err", "total"] }],
|
||||
},
|
||||
},
|
||||
);
|
||||
+67
-76
@@ -31,8 +31,8 @@ import {
|
||||
udpConnectionRequest,
|
||||
xmppConnectionRequest,
|
||||
} from "./connection-request.ts";
|
||||
import { Expression, Task } from "./types.ts";
|
||||
import { evaluate } from "./common/expression/util.ts";
|
||||
import { Task } from "./types.ts";
|
||||
import Expression, { Value } from "./common/expression.ts";
|
||||
import { hashPassword } from "./auth.ts";
|
||||
import { flattenDevice } from "./ui/db.ts";
|
||||
import { ResourceLockedError } from "./common/errors.ts";
|
||||
@@ -42,7 +42,7 @@ const XMPP_CONFIGURED = !!config.get("XMPP_JID");
|
||||
|
||||
export async function connectionRequest(
|
||||
deviceId: string,
|
||||
device?: Record<string, { value?: [boolean | number | string, string] }>,
|
||||
device?: Record<string, Value>,
|
||||
): Promise<string> {
|
||||
if (!device) {
|
||||
const res = await collections.devices.findOne({ _id: deviceId });
|
||||
@@ -58,117 +58,108 @@ export async function connectionRequest(
|
||||
password;
|
||||
|
||||
if (device["InternetGatewayDevice.ManagementServer.ConnectionRequestURL"]) {
|
||||
connectionRequestUrl = (device[
|
||||
"InternetGatewayDevice.ManagementServer.ConnectionRequestURL"
|
||||
].value || [""])[0];
|
||||
udpConnectionRequestAddress = ((
|
||||
connectionRequestUrl =
|
||||
device["InternetGatewayDevice.ManagementServer.ConnectionRequestURL"] ||
|
||||
"";
|
||||
udpConnectionRequestAddress =
|
||||
device[
|
||||
"InternetGatewayDevice.ManagementServer.UDPConnectionRequestAddress"
|
||||
] || {}
|
||||
).value || [""])[0];
|
||||
stunEnable = ((
|
||||
device["InternetGatewayDevice.ManagementServer.STUNEnable"] || {}
|
||||
).value || [""])[0];
|
||||
connReqJabberId = ((
|
||||
device["InternetGatewayDevice.ManagementServer.ConnReqJabberID"] || {}
|
||||
).value || [""])[0];
|
||||
username = ((
|
||||
] || "";
|
||||
stunEnable =
|
||||
device["InternetGatewayDevice.ManagementServer.STUNEnable"] || "";
|
||||
connReqJabberId =
|
||||
device["InternetGatewayDevice.ManagementServer.ConnReqJabberID"] || "";
|
||||
username =
|
||||
device[
|
||||
"InternetGatewayDevice.ManagementServer.ConnectionRequestUsername"
|
||||
] || {}
|
||||
).value || [""])[0];
|
||||
password = ((
|
||||
] || "";
|
||||
password =
|
||||
device[
|
||||
"InternetGatewayDevice.ManagementServer.ConnectionRequestPassword"
|
||||
] || {}
|
||||
).value || [""])[0];
|
||||
] || "";
|
||||
} else {
|
||||
connectionRequestUrl = (device[
|
||||
"Device.ManagementServer.ConnectionRequestURL"
|
||||
].value || [""])[0];
|
||||
udpConnectionRequestAddress = ((
|
||||
device["Device.ManagementServer.UDPConnectionRequestAddress"] || {}
|
||||
).value || [""])[0];
|
||||
stunEnable = ((device["Device.ManagementServer.STUNEnable"] || {})
|
||||
.value || [""])[0];
|
||||
connReqJabberId = ((device["Device.ManagementServer.ConnReqJabberID"] || {})
|
||||
.value || [""])[0];
|
||||
username = ((
|
||||
device["Device.ManagementServer.ConnectionRequestUsername"] || {}
|
||||
).value || [""])[0];
|
||||
password = ((
|
||||
device["Device.ManagementServer.ConnectionRequestPassword"] || {}
|
||||
).value || [""])[0];
|
||||
connectionRequestUrl =
|
||||
device["Device.ManagementServer.ConnectionRequestURL"] || "";
|
||||
udpConnectionRequestAddress =
|
||||
device["Device.ManagementServer.UDPConnectionRequestAddress"] || "";
|
||||
stunEnable = device["Device.ManagementServer.STUNEnable"] || "";
|
||||
connReqJabberId = device["Device.ManagementServer.ConnReqJabberID"] || "";
|
||||
username =
|
||||
device["Device.ManagementServer.ConnectionRequestUsername"] || "";
|
||||
password =
|
||||
device["Device.ManagementServer.ConnectionRequestPassword"] || "";
|
||||
}
|
||||
let remoteAddress;
|
||||
try {
|
||||
remoteAddress = new URL(connectionRequestUrl).hostname;
|
||||
} catch (err) {
|
||||
} catch {
|
||||
return "Invalid connection request URL";
|
||||
}
|
||||
|
||||
const evalCallback = (exp): Expression => {
|
||||
if (!Array.isArray(exp)) return exp;
|
||||
if (exp[0] === "PARAM" && typeof exp[1] === "string") {
|
||||
let name = exp[1];
|
||||
const snapshot = await getRevision();
|
||||
const now = Date.now();
|
||||
|
||||
const evalCallback = (exp: Expression): Expression => {
|
||||
if (exp instanceof Expression.Parameter) {
|
||||
let name = exp.path.toString();
|
||||
if (name === "id") name = "DeviceID.ID";
|
||||
else if (name === "serialNumber") name = "DeviceID.SerialNumber";
|
||||
else if (name === "productClass") name = "DeviceID.ProductClass";
|
||||
else if (name === "oui") name = "DeviceID.OUI";
|
||||
else if (name === "remoteAddress") return remoteAddress;
|
||||
else if (name === "username") return username;
|
||||
else if (name === "password") return password;
|
||||
|
||||
const p = device[name];
|
||||
if (p?.value) return p.value[0];
|
||||
} else if (exp[0] === "FUNC") {
|
||||
if (exp[1] === "REMOTE_ADDRESS") return remoteAddress;
|
||||
else if (exp[1] === "USERNAME") return username;
|
||||
else if (exp[1] === "PASSWORD") return password;
|
||||
else if (name === "remoteAddress")
|
||||
return new Expression.Literal(remoteAddress);
|
||||
else if (name === "username") return new Expression.Literal(username);
|
||||
else if (name === "password") return new Expression.Literal(password);
|
||||
return new Expression.Literal(device[name] ?? null);
|
||||
} else if (exp instanceof Expression.FunctionCall) {
|
||||
if (exp.name === "NOW") return new Expression.Literal(now);
|
||||
else if (exp.name === "REMOTE_ADDRESS")
|
||||
return new Expression.Literal(remoteAddress);
|
||||
else if (exp.name === "USERNAME") return new Expression.Literal(username);
|
||||
else if (exp.name === "PASSWORD") return new Expression.Literal(password);
|
||||
}
|
||||
return exp;
|
||||
};
|
||||
|
||||
const snapshot = await getRevision();
|
||||
const now = Date.now();
|
||||
const UDP_CONNECTION_REQUEST_PORT = +getConfig(
|
||||
const configCallback = (exp: Expression): Expression.Literal => {
|
||||
const e = evalCallback(exp);
|
||||
if (e instanceof Expression.Literal) return e;
|
||||
return new Expression.Literal(null);
|
||||
};
|
||||
|
||||
const UDP_CONNECTION_REQUEST_PORT = getConfig(
|
||||
snapshot,
|
||||
"cwmp.udpConnectionRequestPort",
|
||||
{},
|
||||
now,
|
||||
evalCallback,
|
||||
0,
|
||||
configCallback,
|
||||
);
|
||||
const CONNECTION_REQUEST_TIMEOUT = +getConfig(
|
||||
const CONNECTION_REQUEST_TIMEOUT = getConfig(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestTimeout",
|
||||
{},
|
||||
now,
|
||||
evalCallback,
|
||||
2000,
|
||||
configCallback,
|
||||
);
|
||||
const CONNECTION_REQUEST_ALLOW_BASIC_AUTH = !!getConfig(
|
||||
const CONNECTION_REQUEST_ALLOW_BASIC_AUTH = getConfig(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestAllowBasicAuth",
|
||||
{},
|
||||
now,
|
||||
evalCallback,
|
||||
false,
|
||||
configCallback,
|
||||
);
|
||||
let authExp: Expression = getConfigExpression(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestAuth",
|
||||
);
|
||||
|
||||
if (authExp === undefined) {
|
||||
authExp = [
|
||||
"FUNC",
|
||||
"AUTH",
|
||||
["PARAM", "username"],
|
||||
["PARAM", "password"],
|
||||
] as Expression;
|
||||
if (!authExp) {
|
||||
authExp = new Expression.FunctionCall("AUTH", [
|
||||
new Expression.FunctionCall("USERNAME", []),
|
||||
new Expression.FunctionCall("PASSWORD", []),
|
||||
]);
|
||||
}
|
||||
|
||||
authExp = evaluate(authExp, {}, now, evalCallback);
|
||||
authExp = authExp.evaluate(evalCallback);
|
||||
|
||||
const debug = !!getConfig(snapshot, "cwmp.debug", {}, now, evalCallback);
|
||||
const debug = getConfig(snapshot, "cwmp.debug", false, configCallback);
|
||||
|
||||
let udpProm = Promise.resolve(false);
|
||||
if (udpConnectionRequestAddress && +stunEnable) {
|
||||
@@ -185,7 +176,7 @@ export async function connectionRequest(
|
||||
() => true,
|
||||
() => false,
|
||||
);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Ignore invalid address
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ function parseHeaderFeilds(str: string): Record<string, string> {
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (error) {
|
||||
throw new Error("Unable to parse auth header");
|
||||
throw new Error("Unable to parse auth header", { cause: error });
|
||||
}
|
||||
}
|
||||
res[name.trim()] = value;
|
||||
|
||||
+27
-27
@@ -1,14 +1,14 @@
|
||||
import { PermissionSet, Expression } from "../types.ts";
|
||||
import { evaluate, or } from "./expression/util.ts";
|
||||
import { PermissionSet } from "../types.ts";
|
||||
import Expression from "./expression.ts";
|
||||
|
||||
export default class Authorizer {
|
||||
private declare permissionSets: PermissionSet[];
|
||||
private declare validatorCache: WeakMap<
|
||||
declare private permissionSets: PermissionSet[];
|
||||
declare private validatorCache: WeakMap<
|
||||
any,
|
||||
(mutationType, mutation, any) => boolean
|
||||
>;
|
||||
private declare hasAccessCache: Map<string, boolean>;
|
||||
private declare getFilterCache: Map<string, Expression>;
|
||||
declare private hasAccessCache: Map<string, boolean>;
|
||||
declare private getFilterCache: Map<string, Expression>;
|
||||
|
||||
public constructor(permissionSets: PermissionSet[]) {
|
||||
this.permissionSets = permissionSets;
|
||||
@@ -43,12 +43,12 @@ export default class Authorizer {
|
||||
if (this.getFilterCache.has(cacheKey))
|
||||
return this.getFilterCache.get(cacheKey);
|
||||
|
||||
let filter: Expression = null;
|
||||
let filter: Expression = new Expression.Literal(false);
|
||||
for (const permissionSet of this.permissionSets) {
|
||||
for (const perm of permissionSet) {
|
||||
if (perm[resourceType]) {
|
||||
if (perm[resourceType].access >= access)
|
||||
filter = or(filter, perm[resourceType].filter);
|
||||
filter = Expression.or(filter, perm[resourceType].filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ export default class Authorizer {
|
||||
if (this.validatorCache.has(resource))
|
||||
return this.validatorCache.get(resource);
|
||||
|
||||
const validators: Expression[] = [];
|
||||
let validators: Expression = new Expression.Literal(false);
|
||||
|
||||
for (const permissionSet of this.permissionSets) {
|
||||
for (const perm of permissionSet) {
|
||||
@@ -73,7 +73,7 @@ export default class Authorizer {
|
||||
perm[resourceType].access >= 3 &&
|
||||
perm[resourceType].validate
|
||||
)
|
||||
validators.push(perm[resourceType].validate);
|
||||
validators = Expression.or(validators, perm[resourceType].validate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +82,6 @@ export default class Authorizer {
|
||||
mutation: any,
|
||||
any: any,
|
||||
): boolean => {
|
||||
if (!validators.length) return false;
|
||||
|
||||
const object = {
|
||||
mutationType,
|
||||
mutation,
|
||||
@@ -92,31 +90,33 @@ export default class Authorizer {
|
||||
options: any,
|
||||
};
|
||||
|
||||
const valueFunction = (paramName): any => {
|
||||
const entry = paramName.split(".", 1)[0];
|
||||
paramName = paramName.slice(entry.length + 1);
|
||||
const now = Date.now();
|
||||
const res = validators.evaluate((exp) => {
|
||||
if (exp instanceof Expression.Literal) return exp;
|
||||
if (exp instanceof Expression.Parameter) {
|
||||
if (exp.path.colon) return new Expression.Literal(null);
|
||||
const entry = exp.path.segments[0] as string;
|
||||
const paramName = exp.path.slice(1);
|
||||
let value = null;
|
||||
if (["mutation", "options"].includes(entry)) {
|
||||
value = object[entry];
|
||||
for (const seg of paramName.split(".")) {
|
||||
for (const seg of paramName.segments) {
|
||||
if (value == null) break;
|
||||
if (typeof value !== "object") value = null;
|
||||
else value = value[seg];
|
||||
else value = value[seg as string];
|
||||
}
|
||||
} else if (object[entry]) {
|
||||
if (paramName) value = object[entry][paramName];
|
||||
if (paramName.length) value = object[entry][paramName.toString()];
|
||||
else value = object[entry];
|
||||
}
|
||||
return new Expression.Literal(value);
|
||||
} else if (exp instanceof Expression.FunctionCall) {
|
||||
if (exp.name === "NOW") return new Expression.Literal(now);
|
||||
}
|
||||
return new Expression.Literal(null);
|
||||
}).value;
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
const res = evaluate(
|
||||
validators.length > 1 ? ["OR", validators] : validators[0],
|
||||
valueFunction,
|
||||
Date.now(),
|
||||
);
|
||||
return !Array.isArray(res) && !!res;
|
||||
return !!res;
|
||||
};
|
||||
|
||||
this.validatorCache.set(resource, validator);
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
import {
|
||||
Cursor,
|
||||
parseExpression,
|
||||
stringifyExpression,
|
||||
} from "./expression/parser.ts";
|
||||
import Path from "./path.ts";
|
||||
import { reduce } from "./expression/evaluate.ts";
|
||||
|
||||
export type Value = string | number | boolean | null;
|
||||
|
||||
export abstract class Expression {
|
||||
private _string: string;
|
||||
|
||||
abstract map(fn: (e: Expression, i: number) => Expression): Expression;
|
||||
abstract mapAsync(
|
||||
fn: (e: Expression, i: number) => Promise<Expression>,
|
||||
): Promise<Expression>;
|
||||
|
||||
toString(): string {
|
||||
if (!this._string) this._string = stringifyExpression(this);
|
||||
return this._string;
|
||||
}
|
||||
|
||||
evaluate<T extends Expression>(fn: (e: Expression) => T = (e) => e as T): T {
|
||||
return fn(reduce(this.map((e) => e.evaluate(fn))));
|
||||
}
|
||||
|
||||
async evaluateAsync<T extends Expression>(
|
||||
fn: (e: Expression) => Promise<T>,
|
||||
): Promise<T> {
|
||||
return await fn(
|
||||
reduce(await this.mapAsync(async (e) => await e.evaluateAsync(fn))),
|
||||
);
|
||||
}
|
||||
|
||||
static parse(input: string): Expression {
|
||||
const cursor = new Cursor(input);
|
||||
const exp = parseExpression(cursor);
|
||||
if (cursor.charCode) throw new Error("Unexpected character");
|
||||
return exp;
|
||||
}
|
||||
|
||||
static and(left: Expression, right: Expression): Expression {
|
||||
// Flatten same-operator tree into operands
|
||||
const operands: Expression[] = [];
|
||||
const stack: Expression[] = [right, left];
|
||||
while (stack.length) {
|
||||
const e = stack.pop()!;
|
||||
if (e instanceof Expression.Binary && e.operator === "AND")
|
||||
stack.push(e.right, e.left);
|
||||
else operands.push(e);
|
||||
}
|
||||
|
||||
// Fold literals using three-valued AND logic
|
||||
let folded: boolean | null = true;
|
||||
let i = 0;
|
||||
while (i < operands.length) {
|
||||
const e = operands[i];
|
||||
if (e instanceof Expression.Literal) {
|
||||
operands.splice(i, 1);
|
||||
if (e.value == null) folded = folded === false ? false : null;
|
||||
else if (!e.value) return new Expression.Literal(false);
|
||||
// truthy is identity for AND; discard
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild: folded value + remaining non-literal operands
|
||||
if (!operands.length) return new Expression.Literal(folded);
|
||||
let result = operands.reduce((a, b) => new Expression.Binary("AND", a, b));
|
||||
if (folded === null)
|
||||
result = new Expression.Binary(
|
||||
"AND",
|
||||
new Expression.Literal(null),
|
||||
result,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
static or(left: Expression, right: Expression): Expression {
|
||||
// Flatten same-operator tree into operands
|
||||
const operands: Expression[] = [];
|
||||
const stack: Expression[] = [right, left];
|
||||
while (stack.length) {
|
||||
const e = stack.pop()!;
|
||||
if (e instanceof Expression.Binary && e.operator === "OR")
|
||||
stack.push(e.right, e.left);
|
||||
else operands.push(e);
|
||||
}
|
||||
|
||||
// Fold literals using three-valued OR logic
|
||||
let folded: boolean | null = false;
|
||||
let i = 0;
|
||||
while (i < operands.length) {
|
||||
const e = operands[i];
|
||||
if (e instanceof Expression.Literal) {
|
||||
operands.splice(i, 1);
|
||||
if (e.value == null) folded = folded === true ? true : null;
|
||||
else if (e.value) return new Expression.Literal(true);
|
||||
// falsy is identity for OR; discard
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild: folded value + remaining non-literal operands
|
||||
if (!operands.length) return new Expression.Literal(folded);
|
||||
let result = operands.reduce((a, b) => new Expression.Binary("OR", a, b));
|
||||
if (folded === null)
|
||||
result = new Expression.Binary(
|
||||
"OR",
|
||||
new Expression.Literal(null),
|
||||
result,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||
export namespace Expression {
|
||||
export class Literal extends Expression {
|
||||
constructor(public readonly value: Value) {
|
||||
super();
|
||||
}
|
||||
|
||||
map(): Literal {
|
||||
return this;
|
||||
}
|
||||
|
||||
async mapAsync(): Promise<Literal> {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class Parameter extends Expression {
|
||||
constructor(public readonly path: Path) {
|
||||
super();
|
||||
}
|
||||
|
||||
map(): Parameter {
|
||||
return this;
|
||||
}
|
||||
|
||||
async mapAsync(): Promise<Parameter> {
|
||||
return this;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.path.toString();
|
||||
}
|
||||
}
|
||||
|
||||
export class Unary extends Expression {
|
||||
constructor(
|
||||
public readonly operator: string,
|
||||
public readonly operand: Expression,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
map(fn: (e: Expression, i: number) => Expression): Unary {
|
||||
const operand = fn(this.operand, 0);
|
||||
if (operand === this.operand) return this;
|
||||
return Reflect.construct(this.constructor, [this.operator, operand]);
|
||||
}
|
||||
|
||||
async mapAsync(
|
||||
fn: (e: Expression, i: number) => Promise<Expression>,
|
||||
): Promise<Unary> {
|
||||
const operand = await fn(this.operand, 0);
|
||||
if (operand === this.operand) return this;
|
||||
return Reflect.construct(this.constructor, [this.operator, operand]);
|
||||
}
|
||||
}
|
||||
|
||||
export class Binary extends Expression {
|
||||
constructor(
|
||||
public readonly operator: string,
|
||||
public readonly left: Expression,
|
||||
public readonly right: Expression,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
map(fn: (e: Expression, i: number) => Expression): Binary {
|
||||
const left = fn(this.left, 0);
|
||||
const right = fn(this.right, 1);
|
||||
if (left === this.left && right === this.right) return this;
|
||||
return Reflect.construct(this.constructor, [this.operator, left, right]);
|
||||
}
|
||||
|
||||
async mapAsync(
|
||||
fn: (e: Expression, i: number) => Promise<Expression>,
|
||||
): Promise<Binary> {
|
||||
const left = await fn(this.left, 0);
|
||||
const right = await fn(this.right, 1);
|
||||
if (left === this.left && right === this.right) return this;
|
||||
return Reflect.construct(this.constructor, [this.operator, left, right]);
|
||||
}
|
||||
}
|
||||
|
||||
export class FunctionCall extends Expression {
|
||||
constructor(
|
||||
public readonly name: string,
|
||||
public readonly args: Expression[],
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
map(fn: (e: Expression, i: number) => Expression): FunctionCall {
|
||||
const args = this.args.map(fn);
|
||||
if (args.every((arg, i) => arg === this.args[i])) return this;
|
||||
return new FunctionCall(this.name, args);
|
||||
}
|
||||
|
||||
async mapAsync(
|
||||
fn: (e: Expression, i: number) => Promise<Expression>,
|
||||
): Promise<FunctionCall> {
|
||||
const args = await Promise.all(this.args.map(fn));
|
||||
if (args.every((arg, i) => arg === this.args[i])) return this;
|
||||
return new FunctionCall(this.name, args);
|
||||
}
|
||||
}
|
||||
|
||||
export class Conditional extends Expression {
|
||||
constructor(
|
||||
public readonly condition: Expression,
|
||||
public readonly then: Expression,
|
||||
public readonly otherwise: Expression,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
map(fn: (e: Expression, i: number) => Expression): Conditional {
|
||||
const condition = fn(this.condition, 0);
|
||||
const then = fn(this.then, 1);
|
||||
const otherwise = fn(this.otherwise, 2);
|
||||
if (
|
||||
condition === this.condition &&
|
||||
then === this.then &&
|
||||
otherwise === this.otherwise
|
||||
)
|
||||
return this;
|
||||
return new Conditional(condition, then, otherwise);
|
||||
}
|
||||
|
||||
async mapAsync(
|
||||
fn: (e: Expression, i: number) => Promise<Expression>,
|
||||
): Promise<Conditional> {
|
||||
const condition = await fn(this.condition, 0);
|
||||
const then = await fn(this.then, 1);
|
||||
const otherwise = await fn(this.otherwise, 2);
|
||||
if (
|
||||
condition === this.condition &&
|
||||
then === this.then &&
|
||||
otherwise === this.otherwise
|
||||
)
|
||||
return this;
|
||||
return new Conditional(condition, then, otherwise);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function extractPaths(exp: Expression): Path[] {
|
||||
if (exp instanceof Expression.Parameter) return [exp.path];
|
||||
const paths: Path[] = [];
|
||||
exp.map((e) => {
|
||||
if (e instanceof Expression.Parameter) paths.push(e.path);
|
||||
else paths.push(...extractPaths(e));
|
||||
return e;
|
||||
});
|
||||
return paths;
|
||||
}
|
||||
|
||||
export function parseList(input: string): Expression[] {
|
||||
const CHAR_COMMA = 44;
|
||||
const res: Expression[] = [];
|
||||
const cursor = new Cursor(input);
|
||||
cursor.skipwhitespace();
|
||||
if (!cursor.charCode) return res;
|
||||
res.push(parseExpression(cursor));
|
||||
while (cursor.charCode === CHAR_COMMA) {
|
||||
cursor.step();
|
||||
res.push(parseExpression(cursor));
|
||||
}
|
||||
if (cursor.charCode) throw new Error("Unexpected character");
|
||||
return res;
|
||||
}
|
||||
|
||||
export default Expression;
|
||||
@@ -1,87 +0,0 @@
|
||||
export function and(a: bigint, b: bigint): bigint {
|
||||
return a & b;
|
||||
}
|
||||
|
||||
export function or(a: bigint, b: bigint): bigint {
|
||||
return a | b;
|
||||
}
|
||||
|
||||
export function xor(a: bigint, b: bigint): bigint {
|
||||
return a ^ b;
|
||||
}
|
||||
|
||||
export function not(a: bigint): bigint {
|
||||
return ~a;
|
||||
}
|
||||
|
||||
export function lshift(a: bigint, b: bigint): bigint {
|
||||
return a << b;
|
||||
}
|
||||
|
||||
export function rshift(a: bigint, b: bigint): bigint {
|
||||
return a >> b;
|
||||
}
|
||||
|
||||
export function add(a: bigint, b: bigint): bigint {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
export function sub(a: bigint, b: bigint): bigint {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
export function mul(a: bigint, b: bigint): bigint {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
export function div(a: bigint, b: bigint): bigint {
|
||||
return a / b;
|
||||
}
|
||||
|
||||
export function exp(a: bigint, b: bigint): bigint {
|
||||
return a ** b;
|
||||
}
|
||||
|
||||
export function rem(a: bigint, b: bigint): bigint {
|
||||
return a % b;
|
||||
}
|
||||
|
||||
export function toNumber(a: bigint): number {
|
||||
return Number(a);
|
||||
}
|
||||
|
||||
export function eq(a: bigint, b: bigint): boolean {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
export function ne(a: bigint, b: bigint): boolean {
|
||||
return a !== b;
|
||||
}
|
||||
|
||||
export function lt(a: bigint, b: bigint): boolean {
|
||||
return a < b;
|
||||
}
|
||||
|
||||
export function lte(a: bigint, b: bigint): boolean {
|
||||
return a <= b;
|
||||
}
|
||||
|
||||
export function gt(a: bigint, b: bigint): boolean {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
export function gte(a: bigint, b: bigint): boolean {
|
||||
return a >= b;
|
||||
}
|
||||
|
||||
export function asUintN(a: number, b: bigint): bigint {
|
||||
return BigInt.asUintN(a, b);
|
||||
}
|
||||
|
||||
export function asIntN(a: number, b: bigint): bigint {
|
||||
return BigInt.asIntN(a, b);
|
||||
}
|
||||
|
||||
const _BigInt = BigInt;
|
||||
type _bigint = bigint;
|
||||
export { _BigInt as BigInt, type _bigint as bigint };
|
||||
@@ -0,0 +1,218 @@
|
||||
import Expression from "../expression.ts";
|
||||
import { likePatternToRegExp } from "./parser.ts";
|
||||
|
||||
function compare(
|
||||
a: boolean | number | string,
|
||||
b: boolean | number | string,
|
||||
): number {
|
||||
if (typeof a === "boolean") a = +a;
|
||||
if (typeof b === "boolean") b = +b;
|
||||
if (typeof a !== typeof b) return typeof a === "string" ? 1 : -1;
|
||||
return a > b ? 1 : a < b ? -1 : 0;
|
||||
}
|
||||
|
||||
function toNumber(a: boolean | number | string): number {
|
||||
switch (typeof a) {
|
||||
case "number":
|
||||
return a;
|
||||
case "boolean":
|
||||
return +a;
|
||||
case "string":
|
||||
return parseFloat(a) || 0;
|
||||
}
|
||||
}
|
||||
|
||||
function toString(a: boolean | number | string): string {
|
||||
switch (typeof a) {
|
||||
case "string":
|
||||
return a;
|
||||
case "number":
|
||||
return a.toString();
|
||||
case "boolean":
|
||||
return (+a).toString();
|
||||
}
|
||||
}
|
||||
|
||||
const regExpCache: WeakMap<Expression.Literal, RegExp> = new WeakMap();
|
||||
|
||||
export function reduce(exp: Expression): Expression {
|
||||
if (exp instanceof Expression.Literal) return exp;
|
||||
|
||||
if (exp instanceof Expression.Unary) {
|
||||
if (exp.operator === "NOT") {
|
||||
if (exp.operand instanceof Expression.Literal) {
|
||||
if (exp.operand.value == null) return exp.operand;
|
||||
return new Expression.Literal(!exp.operand.value);
|
||||
} else if (exp.operand instanceof Expression.Unary) {
|
||||
if (exp.operand.operator === "NOT") return exp.operand.operand;
|
||||
}
|
||||
} else if (exp.operator === "IS NULL") {
|
||||
if (exp.operand instanceof Expression.Literal) {
|
||||
return new Expression.Literal(exp.operand.value == null);
|
||||
}
|
||||
} else if (exp.operator === "IS NOT NULL") {
|
||||
if (exp.operand instanceof Expression.Literal) {
|
||||
return new Expression.Literal(exp.operand.value != null);
|
||||
}
|
||||
}
|
||||
} else if (exp instanceof Expression.Binary) {
|
||||
if (exp.operator === "AND") {
|
||||
return Expression.and(exp.left, exp.right);
|
||||
} else if (exp.operator === "OR") {
|
||||
return Expression.or(exp.left, exp.right);
|
||||
} else if (["=", ">", "<", "<>", ">=", "<="].includes(exp.operator)) {
|
||||
if (exp.left instanceof Expression.Literal && exp.left.value == null)
|
||||
return exp.left;
|
||||
if (exp.right instanceof Expression.Literal && exp.right.value == null)
|
||||
return exp.right;
|
||||
if (
|
||||
exp.left instanceof Expression.Literal &&
|
||||
exp.right instanceof Expression.Literal
|
||||
) {
|
||||
const c = compare(exp.left.value, exp.right.value);
|
||||
switch (exp.operator) {
|
||||
case "=":
|
||||
return new Expression.Literal(c === 0);
|
||||
case ">":
|
||||
return new Expression.Literal(c > 0);
|
||||
case "<":
|
||||
return new Expression.Literal(c < 0);
|
||||
case "<>":
|
||||
return new Expression.Literal(c !== 0);
|
||||
case ">=":
|
||||
return new Expression.Literal(c >= 0);
|
||||
case "<=":
|
||||
return new Expression.Literal(c <= 0);
|
||||
}
|
||||
}
|
||||
} else if (["+", "-", "*", "/"].includes(exp.operator)) {
|
||||
if (exp.left instanceof Expression.Literal && exp.left.value == null)
|
||||
return exp.left;
|
||||
if (exp.right instanceof Expression.Literal && exp.right.value == null)
|
||||
return exp.right;
|
||||
if (
|
||||
exp.left instanceof Expression.Literal &&
|
||||
exp.right instanceof Expression.Literal
|
||||
) {
|
||||
const a = toNumber(exp.left.value);
|
||||
const b = toNumber(exp.right.value);
|
||||
switch (exp.operator) {
|
||||
case "+":
|
||||
return new Expression.Literal(a + b);
|
||||
case "-":
|
||||
return new Expression.Literal(a - b);
|
||||
case "*":
|
||||
return new Expression.Literal(a * b);
|
||||
case "/":
|
||||
return new Expression.Literal(a / b);
|
||||
}
|
||||
}
|
||||
} else if (exp.operator === "%") {
|
||||
if (exp.left instanceof Expression.Literal && exp.left.value == null)
|
||||
return exp.left;
|
||||
if (exp.right instanceof Expression.Literal && exp.right.value == null)
|
||||
return exp.right;
|
||||
if (
|
||||
exp.left instanceof Expression.Literal &&
|
||||
exp.right instanceof Expression.Literal
|
||||
) {
|
||||
const a = toNumber(exp.left.value);
|
||||
const b = Math.trunc(toNumber(exp.right.value));
|
||||
if (b === 0) return new Expression.Literal(null);
|
||||
return new Expression.Literal(a % b);
|
||||
}
|
||||
} else if (exp.operator === "||") {
|
||||
if (exp.left instanceof Expression.Literal && exp.left.value == null)
|
||||
return exp.left;
|
||||
if (exp.right instanceof Expression.Literal && exp.right.value == null)
|
||||
return exp.right;
|
||||
if (
|
||||
exp.left instanceof Expression.Literal &&
|
||||
exp.right instanceof Expression.Literal
|
||||
) {
|
||||
const a = toString(exp.left.value);
|
||||
const b = toString(exp.right.value);
|
||||
return new Expression.Literal(a + b);
|
||||
}
|
||||
} else if (exp.operator === "LIKE") {
|
||||
if (exp.left instanceof Expression.Literal && exp.left.value == null)
|
||||
return exp.left;
|
||||
if (exp.right instanceof Expression.Literal && exp.right.value == null)
|
||||
return exp.right;
|
||||
if (
|
||||
exp.left instanceof Expression.Literal &&
|
||||
exp.right instanceof Expression.Literal
|
||||
) {
|
||||
const s = toString(exp.left.value);
|
||||
let r = regExpCache.get(exp.right);
|
||||
if (!r) {
|
||||
r = likePatternToRegExp(toString(exp.right.value));
|
||||
regExpCache.set(exp.right, r);
|
||||
}
|
||||
return new Expression.Literal(r.test(s));
|
||||
}
|
||||
} else if (exp.operator === "NOT LIKE") {
|
||||
if (exp.left instanceof Expression.Literal && exp.left.value == null)
|
||||
return exp.left;
|
||||
if (exp.right instanceof Expression.Literal && exp.right.value == null)
|
||||
return exp.right;
|
||||
if (
|
||||
exp.left instanceof Expression.Literal &&
|
||||
exp.right instanceof Expression.Literal
|
||||
) {
|
||||
const s = toString(exp.left.value);
|
||||
let r = regExpCache.get(exp.right);
|
||||
if (!r) {
|
||||
r = likePatternToRegExp(toString(exp.right.value));
|
||||
regExpCache.set(exp.right, r);
|
||||
}
|
||||
return new Expression.Literal(!r.test(s));
|
||||
}
|
||||
}
|
||||
} else if (exp instanceof Expression.FunctionCall) {
|
||||
if (exp.name === "COALESCE") {
|
||||
const args = exp.args.filter(
|
||||
(arg) => !(arg instanceof Expression.Literal && arg.value == null),
|
||||
);
|
||||
if (!args.length) return new Expression.Literal(null);
|
||||
if (args.length === 1) return args[0];
|
||||
if (args[0] instanceof Expression.Literal) return args[0];
|
||||
if (args.length !== exp.args.length)
|
||||
return new Expression.FunctionCall("COALESCE", args);
|
||||
} else if (exp.name === "UPPER") {
|
||||
if (exp.args[0] instanceof Expression.Literal) {
|
||||
if (exp.args[0].value == null) return exp.args[0];
|
||||
const a = toString(exp.args[0].value);
|
||||
return new Expression.Literal(a.toUpperCase());
|
||||
}
|
||||
} else if (exp.name === "LOWER") {
|
||||
if (exp.args[0] instanceof Expression.Literal) {
|
||||
if (exp.args[0].value == null) return exp.args[0];
|
||||
const a = toString(exp.args[0].value);
|
||||
return new Expression.Literal(a.toLowerCase());
|
||||
}
|
||||
} else if (exp.name === "ROUND") {
|
||||
let p = 0;
|
||||
if (exp.args.length > 1) {
|
||||
if (exp.args[1] instanceof Expression.Literal) {
|
||||
if (exp.args[1].value == null) return exp.args[1];
|
||||
p = Math.trunc(toNumber(exp.args[1].value));
|
||||
}
|
||||
}
|
||||
if (exp.args[0] instanceof Expression.Literal) {
|
||||
if (exp.args[0].value == null) return exp.args[0];
|
||||
const n = toNumber(exp.args[0].value);
|
||||
const d = 10 ** p;
|
||||
const m = n * d * (1 + Number.EPSILON);
|
||||
return new Expression.Literal(Math.round(m) / d);
|
||||
}
|
||||
}
|
||||
} else if (exp instanceof Expression.Conditional) {
|
||||
if (exp.condition instanceof Expression.Literal) {
|
||||
if (exp.condition.value) return exp.then;
|
||||
return exp.otherwise;
|
||||
}
|
||||
}
|
||||
|
||||
return exp;
|
||||
}
|
||||
+224
-226
@@ -1,22 +1,15 @@
|
||||
import * as BI from "./bigint.ts";
|
||||
import { Expression } from "../../types.ts";
|
||||
import { map } from "./parser.ts";
|
||||
import { and, evaluateCallback } from "./util.ts";
|
||||
|
||||
const ZERO = BI.BigInt(0);
|
||||
const ONE = BI.BigInt(1);
|
||||
const TWO = BI.BigInt(2);
|
||||
const NEGATIVE_ONE = BI.BigInt(-1);
|
||||
import Expression from "../expression.ts";
|
||||
import { reduce } from "./evaluate.ts";
|
||||
|
||||
class Indeterminates {
|
||||
public declare map: Map<string, number>;
|
||||
public declare sortedKeys: string[];
|
||||
declare public map: Map<Expression, number>;
|
||||
declare public sortedKeys: Expression[];
|
||||
|
||||
public constructor(str?: string) {
|
||||
public constructor(exp?: Expression) {
|
||||
this.map = new Map();
|
||||
if (str) {
|
||||
this.map.set(str, 1);
|
||||
this.sortedKeys = [str];
|
||||
if (exp) {
|
||||
this.map.set(exp, 1);
|
||||
this.sortedKeys = [exp];
|
||||
} else {
|
||||
this.sortedKeys = [];
|
||||
}
|
||||
@@ -37,27 +30,31 @@ class Indeterminates {
|
||||
const res = new Indeterminates();
|
||||
res.sortedKeys = indeterminates1.sortedKeys.slice();
|
||||
res.map = new Map(indeterminates1.map);
|
||||
const strMap: Map<string, Expression> = new Map();
|
||||
for (const k of res.map.keys()) strMap.set(k.toString(), k);
|
||||
|
||||
for (const [key, val] of indeterminates2.map) {
|
||||
const v = res.map.get(key);
|
||||
if (!v) {
|
||||
const k = strMap.get(key.toString());
|
||||
if (!k) {
|
||||
res.map.set(key, val);
|
||||
res.sortedKeys.push(key);
|
||||
} else {
|
||||
const v2 = val + v;
|
||||
const v2 = val + res.map.get(k);
|
||||
if (!v2) {
|
||||
res.map.delete(key);
|
||||
res.sortedKeys = res.sortedKeys.filter((s) => s !== key);
|
||||
res.map.delete(k);
|
||||
res.sortedKeys = res.sortedKeys.filter((s) => s !== k);
|
||||
} else {
|
||||
res.map.set(key, v2);
|
||||
res.map.set(k, v2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.sortedKeys.sort((a, b) => {
|
||||
if (a.length !== b.length) return b.length - a.length;
|
||||
else if (a > b) return 1;
|
||||
else if (a < b) return -1;
|
||||
const str1 = a.toString();
|
||||
const str2 = b.toString();
|
||||
if (str1.length !== str2.length) return str2.length - str1.length;
|
||||
else if (str1 > str2) return 1;
|
||||
else if (str1 < str2) return -1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
@@ -73,10 +70,10 @@ class Indeterminates {
|
||||
const k2 = b.sortedKeys[i];
|
||||
const w2 = b.map.get(k2);
|
||||
if (w1 !== w2) return w2 - w1;
|
||||
if (k1.length > k2.length) return -1;
|
||||
else if (k1.length < k2.length) return 1;
|
||||
else if (k1 > k2) return 1;
|
||||
else if (k1 < k2) return -1;
|
||||
if (k1.toString().length > k2.toString().length) return -1;
|
||||
else if (k1.toString().length < k2.toString().length) return 1;
|
||||
else if (k1.toString() > k2.toString()) return 1;
|
||||
else if (k1.toString() < k2.toString()) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -84,26 +81,35 @@ class Indeterminates {
|
||||
|
||||
interface Term {
|
||||
indeterminates: Indeterminates;
|
||||
coefficientNumerator: BI.bigint;
|
||||
coefficientDenominator: BI.bigint;
|
||||
coefficientNumerator: bigint;
|
||||
coefficientDenominator: bigint;
|
||||
}
|
||||
|
||||
function findGcd(a: BI.bigint, b: BI.bigint): BI.bigint {
|
||||
while (BI.ne(b, ZERO)) {
|
||||
function findGcd(a: bigint, b: bigint): bigint {
|
||||
while (b !== 0n) {
|
||||
const t = b;
|
||||
b = BI.rem(a, b);
|
||||
b = a % b;
|
||||
a = t;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
class Polynomial {
|
||||
public declare terms: Term[];
|
||||
class Polynomial extends Expression {
|
||||
declare public terms: Term[];
|
||||
|
||||
public constructor(terms: Term[]) {
|
||||
super();
|
||||
this.terms = terms;
|
||||
}
|
||||
|
||||
map(): Polynomial {
|
||||
return this;
|
||||
}
|
||||
|
||||
async mapAsync(): Promise<Polynomial> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static simplifyTerms(terms: Term[]): Term[] {
|
||||
const ts = terms
|
||||
.slice()
|
||||
@@ -115,40 +121,37 @@ class Polynomial {
|
||||
const t1 = ts[i - 1];
|
||||
const t2 = ts[i];
|
||||
if (Indeterminates.compare(t1.indeterminates, t2.indeterminates) === 0) {
|
||||
const numerator = BI.add(
|
||||
BI.mul(t1.coefficientNumerator, t2.coefficientDenominator),
|
||||
BI.mul(t2.coefficientNumerator, t1.coefficientDenominator),
|
||||
);
|
||||
const numerator =
|
||||
t1.coefficientNumerator * t2.coefficientDenominator +
|
||||
t2.coefficientNumerator * t1.coefficientDenominator;
|
||||
|
||||
const denominator = BI.mul(
|
||||
t1.coefficientDenominator,
|
||||
t2.coefficientDenominator,
|
||||
);
|
||||
const denominator =
|
||||
t1.coefficientDenominator * t2.coefficientDenominator;
|
||||
|
||||
const gcd = findGcd(numerator, denominator);
|
||||
|
||||
ts[i] = {
|
||||
indeterminates: t2.indeterminates,
|
||||
coefficientNumerator: BI.div(numerator, gcd),
|
||||
coefficientDenominator: BI.div(denominator, gcd),
|
||||
coefficientNumerator: numerator / gcd,
|
||||
coefficientDenominator: denominator / gcd,
|
||||
};
|
||||
ts[i - 1] = {
|
||||
indeterminates: t1.indeterminates,
|
||||
coefficientNumerator: ZERO,
|
||||
coefficientNumerator: 0n,
|
||||
coefficientDenominator: t1.coefficientDenominator,
|
||||
};
|
||||
}
|
||||
}
|
||||
return ts.filter((v) => BI.ne(v.coefficientNumerator, ZERO));
|
||||
return ts.filter((v) => v.coefficientNumerator !== 0n);
|
||||
}
|
||||
|
||||
public static fromIndeterminate(indeterminate: Expression): Polynomial {
|
||||
const indeterminates = new Indeterminates(JSON.stringify(indeterminate));
|
||||
const indeterminates = new Indeterminates(indeterminate);
|
||||
const terms = [
|
||||
{
|
||||
indeterminates: indeterminates,
|
||||
coefficientNumerator: ONE,
|
||||
coefficientDenominator: ONE,
|
||||
coefficientNumerator: 1n,
|
||||
coefficientDenominator: 1n,
|
||||
},
|
||||
];
|
||||
return new Polynomial(terms);
|
||||
@@ -156,15 +159,12 @@ class Polynomial {
|
||||
|
||||
public static fromConstant(constant: number): Polynomial {
|
||||
const [int, frac] = Math.abs(constant).toString(2).split(".", 2);
|
||||
let numerator = BI.BigInt("0b" + int);
|
||||
if (constant < 0) numerator = BI.mul(numerator, NEGATIVE_ONE);
|
||||
let denominator = ONE;
|
||||
let numerator = BigInt("0b" + int);
|
||||
if (constant < 0) numerator = numerator / -1n;
|
||||
let denominator = 1n;
|
||||
if (frac) {
|
||||
denominator = BI.exp(TWO, BI.BigInt(frac.length));
|
||||
numerator = BI.add(
|
||||
BI.mul(numerator, denominator),
|
||||
BI.BigInt("0b" + frac),
|
||||
);
|
||||
denominator = 2n ** BigInt(frac.length);
|
||||
numerator = numerator * denominator + BigInt("0b" + frac);
|
||||
}
|
||||
|
||||
const terms = [
|
||||
@@ -181,7 +181,7 @@ class Polynomial {
|
||||
public negation(): Polynomial {
|
||||
const terms = this.terms.map((t) => ({
|
||||
indeterminates: t.indeterminates,
|
||||
coefficientNumerator: BI.mul(t.coefficientNumerator, NEGATIVE_ONE),
|
||||
coefficientNumerator: t.coefficientNumerator * -1n,
|
||||
coefficientDenominator: t.coefficientDenominator,
|
||||
}));
|
||||
|
||||
@@ -217,14 +217,9 @@ class Polynomial {
|
||||
|
||||
for (const t1 of this.terms) {
|
||||
for (const t2 of rhs.terms) {
|
||||
const numerator = BI.mul(
|
||||
t1.coefficientNumerator,
|
||||
t2.coefficientNumerator,
|
||||
);
|
||||
const denominator = BI.mul(
|
||||
t1.coefficientDenominator,
|
||||
t2.coefficientDenominator,
|
||||
);
|
||||
const numerator = t1.coefficientNumerator * t2.coefficientNumerator;
|
||||
const denominator =
|
||||
t1.coefficientDenominator * t2.coefficientDenominator;
|
||||
const gcd = findGcd(numerator, denominator);
|
||||
|
||||
terms.push({
|
||||
@@ -232,8 +227,8 @@ class Polynomial {
|
||||
t1.indeterminates,
|
||||
t2.indeterminates,
|
||||
),
|
||||
coefficientNumerator: BI.div(numerator, gcd),
|
||||
coefficientDenominator: BI.div(denominator, gcd),
|
||||
coefficientNumerator: numerator / gcd,
|
||||
coefficientDenominator: denominator / gcd,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -245,41 +240,49 @@ class Polynomial {
|
||||
return this.multiply(rhs.reciprocal());
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
const add: string[] = [];
|
||||
public toExpression(): Expression {
|
||||
const add: Expression[] = [];
|
||||
for (const t of this.terms) {
|
||||
const coefficient =
|
||||
BI.toNumber(t.coefficientNumerator) /
|
||||
BI.toNumber(t.coefficientDenominator);
|
||||
Number(t.coefficientNumerator) / Number(t.coefficientDenominator);
|
||||
|
||||
const mul: string[] = [];
|
||||
const mul: Expression[] = [];
|
||||
if (t.indeterminates.sortedKeys.length) {
|
||||
for (const k of t.indeterminates.sortedKeys) {
|
||||
const w = t.indeterminates.map.get(k);
|
||||
for (let i = Math.abs(w); i > 0; --i) {
|
||||
if (w > 0) mul.push(k);
|
||||
else mul.push(`["/",1,${k}]`);
|
||||
else
|
||||
mul.push(
|
||||
new Expression.Binary("/", new Expression.Literal(1), k),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (coefficient !== 1) mul.push(coefficient.toString());
|
||||
if (coefficient !== 1) mul.push(new Expression.Literal(coefficient));
|
||||
|
||||
if (mul.length > 1) add.push(`["*",${mul.join(",")}]`);
|
||||
else add.push(mul["0"]);
|
||||
while (mul.length > 1) {
|
||||
const r = mul.pop();
|
||||
const l = mul.pop();
|
||||
mul.push(new Expression.Binary("*", l, r));
|
||||
}
|
||||
add.push(mul[0]);
|
||||
} else {
|
||||
add.push(coefficient.toString());
|
||||
add.push(new Expression.Literal(coefficient));
|
||||
}
|
||||
}
|
||||
|
||||
if (!add.length) return "0";
|
||||
else if (add.length === 1) return add[0];
|
||||
else return `["+",${add.join(",")}]`;
|
||||
while (add.length > 1) {
|
||||
const r = add.pop();
|
||||
const l = add.pop();
|
||||
add.push(new Expression.Binary("+", l, r));
|
||||
}
|
||||
|
||||
if (!add.length) return new Expression.Literal(0);
|
||||
return add[0];
|
||||
}
|
||||
}
|
||||
|
||||
const ADDITIVE_IDENTITY = Polynomial.fromConstant(0);
|
||||
const MULTIPLICATIVE_IDENTITY = Polynomial.fromConstant(1);
|
||||
|
||||
const SWAPPED_OPS = {
|
||||
"=": "=",
|
||||
"<>": "<>",
|
||||
@@ -289,156 +292,163 @@ const SWAPPED_OPS = {
|
||||
"<=": ">=",
|
||||
};
|
||||
|
||||
function normalizeCallback(exp: Expression): Expression {
|
||||
if (!Array.isArray(exp)) return exp;
|
||||
const op = exp[0];
|
||||
|
||||
if (op === "FUNC" && exp[1] === "COALESCE") {
|
||||
const res: Expression[] = ["CASE"];
|
||||
for (let i = 2; i < exp.length; ++i)
|
||||
res.push(normalizeCallback(["IS NOT NULL", exp[i]]), exp[i]);
|
||||
return normalizeCallback(res);
|
||||
}
|
||||
|
||||
if (op === "CASE") {
|
||||
const res = [] as [Expression, Expression][];
|
||||
for (let i = 1; i < exp.length; i += 2) {
|
||||
let w = exp[i];
|
||||
if (w instanceof Polynomial) w = JSON.parse(w.toString());
|
||||
if (!Array.isArray(w) && !w) continue;
|
||||
const t = exp[i + 1];
|
||||
if (!Array.isArray(t) || t[0] !== "CASE") {
|
||||
res.push([w, t]);
|
||||
continue;
|
||||
}
|
||||
for (let j = 1; j < t.length; j += 2) res.push([and(w, t[j]), t[j + 1]]);
|
||||
res.push([w, null]);
|
||||
if (!Array.isArray(w) && w) break;
|
||||
}
|
||||
while (res[res.length - 1][1] == null) res.pop();
|
||||
return ["CASE", ...res.flat()];
|
||||
}
|
||||
|
||||
const permutations: Map<number, [Expression, Expression][]> = new Map();
|
||||
for (const [i, e] of exp.entries()) {
|
||||
if (!Array.isArray(e)) continue;
|
||||
if (e[0] !== "CASE") continue;
|
||||
const perms: [Expression, Expression][] = [];
|
||||
for (let j = 1; j < e.length; j += 2) perms.push([e[j], e[j + 1]]);
|
||||
perms.push([true, null]);
|
||||
permutations.set(i, perms);
|
||||
}
|
||||
|
||||
if (permutations.size) {
|
||||
let res: [Expression, Expression][] = [[true, exp]];
|
||||
for (const [i, perms] of permutations) {
|
||||
const res2: [Expression, Expression][] = [];
|
||||
for (const [w, t] of perms) {
|
||||
res2.push(
|
||||
...res.map((r) => {
|
||||
const e = (r[1] as Expression[]).slice();
|
||||
e[i] = t;
|
||||
return [and(w, r[0]), e] as [Expression, Expression];
|
||||
}),
|
||||
function cartesianProduct<T>(arrays: T[][]): T[][] {
|
||||
return arrays.reduce<T[][]>(
|
||||
(acc, cur) => acc.flatMap((a) => cur.map((item) => [...a, item])),
|
||||
[[]],
|
||||
);
|
||||
if (!Array.isArray(w) && w) break;
|
||||
}
|
||||
res = res2;
|
||||
}
|
||||
}
|
||||
|
||||
for (const r of res) r[1] = normalizeCallback(r[1]);
|
||||
|
||||
if (res[0][0] === true) return res[0][1];
|
||||
while (res[res.length - 1][1] == null) res.pop();
|
||||
|
||||
return ["CASE", ...res.flat()];
|
||||
}
|
||||
|
||||
function toPolynomial(e: Expression): Polynomial {
|
||||
if (e == null) return null;
|
||||
function toPolynomial(e: Expression): Polynomial {
|
||||
if (e instanceof Polynomial) return e;
|
||||
if (typeof e === "number") return Polynomial.fromConstant(e);
|
||||
if (typeof e === "string")
|
||||
return Polynomial.fromConstant(parseFloat(e) || 0);
|
||||
if (typeof e === "boolean") return Polynomial.fromConstant(+e);
|
||||
if (e instanceof Expression.Literal) {
|
||||
if (e.value == null) return null;
|
||||
if (typeof e.value === "number") return Polynomial.fromConstant(e.value);
|
||||
if (typeof e.value === "string")
|
||||
return Polynomial.fromConstant(parseFloat(e.value) || 0);
|
||||
if (typeof e.value === "boolean") return Polynomial.fromConstant(+e.value);
|
||||
}
|
||||
return Polynomial.fromIndeterminate(e);
|
||||
}
|
||||
|
||||
function fromPolynomial(e: Expression): Expression {
|
||||
if (e instanceof Polynomial) return e.toExpression();
|
||||
if (e instanceof Expression.Conditional) return e.map(fromPolynomial);
|
||||
return e;
|
||||
}
|
||||
|
||||
function normalizeCallback(exp: Expression): Expression {
|
||||
if (exp instanceof Expression.FunctionCall) {
|
||||
if (exp.name === "COALESCE") {
|
||||
let e: Expression = new Expression.Literal(null);
|
||||
for (let i = exp.args.length - 1; i >= 0; --i) {
|
||||
e = new Expression.Conditional(
|
||||
normalizeCallback(new Expression.Unary("IS NOT NULL", exp.args[i])),
|
||||
exp.args[i],
|
||||
e,
|
||||
);
|
||||
}
|
||||
return normalizeCallback(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (op === "+") {
|
||||
const args: Polynomial[] = [];
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const p = toPolynomial(exp[i]);
|
||||
if (p == null) return null;
|
||||
args.push(p);
|
||||
if (exp instanceof Expression.Conditional) {
|
||||
let e = exp;
|
||||
|
||||
if (e.condition instanceof Polynomial)
|
||||
e = new Expression.Conditional(
|
||||
e.condition.toExpression(),
|
||||
e.then,
|
||||
e.otherwise,
|
||||
);
|
||||
|
||||
if (e.then instanceof Expression.Conditional) {
|
||||
e = new Expression.Conditional(
|
||||
Expression.and(e.condition, e.then.condition),
|
||||
e.then.then,
|
||||
new Expression.Conditional(e.condition, e.then.otherwise, e.otherwise),
|
||||
);
|
||||
}
|
||||
return args.reduce(
|
||||
(previousValue, currentValue) => previousValue.add(currentValue),
|
||||
ADDITIVE_IDENTITY,
|
||||
) as unknown as Expression;
|
||||
} else if (op === "*") {
|
||||
const args: Polynomial[] = [];
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const p = toPolynomial(exp[i]);
|
||||
if (p == null) return null;
|
||||
args.push(p);
|
||||
|
||||
return e;
|
||||
}
|
||||
return args.reduce(
|
||||
(previousValue, currentValue) => previousValue.multiply(currentValue),
|
||||
MULTIPLICATIVE_IDENTITY,
|
||||
) as unknown as Expression;
|
||||
} else if (op === "-") {
|
||||
const args: Polynomial[] = [];
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const p = toPolynomial(exp[i]);
|
||||
if (p == null) return null;
|
||||
args.push(p);
|
||||
|
||||
const combs: [Expression, Expression][][] = [];
|
||||
const callback: (e: Expression) => Expression = (e) => {
|
||||
if (e instanceof Expression.Conditional) {
|
||||
combs[combs.length - 1].push([e.condition, e.then]);
|
||||
callback(e.otherwise);
|
||||
} else {
|
||||
combs[combs.length - 1].push([new Expression.Literal(true), e]);
|
||||
}
|
||||
return args.reduce((previousValue, currentValue) =>
|
||||
previousValue.subtract(currentValue),
|
||||
) as unknown as Expression;
|
||||
} else if (op === "/") {
|
||||
const args: Polynomial[] = [];
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const p = toPolynomial(exp[i]);
|
||||
if (p == null) return null;
|
||||
args.push(p);
|
||||
return e;
|
||||
};
|
||||
|
||||
exp.map((e) => {
|
||||
combs.push([]);
|
||||
callback(e);
|
||||
return e;
|
||||
});
|
||||
|
||||
if (combs.some((a) => a.length > 1)) {
|
||||
let res: Expression = new Expression.Literal(null);
|
||||
for (const p of cartesianProduct(combs).reverse()) {
|
||||
let condition: Expression = new Expression.Literal(true);
|
||||
const e = reduce(
|
||||
normalizeCallback(
|
||||
exp.map((_, i) => {
|
||||
condition = Expression.and(condition, p[i][0]);
|
||||
return p[i][1];
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (!(condition instanceof Expression.Literal))
|
||||
res = new Expression.Conditional(condition, e, res);
|
||||
else if (condition.value) res = e;
|
||||
}
|
||||
return args.reduce((previousValue, currentValue) =>
|
||||
previousValue.divide(currentValue),
|
||||
) as unknown as Expression;
|
||||
} else if (["=", "<>", ">", ">=", "<", "<="].includes(op)) {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
return res;
|
||||
}
|
||||
|
||||
if (exp instanceof Expression.Binary) {
|
||||
if (["+", "-", "*", "/"].includes(exp.operator)) {
|
||||
const lhs = toPolynomial(exp.left);
|
||||
const rhs = toPolynomial(exp.right);
|
||||
if (lhs == null || rhs == null) return new Expression.Literal(null);
|
||||
if (exp.operator === "+") return lhs.add(rhs);
|
||||
if (exp.operator === "-") return lhs.subtract(rhs);
|
||||
if (exp.operator === "*") return lhs.multiply(rhs);
|
||||
if (exp.operator === "/") return lhs.divide(rhs);
|
||||
} else if ([">", ">=", "<", "<=", "=", "<>"].includes(exp.operator)) {
|
||||
let lhs: Polynomial, rhs: Polynomial;
|
||||
if (exp[1] instanceof Polynomial) lhs = exp[1];
|
||||
else if (typeof exp[1] === "number") lhs = Polynomial.fromConstant(exp[1]);
|
||||
|
||||
if (exp[2] instanceof Polynomial) rhs = exp[2];
|
||||
else if (typeof exp[2] === "number") rhs = Polynomial.fromConstant(exp[2]);
|
||||
if (exp.left instanceof Polynomial) lhs = exp.left;
|
||||
else if (exp.left instanceof Expression.Literal) {
|
||||
if (exp.left.value == null) return exp.left;
|
||||
else if (typeof exp.left.value === "number")
|
||||
lhs = Polynomial.fromConstant(exp.left.value);
|
||||
}
|
||||
|
||||
if (exp.right instanceof Polynomial) rhs = exp.right;
|
||||
else if (exp.right instanceof Expression.Literal) {
|
||||
if (exp.right.value == null) return exp.right;
|
||||
else if (typeof exp.right.value === "number")
|
||||
rhs = Polynomial.fromConstant(exp.right.value);
|
||||
}
|
||||
|
||||
if (lhs || rhs) {
|
||||
if (!lhs) lhs = Polynomial.fromIndeterminate(exp[1]);
|
||||
if (!rhs) rhs = Polynomial.fromIndeterminate(exp[2]);
|
||||
if (!lhs) lhs = Polynomial.fromIndeterminate(exp.left);
|
||||
if (!rhs) rhs = Polynomial.fromIndeterminate(exp.right);
|
||||
|
||||
lhs = lhs.subtract(rhs);
|
||||
rhs = lhs.constant().negation();
|
||||
lhs = lhs.add(rhs);
|
||||
|
||||
if (!lhs.terms.length) {
|
||||
exp = [op, JSON.parse(lhs.toString()), JSON.parse(rhs.toString())];
|
||||
const l = lhs.toExpression() as Expression.Literal;
|
||||
const r = rhs.toExpression() as Expression.Literal;
|
||||
|
||||
if (exp.operator === "=")
|
||||
return new Expression.Literal(l.value === r.value);
|
||||
else if (exp.operator === "<>")
|
||||
return new Expression.Literal(l.value !== r.value);
|
||||
else if (exp.operator === ">")
|
||||
return new Expression.Literal(l.value > r.value);
|
||||
else if (exp.operator === ">=")
|
||||
return new Expression.Literal(l.value >= r.value);
|
||||
else if (exp.operator === "<")
|
||||
return new Expression.Literal(l.value < r.value);
|
||||
else if (exp.operator === "<=")
|
||||
return new Expression.Literal(l.value <= r.value);
|
||||
else throw new Error("Invalid operator");
|
||||
}
|
||||
|
||||
if (op === "=") return exp[1] === exp[2];
|
||||
else if (op === "<>") return exp[1] !== exp[2];
|
||||
else if (op === ">") return exp[1] > exp[2];
|
||||
else if (op === ">=") return exp[1] >= exp[2];
|
||||
else if (op === "<") return exp[1] < exp[2];
|
||||
else if (op === "<=") return exp[1] <= exp[2];
|
||||
} else {
|
||||
let flipOp = 1;
|
||||
|
||||
const n = lhs.terms[0].coefficientNumerator;
|
||||
const d = lhs.terms[0].coefficientDenominator;
|
||||
|
||||
if (BI.lt(n, ZERO) || BI.lt(d, ZERO)) flipOp *= -1;
|
||||
if (n < 0n || d < 0n) flipOp *= -1;
|
||||
|
||||
const reciprocal = new Polynomial([
|
||||
{
|
||||
@@ -463,30 +473,18 @@ function normalizeCallback(exp: Expression): Expression {
|
||||
rhs = rhs.reciprocal();
|
||||
}
|
||||
|
||||
if (flipOp < 0) exp = [SWAPPED_OPS[op], lhs, rhs];
|
||||
else exp = [op, lhs, rhs];
|
||||
if (flipOp > 0) exp = new Expression.Binary(exp.operator, lhs, rhs);
|
||||
else exp = new Expression.Binary(SWAPPED_OPS[exp.operator], lhs, rhs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Restore polynomial expressions
|
||||
exp = exp.map((e) =>
|
||||
e instanceof Polynomial ? JSON.parse(e.toString()) : e,
|
||||
);
|
||||
|
||||
exp = evaluateCallback(exp);
|
||||
exp = exp.map(fromPolynomial);
|
||||
|
||||
return exp;
|
||||
}
|
||||
|
||||
export default function normalize(expr: Expression): Expression {
|
||||
expr = map(expr, normalizeCallback);
|
||||
if (expr instanceof Polynomial) {
|
||||
expr = JSON.parse(expr.toString());
|
||||
} else if (Array.isArray(expr) && expr[0] === "CASE") {
|
||||
expr = expr.map((e) =>
|
||||
e instanceof Polynomial ? JSON.parse(e.toString()) : e,
|
||||
);
|
||||
}
|
||||
return expr;
|
||||
export default function normalize(exp: Expression): Expression {
|
||||
return fromPolynomial(exp.evaluate(normalizeCallback));
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { complement } from "espresso-iisojs";
|
||||
import { Expression } from "../../types";
|
||||
import normalize from "./normalize";
|
||||
import { Clause, SynthContext } from "./synth";
|
||||
import { and, or } from "./util";
|
||||
import Expression from "../expression.ts";
|
||||
import normalize from "./normalize.ts";
|
||||
import { Clause, SynthContext } from "./synth.ts";
|
||||
import Path from "../path.ts";
|
||||
|
||||
type Bookmark = Record<string, null | boolean | number | string>;
|
||||
|
||||
@@ -27,25 +27,35 @@ export function bookmarkToExpression(
|
||||
): Expression {
|
||||
return Object.entries(sort)
|
||||
.reverse()
|
||||
.reduce((cur, kv) => {
|
||||
.reduce(
|
||||
(cur, kv) => {
|
||||
const [param, asc] = kv;
|
||||
const p = new Expression.Parameter(Path.parse(param));
|
||||
const b = new Expression.Literal(bookmark[param]);
|
||||
if (asc < 0) {
|
||||
if (bookmark[param] == null) {
|
||||
return or(
|
||||
["IS NOT NULL", ["PARAM", param]],
|
||||
and(["IS NULL", ["PARAM", param]], cur),
|
||||
return Expression.or(
|
||||
new Expression.Unary("IS NOT NULL", p),
|
||||
Expression.and(new Expression.Unary("IS NULL", p), cur),
|
||||
);
|
||||
}
|
||||
let f = null;
|
||||
f = or(f, [">", ["PARAM", param], bookmark[param]]);
|
||||
return or(f, and(["=", ["PARAM", param], bookmark[param]], cur));
|
||||
return new Expression.Binary(
|
||||
"OR",
|
||||
new Expression.Binary(">", p, b),
|
||||
new Expression.Binary("AND", new Expression.Binary("=", p, b), cur),
|
||||
);
|
||||
} else {
|
||||
let f: Expression = ["IS NULL", ["PARAM", param]];
|
||||
if (bookmark[param] == null) return and(f, cur);
|
||||
f = or(f, ["<", ["PARAM", param], bookmark[param]]);
|
||||
return or(f, and(["=", ["PARAM", param], bookmark[param]], cur));
|
||||
let f: Expression = new Expression.Unary("IS NULL", p);
|
||||
if (bookmark[param] == null) return Expression.and(f, cur);
|
||||
f = Expression.or(f, new Expression.Binary("<", p, b));
|
||||
return Expression.or(
|
||||
f,
|
||||
Expression.and(new Expression.Binary("=", p, b), cur),
|
||||
);
|
||||
}
|
||||
}, true as Expression);
|
||||
},
|
||||
new Expression.Literal(true) as Expression,
|
||||
);
|
||||
}
|
||||
|
||||
function getCover(
|
||||
@@ -60,7 +70,7 @@ function getCover(
|
||||
const nextCov: Minterm = [];
|
||||
|
||||
if (sort > 0) {
|
||||
const lhs = new Clause.Exp(["PARAM", param]);
|
||||
const lhs = new Clause.Exp(new Expression.Parameter(Path.parse(param)));
|
||||
const isNull = context.getVar(new Clause.IsNull(lhs));
|
||||
cov.push((isNull << 2) ^ 2);
|
||||
}
|
||||
@@ -68,15 +78,15 @@ function getCover(
|
||||
for (const m of minterm) {
|
||||
const clause = context.getClause(m >>> 2);
|
||||
if (clause instanceof Clause.IsNull) {
|
||||
const lhs = clause.operand.expression();
|
||||
if (!Array.isArray(lhs) || lhs[0] !== "PARAM") continue;
|
||||
if (lhs[1] !== param) continue;
|
||||
if (!(clause.operand instanceof Clause.Exp)) continue;
|
||||
if (!(clause.operand.exp instanceof Expression.Parameter)) continue;
|
||||
if (clause.operand.exp.path.toString() !== param) continue;
|
||||
nextCov.push(m);
|
||||
if (sort < 0 && m & 1) cov.push(m, m ^ 1);
|
||||
} else if (clause instanceof Clause.Compare) {
|
||||
const lhs = clause.lhs.expression();
|
||||
if (!Array.isArray(lhs) || lhs[0] !== "PARAM") continue;
|
||||
if (lhs[1] !== param) continue;
|
||||
if (!(clause.lhs instanceof Clause.Exp)) continue;
|
||||
if (!(clause.lhs.exp instanceof Expression.Parameter)) continue;
|
||||
if (clause.lhs.exp.path.toString() !== param) continue;
|
||||
if (!(m & 1) && sort > 0) continue;
|
||||
nextCov.push(m);
|
||||
|
||||
@@ -117,18 +127,20 @@ export function paginate(
|
||||
sort: Record<string, number>,
|
||||
): [Expression, Expression] {
|
||||
fetched = normalize(fetched);
|
||||
if (!fetched) return [false, toFetch];
|
||||
if (fetched instanceof Expression.Literal && !fetched.value)
|
||||
return [new Expression.Literal(false), toFetch];
|
||||
|
||||
toFetch = normalize(toFetch);
|
||||
if (!toFetch) return [false, toFetch];
|
||||
if (toFetch instanceof Expression.Literal && !toFetch.value)
|
||||
return [new Expression.Literal(false), toFetch];
|
||||
|
||||
const synth1 = Clause.fromExpression(fetched);
|
||||
const synth2 = Clause.fromExpression(toFetch);
|
||||
|
||||
const context = new SynthContext();
|
||||
|
||||
const expr1Minterms = synth1.true(context);
|
||||
const expr2MintermsC = complement(synth2.true(context));
|
||||
const expr1Minterms = synth1.getMinterms(context, 0b100);
|
||||
const expr2MintermsC = synth2.getMinterms(context, 0b011);
|
||||
|
||||
const gaps = context.sanitizeMinterms(
|
||||
complement([
|
||||
|
||||
+492
-394
@@ -1,5 +1,166 @@
|
||||
import parsimmon from "parsimmon";
|
||||
import { Expression } from "../../types.ts";
|
||||
import Path from "../path.ts";
|
||||
import Expression from "../expression.ts";
|
||||
|
||||
export class Cursor {
|
||||
input: string;
|
||||
pos: number;
|
||||
boundryStack: number[][];
|
||||
boundry: number[];
|
||||
charCode: number;
|
||||
|
||||
constructor(input: string) {
|
||||
this.input = input;
|
||||
this.pos = 0;
|
||||
this.boundryStack = [];
|
||||
this.boundry = [];
|
||||
this.charCode = input.charCodeAt(0) || 0;
|
||||
}
|
||||
|
||||
fork(): Cursor {
|
||||
const cursor = new Cursor(this.input);
|
||||
cursor.pos = this.pos;
|
||||
cursor.boundryStack = this.boundryStack.slice();
|
||||
cursor.boundry = this.boundry;
|
||||
cursor.charCode = this.charCode;
|
||||
return cursor;
|
||||
}
|
||||
|
||||
sync(cursor: Cursor): void {
|
||||
this.pos = cursor.pos;
|
||||
this.boundryStack = cursor.boundryStack.slice();
|
||||
this.boundry = cursor.boundry;
|
||||
this.charCode = cursor.charCode;
|
||||
}
|
||||
|
||||
read(cur: Cursor): string {
|
||||
return this.input.slice(this.pos, cur.pos);
|
||||
}
|
||||
|
||||
step(): Cursor {
|
||||
if (this.charCode === 0) return this;
|
||||
++this.pos;
|
||||
this.charCode = this.input.charCodeAt(this.pos) || 0;
|
||||
if (this.boundry.includes(this.charCode)) this.charCode = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
walk(callback: (charCode: number) => boolean): Cursor {
|
||||
while (this.charCode && callback(this.charCode)) this.step();
|
||||
return this;
|
||||
}
|
||||
|
||||
descend(chars: number[], override = true): void {
|
||||
this.boundryStack.push(this.boundry);
|
||||
if (override) this.boundry = chars;
|
||||
else this.boundry = [...this.boundry, ...chars];
|
||||
this.charCode = this.input.charCodeAt(this.pos) || 0;
|
||||
if (this.boundry.includes(this.charCode)) this.charCode = 0;
|
||||
}
|
||||
|
||||
ascend(): void {
|
||||
if (!this.boundryStack.length) throw new Error("Unmatched boundry");
|
||||
this.boundry = this.boundryStack.pop();
|
||||
this.charCode = this.input.charCodeAt(this.pos) || 0;
|
||||
}
|
||||
|
||||
skipwhitespace(): Cursor {
|
||||
return this.walk((c) => c <= 32);
|
||||
}
|
||||
}
|
||||
|
||||
const CHAR_SINGLE_QUOTE = 39;
|
||||
const CHAR_DOUBLE_QUOTE = 34;
|
||||
const CHAR_OPEN_PAREN = 40;
|
||||
const CHAR_CLOSE_PAREN = 41;
|
||||
const CHAR_COMMA = 44;
|
||||
const CHAR_PERIOD = 46;
|
||||
const CHAR_COLON = 58;
|
||||
const CHAR_OPEN_BRACKET = 91;
|
||||
const CHAR_BACKSLASH = 92;
|
||||
const CHAR_CLOSE_BRACKET = 93;
|
||||
|
||||
const BINARY_OPERATORS = [
|
||||
">=",
|
||||
"<=",
|
||||
"<>",
|
||||
"=",
|
||||
">",
|
||||
"<",
|
||||
"LIKE",
|
||||
"NOT LIKE",
|
||||
"AND",
|
||||
"OR",
|
||||
"*",
|
||||
"/",
|
||||
"%",
|
||||
"||",
|
||||
"-",
|
||||
"+",
|
||||
];
|
||||
|
||||
const PRECEDENCE = {
|
||||
OR: 10,
|
||||
AND: 11,
|
||||
NOT: 12,
|
||||
"=": 20,
|
||||
"<>": 20,
|
||||
">": 20,
|
||||
">=": 20,
|
||||
"<": 20,
|
||||
"<=": 20,
|
||||
LIKE: 20,
|
||||
"NOT LIKE": 20,
|
||||
"IS NULL": 20,
|
||||
"IS NOT NULL": 20,
|
||||
"||": 30,
|
||||
"+": 31,
|
||||
"-": 31,
|
||||
"*": 32,
|
||||
"/": 32,
|
||||
"%": 32,
|
||||
};
|
||||
|
||||
function* range(s: number, e: number): Generator<number> {
|
||||
for (let i = s; i < e; i++) yield i;
|
||||
}
|
||||
|
||||
const PATH_CHARS = new Set<number>([
|
||||
...range(65, 91),
|
||||
...range(97, 123),
|
||||
...range(48, 58),
|
||||
95,
|
||||
45,
|
||||
42,
|
||||
123,
|
||||
125,
|
||||
]);
|
||||
|
||||
function findOperator(cursor: Cursor): string {
|
||||
cursor.skipwhitespace();
|
||||
let found = "";
|
||||
let foundCursor = cursor;
|
||||
let operators = [...BINARY_OPERATORS, "IS NULL", "IS NOT NULL"];
|
||||
for (let i = 0; operators.length && cursor.charCode; ++i) {
|
||||
let c = cursor.charCode;
|
||||
cursor.step();
|
||||
if (c >= 97 && c <= 122) c -= 32;
|
||||
if (c <= 32) {
|
||||
cursor.skipwhitespace();
|
||||
c = 32;
|
||||
}
|
||||
operators = operators.filter((o) => {
|
||||
if (o.charCodeAt(i) !== c) return false;
|
||||
if (o.length === i + 1) {
|
||||
found = o;
|
||||
foundCursor = cursor.fork();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
if (found) cursor.sync(foundCursor);
|
||||
return found;
|
||||
}
|
||||
|
||||
// Turn escaped characters into real ones (e.g. "\\n" becomes "\n").
|
||||
function interpretEscapes(str): string {
|
||||
@@ -21,423 +182,330 @@ function interpretEscapes(str): string {
|
||||
});
|
||||
}
|
||||
|
||||
export function map<T>(exp: Expression, callback: (e: Expression) => T): T {
|
||||
if (!Array.isArray(exp)) return callback(exp);
|
||||
export function parseExpression(cursor: Cursor, presedence = 0): Expression {
|
||||
cursor.skipwhitespace();
|
||||
const char = cursor.charCode;
|
||||
let lhs: Expression;
|
||||
if (char === CHAR_OPEN_PAREN) {
|
||||
cursor.step();
|
||||
cursor.descend([CHAR_CLOSE_PAREN]);
|
||||
lhs = parseExpression(cursor, 0);
|
||||
cursor.ascend();
|
||||
|
||||
let clone;
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const sub = map(exp[i], callback);
|
||||
if (sub !== exp[i]) {
|
||||
clone = clone || exp.slice();
|
||||
clone[i] = sub;
|
||||
cursor.skipwhitespace();
|
||||
if (cursor.charCode !== CHAR_CLOSE_PAREN)
|
||||
throw new Error("Expected ')'" + cursor.pos + " " + cursor.charCode);
|
||||
cursor.step();
|
||||
} else if (char === CHAR_DOUBLE_QUOTE) {
|
||||
cursor.step();
|
||||
const cursor2 = cursor.fork();
|
||||
for (
|
||||
cursor2.descend([]);
|
||||
cursor2.charCode !== CHAR_DOUBLE_QUOTE;
|
||||
cursor2.step()
|
||||
) {
|
||||
if (!cursor2.charCode) throw new Error("Unterminated string");
|
||||
if (cursor2.charCode === CHAR_BACKSLASH) cursor2.step();
|
||||
}
|
||||
cursor2.ascend();
|
||||
const str = cursor.read(cursor2);
|
||||
cursor.sync(cursor2);
|
||||
cursor.step();
|
||||
return new Expression.Literal(interpretEscapes(str));
|
||||
} else if (char === CHAR_SINGLE_QUOTE) {
|
||||
cursor.step();
|
||||
const cursor2 = cursor.fork();
|
||||
for (
|
||||
cursor2.descend([]);
|
||||
cursor2.charCode !== CHAR_SINGLE_QUOTE;
|
||||
cursor2.step()
|
||||
) {
|
||||
if (!cursor2.charCode) throw new Error("Unterminated string");
|
||||
if (cursor2.charCode === CHAR_BACKSLASH) cursor2.step();
|
||||
}
|
||||
cursor2.ascend();
|
||||
const str = cursor.read(cursor2);
|
||||
cursor.sync(cursor2);
|
||||
cursor.step();
|
||||
return new Expression.Literal(str.replaceAll("''", "'"));
|
||||
} else {
|
||||
const cursor2 = cursor.fork();
|
||||
cursor.walk(
|
||||
(c) => c > 32 && c !== CHAR_OPEN_PAREN && c !== CHAR_OPEN_BRACKET,
|
||||
);
|
||||
const token = cursor2.read(cursor);
|
||||
if (!token) throw new Error("Invalid expression");
|
||||
if (/^true$/i.test(token)) lhs = new Expression.Literal(true);
|
||||
else if (/^false$/i.test(token)) lhs = new Expression.Literal(false);
|
||||
else if (/^null$/i.test(token)) lhs = new Expression.Literal(null);
|
||||
else if (/^-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?$/.test(token))
|
||||
lhs = new Expression.Literal(Number(token));
|
||||
else if (/^not$/i.test(token)) {
|
||||
lhs = new Expression.Unary(
|
||||
"NOT",
|
||||
parseExpression(cursor, PRECEDENCE["NOT"]),
|
||||
);
|
||||
} else if (/^case$/i.test(token)) {
|
||||
const pairs: [Expression, Expression][] = [];
|
||||
for (;;) {
|
||||
cursor.skipwhitespace();
|
||||
const whenStr = cursor.fork().read(cursor.walk((c) => c > 32));
|
||||
if (/^else$/i.test(whenStr)) {
|
||||
lhs = parseExpression(cursor);
|
||||
continue;
|
||||
}
|
||||
if (/^end$/i.test(whenStr)) break;
|
||||
else if (lhs) throw new Error("Expected END");
|
||||
if (!/^when$/i.test(whenStr)) throw new Error("Expected WHEN");
|
||||
const condition = parseExpression(cursor);
|
||||
cursor.skipwhitespace();
|
||||
const thenStr = cursor.fork().read(cursor.walk((c) => c > 32));
|
||||
if (!/^then$/i.test(thenStr)) throw new Error("Expected THEN");
|
||||
const then = parseExpression(cursor);
|
||||
pairs.push([condition, then]);
|
||||
}
|
||||
if (!lhs) lhs = new Expression.Literal(null);
|
||||
while (pairs.length) {
|
||||
const [condition, then] = pairs.pop();
|
||||
lhs = new Expression.Conditional(condition, then, lhs);
|
||||
}
|
||||
} else if (cursor.charCode === CHAR_OPEN_PAREN) {
|
||||
cursor.step();
|
||||
cursor.descend([CHAR_CLOSE_PAREN]);
|
||||
cursor.skipwhitespace();
|
||||
const args = [] as Expression[];
|
||||
while (cursor.charCode) {
|
||||
cursor.descend([CHAR_COMMA], false);
|
||||
const e = parseExpression(cursor);
|
||||
args.push(e);
|
||||
cursor.ascend();
|
||||
cursor.skipwhitespace();
|
||||
if ((cursor.charCode as number) !== CHAR_COMMA) break;
|
||||
cursor.step();
|
||||
}
|
||||
cursor.ascend();
|
||||
cursor.step();
|
||||
lhs = new Expression.FunctionCall(token, args);
|
||||
} else {
|
||||
const p = parsePath(cursor2);
|
||||
lhs = new Expression.Parameter(p);
|
||||
cursor.sync(cursor2);
|
||||
}
|
||||
}
|
||||
|
||||
return callback(clone || exp);
|
||||
for (;;) {
|
||||
cursor.skipwhitespace();
|
||||
const cursor2 = cursor.fork();
|
||||
const op = findOperator(cursor2);
|
||||
const p = PRECEDENCE[op];
|
||||
if (p <= presedence) return lhs;
|
||||
if (op === "IS NULL") {
|
||||
lhs = new Expression.Unary("IS NULL", lhs);
|
||||
} else if (op === "IS NOT NULL") {
|
||||
lhs = new Expression.Unary("IS NOT NULL", lhs);
|
||||
} else if (BINARY_OPERATORS.includes(op)) {
|
||||
lhs = new Expression.Binary(op, lhs, parseExpression(cursor2, p));
|
||||
} else if (op) throw new Error("Unrecognized operator: " + op);
|
||||
else break;
|
||||
cursor.sync(cursor2);
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
|
||||
export async function mapAsync(
|
||||
exp: Expression,
|
||||
callback: (e: Expression) => Promise<Expression>,
|
||||
): Promise<Expression> {
|
||||
if (!Array.isArray(exp)) return callback(exp);
|
||||
|
||||
let clone;
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const sub = await mapAsync(exp[i], callback);
|
||||
if (sub !== exp[i]) {
|
||||
clone = clone || exp.slice();
|
||||
clone[i] = sub;
|
||||
// Parse old-format alias value: unquoted, double-quoted, or single-quoted.
|
||||
// Returns the parsed string value. Cursor is advanced past the value.
|
||||
function parseOldAliasValue(cursor: Cursor): string {
|
||||
cursor.walk((c) => c <= 32); // skip leading whitespace
|
||||
const c = cursor.charCode;
|
||||
if (c === CHAR_DOUBLE_QUOTE) {
|
||||
// Double-quoted: use JSON.parse semantics (handles \", \n, \uXXXX etc.)
|
||||
// Descend with no boundaries so commas/brackets inside quotes are not
|
||||
// treated as terminators.
|
||||
const start = cursor.pos;
|
||||
cursor.step();
|
||||
cursor.descend([]);
|
||||
while (cursor.charCode) {
|
||||
if (cursor.charCode === CHAR_BACKSLASH) {
|
||||
cursor.step();
|
||||
if (!cursor.charCode) break;
|
||||
} else if (cursor.charCode === CHAR_DOUBLE_QUOTE) {
|
||||
cursor.ascend();
|
||||
cursor.step();
|
||||
return JSON.parse(cursor.input.slice(start, cursor.pos)) as string;
|
||||
}
|
||||
cursor.step();
|
||||
}
|
||||
throw new Error("Unterminated string");
|
||||
}
|
||||
if (c === CHAR_SINGLE_QUOTE) {
|
||||
// Single-quoted: strip quotes, no escape processing
|
||||
cursor.step();
|
||||
cursor.descend([]);
|
||||
const start = cursor.pos;
|
||||
while (cursor.charCode && cursor.charCode !== CHAR_SINGLE_QUOTE)
|
||||
cursor.step();
|
||||
if (!cursor.charCode) throw new Error("Unterminated string");
|
||||
const value = cursor.input.slice(start, cursor.pos);
|
||||
cursor.ascend();
|
||||
cursor.step();
|
||||
return value;
|
||||
}
|
||||
// Unquoted: read until , or ] (respecting boundary), trim result
|
||||
const start = cursor.pos;
|
||||
cursor.walk(() => true);
|
||||
return cursor.input.slice(start, cursor.pos).trim();
|
||||
}
|
||||
|
||||
// Parse old-format alias content: key:value,key:value,...
|
||||
// Cursor should be positioned at the start of bracket content
|
||||
// (after '[', with ']' set as boundary).
|
||||
// Returns an Expression: Binary("AND", ...) chain of Binary("=", param, literal).
|
||||
// Empty content returns Literal(true).
|
||||
function parseOldAlias(cursor: Cursor): Expression {
|
||||
cursor.skipwhitespace();
|
||||
if (!cursor.charCode) return new Expression.Literal(true);
|
||||
|
||||
let result: Expression | null = null;
|
||||
for (;;) {
|
||||
cursor.skipwhitespace();
|
||||
// Parse key as a path (supports nested aliases via parsePath).
|
||||
// Add colon and comma as boundaries so the path stops there.
|
||||
cursor.descend([CHAR_COLON, CHAR_COMMA], false);
|
||||
const keyPath = parsePath(cursor);
|
||||
cursor.ascend();
|
||||
|
||||
// After ascend, check for the colon separator
|
||||
cursor.skipwhitespace();
|
||||
if ((cursor.charCode as number) !== CHAR_COLON)
|
||||
throw new Error("Expected ':'");
|
||||
cursor.step();
|
||||
|
||||
// Parse value (reads until boundary , or ])
|
||||
cursor.descend([CHAR_COMMA], false);
|
||||
const value = parseOldAliasValue(cursor);
|
||||
cursor.ascend();
|
||||
|
||||
const pair = new Expression.Binary(
|
||||
"=",
|
||||
new Expression.Parameter(keyPath),
|
||||
new Expression.Literal(value),
|
||||
);
|
||||
result = result ? new Expression.Binary("AND", result, pair) : pair;
|
||||
|
||||
cursor.skipwhitespace();
|
||||
if ((cursor.charCode as number) !== CHAR_COMMA) break;
|
||||
cursor.step();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function parsePath(cur: Cursor): Path {
|
||||
const segments: (string | Expression)[] = [];
|
||||
let colon = 0;
|
||||
const cur2 = cur.fork();
|
||||
for (;;) {
|
||||
let char = cur2.charCode;
|
||||
let exp: Expression;
|
||||
|
||||
if (char === CHAR_OPEN_BRACKET) {
|
||||
if (cur2.pos !== cur.pos) throw new Error("Invalid path");
|
||||
cur2.step();
|
||||
cur2.descend([CHAR_CLOSE_BRACKET]);
|
||||
// Try parsing as expression first
|
||||
const cur3 = cur2.fork();
|
||||
let err: unknown;
|
||||
try {
|
||||
exp = parseExpression(cur3);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
// Fall back to old alias format if not a valid expression or produced
|
||||
// a bare Parameter (old format like [a:1] mis-parsed as a path).
|
||||
if (err || exp instanceof Expression.Parameter) {
|
||||
exp = parseOldAlias(cur2);
|
||||
} else {
|
||||
cur2.sync(cur3);
|
||||
}
|
||||
cur2.ascend();
|
||||
char = cur2.charCode;
|
||||
if (char !== CHAR_CLOSE_BRACKET) throw new Error("Expected ']'");
|
||||
char = cur2.step().charCode;
|
||||
}
|
||||
|
||||
return callback(clone || exp);
|
||||
}
|
||||
if (!PATH_CHARS.has(char)) {
|
||||
if (cur2.pos <= cur.pos) throw new Error("Invalid path");
|
||||
|
||||
function binaryLeft(
|
||||
operatorsParser: parsimmon.Parser<string>,
|
||||
nextParser: parsimmon.Parser<Expression>,
|
||||
): parsimmon.Parser<Expression> {
|
||||
return parsimmon.seqMap(
|
||||
nextParser,
|
||||
parsimmon.seq(operatorsParser, nextParser).many(),
|
||||
(first, rest) =>
|
||||
rest.reduce((acc, ch) => {
|
||||
const [op, another] = ch;
|
||||
if (Array.isArray(acc) && op === acc[0]) return acc.concat([another]);
|
||||
return [op, acc, another];
|
||||
}, first),
|
||||
);
|
||||
}
|
||||
|
||||
const lang = parsimmon.createLanguage({
|
||||
ComparisonOperator: function () {
|
||||
return parsimmon
|
||||
.alt(
|
||||
parsimmon.string(">="),
|
||||
parsimmon.string("<>"),
|
||||
parsimmon.string("<="),
|
||||
parsimmon.string("="),
|
||||
parsimmon.string(">"),
|
||||
parsimmon.string("<"),
|
||||
)
|
||||
.skip(parsimmon.optWhitespace);
|
||||
},
|
||||
LikeOperator: function () {
|
||||
return parsimmon
|
||||
.alt(
|
||||
parsimmon.regexp(/like/i).result("LIKE").desc("LIKE"),
|
||||
parsimmon
|
||||
.regexp(/not\s+like/i)
|
||||
.result("NOT LIKE")
|
||||
.desc("NOT LIKE"),
|
||||
)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace);
|
||||
},
|
||||
IsNullOperator: function () {
|
||||
return parsimmon
|
||||
.alt(
|
||||
parsimmon
|
||||
.regexp(/is\s+null/i)
|
||||
.result("IS NULL")
|
||||
.desc("IS NULL"),
|
||||
parsimmon
|
||||
.regexp(/is\s+not\s+null/i)
|
||||
.result("IS NOT NULL")
|
||||
.desc("IS NOT NULL"),
|
||||
)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace);
|
||||
},
|
||||
NotOperator: function () {
|
||||
return parsimmon
|
||||
.regexp(/not/i)
|
||||
.result("NOT")
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("NOT");
|
||||
},
|
||||
AndOperator: function () {
|
||||
return parsimmon
|
||||
.regexp(/and/i)
|
||||
.result("AND")
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("AND");
|
||||
},
|
||||
OrOperator: function () {
|
||||
return parsimmon
|
||||
.regexp(/or/i)
|
||||
.result("OR")
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("OR");
|
||||
},
|
||||
Parameter: function (r) {
|
||||
return parsimmon
|
||||
.alt(
|
||||
parsimmon.regexp(/[a-zA-Z0-9_.*-]+/),
|
||||
r.Expression.wrap(
|
||||
parsimmon.string("{").skip(parsimmon.optWhitespace),
|
||||
parsimmon.string("}"),
|
||||
),
|
||||
)
|
||||
.atLeast(1)
|
||||
.map((x) => ["PARAM", x.length > 1 ? ["||"].concat(x) : x[0]])
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("parameter");
|
||||
},
|
||||
StringValueSql: function () {
|
||||
return parsimmon
|
||||
.regexp(/'([^']*)'/, 1)
|
||||
.atLeast(1)
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.map((s) => s.join("'"))
|
||||
.desc("string");
|
||||
},
|
||||
StringValueJs: function () {
|
||||
return parsimmon
|
||||
.regexp(/"((?:\\.|.)*?)"/, 1)
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.map(interpretEscapes)
|
||||
.desc("string");
|
||||
},
|
||||
NumberValue: function () {
|
||||
return parsimmon
|
||||
.regexp(/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.map(Number)
|
||||
.desc("number");
|
||||
},
|
||||
BooleanValue: function () {
|
||||
return parsimmon
|
||||
.alt(
|
||||
parsimmon.regexp(/true/i).result(true).desc("TRUE"),
|
||||
parsimmon.regexp(/false/i).result(false).desc("FALSE"),
|
||||
)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace);
|
||||
},
|
||||
NullValue: function () {
|
||||
return parsimmon
|
||||
.regexp(/null/i)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.result(null)
|
||||
.desc("NULL");
|
||||
},
|
||||
FuncValue: function (r) {
|
||||
return parsimmon.seqMap(
|
||||
parsimmon
|
||||
.regexp(/([a-zA-Z0-9_]+)/, 1)
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("function"),
|
||||
r.ExpressionList.wrap(
|
||||
parsimmon.string("(").skip(parsimmon.optWhitespace),
|
||||
parsimmon.string(")").skip(parsimmon.optWhitespace),
|
||||
),
|
||||
(f, args) => ["FUNC", f.toUpperCase()].concat(args),
|
||||
);
|
||||
},
|
||||
WhenPair: function (r) {
|
||||
return parsimmon.seq(
|
||||
parsimmon
|
||||
.regexp(/when/i)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("WHEN")
|
||||
.then(r.Expression),
|
||||
parsimmon
|
||||
.regexp(/then/i)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("THEN")
|
||||
.then(r.Expression),
|
||||
);
|
||||
},
|
||||
CaseStatement: function (r) {
|
||||
return parsimmon.seqMap(
|
||||
parsimmon
|
||||
.regexp(/case/i)
|
||||
.result("CASE")
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("CASE"),
|
||||
r.WhenPair.many(),
|
||||
parsimmon
|
||||
.regexp(/else/i)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/))
|
||||
.skip(parsimmon.optWhitespace)
|
||||
.desc("ELSE")
|
||||
.then(r.Expression)
|
||||
.map((e) => [[true, e]])
|
||||
.fallback(null)
|
||||
.skip(
|
||||
parsimmon
|
||||
.regex(/end/i)
|
||||
.notFollowedBy(parsimmon.regexp(/[a-zA-Z0-9_]/)),
|
||||
)
|
||||
.skip(parsimmon.optWhitespace),
|
||||
(...arr) => arr.flat(2),
|
||||
);
|
||||
},
|
||||
Value: function (r) {
|
||||
return parsimmon.alt(
|
||||
r.NullValue,
|
||||
r.BooleanValue,
|
||||
r.NumberValue,
|
||||
r.StringValueSql,
|
||||
r.StringValueJs,
|
||||
r.FuncValue,
|
||||
r.CaseStatement,
|
||||
);
|
||||
},
|
||||
ValueExpression: function (r) {
|
||||
return binaryLeft(
|
||||
parsimmon.string("||").skip(parsimmon.optWhitespace),
|
||||
binaryLeft(
|
||||
parsimmon
|
||||
.alt(parsimmon.string("+"), parsimmon.string("-"))
|
||||
.skip(parsimmon.optWhitespace),
|
||||
binaryLeft(
|
||||
parsimmon
|
||||
.alt(
|
||||
parsimmon.string("*"),
|
||||
parsimmon.string("/"),
|
||||
parsimmon.string("%"),
|
||||
)
|
||||
.skip(parsimmon.optWhitespace),
|
||||
parsimmon.alt(
|
||||
r.Value,
|
||||
r.Parameter,
|
||||
r.Expression.wrap(
|
||||
parsimmon.string("(").skip(parsimmon.optWhitespace),
|
||||
parsimmon.string(")").skip(parsimmon.optWhitespace),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
Comparison: function (r) {
|
||||
return parsimmon.alt(
|
||||
parsimmon.seqMap(r.ValueExpression, r.IsNullOperator, (p, o) => [o, p]),
|
||||
parsimmon.seqMap(
|
||||
r.ValueExpression,
|
||||
r.ComparisonOperator,
|
||||
r.ValueExpression,
|
||||
(p, o, v) => [o, p, v],
|
||||
),
|
||||
parsimmon.seqMap(
|
||||
r.ValueExpression,
|
||||
r.LikeOperator,
|
||||
r.ValueExpression.skip(
|
||||
parsimmon
|
||||
.regexp(/escape/i)
|
||||
.result("ESCAPE")
|
||||
.skip(parsimmon.whitespace)
|
||||
.desc("ESCAPE"),
|
||||
),
|
||||
r.ValueExpression,
|
||||
(a, b, c, d) => [b, a, c, d],
|
||||
),
|
||||
parsimmon.seqMap(
|
||||
r.ValueExpression,
|
||||
r.LikeOperator,
|
||||
r.ValueExpression,
|
||||
(a, b, c) => [b, a, c],
|
||||
),
|
||||
);
|
||||
},
|
||||
ExpressionList: function (r) {
|
||||
return r.Expression.sepBy(
|
||||
parsimmon.string(",").skip(parsimmon.optWhitespace),
|
||||
);
|
||||
},
|
||||
Expression: function (r) {
|
||||
function unary(
|
||||
operatorsParser: parsimmon.Parser<string>,
|
||||
nextParser: parsimmon.Parser<Expression>,
|
||||
): parsimmon.Parser<Expression> {
|
||||
return parsimmon.seq(operatorsParser, nextParser).or(nextParser);
|
||||
if (char === CHAR_COLON) {
|
||||
if (colon) throw new Error("Multiple colons");
|
||||
colon = segments.length + 1;
|
||||
}
|
||||
|
||||
return binaryLeft(
|
||||
r.OrOperator,
|
||||
binaryLeft(
|
||||
r.AndOperator,
|
||||
unary(r.NotOperator, r.Comparison.or(r.ValueExpression)),
|
||||
),
|
||||
).trim(parsimmon.optWhitespace);
|
||||
},
|
||||
});
|
||||
if (exp) segments.push(exp);
|
||||
else segments.push(cur.read(cur2));
|
||||
|
||||
export function parse(str: string): Expression {
|
||||
if (!str) return null;
|
||||
return lang.Expression.tryParse(str);
|
||||
if (char !== CHAR_PERIOD && char !== CHAR_COLON) break;
|
||||
cur2.step();
|
||||
cur.sync(cur2);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (exp) throw new Error("Invalid path");
|
||||
cur2.step();
|
||||
}
|
||||
cur.sync(cur2);
|
||||
|
||||
if (colon) colon = segments.length - colon;
|
||||
return new Path(segments, colon);
|
||||
}
|
||||
|
||||
export function parseList(str: string): Expression[] {
|
||||
if (!str) return [];
|
||||
return lang.ExpressionList.tryParse(str);
|
||||
}
|
||||
|
||||
export function stringify(exp: Expression, level = 0): string {
|
||||
if (!Array.isArray(exp)) return JSON.stringify(exp);
|
||||
|
||||
const opLevels = {
|
||||
OR: 10,
|
||||
AND: 11,
|
||||
NOT: 12,
|
||||
"=": 20,
|
||||
"<>": 20,
|
||||
">": 20,
|
||||
">=": 20,
|
||||
"<": 20,
|
||||
"<=": 20,
|
||||
LIKE: 20,
|
||||
"NOT LIKE": 20,
|
||||
"IS NULL": 20,
|
||||
"IS NOT NULL": 20,
|
||||
"||": 30,
|
||||
"+": 31,
|
||||
"-": 31,
|
||||
"*": 32,
|
||||
"/": 32,
|
||||
"%": 32,
|
||||
};
|
||||
|
||||
const op = exp[0].toUpperCase();
|
||||
|
||||
function wrap(e): string {
|
||||
if (opLevels[op] <= level) return `(${e})`;
|
||||
export function stringifyExpression(exp: Expression, level = 0): string {
|
||||
function wrap(e: string, op: string): string {
|
||||
if (PRECEDENCE[op] <= level) return `(${e})`;
|
||||
else return e;
|
||||
}
|
||||
|
||||
if (op === "FUNC") {
|
||||
if (exp instanceof Expression.Literal) {
|
||||
if (exp.value == null) return "NULL";
|
||||
if (exp.value === true) return "TRUE";
|
||||
if (exp.value === false) return "FALSE";
|
||||
return JSON.stringify(exp.value);
|
||||
} else if (exp instanceof Expression.Unary) {
|
||||
if (exp.operator === "NOT") {
|
||||
return wrap(
|
||||
`${exp[1]}(${exp
|
||||
.slice(2)
|
||||
.map((e) => stringify(e))
|
||||
.join(", ")})`,
|
||||
`NOT ${stringifyExpression(exp.operand, PRECEDENCE[exp.operator])}`,
|
||||
"NOT",
|
||||
);
|
||||
} else if (op === "PARAM") {
|
||||
if (typeof exp[1] === "string") {
|
||||
return wrap(exp[1]);
|
||||
} else if (Array.isArray(exp[1]) && exp[1][0] === "||") {
|
||||
} else if (exp.operator === "IS NULL" || exp.operator === "IS NOT NULL") {
|
||||
return wrap(
|
||||
exp[1]
|
||||
.slice(1)
|
||||
.map((p) => {
|
||||
if (typeof p === "string") return p;
|
||||
else return `{${stringify(p)}}`;
|
||||
})
|
||||
.join(""),
|
||||
);
|
||||
} else {
|
||||
return wrap(`{${stringify(exp[1])}}`);
|
||||
}
|
||||
} else if (op === "IS NULL" || op === "IS NOT NULL") {
|
||||
return wrap(`${stringify(exp[1], opLevels[op])} ${op}`);
|
||||
} else if (op === "LIKE" || op === "NOT LIKE") {
|
||||
if (exp[3]) {
|
||||
return wrap(
|
||||
`${stringify(exp[1], opLevels[op])} ${op} ${stringify(
|
||||
exp[2],
|
||||
opLevels[op],
|
||||
)} ESCAPE ${stringify(exp[3], opLevels[op])}`,
|
||||
);
|
||||
} else {
|
||||
return wrap(
|
||||
`${stringify(exp[1], opLevels[op])} ${op} ${stringify(
|
||||
exp[2],
|
||||
opLevels[op],
|
||||
)}`,
|
||||
`${stringifyExpression(exp.operand, PRECEDENCE[exp.operator])} ${exp.operator}`,
|
||||
exp.operator,
|
||||
);
|
||||
}
|
||||
} else if (op === "CASE") {
|
||||
const parts: string[] = ["CASE"];
|
||||
for (let i = 1; i < exp.length - 1; i += 2) {
|
||||
if (!Array.isArray(exp[i]) && exp[i]) {
|
||||
if (exp[i + 1] != null) parts.push("ELSE", stringify(exp[i + 1]));
|
||||
break;
|
||||
} else if (exp instanceof Expression.Binary) {
|
||||
const op = exp.operator;
|
||||
if (!(op in PRECEDENCE)) throw new Error("Invalid operator");
|
||||
return wrap(
|
||||
`${stringifyExpression(exp.left, PRECEDENCE[op] - 1)} ${exp.operator} ${stringifyExpression(exp.right, PRECEDENCE[op])}`,
|
||||
op,
|
||||
);
|
||||
} else if (exp instanceof Expression.Parameter) {
|
||||
return exp.path.toString();
|
||||
} else if (exp instanceof Expression.FunctionCall) {
|
||||
return `${exp.name}(${exp.args.map((a) => stringifyExpression(a)).join(", ")})`;
|
||||
} else if (exp instanceof Expression.Conditional) {
|
||||
let str = `CASE WHEN ${stringifyExpression(exp.condition)} THEN ${stringifyExpression(exp.then)}`;
|
||||
if (
|
||||
exp.otherwise instanceof Expression.Literal &&
|
||||
exp.otherwise.value == null
|
||||
)
|
||||
str += " END";
|
||||
else if (exp.otherwise instanceof Expression.Conditional)
|
||||
str += stringifyExpression(exp.otherwise).slice(4);
|
||||
else str += ` ELSE ${stringifyExpression(exp.otherwise)} END`;
|
||||
return str;
|
||||
}
|
||||
parts.push("WHEN", stringify(exp[i]), "THEN", stringify(exp[i + 1]));
|
||||
}
|
||||
parts.push("END");
|
||||
return parts.join(" ");
|
||||
} else if (op in opLevels) {
|
||||
const parts = exp.slice(1).map((e, i) => {
|
||||
return stringify(e, opLevels[exp[0]] + Math.min(i - 1, 0));
|
||||
});
|
||||
|
||||
if (op === "NOT") return wrap(`${op} ${parts[0]}`);
|
||||
else return wrap(parts.join(` ${op} `));
|
||||
} else {
|
||||
throw new Error(`Unrecognized operator ${exp[0]}`);
|
||||
}
|
||||
throw new Error("Invalid expression");
|
||||
}
|
||||
|
||||
export function parseLikePattern(pat: string, esc: string): string[] {
|
||||
@@ -457,3 +525,33 @@ export function parseLikePattern(pat: string, esc: string): string[] {
|
||||
}
|
||||
return chars.filter((c) => c);
|
||||
}
|
||||
|
||||
export function likePatternToRegExp(pat: string, 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);
|
||||
}
|
||||
+445
-370
File diff suppressed because it is too large.
Load diff
@@ -1,393 +0,0 @@
|
||||
import { map, mapAsync, parseLikePattern } from "./parser.ts";
|
||||
import { Expression } from "../../types.ts";
|
||||
|
||||
const isArray = Array.isArray;
|
||||
|
||||
const regExpCache = new WeakMap<any, RegExp>();
|
||||
|
||||
const REDUCE_SKIP = {};
|
||||
function reduce(exp, callback): Expression {
|
||||
let loop = true;
|
||||
while (loop) {
|
||||
loop = false;
|
||||
for (let i = 2; i < exp.length; ++i) {
|
||||
const res = callback(exp[i - 1], exp[i], i - 2);
|
||||
if (res !== REDUCE_SKIP) {
|
||||
loop = true;
|
||||
exp = exp.slice();
|
||||
exp.splice(i - 1, 2, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (exp.length === 2) return exp[1];
|
||||
return exp;
|
||||
}
|
||||
|
||||
export function likePatternToRegExp(pat: string, 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);
|
||||
}
|
||||
|
||||
function compare(
|
||||
a: boolean | number | string,
|
||||
b: boolean | number | string,
|
||||
): number {
|
||||
if (typeof a === "boolean") a = +a;
|
||||
if (typeof b === "boolean") b = +b;
|
||||
if (typeof a !== typeof b) return typeof a === "string" ? 1 : -1;
|
||||
return a > b ? 1 : a < b ? -1 : 0;
|
||||
}
|
||||
|
||||
function toNumber(a: boolean | number | string): number {
|
||||
switch (typeof a) {
|
||||
case "number":
|
||||
return a;
|
||||
case "boolean":
|
||||
return +a;
|
||||
case "string":
|
||||
return parseFloat(a) || 0;
|
||||
}
|
||||
}
|
||||
|
||||
function toString(a: boolean | number | string): string {
|
||||
switch (typeof a) {
|
||||
case "string":
|
||||
return a;
|
||||
case "number":
|
||||
return a.toString();
|
||||
case "boolean":
|
||||
return (+a).toString();
|
||||
}
|
||||
}
|
||||
|
||||
export function evaluateCallback(exp: Expression): Expression {
|
||||
if (!Array.isArray(exp)) return exp;
|
||||
if (exp[0] === "CASE") {
|
||||
for (let i = 1; i < exp.length; i += 2) {
|
||||
if (Array.isArray(exp[i])) return exp;
|
||||
if (exp[i]) return exp[i + 1];
|
||||
}
|
||||
return null;
|
||||
} else if (exp[0] === "FUNC") {
|
||||
if (exp[1] === "COALESCE") {
|
||||
const args: Expression[] = [];
|
||||
for (let i = 2; i < exp.length; ++i) {
|
||||
const e = exp[i];
|
||||
if (e == null) continue;
|
||||
args.push(e);
|
||||
if (!Array.isArray(e)) break;
|
||||
}
|
||||
if (!args.length) return null;
|
||||
if (args.length === 1) return args[0];
|
||||
return ["FUNC", "COALESCE", ...args];
|
||||
} else if (exp[1] === "UPPER") {
|
||||
if (exp[2] == null) return null;
|
||||
if (!isArray(exp[2])) return toString(exp[2]).toUpperCase();
|
||||
} else if (exp[1] === "LOWER") {
|
||||
if (exp[2] == null) return null;
|
||||
if (!isArray(exp[2])) return toString(exp[2]).toLowerCase();
|
||||
} else if (exp[1] === "ROUND") {
|
||||
const n = exp[2];
|
||||
const p = exp.length > 3 ? exp[3] : 0;
|
||||
if (n == null || p == null) return null;
|
||||
if (!isArray(n) && !isArray(p)) {
|
||||
const d = 10 ** p;
|
||||
const m = n * d * (1 + Number.EPSILON);
|
||||
return Math.round(m) / d;
|
||||
}
|
||||
}
|
||||
} else if (exp[0] === "PARAM") {
|
||||
if (exp[1] == null) return null;
|
||||
} else if (exp[0] === "AND") {
|
||||
for (let i = 1; i < exp.length; ++i)
|
||||
if (!Array.isArray(exp[i]) && exp[i] != null && !exp[i]) return false;
|
||||
const args: Expression[] = [];
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const ee = exp[i];
|
||||
if (ee == null) return null;
|
||||
if (Array.isArray(ee)) {
|
||||
if (ee[0] === "AND") args.push(...ee.slice(1));
|
||||
else args.push(ee);
|
||||
}
|
||||
}
|
||||
if (!args.length) return true;
|
||||
if (args.length === 1) args.push(true);
|
||||
return ["AND", ...args];
|
||||
} else if (exp[0] === "OR") {
|
||||
const args: Expression[] = [];
|
||||
for (let i = 1; i < exp.length; ++i) {
|
||||
const ee = exp[i];
|
||||
if (Array.isArray(ee)) {
|
||||
if (ee[0] === "OR") args.push(...ee.slice(1));
|
||||
else args.push(ee);
|
||||
} else if (ee) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!args.length) return exp.some((ee) => ee == null) ? null : false;
|
||||
if (args.length === 1) args.push(false);
|
||||
return ["OR", ...args];
|
||||
} else if (exp[0] === "NOT") {
|
||||
if (exp[1] == null) return null;
|
||||
if (!isArray(exp[1])) return !exp[1];
|
||||
else if (exp[1][0] === "NOT") return exp[1][1];
|
||||
} else if (exp[0] === "IS NULL") {
|
||||
if (isArray(exp[1])) return exp;
|
||||
else if (exp[1] == null) return true;
|
||||
else return false;
|
||||
} else if (exp[0] === "IS NOT NULL") {
|
||||
if (isArray(exp[1])) return exp;
|
||||
else if (exp[1] != null) return true;
|
||||
else return false;
|
||||
} else if (exp[0] === "LIKE") {
|
||||
if (isArray(exp[1]) || isArray(exp[2]) || isArray(exp[3])) return exp;
|
||||
else if (
|
||||
exp[1] == null ||
|
||||
exp[2] == null ||
|
||||
((exp as any[]).length >= 4 && exp[3] == null)
|
||||
)
|
||||
return null;
|
||||
let r = regExpCache.get(exp as any[]);
|
||||
if (!r) {
|
||||
r = likePatternToRegExp(exp[2], exp[3]);
|
||||
regExpCache.set(exp as any[], r);
|
||||
}
|
||||
return r.test(exp[1]);
|
||||
} else if (exp[0] === "NOT LIKE") {
|
||||
if (isArray(exp[1]) || isArray(exp[2]) || isArray(exp[3])) return exp;
|
||||
else if (
|
||||
exp[1] == null ||
|
||||
exp[2] == null ||
|
||||
((exp as any[]).length >= 4 && exp[3] == null)
|
||||
)
|
||||
return null;
|
||||
let r = regExpCache.get(exp as any[]);
|
||||
if (!r) {
|
||||
r = likePatternToRegExp(exp[2], exp[3]);
|
||||
regExpCache.set(exp as any[], r);
|
||||
}
|
||||
return !r.test(exp[1]);
|
||||
} else if (exp[0] === "=") {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
if (isArray(exp[1]) || isArray(exp[2])) return exp;
|
||||
return compare(exp[1], exp[2]) === 0;
|
||||
} else if (exp[0] === "<>") {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
if (isArray(exp[1]) || isArray(exp[2])) return exp;
|
||||
return compare(exp[1], exp[2]) !== 0;
|
||||
} else if (exp[0] === ">") {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
if (isArray(exp[1]) || isArray(exp[2])) return exp;
|
||||
return compare(exp[1], exp[2]) > 0;
|
||||
} else if (exp[0] === ">=") {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
if (isArray(exp[1]) || isArray(exp[2])) return exp;
|
||||
return compare(exp[1], exp[2]) >= 0;
|
||||
} else if (exp[0] === "<") {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
if (isArray(exp[1]) || isArray(exp[2])) return exp;
|
||||
return compare(exp[1], exp[2]) < 0;
|
||||
} else if (exp[0] === "<=") {
|
||||
if (exp[1] == null || exp[2] == null) return null;
|
||||
if (isArray(exp[1]) || isArray(exp[2])) return exp;
|
||||
return compare(exp[1], exp[2]) <= 0;
|
||||
} else if (exp[0] === "*") {
|
||||
return reduce(exp, (a, b) => {
|
||||
if (a == null || b == null) return null;
|
||||
if (!isArray(a) && !isArray(b)) return toNumber(a) * toNumber(b);
|
||||
return REDUCE_SKIP;
|
||||
});
|
||||
} else if (exp[0] === "/") {
|
||||
return reduce(exp, (a, b, i) => {
|
||||
if (a == null || b == null) return null;
|
||||
if (isArray(a) || isArray(b)) return REDUCE_SKIP;
|
||||
const n1 = toNumber(a);
|
||||
const n2 = toNumber(b);
|
||||
if (i !== 0) return n1 * n2;
|
||||
if (n2 === 0) return null;
|
||||
return n1 / n2;
|
||||
});
|
||||
} else if (exp[0] === "+") {
|
||||
return reduce(exp, (a, b) => {
|
||||
if (a == null || b == null) return null;
|
||||
if (!isArray(a) && !isArray(b)) return toNumber(a) + toNumber(b);
|
||||
return REDUCE_SKIP;
|
||||
});
|
||||
} else if (exp[0] === "-") {
|
||||
return reduce(exp, (a, b, i) => {
|
||||
if (a == null || b == null) return null;
|
||||
if (!isArray(a) && !isArray(b))
|
||||
return i === 0 ? toNumber(a) - toNumber(b) : toNumber(a) + toNumber(b);
|
||||
return REDUCE_SKIP;
|
||||
});
|
||||
} else if (exp[0] === "%") {
|
||||
return reduce(exp, (a, b, i) => {
|
||||
if (a == null || b == null) return null;
|
||||
if (isArray(a) || isArray(b) || i !== 0) return REDUCE_SKIP;
|
||||
const n1 = toNumber(a);
|
||||
const n2 = Math.trunc(toNumber(b));
|
||||
if (n2 === 0) return null;
|
||||
return n1 % n2;
|
||||
});
|
||||
} else if (exp[0] === "||") {
|
||||
return reduce(exp, (a, b) => {
|
||||
if (a == null || b == null) return null;
|
||||
if (!isArray(a) && !isArray(b)) return toString(a) + toString(b);
|
||||
return REDUCE_SKIP;
|
||||
});
|
||||
}
|
||||
return exp;
|
||||
}
|
||||
|
||||
export function evaluate(
|
||||
exp: Expression,
|
||||
obj: Record<string, unknown> | ((e: string) => Expression),
|
||||
now: number,
|
||||
cb?: (e: Expression) => Expression,
|
||||
): string | number | boolean | null;
|
||||
export function evaluate(
|
||||
exp: Expression,
|
||||
obj?: Record<string, unknown> | ((e: string) => Expression),
|
||||
now?: number,
|
||||
cb?: (e: Expression) => Expression,
|
||||
): Expression;
|
||||
export function evaluate(
|
||||
exp: Expression,
|
||||
obj?: Record<string, unknown> | ((e: string) => Expression),
|
||||
now?: number,
|
||||
cb?: (e: Expression) => Expression,
|
||||
): Expression {
|
||||
return map(exp, (e) => {
|
||||
if (cb) e = cb(e);
|
||||
if (!isArray(e)) return e;
|
||||
|
||||
if (e[0] === "FUNC" && e[1] === "NOW") {
|
||||
if (now) return now;
|
||||
} else if (e[0] === "PARAM") {
|
||||
if (e[1] == null) return null;
|
||||
if (obj && !isArray(e[1])) {
|
||||
let v;
|
||||
if (typeof obj === "function") v = obj(e[1]);
|
||||
else v = obj[e[1]];
|
||||
|
||||
if (v == null) return null;
|
||||
if (typeof v === "object") v = v.value ? v.value[0] : null;
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return evaluateCallback(e);
|
||||
});
|
||||
}
|
||||
|
||||
export async function evaluateAsync(
|
||||
exp: Expression,
|
||||
obj: Record<string, unknown>,
|
||||
now: number,
|
||||
cb?: (e: Expression) => Promise<Expression>,
|
||||
): Promise<string | number | boolean | null>;
|
||||
export async function evaluateAsync(
|
||||
exp: Expression,
|
||||
obj?: Record<string, unknown>,
|
||||
now?: number,
|
||||
cb?: (e: Expression) => Promise<Expression>,
|
||||
): Promise<Expression>;
|
||||
export async function evaluateAsync(
|
||||
exp: Expression,
|
||||
obj?: Record<string, unknown>,
|
||||
now?: number,
|
||||
cb?: (e: Expression) => Promise<Expression>,
|
||||
): Promise<Expression> {
|
||||
return mapAsync(exp, async (e) => {
|
||||
if (cb) e = await cb(e);
|
||||
if (!isArray(e)) return e;
|
||||
|
||||
if (e[0] === "FUNC") {
|
||||
if (e[1] === "NOW") if (now) return now;
|
||||
} else if (e[0] === "PARAM") {
|
||||
if (e[1] == null) return null;
|
||||
if (obj && !isArray(e[1])) {
|
||||
let v = obj[e[1]];
|
||||
if (v == null) return null;
|
||||
if (typeof v === "object") v = v["value"] ? v["value"][0] : null;
|
||||
return v as Expression;
|
||||
}
|
||||
}
|
||||
return evaluateCallback(e);
|
||||
});
|
||||
}
|
||||
|
||||
export function and(exp1: Expression, exp2: Expression): Expression {
|
||||
if (exp1 != null && !exp1) return false;
|
||||
if (exp2 != null && !exp2) return false;
|
||||
if (!Array.isArray(exp1) && !Array.isArray(exp2)) {
|
||||
if (exp1 == null || exp2 == null) return null;
|
||||
return true;
|
||||
}
|
||||
if (!Array.isArray(exp2) && exp1[0] === "AND") return exp1;
|
||||
if (!Array.isArray(exp1) && exp2[0] === "AND") return exp2;
|
||||
|
||||
const res: Expression = ["AND"];
|
||||
|
||||
if (Array.isArray(exp1) && exp1[0] === "AND") res.push(...exp1.slice(1));
|
||||
else res.push(exp1);
|
||||
|
||||
if (Array.isArray(exp2) && exp2[0] === "AND") res.push(...exp2.slice(1));
|
||||
else res.push(exp2);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function or(exp1: Expression, exp2: Expression): Expression {
|
||||
if (!Array.isArray(exp1) && exp1) return true;
|
||||
if (!Array.isArray(exp2) && exp2) return true;
|
||||
if (!Array.isArray(exp1) && !Array.isArray(exp2)) {
|
||||
if (exp1 == null || exp2 == null) return null;
|
||||
return false;
|
||||
}
|
||||
if (!Array.isArray(exp2) && exp1[0] === "OR") return exp1;
|
||||
if (!Array.isArray(exp1) && exp2[0] === "OR") return exp2;
|
||||
|
||||
const res: Expression = ["OR"];
|
||||
|
||||
if (Array.isArray(exp1) && exp1[0] === "OR") res.push(...exp1.slice(1));
|
||||
else res.push(exp1);
|
||||
|
||||
if (Array.isArray(exp2) && exp2[0] === "OR") res.push(...exp2.slice(1));
|
||||
else res.push(exp2);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function extractParams(exp: Expression): Expression[] {
|
||||
const params = new Set<Expression>();
|
||||
map(exp, (e) => {
|
||||
if (isArray(e) && e[0] === "PARAM") params.add(e[1]);
|
||||
return e;
|
||||
});
|
||||
return Array.from(params);
|
||||
}
|
||||
+67
-63
@@ -1,40 +1,32 @@
|
||||
import Path from "./path.ts";
|
||||
|
||||
export default class PathSet {
|
||||
private declare lengthIndex: Set<Path>[];
|
||||
private declare fragmentIndex: Map<string, Set<Path>>[];
|
||||
private declare stringIndex: Map<string, Path>;
|
||||
private paramSegmentIndex: Map<string, Set<Path>>[] = [];
|
||||
private attrSegmentIndex: Map<string, Set<Path>>[] = [];
|
||||
private stringIndex: Map<string, Path> = new Map();
|
||||
|
||||
public constructor() {
|
||||
this.lengthIndex = [];
|
||||
this.fragmentIndex = [];
|
||||
this.stringIndex = new Map();
|
||||
}
|
||||
public constructor() {}
|
||||
|
||||
public get depth(): number {
|
||||
return this.lengthIndex.length;
|
||||
}
|
||||
|
||||
public add(path: Path): Path {
|
||||
public add(pathStr: string): Path {
|
||||
let path: Path = this.get(pathStr);
|
||||
if (path) return path;
|
||||
path = Path.parse(pathStr);
|
||||
if (path.alias) throw new Error("PathSet does not support aliased paths");
|
||||
const p = this.get(path);
|
||||
|
||||
if (p) return p;
|
||||
|
||||
this.stringIndex.set(path.toString(), path);
|
||||
|
||||
while (this.lengthIndex.length <= path.length) {
|
||||
this.lengthIndex.push(new Set());
|
||||
// fragmentIndex is one less than lengthIndex
|
||||
if (this.lengthIndex.length > 1) this.fragmentIndex.push(new Map());
|
||||
}
|
||||
while (this.paramSegmentIndex.length < path.paramLength)
|
||||
this.paramSegmentIndex.push(new Map());
|
||||
|
||||
const lengthIndex = this.lengthIndex[path.length];
|
||||
lengthIndex.add(path);
|
||||
while (this.attrSegmentIndex.length < path.attrLength)
|
||||
this.attrSegmentIndex.push(new Map());
|
||||
|
||||
for (let i = 0; i < path.length; ++i) {
|
||||
const fragment = path.segments[i] as string;
|
||||
const fragmentIndex = this.fragmentIndex[i];
|
||||
const fragmentIndex =
|
||||
i < path.paramLength
|
||||
? this.paramSegmentIndex[i]
|
||||
: this.attrSegmentIndex[i - path.paramLength];
|
||||
|
||||
let fragmentIndexSet = fragmentIndex.get(fragment);
|
||||
if (!fragmentIndexSet) {
|
||||
@@ -48,63 +40,75 @@ export default class PathSet {
|
||||
return path;
|
||||
}
|
||||
|
||||
public get(path: Path): Path {
|
||||
return this.stringIndex.get(path.toString()) || null;
|
||||
public get(path: string): Path {
|
||||
return this.stringIndex.get(path);
|
||||
}
|
||||
|
||||
public find(
|
||||
public findCompat(
|
||||
path: Path,
|
||||
superset = false,
|
||||
subset = false,
|
||||
depth: number = path.length,
|
||||
depth = path.length,
|
||||
): Path[] {
|
||||
if (path.attrLength)
|
||||
throw new Error("findCompat() does not support attribute paths");
|
||||
|
||||
depth = Math.min(31, depth);
|
||||
const paramMask = (1 << path.paramLength) - 1;
|
||||
let mask = ((0b10 << depth) - 1) & ~paramMask;
|
||||
if (superset) mask |= ~path.wildcard & paramMask;
|
||||
if (subset) mask |= path.wildcard;
|
||||
return this.find(path, mask, 1);
|
||||
}
|
||||
|
||||
public find(path: Path, paramMask: number, attrMask: number): Path[] {
|
||||
if (path.alias) throw new Error("PathSet does not support aliased paths");
|
||||
if (path.paramLength > this.paramSegmentIndex.length) return [];
|
||||
if (path.attrLength > this.attrSegmentIndex.length) return [];
|
||||
|
||||
const len = path.length;
|
||||
const emptySet: Set<Path> = new Set();
|
||||
|
||||
if (!superset && depth === len && (!subset || !path.wildcard)) {
|
||||
const p = this.get(path);
|
||||
return p ? [p] : [];
|
||||
const indexes: [Set<Path>, Set<Path>][] = [];
|
||||
|
||||
const paramLengthMask = (1 << path.paramLength) - 1;
|
||||
const attrLengthMask = (1 << path.attrLength) - 1;
|
||||
|
||||
const mask = (paramMask & paramLengthMask) | (attrMask << path.paramLength);
|
||||
|
||||
for (const [i, s] of path.segments.entries()) {
|
||||
const b = 1 << i;
|
||||
const m = mask & b;
|
||||
const w = b & path.wildcard;
|
||||
if (w && m) continue;
|
||||
const idxSet =
|
||||
i < path.paramLength
|
||||
? this.paramSegmentIndex[i]
|
||||
: this.attrSegmentIndex[i - path.paramLength];
|
||||
const idx1 = idxSet.get(s as string) || emptySet;
|
||||
let idx2 = emptySet;
|
||||
if (m) idx2 = idxSet.get("*") || emptySet;
|
||||
indexes.push([idx1, idx2]);
|
||||
}
|
||||
|
||||
const lengthIndex = this.lengthIndex.slice(len, depth + 1);
|
||||
if (!lengthIndex.length) return [];
|
||||
indexes.sort((a, b) => a[0].size + a[1].size - (b[0].size + b[1].size));
|
||||
|
||||
let res;
|
||||
for (let i = len - 1; i >= 0; --i) {
|
||||
let fragmentIndexSet2;
|
||||
const fragmentIndex = this.fragmentIndex[i];
|
||||
let res: Path[];
|
||||
|
||||
if ((path.wildcard >> i) & 1) {
|
||||
if (subset) continue;
|
||||
} else if (superset) {
|
||||
fragmentIndexSet2 = fragmentIndex.get("*");
|
||||
}
|
||||
if (!indexes.length) res = [...this.stringIndex.values()];
|
||||
else res = [...indexes[0][0], ...indexes[0][1]];
|
||||
|
||||
const fragment = path.segments[i] as string;
|
||||
const fragmentIndexSet1 = fragmentIndex.get(fragment);
|
||||
const paramCover = ~paramLengthMask & paramMask;
|
||||
const attrCover = ~attrLengthMask & attrMask;
|
||||
|
||||
if (!fragmentIndexSet1) {
|
||||
if (!fragmentIndexSet2) return [];
|
||||
if (!res) res = [...fragmentIndexSet2];
|
||||
else res = res.filter((r) => fragmentIndexSet2.has(r));
|
||||
} else if (!fragmentIndexSet2) {
|
||||
if (!res) res = [...fragmentIndexSet1];
|
||||
else res = res.filter((r) => fragmentIndexSet1.has(r));
|
||||
} else {
|
||||
if (!res) {
|
||||
res = [...fragmentIndexSet1, ...fragmentIndexSet2];
|
||||
} else {
|
||||
res = res.filter(
|
||||
(r) => fragmentIndexSet1.has(r) || fragmentIndexSet2.has(r),
|
||||
(p) =>
|
||||
(1 << p.paramLength) & paramCover && (1 << p.attrLength) & attrCover,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!res.length) return res;
|
||||
}
|
||||
|
||||
if (!res) res = [].concat(...lengthIndex.map((a) => [...a]));
|
||||
else res = res.filter((r) => lengthIndex.some((a) => a.has(r)));
|
||||
for (let i = 1; i < indexes.length; ++i) {
|
||||
const [idx1, idx2] = indexes[i];
|
||||
res = res.filter((p) => idx1.has(p) || idx2.has(p));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
+76
-171
@@ -1,162 +1,31 @@
|
||||
type Segments = (string | Alias)[];
|
||||
type Alias = [Path, string][];
|
||||
import Expression from "./expression.ts";
|
||||
import { Cursor, parsePath } from "./expression/parser.ts";
|
||||
|
||||
type Segments = (string | Expression)[];
|
||||
|
||||
let cache1 = new Map<string, Path>();
|
||||
let cache2 = new Map<string, Path>();
|
||||
|
||||
const CHAR_SPACE = 32;
|
||||
const CHAR_BACKSLASH = 92;
|
||||
const CHAR_COLON = 58;
|
||||
const CHAR_QUOTE = 34;
|
||||
const CHAR_OPEN_BRACKET = 91;
|
||||
const CHAR_CLOSE_BRACKET = 93;
|
||||
const CHAR_ASTERISK = 42;
|
||||
const CHAR_COMMA = 44;
|
||||
const CHAR_DOT = 46;
|
||||
|
||||
function charCodeAt(str: string, index: number): number {
|
||||
if (index >= str.length) return 0;
|
||||
return str.charCodeAt(index);
|
||||
}
|
||||
|
||||
function legalChar(c: number): boolean {
|
||||
return (
|
||||
(c >= 97 && c <= 122) ||
|
||||
(c >= 65 && c <= 90) ||
|
||||
(c >= 48 && c <= 57) ||
|
||||
c === 95 ||
|
||||
c === 45
|
||||
);
|
||||
}
|
||||
|
||||
export default class Path {
|
||||
public declare readonly segments: Segments;
|
||||
public declare readonly wildcard: number;
|
||||
public declare readonly alias: number;
|
||||
protected declare _string: string;
|
||||
protected declare _stringIndex: number[];
|
||||
declare public readonly segments: Segments;
|
||||
declare public readonly colon: number;
|
||||
declare public readonly wildcard: number;
|
||||
declare public readonly alias: number;
|
||||
declare protected _string: string;
|
||||
declare protected _stringIndex: number[];
|
||||
|
||||
protected static parseAliasValue(
|
||||
pattern: string,
|
||||
index: number,
|
||||
): { index: number; value: string } {
|
||||
let i = index;
|
||||
while (charCodeAt(pattern, i) === CHAR_SPACE) ++i;
|
||||
if (charCodeAt(pattern, i) === CHAR_QUOTE) {
|
||||
for (let j = i + 1; j < pattern.length; ++j) {
|
||||
if (
|
||||
pattern.charCodeAt(j) === CHAR_QUOTE &&
|
||||
pattern.charCodeAt(j - 1) !== CHAR_BACKSLASH
|
||||
) {
|
||||
try {
|
||||
++j;
|
||||
const v = JSON.parse(pattern.slice(i, j));
|
||||
return { index: j, value: v };
|
||||
} catch {
|
||||
return { index, value: null };
|
||||
}
|
||||
}
|
||||
}
|
||||
return { index, value: null };
|
||||
}
|
||||
|
||||
for (; i < pattern.length; ++i) {
|
||||
const c = pattern.charCodeAt(i);
|
||||
if (c === CHAR_CLOSE_BRACKET || c === CHAR_COMMA) break;
|
||||
}
|
||||
|
||||
return { index: i, value: pattern.slice(index, i).trim() };
|
||||
}
|
||||
|
||||
protected static parseAliasPath(
|
||||
pattern: string,
|
||||
index: number,
|
||||
): { index: number; path: Path } {
|
||||
let i = index;
|
||||
while (charCodeAt(pattern, i) === CHAR_SPACE) ++i;
|
||||
const { index: idx, segments } = Path.parsePath(pattern, i);
|
||||
if (idx === i) return { index, path: null };
|
||||
i = idx;
|
||||
while (charCodeAt(pattern, i) === CHAR_SPACE) ++i;
|
||||
if (charCodeAt(pattern, i) !== CHAR_COLON) return { index, path: null };
|
||||
return { index: i + 1, path: new Path(segments) };
|
||||
}
|
||||
|
||||
protected static parseAlias(
|
||||
pattern: string,
|
||||
index: number,
|
||||
): { index: number; alias: Alias } {
|
||||
const alias: Alias = [];
|
||||
let i = index;
|
||||
for (;;) {
|
||||
let path: Path;
|
||||
let value: string;
|
||||
({ index: i, path } = Path.parseAliasPath(pattern, i));
|
||||
if (!path) break;
|
||||
({ index: i, value } = Path.parseAliasValue(pattern, i));
|
||||
if (value == null) break;
|
||||
alias.push([path, value]);
|
||||
index = i;
|
||||
|
||||
if (charCodeAt(pattern, index) !== CHAR_COMMA) break;
|
||||
++i;
|
||||
}
|
||||
|
||||
// Ensure identical expressions have idential string representation
|
||||
alias.sort((a, b) => {
|
||||
if (a[0].toString() > b[0].toString()) return 1;
|
||||
else if (a[0].toString() < b[0].toString()) return -1;
|
||||
else if (a[1] > b[1]) return 1;
|
||||
else if (a[1] < b[1]) return -1;
|
||||
else return 0;
|
||||
});
|
||||
|
||||
Object.freeze(alias);
|
||||
return { index, alias };
|
||||
}
|
||||
|
||||
protected static parsePath(
|
||||
pattern: string,
|
||||
index: number,
|
||||
): { index: number; segments: Segments } {
|
||||
const segments = [];
|
||||
|
||||
let segStart = index;
|
||||
let wildcard = -1;
|
||||
for (let i = index; i <= pattern.length; ++i) {
|
||||
const c = charCodeAt(pattern, i);
|
||||
if (c === CHAR_OPEN_BRACKET && i === segStart) {
|
||||
const { index: idx, alias } = Path.parseAlias(pattern, i + 1);
|
||||
if (charCodeAt(pattern, idx) !== CHAR_CLOSE_BRACKET) break;
|
||||
segments.push(alias);
|
||||
index = i = idx + 1;
|
||||
segStart = i + 1;
|
||||
} else if (c === CHAR_ASTERISK && i === segStart) {
|
||||
wildcard = i + 1;
|
||||
} else if (!legalChar(c) || i === wildcard) {
|
||||
if (i === segStart) break;
|
||||
const s = pattern.slice(segStart, i);
|
||||
segments.push(s);
|
||||
index = i;
|
||||
if (c !== CHAR_DOT) break;
|
||||
segStart = i + 1;
|
||||
}
|
||||
}
|
||||
constructor(segments: Segments, colon: number) {
|
||||
if (!(colon <= segments.length)) throw new Error("Invalid path");
|
||||
if (segments.length > 32) throw new Error("Path too long");
|
||||
|
||||
Object.freeze(segments);
|
||||
return { index, segments };
|
||||
}
|
||||
|
||||
protected constructor(segments: Segments) {
|
||||
let alias = 0;
|
||||
let wildcard = 0;
|
||||
const arr = segments.map((s, i) => {
|
||||
if (Array.isArray(s)) {
|
||||
if (s instanceof Expression) {
|
||||
alias |= 1 << i;
|
||||
const parts = s.map(
|
||||
(al) => `${al[0].toString()}:${JSON.stringify(al[1])}`,
|
||||
);
|
||||
return `[${parts.join(",")}]`;
|
||||
return `[${s.toString()}]`;
|
||||
} else if (s === "*") {
|
||||
wildcard |= 1 << i;
|
||||
}
|
||||
@@ -167,23 +36,27 @@ export default class Path {
|
||||
const stringIndex = arr.map((s, i) => (offset += s.length) + i);
|
||||
|
||||
this.segments = segments;
|
||||
this.colon = colon;
|
||||
this.wildcard = wildcard;
|
||||
this.alias = alias;
|
||||
this._string = arr.join(".");
|
||||
if (!colon) this._string = arr.join(".");
|
||||
else
|
||||
this._string =
|
||||
arr.slice(0, -colon).join(".") + ":" + arr.slice(-colon).join(".");
|
||||
this._stringIndex = stringIndex;
|
||||
}
|
||||
|
||||
public static parse(str: string): Path {
|
||||
let path = cache1.get(str);
|
||||
public static parse(input: string): Path {
|
||||
let path = cache1.get(input);
|
||||
if (!path) {
|
||||
path = cache2.get(str);
|
||||
path = cache2.get(input);
|
||||
if (!path) {
|
||||
const { index, segments } = Path.parsePath(str, 0);
|
||||
if (index < str.length) throw new Error("Invalid parameter path");
|
||||
path = new Path(segments);
|
||||
if (path.toString() !== str) cache1.set(path.toString(), path);
|
||||
const cursor = new Cursor(input);
|
||||
path = parsePath(cursor);
|
||||
if (cursor.charCode) throw new Error("Unexpected character");
|
||||
if (path.toString() !== input) cache1.set(path.toString(), path);
|
||||
}
|
||||
cache1.set(str, path);
|
||||
cache1.set(input, path);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
@@ -192,6 +65,14 @@ export default class Path {
|
||||
return this.segments.length;
|
||||
}
|
||||
|
||||
public get paramLength(): number {
|
||||
return this.segments.length - this.colon;
|
||||
}
|
||||
|
||||
public get attrLength(): number {
|
||||
return this.colon;
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return this._string;
|
||||
}
|
||||
@@ -200,25 +81,27 @@ export default class Path {
|
||||
if (start < 0) start = Math.max(0, this.segments.length + start);
|
||||
if (end < 0) end = Math.max(0, this.segments.length + end);
|
||||
|
||||
let str;
|
||||
if (start >= end) {
|
||||
str = "";
|
||||
} else {
|
||||
const i1 = start > 0 ? this._stringIndex[start - 1] + 1 : 0;
|
||||
if (start >= end) return Path.root;
|
||||
|
||||
let i1 = start > 0 ? this._stringIndex[start - 1] + 1 : 0;
|
||||
// Include the ":" when slicing exactly at the colon boundary
|
||||
if (this.colon && start === this.segments.length - this.colon) --i1;
|
||||
const i2 =
|
||||
end <= this.segments.length
|
||||
? this._stringIndex[end - 1]
|
||||
: this._string.length;
|
||||
str = this._string.slice(i1, i2);
|
||||
}
|
||||
const str = this._string.slice(i1, i2);
|
||||
|
||||
let path = cache1.get(str);
|
||||
if (!path) {
|
||||
path = cache2.get(str);
|
||||
if (!path) {
|
||||
const segments = this.segments.slice(start, end);
|
||||
Object.freeze(segments);
|
||||
path = new Path(segments);
|
||||
const colon =
|
||||
start <= this.segments.length - this.colon
|
||||
? Math.max(0, this.colon - this.segments.length + end)
|
||||
: 0;
|
||||
path = new Path(segments, colon);
|
||||
}
|
||||
cache1.set(str, path);
|
||||
}
|
||||
@@ -230,15 +113,28 @@ export default class Path {
|
||||
if (!path2._string) return this;
|
||||
else if (!this._string) return path2;
|
||||
|
||||
const str = `${this._string}.${path2._string}`;
|
||||
if (this.colon && path2.colon && path2.colon < path2.segments.length)
|
||||
throw new Error("Invalid path");
|
||||
|
||||
const colon = this.colon ? this.colon + path2.segments.length : path2.colon;
|
||||
|
||||
let str;
|
||||
if (this.colon && path2.colon === path2.segments.length) {
|
||||
// Right is all-colon; strip its ":" prefix and join with "."
|
||||
str = `${this._string}.${path2._string.slice(1)}`;
|
||||
} else if (path2.colon === path2.segments.length) {
|
||||
// Left has no colon; right is all-colon; concatenate directly
|
||||
str = `${this._string}${path2._string}`;
|
||||
} else {
|
||||
str = `${this._string}.${path2._string}`;
|
||||
}
|
||||
|
||||
let path = cache1.get(str);
|
||||
if (!path) {
|
||||
path = cache2.get(str);
|
||||
if (!path) {
|
||||
const segments = this.segments.concat(path2.segments);
|
||||
Object.freeze(segments);
|
||||
path = new Path(segments);
|
||||
path = new Path(segments, colon);
|
||||
}
|
||||
cache1.set(str, path);
|
||||
}
|
||||
@@ -248,21 +144,30 @@ export default class Path {
|
||||
|
||||
public stripAlias(): Path {
|
||||
if (!this.alias) return this;
|
||||
const segments = this.segments.map((s) => (Array.isArray(s) ? "*" : s));
|
||||
const str = segments.join(".");
|
||||
const segments = this.segments.map((s) =>
|
||||
s instanceof Expression ? "*" : s,
|
||||
);
|
||||
let str: string;
|
||||
if (!this.colon) str = segments.join(".");
|
||||
else
|
||||
str =
|
||||
segments.slice(0, -this.colon).join(".") +
|
||||
":" +
|
||||
segments.slice(-this.colon).join(".");
|
||||
|
||||
let path = cache1.get(str);
|
||||
if (!path) {
|
||||
path = cache2.get(str);
|
||||
if (!path) {
|
||||
Object.freeze(segments);
|
||||
path = new Path(segments);
|
||||
path = new Path(segments, this.colon);
|
||||
}
|
||||
cache1.set(str, path);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
static root = new Path([], 0);
|
||||
}
|
||||
|
||||
const interval = setInterval(() => {
|
||||
|
||||
+25
-22
@@ -1,8 +1,7 @@
|
||||
import * as crypto from "node:crypto";
|
||||
import * as dgram from "node:dgram";
|
||||
import * as http from "node:http";
|
||||
import { evaluateAsync } from "./common/expression/util.ts";
|
||||
import { Expression } from "./types.ts";
|
||||
import Expression, { Value } from "./common/expression.ts";
|
||||
import * as auth from "./auth.ts";
|
||||
import * as extensions from "./extensions.ts";
|
||||
import * as debug from "./debug.ts";
|
||||
@@ -13,29 +12,33 @@ import * as logger from "../lib/logger.ts";
|
||||
|
||||
async function extractAuth(
|
||||
exp: Expression,
|
||||
dflt: any,
|
||||
dflt: Value,
|
||||
): Promise<[string, string, Expression]> {
|
||||
let username, password;
|
||||
const _exp = await evaluateAsync(
|
||||
exp,
|
||||
{},
|
||||
0,
|
||||
let username: string, password: string;
|
||||
const _exp = await exp.evaluateAsync(
|
||||
async (e: Expression): Promise<Expression> => {
|
||||
if (!username && Array.isArray(e) && e[0] === "FUNC") {
|
||||
if (e[1] === "EXT") {
|
||||
if (typeof e[2] !== "string" || typeof e[3] !== "string") return null;
|
||||
if (e instanceof Expression.Parameter)
|
||||
return new Expression.Literal(null);
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW") return new Expression.Literal(0);
|
||||
if (!username) {
|
||||
if (e.name === "EXT") {
|
||||
if (!e.args.every((a) => a instanceof Expression.Literal))
|
||||
return new Expression.Literal(null);
|
||||
const args = e.args.map((a) => a.value.toString());
|
||||
if (typeof args[0] !== "string" || typeof args[1] !== "string")
|
||||
return new Expression.Literal(null);
|
||||
|
||||
for (let i = 4; i < e.length; i++)
|
||||
if (Array.isArray(e[i])) return null;
|
||||
|
||||
const { fault, value } = await extensions.run(e.slice(2));
|
||||
return fault ? null : value;
|
||||
} else if (e[1] === "AUTH") {
|
||||
if (!Array.isArray(e[2]) && !Array.isArray(e[3])) {
|
||||
username = e[2] || "";
|
||||
password = e[3] || "";
|
||||
const { fault, value } = await extensions.run(args);
|
||||
if (fault) return new Expression.Literal(null);
|
||||
return new Expression.Literal(value);
|
||||
} else if (e.name === "AUTH") {
|
||||
if (e.args.every((a) => a instanceof Expression.Literal)) {
|
||||
username = `${e.args[0].value ?? ""}`;
|
||||
password = `${e.args[1].value ?? ""}`;
|
||||
}
|
||||
return new Expression.Literal(dflt);
|
||||
}
|
||||
return dflt;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
@@ -157,7 +160,7 @@ export async function httpConnectionRequest(
|
||||
authHeader = auth.parseWwwAuthenticateHeader(
|
||||
res.headers["www-authenticate"],
|
||||
);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
return "Connection request error: Error parsing www-authenticate header";
|
||||
}
|
||||
[username, password, authExp] = await extractAuth(authExp, false);
|
||||
|
||||
+93
-87
@@ -10,11 +10,7 @@ import * as config from "./config.ts";
|
||||
import { generateDeviceId, once, setTimeoutPromise } from "./util.ts";
|
||||
import * as soap from "./soap.ts";
|
||||
import * as session from "./session.ts";
|
||||
import {
|
||||
evaluateAsync,
|
||||
evaluate,
|
||||
extractParams,
|
||||
} from "./common/expression/util.ts";
|
||||
import Expression, { Value } from "./common/expression.ts";
|
||||
import * as cache from "./cache.ts";
|
||||
import * as lock from "./lock.ts";
|
||||
import * as localCache from "./cwmp/local-cache.ts";
|
||||
@@ -39,7 +35,6 @@ import {
|
||||
AcsRequest,
|
||||
SessionFault,
|
||||
Fault,
|
||||
Expression,
|
||||
SoapMessage,
|
||||
InformRequest,
|
||||
Preset,
|
||||
@@ -80,7 +75,7 @@ async function authenticate(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.auth",
|
||||
);
|
||||
if (authExpression == null) return true;
|
||||
if (!authExpression) return true;
|
||||
|
||||
let authentication;
|
||||
|
||||
@@ -89,7 +84,7 @@ async function authenticate(
|
||||
authentication = auth.parseAuthorizationHeader(
|
||||
sessionContext.httpRequest.headers["authorization"],
|
||||
);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -107,56 +102,65 @@ async function authenticate(
|
||||
authentication["body"] = body;
|
||||
}
|
||||
|
||||
const res = await evaluateAsync(
|
||||
authExpression,
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
const res = await authExpression.evaluateAsync(
|
||||
async (e: Expression): Promise<Expression> => {
|
||||
e = session.configContextCallback(sessionContext, e);
|
||||
if (Array.isArray(e) && e[0] === "FUNC") {
|
||||
if (e[1] === "EXT") {
|
||||
if (typeof e[2] !== "string" || typeof e[3] !== "string") return null;
|
||||
if (e instanceof Expression.Parameter)
|
||||
return new Expression.Literal(null);
|
||||
|
||||
for (let i = 4; i < e.length; i++)
|
||||
if (Array.isArray(e[i])) return null;
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW")
|
||||
return new Expression.Literal(sessionContext.timestamp);
|
||||
if (e.name === "EXT") {
|
||||
if (!e.args.every((a) => a instanceof Expression.Literal))
|
||||
return new Expression.Literal(null);
|
||||
|
||||
const { fault, value } = await extensions.run(e.slice(2));
|
||||
return fault ? null : value;
|
||||
} else if (e[1] === "AUTH") {
|
||||
const username = e[2];
|
||||
const password = e[3];
|
||||
const args = e.args.map((a) => a.value.toString());
|
||||
if (typeof args[0] !== "string" || typeof args[1] !== "string")
|
||||
return new Expression.Literal(null);
|
||||
|
||||
const { fault, value } = await extensions.run(args);
|
||||
if (fault) return new Expression.Literal(null);
|
||||
return new Expression.Literal(value);
|
||||
} else if (e.name === "AUTH") {
|
||||
if (e.args.every((a) => a instanceof Expression.Literal)) {
|
||||
const username = e.args[0].value;
|
||||
const password = e.args[1].value;
|
||||
if (username != null && password != null && authentication) {
|
||||
if (authentication["method"] === "Basic") {
|
||||
return (
|
||||
authentication["username"] === e[2] &&
|
||||
authentication["password"] === e[3]
|
||||
return new Expression.Literal(
|
||||
authentication["username"] === username.toString() &&
|
||||
authentication["password"] === password.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
if (authentication["method"] === "Digest") {
|
||||
const expected = auth.digest(
|
||||
username,
|
||||
username.toString(),
|
||||
REALM,
|
||||
password,
|
||||
password.toString(),
|
||||
authentication["nonce"],
|
||||
"POST",
|
||||
authentication["uri"],
|
||||
authentication["qop"],
|
||||
authentication["body"],
|
||||
body,
|
||||
authentication["cnonce"],
|
||||
authentication["nc"],
|
||||
);
|
||||
return expected === authentication["response"];
|
||||
return new Expression.Literal(
|
||||
expected === authentication["response"],
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return new Expression.Literal(false);
|
||||
}
|
||||
}
|
||||
return e;
|
||||
},
|
||||
);
|
||||
|
||||
if (res && !Array.isArray(res)) return true;
|
||||
if (res instanceof Expression.Literal) return !!res.value;
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -315,8 +319,7 @@ async function inform(
|
||||
const cookiesPath = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.cookiesPath",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
"",
|
||||
(e) => session.configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -406,8 +409,7 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
|
||||
const RETRY_DELAY = +localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.retryDelay",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
300,
|
||||
(e) => session.configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -434,14 +436,14 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
|
||||
deviceData.attributes.revision = 1;
|
||||
|
||||
const deviceEvents = {};
|
||||
for (const p of deviceData.paths.find(Path.parse("Events.*"), false, true)) {
|
||||
for (const p of deviceData.paths.find(Path.parse("Events"), 0b001, 0b1)) {
|
||||
const attrs = deviceData.attributes.get(p);
|
||||
const t = attrs?.value[1][0] as number;
|
||||
if (t >= sessionContext.timestamp)
|
||||
deviceEvents[p.segments[1] as string] = true;
|
||||
}
|
||||
|
||||
const parameters: { [name: string]: Path } = {};
|
||||
const parameters = new Set<string>();
|
||||
const filteredPresets: Preset[] = [];
|
||||
|
||||
for (const preset of presets) {
|
||||
@@ -470,29 +472,31 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
|
||||
continue;
|
||||
}
|
||||
|
||||
filteredPresets.push(preset);
|
||||
for (const k of extractParams(
|
||||
evaluate(preset.precondition, null, sessionContext.timestamp),
|
||||
)) {
|
||||
const pre = { ...preset };
|
||||
const evalCallback = (e: Expression): Expression => {
|
||||
if (e instanceof Expression.FunctionCall && e.name === "NOW")
|
||||
return new Expression.Literal(sessionContext.timestamp);
|
||||
if (e instanceof Expression.Parameter) {
|
||||
// Mark channel in case of fault during fetching precondition
|
||||
sessionContext.channels[preset.channel] = 0;
|
||||
if (typeof k === "string") parameters[k] = Path.parse(k);
|
||||
}
|
||||
for (const prov of preset.provisions) {
|
||||
for (const arg of prov.slice(1)) {
|
||||
for (const k of extractParams(
|
||||
evaluate(arg, null, sessionContext.timestamp),
|
||||
)) {
|
||||
// Mark channel in case of fault during fetching precondition
|
||||
sessionContext.channels[preset.channel] = 0;
|
||||
if (typeof k === "string") parameters[k] = Path.parse(k);
|
||||
}
|
||||
}
|
||||
parameters.add(e.path.toString());
|
||||
}
|
||||
return e;
|
||||
};
|
||||
|
||||
pre.precondition = preset.precondition.evaluate(evalCallback);
|
||||
|
||||
pre.provisions = pre.provisions.map((prov) => {
|
||||
let args = prov.slice(1) as Expression[];
|
||||
args = args.map((arg) => arg.evaluate(evalCallback));
|
||||
return [prov[0], ...args];
|
||||
});
|
||||
|
||||
filteredPresets.push(pre);
|
||||
}
|
||||
|
||||
const declarations = Object.values(parameters).map((v) => ({
|
||||
path: v,
|
||||
const declarations = [...parameters].map((v) => ({
|
||||
path: Path.parse(v),
|
||||
pathGet: 1,
|
||||
pathSet: null,
|
||||
attrGet: { value: 1 },
|
||||
@@ -520,22 +524,21 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
|
||||
session.addProvisions(sessionContext, whiteList, whiteListProvisions);
|
||||
|
||||
const appendProvisionsToFaults = {};
|
||||
|
||||
const evalCallback2 = (e: Expression): Expression.Literal => {
|
||||
e = session.configContextCallback(sessionContext, e);
|
||||
if (!(e instanceof Expression.Literal)) return new Expression.Literal(null);
|
||||
return e;
|
||||
};
|
||||
|
||||
for (const p of filteredPresets) {
|
||||
if (
|
||||
evaluate(p.precondition, {}, sessionContext.timestamp, (e) =>
|
||||
session.configContextCallback(sessionContext, e),
|
||||
)
|
||||
) {
|
||||
if (p.precondition.evaluate(evalCallback2).value) {
|
||||
const provs = p.provisions.map((pp) => [
|
||||
pp[0],
|
||||
...pp
|
||||
.slice(1)
|
||||
.map((arg) =>
|
||||
evaluate(arg, {}, sessionContext.timestamp, (e) =>
|
||||
session.configContextCallback(sessionContext, e),
|
||||
),
|
||||
),
|
||||
]) as [string, ...Expression[]][];
|
||||
.map((arg) => (arg as Expression).evaluate(evalCallback2).value),
|
||||
]) as [string, ...Value[]][];
|
||||
if (blackList[p.channel] === 2) {
|
||||
appendProvisionsToFaults[p.channel] = (
|
||||
appendProvisionsToFaults[p.channel] || []
|
||||
@@ -925,16 +928,18 @@ export async function onConnection(socket: Socket): Promise<void> {
|
||||
export async function onClientError(err: Error, socket: Socket): Promise<void> {
|
||||
const remoteAddress = getSocketEndpoints(socket).remoteAddress;
|
||||
const cacheSnapshot = await localCache.getRevision();
|
||||
const debugEnabled = !!localCache.getConfig(
|
||||
const debugEnabled = localCache.getConfig(
|
||||
cacheSnapshot,
|
||||
"cwmp.debug",
|
||||
{
|
||||
remoteAddress: remoteAddress,
|
||||
},
|
||||
Date.now(),
|
||||
false,
|
||||
(e) => {
|
||||
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "REMOTE_ADDRESS")
|
||||
return remoteAddress;
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "REMOTE_ADDRESS")
|
||||
return new Expression.Literal(remoteAddress);
|
||||
if (e.name === "NOW") return new Expression.Literal(Date.now());
|
||||
}
|
||||
if (!(e instanceof Expression.Literal))
|
||||
return new Expression.Literal(null);
|
||||
return e;
|
||||
},
|
||||
);
|
||||
@@ -1030,8 +1035,7 @@ async function processRequest(
|
||||
sessionContext.debug = !!localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.debug",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
false,
|
||||
(e) => session.configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -1039,8 +1043,7 @@ async function processRequest(
|
||||
sessionContext.timeout = +localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.sessionTimeout",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
30,
|
||||
(e) => session.configContextCallback(sessionContext, e),
|
||||
);
|
||||
}
|
||||
@@ -1274,7 +1277,7 @@ async function clientError(
|
||||
body: string,
|
||||
msg: string,
|
||||
): Promise<void> {
|
||||
let debugEnabled = false;
|
||||
let debugEnabled: boolean;
|
||||
let deviceId: string = null;
|
||||
|
||||
if (sessionContext) {
|
||||
@@ -1282,16 +1285,19 @@ async function clientError(
|
||||
deviceId = sessionContext.deviceId;
|
||||
} else {
|
||||
const cacheSnapshot = await localCache.getRevision();
|
||||
debugEnabled = !!localCache.getConfig(
|
||||
const remoteAddress = getRequestOrigin(httpRequest).remoteAddress;
|
||||
debugEnabled = localCache.getConfig(
|
||||
cacheSnapshot,
|
||||
"cwmp.debug",
|
||||
{
|
||||
remoteAddress: getRequestOrigin(httpRequest).remoteAddress,
|
||||
},
|
||||
Date.now(),
|
||||
false,
|
||||
(e) => {
|
||||
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "REMOTE_ADDRESS")
|
||||
return getRequestOrigin(httpRequest).remoteAddress;
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "REMOTE_ADDRESS")
|
||||
return new Expression.Literal(remoteAddress);
|
||||
if (e.name === "NOW") return new Expression.Literal(Date.now());
|
||||
}
|
||||
if (!(e instanceof Expression.Literal))
|
||||
return new Expression.Literal(null);
|
||||
return e;
|
||||
},
|
||||
);
|
||||
@@ -1312,7 +1318,7 @@ async function clientError(
|
||||
function decodeString(buffer: Buffer, charset: string): string {
|
||||
try {
|
||||
return buffer.toString(charset as BufferEncoding);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
if (encodingExists(charset)) return decode(buffer, charset);
|
||||
}
|
||||
return null;
|
||||
@@ -1383,7 +1389,7 @@ async function listenerAsync(
|
||||
// connection is closed before the stream is finished.
|
||||
// For Node 12.9+ we can just use stream.readableEnded
|
||||
if (!readableEnded) throw new Error("Connection closed");
|
||||
} catch (err) {
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+22
-27
@@ -7,7 +7,6 @@ import {
|
||||
Task,
|
||||
Operation,
|
||||
} from "../types.ts";
|
||||
import Path from "../common/path.ts";
|
||||
import { collections } from "../db/db.ts";
|
||||
import { optimizeProjection } from "../db/util.ts";
|
||||
import * as MongoTypes from "../db/types.ts";
|
||||
@@ -23,21 +22,17 @@ function compareAccessLists(list1: string[], list2: string[]): boolean {
|
||||
export async function fetchDevice(
|
||||
id: string,
|
||||
timestamp: number,
|
||||
): Promise<[Path, number, Attributes?][]> {
|
||||
const res: [Path, number, Attributes?][] = [
|
||||
): Promise<[string, number, Attributes?][]> {
|
||||
const res: [string, number, Attributes?][] = [
|
||||
["Events", timestamp, { object: [timestamp, 1], writable: [timestamp, 0] }],
|
||||
[
|
||||
Path.parse("Events"),
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 0] },
|
||||
],
|
||||
[
|
||||
Path.parse("DeviceID"),
|
||||
"DeviceID",
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 0] },
|
||||
],
|
||||
];
|
||||
|
||||
const device = await collections.devices.findOne({ _id: id });
|
||||
const device = (await collections.devices.findOne({ _id: id })) as any;
|
||||
if (!device) return null;
|
||||
|
||||
function storeParams(
|
||||
@@ -77,8 +72,8 @@ export async function fetchDevice(
|
||||
attrs.accessList = [obj["_attributesTimestamp"] || 1, obj["_accessList"]];
|
||||
|
||||
try {
|
||||
res.push([Path.parse(path), t, attrs]);
|
||||
} catch (err) {
|
||||
res.push([path, t, attrs]);
|
||||
} catch {
|
||||
// The path parser is now more strict so we might be in a situation where
|
||||
// the database contains invalid paths from before this change So here we
|
||||
// encode the invalid characters.
|
||||
@@ -86,7 +81,7 @@ export async function fetchDevice(
|
||||
splits[splits.length - 1] =
|
||||
encodeTag(splits[splits.length - 1]) + INVALID_PATH_SUFFIX;
|
||||
path = splits.join(".");
|
||||
res.push([Path.parse(path), t, attrs]);
|
||||
res.push([path, t, attrs]);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -98,17 +93,17 @@ export async function fetchDevice(
|
||||
}
|
||||
|
||||
if (obj["_object"] && obj["_timestamp"])
|
||||
res.push([Path.parse(path + ".*"), obj["_timestamp"]]);
|
||||
res.push([path + ".*", obj["_timestamp"]]);
|
||||
}
|
||||
|
||||
const ts: number = +device["_timestamp"] || 0;
|
||||
if (ts) res.push([Path.parse("*"), ts]);
|
||||
if (ts) res.push(["*", ts]);
|
||||
|
||||
for (const [k, v] of Object.entries(device)) {
|
||||
switch (k) {
|
||||
case "_lastInform":
|
||||
res.push([
|
||||
Path.parse("Events.Inform"),
|
||||
"Events.Inform",
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
@@ -119,7 +114,7 @@ export async function fetchDevice(
|
||||
break;
|
||||
case "_lastBoot":
|
||||
res.push([
|
||||
Path.parse("Events.1_BOOT"),
|
||||
"Events.1_BOOT",
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
@@ -130,7 +125,7 @@ export async function fetchDevice(
|
||||
break;
|
||||
case "_lastBootstrap":
|
||||
res.push([
|
||||
Path.parse("Events.0_BOOTSTRAP"),
|
||||
"Events.0_BOOTSTRAP",
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
@@ -142,7 +137,7 @@ export async function fetchDevice(
|
||||
case "_registered":
|
||||
// Use current timestamp for registered event attribute timestamps
|
||||
res.push([
|
||||
Path.parse("Events.Registered"),
|
||||
"Events.Registered",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -153,7 +148,7 @@ export async function fetchDevice(
|
||||
break;
|
||||
case "_id":
|
||||
res.push([
|
||||
Path.parse("DeviceID.ID"),
|
||||
"DeviceID.ID",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -165,7 +160,7 @@ export async function fetchDevice(
|
||||
case "_tags":
|
||||
if ((v as string[]).length) {
|
||||
res.push([
|
||||
Path.parse("Tags"),
|
||||
"Tags",
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 0] },
|
||||
]);
|
||||
@@ -173,7 +168,7 @@ export async function fetchDevice(
|
||||
|
||||
for (const t of v as string[]) {
|
||||
res.push([
|
||||
Path.parse("Tags." + encodeTag(t)),
|
||||
"Tags." + encodeTag(t),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -186,7 +181,7 @@ export async function fetchDevice(
|
||||
case "_deviceId":
|
||||
if (v["_Manufacturer"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.Manufacturer"),
|
||||
"DeviceID.Manufacturer",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -198,7 +193,7 @@ export async function fetchDevice(
|
||||
|
||||
if (v["_OUI"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.OUI"),
|
||||
"DeviceID.OUI",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -210,7 +205,7 @@ export async function fetchDevice(
|
||||
|
||||
if (v["_ProductClass"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.ProductClass"),
|
||||
"DeviceID.ProductClass",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -222,7 +217,7 @@ export async function fetchDevice(
|
||||
|
||||
if (v["_SerialNumber"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.SerialNumber"),
|
||||
"DeviceID.SerialNumber",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -257,7 +252,7 @@ export async function saveDevice(
|
||||
)
|
||||
continue;
|
||||
|
||||
const parent = deviceData.paths.get(diff[0].slice(0, -1));
|
||||
const parent = deviceData.paths.get(diff[0].slice(0, -1).toString());
|
||||
|
||||
// Param timestamps may be greater than session timestamp to track revisions
|
||||
if (diff[2] > sessionTimestamp) diff[2] = sessionTimestamp;
|
||||
|
||||
+74
-58
@@ -1,15 +1,12 @@
|
||||
import * as vm from "node:vm";
|
||||
import * as crypto from "node:crypto";
|
||||
import * as config from "../config.ts";
|
||||
import { collections } from "../db/db.ts";
|
||||
import { convertOldPrecondition } from "../db/util.ts";
|
||||
import * as logger from "../logger.ts";
|
||||
import * as scheduling from "../scheduling.ts";
|
||||
import * as expression from "../common/expression/util.ts";
|
||||
import { parse } from "../common/expression/parser.ts";
|
||||
import Expression, { Value } from "../common/expression.ts";
|
||||
import {
|
||||
Preset,
|
||||
Expression,
|
||||
Provisions,
|
||||
VirtualParameters,
|
||||
Files,
|
||||
@@ -82,7 +79,7 @@ async function fetchPresets(): Promise<[string, Preset[]]> {
|
||||
try {
|
||||
schedule.duration = +parts.shift() * 1000;
|
||||
schedule.schedule = scheduling.parseCron(parts.join(" "));
|
||||
} catch (err) {
|
||||
} catch {
|
||||
logger.warn({
|
||||
message: "Invalid preset schedule",
|
||||
preset: preset["_id"],
|
||||
@@ -93,18 +90,18 @@ async function fetchPresets(): Promise<[string, Preset[]]> {
|
||||
}
|
||||
|
||||
const events = preset["events"] || {};
|
||||
let precondition = true as Expression;
|
||||
let precondition: Expression = new Expression.Literal(true);
|
||||
if (preset["precondition"]) {
|
||||
try {
|
||||
precondition = parse(preset["precondition"]);
|
||||
} catch (error) {
|
||||
precondition = Expression.parse(preset["precondition"]);
|
||||
} catch {
|
||||
precondition = convertOldPrecondition(
|
||||
JSON.parse(preset["precondition"]),
|
||||
);
|
||||
}
|
||||
|
||||
// Simplify expression
|
||||
precondition = expression.evaluate(precondition);
|
||||
precondition = precondition.evaluate((e) => e);
|
||||
}
|
||||
|
||||
const _provisions: Preset["provisions"] = [];
|
||||
@@ -113,23 +110,42 @@ async function fetchPresets(): Promise<[string, Preset[]]> {
|
||||
for (const c of preset.configurations) {
|
||||
switch (c.type) {
|
||||
case "age":
|
||||
_provisions.push(["refresh", c.name, +c.age]);
|
||||
_provisions.push([
|
||||
"refresh",
|
||||
new Expression.Literal(c.name),
|
||||
new Expression.Literal(+c.age),
|
||||
]);
|
||||
break;
|
||||
|
||||
case "value":
|
||||
_provisions.push(["value", c.name, c.value]);
|
||||
_provisions.push([
|
||||
"value",
|
||||
new Expression.Literal(c.name),
|
||||
new Expression.Literal(c.value),
|
||||
]);
|
||||
break;
|
||||
|
||||
case "add_tag":
|
||||
_provisions.push(["tag", c.tag, true]);
|
||||
_provisions.push([
|
||||
"tag",
|
||||
new Expression.Literal(c.tag),
|
||||
new Expression.Literal(true),
|
||||
]);
|
||||
break;
|
||||
|
||||
case "delete_tag":
|
||||
_provisions.push(["tag", c.tag, false]);
|
||||
_provisions.push([
|
||||
"tag",
|
||||
new Expression.Literal(c.tag),
|
||||
new Expression.Literal(false),
|
||||
]);
|
||||
break;
|
||||
|
||||
case "provision":
|
||||
_provisions.push([c.name, ...(c.args || [])]);
|
||||
_provisions.push([
|
||||
c.name,
|
||||
...(c.args || []).map((a) => Expression.parse(a)),
|
||||
]);
|
||||
break;
|
||||
|
||||
case "add_object":
|
||||
@@ -139,11 +155,19 @@ async function fetchPresets(): Promise<[string, Preset[]]> {
|
||||
.map((k) => `${k}:${JSON.stringify(obj[k])}`)
|
||||
.join(",");
|
||||
const p = `${c.name}.[${alias}]`;
|
||||
_provisions.push(["instances", p, 1]);
|
||||
_provisions.push([
|
||||
"instances",
|
||||
new Expression.Literal(p),
|
||||
new Expression.Literal(1),
|
||||
]);
|
||||
|
||||
for (const k in obj) {
|
||||
if (!k.startsWith("_") && !(obj["_keys"].indexOf(k) !== -1))
|
||||
_provisions.push(["value", `${p}.${k}`, obj[k]]);
|
||||
_provisions.push([
|
||||
"value",
|
||||
new Expression.Literal(`${p}.${k}`),
|
||||
new Expression.Literal(obj[k]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,7 +181,11 @@ async function fetchPresets(): Promise<[string, Preset[]]> {
|
||||
.map((k) => `${k}:${JSON.stringify(obj[k])}`)
|
||||
.join(",");
|
||||
const p = `${c.name}.[${alias}]`;
|
||||
_provisions.push(["instances", p, 0]);
|
||||
_provisions.push([
|
||||
"instances",
|
||||
new Expression.Literal(p),
|
||||
new Expression.Literal(0),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,7 +279,7 @@ async function fetchConfig(): Promise<[string, Config]> {
|
||||
|
||||
for (const c of conf) {
|
||||
// Evaluate expressions to simplify them
|
||||
_config[c._id] = expression.evaluate(parse(c.value));
|
||||
_config[c._id] = Expression.parse(c.value).evaluate((e) => e);
|
||||
}
|
||||
|
||||
return [h, _config];
|
||||
@@ -303,49 +331,37 @@ export function getFiles(revision: string): Files {
|
||||
}
|
||||
|
||||
export function getConfig(
|
||||
snapshotKey: string,
|
||||
revision: string,
|
||||
key: string,
|
||||
context: Record<string, unknown>,
|
||||
now: number,
|
||||
cb?: (e: Expression) => Expression,
|
||||
): string | number | boolean | null {
|
||||
const snapshot = localCache.get(snapshotKey);
|
||||
dflt: string,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): string;
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
dflt: number,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): number;
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
dflt: boolean,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): boolean;
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
dflt: Value,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): Value {
|
||||
const snapshot = localCache.get(revision);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
|
||||
const oldOpts = {
|
||||
"cwmp.downloadTimeout": "DOWNLOAD_TIMEOUT",
|
||||
"cwmp.debug": "DEBUG",
|
||||
"cwmp.retryDelay": "RETRY_DELAY",
|
||||
"cwmp.sessionTimeout": "SESSION_TIMEOUT",
|
||||
"cwmp.connectionRequestTimeout": "CONNECTION_REQUEST_TIMEOUT",
|
||||
"cwmp.gpnNextLevel": "GPN_NEXT_LEVEL",
|
||||
"cwmp.gpvBatchSize": "GPV_BATCH_SIZE",
|
||||
"cwmp.cookiesPath": "COOKIES_PATH",
|
||||
"cwmp.datetimeMilliseconds": "DATETIME_MILLISECONDS",
|
||||
"cwmp.booleanLiteral": "BOOLEAN_LITERAL",
|
||||
"cwmp.connectionRequestAllowBasicAuth":
|
||||
"CONNECTION_REQUEST_ALLOW_BASIC_AUTH",
|
||||
"cwmp.maxCommitIterations": "MAX_COMMIT_ITERATIONS",
|
||||
"cwmp.deviceOnlineThreshold": "DEVICE_ONLINE_THRESHOLD",
|
||||
"cwmp.udpConnectionRequestPort": "UDP_CONNECTION_REQUEST_PORT",
|
||||
};
|
||||
|
||||
if (!(key in snapshot.config)) {
|
||||
if (key in oldOpts) {
|
||||
let id;
|
||||
if (context?.["id"]) {
|
||||
id = context["id"];
|
||||
} else if (cb) {
|
||||
id = cb(["PARAM", "DeviceID.ID"]);
|
||||
if (Array.isArray(id)) id = null;
|
||||
}
|
||||
return config.get(oldOpts[key], id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const v = expression.evaluate(snapshot.config[key], context, now, cb);
|
||||
return Array.isArray(v) ? null : v;
|
||||
const e = snapshot.config[key];
|
||||
if (!e) return dflt;
|
||||
const v = e.evaluate(fn).value;
|
||||
if (typeof v !== typeof dflt) return dflt;
|
||||
return v;
|
||||
}
|
||||
|
||||
export function getConfigExpression(
|
||||
|
||||
+56
-50
@@ -1,7 +1,8 @@
|
||||
import { Filter } from "mongodb";
|
||||
import { EJSON } from "bson";
|
||||
import { complement } from "espresso-iisojs";
|
||||
import { parseLikePattern } from "../common/expression/parser.ts";
|
||||
import { Expression } from "../types.ts";
|
||||
import Expression from "../common/expression.ts";
|
||||
import { decodeTag } from "../util.ts";
|
||||
import {
|
||||
SynthContextBase,
|
||||
@@ -10,21 +11,14 @@ import {
|
||||
Clause,
|
||||
} from "../common/expression/synth.ts";
|
||||
import normalize from "../common/expression/normalize.ts";
|
||||
import * as BI from "../common/expression/bigint.ts";
|
||||
|
||||
type Minterm = number[];
|
||||
|
||||
function getParam(exp: Expression, collection: string): string {
|
||||
if (!Array.isArray(exp))
|
||||
if (!(exp instanceof Expression.Parameter))
|
||||
throw new Error("Left-hand operand must be a parameter");
|
||||
|
||||
if (exp[0] !== "PARAM")
|
||||
throw new Error("Left-hand operand must be a parameter");
|
||||
|
||||
if (typeof exp[1] !== "string")
|
||||
throw new Error("Left-hand operand must be a parameter");
|
||||
|
||||
const p = exp[1];
|
||||
const p = exp.path.toString();
|
||||
if (collection === "devices") {
|
||||
if (p === "DeviceID.ID") return "_id";
|
||||
else if (p === "DeviceID") return "_deviceId";
|
||||
@@ -117,12 +111,12 @@ function getTypes(parameter: string, collection: string): string[] {
|
||||
|
||||
function roundOid(oid: string, roundUp: boolean): string {
|
||||
const match = (oid.match(/^[0-9a-f]*/)?.[0] ?? "").slice(0, 24);
|
||||
let num = BI.BigInt("0x0" + match);
|
||||
let num = BigInt("0x0" + match);
|
||||
let lastChar = 0;
|
||||
if (oid.length > match.length) lastChar += oid.charCodeAt(match.length);
|
||||
if (match.length < 24) lastChar -= 48;
|
||||
if (lastChar > 0 && roundUp) num++;
|
||||
num <<= BI.BigInt(4 * (24 - match.length));
|
||||
num <<= BigInt(4 * (24 - match.length));
|
||||
if (lastChar < 0 && !roundUp) --num;
|
||||
const str = num.toString(16);
|
||||
if (str.length > 24 || str.startsWith("-")) return null;
|
||||
@@ -259,11 +253,12 @@ class MongoSynthContext extends SynthContextBase<Clause, MongoClause> {
|
||||
super();
|
||||
}
|
||||
|
||||
getMinterms(clause: Clause, res: true | false | null): number[][] {
|
||||
const exp = clause.expression();
|
||||
if (!Array.isArray(exp)) throw new Error("Invalid query expression");
|
||||
getMinterms(clause: Clause, res: number): number[][] {
|
||||
res = res & 0b111;
|
||||
if (res & (res - 1)) return complement(this.getMinterms(clause, ~res));
|
||||
|
||||
if (res === null) {
|
||||
const exp = clause.expression();
|
||||
if (res === 0b001) {
|
||||
const minterms: number[][] = [];
|
||||
for (const dep of clause.getNullables()) {
|
||||
const e = dep.operand.expression();
|
||||
@@ -280,36 +275,42 @@ class MongoSynthContext extends SynthContextBase<Clause, MongoClause> {
|
||||
return minterms;
|
||||
}
|
||||
|
||||
if ([">", "<", "="].includes(exp[0])) {
|
||||
const param = getParam(exp[1], this.collection);
|
||||
if (!(exp instanceof Expression.Binary))
|
||||
throw new Error("Invalid query expression");
|
||||
|
||||
let rhs = exp[2];
|
||||
if (typeof rhs === "boolean") rhs = +rhs;
|
||||
else if (typeof rhs !== "string" && typeof rhs !== "number")
|
||||
const truthy = res === 0b100;
|
||||
|
||||
if ([">", "<", "="].includes(exp.operator)) {
|
||||
const param = getParam(exp.left, this.collection);
|
||||
|
||||
if (!(exp.right instanceof Expression.Literal))
|
||||
throw new Error(`Right-hand operand must be a literal value`);
|
||||
|
||||
let rhs = exp.right.value;
|
||||
if (typeof rhs === "boolean") rhs = +rhs;
|
||||
|
||||
if (
|
||||
param.startsWith("Tags.") &&
|
||||
this.collection === "devices" &&
|
||||
exp[0] === "="
|
||||
exp.operator === "="
|
||||
) {
|
||||
const t = decodeTag(param.slice(5));
|
||||
const c = new MongoClauseArray("_tags", t);
|
||||
if (typeof rhs === "string") rhs = 2;
|
||||
if (exp[0] === "=") {
|
||||
if ((rhs === 1) !== res) return [];
|
||||
} else if (exp[0] === ">") {
|
||||
if ((res && rhs >= 1) || (!res && rhs < 1)) return [];
|
||||
} else if (exp[0] === "<") {
|
||||
if ((res && rhs <= 1) || (!res && rhs > 1)) return [];
|
||||
if (exp.operator === "=") {
|
||||
if ((rhs === 1) !== truthy) return [];
|
||||
} else if (exp.operator === ">") {
|
||||
if ((truthy && rhs >= 1) || (!truthy && rhs < 1)) return [];
|
||||
} else if (exp.operator === "<") {
|
||||
if ((truthy && rhs <= 1) || (!truthy && rhs > 1)) return [];
|
||||
}
|
||||
return [[(this.getVar(c) << 1) ^ 1]];
|
||||
}
|
||||
|
||||
let op: "$gt" | "$lt" | "$eq" | "$gte" | "$lte";
|
||||
if (exp[0] === "=") op = "$eq";
|
||||
else if (exp[0] === ">") op = res ? "$gt" : "$lte";
|
||||
else if (exp[0] === "<") op = res ? "$lt" : "$gte";
|
||||
if (exp.operator === "=") op = "$eq";
|
||||
else if (exp.operator === ">") op = truthy ? "$gt" : "$lte";
|
||||
else if (exp.operator === "<") op = truthy ? "$lt" : "$gte";
|
||||
|
||||
const possibleTypes = new Set(getTypes(param, this.collection));
|
||||
const clauses: MongoClause[] = [];
|
||||
@@ -333,7 +334,7 @@ class MongoSynthContext extends SynthContextBase<Clause, MongoClause> {
|
||||
}
|
||||
}
|
||||
|
||||
if (op === "$eq" && !res) {
|
||||
if (op === "$eq" && !truthy) {
|
||||
clauses.push(new MongoClauseCompare(param, "$eq", null, ""));
|
||||
return [clauses.map((c) => this.getVar(c) << 1)];
|
||||
}
|
||||
@@ -382,30 +383,35 @@ class MongoSynthContext extends SynthContextBase<Clause, MongoClause> {
|
||||
}
|
||||
|
||||
return clauses.map((c) => [(this.getVar(c) << 1) ^ 1]);
|
||||
} else if (exp[0] === "LIKE") {
|
||||
const pat = exp[2];
|
||||
if (typeof pat !== "string")
|
||||
} else if (exp.operator === "LIKE") {
|
||||
if (
|
||||
!(
|
||||
exp.right instanceof Expression.Literal &&
|
||||
typeof exp.right.value === "string"
|
||||
)
|
||||
)
|
||||
throw new Error("Right-hand operand of 'LIKE' must be a string");
|
||||
let p = exp[1];
|
||||
const pat = exp.right.value;
|
||||
|
||||
let p = exp.left;
|
||||
let caseSensitive = true;
|
||||
if (
|
||||
Array.isArray(p) &&
|
||||
p[0] === "FUNC" &&
|
||||
["UPPER", "LOWER"].includes(p[1])
|
||||
p instanceof Expression.FunctionCall &&
|
||||
["UPPER", "LOWER"].includes(p.name)
|
||||
) {
|
||||
if (p[1] === "UPPER" && pat !== pat.toUpperCase())
|
||||
return res ? [] : [[]];
|
||||
if (p[1] === "LOWER" && pat !== pat.toLowerCase())
|
||||
return res ? [] : [[]];
|
||||
if (p.name === "UPPER" && pat !== pat.toUpperCase())
|
||||
return truthy ? [] : [[]];
|
||||
if (p.name === "LOWER" && pat !== pat.toLowerCase())
|
||||
return truthy ? [] : [[]];
|
||||
caseSensitive = false;
|
||||
p = p[2];
|
||||
p = p.args[0];
|
||||
}
|
||||
|
||||
const param = getParam(p, this.collection);
|
||||
const c = new MongoClauseLike(param, pat, exp[3], caseSensitive);
|
||||
if (res) return [[(this.getVar(c) << 1) ^ 1]];
|
||||
const typeClase = new MongoClauseType(param, "string");
|
||||
const r = [[this.getVar(c) << 1, (this.getVar(typeClase) << 1) ^ 1]];
|
||||
const c = new MongoClauseLike(param, pat, null, caseSensitive);
|
||||
if (truthy) return [[(this.getVar(c) << 1) ^ 1]];
|
||||
const typeClause = new MongoClauseCompare(param, "$gte", "", "string");
|
||||
const r = [[this.getVar(c) << 1, (this.getVar(typeClause) << 1) ^ 1]];
|
||||
return r;
|
||||
} else {
|
||||
throw new Error("Invalid query expression");
|
||||
@@ -641,7 +647,7 @@ export function toMongoQuery(
|
||||
exp = normalize(exp);
|
||||
const clause = Clause.fromExpression(normalize(exp));
|
||||
const context = new MongoSynthContext(resource);
|
||||
const minterms = clause.true(context);
|
||||
const minterms = clause.getMinterms(context, 0b100);
|
||||
const minimized = context.minimize(minterms);
|
||||
if (!minimized.length) return false;
|
||||
return EJSON.deserialize(context.toQuery(minimized));
|
||||
@@ -650,5 +656,5 @@ export function toMongoQuery(
|
||||
export function validQuery(exp: Expression, resource: string): void {
|
||||
const clause = Clause.fromExpression(normalize(exp));
|
||||
const context = new MongoSynthContext(resource);
|
||||
clause.true(context);
|
||||
clause.getMinterms(context, 0b100);
|
||||
}
|
||||
+4
-3
@@ -1,5 +1,6 @@
|
||||
import { ObjectId } from "mongodb";
|
||||
import { Expression, FaultStruct } from "../types.ts";
|
||||
import { FaultStruct } from "../types.ts";
|
||||
import { Value } from "../common/expression.ts";
|
||||
|
||||
export interface Fault {
|
||||
_id: string;
|
||||
@@ -71,7 +72,7 @@ interface TaskDeleteObject extends TaskBase {
|
||||
|
||||
interface TaskProvisions extends TaskBase {
|
||||
name: "provisions";
|
||||
provisions?: [string, ...Expression[]][];
|
||||
provisions?: [string, ...Value[]][];
|
||||
}
|
||||
|
||||
export type Task =
|
||||
@@ -122,7 +123,7 @@ type Configuration =
|
||||
| { type: "delete_tag"; tag: string }
|
||||
| { type: "add_object"; name: string; object: string }
|
||||
| { type: "delete_object"; name: string; object: string }
|
||||
| { type: "provision"; name: string; args?: Expression[] };
|
||||
| { type: "provision"; name: string; args?: string[] };
|
||||
|
||||
export interface Preset {
|
||||
_id: string;
|
||||
|
||||
+100
-40
@@ -1,4 +1,5 @@
|
||||
import { Expression } from "../types.ts";
|
||||
import Expression, { Value } from "../common/expression.ts";
|
||||
import Path from "../common/path.ts";
|
||||
import { encodeTag } from "../util.ts";
|
||||
|
||||
// Optimize projection by removing overlaps
|
||||
@@ -26,44 +27,62 @@ export function optimizeProjection(obj: { [path: string]: 1 }): {
|
||||
|
||||
export function convertOldPrecondition(q: Record<string, unknown>): Expression {
|
||||
function recursive(_query): Expression {
|
||||
const expressions: Expression[] = [];
|
||||
let res: Expression = new Expression.Literal(true);
|
||||
for (const [k, v] of Object.entries(_query)) {
|
||||
if (k[0] === "$") {
|
||||
if (k === "$and") {
|
||||
const and: Expression = ["AND"];
|
||||
for (const vv of Object.values(v)) and.push(recursive(vv));
|
||||
expressions.push(and);
|
||||
for (const vv of Object.values(v))
|
||||
res = Expression.and(res, recursive(vv));
|
||||
} else if (k === "$or") {
|
||||
const or: Expression = ["OR"];
|
||||
for (const vv of Object.values(v)) or.push(recursive(vv));
|
||||
expressions.push(or);
|
||||
let or: Expression = new Expression.Literal(false);
|
||||
for (const vv of Object.values(v))
|
||||
or = Expression.or(or, recursive(vv));
|
||||
res = Expression.and(res, or);
|
||||
} else {
|
||||
throw new Error(`Operator ${k} not supported`);
|
||||
}
|
||||
} else if (k === "_tags") {
|
||||
if (typeof v === "object") {
|
||||
if (Array.isArray(v)) throw new Error(`Invalid type`);
|
||||
const conjs: Expression[] = [];
|
||||
for (const [op, val] of Object.entries(v)) {
|
||||
if (op === "$ne") {
|
||||
if (typeof v["$ne"] !== "string")
|
||||
throw new Error("Only string values are allowed for _tags");
|
||||
conjs.push(["IS NULL", ["PARAM", `Tags.${encodeTag(val)}`]]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Unary(
|
||||
"IS NULL",
|
||||
new Expression.Parameter(
|
||||
Path.parse(`Tags.${encodeTag(val)}`),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else if (op === "$eq") {
|
||||
if (typeof v["$eq"] !== "string")
|
||||
throw new Error("Only string values are allowed for _tags");
|
||||
conjs.push(["IS NOT NULL", ["PARAM", `Tags.${encodeTag(val)}`]]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Unary(
|
||||
"IS NOT NULL",
|
||||
new Expression.Parameter(
|
||||
Path.parse(`Tags.${encodeTag(val)}`),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
throw new Error(`Invalid tag query`);
|
||||
}
|
||||
}
|
||||
if (conjs.length === 1) expressions.push(conjs[0]);
|
||||
else if (conjs.length > 1) expressions.push(["AND", ...conjs]);
|
||||
} else {
|
||||
expressions.push([
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Unary(
|
||||
"IS NOT NULL",
|
||||
["PARAM", `Tags.${encodeTag(v as string)}`],
|
||||
]);
|
||||
new Expression.Parameter(
|
||||
Path.parse(`Tags.${encodeTag(v as string)}`),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
} else if (k.startsWith("Tags.")) {
|
||||
let exists: boolean;
|
||||
@@ -73,51 +92,92 @@ export function convertOldPrecondition(q: Record<string, unknown>): Expression {
|
||||
else if (v.hasOwnProperty("$exists")) exists = !!v["$exists"];
|
||||
else throw new Error(`Invalid tag query`);
|
||||
|
||||
expressions.push([exists ? "IS NOT NULL" : "IS NULL", ["PARAM", k]]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Unary(
|
||||
exists ? "IS NOT NULL" : "IS NULL",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
),
|
||||
);
|
||||
} else if (typeof v === "object") {
|
||||
if (Array.isArray(v)) throw new Error(`Invalid type`);
|
||||
|
||||
const exps: Expression[] = [];
|
||||
for (const [kk, vv] of Object.entries(v)) {
|
||||
if (kk === "$eq") {
|
||||
exps.push(["=", ["PARAM", k], vv]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
new Expression.Literal(vv),
|
||||
),
|
||||
);
|
||||
} else if (kk === "$ne") {
|
||||
exps.push([
|
||||
"OR",
|
||||
["<>", ["PARAM", k], vv],
|
||||
["IS NULL", ["PARAM", k]],
|
||||
]);
|
||||
const p = new Expression.Parameter(Path.parse(k));
|
||||
res = Expression.and(
|
||||
res,
|
||||
Expression.or(
|
||||
new Expression.Binary("<>", p, new Expression.Literal(vv)),
|
||||
new Expression.Unary("IS NULL", p),
|
||||
),
|
||||
);
|
||||
} else if (kk === "$lt") {
|
||||
exps.push(["<", ["PARAM", k], vv]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Binary(
|
||||
"<",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
new Expression.Literal(vv),
|
||||
),
|
||||
);
|
||||
} else if (kk === "$lte") {
|
||||
exps.push(["<=", ["PARAM", k], vv]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Binary(
|
||||
"<=",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
new Expression.Literal(vv),
|
||||
),
|
||||
);
|
||||
} else if (kk === "$gt") {
|
||||
exps.push([">", ["PARAM", k], vv]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Binary(
|
||||
">",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
new Expression.Literal(vv),
|
||||
),
|
||||
);
|
||||
} else if (kk === "$gte") {
|
||||
exps.push([">=", ["PARAM", k], vv]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Binary(
|
||||
">=",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
new Expression.Literal(vv),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
throw new Error(`Operator ${kk} not supported`);
|
||||
}
|
||||
if (!["string", "number", "boolean"].includes(typeof vv))
|
||||
throw new Error(`Invalid value for ${kk} operator`);
|
||||
}
|
||||
if (exps.length === 1) {
|
||||
expressions.push(exps[0]);
|
||||
} else if (exps.length > 1) {
|
||||
const and: Expression = ["AND"];
|
||||
expressions.push(and.concat(exps));
|
||||
}
|
||||
} else {
|
||||
expressions.push(["=", ["PARAM", k], v]);
|
||||
res = Expression.and(
|
||||
res,
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
new Expression.Parameter(Path.parse(k)),
|
||||
new Expression.Literal(v as Value),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (expressions.length === 1) return expressions[0];
|
||||
if (expressions.length === 0) return true;
|
||||
return ["AND", ...expressions];
|
||||
return res;
|
||||
}
|
||||
|
||||
// empty filter
|
||||
if (!Object.keys(q).length) return true;
|
||||
if (!Object.keys(q).length) return new Expression.Literal(true);
|
||||
|
||||
return recursive(q);
|
||||
}
|
||||
+42
-13
@@ -1,3 +1,4 @@
|
||||
import Expression, { Value } from "./common/expression.ts";
|
||||
import Path from "./common/path.ts";
|
||||
import {
|
||||
DeviceData,
|
||||
@@ -84,9 +85,9 @@ export function getAliasDeclarations(
|
||||
|
||||
if (path.alias) {
|
||||
for (const [i, alias] of path.segments.entries()) {
|
||||
if (Array.isArray(alias)) {
|
||||
if (alias instanceof Expression) {
|
||||
const parent = stripped.slice(0, i + 1);
|
||||
for (const [p] of alias as [Path, string][]) {
|
||||
for (const [p] of expressionToAlias(alias)) {
|
||||
decs = decs.concat(
|
||||
getAliasDeclarations(parent.concat(p), timestamp, {
|
||||
value: timestamp,
|
||||
@@ -100,6 +101,22 @@ export function getAliasDeclarations(
|
||||
return decs;
|
||||
}
|
||||
|
||||
export function expressionToAlias(exp: Expression): [Path, Value][] {
|
||||
if (exp instanceof Expression.Literal && exp.value === true) return [];
|
||||
if (exp instanceof Expression.Binary) {
|
||||
if (exp.operator === "AND")
|
||||
return [...expressionToAlias(exp.left), ...expressionToAlias(exp.right)];
|
||||
else if (exp.operator === "=") {
|
||||
if (
|
||||
exp.left instanceof Expression.Parameter &&
|
||||
exp.right instanceof Expression.Literal
|
||||
)
|
||||
return [[exp.left.path, exp.right.value]];
|
||||
}
|
||||
}
|
||||
throw new Error("Invalid alias expression");
|
||||
}
|
||||
|
||||
export function unpack(
|
||||
deviceData: DeviceData,
|
||||
path: Path,
|
||||
@@ -107,17 +124,19 @@ export function unpack(
|
||||
): Path[] {
|
||||
let allMatches = [] as Path[];
|
||||
if (!path.alias) {
|
||||
for (const p of deviceData.paths.find(path, false, true))
|
||||
for (const p of deviceData.paths.findCompat(path, false, true))
|
||||
if (deviceData.attributes.has(p, revision)) allMatches.push(p);
|
||||
} else {
|
||||
const wildcardPath = path.stripAlias();
|
||||
|
||||
for (const p of deviceData.paths.find(wildcardPath, false, true))
|
||||
for (const p of deviceData.paths.findCompat(wildcardPath, false, true))
|
||||
if (deviceData.attributes.has(p, revision)) allMatches.push(p);
|
||||
|
||||
for (let i = path.length - 1; i >= 0; --i) {
|
||||
if (path.alias & (1 << i)) {
|
||||
for (const [param, val] of path.segments[i] as [Path, string][]) {
|
||||
for (const [param, val] of expressionToAlias(
|
||||
path.segments[i] as Expression,
|
||||
)) {
|
||||
const p = wildcardPath.slice(0, i + 1).concat(param);
|
||||
const unpacked = unpack(deviceData, p, revision);
|
||||
const filtered: Path[] = [];
|
||||
@@ -189,7 +208,7 @@ export function clear(
|
||||
attributes.value = attributes.object;
|
||||
}
|
||||
|
||||
for (const p of deviceData.paths.find(
|
||||
for (const p of deviceData.paths.findCompat(
|
||||
path,
|
||||
true,
|
||||
true,
|
||||
@@ -252,12 +271,12 @@ function compareEquality(a, b): boolean {
|
||||
|
||||
export function set(
|
||||
deviceData: DeviceData,
|
||||
path: Path,
|
||||
pathStr: string,
|
||||
timestamp: number,
|
||||
attributes: Attributes,
|
||||
toClear?: Clear[],
|
||||
): Clear[] {
|
||||
path = deviceData.paths.add(path);
|
||||
const path = deviceData.paths.add(pathStr);
|
||||
|
||||
const currentTimestamp = deviceData.timestamps.get(path);
|
||||
|
||||
@@ -312,7 +331,7 @@ export function set(
|
||||
if (path.length > 1) {
|
||||
toClear = set(
|
||||
deviceData,
|
||||
path.slice(0, path.length - 1),
|
||||
path.slice(0, path.length - 1).toString(),
|
||||
timestamp,
|
||||
{ object: [timestamp, 1] },
|
||||
toClear,
|
||||
@@ -326,7 +345,12 @@ export function set(
|
||||
deviceData.attributes.delete(path);
|
||||
changeFlags |= 1;
|
||||
} else if (path.wildcard) {
|
||||
for (const p of deviceData.paths.find(path, false, true, path.length)) {
|
||||
for (const p of deviceData.paths.findCompat(
|
||||
path,
|
||||
false,
|
||||
true,
|
||||
path.length,
|
||||
)) {
|
||||
if (timestamp > deviceData.timestamps.get(p)) {
|
||||
toClear = toClear || [];
|
||||
toClear.push([p, timestamp]);
|
||||
@@ -343,7 +367,12 @@ export function set(
|
||||
toClear = toClear || [];
|
||||
toClear.push([path, 0, { object: attributes.object[0] }, changeFlags]);
|
||||
} else {
|
||||
for (const p of deviceData.paths.find(path, true, false, path.length)) {
|
||||
for (const p of deviceData.paths.findCompat(
|
||||
path,
|
||||
true,
|
||||
false,
|
||||
path.length,
|
||||
)) {
|
||||
const tracker = deviceData.trackers.get(p);
|
||||
for (const k in tracker)
|
||||
if (tracker[k] & changeFlags) deviceData.changes.add(k);
|
||||
@@ -356,11 +385,11 @@ export function set(
|
||||
|
||||
export function track(
|
||||
deviceData: DeviceData,
|
||||
path: Path,
|
||||
pathStr: string,
|
||||
marker: string,
|
||||
attributes?: string[],
|
||||
): void {
|
||||
path = deviceData.paths.add(path);
|
||||
const path = deviceData.paths.add(pathStr);
|
||||
let f = 1;
|
||||
|
||||
if (attributes)
|
||||
|
||||
+2
-2
@@ -20,12 +20,12 @@ const cidrs: [IPv4 | IPv6, number][] = [];
|
||||
for (const str of FORWARDED_HEADER.split(",").map((s) => s.trim())) {
|
||||
try {
|
||||
cidrs.push(parseCIDR(str));
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Not a valid CIDR format, try parsing as IP
|
||||
try {
|
||||
const ip = parse(str);
|
||||
cidrs.push([ip, ip.toByteArray().length * 8]);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Not a valid IP either, ignore
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ function matchEtag(etag: string, header: string): boolean {
|
||||
if (t.startsWith("W/")) t = t.substring(2);
|
||||
try {
|
||||
t = JSON.parse(t);
|
||||
} catch (e) {
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
if (t === "*") return true;
|
||||
|
||||
+17
-57
@@ -9,6 +9,9 @@ import {
|
||||
putUser,
|
||||
} from "./ui/db.ts";
|
||||
import { del } from "./cache.ts";
|
||||
import BOOTSTRAP_SCRIPT from "../seed/bootstrap.js" with { type: "text" };
|
||||
import DEFAULT_SCRIPT from "../seed/default.js" with { type: "text" };
|
||||
import INFORM_SCRIPT from "../seed/inform.js" with { type: "text" };
|
||||
|
||||
interface Status {
|
||||
users: boolean;
|
||||
@@ -19,58 +22,6 @@ interface Status {
|
||||
overview: boolean;
|
||||
}
|
||||
|
||||
const BOOTSTRAP_SCRIPT = `
|
||||
const now = Date.now();
|
||||
|
||||
// Clear cached data model to force a refresh
|
||||
clear("Device", now);
|
||||
clear("InternetGatewayDevice", now);
|
||||
`.trim();
|
||||
|
||||
const DEFAULT_SCRIPT = `
|
||||
const hourly = Date.now(3600000);
|
||||
|
||||
// Refresh basic parameters hourly
|
||||
declare("InternetGatewayDevice.DeviceInfo.HardwareVersion", {path: hourly, value: hourly});
|
||||
declare("InternetGatewayDevice.DeviceInfo.SoftwareVersion", {path: hourly, value: hourly});
|
||||
declare("InternetGatewayDevice.WANDevice.*.WANConnectionDevice.*.WANIPConnection.*.MACAddress", {path: hourly, value: hourly});
|
||||
declare("InternetGatewayDevice.WANDevice.*.WANConnectionDevice.*.WANIPConnection.*.ExternalIPAddress", {path: hourly, value: hourly});
|
||||
declare("InternetGatewayDevice.LANDevice.*.WLANConfiguration.*.SSID", {path: hourly, value: hourly});
|
||||
// Don't refresh password field periodically because CPEs always report blank passowrds for security reasons
|
||||
declare("InternetGatewayDevice.LANDevice.*.WLANConfiguration.*.KeyPassphrase", {path: hourly, value: 1});
|
||||
declare("InternetGatewayDevice.LANDevice.*.Hosts.Host.*.HostName", {path: hourly, value: hourly});
|
||||
declare("InternetGatewayDevice.LANDevice.*.Hosts.Host.*.IPAddress", {path: hourly, value: hourly});
|
||||
declare("InternetGatewayDevice.LANDevice.*.Hosts.Host.*.MACAddress", {path: hourly, value: hourly});
|
||||
`.trim();
|
||||
|
||||
const INFORM_SCRIPT = `
|
||||
// Device ID as user name
|
||||
const username = declare("DeviceID.ID", {value: 1}).value[0]
|
||||
|
||||
// Password will be fixed for a given device because Math.random() is seeded with device ID by default.
|
||||
const password = Math.trunc(Math.random() * Number.MAX_SAFE_INTEGER).toString(36);
|
||||
|
||||
const informInterval = 300;
|
||||
|
||||
// Refresh values daily
|
||||
const daily = Date.now(86400000);
|
||||
|
||||
// Unique inform offset per device for better load distribution
|
||||
const informTime = daily % 86400000;
|
||||
|
||||
declare("InternetGatewayDevice.ManagementServer.ConnectionRequestUsername", {value: daily}, {value: username});
|
||||
declare("InternetGatewayDevice.ManagementServer.ConnectionRequestPassword", {value: daily}, {value: password});
|
||||
declare("InternetGatewayDevice.ManagementServer.PeriodicInformEnable", {value: daily}, {value: true});
|
||||
declare("InternetGatewayDevice.ManagementServer.PeriodicInformInterval", {value: daily}, {value: informInterval});
|
||||
declare("InternetGatewayDevice.ManagementServer.PeriodicInformTime", {value: daily}, {value: informTime});
|
||||
|
||||
declare("Device.ManagementServer.ConnectionRequestUsername", {value: daily}, {value: username});
|
||||
declare("Device.ManagementServer.ConnectionRequestPassword", {value: daily}, {value: password});
|
||||
declare("Device.ManagementServer.PeriodicInformEnable", {value: daily}, {value: true});
|
||||
declare("Device.ManagementServer.PeriodicInformInterval", {value: daily}, {value: informInterval});
|
||||
declare("Device.ManagementServer.PeriodicInformTime", {value: daily}, {value: informTime});
|
||||
`.trim();
|
||||
|
||||
export async function getStatus(): Promise<Status> {
|
||||
const [configSnapshot, presetCount] = await Promise.all([
|
||||
getRevision(),
|
||||
@@ -79,14 +30,23 @@ export async function getStatus(): Promise<Status> {
|
||||
const users = getUsers(configSnapshot);
|
||||
const ui = getUiConfig(configSnapshot);
|
||||
|
||||
return {
|
||||
const status = {
|
||||
users: !Object.keys(users).length,
|
||||
presets: !presetCount,
|
||||
filters: !Object.keys(ui["filters"]).length,
|
||||
device: !Object.keys(ui["device"]).length,
|
||||
index: !Object.keys(ui["index"]).length,
|
||||
overview: !Object.keys(ui["overview"]).length,
|
||||
filters: true,
|
||||
device: true,
|
||||
index: true,
|
||||
overview: true,
|
||||
};
|
||||
|
||||
for (const k of Object.keys(ui)) {
|
||||
if (k.startsWith("filters.")) status.filters = false;
|
||||
if (k.startsWith("device.")) status.device = false;
|
||||
if (k.startsWith("index.")) status.index = false;
|
||||
if (k.startsWith("overview.")) status.overview = false;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
export async function seed(options: Record<string, boolean>): Promise<void> {
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ interface Instance {
|
||||
}
|
||||
|
||||
export default class InstanceSet {
|
||||
private declare set: Set<Instance>;
|
||||
declare private set: Set<Instance>;
|
||||
|
||||
public constructor() {
|
||||
this.set = new Set();
|
||||
|
||||
+23
-21
@@ -14,6 +14,7 @@ import { flattenDevice } from "./ui/db.ts";
|
||||
import { getRequestOrigin } from "./forwarded.ts";
|
||||
import { acquireLock, releaseLock } from "./lock.ts";
|
||||
import { ResourceLockedError } from "./common/errors.ts";
|
||||
import Expression from "./common/expression.ts";
|
||||
|
||||
const DEVICE_TASKS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tasks\/?$/;
|
||||
const TASKS_REGEX = /^\/tasks\/([a-zA-Z0-9\-_%]+)(\/[a-zA-Z_]*)?$/;
|
||||
@@ -349,6 +350,24 @@ async function handler(
|
||||
const lastInform = (dev["_lastInform"] as Date).getTime();
|
||||
const device = flattenDevice(dev);
|
||||
|
||||
const configCallback = (e: Expression): Expression.Literal => {
|
||||
if (e instanceof Expression.Literal) return e;
|
||||
else if (e instanceof Expression.Parameter) {
|
||||
const p = device[e.path.toString()];
|
||||
if (p != null) return new Expression.Literal(p);
|
||||
} else if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW") return new Expression.Literal(Date.now());
|
||||
if (e.name === "REMOTE_ADDRESS") {
|
||||
for (const root of ["InternetGatewayDevice", "Device"]) {
|
||||
const p = device[`${root}.ManagementServer.ConnectionRequestURL`];
|
||||
if (p != null)
|
||||
return new Expression.Literal(new URL(p as string).hostname);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Expression.Literal(null);
|
||||
};
|
||||
|
||||
let onlineThreshold: number;
|
||||
if (url.searchParams.has("timeout")) {
|
||||
onlineThreshold = parseInt(url.searchParams.get("timeout"));
|
||||
@@ -357,26 +376,9 @@ async function handler(
|
||||
onlineThreshold = getConfig(
|
||||
revision,
|
||||
"cwmp.deviceOnlineThreshold",
|
||||
{},
|
||||
Date.now(),
|
||||
(exp) => {
|
||||
if (!Array.isArray(exp)) return exp;
|
||||
if (exp[0] === "PARAM") {
|
||||
const p = device[exp[1]];
|
||||
if (p?.value) return p.value[0];
|
||||
} else if (exp[0] === "FUNC") {
|
||||
if (exp[1] === "REMOTE_ADDRESS") {
|
||||
for (const root of ["InternetGatewayDevice", "Device"]) {
|
||||
const p =
|
||||
device[`${root}.ManagementServer.ConnectionRequestURL`];
|
||||
if (p?.value) return new URL(p.value[0] as string).hostname;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return exp;
|
||||
},
|
||||
) as number;
|
||||
4000,
|
||||
configCallback,
|
||||
);
|
||||
}
|
||||
|
||||
let status = await apiFunctions.connectionRequest(deviceId, device);
|
||||
@@ -501,7 +503,7 @@ async function handler(
|
||||
};
|
||||
try {
|
||||
await filesBucket.delete(filename as unknown as ObjectId);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Ignore error if file doesn't exist
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -93,7 +93,7 @@ export function start(
|
||||
res.writeHead(500, { Connection: "close" });
|
||||
res.end(`${err.name}: ${err.message}`);
|
||||
}
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
throw err;
|
||||
|
||||
+147
-184
@@ -25,7 +25,6 @@ import {
|
||||
TransferCompleteRequest,
|
||||
Operation,
|
||||
ScriptResult,
|
||||
Expression,
|
||||
GetParameterValues,
|
||||
GetParameterAttributes,
|
||||
GetParameterNames,
|
||||
@@ -42,6 +41,7 @@ import {
|
||||
import { getRequestOrigin } from "./forwarded.ts";
|
||||
import * as logger from "./logger.ts";
|
||||
import { encodeTag } from "./util.ts";
|
||||
import Expression, { Value } from "./common/expression.ts";
|
||||
|
||||
const VALID_PARAM_TYPES = new Set([
|
||||
"xsd:int",
|
||||
@@ -102,29 +102,25 @@ function generateRpcId(sessionContext: SessionContext): string {
|
||||
export function configContextCallback(
|
||||
sessionContext: SessionContext,
|
||||
exp: Expression,
|
||||
): Expression {
|
||||
if (!Array.isArray(exp)) return exp;
|
||||
if (exp[0] === "PARAM" && typeof exp[1] === "string") {
|
||||
let name = exp[1];
|
||||
if (name === "id") name = "DeviceID.ID";
|
||||
else if (name === "serialNumber") name = "DeviceID.SerialNumber";
|
||||
else if (name === "productClass") name = "DeviceID.ProductClass";
|
||||
else if (name === "oui") name = "DeviceID.OUI";
|
||||
else if (name === "remoteAddress")
|
||||
return getRequestOrigin(sessionContext.httpRequest).remoteAddress;
|
||||
|
||||
): Expression.Literal {
|
||||
if (exp instanceof Expression.Literal) return exp;
|
||||
else if (exp instanceof Expression.FunctionCall) {
|
||||
if (exp.name === "NOW")
|
||||
return new Expression.Literal(sessionContext.timestamp);
|
||||
if (exp.name === "REMOTE_ADDRESS")
|
||||
return new Expression.Literal(
|
||||
getRequestOrigin(sessionContext.httpRequest).remoteAddress,
|
||||
);
|
||||
} else if (exp instanceof Expression.Parameter) {
|
||||
const deviceData = sessionContext.deviceData;
|
||||
const paths = deviceData.paths;
|
||||
const path = paths.get(Path.parse(name));
|
||||
const path = paths.get(exp.path.toString());
|
||||
if (path) {
|
||||
const attrs = deviceData.attributes.get(path, 1);
|
||||
if (attrs?.value?.[1]) return attrs.value[1][0];
|
||||
if (attrs?.value?.[1]) return new Expression.Literal(attrs.value[1][0]);
|
||||
}
|
||||
} else if (exp[0] === "FUNC") {
|
||||
if (exp[1] === "REMOTE_ADDRESS")
|
||||
return getRequestOrigin(sessionContext.httpRequest).remoteAddress;
|
||||
}
|
||||
return exp;
|
||||
return new Expression.Literal(null);
|
||||
}
|
||||
|
||||
export async function inform(
|
||||
@@ -132,9 +128,9 @@ export async function inform(
|
||||
rpcReq: InformRequest,
|
||||
): Promise<AcsResponse> {
|
||||
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
|
||||
const params: [Path, number, Attributes][] = [
|
||||
const params: [string, number, Attributes][] = [
|
||||
[
|
||||
Path.parse("DeviceID.Manufacturer"),
|
||||
"DeviceID.Manufacturer",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -144,7 +140,7 @@ export async function inform(
|
||||
],
|
||||
|
||||
[
|
||||
Path.parse("DeviceID.OUI"),
|
||||
"DeviceID.OUI",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -154,7 +150,7 @@ export async function inform(
|
||||
],
|
||||
|
||||
[
|
||||
Path.parse("DeviceID.ProductClass"),
|
||||
"DeviceID.ProductClass",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -164,7 +160,7 @@ export async function inform(
|
||||
],
|
||||
|
||||
[
|
||||
Path.parse("DeviceID.SerialNumber"),
|
||||
"DeviceID.SerialNumber",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -177,7 +173,7 @@ export async function inform(
|
||||
for (const p of rpcReq.parameterList) {
|
||||
const path = p[0];
|
||||
params.push([
|
||||
path,
|
||||
path.toString(),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -187,7 +183,7 @@ export async function inform(
|
||||
}
|
||||
|
||||
params.push([
|
||||
Path.parse("Events.Inform"),
|
||||
"Events.Inform",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -198,7 +194,7 @@ export async function inform(
|
||||
|
||||
for (const e of rpcReq.event) {
|
||||
params.push([
|
||||
Path.parse(`Events.${encodeTag(e.replace(/\s+/g, "_"))}`),
|
||||
`Events.${encodeTag(e.replace(/\s+/g, "_"))}`,
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -210,7 +206,7 @@ export async function inform(
|
||||
|
||||
if (sessionContext.new) {
|
||||
params.push([
|
||||
Path.parse("DeviceID.ID"),
|
||||
"DeviceID.ID",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -219,7 +215,7 @@ export async function inform(
|
||||
},
|
||||
]);
|
||||
params.push([
|
||||
Path.parse("Events.Registered"),
|
||||
"Events.Registered",
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -235,7 +231,7 @@ export async function inform(
|
||||
let toClear = null;
|
||||
for (const p of params) {
|
||||
// Don't need to clear wildcards for Events
|
||||
if (p[0].segments[0] === "Events") {
|
||||
if (p[0].startsWith("Events.")) {
|
||||
device.set(sessionContext.deviceData, p[0], p[1], p[2]);
|
||||
} else {
|
||||
toClear = device.set(
|
||||
@@ -301,69 +297,49 @@ export async function transferComplete(
|
||||
let toClear = null;
|
||||
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
|
||||
|
||||
let p;
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.LastDownload`),
|
||||
);
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
`Downloads.${instance}.LastDownload`,
|
||||
timestamp,
|
||||
{ value: [timestamp, [operation.timestamp, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
);
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.LastFileType`),
|
||||
);
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
`Downloads.${instance}.LastFileType`,
|
||||
timestamp,
|
||||
{ value: [timestamp, [operation.args.fileType, "xsd:string"]] },
|
||||
toClear,
|
||||
);
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.LastFileName`),
|
||||
);
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
`Downloads.${instance}.LastFileName`,
|
||||
timestamp,
|
||||
{ value: [timestamp, [operation.args.fileName, "xsd:string"]] },
|
||||
toClear,
|
||||
);
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.LastTargetFileName`),
|
||||
);
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
`Downloads.${instance}.LastTargetFileName`,
|
||||
timestamp,
|
||||
{ value: [timestamp, [operation.args.targetFileName, "xsd:string"]] },
|
||||
toClear,
|
||||
);
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.StartTime`),
|
||||
);
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
`Downloads.${instance}.StartTime`,
|
||||
timestamp,
|
||||
{ value: [timestamp, [+rpcReq.startTime, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
);
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.CompleteTime`),
|
||||
);
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
`Downloads.${instance}.CompleteTime`,
|
||||
timestamp,
|
||||
{ value: [timestamp, [+rpcReq.completeTime, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
@@ -387,21 +363,21 @@ function revertDownloadParameters(
|
||||
): void {
|
||||
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
|
||||
|
||||
let p;
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.LastDownload`),
|
||||
const lastDownloadPath = sessionContext.deviceData.paths.add(
|
||||
`Downloads.${instance}.LastDownload`,
|
||||
);
|
||||
|
||||
const lastDownload = sessionContext.deviceData.attributes.get(p);
|
||||
const lastDownload =
|
||||
sessionContext.deviceData.attributes.get(lastDownloadPath);
|
||||
|
||||
p = sessionContext.deviceData.paths.add(
|
||||
Path.parse(`Downloads.${instance}.Download`),
|
||||
);
|
||||
|
||||
const toClear = device.set(sessionContext.deviceData, p, timestamp, {
|
||||
const toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
`Downloads.${instance}.Download`,
|
||||
timestamp,
|
||||
{
|
||||
value: [timestamp, [lastDownload?.value[1]?.[0] || 0, "xsd:dateTime"]],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
if (toClear) {
|
||||
for (const c of toClear)
|
||||
@@ -426,11 +402,10 @@ export async function timeoutOperations(
|
||||
throw new Error(`Unknown operation name ${operation.name}`);
|
||||
|
||||
const DOWNLOAD_TIMEOUT =
|
||||
+localCache.getConfig(
|
||||
localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.downloadTimeout",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
3600,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) * 1000;
|
||||
|
||||
@@ -443,11 +418,10 @@ export async function timeoutOperations(
|
||||
commandKey: commandKey,
|
||||
});
|
||||
|
||||
const SUCCESS_ON_TIMEOUT = +localCache.getConfig(
|
||||
const SUCCESS_ON_TIMEOUT = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.downloadSuccessOnTimeout",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
false,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -486,16 +460,15 @@ export async function timeoutOperations(
|
||||
export function addProvisions(
|
||||
sessionContext: SessionContext,
|
||||
channel: string,
|
||||
provisions: [string, ...Expression[]][],
|
||||
provisions: [string, ...Value[]][],
|
||||
): 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.timestamp,
|
||||
32,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) * 2;
|
||||
|
||||
@@ -549,8 +522,7 @@ export function clearProvisions(sessionContext: SessionContext): void {
|
||||
+localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.maxCommitIterations",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
32,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) * 2;
|
||||
|
||||
@@ -848,22 +820,20 @@ function runDeclarations(
|
||||
(path.alias | path.wildcard) & 1 ||
|
||||
path.segments[0] === "VirtualParameters"
|
||||
) {
|
||||
sessionContext.deviceData.paths.add(Path.parse("VirtualParameters"));
|
||||
sessionContext.deviceData.paths.add("VirtualParameters");
|
||||
if ((path.alias | path.wildcard) & 2) {
|
||||
sessionContext.deviceData.paths.add(Path.parse("VirtualParameters.*"));
|
||||
sessionContext.deviceData.paths.add("VirtualParameters.*");
|
||||
for (const k of Object.keys(allVirtualParameters)) {
|
||||
sessionContext.deviceData.paths.add(
|
||||
Path.parse(`VirtualParameters.${k}`),
|
||||
);
|
||||
sessionContext.deviceData.paths.add(`VirtualParameters.${k}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((path.alias | path.wildcard) & 1 || path.segments[0] === "Reboot")
|
||||
sessionContext.deviceData.paths.add(Path.parse("Reboot"));
|
||||
sessionContext.deviceData.paths.add("Reboot");
|
||||
|
||||
if ((path.alias | path.wildcard) & 1 || path.segments[0] === "FactoryReset")
|
||||
sessionContext.deviceData.paths.add(Path.parse("FactoryReset"));
|
||||
sessionContext.deviceData.paths.add("FactoryReset");
|
||||
|
||||
if (path.alias) {
|
||||
const aliasDecs = device.getAliasDeclarations(
|
||||
@@ -871,12 +841,12 @@ function runDeclarations(
|
||||
declaration.pathGet || 1,
|
||||
);
|
||||
for (const ad of aliasDecs) {
|
||||
const p = sessionContext.deviceData.paths.add(ad.path);
|
||||
const p = sessionContext.deviceData.paths.add(ad.path.toString());
|
||||
allDeclareTimestamps.set(
|
||||
p,
|
||||
Math.max(ad.pathGet || 1, allDeclareTimestamps.get(p) || 0),
|
||||
);
|
||||
let attrTrackers;
|
||||
let attrTrackers: string[];
|
||||
if (ad.attrGet) {
|
||||
attrTrackers = Object.keys(ad.attrGet);
|
||||
mergeAttributeTimestamps(p, ad.attrGet);
|
||||
@@ -884,7 +854,7 @@ function runDeclarations(
|
||||
|
||||
device.track(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
p.toString(),
|
||||
"prerequisite",
|
||||
attrTrackers,
|
||||
);
|
||||
@@ -900,14 +870,14 @@ function runDeclarations(
|
||||
mergeAttributeTimestamps(u, declaration.attrGet);
|
||||
}
|
||||
} else {
|
||||
path = sessionContext.deviceData.paths.add(path);
|
||||
path = sessionContext.deviceData.paths.add(path.toString());
|
||||
allDeclareTimestamps.set(
|
||||
path,
|
||||
Math.max(declaration.pathGet || 1, allDeclareTimestamps.get(path) || 0),
|
||||
);
|
||||
if (declaration.attrGet)
|
||||
mergeAttributeTimestamps(path, declaration.attrGet);
|
||||
device.track(sessionContext.deviceData, path, "prerequisite");
|
||||
device.track(sessionContext.deviceData, path.toString(), "prerequisite");
|
||||
}
|
||||
|
||||
if (declaration.attrSet) {
|
||||
@@ -928,7 +898,7 @@ function runDeclarations(
|
||||
}
|
||||
|
||||
if (declaration.pathSet != null) {
|
||||
let minInstances, maxInstances;
|
||||
let minInstances: number, maxInstances: number;
|
||||
if (Array.isArray(declaration.pathSet)) {
|
||||
minInstances = declaration.pathSet[0];
|
||||
maxInstances = declaration.pathSet[1];
|
||||
@@ -938,17 +908,19 @@ function runDeclarations(
|
||||
|
||||
let parent = path.slice(0, -1);
|
||||
|
||||
let keys;
|
||||
if (Array.isArray(path.segments[path.length - 1])) {
|
||||
let keys: Record<string, string>;
|
||||
if (path.segments[path.length - 1] instanceof Expression) {
|
||||
keys = {};
|
||||
for (const [p, v] of path.segments[path.length - 1])
|
||||
keys[p.toString()] = v;
|
||||
for (const [p, v] of device.expressionToAlias(
|
||||
path.segments[path.length - 1] as Expression,
|
||||
))
|
||||
keys[p.toString()] = v as string;
|
||||
} else if (path.segments[path.length - 1] === "*") {
|
||||
keys = {};
|
||||
}
|
||||
|
||||
if (!parent.wildcard && !parent.alias) {
|
||||
parent = sessionContext.deviceData.paths.add(parent);
|
||||
parent = sessionContext.deviceData.paths.add(parent.toString());
|
||||
if (!unpacked)
|
||||
unpacked = device.unpack(sessionContext.deviceData, path);
|
||||
|
||||
@@ -1143,8 +1115,7 @@ export async function rpcRequest(
|
||||
+localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.maxCommitIterations",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
32,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) * 2;
|
||||
|
||||
@@ -1184,7 +1155,7 @@ export async function rpcRequest(
|
||||
);
|
||||
const timestamp = sessionContext.timestamp + sessionContext.iteration;
|
||||
|
||||
let toClear;
|
||||
let toClear: Clear[];
|
||||
|
||||
const allVirtualParameters = localCache.getVirtualParameters(
|
||||
sessionContext.cacheSnapshot,
|
||||
@@ -1198,7 +1169,7 @@ export async function rpcRequest(
|
||||
if (!sessionContext.deviceData.attributes.has(declaration[0])) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
declaration[0],
|
||||
declaration[0].toString(),
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 0] },
|
||||
toClear,
|
||||
@@ -1211,7 +1182,7 @@ export async function rpcRequest(
|
||||
for (const k of Object.keys(allVirtualParameters)) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`VirtualParameters.${k}`),
|
||||
`VirtualParameters.${k}`,
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -1221,7 +1192,7 @@ export async function rpcRequest(
|
||||
}
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
declaration[0],
|
||||
declaration[0].toString(),
|
||||
timestamp,
|
||||
null,
|
||||
toClear,
|
||||
@@ -1235,7 +1206,7 @@ export async function rpcRequest(
|
||||
if (!sessionContext.deviceData.attributes.has(declaration[0])) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
declaration[0],
|
||||
declaration[0].toString(),
|
||||
timestamp,
|
||||
{ object: [timestamp, 0] },
|
||||
toClear,
|
||||
@@ -1247,7 +1218,7 @@ export async function rpcRequest(
|
||||
}
|
||||
}
|
||||
|
||||
for (const p of sessionContext.deviceData.paths.find(
|
||||
for (const p of sessionContext.deviceData.paths.findCompat(
|
||||
declaration[0],
|
||||
false,
|
||||
true,
|
||||
@@ -1290,7 +1261,7 @@ export async function rpcRequest(
|
||||
return rpcRequest(sessionContext, null);
|
||||
}
|
||||
|
||||
let toClear;
|
||||
let toClear: Clear[];
|
||||
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
|
||||
|
||||
// Update tags
|
||||
@@ -1299,7 +1270,7 @@ export async function rpcRequest(
|
||||
if (v && !c) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
p.toString(),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -1311,7 +1282,7 @@ export async function rpcRequest(
|
||||
} else if (c && !v) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
p.toString(),
|
||||
timestamp,
|
||||
null,
|
||||
toClear,
|
||||
@@ -1320,11 +1291,11 @@ export async function rpcRequest(
|
||||
}
|
||||
|
||||
// Downloads
|
||||
let index;
|
||||
let index: number;
|
||||
for (const instance of sessionContext.syncState.downloadsToCreate) {
|
||||
if (index == null) {
|
||||
index = 0;
|
||||
for (const p of sessionContext.deviceData.paths.find(
|
||||
for (const p of sessionContext.deviceData.paths.findCompat(
|
||||
Path.parse("Downloads.*"),
|
||||
false,
|
||||
true,
|
||||
@@ -1341,7 +1312,7 @@ export async function rpcRequest(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse("Downloads"),
|
||||
"Downloads",
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 1] },
|
||||
toClear,
|
||||
@@ -1349,7 +1320,7 @@ export async function rpcRequest(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${index}`),
|
||||
`Downloads.${index}`,
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 1] },
|
||||
toClear,
|
||||
@@ -1383,7 +1354,7 @@ export async function rpcRequest(
|
||||
for (const [k, v] of Object.entries(params)) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${index}.${k}`),
|
||||
`Downloads.${index}.${k}`,
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -1399,7 +1370,7 @@ export async function rpcRequest(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${index}.*`),
|
||||
`Downloads.${index}.*`,
|
||||
timestamp,
|
||||
null,
|
||||
toClear,
|
||||
@@ -1411,7 +1382,7 @@ export async function rpcRequest(
|
||||
for (const instance of sessionContext.syncState.downloadsToDelete) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
instance,
|
||||
instance.toString(),
|
||||
timestamp,
|
||||
null,
|
||||
toClear,
|
||||
@@ -1432,7 +1403,7 @@ export async function rpcRequest(
|
||||
if (val[0] !== attrs.value[1][0]) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
p,
|
||||
p.toString(),
|
||||
timestamp,
|
||||
{ value: [timestamp, val] },
|
||||
toClear,
|
||||
@@ -1504,7 +1475,7 @@ export async function rpcRequest(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`VirtualParameters.${vparams[i][0]}`),
|
||||
`VirtualParameters.${vparams[i][0]}`,
|
||||
timestamp,
|
||||
vpu,
|
||||
toClear,
|
||||
@@ -1527,7 +1498,7 @@ function generateGetRpcRequest(
|
||||
|
||||
for (const path of syncState.refreshAttributes.exist) {
|
||||
let found = false;
|
||||
for (const p of sessionContext.deviceData.paths.find(
|
||||
for (const p of sessionContext.deviceData.paths.findCompat(
|
||||
path,
|
||||
false,
|
||||
true,
|
||||
@@ -1547,7 +1518,9 @@ function generateGetRpcRequest(
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
const p = sessionContext.deviceData.paths.add(path.slice(0, -1));
|
||||
const p = sessionContext.deviceData.paths.add(
|
||||
path.slice(0, -1).toString(),
|
||||
);
|
||||
syncState.gpn.add(p);
|
||||
const f = 1 << p.length;
|
||||
syncState.gpnPatterns.set(p, f | syncState.gpnPatterns.get(p));
|
||||
@@ -1557,7 +1530,7 @@ function generateGetRpcRequest(
|
||||
|
||||
for (const path of syncState.refreshAttributes.object) {
|
||||
let found = false;
|
||||
for (const p of sessionContext.deviceData.paths.find(
|
||||
for (const p of sessionContext.deviceData.paths.findCompat(
|
||||
path,
|
||||
false,
|
||||
true,
|
||||
@@ -1577,7 +1550,9 @@ function generateGetRpcRequest(
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
const p = sessionContext.deviceData.paths.add(path.slice(0, -1));
|
||||
const p = sessionContext.deviceData.paths.add(
|
||||
path.slice(0, -1).toString(),
|
||||
);
|
||||
syncState.gpn.add(p);
|
||||
const f = 1 << p.length;
|
||||
syncState.gpnPatterns.set(p, f | syncState.gpnPatterns.get(p));
|
||||
@@ -1586,7 +1561,7 @@ function generateGetRpcRequest(
|
||||
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).toString());
|
||||
syncState.gpn.add(p);
|
||||
const f = 1 << p.length;
|
||||
syncState.gpnPatterns.set(p, f | syncState.gpnPatterns.get(p));
|
||||
@@ -1597,8 +1572,7 @@ function generateGetRpcRequest(
|
||||
const GPN_NEXT_LEVEL = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.gpnNextLevel",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
0,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) as number;
|
||||
|
||||
@@ -1609,11 +1583,10 @@ function generateGetRpcRequest(
|
||||
|
||||
// Skip root GPN workaround
|
||||
if (path && !path.length) {
|
||||
const SKIP_ROOT_GPN = !!localCache.getConfig(
|
||||
const SKIP_ROOT_GPN = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.skipRootGpn",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
false,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -1634,7 +1607,7 @@ function generateGetRpcRequest(
|
||||
let est = 0;
|
||||
if (path.length >= GPN_NEXT_LEVEL) {
|
||||
const patterns: [Path, number][] = [[path, 0]];
|
||||
for (const p of sessionContext.deviceData.paths.find(
|
||||
for (const p of sessionContext.deviceData.paths.findCompat(
|
||||
path,
|
||||
true,
|
||||
false,
|
||||
@@ -1651,7 +1624,7 @@ function generateGetRpcRequest(
|
||||
syncState.gpn.delete(path);
|
||||
} else {
|
||||
nextLevel = false;
|
||||
for (const p of sessionContext.deviceData.paths.find(
|
||||
for (const p of sessionContext.deviceData.paths.findCompat(
|
||||
path,
|
||||
false,
|
||||
true,
|
||||
@@ -1672,8 +1645,7 @@ function generateGetRpcRequest(
|
||||
const GPV_BATCH_SIZE = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.gpvBatchSize",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
32,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) as number;
|
||||
|
||||
@@ -1703,8 +1675,7 @@ function generateGetRpcRequest(
|
||||
const GPV_BATCH_SIZE = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.gpvBatchSize",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
32,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) as number;
|
||||
|
||||
@@ -1764,11 +1735,10 @@ function generateSetRpcRequest(
|
||||
|
||||
const deviceData = sessionContext.deviceData;
|
||||
|
||||
const SKIP_WRITABLE_CHECK = !!localCache.getConfig(
|
||||
const SKIP_WRITABLE_CHECK = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.skipWritableCheck",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
false,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -1810,24 +1780,21 @@ function generateSetRpcRequest(
|
||||
const GPV_BATCH_SIZE = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.gpvBatchSize",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
32,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
) as number;
|
||||
|
||||
const DATETIME_MILLISECONDS = !!localCache.getConfig(
|
||||
const DATETIME_MILLISECONDS = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.datetimeMilliseconds",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
true,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
const BOOLEAN_LITERAL = !!localCache.getConfig(
|
||||
const BOOLEAN_LITERAL = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.booleanLiteral",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
true,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -1904,15 +1871,13 @@ function generateSetRpcRequest(
|
||||
const t2 = attrs?.value?.[1]?.[0] as number;
|
||||
if (!(t <= t2)) {
|
||||
const fileTypeAttrs = deviceData.attributes.get(
|
||||
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("FileType"))),
|
||||
deviceData.paths.get(p.slice(0, -1).toString() + ".FileType"),
|
||||
);
|
||||
const fileNameAttrs = deviceData.attributes.get(
|
||||
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("FileName"))),
|
||||
deviceData.paths.get(p.slice(0, -1).toString() + ".FileName"),
|
||||
);
|
||||
const targetFileNameAttrs = deviceData.attributes.get(
|
||||
deviceData.paths.get(
|
||||
p.slice(0, -1).concat(Path.parse("TargetFileName")),
|
||||
),
|
||||
deviceData.paths.get(p.slice(0, -1).toString() + ".TargetFileName"),
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -1928,7 +1893,7 @@ function generateSetRpcRequest(
|
||||
|
||||
// Reboot
|
||||
if (syncState.reboot > 0 && syncState.reboot <= sessionContext.timestamp) {
|
||||
const p = sessionContext.deviceData.paths.get(Path.parse("Reboot"));
|
||||
const p = sessionContext.deviceData.paths.get("Reboot");
|
||||
const attrs = p ? sessionContext.deviceData.attributes.get(p) : null;
|
||||
const t = attrs?.value?.[1][0] as number;
|
||||
if (!(t >= syncState.reboot)) {
|
||||
@@ -1942,7 +1907,7 @@ function generateSetRpcRequest(
|
||||
syncState.factoryReset > 0 &&
|
||||
syncState.factoryReset <= sessionContext.timestamp
|
||||
) {
|
||||
const p = sessionContext.deviceData.paths.get(Path.parse("FactoryReset"));
|
||||
const p = sessionContext.deviceData.paths.get("FactoryReset");
|
||||
const attrs = p ? sessionContext.deviceData.attributes.get(p) : null;
|
||||
const t = attrs?.value?.[1][0] as number;
|
||||
if (!(t >= syncState.factoryReset)) {
|
||||
@@ -2064,8 +2029,7 @@ function processDeclarations(
|
||||
const deviceData = sessionContext.deviceData;
|
||||
const syncState = sessionContext.syncState;
|
||||
|
||||
const root = sessionContext.deviceData.paths.add(Path.parse(""));
|
||||
const paths = deviceData.paths.find(root, false, true, 99);
|
||||
const paths = deviceData.paths.findCompat(Path.root, false, true, 99);
|
||||
paths.sort((a, b): number =>
|
||||
a.wildcard === b.wildcard ? a.length - b.length : a.wildcard - b.wildcard,
|
||||
);
|
||||
@@ -2358,7 +2322,7 @@ function processDeclarations(
|
||||
allDeclareAttributeTimestamps.size ||
|
||||
allDeclareAttributeValues.size
|
||||
)
|
||||
func(root, 1, 0, paths);
|
||||
func(Path.root, 1, 0, [Path.root, ...paths]);
|
||||
|
||||
return virtualParameterDeclarations;
|
||||
}
|
||||
@@ -2372,7 +2336,7 @@ function processInstances(
|
||||
maxInstances: number,
|
||||
defer: boolean,
|
||||
): void {
|
||||
parent = sessionContext.deviceData.paths.add(parent);
|
||||
parent = sessionContext.deviceData.paths.add(parent.toString());
|
||||
let instancesToCreate: InstanceSet, instancesToDelete: Set<Path>;
|
||||
if (parent.segments[0] === "Downloads") {
|
||||
if (parent.length !== 1) return;
|
||||
@@ -2491,19 +2455,17 @@ export async function rpcResponse(
|
||||
if (!parameterList.length) {
|
||||
sessionContext.rpcRequest = null;
|
||||
} else {
|
||||
const DATETIME_MILLISECONDS = !!localCache.getConfig(
|
||||
const DATETIME_MILLISECONDS = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.datetimeMilliseconds",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
true,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
const BOOLEAN_LITERAL = !!localCache.getConfig(
|
||||
const BOOLEAN_LITERAL = localCache.getConfig(
|
||||
sessionContext.cacheSnapshot,
|
||||
"cwmp.booleanLiteral",
|
||||
{},
|
||||
sessionContext.timestamp,
|
||||
true,
|
||||
(e) => configContextCallback(sessionContext, e),
|
||||
);
|
||||
|
||||
@@ -2543,7 +2505,7 @@ export async function rpcResponse(
|
||||
}
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
path,
|
||||
path.toString(),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -2562,7 +2524,7 @@ export async function rpcResponse(
|
||||
});
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(p),
|
||||
p,
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
@@ -2589,7 +2551,7 @@ export async function rpcResponse(
|
||||
}
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
path,
|
||||
path.toString(),
|
||||
timestamp,
|
||||
{
|
||||
notification: [timestamp, notification],
|
||||
@@ -2608,7 +2570,7 @@ export async function rpcResponse(
|
||||
});
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(p),
|
||||
p,
|
||||
timestamp,
|
||||
{
|
||||
notification: [timestamp, 0],
|
||||
@@ -2623,7 +2585,8 @@ export async function rpcResponse(
|
||||
return invalidResponse("Response name does not match request name");
|
||||
|
||||
let root: Path;
|
||||
if (rpcReq.parameterPath.endsWith("."))
|
||||
if (!rpcReq.parameterPath) root = Path.root;
|
||||
else if (rpcReq.parameterPath.endsWith("."))
|
||||
root = Path.parse(rpcReq.parameterPath.slice(0, -1));
|
||||
else root = Path.parse(rpcReq.parameterPath);
|
||||
|
||||
@@ -2667,7 +2630,7 @@ export async function rpcResponse(
|
||||
"VirtualParameters",
|
||||
"Downloads",
|
||||
]) {
|
||||
const p = sessionContext.deviceData.paths.get(Path.parse(n));
|
||||
const p = sessionContext.deviceData.paths.get(n);
|
||||
if (p && sessionContext.deviceData.attributes.has(p))
|
||||
sessionContext.deviceData.timestamps.set(p, timestamp);
|
||||
}
|
||||
@@ -2693,7 +2656,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
path,
|
||||
path.toString(),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, object ? 1 : 0],
|
||||
@@ -2706,7 +2669,7 @@ export async function rpcResponse(
|
||||
for (const path of wildcardParams) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
path,
|
||||
path.toString(),
|
||||
timestamp,
|
||||
null,
|
||||
toClear,
|
||||
@@ -2719,7 +2682,7 @@ export async function rpcResponse(
|
||||
for (const p of rpcReq.parameterList) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(p[0]),
|
||||
p[0],
|
||||
timestamp + 1,
|
||||
{
|
||||
object: [timestamp + 1, 0],
|
||||
@@ -2755,7 +2718,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(p[0]),
|
||||
p[0],
|
||||
timestamp + 1,
|
||||
attrs,
|
||||
toClear,
|
||||
@@ -2767,7 +2730,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(rpcReq.objectName + rpcRes.instanceNumber),
|
||||
rpcReq.objectName + rpcRes.instanceNumber,
|
||||
timestamp + 1,
|
||||
{ object: [timestamp + 1, 1] },
|
||||
toClear,
|
||||
@@ -2778,7 +2741,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(rpcReq.objectName.slice(0, -1)),
|
||||
rpcReq.objectName.slice(0, -1),
|
||||
timestamp + 1,
|
||||
null,
|
||||
toClear,
|
||||
@@ -2789,7 +2752,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse("Reboot"),
|
||||
"Reboot",
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
@@ -2800,7 +2763,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse("FactoryReset"),
|
||||
"FactoryReset",
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
@@ -2811,7 +2774,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.Download`),
|
||||
`Downloads.${rpcReq.instance}.Download`,
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
@@ -2820,7 +2783,7 @@ export async function rpcResponse(
|
||||
if (rpcRes.status === 0) {
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.LastDownload`),
|
||||
`Downloads.${rpcReq.instance}.LastDownload`,
|
||||
timestamp + 1,
|
||||
{
|
||||
value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]],
|
||||
@@ -2830,7 +2793,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.LastFileType`),
|
||||
`Downloads.${rpcReq.instance}.LastFileType`,
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [rpcReq.fileType, "xsd:string"]] },
|
||||
toClear,
|
||||
@@ -2838,7 +2801,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.LastFileName`),
|
||||
`Downloads.${rpcReq.instance}.LastFileName`,
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [rpcReq.fileType, "xsd:string"]] },
|
||||
toClear,
|
||||
@@ -2846,7 +2809,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.LastTargetFileName`),
|
||||
`Downloads.${rpcReq.instance}.LastTargetFileName`,
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [rpcReq.fileType, "xsd:string"]] },
|
||||
toClear,
|
||||
@@ -2854,7 +2817,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.StartTime`),
|
||||
`Downloads.${rpcReq.instance}.StartTime`,
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [+rpcRes.startTime, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
@@ -2862,7 +2825,7 @@ export async function rpcResponse(
|
||||
|
||||
toClear = device.set(
|
||||
sessionContext.deviceData,
|
||||
Path.parse(`Downloads.${rpcReq.instance}.CompleteTime`),
|
||||
`Downloads.${rpcReq.instance}.CompleteTime`,
|
||||
timestamp + 1,
|
||||
{ value: [timestamp + 1, [+rpcRes.completeTime, "xsd:dateTime"]] },
|
||||
toClear,
|
||||
@@ -2977,7 +2940,7 @@ export async function deserialize(
|
||||
|
||||
const deviceData = initDeviceData();
|
||||
for (const r of sessionContext.deviceData as unknown as any[]) {
|
||||
const path = deviceData.paths.add(Path.parse(r[0]));
|
||||
const path = deviceData.paths.add(r[0]);
|
||||
|
||||
if (r[1]) deviceData.trackers.set(path, r[1]);
|
||||
|
||||
@@ -2999,8 +2962,8 @@ export async function serialize(
|
||||
): Promise<string> {
|
||||
const deviceData = [];
|
||||
|
||||
for (const path of sessionContext.deviceData.paths.find(
|
||||
Path.parse(""),
|
||||
for (const path of sessionContext.deviceData.paths.findCompat(
|
||||
Path.root,
|
||||
false,
|
||||
false,
|
||||
99,
|
||||
|
||||
+3
-3
@@ -119,7 +119,7 @@ function parameterInfoList(xml: Element): [Path, boolean, boolean][] {
|
||||
if (param && !param.endsWith("."))
|
||||
return [Path.parse(param), false, parsed];
|
||||
else return [Path.parse(param.slice(0, -1)), true, parsed];
|
||||
} catch (err) {
|
||||
} catch {
|
||||
warnings.push({
|
||||
message: "Missing or invalid XML node",
|
||||
element: "Name",
|
||||
@@ -201,7 +201,7 @@ function parameterValueList(
|
||||
}
|
||||
try {
|
||||
return [Path.parse(param), parsed, valueType];
|
||||
} catch (err) {
|
||||
} catch {
|
||||
warnings.push({
|
||||
message: "Missing or invalid XML node",
|
||||
element: "Name",
|
||||
@@ -250,7 +250,7 @@ function parameterAttributeList(xml: Element): [Path, number, string[]][] {
|
||||
|
||||
try {
|
||||
return [Path.parse(param), notification, accessList];
|
||||
} catch (err) {
|
||||
} catch {
|
||||
warnings.push({
|
||||
message: "Missing or invalid XML node",
|
||||
element: "Name",
|
||||
|
||||
+5
-15
@@ -4,8 +4,7 @@ import Path from "./common/path.ts";
|
||||
import PathSet from "./common/path-set.ts";
|
||||
import VersionedMap from "./versioned-map.ts";
|
||||
import InstanceSet from "./instance-set.ts";
|
||||
|
||||
export type Expression = string | number | boolean | null | any[];
|
||||
import Expression, { Value } from "./common/expression.ts";
|
||||
|
||||
export interface Fault {
|
||||
code: string;
|
||||
@@ -160,7 +159,7 @@ export interface Task {
|
||||
fileName?: string;
|
||||
targetFileName?: string;
|
||||
expiry?: number;
|
||||
provisions?: [string, ...Expression[]][];
|
||||
provisions?: [string, ...Value[]][];
|
||||
}
|
||||
|
||||
export interface Operation {
|
||||
@@ -470,7 +469,7 @@ export interface Permissions {
|
||||
[resource: string]: {
|
||||
access: number;
|
||||
filter: Expression;
|
||||
validate?: Expression;
|
||||
validate: Expression;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -479,7 +478,7 @@ export interface Permissions {
|
||||
export type PermissionSet = {
|
||||
[resource: string]: {
|
||||
access: number;
|
||||
validate?: Expression;
|
||||
validate: Expression;
|
||||
filter: Expression;
|
||||
};
|
||||
}[];
|
||||
@@ -488,16 +487,7 @@ export interface Config {
|
||||
[name: string]: Expression;
|
||||
}
|
||||
|
||||
export interface UiConfig {
|
||||
filters: Record<string, unknown>;
|
||||
device: Record<string, unknown>;
|
||||
index: Record<string, unknown>;
|
||||
overview: {
|
||||
charts?: Record<string, unknown>;
|
||||
groups?: Record<string, unknown>;
|
||||
};
|
||||
pageSize?: Expression;
|
||||
}
|
||||
export type UiConfig = Record<string, string>;
|
||||
|
||||
export interface SoapMessage {
|
||||
id: string;
|
||||
|
||||
@@ -105,6 +105,9 @@ router.post("/login", async (ctx) => {
|
||||
|
||||
const username = ctx.request.body.username;
|
||||
const password = ctx.request.body.password;
|
||||
const remember = ctx.request.body.remember;
|
||||
const TWO_WEEKS_SECS = 1209600;
|
||||
const ONE_DAY_SECS = 86400;
|
||||
|
||||
const log = {
|
||||
message: "Log in",
|
||||
@@ -115,8 +118,14 @@ router.post("/login", async (ctx) => {
|
||||
|
||||
function success(authMethod): void {
|
||||
log.method = authMethod;
|
||||
const token = jwt.sign({ username, authMethod }, JWT_SECRET);
|
||||
ctx.cookies.set(JWT_COOKIE, token, { sameSite: "lax" });
|
||||
const expiresIn = remember ? TWO_WEEKS_SECS : ONE_DAY_SECS;
|
||||
const token = jwt.sign({ username, authMethod }, JWT_SECRET, {
|
||||
expiresIn,
|
||||
});
|
||||
ctx.cookies.set(JWT_COOKIE, token, {
|
||||
sameSite: "lax",
|
||||
maxAge: remember ? expiresIn * 1000 : null,
|
||||
});
|
||||
ctx.body = JSON.stringify(token);
|
||||
logger.accessInfo(log);
|
||||
}
|
||||
@@ -205,8 +214,17 @@ router.get("/", async (ctx) => {
|
||||
// be problematic when using relatives asset paths in HTML
|
||||
if (ctx.path.endsWith("//")) return;
|
||||
|
||||
const permissionSets: PermissionSet[] =
|
||||
ctx.state.authorizer.getPermissionSets();
|
||||
const ps: PermissionSet[] = ctx.state.authorizer.getPermissionSets();
|
||||
const permissionSets = ps.map((p) =>
|
||||
p.map((s) =>
|
||||
Object.fromEntries(
|
||||
Object.entries(s).map(([resource, { access, validate, filter }]) => [
|
||||
resource,
|
||||
{ access, validate: validate.toString(), filter: filter.toString() },
|
||||
]),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
let wizard = "";
|
||||
if (!Object.keys(localCache.getUsers(ctx.state.configSnapshot)).length)
|
||||
@@ -219,12 +237,10 @@ router.get("/", async (ctx) => {
|
||||
<link rel="shortcut icon" type="image/png" href="${FAVICON_PNG}" />
|
||||
<link rel="stylesheet" href="${APP_CSS}">
|
||||
</head>
|
||||
<body>
|
||||
<body class="h-full bg-stone-100">
|
||||
<noscript>GenieACS UI requires JavaScript to work. Please enable JavaScript in your browser.</noscript>
|
||||
<script>
|
||||
window.clientConfig = ${JSON.stringify({
|
||||
ui: localCache.getUiConfig(ctx.state.configSnapshot),
|
||||
})};
|
||||
window.clientConfig = ${JSON.stringify(localCache.getUiConfig(ctx.state.configSnapshot))};
|
||||
window.configSnapshot = ${JSON.stringify(ctx.state.configSnapshot)};
|
||||
window.genieacsVersion = ${JSON.stringify(VERSION)};
|
||||
window.username = ${JSON.stringify(
|
||||
|
||||
+198
-126
@@ -3,11 +3,11 @@ import Router from "@koa/router";
|
||||
import { ObjectId } from "mongodb";
|
||||
import * as db from "./db.ts";
|
||||
import * as apiFunctions from "../api-functions.ts";
|
||||
import { evaluate, and, extractParams } from "../common/expression/util.ts";
|
||||
import { parse } from "../common/expression/parser.ts";
|
||||
import Expression, { extractPaths } from "../common/expression.ts";
|
||||
import Path from "../common/path.ts";
|
||||
import * as logger from "../logger.ts";
|
||||
import { getConfig } from "../ui/local-cache.ts";
|
||||
import { Expression, Task } from "../types.ts";
|
||||
import { Task } from "../types.ts";
|
||||
import { generateSalt, hashPassword } from "../auth.ts";
|
||||
import { del } from "../cache.ts";
|
||||
import Authorizer from "../common/authorizer.ts";
|
||||
@@ -67,11 +67,14 @@ router.get(`/devices/:id.csv`, async (ctx) => {
|
||||
id: ctx.params.id,
|
||||
};
|
||||
|
||||
const filter = and(authorizer.getFilter("devices", 2), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter("devices", 2),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS.devices],
|
||||
ctx.params.id,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS.devices)),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
|
||||
if (!authorizer.hasAccess("devices", 2)) {
|
||||
logUnauthorizedWarning(log);
|
||||
@@ -89,36 +92,51 @@ router.get(`/devices/:id.csv`, async (ctx) => {
|
||||
);
|
||||
|
||||
const lines: string[] = [
|
||||
"Parameter,Object,Object timestamp,Writable,Writable timestamp,Value,Value type,Value timestamp,Notification,Notification timestamp,Access list,Access list timestamp",
|
||||
"Parameter,Object,Writable,Value,Value type,Timestamp,Value timestamp,Notification,Access list,Attributes timestamp",
|
||||
];
|
||||
|
||||
for (const k of Object.keys(device).sort()) {
|
||||
const p = device[k];
|
||||
let value = "";
|
||||
let type = "";
|
||||
if (p.value) {
|
||||
value = p.value[0];
|
||||
type = p.value[1];
|
||||
if (type === "xsd:dateTime" && typeof value === "number")
|
||||
const keys = Object.keys(device).sort();
|
||||
let prevParam = "";
|
||||
let attrs: Record<string, string | number | boolean | null> = {};
|
||||
|
||||
function flushRow(): void {
|
||||
if (!prevParam) return;
|
||||
let value: string | number | boolean | null = attrs["value"] ?? "";
|
||||
if (attrs["type"] === "xsd:dateTime" && typeof value === "number")
|
||||
value = new Date(value).toJSON();
|
||||
}
|
||||
|
||||
const row = [
|
||||
k,
|
||||
p.object,
|
||||
new Date(p.objectTimestamp).toJSON(),
|
||||
p.writable,
|
||||
new Date(p.writableTimestamp).toJSON(),
|
||||
prevParam,
|
||||
attrs["object"] ?? "",
|
||||
attrs["writable"] ?? "",
|
||||
`"${String(value).replace(/"/g, '""')}"`,
|
||||
type,
|
||||
new Date(p.valueTimestamp).toJSON(),
|
||||
p.notification,
|
||||
new Date(p.notificationTimestamp).toJSON(),
|
||||
p.accessList ? p.accessList.join(", ") : "",
|
||||
new Date(p.accessListTimestamp).toJSON(),
|
||||
attrs["type"] ?? "",
|
||||
attrs["timestamp"] != null ? new Date(+attrs["timestamp"]).toJSON() : "",
|
||||
attrs["valueTimestamp"] != null
|
||||
? new Date(+attrs["valueTimestamp"]).toJSON()
|
||||
: "",
|
||||
attrs["notification"] ?? "",
|
||||
attrs["accessList"] ?? "",
|
||||
attrs["attributesTimestamp"] != null
|
||||
? new Date(+attrs["attributesTimestamp"]).toJSON()
|
||||
: "",
|
||||
];
|
||||
lines.push(row.map((r) => (r != null ? r : "")).join(","));
|
||||
}
|
||||
|
||||
for (const k of keys) {
|
||||
const colonIdx = k.lastIndexOf(":");
|
||||
const param = colonIdx === -1 ? k : k.slice(0, colonIdx);
|
||||
const attr = colonIdx === -1 ? "value" : k.slice(colonIdx + 1);
|
||||
|
||||
if (param !== prevParam) {
|
||||
flushRow();
|
||||
prevParam = param;
|
||||
attrs = {};
|
||||
}
|
||||
attrs[attr] = device[k];
|
||||
}
|
||||
flushRow();
|
||||
ctx.body = lines.join("\n");
|
||||
logger.accessInfo(log);
|
||||
});
|
||||
@@ -128,7 +146,10 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
const authorizer: Authorizer = ctx.state.authorizer;
|
||||
let filter: Expression = authorizer.getFilter(resource, 1);
|
||||
if (ctx.request.query.filter)
|
||||
filter = and(filter, parse(singleParam(ctx.request.query.filter)));
|
||||
filter = Expression.and(
|
||||
filter,
|
||||
Expression.parse(singleParam(ctx.request.query.filter)),
|
||||
);
|
||||
|
||||
const log = {
|
||||
message: `Count ${resource}`,
|
||||
@@ -144,11 +165,18 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
|
||||
// Exclude temporary tasks and faults
|
||||
if (resource === "tasks" || resource === "faults") {
|
||||
filter = and(filter, [
|
||||
"OR",
|
||||
[">=", ["PARAM", "expiry"], Date.now() + 60000],
|
||||
["IS NULL", ["PARAM", "expiry"]],
|
||||
]);
|
||||
const p = new Expression.Parameter(Path.parse("expiry"));
|
||||
filter = Expression.and(
|
||||
filter,
|
||||
Expression.or(
|
||||
new Expression.Binary(
|
||||
">=",
|
||||
p,
|
||||
new Expression.Literal(Date.now() + 60000),
|
||||
),
|
||||
new Expression.Unary("IS NULL", p),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const count = await db.count(resource, filter);
|
||||
@@ -165,7 +193,10 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
const options: Parameters<typeof db.query>[2] = {};
|
||||
let filter: Expression = authorizer.getFilter(resource, 2);
|
||||
if (ctx.request.query.filter)
|
||||
filter = and(filter, parse(singleParam(ctx.request.query.filter)));
|
||||
filter = Expression.and(
|
||||
filter,
|
||||
Expression.parse(singleParam(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;
|
||||
if (ctx.request.query.sort)
|
||||
@@ -193,11 +224,18 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
|
||||
// Exclude temporary tasks and faults
|
||||
if (resource === "tasks" || resource === "faults") {
|
||||
filter = and(filter, [
|
||||
"OR",
|
||||
[">=", ["PARAM", "expiry"], Date.now() + 60000],
|
||||
["IS NULL", ["PARAM", "expiry"]],
|
||||
]);
|
||||
const p = new Expression.Parameter(Path.parse("expiry"));
|
||||
filter = Expression.and(
|
||||
filter,
|
||||
Expression.or(
|
||||
new Expression.Binary(
|
||||
">=",
|
||||
p,
|
||||
new Expression.Literal(Date.now() + 60000),
|
||||
),
|
||||
new Expression.Unary("IS NULL", p),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
logger.accessInfo(log);
|
||||
@@ -219,7 +257,10 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
const options: Parameters<typeof db.query>[2] = { projection: {} };
|
||||
let filter: Expression = authorizer.getFilter(resource, 2);
|
||||
if (ctx.request.query.filter)
|
||||
filter = and(filter, parse(singleParam(ctx.request.query.filter)));
|
||||
filter = Expression.and(
|
||||
filter,
|
||||
Expression.parse(singleParam(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;
|
||||
if (ctx.request.query.sort)
|
||||
@@ -239,25 +280,38 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
return void (ctx.status = 403);
|
||||
}
|
||||
|
||||
const columns: Record<string, Expression> = JSON.parse(
|
||||
const columnsStr: Record<string, string> = JSON.parse(
|
||||
singleParam(ctx.request.query.columns),
|
||||
);
|
||||
const now = Date.now();
|
||||
|
||||
for (const [k, v] of Object.entries(columns)) {
|
||||
const e = evaluate(parse(v as string), null, now);
|
||||
columns[k] = e;
|
||||
for (const p of extractParams(e))
|
||||
if (typeof p === "string") options.projection[p] = 1;
|
||||
}
|
||||
const now = Date.now();
|
||||
const columns: Record<string, Expression> = Object.fromEntries(
|
||||
Object.entries(columnsStr).map(([k, v]) => {
|
||||
let exp = Expression.parse(v);
|
||||
exp = exp.evaluate((e) => {
|
||||
if (e instanceof Expression.FunctionCall && e.name === "NOW")
|
||||
return new Expression.Literal(now);
|
||||
return e;
|
||||
});
|
||||
for (const p of extractPaths(exp)) options.projection[p.toString()] = 1;
|
||||
return [k, exp];
|
||||
}),
|
||||
);
|
||||
|
||||
// Exclude temporary tasks and faults
|
||||
if (resource === "tasks" || resource === "faults") {
|
||||
filter = and(filter, [
|
||||
"OR",
|
||||
[">=", ["PARAM", "expiry"], Date.now() + 60000],
|
||||
["IS NULL", ["PARAM", "expiry"]],
|
||||
]);
|
||||
const p = new Expression.Parameter(Path.parse("expiry"));
|
||||
filter = Expression.and(
|
||||
filter,
|
||||
Expression.or(
|
||||
new Expression.Binary(
|
||||
">=",
|
||||
p,
|
||||
new Expression.Literal(Date.now() + 60000),
|
||||
),
|
||||
new Expression.Unary("IS NULL", p),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
logger.accessInfo(log);
|
||||
@@ -272,45 +326,40 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
"\n";
|
||||
for await (const obj of db.query(resource, filter, options)) {
|
||||
const arr = Object.values(columns).map((exp) => {
|
||||
const v = evaluate(exp, obj, null, (e) => {
|
||||
if (Array.isArray(e)) {
|
||||
if (e[0] === "PARAM") {
|
||||
return exp.evaluate((e) => {
|
||||
if (e instanceof Expression.Literal) return e;
|
||||
else if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW") return new Expression.Literal(now);
|
||||
if (e.name === "DATE_STRING") {
|
||||
if (e.args[0] instanceof Expression.Literal)
|
||||
return new Expression.Literal(
|
||||
new Date(e.args[0].value as number).toJSON(),
|
||||
);
|
||||
}
|
||||
} else if (e instanceof Expression.Parameter) {
|
||||
let v = obj[e.path.toString()];
|
||||
if (resource === "devices") {
|
||||
if (e[1] === "Tags") {
|
||||
if (e.path.toString() === "Tags") {
|
||||
const tags = [];
|
||||
for (const p in obj)
|
||||
if (p.startsWith("Tags."))
|
||||
if (p.startsWith("Tags.") && p.lastIndexOf(":") === -1)
|
||||
tags.push(decodeTag(p.slice(5)));
|
||||
|
||||
return tags.join(", ");
|
||||
v = tags.join(", ");
|
||||
}
|
||||
if (e === exp) {
|
||||
const p = obj[e[1]];
|
||||
if (
|
||||
p &&
|
||||
p.value &&
|
||||
p.value[1] === "xsd:dateTime" &&
|
||||
typeof p.value[0] === "number"
|
||||
)
|
||||
return new Date(p.value[0]).toJSON();
|
||||
const type = obj[e.path.toString() + ":type"];
|
||||
if (type === "xsd:dateTime" && typeof v === "number")
|
||||
v = new Date(v).toJSON();
|
||||
}
|
||||
} else if (resource === "faults") {
|
||||
if (e[1] === "detail") return yamlStringify(obj["detail"]);
|
||||
}
|
||||
} else if (e[0] === "FUNC") {
|
||||
if (e[1] === "DATE_STRING") {
|
||||
if (e[2] && !Array.isArray(e[2]))
|
||||
return new Date(e[2]).toJSON();
|
||||
}
|
||||
}
|
||||
if (e.path.toString() === "detail") v = yamlStringify(v);
|
||||
}
|
||||
|
||||
return e;
|
||||
});
|
||||
|
||||
if (Array.isArray(v) || v == null) return "";
|
||||
if (typeof v === "string") return `"${v.replace(/"/g, '""')}"`;
|
||||
return v;
|
||||
if (typeof v === "string") v = `"${v.replace(/"/g, '""')}"`;
|
||||
if (v != null) return new Expression.Literal(v);
|
||||
}
|
||||
return new Expression.Literal(null);
|
||||
}).value;
|
||||
});
|
||||
yield arr.join(",") + "\n";
|
||||
}
|
||||
@@ -326,11 +375,15 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
filter: `${RESOURCE_IDS[resource]} = "${ctx.params.id}"`,
|
||||
};
|
||||
|
||||
const filter = and(authorizer.getFilter(resource, 2), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter(resource, 2),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS[resource]],
|
||||
ctx.params.id,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS[resource])),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
|
||||
if (!authorizer.hasAccess(resource, 2)) {
|
||||
logUnauthorizedWarning(log);
|
||||
return void (ctx.status = 403);
|
||||
@@ -352,11 +405,14 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
filter: `${RESOURCE_IDS[resource]} = "${ctx.params.id}"`,
|
||||
};
|
||||
|
||||
const filter = and(authorizer.getFilter(resource, 2), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter(resource, 2),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS[resource]],
|
||||
ctx.params.id,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS[resource])),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
if (!authorizer.hasAccess(resource, 2)) {
|
||||
logUnauthorizedWarning(log);
|
||||
return void (ctx.status = 403);
|
||||
@@ -379,11 +435,14 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
id: ctx.params.id,
|
||||
};
|
||||
|
||||
const filter = and(authorizer.getFilter(resource, 3), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter(resource, 3),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS[resource]],
|
||||
ctx.params.id,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS[resource])),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
if (!authorizer.hasAccess(resource, 3)) {
|
||||
logUnauthorizedWarning(log);
|
||||
return void (ctx.status = 403);
|
||||
@@ -467,11 +526,14 @@ router.get("/blob/files/:id", async (ctx) => {
|
||||
id: id,
|
||||
};
|
||||
|
||||
const filter = and(authorizer.getFilter(resource, 2), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter(resource, 2),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS[resource]],
|
||||
ctx.params.id,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS[resource])),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
|
||||
if (!authorizer.hasAccess(resource, 2)) {
|
||||
logUnauthorizedWarning(log);
|
||||
@@ -519,7 +581,7 @@ router.put("/files/:id", async (ctx) => {
|
||||
|
||||
try {
|
||||
await db.deleteFile(id);
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// File doesn't exist, ignore
|
||||
}
|
||||
|
||||
@@ -566,11 +628,14 @@ router.post("/devices/:id/tasks", async (ctx) => {
|
||||
let statuses: { _id: string; status: string }[];
|
||||
|
||||
try {
|
||||
const filter = and(authorizer.getFilter("devices", 3), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter("devices", 3),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", "DeviceID.ID"],
|
||||
deviceId,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS["devices"])),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
device = (await db.query("devices", filter).next()).value;
|
||||
if (!device) return void (ctx.status = 404);
|
||||
|
||||
@@ -596,30 +661,31 @@ router.post("/devices/:id/tasks", async (ctx) => {
|
||||
await releaseLock(`cwmp_session_${deviceId}`, token);
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
const onlineThreshold = getConfig(
|
||||
ctx.state.configSnapshot,
|
||||
"cwmp.deviceOnlineThreshold",
|
||||
{},
|
||||
Date.now(),
|
||||
4000,
|
||||
(exp) => {
|
||||
if (!Array.isArray(exp)) return exp;
|
||||
if (exp[0] === "PARAM") {
|
||||
const p = device[exp[1]];
|
||||
if (p?.value) return p.value[0];
|
||||
} else if (exp[0] === "FUNC") {
|
||||
if (exp[1] === "REMOTE_ADDRESS") {
|
||||
if (exp instanceof Expression.Literal) return exp;
|
||||
else if (exp instanceof Expression.Parameter) {
|
||||
const p = device[exp.path.toString()];
|
||||
if (p != null) return new Expression.Literal(p);
|
||||
} else if (exp instanceof Expression.FunctionCall) {
|
||||
if (exp.name === "NOW") return new Expression.Literal(now);
|
||||
if (exp.name === "REMOTE_ADDRESS") {
|
||||
for (const root of ["InternetGatewayDevice", "Device"]) {
|
||||
const p = device[`${root}.ManagementServer.ConnectionRequestURL`];
|
||||
if (p?.value) return new URL(p.value[0]).hostname;
|
||||
}
|
||||
return null;
|
||||
if (p != null) return new Expression.Literal(new URL(p).hostname);
|
||||
}
|
||||
}
|
||||
return exp;
|
||||
}
|
||||
return new Expression.Literal(null);
|
||||
},
|
||||
) as number;
|
||||
);
|
||||
|
||||
const lastInform = device["Events.Inform"].value[0] as number;
|
||||
const lastInform = device["Events.Inform"] as number;
|
||||
|
||||
let status = await apiFunctions.connectionRequest(deviceId, device);
|
||||
if (!status) {
|
||||
@@ -667,11 +733,14 @@ router.post("/devices/:id/tags", async (ctx) => {
|
||||
tags: ctx.request.body,
|
||||
};
|
||||
|
||||
const filter = and(authorizer.getFilter("devices", 3), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter("devices", 3),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", "DeviceID.ID"],
|
||||
ctx.params.id,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS["devices"])),
|
||||
new Expression.Literal(ctx.params.id),
|
||||
),
|
||||
);
|
||||
if (!authorizer.hasAccess("devices", 3)) {
|
||||
logUnauthorizedWarning(log);
|
||||
return void (ctx.status = 403);
|
||||
@@ -745,11 +814,14 @@ router.put("/users/:id/password", async (ctx) => {
|
||||
const newPassword = ctx.request.body.newPassword;
|
||||
|
||||
if (ctx.state.user) {
|
||||
const filter = and(authorizer.getFilter("users", 3), [
|
||||
const filter = Expression.and(
|
||||
authorizer.getFilter("users", 3),
|
||||
new Expression.Binary(
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS.users],
|
||||
username,
|
||||
]);
|
||||
new Expression.Parameter(Path.parse(RESOURCE_IDS["users"])),
|
||||
new Expression.Literal(username),
|
||||
),
|
||||
);
|
||||
const { value: res } = await db.query("users", filter).next();
|
||||
if (!res) return void (ctx.status = 404);
|
||||
const validate = authorizer.getValidator("users", res);
|
||||
|
||||
+66
-136
@@ -2,12 +2,11 @@ import { Script } from "node:vm";
|
||||
import { Readable } from "node:stream";
|
||||
import { Collection, ObjectId, WithoutId } from "mongodb";
|
||||
import { encodeTag } from "../util.ts";
|
||||
import { evaluate } from "../common/expression/util.ts";
|
||||
import { Expression, Fault, Task } from "../types.ts";
|
||||
import { Fault, Task } from "../types.ts";
|
||||
import { collections, filesBucket } from "../db/db.ts";
|
||||
import { convertOldPrecondition, optimizeProjection } from "../db/util.ts";
|
||||
import * as MongoTypes from "../db/types.ts";
|
||||
import { parse, parseList, stringify } from "../common/expression/parser.ts";
|
||||
import Expression, { parseList, Value } from "../common/expression.ts";
|
||||
import { toMongoQuery } from "../db/synth.ts";
|
||||
|
||||
function processDeviceProjection(
|
||||
@@ -61,21 +60,8 @@ function parseDate(d: Date): number | string {
|
||||
return isNaN(n) ? "" + d : n;
|
||||
}
|
||||
|
||||
interface FlatAttributes {
|
||||
object?: boolean;
|
||||
objectTimestamp?: number;
|
||||
writable?: boolean;
|
||||
writableTimestamp?: number;
|
||||
value?: [string | number | boolean, string];
|
||||
valueTimestamp?: number;
|
||||
notification?: number;
|
||||
notificationTimestamp?: number;
|
||||
accessList?: string[];
|
||||
accessListTimestamp?: number;
|
||||
}
|
||||
|
||||
export interface FlatDevice {
|
||||
[param: string]: FlatAttributes;
|
||||
[param: string]: Value;
|
||||
}
|
||||
|
||||
export function flattenDevice(device: Record<string, unknown>): FlatDevice {
|
||||
@@ -88,100 +74,38 @@ export function flattenDevice(device: Record<string, unknown>): FlatDevice {
|
||||
for (const [name, tree] of Object.entries(input)) {
|
||||
if (!root) {
|
||||
if (name === "_lastInform") {
|
||||
output["Events.Inform"] = {
|
||||
value: [parseDate(tree as Date), "xsd:dateTime"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["Events.Inform"] = parseDate(tree as Date);
|
||||
output["Events.Inform:type"] = "xsd:dateTime";
|
||||
} else if (name === "_registered") {
|
||||
output["Events.Registered"] = {
|
||||
value: [parseDate(tree as Date), "xsd:dateTime"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["Events.Registered"] = parseDate(tree as Date);
|
||||
output["Events.Registered:type"] = "xsd:dateTime";
|
||||
} else if (name === "_lastBoot") {
|
||||
output["Events.1_BOOT"] = {
|
||||
value: [parseDate(tree as Date), "xsd:dateTime"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["Events.1_BOOT"] = parseDate(tree as Date);
|
||||
output["Events.1_BOOT:type"] = "xsd:dateTime";
|
||||
} else if (name === "_lastBootstrap") {
|
||||
output["Events.0_BOOTSTRAP"] = {
|
||||
value: [parseDate(tree as Date), "xsd:dateTime"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["Events.0_BOOTSTRAP"] = parseDate(tree as Date);
|
||||
output["Events.0_BOOTSTRAP:type"] = "xsd:dateTime";
|
||||
} else if (name === "_id") {
|
||||
output["DeviceID.ID"] = {
|
||||
value: [tree as string, "xsd:string"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["DeviceID.ID"] = tree as string;
|
||||
output["DeviceID.ID:type"] = "xsd:dateTime";
|
||||
} else if (name === "_deviceId") {
|
||||
output["DeviceID.Manufacturer"] = {
|
||||
value: [tree["_Manufacturer"], "xsd:string"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["DeviceID.OUI"] = {
|
||||
value: [tree["_OUI"], "xsd:string"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["DeviceID.ProductClass"] = {
|
||||
value: [tree["_ProductClass"], "xsd:string"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["DeviceID.SerialNumber"] = {
|
||||
value: [tree["_SerialNumber"], "xsd:string"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["DeviceID.Manufacturer"] = tree["_Manufacturer"];
|
||||
output["DeviceID.Manufacturer:type"] = "xsd:string";
|
||||
output["DeviceID.OUI"] = tree["_OUI"];
|
||||
output["DeviceID.OUI:type"] = "xsd:string";
|
||||
output["DeviceID.ProductClass"] = tree["_ProductClass"];
|
||||
output["DeviceID.ProductClass:type"] = "xsd:string";
|
||||
output["DeviceID.SerialNumber"] = tree["_SerialNumber"];
|
||||
output["DeviceID.SerialNumber:type"] = "xsd:string";
|
||||
} else if (name === "_tags") {
|
||||
output["Tags"] = {
|
||||
writable: false,
|
||||
writableTimestamp: timestamp,
|
||||
object: true,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
output["Tags:object"] = true;
|
||||
output["Tags:writable"] = true;
|
||||
|
||||
for (const t of tree as string[]) {
|
||||
output[`Tags.${encodeTag(t)}`] = {
|
||||
value: [true, "xsd:boolean"],
|
||||
valueTimestamp: timestamp,
|
||||
writable: true,
|
||||
writableTimestamp: timestamp,
|
||||
object: false,
|
||||
objectTimestamp: timestamp,
|
||||
};
|
||||
const et = encodeTag(t);
|
||||
output[`Tags.${et}`] = true;
|
||||
output[`Tags.${et}:type`] = "xsd:boolean";
|
||||
output[`Tags.${et}:writable`] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,39 +118,36 @@ export function flattenDevice(device: Record<string, unknown>): FlatDevice {
|
||||
else if (+input["_timestamp"] > timestamp)
|
||||
childrenTimestamp = +input["_timestamp"];
|
||||
|
||||
const attrs: FlatAttributes = {};
|
||||
const r = root ? `${root}.${name}` : name;
|
||||
|
||||
if (tree["_value"] != null) {
|
||||
attrs.value = [
|
||||
tree["_value"] instanceof Date ? +tree["_value"] : tree["_value"],
|
||||
tree["_type"],
|
||||
];
|
||||
attrs.valueTimestamp = +(tree["_timestamp"] || childrenTimestamp);
|
||||
attrs.object = false;
|
||||
attrs.objectTimestamp = childrenTimestamp;
|
||||
output[r] =
|
||||
tree["_value"] instanceof Date ? +tree["_value"] : tree["_value"];
|
||||
output[`${r}:type`] = tree["_type"];
|
||||
output[`${r}:timestamp`] = childrenTimestamp;
|
||||
output[`${r}:valueTimestamp`] = +(
|
||||
tree["_timestamp"] || childrenTimestamp
|
||||
);
|
||||
} else if (tree["_object"] != null) {
|
||||
attrs.object = tree["_object"];
|
||||
attrs.objectTimestamp = childrenTimestamp;
|
||||
output[`${r}:object`] = tree["_object"];
|
||||
output[`${r}:timestamp`] = childrenTimestamp;
|
||||
}
|
||||
|
||||
if (tree["_writable"] != null) {
|
||||
attrs.writable = tree["_writable"];
|
||||
attrs.writableTimestamp = childrenTimestamp;
|
||||
output[`${r}:writable`] = tree["_writable"];
|
||||
output[`${r}:timestamp`] = childrenTimestamp;
|
||||
}
|
||||
|
||||
if (tree["_notification"] != null) {
|
||||
attrs.notification = tree["_notification"];
|
||||
attrs.notificationTimestamp = +tree["_attributesTimestamp"] || 1;
|
||||
output[`${r}:notification`] = tree["_notification"];
|
||||
output[`${r}:attributesTimestamp`] = +tree["_attributesTimestamp"] || 1;
|
||||
}
|
||||
|
||||
if (tree["_accessList"] != null) {
|
||||
attrs.accessList = tree["_accessList"];
|
||||
attrs.accessListTimestamp = +tree["_attributesTimestamp"] || 1;
|
||||
output[`${r}:accessList`] = tree["_accessList"].join(",");
|
||||
output[`${r}:attributesTimestamp`] = +tree["_attributesTimestamp"] || 1;
|
||||
}
|
||||
|
||||
const r = root ? `${root}.${name}` : name;
|
||||
output[r] = attrs;
|
||||
|
||||
if (attrs.object || tree["object"] == null)
|
||||
recursive(tree, r, output, childrenTimestamp);
|
||||
}
|
||||
}
|
||||
@@ -259,14 +180,12 @@ function flattenPreset(
|
||||
if (p.precondition) {
|
||||
try {
|
||||
// Try parse to check expression validity
|
||||
parse(p.precondition as string);
|
||||
} catch (error) {
|
||||
p.precondition = convertOldPrecondition(
|
||||
JSON.parse(p.precondition as string),
|
||||
);
|
||||
p.precondition = (p.precondition as string).length
|
||||
? stringify(p.precondition as Expression)
|
||||
: "";
|
||||
Expression.parse(p.precondition as string);
|
||||
} catch {
|
||||
const e = convertOldPrecondition(JSON.parse(p.precondition as string));
|
||||
if (e instanceof Expression.Literal && e.value)
|
||||
p.precondition = e.toString();
|
||||
else p.precondition = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +204,7 @@ function flattenPreset(
|
||||
) {
|
||||
p.provision = provision.name;
|
||||
p.provisionArgs = provision.args
|
||||
? provision.args.map((a) => stringify(a)).join(", ")
|
||||
? provision.args.map((a) => a.toString()).join(", ")
|
||||
: "";
|
||||
}
|
||||
|
||||
@@ -309,8 +228,7 @@ function preProcessPreset(data: Record<string, unknown>): MongoTypes.Preset {
|
||||
const preset = Object.assign({}, data);
|
||||
|
||||
if (!preset.precondition) preset.precondition = "";
|
||||
// Try parse to check expression validity
|
||||
parse(preset.precondition as string);
|
||||
else Expression.parse(preset.precondition as string); // Try parse to check validity
|
||||
|
||||
preset.weight = parseInt(preset.weight as string) || 0;
|
||||
|
||||
@@ -361,7 +279,13 @@ export async function* query(
|
||||
options?: QueryOptions,
|
||||
): AsyncGenerator<any, void, undefined> {
|
||||
options = options || {};
|
||||
filter = evaluate(filter, null, Date.now());
|
||||
const now = Date.now();
|
||||
filter = filter.evaluate((e) => {
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW") return new Expression.Literal(now);
|
||||
}
|
||||
return e;
|
||||
});
|
||||
const q = toMongoQuery(filter, resource);
|
||||
if (!q) return;
|
||||
|
||||
@@ -408,7 +332,13 @@ export async function* query(
|
||||
|
||||
export function count(resource: string, filter: Expression): Promise<number> {
|
||||
const collection = collections[resource] as Collection<any>;
|
||||
filter = evaluate(filter, null, Date.now());
|
||||
const now = Date.now();
|
||||
filter = filter.evaluate((e) => {
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW") return new Expression.Literal(now);
|
||||
}
|
||||
return e;
|
||||
});
|
||||
const q = toMongoQuery(filter, resource);
|
||||
if (!q) return Promise.resolve(0);
|
||||
return collection.countDocuments(q);
|
||||
|
||||
+39
-66
@@ -1,9 +1,7 @@
|
||||
import * as crypto from "node:crypto";
|
||||
import * as config from "../config.ts";
|
||||
import { collections } from "../db/db.ts";
|
||||
import * as expression from "../common/expression/util.ts";
|
||||
import { parse } from "../common/expression/parser.ts";
|
||||
import { Expression, Users, Permissions, Config, UiConfig } from "../types.ts";
|
||||
import Expression, { Value } from "../common/expression.ts";
|
||||
import { Users, Permissions, Config, UiConfig } from "../types.ts";
|
||||
import { LocalCache } from "../local-cache.ts";
|
||||
|
||||
interface Snapshot {
|
||||
@@ -26,12 +24,14 @@ async function fetchPermissions(): Promise<[string, Permissions]> {
|
||||
if (!permissions[p.role]) permissions[p.role] = {};
|
||||
if (!permissions[p.role][p.access]) permissions[p.role][p.access] = {};
|
||||
|
||||
let validate: Expression;
|
||||
if (p.validate) validate = Expression.parse(p.validate);
|
||||
else validate = new Expression.Literal(true);
|
||||
permissions[p.role][p.access][p.resource] = {
|
||||
access: p.access,
|
||||
filter: parse(p.filter || "true"),
|
||||
filter: Expression.parse(p.filter || "true"),
|
||||
validate,
|
||||
};
|
||||
if (p.validate)
|
||||
permissions[p.role][p.access][p.resource].validate = parse(p.validate);
|
||||
}
|
||||
|
||||
return [h, permissions];
|
||||
@@ -62,33 +62,18 @@ async function fetchConfig(): Promise<[string, Config, UiConfig]> {
|
||||
conf.sort((a, b) => (a._id > b._id ? 1 : -1));
|
||||
const h = crypto.createHash("md5").update(JSON.stringify(conf)).digest("hex");
|
||||
|
||||
const ui = {
|
||||
filters: {},
|
||||
device: {},
|
||||
index: {},
|
||||
overview: {},
|
||||
pageSize: null,
|
||||
};
|
||||
const ui: UiConfig = {};
|
||||
|
||||
const _config = {};
|
||||
|
||||
for (const c of conf) {
|
||||
// Evaluate expressions to simplify them
|
||||
const val = expression.evaluate(parse(c.value));
|
||||
_config[c._id] = val;
|
||||
if (c._id.startsWith("ui.")) {
|
||||
const keys = c._id.split(".");
|
||||
if (!(keys[1] in ui)) continue;
|
||||
// remove the first key(ui)
|
||||
keys.shift();
|
||||
let ref = ui;
|
||||
while (keys.length > 1) {
|
||||
const k = keys.shift();
|
||||
if (ref[k] == null || typeof ref[k] !== "object") ref[k] = {};
|
||||
ref = ref[k];
|
||||
}
|
||||
ref[keys[0]] = val;
|
||||
ui[c._id.slice(3)] = c.value;
|
||||
continue;
|
||||
}
|
||||
// Evaluate expressions to simplify them
|
||||
const val = Expression.parse(c.value).evaluate((e) => e);
|
||||
_config[c._id] = val;
|
||||
}
|
||||
|
||||
return [h, _config, ui];
|
||||
@@ -123,47 +108,35 @@ export async function getRevision(): Promise<string> {
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
context: Record<string, unknown>,
|
||||
now: number,
|
||||
cb?: (e: Expression) => Expression,
|
||||
): string | number | boolean | null {
|
||||
dflt: string,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): string;
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
dflt: number,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): number;
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
dflt: boolean,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): boolean;
|
||||
export function getConfig(
|
||||
revision: string,
|
||||
key: string,
|
||||
dflt: Value,
|
||||
fn: (e: Expression) => Expression.Literal,
|
||||
): Value {
|
||||
const snapshot = localCache.get(revision);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
|
||||
const oldOpts = {
|
||||
"cwmp.downloadTimeout": "DOWNLOAD_TIMEOUT",
|
||||
"cwmp.debug": "DEBUG",
|
||||
"cwmp.retryDelay": "RETRY_DELAY",
|
||||
"cwmp.sessionTimeout": "SESSION_TIMEOUT",
|
||||
"cwmp.connectionRequestTimeout": "CONNECTION_REQUEST_TIMEOUT",
|
||||
"cwmp.gpnNextLevel": "GPN_NEXT_LEVEL",
|
||||
"cwmp.gpvBatchSize": "GPV_BATCH_SIZE",
|
||||
"cwmp.cookiesPath": "COOKIES_PATH",
|
||||
"cwmp.datetimeMilliseconds": "DATETIME_MILLISECONDS",
|
||||
"cwmp.booleanLiteral": "BOOLEAN_LITERAL",
|
||||
"cwmp.connectionRequestAllowBasicAuth":
|
||||
"CONNECTION_REQUEST_ALLOW_BASIC_AUTH",
|
||||
"cwmp.maxCommitIterations": "MAX_COMMIT_ITERATIONS",
|
||||
"cwmp.deviceOnlineThreshold": "DEVICE_ONLINE_THRESHOLD",
|
||||
"cwmp.udpConnectionRequestPort": "UDP_CONNECTION_REQUEST_PORT",
|
||||
};
|
||||
|
||||
if (!(key in snapshot.config)) {
|
||||
if (key in oldOpts) {
|
||||
let id;
|
||||
if (context?.["id"]) {
|
||||
id = context["id"];
|
||||
} else if (cb) {
|
||||
id = cb(["PARAM", "DeviceID.ID"]);
|
||||
if (Array.isArray(id)) id = null;
|
||||
}
|
||||
return config.get(oldOpts[key], id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const v = expression.evaluate(snapshot.config[key], context, now, cb);
|
||||
return Array.isArray(v) ? null : v;
|
||||
const e = snapshot.config[key];
|
||||
if (!e) return dflt;
|
||||
const v = e.evaluate(fn).value;
|
||||
if (typeof v !== typeof dflt) return dflt;
|
||||
return v;
|
||||
}
|
||||
|
||||
export function getConfigExpression(revision: string, key: string): Expression {
|
||||
|
||||
+17
-5
@@ -7,10 +7,10 @@ interface Revisions<V> {
|
||||
}
|
||||
|
||||
export default class VersionedMap<K, V> {
|
||||
private declare _sizeDiff: number[];
|
||||
private declare _revision: number;
|
||||
private declare map: Map<K, (V | symbol)[]>;
|
||||
public declare dirty: number;
|
||||
declare private _sizeDiff: number[];
|
||||
declare private _revision: number;
|
||||
declare private map: Map<K, (V | symbol)[]>;
|
||||
declare public dirty: number;
|
||||
|
||||
public constructor() {
|
||||
this._sizeDiff = [0];
|
||||
@@ -112,6 +112,8 @@ export default class VersionedMap<K, V> {
|
||||
prev = v;
|
||||
}
|
||||
|
||||
if (prev === NONEXISTENT && !res.delete) return null;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -122,6 +124,8 @@ export default class VersionedMap<K, V> {
|
||||
del,
|
||||
);
|
||||
|
||||
if (!mutations) return;
|
||||
|
||||
const revisions = [];
|
||||
|
||||
let prev: V | symbol = NONEXISTENT;
|
||||
@@ -149,7 +153,7 @@ export default class VersionedMap<K, V> {
|
||||
|
||||
public *diff(): IterableIterator<[K, V, V]> {
|
||||
for (const [key, revisions] of this.map) {
|
||||
if (revisions.length === 1) continue;
|
||||
if (revisions.length <= 1) continue;
|
||||
let first = revisions[0];
|
||||
let last = revisions[revisions.length - 1];
|
||||
if (first === NONEXISTENT && last === NONEXISTENT) continue;
|
||||
@@ -179,4 +183,12 @@ export default class VersionedMap<K, V> {
|
||||
this.map.delete(k);
|
||||
}
|
||||
}
|
||||
|
||||
public *[Symbol.iterator](): IterableIterator<[K, V]> {
|
||||
for (const [key, revisions] of this.map) {
|
||||
const last = revisions[revisions.length - 1];
|
||||
if (last === NONEXISTENT) continue;
|
||||
yield [key, last as V];
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -41,7 +41,7 @@ export function parseXmlDeclaration(buffer: Buffer): Attribute[] {
|
||||
str = str.slice(0, str.indexOf("?>"));
|
||||
try {
|
||||
return parseAttrs(str.slice(5));
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+6021
-4519
File diff suppressed because it is too large.
Load diff
+31
-25
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "genieacs",
|
||||
"version": "1.2.13",
|
||||
"version": "1.3.0-dev",
|
||||
"description": "A TR-069 Auto Configuration Server (ACS)",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -26,41 +26,47 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@breejs/later": "^4.2.0",
|
||||
"@koa/bodyparser": "^5.1.1",
|
||||
"@koa/router": "^13.1.0",
|
||||
"@koa/bodyparser": "^5.1.2",
|
||||
"@koa/router": "^13.1.1",
|
||||
"bson": "^4.7.2",
|
||||
"espresso-iisojs": "^1.0.8",
|
||||
"iconv-lite": "^0.6.3",
|
||||
"ipaddr.js": "^2.2.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"koa": "^2.15.3",
|
||||
"koa-compress": "^5.1.0",
|
||||
"ipaddr.js": "^2.3.0",
|
||||
"jsonwebtoken": "^9.0.3",
|
||||
"koa": "^2.16.4",
|
||||
"koa-compress": "^5.2.0",
|
||||
"koa-jwt": "^4.0.3",
|
||||
"koa-send": "^5.0.1",
|
||||
"mongodb": "^4.16.0",
|
||||
"parsimmon": "^1.18.1",
|
||||
"seedrandom": "^3.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/codemirror": "^5.60.15",
|
||||
"@types/jsonwebtoken": "^9.0.6",
|
||||
"@codemirror/commands": "^6.10.2",
|
||||
"@codemirror/lang-javascript": "^6.2.5",
|
||||
"@codemirror/lang-yaml": "^6.1.2",
|
||||
"@codemirror/language": "^6.12.2",
|
||||
"@codemirror/state": "^6.5.4",
|
||||
"@codemirror/view": "^6.39.16",
|
||||
"@eslint/js": "^10.0.1",
|
||||
"@tailwindcss/cli": "^4.2.1",
|
||||
"@tailwindcss/forms": "^0.5.11",
|
||||
"@types/jsonwebtoken": "^9.0.10",
|
||||
"@types/koa": "^2.15.0",
|
||||
"@types/koa-compress": "^4.0.6",
|
||||
"@types/mithril": "^2.2.6",
|
||||
"@types/node": "^20.14.2",
|
||||
"@types/parsimmon": "^1.10.9",
|
||||
"@types/koa-compress": "^4.0.7",
|
||||
"@types/mithril": "^2.2.7",
|
||||
"@types/node": "^25.3.5",
|
||||
"@types/seedrandom": "^3.0.8",
|
||||
"@typescript-eslint/eslint-plugin": "^7.12.0",
|
||||
"@typescript-eslint/parser": "^7.12.0",
|
||||
"codemirror": "^5.65.16",
|
||||
"esbuild": "^0.25.0",
|
||||
"eslint": "^8.57.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"mithril": "^2.2.2",
|
||||
"prettier": "^3.3.1",
|
||||
"sql.js": "^1.10.3",
|
||||
"svgo": "^3.3.2",
|
||||
"typescript": "^5.4.5",
|
||||
"esbuild": "^0.27.4",
|
||||
"eslint": "^10.0.3",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
"globals": "^17.4.0",
|
||||
"mithril": "^2.3.8",
|
||||
"prettier": "^3.8.1",
|
||||
"sql.js": "^1.14.1",
|
||||
"svgo": "^3.3.3",
|
||||
"tailwindcss": "^4.2.1",
|
||||
"typescript": "^5.9.3",
|
||||
"typescript-eslint": "^8.56.1",
|
||||
"yaml": "^1.10.2"
|
||||
},
|
||||
"engines": {
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
const now = Date.now();
|
||||
|
||||
// Clear cached data model to force a refresh
|
||||
clear("Device", now);
|
||||
clear("InternetGatewayDevice", now);
|
||||
@@ -0,0 +1,40 @@
|
||||
const hourly = Date.now(3600000);
|
||||
|
||||
// Refresh basic parameters hourly
|
||||
declare("InternetGatewayDevice.DeviceInfo.HardwareVersion", {
|
||||
path: hourly,
|
||||
value: hourly,
|
||||
});
|
||||
declare("InternetGatewayDevice.DeviceInfo.SoftwareVersion", {
|
||||
path: hourly,
|
||||
value: hourly,
|
||||
});
|
||||
declare(
|
||||
"InternetGatewayDevice.WANDevice.*.WANConnectionDevice.*.WANIPConnection.*.MACAddress",
|
||||
{ path: hourly, value: hourly },
|
||||
);
|
||||
declare(
|
||||
"InternetGatewayDevice.WANDevice.*.WANConnectionDevice.*.WANIPConnection.*.ExternalIPAddress",
|
||||
{ path: hourly, value: hourly },
|
||||
);
|
||||
declare("InternetGatewayDevice.LANDevice.*.WLANConfiguration.*.SSID", {
|
||||
path: hourly,
|
||||
value: hourly,
|
||||
});
|
||||
// Don't refresh password field periodically because CPEs always report blank passowrds for security reasons
|
||||
declare("InternetGatewayDevice.LANDevice.*.WLANConfiguration.*.KeyPassphrase", {
|
||||
path: hourly,
|
||||
value: 1,
|
||||
});
|
||||
declare("InternetGatewayDevice.LANDevice.*.Hosts.Host.*.HostName", {
|
||||
path: hourly,
|
||||
value: hourly,
|
||||
});
|
||||
declare("InternetGatewayDevice.LANDevice.*.Hosts.Host.*.IPAddress", {
|
||||
path: hourly,
|
||||
value: hourly,
|
||||
});
|
||||
declare("InternetGatewayDevice.LANDevice.*.Hosts.Host.*.MACAddress", {
|
||||
path: hourly,
|
||||
value: hourly,
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
// Device ID as user name
|
||||
const username = declare("DeviceID.ID", { value: 1 }).value[0];
|
||||
|
||||
// Password will be fixed for a given device because Math.random() is seeded with device ID by default.
|
||||
const password = Math.trunc(Math.random() * Number.MAX_SAFE_INTEGER).toString(
|
||||
36,
|
||||
);
|
||||
|
||||
const informInterval = 300;
|
||||
|
||||
// Refresh values daily
|
||||
const daily = Date.now(86400000);
|
||||
|
||||
// Unique inform offset per device for better load distribution
|
||||
const informTime = daily % 86400000;
|
||||
|
||||
declare(
|
||||
"InternetGatewayDevice.ManagementServer.ConnectionRequestUsername",
|
||||
{ value: daily },
|
||||
{ value: username },
|
||||
);
|
||||
declare(
|
||||
"InternetGatewayDevice.ManagementServer.ConnectionRequestPassword",
|
||||
{ value: daily },
|
||||
{ value: password },
|
||||
);
|
||||
declare(
|
||||
"InternetGatewayDevice.ManagementServer.PeriodicInformEnable",
|
||||
{ value: daily },
|
||||
{ value: true },
|
||||
);
|
||||
declare(
|
||||
"InternetGatewayDevice.ManagementServer.PeriodicInformInterval",
|
||||
{ value: daily },
|
||||
{ value: informInterval },
|
||||
);
|
||||
declare(
|
||||
"InternetGatewayDevice.ManagementServer.PeriodicInformTime",
|
||||
{ value: daily },
|
||||
{ value: informTime },
|
||||
);
|
||||
|
||||
declare(
|
||||
"Device.ManagementServer.ConnectionRequestUsername",
|
||||
{ value: daily },
|
||||
{ value: username },
|
||||
);
|
||||
declare(
|
||||
"Device.ManagementServer.ConnectionRequestPassword",
|
||||
{ value: daily },
|
||||
{ value: password },
|
||||
);
|
||||
declare(
|
||||
"Device.ManagementServer.PeriodicInformEnable",
|
||||
{ value: daily },
|
||||
{ value: true },
|
||||
);
|
||||
declare(
|
||||
"Device.ManagementServer.PeriodicInformInterval",
|
||||
{ value: daily },
|
||||
{ value: informInterval },
|
||||
);
|
||||
declare(
|
||||
"Device.ManagementServer.PeriodicInformTime",
|
||||
{ value: daily },
|
||||
{ value: informTime },
|
||||
);
|
||||
Vendored
+71
@@ -0,0 +1,71 @@
|
||||
interface Timestamps {
|
||||
path?: number;
|
||||
object?: number;
|
||||
writable?: number;
|
||||
value?: number;
|
||||
notification?: number;
|
||||
accessList?: number;
|
||||
}
|
||||
|
||||
interface Values {
|
||||
path?: number | [number, number];
|
||||
object?: boolean;
|
||||
writable?: boolean;
|
||||
value?: string | number | boolean | [string | number | boolean, string?];
|
||||
notification?: number;
|
||||
accessList?: string[];
|
||||
}
|
||||
|
||||
interface ParameterWrapper extends Iterable<ParameterWrapper> {
|
||||
readonly path: string | undefined;
|
||||
readonly size: number | undefined;
|
||||
readonly object: 0 | 1 | undefined;
|
||||
readonly writable: 0 | 1 | undefined;
|
||||
readonly value: [string | number | boolean, string] | undefined;
|
||||
readonly notification: number | undefined;
|
||||
readonly accessList: string[] | undefined;
|
||||
}
|
||||
|
||||
declare function declare(
|
||||
path: string,
|
||||
timestamps?: Timestamps | null,
|
||||
values?: Values | null,
|
||||
): ParameterWrapper;
|
||||
|
||||
declare function clear(
|
||||
path: string,
|
||||
timestamp: number,
|
||||
attributes?: Timestamps,
|
||||
): void;
|
||||
|
||||
declare function commit(): void;
|
||||
|
||||
declare function ext(...args: unknown[]): unknown;
|
||||
|
||||
declare function log(msg: string, meta?: Record<string, unknown>): void;
|
||||
|
||||
declare const args: unknown[];
|
||||
|
||||
interface DateConstructor {
|
||||
new (): Date;
|
||||
new (
|
||||
year: number,
|
||||
monthIndex?: number,
|
||||
day?: number,
|
||||
hours?: number,
|
||||
minutes?: number,
|
||||
seconds?: number,
|
||||
milliseconds?: number,
|
||||
): Date;
|
||||
now(intervalOrCron?: number | string, variance?: number): number;
|
||||
parse(dateString: string): number;
|
||||
UTC(
|
||||
year: number,
|
||||
monthIndex?: number,
|
||||
day?: number,
|
||||
hours?: number,
|
||||
minutes?: number,
|
||||
seconds?: number,
|
||||
milliseconds?: number,
|
||||
): number;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"noEmit": true,
|
||||
"target": "ES2022",
|
||||
"types": []
|
||||
},
|
||||
"include": ["./*.js", "./*.d.ts"]
|
||||
}
|
||||
+6
-9
@@ -2,13 +2,13 @@ import test from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { EJSON } from "bson";
|
||||
import { Filter } from "mongodb";
|
||||
import { stringify, parse } from "../lib/common/expression/parser.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
import { convertOldPrecondition } from "../lib/db/util.ts";
|
||||
import { toMongoQuery } from "../lib/db/synth.ts";
|
||||
|
||||
void test("convertOldPrecondition", () => {
|
||||
const tests = [
|
||||
[{}, "true"],
|
||||
[{}, "TRUE"],
|
||||
[{ test: "test" }, 'test = "test"'],
|
||||
[{ test: { $eq: "test" } }, 'test = "test"'],
|
||||
[{ test: { $ne: "test" } }, 'test <> "test" OR test IS NULL'],
|
||||
@@ -31,7 +31,7 @@ void test("convertOldPrecondition", () => {
|
||||
[{ test: "test", test2: "test2" }, 'test = "test" AND test2 = "test2"'],
|
||||
[
|
||||
{ $or: [{ test: "test" }, { test: { $ne: "test" } }] },
|
||||
'test = "test" OR (test <> "test" OR test IS NULL)',
|
||||
'test = "test" OR test <> "test" OR test IS NULL',
|
||||
],
|
||||
[
|
||||
{ test: { $gte: "test1", $ne: "test2" } },
|
||||
@@ -54,7 +54,7 @@ void test("convertOldPrecondition", () => {
|
||||
|
||||
for (const t of tests) {
|
||||
assert.strictEqual(
|
||||
stringify(convertOldPrecondition(t[0] as Record<string, unknown>)),
|
||||
convertOldPrecondition(t[0] as Record<string, unknown>).toString(),
|
||||
t[1],
|
||||
);
|
||||
}
|
||||
@@ -86,7 +86,6 @@ void test("toMongoQuery", async () => {
|
||||
"Param1 <> 1657844103524",
|
||||
{
|
||||
"Param1._value": { $ne: 1657844103524 },
|
||||
|
||||
$and: [
|
||||
{
|
||||
"Param1._value": { $ne: { $date: "2022-07-15T00:15:03.524Z" } },
|
||||
@@ -114,7 +113,6 @@ void test("toMongoQuery", async () => {
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
[
|
||||
"LOWER(Param1) LIKE 'value'",
|
||||
{
|
||||
@@ -146,7 +144,7 @@ void test("toMongoQuery", async () => {
|
||||
];
|
||||
|
||||
for (const [expStr, expect] of queries) {
|
||||
const exp = parse(expStr);
|
||||
const exp = Expression.parse(expStr);
|
||||
let query = toMongoQuery(exp, "devices");
|
||||
if (query) query = EJSON.serialize(query);
|
||||
assert.deepStrictEqual(query, expect);
|
||||
@@ -156,11 +154,10 @@ void test("toMongoQuery", async () => {
|
||||
["Param1 = Param2", "Right-hand operand must be a literal value"],
|
||||
["Param1 LIKE Param2", "Right-hand operand of 'LIKE' must be a string"],
|
||||
["NOW() = 1", "Left-hand operand must be a parameter"],
|
||||
["param{param2} = 1", "Left-hand operand must be a parameter"],
|
||||
];
|
||||
|
||||
for (const [expStr, err] of failQueries) {
|
||||
const exp = parse(expStr);
|
||||
const exp = Expression.parse(expStr);
|
||||
assert.throws(() => toMongoQuery(exp, "devices"), {
|
||||
message: err,
|
||||
});
|
||||
|
||||
+22
-19
@@ -7,7 +7,7 @@ import VersionedMap from "../lib/versioned-map.ts";
|
||||
import { Attributes } from "../lib/types.ts";
|
||||
|
||||
void test("getAliasDeclarations", () => {
|
||||
const path = Path.parse("a.[aa:10,bb.[aaa:100].cc:1].b");
|
||||
const path = Path.parse("a.[aa = 10 AND bb.[aaa = 100].cc = 1].b");
|
||||
const decs = device.getAliasDeclarations(path, 99);
|
||||
|
||||
const expected = ["a.*.b", "a.*.aa", "a.*.bb.*.cc", "a.*.bb.*.aaa"];
|
||||
@@ -32,66 +32,69 @@ void test("unpack", () => {
|
||||
changes: new Set<string>(),
|
||||
};
|
||||
|
||||
device.set(deviceData, Path.parse("a.1.b"), now, {
|
||||
device.set(deviceData, "a.1.b", now, {
|
||||
value: [now, ["b", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.c"), now, {
|
||||
device.set(deviceData, "a.1.c", now, {
|
||||
value: [now, ["c", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.a.1.a"), now, {
|
||||
device.set(deviceData, "a.1.a.1.a", now, {
|
||||
value: [now, ["", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.a.1.b"), now, {
|
||||
device.set(deviceData, "a.1.a.1.b", now, {
|
||||
value: [now, ["b1", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.a.1.c"), now, {
|
||||
device.set(deviceData, "a.1.a.1.c", now, {
|
||||
value: [now, ["c1", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.a.2.a"), now, {
|
||||
device.set(deviceData, "a.1.a.2.a", now, {
|
||||
value: [now, ["", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.a.2.b"), now, {
|
||||
device.set(deviceData, "a.1.a.2.b", now, {
|
||||
value: [now, ["b2", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.1.a.2.c"), now, {
|
||||
device.set(deviceData, "a.1.a.2.c", now, {
|
||||
value: [now, ["c2", "xsd:string"]],
|
||||
});
|
||||
|
||||
device.set(deviceData, Path.parse("a.2.b"), now, {
|
||||
device.set(deviceData, "a.2.b", now, {
|
||||
value: [now, ["b", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.c"), now, {
|
||||
device.set(deviceData, "a.2.c", now, {
|
||||
value: [now, ["c", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.a.1.a"), now, {
|
||||
device.set(deviceData, "a.2.a.1.a", now, {
|
||||
value: [now, ["", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.a.1.b"), now, {
|
||||
device.set(deviceData, "a.2.a.1.b", now, {
|
||||
value: [now, ["b1", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.a.1.c"), now, {
|
||||
device.set(deviceData, "a.2.a.1.c", now, {
|
||||
value: [now, ["c1", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.a.2.a"), now, {
|
||||
device.set(deviceData, "a.2.a.2.a", now, {
|
||||
value: [now, ["", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.a.2.b"), now, {
|
||||
device.set(deviceData, "a.2.a.2.b", now, {
|
||||
value: [now, ["c1", "xsd:string"]],
|
||||
});
|
||||
device.set(deviceData, Path.parse("a.2.a.2.c"), now, {
|
||||
device.set(deviceData, "a.2.a.2.c", now, {
|
||||
value: [now, ["b1", "xsd:string"]],
|
||||
});
|
||||
|
||||
let unpacked: Path[];
|
||||
unpacked = device.unpack(
|
||||
deviceData,
|
||||
Path.parse("a.[b:b,c:c].a.[b:b1,c:c1].a"),
|
||||
Path.parse("a.[b='b' AND c='c'].a.[b='b1' AND c='c1'].a"),
|
||||
);
|
||||
assert.strictEqual(unpacked.length, 2);
|
||||
assert.strictEqual(unpacked[0].toString(), "a.1.a.1.a");
|
||||
assert.strictEqual(unpacked[1].toString(), "a.2.a.1.a");
|
||||
|
||||
unpacked = device.unpack(deviceData, Path.parse("a.*.a.[b:c1,c:b1].a"));
|
||||
unpacked = device.unpack(
|
||||
deviceData,
|
||||
Path.parse("a.*.a.[b='c1' AND c='b1'].a"),
|
||||
);
|
||||
assert.strictEqual(unpacked.length, 1);
|
||||
assert.strictEqual(unpacked[0].toString(), "a.2.a.2.a");
|
||||
});
|
||||
+10
-10
@@ -1,13 +1,12 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert";
|
||||
import initSqlJs from "sql.js/dist/sql-asm.js";
|
||||
import { parse, stringify } from "../lib/common/expression/parser.ts";
|
||||
import {
|
||||
bookmarkToExpression,
|
||||
paginate,
|
||||
toBookmark,
|
||||
} from "../lib/common/expression/pagination.ts";
|
||||
import { Expression } from "../lib/types.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
import { covers, minimize } from "../lib/common/expression/synth.ts";
|
||||
|
||||
const VALUES = [null, -1, false, "a"];
|
||||
@@ -79,15 +78,15 @@ async function testPaginate(
|
||||
.map(([k, v]) => `${k} ${v > 0 ? "ASC" : "DESC"}`)
|
||||
.join(", ");
|
||||
|
||||
const allMatches = await query(`${stringify(q2)} ORDER BY ${orderBy}`);
|
||||
const allMatches = await query(`${q2.toString()} ORDER BY ${orderBy}`);
|
||||
const [fulfilled, diff] = paginate(q1, q2, sort);
|
||||
assert.ok(covers(q1, fulfilled));
|
||||
|
||||
const fulfilledMatches = await query(
|
||||
`${stringify(fulfilled)} ORDER BY ${orderBy}`,
|
||||
`${fulfilled.toString()} ORDER BY ${orderBy}`,
|
||||
);
|
||||
|
||||
const diffMatches = await query(`${stringify(diff)} ORDER BY ${orderBy}`);
|
||||
const diffMatches = await query(`${diff.toString()} ORDER BY ${orderBy}`);
|
||||
assert.deepStrictEqual(allMatches, [...fulfilledMatches, ...diffMatches]);
|
||||
|
||||
if (allMatches.length === fulfilledMatches.length) return;
|
||||
@@ -98,14 +97,14 @@ async function testPaginate(
|
||||
);
|
||||
const bookmark = toBookmark(sort, nextMatches[nextMatches.length - 1]);
|
||||
const bookmarkFilter = bookmarkToExpression(bookmark, sort);
|
||||
const capped = ["AND", q2, bookmarkFilter];
|
||||
const capped = Expression.and(q2, bookmarkFilter);
|
||||
|
||||
assert.ok(!covers(q1, capped));
|
||||
const cappedMatches = await query(
|
||||
`(${stringify(capped)}) ORDER BY ${orderBy}`,
|
||||
`(${capped.toString()}) ORDER BY ${orderBy}`,
|
||||
);
|
||||
assert.deepStrictEqual(cappedMatches, [...fulfilledMatches, ...nextMatches]);
|
||||
const min = minimize(["OR", q1, capped]);
|
||||
const min = minimize(Expression.or(q1, capped));
|
||||
await testPaginate(min, q2, sort);
|
||||
}
|
||||
|
||||
@@ -115,6 +114,7 @@ void test("paginate", async () => {
|
||||
const sortOrders = getAllSortOrders(params);
|
||||
|
||||
for (const [q1, q2] of cases)
|
||||
for (const sort of sortOrders)
|
||||
await testPaginate(parse(q1), parse(q2), sort);
|
||||
for (const sort of sortOrders) {
|
||||
await testPaginate(Expression.parse(q1), Expression.parse(q2), sort);
|
||||
}
|
||||
});
|
||||
+24
-28
@@ -3,67 +3,63 @@ import assert from "node:assert";
|
||||
import Path from "../lib/common/path.ts";
|
||||
import PathSet from "../lib/common/path-set.ts";
|
||||
|
||||
void test("depth", () => {
|
||||
const pathSet = new PathSet();
|
||||
assert.strictEqual(pathSet.depth, 0);
|
||||
pathSet.add(Path.parse(""));
|
||||
assert.strictEqual(pathSet.depth, 1);
|
||||
pathSet.add(Path.parse("a"));
|
||||
assert.strictEqual(pathSet.depth, 2);
|
||||
});
|
||||
|
||||
void test("add", () => {
|
||||
const pathSet = new PathSet();
|
||||
pathSet.add(Path.parse("a"));
|
||||
pathSet.add(Path.parse("a"));
|
||||
assert.strictEqual(pathSet.find(Path.parse("a"), true, true, 99).length, 1);
|
||||
pathSet.add("a");
|
||||
pathSet.add("a");
|
||||
assert.strictEqual(
|
||||
pathSet.findCompat(Path.parse("a"), true, true, 99).length,
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
void test("get", () => {
|
||||
const pathSet = new PathSet();
|
||||
pathSet.add(Path.parse("a.*"));
|
||||
pathSet.add(Path.parse("a.a"));
|
||||
pathSet.add(Path.parse("*.*"));
|
||||
pathSet.add("a.*");
|
||||
pathSet.add("a.a");
|
||||
pathSet.add("*.*");
|
||||
|
||||
assert.strictEqual(pathSet.get(Path.parse("a.*")).toString(), "a.*");
|
||||
assert.strictEqual(pathSet.get(Path.parse("*.a")), null);
|
||||
assert.strictEqual(pathSet.get("a.*").toString(), "a.*");
|
||||
assert.equal(pathSet.get("*.a"), null);
|
||||
});
|
||||
|
||||
void test("find", () => {
|
||||
const pathSet = new PathSet();
|
||||
pathSet.add(Path.parse("a"));
|
||||
pathSet.add(Path.parse("a.*"));
|
||||
pathSet.add(Path.parse("a.a"));
|
||||
pathSet.add(Path.parse("*.a"));
|
||||
pathSet.add(Path.parse("*.*"));
|
||||
pathSet.add("a");
|
||||
pathSet.add("a.*");
|
||||
pathSet.add("a.a");
|
||||
pathSet.add("*.a");
|
||||
pathSet.add("*.*");
|
||||
|
||||
assert.deepStrictEqual(
|
||||
pathSet.find(Path.parse(""), true, true, 1).map((p) => p.toString()),
|
||||
pathSet.findCompat(Path.root, true, true, 1).map((p) => p.toString()),
|
||||
["a"],
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
pathSet.find(Path.parse(""), false, false, 2).map((p) => p.toString()),
|
||||
pathSet.findCompat(Path.root, false, false, 2).map((p) => p.toString()),
|
||||
["a", "a.*", "a.a", "*.a", "*.*"],
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
pathSet.find(Path.parse("a.*"), false, false).map((p) => p.toString()),
|
||||
pathSet
|
||||
.findCompat(Path.parse("a.*"), false, false)
|
||||
.map((p) => p.toString()),
|
||||
["a.*"],
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
pathSet.find(Path.parse("a.*"), false, true).map((p) => p.toString()),
|
||||
pathSet.findCompat(Path.parse("a.*"), false, true).map((p) => p.toString()),
|
||||
["a.*", "a.a"],
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
pathSet.find(Path.parse("a.*"), true, false).map((p) => p.toString()),
|
||||
pathSet.findCompat(Path.parse("a.*"), true, false).map((p) => p.toString()),
|
||||
["a.*", "*.*"],
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
pathSet.find(Path.parse("a.*"), true, true).map((p) => p.toString()),
|
||||
pathSet.findCompat(Path.parse("a.*"), true, true).map((p) => p.toString()),
|
||||
["a.*", "a.a", "*.a", "*.*"],
|
||||
);
|
||||
});
|
||||
+138
-15
@@ -8,38 +8,161 @@ void test("parse", () => {
|
||||
assert.throws(() => Path.parse(".a"));
|
||||
assert.throws(() => Path.parse("a."));
|
||||
assert.throws(() => Path.parse("a..b"));
|
||||
assert.throws(() => Path.parse("b*"));
|
||||
assert.throws(() => Path.parse("*b"));
|
||||
assert.doesNotThrow(() => Path.parse("b*"));
|
||||
assert.doesNotThrow(() => Path.parse("*b"));
|
||||
assert.throws(() => Path.parse("a.b c.d"));
|
||||
assert.throws(() => Path.parse("a["));
|
||||
assert.throws(() => Path.parse("a[b"));
|
||||
assert.throws(() => Path.parse("a[b:"));
|
||||
assert.throws(() => Path.parse('a[b:"waef]'));
|
||||
assert.doesNotThrow(() => Path.parse("[]"));
|
||||
assert.doesNotThrow(() => Path.parse("[a:]"));
|
||||
assert.doesNotThrow(() => Path.parse("*"));
|
||||
assert.doesNotThrow(() => Path.parse(""));
|
||||
assert.throws(() => Path.parse(""));
|
||||
});
|
||||
|
||||
void test("toString", () => {
|
||||
const path1 = Path.parse('abc.[ abc : 123 , 123 : abc , 123: " abc "].123');
|
||||
const path2 = Path.parse('abc.[123: " abc ",abc:123,123:abc].123');
|
||||
const path1 = Path.parse('abc.[ abc=123 and def=" abc " ].123');
|
||||
const path2 = Path.parse('abc.[abc = 123 AND def = " abc "].123');
|
||||
assert.strictEqual(path1.toString(), path2.toString());
|
||||
});
|
||||
|
||||
void test("slice", () => {
|
||||
const path = Path.parse("a.*.b.[x:y].c");
|
||||
const path = Path.parse(`a.*.b.[x = "y"].c`);
|
||||
const sliced = path.slice(1, -1);
|
||||
assert.strictEqual(sliced.toString(), '*.b.[x:"y"]');
|
||||
assert.strictEqual(sliced.toString(), '*.b.[x = "y"]');
|
||||
assert.strictEqual(sliced.alias, 0b100);
|
||||
assert.strictEqual(sliced.wildcard, 0b1);
|
||||
|
||||
const path2 = Path.parse("a.b:c.d");
|
||||
|
||||
// Trim from right into colon region
|
||||
assert.strictEqual(path2.slice(0, 3).toString(), "a.b:c");
|
||||
assert.strictEqual(path2.slice(0, 3).colon, 1);
|
||||
|
||||
// Trim from right past colon region
|
||||
assert.strictEqual(path2.slice(0, 2).toString(), "a.b");
|
||||
assert.strictEqual(path2.slice(0, 2).colon, 0);
|
||||
|
||||
// Start exactly at colon boundary (all-colon result)
|
||||
assert.strictEqual(path2.slice(2, 4).toString(), ":c.d");
|
||||
assert.strictEqual(path2.slice(2, 4).colon, 2);
|
||||
|
||||
// Start past colon boundary (colon dropped)
|
||||
assert.strictEqual(path2.slice(3, 4).toString(), "d");
|
||||
assert.strictEqual(path2.slice(3, 4).colon, 0);
|
||||
|
||||
// Span across boundary from both sides
|
||||
assert.strictEqual(path2.slice(1, 3).toString(), "b:c");
|
||||
assert.strictEqual(path2.slice(1, 3).colon, 1);
|
||||
});
|
||||
|
||||
void test("concat", () => {
|
||||
const path1 = Path.parse("a");
|
||||
const path2 = Path.parse("*.[a:b]");
|
||||
const concat = path1.concat(path2);
|
||||
assert.strictEqual(concat.toString(), 'a.*.[a:"b"]');
|
||||
assert.strictEqual(concat.alias, 0b100);
|
||||
assert.strictEqual(concat.wildcard, 0b10);
|
||||
// Alias and wildcard propagation
|
||||
const c0 = Path.parse("a").concat(Path.parse('*.[a = "b"]'));
|
||||
assert.strictEqual(c0.toString(), 'a.*.[a = "b"]');
|
||||
assert.strictEqual(c0.alias, 0b100);
|
||||
assert.strictEqual(c0.wildcard, 0b10);
|
||||
|
||||
// Left plain, right all-colon
|
||||
const allColon = Path.parse("a:b.c").slice(1, 3);
|
||||
const c1 = Path.parse("a.b").concat(allColon);
|
||||
assert.strictEqual(c1.toString(), "a.b:b.c");
|
||||
assert.strictEqual(c1.colon, 2);
|
||||
|
||||
// Left colon, right all-colon
|
||||
const c2 = Path.parse("a:b").concat(Path.parse("a.b:c.d").slice(2, 4));
|
||||
assert.strictEqual(c2.toString(), "a:b.c.d");
|
||||
assert.strictEqual(c2.colon, 3);
|
||||
|
||||
// Both have mixed colon — should throw
|
||||
assert.throws(() => Path.parse("a:b").concat(Path.parse("c:d")));
|
||||
});
|
||||
|
||||
void test("old alias format", () => {
|
||||
// Empty brackets
|
||||
const empty = Path.parse("a.[].b");
|
||||
assert.strictEqual(empty.alias, 0b10);
|
||||
assert.strictEqual(empty.toString(), "a.[TRUE].b");
|
||||
|
||||
// Single key-value
|
||||
const single = Path.parse("a.[b:c].d");
|
||||
assert.strictEqual(single.alias, 0b10);
|
||||
assert.strictEqual(single.toString(), 'a.[b = "c"].d');
|
||||
|
||||
// Multiple key-value pairs
|
||||
const multi = Path.parse("a.[b:1,c:2].d");
|
||||
assert.strictEqual(multi.alias, 0b10);
|
||||
assert.strictEqual(multi.toString(), 'a.[b = "1" AND c = "2"].d');
|
||||
|
||||
// Key with empty value
|
||||
const emptyVal = Path.parse("a.[b:].d");
|
||||
assert.strictEqual(emptyVal.toString(), 'a.[b = ""].d');
|
||||
|
||||
// Value containing colons (split on first : only)
|
||||
const colonVal = Path.parse("a.[b:c:d].e");
|
||||
assert.strictEqual(colonVal.toString(), 'a.[b = "c:d"].e');
|
||||
|
||||
// Value containing spaces (trimmed)
|
||||
const spaceVal = Path.parse("a.[b:hello world].c");
|
||||
assert.strictEqual(spaceVal.toString(), 'a.[b = "hello world"].c');
|
||||
|
||||
// Unquoted values are trimmed
|
||||
const trimmed = Path.parse("a.[b: hello ].c");
|
||||
assert.strictEqual(trimmed.toString(), 'a.[b = "hello"].c');
|
||||
|
||||
// Whitespace around keys is trimmed
|
||||
const keySpace = Path.parse("a.[ b : c ].d");
|
||||
assert.strictEqual(keySpace.toString(), 'a.[b = "c"].d');
|
||||
|
||||
// Equivalence with new format
|
||||
const oldFmt = Path.parse("x.[a:1,b:2].y");
|
||||
const newFmt = Path.parse('x.[a = "1" AND b = "2"].y');
|
||||
assert.strictEqual(oldFmt.toString(), newFmt.toString());
|
||||
|
||||
// Nested old-format alias
|
||||
const nested = Path.parse("a.[b.[x:1].c:2].d");
|
||||
assert.strictEqual(nested.toString(), 'a.[b.[x = "1"].c = "2"].d');
|
||||
|
||||
// New SQL format still works
|
||||
const sql = Path.parse("a.[b = 1 AND c = 2].d");
|
||||
assert.strictEqual(sql.toString(), "a.[b = 1 AND c = 2].d");
|
||||
|
||||
// Double-quoted value containing closing bracket
|
||||
const quotedBracket = Path.parse('a.[b:"hello]world"].c');
|
||||
assert.strictEqual(quotedBracket.toString(), 'a.[b = "hello]world"].c');
|
||||
|
||||
// Double-quoted value containing comma
|
||||
const quotedComma = Path.parse('a.[b:"hello,world"].c');
|
||||
assert.strictEqual(quotedComma.toString(), 'a.[b = "hello,world"].c');
|
||||
|
||||
// Double-quoted value with escape sequences (JSON semantics)
|
||||
const escaped = Path.parse('a.[b:"hello\\"world"].c');
|
||||
assert.strictEqual(escaped.toString(), 'a.[b = "hello\\"world"].c');
|
||||
|
||||
// Single-quoted value
|
||||
const singleQuoted = Path.parse("a.[b:'value'].c");
|
||||
assert.strictEqual(singleQuoted.toString(), 'a.[b = "value"].c');
|
||||
|
||||
// Invalid old format (no colon) still throws
|
||||
assert.throws(() => Path.parse("[abc]"));
|
||||
});
|
||||
|
||||
void test("slice concat round-trip", () => {
|
||||
const path = Path.parse("a.b:c.d");
|
||||
|
||||
// Round-trips at every split position up to and including the boundary
|
||||
for (let i = 1; i <= path.paramLength; i++) {
|
||||
const rejoined = path.slice(0, i).concat(path.slice(i));
|
||||
assert.strictEqual(rejoined.toString(), path.toString());
|
||||
assert.strictEqual(rejoined.colon, path.colon);
|
||||
}
|
||||
|
||||
// Split inside the colon region: right half loses colon,
|
||||
// but concat still extends the left's attr region
|
||||
const left = path.slice(0, 3);
|
||||
const right = path.slice(3);
|
||||
assert.strictEqual(left.colon, 1);
|
||||
assert.strictEqual(right.colon, 0);
|
||||
const rejoined = left.concat(right);
|
||||
assert.strictEqual(rejoined.toString(), "a.b:c.d");
|
||||
assert.strictEqual(rejoined.colon, 2);
|
||||
});
|
||||
+124
-6
@@ -1,8 +1,12 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert";
|
||||
import initSqlJs from "sql.js/dist/sql-asm.js";
|
||||
import { minimize, unionDiff } from "../lib/common/expression/synth.ts";
|
||||
import { parse, stringify } from "../lib/common/expression/parser.ts";
|
||||
import { covers, minimize, unionDiff } from "../lib/common/expression/synth.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
|
||||
function isFalse(expr: Expression): boolean {
|
||||
return expr instanceof Expression.Literal && expr.value === false;
|
||||
}
|
||||
|
||||
const STRING_VALUES = [null, "", "a", "ab", "ab10", "ab-10"];
|
||||
const DECIMAL_VALUES = [null, 0, -10, 10];
|
||||
@@ -86,7 +90,7 @@ void test("minimize", async () => {
|
||||
|
||||
for (const c of cases) {
|
||||
const res1 = await query(c);
|
||||
const min = stringify(minimize(parse(c), true));
|
||||
const min = minimize(Expression.parse(c), true).toString();
|
||||
const res2 = await query(min);
|
||||
assert.strictEqual(setsEqual(res1, res2), true);
|
||||
}
|
||||
@@ -104,9 +108,9 @@ void test("unionDiff", async () => {
|
||||
for (const [c1, c2] of getPermutations(cases, cases)) {
|
||||
const res1 = await query(c1);
|
||||
const res2 = await query(c2);
|
||||
const [union, diff] = unionDiff(parse(c1), parse(c2));
|
||||
const res3 = await query(stringify(union));
|
||||
const res4 = await query(stringify(diff));
|
||||
const [union, diff] = unionDiff(Expression.parse(c1), Expression.parse(c2));
|
||||
const res3 = await query(union.toString());
|
||||
const res4 = await query(diff.toString());
|
||||
|
||||
const unionSet = new Set([...res1, ...res2]);
|
||||
const diffSet = new Set(Array.from(res2).filter((r) => !res1.has(r)));
|
||||
@@ -115,3 +119,117 @@ void test("unionDiff", async () => {
|
||||
assert.strictEqual(setsEqual(res4, diffSet), true);
|
||||
}
|
||||
});
|
||||
|
||||
void test("covers", async () => {
|
||||
assert.strictEqual(
|
||||
covers(Expression.parse("false"), Expression.parse("false")),
|
||||
true,
|
||||
);
|
||||
assert.strictEqual(
|
||||
covers(
|
||||
Expression.parse("false"),
|
||||
Expression.parse("decimal > 5 AND decimal < 3"),
|
||||
),
|
||||
true,
|
||||
);
|
||||
assert.strictEqual(
|
||||
covers(Expression.parse("true"), Expression.parse("decimal > 0")),
|
||||
true,
|
||||
);
|
||||
assert.strictEqual(
|
||||
covers(Expression.parse("true"), Expression.parse("false")),
|
||||
true,
|
||||
);
|
||||
assert.strictEqual(
|
||||
covers(Expression.parse("false"), Expression.parse("decimal > 0")),
|
||||
false,
|
||||
);
|
||||
assert.strictEqual(
|
||||
covers(Expression.parse("decimal >= 0"), Expression.parse("decimal > 0")),
|
||||
true,
|
||||
);
|
||||
assert.strictEqual(
|
||||
covers(Expression.parse("decimal > 0"), Expression.parse("decimal >= 0")),
|
||||
false,
|
||||
);
|
||||
|
||||
const cases = [
|
||||
["decimal >= 0", "decimal > 0"],
|
||||
["decimal > 0", "decimal > 5"],
|
||||
["string IS NOT NULL", "string = 'a'"],
|
||||
["true", "decimal > 0"],
|
||||
];
|
||||
|
||||
for (const [c1, c2] of cases) {
|
||||
const res1 = await query(c1);
|
||||
const res2 = await query(c2);
|
||||
const coversResult = covers(Expression.parse(c1), Expression.parse(c2));
|
||||
const actuallyCovers = Array.from(res2).every((r) => res1.has(r));
|
||||
|
||||
assert.strictEqual(
|
||||
coversResult,
|
||||
actuallyCovers,
|
||||
`covers(${c1}, ${c2}) should match actual coverage`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
void test("LIKE-Compare DC set relationships", () => {
|
||||
const likeExpr = Expression.parse("string LIKE 'a%'");
|
||||
|
||||
const eqExpr = Expression.parse("string = 'a'");
|
||||
const conjExpr: Expression = new Expression.Binary("AND", eqExpr, likeExpr);
|
||||
assert.strictEqual(
|
||||
isFalse(minimize(conjExpr, true)),
|
||||
false,
|
||||
"(string = 'a') AND (string LIKE 'a%') should NOT minimize to false",
|
||||
);
|
||||
|
||||
const nonMatchingExpr = Expression.parse("string = 'b'");
|
||||
const conjNonMatch: Expression = new Expression.Binary(
|
||||
"AND",
|
||||
nonMatchingExpr,
|
||||
likeExpr,
|
||||
);
|
||||
assert.strictEqual(
|
||||
isFalse(minimize(conjNonMatch, true)),
|
||||
true,
|
||||
"(string = 'b') AND (string LIKE 'a%') should minimize to false",
|
||||
);
|
||||
});
|
||||
|
||||
void test("LIKE-Compare DC set with range operators", () => {
|
||||
const likeExpr = Expression.parse("string LIKE 'abc%'");
|
||||
|
||||
const ltExpr = Expression.parse("string < 'abc'");
|
||||
const ltConj: Expression = new Expression.Binary("AND", ltExpr, likeExpr);
|
||||
assert.strictEqual(
|
||||
isFalse(minimize(ltConj, true)),
|
||||
true,
|
||||
"(string < 'abc') AND (string LIKE 'abc%') should be false",
|
||||
);
|
||||
|
||||
const ltExpr2 = Expression.parse("string < 'abd'");
|
||||
const ltConj2: Expression = new Expression.Binary("AND", ltExpr2, likeExpr);
|
||||
assert.strictEqual(
|
||||
isFalse(minimize(ltConj2, true)),
|
||||
false,
|
||||
"(string < 'abd') AND (string LIKE 'abc%') should NOT be false",
|
||||
);
|
||||
|
||||
const gtExpr = Expression.parse("string > 'abd'");
|
||||
const gtConj: Expression = new Expression.Binary("AND", gtExpr, likeExpr);
|
||||
assert.strictEqual(
|
||||
isFalse(minimize(gtConj, true)),
|
||||
true,
|
||||
"(string > 'abd') AND (string LIKE 'abc%') should be false",
|
||||
);
|
||||
|
||||
const gtExpr2 = Expression.parse("string > 'abc'");
|
||||
const gtConj2: Expression = new Expression.Binary("AND", gtExpr2, likeExpr);
|
||||
assert.strictEqual(
|
||||
isFalse(minimize(gtConj2, true)),
|
||||
false,
|
||||
"(string > 'abc') AND (string LIKE 'abc%') should NOT be false",
|
||||
);
|
||||
});
|
||||
+5
-2
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2022", "dom"],
|
||||
"module": "ES2022",
|
||||
"module": "Preserve",
|
||||
"moduleResolution": "bundler",
|
||||
"target": "ES2022",
|
||||
"resolveJsonModule": true,
|
||||
@@ -11,7 +11,10 @@
|
||||
"strictFunctionTypes": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"allowImportingTsExtensions": true
|
||||
"allowImportingTsExtensions": true,
|
||||
"skipLibCheck": true,
|
||||
"jsx": "react",
|
||||
"jsxFactory": "m"
|
||||
},
|
||||
"include": [
|
||||
"./bin/*.ts",
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"node": false
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
import m, { ClosureComponent, Component } from "mithril";
|
||||
|
||||
const component: ClosureComponent = (): Component => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
const active = { [vnode.attrs["page"]]: "active" };
|
||||
const tabs = [];
|
||||
|
||||
if (window.authorizer.hasAccess("presets", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["presets"] },
|
||||
m("a", { href: "#!/admin/presets" }, "Presets"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (window.authorizer.hasAccess("provisions", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["provisions"] },
|
||||
m("a", { href: "#!/admin/provisions" }, "Provisions"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (window.authorizer.hasAccess("virtualParameters", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["virtualParameters"] },
|
||||
m(
|
||||
"a",
|
||||
{ href: "#!/admin/virtualParameters" },
|
||||
"Virtual Parameters",
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (window.authorizer.hasAccess("files", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["files"] },
|
||||
m("a", { href: "#!/admin/files" }, "Files"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (window.authorizer.hasAccess("config", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["config"] },
|
||||
m("a", { href: "#!/admin/config" }, "Config"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (window.authorizer.hasAccess("permissions", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["permissions"] },
|
||||
m("a", { href: "#!/admin/permissions" }, "Permissions"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (window.authorizer.hasAccess("users", 1)) {
|
||||
tabs.push(
|
||||
m(
|
||||
"li",
|
||||
{ class: active["users"] },
|
||||
m("a", { href: "#!/admin/users" }, "Users"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return m("nav#side-menu", m("ul", tabs));
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default component;
|
||||
@@ -1,8 +1,8 @@
|
||||
import m, { RouteResolver } from "mithril";
|
||||
import layout from "./layout.ts";
|
||||
import layout from "./layout.tsx";
|
||||
import * as store from "./store.ts";
|
||||
import * as wizardPage from "./wizard-page.ts";
|
||||
import * as loginPage from "./login-page.ts";
|
||||
import * as loginPage from "./login-page.tsx";
|
||||
import * as overviewPage from "./overview-page.ts";
|
||||
import * as devicesPage from "./devices-page.ts";
|
||||
import * as devicePage from "./device-page.ts";
|
||||
@@ -16,32 +16,45 @@ import * as configPage from "./config-page.ts";
|
||||
import * as permissionsPage from "./permissions-page.ts";
|
||||
import * as usersPage from "./users-page.ts";
|
||||
import Authorizer from "../lib//common/authorizer.ts";
|
||||
import * as notifications from "./notifications.ts";
|
||||
import { contextifyComponent } from "./components.ts";
|
||||
import { PermissionSet, UiConfig } from "../lib/types.ts";
|
||||
import drawerComponent from "./drawer-component.ts";
|
||||
import { render as renderOverlay } from "./overlay.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
// --- BEGIN UNIFI THEME ---
|
||||
import * as unifiDevicesPage from "./unifi-devices-page.ts";
|
||||
import * as unifiDevicePage from "./unifi-device-detail-page.ts";
|
||||
// --- END UNIFI THEME ---
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
authorizer: Authorizer;
|
||||
permissionSets: PermissionSet[];
|
||||
permissionSets: {
|
||||
[resource: string]: { access: number; validate: string; filter: string };
|
||||
}[][];
|
||||
username: string;
|
||||
clientConfig: { ui: UiConfig };
|
||||
clientConfig: UiConfig;
|
||||
configSnapshot: string;
|
||||
genieacsVersion: string;
|
||||
}
|
||||
}
|
||||
|
||||
window.authorizer = new Authorizer(window.permissionSets);
|
||||
const permissionSets: PermissionSet[] = window.permissionSets.map((p) =>
|
||||
p.map((s) =>
|
||||
Object.fromEntries(
|
||||
Object.entries(s).map(([resource, { access, validate, filter }]) => [
|
||||
resource,
|
||||
{
|
||||
access,
|
||||
validate: Expression.parse(validate),
|
||||
filter: Expression.parse(filter),
|
||||
},
|
||||
]),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const adminPages = [
|
||||
"presets",
|
||||
"provisions",
|
||||
"virtualParameters",
|
||||
"files",
|
||||
"config",
|
||||
"users",
|
||||
"permissions",
|
||||
];
|
||||
window.authorizer = new Authorizer(permissionSets);
|
||||
|
||||
let state;
|
||||
|
||||
@@ -52,10 +65,14 @@ function pagify(pageName, page): RouteResolver {
|
||||
let p;
|
||||
if (state?.error) p = m(errorPage.component, state);
|
||||
else p = m(contextifyComponent(page.component), state);
|
||||
const attrs = {};
|
||||
attrs["page"] = pageName;
|
||||
attrs["oncreate"] = attrs["onupdate"] = () => {
|
||||
const attrs = {
|
||||
page: pageName,
|
||||
oncreate: () => {
|
||||
store.fulfill(lastRenderTimestamp);
|
||||
},
|
||||
onupdate: () => {
|
||||
store.fulfill(lastRenderTimestamp);
|
||||
},
|
||||
};
|
||||
return m(layout, attrs, p);
|
||||
},
|
||||
@@ -78,10 +95,8 @@ function pagify(pageName, page): RouteResolver {
|
||||
resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!window.username && err.message.indexOf("authorized") >= 0) {
|
||||
notifications.push("error", err.message);
|
||||
if (!window.username && err.message.indexOf("authorized") >= 0)
|
||||
m.route.set("/login", { continue: requestedPath });
|
||||
}
|
||||
state = { error: err.message };
|
||||
resolve();
|
||||
});
|
||||
@@ -91,37 +106,22 @@ function pagify(pageName, page): RouteResolver {
|
||||
return component;
|
||||
}
|
||||
|
||||
function redirectAdminPage(): RouteResolver {
|
||||
const component: RouteResolver = {
|
||||
onmatch: () => {
|
||||
for (const page of adminPages) {
|
||||
if (window.authorizer.hasAccess(page, 2)) {
|
||||
m.route.set(`/admin/${page}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
};
|
||||
return component;
|
||||
}
|
||||
|
||||
m.route(document.body, "/overview", {
|
||||
"/wizard": pagify("wizard", wizardPage),
|
||||
"/login": pagify("login", loginPage),
|
||||
"/overview": pagify("overview", overviewPage),
|
||||
"/devices": pagify("devices", devicesPage),
|
||||
"/devices/:id": pagify("devices", devicePage),
|
||||
// --- BEGIN UNIFI THEME ---
|
||||
"/devices": pagify("devices", unifiDevicesPage),
|
||||
"/devices/:id": pagify("devices", unifiDevicePage),
|
||||
// --- END UNIFI THEME ---
|
||||
"/faults": pagify("faults", faultsPage),
|
||||
"/admin": redirectAdminPage(),
|
||||
"/admin/presets": pagify("presets", presetsPage),
|
||||
"/admin/provisions": pagify("provisions", provisionsPage),
|
||||
"/admin/virtualParameters": pagify(
|
||||
"virtualParameters",
|
||||
virtualParametersPage,
|
||||
),
|
||||
"/admin/files": pagify("files", filesPage),
|
||||
"/admin/config": pagify("config", configPage),
|
||||
"/admin/users": pagify("users", usersPage),
|
||||
"/admin/permissions": pagify("permissions", permissionsPage),
|
||||
"/presets": pagify("presets", presetsPage),
|
||||
"/provisions": pagify("provisions", provisionsPage),
|
||||
"/virtualParameters": pagify("virtualParameters", virtualParametersPage),
|
||||
"/files": pagify("files", filesPage),
|
||||
"/config": pagify("config", configPage),
|
||||
"/users": pagify("users", usersPage),
|
||||
"/permissions": pagify("permissions", permissionsPage),
|
||||
"/login": {
|
||||
render: () => [m(loginPage.component), renderOverlay(), m(drawerComponent)],
|
||||
},
|
||||
});
|
||||
+16
-12
@@ -4,15 +4,15 @@ type AutocompleteCallback = (
|
||||
) => void;
|
||||
|
||||
export default class Autocomplete {
|
||||
private declare callback: AutocompleteCallback;
|
||||
private declare element: HTMLInputElement;
|
||||
private declare hideTimeout: NodeJS.Timeout;
|
||||
private declare visible: boolean;
|
||||
private declare default: string;
|
||||
private declare selection: number;
|
||||
private declare container: HTMLElement;
|
||||
declare private callback: AutocompleteCallback;
|
||||
declare private element: HTMLInputElement;
|
||||
declare private hideTimeout: NodeJS.Timeout;
|
||||
declare private visible: boolean;
|
||||
declare private default: string;
|
||||
declare private selection: number;
|
||||
declare private container: HTMLElement;
|
||||
|
||||
public constructor(className: string, callback: AutocompleteCallback) {
|
||||
public constructor(callback: AutocompleteCallback) {
|
||||
this.callback = callback;
|
||||
this.element = null;
|
||||
this.hideTimeout = null;
|
||||
@@ -24,7 +24,8 @@ export default class Autocomplete {
|
||||
this.container.style.position = "absolute";
|
||||
this.container.style.display = "block";
|
||||
this.container.style.opacity = "0";
|
||||
this.container.className = className;
|
||||
this.container.className =
|
||||
"absolute py-1 mt-2 rounded-md shadow-lg bg-white";
|
||||
}
|
||||
|
||||
public attach(el: HTMLInputElement): void {
|
||||
@@ -118,7 +119,9 @@ export default class Autocomplete {
|
||||
if (!this.visible) {
|
||||
if (!this.hideTimeout) {
|
||||
document.body.appendChild(this.container);
|
||||
window.getComputedStyle(this.container).opacity;
|
||||
// Force style recalc so the initial opacity is resolved before
|
||||
// setting it to "1", allowing the CSS transition to play.
|
||||
void window.getComputedStyle(this.container).opacity;
|
||||
} else {
|
||||
clearTimeout(this.hideTimeout);
|
||||
this.hideTimeout = null;
|
||||
@@ -140,9 +143,10 @@ export default class Autocomplete {
|
||||
for (const [idx, suggestion] of suggestions.entries()) {
|
||||
const e = document.createElement("div");
|
||||
if (suggestion.tip) e.title = suggestion.tip;
|
||||
e.classList.add("suggestion");
|
||||
e.className =
|
||||
"text-stone-700 block px-4 py-2 text-sm hover:bg-stone-100 hover:text-stone-900";
|
||||
if (idx === this.selection) {
|
||||
e.classList.add("selected");
|
||||
e.classList.add("bg-stone-100", "text-stone-900");
|
||||
selectedElement = e;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,14 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
const form = [
|
||||
m(
|
||||
"p",
|
||||
m("label", { for: "username" }, "Username"),
|
||||
m("input", {
|
||||
m(
|
||||
"label.block text-sm font-semibold text-stone-700 mt-2 mb-1",
|
||||
{ for: "username" },
|
||||
"Username",
|
||||
),
|
||||
m(
|
||||
"input.shadow-sm focus:ring-cyan-500 focus:border-cyan-500 block sm:text-sm border-stone-300 rounded-md",
|
||||
{
|
||||
name: "username",
|
||||
type: "text",
|
||||
value: vnode.state["username"],
|
||||
@@ -33,7 +39,8 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
oncreate: (_vnode) => {
|
||||
(_vnode.dom as HTMLSelectElement).focus();
|
||||
},
|
||||
}),
|
||||
},
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
@@ -48,31 +55,38 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
form.push(
|
||||
m(
|
||||
"p",
|
||||
m("label", { for: f }, l),
|
||||
m("input", {
|
||||
m(
|
||||
"label.block text-sm font-semibold text-stone-700 mt-2 mb-1",
|
||||
{ for: f },
|
||||
l,
|
||||
),
|
||||
m(
|
||||
"input.shadow-sm focus:ring-cyan-500 focus:border-cyan-500 block sm:text-sm border-stone-300 rounded-md",
|
||||
{
|
||||
name: f,
|
||||
type: "password",
|
||||
value: vnode.state[f],
|
||||
oninput: (e) => {
|
||||
vnode.state[f] = e.target.value;
|
||||
},
|
||||
}),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const submit = m(
|
||||
"button.primary",
|
||||
"button.ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-xs text-sm font-medium rounded-md text-white bg-cyan-600 hover:bg-cyan-700 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500",
|
||||
{
|
||||
type: "submit",
|
||||
},
|
||||
"Change password",
|
||||
) as VnodeDOM;
|
||||
|
||||
form.push(m(".actions-bar", submit));
|
||||
form.push(m("div.flex justify-end mt-5", submit));
|
||||
|
||||
const children = [
|
||||
m("h1", "Change password"),
|
||||
m("h2.text-lg leading-6 font-medium text-stone-900", "Change password"),
|
||||
m(
|
||||
"form",
|
||||
{
|
||||
|
||||
+49
-24
@@ -1,4 +1,3 @@
|
||||
import { Editor } from "codemirror";
|
||||
import { ClosureComponent } from "mithril";
|
||||
import { m } from "./components.ts";
|
||||
import { codeMirror } from "./dynamic-loader.ts";
|
||||
@@ -11,41 +10,67 @@ interface Attrs {
|
||||
focus?: boolean;
|
||||
onSubmit?: (dom: Element) => void;
|
||||
onChange?: (value: string) => void;
|
||||
onReady?: (editor: Editor) => void;
|
||||
}
|
||||
|
||||
const component: ClosureComponent<Attrs> = () => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
return m("textarea", {
|
||||
name: vnode.attrs.id,
|
||||
value: vnode.attrs.value,
|
||||
return m("div.font-mono text-sm", {
|
||||
oncreate: (_vnode) => {
|
||||
const editor = codeMirror.fromTextArea(
|
||||
_vnode.dom as HTMLTextAreaElement,
|
||||
{
|
||||
mode: vnode.attrs.mode,
|
||||
lineNumbers: true,
|
||||
readOnly: vnode.attrs.readOnly,
|
||||
extraKeys: {
|
||||
"Ctrl-Enter": () => {
|
||||
if (vnode.attrs.onSubmit) vnode.attrs.onSubmit(_vnode.dom);
|
||||
},
|
||||
"Cmd-Enter": () => {
|
||||
if (vnode.attrs.onSubmit) vnode.attrs.onSubmit(_vnode.dom);
|
||||
const theme = codeMirror.EditorView.theme({
|
||||
"&.cm-editor": {
|
||||
display: "block",
|
||||
width: "50rem",
|
||||
height: "30rem",
|
||||
"max-width": "100%",
|
||||
"border-radius": "0.375rem",
|
||||
"border-width": "1px",
|
||||
"border-color": "var(--color-stone-300)",
|
||||
"box-shadow":
|
||||
"var(--tw-ring-shadow, 0 0 #0000), 0 1px 2px 0 rgb(0 0 0 / 0.05)",
|
||||
overflow: "hidden",
|
||||
"& > .cm-scroller": {
|
||||
"font-family": "inherit",
|
||||
"line-height": "inherit",
|
||||
},
|
||||
},
|
||||
"&.cm-editor.cm-focused": {
|
||||
outline: "none",
|
||||
"border-color": "var(--color-cyan-500)",
|
||||
"--tw-ring-shadow": "0 0 0 1px var(--color-cyan-500)",
|
||||
},
|
||||
);
|
||||
|
||||
if (vnode.attrs.onChange) {
|
||||
editor.on("change", (e) => {
|
||||
vnode.attrs.onChange(e.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
const extensions = [
|
||||
theme,
|
||||
codeMirror.lineNumbers(),
|
||||
codeMirror.history(),
|
||||
codeMirror.syntaxHighlighting(codeMirror.defaultHighlightStyle),
|
||||
codeMirror.keymap.of([
|
||||
...codeMirror.defaultKeymap,
|
||||
...codeMirror.historyKeymap,
|
||||
]),
|
||||
codeMirror.EditorState.readOnly.of(!!vnode.attrs.readOnly),
|
||||
codeMirror.EditorView.updateListener.of((update) => {
|
||||
if (update.docChanged && vnode.attrs.onChange)
|
||||
vnode.attrs.onChange(update.state.doc.toString());
|
||||
}),
|
||||
];
|
||||
|
||||
if (vnode.attrs.mode === "javascript")
|
||||
extensions.push(codeMirror.javascript());
|
||||
else if (vnode.attrs.mode === "yaml")
|
||||
extensions.push(codeMirror.yaml());
|
||||
|
||||
const editor = new codeMirror.EditorView({
|
||||
state: codeMirror.EditorState.create({
|
||||
doc: vnode.attrs.value,
|
||||
extensions,
|
||||
}),
|
||||
parent: _vnode.dom as HTMLTextAreaElement,
|
||||
});
|
||||
|
||||
if (vnode.attrs.focus) editor.focus();
|
||||
if (vnode.attrs.onReady) vnode.attrs.onReady(editor);
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
export { EditorView, keymap, lineNumbers } from "@codemirror/view";
|
||||
export { EditorState } from "@codemirror/state";
|
||||
export { history, historyKeymap, defaultKeymap } from "@codemirror/commands";
|
||||
export {
|
||||
syntaxHighlighting,
|
||||
defaultHighlightStyle,
|
||||
} from "@codemirror/language";
|
||||
export { javascript } from "@codemirror/lang-javascript";
|
||||
export { yaml } from "@codemirror/lang-yaml";
|
||||
+1
-1
@@ -127,7 +127,7 @@ export function contextifyComponent(component: ComponentTypes): ComponentTypes {
|
||||
return res;
|
||||
} catch (err) {
|
||||
return m(
|
||||
"p.error",
|
||||
"p.text-sm font-bold text-red-500 cursor-pointer",
|
||||
{
|
||||
title: "Click to print stack trace to console",
|
||||
onclick: () => console.error(err),
|
||||
|
||||
@@ -1,26 +1,41 @@
|
||||
import { ClosureComponent } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import * as taskQueue from "../task-queue.ts";
|
||||
import { parse } from "../../lib/common/expression/parser.ts";
|
||||
import memoize from "../../lib/common/memoize.ts";
|
||||
import { getIcon } from "../icons.ts";
|
||||
import { icon } from "../tailwind-utility-components.ts";
|
||||
import { QueryResponse, evaluateExpression } from "../store.ts";
|
||||
import debounce from "../../lib/common/debounce.ts";
|
||||
import { Expression } from "../../lib/types.ts";
|
||||
import Expression, { Value } from "../../lib/common/expression.ts";
|
||||
import { FlatDevice } from "../../lib/ui/db.ts";
|
||||
|
||||
const memoizedParse = memoize(parse);
|
||||
import Path from "../../lib/common/path.ts";
|
||||
|
||||
function escapeRegExp(str): string {
|
||||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
|
||||
}
|
||||
|
||||
const keysByDepth: WeakMap<Record<string, unknown>, string[][]> = new WeakMap();
|
||||
interface Parameter {
|
||||
path: Expression.Parameter;
|
||||
value?: Value;
|
||||
writable?: boolean;
|
||||
object?: boolean;
|
||||
}
|
||||
|
||||
function orderKeysByDepth(device: Record<string, unknown>): string[][] {
|
||||
if (keysByDepth.has(device)) return keysByDepth.get(device);
|
||||
const res: string[][] = [];
|
||||
for (const key of Object.keys(device)) {
|
||||
const prepareParams = memoize((device: FlatDevice): Parameter[][] => {
|
||||
const map = new Map<string, Parameter>();
|
||||
|
||||
for (const [k, v] of Object.entries(device)) {
|
||||
const [param, attr] = k.split(":");
|
||||
let attrs = map.get(param);
|
||||
if (!attrs)
|
||||
map.set(
|
||||
param,
|
||||
(attrs = { path: new Expression.Parameter(Path.parse(param)) }),
|
||||
);
|
||||
attrs[attr || "value"] = v;
|
||||
}
|
||||
|
||||
const res: Parameter[][] = [];
|
||||
for (const [key, attrs] of map) {
|
||||
let count = 0;
|
||||
for (
|
||||
let i = key.lastIndexOf(".", key.length - 2);
|
||||
@@ -29,11 +44,10 @@ function orderKeysByDepth(device: Record<string, unknown>): string[][] {
|
||||
)
|
||||
++count;
|
||||
while (res.length <= count) res.push([]);
|
||||
res[count].push(key);
|
||||
res[count].push(attrs);
|
||||
}
|
||||
keysByDepth.set(device, res);
|
||||
return res;
|
||||
}
|
||||
});
|
||||
|
||||
interface Attrs {
|
||||
device: FlatDevice;
|
||||
@@ -51,18 +65,25 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
const device = vnode.attrs.device;
|
||||
const allParams = prepareParams(device);
|
||||
|
||||
const limit =
|
||||
(evaluateExpression(vnode.attrs.limit, device) as number) || 100;
|
||||
let limit = 100;
|
||||
if (vnode.attrs.limit) {
|
||||
const l = evaluateExpression(vnode.attrs.limit, device);
|
||||
if (typeof l.value === "number") limit = l.value;
|
||||
}
|
||||
|
||||
const search = m("input", {
|
||||
const search = m(
|
||||
"input.appearance-none border-0 block w-full px-4 py-3 border-stone-300 placeholder-stone-500 text-stone-900 focus:ring-cyan-500 text-sm rounded-t-lg font-mono focus:ring-2",
|
||||
{
|
||||
type: "text",
|
||||
placeholder: "Search parameters",
|
||||
oninput: (e) => {
|
||||
formQueryString(e.target.value);
|
||||
e.redraw = false;
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
const instanceRegex = /\.[0-9]+$/;
|
||||
let re;
|
||||
@@ -72,37 +93,36 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
re = new RegExp(keywords.map((s) => escapeRegExp(s)).join(".*"), "i");
|
||||
}
|
||||
|
||||
const filteredKeys: string[] = [];
|
||||
const allKeys = orderKeysByDepth(device);
|
||||
const filteredParams: Parameter[] = [];
|
||||
let count = 0;
|
||||
for (const keys of allKeys) {
|
||||
for (const keys of allParams) {
|
||||
let c = 0;
|
||||
for (const k of keys) {
|
||||
const p = device[k];
|
||||
const str = p.value?.[0] ? `${k} ${p.value[0]}` : k;
|
||||
const str = k.value ? `${k.path.toString()} ${k.value}` : k;
|
||||
if (re && !re.test(str)) continue;
|
||||
++c;
|
||||
if (count < limit) filteredKeys.push(k);
|
||||
if (count < limit) filteredParams.push(k);
|
||||
}
|
||||
count += c;
|
||||
}
|
||||
|
||||
filteredKeys.sort();
|
||||
filteredParams.sort((a, b) => {
|
||||
if (a.path < b.path) return -1;
|
||||
if (a.path > b.path) return 1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
const rows = filteredKeys.map((k) => {
|
||||
const p = device[k];
|
||||
const rows = filteredParams.map((p) => {
|
||||
const val = [];
|
||||
const attrs = { key: k };
|
||||
|
||||
if (p.object === false) {
|
||||
if (p.value) {
|
||||
val.push(
|
||||
m(
|
||||
"parameter",
|
||||
Object.assign({ device: device, parameter: memoizedParse(k) }),
|
||||
Object.assign({ device: device, parameter: p.path }),
|
||||
),
|
||||
);
|
||||
} else if (p.object && p.writable) {
|
||||
if (instanceRegex.test(k)) {
|
||||
if (instanceRegex.test(p.path.toString())) {
|
||||
val.push(
|
||||
m(
|
||||
"button",
|
||||
@@ -111,12 +131,16 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "deleteObject",
|
||||
device: device["DeviceID.ID"].value[0] as string,
|
||||
objectName: k,
|
||||
device: device["DeviceID.ID"] as string,
|
||||
objectName: p.path.toString(),
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("delete-instance"),
|
||||
m(icon, {
|
||||
name: "delete-instance",
|
||||
class:
|
||||
"inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@@ -128,12 +152,16 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "addObject",
|
||||
device: device["DeviceID.ID"].value[0] as string,
|
||||
objectName: k,
|
||||
device: device["DeviceID.ID"] as string,
|
||||
objectName: p.path.toString(),
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("add-instance"),
|
||||
m(icon, {
|
||||
name: "add-instance",
|
||||
class:
|
||||
"inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -147,20 +175,25 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "getParameterValues",
|
||||
device: device["DeviceID.ID"].value[0] as string,
|
||||
parameterNames: [k],
|
||||
device: device["DeviceID.ID"] as string,
|
||||
parameterNames: [p.path.toString()],
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("refresh"),
|
||||
m(icon, {
|
||||
name: "refresh",
|
||||
class: "inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return m(
|
||||
"tr",
|
||||
attrs,
|
||||
m("td.left", m("long-text", { text: k })),
|
||||
m("td.right", val),
|
||||
m(
|
||||
"td.pl-4 pr-2 py-2 truncate",
|
||||
m("long-text", { text: p.path.toString() }),
|
||||
),
|
||||
m("td.pr-4 py-2 text-right flex justify-end", val),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -168,25 +201,37 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
"loading",
|
||||
{ queries: [vnode.attrs.deviceQuery] },
|
||||
m(
|
||||
".all-parameters",
|
||||
".bg-white shadow-sm rounded-lg",
|
||||
search,
|
||||
m(
|
||||
"a.download-csv",
|
||||
".overflow-hidden",
|
||||
m(
|
||||
".overflow-y-scroll h-96 shadow-inner",
|
||||
m(
|
||||
"table.w-full table-fixed font-mono text-xs text-stone-900",
|
||||
m("tbody.divide-y divide-stone-200", rows),
|
||||
),
|
||||
),
|
||||
m(
|
||||
"div.text-stone-700 px-4 py-3 flex justify-between items-end",
|
||||
m(
|
||||
"span.text-xs",
|
||||
"Displaying ",
|
||||
m("span.font-medium", "" + filteredParams.length),
|
||||
" out of ",
|
||||
m("span.font-medium", "" + count),
|
||||
" parameters",
|
||||
),
|
||||
m(
|
||||
"a.text-cyan-700 hover:text-cyan-900 text-sm font-medium",
|
||||
{
|
||||
href: `api/devices/${encodeURIComponent(
|
||||
device["DeviceID.ID"].value[0],
|
||||
device["DeviceID.ID"],
|
||||
)}.csv`,
|
||||
download: "",
|
||||
style: "float: right;",
|
||||
},
|
||||
"Download",
|
||||
),
|
||||
search,
|
||||
m(
|
||||
".parameter-list",
|
||||
m("table", m("tbody", rows)),
|
||||
m(
|
||||
"m",
|
||||
`Displaying ${filteredKeys.length} out of ${count} parameters.`,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
+16
-15
@@ -1,7 +1,6 @@
|
||||
import { Attributes, ClosureComponent } from "mithril";
|
||||
import { map } from "../../lib/common/expression/parser.ts";
|
||||
import memoize from "../../lib/common/memoize.ts";
|
||||
import { Expression } from "../../lib/types.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
import { m } from "../components.ts";
|
||||
import { evaluateExpression, getTimestamp } from "../store.ts";
|
||||
import { FlatDevice } from "../../lib/ui/db.ts";
|
||||
@@ -14,15 +13,16 @@ const evaluateAttributes = memoize(
|
||||
): Attributes => {
|
||||
const res: Attributes = {};
|
||||
for (const [k, v] of Object.entries(attrs)) {
|
||||
const vv = map(v, (e) => {
|
||||
if (
|
||||
Array.isArray(e) &&
|
||||
e[0] === "FUNC" &&
|
||||
e[1] === "ENCODEURICOMPONENT"
|
||||
) {
|
||||
const a = evaluateExpression(e[2], obj);
|
||||
if (a == null) return null;
|
||||
return encodeURIComponent(a as string);
|
||||
const vv = v.evaluate((e) => {
|
||||
if (e instanceof Expression.Literal) return e;
|
||||
else if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "ENCODEURICOMPONENT") {
|
||||
const a = evaluateExpression(e.args[0], obj);
|
||||
if (a instanceof Expression.Literal) {
|
||||
if (a.value == null) return new Expression.Literal(null);
|
||||
return new Expression.Literal(encodeURIComponent(a.value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return e;
|
||||
});
|
||||
@@ -47,9 +47,10 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
}
|
||||
|
||||
const children = Object.values(vnode.attrs.components).map((c) => {
|
||||
if (Array.isArray(c)) c = evaluateExpression(c, device || {});
|
||||
if (c instanceof Expression)
|
||||
c = evaluateExpression(c, device || {}).value;
|
||||
if (typeof c !== "object") return `${c}`;
|
||||
const type = evaluateExpression(c["type"], device || {});
|
||||
const type = evaluateExpression(c["type"], device || {}).value;
|
||||
if (!type) return null;
|
||||
return m(type as string, c);
|
||||
});
|
||||
@@ -59,8 +60,8 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
if (el == null) return children;
|
||||
|
||||
let attrs: Attributes;
|
||||
if (Array.isArray(el)) {
|
||||
el = evaluateExpression(el, device || {});
|
||||
if (el instanceof Expression) {
|
||||
el = evaluateExpression(el, device || {}).value;
|
||||
} else if (typeof el === "object") {
|
||||
if (el["attributes"] != null) {
|
||||
attrs = evaluateAttributes(
|
||||
|
||||
@@ -13,13 +13,13 @@ const component: ClosureComponent = (): Component => {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.primary",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Reboot device",
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "reboot",
|
||||
device: device["DeviceID.ID"].value[0],
|
||||
device: device["DeviceID.ID"],
|
||||
});
|
||||
},
|
||||
},
|
||||
@@ -29,13 +29,13 @@ const component: ClosureComponent = (): Component => {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.critical",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Factory reset device",
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "factoryReset",
|
||||
device: device["DeviceID.ID"].value[0],
|
||||
device: device["DeviceID.ID"],
|
||||
});
|
||||
},
|
||||
},
|
||||
@@ -45,13 +45,13 @@ const component: ClosureComponent = (): Component => {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.critical",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Push a firmware or a config file",
|
||||
onclick: () => {
|
||||
taskQueue.stageDownload({
|
||||
name: "download",
|
||||
devices: [device["DeviceID.ID"].value[0]],
|
||||
devices: [device["DeviceID.ID"]],
|
||||
});
|
||||
},
|
||||
},
|
||||
@@ -61,12 +61,12 @@ const component: ClosureComponent = (): Component => {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.primary",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Delete device",
|
||||
onclick: () => {
|
||||
if (!confirm("Deleting this device. Are you sure?")) return;
|
||||
const deviceId = device["DeviceID.ID"].value[0];
|
||||
const deviceId = device["DeviceID.ID"];
|
||||
|
||||
store
|
||||
.deleteResource("devices", deviceId)
|
||||
@@ -83,7 +83,7 @@ const component: ClosureComponent = (): Component => {
|
||||
),
|
||||
);
|
||||
|
||||
return m(".actions-bar", buttons);
|
||||
return m("div.flex gap-3 mt-4", buttons);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -2,19 +2,25 @@ import { ClosureComponent, Component } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import * as store from "../store.ts";
|
||||
import * as notifications from "../notifications.ts";
|
||||
import { getIcon } from "../icons.ts";
|
||||
import { stringify } from "../../lib/common/yaml.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
import Path from "../../lib/common/path.ts";
|
||||
|
||||
const component: ClosureComponent = (): Component => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
const device = vnode.attrs["device"];
|
||||
const deviceId = device["DeviceID.ID"].value[0];
|
||||
const faults = store.fetch("faults", [
|
||||
"AND",
|
||||
[">", ["PARAM", "_id"], `${deviceId}:`],
|
||||
["<", ["PARAM", "_id"], `${deviceId}:zzzz`],
|
||||
]);
|
||||
const deviceId = device["DeviceID.ID"];
|
||||
const p = new Expression.Parameter(Path.parse("_id"));
|
||||
const exp = Expression.and(
|
||||
new Expression.Binary(">", p, new Expression.Literal(`${deviceId}:`)),
|
||||
new Expression.Binary(
|
||||
"<",
|
||||
p,
|
||||
new Expression.Literal(`${deviceId}:zzzz`),
|
||||
),
|
||||
);
|
||||
const faults = store.fetch("faults", exp);
|
||||
|
||||
const headers = [
|
||||
"Channel",
|
||||
@@ -23,24 +29,64 @@ const component: ClosureComponent = (): Component => {
|
||||
"Detail",
|
||||
"Retries",
|
||||
"Timestamp",
|
||||
].map((l) => m("th", l));
|
||||
const thead = m("thead", m("tr", headers));
|
||||
"",
|
||||
].map((l, i) => {
|
||||
let padding: string;
|
||||
if (i === 0) padding = "pl-6 pr-3";
|
||||
else if (i === 6) padding = "pl-3";
|
||||
else padding = "px-3";
|
||||
return m(
|
||||
"th",
|
||||
{
|
||||
scope: "col",
|
||||
class:
|
||||
"py-3.5 text-left text-sm font-semibold text-stone-500 " +
|
||||
padding,
|
||||
},
|
||||
l,
|
||||
);
|
||||
});
|
||||
const thead = m("thead.bg-stone-50", m("tr", headers));
|
||||
|
||||
const rows = [];
|
||||
for (const f of faults.value) {
|
||||
rows.push([
|
||||
m("td", f["channel"]),
|
||||
m("td", f["code"]),
|
||||
m("td", m("long-text", { text: f["message"] })),
|
||||
m("td", m("long-text", { text: stringify(f["detail"]) })),
|
||||
|
||||
m("td", f["retries"]),
|
||||
m("td", new Date(f["timestamp"]).toLocaleString()),
|
||||
rows.push(
|
||||
m(
|
||||
"td",
|
||||
"tr",
|
||||
m(
|
||||
"td.whitespace-nowrap pl-6 pr-3 py-4 text-sm text-stone-900",
|
||||
f["channel"],
|
||||
),
|
||||
m(
|
||||
"td.whitespace-nowrap px-3 py-4 text-sm text-stone-900",
|
||||
f["code"],
|
||||
),
|
||||
m(
|
||||
"td.whitespace-nowrap px-3 py-4 text-sm text-stone-900",
|
||||
m("long-text", { text: f["message"], class: "max-w-xs" }),
|
||||
),
|
||||
m(
|
||||
"td.whitespace-nowrap px-3 py-4 text-sm text-stone-900",
|
||||
m("long-text", {
|
||||
text: stringify(f["detail"]),
|
||||
class: "max-w-xs",
|
||||
}),
|
||||
),
|
||||
|
||||
m(
|
||||
"td.whitespace-nowrap px-3 py-4 text-sm text-stone-900",
|
||||
f["retries"],
|
||||
),
|
||||
m(
|
||||
"td.whitespace-nowrap px-3 py-4 text-sm text-stone-900",
|
||||
new Date(f["timestamp"]).toLocaleString(),
|
||||
),
|
||||
m(
|
||||
"td.whitespace-nowrap pl-3 pr-6 py-4 text-sm text-stone-900",
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
class: "text-cyan-700 hover:text-cyan-900 font-medium",
|
||||
title: "Delete fault",
|
||||
onclick: (e) => {
|
||||
e.redraw = false;
|
||||
@@ -57,29 +103,37 @@ const component: ClosureComponent = (): Component => {
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("remove"),
|
||||
"Delete",
|
||||
),
|
||||
),
|
||||
]);
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let tbody;
|
||||
if (rows.length) {
|
||||
tbody = m(
|
||||
"tbody",
|
||||
rows.map((r) => m("tr", r)),
|
||||
);
|
||||
} else {
|
||||
tbody = m(
|
||||
"tbody",
|
||||
m("tr.empty", m("td", { colspan: headers.length }, "No faults")),
|
||||
if (!rows.length) {
|
||||
rows.push(
|
||||
m(
|
||||
"tr",
|
||||
m(
|
||||
"td.bg-stripes text-sm font-medium text-center text-stone-500 p-4",
|
||||
{ colspan: headers.length },
|
||||
"No faults",
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return m(
|
||||
"loading",
|
||||
{ queries: [faults] },
|
||||
m("table.table", thead, tbody),
|
||||
m(
|
||||
"div.shadow-sm overflow-hidden rounded-lg w-max",
|
||||
m(
|
||||
"table.divide-y divide-stone-200",
|
||||
thead,
|
||||
m("tbody.divide-y divide-stone-200 bg-white", rows),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
import { ClosureComponent, Component } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import { evaluateExpression } from "../store.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
|
||||
const component: ClosureComponent = (): Component => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
let deviceId;
|
||||
const device = vnode.attrs["device"];
|
||||
if (device) deviceId = device["DeviceID.ID"].value[0];
|
||||
if (device) deviceId = device["DeviceID.ID"];
|
||||
|
||||
const children = Object.values(vnode.attrs["components"]).map((c) => {
|
||||
if (Array.isArray(c)) c = evaluateExpression(c, device || {});
|
||||
if (c instanceof Expression)
|
||||
c = evaluateExpression(c, device ?? {}).value;
|
||||
if (typeof c !== "object") return `${c}`;
|
||||
const type = evaluateExpression(c["type"], device || {});
|
||||
const type = evaluateExpression(c["type"], device ?? {}).value;
|
||||
if (!type) return null;
|
||||
const attrs = Object.assign({}, vnode.attrs, c);
|
||||
return m(type as string, attrs);
|
||||
});
|
||||
if (deviceId) {
|
||||
return m(
|
||||
"a",
|
||||
"a.text-cyan-700 hover:text-cyan-900 font-medium",
|
||||
{ href: `#!/devices/${encodeURIComponent(deviceId)}` },
|
||||
children,
|
||||
);
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
import { ClosureComponent, Component } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import config from "../config.ts";
|
||||
import { overview } from "../config.ts";
|
||||
import { evaluateExpression } from "../store.ts";
|
||||
|
||||
const CHARTS = config.ui.overview.charts;
|
||||
const CHARTS = overview.charts;
|
||||
|
||||
const component: ClosureComponent = (): Component => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
const device = vnode.attrs["device"];
|
||||
const chartName = evaluateExpression(vnode.attrs["chart"], device || {});
|
||||
const chart = CHARTS[chartName as string] as Record<string, unknown>;
|
||||
const chartName = evaluateExpression(
|
||||
vnode.attrs["chart"],
|
||||
device ?? {},
|
||||
).value;
|
||||
const chart = CHARTS[chartName as string];
|
||||
if (!chart) return null;
|
||||
for (const slice of Object.values(chart.slices)) {
|
||||
const filter = slice["filter"];
|
||||
if (evaluateExpression(filter, device || {})) {
|
||||
for (const slice of chart.slices) {
|
||||
const filter = slice.filter;
|
||||
if (evaluateExpression(filter, device ?? {}).value) {
|
||||
const dot = m(
|
||||
"svg",
|
||||
"svg.inline",
|
||||
{
|
||||
width: "1em",
|
||||
height: "1em",
|
||||
style: "margin: 0 0.2em 0.2em",
|
||||
xmlns: "http://www.w3.org/2000/svg",
|
||||
"xmlns:xlink": "http://www.w3.org/1999/xlink",
|
||||
},
|
||||
m("circle", {
|
||||
m("circle.stroke-stone-200 stroke-1", {
|
||||
cx: "0.5em",
|
||||
cy: "0.5em",
|
||||
r: "0.4em",
|
||||
fill: evaluateExpression(slice["color"], null),
|
||||
fill: slice.color,
|
||||
}),
|
||||
);
|
||||
return m(
|
||||
"span.overview-dot",
|
||||
dot,
|
||||
evaluateExpression(slice["label"], null),
|
||||
);
|
||||
return m("span", dot, slice.label);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { ClosureComponent, VnodeDOM } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import { QueryResponse, evaluateExpression } from "../store.ts";
|
||||
import { FlatDevice } from "../../lib/ui/db.ts";
|
||||
import { Expression } from "../../lib/types.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
|
||||
interface Attrs {
|
||||
device: FlatDevice;
|
||||
@@ -19,7 +19,11 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
const device = vnode.attrs.device;
|
||||
|
||||
const rows = Object.values(vnode.attrs.parameters).map((parameter) => {
|
||||
const type = evaluateExpression(parameter.type, device);
|
||||
let type = "parameter";
|
||||
if (parameter.type) {
|
||||
const t = evaluateExpression(parameter.type, device);
|
||||
if (typeof t.value === "string") type = t.value;
|
||||
}
|
||||
const p = m.context(
|
||||
{
|
||||
device: device,
|
||||
@@ -30,7 +34,7 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
);
|
||||
|
||||
return m(
|
||||
"tr",
|
||||
"div.py-3 grid grid-cols-3 gap-4 px-6",
|
||||
{
|
||||
oncreate: (vn) => {
|
||||
(vn.dom as HTMLElement).style.display = (p as VnodeDOM).dom
|
||||
@@ -43,15 +47,22 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
: "none";
|
||||
},
|
||||
},
|
||||
m("th", evaluateExpression(parameter.label, device)),
|
||||
m("td", p),
|
||||
m(
|
||||
"dt.text-sm font-medium text-stone-500",
|
||||
evaluateExpression(parameter["label"], device).value,
|
||||
),
|
||||
m("dd.text-sm text-stone-900 col-span-2", p),
|
||||
);
|
||||
});
|
||||
|
||||
return m(
|
||||
"loading",
|
||||
{ queries: [vnode.attrs.deviceQuery] },
|
||||
m("table.parameter-list", rows),
|
||||
m(
|
||||
"dl.bg-white shadow-sm overflow-hidden rounded-lg w-max py-1",
|
||||
{ class: "[&>*+*]:border-t [&>*+*]:border-stone-200" },
|
||||
rows,
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -2,78 +2,103 @@ import { ClosureComponent } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import * as taskQueue from "../task-queue.ts";
|
||||
import { QueryResponse, evaluateExpression } from "../store.ts";
|
||||
import * as expressionParser from "../../lib/common/expression/parser.ts";
|
||||
import { getIcon } from "../icons.ts";
|
||||
import { icon } from "../tailwind-utility-components.ts";
|
||||
import { FlatDevice } from "../../lib/ui/db.ts";
|
||||
import { Expression } from "../../lib/types.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
import Path from "../../lib/common/path.ts";
|
||||
|
||||
interface Attrs {
|
||||
device: FlatDevice;
|
||||
parameter: Expression;
|
||||
label: Expression;
|
||||
childParameters: Record<string, { label: string; parameter: Expression }>;
|
||||
childParameters: Record<string, { label: Expression; parameter: Expression }>;
|
||||
filter?: Expression;
|
||||
deviceQuery: QueryResponse;
|
||||
}
|
||||
|
||||
const component: ClosureComponent<Attrs> = () => {
|
||||
let object: Path;
|
||||
let parameters: { label: Expression; parameter: Expression }[];
|
||||
|
||||
return {
|
||||
oninit: (vnode) => {
|
||||
const obj = vnode.attrs.parameter;
|
||||
if (!Array.isArray(obj) || obj[0] !== "PARAM")
|
||||
if (!(obj instanceof Expression.Parameter))
|
||||
throw new Error("Object must be a parameter path");
|
||||
vnode.state["object"] = obj[1];
|
||||
vnode.state["parameters"] = Object.values(vnode.attrs.childParameters);
|
||||
object = obj.path;
|
||||
parameters = Object.values(vnode.attrs.childParameters);
|
||||
},
|
||||
view: (vnode) => {
|
||||
const device = vnode.attrs.device;
|
||||
const object = evaluateExpression(vnode.state["object"], device);
|
||||
const parameters = vnode.state["parameters"];
|
||||
|
||||
if (typeof object !== "string" || !device[object]) return null;
|
||||
|
||||
const instances: Set<string> = new Set();
|
||||
const prefix = `${object}.`;
|
||||
const prefix = `${object.toString()}.`;
|
||||
for (const p in device) {
|
||||
if (p.startsWith(prefix)) {
|
||||
if (!p.startsWith(prefix)) continue;
|
||||
if (p.lastIndexOf(":") !== -1) continue;
|
||||
const i = p.indexOf(".", prefix.length);
|
||||
if (i === -1) instances.add(p);
|
||||
else instances.add(p.slice(0, i));
|
||||
}
|
||||
}
|
||||
|
||||
const headers = Object.values(parameters).map((p) =>
|
||||
m("th", evaluateExpression(p["label"], device)),
|
||||
const headers = parameters.map((p, i) => {
|
||||
const padding = i ? "px-3" : "pl-6 pr-3";
|
||||
|
||||
return m(
|
||||
"th",
|
||||
{
|
||||
scope: "col",
|
||||
class:
|
||||
"py-3.5 text-left text-sm font-semibold text-stone-500 " +
|
||||
padding,
|
||||
},
|
||||
evaluateExpression(p.label, device).value,
|
||||
);
|
||||
});
|
||||
|
||||
const thead = m("thead", m("tr", headers));
|
||||
headers.push(m("th.pl-3", { scope: "col" }));
|
||||
|
||||
const thead = m("thead.bg-stone-50", m("tr", headers));
|
||||
|
||||
const rows = [];
|
||||
for (const i of instances) {
|
||||
let filter = "filter" in vnode.attrs ? vnode.attrs.filter : true;
|
||||
let filter: Expression =
|
||||
"filter" in vnode.attrs
|
||||
? vnode.attrs.filter
|
||||
: new Expression.Literal(true);
|
||||
|
||||
filter = expressionParser.map(filter, (e) => {
|
||||
if (Array.isArray(e) && e[0] === "PARAM")
|
||||
return ["PARAM", ["||", i, ".", e[1]]];
|
||||
const root = Path.parse(i);
|
||||
filter = filter.evaluate((e) => {
|
||||
if (e instanceof Expression.Parameter)
|
||||
return new Expression.Parameter(root.concat(e.path));
|
||||
return e;
|
||||
});
|
||||
|
||||
if (!evaluateExpression(filter, device)) continue;
|
||||
if (!evaluateExpression(filter, device).value) continue;
|
||||
|
||||
const row = parameters.map((p) => {
|
||||
const param = expressionParser.map(p.parameter, (e) => {
|
||||
if (Array.isArray(e) && e[0] === "PARAM")
|
||||
return ["PARAM", ["||", i, ".", e[1]]];
|
||||
const row = parameters.map((p, j) => {
|
||||
const padding = j ? "px-3" : "pl-6 pr-3";
|
||||
|
||||
const param = p.parameter.evaluate((e) => {
|
||||
if (e instanceof Expression.Parameter)
|
||||
return new Expression.Parameter(root.concat(e.path));
|
||||
return e;
|
||||
});
|
||||
|
||||
let type = "parameter";
|
||||
if (p["type"] instanceof Expression)
|
||||
type = evaluateExpression(p["type"], device).value + "";
|
||||
|
||||
return m(
|
||||
"td",
|
||||
{
|
||||
class: "whitespace-nowrap py-4 text-sm text-stone-900 " + padding,
|
||||
},
|
||||
m.context(
|
||||
{
|
||||
device: device,
|
||||
parameter: param,
|
||||
},
|
||||
p.type || "parameter",
|
||||
type,
|
||||
Object.assign({}, p, {
|
||||
device: device,
|
||||
parameter: param,
|
||||
@@ -83,10 +108,14 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
);
|
||||
});
|
||||
|
||||
if (device[i].writable === true) {
|
||||
if (device[i + ":writable"]) {
|
||||
row.push(
|
||||
m(
|
||||
"td",
|
||||
{
|
||||
class:
|
||||
"whitespace-nowrap pl-3 pr-6 py-4 text-sm text-stone-900",
|
||||
},
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
@@ -94,26 +123,38 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "deleteObject",
|
||||
device: device["DeviceID.ID"].value[0] as string,
|
||||
device: device["DeviceID.ID"] as string,
|
||||
objectName: i,
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("delete-instance"),
|
||||
m(icon, {
|
||||
name: "delete-instance",
|
||||
class: "inline h-4 w-4 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
row.push(m("td"));
|
||||
}
|
||||
rows.push(m("tr", row));
|
||||
}
|
||||
|
||||
if (!rows.length) {
|
||||
rows.push(
|
||||
m("tr.empty", m("td", { colspan: headers.length }, "No instances")),
|
||||
m(
|
||||
"tr",
|
||||
m(
|
||||
"td.bg-stripes text-sm font-medium text-center text-stone-500 p-4",
|
||||
{ colspan: headers.length },
|
||||
"No instances",
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (device[object].writable === true) {
|
||||
if (device[object.toString() + ":writable"]) {
|
||||
rows.push(
|
||||
m(
|
||||
"tr",
|
||||
@@ -127,12 +168,16 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
onclick: () => {
|
||||
taskQueue.queueTask({
|
||||
name: "addObject",
|
||||
device: device["DeviceID.ID"].value[0] as string,
|
||||
objectName: object,
|
||||
device: device["DeviceID.ID"] as string,
|
||||
objectName: object.toString(),
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("add-instance"),
|
||||
m(icon, {
|
||||
name: "add-instance",
|
||||
class:
|
||||
"inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -149,7 +194,14 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
m(
|
||||
"loading",
|
||||
{ queries: [vnode.attrs.deviceQuery] },
|
||||
m("table.table", thead, m("tbody", rows)),
|
||||
m(
|
||||
"div.shadow-sm overflow-hidden rounded-lg w-max",
|
||||
m(
|
||||
"table.divide-y divide-stone-200",
|
||||
thead,
|
||||
m("tbody.divide-y divide-stone-200 bg-white", rows),
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
},
|
||||
|
||||
+46
-50
@@ -2,54 +2,52 @@ import { ClosureComponent, Component, VnodeDOM } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import * as taskQueue from "../task-queue.ts";
|
||||
import * as store from "../store.ts";
|
||||
import { evaluate } from "../../lib/common/expression/util.ts";
|
||||
import Expression, { Value } from "../../lib/common/expression.ts";
|
||||
import memoize from "../../lib/common/memoize.ts";
|
||||
import timeAgo from "../timeago.ts";
|
||||
import { getIcon } from "../icons.ts";
|
||||
import { icon } from "../tailwind-utility-components.ts";
|
||||
|
||||
const evaluateParam = memoize((exp, obj, now: number) => {
|
||||
const evaluateParam = memoize(
|
||||
(
|
||||
exp: Expression,
|
||||
obj: any,
|
||||
now: number,
|
||||
): { value: Value; timestamp: number; parameter: string } => {
|
||||
let timestamp = now;
|
||||
exp = evaluate(exp, null, now, (e) => {
|
||||
if (!Array.isArray(e)) return e;
|
||||
for (let i = 1; i < e.length; ++i) {
|
||||
if (
|
||||
Array.isArray(e[i]) &&
|
||||
e[i][0] === "PARAM" &&
|
||||
!Array.isArray(e[i][1])
|
||||
) {
|
||||
let v = null;
|
||||
const p = obj[e[i][1]];
|
||||
if (p?.value) {
|
||||
v = p.value[0];
|
||||
timestamp = Math.min(timestamp, p.valueTimestamp);
|
||||
const valueMap: Map<Expression.Literal, string> = new Map();
|
||||
const lit = exp.evaluate((e): Expression.Literal => {
|
||||
if (e instanceof Expression.Literal) return e;
|
||||
if (e instanceof Expression.Parameter) {
|
||||
let v = obj[e.path.toString()];
|
||||
if (v) {
|
||||
timestamp = Math.min(
|
||||
timestamp,
|
||||
obj[e.path.toString() + ":valueTimestamp"] ?? 0,
|
||||
);
|
||||
const t = obj[e.path.toString() + ":type"];
|
||||
if (t === "xsd:dateTime" && typeof v === "number")
|
||||
v = new Date(v).toLocaleString();
|
||||
const val = new Expression.Literal(v);
|
||||
valueMap.set(val, e.path.toString());
|
||||
return val;
|
||||
}
|
||||
e = e.slice();
|
||||
e[i] = v;
|
||||
} else if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "NOW") return new Expression.Literal(now);
|
||||
else if (e.name === "DATE_STRING") {
|
||||
const v = e.args[0];
|
||||
if (v instanceof Expression.Literal) {
|
||||
return new Expression.Literal(
|
||||
new Date(v.value as string | number).toLocaleString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (e[0] === "FUNC" && e[1] === "DATE_STRING" && !Array.isArray(e[2]))
|
||||
return new Date(e[2]).toLocaleString();
|
||||
return e;
|
||||
}
|
||||
return new Expression.Literal(null);
|
||||
});
|
||||
|
||||
let parameter = null;
|
||||
let value = null;
|
||||
|
||||
if (!Array.isArray(exp)) {
|
||||
value = exp;
|
||||
} else if (exp[0] === "PARAM") {
|
||||
const p = obj[exp[1]];
|
||||
if (p?.value) {
|
||||
timestamp = p.valueTimestamp;
|
||||
value = p.value[0];
|
||||
parameter = exp[1];
|
||||
if (p.value[1] === "xsd:dateTime" && typeof value === "number")
|
||||
value = new Date(value).toLocaleString();
|
||||
}
|
||||
}
|
||||
|
||||
return { value, timestamp, parameter };
|
||||
});
|
||||
return { value: lit.value, timestamp, parameter: valueMap.get(lit) };
|
||||
},
|
||||
);
|
||||
|
||||
const component: ClosureComponent = (): Component => {
|
||||
return {
|
||||
@@ -65,7 +63,7 @@ const component: ClosureComponent = (): Component => {
|
||||
if (value == null) return null;
|
||||
|
||||
let edit;
|
||||
if (device[parameter]?.writable) {
|
||||
if (device[parameter + ":writable"]) {
|
||||
edit = m(
|
||||
"button",
|
||||
{
|
||||
@@ -73,27 +71,25 @@ const component: ClosureComponent = (): Component => {
|
||||
onclick: () => {
|
||||
taskQueue.stageSpv({
|
||||
name: "setParameterValues",
|
||||
devices: [device["DeviceID.ID"].value[0]],
|
||||
devices: [device["DeviceID.ID"]],
|
||||
parameterValues: [
|
||||
[
|
||||
parameter,
|
||||
device[parameter].value[0],
|
||||
device[parameter].value[1],
|
||||
],
|
||||
[parameter, device[parameter], device[parameter + ":type"]],
|
||||
],
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("edit"),
|
||||
m(icon, {
|
||||
name: "edit",
|
||||
class: "inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const el = m("long-text", { text: `${value}` });
|
||||
|
||||
return m(
|
||||
"span",
|
||||
"span.inline-flex overflow-hidden align-top",
|
||||
{
|
||||
class: "parameter-value",
|
||||
onmouseover: (e) => {
|
||||
e.redraw = false;
|
||||
// Don't update any child element
|
||||
@@ -104,7 +100,7 @@ const component: ClosureComponent = (): Component => {
|
||||
}
|
||||
},
|
||||
},
|
||||
el,
|
||||
m("span.truncate", el),
|
||||
edit,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -47,7 +47,7 @@ const component: ClosureComponent = (vn): Component => {
|
||||
try {
|
||||
const url = new URL(param.value[0]);
|
||||
h = url.hostname;
|
||||
} catch (err) {
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ const component: ClosureComponent = (vn): Component => {
|
||||
}
|
||||
}
|
||||
|
||||
return m("div", host ? `Pinging ${host}:` : "");
|
||||
return m("div.text-sm my-4", host ? `Pinging ${host}:` : "");
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,31 +1,41 @@
|
||||
import { ClosureComponent, Component } from "mithril";
|
||||
import { ClosureComponent } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import * as taskQueue from "../task-queue.ts";
|
||||
import * as store from "../store.ts";
|
||||
import * as notifications from "../notifications.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
import { FlatDevice } from "../../lib/ui/db.ts";
|
||||
|
||||
const component: ClosureComponent = (): Component => {
|
||||
interface Attrs {
|
||||
device: FlatDevice;
|
||||
parameters: Record<string, Expression>;
|
||||
}
|
||||
|
||||
const component: ClosureComponent<Attrs> = () => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
const device = vnode.attrs["device"];
|
||||
const device = vnode.attrs.device;
|
||||
|
||||
return m(
|
||||
"button.primary",
|
||||
"button",
|
||||
{
|
||||
class:
|
||||
"px-2.5 py-1.5 border border-transparent text-xs font-medium rounded-sm shadow-xs text-white bg-cyan-600 hover:bg-cyan-700 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500",
|
||||
title: "Initiate session and refresh basic parameters",
|
||||
onclick: (e) => {
|
||||
e.target.disabled = true;
|
||||
const params = Object.values(vnode.attrs["parameters"])
|
||||
.map((exp) => {
|
||||
if (!Array.isArray(exp) || exp[0] !== "PARAM") return null;
|
||||
return store.evaluateExpression(exp[1], device) as string;
|
||||
if (exp instanceof Expression.Parameter)
|
||||
return exp.path.toString();
|
||||
return null;
|
||||
})
|
||||
.filter((exp) => !!exp);
|
||||
|
||||
const task = {
|
||||
name: "getParameterValues",
|
||||
parameterNames: params,
|
||||
device: device["DeviceID.ID"].value[0],
|
||||
device: device["DeviceID.ID"] as string,
|
||||
};
|
||||
|
||||
taskQueue
|
||||
|
||||
+45
-13
@@ -2,9 +2,9 @@ import { ClosureComponent } from "mithril";
|
||||
import { m } from "../components.ts";
|
||||
import * as notifications from "../notifications.ts";
|
||||
import * as store from "../store.ts";
|
||||
import { getIcon } from "../icons.ts";
|
||||
import { icon } from "../tailwind-utility-components.ts";
|
||||
import { decodeTag } from "../../lib/util.ts";
|
||||
import { Expression } from "../../lib/types.ts";
|
||||
import Expression from "../../lib/common/expression.ts";
|
||||
import { FlatDevice } from "../../lib/ui/db.ts";
|
||||
|
||||
interface Attrs {
|
||||
@@ -22,29 +22,46 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
|
||||
const tags = [];
|
||||
for (const p of Object.keys(device))
|
||||
if (p.startsWith("Tags.")) tags.push(decodeTag(p.slice(5)));
|
||||
if (p.startsWith("Tags.") && p.lastIndexOf(":") === -1)
|
||||
tags.push(decodeTag(p.slice(5)));
|
||||
|
||||
tags.sort();
|
||||
|
||||
if (!writable) {
|
||||
return m(
|
||||
".tags",
|
||||
tags.map((t) => m("span.tag", t)),
|
||||
"div",
|
||||
tags.map((t) =>
|
||||
m(
|
||||
"span",
|
||||
{
|
||||
class:
|
||||
"inline-flex items-center px-3 py-0.5 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800 mr-2 -my-0.5 ring-1 ring-yellow-200",
|
||||
},
|
||||
t,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return m(
|
||||
".tags",
|
||||
"div",
|
||||
tags.map((tag) =>
|
||||
m(
|
||||
"span.tag",
|
||||
"span",
|
||||
{
|
||||
class:
|
||||
"inline-flex items-center pl-3 pr-1 py-0.5 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800 mr-2 ring-1 ring-yellow-200",
|
||||
},
|
||||
tag,
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
title: "Remove tag",
|
||||
class:
|
||||
"flex-shrink-0 ml-0.5 h-4 w-4 rounded-full inline-flex items-center justify-center text-yellow-400 hover:bg-yellow-200 hover:text-yellow-500 focus:outline-hidden focus:bg-yellow-500 focus:text-white",
|
||||
onclick: (e) => {
|
||||
e.target.disabled = true;
|
||||
const deviceId = device["DeviceID.ID"].value[0] as string;
|
||||
const deviceId = device["DeviceID.ID"] as string;
|
||||
store
|
||||
.updateTags(deviceId, { [tag]: false })
|
||||
.then(() => {
|
||||
@@ -64,19 +81,30 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("remove"),
|
||||
m("span.sr-only", "Remove tag"),
|
||||
m(icon, {
|
||||
name: "remove",
|
||||
class: "h-4 w-4",
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
m(
|
||||
"span.tag.writable",
|
||||
m.trust(" "),
|
||||
"span",
|
||||
{
|
||||
class:
|
||||
"inline-flex items-center pl-1 pr-1 py-0.5 rounded-full text-sm font-medium bg-yellow-50 ring-1 ring-yellow-200",
|
||||
},
|
||||
m.trust("​"),
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
title: "Add tag",
|
||||
class:
|
||||
"flex-shrink-0 h-4 w-4 rounded-full inline-flex items-center justify-center text-yellow-400 hover:bg-yellow-200 hover:text-yellow-500 focus:outline-hidden focus:bg-yellow-500 focus:text-white",
|
||||
onclick: (e) => {
|
||||
e.target.disabled = true;
|
||||
const deviceId = device["DeviceID.ID"].value[0] as string;
|
||||
const deviceId = device["DeviceID.ID"] as string;
|
||||
const tag = prompt(`Enter tag to assign to device:`);
|
||||
if (!tag) {
|
||||
e.target.disabled = false;
|
||||
@@ -96,7 +124,11 @@ const component: ClosureComponent<Attrs> = () => {
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("add"),
|
||||
m("span.sr-only", "Add tag"),
|
||||
m(icon, {
|
||||
name: "add",
|
||||
class: "h-4 w-4",
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -79,7 +79,7 @@ export function structureConfig(config: Config[]): any {
|
||||
if (idx >= 0 && idx < MAX_ARRAY_SIZE && String(idx) === key) {
|
||||
const pos = Math.floor(idx / MAX_BITS);
|
||||
if (!indexes[pos]) indexes[pos] = 0;
|
||||
indexes[pos] |= 1 << idx % MAX_BITS;
|
||||
indexes[pos] |= 1 << (idx % MAX_BITS);
|
||||
} else {
|
||||
indexes = [];
|
||||
break;
|
||||
|
||||
+37
-22
@@ -5,9 +5,9 @@ import * as notifications from "./notifications.ts";
|
||||
import putFormComponent from "./put-form-component.ts";
|
||||
import uiConfigComponent from "./ui-config-component.ts";
|
||||
import * as overlay from "./overlay.ts";
|
||||
import { parse, stringify } from "../lib/common/expression/parser.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
import { loadCodeMirror, loadYaml } from "./dynamic-loader.ts";
|
||||
import { getIcon } from "./icons.ts";
|
||||
import { icon } from "./tailwind-utility-components.ts";
|
||||
|
||||
const attributes = [
|
||||
{ id: "_id", label: "Key" },
|
||||
@@ -30,8 +30,8 @@ function putActionHandler(action, _object, isNew?): Promise<ValidationErrors> {
|
||||
if (!id.match(regex)) return void resolve({ _id: "Invalid ID" });
|
||||
|
||||
try {
|
||||
object.value = stringify(parse(object.value || ""));
|
||||
} catch (err) {
|
||||
object.value = Expression.parse(object.value || "").toString();
|
||||
} catch {
|
||||
return void resolve({
|
||||
value: "Config value must be valid expression",
|
||||
});
|
||||
@@ -64,6 +64,7 @@ function putActionHandler(action, _object, isNew?): Promise<ValidationErrors> {
|
||||
})
|
||||
.catch(reject);
|
||||
} else if (action === "delete") {
|
||||
if (!confirm("Deleting config. Are you sure?")) return void resolve(null);
|
||||
store
|
||||
.deleteResource("config", object["_id"])
|
||||
.then(() => {
|
||||
@@ -166,7 +167,10 @@ function renderTable(confsResponse, searchString): Children {
|
||||
);
|
||||
},
|
||||
},
|
||||
getIcon("edit"),
|
||||
m(icon, {
|
||||
name: "edit",
|
||||
class: "inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
);
|
||||
|
||||
const del = m(
|
||||
@@ -181,26 +185,32 @@ function renderTable(confsResponse, searchString): Children {
|
||||
});
|
||||
},
|
||||
},
|
||||
getIcon("remove"),
|
||||
m(icon, {
|
||||
name: "remove",
|
||||
class: "inline h-4 w-4 ml-1 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
);
|
||||
|
||||
rows.push(
|
||||
m(
|
||||
"tr",
|
||||
attrs,
|
||||
m("td.left", m("long-text", { text: conf._id })),
|
||||
m("td.pl-4 pr-2 py-2 truncate", m("long-text", { text: conf._id })),
|
||||
m(
|
||||
"td.right",
|
||||
m("span", [m("long-text", { text: `${conf.value}` }), edit, del]),
|
||||
"td.px-2 py-2 text-right truncate",
|
||||
m("long-text", { text: `${conf.value}` }),
|
||||
),
|
||||
m("td.pl-2 pr-4 py-2 w-max", edit, del),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (!rows.length)
|
||||
rows.push(m("tr.empty", m("td", { colspan: 2 }, "No config")));
|
||||
if (!rows.length) rows.push(m("tr", m("td", { colspan: 3 }, "No config")));
|
||||
|
||||
return m("table", m("tbody", rows));
|
||||
return m(
|
||||
"table.w-full table-fixed font-mono text-sm text-stone-700",
|
||||
m("tbody.bg-white divide-y divide-stone-200", rows),
|
||||
);
|
||||
}
|
||||
|
||||
export const component: ClosureComponent = (): Component => {
|
||||
@@ -208,7 +218,9 @@ export const component: ClosureComponent = (): Component => {
|
||||
view: (vnode) => {
|
||||
document.title = "Config - GenieACS";
|
||||
|
||||
const search = m("input", {
|
||||
const search = m(
|
||||
"input.appearance-none block w-full px-3 py-2 border-stone-300 placeholder-stone-500 text-stone-900 focus:ring-cyan-500 focus:border-cyan-500 sm:text-sm rounded-md mt-1 max-w-screen-sm shadow-sm mb-5",
|
||||
{
|
||||
type: "text",
|
||||
placeholder: "Search config",
|
||||
oninput: (e) => {
|
||||
@@ -217,16 +229,17 @@ export const component: ClosureComponent = (): Component => {
|
||||
clearTimeout(vnode.state["timeout"]);
|
||||
vnode.state["timeout"] = setTimeout(m.redraw, 250);
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
const confs = store.fetch("config", true);
|
||||
const confs = store.fetch("config", new Expression.Literal(true));
|
||||
|
||||
let newConfig;
|
||||
const subs = [];
|
||||
|
||||
if (window.authorizer.hasAccess("config", 3)) {
|
||||
newConfig = m(
|
||||
"button.primary",
|
||||
"button.mr-4 px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500",
|
||||
{
|
||||
title: "Create new config",
|
||||
onclick: () => {
|
||||
@@ -295,7 +308,7 @@ export const component: ClosureComponent = (): Component => {
|
||||
const attrs = { prefix: sub.prefix, name: sub.name, data: sub.data };
|
||||
subs.push(
|
||||
m(
|
||||
"button",
|
||||
"button.mr-4 px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500",
|
||||
{
|
||||
onclick: () => {
|
||||
let cb: () => Children = null;
|
||||
@@ -345,19 +358,21 @@ export const component: ClosureComponent = (): Component => {
|
||||
}
|
||||
|
||||
return [
|
||||
m("h1", "Listing config"),
|
||||
m("h1.text-xl font-medium text-stone-900 mb-5", "Listing config"),
|
||||
m(
|
||||
"loading",
|
||||
{ queries: [confs] },
|
||||
m(
|
||||
".all-parameters",
|
||||
"div",
|
||||
search,
|
||||
m(
|
||||
".parameter-list",
|
||||
{ style: "height: 400px" },
|
||||
".shadow-sm overflow-hidden border-b border-stone-200 rounded-lg bg-white",
|
||||
m(
|
||||
".overflow-y-scroll h-96",
|
||||
renderTable(confs, vnode.state["searchString"]),
|
||||
),
|
||||
m(".actions-bar", [newConfig].concat(subs)),
|
||||
),
|
||||
m(".mt-5", [newConfig].concat(subs)),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
+111
-1
@@ -1,3 +1,113 @@
|
||||
export default window.clientConfig;
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
export const configSnapshot = window.configSnapshot;
|
||||
export const genieacsVersion = window.genieacsVersion;
|
||||
|
||||
type Filters = { label: string; parameter: Expression; type: string }[];
|
||||
type pageSize = number;
|
||||
type overview = {
|
||||
charts: {
|
||||
[name: string]: {
|
||||
label: string;
|
||||
slices: {
|
||||
label: string;
|
||||
filter: Expression;
|
||||
color: string;
|
||||
}[];
|
||||
};
|
||||
};
|
||||
groups: {
|
||||
label: string;
|
||||
charts: string[];
|
||||
}[];
|
||||
};
|
||||
|
||||
type Index = {
|
||||
label: string;
|
||||
type?: string;
|
||||
parameter: Expression;
|
||||
unsortable: boolean;
|
||||
raw: NestedRecord;
|
||||
}[];
|
||||
|
||||
type NestedRecord = { [k: string]: Expression | NestedRecord };
|
||||
|
||||
const conf: NestedRecord = {};
|
||||
for (const [key, value] of Object.entries(window.clientConfig)) {
|
||||
const exp = Expression.parse(value).evaluate((e) => e);
|
||||
let ref = conf;
|
||||
const keyParts = key.split(".");
|
||||
while (keyParts.length > 1) {
|
||||
const k = keyParts.shift();
|
||||
if (ref[k] == null || typeof ref[k] !== "object") ref[k] = {};
|
||||
ref = ref[k] as NestedRecord;
|
||||
}
|
||||
ref[keyParts[0]] = exp;
|
||||
}
|
||||
|
||||
export const filters: Filters = [];
|
||||
export let pageSize: number = 10;
|
||||
export const overview: overview = { charts: {}, groups: [] };
|
||||
export const index: Index = [];
|
||||
export let device: NestedRecord = {};
|
||||
|
||||
for (const obj of Object.values(conf["filters"] || {})) {
|
||||
let label = "";
|
||||
let parameter: Expression = new Expression.Literal(false);
|
||||
let type = "string";
|
||||
if (obj["label"] instanceof Expression.Literal)
|
||||
label = obj["label"].value as string;
|
||||
if (obj["parameter"] instanceof Expression) parameter = obj["parameter"];
|
||||
if (obj["type"] instanceof Expression.Literal)
|
||||
type = obj["type"].value as string;
|
||||
filters.push({ label, parameter, type });
|
||||
}
|
||||
|
||||
for (const obj of Object.values(conf["index"] || {})) {
|
||||
let label = "";
|
||||
let parameter: Expression = new Expression.Literal(null);
|
||||
let unsortable = false;
|
||||
let type = "";
|
||||
if (obj["label"] instanceof Expression.Literal)
|
||||
label = obj["label"].value as string;
|
||||
if (obj["type"] instanceof Expression.Literal)
|
||||
type = obj["type"].value as string;
|
||||
if (obj["parameter"] instanceof Expression) parameter = obj["parameter"];
|
||||
if (obj["unsortable"] instanceof Expression.Literal)
|
||||
unsortable = obj["unsortable"].value as boolean;
|
||||
index.push({ label, type, parameter, unsortable, raw: obj });
|
||||
}
|
||||
|
||||
for (const obj of Object.values(conf["overview"]?.["groups"] || {})) {
|
||||
let label = "";
|
||||
const charts: string[] = [];
|
||||
if (obj["label"] instanceof Expression.Literal)
|
||||
label = obj["label"].value as string;
|
||||
for (const chart of Object.values(obj["charts"] || {})) {
|
||||
if (chart instanceof Expression.Literal) charts.push(chart.value as string);
|
||||
}
|
||||
overview.groups.push({ label, charts });
|
||||
}
|
||||
|
||||
for (const [name, obj] of Object.entries(conf["overview"]?.["charts"] || {})) {
|
||||
const slices: { label: string; filter: Expression; color: string }[] = [];
|
||||
for (const slice of Object.values(obj["slices"] || {})) {
|
||||
let label = "";
|
||||
let filter: Expression = new Expression.Literal(false);
|
||||
let color = "";
|
||||
if (slice["label"] instanceof Expression.Literal)
|
||||
label = slice["label"].value as string;
|
||||
if (slice["filter"] instanceof Expression) filter = slice["filter"];
|
||||
if (slice["color"] instanceof Expression.Literal)
|
||||
color = slice["color"].value as string;
|
||||
slices.push({ label, filter, color });
|
||||
}
|
||||
let label = "";
|
||||
if (obj["label"] instanceof Expression.Literal)
|
||||
label = obj["label"].value as string;
|
||||
overview.charts[name] = { label, slices };
|
||||
}
|
||||
|
||||
if (conf["pageSize"] instanceof Expression.Literal)
|
||||
pageSize = +conf["pageSize"].value || 10;
|
||||
|
||||
device = conf["device"] as NestedRecord;
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+141
-777
@@ -1,805 +1,169 @@
|
||||
@import "normalize.css";
|
||||
@import "codemirror/lib/codemirror.css";
|
||||
@import "tailwindcss";
|
||||
@plugin "@tailwindcss/forms";
|
||||
/* --- BEGIN UNIFI THEME --- */
|
||||
@import "./unifi-theme.css";
|
||||
/* --- END UNIFI THEME --- */
|
||||
|
||||
:root {
|
||||
--base-font-size: 13px;
|
||||
--base-line-height: 16px;
|
||||
@source inline("h-full bg-stone-100");
|
||||
@source "../../ui/**/*.{ts,tsx}";
|
||||
|
||||
--color1: #ebe0bf;
|
||||
--color2: #b9b098;
|
||||
--color3: #f2ebd5;
|
||||
--color4: #046380;
|
||||
--color5: #002f2f;
|
||||
@theme {
|
||||
--font-sans:
|
||||
"Inter", ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji",
|
||||
"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
|
||||
--font-mono:
|
||||
"Roboto Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
|
||||
"Liberation Mono", "Courier New", monospace;
|
||||
|
||||
--fade: 0.2s;
|
||||
/* Reset default color palette to match v3 exclusive palette */
|
||||
--color-*: initial;
|
||||
|
||||
--disabled: #666;
|
||||
--color-black: #000;
|
||||
--color-white: #fff;
|
||||
|
||||
--status-red: 0 80% 80%;
|
||||
--status-green: 125 80% 80%;
|
||||
--status-gray: 58 3% 80%;
|
||||
--status-yellow: 46 79% 80%;
|
||||
--color-cyan-50: #edfdfe;
|
||||
--color-cyan-100: #d3f7fa;
|
||||
--color-cyan-200: #aceef5;
|
||||
--color-cyan-300: #72e0ee;
|
||||
--color-cyan-400: #31c8df;
|
||||
--color-cyan-500: #15abc5;
|
||||
--color-cyan-600: #1589a5;
|
||||
--color-cyan-700: #186e86;
|
||||
--color-cyan-800: #1c5a6e;
|
||||
--color-cyan-900: #1c4b5d;
|
||||
|
||||
--color-stone-50: #faf8f8;
|
||||
--color-stone-100: #f5f2f0;
|
||||
--color-stone-200: #e7e4e2;
|
||||
--color-stone-300: #d6d3d1;
|
||||
--color-stone-400: #a8a29e;
|
||||
--color-stone-500: #78716c;
|
||||
--color-stone-600: #57534e;
|
||||
--color-stone-700: #44403c;
|
||||
--color-stone-800: #292524;
|
||||
--color-stone-900: #1c1917;
|
||||
|
||||
--color-red-50: #fdf3f3;
|
||||
--color-red-100: #fce4e4;
|
||||
--color-red-200: #facece;
|
||||
--color-red-300: #f5acac;
|
||||
--color-red-400: #ee7b7b;
|
||||
--color-red-500: #e25151;
|
||||
--color-red-600: #ce3434;
|
||||
--color-red-700: #ad2828;
|
||||
--color-red-800: #902424;
|
||||
--color-red-900: #782424;
|
||||
|
||||
--color-emerald-50: #edfcf5;
|
||||
--color-emerald-100: #d4f7e5;
|
||||
--color-emerald-200: #adedd0;
|
||||
--color-emerald-300: #77deb5;
|
||||
--color-emerald-400: #40c796;
|
||||
--color-emerald-500: #1dac7d;
|
||||
--color-emerald-600: #108b65;
|
||||
--color-emerald-700: #0d6f53;
|
||||
--color-emerald-800: #0d5843;
|
||||
--color-emerald-900: #0b4938;
|
||||
|
||||
--color-yellow-50: #fcfbea;
|
||||
--color-yellow-100: #faf5c7;
|
||||
--color-yellow-200: #f5e993;
|
||||
--color-yellow-300: #efd755;
|
||||
--color-yellow-400: #e9c226;
|
||||
--color-yellow-500: #d9aa19;
|
||||
--color-yellow-600: #bb8513;
|
||||
--color-yellow-700: #956013;
|
||||
--color-yellow-800: #7c4c17;
|
||||
--color-yellow-900: #6a3f19;
|
||||
|
||||
--color-blue-50: #f0f6fe;
|
||||
--color-blue-100: #deeafb;
|
||||
--color-blue-200: #c4dcf9;
|
||||
--color-blue-300: #9bc5f5;
|
||||
--color-blue-400: #6ca6ee;
|
||||
--color-blue-500: #4985e8;
|
||||
--color-blue-600: #3469dc;
|
||||
--color-blue-700: #2b55ca;
|
||||
--color-blue-800: #2946a4;
|
||||
--color-blue-900: #263e82;
|
||||
|
||||
--background-image-stripes: repeating-linear-gradient(
|
||||
-45deg,
|
||||
transparent,
|
||||
transparent 10px,
|
||||
rgba(0, 0, 0, 0.02) 10px,
|
||||
rgba(0, 0, 0, 0.02) 20px
|
||||
);
|
||||
}
|
||||
|
||||
body,
|
||||
input,
|
||||
button,
|
||||
select,
|
||||
option,
|
||||
textarea {
|
||||
outline-color: var(--color4);
|
||||
font-family: sans-serif;
|
||||
color: var(--color5);
|
||||
}
|
||||
|
||||
a {
|
||||
color: var(--color4);
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background-color var(--fade),
|
||||
color var(--fade);
|
||||
&:visited {
|
||||
color: var(--color4);
|
||||
@supports (font-variation-settings: normal) {
|
||||
@font-face {
|
||||
font-family: "Roboto Mono";
|
||||
src: url("RobotoMono.woff2") format("woff2-variations");
|
||||
font-weight: 100 700;
|
||||
font-style: normal;
|
||||
unicode-range:
|
||||
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
|
||||
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
|
||||
U+FEFF, U+FFFD;
|
||||
font-display: swap;
|
||||
}
|
||||
&:hover {
|
||||
color: white;
|
||||
background-color: var(--color4);
|
||||
}
|
||||
/* text-decoration does not propagate to inline-block children */
|
||||
& span {
|
||||
text-decoration: inherit;
|
||||
}
|
||||
}
|
||||
|
||||
strong,
|
||||
b {
|
||||
font-weight: bold;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "Inter";
|
||||
font-weight: 100 900;
|
||||
font-display: swap;
|
||||
font-style: normal;
|
||||
font-named-instance: "Regular";
|
||||
src: url("InterVariable.woff2") format("woff2-variations");
|
||||
unicode-range:
|
||||
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
|
||||
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
|
||||
U+FEFF, U+FFFD;
|
||||
}
|
||||
|
||||
i {
|
||||
@font-face {
|
||||
font-family: "Inter";
|
||||
font-weight: 100 900;
|
||||
font-display: swap;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
@keyframes pending-blink {
|
||||
from {
|
||||
background-color: hsl(var(--status-green) / 0.9);
|
||||
}
|
||||
to {
|
||||
background-color: transparent;
|
||||
font-named-instance: "Italic";
|
||||
src: url("InterVariable-Italic.woff2") format("woff2-variations");
|
||||
unicode-range:
|
||||
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
|
||||
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
|
||||
U+FEFF, U+FFFD;
|
||||
}
|
||||
}
|
||||
|
||||
body {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#content-wrapper {
|
||||
flex-grow: 1;
|
||||
display: flex;
|
||||
overflow-x: auto;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
#header {
|
||||
background-color: #f2eddf;
|
||||
border-bottom: 1px solid var(--color1);
|
||||
overflow: visible;
|
||||
position: relative;
|
||||
|
||||
& > .drawer-wrapper {
|
||||
position: fixed;
|
||||
z-index: 10;
|
||||
width: 720px;
|
||||
margin-left: -360px;
|
||||
top: 0;
|
||||
left: 50%;
|
||||
& > .drawer {
|
||||
background-color: rgba(255, 255, 255, 0.9);
|
||||
border-bottom-left-radius: 6px;
|
||||
border-bottom-right-radius: 6px;
|
||||
border-bottom: 1px solid var(--color1);
|
||||
border-left: 1px solid var(--color1);
|
||||
border-right: 1px solid var(--color1);
|
||||
margin-top: -1px;
|
||||
box-shadow: 0 0 10px #ccc;
|
||||
overflow: hidden;
|
||||
transition:
|
||||
height var(--fade),
|
||||
opacity var(--fade);
|
||||
|
||||
& > div {
|
||||
padding: 0.3em;
|
||||
.device-page {
|
||||
h1 {
|
||||
@apply text-xl font-medium text-stone-900 mb-5;
|
||||
}
|
||||
|
||||
& > .status {
|
||||
display: flex;
|
||||
& > span {
|
||||
margin-right: 1em;
|
||||
padding-left: 0.2em;
|
||||
padding-right: 0.2em;
|
||||
border-radius: 0.4em;
|
||||
}
|
||||
& > .pending.active {
|
||||
&.active {
|
||||
animation: 0.5s linear 0s infinite alternate pending-blink;
|
||||
}
|
||||
}
|
||||
& > .stale.active {
|
||||
background-color: hsl(var(--status-gray) / 0.9);
|
||||
}
|
||||
& > .fault.active {
|
||||
background-color: hsl(var(--status-red) / 0.9);
|
||||
}
|
||||
& > .queued.active {
|
||||
font-weight: bold;
|
||||
h2 {
|
||||
@apply text-lg font-semibold text-stone-900 mb-5 mt-8;
|
||||
}
|
||||
|
||||
& > .actions {
|
||||
flex-grow: 1;
|
||||
text-align: right;
|
||||
}
|
||||
h3 {
|
||||
@apply text-lg font-medium text-stone-900 mb-5 mt-8;
|
||||
}
|
||||
|
||||
& > .staging {
|
||||
& .parameter {
|
||||
font-style: italic;
|
||||
max-width: 500px;
|
||||
text-overflow: ellipsis;
|
||||
display: inline-block;
|
||||
overflow: hidden;
|
||||
vertical-align: text-bottom;
|
||||
}
|
||||
|
||||
& input {
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
padding: 0.3em;
|
||||
margin-top: 0.3em;
|
||||
margin-bottom: 0.3em;
|
||||
}
|
||||
& .actions {
|
||||
text-align: right;
|
||||
}
|
||||
}
|
||||
|
||||
& > .queue {
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
& > .pending {
|
||||
animation: 0.5s linear 0s infinite alternate pending-blink;
|
||||
border-radius: 0.4em;
|
||||
}
|
||||
& > .stale {
|
||||
background-color: hsl(var(--status-gray) / 0.9);
|
||||
border-radius: 0.4em;
|
||||
}
|
||||
& > .fault {
|
||||
background-color: hsl(var(--status-red) / 0.9);
|
||||
border-radius: 0.4em;
|
||||
}
|
||||
& .parameter {
|
||||
font-style: italic;
|
||||
max-width: 400px;
|
||||
text-overflow: ellipsis;
|
||||
display: inline-block;
|
||||
overflow: hidden;
|
||||
vertical-align: text-bottom;
|
||||
}
|
||||
& .value {
|
||||
max-width: 200px;
|
||||
text-overflow: ellipsis;
|
||||
display: inline-block;
|
||||
overflow: hidden;
|
||||
vertical-align: text-bottom;
|
||||
white-space: nowrap;
|
||||
}
|
||||
& > div {
|
||||
display: flex;
|
||||
& > .actions {
|
||||
flex-grow: 1;
|
||||
text-align: right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
& > .notifications-wrapper {
|
||||
& > .notification {
|
||||
padding: 0.3em;
|
||||
left: 0;
|
||||
right: 0;
|
||||
background-color: rgba(255, 255, 255, 0.9);
|
||||
border: 1px solid var(--color1);
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 0 10px #ccc;
|
||||
transition:
|
||||
opacity var(--fade),
|
||||
top var(--fade);
|
||||
|
||||
&.error {
|
||||
background-color: hsl(var(--status-red) / 0.9);
|
||||
border: 1px solid hsl(var(--status-red));
|
||||
}
|
||||
&.success {
|
||||
background-color: hsl(var(--status-green) / 0.9);
|
||||
border: 1px solid hsl(var(--status-green));
|
||||
}
|
||||
&.warning {
|
||||
background-color: hsl(var(--status-yellow) / 0.9);
|
||||
border: 1px solid hsl(var(--status-yellow));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
& > nav {
|
||||
display: inline-block;
|
||||
overflow: visible;
|
||||
position: absolute;
|
||||
bottom: -1px;
|
||||
|
||||
& > ul {
|
||||
font-weight: bold;
|
||||
display: inline-block;
|
||||
margin: 0 10px;
|
||||
padding: 0;
|
||||
|
||||
& > li {
|
||||
margin: 0 2px;
|
||||
background-color: var(--color1);
|
||||
display: inline-block;
|
||||
border-top-left-radius: 6px;
|
||||
border-top-right-radius: 6px;
|
||||
border-top: 1px solid var(--color1);
|
||||
border-left: 1px solid var(--color1);
|
||||
border-right: 1px solid var(--color1);
|
||||
transition: background-color var(--fade);
|
||||
&.active,
|
||||
&:hover {
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
& > a,
|
||||
& > a:visited,
|
||||
& > a:hover {
|
||||
display: inline-block;
|
||||
min-width: 140px;
|
||||
color: var(--color4);
|
||||
text-decoration: none;
|
||||
background: none;
|
||||
padding: 8px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
& > .logo {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
& > img {
|
||||
margin: 10px;
|
||||
height: 56px;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
& > .version {
|
||||
position: absolute;
|
||||
bottom: 5px;
|
||||
right: 10px;
|
||||
font-family: monospace;
|
||||
font-size: 10px;
|
||||
color: #666;
|
||||
}
|
||||
}
|
||||
|
||||
& > .user-menu {
|
||||
float: right;
|
||||
margin: 5px;
|
||||
}
|
||||
}
|
||||
|
||||
#side-menu {
|
||||
width: 200px;
|
||||
min-width: 200px;
|
||||
& > ul {
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
|
||||
& > li {
|
||||
& > a {
|
||||
display: block;
|
||||
text-decoration: none;
|
||||
margin: 5px 0;
|
||||
padding: 8px;
|
||||
border-bottom-right-radius: 6px;
|
||||
border-top-right-radius: 6px;
|
||||
background-color: var(--color3);
|
||||
&:hover {
|
||||
background-color: var(--color4);
|
||||
color: var(--color3);
|
||||
}
|
||||
}
|
||||
|
||||
&.active {
|
||||
font-weight: bold;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#content {
|
||||
flex-grow: 1;
|
||||
margin: 10px;
|
||||
min-height: 250px;
|
||||
|
||||
/* Workaround for Chrome ignoring bottom margin when flex container overflows */
|
||||
&::after {
|
||||
content: "";
|
||||
display: block;
|
||||
padding-bottom: 1px;
|
||||
}
|
||||
}
|
||||
|
||||
table.table {
|
||||
border-spacing: 0;
|
||||
text-align: left;
|
||||
overflow-x: auto;
|
||||
|
||||
& table.table {
|
||||
font-size: 90%;
|
||||
}
|
||||
|
||||
& th {
|
||||
white-space: nowrap;
|
||||
color: var(--color4);
|
||||
font-weight: bold;
|
||||
border-bottom: 2px solid var(--color4);
|
||||
margin: 0;
|
||||
padding: 0.2em 0.5em;
|
||||
}
|
||||
|
||||
& td {
|
||||
margin: 0;
|
||||
padding: 0.2em 0.5em;
|
||||
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
& tfoot td > a {
|
||||
margin-left: 1em;
|
||||
}
|
||||
|
||||
&.highlight > tbody > tr {
|
||||
& > td {
|
||||
border-bottom: 1px dotted var(--color1);
|
||||
}
|
||||
transition: background-color var(--fade);
|
||||
&:hover {
|
||||
background-color: var(--color3);
|
||||
}
|
||||
}
|
||||
|
||||
& tbody > tr.empty {
|
||||
text-align: center;
|
||||
color: var(--disabled);
|
||||
&:hover {
|
||||
background-color: initial;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: var(--color4);
|
||||
font-size: 130%;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
h2 {
|
||||
color: var(--color5);
|
||||
font-weight: bold;
|
||||
font-size: 120%;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: var(--color5);
|
||||
font-size: 110%;
|
||||
}
|
||||
|
||||
p.error {
|
||||
color: #a00;
|
||||
}
|
||||
|
||||
/* Remove button padding in FF */
|
||||
input::-moz-focus-inner {
|
||||
border: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
input[type="button"],
|
||||
button {
|
||||
background: transparent;
|
||||
border: 0;
|
||||
color: var(--color4);
|
||||
border-radius: 4px;
|
||||
transition:
|
||||
background-color var(--fade),
|
||||
color var(--fade);
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
|
||||
&.primary {
|
||||
border: 1px solid var(--color1);
|
||||
background-color: var(--color3);
|
||||
}
|
||||
&.critical {
|
||||
border: 1px solid var(--color1);
|
||||
background-color: var(--color3);
|
||||
color: #700;
|
||||
}
|
||||
&:hover:enabled {
|
||||
background-color: var(--color4);
|
||||
color: var(--color3);
|
||||
}
|
||||
&:disabled {
|
||||
color: var(--disabled);
|
||||
cursor: not-allowed;
|
||||
}
|
||||
}
|
||||
|
||||
.CodeMirror,
|
||||
textarea,
|
||||
select,
|
||||
input {
|
||||
display: block;
|
||||
border: 1px solid var(--color1);
|
||||
background-color: var(--color3);
|
||||
border-radius: 4px;
|
||||
transition:
|
||||
border-color var(--fade),
|
||||
background-color var(--fade);
|
||||
&.error {
|
||||
background-color: #efcbcf;
|
||||
border-color: #e6b2b7;
|
||||
}
|
||||
&:focus,
|
||||
&:hover {
|
||||
border-color: var(--color4);
|
||||
}
|
||||
}
|
||||
|
||||
.filter {
|
||||
margin-bottom: 0.5em;
|
||||
& > input {
|
||||
font-size: 90%;
|
||||
width: 35em;
|
||||
margin: 2px;
|
||||
padding: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.page-overview {
|
||||
& > h1 {
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
.overview-chart-group {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
& h2 {
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
table.parameter-list {
|
||||
border-spacing: 0;
|
||||
text-align: left;
|
||||
& th {
|
||||
padding: 0;
|
||||
padding-right: 1em;
|
||||
}
|
||||
}
|
||||
|
||||
.overview-dot {
|
||||
& > svg {
|
||||
margin-bottom: -0.125em;
|
||||
margin-left: 0.2em;
|
||||
margin-right: 0.2em;
|
||||
& > circle {
|
||||
stroke: var(--color5);
|
||||
stroke-width: 0.08em;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
span.inform {
|
||||
& > * {
|
||||
margin-right: 0.5em;
|
||||
}
|
||||
}
|
||||
|
||||
.pie-chart {
|
||||
margin: 20px;
|
||||
text-align: center;
|
||||
|
||||
& > svg {
|
||||
& > path {
|
||||
stroke: var(--color1);
|
||||
stroke-width: 1px;
|
||||
}
|
||||
|
||||
& > a {
|
||||
transition: opacity var(--fade);
|
||||
opacity: 0;
|
||||
|
||||
& > text {
|
||||
font-weight: bold;
|
||||
opacity: 0.4;
|
||||
}
|
||||
|
||||
& > path {
|
||||
stroke: var(--color4);
|
||||
}
|
||||
|
||||
&:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
& > .legend {
|
||||
text-align: left;
|
||||
margin: 15px;
|
||||
|
||||
& > .legend-line {
|
||||
& > .color {
|
||||
height: 12px;
|
||||
width: 12px;
|
||||
margin-right: 5px;
|
||||
border: 1px solid var(--color1);
|
||||
display: inline-block;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
span.na {
|
||||
color: var(--disabled);
|
||||
}
|
||||
|
||||
.all-parameters {
|
||||
& > input {
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
margin-bottom: 2px;
|
||||
padding: 4px;
|
||||
width: 700px;
|
||||
}
|
||||
|
||||
& > .parameter-list {
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
& button {
|
||||
font-size: 16px;
|
||||
}
|
||||
overflow-y: scroll;
|
||||
height: 200px;
|
||||
|
||||
& > table {
|
||||
width: 100%;
|
||||
table-layout: fixed;
|
||||
border-spacing: 0;
|
||||
& > tbody {
|
||||
& > tr {
|
||||
& > td {
|
||||
overflow: hidden;
|
||||
white-space: nowrap;
|
||||
border-bottom: 1px dotted var(--color1);
|
||||
}
|
||||
transition: background-color var(--fade);
|
||||
&:hover {
|
||||
background-color: var(--color3);
|
||||
}
|
||||
|
||||
& > td.left {
|
||||
& > * {
|
||||
max-width: 100%;
|
||||
}
|
||||
}
|
||||
& > td.right {
|
||||
text-align: right;
|
||||
padding-right: 0.5em;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.container-right {
|
||||
float: right;
|
||||
clear: right;
|
||||
}
|
||||
|
||||
.container-full-width {
|
||||
clear: both;
|
||||
}
|
||||
|
||||
.actions-bar {
|
||||
margin-top: 10px;
|
||||
margin-bottom: 10px;
|
||||
span.inform {
|
||||
@apply flex gap-3;
|
||||
& > button {
|
||||
margin-right: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
span.tag {
|
||||
display: inline-block;
|
||||
background-image: url('data:image/svg+xml;utf-8,<svg xmlns="http://www.w3.org/2000/svg" width="190" height="16"><path d="M6.59 15.42l-6-6a2 2 0 0 1 0-2.83l6-6A2 2 0 0 1 8 0h180a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H8a2 2 0 0 1-1.41-.58zM4.35 6.59A2 2 0 1 0 7.17 9.4 2 2 0 0 0 4.35 6.6z" fill="%23e8d5b2"></path></svg>');
|
||||
background-size: cover;
|
||||
background-repeat: no-repeat;
|
||||
border-radius: 0.2em;
|
||||
padding-left: 1em;
|
||||
padding-right: 0.2em;
|
||||
}
|
||||
|
||||
.tags {
|
||||
& > .tag {
|
||||
margin-right: 0.5em;
|
||||
}
|
||||
}
|
||||
|
||||
.overlay-wrapper {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
right: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
background-color: rgba(0, 0, 0, 0.2);
|
||||
z-index: 100;
|
||||
transition:
|
||||
opacity var(--fade),
|
||||
top var(--fade);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
& > .overlay {
|
||||
max-width: 90vw;
|
||||
max-height: 90vh;
|
||||
background-color: white;
|
||||
border: 1px solid var(--color1);
|
||||
border-radius: 10px;
|
||||
padding: 10px;
|
||||
box-shadow: 0 0 15px #aaa;
|
||||
overflow: auto;
|
||||
}
|
||||
}
|
||||
|
||||
.overlay > .put-form {
|
||||
min-width: 700px;
|
||||
}
|
||||
|
||||
.autocomplete {
|
||||
box-shadow: 0 0 10px #ccc;
|
||||
background-color: rgba(255, 255, 255, 0.9);
|
||||
border: 1px solid var(--color1);
|
||||
border-radius: 4px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
transition: opacity var(--fade);
|
||||
& > .suggestion {
|
||||
padding: 5px;
|
||||
|
||||
&.selected,
|
||||
&:hover {
|
||||
background-color: var(--color3);
|
||||
@apply -my-1.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
span.long-text {
|
||||
display: inline-block;
|
||||
overflow-x: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
max-width: 400px;
|
||||
/*
|
||||
"overflow: hidden" changes the baseline as explained here:
|
||||
https://stackoverflow.com/questions/9273016/why-is-this-inline-block-element-pushed-downward
|
||||
*/
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
span.long-text-overflowed {
|
||||
@layer base {
|
||||
button:not(:disabled),
|
||||
[role="button"]:not(:disabled) {
|
||||
cursor: pointer;
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
text-decoration-style: dotted;
|
||||
}
|
||||
|
||||
:focus-visible {
|
||||
@apply outline-2 outline-cyan-500;
|
||||
}
|
||||
}
|
||||
|
||||
textarea.long-text {
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
min-width: 10vw;
|
||||
min-height: 10vh;
|
||||
}
|
||||
|
||||
span.parameter-value {
|
||||
& > span:empty::before {
|
||||
content: "blank";
|
||||
color: #aaa;
|
||||
}
|
||||
}
|
||||
|
||||
.wizard-dialog {
|
||||
max-width: 720px;
|
||||
margin: 10px auto;
|
||||
padding: 10px;
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--color1);
|
||||
box-shadow: 0 0 10px #ccc;
|
||||
}
|
||||
|
||||
.icon {
|
||||
display: block;
|
||||
width: 1em;
|
||||
height: 1em;
|
||||
fill: none;
|
||||
stroke: currentColor;
|
||||
stroke-width: 2;
|
||||
stroke-linecap: round;
|
||||
stroke-linejoin: round;
|
||||
&.icon-unsorted {
|
||||
opacity: 0.3;
|
||||
}
|
||||
}
|
||||
|
||||
.loading {
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
100% {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.loading-overlay {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
&::after {
|
||||
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiBwcmVzZXJ2ZUFzcGVjdFJhdGlvPSJ4TWlkWU1pZCI+PHBhdGggZD0iTTEwIDUwYTQwIDQwIDAgMDA4MCAwIDQwIDQyIDAgMDEtODAgMCIgZmlsbD0iI2IxMmQ1YyIgLz48L3N2Zz4=);
|
||||
content: "";
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
animation: spin 1s linear infinite;
|
||||
background-repeat: no-repeat;
|
||||
background-size: 100% 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.progress {
|
||||
background-color: #eee;
|
||||
overflow: hidden;
|
||||
height: 3px;
|
||||
& > .progress-bar {
|
||||
height: 100%;
|
||||
background-color: #b12d5c;
|
||||
}
|
||||
}
|
||||
|
||||
/* To keep consistent style after changing from Quirks to Standards mode */
|
||||
td,
|
||||
th {
|
||||
line-height: normal;
|
||||
}
|
||||
Vendored
-450
@@ -1,450 +0,0 @@
|
||||
/*! normalize.css v7.0.0 | MIT License | github.com/necolas/normalize.css */
|
||||
|
||||
/* Document
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* 1. Correct the line height in all browsers.
|
||||
* 2. Prevent adjustments of font size after orientation changes in
|
||||
* IE on Windows Phone and in iOS.
|
||||
*/
|
||||
|
||||
html {
|
||||
line-height: 1.15; /* 1 */
|
||||
-ms-text-size-adjust: 100%; /* 2 */
|
||||
-webkit-text-size-adjust: 100%; /* 2 */
|
||||
}
|
||||
|
||||
/* Sections
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* Remove the margin in all browsers (opinionated).
|
||||
*/
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct display in IE 9-.
|
||||
*/
|
||||
|
||||
article,
|
||||
aside,
|
||||
footer,
|
||||
header,
|
||||
nav,
|
||||
section {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Correct the font size and margin on `h1` elements within `section` and
|
||||
* `article` contexts in Chrome, Firefox, and Safari.
|
||||
*/
|
||||
|
||||
h1 {
|
||||
font-size: 2em;
|
||||
margin: 0.67em 0;
|
||||
}
|
||||
|
||||
/* Grouping content
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* Add the correct display in IE 9-.
|
||||
* 1. Add the correct display in IE.
|
||||
*/
|
||||
|
||||
figcaption,
|
||||
figure,
|
||||
main {
|
||||
/* 1 */
|
||||
display: block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct margin in IE 8.
|
||||
*/
|
||||
|
||||
figure {
|
||||
margin: 1em 40px;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Add the correct box sizing in Firefox.
|
||||
* 2. Show the overflow in Edge and IE.
|
||||
*/
|
||||
|
||||
hr {
|
||||
box-sizing: content-box; /* 1 */
|
||||
height: 0; /* 1 */
|
||||
overflow: visible; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Correct the inheritance and scaling of font size in all browsers.
|
||||
* 2. Correct the odd `em` font sizing in all browsers.
|
||||
*/
|
||||
|
||||
pre {
|
||||
font-family: monospace, monospace; /* 1 */
|
||||
font-size: 1em; /* 2 */
|
||||
}
|
||||
|
||||
/* Text-level semantics
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* 1. Remove the gray background on active links in IE 10.
|
||||
* 2. Remove gaps in links underline in iOS 8+ and Safari 8+.
|
||||
*/
|
||||
|
||||
a {
|
||||
background-color: transparent; /* 1 */
|
||||
-webkit-text-decoration-skip: objects; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Remove the bottom border in Chrome 57- and Firefox 39-.
|
||||
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
|
||||
*/
|
||||
|
||||
abbr[title] {
|
||||
border-bottom: none; /* 1 */
|
||||
text-decoration: underline; /* 2 */
|
||||
text-decoration: underline dotted; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent the duplicate application of `bolder` by the next rule in Safari 6.
|
||||
*/
|
||||
|
||||
b,
|
||||
strong {
|
||||
font-weight: inherit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct font weight in Chrome, Edge, and Safari.
|
||||
*/
|
||||
|
||||
b,
|
||||
strong {
|
||||
font-weight: bolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Correct the inheritance and scaling of font size in all browsers.
|
||||
* 2. Correct the odd `em` font sizing in all browsers.
|
||||
*/
|
||||
|
||||
code,
|
||||
kbd,
|
||||
samp {
|
||||
font-family: monospace, monospace; /* 1 */
|
||||
font-size: 1em; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct font style in Android 4.3-.
|
||||
*/
|
||||
|
||||
dfn {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct background and color in IE 9-.
|
||||
*/
|
||||
|
||||
mark {
|
||||
background-color: #ff0;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct font size in all browsers.
|
||||
*/
|
||||
|
||||
small {
|
||||
font-size: 80%;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent `sub` and `sup` elements from affecting the line height in
|
||||
* all browsers.
|
||||
*/
|
||||
|
||||
sub,
|
||||
sup {
|
||||
font-size: 75%;
|
||||
line-height: 0;
|
||||
position: relative;
|
||||
vertical-align: baseline;
|
||||
}
|
||||
|
||||
sub {
|
||||
bottom: -0.25em;
|
||||
}
|
||||
|
||||
sup {
|
||||
top: -0.5em;
|
||||
}
|
||||
|
||||
/* Embedded content
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* Add the correct display in IE 9-.
|
||||
*/
|
||||
|
||||
audio,
|
||||
video {
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct display in iOS 4-7.
|
||||
*/
|
||||
|
||||
audio:not([controls]) {
|
||||
display: none;
|
||||
height: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the border on images inside links in IE 10-.
|
||||
*/
|
||||
|
||||
img {
|
||||
border-style: none;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide the overflow in IE.
|
||||
*/
|
||||
|
||||
svg:not(:root) {
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* Forms
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* 1. Change the font styles in all browsers (opinionated).
|
||||
* 2. Remove the margin in Firefox and Safari.
|
||||
*/
|
||||
|
||||
button,
|
||||
input,
|
||||
optgroup,
|
||||
select,
|
||||
textarea {
|
||||
font-family: sans-serif; /* 1 */
|
||||
font-size: 100%; /* 1 */
|
||||
line-height: 1.15; /* 1 */
|
||||
margin: 0; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the overflow in IE.
|
||||
* 1. Show the overflow in Edge.
|
||||
*/
|
||||
|
||||
button,
|
||||
input {
|
||||
/* 1 */
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the inheritance of text transform in Edge, Firefox, and IE.
|
||||
* 1. Remove the inheritance of text transform in Firefox.
|
||||
*/
|
||||
|
||||
button,
|
||||
select {
|
||||
/* 1 */
|
||||
text-transform: none;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
|
||||
* controls in Android 4.
|
||||
* 2. Correct the inability to style clickable types in iOS and Safari.
|
||||
*/
|
||||
|
||||
button,
|
||||
html [type="button"],
|
||||
/* 1 */ [type="reset"],
|
||||
[type="submit"] {
|
||||
-webkit-appearance: button; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the inner border and padding in Firefox.
|
||||
*/
|
||||
|
||||
button::-moz-focus-inner,
|
||||
[type="button"]::-moz-focus-inner,
|
||||
[type="reset"]::-moz-focus-inner,
|
||||
[type="submit"]::-moz-focus-inner {
|
||||
border-style: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the focus styles unset by the previous rule.
|
||||
*/
|
||||
|
||||
button:-moz-focusring,
|
||||
[type="button"]:-moz-focusring,
|
||||
[type="reset"]:-moz-focusring,
|
||||
[type="submit"]:-moz-focusring {
|
||||
outline: 1px dotted ButtonText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Correct the padding in Firefox.
|
||||
*/
|
||||
|
||||
fieldset {
|
||||
padding: 0.35em 0.75em 0.625em;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Correct the text wrapping in Edge and IE.
|
||||
* 2. Correct the color inheritance from `fieldset` elements in IE.
|
||||
* 3. Remove the padding so developers are not caught out when they zero out
|
||||
* `fieldset` elements in all browsers.
|
||||
*/
|
||||
|
||||
legend {
|
||||
box-sizing: border-box; /* 1 */
|
||||
color: inherit; /* 2 */
|
||||
display: table; /* 1 */
|
||||
max-width: 100%; /* 1 */
|
||||
padding: 0; /* 3 */
|
||||
white-space: normal; /* 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Add the correct display in IE 9-.
|
||||
* 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.
|
||||
*/
|
||||
|
||||
progress {
|
||||
display: inline-block; /* 1 */
|
||||
vertical-align: baseline; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the default vertical scrollbar in IE.
|
||||
*/
|
||||
|
||||
textarea {
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Add the correct box sizing in IE 10-.
|
||||
* 2. Remove the padding in IE 10-.
|
||||
*/
|
||||
|
||||
[type="checkbox"],
|
||||
[type="radio"] {
|
||||
box-sizing: border-box; /* 1 */
|
||||
padding: 0; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Correct the cursor style of increment and decrement buttons in Chrome.
|
||||
*/
|
||||
|
||||
[type="number"]::-webkit-inner-spin-button,
|
||||
[type="number"]::-webkit-outer-spin-button {
|
||||
height: auto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Correct the odd appearance in Chrome and Safari.
|
||||
* 2. Correct the outline style in Safari.
|
||||
*/
|
||||
|
||||
[type="search"] {
|
||||
-webkit-appearance: textfield; /* 1 */
|
||||
outline-offset: -2px; /* 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the inner padding and cancel buttons in Chrome and Safari on macOS.
|
||||
*/
|
||||
|
||||
[type="search"]::-webkit-search-cancel-button,
|
||||
[type="search"]::-webkit-search-decoration {
|
||||
-webkit-appearance: none;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Correct the inability to style clickable types in iOS and Safari.
|
||||
* 2. Change font properties to `inherit` in Safari.
|
||||
*/
|
||||
|
||||
::-webkit-file-upload-button {
|
||||
-webkit-appearance: button; /* 1 */
|
||||
font: inherit; /* 2 */
|
||||
}
|
||||
|
||||
/* Interactive
|
||||
========================================================================== */
|
||||
|
||||
/*
|
||||
* Add the correct display in IE 9-.
|
||||
* 1. Add the correct display in Edge, IE, and Firefox.
|
||||
*/
|
||||
|
||||
details,
|
||||
/* 1 */ menu {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add the correct display in all browsers.
|
||||
*/
|
||||
|
||||
summary {
|
||||
display: list-item;
|
||||
}
|
||||
|
||||
/* Scripting
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* Add the correct display in IE 9-.
|
||||
*/
|
||||
|
||||
canvas {
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the correct display in IE.
|
||||
*/
|
||||
|
||||
template {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Hidden
|
||||
========================================================================== */
|
||||
|
||||
/**
|
||||
* Add the correct display in IE 10-.
|
||||
*/
|
||||
|
||||
[hidden] {
|
||||
display: none;
|
||||
}
|
||||
@@ -0,0 +1,610 @@
|
||||
/* =============================================================
|
||||
* UniFi UI Theme — GenieACS
|
||||
* Isolates all UniFi Controller visual overrides so that
|
||||
* upstream GenieACS merges touch this file at most.
|
||||
* ============================================================= */
|
||||
|
||||
/* ---- Sidebar ---- */
|
||||
.unifi-sidebar {
|
||||
background-color: #0d1b2a !important;
|
||||
border-right: 1px solid #111e2e !important;
|
||||
}
|
||||
|
||||
.unifi-sidebar .unifi-brand {
|
||||
padding: 20px 16px 12px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.unifi-sidebar .unifi-brand img {
|
||||
filter: brightness(0) invert(1);
|
||||
height: 32px;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
.unifi-nav-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px 12px;
|
||||
border-radius: 6px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: #8b9bb4;
|
||||
text-decoration: none;
|
||||
transition: background-color 0.15s, color 0.15s;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.unifi-nav-item:hover {
|
||||
background-color: rgba(255, 255, 255, 0.06);
|
||||
color: #c8d6e8;
|
||||
}
|
||||
|
||||
.unifi-nav-item.active {
|
||||
background-color: rgba(0, 111, 255, 0.14);
|
||||
color: #4d9de0;
|
||||
}
|
||||
|
||||
.unifi-nav-item svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
flex-shrink: 0;
|
||||
opacity: 0.75;
|
||||
}
|
||||
|
||||
.unifi-nav-item.active svg {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.unifi-sidebar-footer {
|
||||
padding: 12px;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.07);
|
||||
color: #8b9bb4;
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.unifi-sidebar-footer .unifi-user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 4px;
|
||||
}
|
||||
|
||||
.unifi-sidebar-footer .unifi-logout-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: #4d9de0;
|
||||
cursor: pointer;
|
||||
font-size: 0.8125rem;
|
||||
padding: 0;
|
||||
margin-left: auto;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.unifi-sidebar-footer .unifi-logout-btn:hover {
|
||||
color: #72b8f5;
|
||||
}
|
||||
|
||||
.unifi-version {
|
||||
font-size: 0.6875rem;
|
||||
font-family: var(--font-mono, monospace);
|
||||
color: #3d5166;
|
||||
text-align: right;
|
||||
padding: 6px 12px;
|
||||
}
|
||||
|
||||
/* ---- Page header ---- */
|
||||
.unifi-page-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 24px;
|
||||
padding-bottom: 0;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.unifi-page-title {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
color: #1a2537;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* ---- Stat cards row ---- */
|
||||
.unifi-stat-row {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
|
||||
gap: 16px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.unifi-stat-card {
|
||||
background: #ffffff;
|
||||
border-radius: 8px;
|
||||
padding: 16px 20px;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.07);
|
||||
border: 1px solid #e8eaf0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.unifi-stat-card__label {
|
||||
font-size: 0.75rem;
|
||||
color: #7a8699;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
}
|
||||
|
||||
.unifi-stat-card__value {
|
||||
font-size: 1.75rem;
|
||||
font-weight: 700;
|
||||
color: #1a2537;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.unifi-stat-card__value.online { color: #43b02a; }
|
||||
.unifi-stat-card__value.warning { color: #ffa720; }
|
||||
.unifi-stat-card__value.offline { color: #e53935; }
|
||||
|
||||
/* ---- Toolbar ---- */
|
||||
.unifi-toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.unifi-toolbar-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
|
||||
.unifi-toolbar-right { display: flex; align-items: center; gap: 10px; }
|
||||
|
||||
/* ---- Page size selector ---- */
|
||||
.unifi-page-size-selector {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 0.8125rem;
|
||||
color: #7a8699;
|
||||
}
|
||||
|
||||
.unifi-page-size-selector select {
|
||||
background: #fff;
|
||||
border: 1px solid #d0d6e0;
|
||||
border-radius: 6px;
|
||||
padding: 4px 8px;
|
||||
font-size: 0.8125rem;
|
||||
color: #1a2537;
|
||||
cursor: pointer;
|
||||
appearance: auto;
|
||||
}
|
||||
|
||||
/* ---- Buttons ---- */
|
||||
.unifi-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 7px 14px;
|
||||
border-radius: 6px;
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
border: 1px solid transparent;
|
||||
transition: background-color 0.15s, opacity 0.15s;
|
||||
white-space: nowrap;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.unifi-btn:disabled { opacity: 0.45; cursor: not-allowed; }
|
||||
|
||||
.unifi-btn--primary {
|
||||
background-color: #006fff;
|
||||
color: #fff;
|
||||
border-color: #006fff;
|
||||
}
|
||||
.unifi-btn--primary:hover:not(:disabled) { background-color: #005ee0; border-color: #005ee0; }
|
||||
|
||||
.unifi-btn--secondary {
|
||||
background-color: #fff;
|
||||
color: #1a2537;
|
||||
border-color: #d0d6e0;
|
||||
}
|
||||
.unifi-btn--secondary:hover:not(:disabled) { background-color: #f4f5f7; }
|
||||
|
||||
.unifi-btn--danger {
|
||||
background-color: #fff;
|
||||
color: #e53935;
|
||||
border-color: #e53935;
|
||||
}
|
||||
.unifi-btn--danger:hover:not(:disabled) { background-color: #fff0f0; }
|
||||
|
||||
.unifi-btn--icon {
|
||||
padding: 6px;
|
||||
background: none;
|
||||
border: 1px solid #d0d6e0;
|
||||
border-radius: 6px;
|
||||
color: #7a8699;
|
||||
cursor: pointer;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
}
|
||||
.unifi-btn--icon:hover { background: #f4f5f7; color: #1a2537; }
|
||||
|
||||
/* ---- Device card grid ---- */
|
||||
.unifi-device-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.unifi-device-card {
|
||||
background: #ffffff;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #e8eaf0;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
transition: box-shadow 0.15s, border-color 0.15s, transform 0.1s;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.unifi-device-card:hover {
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
|
||||
border-color: #b8c0d0;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.unifi-device-card__header {
|
||||
padding: 14px 16px 10px;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 10px;
|
||||
border-bottom: 1px solid #f0f2f6;
|
||||
}
|
||||
|
||||
.unifi-device-card__name {
|
||||
font-size: 0.9375rem;
|
||||
font-weight: 600;
|
||||
color: #1a2537;
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.unifi-device-card__model {
|
||||
font-size: 0.75rem;
|
||||
color: #7a8699;
|
||||
margin: 2px 0 0;
|
||||
}
|
||||
|
||||
.unifi-device-card__body {
|
||||
padding: 12px 16px;
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 7px;
|
||||
}
|
||||
|
||||
.unifi-device-card__row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.unifi-device-card__row-label {
|
||||
color: #7a8699;
|
||||
min-width: 52px;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.unifi-device-card__row-value {
|
||||
color: #1a2537;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
font-family: var(--font-mono, monospace);
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.unifi-device-card__row-value.empty {
|
||||
color: #b0b8c8;
|
||||
font-family: var(--font-sans, sans-serif);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.unifi-device-card__footer {
|
||||
padding: 8px 16px;
|
||||
background: #f8f9fc;
|
||||
border-top: 1px solid #f0f2f6;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
font-size: 0.75rem;
|
||||
color: #9aa5b8;
|
||||
}
|
||||
|
||||
.unifi-device-card__tags {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
/* ---- Status dot / badge ---- */
|
||||
.unifi-status-dot {
|
||||
display: inline-block;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 50%;
|
||||
flex-shrink: 0;
|
||||
margin-top: 3px;
|
||||
}
|
||||
|
||||
.unifi-status-dot--online { background-color: #43b02a; box-shadow: 0 0 0 3px rgba(67,176,42,0.18); }
|
||||
.unifi-status-dot--warning { background-color: #ffa720; box-shadow: 0 0 0 3px rgba(255,167,32,0.18); }
|
||||
.unifi-status-dot--offline { background-color: #e53935; box-shadow: 0 0 0 3px rgba(229,57,53,0.18); }
|
||||
.unifi-status-dot--unknown { background-color: #b0b8c8; }
|
||||
|
||||
.unifi-status-badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
padding: 2px 7px;
|
||||
border-radius: 999px;
|
||||
font-size: 0.6875rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.03em;
|
||||
}
|
||||
|
||||
.unifi-status-badge--online { background: rgba(67,176,42,0.12); color: #2e8020; }
|
||||
.unifi-status-badge--warning { background: rgba(255,167,32,0.12); color: #b87000; }
|
||||
.unifi-status-badge--offline { background: rgba(229,57,53,0.12); color: #c62828; }
|
||||
.unifi-status-badge--unknown { background: rgba(176,184,200,0.18); color: #7a8699; }
|
||||
|
||||
/* ---- Tag badge ---- */
|
||||
.unifi-tag {
|
||||
display: inline-block;
|
||||
padding: 1px 7px;
|
||||
border-radius: 999px;
|
||||
font-size: 0.6875rem;
|
||||
background: rgba(0, 111, 255, 0.1);
|
||||
color: #006fff;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* ---- Pagination ---- */
|
||||
.unifi-pagination {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
margin-top: 24px;
|
||||
font-size: 0.8125rem;
|
||||
color: #7a8699;
|
||||
}
|
||||
|
||||
.unifi-pagination__info {
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
/* ---- Device detail page ---- */
|
||||
.unifi-device-detail-header {
|
||||
background: #fff;
|
||||
border: 1px solid #e8eaf0;
|
||||
border-radius: 8px;
|
||||
padding: 20px 24px;
|
||||
margin-bottom: 20px;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__top {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 14px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__icon {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 8px;
|
||||
background: #f0f6ff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #006fff;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__info {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__name {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
color: #1a2537;
|
||||
margin: 0 0 4px;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__meta {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
font-size: 0.8125rem;
|
||||
color: #7a8699;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__kv {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
|
||||
gap: 12px 24px;
|
||||
margin-top: 16px;
|
||||
padding-top: 16px;
|
||||
border-top: 1px solid #f0f2f6;
|
||||
}
|
||||
|
||||
.unifi-detail-kv__label {
|
||||
font-size: 0.75rem;
|
||||
color: #7a8699;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.unifi-detail-kv__value {
|
||||
font-size: 0.875rem;
|
||||
color: #1a2537;
|
||||
font-family: var(--font-mono, monospace);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.unifi-detail-kv__value.empty {
|
||||
color: #b0b8c8;
|
||||
font-family: var(--font-sans, sans-serif);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.unifi-device-content {
|
||||
background: #ffffff;
|
||||
border: 1px solid #e8eaf0;
|
||||
border-radius: 8px;
|
||||
padding: 20px 24px;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
/* ---- Back button ---- */
|
||||
.unifi-back-link {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
color: #4d9de0;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
text-decoration: none;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.unifi-back-link:hover { color: #2880cc; }
|
||||
|
||||
/* ---- Login page override ---- */
|
||||
.unifi-login-wrapper {
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(135deg, #0d1b2a 0%, #1a2f45 100%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.unifi-login-card {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 40px 36px;
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
/* ---- Table overrides (for inner device page content) ---- */
|
||||
.device-page table {
|
||||
border-collapse: separate;
|
||||
border-spacing: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.device-page table th {
|
||||
background: #f8f9fc;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
color: #7a8699;
|
||||
padding: 10px 14px;
|
||||
border-bottom: 1px solid #e8eaf0;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.device-page table td {
|
||||
padding: 9px 14px;
|
||||
border-bottom: 1px solid #f0f2f6;
|
||||
font-size: 0.875rem;
|
||||
color: #1a2537;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.device-page table tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.device-page table tr:hover td {
|
||||
background-color: #f8faff;
|
||||
}
|
||||
|
||||
/* ---- Main content background ---- */
|
||||
.unifi-main-bg {
|
||||
background: #f4f5f7;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
/* ---- Users / permissions pages ---- */
|
||||
.unifi-admin-section {
|
||||
background: #fff;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #e8eaf0;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.unifi-admin-section__header {
|
||||
padding: 14px 20px;
|
||||
border-bottom: 1px solid #e8eaf0;
|
||||
background: #f8f9fc;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.unifi-admin-section__title {
|
||||
font-size: 0.9375rem;
|
||||
font-weight: 600;
|
||||
color: #1a2537;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* ---- Responsive adjustments ---- */
|
||||
@media (max-width: 640px) {
|
||||
.unifi-device-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.unifi-stat-row {
|
||||
grid-template-columns: 1fr 1fr;
|
||||
}
|
||||
|
||||
.unifi-device-detail-header__kv {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
+27
-12
@@ -1,7 +1,9 @@
|
||||
import { ClosureComponent, Component } from "mithril";
|
||||
import { ClosureComponent } from "mithril";
|
||||
import { m } from "./components.ts";
|
||||
import config from "./config.ts";
|
||||
import { device as deviceConfig } from "./config.ts";
|
||||
import * as store from "./store.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
import Path from "../lib/common/path.ts";
|
||||
|
||||
export function init(
|
||||
args: Record<string, unknown>,
|
||||
@@ -14,19 +16,32 @@ export function init(
|
||||
|
||||
return Promise.resolve({
|
||||
deviceId: args.id,
|
||||
deviceFilter: ["=", ["PARAM", "DeviceID.ID"], args.id],
|
||||
deviceFilter: new Expression.Binary(
|
||||
"=",
|
||||
new Expression.Parameter(Path.parse("DeviceID.ID")),
|
||||
new Expression.Literal(args.id as string),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export const component: ClosureComponent = (): Component => {
|
||||
interface Attrs {
|
||||
deviceId: string;
|
||||
deviceFilter: Expression;
|
||||
}
|
||||
|
||||
export const component: ClosureComponent<Attrs> = () => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
document.title = `${vnode.attrs["deviceId"]} - Devices - GenieACS`;
|
||||
document.title = `${vnode.attrs.deviceId} - Devices - GenieACS`;
|
||||
|
||||
const dev = store.fetch("devices", vnode.attrs["deviceFilter"]);
|
||||
const dev = store.fetch("devices", vnode.attrs.deviceFilter);
|
||||
if (!dev.value.length) {
|
||||
if (!dev.fulfilling)
|
||||
return m("p.error", `No such device ${vnode.attrs["deviceId"]}`);
|
||||
if (!dev.fulfilling) {
|
||||
return m(
|
||||
"p.text-sm font-bold text-red-500",
|
||||
`No such device ${vnode.attrs["deviceId"]}`,
|
||||
);
|
||||
}
|
||||
return m(
|
||||
"loading",
|
||||
{ queries: [dev] },
|
||||
@@ -34,20 +49,20 @@ export const component: ClosureComponent = (): Component => {
|
||||
);
|
||||
}
|
||||
|
||||
const conf = config.ui.device;
|
||||
const conf = deviceConfig;
|
||||
const cmps = [];
|
||||
|
||||
for (const c of Object.values(conf)) {
|
||||
cmps.push(
|
||||
m.context(
|
||||
{ device: dev.value[0], deviceQuery: dev },
|
||||
store.evaluateExpression(c["type"], null) as string,
|
||||
c,
|
||||
store.evaluateExpression(c["type"], {}).value as string,
|
||||
c as any,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return [m("h1", vnode.attrs["deviceId"]), cmps];
|
||||
return m("div.device-page", m("h1", vnode.attrs["deviceId"]), cmps);
|
||||
},
|
||||
};
|
||||
};
|
||||
+72
-60
@@ -1,63 +1,72 @@
|
||||
import { ClosureComponent, Component, Children } from "mithril";
|
||||
import { ClosureComponent, Children } from "mithril";
|
||||
import { m } from "./components.ts";
|
||||
import config from "./config.ts";
|
||||
import { pageSize as PAGE_SIZE, index as indexConfig } from "./config.ts";
|
||||
import indexTableComponent from "./index-table-component.ts";
|
||||
import filterComponent from "./filter-component.ts";
|
||||
import * as store from "./store.ts";
|
||||
import { queueTask, stageDownload } from "./task-queue.ts";
|
||||
import * as notifications from "./notifications.ts";
|
||||
import { parse, stringify, map } from "../lib/common/expression/parser.ts";
|
||||
import { evaluate, extractParams } from "../lib/common/expression/util.ts";
|
||||
import Expression, { extractPaths } from "../lib/common/expression.ts";
|
||||
import memoize from "../lib/common/memoize.ts";
|
||||
import * as smartQuery from "./smart-query.ts";
|
||||
|
||||
const PAGE_SIZE = config.ui.pageSize || 10;
|
||||
|
||||
const memoizedParse = memoize(parse);
|
||||
const memoizedJsonParse = memoize(JSON.parse);
|
||||
const memoizedGetSortable = memoize((p) => {
|
||||
const expressionParams = extractParams(p);
|
||||
if (expressionParams.length === 1) {
|
||||
const param = evaluate(expressionParams[0]);
|
||||
if (typeof param === "string") return param;
|
||||
}
|
||||
const memoizedGetSortable = memoize((p: Expression) => {
|
||||
const expressionParams = extractPaths(p);
|
||||
if (expressionParams.length === 1) return expressionParams[0];
|
||||
return null;
|
||||
});
|
||||
|
||||
const getDownloadUrl = memoize((filter, indexParameters) => {
|
||||
const getDownloadUrl = memoize(
|
||||
(
|
||||
filter: Expression,
|
||||
indexParameters: { label: string; parameter: Expression }[],
|
||||
) => {
|
||||
const columns = {};
|
||||
for (const p of indexParameters)
|
||||
columns[store.evaluateExpression(p.label, null) as string] = stringify(
|
||||
p.parameter,
|
||||
);
|
||||
for (const p of indexParameters) columns[p.label] = p.parameter.toString();
|
||||
return `api/devices.csv?${m.buildQueryString({
|
||||
filter: stringify(filter),
|
||||
filter: filter.toString(),
|
||||
columns: JSON.stringify(columns),
|
||||
})}`;
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
const unpackSmartQuery = memoize((query) => {
|
||||
return map(query, (e) => {
|
||||
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "Q")
|
||||
return smartQuery.unpack("devices", e[2], e[3]);
|
||||
const unpackSmartQuery = memoize((query: Expression) => {
|
||||
return query.evaluate((e) => {
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "Q") {
|
||||
if (
|
||||
e.args[0] instanceof Expression.Literal &&
|
||||
e.args[1] instanceof Expression.Literal
|
||||
) {
|
||||
return smartQuery.unpack(
|
||||
"devices",
|
||||
e.args[0].value as string,
|
||||
e.args[1].value as string,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return e;
|
||||
});
|
||||
});
|
||||
|
||||
export function init(
|
||||
args: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown>> {
|
||||
export function init(args: Record<string, unknown>): Promise<Attrs> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!window.authorizer.hasAccess("devices", 2))
|
||||
return void reject(new Error("You are not authorized to view this page"));
|
||||
|
||||
const filter = args.hasOwnProperty("filter") ? "" + args["filter"] : "";
|
||||
const sort = args.hasOwnProperty("sort") ? "" + args["sort"] : "";
|
||||
const indexParameters = Object.values(config.ui.index);
|
||||
let filter: Expression = null;
|
||||
let sort: Record<string, number> = null;
|
||||
if (args.hasOwnProperty("filter"))
|
||||
filter = Expression.parse(args["filter"] as string);
|
||||
if (args.hasOwnProperty("sort")) sort = JSON.parse(args["sort"] as string);
|
||||
const indexParameters = indexConfig;
|
||||
if (!indexParameters.length) {
|
||||
indexParameters.push({
|
||||
label: "ID",
|
||||
parameter: ["PARAM", "DeviceID.ID"],
|
||||
parameter: Expression.parse("DeviceID.ID"),
|
||||
unsortable: false,
|
||||
raw: {},
|
||||
});
|
||||
}
|
||||
resolve({ filter, indexParameters, sort });
|
||||
@@ -69,7 +78,7 @@ function renderActions(selected: Set<string>): Children {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.primary",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Reboot selected devices",
|
||||
disabled: !selected.size,
|
||||
@@ -87,7 +96,7 @@ function renderActions(selected: Set<string>): Children {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.critical",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Factory reset selected devices",
|
||||
disabled: !selected.size,
|
||||
@@ -105,7 +114,7 @@ function renderActions(selected: Set<string>): Children {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.critical",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Push a firmware or a config file",
|
||||
disabled: !selected.size,
|
||||
@@ -122,7 +131,7 @@ function renderActions(selected: Set<string>): Children {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.primary",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Delete selected devices",
|
||||
disabled: !selected.size,
|
||||
@@ -153,7 +162,7 @@ function renderActions(selected: Set<string>): Children {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.primary",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Tag selected devices",
|
||||
disabled: !selected.size,
|
||||
@@ -185,7 +194,7 @@ function renderActions(selected: Set<string>): Children {
|
||||
|
||||
buttons.push(
|
||||
m(
|
||||
"button.primary",
|
||||
"button.px-4 py-2 border border-stone-300 shadow-xs text-sm font-medium rounded-md text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
{
|
||||
title: "Untag selected devices",
|
||||
disabled: !selected.size,
|
||||
@@ -220,11 +229,17 @@ function renderActions(selected: Set<string>): Children {
|
||||
return buttons;
|
||||
}
|
||||
|
||||
export const component: ClosureComponent = (): Component => {
|
||||
interface Attrs {
|
||||
indexParameters: typeof indexConfig;
|
||||
filter?: Expression;
|
||||
sort?: Record<string, number>;
|
||||
}
|
||||
|
||||
export const component: ClosureComponent<Attrs> = () => {
|
||||
return {
|
||||
view: (vnode) => {
|
||||
document.title = "Devices - GenieACS";
|
||||
const attributes = vnode.attrs["indexParameters"];
|
||||
const attributes = vnode.attrs.indexParameters;
|
||||
|
||||
function showMore(): void {
|
||||
vnode.state["showCount"] =
|
||||
@@ -232,22 +247,22 @@ export const component: ClosureComponent = (): Component => {
|
||||
m.redraw();
|
||||
}
|
||||
|
||||
function onFilterChanged(filter): void {
|
||||
const ops = { filter };
|
||||
if (vnode.attrs["sort"]) ops["sort"] = vnode.attrs["sort"];
|
||||
function onFilterChanged(filter: Expression): void {
|
||||
const ops = {};
|
||||
if (!(filter instanceof Expression.Literal && filter.value))
|
||||
ops["filter"] = filter.toString();
|
||||
if (vnode.attrs.sort) ops["sort"] = vnode.attrs.sort;
|
||||
m.route.set("/devices", ops);
|
||||
}
|
||||
|
||||
const sort = vnode.attrs["sort"]
|
||||
? memoizedJsonParse(vnode.attrs["sort"])
|
||||
: {};
|
||||
const sort = vnode.attrs.sort || {};
|
||||
|
||||
const sortAttributes = {};
|
||||
for (let i = 0; i < attributes.length; i++) {
|
||||
const attr = attributes[i];
|
||||
if (attr.unsortable) continue;
|
||||
const param = memoizedGetSortable(attr.parameter);
|
||||
if (param) sortAttributes[i] = sort[param] || 0;
|
||||
if (param) sortAttributes[i] = sort[param.toString()] || 0;
|
||||
}
|
||||
|
||||
function onSortChange(sortedAttrs): void {
|
||||
@@ -256,17 +271,16 @@ export const component: ClosureComponent = (): Component => {
|
||||
const param = memoizedGetSortable(
|
||||
attributes[Math.abs(index) - 1].parameter,
|
||||
);
|
||||
_sort[param] = Math.sign(index);
|
||||
_sort[param.toString()] = Math.sign(index);
|
||||
}
|
||||
const ops = { sort: JSON.stringify(_sort) };
|
||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||
m.route.set("/devices", ops);
|
||||
}
|
||||
|
||||
let filter = vnode.attrs["filter"]
|
||||
? memoizedParse(vnode.attrs["filter"])
|
||||
: true;
|
||||
filter = unpackSmartQuery(filter);
|
||||
const filter = unpackSmartQuery(
|
||||
vnode.attrs["filter"] ?? new Expression.Literal(true),
|
||||
);
|
||||
|
||||
const devs = store.fetch("devices", filter, {
|
||||
limit: vnode.state["showCount"] || PAGE_SIZE,
|
||||
@@ -280,15 +294,15 @@ export const component: ClosureComponent = (): Component => {
|
||||
return m.context(
|
||||
{ device: device, parameter: attr.parameter },
|
||||
attr.type || "parameter",
|
||||
attr,
|
||||
attr.raw,
|
||||
);
|
||||
};
|
||||
|
||||
const attrs = {};
|
||||
attrs["attributes"] = attributes.map((a) => ({
|
||||
...a,
|
||||
label: store.evaluateExpression(a.label, null),
|
||||
type: store.evaluateExpression(a.type, null),
|
||||
label: a.label,
|
||||
type: a.type,
|
||||
}));
|
||||
attrs["data"] = devs.value;
|
||||
attrs["total"] = count.value;
|
||||
@@ -299,11 +313,9 @@ export const component: ClosureComponent = (): Component => {
|
||||
attrs["valueCallback"] = valueCallback;
|
||||
attrs["recordActionsCallback"] = (device): Children => {
|
||||
return m(
|
||||
"a",
|
||||
"a.text-cyan-700 hover:text-cyan-900",
|
||||
{
|
||||
href: `#!/devices/${encodeURIComponent(
|
||||
device["DeviceID.ID"].value[0],
|
||||
)}`,
|
||||
href: `#!/devices/${encodeURIComponent(device["DeviceID.ID"])}`,
|
||||
},
|
||||
"Show",
|
||||
);
|
||||
@@ -319,7 +331,7 @@ export const component: ClosureComponent = (): Component => {
|
||||
};
|
||||
|
||||
return [
|
||||
m("h1", "Listing devices"),
|
||||
m("h1.text-xl font-medium text-stone-900 mb-5", "Listing devices"),
|
||||
m(filterComponent, filterAttrs),
|
||||
m("loading", { queries: [devs, count] }, m(indexTableComponent, attrs)),
|
||||
];
|
||||
|
||||
+224
-102
@@ -7,7 +7,7 @@ import m, {
|
||||
} from "mithril";
|
||||
import * as store from "./store.ts";
|
||||
import * as notifications from "./notifications.ts";
|
||||
import { getIcon } from "./icons.ts";
|
||||
import { icon } from "./tailwind-utility-components.ts";
|
||||
import {
|
||||
clear,
|
||||
commit,
|
||||
@@ -18,17 +18,10 @@ import {
|
||||
queueTask,
|
||||
StageTask,
|
||||
} from "./task-queue.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
|
||||
const invalid: WeakSet<StageTask> = new WeakSet();
|
||||
|
||||
function mparam(param): Children {
|
||||
return m("span.parameter", { title: param }, `${param}`);
|
||||
}
|
||||
|
||||
function mval(val): Children {
|
||||
return m("span.value", { title: val }, `${val}`);
|
||||
}
|
||||
|
||||
function renderStagingSpv(task: StageTask, queueFunc, cancelFunc): Children {
|
||||
function keydown(e: KeyboardEvent): void {
|
||||
if (e.key === "Enter") queueFunc();
|
||||
@@ -39,7 +32,7 @@ function renderStagingSpv(task: StageTask, queueFunc, cancelFunc): Children {
|
||||
let input;
|
||||
if (task.parameterValues[0][2] === "xsd:boolean") {
|
||||
input = m(
|
||||
"select",
|
||||
"select.mt-1 w-full block pl-3 pr-10 py-2 text-base border-stone-300 focus:outline-hidden focus:ring-cyan-500 focus:border-cyan-500 sm:text-sm rounded-md",
|
||||
{
|
||||
value: task.parameterValues[0][1].toString(),
|
||||
onchange: (e) => {
|
||||
@@ -61,7 +54,9 @@ function renderStagingSpv(task: StageTask, queueFunc, cancelFunc): Children {
|
||||
let value = task.parameterValues[0][1];
|
||||
if (type === "xsd:dateTime" && typeof value === "number")
|
||||
value = new Date(value).toJSON() || value;
|
||||
input = m("input", {
|
||||
input = m(
|
||||
"input.mt-1 w-full shadow-xs focus:ring-cyan-500 focus:border-cyan-500 block sm:text-sm border-stone-300 rounded-md",
|
||||
{
|
||||
type: ["xsd:int", "xsd:unsignedInt"].includes(type) ? "number" : "text",
|
||||
value: value,
|
||||
oninput: (e) => {
|
||||
@@ -76,16 +71,32 @@ function renderStagingSpv(task: StageTask, queueFunc, cancelFunc): Children {
|
||||
// we're animating height and using overflow: hidden
|
||||
(vnode.dom.parentNode.parentNode as Element).scrollTop = 0;
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return [m("span", "Editing ", mparam(task.parameterValues[0][0])), input];
|
||||
return [
|
||||
m(
|
||||
"span.text-sm text-stone-700 inline-flex max-w-full gap-2",
|
||||
"Editing",
|
||||
m(
|
||||
"span",
|
||||
{
|
||||
title: task.parameterValues[0][0],
|
||||
dir: "rtl",
|
||||
class: "italic pr-1 min-w-0 truncate",
|
||||
},
|
||||
task.parameterValues[0][0],
|
||||
),
|
||||
),
|
||||
input,
|
||||
];
|
||||
}
|
||||
|
||||
function renderStagingDownload(task: StageTask): Children {
|
||||
if (!task.fileName || !task.fileType) invalid.add(task);
|
||||
else invalid.delete(task);
|
||||
const files = store.fetch("files", true);
|
||||
const files = store.fetch("files", new Expression.Literal(true));
|
||||
let oui = "";
|
||||
let productClass = "";
|
||||
for (const d of task.devices) {
|
||||
@@ -138,11 +149,13 @@ function renderStagingDownload(task: StageTask): Children {
|
||||
),
|
||||
);
|
||||
|
||||
return [
|
||||
"Push ",
|
||||
return m("div.flex items-center gap-2 text-sm text-stone-700 max-w-full", [
|
||||
"Push",
|
||||
m(
|
||||
"select",
|
||||
{
|
||||
class:
|
||||
"min-w-0 pl-3 pr-10 py-2 text-base border-stone-300 focus:outline-hidden focus:ring-cyan-500 focus:border-cyan-500 sm:text-sm rounded-md",
|
||||
onchange: (e) => {
|
||||
const f = e.target.value;
|
||||
task.fileName = f;
|
||||
@@ -151,21 +164,22 @@ function renderStagingDownload(task: StageTask): Children {
|
||||
if (file._id === f) task.fileType = file["metadata.fileType"];
|
||||
},
|
||||
disabled: files.fulfilling,
|
||||
style: "width: 350px",
|
||||
},
|
||||
filesList,
|
||||
),
|
||||
" as ",
|
||||
"as",
|
||||
m(
|
||||
"select",
|
||||
{
|
||||
class:
|
||||
"min-w-0 pl-3 pr-10 py-2 text-base border-stone-300 focus:outline-hidden focus:ring-cyan-500 focus:border-cyan-500 sm:text-sm rounded-md",
|
||||
onchange: (e) => {
|
||||
task.fileType = e.target.value;
|
||||
},
|
||||
},
|
||||
typesList,
|
||||
),
|
||||
];
|
||||
]);
|
||||
}
|
||||
|
||||
function renderStaging(staging: Set<StageTask>): Child[] {
|
||||
@@ -190,17 +204,34 @@ function renderStaging(staging: Set<StageTask>): Child[] {
|
||||
else if (s.name === "download") elms = renderStagingDownload(s);
|
||||
|
||||
const queue = m(
|
||||
"button.primary",
|
||||
{ title: "Queue task", onclick: queueFunc, disabled: invalid.has(s) },
|
||||
"button",
|
||||
{
|
||||
class:
|
||||
"px-2.5 py-1.5 border border-transparent text-xs font-medium rounded-sm shadow-xs text-white bg-cyan-600 hover:bg-cyan-700 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
title: "Queue task",
|
||||
onclick: queueFunc,
|
||||
disabled: invalid.has(s),
|
||||
},
|
||||
"Queue",
|
||||
);
|
||||
const cancel = m(
|
||||
"button",
|
||||
{ title: "Cancel edit", onclick: cancelFunc },
|
||||
{
|
||||
class:
|
||||
"px-2.5 py-1.5 border border-stone-300 shadow-xs text-xs font-medium rounded-sm text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
title: "Cancel edit",
|
||||
onclick: cancelFunc,
|
||||
},
|
||||
"Cancel",
|
||||
);
|
||||
|
||||
elements.push(m(".staging", elms, m("div.actions", queue, cancel)));
|
||||
elements.push(
|
||||
m(
|
||||
"div.p-4",
|
||||
elms,
|
||||
m("div.flex mt-4 justify-center gap-4", cancel, queue),
|
||||
),
|
||||
);
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
@@ -214,9 +245,10 @@ function renderQueue(queue: Set<QueueTask>): Child[] {
|
||||
}
|
||||
|
||||
for (const [k, v] of Object.entries(devices)) {
|
||||
details.push(m("strong", k));
|
||||
details.push(m("h3.font-semibold text-stone-700", k));
|
||||
for (const t of v) {
|
||||
const actions = [];
|
||||
const actions: ReturnType<typeof m>[] = [];
|
||||
let task: ReturnType<typeof m>;
|
||||
|
||||
if (t.status === "fault" || t.status === "stale") {
|
||||
actions.push(
|
||||
@@ -228,7 +260,10 @@ function renderQueue(queue: Set<QueueTask>): Child[] {
|
||||
queueTask(t);
|
||||
},
|
||||
},
|
||||
getIcon("retry"),
|
||||
m(icon, {
|
||||
name: "retry",
|
||||
class: "inline h-4 w-4 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -242,74 +277,116 @@ function renderQueue(queue: Set<QueueTask>): Child[] {
|
||||
deleteTask(t);
|
||||
},
|
||||
},
|
||||
getIcon("remove"),
|
||||
m(icon, {
|
||||
name: "remove",
|
||||
class: "inline h-4 w-4 text-cyan-700 hover:text-cyan-900",
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (t.name === "setParameterValues") {
|
||||
details.push(
|
||||
m(
|
||||
`div.${t.status}`,
|
||||
task = m(
|
||||
"span.text-stone-900 inline-flex max-w-full gap-2",
|
||||
"Set",
|
||||
m(
|
||||
"span",
|
||||
"Set ",
|
||||
mparam(t.parameterValues[0][0]),
|
||||
" to '",
|
||||
mval(t.parameterValues[0][1]),
|
||||
"'",
|
||||
{
|
||||
title: t.parameterValues[0][0],
|
||||
dir: "rtl",
|
||||
class: "italic pr-1 min-w-0 truncate",
|
||||
},
|
||||
t.parameterValues[0][0],
|
||||
),
|
||||
m(".actions", actions),
|
||||
"to",
|
||||
m(
|
||||
"span",
|
||||
{
|
||||
title: t.parameterValues[0][1],
|
||||
class: "min-w-0 truncate",
|
||||
},
|
||||
t.parameterValues[0][1],
|
||||
),
|
||||
);
|
||||
} else if (t.name === "refreshObject") {
|
||||
details.push(
|
||||
task = m(
|
||||
"span.text-stone-900 inline-flex max-w-full gap-2",
|
||||
"Refresh",
|
||||
m(
|
||||
`div.${t.status}`,
|
||||
m("span", "Refresh ", mparam(t.parameterName)),
|
||||
m(".actions", actions),
|
||||
"span",
|
||||
{
|
||||
title: t.parameterName,
|
||||
dir: "rtl",
|
||||
class: "italic pr-1 min-w-0 truncate",
|
||||
},
|
||||
t.parameterName,
|
||||
),
|
||||
);
|
||||
} else if (t.name === "reboot") {
|
||||
details.push(m(`div.${t.status}`, "Reboot", m(".actions", actions)));
|
||||
task = m("span.text-stone-900", "Reboot");
|
||||
} else if (t.name === "factoryReset") {
|
||||
details.push(
|
||||
m(`div.${t.status}`, "Factory reset", m(".actions", actions)),
|
||||
);
|
||||
task = m("span.text-stone-900", "Factory reset");
|
||||
} else if (t.name === "addObject") {
|
||||
details.push(
|
||||
task = m(
|
||||
"span.text-stone-900 inline-flex max-w-full gap-2",
|
||||
"Add",
|
||||
m(
|
||||
`div.${t.status}`,
|
||||
m("span", "Add ", mparam(t.objectName)),
|
||||
m(".actions", actions),
|
||||
"span",
|
||||
{
|
||||
title: t.objectName,
|
||||
dir: "rtl",
|
||||
class: "italic pr-1 min-w-0 truncate",
|
||||
},
|
||||
t.objectName,
|
||||
),
|
||||
);
|
||||
} else if (t.name === "deleteObject") {
|
||||
details.push(
|
||||
task = m(
|
||||
"span.text-stone-900 inline-flex max-w-full gap-2",
|
||||
"Delete",
|
||||
m(
|
||||
`div.${t.status}`,
|
||||
m("span", "Delete ", mparam(t.objectName)),
|
||||
m(".actions", actions),
|
||||
"span",
|
||||
{
|
||||
title: t.objectName,
|
||||
dir: "rtl",
|
||||
class: "italic pr-1 min-w-0 truncate",
|
||||
},
|
||||
t.objectName,
|
||||
),
|
||||
);
|
||||
} else if (t.name === "getParameterValues") {
|
||||
details.push(
|
||||
m(
|
||||
`div.${t.status}`,
|
||||
task = m(
|
||||
"span.text-stone-900",
|
||||
`Refresh ${t.parameterNames.length} parameters`,
|
||||
m(".actions", actions),
|
||||
),
|
||||
);
|
||||
} else if (t.name === "download") {
|
||||
details.push(
|
||||
m(
|
||||
`div.${t.status}`,
|
||||
task = m(
|
||||
"span.text-stone-900",
|
||||
`Push file: ${t.fileName} (${t.fileType})`,
|
||||
m(".actions", actions),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
details.push(m(`div.${t.status}`, t.name, m(".actions", actions)));
|
||||
task = m("span.text-stone-900", t.name);
|
||||
}
|
||||
|
||||
let bgDiv: ReturnType<typeof m>;
|
||||
if (t.status === "pending") {
|
||||
bgDiv = m(
|
||||
"div.block absolute inset-0 bg-emerald-200 rounded-sm animate-pulse",
|
||||
"",
|
||||
);
|
||||
} else if (t.status === "fault") {
|
||||
bgDiv = m("div.block absolute inset-0 bg-red-200 rounded-sm", "");
|
||||
} else if (t.status === "stale") {
|
||||
bgDiv = m("div.block absolute inset-0 bg-stone-200 rounded-sm", "");
|
||||
}
|
||||
|
||||
details.push(
|
||||
m(
|
||||
"div.flex justify-between w-full rounded-sm items-center relative",
|
||||
bgDiv,
|
||||
m("div.overflow-hidden relative", task),
|
||||
m("div.flex whitespace-nowrap gap-2 ml-2 relative", actions),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,20 +397,47 @@ function renderNotifications(notifs): Child[] {
|
||||
const notificationElements: Child[] = [];
|
||||
|
||||
for (const n of notifs) {
|
||||
let notifColors = "",
|
||||
buttonColors = "";
|
||||
if (n.type === "success") {
|
||||
notifColors = "bg-emerald-50 text-emerald-800 border-emerald-100";
|
||||
buttonColors =
|
||||
"hover:bg-emerald-100 text-emerald-800 focus:ring-offset-emerald-50 focus:ring-emerald-600";
|
||||
} else if (n.type === "error") {
|
||||
notifColors = "bg-red-50 text-red-800 border-red-100";
|
||||
buttonColors =
|
||||
"hover:bg-red-100 text-red-800 focus:ring-offset-red-50 focus:ring-red-600";
|
||||
} else if (n.type === "warning") {
|
||||
notifColors = "bg-yellow-50 text-yellow-800 border-yellow-100";
|
||||
buttonColors =
|
||||
"hover:bg-yellow-100 text-yellow-800 focus:ring-offset-yellow-50 focus:ring-yellow-600";
|
||||
}
|
||||
|
||||
let buttons;
|
||||
if (n.actions) {
|
||||
const btns = Object.entries(n.actions).map(([label, onclick]) =>
|
||||
m("button.primary", { onclick: onclick }, label),
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
class:
|
||||
"ml-2 px-2 py-1.5 -my-1.5 rounded-md text-sm font-medium focus:outline-hidden focus:ring-2 focus:ring-offset-2 " +
|
||||
buttonColors,
|
||||
onclick: onclick,
|
||||
},
|
||||
label,
|
||||
),
|
||||
);
|
||||
if (btns.length) buttons = m("div", { style: "float: right" }, btns);
|
||||
if (btns.length) buttons = m("div", btns);
|
||||
}
|
||||
|
||||
notificationElements.push(
|
||||
m(
|
||||
"div.notification",
|
||||
"div",
|
||||
{
|
||||
class: n.type,
|
||||
style: "position: absolute;opacity: 0",
|
||||
class:
|
||||
"absolute flex justify-between rounded-md w-full text-sm shadow-md p-4 border transition-[top,opacity] " +
|
||||
notifColors,
|
||||
style: "opacity: 0",
|
||||
oncreate: (vnode) => {
|
||||
(vnode.dom as HTMLDivElement).style.opacity = "1";
|
||||
},
|
||||
@@ -347,7 +451,8 @@ function renderNotifications(notifs): Child[] {
|
||||
},
|
||||
key: n.timestamp,
|
||||
},
|
||||
m("div", buttons, n.message),
|
||||
n.message,
|
||||
buttons,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -367,10 +472,10 @@ const component: ClosureComponent = (): Component => {
|
||||
const queueElements = renderQueue(queue);
|
||||
|
||||
function repositionNotifications(): void {
|
||||
let top = 10;
|
||||
let top = 16;
|
||||
for (const c of notificationElements as VnodeDOM[]) {
|
||||
(c.dom as HTMLDivElement).style.top = `${top}px`;
|
||||
top += (c.dom as HTMLDivElement).offsetHeight + 10;
|
||||
top += (c.dom as HTMLDivElement).offsetHeight + 16;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,10 +502,23 @@ const component: ClosureComponent = (): Component => {
|
||||
for (const t of queue) statusCount[t["status"]] += 1;
|
||||
|
||||
const actions = m(
|
||||
".actions",
|
||||
"div.flex ml-auto gap-2",
|
||||
m(
|
||||
"button.primary",
|
||||
"button",
|
||||
{
|
||||
class:
|
||||
"px-2.5 py-1.5 -my-1.5 border border-stone-300 shadow-xs text-xs font-medium rounded-sm text-stone-700 bg-white hover:bg-stone-50 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
title: "Clear tasks",
|
||||
onclick: clear,
|
||||
disabled: !queueElements.length,
|
||||
},
|
||||
"Clear",
|
||||
),
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
class:
|
||||
"px-2.5 py-1.5 -my-1.5 border border-transparent text-xs font-medium rounded-sm shadow-xs text-white bg-cyan-600 hover:bg-cyan-700 focus:outline-hidden focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
title: "Commit queued tasks",
|
||||
disabled: !statusCount.queued,
|
||||
onclick: () => {
|
||||
@@ -458,45 +576,47 @@ const component: ClosureComponent = (): Component => {
|
||||
},
|
||||
"Commit",
|
||||
),
|
||||
m(
|
||||
"button",
|
||||
{
|
||||
title: "Clear tasks",
|
||||
onclick: clear,
|
||||
disabled: !queueElements.length,
|
||||
},
|
||||
"Clear",
|
||||
),
|
||||
);
|
||||
|
||||
statusElement = m(
|
||||
".status",
|
||||
"div.flex p-4 gap-5 items-center text-sm",
|
||||
m(
|
||||
"span.queued",
|
||||
{ class: statusCount.queued ? "active" : "" },
|
||||
"span.text-stone-700 -mx-1 px-1",
|
||||
{ class: statusCount.queued ? "font-semibold" : "" },
|
||||
`Queued: ${statusCount.queued}`,
|
||||
),
|
||||
m(
|
||||
"span.pending",
|
||||
{ class: statusCount.pending ? "active" : "" },
|
||||
`Pending: ${statusCount.pending}`,
|
||||
"span.text-stone-700 relative",
|
||||
statusCount.pending
|
||||
? m(
|
||||
"div.block absolute -inset-x-1 inset-y-0 rounded-sm bg-emerald-200 animate-pulse",
|
||||
"",
|
||||
)
|
||||
: null,
|
||||
m("span.relative", `Pending: ${statusCount.pending}`),
|
||||
),
|
||||
m(
|
||||
"span.fault",
|
||||
{ class: statusCount.fault ? "active" : "" },
|
||||
`Fault: ${statusCount.fault}`,
|
||||
"span.text-stone-700 relative",
|
||||
m("span.relative", `Fault: ${statusCount.fault}`),
|
||||
),
|
||||
m(
|
||||
"span.stale",
|
||||
{ class: statusCount.stale ? "active" : "" },
|
||||
`Stale: ${statusCount.stale}`,
|
||||
"span.text-stone-700 relative",
|
||||
statusCount.stale
|
||||
? m(
|
||||
"div.block absolute -inset-x-1 inset-y-0 rounded-sm bg-stone-200",
|
||||
"",
|
||||
)
|
||||
: null,
|
||||
m("span.relative", `Stale: ${statusCount.stale}`),
|
||||
),
|
||||
actions,
|
||||
);
|
||||
|
||||
drawerElement = m(
|
||||
".drawer",
|
||||
"div",
|
||||
{
|
||||
class:
|
||||
"w-[48rem] mx-auto pointer-events-auto bg-white rounded-b-lg border-stone-300 border-x border-b shadow-md overflow-hidden transition-[height] -mt-px",
|
||||
key: "drawer",
|
||||
style: "opacity: 0;height: 0;",
|
||||
oncreate: (vnode2) => {
|
||||
@@ -504,20 +624,20 @@ const component: ClosureComponent = (): Component => {
|
||||
(vnode2.dom as HTMLDivElement).style.opacity = "1";
|
||||
resizeDrawer();
|
||||
},
|
||||
onmouseover: (e) => {
|
||||
onmouseenter: (e) => {
|
||||
if (drawerElement.dom.style.opacity === "0") return;
|
||||
vnode.state["mouseIn"] = true;
|
||||
resizeDrawer();
|
||||
e.redraw = false;
|
||||
},
|
||||
onmouseleave: (e) => {
|
||||
if (drawerElement.dom.style.opacity === "0") return;
|
||||
vnode.state["mouseIn"] = false;
|
||||
resizeDrawer();
|
||||
e.redraw = false;
|
||||
},
|
||||
onupdate: resizeDrawer,
|
||||
onbeforeremove: (vnode2) => {
|
||||
(vnode2.dom as HTMLDivElement).onmouseover = null;
|
||||
(vnode2.dom as HTMLDivElement).onmouseleave = null;
|
||||
(vnode2.dom as HTMLDivElement).style.opacity = "0";
|
||||
(vnode2.dom as HTMLDivElement).style.height = "0";
|
||||
return new Promise((resolve) => {
|
||||
@@ -526,18 +646,20 @@ const component: ClosureComponent = (): Component => {
|
||||
},
|
||||
},
|
||||
statusElement,
|
||||
stagingElements.length ? stagingElements : m(".queue", queueElements),
|
||||
stagingElements.length
|
||||
? stagingElements
|
||||
: m("div.px-4 pb-4 text-sm", queueElements),
|
||||
);
|
||||
}
|
||||
|
||||
return m(
|
||||
"div.drawer-wrapper",
|
||||
"div.fixed pointer-events-none inset-0 z-30",
|
||||
drawerElement,
|
||||
m(
|
||||
"div.notifications-wrapper",
|
||||
"div",
|
||||
{
|
||||
class: "relative w-[48rem] mx-auto pointer-events-auto",
|
||||
key: "notifications",
|
||||
style: "position: relative;",
|
||||
onupdate: repositionNotifications,
|
||||
oncreate: repositionNotifications,
|
||||
},
|
||||
|
||||
+7
-12
@@ -1,7 +1,7 @@
|
||||
import * as notifications from "./notifications.ts";
|
||||
|
||||
export let codeMirror: typeof import("codemirror");
|
||||
export let yaml: typeof import("yaml");
|
||||
export let codeMirror: typeof import("./codemirror-loader");
|
||||
export let yaml: typeof import("./yaml-loader");
|
||||
|
||||
let note;
|
||||
|
||||
@@ -23,14 +23,9 @@ export function loadCodeMirror(): Promise<void> {
|
||||
if (codeMirror) return Promise.resolve();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const promises = [
|
||||
import("codemirror"),
|
||||
import("codemirror/mode/javascript/javascript"),
|
||||
import("codemirror/mode/yaml/yaml"),
|
||||
];
|
||||
Promise.all(promises)
|
||||
.then((modules) => {
|
||||
codeMirror = modules[0].default;
|
||||
import("./codemirror-loader")
|
||||
.then((module) => {
|
||||
codeMirror = module;
|
||||
resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
@@ -44,9 +39,9 @@ export function loadYaml(): Promise<void> {
|
||||
if (yaml) return Promise.resolve();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
import("yaml")
|
||||
import("./yaml-loader")
|
||||
.then((module) => {
|
||||
yaml = module.default;
|
||||
yaml = module;
|
||||
resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
Loaded 100 of 144 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user