Download src/extensions/canvas-extension-module-loader.ts from SaylorTwift/openhands: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/SaylorTwift/openhands/resolve/main/src/extensions/canvas-extension-module-loader.ts
- Command line
-
hf download hf://SaylorTwift/openhands/src/extensions/canvas-extension-module-loader.ts
-
curl -L -o canvas-extension-module-loader.ts https://huggingface.co/SaylorTwift/openhands/resolve/main/src/extensions/canvas-extension-module-loader.ts
1.25 kB
| import type { CanvasExtensionModule } from "#/types/canvas-extension"; | |
| export class InvalidCanvasExtensionModuleError extends Error { | |
| constructor(message: string) { | |
| super(message); | |
| this.name = "InvalidCanvasExtensionModuleError"; | |
| } | |
| } | |
| export function assertCanvasExtensionModule( | |
| value: unknown, | |
| ): asserts value is CanvasExtensionModule { | |
| if ( | |
| typeof value !== "object" || | |
| value === null || | |
| !("activate" in value) || | |
| typeof value.activate !== "function" | |
| ) { | |
| throw new InvalidCanvasExtensionModuleError( | |
| 'Canvas Extension entrypoint must export an "activate" function.', | |
| ); | |
| } | |
| } | |
| /** | |
| * Import an authenticated, self-contained ESM bundle without putting the | |
| * Agent Server session key in a URL. The source has already been fetched by | |
| * Canvas through its authenticated HTTP client. | |
| */ | |
| export async function loadCanvasExtensionModule( | |
| source: string, | |
| ): Promise<CanvasExtensionModule> { | |
| const blob = new Blob([source], { type: "text/javascript" }); | |
| const moduleUrl = URL.createObjectURL(blob); | |
| try { | |
| const imported: unknown = await import(/* @vite-ignore */ moduleUrl); | |
| assertCanvasExtensionModule(imported); | |
| return imported; | |
| } finally { | |
| URL.revokeObjectURL(moduleUrl); | |
| } | |
| } | |