40 lines
1.7 KiB
JavaScript
40 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
exports = module.exports = {
|
|
getAppVersion
|
|
};
|
|
|
|
const assert = require('node:assert'),
|
|
BoxError = require('./boxerror.js'),
|
|
manifestFormat = require('@cloudron/manifest-format'),
|
|
safe = require('safetydance'),
|
|
superagent = require('@cloudron/superagent');
|
|
|
|
async function getAppVersion(url, version) {
|
|
assert.strictEqual(typeof url, 'string');
|
|
assert.strictEqual(typeof version, 'string');
|
|
|
|
if (!url.startsWith('https://')) throw new BoxError(BoxError.BAD_FIELD, 'URL must use HTTPS');
|
|
|
|
const [error, response] = await safe(superagent.get(url).timeout(60 * 1000).ok(() => true));
|
|
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
|
if (response.status === 404) throw new BoxError(BoxError.NOT_FOUND, 'CloudronVersions.json not found');
|
|
if (response.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Fetch failed: ${response.status}`);
|
|
|
|
const versions = response.body;
|
|
if (!versions || typeof versions !== 'object') throw new BoxError(BoxError.EXTERNAL_ERROR, 'Invalid CloudronVersions.json format');
|
|
|
|
const sortedVersions = Object.keys(versions).sort(manifestFormat.packageVersionCompare);
|
|
const versionData = version === 'latest' ? versions[sortedVersions.at(-1)] : versions[version];
|
|
if (!versionData) throw new BoxError(BoxError.NOT_FOUND, `Version ${version} not found`);
|
|
|
|
const manifestError = manifestFormat.checkVersionsRequirements(versionData.manifest);
|
|
if (manifestError) throw new BoxError(BoxError.BAD_FIELD, `Invalid manifest: ${manifestError.message}`);
|
|
|
|
return {
|
|
id: versionData.manifest.id,
|
|
iconUrl: versionData.manifest.iconUrl,
|
|
...versionData // { manifest, publishState, creationDate, ts }
|
|
};
|
|
}
|