27 lines
811 B
JavaScript
Executable File
27 lines
811 B
JavaScript
Executable File
'use strict'
|
|
|
|
const http = require('http')
|
|
const https = require('https')
|
|
const { URL } = require('url')
|
|
|
|
/** Streams the given remote image URL through to the response (CORS-friendly). */
|
|
function proxyArtwork(url, res) {
|
|
if (!url) { res.writeHead(404); res.end(); return }
|
|
let parsed
|
|
try { parsed = new URL(url) } catch { res.writeHead(400); res.end(); return }
|
|
|
|
const lib = parsed.protocol === 'https:' ? https : http
|
|
lib.get(url, (upstream) => {
|
|
res.writeHead(upstream.statusCode ?? 200, {
|
|
'Content-Type': upstream.headers['content-type'] || 'image/jpeg',
|
|
'Cache-Control': 'public, max-age=60',
|
|
'Access-Control-Allow-Origin': '*',
|
|
})
|
|
upstream.pipe(res)
|
|
}).on('error', () => {
|
|
try { res.writeHead(502); res.end() } catch { /* already sent */ }
|
|
})
|
|
}
|
|
|
|
module.exports = { proxyArtwork }
|