@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
} ;
6
6
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
7
7
const fastify_1 = __importDefault ( require ( "fastify" ) ) ;
8
+ const util = require ( 'util' ) ;
8
9
const yargs_1 = __importDefault ( require ( "yargs/yargs" ) ) ;
9
10
const schema_1 = require ( "./schema" ) ;
10
11
const OSRM = require ( '../lib/index.js' ) ;
@@ -13,84 +14,28 @@ var Format;
13
14
Format [ "Json" ] = "json" ;
14
15
Format [ "Flatbuffers" ] = "flatbuffers" ;
15
16
} ) ( 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
+ }
94
39
}
95
40
async function handleNearest ( osrm , coordinates , query , format ) {
96
41
const options = {
@@ -100,8 +45,7 @@ async function handleNearest(osrm, coordinates, query, format) {
100
45
if ( query . number !== undefined ) {
101
46
options . number = query . number ;
102
47
}
103
- const res = await nearest ( osrm , options ) ;
104
- return res ;
48
+ return osrm . nearest ( options ) ;
105
49
}
106
50
async function handleTable ( osrm , coordinates , query , format ) {
107
51
const options = {
@@ -123,7 +67,7 @@ async function handleTable(osrm, coordinates, query, format) {
123
67
if ( query . destinations && query . destinations !== 'all' ) {
124
68
options . destinations = query . destinations ;
125
69
}
126
- return table ( osrm , options ) ;
70
+ return osrm . table ( options ) ;
127
71
}
128
72
function handleCommonParams ( query , options , format ) {
129
73
options . format = format ;
@@ -193,7 +137,7 @@ async function handleMatch(osrm, coordinates, query, format) {
193
137
if ( query . tidy ) {
194
138
options . tidy = query . tidy ;
195
139
}
196
- const res = await match ( osrm , options ) ;
140
+ const res = await osrm . match ( options ) ;
197
141
return res ;
198
142
}
199
143
async function handleTrip ( osrm , coordinates , query , format ) {
@@ -210,7 +154,7 @@ async function handleTrip(osrm, coordinates, query, format) {
210
154
if ( query . destination ) {
211
155
options . destination = query . destination ;
212
156
}
213
- const res = await trip ( osrm , options ) ;
157
+ const res = await osrm . trip ( options ) ;
214
158
return res ;
215
159
}
216
160
async function handleRoute ( osrm , coordinates , query , format ) {
@@ -227,7 +171,7 @@ async function handleRoute(osrm, coordinates, query, format) {
227
171
if ( query . waypoints ) {
228
172
options . waypoints = query . waypoints ;
229
173
}
230
- const res = await route ( osrm , options ) ;
174
+ const res = await osrm . route ( options ) ;
231
175
return res ;
232
176
}
233
177
async function main ( ) {
@@ -256,7 +200,7 @@ async function main() {
256
200
process . stdout . write ( 'v5.27.0\n' ) ;
257
201
return ;
258
202
}
259
- const osrm = new OSRM ( {
203
+ const osrm = new OSRMWrapper ( new OSRM ( {
260
204
path : argv . _ [ 0 ] ,
261
205
dataset_name : argv . dataset_name ,
262
206
algorithm : argv . algorithm ,
@@ -268,7 +212,7 @@ async function main() {
268
212
max_nearest_size : argv . max_nearest_size ,
269
213
max_alternatives : argv . max_alternatives ,
270
214
max_matching_radius : argv . max_matching_size
271
- } ) ;
215
+ } ) ) ;
272
216
const fastify = ( 0 , fastify_1 . default ) ( {
273
217
logger : true ,
274
218
maxParamLength : Number . MAX_SAFE_INTEGER ,
@@ -332,7 +276,7 @@ async function main() {
332
276
fastify . get ( '/tile/v1/:profile/tile(:x,:y,:zoom).mvt' , { schema : schema_1 . tileSchema } , async ( request , reply ) => {
333
277
const { x, y, zoom } = request . params ;
334
278
reply . type ( 'application/x-protobuf' ) . code ( 200 ) ;
335
- return tile ( osrm , [ zoom , x , y ] ) ;
279
+ return osrm . tile ( [ zoom , x , y ] ) ;
336
280
} ) ;
337
281
fastify . listen ( { port : argv . port , host : argv . ip } , ( err , address ) => {
338
282
if ( err ) {
0 commit comments