rework PR comments

This commit is contained in:
Claus-Peter Hübner 2022-12-02 15:27:26 +01:00
parent f7a7049e99
commit ea6fb28cf8

View File

@ -21,6 +21,13 @@ enum ApiVersionType {
V1_1 = 'v1_1', V1_1 = 'v1_1',
V2_0 = 'v2_0', V2_0 = 'v2_0',
} }
type CommunityApi = {
api: string
url: string
}
type CommunityApiList = {
apiVersions: CommunityApi[]
}
export const startDHT = async ( export const startDHT = async (
// connection: Connection, // connection: Connection,
@ -32,9 +39,13 @@ export const startDHT = async (
logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`)
logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`)
const apiList = { const apiList: CommunityApiList = {
apiVersions: Object.values(ApiVersionType).map(function (apiEnum) { 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)}`) logger.debug(`ApiList: ${JSON.stringify(apiList)}`)
@ -52,16 +63,10 @@ export const startDHT = async (
socket.on('data', async (data: Buffer) => { socket.on('data', async (data: Buffer) => {
try { try {
logger.info(`data: ${data.toString('ascii')}`) logger.info(`data: ${data.toString('ascii')}`)
const json = JSON.parse(data.toString('ascii')) const apiVersionList: CommunityApiList = JSON.parse(data.toString('ascii'))
if ( if (apiVersionList && apiVersionList.apiVersions) {
json.apiVersions && for (let i = 0; i < apiVersionList.apiVersions.length; i++) {
Array.isArray(json.apiVersions) && const apiVersion = apiVersionList.apiVersions[i]
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 variables = { const variables = {
apiVersion: apiVersion.api, apiVersion: apiVersion.api,
@ -149,12 +154,9 @@ export const startDHT = async (
socket.on('open', function () { socket.on('open', function () {
// noiseSocket fully open with the other peer // noiseSocket fully open with the other peer
// console.log("writing to socket");
socket.write(Buffer.from(JSON.stringify(apiList))) socket.write(Buffer.from(JSON.stringify(apiList)))
successfulRequests.push(remotePubKey) successfulRequests.push(remotePubKey)
}) })
// pipe it somewhere like any duplex stream
// process.stdin.pipe(noiseSocket).pipe(process.stdout)
}) })
}, POLLTIME) }, POLLTIME)
} catch (err) { } catch (err) {