@contember/engine-panel 2.2.0-alpha.4
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/CLAUDE.md +29 -0
- package/dist/development/index.cjs +21 -0
- package/dist/development/index.cjs.map +1 -0
- package/dist/development/index.js +21 -0
- package/dist/development/index.js.map +1 -0
- package/dist/development/src/PanelAccessCheck.cjs +21 -0
- package/dist/development/src/PanelAccessCheck.cjs.map +1 -0
- package/dist/development/src/PanelAccessCheck.js +21 -0
- package/dist/development/src/PanelAccessCheck.js.map +1 -0
- package/dist/development/src/PanelApiController.cjs +60 -0
- package/dist/development/src/PanelApiController.cjs.map +1 -0
- package/dist/development/src/PanelApiController.js +60 -0
- package/dist/development/src/PanelApiController.js.map +1 -0
- package/dist/development/src/PanelAssets.cjs +57 -0
- package/dist/development/src/PanelAssets.cjs.map +1 -0
- package/dist/development/src/PanelAssets.js +57 -0
- package/dist/development/src/PanelAssets.js.map +1 -0
- package/dist/development/src/PanelController.cjs +108 -0
- package/dist/development/src/PanelController.cjs.map +1 -0
- package/dist/development/src/PanelController.js +108 -0
- package/dist/development/src/PanelController.js.map +1 -0
- package/dist/development/src/PanelMount.cjs +27 -0
- package/dist/development/src/PanelMount.cjs.map +1 -0
- package/dist/development/src/PanelMount.js +27 -0
- package/dist/development/src/PanelMount.js.map +1 -0
- package/dist/development/src/PanelPlugin.cjs +66 -0
- package/dist/development/src/PanelPlugin.cjs.map +1 -0
- package/dist/development/src/PanelPlugin.js +66 -0
- package/dist/development/src/PanelPlugin.js.map +1 -0
- package/dist/development/src/TtlCache.cjs +47 -0
- package/dist/development/src/TtlCache.cjs.map +1 -0
- package/dist/development/src/TtlCache.js +47 -0
- package/dist/development/src/TtlCache.js.map +1 -0
- package/dist/development/src/generated/assets.cjs +46 -0
- package/dist/development/src/generated/assets.cjs.map +1 -0
- package/dist/development/src/generated/assets.js +46 -0
- package/dist/development/src/generated/assets.js.map +1 -0
- package/dist/production/index.cjs +21 -0
- package/dist/production/index.cjs.map +1 -0
- package/dist/production/index.js +21 -0
- package/dist/production/index.js.map +1 -0
- package/dist/production/src/PanelAccessCheck.cjs +21 -0
- package/dist/production/src/PanelAccessCheck.cjs.map +1 -0
- package/dist/production/src/PanelAccessCheck.js +21 -0
- package/dist/production/src/PanelAccessCheck.js.map +1 -0
- package/dist/production/src/PanelApiController.cjs +60 -0
- package/dist/production/src/PanelApiController.cjs.map +1 -0
- package/dist/production/src/PanelApiController.js +60 -0
- package/dist/production/src/PanelApiController.js.map +1 -0
- package/dist/production/src/PanelAssets.cjs +57 -0
- package/dist/production/src/PanelAssets.cjs.map +1 -0
- package/dist/production/src/PanelAssets.js +57 -0
- package/dist/production/src/PanelAssets.js.map +1 -0
- package/dist/production/src/PanelController.cjs +108 -0
- package/dist/production/src/PanelController.cjs.map +1 -0
- package/dist/production/src/PanelController.js +108 -0
- package/dist/production/src/PanelController.js.map +1 -0
- package/dist/production/src/PanelMount.cjs +27 -0
- package/dist/production/src/PanelMount.cjs.map +1 -0
- package/dist/production/src/PanelMount.js +27 -0
- package/dist/production/src/PanelMount.js.map +1 -0
- package/dist/production/src/PanelPlugin.cjs +66 -0
- package/dist/production/src/PanelPlugin.cjs.map +1 -0
- package/dist/production/src/PanelPlugin.js +66 -0
- package/dist/production/src/PanelPlugin.js.map +1 -0
- package/dist/production/src/TtlCache.cjs +47 -0
- package/dist/production/src/TtlCache.cjs.map +1 -0
- package/dist/production/src/TtlCache.js +47 -0
- package/dist/production/src/TtlCache.js.map +1 -0
- package/dist/production/src/generated/assets.cjs +46 -0
- package/dist/production/src/generated/assets.cjs.map +1 -0
- package/dist/production/src/generated/assets.js +46 -0
- package/dist/production/src/generated/assets.js.map +1 -0
- package/dist/tests/cases/unit/panelController.test.d.ts +2 -0
- package/dist/tests/cases/unit/panelController.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/panelMount.test.d.ts +2 -0
- package/dist/tests/cases/unit/panelMount.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/ttlCache.test.d.ts +2 -0
- package/dist/tests/cases/unit/ttlCache.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -0
- package/dist/types/PanelAccessCheck.d.ts +16 -0
- package/dist/types/PanelAccessCheck.d.ts.map +1 -0
- package/dist/types/PanelApiController.d.ts +58 -0
- package/dist/types/PanelApiController.d.ts.map +1 -0
- package/dist/types/PanelAssets.d.ts +32 -0
- package/dist/types/PanelAssets.d.ts.map +1 -0
- package/dist/types/PanelController.d.ts +46 -0
- package/dist/types/PanelController.d.ts.map +1 -0
- package/dist/types/PanelMount.d.ts +26 -0
- package/dist/types/PanelMount.d.ts.map +1 -0
- package/dist/types/PanelPlugin.d.ts +6 -0
- package/dist/types/PanelPlugin.d.ts.map +1 -0
- package/dist/types/TtlCache.d.ts +16 -0
- package/dist/types/TtlCache.d.ts.map +1 -0
- package/dist/types/generated/assets.d.ts +3 -0
- package/dist/types/generated/assets.d.ts.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +40 -0
- package/scripts/buildAssets.ts +79 -0
- package/src/PanelAccessCheck.ts +24 -0
- package/src/PanelApiController.ts +126 -0
- package/src/PanelAssets.ts +69 -0
- package/src/PanelController.ts +166 -0
- package/src/PanelMount.ts +34 -0
- package/src/PanelPlugin.ts +77 -0
- package/src/TtlCache.ts +53 -0
- package/src/index.ts +11 -0
- package/src/tsconfig.json +9 -0
- package/tests/cases/unit/panelController.test.ts +159 -0
- package/tests/cases/unit/panelMount.test.ts +55 -0
- package/tests/cases/unit/ttlCache.test.ts +55 -0
- package/tests/tsconfig.json +9 -0
- package/tsconfig.json +8 -0
- package/tsconfig.settings.json +6 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PanelPlugin } from './PanelPlugin.js'
|
|
2
|
+
|
|
3
|
+
export * from './PanelAccessCheck.js'
|
|
4
|
+
export * from './PanelApiController.js'
|
|
5
|
+
export * from './PanelAssets.js'
|
|
6
|
+
export * from './PanelController.js'
|
|
7
|
+
export * from './PanelMount.js'
|
|
8
|
+
export * from './PanelPlugin.js'
|
|
9
|
+
export * from './TtlCache.js'
|
|
10
|
+
|
|
11
|
+
export default PanelPlugin
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { gunzipSync, gzipSync } from 'node:zlib'
|
|
3
|
+
import { PanelAssetMap, PanelAssetStore, PanelController, PanelRequest } from '../../../src/index.js'
|
|
4
|
+
|
|
5
|
+
const asset = (contentType: string, content: string) => ({ contentType, gzip: gzipSync(Buffer.from(content, 'utf8')).toString('base64') })
|
|
6
|
+
|
|
7
|
+
const indexSource = [
|
|
8
|
+
'<!DOCTYPE html>',
|
|
9
|
+
'<base href="__CONTEMBER_PANEL_BASE__">',
|
|
10
|
+
'<script id="contember-panel-config" type="application/json">__CONTEMBER_PANEL_CONFIG__</script>',
|
|
11
|
+
'<script type="module" src="./assets/index-abc123.js"></script>',
|
|
12
|
+
].join('\n')
|
|
13
|
+
|
|
14
|
+
const assets: PanelAssetMap = {
|
|
15
|
+
'index.html': asset('text/html; charset=utf-8', indexSource),
|
|
16
|
+
'assets/index-abc123.js': asset('text/javascript; charset=utf-8', 'console.log(1)'),
|
|
17
|
+
'assets/index-def456.css': asset('text/css; charset=utf-8', 'body{color:red}'),
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const createController = (basePath = '/panel', pluginApis: () => readonly string[] = () => []) =>
|
|
21
|
+
new PanelController({
|
|
22
|
+
basePath,
|
|
23
|
+
apiBaseUrl: `${basePath}/api`,
|
|
24
|
+
assets: new PanelAssetStore(assets),
|
|
25
|
+
pluginApis,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const get = (path: string | undefined, overrides: Partial<PanelRequest> = {}) =>
|
|
29
|
+
createController().handle({ method: 'GET', path, acceptEncoding: 'gzip, deflate', ...overrides })
|
|
30
|
+
|
|
31
|
+
const bodyText = (response: { headers: Readonly<Record<string, string>>; body: Buffer }) =>
|
|
32
|
+
(response.headers['Content-Encoding'] === 'gzip' ? gunzipSync(response.body) : response.body).toString('utf8')
|
|
33
|
+
|
|
34
|
+
describe('panel controller', () => {
|
|
35
|
+
test('serves the app shell at the panel root', () => {
|
|
36
|
+
const response = get(undefined)
|
|
37
|
+
|
|
38
|
+
expect(response.status).toBe(200)
|
|
39
|
+
expect(response.headers['Content-Type']).toBe('text/html; charset=utf-8')
|
|
40
|
+
expect(bodyText(response)).toContain('<!DOCTYPE html>')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('substitutes the base href and the runtime config', () => {
|
|
44
|
+
const html = bodyText(get(undefined))
|
|
45
|
+
|
|
46
|
+
expect(html).toContain('<base href="/panel/">')
|
|
47
|
+
expect(html).toContain('{"basePath":"/panel/","apiBaseUrl":"/panel/api","pluginApis":[]}')
|
|
48
|
+
expect(html).not.toContain('__CONTEMBER_PANEL_')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('follows a configured mount path', () => {
|
|
52
|
+
const controller = createController('/admin-console')
|
|
53
|
+
const html = gunzipSync(controller.handle({ method: 'GET', path: undefined, acceptEncoding: 'gzip' }).body).toString('utf8')
|
|
54
|
+
|
|
55
|
+
expect(html).toContain('<base href="/admin-console/">')
|
|
56
|
+
expect(html).toContain('"apiBaseUrl":"/admin-console/api"')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('treats dollar sequences in the configured mount path literally', () => {
|
|
60
|
+
const controller = createController('/console/$&')
|
|
61
|
+
const html = gunzipSync(controller.handle({ method: 'GET', path: undefined, acceptEncoding: 'gzip' }).body).toString('utf8')
|
|
62
|
+
|
|
63
|
+
expect(html).toContain('<base href="/console/$&/">')
|
|
64
|
+
expect(html).toContain('"basePath":"/console/$&/"')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('serves a built asset with its content type', () => {
|
|
68
|
+
const response = get('assets/index-abc123.js')
|
|
69
|
+
|
|
70
|
+
expect(response.headers['Content-Type']).toBe('text/javascript; charset=utf-8')
|
|
71
|
+
expect(bodyText(response)).toBe('console.log(1)')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('caches hashed assets immutably and never the shell', () => {
|
|
75
|
+
expect(get('assets/index-def456.css').headers['Cache-Control']).toBe('public, max-age=31536000, immutable')
|
|
76
|
+
expect(get(undefined).headers['Cache-Control']).toBe('no-store')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('falls back to the app shell for client-side routes', () => {
|
|
80
|
+
const response = get('p/my-project/members')
|
|
81
|
+
|
|
82
|
+
expect(response.status).toBe(200)
|
|
83
|
+
expect(response.headers['Content-Type']).toBe('text/html; charset=utf-8')
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// A traversal cannot reach outside the asset map anyway — there is no filesystem — but it must not
|
|
87
|
+
// resolve to an asset either, and the app shell is the honest answer for an unknown path.
|
|
88
|
+
test('does not resolve traversal or encoded traversal to an asset', () => {
|
|
89
|
+
for (const path of ['../../etc/passwd', '..%2f..%2fetc%2fpasswd', 'assets/../../index.html']) {
|
|
90
|
+
expect(get(path).headers['Content-Type']).toBe('text/html; charset=utf-8')
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('sends the shell for a request that spells out index.html', () => {
|
|
95
|
+
// Serving the raw asset would leak the unsubstituted placeholders.
|
|
96
|
+
expect(bodyText(get('index.html'))).toContain('<base href="/panel/">')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('decompresses when the client does not accept gzip', () => {
|
|
100
|
+
const response = get('assets/index-abc123.js', { acceptEncoding: undefined })
|
|
101
|
+
|
|
102
|
+
expect(response.headers['Content-Encoding']).toBeUndefined()
|
|
103
|
+
expect(response.body.toString('utf8')).toBe('console.log(1)')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('does not decompress a HEAD response body', () => {
|
|
107
|
+
const response = get('assets/index-abc123.js', { method: 'HEAD', acceptEncoding: undefined })
|
|
108
|
+
|
|
109
|
+
expect(response.body).toHaveLength(0)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('sets the security headers on the shell only', () => {
|
|
113
|
+
const shell = get(undefined)
|
|
114
|
+
expect(shell.headers['Content-Security-Policy']).toContain(`frame-ancestors 'none'`)
|
|
115
|
+
expect(shell.headers['X-Robots-Tag']).toBe('noindex, nofollow')
|
|
116
|
+
expect(shell.headers['X-Content-Type-Options']).toBe('nosniff')
|
|
117
|
+
|
|
118
|
+
expect(get('assets/index-abc123.js').headers['Content-Security-Policy']).toBeUndefined()
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('rejects anything but GET and HEAD', () => {
|
|
122
|
+
const response = createController().handle({ method: 'POST', path: undefined, acceptEncoding: 'gzip' })
|
|
123
|
+
|
|
124
|
+
expect(response.status).toBe(405)
|
|
125
|
+
expect(response.headers['Allow']).toBe('GET, HEAD')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// The plugins that mount into the panel register after the controller is constructed, so the
|
|
129
|
+
// shell must be rendered from the list as it stands when it is first served.
|
|
130
|
+
test('reports the plugin APIs mounted by the time the shell is served', () => {
|
|
131
|
+
const mounted: string[] = []
|
|
132
|
+
const controller = createController('/panel', () => mounted)
|
|
133
|
+
mounted.push('actions')
|
|
134
|
+
|
|
135
|
+
const html = gunzipSync(controller.handle({ method: 'GET', path: undefined, acceptEncoding: 'gzip' }).body).toString('utf8')
|
|
136
|
+
|
|
137
|
+
expect(html).toContain('"pluginApis":["actions"]')
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test('refuses to start without a built panel', () => {
|
|
141
|
+
expect(() => new PanelController({ basePath: '/panel', apiBaseUrl: '/panel/api', assets: new PanelAssetStore({}), pluginApis: () => [] }))
|
|
142
|
+
.toThrow(/not built into this binary/)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('panel asset store', () => {
|
|
147
|
+
test('reports an unbuilt panel', () => {
|
|
148
|
+
expect(new PanelAssetStore({}).isEmpty()).toBe(true)
|
|
149
|
+
expect(new PanelAssetStore(assets).isEmpty()).toBe(false)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('decodes an asset once', () => {
|
|
153
|
+
const store = new PanelAssetStore(assets)
|
|
154
|
+
|
|
155
|
+
expect(store.getGzip('assets/index-abc123.js')).toBe(store.getGzip('assets/index-abc123.js'))
|
|
156
|
+
expect(store.getGzip('missing.js')).toBeUndefined()
|
|
157
|
+
expect(store.getIdentity('assets/index-abc123.js')).toBe(store.getIdentity('assets/index-abc123.js'))
|
|
158
|
+
})
|
|
159
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { HttpController, PanelApiMountTarget } from '@contember/engine-http'
|
|
2
|
+
import { describe, expect, test } from 'bun:test'
|
|
3
|
+
import { ConfiguredPanelAccessCheck, PanelApiControllerFactory, PanelAssetStore, PanelMount } from '../../../src/index.js'
|
|
4
|
+
|
|
5
|
+
type RegisteredRoute = { module: string; mask: string; controller: HttpController }
|
|
6
|
+
|
|
7
|
+
const createTarget = () => {
|
|
8
|
+
const routes: RegisteredRoute[] = []
|
|
9
|
+
const target: PanelApiMountTarget = { addRoute: (module, mask, controller) => void routes.push({ module, mask, controller }) }
|
|
10
|
+
return { routes, target }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const createMount = (basePath = '/panel') =>
|
|
14
|
+
new PanelMount(basePath, new PanelAssetStore({}), new PanelApiControllerFactory(new ConfiguredPanelAccessCheck()))
|
|
15
|
+
|
|
16
|
+
const controller: HttpController = () => undefined
|
|
17
|
+
|
|
18
|
+
describe('panel mount', () => {
|
|
19
|
+
test('registers a plugin API under the panel API root', () => {
|
|
20
|
+
const { routes, target } = createTarget()
|
|
21
|
+
createMount().mount(target, { name: 'actions', path: 'actions/:projectSlug', controller })
|
|
22
|
+
|
|
23
|
+
expect(routes).toHaveLength(1)
|
|
24
|
+
expect(routes[0].module).toBe('panel-api')
|
|
25
|
+
expect(routes[0].mask).toBe('/panel/api/actions/:projectSlug')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('follows the configured mount path', () => {
|
|
29
|
+
const { routes, target } = createTarget()
|
|
30
|
+
createMount('/admin-console').mount(target, { name: 'actions', path: 'actions/:projectSlug', controller })
|
|
31
|
+
|
|
32
|
+
expect(routes[0].mask).toBe('/admin-console/api/actions/:projectSlug')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
// The gate is what makes this different from the public route, so the plugin must not get the
|
|
36
|
+
// controller it handed in.
|
|
37
|
+
test('puts the mounted controller behind the panel gate', () => {
|
|
38
|
+
const { routes, target } = createTarget()
|
|
39
|
+
createMount().mount(target, { name: 'actions', path: 'actions/:projectSlug', controller })
|
|
40
|
+
|
|
41
|
+
expect(routes[0].controller).not.toBe(controller)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('reports what has been mounted', () => {
|
|
45
|
+
const { target } = createTarget()
|
|
46
|
+
const mount = createMount()
|
|
47
|
+
|
|
48
|
+
expect(mount.pluginApis).toEqual([])
|
|
49
|
+
|
|
50
|
+
mount.mount(target, { name: 'actions', path: 'actions/:projectSlug', controller })
|
|
51
|
+
mount.mount(target, { name: 'actions', path: 'actions/:projectSlug/detail', controller })
|
|
52
|
+
|
|
53
|
+
expect(mount.pluginApis).toEqual(['actions'])
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { TtlCache } from '../../../src/TtlCache.js'
|
|
3
|
+
|
|
4
|
+
const clock = (start = 0) => {
|
|
5
|
+
let now = start
|
|
6
|
+
return { now: () => now, advance: (ms: number) => now += ms }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('ttl cache', () => {
|
|
10
|
+
test('computes once within the ttl', async () => {
|
|
11
|
+
let calls = 0
|
|
12
|
+
const cache = new TtlCache<number>(1000, 1000, clock().now)
|
|
13
|
+
const compute = async () => ++calls
|
|
14
|
+
|
|
15
|
+
expect(await cache.resolve('a', compute)).toBe(1)
|
|
16
|
+
expect(await cache.resolve('a', compute)).toBe(1)
|
|
17
|
+
expect(calls).toBe(1)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('recomputes after the ttl expires', async () => {
|
|
21
|
+
let calls = 0
|
|
22
|
+
const time = clock()
|
|
23
|
+
const cache = new TtlCache<number>(1000, 1000, time.now)
|
|
24
|
+
const compute = async () => ++calls
|
|
25
|
+
|
|
26
|
+
await cache.resolve('a', compute)
|
|
27
|
+
time.advance(1001)
|
|
28
|
+
|
|
29
|
+
expect(await cache.resolve('a', compute)).toBe(2)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('keeps keys apart', async () => {
|
|
33
|
+
const cache = new TtlCache<string>(1000, 1000, clock().now)
|
|
34
|
+
|
|
35
|
+
expect(await cache.resolve('a', async () => 'first')).toBe('first')
|
|
36
|
+
expect(await cache.resolve('b', async () => 'second')).toBe('second')
|
|
37
|
+
expect(await cache.resolve('a', async () => 'ignored')).toBe('first')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('sweeps expired entries once it grows past the threshold', async () => {
|
|
41
|
+
const time = clock()
|
|
42
|
+
const cache = new TtlCache<number>(1000, 5, time.now)
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < 4; i++) {
|
|
45
|
+
await cache.resolve(`key-${i}`, async () => i)
|
|
46
|
+
}
|
|
47
|
+
expect(cache.size).toBe(4)
|
|
48
|
+
|
|
49
|
+
time.advance(1001)
|
|
50
|
+
// The fifth insert crosses the threshold, so the four stale entries go with it.
|
|
51
|
+
await cache.resolve('fresh', async () => 99)
|
|
52
|
+
|
|
53
|
+
expect(cache.size).toBe(1)
|
|
54
|
+
})
|
|
55
|
+
})
|
package/tsconfig.json
ADDED