Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/komga-unset-site.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lnreader/plugin-runtime': patch
---

Accept self-hosted plugins (such as komga) whose `site` is undefined until their URL setting is configured.
11 changes: 7 additions & 4 deletions packages/plugin-runtime/src/plugins/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ export function assertPluginShape(
throw new InvalidPluginError(id, 'module did not export a plugin instance');
}
const p = value as Record<string, unknown>;
// Self-hosted sources (e.g. komga) read `site` from a user setting, so it
// is empty or undefined until configured. Normalize it to '' so callers
// can treat it as a string; the runner reports NEEDS_CONFIG on use.
if (p.pluginSettings && (p.site === undefined || p.site === null)) {
p.site = '';
}
for (const field of ['id', 'name', 'site', 'version'] as const) {
// Self-hosted sources (e.g. komga) read `site` from a user setting, so it
// may be empty until configured.
const optional =
field === 'site' && typeof p.site === 'string' && !!p.pluginSettings;
const optional = field === 'site' && !!p.pluginSettings;
if (typeof p[field] !== 'string' || (!p[field] && !optional)) {
throw new InvalidPluginError(id, `missing string field "${field}"`);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/plugin-runtime/test/sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ describe('sandbox', () => {
);
});

it('accepts an unset site on plugins with settings', async () => {
const d = await deps();
const methods = [
'popularNovels',
'parseNovel',
'parseChapter',
'searchNovels',
]
.map(m => `${m}() {}`)
.join(', ');
const code = (site: string) =>
`exports.default = { id: 'k', name: 'K', version: '1', site: ${site}, ` +
`pluginSettings: { url: { value: '', label: 'URL' } }, ${methods} }`;
expect(loadPlugin(code('undefined'), 'k', d).site).toBe('');
expect(loadPlugin(code("''"), 'k', d).site).toBe('');
const noSettings = code('undefined').replace(
/pluginSettings: .*?\} \},/,
'',
);
expect(() => loadPlugin(noSettings, 'k', d)).toThrow(
'missing string field "site"',
);
});

it('times out runaway top-level code', async () => {
const d = { ...(await deps()), loadTimeoutMs: 50 };
expect(() => loadPlugin('while (true) {}', 'spin', d)).toThrow(/timed out/);
Expand Down
Loading