File size: 1,682 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import express from 'express'
import path from 'node:path'
import { fileURLToPath } from 'url'
import { compileFile } from '../../compiler/index.js'
import variantsFile from '../../compiler/variants.json' with { type: 'json' }

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const isMainModule = fileURLToPath(import.meta.url) === process.argv[1]

const app = express()
const LOCAL_SERVER_PORT = 3000
const FIXTURES_PATH = path.join(__dirname, '/../fixtures')
const VARIANTS = variantsFile.legacyVariants.concat(variantsFile.manualVariants)

export const LOCAL_SERVER_ADDR = `http://localhost:${LOCAL_SERVER_PORT}`

export function runLocalFileServer() {
  app.use(express.static(FIXTURES_PATH))

  app.get('/tracker/js/*', async (req, res) => {
    const name = req.params[0]
    const variant = VARIANTS.find((variant) => variant.name === name)
    if (!variant) {
      res
        .type('application/javascript')
        .status(404)
        .send({ error: new Error(`Variant not found with name ${name}`) })
    } else {
      let code = await compileFile(variant, { returnCode: true })

      if (name === 'plausible-web.js') {
        code = code.replace('"<%= @config_js %>"', req.query.script_config)
      }

      res.type('application/javascript').send(code)
    }
  })

  // A test utility - serve an image with an artificial delay
  app.get('/img/slow-image', (_req, res) => {
    setTimeout(() => {
      res.sendFile(path.join(FIXTURES_PATH, '/img/black3x3000.png'))
    }, 100)
  })

  app.listen(LOCAL_SERVER_PORT, function () {
    console.log(`Local server listening on ${LOCAL_SERVER_ADDR}`)
  })
}

if (isMainModule) {
  runLocalFileServer()
}