()
| 36 | const response = await fetch(REGISTRY_URL, { cache: 'no-cache', signal }) |
| 37 | ensureSuccessfulResponse(response) |
| 38 | const text = await readBoundedText(response, MAX_PLUGIN_REGISTRY_BYTES) |
| 39 | return validateRegistry(JSON.parse(text)) |
| 40 | } |
| 41 | |
| 42 | function validateRegistry(value: unknown): RegistryEntry[] { |
| 43 | if (!Array.isArray(value)) throw new Error('Plugin registry is invalid.') |
| 44 | |
| 45 | return value.map((entry) => { |
| 46 | if ( |
| 47 | !isRecord(entry) || |
| 48 | typeof entry.name !== 'string' || |
| 49 | !REGISTERED_PLUGIN_SOURCE_RE.test(`@${entry.name}`) || |
| 50 | typeof entry.url !== 'string' |
| 51 | ) { |
| 52 | throw new Error('Plugin registry is invalid.') |
| 53 | } |
| 54 | validatePluginUrl(entry.url, false) |
| 55 | return { name: entry.name, url: entry.url } |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | export type PluginData = { |
| 60 | code: string |
| 61 | integrity: string |
| 62 | pluginName: string |
| 63 | resolvedUrl: string |
| 64 | source: string // can be a URL or a registered plugin name like `@{plugin-name}` |
| 65 | } |
| 66 | |
| 67 | export function usePluginInstall() { |
| 68 | const { show } = useToast() |
| 69 | |
| 70 | const validity = shallowRef('') |
| 71 | const installing = shallowRef(false) |
| 72 | let controller: AbortController | null = null |
| 73 | |
| 74 | function cancel() { |
| 75 | controller?.abort() |
| 76 | controller = null |
| 77 | installing.value = false |
| 78 | } |
| 79 | |
| 80 | async function install(src: string, isUpdate = false) { |
| 81 | if (installing.value) { |
| 82 | return null |
| 83 | } |
| 84 | |
| 85 | const currentController = new AbortController() |
| 86 | controller = currentController |
| 87 | const { signal } = currentController |
| 88 | const isCurrent = () => controller === currentController && !signal.aborted |
| 89 | |
| 90 | installing.value = true |
| 91 | |
| 92 | try { |
| 93 | const url = REGISTERED_PLUGIN_SOURCE_RE.test(src) |
| 94 | ? await getRegisteredPluginSource(src, signal) |
| 95 | : src |
no test coverage detected