Skip to content

Commit c074949

Browse files
wip
1 parent 33af697 commit c074949

File tree

2 files changed

+69
-176
lines changed

2 files changed

+69
-176
lines changed

server/server.js

Lines changed: 31 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
};
66
Object.defineProperty(exports, "__esModule", { value: true });
77
const fastify_1 = __importDefault(require("fastify"));
8+
const util = require('util');
89
const yargs_1 = __importDefault(require("yargs/yargs"));
910
const schema_1 = require("./schema");
1011
const OSRM = require('../lib/index.js');
@@ -13,84 +14,28 @@ var Format;
1314
Format["Json"] = "json";
1415
Format["Flatbuffers"] = "flatbuffers";
1516
})(Format || (Format = {}));
16-
// TODO: a lot of copy-paste
17-
async function tile(osrm, zxy) {
18-
const promise = new Promise((resolve, reject) => {
19-
osrm.tile(zxy, function (err, response) {
20-
if (err) {
21-
reject(err);
22-
}
23-
else {
24-
resolve(response);
25-
}
26-
});
27-
});
28-
return promise;
29-
}
30-
async function route(osrm, options) {
31-
const promise = new Promise((resolve, reject) => {
32-
osrm.route(options, function (err, response) {
33-
if (err) {
34-
reject(err);
35-
}
36-
else {
37-
resolve(response);
38-
}
39-
});
40-
});
41-
return promise;
42-
}
43-
async function nearest(osrm, options) {
44-
const promise = new Promise((resolve, reject) => {
45-
osrm.nearest(options, function (err, response) {
46-
if (err) {
47-
reject(err);
48-
}
49-
else {
50-
resolve(response);
51-
}
52-
});
53-
});
54-
return promise;
55-
}
56-
async function table(osrm, options) {
57-
const promise = new Promise((resolve, reject) => {
58-
osrm.table(options, function (err, response) {
59-
if (err) {
60-
reject(err);
61-
}
62-
else {
63-
resolve(response);
64-
}
65-
});
66-
});
67-
return promise;
68-
}
69-
async function trip(osrm, options) {
70-
const promise = new Promise((resolve, reject) => {
71-
osrm.trip(options, function (err, response) {
72-
if (err) {
73-
reject(err);
74-
}
75-
else {
76-
resolve(response);
77-
}
78-
});
79-
});
80-
return promise;
81-
}
82-
async function match(osrm, options) {
83-
const promise = new Promise((resolve, reject) => {
84-
osrm.match(options, function (err, response) {
85-
if (err) {
86-
reject(err);
87-
}
88-
else {
89-
resolve(response);
90-
}
91-
});
92-
});
93-
return promise;
17+
class OSRMWrapper {
18+
constructor(osrm) {
19+
this.osrm = osrm;
20+
}
21+
async tile(zxy) {
22+
return util.promisify(this.osrm.tile.bind(this.osrm))(zxy);
23+
}
24+
async route(options) {
25+
return util.promisify(this.osrm.route.bind(this.osrm))(options);
26+
}
27+
async nearest(options) {
28+
return util.promisify(this.osrm.nearest.bind(this.osrm))(options);
29+
}
30+
async table(options) {
31+
return util.promisify(this.osrm.table.bind(this.osrm))(options);
32+
}
33+
async trip(options) {
34+
return util.promisify(this.osrm.trip.bind(this.osrm))(options);
35+
}
36+
async match(options) {
37+
return util.promisify(this.osrm.match.bind(this.osrm))(options);
38+
}
9439
}
9540
async function handleNearest(osrm, coordinates, query, format) {
9641
const options = {
@@ -100,8 +45,7 @@ async function handleNearest(osrm, coordinates, query, format) {
10045
if (query.number !== undefined) {
10146
options.number = query.number;
10247
}
103-
const res = await nearest(osrm, options);
104-
return res;
48+
return osrm.nearest(options);
10549
}
10650
async function handleTable(osrm, coordinates, query, format) {
10751
const options = {
@@ -123,7 +67,7 @@ async function handleTable(osrm, coordinates, query, format) {
12367
if (query.destinations && query.destinations !== 'all') {
12468
options.destinations = query.destinations;
12569
}
126-
return table(osrm, options);
70+
return osrm.table(options);
12771
}
12872
function handleCommonParams(query, options, format) {
12973
options.format = format;
@@ -193,7 +137,7 @@ async function handleMatch(osrm, coordinates, query, format) {
193137
if (query.tidy) {
194138
options.tidy = query.tidy;
195139
}
196-
const res = await match(osrm, options);
140+
const res = await osrm.match(options);
197141
return res;
198142
}
199143
async function handleTrip(osrm, coordinates, query, format) {
@@ -210,7 +154,7 @@ async function handleTrip(osrm, coordinates, query, format) {
210154
if (query.destination) {
211155
options.destination = query.destination;
212156
}
213-
const res = await trip(osrm, options);
157+
const res = await osrm.trip(options);
214158
return res;
215159
}
216160
async function handleRoute(osrm, coordinates, query, format) {
@@ -227,7 +171,7 @@ async function handleRoute(osrm, coordinates, query, format) {
227171
if (query.waypoints) {
228172
options.waypoints = query.waypoints;
229173
}
230-
const res = await route(osrm, options);
174+
const res = await osrm.route(options);
231175
return res;
232176
}
233177
async function main() {
@@ -256,7 +200,7 @@ async function main() {
256200
process.stdout.write('v5.27.0\n');
257201
return;
258202
}
259-
const osrm = new OSRM({
203+
const osrm = new OSRMWrapper(new OSRM({
260204
path: argv._[0],
261205
dataset_name: argv.dataset_name,
262206
algorithm: argv.algorithm,
@@ -268,7 +212,7 @@ async function main() {
268212
max_nearest_size: argv.max_nearest_size,
269213
max_alternatives: argv.max_alternatives,
270214
max_matching_radius: argv.max_matching_size
271-
});
215+
}));
272216
const fastify = (0, fastify_1.default)({
273217
logger: true,
274218
maxParamLength: Number.MAX_SAFE_INTEGER,
@@ -332,7 +276,7 @@ async function main() {
332276
fastify.get('/tile/v1/:profile/tile(:x,:y,:zoom).mvt', { schema: schema_1.tileSchema }, async (request, reply) => {
333277
const { x, y, zoom } = request.params;
334278
reply.type('application/x-protobuf').code(200);
335-
return tile(osrm, [zoom, x, y]);
279+
return osrm.tile([zoom, x, y]);
336280
});
337281
fastify.listen({ port: argv.port, host: argv.ip }, (err, address) => {
338282
if (err) {

0 commit comments

Comments
 (0)