🎉 Initial commit

ft/strip-markdown
KaKi87 3 months ago
commit 44dfc811dd

3
.gitignore vendored

@ -0,0 +1,3 @@
.idea
data
config.js

@ -0,0 +1,100 @@
import axios from 'https://cdn.jsdelivr.net/npm/axios@1.1.3/dist/esm/axios.min.js';
import axiosFetchAdapter from 'https://jspm.dev/@vespaiach/axios-fetch-adapter@0.3.1';
import pogo from 'https://deno.land/x/pogo@v0.5.2/main.ts';
import {
createServerHelper
} from 'https://git.kaki87.net/KaKi87/pogo-helpers/raw/commit/c09bb256d310dad935c9406c322a896b7898fa66/mod.js';
import Joi from 'https://jspm.dev/joi@17.6.1';
import * as semver from 'https://deno.land/std@0.161.0/semver/mod.ts';
import {
port
} from './config.js';
axios.defaults.adapter = axiosFetchAdapter;
const
server = pogo.server({
port
}),
{
createRoute
} = createServerHelper(
server,
{
isCors: true,
onError: error => console.error(error)
}
);
createRoute({
method: 'GET',
path: '/update/{owner}/{repo}/{target}/{currentVersion}',
schema: Joi.object({
path: Joi.object({
owner: Joi.string().required(),
repo: Joi.string().required(),
target: Joi.string().required().valid('linux'),
currentVersion: Joi.string().required().custom((value, helpers) => semver.valid(value) ? value : helpers.error('any.invalid'))
}),
params: Joi.object()
}),
handler: async (
request,
h,
{
path,
response
}
) => {
const
{
owner,
repo,
target,
currentVersion
} = path,
{
[owner]: {
[repo]: {
host
} = {}
} = {}
} = JSON.parse(await Deno.readTextFile('./data/repos.json') || '{}');
if(host){
const
assetNameSuffix = {
'linux': '_amd64.AppImage.tar.gz',
'windows': '_x64.msi.zip'
}[target],
getSignatureAsset = release => release['assets'].find(asset => asset.name.endsWith(`${assetNameSuffix}.sig`)),
getTargetAsset = release => release['assets'].find(asset => asset.name.endsWith(assetNameSuffix)),
release = (await axios({
baseURL: `https://${host}/api/v1`,
url: `/repos/${owner}/${repo}/releases`
})).data.find(release =>
semver.gt(release['tag_name'], currentVersion)
&&
getSignatureAsset(release)
&&
getTargetAsset(release)
);
if(release){
response.body = {
'url': getTargetAsset(release)['browser_download_url'],
'version': release['tag_name'],
'notes': release['body'],
'pub_date': release['published_at'],
'signature': (await axios(getSignatureAsset(release)['browser_download_url'])).data
};
}
else
response.code(204);
}
else
response.code(404);
return response;
}
});
server.start();
Loading…
Cancel
Save