From d2ca8ed55b3ece4e2613773f606b3d7e13ed06e6 Mon Sep 17 00:00:00 2001 From: baldo Date: Thu, 28 Jul 2022 14:09:46 +0200 Subject: [PATCH] Make node filename parsing more explicit and add stronger typing. --- server/services/monitoringService.ts | 2 -- server/services/nodeService.ts | 45 ++++++++++++++++------------ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/server/services/monitoringService.ts b/server/services/monitoringService.ts index 5f6c2d5..11c9231 100644 --- a/server/services/monitoringService.ts +++ b/server/services/monitoringService.ts @@ -176,7 +176,6 @@ async function storeNodeInformation(nodeData: ParsedNode, node: StoredNode): Pro const isValidMac = forConstraint(CONSTRAINTS.node.mac, false); -// TODO: Use sparkson for JSON parsing. export function parseNode(importTimestamp: UnixTimestampSeconds, nodeData: any): ParsedNode { if (!_.isPlainObject(nodeData)) { throw new Error( @@ -249,7 +248,6 @@ export function parseNode(importTimestamp: UnixTimestampSeconds, nodeData: any): }; } -// TODO: Use sparkson for JSON parsing. export function parseNodesJson(body: string): NodesParsingResult { Logger.tag('monitoring', 'information-retrieval').debug('Parsing nodes.json...'); diff --git a/server/services/nodeService.ts b/server/services/nodeService.ts index 7a12def..8347640 100644 --- a/server/services/nodeService.ts +++ b/server/services/nodeService.ts @@ -1,4 +1,3 @@ -import _ from "lodash"; import async from "async"; import crypto from "crypto"; import oldFs, {promises as fs} from "graceful-fs"; @@ -18,7 +17,12 @@ import { EmailAddress, FastdKey, Hostname, + isFastdKey, + isHostname, + isMAC, + isMonitoringToken, isStoredNode, + isToken, MAC, MailType, MonitoringState, @@ -29,6 +33,7 @@ import { StoredNode, Token, toUnixTimestampSeconds, + TypeGuard, unhandledEnumField, UnixTimestampMilliseconds, UnixTimestampSeconds @@ -45,13 +50,12 @@ type NodeFilter = { monitoringToken?: MonitoringToken, } -// TODO: Newtypes? type NodeFilenameParsed = { - hostname?: string, - mac?: string, - key?: string, - token?: string, - monitoringToken?: string, + hostname?: Hostname, + mac?: MAC, + key?: FastdKey, + token?: Token, + monitoringToken?: MonitoringToken, } enum LINE_PREFIX { @@ -65,7 +69,6 @@ enum LINE_PREFIX { MONITORING_TOKEN = "# Monitoring-Token: ", } -const filenameParts = ['hostname', 'mac', 'key', 'token', 'monitoringToken']; function generateToken(): Type { return crypto.randomBytes(8).toString('hex') as Type; @@ -108,16 +111,20 @@ async function findFilesInPeersPath(): Promise { } function parseNodeFilename(filename: string): NodeFilenameParsed { - const parts = filename.split('@', filenameParts.length); - const parsed: { [key: string]: string | undefined } = {}; - const zippedParts = _.zip(filenameParts, parts); - for (const part of zippedParts) { - const key = part[0]; - if (key) { - parsed[key] = part[1]; - } + const parts = filename.split('@', 5); + + function get(isT: TypeGuard, index: number): T | undefined { + const value = index >= 0 && index < parts.length ? parts[index] : undefined; + return isT(value) ? value : undefined; } - return parsed; + + return { + hostname: get(isHostname, 0), + mac: get(isMAC, 1), + key: get(isFastdKey, 2), + token: get(isToken, 3), + monitoringToken: get(isMonitoringToken, 4), + }; } function isDuplicate(filter: NodeFilter, token?: Token): boolean { @@ -446,7 +453,7 @@ export async function updateNode(token: Token, node: CreateOrUpdateNode): Promis // monitoring just has been enabled monitoringState = MonitoringState.PENDING; monitoringToken = generateToken(); - break; + break; case MonitoringState.PENDING: case MonitoringState.ACTIVE: @@ -460,7 +467,7 @@ export async function updateNode(token: Token, node: CreateOrUpdateNode): Promis monitoringState = currentNode.monitoringState; monitoringToken = nodeSecrets.monitoringToken || generateToken(); } - break; + break; default: unhandledEnumField(currentNode.monitoringState);