Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust logic for two artifacts #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.pnp.*

.vercel
1 change: 0 additions & 1 deletion .nowignore

This file was deleted.

46 changes: 46 additions & 0 deletions api/[arch].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {NowRequest, NowResponse} from '@now/node'
import got from 'got';

type Arch = 'arm64' | 'x64';

interface Asset {
name: string
browser_download_url: string
}

export default async (req: NowRequest, res: NowResponse) => {
const {arch} = req.query as { arch: Arch };

if (arch !== 'arm64' && arch !== 'x64') {
return res.status(400).send(`Unrecognized arch "${arch}". Expected x64 or arm64.`);
}

const {body} = await got.get<{assets?: Asset[]}>('https://api.github.com/repos/wulkano/kap/releases/latest', {
responseType: 'json'
});

const assets = body.assets ?? [];

const dmgArtifact = arch === 'arm64' ? arm64Asset(assets) : x64Asset(assets);
const dmgLink = dmgArtifact?.browser_download_url;

if (dmgLink) {
res.writeHead(302, {
Location: dmgLink
});
res.end();
} else {
res.status(404);
res.send('Artifact not found');
}
}

function arm64Asset(assets: Asset[]) {
return assets.find(asset => asset.name.endsWith('arm64.dmg'));
}

function x64Asset(assets: Asset[]) {
return assets.find(asset => asset.name.endsWith('x64.dmg')) ??
// Compatibility with odler assets, when x64 was just named `Kap-x.x.x.dmg`
assets.find(asset => asset.name.endsWith('.dmg') && !asset.name.endsWith('arm64.dmg'))
}
23 changes: 4 additions & 19 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import {NowRequest, NowResponse} from '@now/node'
import got from 'got';

export default async (req: NowRequest, res: NowResponse) => {
const {body} = await got.get<any>('https://api.github.com/repos/wulkano/kap/releases/latest', {
responseType: 'json'
});

const dmgArtifact = (body.assets || []).find(asset => asset.name.includes('dmg'));
const dmgLink = dmgArtifact && dmgArtifact.browser_download_url;

if (dmgLink) {
res.writeHead(302, {
Location: dmgLink
});
res.end();
} else {
res.status(404);
res.send('Artifact not found');
}
}
export default (req: NowRequest, res: NowResponse) => {
res.setHeader('Cache-Control', 'public, max-age=31536000');
return res.status(410).send('Use /api/x64 or /api/arm64 instead');
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"got": "^11.0.2"
},
"devDependencies": {
"@now/node": "^1.5.1",
"@now/node": "^1.8.5",
"@types/got": "^9.6.10",
"@types/node": "^13.13.2",
"typescript": "^3.8.3"
Expand Down
Loading