Skip to content

Commit a8afe3a

Browse files
committed
add auth with gh api, for rate limits
1 parent 3c2daca commit a8afe3a

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ WIP public electron update server.
1111

1212
```bash
1313
$ npm install
14-
$ npm start
14+
$ TOKEN=GHTOKEN npm start
1515
```
1616

1717
To try with an actual electron app, run:

bin/update-server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
process.title = 'update-server'
66

77
const Updates = require('..')
8+
const { TOKEN: token } = process.env
89

9-
const updates = new Updates()
10+
const updates = new Updates({ token })
1011
const server = updates.listen(3000, () => {
1112
console.log(`http://localhost:${server.address().port}`)
1213
})

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const http = require('http')
44
const fetch = require('node-fetch')
55

66
class Updates {
7+
constructor ({ token } = {}) {
8+
this.token = token
9+
}
710
listen (port, cb) {
811
if (typeof port === 'function') {
912
;[port, cb] = [undefined, port]
@@ -54,6 +57,7 @@ class Updates {
5457
async getLatest (account, repository, platform) {
5558
const url = `https://api.github.com/repos/${account}/${repository}/releases?per_page=100`
5659
const headers = { Accept: 'application/vnd.github.preview' }
60+
if (this.token) headers.Authorization = `token ${this.token}`
5761
const res = await fetch(url, { headers })
5862

5963
if (res.status === 403) {
@@ -67,6 +71,7 @@ class Updates {
6771

6872
const releases = await res.json()
6973
for (const release of releases) {
74+
if (release.draft || release.prerelease) continue
7075
for (const asset of release.assets) {
7176
if (assetPlatform(asset.name) === platform) {
7277
return {

test/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ const { test } = require('tap')
44
const fetch = require('node-fetch')
55
const Updates = require('..')
66

7+
const { TOKEN: token } = process.env
8+
79
const createServer = () =>
810
new Promise(resolve => {
9-
const updates = new Updates()
11+
const updates = new Updates({ token })
1012
const server = updates.listen(() => {
1113
resolve({
1214
server,
@@ -16,6 +18,8 @@ const createServer = () =>
1618
})
1719

1820
test('Updates', async t => {
21+
t.ok(token, 'token required')
22+
1923
const { server, address } = await createServer()
2024

2125
await t.test('Routes', async t => {
@@ -33,11 +37,15 @@ test('Updates', async t => {
3337

3438
await t.test('exists but no updates', async t => {
3539
let res = await fetch(
36-
`https://api.github.com/repos/dat-land/dat-desktop/releases?per_page=1`
40+
`https://api.github.com/repos/dat-land/dat-desktop/releases?per_page=100`,
41+
{ headers: { Authorization: `token ${token}` } }
3742
)
3843
const releases = await res.json()
44+
const release = releases.find(
45+
release => !release.draft && !release.prerelease
46+
)
3947
res = await fetch(
40-
`${address}/dat-land/dat-desktop/darwin/${releases[0].name}`
48+
`${address}/dat-land/dat-desktop/darwin/${release.name}`
4149
)
4250
t.equal(res.status, 204)
4351
})

0 commit comments

Comments
 (0)