mockaton 10.5.1 → 10.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Api.js +5 -19
- package/src/ApiCommander.js +1 -7
- package/src/DashboardHtml.js +37 -12
- package/src/utils/http-response.js +3 -1
package/package.json
CHANGED
package/src/Api.js
CHANGED
|
@@ -4,29 +4,21 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { join } from 'node:path'
|
|
7
|
-
import { randomBytes } from 'node:crypto'
|
|
8
7
|
|
|
9
8
|
import { cookie } from './cookie.js'
|
|
10
|
-
import DashboardHtml from './DashboardHtml.js'
|
|
11
9
|
import { parseJSON } from './utils/http-request.js'
|
|
12
10
|
import { uiSyncVersion } from './Watcher.js'
|
|
13
11
|
import * as staticCollection from './staticCollection.js'
|
|
14
12
|
import * as mockBrokersCollection from './mockBrokersCollection.js'
|
|
15
13
|
import { config, ConfigValidator } from './config.js'
|
|
14
|
+
import { LinkHeader, DashboardHtml, CSP, dashboardAssets } from './DashboardHtml.js'
|
|
16
15
|
import { DF, API, LONG_POLL_SERVER_TIMEOUT } from './ApiConstants.js'
|
|
17
16
|
import { sendOK, sendJSON, sendUnprocessableContent, sendFile, sendHTML } from './utils/http-response.js'
|
|
18
17
|
|
|
19
18
|
|
|
20
19
|
export const apiGetRequests = new Map([
|
|
21
|
-
[API.dashboard,
|
|
22
|
-
...[
|
|
23
|
-
'/Dashboard.css',
|
|
24
|
-
'/Dashboard.js',
|
|
25
|
-
'/ApiConstants.js',
|
|
26
|
-
'/ApiCommander.js',
|
|
27
|
-
'/Filename.js',
|
|
28
|
-
'/Logo.svg'
|
|
29
|
-
].map(f => [API.dashboard + f, serveStatic(f)]),
|
|
20
|
+
[API.dashboard, serveDashboard],
|
|
21
|
+
...dashboardAssets.map(f => [API.dashboard + f, serveStatic(f)]),
|
|
30
22
|
|
|
31
23
|
[API.state, getState],
|
|
32
24
|
[API.syncVersion, longPollClientSyncVersion],
|
|
@@ -52,14 +44,8 @@ export const apiPatchRequests = new Map([
|
|
|
52
44
|
|
|
53
45
|
/** # GET */
|
|
54
46
|
|
|
55
|
-
function
|
|
56
|
-
|
|
57
|
-
response.setHeader('Content-Security-Policy', [
|
|
58
|
-
`default-src 'self'`,
|
|
59
|
-
`img-src data: blob: 'self'`,
|
|
60
|
-
`script-src 'nonce-${nonce}' 'self'`
|
|
61
|
-
].join(';'))
|
|
62
|
-
sendHTML(response, DashboardHtml(nonce))
|
|
47
|
+
function serveDashboard(_, response) {
|
|
48
|
+
sendHTML(response, DashboardHtml, CSP, LinkHeader)
|
|
63
49
|
}
|
|
64
50
|
|
|
65
51
|
function serveStatic(f) {
|
package/src/ApiCommander.js
CHANGED
|
@@ -17,13 +17,7 @@ export class Commander {
|
|
|
17
17
|
|
|
18
18
|
/** @returns {JsonPromise<State>} */
|
|
19
19
|
getState() {
|
|
20
|
-
|
|
21
|
-
if (globalThis._aotFetch?.[url]) { // github.com/ericfortis/aot-fetch-demo
|
|
22
|
-
const promise = globalThis._aotFetch[url]
|
|
23
|
-
delete globalThis._aotFetch[url]
|
|
24
|
-
return promise
|
|
25
|
-
}
|
|
26
|
-
return fetch(url)
|
|
20
|
+
return fetch(this.#addr + API.state)
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
/** @returns {JsonPromise<number>} */
|
package/src/DashboardHtml.js
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { API } from './ApiConstants.js'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const DashboardHtml = `<!DOCTYPE html>
|
|
3
5
|
<html lang="en-US">
|
|
4
6
|
<head>
|
|
5
7
|
<meta charset="UTF-8">
|
|
6
|
-
|
|
7
|
-
<script nonce="${nonce}">
|
|
8
|
-
// Fetches critical API data ahead-of-time.
|
|
9
|
-
// It’s before loading the stylesheet because CSS blocks.
|
|
10
|
-
// https://github.com/ericfortis/aot-fetch-demo
|
|
11
|
-
const initialState = location.origin + '/mockaton/state'
|
|
12
|
-
window._aotFetch = { [initialState]: fetch(initialState) }
|
|
13
|
-
</script>
|
|
14
|
-
|
|
15
8
|
<link rel="stylesheet" href="./mockaton/Dashboard.css" />
|
|
16
|
-
|
|
17
9
|
<link rel="icon" href="data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m235 33.7v202c0 9.19-5.81 14-17.4 14-11.6 0-17.4-4.83-17.4-14v-151c-0.115-4.49-6.72-5.88-8.46-0.87l-48.3 155c-2.22 7.01-7.72 10.1-16 9.9-3.63-0.191-7.01-1.14-9.66-2.89-2.89-1.72-4.83-4.34-5.57-7.72-11.1-37-22.6-74.3-34.1-111-4.34-14-8.95-31.4-14-48.3-1.82-4.83-8.16-5.32-8.46 1.16v156c0 9.19-5.81 14-17.4 14-11.6 0-17.4-4.83-17.4-14v-207c0-5.74 2.62-13.2 9.39-16.3 7.5-3.14 15-4.05 21.8-3.8 3.14 0 6.03 0.686 8.95 1.46 3.14 0.797 6.03 1.98 8.7 3.63 2.65 1.38 5.32 3.14 7.5 5.57 2.22 2.22 3.87 4.83 5.07 7.72l45.8 157c4.63-15.9 32.4-117 33.3-121 4.12-13.8 7.72-26.5 10.9-38.7 1.16-2.65 2.89-5.32 5.07-7.5 2.15-2.15 4.58-4.12 7.5-5.32 2.65-1.57 5.57-2.89 8.46-3.63 3.14-0.797 9.44-0.988 12.1-0.988 11.6 1.07 29.4 9.14 29.4 27z' fill='%23808080'/%3E%3C/svg%3E">
|
|
18
10
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
19
11
|
<meta name="description" content="HTTP Mock Server">
|
|
@@ -24,3 +16,36 @@ export default (nonce) => `
|
|
|
24
16
|
</body>
|
|
25
17
|
</html>
|
|
26
18
|
`
|
|
19
|
+
|
|
20
|
+
export const CSP = [
|
|
21
|
+
`default-src 'self'`,
|
|
22
|
+
`img-src data: blob: 'self'`
|
|
23
|
+
].join(';')
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const assets = {
|
|
27
|
+
style: ['/Dashboard.css'],
|
|
28
|
+
script: ['/Dashboard.js'],
|
|
29
|
+
module: ['/ApiConstants.js', '/ApiCommander.js', '/Filename.js'],
|
|
30
|
+
image: ['/Logo.svg']
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const dashboardAssets = Object.values(assets).flat()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// Link Header. This is not very useful in localhost (only optimizes 2ms).
|
|
37
|
+
const attrMap = {
|
|
38
|
+
module: 'rel=modulepreload',
|
|
39
|
+
image: 'rel=preload; as=image',
|
|
40
|
+
style: 'rel=preload; as=style',
|
|
41
|
+
script: 'rel=preload; as=script; crossorigin',
|
|
42
|
+
fetch: 'rel=preload; as=fetch; crossorigin'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let links = []
|
|
46
|
+
for (const [type, files] of Object.entries(assets))
|
|
47
|
+
for (const f of files)
|
|
48
|
+
links.push(`<${API.dashboard + f}>; ${attrMap[type]}`)
|
|
49
|
+
links.push(`<${API.state}>; ${attrMap.fetch}`)
|
|
50
|
+
|
|
51
|
+
export const LinkHeader = links.join(',')
|
|
@@ -9,9 +9,11 @@ export function sendOK(response) {
|
|
|
9
9
|
response.end()
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export function sendHTML(response, html) {
|
|
12
|
+
export function sendHTML(response, html, csp, link) {
|
|
13
13
|
logger.access(response)
|
|
14
14
|
response.setHeader('Content-Type', mimeFor('html'))
|
|
15
|
+
response.setHeader('Content-Security-Policy', csp)
|
|
16
|
+
response.setHeader('Link', link)
|
|
15
17
|
response.end(html)
|
|
16
18
|
}
|
|
17
19
|
|