padavan 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "padavan",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "The core library for interacting with routers running Padavan firmware. Provides a programmatic API for local control via HTTP.",
package/src/index.js CHANGED
@@ -2,6 +2,7 @@ import { debuglog, inspect } from 'util';
2
2
  import jszip from 'jszip';
3
3
  import HttpClient from './transport/http.js';
4
4
  import GitHubClient from './transport/github.js';
5
+ import { sanitizeObject } from './utils/sanitizers.js';
5
6
  import {
6
7
  parsePageInputs, parseNvramOutput, parseLooseJson, normalizeTrafficHistory,
7
8
  extractJsVariable, extractTextareaValue, extractMacsFromTextarea, extractCurrentChannel
@@ -94,7 +95,7 @@ export default class Padavan {
94
95
  * @type {Promise<any>}
95
96
  */
96
97
  #commandQueue = Promise.resolve();
97
-
98
+
98
99
  /**
99
100
  * Кэшированный промис запроса NVRAM.
100
101
  * @type {Promise<Record<string, string>>|null}
@@ -118,13 +119,16 @@ export default class Padavan {
118
119
  * @param {Config} config Конфигурация для подключения.
119
120
  */
120
121
  constructor(config = {}) {
121
- this.config = config;
122
+ const sanitized = sanitizeObject(config, ['credentials', 'logLevel']);
123
+ if (sanitized.credentials)
124
+ sanitized.credentials = sanitizeObject(sanitized.credentials, ['host', 'port', 'username', 'password', 'repo', 'branch', 'token']);
125
+ this.config = sanitized;
122
126
  this.#debugLog = debuglog(LIB_ID);
123
127
  this.#isDebugEnvEnabled = process.env.NODE_DEBUG && new RegExp(`\\b${LIB_ID}\\b`, 'i').test(process.env.NODE_DEBUG);
124
128
  this.#logLevelNumber = LOG_LEVELS[config?.logLevel] || LOG_LEVELS[DEFAULT_LOG_LEVEL];
125
129
  this.#http = new HttpClient(this.config?.credentials, this.log.bind(this));
126
130
  this.#github = new GitHubClient(this.config?.credentials, this.log.bind(this));
127
- this.log('debug', 'Padavan instance created with config:', config);
131
+ this.log('debug', 'Padavan instance created with config:', this.config);
128
132
  };
129
133
 
130
134
  /**
@@ -596,11 +600,6 @@ export default class Padavan {
596
600
  const artifact = await this.#github.getLatestArtifact();
597
601
  const toIdMatch = artifact.name.match(/-([0-9a-f]{7,})$/);
598
602
  const toId = toIdMatch ? toIdMatch[1]?.substring(0, 7) : null;
599
- console.log({
600
- currentFirmware, fromId,
601
- artifact,
602
- toIdMatch, toId
603
- });
604
603
 
605
604
  if (!fromId || !toId)
606
605
  throw new Error(`Could not determine firmware versions (current: ${fromId}, latest: ${toId})`);
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Рекурсивно очищает объект от пустых значений и неразрешенных ключей
3
+ * @param {Object} obj Исходный объект.
4
+ * @param {string[]} [allowedKeys] Список разрешенных ключей (если null - разрешены все).
5
+ * @returns {Object} Очищенный объект.
6
+ */
7
+ export function sanitizeObject(obj, allowedKeys = null) {
8
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj))
9
+ return obj;
10
+
11
+ const result = {};
12
+ for (const [key, value] of Object.entries(obj)) {
13
+ if (allowedKeys && !allowedKeys.includes(key))
14
+ continue;
15
+ if (value === '' || value === null || value === undefined)
16
+ continue;
17
+ if (typeof value === 'object' && !Array.isArray(value)) {
18
+ const cleaned = sanitizeObject(value);
19
+ if (Object.keys(cleaned).length > 0)
20
+ result[key] = cleaned;
21
+ } else
22
+ result[key] = value;
23
+ }
24
+
25
+ return result;
26
+ };