From ea6fb28cf865437f9b573e3ed4e6a30f91283aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Fri, 2 Dec 2022 15:27:26 +0100 Subject: [PATCH] rework PR comments --- backend/src/federation/index.ts | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/backend/src/federation/index.ts b/backend/src/federation/index.ts index 889eae9fc..bb3dc7fab 100644 --- a/backend/src/federation/index.ts +++ b/backend/src/federation/index.ts @@ -21,6 +21,13 @@ enum ApiVersionType { V1_1 = 'v1_1', V2_0 = 'v2_0', } +type CommunityApi = { + api: string + url: string +} +type CommunityApiList = { + apiVersions: CommunityApi[] +} export const startDHT = async ( // connection: Connection, @@ -32,9 +39,13 @@ export const startDHT = async ( logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) - const apiList = { + const apiList: CommunityApiList = { apiVersions: Object.values(ApiVersionType).map(function (apiEnum) { - return { api: apiEnum, url: CONFIG.FEDERATION_COMMUNITY_URL } + const comApi: CommunityApi = { + api: apiEnum, + url: CONFIG.FEDERATION_COMMUNITY_URL || 'not configured', + } + return comApi }), } logger.debug(`ApiList: ${JSON.stringify(apiList)}`) @@ -52,16 +63,10 @@ export const startDHT = async ( socket.on('data', async (data: Buffer) => { try { logger.info(`data: ${data.toString('ascii')}`) - const json = JSON.parse(data.toString('ascii')) - if ( - json.apiVersions && - Array.isArray(json.apiVersions) && - json.apiVersions.length > 0 && - typeof json.apiVersions[0].api === 'string' && - typeof json.apiVersions[0].url === 'string' - ) { - for (let i = 0; i < json.apiVersions.length; i++) { - const apiVersion = json.apiVersions[i] + const apiVersionList: CommunityApiList = JSON.parse(data.toString('ascii')) + if (apiVersionList && apiVersionList.apiVersions) { + for (let i = 0; i < apiVersionList.apiVersions.length; i++) { + const apiVersion = apiVersionList.apiVersions[i] const variables = { apiVersion: apiVersion.api, @@ -149,12 +154,9 @@ export const startDHT = async ( socket.on('open', function () { // noiseSocket fully open with the other peer - // console.log("writing to socket"); socket.write(Buffer.from(JSON.stringify(apiList))) successfulRequests.push(remotePubKey) }) - // pipe it somewhere like any duplex stream - // process.stdin.pipe(noiseSocket).pipe(process.stdout) }) }, POLLTIME) } catch (err) {