-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordpress-workload-primitives.ts
More file actions
199 lines (178 loc) · 8.2 KB
/
Copy pathwordpress-workload-primitives.ts
File metadata and controls
199 lines (178 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { commandArg, commandJsonArg } from "./command-codecs.js"
import { DEFAULT_WORDPRESS_VERSION } from "./runtime-defaults.js"
import type { RuntimePreviewSpec, WorkspaceRecipe, WorkspaceRecipeMount, WorkspaceRecipeRuntimeOverlay, WorkspaceRecipeStagedFile, WorkspaceRecipeStep } from "./runtime-contracts.js"
import { isPlainObject, stripUndefined } from "./object-utils.js"
import { performanceObservationCaptureRequest, type PerformanceObservationCaptureRequest } from "./performance-observation.js"
export const WORDPRESS_WORKLOAD_RUN_SCHEMA = "wp-codebox/wordpress-workload-run/v1" as const
export const WORDPRESS_ABILITY_STEP_SCHEMA = "wp-codebox/wordpress-ability-step/v1" as const
export const PLAYGROUND_PREVIEW_URL_SCHEMA = "wp-codebox/playground-preview-url/v1" as const
export type PlaygroundPreviewUrlMode = "local" | "public" | "secure"
export interface WordPressAbilityStepOptions {
name: string
input?: Record<string, unknown>
expectedResultSchema?: string | Record<string, unknown>
allowFailure?: boolean
advisory?: boolean
}
export interface WordPressWorkloadRunRecipeOptions {
wordpressVersion?: string
/** Local WordPress core source mounted at /wordpress before Playground starts. */
wordpressDirectory?: string
blueprint?: unknown
preview?: RuntimePreviewSpec
mounts?: WorkspaceRecipeMount[]
runtimeStackMounts?: WorkspaceRecipeMount[]
runtimeOverlays?: WorkspaceRecipeRuntimeOverlay[]
runtimeEnv?: Record<string, string | number | boolean>
secretEnv?: string[]
stagedFiles?: WorkspaceRecipeStagedFile[]
before?: WorkspaceRecipeStep[]
steps: WorkspaceRecipeStep[]
after?: WorkspaceRecipeStep[]
metadata?: Record<string, unknown>
capture?: PerformanceObservationCaptureRequest
enableQueryCapture?: boolean
}
export interface PlaygroundPreviewUrlOptions {
localUrl: string
publicUrl?: string
path?: string
mode?: PlaygroundPreviewUrlMode
}
export interface PlaygroundPreviewUrlContract {
schema: typeof PLAYGROUND_PREVIEW_URL_SCHEMA
mode: PlaygroundPreviewUrlMode
localUrl: string
effectiveUrl: string
publicUrl?: string
diagnostics: Array<{ code: string; severity: "info" | "warning" | "error"; message: string }>
}
export function wordpressAbilityStep(options: WordPressAbilityStepOptions): WorkspaceRecipeStep {
const name = stringValue(options.name)
if (!name) {
throw new Error("wordpressAbilityStep requires name")
}
return stripUndefined({
command: "wordpress.ability",
args: [
commandArg("name", name),
commandJsonArg("input", isPlainObject(options.input) ? options.input : {}),
...(options.expectedResultSchema ? [commandJsonArg("expected-result-schema", options.expectedResultSchema)] : []),
],
allowFailure: options.allowFailure,
advisory: options.advisory,
}) as WorkspaceRecipeStep
}
export function wordpressWorkloadRunRecipe(options: WordPressWorkloadRunRecipeOptions): WorkspaceRecipe {
if (!Array.isArray(options.steps) || options.steps.length === 0) {
throw new Error("wordpressWorkloadRunRecipe requires at least one step")
}
return stripUndefined({
schema: "wp-codebox/workspace-recipe/v1",
runtime: stripUndefined({
backend: "wordpress-playground",
wp: options.wordpressVersion ?? DEFAULT_WORDPRESS_VERSION,
assets: options.wordpressDirectory ? { wordpressDirectory: options.wordpressDirectory } : undefined,
blueprint: options.blueprint ?? { steps: [] },
preview: options.preview,
stack: Array.isArray(options.runtimeStackMounts) && options.runtimeStackMounts.length > 0 ? { mounts: options.runtimeStackMounts } : undefined,
overlays: Array.isArray(options.runtimeOverlays) && options.runtimeOverlays.length > 0 ? options.runtimeOverlays : undefined,
}),
inputs: stripUndefined({
mounts: Array.isArray(options.mounts) ? options.mounts : [],
runtimeEnv: runtimeEnv(options.runtimeEnv),
secretEnv: stringList(options.secretEnv),
stagedFiles: Array.isArray(options.stagedFiles) && options.stagedFiles.length > 0 ? options.stagedFiles : undefined,
}),
workflow: stripUndefined({
before: normalizeRecipeSteps(options.before),
steps: normalizeRecipeSteps(options.steps),
after: normalizeRecipeSteps(options.after),
}),
metadata: stripUndefined({
...options.metadata,
public_contract: WORDPRESS_WORKLOAD_RUN_SCHEMA,
capture: normalizedCapture(options),
}),
}) as WorkspaceRecipe
}
export const buildWordPressWorkloadRunRecipe = wordpressWorkloadRunRecipe
export function playgroundPreviewUrl(options: PlaygroundPreviewUrlOptions): PlaygroundPreviewUrlContract {
const localUrl = normalizedHttpUrl(options.localUrl, "localUrl")
const publicUrl = options.publicUrl ? normalizedHttpUrl(options.publicUrl, "publicUrl") : undefined
const mode = options.mode ?? (publicUrl ? "public" : "local")
if (mode !== "local" && mode !== "public" && mode !== "secure") {
throw new Error(`playgroundPreviewUrl mode must be local, public, or secure: ${String(options.mode)}`)
}
const diagnostics: PlaygroundPreviewUrlContract["diagnostics"] = []
if ((mode === "public" || mode === "secure") && !publicUrl) {
diagnostics.push({ code: "preview-public-url-missing", severity: "error", message: `playgroundPreviewUrl mode=${mode} requires publicUrl` })
}
if (mode === "secure" && publicUrl && new URL(publicUrl).protocol !== "https:") {
diagnostics.push({ code: "preview-public-url-not-https", severity: "error", message: "playgroundPreviewUrl mode=secure requires an HTTPS publicUrl" })
}
const baseUrl = diagnostics.some((diagnostic) => diagnostic.severity === "error") || mode === "local" ? localUrl : publicUrl ?? localUrl
return stripUndefined({
schema: PLAYGROUND_PREVIEW_URL_SCHEMA,
mode,
localUrl,
publicUrl,
effectiveUrl: resolveUrl(options.path ?? "/", baseUrl),
diagnostics,
}) as PlaygroundPreviewUrlContract
}
function normalizeRecipeSteps(steps: WorkspaceRecipeStep[] | undefined): WorkspaceRecipeStep[] | undefined {
if (!Array.isArray(steps) || steps.length === 0) return undefined
return steps.map((step, index) => {
const command = stringValue(step.command)
if (!command) {
throw new Error(`wordpressWorkloadRunRecipe step ${index} requires command`)
}
return stripUndefined({
command,
args: Array.isArray(step.args) ? step.args.map((arg) => String(arg)) : undefined,
allowFailure: step.allowFailure,
advisory: step.advisory,
}) as WorkspaceRecipeStep
})
}
function runtimeEnv(value: WordPressWorkloadRunRecipeOptions["runtimeEnv"]): Record<string, string> | undefined {
if (!isPlainObject(value)) return undefined
const entries = Object.entries(value)
.map(([name, entry]) => [name.trim(), typeof entry === "boolean" ? (entry ? "1" : "") : String(entry)] as const)
.filter(([name]) => /^[A-Z_][A-Z0-9_]*$/.test(name))
return entries.length > 0 ? Object.fromEntries(entries) : undefined
}
function normalizedHttpUrl(value: string, label: string): string {
try {
const url = new URL(value)
if (url.protocol !== "http:" && url.protocol !== "https:") {
throw new Error("unsupported protocol")
}
return url.toString()
} catch (error) {
throw new Error(`${label} must be an http(s) URL: ${error instanceof Error ? error.message : String(error)}`)
}
}
function resolveUrl(pathOrUrl: string, baseUrl: string): string {
try {
return normalizedHttpUrl(pathOrUrl, "path")
} catch {
return new URL(pathOrUrl, baseUrl).toString()
}
}
function stringList(value: unknown): string[] | undefined {
if (!Array.isArray(value)) return undefined
const items = [...new Set(value.map((item) => String(item).trim()).filter(Boolean))]
return items.length > 0 ? items : undefined
}
function stringValue(value: unknown): string {
return value === undefined || value === null ? "" : String(value).trim()
}
function normalizedCapture(options: Pick<WordPressWorkloadRunRecipeOptions, "capture" | "enableQueryCapture">): PerformanceObservationCaptureRequest | undefined {
const capture = performanceObservationCaptureRequest(options.capture)
if (typeof options.enableQueryCapture === "boolean") {
capture.queries = options.enableQueryCapture
}
return Object.keys(capture).length > 0 ? capture : undefined
}