Skip to content

Commit ad5c6dd

Browse files
authored
Upgrade restify to v9.0.0 (#702)
* Upgrade restify to v9.0.0 * Update more API route handlers * Resolve streams * Update csv-stringify * Update csv stringifier * Flush headers to fix csv format * Fix liniting and add error handler * Flush headers before streaming response to client * Flush headers again
1 parent b6f4766 commit ad5c6dd

16 files changed

Lines changed: 399 additions & 281 deletions

packages/api/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const
1717
config = require('config'),
1818
{ preRequest, preCors, Honeybadger, getVersion, postToMattermost } = require('./lib/helpers/apiUtils'),
1919
routes = require('./lib/routes'),
20-
bunyan = require('bunyan');
20+
pino = require('pino');
2121

22-
const log = bunyan.createLogger({ name: 'opensensemap-api', serializers: bunyan.stdSerializers });
22+
// const log = bunyan.createLogger({ name: 'opensensemap-api', serializers: bunyan.stdSerializers });
23+
const log = pino({ name: 'opensensemap-api', sserializers: pino.stdSerializers });
2324

2425
const server = restify.createServer({
2526
name: `opensensemap-api (${getVersion})`,

packages/api/lib/controllers/boxesController.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const
132132
* @apiUse ContentTypeJSON
133133
*
134134
*/
135-
const updateBox = async function updateBox (req, res, next) {
135+
const updateBox = async function updateBox (req, res) {
136136
try {
137137
let box = await Box.findBoxById(req._userParams.boxId, { lean: false, populate: false });
138138
box = await box.updateBox(req._userParams);
@@ -143,7 +143,7 @@ const updateBox = async function updateBox (req, res, next) {
143143
res.send({ code: 'Ok', data: box.toJSON({ includeSecrets: true }) });
144144
clearCache(['getBoxes']);
145145
} catch (err) {
146-
handleError(err, next);
146+
return handleError(err);
147147
}
148148
};
149149

@@ -167,12 +167,12 @@ const updateBox = async function updateBox (req, res, next) {
167167
* { "coordinates": [7.68323, 51.9423], "type": "Point", "timestamp": "2017-07-27T12:02:00Z"}
168168
* ]
169169
*/
170-
const getBoxLocations = async function getBoxLocations (req, res, next) {
170+
const getBoxLocations = async function getBoxLocations (req, res) {
171171
try {
172172
const box = await Box.findBoxById(req._userParams.boxId, { onlyLocations: true, lean: false });
173173
res.send(await box.getLocations(req._userParams));
174174
} catch (err) {
175-
handleError(err, next);
175+
return handleError(err);
176176
}
177177
};
178178

@@ -211,7 +211,7 @@ const geoJsonStringifyReplacer = function geoJsonStringifyReplacer (key, box) {
211211
* @apiSampleRequest https://api.opensensemap.org/boxes?date=2015-03-07T02:50Z&phenomenon=Temperatur
212212
* @apiSampleRequest https://api.opensensemap.org/boxes?date=2015-03-07T02:50Z,2015-04-07T02:50Z&phenomenon=Temperatur
213213
*/
214-
const getBoxes = async function getBoxes (req, res, next) {
214+
const getBoxes = async function getBoxes (req, res) {
215215
// content-type is always application/json for this route
216216
res.header('Content-Type', 'application/json; charset=utf-8');
217217

@@ -252,7 +252,7 @@ const getBoxes = async function getBoxes (req, res, next) {
252252
})
253253
.pipe(res);
254254
} catch (err) {
255-
handleError(err, next);
255+
return handleError(err);
256256
}
257257
};
258258

@@ -357,7 +357,7 @@ const getBoxes = async function getBoxes (req, res, next) {
357357
}
358358
*/
359359

360-
const getBox = async function getBox (req, res, next) {
360+
const getBox = async function getBox (req, res) {
361361
const { format, boxId } = req._userParams;
362362

363363
try {
@@ -372,7 +372,7 @@ const getBox = async function getBox (req, res, next) {
372372
}
373373
res.send(box);
374374
} catch (err) {
375-
handleError(err, next);
375+
return handleError(err);
376376
}
377377
};
378378

@@ -406,7 +406,7 @@ const getBox = async function getBox (req, res, next) {
406406
* @apiUse ContentTypeJSON
407407
* @apiUse JWTokenAuth
408408
*/
409-
const postNewBox = async function postNewBox (req, res, next) {
409+
const postNewBox = async function postNewBox (req, res) {
410410
try {
411411
let newBox = await req.user.addBox(req._userParams);
412412
newBox = await Box.populate(newBox, Box.BOX_SUB_PROPS_FOR_POPULATION);
@@ -422,7 +422,7 @@ const postNewBox = async function postNewBox (req, res, next) {
422422
}](https://opensensemap.org/explore/${newBox._id})`
423423
);
424424
} catch (err) {
425-
handleError(err, next);
425+
return handleError(err);
426426
}
427427
};
428428

@@ -443,7 +443,7 @@ const postNewBox = async function postNewBox (req, res, next) {
443443
* @apiUse JWTokenAuth
444444
* @apiUse BoxIdParam
445445
*/
446-
const getSketch = async function getSketch (req, res, next) {
446+
const getSketch = async function getSketch (req, res) {
447447
res.header('Content-Type', 'text/plain; charset=utf-8');
448448
try {
449449
const box = await Box.findBoxById(req._userParams.boxId, { populate: false, lean: false });
@@ -468,7 +468,7 @@ const getSketch = async function getSketch (req, res, next) {
468468

469469
res.send(box.getSketch(params));
470470
} catch (err) {
471-
handleError(err, next);
471+
return handleError(err);
472472
}
473473
};
474474

@@ -482,7 +482,7 @@ const getSketch = async function getSketch (req, res, next) {
482482
* @apiUse JWTokenAuth
483483
* @apiUse BoxIdParam
484484
*/
485-
const deleteBox = async function deleteBox (req, res, next) {
485+
const deleteBox = async function deleteBox (req, res) {
486486
const { password, boxId } = req._userParams;
487487

488488
try {
@@ -493,7 +493,7 @@ const deleteBox = async function deleteBox (req, res, next) {
493493
postToMattermost(`Box deleted: ${req.user.name} (${redactEmail(req.user.email)}) just deleted "${box.name}" (${boxId})`);
494494

495495
} catch (err) {
496-
handleError(err, next);
496+
return handleError(err);
497497
}
498498
};
499499

@@ -505,15 +505,15 @@ const deleteBox = async function deleteBox (req, res, next) {
505505
* @apiUse JWTokenAuth
506506
* @apiUse BoxIdParam
507507
*/
508-
const getTransfer = async function getTransfer (req, res, next) {
508+
const getTransfer = async function getTransfer (req, res) {
509509
const { boxId } = req._userParams;
510510
try {
511511
const transfer = await Claim.findClaimByDeviceID(boxId);
512512
res.send(200, {
513513
data: transfer,
514514
});
515515
} catch (err) {
516-
handleError(err, next);
516+
return handleError(err);
517517
}
518518
};
519519

@@ -526,7 +526,7 @@ const getTransfer = async function getTransfer (req, res, next) {
526526
* @apiParam (RequestBody) {RFC3339Date} expiresAt Expiration date for transfer token (default: 24 hours from now).
527527
* @apiUse JWTokenAuth
528528
*/
529-
const createTransfer = async function createTransfer (req, res, next) {
529+
const createTransfer = async function createTransfer (req, res) {
530530
const { boxId, date } = req._userParams;
531531
try {
532532
const transferCode = await req.user.transferBox(boxId, date);
@@ -535,7 +535,7 @@ const createTransfer = async function createTransfer (req, res, next) {
535535
data: transferCode,
536536
});
537537
} catch (err) {
538-
handleError(err, next);
538+
return handleError(err);
539539
}
540540
};
541541

@@ -549,7 +549,7 @@ const createTransfer = async function createTransfer (req, res, next) {
549549
* @apiUse JWTokenAuth
550550
* @apiUse BoxIdParam
551551
*/
552-
const updateTransfer = async function updateTransfer (req, res, next) {
552+
const updateTransfer = async function updateTransfer (req, res) {
553553
const { boxId, token, date } = req._userParams;
554554
try {
555555
const transfer = await req.user.updateTransfer(boxId, token, date);
@@ -558,7 +558,7 @@ const updateTransfer = async function updateTransfer (req, res, next) {
558558
data: transfer,
559559
});
560560
} catch (err) {
561-
handleError(err, next);
561+
return handleError(err);
562562
}
563563
};
564564

@@ -571,13 +571,13 @@ const updateTransfer = async function updateTransfer (req, res, next) {
571571
* @apiParam (RequestBody) {String} token Transfer token you want to revoke.
572572
* @apiUse JWTokenAuth
573573
*/
574-
const removeTransfer = async function removeTransfer (req, res, next) {
574+
const removeTransfer = async function removeTransfer (req, res) {
575575
const { boxId, token } = req._userParams;
576576
try {
577577
await req.user.removeTransfer(boxId, token);
578578
res.send(204);
579579
} catch (err) {
580-
handleError(err, next);
580+
return handleError(err);
581581
}
582582
};
583583

@@ -590,7 +590,7 @@ const removeTransfer = async function removeTransfer (req, res, next) {
590590
* @apiParam (RequestBody) {String} token the token to claim a senseBox
591591
* @apiUse JWTokenAuth
592592
*/
593-
const claimBox = async function claimBox (req, res, next) {
593+
const claimBox = async function claimBox (req, res) {
594594
const { token } = req._userParams;
595595

596596
try {
@@ -601,7 +601,7 @@ const claimBox = async function claimBox (req, res, next) {
601601

602602
res.send(200, { message: 'Device successfully claimed!' });
603603
} catch (err) {
604-
handleError(err, next);
604+
return handleError(err);
605605
}
606606
};
607607

packages/api/lib/controllers/managementController.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { Box, User } = require('@sensebox/opensensemap-api-models'),
1010
jsonstringify = require('stringify-stream');
1111

1212

13-
const listBoxes = async function listBoxes (req, res, next) {
13+
const listBoxes = async function listBoxes (req, res) {
1414
// default format
1515
const stringifier = jsonstringify({ open: '[', close: ']' });
1616

@@ -23,11 +23,11 @@ const listBoxes = async function listBoxes (req, res, next) {
2323
})
2424
.pipe(res);
2525
} catch (err) {
26-
handleError(err, next);
26+
return handleError(err);
2727
}
2828
};
2929

30-
const listUsers = async function listUsers (req, res, next) {
30+
const listUsers = async function listUsers (req, res) {
3131
try {
3232
const users = await User.find()
3333
.then(function (users) {
@@ -40,11 +40,11 @@ const listUsers = async function listUsers (req, res, next) {
4040

4141
res.send({ code: 'Ok', users });
4242
} catch (err) {
43-
handleError(err, next);
43+
return handleError(err);
4444
}
4545
};
4646

47-
const getUser = async function getUser (req, res, next) {
47+
const getUser = async function getUser (req, res) {
4848
const { userId } = req._userParams;
4949

5050
try {
@@ -57,11 +57,11 @@ const getUser = async function getUser (req, res, next) {
5757
});
5858
res.send(user.toJSON({ includeSecrets: true }));
5959
} catch (err) {
60-
handleError(err, next);
60+
return handleError(err);
6161
}
6262
};
6363

64-
const getBox = async function getBox (req, res, next) {
64+
const getBox = async function getBox (req, res) {
6565
const { boxId } = req._userParams;
6666

6767
try {
@@ -74,11 +74,11 @@ const getBox = async function getBox (req, res, next) {
7474

7575
res.send(box);
7676
} catch (err) {
77-
handleError(err, next);
77+
return handleError(err);
7878
}
7979
};
8080

81-
const deleteBoxes = async function deleteBoxes (req, res, next) {
81+
const deleteBoxes = async function deleteBoxes (req, res) {
8282
const { boxIds } = req._userParams;
8383

8484
try {
@@ -90,11 +90,11 @@ const deleteBoxes = async function deleteBoxes (req, res, next) {
9090
}
9191
res.send({ boxIds });
9292
} catch (err) {
93-
handleError(err, next);
93+
return handleError(err);
9494
}
9595
};
9696

97-
const updateBox = async function updateBox (req, res, next) {
97+
const updateBox = async function updateBox (req, res) {
9898
try {
9999
const { owner, boxId } = req._userParams;
100100
// update owner
@@ -117,17 +117,17 @@ const updateBox = async function updateBox (req, res, next) {
117117
res.send({ code: 'Ok', data: box });
118118
clearCache(['getBoxes']);
119119
} catch (err) {
120-
handleError(err, next);
120+
return handleError(err);
121121
}
122122
};
123123

124-
const updateUser = async function updateUser (req, res, next) {
124+
const updateUser = async function updateUser (req, res) {
125125
try {
126126
const { userId } = req._userParams;
127127

128128
const user = await User.findById(userId);
129129
if (!user) {
130-
throw new NotFoundError('Box not found');
130+
return Promise.reject(new NotFoundError('Box not found'));
131131
}
132132

133133
for (const param of ['email', 'name', 'password', 'role', 'language']) {
@@ -141,11 +141,11 @@ const updateUser = async function updateUser (req, res, next) {
141141
postToMattermost(`Management Action: User updated: ${req.user.name} (${req.user.email}) just updated "${user.name}" (${user.email})`);
142142
res.send({ code: 'Ok', data: user });
143143
} catch (err) {
144-
handleError(err, next);
144+
return handleError(err);
145145
}
146146
};
147147

148-
const deleteUsers = async function deleteUsers (req, res, next) {
148+
const deleteUsers = async function deleteUsers (req, res) {
149149
try {
150150
const { userIds } = req._userParams;
151151

@@ -160,16 +160,16 @@ const deleteUsers = async function deleteUsers (req, res, next) {
160160
}
161161
res.send({ userIds });
162162
} catch (err) {
163-
handleError(err, next);
163+
return handleError(err);
164164
}
165165
};
166166

167-
const execUserAction = async function execUserAction (req, res, next) {
167+
const execUserAction = async function execUserAction (req, res) {
168168
try {
169169
const { userId, boxId, action } = req._userParams;
170170

171171
if (action === 'resendBoxMail' && !boxId) {
172-
throw new BadRequestError('Action \'resendBoxMail\' requires parameter boxId.');
172+
return Promise.reject(new BadRequestError('Action \'resendBoxMail\' requires parameter boxId.'));
173173
}
174174

175175
const user = await User.findById(userId)
@@ -179,7 +179,7 @@ const execUserAction = async function execUserAction (req, res, next) {
179179
if (action === 'resendBoxMail') {
180180
box = user.boxes.find(id => id.equals(boxId));
181181
if (!box) {
182-
throw new BadRequestError(`Box with id ${boxId} not in this user.`);
182+
return Promise.reject(new BadRequestError(`Box with id ${boxId} not in this user.`));
183183
}
184184
}
185185

@@ -203,7 +203,7 @@ const execUserAction = async function execUserAction (req, res, next) {
203203

204204
res.send({ code: 'Ok' });
205205
} catch (err) {
206-
handleError(err, next);
206+
return handleError(err);
207207
}
208208
};
209209

0 commit comments

Comments
 (0)