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