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
2 changes: 1 addition & 1 deletion packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"keywords": [],
"author": "Ink & Switch",
"dependencies": {
"@inkandswitch/patchwork-filesystem": "^0.1.3",
"@inkandswitch/patchwork-filesystem": "^0.2.7",
"@inkandswitch/patchwork-plugins": "^0.0.5",
"solid-js": "^1.9.3"
},
Expand Down
12 changes: 5 additions & 7 deletions packages/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions packages/src/discover-plugins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const { importPackageFromHttpUrl } = vi.hoisted(() => ({
importPackageFromHttpUrl: vi.fn(),
}));

vi.mock("@inkandswitch/patchwork-filesystem", () => ({
importPackageFromHttpUrl,
}));

import { discoverHttpPlugins } from "./discover-plugins.ts";

describe("discoverHttpPlugins", () => {
beforeEach(() => {
importPackageFromHttpUrl.mockReset();
});

it("imports HTTP packages through the host package importer", async () => {
importPackageFromHttpUrl.mockResolvedValue({
plugins: [{ id: "example", load: () => {} }],
});

await expect(
discoverHttpPlugins("https://example.com/package/")
).resolves.toEqual([{ id: "example" }]);
expect(importPackageFromHttpUrl).toHaveBeenCalledWith(
"https://example.com/package/"
);
});
});
16 changes: 6 additions & 10 deletions packages/src/discover-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function withTimeout<T>(p: Promise<T>, ms: number, what: string): Promise<T> {
* Discover the plugins an http(s) package exports by importing its entry module
* and reading its `plugins` export — the http counterpart to the worker-based
* automerge discovery, and the same thing the host does for suggested/http
* modules (`importModuleFromHttpUrl` → `mod.plugins`).
* modules (`importPackageFromHttpUrl` → `mod.plugins`).
*
* It runs on the MAIN thread deliberately: there is no host worker that imports
* http URLs, and a plain Worker has no importmap to resolve the module's bare
Expand All @@ -138,15 +138,11 @@ export async function discoverHttpPlugins(
url: string,
timeoutMs = 15000
): Promise<PluginDescriptor[]> {
// Reached via the host importmap at runtime; guard so an older filesystem
// bundle degrades to "couldn't preview" instead of a hard module-load error.
const importHttp = filesystem.importModuleFromHttpUrl;
if (typeof importHttp !== "function") {
throw new Error(
"This host build can't import http(s) packages — update @inkandswitch/patchwork-filesystem to 0.1.3+."
);
}
const mod = await withTimeout(importHttp(url), timeoutMs, "importing the package");
const mod = await withTimeout(
filesystem.importPackageFromHttpUrl(url),
timeoutMs,
"importing the package"
);
const plugins: any[] = Array.isArray((mod as any)?.plugins)
? (mod as any).plugins
: [];
Expand Down
Loading