File size: 1,254 Bytes
a070805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);
  }
}