MCPcopy Create free account
hub / github.com/Sandpack/nodebox-runtime / Nodebox

Class Nodebox

packages/nodebox/src/Nodebox.ts:34–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32 * Create a new Node emulator instance
33 */
34export class Nodebox {
35 private channel: MessageSender = null as any;
36 private isConnected: boolean;
37 private url: string;
38
39 /* API */
40 private fileSystemApi: FileSystemApi = null as any;
41 private shellApi: ShellApi = null as any;
42 private previewApi: PreviewApi = null as any;
43
44 constructor(private readonly options: ChannelOptions) {
45 invariant(
46 this.options.iframe,
47 'Failed to create a Nodebox: expected "iframe" argument to be a reference to an <iframe> element but got %j',
48 this.options.iframe
49 );
50
51 this.url = this.options.runtimeUrl || DEFAULT_RUNTIME_URL;
52
53 this.isConnected = false;
54 }
55
56 /**
57 * Connect to the deployed Node Emulator instance.
58 */
59 public async connect(): Promise<void> {
60 const { iframe, cdnUrl } = this.options;
61
62 debug('[message-sender]: Connecting to node emulator...');
63
64 const connectionPromise = new DeferredPromise<void>();
65
66 if (!this.url) {
67 connectionPromise.reject(
68 new Error('Nodebox URL is missing. Did you forget to provide it when creating this Nodebox instance?')
69 );
70 }
71
72 invariant(
73 iframe.contentWindow,
74 'Failed to create a MessageChannel with the Nodebox iframe: no content window found'
75 );
76
77 // Establish a message channel with the worker frame
78 // to communicate with the worker instance.
79 this.channel = new MessageSender(iframe.contentWindow);
80
81 // Connect to the emulator.
82 const frameLoadPromise = new DeferredPromise<void>();
83 iframe.setAttribute('src', this.url);
84 iframe.addEventListener(
85 'load',
86 () => {
87 frameLoadPromise.resolve();
88 },
89 { once: true }
90 );
91 iframe.addEventListener(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected