Adding timeouts for some http queries
This commit is contained in:
parent
cdd3e58c24
commit
2193aceaaa
@ -1,4 +1,5 @@
|
|||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
const AbortController = require('abort-controller');
|
||||||
const DebugInfo = require('../models/DebugInfo');
|
const DebugInfo = require('../models/DebugInfo');
|
||||||
const Vehicle = require('../models/vehicle');
|
const Vehicle = require('../models/vehicle');
|
||||||
const Utils = require('../utils/utils');
|
const Utils = require('../utils/utils');
|
||||||
@ -6,10 +7,26 @@ const Utils = require('../utils/utils');
|
|||||||
const baseUrl = 'https://vin01.ru/v2';
|
const baseUrl = 'https://vin01.ru/v2';
|
||||||
const reportBaseUrl = `${baseUrl}/gibddApp.php`;
|
const reportBaseUrl = `${baseUrl}/gibddApp.php`;
|
||||||
|
|
||||||
|
async function fetchWithTimeout(url, params, timeout) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
controller.abort();
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
|
let requestParams = params ?? {};
|
||||||
|
requestParams.signal = controller.signal;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await fetch(url, requestParams);
|
||||||
|
} finally {
|
||||||
|
clearInterval(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Vin01Provider {
|
class Vin01Provider {
|
||||||
static async getVin(number, token) {
|
static async getVin(number, token) {
|
||||||
let url = `${baseUrl}/getVin.php?key=${token}&gosNumber=${encodeURIComponent(number)}`;
|
let url = `${baseUrl}/getVin.php?key=${token}&gosNumber=${encodeURIComponent(number)}`;
|
||||||
let result = await fetch(url);
|
let result = await fetchWithTimeout(url, null, 12000);
|
||||||
let json = await result.json();
|
let json = await result.json();
|
||||||
if(json.success && json.code == 200) {
|
if(json.success && json.code == 200) {
|
||||||
return json.data.vin;
|
return json.data.vin;
|
||||||
@ -19,7 +36,7 @@ class Vin01Provider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async runCheck(type, vin, token) {
|
static async runCheck(type, vin, token) {
|
||||||
let result = await fetch(reportBaseUrl, {
|
let result = await fetchWithTimeout(reportBaseUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
typeCheckValue: type,
|
typeCheckValue: type,
|
||||||
@ -27,7 +44,7 @@ class Vin01Provider {
|
|||||||
key: token,
|
key: token,
|
||||||
token: null
|
token: null
|
||||||
})
|
})
|
||||||
});
|
}, 15000);
|
||||||
console.log('Vin01 response for: ', type);
|
console.log('Vin01 response for: ', type);
|
||||||
return await result.json();
|
return await result.json();
|
||||||
}
|
}
|
||||||
|
|||||||
33
package-lock.json
generated
33
package-lock.json
generated
@ -9,6 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"abort-controller": "^3.0.0",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
"compress": "^0.99.0",
|
"compress": "^0.99.0",
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
@ -319,6 +320,17 @@
|
|||||||
"react-native": "*"
|
"react-native": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/abort-controller": {
|
||||||
|
"version": "3.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
|
||||||
|
"integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
|
||||||
|
"dependencies": {
|
||||||
|
"event-target-shim": "^5.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
"version": "1.3.7",
|
"version": "1.3.7",
|
||||||
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
|
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
|
||||||
@ -1355,6 +1367,14 @@
|
|||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/event-target-shim": {
|
||||||
|
"version": "5.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
|
||||||
|
"integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/expo-random": {
|
"node_modules/expo-random": {
|
||||||
"version": "8.2.1",
|
"version": "8.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/expo-random/-/expo-random-8.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/expo-random/-/expo-random-8.2.1.tgz",
|
||||||
@ -3418,6 +3438,14 @@
|
|||||||
"prop-types": "^15.6.1"
|
"prop-types": "^15.6.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"abort-controller": {
|
||||||
|
"version": "3.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
|
||||||
|
"integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
|
||||||
|
"requires": {
|
||||||
|
"event-target-shim": "^5.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"accepts": {
|
"accepts": {
|
||||||
"version": "1.3.7",
|
"version": "1.3.7",
|
||||||
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
|
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
|
||||||
@ -4233,6 +4261,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
|
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
|
||||||
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
|
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
|
||||||
},
|
},
|
||||||
|
"event-target-shim": {
|
||||||
|
"version": "5.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
|
||||||
|
"integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ=="
|
||||||
|
},
|
||||||
"expo-random": {
|
"expo-random": {
|
||||||
"version": "8.2.1",
|
"version": "8.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/expo-random/-/expo-random-8.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/expo-random/-/expo-random-8.2.1.tgz",
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
"author": "Selim Mustafaev",
|
"author": "Selim Mustafaev",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"abort-controller": "^3.0.0",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
"compress": "^0.99.0",
|
"compress": "^0.99.0",
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user