Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix webhook default timeout #1492

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export const executeWebhook = async (
json: !isFormData && body && isJson ? body : undefined,
body: (isFormData && body ? body : undefined) as any,
timeout: isNotDefined(env.CHAT_API_TIMEOUT)
? undefined
? false
: params.timeout && params.timeout !== defaultTimeout
? Math.min(params.timeout, maxTimeout) * 1000
: isLongRequest
Expand All @@ -215,30 +215,39 @@ export const executeWebhook = async (

try {
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({
status: 'success',
description: webhookSuccessDescription,
details: {
statusCode: response.status,
response: safeJsonParse(body).data,
response: typeof body === 'string' ? safeJsonParse(body).data : body,
request,
},
})
return {
response: {
statusCode: response.status,
data: safeJsonParse(body).data,
data: typeof body === 'string' ? safeJsonParse(body).data : body,
},
logs,
startTimeShouldBeUpdated: true,
}
} catch (error) {
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 = {
statusCode: error.response.status,
data: safeJsonParse(responseBody).data,
data:
typeof responseBody === 'string'
? safeJsonParse(responseBody).data
: responseBody,
}
logs.push({
status: 'error',
Expand All @@ -255,13 +264,15 @@ export const executeWebhook = async (
const response = {
statusCode: 408,
data: {
message: `Request timed out. (${(request.timeout ?? 0) / 1000}ms)`,
message: `Request timed out. (${
(request.timeout ? request.timeout : 0) / 1000
}ms)`,
},
}
logs.push({
status: 'error',
description: `Webhook request timed out. (${
(request.timeout ?? 0) / 1000
(request.timeout ? request.timeout : 0) / 1000
}s)`,
details: {
response,
Expand Down
3 changes: 1 addition & 2 deletions packages/lib/JSONParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
- After the match there is , OR } without " after it OR ] without " after it
*/
export const JSONParse = (json: string) => {
const numbersBiggerThanMaxInt =
/(?<=[^\\]":[\[]?|[^\\]":\[.*)(-?\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 numbersBiggerThanMaxInt = /(?<!\\)":\s*(-?\d{17,})(?=[,\]}])/g
const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`)

return JSON.parse(serializedData, (_, value) => {
Expand Down