fix: guard bundled channel runtime against TDZ imports

This commit is contained in:
Peter Steinberger
2026-03-28 07:53:22 +00:00
parent 04a40b2613
commit be38986141
2 changed files with 32 additions and 3 deletions

View File

@@ -18,4 +18,24 @@ describe("bundled channel config runtime", () => {
expect(runtimeModule.getBundledChannelConfigSchemaMap().get("msteams")).toBeDefined();
expect(runtimeModule.getBundledChannelRuntimeMap().get("msteams")).toBeDefined();
});
it("falls back to static channel schemas when bundled plugin access hits a TDZ-style ReferenceError", async () => {
vi.resetModules();
vi.doMock("../channels/plugins/bundled.js", () => {
const mockModule = {} as { bundledChannelPlugins?: unknown };
Object.defineProperty(mockModule, "bundledChannelPlugins", {
enumerable: true,
get() {
throw new ReferenceError("Cannot access 'bundledChannelPlugins' before initialization.");
},
});
return mockModule;
});
const runtime = await import("./bundled-channel-config-runtime.js");
const configSchemaMap = runtime.getBundledChannelConfigSchemaMap();
expect(configSchemaMap.has("msteams")).toBe(true);
expect(configSchemaMap.has("whatsapp")).toBe(true);
});
});

View File

@@ -71,9 +71,18 @@ function buildBundledChannelMaps(
}
function readBundledChannelPlugins(): readonly BundledChannelPluginShape[] | undefined {
return Array.isArray(bundledChannelModule.bundledChannelPlugins)
? (bundledChannelModule.bundledChannelPlugins as readonly BundledChannelPluginShape[])
: undefined;
try {
return Array.isArray(bundledChannelModule.bundledChannelPlugins)
? (bundledChannelModule.bundledChannelPlugins as readonly BundledChannelPluginShape[])
: undefined;
} catch (error) {
// Circular bundled channel imports can transiently hit TDZ during test/bootstrap
// initialization. Fall back to metadata/static schemas until the registry is ready.
if (error instanceof ReferenceError) {
return undefined;
}
throw error;
}
}
function getBundledChannelMaps(): BundledChannelMaps {