36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import {
|
|
createStorageUriResolver,
|
|
type StorageUriResolver,
|
|
type UriPlugin,
|
|
} from "@suaveplan/storage-uri";
|
|
import { fallbackUriPlugin } from "../../src/plugin/plugin.js";
|
|
import type { FallbackPluginOptions } from "../../src/types/policy.types.js";
|
|
|
|
/**
|
|
* Build a resolver carrying the fallback plugin wired back to itself plus
|
|
* the supplied scheme plugins. Uses a late-bound proxy so the plugin can
|
|
* recurse through the same resolver instance.
|
|
*/
|
|
export function buildResolverWithFallback(
|
|
extraPlugins: readonly UriPlugin[],
|
|
options?: Omit<FallbackPluginOptions, "resolver">
|
|
): StorageUriResolver {
|
|
let captured: StorageUriResolver | undefined;
|
|
const lazy = new Proxy(
|
|
{},
|
|
{
|
|
get(_t, prop) {
|
|
if (captured === undefined)
|
|
throw new Error("resolver not initialised");
|
|
const v = (captured as unknown as Record<string, unknown>)[
|
|
prop as string
|
|
];
|
|
return typeof v === "function" ? v.bind(captured) : v;
|
|
},
|
|
}
|
|
) as StorageUriResolver;
|
|
const plugin = fallbackUriPlugin({ ...options, resolver: lazy });
|
|
captured = createStorageUriResolver([...extraPlugins, plugin]);
|
|
return captured;
|
|
}
|