worker file split, sending new questions to peers instantly

This commit is contained in:
mrfry
2023-04-24 20:39:15 +02:00
parent 8c4e184741
commit 252826a081
25 changed files with 1016 additions and 705 deletions
+24 -2
View File
@@ -23,7 +23,6 @@ export function get<T = any>(
try {
resolve({ data: JSON.parse(body) })
} catch (e) {
console.log(body)
resolve({ error: e, options: options })
}
})
@@ -46,6 +45,7 @@ interface PostParams {
port: number
bodyObject: any
http?: boolean
cookies?: string
}
// https://nodejs.org/api/http.html#httprequesturl-options-callback
@@ -55,6 +55,7 @@ export function post<T = any>({
port,
bodyObject,
http,
cookies,
}: PostParams): Promise<PostResult<T>> {
const provider = http ? httpRequest : httpsRequest
const body = JSON.stringify(bodyObject)
@@ -69,6 +70,11 @@ export function post<T = any>({
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
...(cookies
? {
cookie: cookies,
}
: {}),
},
},
(res) => {
@@ -85,7 +91,6 @@ export function post<T = any>({
cookies: res.headers['set-cookie'],
})
} catch (e) {
console.log(body)
resolve({ error: e })
}
})
@@ -100,3 +105,20 @@ export function post<T = any>({
req.end()
})
}
export function parseCookies(responseCookies: string[]): {
[key: string]: string
} {
const cookiesArray = responseCookies.join('; ').split('; ')
const parsedCookies: { [key: string]: string } = cookiesArray.reduce(
(acc, cookieString) => {
const [key, val] = cookieString.split('=')
return {
...acc,
[key]: val || true,
}
},
{}
)
return parsedCookies
}