UNPKG

1.96 kBJavaScriptView Raw
1'use strict'
2
3const { $proxifiedUrls } = require('./symbols')
4
5const send = (response, obj) => {
6 let json
7 if (typeof obj !== 'string') {
8 json = JSON.stringify(obj, undefined, 2)
9 } else {
10 json = obj
11 }
12 const length = (new TextEncoder().encode(json)).length
13 response.writeHead(200, {
14 'Content-Type': 'application/json',
15 'Content-Length': length
16 })
17 response.end(json)
18}
19
20const notFound = response => {
21 response.writeHead(404)
22 response.end()
23}
24
25module.exports = {
26 async getJobProgress (job, request, response, pageId, testId) {
27 if (pageId) {
28 const url = Object.keys(job.qunitPages).find(pageUrl => job.qunitPages[pageUrl].id === pageId)
29 if (!url) {
30 return notFound(response)
31 }
32 const qunitPage = { url, ...job.qunitPages[url] }
33 if (!testId) {
34 return send(response, JSON.stringify(qunitPage, (key, value) => {
35 if (key === 'logs') {
36 return undefined
37 }
38 return value
39 }, 2))
40 }
41 let test
42 let moduleName
43 qunitPage.modules.every(module => module.tests.every(candidate => {
44 if (candidate.testId === testId) {
45 moduleName = module.name
46 test = candidate
47 return false
48 }
49 return true
50 }))
51 if (!test) {
52 return notFound(response)
53 }
54 return send(response, {
55 url,
56 pageId,
57 module: moduleName,
58 ...test
59 })
60 }
61 send(response, JSON.stringify({
62 ...job,
63 status: job.status
64 }, (key, value) => {
65 if (key === 'qunitPages' && job[$proxifiedUrls]) {
66 const unproxifiedQunitPages = {}
67 for (const url of Object.keys(job.qunitPages)) {
68 unproxifiedQunitPages[job[$proxifiedUrls][url] || url] = job.qunitPages[url]
69 }
70 return unproxifiedQunitPages
71 }
72 if (key === 'modules') {
73 return undefined
74 }
75 return value
76 }, 2))
77 }
78}