From 9ccbbbdc375055f4eb5a1d256f494578dd963b1b Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Sat, 18 Oct 2025 16:17:01 +0000 Subject: [PATCH] fix(snmp): resolve TypeScript type errors for Deno strict mode - Add Buffer import from node:buffer to manager.ts and types.ts - Fix error handling type assertions (error/retryError/stdError as unknown) - Add explicit type annotation to byte parameter in isPrintableAscii check - All tests now passing (3 SNMP tests + 10 logger tests) --- ts/snmp/manager.ts | 19 ++++++++++--------- ts/snmp/types.ts | 2 ++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ts/snmp/manager.ts b/ts/snmp/manager.ts index 1d9ae84..f3a9ef6 100644 --- a/ts/snmp/manager.ts +++ b/ts/snmp/manager.ts @@ -1,4 +1,5 @@ import * as snmp from "npm:net-snmp@3.20.0"; +import { Buffer } from "node:buffer"; import type { IOidSet, ISnmpConfig, TUpsModel, IUpsStatus } from './types.ts'; import { UpsOidSets } from './oid-sets.ts'; @@ -245,7 +246,7 @@ export class NupstSnmp { // Handle specific types that might need conversion if (Buffer.isBuffer(value)) { // If value is a Buffer, try to convert it to a string if it's printable ASCII - const isPrintableAscii = value.every(byte => byte >= 32 && byte <= 126); + const isPrintableAscii = value.every((byte: number) => byte >= 32 && byte <= 126); if (isPrintableAscii) { value = value.toString(); } @@ -335,10 +336,10 @@ export class NupstSnmp { } catch (error) { if (this.debug) { console.error('---------------------------------------'); - console.error('Error getting UPS status:', error.message); + console.error('Error getting UPS status:', error instanceof Error ? error.message : String(error)); console.error('---------------------------------------'); } - throw new Error(`Failed to get UPS status: ${error.message}`); + throw new Error(`Failed to get UPS status: ${error instanceof Error ? error.message : String(error)}`); } } @@ -373,7 +374,7 @@ export class NupstSnmp { return value; } catch (error) { if (this.debug) { - console.error(`Error getting ${description}:`, error.message); + console.error(`Error getting ${description}:`, error instanceof Error ? error.message : String(error)); } // If we're using SNMPv3, try with different security levels @@ -424,11 +425,11 @@ export class NupstSnmp { return value; } catch (retryError) { if (this.debug) { - console.error(`Retry failed for ${description}:`, retryError.message); + console.error(`Retry failed for ${description}:`, retryError instanceof Error ? retryError.message : String(retryError)); } } } - + // Try with noAuthNoPriv as a last resort if (config.securityLevel === 'authPriv' || config.securityLevel === 'authNoPriv') { const retryConfig = { ...config, securityLevel: 'noAuthNoPriv' as 'noAuthNoPriv' }; @@ -443,11 +444,11 @@ export class NupstSnmp { return value; } catch (retryError) { if (this.debug) { - console.error(`Retry failed for ${description}:`, retryError.message); + console.error(`Retry failed for ${description}:`, retryError instanceof Error ? retryError.message : String(retryError)); } } } - + return 0; } @@ -478,7 +479,7 @@ export class NupstSnmp { return standardValue; } catch (stdError) { if (this.debug) { - console.error(`Standard OID retry failed for ${description}:`, stdError.message); + console.error(`Standard OID retry failed for ${description}:`, stdError instanceof Error ? stdError.message : String(stdError)); } } diff --git a/ts/snmp/types.ts b/ts/snmp/types.ts index 32508f4..8e3d39d 100644 --- a/ts/snmp/types.ts +++ b/ts/snmp/types.ts @@ -2,6 +2,8 @@ * Type definitions for SNMP module */ +import { Buffer } from "node:buffer"; + /** * UPS status interface */