Skip to content

Commit 76af3a3

Browse files
committed
initial version without cache
1 parent 07af400 commit 76af3a3

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@ WIP public electron update server.
44

55
## Routes
66

7-
### `/:owner/:repo/update/:platform/:version`
8-
9-
Check for updates
10-
11-
### `/:owner/:repo/update/win32/:version/RELEASES`
12-
### `/:owner/:repo/download/:platform`
13-
14-
Download latest
7+
### `/:owner/:repo/:platform/:version`
8+
### `/:owner/:repo/win32/:version/RELEASES`
159

1610
## Development
1711

index.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use strict'
22

33
const http = require('http')
4+
const fetch = require('node-fetch')
45

56
class Updates {
6-
constructor () {
7-
this.cache = new Map()
8-
}
9-
107
listen (port, cb) {
118
const server = http.createServer((req, res) => {
129
this.handle(req, res).catch(err => {
@@ -21,41 +18,50 @@ class Updates {
2118

2219
async handle (req, res) {
2320
const segs = req.url.split('/').filter(Boolean)
24-
const [account, repository, method, platform, version] = segs
21+
const [account, repository, platform, version] = segs
22+
if (!account || !repository || !platform || !version) return notFound(res)
2523

26-
if (account && repository && method === 'update' && platform && version) {
27-
this.handleUpdate(res, account, repository, platform, version)
28-
} else if (account && repository && method === 'download' && platform) {
29-
this.handleDownload(res, account, repository, platform)
30-
} else {
31-
notFound(res)
32-
}
24+
await this.handleUpdate(res, account, repository, platform, version)
3325
}
3426

35-
handleUpdate (res, account, repository, platform, version) {
36-
const key = `${account}/${repository}/${platform}`
37-
if (this.cache.has(key)) {
38-
const latest = this.cache.get(key)
39-
if (latest.version === version) return noContent(res)
27+
async handleUpdate (res, account, repository, platform, version) {
28+
const latest = await this.getLatest(account, repository, platform)
29+
30+
if (!latest) {
31+
notFound(res)
32+
} else if (latest.version === version) {
33+
noContent(res)
34+
} else {
4035
json(res, {
4136
name: latest.version,
42-
url: `/${account}/${repository}/download/${platform}`
37+
url: latest.url
4338
})
44-
} else {
45-
if (this.cache.has(`${account / repository}`)) return notFound(res)
4639
}
4740
}
4841

49-
handleDownload (res, account, repository, platform) {
50-
const key = `${account}/${repository}/${platform}`
51-
if (this.cache.has(key)) {
52-
const latest = this.cache.get(key)
53-
redirect(res, latest.url)
54-
} else {
42+
async getLatest (account, repository, platform) {
43+
const url = `https://api.github.com/repos/${account}/${repository}/releases?per_page=100`
44+
const headers = { Accept: 'application/vnd.github.preview' }
45+
const res = await fetch(url, { headers })
46+
const releases = await res.json()
47+
const latest = {}
48+
for (const release of releases) {
49+
for (const asset of release.assets) {
50+
if (assetPlatform(asset.name) === platform) {
51+
latest.version = release.name
52+
latest.url = asset.browser_download_url
53+
return latest
54+
}
55+
}
5556
}
5657
}
5758
}
5859

60+
const assetPlatform = fileName => {
61+
if (/.*mac.*\.zip/.test(fileName)) return 'darwin'
62+
return false
63+
}
64+
5965
const notFound = res => {
6066
res.statusCode = 404
6167
res.end('Not found')
@@ -71,10 +77,4 @@ const json = (res, obj) => {
7177
res.end(JSON.stringify(obj))
7278
}
7379

74-
const redirect = (res, url) => {
75-
res.statusCode = 302
76-
res.setHeader('Location', url)
77-
res.end(url)
78-
}
79-
8080
module.exports = Updates

package-lock.json

Lines changed: 15 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"start": "nodemon bin/update-server.js",
44
"test": "prettier-standard '**/*.js' && standard"
55
},
6-
"dependencies": {},
6+
"dependencies": {
7+
"node-fetch": "^2.1.1"
8+
},
79
"devDependencies": {
810
"nodemon": "^1.17.1",
911
"prettier-standard": "^8.0.0",

0 commit comments

Comments
 (0)