Skip to content

Commit

Permalink
Adds serve from cache flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Beaulieu committed May 2, 2023
1 parent 94340f4 commit ef51986
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ export const config = {
account: process.env.ACCOUNT!,
repository: process.env.REPOSITORY!,
token: process.env.TOKEN,
interval: parseInt(process.env.INTERVAL ?? '5'),
interval: parseInt(process.env.INTERVAL ?? '10'),
prerelease: process.env.PRERELEASE,
password: process.env.PASSWORD,
url: process.env.URL
url: process.env.URL,
serveCache: process.env.SERVE_CACHE === 'true'
}

const cache = new ReleaseCache(config)
Expand Down
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ app.use((err: Error, req: Request, res: Response, next: NextFunction) => {

const port = process.env.PORT ?? 3000

logger.info(`Using setting: ACCOUNT: ${config.account}`)
logger.info(`Using setting: REPOSITORY: ${config.repository}`)
logger.info(`Using setting: URL: ${config.url}`)
logger.info(`Using setting: TOKEN: ${config.token}`)
logger.info(`Using setting: PRERELEASE: ${config.prerelease}`)
logger.info(`Using setting: URL: ${config.url}`)
logger.info(`Using setting: INTERVAL: ${config.interval}`)
logger.info(`Using settings`)
logger.info(`\tACCOUNT: ${config.account}`)
logger.info(`\tREPOSITORY: ${config.repository}`)
logger.info(`\tURL: ${config.url}`)
logger.info(`\tTOKEN: ${config.token}`)
logger.info(`\tSERVE_CACHE: ${config.serveCache}`)
logger.info(`\tPRERELEASE: ${config.prerelease}`)
logger.info(`\tURL: ${config.url}`)
logger.info(`\tINTERVAL: ${config.interval}`)

app.listen(port, () => logger.info(`Chestnut listening on port ${port}`))
6 changes: 3 additions & 3 deletions src/routes/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

export const downloadRouter = express.Router()

const { token } = cache.config
const { token, serveCache } = cache.config

downloadRouter.get('/', async (req, res) => {
const userAgent = parse(req.headers['user-agent'] ?? '')
Expand Down Expand Up @@ -39,7 +39,7 @@ downloadRouter.get('/', async (req, res) => {
return
}

if (shouldProxyPrivateDownload(token)) {
if (shouldProxyPrivateDownload(token, serveCache)) {
proxyPrivateDownload(platforms[platform], res, token)
return
}
Expand Down Expand Up @@ -86,7 +86,7 @@ downloadRouter.get('/:platform', async (req, res) => {
return
}

if (shouldProxyPrivateDownload(token)) {
if (shouldProxyPrivateDownload(token, serveCache)) {
proxyPrivateDownload(latest.platforms[platform], res, token)
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
shouldProxyPrivateDownload
} from '../utils/proxy'

const { token } = cache.config
const { token, serveCache } = cache.config

export const filesRouter = express.Router()

Expand All @@ -29,7 +29,7 @@ filesRouter.get('/:filename', async (req, res) => {
return
}

if (shouldProxyPrivateDownload(token)) {
if (shouldProxyPrivateDownload(token, serveCache)) {
proxyPrivateDownload(latest.files[filename], res, token)
return
}
Expand Down
13 changes: 5 additions & 8 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { logger } from './logger'
import { IFileMetadata } from './releaseCache'

export const shouldProxyPrivateDownload = (
token: string | undefined
): token is string => !!(token && typeof token === 'string' && token.length > 0)
token: string | undefined,
serveCache: boolean | undefined
): token is string =>
!!serveCache && !!(token && typeof token === 'string' && token.length > 0)

export const proxyPrivateDownload = (
file: IFileMetadata,
Expand All @@ -23,8 +25,7 @@ export const proxyPrivateDownload = (
if (cached && fs.existsSync('./tmp/' + name)) {
res.set('Content-Type', content_type)
res.set('Content-Disposition', `attachment; filename=${name}`)
const buffer = getFileFromDisk(file)
res.end(buffer)
fs.createReadStream('./tmp/' + file.name).pipe(res)
return
}

Expand Down Expand Up @@ -81,10 +82,6 @@ export const downloadFileToDisk = async (
})
}

const getFileFromDisk = (file: IFileMetadata) => {
return fs.readFileSync('./tmp/' + file.name)
}

export const clearFilesFromDisk = () => {
if (fs.existsSync('./tmp/')) fs.rmSync('./tmp/', { recursive: true })
}
Expand Down
33 changes: 22 additions & 11 deletions src/utils/releaseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import ms from 'ms'
import fetch from 'node-fetch'
import { platformForFileName } from './aliases'
import { logger } from './logger'
import { clearFilesFromDisk, downloadFileToDisk, toMB } from './proxy'
import {
clearFilesFromDisk,
downloadFileToDisk,
shouldProxyPrivateDownload,
toMB
} from './proxy'

export interface IConfig {
account: string
Expand All @@ -13,6 +18,7 @@ export interface IConfig {
prerelease?: string
url?: string
password?: string
serveCache?: boolean
}
export interface ILatest {
pub_date?: string
Expand Down Expand Up @@ -87,23 +93,25 @@ export class ReleaseCache {
this.latest = {}
this.lastUpdate = null

// populate cache at startup
this.loadCache()
}

shouldProxyPrivateDownload = () => {
getToken = () => {
const { token } = this.config
return token && typeof token === 'string' && token.length > 0
}

refreshCache = async (force = false) => {
logger.info('Checking GitHub for latest release...')
const { account, repository, prerelease, token } = this.config
const { account, repository, prerelease, token, serveCache } = this.config
const repo = account + '/' + repository
const url = `https://api.github.com/repos/${repo}/releases?per_page=100`
const headers: HeadersInit = { Accept: 'application/vnd.github.preview' }
const headers: HeadersInit = {
Accept: 'application/vnd.github.preview',
'User-Agent': `${repo} update server`
}

if (token && typeof token === 'string' && token.length > 0) {
if (this.getToken()) {
headers.Authorization = `token ${token}`
}

Expand All @@ -114,6 +122,9 @@ export class ReleaseCache {

response = await retry(
async () => {
if (this.getToken())
logger.warn('Calling github API using token. Affects rate limit.')

const res = await fetch(url, { headers })
if (res.status !== 200) {
logger.error(
Expand Down Expand Up @@ -185,13 +196,13 @@ export class ReleaseCache {
size: toMB(size)
}

if (token) {
const downloadProm = downloadFileToDisk(metadata, this.config.token)
downloadProm.then(() => {
if (shouldProxyPrivateDownload(token, serveCache)) {
const downloadPromise = downloadFileToDisk(metadata, token)
downloadPromise.then(() => {
metadata.cached = true
logger.info(`${name} is cached`)
})
downloadPromises.push(downloadProm)
downloadPromises.push(downloadPromise)
}

this.latest.files[name] = metadata
Expand All @@ -204,7 +215,7 @@ export class ReleaseCache {
this.latest.platforms[platform] = metadata
}

if (token) {
if (shouldProxyPrivateDownload(token, serveCache)) {
Promise.all(downloadPromises).then(() => {
logger.info(`✅ Finished downloading ${downloadPromises.length} files`)
})
Expand Down

0 comments on commit ef51986

Please sign in to comment.