-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.mjs
87 lines (74 loc) · 2.26 KB
/
server.mjs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import cors from 'cors'
import express from 'express'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const app = express()
const readManifest = (path) => {
return new Promise((resolve) => {
fs.readFile(path, 'utf-8', (e, data) => {
if (e) {
console.error('An error occurred reading the file', e)
resolve({})
} else if (data) {
resolve(JSON.parse(data))
} else {
console.error('Unexpected empty buffer')
resolve({})
}
})
})
}
const render = (name, manifest, entrypoint) => {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>UI Extension: ${name}</title>
<script type="module" src="${manifest[entrypoint]}"></script>
</head>
<body></body>
</html>`
}
const records = [{
uuid: '62aa8145-ed53-4862-b28f-f1bc6b36a3a3',
name: 'Yandex maps',
scope: 'order-card',
entrypoint: 'dist/extension.js',
stylesheet: 'dist/extension.css',
}]
app.use(cors())
app.use('/dist', express.static(path.join(__dirname, '/dist')))
app.get('/', (_, response) => {
response.sendFile(path.join(__dirname, '/index.html'))
})
app.get('/extension/:uuid', async (request, response) => {
const uuid = request.params.uuid
const manifest = await readManifest(path.join(__dirname, '/dist/manifest.json'))
const record = records.find(r => r.uuid === uuid)
if (record) {
response.send(render(record.name, manifest, record.entrypoint))
} else {
response.sendStatus(404)
}
})
app.get('/extension/:uuid/stylesheet', async (request, response) => {
const uuid = request.params.uuid
const manifest = await readManifest(path.join(__dirname, '/dist/manifest.json'))
const record = records.find(r => r.uuid === uuid)
if (record) {
response.sendFile(path.join(__dirname, manifest[record.stylesheet]))
} else {
response.sendStatus(404)
}
})
const server = app.listen(3000, () => {
console.log('Serving on port 3000')
})
process.on('SIGINT', () => {
server.close(() => {
console.log('Server has been stopped')
process.exit(0)
})
})