Proxy badge lookup for shields.io
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

45 righe
1.0 KiB

  1. const axios = require('axios')
  2. const dotenv = require('dotenv')
  3. const http = require('http')
  4. const getBadgeUrl = require('./getBadgeUrl')
  5. dotenv.config()
  6. const server = http.createServer(async (req, res) => {
  7. const { searchParams: rawSearchParams, } = new URL(req.url, process.env.ORIGIN)
  8. const searchParams = Array
  9. .from(rawSearchParams.entries())
  10. .reduce(
  11. (obj, [key, value, ]) => ({
  12. ...obj,
  13. [key]: value,
  14. }),
  15. {}
  16. )
  17. const badgeUrl = await getBadgeUrl(searchParams)
  18. if (badgeUrl === null) {
  19. console.log(JSON.stringify(searchParams), 404)
  20. res.writeHead(404, {})
  21. res.write('No badge available.')
  22. res.end()
  23. return
  24. }
  25. const response = await axios({
  26. method: 'GET',
  27. url: badgeUrl.toString(),
  28. responseType: 'stream'
  29. })
  30. res.writeHead(200, { 'content-type': 'image/svg+xml', })
  31. response.data.on('finish', () => {
  32. console.log(JSON.stringify(searchParams), 200)
  33. res.end()
  34. })
  35. response.data.pipe(res)
  36. })
  37. server.listen(process.env.PORT || 2048)