Skip to content

Commit cca81a6

Browse files
committed
fix(hub): check lazy extraDevframes entries for the reserved id
extraDevframesOf() only read the id of plain entries, so a promise or a factory that resolved to a devframe with the id "pangular" reached initHub and, with duplicationStrategy "throw", failed the whole hub. Wrap each lazy entry so the reserved-id filter runs on what it resolves to; the hub awaits the entries in order, so the order stays as given.
1 parent 8a34baf commit cca81a6

2 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎packages/devtools/src/__tests__/hub.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,51 @@ describe('Pangular Inspector hub extraDevframes', () => {
313313
expect(extraDevframesOf(undefined, warn)).toEqual([]);
314314
expect(warn).toHaveBeenCalledTimes(3);
315315
});
316+
317+
it('drops a pangular devframe that a lazy entry resolves to, keeping the order', async () => {
318+
const warn = vi.fn();
319+
const entries = extraDevframesOf(
320+
[
321+
extra('first'),
322+
Promise.resolve(extra('pangular')),
323+
() => Promise.resolve([extra('acme'), { devframe: extra('pangular') }, null]),
324+
extra('last'),
325+
],
326+
warn,
327+
);
328+
expect(entries).toHaveLength(4);
329+
expect(warn).not.toHaveBeenCalled();
330+
const resolved = await Promise.all(
331+
entries.map((entry) => (typeof entry === 'function' ? entry() : entry)),
332+
);
333+
const ids = resolved
334+
.flat()
335+
.map((entry) => (entry && 'devframe' in entry ? entry.devframe.id : entry?.id));
336+
expect(ids).toEqual(['first', 'acme', undefined, 'last']);
337+
expect(warn).toHaveBeenCalledTimes(2);
338+
});
339+
340+
it('starts the hub when a lazy extra devframe has the pangular id and throws on duplicates', async () => {
341+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
342+
const hub = initPangularHub({
343+
cwd: makeProject({ 'package.json': '{}' }),
344+
ws: false,
345+
auth: false,
346+
allowedOrigins: false,
347+
extraDevframes: [
348+
// With 'throw', a second `pangular` devframe would fail the whole hub.
349+
async () => ({ ...extra('pangular'), duplicationStrategy: 'throw' as const }),
350+
Promise.resolve(extra('acme')),
351+
],
352+
});
353+
hubs.push(hub);
354+
await hub.ready;
355+
const ctx = await hub.context;
356+
expect(ctx.rpc.has('acme:ping')).toBe(true);
357+
expect(ctx.rpc.has('pangular:ping')).toBe(false);
358+
expect((await ctx.agent.invoke('pangular:inspect-forms', {})) as string).toContain('No forms');
359+
warn.mockRestore();
360+
});
316361
});
317362

318363
describe('Pangular Inspector hub behind a web router', () => {

‎packages/devtools/src/hub.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ export function startPangularHub(
252252

253253
/**
254254
* The `extraDevframes` entries to mount. One with Pangular Inspector's own id
255-
* would replace it on the hub, so it is dropped with a warning.
255+
* would replace it on the hub, so it is dropped with a warning. A lazy entry
256+
* (a promise, or a function that returns one) is wrapped so the same check
257+
* runs on what it resolves to; the hub awaits the entries in order, so the
258+
* order stays as given.
256259
*/
257260
export function extraDevframesOf(
258261
input: DevframesInput | undefined,
@@ -263,16 +266,38 @@ export function extraDevframesOf(
263266
warn('[pangular] Ignoring extraDevframes: pass an array, as initHub({ devframes }) takes.');
264267
return [];
265268
}
266-
return input.filter((entry) => {
267-
const id = devframeIdOf(entry);
268-
if (id !== 'pangular') return true;
269+
const keep = (entry: unknown) => {
270+
if (devframeIdOf(entry) !== 'pangular') return true;
269271
warn(
270272
'[pangular] Ignoring an extraDevframes entry with the id "pangular": the hub mounts Pangular Inspector itself.',
271273
);
272274
return false;
275+
};
276+
type Entry = DevframesInput[number];
277+
type Resolved = Awaited<ReturnType<Extract<Entry, () => unknown>>>;
278+
return input.flatMap((entry): Entry[] => {
279+
if (isThenable(entry) || typeof entry === 'function') {
280+
const lazy = entry as PromiseLike<Resolved> | (() => PromiseLike<Resolved>);
281+
return [
282+
async () => {
283+
const resolved = await (typeof lazy === 'function' ? lazy() : lazy);
284+
const list = Array.isArray(resolved) ? resolved : [resolved];
285+
return list.filter((item) => item == null || keep(item));
286+
},
287+
];
288+
}
289+
return keep(entry) ? [entry] : [];
273290
});
274291
}
275292

293+
function isThenable(value: unknown): value is PromiseLike<unknown> {
294+
return (
295+
!!value &&
296+
(typeof value === 'object' || typeof value === 'function') &&
297+
typeof (value as { then?: unknown }).then === 'function'
298+
);
299+
}
300+
276301
/** The id of a devframe or a `{ devframe }` entry; `undefined` for lazy entries. */
277302
function devframeIdOf(entry: unknown): string | undefined {
278303
if (!entry || typeof entry !== 'object') return undefined;

0 commit comments

Comments
 (0)