p2p instant question sending fixes

This commit is contained in:
mrfry
2023-04-26 15:02:50 +02:00
parent 977c118da8
commit f047701106
9 changed files with 75 additions and 56 deletions
+42 -25
View File
@@ -14,19 +14,37 @@ export function get<T = any>(
const provider = useHttp ? http : https
return new Promise((resolve) => {
const req = provider.get(options, function (res) {
const bodyChunks: Uint8Array[] = []
res.on('data', (chunk) => {
bodyChunks.push(chunk)
}).on('end', () => {
const body = Buffer.concat(bodyChunks).toString()
try {
resolve({ data: JSON.parse(body) })
} catch (e) {
resolve({ error: e, options: options })
}
})
})
const req = provider.get(
{
...options,
headers: {
...options?.headers,
'Content-Type': 'application/json',
},
},
function (res) {
const bodyChunks: Uint8Array[] = []
res.on('data', (chunk) => {
bodyChunks.push(chunk)
}).on('end', () => {
const body = Buffer.concat(bodyChunks).toString()
try {
if (res.statusCode === 200) {
resolve({ data: JSON.parse(body) })
} else {
resolve({
data: JSON.parse(body),
error: new Error(
`HTTP response code: ${res.statusCode}`
),
})
}
} catch (e) {
resolve({ error: e, options: options })
}
})
}
)
req.on('error', function (e) {
resolve({ error: e, options: options })
})
@@ -36,7 +54,7 @@ export function get<T = any>(
export interface PostResult<T> {
data?: T
error?: Error
cookies?: string[]
cookie?: string[]
}
interface PostParams {
@@ -45,7 +63,7 @@ interface PostParams {
port: number
bodyObject: any
http?: boolean
cookies?: string
cookie?: string
}
// https://nodejs.org/api/http.html#httprequesturl-options-callback
@@ -55,7 +73,7 @@ export function post<T = any>({
port,
bodyObject,
http,
cookies,
cookie,
}: PostParams): Promise<PostResult<T>> {
const provider = http ? httpRequest : httpsRequest
const body = JSON.stringify(bodyObject)
@@ -70,9 +88,9 @@ export function post<T = any>({
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
...(cookies
...(cookie
? {
cookie: cookies,
cookie: cookie,
}
: {}),
},
@@ -88,7 +106,7 @@ export function post<T = any>({
try {
resolve({
data: JSON.parse(body),
cookies: res.headers['set-cookie'],
cookie: res.headers['set-cookie'],
})
} catch (e) {
resolve({ error: e })
@@ -101,16 +119,15 @@ export function post<T = any>({
resolve({ error: e })
})
req.write(body)
req.end()
req.end(body)
})
}
export function parseCookies(responseCookies: string[]): {
export function parseCookie(responseCookie: string[]): {
[key: string]: string
} {
const cookiesArray = responseCookies.join('; ').split('; ')
const parsedCookies: { [key: string]: string } = cookiesArray.reduce(
const cookieArray = responseCookie.join('; ').split('; ')
const parsedCookie: { [key: string]: string } = cookieArray.reduce(
(acc, cookieString) => {
const [key, val] = cookieString.split('=')
return {
@@ -120,5 +137,5 @@ export function parseCookies(responseCookies: string[]): {
},
{}
)
return parsedCookies
return parsedCookie
}
+3 -3
View File
@@ -1,6 +1,6 @@
import { PeerInfo } from '../types/basicTypes'
import { paths } from './files'
import { parseCookies, post } from './networkUtils'
import { parseCookie, post } from './networkUtils'
import utils from './utils'
export function peerToString(peer: {
@@ -33,7 +33,7 @@ export function updatePeersFile(
}
export async function loginToPeer(peer: PeerInfo): Promise<string | Error> {
const { data, error, cookies } = await post<{
const { data, error, cookie } = await post<{
result: string
msg: string
}>({
@@ -48,6 +48,6 @@ export async function loginToPeer(peer: PeerInfo): Promise<string | Error> {
return data ? new Error(data.msg) : error
}
const parsedCookies = parseCookies(cookies)
const parsedCookies = parseCookie(cookie)
return parsedCookies.sessionID
}