mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2026-04-28 03:07:38 +02:00
worker file split, sending new questions to peers instantly
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user