mirror of
https://github.com/gromlab-ru/svg-sprites.git
synced 2026-07-22 04:40:17 +03:00
test: добавить площадку интеграционного тестирования
- добавлены consumer fixtures для React и Next.js - добавлены typecheck, production build и браузерные smoke-тесты - добавлена интеграционная проверка в CI
This commit is contained in:
45
integration/scripts/serve-static.mjs
Normal file
45
integration/scripts/serve-static.mjs
Normal file
@@ -0,0 +1,45 @@
|
||||
import fs from 'node:fs'
|
||||
import http from 'node:http'
|
||||
import path from 'node:path'
|
||||
|
||||
const [directory, portValue] = process.argv.slice(2)
|
||||
|
||||
if (!directory || !portValue) {
|
||||
throw new Error('Usage: node scripts/serve-static.mjs <directory> <port>')
|
||||
}
|
||||
|
||||
const root = path.resolve(directory)
|
||||
const mimeTypes = {
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.js': 'text/javascript; charset=utf-8',
|
||||
'.json': 'application/json; charset=utf-8',
|
||||
'.svg': 'image/svg+xml',
|
||||
}
|
||||
|
||||
const server = http.createServer((request, response) => {
|
||||
const pathname = decodeURIComponent(new URL(request.url ?? '/', 'http://localhost').pathname)
|
||||
const requestedPath = pathname.endsWith('/') ? `${pathname}index.html` : pathname
|
||||
const filePath = path.resolve(root, `.${requestedPath}`)
|
||||
|
||||
if (filePath !== root && !filePath.startsWith(`${root}${path.sep}`)) {
|
||||
response.writeHead(403).end('Forbidden')
|
||||
return
|
||||
}
|
||||
|
||||
const resolvedPath = fs.existsSync(filePath) && fs.statSync(filePath).isFile()
|
||||
? filePath
|
||||
: path.join(root, 'index.html')
|
||||
|
||||
if (!fs.existsSync(resolvedPath)) {
|
||||
response.writeHead(404).end('Not found')
|
||||
return
|
||||
}
|
||||
|
||||
response.setHeader('Content-Type', mimeTypes[path.extname(resolvedPath)] ?? 'application/octet-stream')
|
||||
fs.createReadStream(resolvedPath).pipe(response)
|
||||
})
|
||||
|
||||
server.listen(Number(portValue), '127.0.0.1', () => {
|
||||
console.log(`Serving ${root} on http://127.0.0.1:${portValue}`)
|
||||
})
|
||||
Reference in New Issue
Block a user