@linxin666/dsh-client-ui-task-board-ssh 0.0.0-stage → 0.4.21
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/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +96 -2
- package/README.zh.md +97 -0
- package/cordis.patch.yml +15 -0
- package/icon.svg +5 -0
- package/lib/client.js +4435 -0
- package/lib/index.js +7082 -0
- package/lib/types/client/PluginSettingsCard.d.ts +255 -0
- package/lib/types/client/RangeBubble.d.ts +46 -0
- package/lib/types/client/SegmentGroup.d.ts +28 -0
- package/lib/types/client/index.d.ts +100 -0
- package/lib/types/client/locales.d.ts +125 -0
- package/lib/types/client/seat-props.d.ts +53 -0
- package/lib/types/client/settings-entry-form.d.ts +49 -0
- package/lib/types/client/settings-form.d.ts +237 -0
- package/lib/types/client/ssh/card-decoration.d.ts +23 -0
- package/lib/types/client/ssh/compose-section.d.ts +7 -0
- package/lib/types/client/ssh/detail-section.d.ts +7 -0
- package/lib/types/client/ssh/extension.d.ts +54 -0
- package/lib/types/client/ssh/log-buffer.d.ts +128 -0
- package/lib/types/client/ssh/log-view.d.ts +37 -0
- package/lib/types/client/ssh/model.d.ts +130 -0
- package/lib/types/client/ssh/settings-section.d.ts +134 -0
- package/lib/types/client/ssh/summary.d.ts +160 -0
- package/lib/types/client/ssh/target-form.d.ts +60 -0
- package/lib/types/core/action.d.ts +89 -0
- package/lib/types/core/adapters/claude.d.ts +15 -0
- package/lib/types/core/adapters/codex.d.ts +15 -0
- package/lib/types/core/adapters/custom.d.ts +29 -0
- package/lib/types/core/adapters/dsh.d.ts +21 -0
- package/lib/types/core/adapters/index.d.ts +25 -0
- package/lib/types/core/adapters/types.d.ts +76 -0
- package/lib/types/core/contract.d.ts +453 -0
- package/lib/types/core/launcher-script.d.ts +13 -0
- package/lib/types/core/launcher.d.ts +153 -0
- package/lib/types/core/ledger.d.ts +123 -0
- package/lib/types/core/shell.d.ts +61 -0
- package/lib/types/core/target.d.ts +149 -0
- package/lib/types/core/task-record.d.ts +74 -0
- package/lib/types/core/workspace.d.ts +74 -0
- package/lib/types/host/board.d.ts +75 -0
- package/lib/types/host/engine.d.ts +563 -0
- package/lib/types/host/extension.d.ts +61 -0
- package/lib/types/host/inspect.d.ts +99 -0
- package/lib/types/host/logs.d.ts +135 -0
- package/lib/types/host/payload.d.ts +75 -0
- package/lib/types/host/prompt.d.ts +44 -0
- package/lib/types/host/queue.d.ts +53 -0
- package/lib/types/host/remote.d.ts +137 -0
- package/lib/types/host/settings.d.ts +149 -0
- package/lib/types/host/timers.d.ts +20 -0
- package/lib/types/host/tools.d.ts +17 -0
- package/lib/types/host/workspace/errors.d.ts +29 -0
- package/lib/types/host/workspace/git.d.ts +108 -0
- package/lib/types/host/workspace/in-place.d.ts +36 -0
- package/lib/types/host/workspace/index.d.ts +28 -0
- package/lib/types/host/workspace/layout.d.ts +62 -0
- package/lib/types/host/workspace/local-git.d.ts +72 -0
- package/lib/types/host/workspace/remote-git.d.ts +111 -0
- package/lib/types/host/workspace/remote.d.ts +85 -0
- package/lib/types/host/workspace/types.d.ts +28 -0
- package/lib/types/host/workspace-seam.d.ts +100 -0
- package/lib/types/index.d.ts +97 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +89 -3
- package/src/client/PluginSettingsCard.tsx +838 -0
- package/src/client/RangeBubble.tsx +164 -0
- package/src/client/SegmentGroup.tsx +93 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +195 -0
- package/src/client/locales.ts +257 -0
- package/src/client/seat-props.ts +58 -0
- package/src/client/settings-card.module.css +821 -0
- package/src/client/settings-entry-form.ts +141 -0
- package/src/client/settings-form.ts +529 -0
- package/src/client/ssh/card-decoration.tsx +55 -0
- package/src/client/ssh/compose-section.tsx +147 -0
- package/src/client/ssh/detail-section.tsx +345 -0
- package/src/client/ssh/extension.ts +271 -0
- package/src/client/ssh/log-buffer.ts +224 -0
- package/src/client/ssh/log-view.tsx +149 -0
- package/src/client/ssh/model.ts +299 -0
- package/src/client/ssh/settings-section.tsx +401 -0
- package/src/client/ssh/summary.ts +388 -0
- package/src/client/ssh/target-form.tsx +380 -0
- package/src/client/ssh.module.css +617 -0
- package/src/core/action.ts +270 -0
- package/src/core/adapters/claude.ts +64 -0
- package/src/core/adapters/codex.ts +83 -0
- package/src/core/adapters/custom.ts +88 -0
- package/src/core/adapters/dsh.ts +87 -0
- package/src/core/adapters/index.ts +74 -0
- package/src/core/adapters/types.ts +95 -0
- package/src/core/contract.ts +515 -0
- package/src/core/launcher-script.ts +313 -0
- package/src/core/launcher.ts +283 -0
- package/src/core/ledger.ts +377 -0
- package/src/core/shell.ts +77 -0
- package/src/core/target.ts +420 -0
- package/src/core/task-record.ts +73 -0
- package/src/core/workspace.ts +78 -0
- package/src/host/board.ts +107 -0
- package/src/host/engine.ts +2026 -0
- package/src/host/extension.ts +310 -0
- package/src/host/inspect.ts +264 -0
- package/src/host/logs.ts +297 -0
- package/src/host/payload.ts +134 -0
- package/src/host/prompt.ts +89 -0
- package/src/host/queue.ts +73 -0
- package/src/host/remote.ts +356 -0
- package/src/host/settings.ts +294 -0
- package/src/host/timers.ts +33 -0
- package/src/host/tools.ts +449 -0
- package/src/host/workspace/errors.ts +43 -0
- package/src/host/workspace/git.ts +305 -0
- package/src/host/workspace/in-place.ts +58 -0
- package/src/host/workspace/index.ts +37 -0
- package/src/host/workspace/layout.ts +122 -0
- package/src/host/workspace/local-git.ts +164 -0
- package/src/host/workspace/remote-git.ts +244 -0
- package/src/host/workspace/remote.ts +181 -0
- package/src/host/workspace/types.ts +34 -0
- package/src/host/workspace-seam.ts +132 -0
- package/src/index.ts +243 -0
- package/src/mount-once.ts +142 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of the task-board SSH provider extension.
|
|
3
|
+
*
|
|
4
|
+
* The row registers one provider with the task board: a card that pins a
|
|
5
|
+
* `dsh-ssh` host runs on that host instead of in a local session. Both services
|
|
6
|
+
* it needs are taken through one cordis dependency scope, so a deployment that
|
|
7
|
+
* serves neither — or mounts them later — keeps starting normally and gains the
|
|
8
|
+
* provider the moment both are there, with nothing registered before then.
|
|
9
|
+
*
|
|
10
|
+
* What this file owns and nothing else does: the Config schema (every field
|
|
11
|
+
* `volatile()`, so a settings edit binds the next run without a restart), the
|
|
12
|
+
* scope wiring, and the single configuration write path. The engine, its remote
|
|
13
|
+
* operations, its tools and its action routing live in `src/host/`.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-task-board-ssh
|
|
16
|
+
*/
|
|
17
|
+
import type { Context, Volatile } from '@deepseek-ai/cordis'
|
|
18
|
+
import z from '@deepseek-ai/schemastery'
|
|
19
|
+
import { resolveDshSshService, resolveTaskBoardHostFace } from './core/contract.ts'
|
|
20
|
+
import { SSH_AGENT_KINDS, type SshAgentKind, type SshExecutionMode } from './core/target.ts'
|
|
21
|
+
import { resolveBoardActionSeat } from './host/board.ts'
|
|
22
|
+
import { SSH_EXTENSION_ID, createSshExtension } from './host/extension.ts'
|
|
23
|
+
import { mountOnce } from './mount-once.ts'
|
|
24
|
+
import {
|
|
25
|
+
parseSshSettings,
|
|
26
|
+
readSettingsFace,
|
|
27
|
+
SshSettingsWriter,
|
|
28
|
+
type SettingsFace,
|
|
29
|
+
type SshProviderSettings,
|
|
30
|
+
} from './host/settings.ts'
|
|
31
|
+
import { processTimers } from './host/timers.ts'
|
|
32
|
+
import { createSshWorkspaceStrategy } from './host/workspace/index.ts'
|
|
33
|
+
import {
|
|
34
|
+
readWorkspaceDirectory,
|
|
35
|
+
resolveWorkspaceRegistry,
|
|
36
|
+
type SshWorkspaceStrategyResolver,
|
|
37
|
+
} from './host/workspace-seam.ts'
|
|
38
|
+
|
|
39
|
+
export { SSH_EXTENSION_ID }
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* npm identity of this package. The host single-instance guard keys on it, so an
|
|
43
|
+
* aggregate install and a standalone install do not double-register.
|
|
44
|
+
*/
|
|
45
|
+
export const PACKAGE_NAME = '@linxin666/dsh-client-ui-task-board-ssh'
|
|
46
|
+
|
|
47
|
+
/** One configured agent command override. */
|
|
48
|
+
export interface SshAgentCommandInput {
|
|
49
|
+
/** Agent CLI the override applies to. */
|
|
50
|
+
kind: SshAgentKind
|
|
51
|
+
/** Start template; must reference `{prompt_file}`. */
|
|
52
|
+
command: string
|
|
53
|
+
/** Resume template; without one a follow-up runs fresh. */
|
|
54
|
+
resumeCommand?: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Plugin config, validated by the same-named schemastery schema. */
|
|
58
|
+
export interface Config {
|
|
59
|
+
/** Master switch of the extension. */
|
|
60
|
+
enabled?: Volatile<boolean>
|
|
61
|
+
/** Host alias a card opens with. */
|
|
62
|
+
defaultHost?: Volatile<string>
|
|
63
|
+
/** Agent CLI a card opens with. */
|
|
64
|
+
defaultAgent?: Volatile<SshAgentKind>
|
|
65
|
+
/** Execution mode a card opens with. */
|
|
66
|
+
defaultMode?: Volatile<SshExecutionMode>
|
|
67
|
+
/** How many runs one host executes at once. */
|
|
68
|
+
perHostCap?: Volatile<number>
|
|
69
|
+
/** Per-agent command templates that replace the built-in command line. */
|
|
70
|
+
agentCommands?: Volatile<SshAgentCommandInput[]>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Profile-patch shape of {@link Config}. */
|
|
74
|
+
export interface ConfigInput {
|
|
75
|
+
enabled?: boolean
|
|
76
|
+
defaultHost?: string
|
|
77
|
+
defaultAgent?: SshAgentKind
|
|
78
|
+
defaultMode?: SshExecutionMode
|
|
79
|
+
perHostCap?: number
|
|
80
|
+
agentCommands?: SshAgentCommandInput[]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare module '@deepseek-ai/cordis' {
|
|
84
|
+
interface Events {
|
|
85
|
+
/**
|
|
86
|
+
* Volatile config values were committed into the running fiber without a
|
|
87
|
+
* remount; dispatched to the owning fiber only.
|
|
88
|
+
* @param paths - the changed config paths, as key arrays.
|
|
89
|
+
* @mode emit
|
|
90
|
+
*/
|
|
91
|
+
'loader/volatile-update'(paths: readonly (readonly string[])[]): void
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Schema of one configured agent command override. */
|
|
96
|
+
const AgentCommandSchema = z.object({
|
|
97
|
+
kind: z.union(SSH_AGENT_KINDS).default('dsh'),
|
|
98
|
+
command: z.string().default(''),
|
|
99
|
+
resumeCommand: z.string().default(''),
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Every configuration field is volatile: a write is committed into the running
|
|
104
|
+
* fiber, and every consumer here reads it at use time, so the next admission,
|
|
105
|
+
* the next run and the next tool call all see the new value without a remount.
|
|
106
|
+
*/
|
|
107
|
+
export const Config: z<ConfigInput, Config> = z.object({
|
|
108
|
+
enabled: z.boolean().default(true).volatile(),
|
|
109
|
+
defaultHost: z.string().default('').volatile(),
|
|
110
|
+
defaultAgent: z.union(SSH_AGENT_KINDS).default('dsh').volatile(),
|
|
111
|
+
defaultMode: z.union(['direct', 'proxy'] as const).default('direct').volatile(),
|
|
112
|
+
perHostCap: z.number().min(1).max(16).default(2).volatile(),
|
|
113
|
+
agentCommands: z.array(AgentCommandSchema).default([]).volatile(),
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Activate the extension's host half.
|
|
118
|
+
*
|
|
119
|
+
* Registration lives behind one cordis dependency scope over both services the
|
|
120
|
+
* provider needs, because the Host loads plugin rows in an order this package
|
|
121
|
+
* does not own: either service may genuinely not exist yet when this row
|
|
122
|
+
* activates, and the scope mounts when both appear and unloads when either is
|
|
123
|
+
* withdrawn. Nothing is registered while it is down.
|
|
124
|
+
*
|
|
125
|
+
* The switch in that scope is this row's own volatile field, so it is re-derived
|
|
126
|
+
* here rather than by the board: the Loader commits a settings write into the
|
|
127
|
+
* running fiber and announces `loader/volatile-update` on this fiber, and this
|
|
128
|
+
* row tears its provider down (engine stopped, tools and launch handler
|
|
129
|
+
* withdrawn, published summary gone) or builds it again from that announcement.
|
|
130
|
+
* No card data is touched on the way out, exactly as the board's own switch
|
|
131
|
+
* semantics promise.
|
|
132
|
+
* @param ctx - the plugin context.
|
|
133
|
+
* @param config - resolved plugin config.
|
|
134
|
+
*/
|
|
135
|
+
function applyImpl(ctx: Context, config?: Config): void {
|
|
136
|
+
const settings = (): SshProviderSettings => parseSshSettings(config)
|
|
137
|
+
|
|
138
|
+
// The settings surface is optional: a deployment that serves none keeps the
|
|
139
|
+
// extension running, and only configuration writes are refused, by name.
|
|
140
|
+
let settingsFace: SettingsFace | undefined
|
|
141
|
+
ctx.inject(['settings'], (scope: Context) => {
|
|
142
|
+
scope.effect(() => {
|
|
143
|
+
const face = readSettingsFace((scope as unknown as { settings?: unknown }).settings)
|
|
144
|
+
if (face === undefined) return () => {}
|
|
145
|
+
settingsFace = face
|
|
146
|
+
return () => { settingsFace = undefined }
|
|
147
|
+
}, 'task-board-ssh: settings surface')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
// The one configuration write path: every caller composes a patch and hands it
|
|
151
|
+
// here, and here is the only place that reaches `ctx.settings.mutate`.
|
|
152
|
+
const settingsWriter = new SshSettingsWriter(() => settingsFace)
|
|
153
|
+
|
|
154
|
+
let injection: ReturnType<Context['inject']> | undefined
|
|
155
|
+
let notifySettings: (() => void) | undefined
|
|
156
|
+
let disposed = false
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Release the provider: its engine stops, its tools and launch handler are
|
|
160
|
+
* withdrawn with the registration, and the summary it published leaves the
|
|
161
|
+
* board snapshot with the entry. Every card keeps its data.
|
|
162
|
+
*/
|
|
163
|
+
const teardownProvider = async (): Promise<void> => {
|
|
164
|
+
const current = injection
|
|
165
|
+
injection = undefined
|
|
166
|
+
if (current !== undefined) await current.dispose()
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Admit the provider again, once, against the services as they stand now. */
|
|
170
|
+
const mountProvider = (): void => {
|
|
171
|
+
if (disposed || injection !== undefined) return
|
|
172
|
+
injection = ctx.inject(['taskBoard', 'dshSsh'], (scope: Context) => {
|
|
173
|
+
scope.effect(() => {
|
|
174
|
+
const board = resolveTaskBoardHostFace(scope)
|
|
175
|
+
const ssh = resolveDshSshService(scope)
|
|
176
|
+
if (board === undefined || ssh === undefined) return () => {}
|
|
177
|
+
// The placement strategies are assembled behind one entry
|
|
178
|
+
// (`src/host/workspace/index.ts`), which is the only thing this file knows
|
|
179
|
+
// about them: the engine holds the frozen `SshWorkspaceStrategy` from
|
|
180
|
+
// `src/core/workspace.ts` and never a strategy implementation.
|
|
181
|
+
const workspaces: SshWorkspaceStrategyResolver = target => createSshWorkspaceStrategy(target)
|
|
182
|
+
// A git run whose repository is the local workspace needs that
|
|
183
|
+
// repository's directory, and the deployment's workspace registry is the
|
|
184
|
+
// only thing that knows it. The same service the board resolves for its
|
|
185
|
+
// own runs, read optionally: a deployment without it keeps mounting, and
|
|
186
|
+
// a run pinned to a local workspace then fails with the strategy's own
|
|
187
|
+
// "was not resolved".
|
|
188
|
+
const localRepository = (workspaceId: string): string | undefined =>
|
|
189
|
+
readWorkspaceDirectory(resolveWorkspaceRegistry(scope), workspaceId)
|
|
190
|
+
const dispose = board.registerExtension(createSshExtension({
|
|
191
|
+
ssh,
|
|
192
|
+
settings,
|
|
193
|
+
settingsWriter,
|
|
194
|
+
timers: processTimers,
|
|
195
|
+
// The applier belongs to the board service, not to the read face this
|
|
196
|
+
// package restates, so it is resolved off the same service by name.
|
|
197
|
+
seat: () => resolveBoardActionSeat(scope),
|
|
198
|
+
workspaces,
|
|
199
|
+
localRepository,
|
|
200
|
+
onSettingsHook: hook => { notifySettings = hook },
|
|
201
|
+
}))
|
|
202
|
+
return () => { dispose() }
|
|
203
|
+
}, 'task-board-ssh: provider registration')
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Bring the provider in line with the switch, waiting out any release first. */
|
|
208
|
+
const sync = async (): Promise<void> => {
|
|
209
|
+
if (disposed) return
|
|
210
|
+
if (!settings().enabled) {
|
|
211
|
+
await teardownProvider()
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
if (injection === undefined) {
|
|
215
|
+
mountProvider()
|
|
216
|
+
} else {
|
|
217
|
+
// Volatile setting edits (e.g. raising perHostCap or changing defaults)
|
|
218
|
+
// wake queued runs and refresh the published summary immediately.
|
|
219
|
+
notifySettings?.()
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// A settings write and a lifecycle step can arrive together, and a re-mount
|
|
224
|
+
// must not race the unload of the scope it replaces: one step at a time.
|
|
225
|
+
let chain: Promise<void> = Promise.resolve()
|
|
226
|
+
const requestSync = (): void => {
|
|
227
|
+
chain = chain.then(() => sync()).catch(error => {
|
|
228
|
+
console.error('[dsh-task-board-ssh] the provider could not be re-derived from its settings', error)
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
ctx.on('loader/volatile-update', () => { requestSync() })
|
|
233
|
+
|
|
234
|
+
ctx.effect(() => {
|
|
235
|
+
requestSync()
|
|
236
|
+
return () => {
|
|
237
|
+
disposed = true
|
|
238
|
+
void teardownProvider()
|
|
239
|
+
}
|
|
240
|
+
}, 'task-board-ssh: provider lifecycle')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export const apply = mountOnce(PACKAGE_NAME, applyImpl)
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Generated by scripts/sync-shared.mjs from shared/host/mount-once.ts. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
|
|
2
|
+
/**
|
|
3
|
+
* Host single-instance guard shared by the plugin family. The family bundle
|
|
4
|
+
* (dsh-web-all / dsh-skins) namespaces every child row id (web-ui-*), so
|
|
5
|
+
* the loader accepts a standalone install of the same package side by side;
|
|
6
|
+
* without this guard the second instance would still re-register the same
|
|
7
|
+
* webserver routes, tools, settings namespaces, and system-prompt sections
|
|
8
|
+
* and fail the boot. mountOnce makes a mount of an already-mounted package a
|
|
9
|
+
* no-op for as long as the first instance lives (the browser half is already
|
|
10
|
+
* deduped by package name in the client module host).
|
|
11
|
+
*
|
|
12
|
+
* A no-op is only safe while the holder is ALIVE. The holder can be disposed
|
|
13
|
+
* long after the refused mount ran its course: the Host reloads a profile by
|
|
14
|
+
* creating the new loader entries before the old ones are torn down (a
|
|
15
|
+
* plugin-manager enable/disable/install write, a settings-driven row reload,
|
|
16
|
+
* HMR), so the new aggregate shell entry mounts its family plugin while the
|
|
17
|
+
* previous entry still owns the name. Dropping that refused mount lost the
|
|
18
|
+
* plugin for good - the previous entry then disposed its own mount, releasing
|
|
19
|
+
* the name with nobody left to take it, and the family row stayed listed as
|
|
20
|
+
* active while its host routes 404ed (the task-board panel showed
|
|
21
|
+
* "board.hostError.notMounted", no degraded record appeared, and only a Host
|
|
22
|
+
* restart recovered it).
|
|
23
|
+
*
|
|
24
|
+
* The refused mount is therefore QUEUED, not dropped, and replayed the moment
|
|
25
|
+
* the holder releases the name - if the waiting fiber is still alive then. The
|
|
26
|
+
* single-instance guarantee is unchanged: exactly one mount is live per
|
|
27
|
+
* package name, and the replay re-enters the guard so a later mount still
|
|
28
|
+
* dedupes against it.
|
|
29
|
+
*
|
|
30
|
+
* The registry rides a global symbol so two module instances of the same
|
|
31
|
+
* package (npm copy vs repository link) still share one verdict. That symbol
|
|
32
|
+
* is a CROSS-REPOSITORY contract, not this file's private state: the four
|
|
33
|
+
* satellite packages (dsh-skins / dsh-pet / dsh-presets /
|
|
34
|
+
* dsh-community-plugins) are separate repositories carrying their own copy of
|
|
35
|
+
* this guard, rebuilt on their own schedule, so the value under `MOUNTED`
|
|
36
|
+
* must keep the shape every published copy reads (a `Set` of package names
|
|
37
|
+
* with `has`/`add`/`delete`). The wait queues this guard added therefore live
|
|
38
|
+
* under their own additive key, and the registry reads back a `Set` even when
|
|
39
|
+
* some other build left a different value there. Changing `MOUNTED`'s shape
|
|
40
|
+
* in place broke that contract once: a satellite's legacy copy created a
|
|
41
|
+
* `Set`, the family's new copy read it as a `Map`, and every family row
|
|
42
|
+
* mounted after it failed with "claims.get is not a function".
|
|
43
|
+
*
|
|
44
|
+
* cordis `ctx.effect` runs its callback immediately and treats the callback's
|
|
45
|
+
* return value as the fiber disposer, so the unmarker is returned, not run.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/** Published cross-repository contract: package names currently mounted. */
|
|
49
|
+
const MOUNTED = Symbol.for('dsh-web.mounted-plugins')
|
|
50
|
+
|
|
51
|
+
/** Additive key this guard owns: refused mounts waiting for the name. */
|
|
52
|
+
const WAITERS = Symbol.for('dsh-web.mounted-plugins.waiters')
|
|
53
|
+
|
|
54
|
+
/** The slice of a cordis context this guard touches. */
|
|
55
|
+
interface MountContext {
|
|
56
|
+
effect?: (effect: () => unknown) => unknown
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One mount that was refused while another fiber owned the package name. */
|
|
60
|
+
interface PendingMount {
|
|
61
|
+
/** Replay the refused mount; a no-op once its own fiber has been disposed. */
|
|
62
|
+
run(): void
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface MountRegistry {
|
|
66
|
+
[MOUNTED]?: unknown
|
|
67
|
+
[WAITERS]?: Map<string, PendingMount[]>
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The shared name registry, always a `Set` whatever another build stored here:
|
|
72
|
+
* a foreign value (an interim shape, a hand-written global) must not take every
|
|
73
|
+
* family plugin down with it.
|
|
74
|
+
* @returns the process-wide set of mounted package names.
|
|
75
|
+
*/
|
|
76
|
+
function mountedSet(): Set<string> {
|
|
77
|
+
const registry = globalThis as MountRegistry
|
|
78
|
+
const existing = registry[MOUNTED]
|
|
79
|
+
if (existing instanceof Set) return existing as Set<string>
|
|
80
|
+
const created = new Set<string>()
|
|
81
|
+
registry[MOUNTED] = created
|
|
82
|
+
return created
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Queue per package name for mounts refused while a holder was alive. */
|
|
86
|
+
function mountWaiters(): Map<string, PendingMount[]> {
|
|
87
|
+
const registry = globalThis as MountRegistry
|
|
88
|
+
return (registry[WAITERS] ??= new Map())
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Wrap a cordis plugin apply so the package runs at most once per process.
|
|
93
|
+
* The first mount registers normally and releases the name when its fiber
|
|
94
|
+
* disposes; a mount refused while that name is held waits for the release and
|
|
95
|
+
* then runs, unless its own fiber disposes first.
|
|
96
|
+
* @param packageName - npm package identity shared by every install source.
|
|
97
|
+
* @param fn - the original plugin apply.
|
|
98
|
+
* @returns an apply of the same shape.
|
|
99
|
+
*/
|
|
100
|
+
export function mountOnce<T extends (...args: any[]) => unknown>(packageName: string, fn: T): T {
|
|
101
|
+
const mount = (...args: unknown[]): unknown => {
|
|
102
|
+
const mounted = mountedSet()
|
|
103
|
+
const ctx = args[0] as MountContext | undefined
|
|
104
|
+
if (mounted.has(packageName)) {
|
|
105
|
+
// Another fiber (the aggregate row, a standalone install, or a copy built
|
|
106
|
+
// from an older revision of this guard) owns the name. Queue instead of
|
|
107
|
+
// dropping: dropping lost the plugin for the rest of the process when the
|
|
108
|
+
// holder was disposed by a reload.
|
|
109
|
+
const waiters = mountWaiters()
|
|
110
|
+
const queue = waiters.get(packageName) ?? []
|
|
111
|
+
let alive = true
|
|
112
|
+
const pending: PendingMount = {
|
|
113
|
+
run: () => {
|
|
114
|
+
if (alive) mount(...args)
|
|
115
|
+
},
|
|
116
|
+
}
|
|
117
|
+
// A fiber that dies while it waits must not be replayed afterwards: its
|
|
118
|
+
// context is disposed and registering effects on it would throw.
|
|
119
|
+
ctx?.effect?.(() => () => {
|
|
120
|
+
alive = false
|
|
121
|
+
const index = queue.indexOf(pending)
|
|
122
|
+
if (index >= 0) queue.splice(index, 1)
|
|
123
|
+
})
|
|
124
|
+
queue.push(pending)
|
|
125
|
+
waiters.set(packageName, queue)
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
mounted.add(packageName)
|
|
129
|
+
ctx?.effect?.(() => () => {
|
|
130
|
+
mounted.delete(packageName)
|
|
131
|
+
const waiters = mountWaiters()
|
|
132
|
+
const queue = waiters.get(packageName)
|
|
133
|
+
if (queue === undefined) return
|
|
134
|
+
waiters.delete(packageName)
|
|
135
|
+
// Defer the handover one microtask so the disposing mount's own effects
|
|
136
|
+
// (routes, ledger, timers) are torn down before the next one registers.
|
|
137
|
+
for (const waiter of queue.splice(0)) queueMicrotask(() => { waiter.run() })
|
|
138
|
+
})
|
|
139
|
+
return fn(...args)
|
|
140
|
+
}
|
|
141
|
+
return mount as T
|
|
142
|
+
}
|