@immediately-run/sandpack-client 2.19.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/.ir-build-stamp.json +6 -0
- package/dist/base-DBh7xJX9.mjs +48 -0
- package/dist/base-DelKLlDk.js +50 -0
- package/dist/clients/base.d.ts +34 -0
- package/dist/clients/event-emitter.d.ts +10 -0
- package/dist/clients/iframe-factory.d.ts +27 -0
- package/dist/clients/index.d.ts +4 -0
- package/dist/clients/node/client.utils.d.ts +7 -0
- package/dist/clients/node/iframe.utils.d.ts +3 -0
- package/dist/clients/node/index.d.ts +49 -0
- package/dist/clients/node/index.js +631 -0
- package/dist/clients/node/index.mjs +629 -0
- package/dist/clients/node/inject-scripts/historyListener.d.ts +5 -0
- package/dist/clients/node/inject-scripts/index.d.ts +1 -0
- package/dist/clients/node/inject-scripts/resize.d.ts +5 -0
- package/dist/clients/node/taskManager.d.ts +20 -0
- package/dist/clients/node/types.d.ts +63 -0
- package/dist/clients/runtime/file-resolver-protocol.d.ts +17 -0
- package/dist/clients/runtime/iframe-protocol.d.ts +17 -0
- package/dist/clients/runtime/immutable-fetch-protocol.d.ts +39 -0
- package/dist/clients/runtime/index.d.ts +76 -0
- package/dist/clients/runtime/index.js +1046 -0
- package/dist/clients/runtime/index.mjs +1044 -0
- package/dist/clients/runtime/mime.d.ts +1 -0
- package/dist/clients/runtime/types.d.ts +129 -0
- package/dist/clients/runtime/utils.d.ts +8 -0
- package/dist/clients/static/index.d.ts +25 -0
- package/dist/clients/static/utils.d.ts +5 -0
- package/dist/consoleHook-DQVWjDRE.mjs +230 -0
- package/dist/consoleHook-znXctRzh.js +236 -0
- package/dist/fs/SandpackFS.d.ts +116 -0
- package/dist/iframe-factory-BcC-S_XQ.js +54 -0
- package/dist/iframe-factory-DybmkzJZ.mjs +51 -0
- package/dist/index--fILWAw8.js +210 -0
- package/dist/index-rbhm_KmF.mjs +208 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +53 -0
- package/dist/index.mjs +40 -0
- package/dist/inject-scripts/consoleHook.d.ts +6 -0
- package/dist/types-BFONOA2L.mjs +531 -0
- package/dist/types-BIIEoWr6.js +534 -0
- package/dist/types.d.ts +348 -0
- package/dist/utils-BiVyytui.js +262 -0
- package/dist/utils-DG1HA4RZ.mjs +250 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.js +13 -0
- package/dist/utils.mjs +2 -0
- package/package.json +82 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../../utils-BiVyytui.js');
|
|
4
|
+
var nodebox = require('@codesandbox/nodebox');
|
|
5
|
+
require('../../types-BIIEoWr6.js');
|
|
6
|
+
var base = require('../../base-DelKLlDk.js');
|
|
7
|
+
var consoleHook = require('../../consoleHook-znXctRzh.js');
|
|
8
|
+
require('outvariant');
|
|
9
|
+
require('@zenfs/core');
|
|
10
|
+
require('dequal');
|
|
11
|
+
|
|
12
|
+
function loadPreviewIframe(iframe, url) {
|
|
13
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
14
|
+
var contentWindow, TIME_OUT, MAX_MANY_TIRES, tries, timeout;
|
|
15
|
+
return utils.__generator(this, function (_a) {
|
|
16
|
+
contentWindow = iframe.contentWindow;
|
|
17
|
+
utils.nullthrows(contentWindow, "Failed to await preview iframe: no content window found");
|
|
18
|
+
TIME_OUT = 90000;
|
|
19
|
+
MAX_MANY_TIRES = 20;
|
|
20
|
+
tries = 0;
|
|
21
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
22
|
+
var triesToSetUrl = function () {
|
|
23
|
+
var onLoadPage = function () {
|
|
24
|
+
clearTimeout(timeout);
|
|
25
|
+
tries = MAX_MANY_TIRES;
|
|
26
|
+
resolve();
|
|
27
|
+
iframe.removeEventListener("load", onLoadPage);
|
|
28
|
+
};
|
|
29
|
+
if (tries >= MAX_MANY_TIRES) {
|
|
30
|
+
reject(utils.createError("Could not able to connect to preview."));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
iframe.setAttribute("src", url);
|
|
34
|
+
timeout = setTimeout(function () {
|
|
35
|
+
triesToSetUrl();
|
|
36
|
+
iframe.removeEventListener("load", onLoadPage);
|
|
37
|
+
}, TIME_OUT);
|
|
38
|
+
tries = tries + 1;
|
|
39
|
+
iframe.addEventListener("load", onLoadPage);
|
|
40
|
+
};
|
|
41
|
+
iframe.addEventListener("error", function () { return reject(new Error("Iframe error")); });
|
|
42
|
+
iframe.addEventListener("abort", function () { return reject(new Error("Aborted")); });
|
|
43
|
+
triesToSetUrl();
|
|
44
|
+
})];
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
var setPreviewIframeProperties = function (iframe, options) {
|
|
49
|
+
iframe.style.border = "0";
|
|
50
|
+
iframe.style.width = options.width || "100%";
|
|
51
|
+
iframe.style.height = options.height || "100%";
|
|
52
|
+
iframe.style.overflow = "hidden";
|
|
53
|
+
iframe.allow = "cross-origin-isolated";
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
57
|
+
function setupHistoryListeners(_a) {
|
|
58
|
+
var scope = _a.scope;
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
var origHistoryProto = window.history.__proto__;
|
|
61
|
+
var historyList = [];
|
|
62
|
+
var historyPosition = 0;
|
|
63
|
+
var dispatchMessage = function (url) {
|
|
64
|
+
parent.postMessage({
|
|
65
|
+
type: "urlchange",
|
|
66
|
+
url: url,
|
|
67
|
+
back: historyPosition > 0,
|
|
68
|
+
forward: historyPosition < historyList.length - 1,
|
|
69
|
+
channelId: scope.channelId,
|
|
70
|
+
}, "*");
|
|
71
|
+
};
|
|
72
|
+
function pushHistory(url, state) {
|
|
73
|
+
// remove "future" locations
|
|
74
|
+
historyList.splice(historyPosition + 1);
|
|
75
|
+
historyList.push({ url: url, state: state });
|
|
76
|
+
historyPosition = historyList.length - 1;
|
|
77
|
+
}
|
|
78
|
+
Object.assign(window.history, {
|
|
79
|
+
go: function (delta) {
|
|
80
|
+
var newPos = historyPosition + delta;
|
|
81
|
+
if (newPos >= 0 && newPos <= historyList.length - 1) {
|
|
82
|
+
historyPosition = newPos;
|
|
83
|
+
var _a = historyList[historyPosition], url = _a.url, state = _a.state;
|
|
84
|
+
origHistoryProto.replaceState.call(window.history, state, "", url);
|
|
85
|
+
var newURL = document.location.href;
|
|
86
|
+
dispatchMessage(newURL);
|
|
87
|
+
window.dispatchEvent(new PopStateEvent("popstate", { state: state }));
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
back: function () {
|
|
91
|
+
window.history.go(-1);
|
|
92
|
+
},
|
|
93
|
+
forward: function () {
|
|
94
|
+
window.history.go(1);
|
|
95
|
+
},
|
|
96
|
+
pushState: function (state, title, url) {
|
|
97
|
+
origHistoryProto.replaceState.call(window.history, state, title, url);
|
|
98
|
+
pushHistory(url, state);
|
|
99
|
+
dispatchMessage(document.location.href);
|
|
100
|
+
},
|
|
101
|
+
replaceState: function (state, title, url) {
|
|
102
|
+
origHistoryProto.replaceState.call(window.history, state, title, url);
|
|
103
|
+
historyList[historyPosition] = { state: state, url: url };
|
|
104
|
+
dispatchMessage(document.location.href);
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
function handleMessage(_a) {
|
|
108
|
+
var data = _a.data;
|
|
109
|
+
if (data.type === "urlback") {
|
|
110
|
+
history.back();
|
|
111
|
+
}
|
|
112
|
+
else if (data.type === "urlforward") {
|
|
113
|
+
history.forward();
|
|
114
|
+
}
|
|
115
|
+
else if (data.type === "refresh") {
|
|
116
|
+
document.location.reload();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
window.addEventListener("message", handleMessage);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
123
|
+
function watchResize(_a) {
|
|
124
|
+
var scope = _a.scope;
|
|
125
|
+
var lastHeight = 0;
|
|
126
|
+
function getDocumentHeight() {
|
|
127
|
+
if (typeof window === "undefined")
|
|
128
|
+
return 0;
|
|
129
|
+
var body = document.body;
|
|
130
|
+
var html = document.documentElement;
|
|
131
|
+
return Math.max(body.scrollHeight, body.offsetHeight, html.offsetHeight);
|
|
132
|
+
}
|
|
133
|
+
function sendResizeEvent() {
|
|
134
|
+
var height = getDocumentHeight();
|
|
135
|
+
if (lastHeight !== height) {
|
|
136
|
+
window.parent.postMessage({
|
|
137
|
+
type: "resize",
|
|
138
|
+
height: height,
|
|
139
|
+
codesandbox: true,
|
|
140
|
+
channelId: scope.channelId,
|
|
141
|
+
}, "*");
|
|
142
|
+
}
|
|
143
|
+
lastHeight = height;
|
|
144
|
+
}
|
|
145
|
+
sendResizeEvent();
|
|
146
|
+
var throttle;
|
|
147
|
+
var observer = new MutationObserver(function () {
|
|
148
|
+
if (throttle === undefined) {
|
|
149
|
+
sendResizeEvent();
|
|
150
|
+
throttle = setTimeout(function () {
|
|
151
|
+
throttle = undefined;
|
|
152
|
+
}, 300);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
observer.observe(document, {
|
|
156
|
+
attributes: true,
|
|
157
|
+
childList: true,
|
|
158
|
+
subtree: true,
|
|
159
|
+
});
|
|
160
|
+
/**
|
|
161
|
+
* Ideally we should only use a `MutationObserver` to trigger a resize event,
|
|
162
|
+
* however, we noted that it's not 100% reliable, so we went for polling strategy as well
|
|
163
|
+
*/
|
|
164
|
+
setInterval(sendResizeEvent, 300);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
168
|
+
var scripts = [
|
|
169
|
+
{ code: setupHistoryListeners.toString(), id: "historyListener" },
|
|
170
|
+
{
|
|
171
|
+
code: "function consoleHook({ scope }) {" + consoleHook.consoleHook + "\n};",
|
|
172
|
+
id: "consoleHook",
|
|
173
|
+
},
|
|
174
|
+
{ code: watchResize.toString(), id: "watchResize" },
|
|
175
|
+
];
|
|
176
|
+
var injectScriptToIframe = function (iframe, channelId) {
|
|
177
|
+
scripts.forEach(function (_a) {
|
|
178
|
+
var _b;
|
|
179
|
+
var code = _a.code, id = _a.id;
|
|
180
|
+
var message = {
|
|
181
|
+
uid: id,
|
|
182
|
+
type: nodebox.INJECT_MESSAGE_TYPE,
|
|
183
|
+
code: "exports.activate = ".concat(code),
|
|
184
|
+
scope: { channelId: channelId },
|
|
185
|
+
};
|
|
186
|
+
(_b = iframe.contentWindow) === null || _b === void 0 ? void 0 : _b.postMessage(message, "*");
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
var SandpackNode = /** @class */ (function (_super) {
|
|
191
|
+
utils.__extends(SandpackNode, _super);
|
|
192
|
+
function SandpackNode(selector, sandboxInfo, options) {
|
|
193
|
+
if (options === void 0) { options = {}; }
|
|
194
|
+
var _this = _super.call(this, selector, sandboxInfo, utils.__assign(utils.__assign({}, options), { bundlerURL: options.bundlerURL })) || this;
|
|
195
|
+
_this.messageChannelId = consoleHook.generateRandomId();
|
|
196
|
+
_this._initPromise = null;
|
|
197
|
+
_this.emitter = new consoleHook.EventEmitter();
|
|
198
|
+
// Assign iframes
|
|
199
|
+
_this.manageIframes(selector);
|
|
200
|
+
// Init emulator
|
|
201
|
+
_this.emulator = new nodebox.Nodebox({
|
|
202
|
+
iframe: _this.emulatorIframe,
|
|
203
|
+
runtimeUrl: _this.options.bundlerURL,
|
|
204
|
+
});
|
|
205
|
+
// Trigger initial compile
|
|
206
|
+
_this.updateSandbox(sandboxInfo);
|
|
207
|
+
return _this;
|
|
208
|
+
}
|
|
209
|
+
// Initialize nodebox, should only ever be called once
|
|
210
|
+
SandpackNode.prototype._init = function () {
|
|
211
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
212
|
+
return utils.__generator(this, function (_a) {
|
|
213
|
+
switch (_a.label) {
|
|
214
|
+
case 0: return [4 /*yield*/, this.emulator.connect()];
|
|
215
|
+
case 1:
|
|
216
|
+
_a.sent();
|
|
217
|
+
return [4 /*yield*/, this.globalListeners()];
|
|
218
|
+
case 2:
|
|
219
|
+
_a.sent();
|
|
220
|
+
return [2 /*return*/];
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* It initializes the emulator and provide it with files, template and script to run
|
|
227
|
+
*/
|
|
228
|
+
SandpackNode.prototype.compile = function () {
|
|
229
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
230
|
+
var shellId, err_1;
|
|
231
|
+
return utils.__generator(this, function (_a) {
|
|
232
|
+
switch (_a.label) {
|
|
233
|
+
case 0:
|
|
234
|
+
_a.trys.push([0, 5, , 6]);
|
|
235
|
+
// 1. Init
|
|
236
|
+
this.status = "initializing";
|
|
237
|
+
this.dispatch({ type: "start", firstLoad: true });
|
|
238
|
+
if (!this._initPromise) {
|
|
239
|
+
this._initPromise = this._init();
|
|
240
|
+
}
|
|
241
|
+
return [4 /*yield*/, this._initPromise];
|
|
242
|
+
case 1:
|
|
243
|
+
_a.sent();
|
|
244
|
+
this.dispatch({ type: "connected" });
|
|
245
|
+
return [4 /*yield*/, this.createShellProcessFromTask()];
|
|
246
|
+
case 2:
|
|
247
|
+
shellId = (_a.sent()).id;
|
|
248
|
+
// 4. Launch Preview
|
|
249
|
+
return [4 /*yield*/, this.createPreviewURLFromId(shellId)];
|
|
250
|
+
case 3:
|
|
251
|
+
// 4. Launch Preview
|
|
252
|
+
_a.sent();
|
|
253
|
+
return [4 /*yield*/, this.setLocationURLIntoIFrame()];
|
|
254
|
+
case 4:
|
|
255
|
+
_a.sent();
|
|
256
|
+
// 5. Returns to consumer
|
|
257
|
+
this.dispatchDoneMessage();
|
|
258
|
+
return [3 /*break*/, 6];
|
|
259
|
+
case 5:
|
|
260
|
+
err_1 = _a.sent();
|
|
261
|
+
this.dispatch({
|
|
262
|
+
type: "action",
|
|
263
|
+
action: "notification",
|
|
264
|
+
notificationType: "error",
|
|
265
|
+
title: consoleHook.getMessageFromError(err_1),
|
|
266
|
+
});
|
|
267
|
+
this.dispatch({ type: "done", compilatonError: true });
|
|
268
|
+
return [3 /*break*/, 6];
|
|
269
|
+
case 6: return [2 /*return*/];
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* It creates a new shell and run the starting task
|
|
276
|
+
*/
|
|
277
|
+
SandpackNode.prototype.createShellProcessFromTask = function () {
|
|
278
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
279
|
+
var packageJsonContent;
|
|
280
|
+
var _a;
|
|
281
|
+
var _this = this;
|
|
282
|
+
return utils.__generator(this, function (_b) {
|
|
283
|
+
switch (_b.label) {
|
|
284
|
+
case 0: return [4 /*yield*/, this.sandboxSetup.fs.readFile("/package.json")];
|
|
285
|
+
case 1:
|
|
286
|
+
packageJsonContent = _b.sent();
|
|
287
|
+
this.emulatorCommand = consoleHook.findStartScriptPackageJson(packageJsonContent);
|
|
288
|
+
this.emulatorShellProcess = this.emulator.shell.create();
|
|
289
|
+
// Shell listeners
|
|
290
|
+
return [4 /*yield*/, this.emulatorShellProcess.on("exit", function (exitCode) {
|
|
291
|
+
_this.dispatch({
|
|
292
|
+
type: "action",
|
|
293
|
+
action: "notification",
|
|
294
|
+
notificationType: "error",
|
|
295
|
+
title: utils.createError("Error: process.exit(".concat(exitCode, ") called.")),
|
|
296
|
+
});
|
|
297
|
+
})];
|
|
298
|
+
case 2:
|
|
299
|
+
// Shell listeners
|
|
300
|
+
_b.sent();
|
|
301
|
+
return [4 /*yield*/, this.emulatorShellProcess.on("progress", function (data) {
|
|
302
|
+
var _a, _b;
|
|
303
|
+
if (data.state === "command_running" ||
|
|
304
|
+
data.state === "starting_command") {
|
|
305
|
+
_this.dispatch({
|
|
306
|
+
type: "shell/progress",
|
|
307
|
+
data: utils.__assign(utils.__assign({}, data), { command: [
|
|
308
|
+
(_a = _this.emulatorCommand) === null || _a === void 0 ? void 0 : _a[0],
|
|
309
|
+
(_b = _this.emulatorCommand) === null || _b === void 0 ? void 0 : _b[1].join(" "),
|
|
310
|
+
].join(" ") }),
|
|
311
|
+
});
|
|
312
|
+
_this.status = "installing-dependencies";
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
_this.dispatch({ type: "shell/progress", data: data });
|
|
316
|
+
})];
|
|
317
|
+
case 3:
|
|
318
|
+
_b.sent();
|
|
319
|
+
this.emulatorShellProcess.stdout.on("data", function (data) {
|
|
320
|
+
_this.dispatch({ type: "stdout", payload: { data: data, type: "out" } });
|
|
321
|
+
});
|
|
322
|
+
this.emulatorShellProcess.stderr.on("data", function (data) {
|
|
323
|
+
_this.dispatch({ type: "stdout", payload: { data: data, type: "err" } });
|
|
324
|
+
});
|
|
325
|
+
return [4 /*yield*/, (_a = this.emulatorShellProcess).runCommand.apply(_a, this.emulatorCommand)];
|
|
326
|
+
case 4: return [2 /*return*/, _b.sent()];
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
};
|
|
331
|
+
SandpackNode.prototype.createPreviewURLFromId = function (id) {
|
|
332
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
333
|
+
var url;
|
|
334
|
+
var _a;
|
|
335
|
+
return utils.__generator(this, function (_b) {
|
|
336
|
+
switch (_b.label) {
|
|
337
|
+
case 0:
|
|
338
|
+
this.iframePreviewUrl = undefined;
|
|
339
|
+
return [4 /*yield*/, this.emulator.preview.getByShellId(id)];
|
|
340
|
+
case 1:
|
|
341
|
+
url = (_b.sent()).url;
|
|
342
|
+
this.iframePreviewUrl = url + ((_a = this.options.startRoute) !== null && _a !== void 0 ? _a : "");
|
|
343
|
+
return [2 /*return*/];
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Nodebox needs to handle two types of iframes at the same time:
|
|
350
|
+
*
|
|
351
|
+
* 1. Runtime iframe: where the emulator process runs, which is responsible
|
|
352
|
+
* for creating the other iframes (hidden);
|
|
353
|
+
* 2. Preview iframes: any other node process that contains a PORT (public);
|
|
354
|
+
*/
|
|
355
|
+
SandpackNode.prototype.manageIframes = function (selector) {
|
|
356
|
+
var _a;
|
|
357
|
+
/**
|
|
358
|
+
* Pick the preview iframe
|
|
359
|
+
*/
|
|
360
|
+
if (typeof selector === "string") {
|
|
361
|
+
var element = document.querySelector(selector);
|
|
362
|
+
utils.nullthrows(element, "The element '".concat(selector, "' was not found"));
|
|
363
|
+
this.iframe = document.createElement("iframe");
|
|
364
|
+
element === null || element === void 0 ? void 0 : element.appendChild(this.iframe);
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
this.iframe = selector;
|
|
368
|
+
}
|
|
369
|
+
// Set preview iframe styles
|
|
370
|
+
setPreviewIframeProperties(this.iframe, this.options);
|
|
371
|
+
utils.nullthrows(this.iframe.parentNode, "The given iframe does not have a parent.");
|
|
372
|
+
/**
|
|
373
|
+
* Create the runtime iframe, which is hidden sibling
|
|
374
|
+
* from the preview one
|
|
375
|
+
*/
|
|
376
|
+
this.emulatorIframe = document.createElement("iframe");
|
|
377
|
+
this.emulatorIframe.classList.add("sp-bridge-frame");
|
|
378
|
+
(_a = this.iframe.parentNode) === null || _a === void 0 ? void 0 : _a.appendChild(this.emulatorIframe);
|
|
379
|
+
};
|
|
380
|
+
SandpackNode.prototype.setLocationURLIntoIFrame = function () {
|
|
381
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
382
|
+
return utils.__generator(this, function (_a) {
|
|
383
|
+
switch (_a.label) {
|
|
384
|
+
case 0:
|
|
385
|
+
if (!this.iframePreviewUrl) return [3 /*break*/, 2];
|
|
386
|
+
return [4 /*yield*/, loadPreviewIframe(this.iframe, this.iframePreviewUrl)];
|
|
387
|
+
case 1:
|
|
388
|
+
_a.sent();
|
|
389
|
+
_a.label = 2;
|
|
390
|
+
case 2: return [2 /*return*/];
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* Send all messages and events to tell to the
|
|
397
|
+
* consumer that the bundler is ready without any error
|
|
398
|
+
*/
|
|
399
|
+
SandpackNode.prototype.dispatchDoneMessage = function () {
|
|
400
|
+
this.status = "done";
|
|
401
|
+
this.dispatch({ type: "done", compilatonError: false });
|
|
402
|
+
if (this.iframePreviewUrl) {
|
|
403
|
+
this.dispatch({
|
|
404
|
+
type: "urlchange",
|
|
405
|
+
url: this.iframePreviewUrl,
|
|
406
|
+
back: false,
|
|
407
|
+
forward: false,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
SandpackNode.prototype.globalListeners = function () {
|
|
412
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
413
|
+
var _this = this;
|
|
414
|
+
return utils.__generator(this, function (_a) {
|
|
415
|
+
switch (_a.label) {
|
|
416
|
+
case 0:
|
|
417
|
+
window.addEventListener("message", function (event) {
|
|
418
|
+
if (event.data.type === nodebox.PREVIEW_LOADED_MESSAGE_TYPE) {
|
|
419
|
+
injectScriptToIframe(_this.iframe, _this.messageChannelId);
|
|
420
|
+
}
|
|
421
|
+
if (event.data.type === "urlchange" &&
|
|
422
|
+
event.data.channelId === _this.messageChannelId) {
|
|
423
|
+
_this.dispatch({
|
|
424
|
+
type: "urlchange",
|
|
425
|
+
url: event.data.url,
|
|
426
|
+
back: event.data.back,
|
|
427
|
+
forward: event.data.forward,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
else if (event.data.channelId === _this.messageChannelId) {
|
|
431
|
+
_this.dispatch(event.data);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
return [4 /*yield*/, this.emulator.fs.watch(["*"], [
|
|
435
|
+
".next",
|
|
436
|
+
"node_modules",
|
|
437
|
+
"build",
|
|
438
|
+
"dist",
|
|
439
|
+
"vendor",
|
|
440
|
+
".config",
|
|
441
|
+
".vuepress",
|
|
442
|
+
], function (message) { return utils.__awaiter(_this, void 0, void 0, function () {
|
|
443
|
+
var event, path, type, _a, content, newContent, err_2;
|
|
444
|
+
return utils.__generator(this, function (_b) {
|
|
445
|
+
switch (_b.label) {
|
|
446
|
+
case 0:
|
|
447
|
+
if (!message)
|
|
448
|
+
return [2 /*return*/];
|
|
449
|
+
event = message;
|
|
450
|
+
path = "newPath" in event
|
|
451
|
+
? event.newPath
|
|
452
|
+
: "path" in event
|
|
453
|
+
? event.path
|
|
454
|
+
: "";
|
|
455
|
+
return [4 /*yield*/, this.emulator.fs.stat(path)];
|
|
456
|
+
case 1:
|
|
457
|
+
type = (_b.sent()).type;
|
|
458
|
+
if (type !== "file")
|
|
459
|
+
return [2 /*return*/, null];
|
|
460
|
+
_b.label = 2;
|
|
461
|
+
case 2:
|
|
462
|
+
_b.trys.push([2, 10, , 11]);
|
|
463
|
+
_a = event.type;
|
|
464
|
+
switch (_a) {
|
|
465
|
+
case "change": return [3 /*break*/, 3];
|
|
466
|
+
case "create": return [3 /*break*/, 3];
|
|
467
|
+
case "remove": return [3 /*break*/, 5];
|
|
468
|
+
case "rename": return [3 /*break*/, 6];
|
|
469
|
+
case "close": return [3 /*break*/, 8];
|
|
470
|
+
}
|
|
471
|
+
return [3 /*break*/, 9];
|
|
472
|
+
case 3: return [4 /*yield*/, this.emulator.fs.readFile(event.path, "utf8")];
|
|
473
|
+
case 4:
|
|
474
|
+
content = _b.sent();
|
|
475
|
+
this.dispatch({
|
|
476
|
+
type: "fs/change",
|
|
477
|
+
path: event.path,
|
|
478
|
+
content: content,
|
|
479
|
+
});
|
|
480
|
+
return [3 /*break*/, 9];
|
|
481
|
+
case 5:
|
|
482
|
+
this.dispatch({
|
|
483
|
+
type: "fs/remove",
|
|
484
|
+
path: event.path,
|
|
485
|
+
});
|
|
486
|
+
return [3 /*break*/, 9];
|
|
487
|
+
case 6:
|
|
488
|
+
this.dispatch({
|
|
489
|
+
type: "fs/remove",
|
|
490
|
+
path: event.oldPath,
|
|
491
|
+
});
|
|
492
|
+
return [4 /*yield*/, this.emulator.fs.readFile(event.newPath, "utf8")];
|
|
493
|
+
case 7:
|
|
494
|
+
newContent = _b.sent();
|
|
495
|
+
this.dispatch({
|
|
496
|
+
type: "fs/change",
|
|
497
|
+
path: event.newPath,
|
|
498
|
+
content: newContent,
|
|
499
|
+
});
|
|
500
|
+
return [3 /*break*/, 9];
|
|
501
|
+
case 8: return [3 /*break*/, 9];
|
|
502
|
+
case 9: return [3 /*break*/, 11];
|
|
503
|
+
case 10:
|
|
504
|
+
err_2 = _b.sent();
|
|
505
|
+
this.dispatch({
|
|
506
|
+
type: "action",
|
|
507
|
+
action: "notification",
|
|
508
|
+
notificationType: "error",
|
|
509
|
+
title: consoleHook.getMessageFromError(err_2),
|
|
510
|
+
});
|
|
511
|
+
return [3 /*break*/, 11];
|
|
512
|
+
case 11: return [2 /*return*/];
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
}); })];
|
|
516
|
+
case 1:
|
|
517
|
+
_a.sent();
|
|
518
|
+
return [2 /*return*/];
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
};
|
|
523
|
+
/**
|
|
524
|
+
* PUBLIC Methods
|
|
525
|
+
*/
|
|
526
|
+
SandpackNode.prototype.restartShellProcess = function () {
|
|
527
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
528
|
+
var _a;
|
|
529
|
+
return utils.__generator(this, function (_b) {
|
|
530
|
+
switch (_b.label) {
|
|
531
|
+
case 0:
|
|
532
|
+
if (!(this.emulatorShellProcess && this.emulatorCommand)) return [3 /*break*/, 3];
|
|
533
|
+
// 1. Set the loading state and clean the URL
|
|
534
|
+
this.dispatch({ type: "start", firstLoad: true });
|
|
535
|
+
this.status = "initializing";
|
|
536
|
+
// 2. Exit shell
|
|
537
|
+
return [4 /*yield*/, this.emulatorShellProcess.kill()];
|
|
538
|
+
case 1:
|
|
539
|
+
// 2. Exit shell
|
|
540
|
+
_b.sent();
|
|
541
|
+
(_a = this.iframe) === null || _a === void 0 ? void 0 : _a.removeAttribute("attr");
|
|
542
|
+
this.emulator.fs.rm("/node_modules/.vite", {
|
|
543
|
+
recursive: true,
|
|
544
|
+
force: true,
|
|
545
|
+
});
|
|
546
|
+
// 3 Run command again
|
|
547
|
+
return [4 /*yield*/, this.compile()];
|
|
548
|
+
case 2:
|
|
549
|
+
// 3 Run command again
|
|
550
|
+
_b.sent();
|
|
551
|
+
_b.label = 3;
|
|
552
|
+
case 3: return [2 /*return*/];
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
SandpackNode.prototype.updateSandbox = function (setup) {
|
|
558
|
+
this.sandboxSetup = utils.__assign(utils.__assign({}, this.sandboxSetup), setup);
|
|
559
|
+
void this.updateSandboxAsync(setup);
|
|
560
|
+
};
|
|
561
|
+
SandpackNode.prototype.updateSandboxAsync = function (setup) {
|
|
562
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
563
|
+
var _a;
|
|
564
|
+
return utils.__generator(this, function (_b) {
|
|
565
|
+
// With native zenfs support in the Nodebox runtime, file content reaches
|
|
566
|
+
// the emulator through the shared filesystem rather than the compile
|
|
567
|
+
// message. Skip re-dispatching once the shell is already running.
|
|
568
|
+
if (((_a = this.emulatorShellProcess) === null || _a === void 0 ? void 0 : _a.state) === "running") {
|
|
569
|
+
return [2 /*return*/];
|
|
570
|
+
}
|
|
571
|
+
this.dispatch({
|
|
572
|
+
codesandbox: true,
|
|
573
|
+
template: setup.template,
|
|
574
|
+
type: "compile",
|
|
575
|
+
});
|
|
576
|
+
return [2 /*return*/];
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
};
|
|
580
|
+
SandpackNode.prototype.dispatch = function (message) {
|
|
581
|
+
return utils.__awaiter(this, void 0, void 0, function () {
|
|
582
|
+
var _a;
|
|
583
|
+
var _b, _c;
|
|
584
|
+
return utils.__generator(this, function (_d) {
|
|
585
|
+
switch (_d.label) {
|
|
586
|
+
case 0:
|
|
587
|
+
_a = message.type;
|
|
588
|
+
switch (_a) {
|
|
589
|
+
case "compile": return [3 /*break*/, 1];
|
|
590
|
+
case "refresh": return [3 /*break*/, 2];
|
|
591
|
+
case "urlback": return [3 /*break*/, 4];
|
|
592
|
+
case "urlforward": return [3 /*break*/, 4];
|
|
593
|
+
case "shell/restart": return [3 /*break*/, 5];
|
|
594
|
+
case "shell/openPreview": return [3 /*break*/, 6];
|
|
595
|
+
}
|
|
596
|
+
return [3 /*break*/, 7];
|
|
597
|
+
case 1:
|
|
598
|
+
this.compile();
|
|
599
|
+
return [3 /*break*/, 8];
|
|
600
|
+
case 2: return [4 /*yield*/, this.setLocationURLIntoIFrame()];
|
|
601
|
+
case 3:
|
|
602
|
+
_d.sent();
|
|
603
|
+
return [3 /*break*/, 8];
|
|
604
|
+
case 4:
|
|
605
|
+
(_c = (_b = this.iframe) === null || _b === void 0 ? void 0 : _b.contentWindow) === null || _c === void 0 ? void 0 : _c.postMessage(message, "*");
|
|
606
|
+
return [3 /*break*/, 8];
|
|
607
|
+
case 5:
|
|
608
|
+
this.restartShellProcess();
|
|
609
|
+
return [3 /*break*/, 8];
|
|
610
|
+
case 6:
|
|
611
|
+
window.open(this.iframePreviewUrl, "_blank");
|
|
612
|
+
return [3 /*break*/, 8];
|
|
613
|
+
case 7:
|
|
614
|
+
this.emitter.dispatch(message);
|
|
615
|
+
_d.label = 8;
|
|
616
|
+
case 8: return [2 /*return*/];
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
});
|
|
620
|
+
};
|
|
621
|
+
SandpackNode.prototype.listen = function (listener) {
|
|
622
|
+
return this.emitter.listener(listener);
|
|
623
|
+
};
|
|
624
|
+
SandpackNode.prototype.destroy = function () {
|
|
625
|
+
this.emulatorIframe.remove();
|
|
626
|
+
this.emitter.cleanup();
|
|
627
|
+
};
|
|
628
|
+
return SandpackNode;
|
|
629
|
+
}(base.SandpackClient));
|
|
630
|
+
|
|
631
|
+
exports.SandpackNode = SandpackNode;
|