@crombolo/server 0.59.0 → 0.61.0
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 +14 -14
- package/src/adapters/audit.ts +24 -4
- package/src/approvals-api.ts +2 -2
- package/src/bundle-admin.ts +194 -25
- package/src/bundle-api.ts +67 -16
- package/src/bundle-code-install.ts +18 -1
- package/src/bundle-grants.ts +80 -15
- package/src/bundles.ts +151 -4
- package/src/component-templates-api.ts +2 -2
- package/src/config.ts +20 -3
- package/src/core.ts +49 -19
- package/src/extension-activity.ts +237 -0
- package/src/extension-components.ts +17 -1
- package/src/extension-discovery.ts +37 -3
- package/src/extension-process.ts +140 -28
- package/src/extension-screens.ts +383 -0
- package/src/grant-policy.ts +57 -0
- package/src/index.ts +10 -1
- package/src/jobs.ts +23 -0
- package/src/marketplace.ts +190 -29
- package/src/ports.ts +1 -2
- package/src/remote.ts +12 -4
- package/src/server.ts +79 -14
- package/src/settings.ts +17 -0
- package/src/site-file.ts +5 -2
package/src/extension-process.ts
CHANGED
|
@@ -28,7 +28,8 @@ import {
|
|
|
28
28
|
import type { ExtensionDescription } from '@crombolo/extension/runner'
|
|
29
29
|
import { RawHtml } from '@crombolo/render'
|
|
30
30
|
import type { Kysely } from 'kysely'
|
|
31
|
-
import type { BundleLog, ServerBundle } from './bundles.ts'
|
|
31
|
+
import type { BundleCaller, BundleLog, ServerBundle } from './bundles.ts'
|
|
32
|
+
import { type ActivityRecorder, personOf, timed } from './extension-activity.ts'
|
|
32
33
|
import { extensionSqlProblem } from './extension-sql.ts'
|
|
33
34
|
|
|
34
35
|
/** The Deno binary the `deno` package installed for this platform, or `CROMBOLO_DENO`. */
|
|
@@ -66,7 +67,8 @@ export async function buildExtension(
|
|
|
66
67
|
siteDir: string,
|
|
67
68
|
outDir: string,
|
|
68
69
|
): Promise<string> {
|
|
69
|
-
|
|
70
|
+
// From its own root when it has one (story 326), so its dependencies resolve from there.
|
|
71
|
+
const entryFile = Bun.resolveSync(pkg.packageName, pkg.root ?? siteDir)
|
|
70
72
|
// As the build sees it: a workspace link resolves to its real place.
|
|
71
73
|
const backOffice = `${join(realpathSync(pkg.directory), BACKOFFICE_DIR)}${sep}`
|
|
72
74
|
mkdirSync(outDir, { recursive: true })
|
|
@@ -232,6 +234,8 @@ export interface ExtensionProcessOptions {
|
|
|
232
234
|
/** Stopped after this long without a call, unless it serves routes. */
|
|
233
235
|
idleMs?: number
|
|
234
236
|
memoryMb?: number
|
|
237
|
+
/** Where what it does is recorded for the activity view (story 327). */
|
|
238
|
+
activity?: ActivityRecorder
|
|
235
239
|
}
|
|
236
240
|
|
|
237
241
|
const START_TIMEOUT_MS = 30_000
|
|
@@ -716,12 +720,43 @@ export class ExtensionProcess {
|
|
|
716
720
|
})
|
|
717
721
|
const problem = extensionSqlProblem(compiled.sql, this.#options.dialect)
|
|
718
722
|
if (problem) throw new Error(problem)
|
|
719
|
-
|
|
723
|
+
const result = await target.executeQuery(compiled)
|
|
724
|
+
// Story 327: the table and how many rows, never the statement or its values.
|
|
725
|
+
const activity = this.#options.activity
|
|
726
|
+
if (activity) {
|
|
727
|
+
const words = compiled.sql.trim().toLowerCase()
|
|
728
|
+
const table = TABLE_OF.exec(compiled.sql)?.[1] ?? null
|
|
729
|
+
const writes = /^(insert|update|delete|replace)\b/.exec(words)?.[1]
|
|
730
|
+
activity.record({
|
|
731
|
+
extensionId: this.id,
|
|
732
|
+
personKey: null,
|
|
733
|
+
personName: null,
|
|
734
|
+
kind: writes ? 'rows' : 'read',
|
|
735
|
+
area: table,
|
|
736
|
+
verb: writes ?? 'select',
|
|
737
|
+
// The rows when the driver says, one statement when it does not.
|
|
738
|
+
count: writes
|
|
739
|
+
? Number(
|
|
740
|
+
result.numAffectedRows ?? (result as { numChangedRows?: bigint }).numChangedRows ?? 1,
|
|
741
|
+
)
|
|
742
|
+
: 1,
|
|
743
|
+
outcome: 'ok',
|
|
744
|
+
})
|
|
745
|
+
}
|
|
746
|
+
return result
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/** What it does is recorded, when the site keeps its activity (story 327). */
|
|
750
|
+
get activity(): ActivityRecorder | undefined {
|
|
751
|
+
return this.#options.activity
|
|
720
752
|
}
|
|
721
753
|
}
|
|
722
754
|
|
|
723
755
|
const ROLLBACK = Symbol('rollback')
|
|
724
756
|
|
|
757
|
+
/** The first table a statement names after `into`, `update` or `from`, quoted or not. */
|
|
758
|
+
const TABLE_OF = /\b(?:into|update|from)\s+["`]?([A-Za-z_][\w.]*)["`]?/i
|
|
759
|
+
|
|
725
760
|
/**
|
|
726
761
|
* The extension, as the CMS mounts it: what it described, with each handler
|
|
727
762
|
* sending its call to the process. The host's checks run on this as they do on
|
|
@@ -738,30 +773,41 @@ export function isolatedBundle(process: ExtensionProcess): ServerBundle {
|
|
|
738
773
|
const cookies = request.headers.has('cookie')
|
|
739
774
|
? [...(context.cookies ?? [])].map(([name, cookie]) => [name, cookie.value])
|
|
740
775
|
: []
|
|
741
|
-
const { value, response } = await
|
|
742
|
-
|
|
776
|
+
const { value, response } = await timed(
|
|
777
|
+
process.activity,
|
|
743
778
|
{
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
method: request.method,
|
|
750
|
-
headers: [...request.headers],
|
|
751
|
-
hasBody: request.body !== null,
|
|
752
|
-
},
|
|
779
|
+
extensionId: d.id,
|
|
780
|
+
...personOf(context.caller as BundleCaller | undefined),
|
|
781
|
+
kind: 'call',
|
|
782
|
+
area: `route ${d.routes[index]?.path ?? index}`,
|
|
783
|
+
verb: method,
|
|
753
784
|
},
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
785
|
+
() =>
|
|
786
|
+
process.call(
|
|
787
|
+
'route',
|
|
788
|
+
{
|
|
789
|
+
route: index,
|
|
790
|
+
method,
|
|
791
|
+
cookies,
|
|
792
|
+
request: {
|
|
793
|
+
url: request.url,
|
|
794
|
+
method: request.method,
|
|
795
|
+
headers: [...request.headers],
|
|
796
|
+
hasBody: request.body !== null,
|
|
797
|
+
},
|
|
798
|
+
},
|
|
799
|
+
{
|
|
800
|
+
params: context.params,
|
|
801
|
+
url: context.url,
|
|
802
|
+
setCookie: context.setCookie,
|
|
803
|
+
member: context.member,
|
|
804
|
+
signal: context.signal,
|
|
805
|
+
bundle: context.bundle,
|
|
806
|
+
caller: context.caller,
|
|
807
|
+
principal: context.principal,
|
|
808
|
+
},
|
|
809
|
+
{ request },
|
|
810
|
+
),
|
|
765
811
|
)
|
|
766
812
|
return response ?? value
|
|
767
813
|
}
|
|
@@ -818,7 +864,19 @@ export function isolatedBundle(process: ExtensionProcess): ServerBundle {
|
|
|
818
864
|
d.events.map((alias) => [
|
|
819
865
|
alias,
|
|
820
866
|
async (context: { signal?: AbortSignal }) =>
|
|
821
|
-
(
|
|
867
|
+
(
|
|
868
|
+
await timed(
|
|
869
|
+
process.activity,
|
|
870
|
+
{
|
|
871
|
+
extensionId: d.id,
|
|
872
|
+
personKey: null,
|
|
873
|
+
personName: null,
|
|
874
|
+
kind: 'call',
|
|
875
|
+
area: `event ${alias}`,
|
|
876
|
+
},
|
|
877
|
+
() => process.call('event', { name: alias }, context),
|
|
878
|
+
)
|
|
879
|
+
).value as never,
|
|
822
880
|
]),
|
|
823
881
|
),
|
|
824
882
|
}
|
|
@@ -829,13 +887,67 @@ export function isolatedBundle(process: ExtensionProcess): ServerBundle {
|
|
|
829
887
|
d.jobs.map((name) => [
|
|
830
888
|
name,
|
|
831
889
|
async (payload: unknown, context: unknown) =>
|
|
832
|
-
(
|
|
833
|
-
|
|
890
|
+
(
|
|
891
|
+
await timed(
|
|
892
|
+
process.activity,
|
|
893
|
+
{
|
|
894
|
+
extensionId: d.id,
|
|
895
|
+
personKey: null,
|
|
896
|
+
personName: null,
|
|
897
|
+
kind: 'call',
|
|
898
|
+
area: `job ${name}`,
|
|
899
|
+
},
|
|
900
|
+
() => process.call('job', { name }, { payload, context }, { timeoutMs: null }),
|
|
901
|
+
)
|
|
902
|
+
).value,
|
|
834
903
|
]),
|
|
835
904
|
),
|
|
836
905
|
}
|
|
837
906
|
: {}),
|
|
838
907
|
...(d.schedules ? { schedules: d.schedules as ServerBundle['schedules'] } : {}),
|
|
908
|
+
...((d.screens ?? []).length > 0
|
|
909
|
+
? {
|
|
910
|
+
screens: Object.fromEntries(
|
|
911
|
+
(d.screens ?? []).map((screen) => {
|
|
912
|
+
const handler =
|
|
913
|
+
(action?: string) =>
|
|
914
|
+
async (context: { signal?: AbortSignal } & Record<string, unknown>) =>
|
|
915
|
+
(
|
|
916
|
+
await timed(
|
|
917
|
+
process.activity,
|
|
918
|
+
{
|
|
919
|
+
extensionId: d.id,
|
|
920
|
+
...personOf(context.caller as BundleCaller | undefined),
|
|
921
|
+
kind: 'screen',
|
|
922
|
+
area: screen.id,
|
|
923
|
+
verb: action ?? 'view',
|
|
924
|
+
},
|
|
925
|
+
() =>
|
|
926
|
+
process.call(
|
|
927
|
+
'screen',
|
|
928
|
+
{ screen: screen.id, ...(action ? { action } : {}) },
|
|
929
|
+
{
|
|
930
|
+
bundle: context.bundle,
|
|
931
|
+
caller: context.caller,
|
|
932
|
+
principal: context.principal,
|
|
933
|
+
state: context.state,
|
|
934
|
+
input: context.input,
|
|
935
|
+
signal: context.signal,
|
|
936
|
+
},
|
|
937
|
+
),
|
|
938
|
+
)
|
|
939
|
+
).value as never
|
|
940
|
+
return [
|
|
941
|
+
screen.id,
|
|
942
|
+
{
|
|
943
|
+
view: handler(),
|
|
944
|
+
actions: Object.fromEntries(screen.actions.map((name) => [name, handler(name)])),
|
|
945
|
+
},
|
|
946
|
+
]
|
|
947
|
+
}),
|
|
948
|
+
) as unknown as ServerBundle['screens'],
|
|
949
|
+
}
|
|
950
|
+
: {}),
|
|
839
951
|
...(d.workflows.length > 0
|
|
840
952
|
? {
|
|
841
953
|
forms: {
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an extension's screen answered, held to the catalogue before it leaves
|
|
3
|
+
* the server (story 323): unknown nodes dropped, every displayed string a
|
|
4
|
+
* string, links kept only to places that cannot run script. The back office
|
|
5
|
+
* renders the result as text, so this is the first of two checks, not the only
|
|
6
|
+
* one. docs/extensions.md, "Screens".
|
|
7
|
+
*/
|
|
8
|
+
import type {
|
|
9
|
+
Button,
|
|
10
|
+
Cell,
|
|
11
|
+
Condition,
|
|
12
|
+
Field,
|
|
13
|
+
Node,
|
|
14
|
+
Row,
|
|
15
|
+
Screen,
|
|
16
|
+
ScreenResult,
|
|
17
|
+
ScreenValue,
|
|
18
|
+
ScreenValues,
|
|
19
|
+
Search,
|
|
20
|
+
Tab,
|
|
21
|
+
} from '@crombolo/extension/screens'
|
|
22
|
+
|
|
23
|
+
const NODE_TYPES = new Set([
|
|
24
|
+
'table',
|
|
25
|
+
'form',
|
|
26
|
+
'button',
|
|
27
|
+
'notice',
|
|
28
|
+
'problem',
|
|
29
|
+
'empty',
|
|
30
|
+
'tabs',
|
|
31
|
+
'progress',
|
|
32
|
+
])
|
|
33
|
+
const FIELD_KINDS = new Set([
|
|
34
|
+
'text',
|
|
35
|
+
'number',
|
|
36
|
+
'boolean',
|
|
37
|
+
'choice',
|
|
38
|
+
'page',
|
|
39
|
+
'media',
|
|
40
|
+
'member',
|
|
41
|
+
'secret',
|
|
42
|
+
'list',
|
|
43
|
+
])
|
|
44
|
+
/** What a list's rows may hold (story 329). */
|
|
45
|
+
const LIST_SUB_KINDS = new Set(['text', 'number', 'boolean', 'choice'])
|
|
46
|
+
const LOOKS = new Set(['primary', 'secondary', 'danger'])
|
|
47
|
+
const TAG_LOOKS = new Set(['default', 'primary', 'secondary', 'positive', 'danger'])
|
|
48
|
+
const LIMIT = { rows: 1000, nodes: 200, fields: 100, text: 10_000 }
|
|
49
|
+
|
|
50
|
+
type Raw = Record<string, unknown>
|
|
51
|
+
const isRecord = (value: unknown): value is Raw =>
|
|
52
|
+
typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
53
|
+
|
|
54
|
+
/** A displayed string: a primitive as text, anything else empty. */
|
|
55
|
+
const text = (value: unknown): string => {
|
|
56
|
+
if (typeof value === 'string') return value.slice(0, LIMIT.text)
|
|
57
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value)
|
|
58
|
+
return ''
|
|
59
|
+
}
|
|
60
|
+
const optionalText = (value: unknown): string | undefined =>
|
|
61
|
+
value === undefined || value === null ? undefined : text(value)
|
|
62
|
+
const finite = (value: unknown): number | undefined =>
|
|
63
|
+
typeof value === 'number' && Number.isFinite(value) ? value : undefined
|
|
64
|
+
const list = (value: unknown, max: number): unknown[] =>
|
|
65
|
+
Array.isArray(value) ? value.slice(0, max) : []
|
|
66
|
+
|
|
67
|
+
/** A link the back office may follow: http, https, or a path of its own. */
|
|
68
|
+
export function safeScreenHref(href: unknown): string | undefined {
|
|
69
|
+
if (typeof href !== 'string') return undefined
|
|
70
|
+
const trimmed = href.trim()
|
|
71
|
+
if (/^https?:\/\//i.test(trimmed)) return trimmed
|
|
72
|
+
if (/^section\//.test(trimmed)) return trimmed
|
|
73
|
+
// One slash: a path on this site. Two would be another host.
|
|
74
|
+
if (/^\/(?!\/)/.test(trimmed) && !trimmed.includes('\\')) return trimmed
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const value = (raw: unknown): ScreenValue | undefined => {
|
|
79
|
+
if (raw === null) return null
|
|
80
|
+
if (typeof raw === 'string') return raw.slice(0, LIMIT.text)
|
|
81
|
+
if (typeof raw === 'number') return Number.isFinite(raw) ? raw : undefined
|
|
82
|
+
if (typeof raw === 'boolean') return raw
|
|
83
|
+
if (Array.isArray(raw)) {
|
|
84
|
+
if (raw.every((item) => typeof item === 'string')) return raw as string[]
|
|
85
|
+
// A list field's rows: plain objects of primitives, nothing deeper.
|
|
86
|
+
return raw.slice(0, LIMIT.rows).flatMap((item) => {
|
|
87
|
+
if (!isRecord(item)) return []
|
|
88
|
+
const row: Record<string, string | number | boolean | null> = {}
|
|
89
|
+
for (const [key, cell] of Object.entries(item).slice(0, LIMIT.fields))
|
|
90
|
+
if (
|
|
91
|
+
cell === null ||
|
|
92
|
+
typeof cell === 'string' ||
|
|
93
|
+
typeof cell === 'boolean' ||
|
|
94
|
+
(typeof cell === 'number' && Number.isFinite(cell))
|
|
95
|
+
)
|
|
96
|
+
row[key] = typeof cell === 'string' ? cell.slice(0, LIMIT.text) : cell
|
|
97
|
+
return [row]
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
return undefined
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A plain object of values, as a form holds and an action is sent. */
|
|
104
|
+
export function screenValues(raw: unknown): ScreenValues {
|
|
105
|
+
if (!isRecord(raw)) return {}
|
|
106
|
+
const out: Record<string, ScreenValue> = {}
|
|
107
|
+
for (const [key, entry] of Object.entries(raw).slice(0, LIMIT.fields)) {
|
|
108
|
+
const kept = value(entry)
|
|
109
|
+
if (kept !== undefined) out[key] = kept
|
|
110
|
+
}
|
|
111
|
+
return out
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const condition = (raw: unknown): Condition | undefined => {
|
|
115
|
+
if (!isRecord(raw) || typeof raw.field !== 'string') return undefined
|
|
116
|
+
const out: Condition = { field: raw.field }
|
|
117
|
+
const equals = value(raw.equals)
|
|
118
|
+
if (raw.equals !== undefined && equals !== undefined) out.equals = equals
|
|
119
|
+
if (Array.isArray(raw.oneOf))
|
|
120
|
+
out.oneOf = raw.oneOf.map(value).filter((v): v is ScreenValue => v !== undefined)
|
|
121
|
+
if (raw.notEmpty === true) out.notEmpty = true
|
|
122
|
+
return out
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface Seen {
|
|
126
|
+
dropped: Set<string>
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const button = (raw: unknown, seen: Seen): Button | undefined => {
|
|
130
|
+
if (!isRecord(raw) || raw.type !== 'button' || typeof raw.action !== 'string') {
|
|
131
|
+
if (isRecord(raw)) seen.dropped.add(String(raw.type ?? 'unnamed'))
|
|
132
|
+
return undefined
|
|
133
|
+
}
|
|
134
|
+
const out: Button = { type: 'button', label: text(raw.label), action: raw.action }
|
|
135
|
+
if (raw.input !== undefined) out.input = screenValues(raw.input)
|
|
136
|
+
if (typeof raw.look === 'string' && LOOKS.has(raw.look)) out.look = raw.look as Button['look']
|
|
137
|
+
const confirm = optionalText(raw.confirm)
|
|
138
|
+
if (confirm) out.confirm = confirm
|
|
139
|
+
if (raw.submit === true) out.submit = true
|
|
140
|
+
if (raw.disabled === true) out.disabled = true
|
|
141
|
+
return out
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const buttons = (raw: unknown, seen: Seen): Button[] =>
|
|
145
|
+
list(raw, LIMIT.nodes)
|
|
146
|
+
.map((entry) => button(entry, seen))
|
|
147
|
+
.filter((entry): entry is Button => entry !== undefined)
|
|
148
|
+
|
|
149
|
+
const cell = (raw: unknown): Cell => {
|
|
150
|
+
if (!isRecord(raw)) return { type: 'text', text: text(raw) }
|
|
151
|
+
switch (raw.type) {
|
|
152
|
+
case 'code':
|
|
153
|
+
return { type: 'code', text: text(raw.text) }
|
|
154
|
+
case 'tag':
|
|
155
|
+
return {
|
|
156
|
+
type: 'tag',
|
|
157
|
+
text: text(raw.text),
|
|
158
|
+
...(typeof raw.look === 'string' && TAG_LOOKS.has(raw.look)
|
|
159
|
+
? { look: raw.look as 'default' }
|
|
160
|
+
: {}),
|
|
161
|
+
}
|
|
162
|
+
case 'link': {
|
|
163
|
+
const href = safeScreenHref(raw.href)
|
|
164
|
+
// A link to anywhere else is still shown, as the words it carried.
|
|
165
|
+
return href
|
|
166
|
+
? { type: 'link', text: text(raw.text), href }
|
|
167
|
+
: { type: 'text', text: text(raw.text) }
|
|
168
|
+
}
|
|
169
|
+
case 'date': {
|
|
170
|
+
const at = new Date(text(raw.value))
|
|
171
|
+
return Number.isNaN(at.getTime())
|
|
172
|
+
? { type: 'text', text: '' }
|
|
173
|
+
: { type: 'date', value: at.toISOString() }
|
|
174
|
+
}
|
|
175
|
+
default:
|
|
176
|
+
return { type: 'text', text: text(raw.text) }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const field = (raw: unknown, seen: Seen, inList = false): Field | undefined => {
|
|
181
|
+
if (!isRecord(raw) || typeof raw.name !== 'string' || typeof raw.kind !== 'string') {
|
|
182
|
+
return undefined
|
|
183
|
+
}
|
|
184
|
+
if (!FIELD_KINDS.has(raw.kind) || (inList && !LIST_SUB_KINDS.has(raw.kind))) {
|
|
185
|
+
seen.dropped.add(inList ? `field ${raw.kind} in a list` : `field ${raw.kind}`)
|
|
186
|
+
return undefined
|
|
187
|
+
}
|
|
188
|
+
const base: Record<string, unknown> = { kind: raw.kind, name: raw.name, label: text(raw.label) }
|
|
189
|
+
const hint = optionalText(raw.hint)
|
|
190
|
+
if (hint) base.hint = hint
|
|
191
|
+
const placeholder = optionalText(raw.placeholder)
|
|
192
|
+
if (placeholder) base.placeholder = placeholder
|
|
193
|
+
if (raw.required === true) base.required = true
|
|
194
|
+
if (raw.readOnly === true) base.readOnly = true
|
|
195
|
+
const visible = condition(raw.visibleWhen)
|
|
196
|
+
if (visible) base.visibleWhen = visible
|
|
197
|
+
const hints = list(raw.hints, LIMIT.fields).flatMap((entry) => {
|
|
198
|
+
if (!isRecord(entry)) return []
|
|
199
|
+
const when = condition(entry.when)
|
|
200
|
+
return when ? [{ when, text: text(entry.text) }] : []
|
|
201
|
+
})
|
|
202
|
+
if (hints.length > 0) base.hints = hints
|
|
203
|
+
switch (raw.kind) {
|
|
204
|
+
case 'text': {
|
|
205
|
+
if (raw.multiline === true) base.multiline = true
|
|
206
|
+
if (typeof raw.pattern === 'string') {
|
|
207
|
+
try {
|
|
208
|
+
new RegExp(raw.pattern, 'u')
|
|
209
|
+
base.pattern = raw.pattern
|
|
210
|
+
} catch {}
|
|
211
|
+
}
|
|
212
|
+
for (const key of ['minLength', 'maxLength'] as const) {
|
|
213
|
+
const n = finite(raw[key])
|
|
214
|
+
if (n !== undefined && n >= 0) base[key] = Math.floor(n)
|
|
215
|
+
}
|
|
216
|
+
break
|
|
217
|
+
}
|
|
218
|
+
case 'number':
|
|
219
|
+
for (const key of ['min', 'max', 'step'] as const) {
|
|
220
|
+
const n = finite(raw[key])
|
|
221
|
+
if (n !== undefined) base[key] = n
|
|
222
|
+
}
|
|
223
|
+
break
|
|
224
|
+
case 'list': {
|
|
225
|
+
base.fields = list(raw.fields, LIMIT.fields)
|
|
226
|
+
.map((entry) => field(entry, seen, true))
|
|
227
|
+
.filter((entry): entry is Field => entry !== undefined)
|
|
228
|
+
const label = optionalText(raw.addLabel)
|
|
229
|
+
if (label) base.addLabel = label
|
|
230
|
+
for (const key of ['min', 'max'] as const) {
|
|
231
|
+
const n = finite(raw[key])
|
|
232
|
+
if (n !== undefined && n >= 0) base[key] = Math.floor(n)
|
|
233
|
+
}
|
|
234
|
+
break
|
|
235
|
+
}
|
|
236
|
+
case 'choice':
|
|
237
|
+
base.options = list(raw.options, LIMIT.rows).flatMap((option) =>
|
|
238
|
+
isRecord(option) && typeof option.value === 'string'
|
|
239
|
+
? [{ value: option.value, label: text(option.label) }]
|
|
240
|
+
: [],
|
|
241
|
+
)
|
|
242
|
+
break
|
|
243
|
+
}
|
|
244
|
+
return base as unknown as Field
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const node = (raw: unknown, seen: Seen, inTab: boolean): Node | undefined => {
|
|
248
|
+
if (!isRecord(raw)) return undefined
|
|
249
|
+
const type = String(raw.type ?? '')
|
|
250
|
+
if (!NODE_TYPES.has(type) || (inTab && type === 'tabs')) {
|
|
251
|
+
seen.dropped.add(inTab && type === 'tabs' ? 'tabs inside tabs' : type || 'unnamed')
|
|
252
|
+
return undefined
|
|
253
|
+
}
|
|
254
|
+
switch (type) {
|
|
255
|
+
case 'table':
|
|
256
|
+
return {
|
|
257
|
+
type: 'table',
|
|
258
|
+
columns: list(raw.columns, 50).map(text),
|
|
259
|
+
rows: list(raw.rows, LIMIT.rows).flatMap((entry): Row[] =>
|
|
260
|
+
isRecord(entry)
|
|
261
|
+
? [
|
|
262
|
+
{
|
|
263
|
+
key: text(entry.key),
|
|
264
|
+
cells: list(entry.cells, 50).map(cell),
|
|
265
|
+
...(entry.actions ? { actions: buttons(entry.actions, seen) } : {}),
|
|
266
|
+
},
|
|
267
|
+
]
|
|
268
|
+
: [],
|
|
269
|
+
),
|
|
270
|
+
}
|
|
271
|
+
case 'form': {
|
|
272
|
+
const fields = list(raw.fields, LIMIT.fields)
|
|
273
|
+
.map((entry) => field(entry, seen))
|
|
274
|
+
.filter((entry): entry is Field => entry !== undefined)
|
|
275
|
+
// A secret is never sent to the browser, whatever the extension put in it.
|
|
276
|
+
const secrets = new Set(fields.filter((f) => f.kind === 'secret').map((f) => f.name))
|
|
277
|
+
const values = Object.fromEntries(
|
|
278
|
+
Object.entries(screenValues(raw.values)).filter(([key]) => !secrets.has(key)),
|
|
279
|
+
)
|
|
280
|
+
const title = optionalText(raw.title)
|
|
281
|
+
return {
|
|
282
|
+
type: 'form',
|
|
283
|
+
fields,
|
|
284
|
+
values,
|
|
285
|
+
actions: buttons(raw.actions, seen),
|
|
286
|
+
...(title ? { title } : {}),
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
case 'button':
|
|
290
|
+
return button(raw, seen)
|
|
291
|
+
case 'notice':
|
|
292
|
+
return { type: 'notice', text: text(raw.text) }
|
|
293
|
+
case 'problem':
|
|
294
|
+
return { type: 'problem', text: text(raw.text) }
|
|
295
|
+
case 'empty': {
|
|
296
|
+
const body = optionalText(raw.text)
|
|
297
|
+
return {
|
|
298
|
+
type: 'empty',
|
|
299
|
+
title: text(raw.title),
|
|
300
|
+
...(body ? { text: body } : {}),
|
|
301
|
+
...(raw.actions ? { actions: buttons(raw.actions, seen) } : {}),
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
case 'progress': {
|
|
305
|
+
const label = optionalText(raw.label)
|
|
306
|
+
const at = finite(raw.value)
|
|
307
|
+
return {
|
|
308
|
+
type: 'progress',
|
|
309
|
+
...(label ? { label } : {}),
|
|
310
|
+
...(at !== undefined ? { value: Math.min(100, Math.max(0, at)) } : {}),
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
case 'tabs':
|
|
314
|
+
return {
|
|
315
|
+
type: 'tabs',
|
|
316
|
+
tabs: list(raw.tabs, 20).flatMap((entry): Tab[] =>
|
|
317
|
+
isRecord(entry)
|
|
318
|
+
? [
|
|
319
|
+
{
|
|
320
|
+
id: text(entry.id),
|
|
321
|
+
label: text(entry.label),
|
|
322
|
+
body: nodes(entry.body, seen, true) as Tab['body'],
|
|
323
|
+
},
|
|
324
|
+
]
|
|
325
|
+
: [],
|
|
326
|
+
),
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return undefined
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const nodes = (raw: unknown, seen: Seen, inTab = false): Node[] =>
|
|
333
|
+
list(raw, LIMIT.nodes)
|
|
334
|
+
.map((entry) => node(entry, seen, inTab))
|
|
335
|
+
.filter((entry): entry is Node => entry !== undefined)
|
|
336
|
+
|
|
337
|
+
const toolbarItem = (raw: unknown, seen: Seen): Button | Search | undefined => {
|
|
338
|
+
if (isRecord(raw) && raw.type === 'search' && typeof raw.action === 'string') {
|
|
339
|
+
const placeholder = optionalText(raw.placeholder)
|
|
340
|
+
const current = optionalText(raw.value)
|
|
341
|
+
return {
|
|
342
|
+
type: 'search',
|
|
343
|
+
action: raw.action,
|
|
344
|
+
...(placeholder ? { placeholder } : {}),
|
|
345
|
+
...(current !== undefined ? { value: current } : {}),
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return button(raw, seen)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* A view's or an action's answer, held to the catalogue. `dropped` names what
|
|
353
|
+
* was left out, for the extension's log; it is never shown.
|
|
354
|
+
*/
|
|
355
|
+
export function normaliseScreenResult(answer: unknown): {
|
|
356
|
+
result: ScreenResult
|
|
357
|
+
dropped: string[]
|
|
358
|
+
} {
|
|
359
|
+
const seen: Seen = { dropped: new Set() }
|
|
360
|
+
const raw = isRecord(answer) && answer.type !== 'screen' ? answer.screen : answer
|
|
361
|
+
const outer = isRecord(answer) && answer.type !== 'screen' ? answer : {}
|
|
362
|
+
const source = isRecord(raw) ? raw : {}
|
|
363
|
+
const screen: Screen = {
|
|
364
|
+
type: 'screen',
|
|
365
|
+
title: text(source.title),
|
|
366
|
+
body: nodes(source.body, seen),
|
|
367
|
+
}
|
|
368
|
+
const toolbar = list(source.toolbar, 20)
|
|
369
|
+
.map((entry) => toolbarItem(entry, seen))
|
|
370
|
+
.filter((entry): entry is Button | Search => entry !== undefined)
|
|
371
|
+
if (toolbar.length > 0) screen.toolbar = toolbar
|
|
372
|
+
const result: ScreenResult = { screen }
|
|
373
|
+
if (isRecord(outer.state)) {
|
|
374
|
+
try {
|
|
375
|
+
result.state = JSON.parse(JSON.stringify(outer.state))
|
|
376
|
+
} catch {
|
|
377
|
+
seen.dropped.add('state that is not plain data')
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const notice = optionalText(outer.notice)
|
|
381
|
+
if (notice) result.notice = notice
|
|
382
|
+
return { result, dropped: [...seen.dropped] }
|
|
383
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who may grant what (story 324): an administrator grants an extension what it
|
|
3
|
+
* asks for, at any level; Acts as you and Unrestricted also need a package
|
|
4
|
+
* someone has vouched for; a host may keep Unrestricted to the site's config.
|
|
5
|
+
* One that asks for nothing needs no grant, so Settings access installs it.
|
|
6
|
+
* One function, so the panel, the API, the command line and a remote all refuse
|
|
7
|
+
* alike. docs/bundles.md, "Levels".
|
|
8
|
+
*/
|
|
9
|
+
import { type AccessLevel, isUpperLevel } from '@crombolo/backoffice-host/registry'
|
|
10
|
+
import type { ProvenanceResult } from './provenance.ts'
|
|
11
|
+
|
|
12
|
+
/** Where Unrestricted may be granted from: the back office, or only the site's config. */
|
|
13
|
+
export type UnrestrictedFrom = 'backoffice' | 'config'
|
|
14
|
+
|
|
15
|
+
/** What a host's `CROMBOLO_UNRESTRICTED_EXTENSIONS` says, `backoffice` unless it says `config`. */
|
|
16
|
+
export const unrestrictedFrom = (value: string | undefined): UnrestrictedFrom =>
|
|
17
|
+
value?.trim().toLowerCase() === 'config' ? 'config' : 'backoffice'
|
|
18
|
+
|
|
19
|
+
/** A package someone has vouched for: signed, vouched for by the host, or built here. */
|
|
20
|
+
export const vouchedFor = (provenance: ProvenanceResult | undefined): boolean =>
|
|
21
|
+
provenance?.state === 'signed' || provenance?.state === 'vouched' || provenance?.state === 'local'
|
|
22
|
+
|
|
23
|
+
export const ADMINISTRATOR_GRANTS = 'An administrator can turn this on.'
|
|
24
|
+
export const NOBODY_VOUCHED =
|
|
25
|
+
"Nobody has vouched for who built this, so it can't be turned on here."
|
|
26
|
+
export const CONFIG_GRANTS =
|
|
27
|
+
'This site turns on Unrestricted extensions only in its configuration, which your developers change.'
|
|
28
|
+
|
|
29
|
+
/** What the person may do about one level, as the panel shows it. */
|
|
30
|
+
export type MayGrant = 'yes' | 'administrator' | 'config'
|
|
31
|
+
|
|
32
|
+
/** What a person may grant at each level here. */
|
|
33
|
+
export function mayGrant(isAdmin: boolean, from: UnrestrictedFrom): Record<AccessLevel, MayGrant> {
|
|
34
|
+
return {
|
|
35
|
+
limited: isAdmin ? 'yes' : 'administrator',
|
|
36
|
+
'acts-as-you': isAdmin ? 'yes' : 'administrator',
|
|
37
|
+
unrestricted: from === 'config' ? 'config' : isAdmin ? 'yes' : 'administrator',
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Why this person may not grant this level of this package here, or undefined
|
|
43
|
+
* when they may. `grants` is whether it asks for anything at all. Pure.
|
|
44
|
+
*/
|
|
45
|
+
export function grantRefusal(options: {
|
|
46
|
+
level: AccessLevel
|
|
47
|
+
grants: boolean
|
|
48
|
+
isAdmin: boolean
|
|
49
|
+
provenance: ProvenanceResult | undefined
|
|
50
|
+
from: UnrestrictedFrom
|
|
51
|
+
}): string | undefined {
|
|
52
|
+
if (!options.grants) return undefined
|
|
53
|
+
if (options.level === 'unrestricted' && options.from === 'config') return CONFIG_GRANTS
|
|
54
|
+
if (!options.isAdmin) return ADMINISTRATOR_GRANTS
|
|
55
|
+
if (isUpperLevel(options.level) && !vouchedFor(options.provenance)) return NOBODY_VOUCHED
|
|
56
|
+
return undefined
|
|
57
|
+
}
|