Files
cloudron-box/src/routes/updater.js
Girish Ramakrishnan 12e073e8cf use node: prefix for requires
mostly because code is being autogenerated by all the AI stuff using
this prefix. it's also used in the stack trace.
2025-08-14 12:55:35 +05:30

66 lines
2.3 KiB
JavaScript

'use strict';
exports = module.exports = {
getAutoupdatePattern,
setAutoupdatePattern,
getBoxUpdate,
checkBoxUpdate,
updateBox,
};
const assert = require('node:assert'),
AuditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
HttpError = require('@cloudron/connect-lastmile').HttpError,
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
safe = require('safetydance'),
updater = require('../updater.js');
async function getAutoupdatePattern(req, res, next) {
const [error, pattern] = await safe(updater.getAutoupdatePattern());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { pattern }));
}
async function setAutoupdatePattern(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
const [error] = await safe(updater.setAutoupdatePattern(req.body.pattern));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function updateBox(req, res, next) {
if ('skipBackup' in req.body && typeof req.body.skipBackup !== 'boolean') return next(new HttpError(400, 'skipBackup must be a boolean'));
// this only initiates the update, progress can be checked via the progress route
const [error, taskId] = await safe(updater.startBoxUpdateTask(req.body, AuditSource.fromRequest(req)));
if (error && error.reason === BoxError.NOT_FOUND) return next(new HttpError(422, error.message));
if (error && error.reason === BoxError.BAD_STATE) return next(new HttpError(409, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, { taskId }));
}
async function getBoxUpdate(req, res, next) {
const [error, boxUpdateInfo] = await safe(updater.getBoxUpdate());
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, { update: boxUpdateInfo }));
}
async function checkBoxUpdate(req, res, next) {
// it can take a while sometimes to get all the app updates one by one
req.clearTimeout();
const [error, result ] = await safe(updater.checkBoxUpdate({ stableOnly: false }));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { update: result }));
}