kopia lustrzana https://github.com/FacilMap/facilmap
Use node-fetch instead of request
rodzic
12a25cb486
commit
0f5c0ca5e2
|
@ -33,5 +33,5 @@
|
|||
#FM_DEV=
|
||||
|
||||
# Set to a comma-separated list of values (or *) to enable debug output by particular components. See https://github.com/visionmedia/debug for the syntax.
|
||||
# Some possible values: request, sql, express:*
|
||||
# Some possible values: sql, express:*
|
||||
#DEBUG=
|
|
@ -65,9 +65,6 @@
|
|||
"node-fetch": "^2.6.1",
|
||||
"p-throttle": "^4.1.1",
|
||||
"promisify-node": "^0.5.0",
|
||||
"request": "^2.88.2",
|
||||
"request-debug": "^0.2.0",
|
||||
"request-promise": "^4.2.6",
|
||||
"sequelize": "^6.3.5",
|
||||
"socket.io": "^4.0.0",
|
||||
"stream-array": "^1.1.2",
|
||||
|
@ -91,9 +88,6 @@
|
|||
"@types/lodash": "^4.14.165",
|
||||
"@types/mapbox__polyline": "^1.0.2",
|
||||
"@types/node-cron": "^2.0.3",
|
||||
"@types/request": "^2.48.5",
|
||||
"@types/request-debug": "^0.2.0",
|
||||
"@types/request-promise": "^4.1.47",
|
||||
"@types/socket.io": "^2.1.12",
|
||||
"@types/string-similarity": "^4.0.0",
|
||||
"debug": "^4.3.1",
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import config from "../config";
|
||||
import request from "../utils/request";
|
||||
import { calculateDistance, DecodedRouteMode } from "facilmap-utils";
|
||||
import { ExtraInfo, Point } from "facilmap-types";
|
||||
import { throttle } from "../utils/utils";
|
||||
import { RawRouteInfo } from "./routing";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
if (!config.orsToken)
|
||||
console.error("Warning: No ORS token configured, calculating routes will fail. Please set ORS_TOKEN in the environment or in config.env.");
|
||||
|
@ -56,48 +56,38 @@ async function calculateRouteInternal(points: Point[], decodedMode: DecodedRoute
|
|||
currentGroup.push(point);
|
||||
}
|
||||
|
||||
let results;
|
||||
const results = await Promise.all(coordGroups.map((coords) => {
|
||||
const req: any = {
|
||||
coordinates: coords.map((point) => [point.lon, point.lat]),
|
||||
radiuses: coords.map(() => -1),
|
||||
instructions: false
|
||||
};
|
||||
|
||||
try {
|
||||
results = await Promise.all(coordGroups.map((coords) => {
|
||||
const req: any = {
|
||||
coordinates: coords.map((point) => [point.lon, point.lat]),
|
||||
radiuses: coords.map(() => -1),
|
||||
instructions: false
|
||||
if(decodedMode.details) {
|
||||
req.elevation = true;
|
||||
req.extra_info = [ "surface", "waytype", "steepness" ];
|
||||
if(decodedMode.mode == "car") {
|
||||
req.extra_info.push("tollways");
|
||||
}
|
||||
}
|
||||
if(decodedMode.avoid) {
|
||||
req.options = {
|
||||
avoid_features: decodedMode.avoid
|
||||
};
|
||||
}
|
||||
if(decodedMode.preference)
|
||||
req.preference = decodedMode.preference;
|
||||
|
||||
if(decodedMode.details) {
|
||||
req.elevation = true;
|
||||
req.extra_info = [ "surface", "waytype", "steepness" ];
|
||||
if(decodedMode.mode == "car") {
|
||||
req.extra_info.push("tollways");
|
||||
}
|
||||
}
|
||||
if(decodedMode.avoid) {
|
||||
req.options = {
|
||||
avoid_features: decodedMode.avoid
|
||||
};
|
||||
}
|
||||
if(decodedMode.preference)
|
||||
req.preference = decodedMode.preference;
|
||||
|
||||
return request.post({
|
||||
url: `${ROUTING_URL}/${ROUTING_MODES[`${decodedMode.mode}-${decodedMode.type || ""}`]}/geojson`,
|
||||
json: true,
|
||||
headers: {
|
||||
'Authorization': config.orsToken,
|
||||
'Accept': '*/*' // Server sends application/geo+json
|
||||
},
|
||||
body: req
|
||||
});
|
||||
}));
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
if(err.response.body && err.response.body.error)
|
||||
throw new Error(err.response.body.error.message);
|
||||
else
|
||||
throw err;
|
||||
}
|
||||
return fetch(`${ROUTING_URL}/${ROUTING_MODES[`${decodedMode.mode}-${decodedMode.type || ""}`]}/geojson`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
...(config.orsToken ? { "Authorization": config.orsToken } : {}),
|
||||
"Accept": "*/*", // Server sends application/geo+json
|
||||
"Content-type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(req)
|
||||
}).then((res) => res.json());
|
||||
}));
|
||||
|
||||
const ret = {
|
||||
trackPoints: [] as Array<Point & { ele?: number }>,
|
||||
|
@ -109,10 +99,13 @@ async function calculateRouteInternal(points: Point[], decodedMode: DecodedRoute
|
|||
};
|
||||
|
||||
for(const body of results) {
|
||||
if(body && body.error) {
|
||||
throw new Error(body.error.message);
|
||||
if(body?.error) {
|
||||
throw new Error(body.error?.message || body.error);
|
||||
}
|
||||
|
||||
if (body?.metadata?.system_message)
|
||||
console.log("OpenRouteService:", body?.metadata?.system_message);
|
||||
|
||||
if(!body?.features?.[0])
|
||||
throw new Error("Invalid response from routing server.");
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import request from "../utils/request";
|
||||
import config from "../config";
|
||||
import { Point, RouteMode } from "facilmap-types";
|
||||
import { RawRouteInfo } from "./routing";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
if (!config.mapboxToken)
|
||||
console.error("Warning: No Mapbox token configured, calculating routes will fail. Please set MAPBOX_TOKEN in the environment or in config.env.");
|
||||
|
@ -36,7 +36,7 @@ export async function calculateOSRMRoute(points: Point[], mode: RouteMode, simpl
|
|||
+ "&overview=" + (simple ? "simplified" : "full")
|
||||
+ "&access_token=" + encodeURIComponent(config.mapboxToken ?? "");
|
||||
|
||||
return request.get({ url, json: true });
|
||||
return fetch(url).then((res) => res.json());
|
||||
}));
|
||||
|
||||
const ret: RawRouteInfo = {
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
import requestPromise from "request-promise";
|
||||
import debug from "debug";
|
||||
import requestDebug from "request-debug";
|
||||
import config from "../config";
|
||||
|
||||
if(debug.enabled("request")) {
|
||||
requestDebug(requestPromise);
|
||||
}
|
||||
|
||||
const request: typeof requestPromise = requestPromise.defaults({
|
||||
gzip: true,
|
||||
headers: {
|
||||
'User-Agent': config.userAgent
|
||||
}
|
||||
});
|
||||
|
||||
export default request;
|
73
yarn.lock
73
yarn.lock
|
@ -640,11 +640,6 @@
|
|||
dependencies:
|
||||
"@babel/types" "^7.3.0"
|
||||
|
||||
"@types/bluebird@*":
|
||||
version "3.5.34"
|
||||
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.34.tgz#0e9f1f4f5dfab98a421fb973b5f5690d22411893"
|
||||
integrity sha512-QMc57Pf067Rr78l6f4FftvuIXPYxu0VYFRKrZk1Clv+LWy7gN2fTBiAiv68askFHEHZcTLPFd01kNlpKOiSPgQ==
|
||||
|
||||
"@types/body-parser@*":
|
||||
version "1.19.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.0.tgz#0685b3c47eb3006ffed117cdd55164b61f80538f"
|
||||
|
@ -653,11 +648,6 @@
|
|||
"@types/connect" "*"
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/caseless@*":
|
||||
version "0.12.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.2.tgz#f65d3d6389e01eeb458bd54dc8f52b95a9463bc8"
|
||||
integrity sha512-6ckxMjBBD8URvjB6J3NcnuAn5Pkl7t3TizAg+xdlzzQGSPSmBcXf8KoIH0ua/i+tio+ZRUHEXp0HEmvaR4kt0w==
|
||||
|
||||
"@types/cheerio@^0.22.27", "@types/cheerio@^0.22.28":
|
||||
version "0.22.28"
|
||||
resolved "https://registry.yarnpkg.com/@types/cheerio/-/cheerio-0.22.28.tgz#90808aabb44fec40fa2950f4c72351e3e4eb065b"
|
||||
|
@ -1018,31 +1008,6 @@
|
|||
"@types/scheduler" "*"
|
||||
csstype "^3.0.2"
|
||||
|
||||
"@types/request-debug@^0.2.0":
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/request-debug/-/request-debug-0.2.0.tgz#d672c3aa4de4a264d9bab35ee5179f90c414683a"
|
||||
integrity sha512-m2cZMOdVSIS5U5BQjI1MB7aoqYpt8luHiOLXlqid/VOJqRL6VqQJmXoXoykw/ud7goqB9gdF/QVcurq1hXqo+w==
|
||||
dependencies:
|
||||
"@types/request" "*"
|
||||
|
||||
"@types/request-promise@^4.1.47":
|
||||
version "4.1.47"
|
||||
resolved "https://registry.yarnpkg.com/@types/request-promise/-/request-promise-4.1.47.tgz#62f58a52476ef6cfe4f38d689cb826959a33a268"
|
||||
integrity sha512-eRSZhAS8SMsrWOM8vbhxFGVZhTbWSJvaRKyufJTdIf4gscUouQvOBlfotPSPHbMR3S7kfkyKbhb1SWPmQdy3KQ==
|
||||
dependencies:
|
||||
"@types/bluebird" "*"
|
||||
"@types/request" "*"
|
||||
|
||||
"@types/request@*", "@types/request@^2.48.5":
|
||||
version "2.48.5"
|
||||
resolved "https://registry.yarnpkg.com/@types/request/-/request-2.48.5.tgz#019b8536b402069f6d11bee1b2c03e7f232937a0"
|
||||
integrity sha512-/LO7xRVnL3DxJ1WkPGDQrp4VTV1reX9RkC85mJ+Qzykj2Bdw+mG15aAfDahc76HtknjzE16SX/Yddn6MxVbmGQ==
|
||||
dependencies:
|
||||
"@types/caseless" "*"
|
||||
"@types/node" "*"
|
||||
"@types/tough-cookie" "*"
|
||||
form-data "^2.5.0"
|
||||
|
||||
"@types/scheduler@*":
|
||||
version "0.16.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.1.tgz#18845205e86ff0038517aab7a18a62a6b9f71275"
|
||||
|
@ -1107,11 +1072,6 @@
|
|||
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.7.tgz#545158342f949e8fd3bfd813224971ecddc3fac4"
|
||||
integrity sha512-0VBprVqfgFD7Ehb2vd8Lh9TG3jP98gvr8rgehQqzztZNI7o8zS8Ad4jyZneKELphpuE212D8J70LnSNQSyO6bQ==
|
||||
|
||||
"@types/tough-cookie@*":
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.0.tgz#fef1904e4668b6e5ecee60c52cc6a078ffa6697d"
|
||||
integrity sha512-I99sngh224D0M7XgW1s120zxCt3VYQ3IQsuw3P3jbq5GG4yc79+ZjyKznyOGIQrflfylLgcfekeZW/vk0yng6A==
|
||||
|
||||
"@types/trusted-types@*":
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.0.tgz#aee6e868fcef74f2b8c71614b6df81a601a42f17"
|
||||
|
@ -1911,7 +1871,7 @@ blob@^0.1.0:
|
|||
dependencies:
|
||||
esm "^3.2.25"
|
||||
|
||||
bluebird@^3.1.1, bluebird@^3.5.0:
|
||||
bluebird@^3.1.1:
|
||||
version "3.7.2"
|
||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
|
||||
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
|
||||
|
@ -3786,15 +3746,6 @@ forever-agent@~0.6.1:
|
|||
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
|
||||
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=
|
||||
|
||||
form-data@^2.5.0:
|
||||
version "2.5.1"
|
||||
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.5.1.tgz#f2cbec57b5e59e23716e128fe44d4e5dd23895f4"
|
||||
integrity sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==
|
||||
dependencies:
|
||||
asynckit "^0.4.0"
|
||||
combined-stream "^1.0.6"
|
||||
mime-types "^2.1.12"
|
||||
|
||||
form-data@^3.0.0:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
|
||||
|
@ -7076,13 +7027,6 @@ repeat-string@^1.6.1:
|
|||
resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637"
|
||||
integrity sha1-jcrkcOHIirwtYA//Sndihtp15jc=
|
||||
|
||||
request-debug@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/request-debug/-/request-debug-0.2.0.tgz#fc054ec817181b04ca41a052c136f61c48abaf78"
|
||||
integrity sha1-/AVOyBcYGwTKQaBSwTb2HEirr3g=
|
||||
dependencies:
|
||||
stringify-clone "^1.0.0"
|
||||
|
||||
request-promise-core@1.1.4:
|
||||
version "1.1.4"
|
||||
resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.4.tgz#3eedd4223208d419867b78ce815167d10593a22f"
|
||||
|
@ -7099,16 +7043,6 @@ request-promise-native@^1.0.9:
|
|||
stealthy-require "^1.1.1"
|
||||
tough-cookie "^2.3.3"
|
||||
|
||||
request-promise@^4.2.6:
|
||||
version "4.2.6"
|
||||
resolved "https://registry.yarnpkg.com/request-promise/-/request-promise-4.2.6.tgz#7e7e5b9578630e6f598e3813c0f8eb342a27f0a2"
|
||||
integrity sha512-HCHI3DJJUakkOr8fNoCc73E5nU5bqITjOYFMDrKHYOXWXrgD/SBaC7LjwuPymUprRyuF06UK7hd/lMHkmUXglQ==
|
||||
dependencies:
|
||||
bluebird "^3.5.0"
|
||||
request-promise-core "1.1.4"
|
||||
stealthy-require "^1.1.1"
|
||||
tough-cookie "^2.3.3"
|
||||
|
||||
request@^2.88.2:
|
||||
version "2.88.2"
|
||||
resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3"
|
||||
|
@ -7897,11 +7831,6 @@ string_decoder@~1.1.1:
|
|||
dependencies:
|
||||
safe-buffer "~5.1.0"
|
||||
|
||||
stringify-clone@^1.0.0:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/stringify-clone/-/stringify-clone-1.1.1.tgz#309a235fb4ecfccd7d388dbe18ba904facaf433b"
|
||||
integrity sha1-MJojX7Ts/M19OI2+GLqQT6yvQzs=
|
||||
|
||||
strip-ansi@^3.0.0, strip-ansi@^3.0.1:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf"
|
||||
|
|
Ładowanie…
Reference in New Issue