Skip to content

Commit

Permalink
Upgrade restify to v9.0.0 (#702)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mpfeil authored Jan 12, 2023
1 parent b6f4766 commit ad5c6dd
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 281 deletions.
5 changes: 3 additions & 2 deletions packages/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const
config = require('config'),
{ preRequest, preCors, Honeybadger, getVersion, postToMattermost } = require('./lib/helpers/apiUtils'),
routes = require('./lib/routes'),
bunyan = require('bunyan');
pino = require('pino');

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

const server = restify.createServer({
name: `opensensemap-api (${getVersion})`,
Expand Down
48 changes: 24 additions & 24 deletions packages/api/lib/controllers/boxesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const
* @apiUse ContentTypeJSON
*
*/
const updateBox = async function updateBox (req, res, next) {
const updateBox = async function updateBox (req, res) {
try {
let box = await Box.findBoxById(req._userParams.boxId, { lean: false, populate: false });
box = await box.updateBox(req._userParams);
Expand All @@ -143,7 +143,7 @@ const updateBox = async function updateBox (req, res, next) {
res.send({ code: 'Ok', data: box.toJSON({ includeSecrets: true }) });
clearCache(['getBoxes']);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

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

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

Expand Down Expand Up @@ -252,7 +252,7 @@ const getBoxes = async function getBoxes (req, res, next) {
})
.pipe(res);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand Down Expand Up @@ -357,7 +357,7 @@ const getBoxes = async function getBoxes (req, res, next) {
}
*/

const getBox = async function getBox (req, res, next) {
const getBox = async function getBox (req, res) {
const { format, boxId } = req._userParams;

try {
Expand All @@ -372,7 +372,7 @@ const getBox = async function getBox (req, res, next) {
}
res.send(box);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand Down Expand Up @@ -406,7 +406,7 @@ const getBox = async function getBox (req, res, next) {
* @apiUse ContentTypeJSON
* @apiUse JWTokenAuth
*/
const postNewBox = async function postNewBox (req, res, next) {
const postNewBox = async function postNewBox (req, res) {
try {
let newBox = await req.user.addBox(req._userParams);
newBox = await Box.populate(newBox, Box.BOX_SUB_PROPS_FOR_POPULATION);
Expand All @@ -422,7 +422,7 @@ const postNewBox = async function postNewBox (req, res, next) {
}](https://opensensemap.org/explore/${newBox._id})`
);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

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

res.send(box.getSketch(params));
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand All @@ -482,7 +482,7 @@ const getSketch = async function getSketch (req, res, next) {
* @apiUse JWTokenAuth
* @apiUse BoxIdParam
*/
const deleteBox = async function deleteBox (req, res, next) {
const deleteBox = async function deleteBox (req, res) {
const { password, boxId } = req._userParams;

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

} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand All @@ -505,15 +505,15 @@ const deleteBox = async function deleteBox (req, res, next) {
* @apiUse JWTokenAuth
* @apiUse BoxIdParam
*/
const getTransfer = async function getTransfer (req, res, next) {
const getTransfer = async function getTransfer (req, res) {
const { boxId } = req._userParams;
try {
const transfer = await Claim.findClaimByDeviceID(boxId);
res.send(200, {
data: transfer,
});
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand All @@ -526,7 +526,7 @@ const getTransfer = async function getTransfer (req, res, next) {
* @apiParam (RequestBody) {RFC3339Date} expiresAt Expiration date for transfer token (default: 24 hours from now).
* @apiUse JWTokenAuth
*/
const createTransfer = async function createTransfer (req, res, next) {
const createTransfer = async function createTransfer (req, res) {
const { boxId, date } = req._userParams;
try {
const transferCode = await req.user.transferBox(boxId, date);
Expand All @@ -535,7 +535,7 @@ const createTransfer = async function createTransfer (req, res, next) {
data: transferCode,
});
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand All @@ -549,7 +549,7 @@ const createTransfer = async function createTransfer (req, res, next) {
* @apiUse JWTokenAuth
* @apiUse BoxIdParam
*/
const updateTransfer = async function updateTransfer (req, res, next) {
const updateTransfer = async function updateTransfer (req, res) {
const { boxId, token, date } = req._userParams;
try {
const transfer = await req.user.updateTransfer(boxId, token, date);
Expand All @@ -558,7 +558,7 @@ const updateTransfer = async function updateTransfer (req, res, next) {
data: transfer,
});
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand All @@ -571,13 +571,13 @@ const updateTransfer = async function updateTransfer (req, res, next) {
* @apiParam (RequestBody) {String} token Transfer token you want to revoke.
* @apiUse JWTokenAuth
*/
const removeTransfer = async function removeTransfer (req, res, next) {
const removeTransfer = async function removeTransfer (req, res) {
const { boxId, token } = req._userParams;
try {
await req.user.removeTransfer(boxId, token);
res.send(204);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

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

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

res.send(200, { message: 'Device successfully claimed!' });
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand Down
42 changes: 21 additions & 21 deletions packages/api/lib/controllers/managementController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { Box, User } = require('@sensebox/opensensemap-api-models'),
jsonstringify = require('stringify-stream');


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

Expand All @@ -23,11 +23,11 @@ const listBoxes = async function listBoxes (req, res, next) {
})
.pipe(res);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const listUsers = async function listUsers (req, res, next) {
const listUsers = async function listUsers (req, res) {
try {
const users = await User.find()
.then(function (users) {
Expand All @@ -40,11 +40,11 @@ const listUsers = async function listUsers (req, res, next) {

res.send({ code: 'Ok', users });
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const getUser = async function getUser (req, res, next) {
const getUser = async function getUser (req, res) {
const { userId } = req._userParams;

try {
Expand All @@ -57,11 +57,11 @@ const getUser = async function getUser (req, res, next) {
});
res.send(user.toJSON({ includeSecrets: true }));
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const getBox = async function getBox (req, res, next) {
const getBox = async function getBox (req, res) {
const { boxId } = req._userParams;

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

res.send(box);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const deleteBoxes = async function deleteBoxes (req, res, next) {
const deleteBoxes = async function deleteBoxes (req, res) {
const { boxIds } = req._userParams;

try {
Expand All @@ -90,11 +90,11 @@ const deleteBoxes = async function deleteBoxes (req, res, next) {
}
res.send({ boxIds });
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const updateBox = async function updateBox (req, res, next) {
const updateBox = async function updateBox (req, res) {
try {
const { owner, boxId } = req._userParams;
// update owner
Expand All @@ -117,17 +117,17 @@ const updateBox = async function updateBox (req, res, next) {
res.send({ code: 'Ok', data: box });
clearCache(['getBoxes']);
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const updateUser = async function updateUser (req, res, next) {
const updateUser = async function updateUser (req, res) {
try {
const { userId } = req._userParams;

const user = await User.findById(userId);
if (!user) {
throw new NotFoundError('Box not found');
return Promise.reject(new NotFoundError('Box not found'));
}

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

const deleteUsers = async function deleteUsers (req, res, next) {
const deleteUsers = async function deleteUsers (req, res) {
try {
const { userIds } = req._userParams;

Expand All @@ -160,16 +160,16 @@ const deleteUsers = async function deleteUsers (req, res, next) {
}
res.send({ userIds });
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

const execUserAction = async function execUserAction (req, res, next) {
const execUserAction = async function execUserAction (req, res) {
try {
const { userId, boxId, action } = req._userParams;

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

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

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

res.send({ code: 'Ok' });
} catch (err) {
handleError(err, next);
return handleError(err);
}
};

Expand Down
Loading

0 comments on commit ad5c6dd

Please sign in to comment.