forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodulePackLoader.ts
More file actions
226 lines (213 loc) · 8.33 KB
/
Copy pathmodulePackLoader.ts
File metadata and controls
226 lines (213 loc) · 8.33 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Plugin module pack loader.
*
* Activated alongside the plugin's editor entrypoint when the plugin has the
* `modules.register` permission and `entrypoints.modules` set. The default
* export of the module pack is either an array of `PluginModuleDefinition`
* or a function that returns one. The host wraps each definition into a
* full host `ModuleDefinition` (see `moduleAdapter.ts`) and registers it
* with the canvas registry.
*
* The caller injects a `componentFactory` so the editor side can supply a
* React-based preview component while the server side uses a stub. This
* keeps `src/core/` free of runtime React imports (Constraint #179).
*
* Lifecycle:
* - `activatePluginModulePack(manifest, mod, componentFactory)` — register
* every module declared by the plugin pack.
* - `deactivatePluginModulePack(pluginId)` — unregister every module that
* was registered for this plugin id.
*/
import type { ComponentType } from 'react'
import { registry } from '@core/module-engine'
import type {
ModuleComponentProps,
} from '@core/module-engine'
import type { PluginManifest } from '@core/plugin-sdk'
import type {
PluginEditorRuntime,
PluginModuleDefinition,
PluginModuleDependencies,
PluginModulePackEntrypoint,
PluginModulesEntrypointModule,
} from '@core/plugin-sdk'
import { assertPluginPermission } from '@core/plugin-sdk'
import {
pluginModuleToHostModule,
PluginModuleValidationError,
type PluginModuleComponentFactory,
} from './moduleAdapter'
/**
* Track which module ids were registered by which plugin so we can remove
* them cleanly on deactivate. Keyed by plugin id.
*/
const registeredByPlugin = new Map<string, Set<string>>()
// Live sandboxed VMs by plugin id. Tracked so every lifecycle teardown can
// dispose the QuickJS context — the emscripten runtime is not reclaimed by JS
// GC, so without this each activate/upgrade/restart cycle leaks one native
// context for the host-process lifetime (ISS-033). Stays empty on the browser
// editor path (activatePluginModulePack), where there is no VM.
const packsByPlugin = new Map<string, SandboxedModulePack>()
function resolveDefinitions(
pluginId: string,
entry: PluginModulePackEntrypoint,
): PluginModuleDefinition[] {
const value = typeof entry === 'function' ? entry({ pluginId }) : entry
if (!Array.isArray(value)) {
throw new PluginModuleValidationError(
`Plugin "${pluginId}" module pack entrypoint must default-export an array (or a function returning one).`,
pluginId,
)
}
return value
}
/**
* Stub component used on the server. The publisher never reads
* `definition.component`, but the type system requires one — so we hand back
* an opaque `ComponentType` that throws if invoked. Type-only React imports
* keep this file out of the runtime-React ban for `src/core/`.
*/
const STUB_COMPONENT_FACTORY: PluginModuleComponentFactory = () => {
return ((): never => {
throw new Error('Plugin module React component is not available on the server.')
}) as unknown as ComponentType<ModuleComponentProps>
}
export function activatePluginModulePack(
manifest: PluginManifest,
mod: PluginModulesEntrypointModule,
componentFactory: PluginModuleComponentFactory = STUB_COMPONENT_FACTORY,
): void {
assertPluginPermission(manifest, 'modules.register')
const definitions = resolveDefinitions(manifest.id, mod.default)
// Replace any previous registrations for this plugin id atomically.
deactivatePluginModulePack(manifest.id)
const ids = new Set<string>()
for (const definition of definitions) {
const hostModule = pluginModuleToHostModule(
manifest.id,
definition,
componentFactory,
manifest.grantedPermissions ?? [],
)
registry.registerOrReplace(hostModule)
ids.add(hostModule.id)
}
registeredByPlugin.set(manifest.id, ids)
}
export function deactivatePluginModulePack(pluginId: string): void {
const pack = packsByPlugin.get(pluginId)
if (pack) {
try {
pack.dispose()
} catch (err) {
console.error(`[plugin:${pluginId}] module pack dispose failed`, err)
}
packsByPlugin.delete(pluginId)
}
const ids = registeredByPlugin.get(pluginId)
if (!ids) return
for (const id of ids) registry.unregister(id)
registeredByPlugin.delete(pluginId)
}
export function listPluginRegisteredModuleIds(pluginId: string): string[] {
return [...(registeredByPlugin.get(pluginId) ?? [])]
}
export function resetPluginModulePacks(): void {
for (const pack of packsByPlugin.values()) {
try {
pack.dispose()
} catch (err) {
console.error(`[plugin:${pack.pluginId}] module pack dispose failed`, err)
}
}
packsByPlugin.clear()
for (const ids of registeredByPlugin.values()) {
for (const id of ids) registry.unregister(id)
}
registeredByPlugin.clear()
}
// ---------------------------------------------------------------------------
// Sandboxed activation — server-side
// ---------------------------------------------------------------------------
/**
* Minimal interface for a sandboxed module pack. Matches the shape of
* `server/plugins/modulePackVm.ts:ModulePackVm` but is declared here so
* `src/core/` stays free of the server's QuickJS dependency at the type
* level. The runtime instance is created by the server and handed in.
*/
export interface SandboxedModulePack {
readonly pluginId: string
readonly modules: ReadonlyArray<{
id: string
name: string
description?: string
category: string
version: string
defaults: Record<string, unknown>
schema: Record<string, unknown>
canHaveChildren?: boolean
htmlTag?: string
hasPreview: boolean
/** Package dependencies declared by the module — surfaced in the Dependencies Panel. */
dependencies?: PluginModuleDependencies
/** Optional iframe-backed editor preview source. */
editorRuntime?: PluginEditorRuntime
}>
render(moduleId: string, props: Record<string, unknown>, children: string[]): { html: string; css?: string; js?: string }
preview(moduleId: string, props: Record<string, unknown>, children: string[]): { html: string; css?: string; js?: string }
dispose(): void
}
/**
* Register every module in a sandboxed pack. Used by the server-side
* lifecycle handler. Each module's render is a thunk that calls back into
* the QuickJS VM — plugin render code never touches the host process.
*
* The browser path uses `activatePluginModulePack(manifest, mod, factory)`
* which evaluates the pack in the browser's JS context. That isn't a
* security boundary (XSS scope, not RCE), so no VM there.
*/
export function activateSandboxedPluginModulePack(
manifest: PluginManifest,
pack: SandboxedModulePack,
): void {
assertPluginPermission(manifest, 'modules.register')
// Replace any previous registrations for this plugin id atomically.
deactivatePluginModulePack(manifest.id)
const ids = new Set<string>()
for (const meta of pack.modules) {
// Synthesize a `PluginModuleDefinition` from the VM's metadata + a
// render thunk that calls back into the sandbox. The host wrapper
// (`pluginModuleToHostModule`) catches errors from the thunk.
const definition: PluginModuleDefinition = {
id: meta.id,
name: meta.name,
description: meta.description,
category: meta.category,
version: meta.version,
defaults: meta.defaults,
// Schema shape from the SDK is `PluginPropertySchema` — already
// serializable JSON, cast through `unknown` is safe.
schema: meta.schema as unknown as PluginModuleDefinition['schema'],
canHaveChildren: meta.canHaveChildren,
htmlTag: meta.htmlTag,
...(meta.dependencies ? { dependencies: meta.dependencies } : {}),
...(meta.editorRuntime ? { editorRuntime: meta.editorRuntime } : {}),
render: (props, children) => pack.render(meta.id, props, children),
preview: meta.hasPreview
? (props, children) => pack.preview(meta.id, props, children)
: undefined,
}
const hostModule = pluginModuleToHostModule(
manifest.id,
definition,
STUB_COMPONENT_FACTORY,
manifest.grantedPermissions ?? [],
)
registry.registerOrReplace(hostModule)
ids.add(hostModule.id)
}
registeredByPlugin.set(manifest.id, ids)
// Track the live VM so the next deactivate/reset disposes it (the
// deactivate above already disposed any prior pack for this id).
packsByPlugin.set(manifest.id, pack)
}