feat(graph)!: fail boot when a token has a second producer (CARD-18, GitHub #35)
topoSort, topoLevels and subgraphFor built their token -> node map with
producedBy.set(key, node), so the last node registered for a token won.
Plugin nodes are registered after module nodes, which let a plugin's
scope.add({ name: 'shadow', provides: ['user'] }) silently replace the
app's own @Provider({ provides: 'user' }) for every route that needed
it. setRunner only rejects duplicate names, and scope.add is the one
path where a name and its tokens differ.
D9 settles it graph-wide: topoSort now throws when a token already has
a producer, naming both nodes and their origins (module:..., plugin,
mesh:<url>, builtin). One check on the whole node set covers every
current and future registration path. It also protects the built-ins
(logger, rooms, events) by rule rather than by registration order.
topoSort runs at finalize, after the mesh splice, so remote steps are
checked too. topoLevels and subgraphFor only ever see nodes topoSort
has already validated, so they keep their plain maps.
overrides replace the runner of an existing token and add no node, so
they are unaffected. Steps that provide nothing (plugin observers) are
not producers. The official plugins (jwt, metrics, rate-limit) pass
their suites against this build.
BREAKING CHANGE: an app whose graph has two nodes providing one token,
e.g. a plugin scope node providing a token a module or built-in
already provides, now fails createApp instead of letting the later
node win.
Refs: GitHub #35
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+274
-3
No files matched your search
+36
-3
@@ -42,10 +42,43 @@ export interface GraphNode {
|
||||
origin: string;
|
||||
}
|
||||
|
||||
/** Orders nodes so every dependency precedes its dependents; throws on missing keys or cycles. */
|
||||
export function topoSort(nodes: GraphNode[], seedKeys: string[], note = ''): GraphNode[] {
|
||||
/**
|
||||
* Maps every token to the one node that produces it; throws when a second node provides a token.
|
||||
*
|
||||
* One producer per token, across modules, plugins, mesh steps and built-ins alike (GitHub #35, D9).
|
||||
* This used to be `producedBy.set(key, node)`, last one wins: plugin nodes are registered after
|
||||
* module nodes, so a plugin's `scope.add` that provided `user` silently replaced the app's own `user`
|
||||
* for every route that needed it. Checking here, on the whole graph, rather than at each place a node
|
||||
* is registered, is what covers every path — current and future — and makes the built-in tokens
|
||||
* (`logger`, `rooms`, `events`) safe by rule instead of by registration order.
|
||||
*/
|
||||
function producers(nodes: GraphNode[]): Map<string, GraphNode> {
|
||||
const producedBy = new Map<string, GraphNode>();
|
||||
for (const node of nodes) for (const key of node.provides) producedBy.set(key, node);
|
||||
|
||||
for (const node of nodes) {
|
||||
for (const key of node.provides) {
|
||||
const prior = producedBy.get(key);
|
||||
|
||||
if (prior && prior !== node) {
|
||||
throw new Error(
|
||||
`token '${key}' is provided twice: by '${prior.name}' (${prior.origin}) and by '${node.name}' (${node.origin}) — ` +
|
||||
'a token has exactly one producer; rename one of them, or use `overrides` to replace a runner',
|
||||
);
|
||||
}
|
||||
|
||||
producedBy.set(key, node);
|
||||
}
|
||||
}
|
||||
|
||||
return producedBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Orders nodes so every dependency precedes its dependents; throws on a token with two producers,
|
||||
* missing keys or cycles.
|
||||
*/
|
||||
export function topoSort(nodes: GraphNode[], seedKeys: string[], note = ''): GraphNode[] {
|
||||
const producedBy = producers(nodes);
|
||||
|
||||
// validate dependencies exist
|
||||
for (const node of nodes) {
|
||||
|
||||
Reference in new issue
Block a user