🐛 Fix webhook default timeout and unoptimized json parser (#1492)
This commit is contained in:
@ -205,7 +205,7 @@ export const executeWebhook = async (
|
|||||||
json: !isFormData && body && isJson ? body : undefined,
|
json: !isFormData && body && isJson ? body : undefined,
|
||||||
body: (isFormData && body ? body : undefined) as any,
|
body: (isFormData && body ? body : undefined) as any,
|
||||||
timeout: isNotDefined(env.CHAT_API_TIMEOUT)
|
timeout: isNotDefined(env.CHAT_API_TIMEOUT)
|
||||||
? undefined
|
? false
|
||||||
: params.timeout && params.timeout !== defaultTimeout
|
: params.timeout && params.timeout !== defaultTimeout
|
||||||
? Math.min(params.timeout, maxTimeout) * 1000
|
? Math.min(params.timeout, maxTimeout) * 1000
|
||||||
: isLongRequest
|
: isLongRequest
|
||||||
@ -215,30 +215,39 @@ export const executeWebhook = async (
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await ky(request.url, omit(request, 'url'))
|
const response = await ky(request.url, omit(request, 'url'))
|
||||||
const body = await response.text()
|
const body = response.headers.get('content-type')?.includes('json')
|
||||||
|
? await response.json()
|
||||||
|
: await response.text()
|
||||||
logs.push({
|
logs.push({
|
||||||
status: 'success',
|
status: 'success',
|
||||||
description: webhookSuccessDescription,
|
description: webhookSuccessDescription,
|
||||||
details: {
|
details: {
|
||||||
statusCode: response.status,
|
statusCode: response.status,
|
||||||
response: safeJsonParse(body).data,
|
response: typeof body === 'string' ? safeJsonParse(body).data : body,
|
||||||
request,
|
request,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
response: {
|
response: {
|
||||||
statusCode: response.status,
|
statusCode: response.status,
|
||||||
data: safeJsonParse(body).data,
|
data: typeof body === 'string' ? safeJsonParse(body).data : body,
|
||||||
},
|
},
|
||||||
logs,
|
logs,
|
||||||
startTimeShouldBeUpdated: true,
|
startTimeShouldBeUpdated: true,
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HTTPError) {
|
if (error instanceof HTTPError) {
|
||||||
const responseBody = await error.response.text()
|
const responseBody = error.response.headers
|
||||||
|
.get('content-type')
|
||||||
|
?.includes('json')
|
||||||
|
? await error.response.json()
|
||||||
|
: await error.response.text()
|
||||||
const response = {
|
const response = {
|
||||||
statusCode: error.response.status,
|
statusCode: error.response.status,
|
||||||
data: safeJsonParse(responseBody).data,
|
data:
|
||||||
|
typeof responseBody === 'string'
|
||||||
|
? safeJsonParse(responseBody).data
|
||||||
|
: responseBody,
|
||||||
}
|
}
|
||||||
logs.push({
|
logs.push({
|
||||||
status: 'error',
|
status: 'error',
|
||||||
@ -255,13 +264,15 @@ export const executeWebhook = async (
|
|||||||
const response = {
|
const response = {
|
||||||
statusCode: 408,
|
statusCode: 408,
|
||||||
data: {
|
data: {
|
||||||
message: `Request timed out. (${(request.timeout ?? 0) / 1000}ms)`,
|
message: `Request timed out. (${
|
||||||
|
(request.timeout ? request.timeout : 0) / 1000
|
||||||
|
}ms)`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
logs.push({
|
logs.push({
|
||||||
status: 'error',
|
status: 'error',
|
||||||
description: `Webhook request timed out. (${
|
description: `Webhook request timed out. (${
|
||||||
(request.timeout ?? 0) / 1000
|
(request.timeout ? request.timeout : 0) / 1000
|
||||||
}s)`,
|
}s)`,
|
||||||
details: {
|
details: {
|
||||||
response,
|
response,
|
||||||
|
@ -12,8 +12,7 @@
|
|||||||
- After the match there is , OR } without " after it OR ] without " after it
|
- After the match there is , OR } without " after it OR ] without " after it
|
||||||
*/
|
*/
|
||||||
export const JSONParse = (json: string) => {
|
export const JSONParse = (json: string) => {
|
||||||
const numbersBiggerThanMaxInt =
|
const numbersBiggerThanMaxInt = /(?<!\\)":\s*(-?\d{17,})(?=[,\]}])/g
|
||||||
/(?<=[^\\]":[\[]?|[^\\]":\[.*)(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\}[^"]|\][^"])/g
|
|
||||||
const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`)
|
const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`)
|
||||||
|
|
||||||
return JSON.parse(serializedData, (_, value) => {
|
return JSON.parse(serializedData, (_, value) => {
|
||||||
|
Reference in New Issue
Block a user