@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,236 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var outvariant = require('outvariant');
|
|
4
|
+
var utils = require('./utils-BiVyytui.js');
|
|
5
|
+
require('./types-BIIEoWr6.js');
|
|
6
|
+
|
|
7
|
+
var EventEmitter = /** @class */ (function () {
|
|
8
|
+
function EventEmitter() {
|
|
9
|
+
this.listeners = {};
|
|
10
|
+
this.listenersCount = 0;
|
|
11
|
+
this.channelId = Math.floor(Math.random() * 1000000);
|
|
12
|
+
this.listeners = [];
|
|
13
|
+
}
|
|
14
|
+
EventEmitter.prototype.cleanup = function () {
|
|
15
|
+
this.listeners = {};
|
|
16
|
+
this.listenersCount = 0;
|
|
17
|
+
};
|
|
18
|
+
EventEmitter.prototype.dispatch = function (message) {
|
|
19
|
+
Object.values(this.listeners).forEach(function (listener) { return listener(message); });
|
|
20
|
+
};
|
|
21
|
+
EventEmitter.prototype.listener = function (listener) {
|
|
22
|
+
var _this = this;
|
|
23
|
+
if (typeof listener !== "function") {
|
|
24
|
+
return function () {
|
|
25
|
+
return;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
var listenerId = this.listenersCount;
|
|
29
|
+
this.listeners[listenerId] = listener;
|
|
30
|
+
this.listenersCount++;
|
|
31
|
+
return function () {
|
|
32
|
+
delete _this.listeners[listenerId];
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
return EventEmitter;
|
|
36
|
+
}());
|
|
37
|
+
|
|
38
|
+
function isCommand(char) {
|
|
39
|
+
return /[a-zA-Z.]/.test(char);
|
|
40
|
+
}
|
|
41
|
+
function isAlpha(char) {
|
|
42
|
+
return /[a-zA-Z]/.test(char);
|
|
43
|
+
}
|
|
44
|
+
function isWhitespace(char) {
|
|
45
|
+
return /\s/.test(char);
|
|
46
|
+
}
|
|
47
|
+
function isOperator(char) {
|
|
48
|
+
return /[&|]/.test(char);
|
|
49
|
+
}
|
|
50
|
+
function isArgument(char) {
|
|
51
|
+
return /-/.test(char);
|
|
52
|
+
}
|
|
53
|
+
function isString(char) {
|
|
54
|
+
return /["']/.test(char);
|
|
55
|
+
}
|
|
56
|
+
function isEnvVar(char) {
|
|
57
|
+
return isAlpha(char) && char === char.toUpperCase();
|
|
58
|
+
}
|
|
59
|
+
var TokenType;
|
|
60
|
+
(function (TokenType) {
|
|
61
|
+
TokenType["OR"] = "OR";
|
|
62
|
+
TokenType["AND"] = "AND";
|
|
63
|
+
TokenType["PIPE"] = "PIPE";
|
|
64
|
+
TokenType["Command"] = "Command";
|
|
65
|
+
TokenType["Argument"] = "Argument";
|
|
66
|
+
TokenType["String"] = "String";
|
|
67
|
+
TokenType["EnvVar"] = "EnvVar";
|
|
68
|
+
})(TokenType || (TokenType = {}));
|
|
69
|
+
var operators = new Map([
|
|
70
|
+
["&&", { type: TokenType.AND }],
|
|
71
|
+
["||", { type: TokenType.OR }],
|
|
72
|
+
["|", { type: TokenType.PIPE }],
|
|
73
|
+
["-", { type: TokenType.Argument }],
|
|
74
|
+
]);
|
|
75
|
+
function tokenize(input) {
|
|
76
|
+
var current = 0;
|
|
77
|
+
var tokens = [];
|
|
78
|
+
function parseCommand() {
|
|
79
|
+
var value = "";
|
|
80
|
+
while (isCommand(input[current]) && current < input.length) {
|
|
81
|
+
value += input[current];
|
|
82
|
+
current++;
|
|
83
|
+
}
|
|
84
|
+
return { type: TokenType.Command, value: value };
|
|
85
|
+
}
|
|
86
|
+
function parseOperator() {
|
|
87
|
+
var value = "";
|
|
88
|
+
while (isOperator(input[current]) && current < input.length) {
|
|
89
|
+
value += input[current];
|
|
90
|
+
current++;
|
|
91
|
+
}
|
|
92
|
+
return operators.get(value);
|
|
93
|
+
}
|
|
94
|
+
function parseArgument() {
|
|
95
|
+
var value = "";
|
|
96
|
+
while ((isArgument(input[current]) || isAlpha(input[current])) &&
|
|
97
|
+
current < input.length) {
|
|
98
|
+
value += input[current];
|
|
99
|
+
current++;
|
|
100
|
+
}
|
|
101
|
+
return { type: TokenType.Argument, value: value };
|
|
102
|
+
}
|
|
103
|
+
function parseString() {
|
|
104
|
+
var openCloseQuote = input[current];
|
|
105
|
+
var value = input[current];
|
|
106
|
+
current++;
|
|
107
|
+
while (input[current] !== openCloseQuote && current < input.length) {
|
|
108
|
+
value += input[current];
|
|
109
|
+
current++;
|
|
110
|
+
}
|
|
111
|
+
value += input[current];
|
|
112
|
+
current++;
|
|
113
|
+
return { type: TokenType.String, value: value };
|
|
114
|
+
}
|
|
115
|
+
function parseEnvVars() {
|
|
116
|
+
var value = {};
|
|
117
|
+
var parseSingleEnv = function () {
|
|
118
|
+
var key = "";
|
|
119
|
+
var pair = "";
|
|
120
|
+
while (input[current] !== "=" && current < input.length) {
|
|
121
|
+
key += input[current];
|
|
122
|
+
current++;
|
|
123
|
+
}
|
|
124
|
+
// Skip equal
|
|
125
|
+
if (input[current] === "=") {
|
|
126
|
+
current++;
|
|
127
|
+
}
|
|
128
|
+
while (input[current] !== " " && current < input.length) {
|
|
129
|
+
pair += input[current];
|
|
130
|
+
current++;
|
|
131
|
+
}
|
|
132
|
+
value[key] = pair;
|
|
133
|
+
};
|
|
134
|
+
while (isEnvVar(input[current]) && current < input.length) {
|
|
135
|
+
parseSingleEnv();
|
|
136
|
+
current++;
|
|
137
|
+
}
|
|
138
|
+
return { type: TokenType.EnvVar, value: value };
|
|
139
|
+
}
|
|
140
|
+
while (current < input.length) {
|
|
141
|
+
var currentChar = input[current];
|
|
142
|
+
if (isWhitespace(currentChar)) {
|
|
143
|
+
current++;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
switch (true) {
|
|
147
|
+
case isEnvVar(currentChar):
|
|
148
|
+
tokens.push(parseEnvVars());
|
|
149
|
+
break;
|
|
150
|
+
case isCommand(currentChar):
|
|
151
|
+
tokens.push(parseCommand());
|
|
152
|
+
break;
|
|
153
|
+
case isOperator(currentChar):
|
|
154
|
+
tokens.push(parseOperator());
|
|
155
|
+
break;
|
|
156
|
+
case isArgument(currentChar):
|
|
157
|
+
tokens.push(parseArgument());
|
|
158
|
+
break;
|
|
159
|
+
case isString(currentChar):
|
|
160
|
+
tokens.push(parseString());
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
throw new Error("Unknown character: ".concat(currentChar));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return tokens;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
var counter = 0;
|
|
170
|
+
function generateRandomId() {
|
|
171
|
+
var now = Date.now();
|
|
172
|
+
var randomNumber = Math.round(Math.random() * 10000);
|
|
173
|
+
var count = (counter += 1);
|
|
174
|
+
return (+"".concat(now).concat(randomNumber).concat(count)).toString(16);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Figure out which script it must run to start a server
|
|
178
|
+
*/
|
|
179
|
+
var findStartScriptPackageJson = function (packageJson) {
|
|
180
|
+
var scripts = {};
|
|
181
|
+
// TODO: support postinstall
|
|
182
|
+
var possibleKeys = ["dev", "start"];
|
|
183
|
+
try {
|
|
184
|
+
scripts = JSON.parse(packageJson).scripts;
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
throw utils.createError("Could not parse package.json file: " + e.message);
|
|
188
|
+
}
|
|
189
|
+
outvariant.invariant(scripts, "Failed to start. Please provide a `start` or `dev` script on the package.json");
|
|
190
|
+
var _loop_1 = function (index) {
|
|
191
|
+
if (possibleKeys[index] in scripts) {
|
|
192
|
+
var script = possibleKeys[index];
|
|
193
|
+
var candidate = scripts[script];
|
|
194
|
+
var env_1 = {};
|
|
195
|
+
var command_1 = "";
|
|
196
|
+
var args_1 = [];
|
|
197
|
+
tokenize(candidate).forEach(function (item) {
|
|
198
|
+
var commandNotFoundYet = command_1 === "";
|
|
199
|
+
if (item.type === TokenType.EnvVar) {
|
|
200
|
+
env_1 = item.value;
|
|
201
|
+
}
|
|
202
|
+
if (item.type === TokenType.Command && commandNotFoundYet) {
|
|
203
|
+
command_1 = item.value;
|
|
204
|
+
}
|
|
205
|
+
if (item.type === TokenType.Argument ||
|
|
206
|
+
(!commandNotFoundYet && item.type === TokenType.Command)) {
|
|
207
|
+
args_1.push(item.value);
|
|
208
|
+
}
|
|
209
|
+
// TODO: support TokenType.AND, TokenType.OR, TokenType.PIPE
|
|
210
|
+
});
|
|
211
|
+
return { value: [command_1, args_1, { env: env_1 }] };
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
for (var index = 0; index < possibleKeys.length; index++) {
|
|
215
|
+
var state_1 = _loop_1(index);
|
|
216
|
+
if (typeof state_1 === "object")
|
|
217
|
+
return state_1.value;
|
|
218
|
+
}
|
|
219
|
+
throw utils.createError("Failed to start. Please provide a `start` or `dev` script on the package.json");
|
|
220
|
+
};
|
|
221
|
+
var getMessageFromError = function (error) {
|
|
222
|
+
if (typeof error === "string")
|
|
223
|
+
return error;
|
|
224
|
+
if (typeof error === "object" && "message" in error) {
|
|
225
|
+
return error.message;
|
|
226
|
+
}
|
|
227
|
+
return utils.createError("The server could not be reached. Make sure that the node script is running and that a port has been started.");
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
var consoleHook = "function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,\"default\")?t.default:t}var r,e,n,a={},o={},i={},u={},s={},f={};function c(){return n||(n=1,f.__esModule=!0,f.update=f.state=void 0,f.update=function(t){f.state=t}),f}var l,d,p={},h={};function m(){return d||(d=1,function(t){var r=p&&p.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};t.__esModule=!0;var e=r((l||(l=1,function(t){var r=h&&h.__assign||function(){return r=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var a in r=arguments[e])Object.prototype.hasOwnProperty.call(r,a)&&(t[a]=r[a]);return t},r.apply(this,arguments)};t.__esModule=!0,t.initialState=void 0,t.initialState={timings:{},count:{}};var e=function(){return\"undefined\"!=typeof performance&&performance.now?performance.now():Date.now()};t.default=function(n,a){var o,i,u;switch(void 0===n&&(n=t.initialState),a.type){case\"COUNT\":var s=n.count[a.name]||0;return r(r({},n),{count:r(r({},n.count),(o={},o[a.name]=s+1,o))});case\"TIME_START\":return r(r({},n),{timings:r(r({},n.timings),(i={},i[a.name]={start:e()},i))});case\"TIME_END\":var f=n.timings[a.name],c=e(),l=c-f.start;return r(r({},n),{timings:r(r({},n.timings),(u={},u[a.name]=r(r({},f),{end:c,time:l}),u))});default:return n}}}(h)),h)),n=c();t.default=function(t){(0,n.update)((0,e.default)(n.state,t))}}(p)),p}var y,v,_={};function b(){return y||(y=1,_.__esModule=!0,_.timeEnd=_.timeStart=_.count=void 0,_.count=function(t){return{type:\"COUNT\",name:t}},_.timeStart=function(t){return{type:\"TIME_START\",name:t}},_.timeEnd=function(t){return{type:\"TIME_END\",name:t}}),_}var g,M,S,T={},O={};var w,A,j,z,E={},k={},C={},D={},I={};var N,R,x,P,V={},B={};function L(){return R||(R=1,function(t){t.__esModule=!0;var r=\"@t\",e=/^#*@(t|r)$/,n=\"__console_feed_remaining__\",a=(0,eval)(\"this\"),o=\"function\"==typeof ArrayBuffer,i=\"function\"==typeof Map,u=\"function\"==typeof Set,s=[\"Int8Array\",\"Uint8Array\",\"Uint8ClampedArray\",\"Int16Array\",\"Uint16Array\",\"Int32Array\",\"Uint32Array\",\"Float32Array\",\"Float64Array\"],f=Array.prototype.slice,c={serialize:function(t){return JSON.stringify(t)},deserialize:function(t){return JSON.parse(t)}},l=function(){function t(t,r,e){this.references=t,this.transforms=r,this.transformsMap=this._makeTransformsMap(),this.circularCandidates=[],this.circularCandidatesDescrs=[],this.circularRefCount=0,this.limit=null!=e?e:1/0}return t._createRefMark=function(t){var r=Object.create(null);return r[\"@r\"]=t,r},t.prototype._createCircularCandidate=function(t,r,e){this.circularCandidates.push(t),this.circularCandidatesDescrs.push({parent:r,key:e,refIdx:-1})},t.prototype._applyTransform=function(t,e,n,a){var o=Object.create(null),i=a.toSerializable(t);return\"object\"==typeof i&&this._createCircularCandidate(t,e,n),o[r]=a.type,o.data=this._handleValue(function(){return i},e,n),o},t.prototype._handleArray=function(t){for(var r=[],e=Math.min(t.length,this.limit),a=t.length-e,o=function(e){r[e]=i._handleValue(function(){return t[e]},r,e)},i=this,u=0;u<e;u++)o(u);return r[e]=n+a,r},t.prototype._handlePlainObject=function(t){var r,a,o=Object.create(null),i=0,u=0,s=function(r){if(Reflect.has(t,r)){if(i>=f.limit)return u++,\"continue\";var n=e.test(r)?\"#\".concat(r):r;o[n]=f._handleValue(function(){return t[r]},o,n),i++,u++}},f=this;for(var c in t)s(c);var l=u-i,d=null===(a=null===(r=null==t?void 0:t.__proto__)||void 0===r?void 0:r.constructor)||void 0===a?void 0:a.name;return d&&\"Object\"!==d&&(o.constructor={name:d}),l&&(o[n]=l),o},t.prototype._handleObject=function(t,r,e){return this._createCircularCandidate(t,r,e),Array.isArray(t)?this._handleArray(t):this._handlePlainObject(t)},t.prototype._ensureCircularReference=function(r){var e=this.circularCandidates.indexOf(r);if(e>-1){var n=this.circularCandidatesDescrs[e];return-1===n.refIdx&&(n.refIdx=n.parent?++this.circularRefCount:0),t._createRefMark(n.refIdx)}return null},t.prototype._handleValue=function(t,r,e){try{var n=t(),a=typeof n,o=\"object\"===a&&null!==n;if(o){var i=this._ensureCircularReference(n);if(i)return i}var u=this._findTransform(a,n);return u?this._applyTransform(n,r,e,u):o?this._handleObject(n,r,e):n}catch(t){try{return this._handleValue(function(){return t instanceof Error?t:new Error(t)},r,e)}catch(t){return null}}},t.prototype._makeTransformsMap=function(){if(i){var t=new Map;return this.transforms.forEach(function(r){r.lookup&&t.set(r.lookup,r)}),t}},t.prototype._findTransform=function(t,r){if(i&&r&&r.constructor&&(null==(a=this.transformsMap.get(r.constructor))?void 0:a.shouldTransform(t,r)))return a;for(var e=0,n=this.transforms;e<n.length;e++){var a;if((a=n[e]).shouldTransform(t,r))return a}},t.prototype.transform=function(){for(var r=this,e=[this._handleValue(function(){return r.references},null,null)],n=0,a=this.circularCandidatesDescrs;n<a.length;n++){var o=a[n];o.refIdx>0&&(e[o.refIdx]=o.parent[o.key],o.parent[o.key]=t._createRefMark(o.refIdx))}return e},t}(),d=function(){function t(t,r){this.activeTransformsStack=[],this.visitedRefs=Object.create(null),this.references=t,this.transformMap=r}return t.prototype._handlePlainObject=function(t){var r=Object.create(null);for(var n in\"constructor\"in t&&(t.constructor&&\"string\"==typeof t.constructor.name||(t.constructor={name:\"Object\"})),t)t.hasOwnProperty(n)&&(this._handleValue(t[n],t,n),e.test(n)&&(r[n.substring(1)]=t[n],delete t[n]));for(var a in r)t[a]=r[a]},t.prototype._handleTransformedObject=function(t,e,n){var a=t[r],o=this.transformMap[a];if(!o)throw new Error(\"Can't find transform for \\\"\".concat(a,'\" type.'));this.activeTransformsStack.push(t),this._handleValue(t.data,t,\"data\"),this.activeTransformsStack.pop(),e[n]=o.fromSerializable(t.data)},t.prototype._handleCircularSelfRefDuringTransform=function(t,r,e){var n=this.references;Object.defineProperty(r,e,{val:void 0,configurable:!0,enumerable:!0,get:function(){return void 0===this.val&&(this.val=n[t]),this.val},set:function(t){this.val=t}})},t.prototype._handleCircularRef=function(t,r,e){this.activeTransformsStack.includes(this.references[t])?this._handleCircularSelfRefDuringTransform(t,r,e):(this.visitedRefs[t]||(this.visitedRefs[t]=!0,this._handleValue(this.references[t],this.references,t)),r[e]=this.references[t])},t.prototype._handleValue=function(t,e,n){if(\"object\"==typeof t&&null!==t){var a=t[\"@r\"];if(void 0!==a)this._handleCircularRef(a,e,n);else if(t[r])this._handleTransformedObject(t,e,n);else if(Array.isArray(t))for(var o=0;o<t.length;o++)this._handleValue(t[o],t,o);else this._handlePlainObject(t)}},t.prototype.transform=function(){return this.visitedRefs[0]=!0,this._handleValue(this.references[0],this.references,0),this.references[0]},t}(),p=[{type:\"[[NaN]]\",shouldTransform:function(t,r){return\"number\"===t&&isNaN(r)},toSerializable:function(){return\"\"},fromSerializable:function(){return NaN}},{type:\"[[undefined]]\",shouldTransform:function(t){return\"undefined\"===t},toSerializable:function(){return\"\"},fromSerializable:function(){}},{type:\"[[Date]]\",lookup:Date,shouldTransform:function(t,r){return r instanceof Date},toSerializable:function(t){return t.getTime()},fromSerializable:function(t){var r=new Date;return r.setTime(t),r}},{type:\"[[RegExp]]\",lookup:RegExp,shouldTransform:function(t,r){return r instanceof RegExp},toSerializable:function(t){var r={src:t.source,flags:\"\"};return t.globalThis&&(r.flags+=\"g\"),t.ignoreCase&&(r.flags+=\"i\"),t.multiline&&(r.flags+=\"m\"),r},fromSerializable:function(t){return new RegExp(t.src,t.flags)}},{type:\"[[Error]]\",lookup:Error,shouldTransform:function(t,r){return r instanceof Error},toSerializable:function(t){var r,e;return t.stack||null===(e=(r=Error).captureStackTrace)||void 0===e||e.call(r,t),{name:t.name,message:t.message,stack:t.stack}},fromSerializable:function(t){var r=new(a[t.name]||Error)(t.message);return r.stack=t.stack,r}},{type:\"[[ArrayBuffer]]\",lookup:o&&ArrayBuffer,shouldTransform:function(t,r){return o&&r instanceof ArrayBuffer},toSerializable:function(t){var r=new Int8Array(t);return f.call(r)},fromSerializable:function(t){if(o){var r=new ArrayBuffer(t.length);return new Int8Array(r).set(t),r}return t}},{type:\"[[TypedArray]]\",shouldTransform:function(t,r){if(o)return ArrayBuffer.isView(r)&&!(r instanceof DataView);for(var e=0,n=s;e<n.length;e++){var i=n[e];if(\"function\"==typeof a[i]&&r instanceof a[i])return!0}return!1},toSerializable:function(t){return{ctorName:t.constructor.name,arr:f.call(t)}},fromSerializable:function(t){return\"function\"==typeof a[t.ctorName]?new a[t.ctorName](t.arr):t.arr}},{type:\"[[Map]]\",lookup:i&&Map,shouldTransform:function(t,r){return i&&r instanceof Map},toSerializable:function(t){var r=[];return t.forEach(function(t,e){r.push(e),r.push(t)}),r},fromSerializable:function(t){if(i){for(var r=new Map,e=0;e<t.length;e+=2)r.set(t[e],t[e+1]);return r}for(var n=[],a=0;a<t.length;a+=2)n.push([t[e],t[e+1]]);return n}},{type:\"[[Set]]\",lookup:u&&Set,shouldTransform:function(t,r){return u&&r instanceof Set},toSerializable:function(t){var r=[];return t.forEach(function(t){r.push(t)}),r},fromSerializable:function(t){if(u){for(var r=new Set,e=0;e<t.length;e++)r.add(t[e]);return r}return t}}],h=function(){function t(t){this.transforms=[],this.transformsMap=Object.create(null),this.serializer=t||c,this.addTransforms(p)}return t.prototype.addTransforms=function(t){for(var r=0,e=t=Array.isArray(t)?t:[t];r<e.length;r++){var n=e[r];if(this.transformsMap[n.type])throw new Error('Transform with type \"'.concat(n.type,'\" was already added.'));this.transforms.push(n),this.transformsMap[n.type]=n}return this},t.prototype.removeTransforms=function(t){for(var r=0,e=t=Array.isArray(t)?t:[t];r<e.length;r++){var n=e[r],a=this.transforms.indexOf(n);a>-1&&this.transforms.splice(a,1),delete this.transformsMap[n.type]}return this},t.prototype.encode=function(t,r){var e=new l(t,this.transforms,r).transform();return this.serializer.serialize(e)},t.prototype.decode=function(t){var r=this.serializer.deserialize(t);return new d(r,this.transformsMap).transform()},t}();t.default=h}(B)),B}function H(){if(x)return E;x=1;var t=E&&E.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};E.__esModule=!0,E.Decode=E.Encode=void 0;var r,e,n=t((w||(w=1,(r=k).__esModule=!0,function(t){t[t.infinity=0]=\"infinity\",t[t.minusInfinity=1]=\"minusInfinity\",t[t.minusZero=2]=\"minusZero\"}(e||(e={})),r.default={type:\"Arithmetic\",lookup:Number,shouldTransform:function(t,r){return\"number\"===t&&(r===1/0||r===-1/0||function(t){return 1/t==-1/0}(r))},toSerializable:function(t){return t===1/0?e.infinity:t===-1/0?e.minusInfinity:e.minusZero},fromSerializable:function(t){return t===e.infinity?1/0:t===e.minusInfinity?-1/0:t===e.minusZero?-0:t}}),k)),a=t(function(){return A||(A=1,(t=C).__esModule=!0,t.default={type:\"BigInt\",shouldTransform:function(t,r){return\"bigint\"==typeof r},toSerializable:function(t){return\"\".concat(t,\"n\")},fromSerializable:function(t){return BigInt(t.slice(0,-1))}}),C;var t}()),o=t(function(){return j||(j=1,(t=D).__esModule=!0,t.default={type:\"Function\",lookup:Function,shouldTransform:function(t,r){return\"function\"==typeof r},toSerializable:function(t){var r=\"\";try{r=t.toString().substring(r.indexOf(\"{\")+1,r.lastIndexOf(\"}\"))}catch(t){}return{name:t.name,body:r,proto:Object.getPrototypeOf(t).constructor.name}},fromSerializable:function(t){try{var r=function(){};return\"string\"==typeof t.name&&Object.defineProperty(r,\"name\",{value:t.name,writable:!1}),\"string\"==typeof t.body&&Object.defineProperty(r,\"body\",{value:t.body,writable:!1}),\"string\"==typeof t.proto&&(r.constructor={name:t.proto}),r}catch(r){return t}}}),D;var t}()),i=t((z||(z=1,function(t){var r;function e(t){for(var r={},e=0,n=t.attributes;e<n.length;e++){var a=n[e];r[a.name]=a.value}return r}t.__esModule=!0,t.default={type:\"HTMLElement\",shouldTransform:function(t,r){return r&&r.children&&\"string\"==typeof r.innerHTML&&\"string\"==typeof r.tagName},toSerializable:function(t){return{tagName:t.tagName.toLowerCase(),attributes:e(t),innerHTML:t.innerHTML}},fromSerializable:function(t){try{var e=(r||(r=document.implementation.createHTMLDocument(\"sandbox\"))).createElement(t.tagName);e.innerHTML=t.innerHTML;for(var n=0,a=Object.keys(t.attributes);n<a.length;n++){var o=a[n];try{e.setAttribute(o,t.attributes[o])}catch(t){}}return e}catch(r){return t}}}}(I)),I)),u=t(function(){return N||(N=1,r=V&&V.__assign||function(){return r=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var a in r=arguments[e])Object.prototype.hasOwnProperty.call(r,a)&&(t[a]=r[a]);return t},r.apply(this,arguments)},(t=V).__esModule=!0,t.default={type:\"Map\",lookup:Map,shouldTransform:function(t,r){return r&&r.constructor&&\"Map\"===r.constructor.name},toSerializable:function(t){var r={};return t.forEach(function(t,e){var n=\"object\"==typeof e?JSON.stringify(e):e;r[n]=t}),{name:\"Map\",body:r,proto:Object.getPrototypeOf(t).constructor.name}},fromSerializable:function(t){var e=t.body,n=r({},e);return\"string\"==typeof t.proto&&(n.constructor={name:t.proto}),n}}),V;var t,r}()),s=t(L()),f=[i.default,o.default,n.default,u.default,a.default],c=new s.default;return c.addTransforms(f),E.Encode=function(t,r){return JSON.parse(c.encode(t,r))},E.Decode=function(t){var r=c.decode(JSON.stringify(t));return r.data.pop(),r},E}var U=t((P||(P=1,function(t){var n=a&&a.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};t.__esModule=!0;var f=n((r||(r=1,function(t){t.__esModule=!0,t.default=[\"log\",\"debug\",\"info\",\"warn\",\"error\",\"table\",\"clear\",\"time\",\"timeEnd\",\"count\",\"assert\",\"command\",\"result\",\"dir\"]}(o)),o)),l=n((S||(S=1,function(t){var r=i&&i.__assign||function(){return r=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var a in r=arguments[e])Object.prototype.hasOwnProperty.call(r,a)&&(t[a]=r[a]);return t},r.apply(this,arguments)},n=i&&i.__createBinding||(Object.create?function(t,r,e,n){void 0===n&&(n=e);var a=Object.getOwnPropertyDescriptor(r,e);a&&!(\"get\"in a?!r.__esModule:a.writable||a.configurable)||(a={enumerable:!0,get:function(){return r[e]}}),Object.defineProperty(t,n,a)}:function(t,r,e,n){void 0===n&&(n=e),t[n]=r[e]}),a=i&&i.__setModuleDefault||(Object.create?function(t,r){Object.defineProperty(t,\"default\",{enumerable:!0,value:r})}:function(t,r){t.default=r}),o=i&&i.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)\"default\"!==e&&Object.prototype.hasOwnProperty.call(t,e)&&n(r,t,e);return a(r,t),r},f=i&&i.__spreadArray||function(t,r,e){if(e||2===arguments.length)for(var n,a=0,o=r.length;a<o;a++)!n&&a in r||(n||(n=Array.prototype.slice.call(r,0,a)),n[a]=r[a]);return t.concat(n||Array.prototype.slice.call(r))},l=i&&i.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};t.__esModule=!0;var d=l(function(){return e||(e=1,(t=u).__esModule=!0,t.default=function(){var t=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)};return t()+t()+\"-\"+t()+\"-\"+t()+\"-\"+t()+\"-\"+t()+\"-\"+Date.now()}),u;var t}()),p=o(function(){if(v)return s;v=1;var t=s&&s.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};s.__esModule=!0,s.stop=s.start=void 0;var r=c(),e=t(m()),n=b();return s.start=function(t){(0,e.default)((0,n.timeStart)(t))},s.stop=function(t){var a=null===r.state||void 0===r.state?void 0:r.state.timings[t];if(a&&!a.end){(0,e.default)((0,n.timeEnd)(t));var o=r.state.timings[t].time;return{method:\"log\",data:[\"\".concat(t,\": \").concat(o,\"ms\")]}}return{method:\"warn\",data:[\"Timer '\".concat(t,\"' does not exist\")]}},s}()),h=o(function(){if(g)return T;g=1;var t=T&&T.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};T.__esModule=!0,T.increment=void 0;var r=c(),e=t(m()),n=b();return T.increment=function(t){(0,e.default)((0,n.count)(t));var a=r.state.count[t];return{method:\"log\",data:[\"\".concat(t,\": \").concat(a)]}},T}()),y=o(function(){if(M)return O;M=1;var t=O&&O.__spreadArray||function(t,r,e){if(e||2===arguments.length)for(var n,a=0,o=r.length;a<o;a++)!n&&a in r||(n||(n=Array.prototype.slice.call(r,0,a)),n[a]=r[a]);return t.concat(n||Array.prototype.slice.call(r))};return O.__esModule=!0,O.test=void 0,O.test=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return!r&&(0===e.length&&e.push(\"console.assert\"),{method:\"error\",data:t([\"Assertion failed:\"],e,!0)})},O}());t.default=function(t,e,n){var a=n||(0,d.default)();switch(t){case\"clear\":return{method:t,id:a};case\"count\":return!!(o=\"string\"==typeof e[0]?e[0]:\"default\")&&r(r({},h.increment(o)),{id:a});case\"time\":case\"timeEnd\":var o;return!!(o=\"string\"==typeof e[0]?e[0]:\"default\")&&(\"time\"===t?(p.start(o),!1):r(r({},p.stop(o)),{id:a}));case\"assert\":if(0!==e.length){var i=y.test.apply(y,f([e[0]],e.slice(1),!1));if(i)return r(r({},i),{id:a})}return!1;case\"error\":return{method:t,id:a,data:e.map(function(t){try{return t.stack||t}catch(r){return t}})};default:return{method:t,id:a,data:e}}}}(i)),i)),d=H();t.default=function(t,r,e,n){void 0===e&&(e=!0);for(var a=t,o={pointers:{},src:{npm:\"https://npmjs.com/package/console-feed\",github:\"https://github.com/samdenty/console-feed\"}},i=function(t){var i=a[t];a[t]=function(){i.apply(this,arguments);var a=[].slice.call(arguments);setTimeout(function(){var o=(0,l.default)(t,a);if(o){var i=o;e&&(i=(0,d.Encode)(o,n)),r(i,o)}})},o.pointers[t]=i},u=0,s=f.default;u<s.length;u++)i(s[u]);return a.feed=o,a}}(a)),a)),J=H();U(window.console,function(t){var r=J.Encode(t);parent.postMessage({type:\"console\",codesandbox:!0,log:Array.isArray(r)?r[0]:r,channelId:scope.channelId},\"*\")});\n";
|
|
231
|
+
|
|
232
|
+
exports.EventEmitter = EventEmitter;
|
|
233
|
+
exports.consoleHook = consoleHook;
|
|
234
|
+
exports.findStartScriptPackageJson = findStartScriptPackageJson;
|
|
235
|
+
exports.generateRandomId = generateRandomId;
|
|
236
|
+
exports.getMessageFromError = getMessageFromError;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { BoundContext } from "@zenfs/core";
|
|
2
|
+
/**
|
|
3
|
+
* Per-file UI metadata. The file *content* lives in the filesystem as bytes;
|
|
4
|
+
* everything else (visibility, editability, initial focus) is kept in a sidecar
|
|
5
|
+
* file at {@link META_PATH}.
|
|
6
|
+
*/
|
|
7
|
+
export interface FileMeta {
|
|
8
|
+
hidden?: boolean;
|
|
9
|
+
active?: boolean;
|
|
10
|
+
readOnly?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export type FileMetaMap = Record<string, FileMeta>;
|
|
13
|
+
/**
|
|
14
|
+
* Path (within the SandpackFS) of the sidecar metadata file. Anything below
|
|
15
|
+
* `/.sandpack/` is treated as internal and excluded from {@link SandpackFS.list}.
|
|
16
|
+
*/
|
|
17
|
+
export declare const META_PATH = "/.sandpack/meta.json";
|
|
18
|
+
/**
|
|
19
|
+
* A filesystem change. `external` is `true` when the change originated from the
|
|
20
|
+
* child iframe (relayed through the ZenFS `Port` / `attachFS` boundary) and
|
|
21
|
+
* `false` for local edits made through this `SandpackFS`. The editor reacts
|
|
22
|
+
* only to `external` changes — that origin tag is what prevents the editor from
|
|
23
|
+
* reacting to its own writes.
|
|
24
|
+
*/
|
|
25
|
+
export interface SandpackFSChange {
|
|
26
|
+
path: string;
|
|
27
|
+
external: boolean;
|
|
28
|
+
}
|
|
29
|
+
export type SandpackFSListener = (change: SandpackFSChange) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Shape Sandpack accepts at the public boundary. Each entry carries the
|
|
32
|
+
* file body (`code`) and any optional {@link FileMeta} flags. The more
|
|
33
|
+
* permissive `string | {...}` form used in user-facing React props is
|
|
34
|
+
* normalized to this object shape before being handed to
|
|
35
|
+
* {@link SandpackFS.fromFiles}.
|
|
36
|
+
*/
|
|
37
|
+
export type SandpackFilesInput = Record<string, FileMeta & {
|
|
38
|
+
code: string;
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* A filesystem-shaped handle over the files Sandpack renders. It wraps a
|
|
42
|
+
* ZenFS @see FileSystem mounted at a unique path prefix so multiple
|
|
43
|
+
* `SandpackProvider` instances stay isolated.
|
|
44
|
+
*
|
|
45
|
+
* All reads / writes are async. Changes emit a single coalesced notification
|
|
46
|
+
* (watcher or explicit helper calls) so React can subscribe via
|
|
47
|
+
* `useSyncExternalStore`.
|
|
48
|
+
*/
|
|
49
|
+
export declare class SandpackFS {
|
|
50
|
+
readonly fsContext: BoundContext;
|
|
51
|
+
readonly remotePortFactory: (onRemoteChange: (path: string) => void) => Promise<MessagePort>;
|
|
52
|
+
private readonly onWrite?;
|
|
53
|
+
private readonly listeners;
|
|
54
|
+
private metaCache;
|
|
55
|
+
private sidecarEnvironment;
|
|
56
|
+
private sidecarMode;
|
|
57
|
+
private disposed;
|
|
58
|
+
private constructor();
|
|
59
|
+
/**
|
|
60
|
+
* Create the `MessagePort` shared with the child iframe, wiring the iframe's
|
|
61
|
+
* write notifications back into this instance. The host's factory forwards
|
|
62
|
+
* `onRemoteChange` to `exportZenFS`, which calls it whenever the iframe writes
|
|
63
|
+
* a file over the Port — surfaced here as an `external` change.
|
|
64
|
+
*/
|
|
65
|
+
connectRemote(): Promise<MessagePort>;
|
|
66
|
+
/**
|
|
67
|
+
* Create a new filesystem backed by an InMemory store and seed it with the
|
|
68
|
+
* given files.
|
|
69
|
+
*/
|
|
70
|
+
static fromFiles(files: SandpackFilesInput | undefined, options: {
|
|
71
|
+
environment?: string;
|
|
72
|
+
mode?: string;
|
|
73
|
+
} | undefined, remotePortFactory: (onRemoteChange: (path: string) => void) => Promise<MessagePort>, onWrite?: (path: string) => void): Promise<SandpackFS>;
|
|
74
|
+
/**
|
|
75
|
+
* Adopt an existing ZenFS filesystem. The caller is responsible for its
|
|
76
|
+
* lifecycle - {@link dispose} will unmount but not destroy the underlying
|
|
77
|
+
* store.
|
|
78
|
+
*/
|
|
79
|
+
static fromFileSystemContext(fsContext: BoundContext, remotePortFactory: (onRemoteChange: (path: string) => void) => Promise<MessagePort>, onWrite?: (path: string) => void): Promise<SandpackFS>;
|
|
80
|
+
/**
|
|
81
|
+
* Return every visible file path (leading `/`). Metadata sidecar and
|
|
82
|
+
* everything under `/.sandpack/` is excluded.
|
|
83
|
+
*/
|
|
84
|
+
list(): Promise<string[]>;
|
|
85
|
+
readFile(path: string): Promise<string>;
|
|
86
|
+
writeFile(path: string, content: string): Promise<void>;
|
|
87
|
+
unlink(path: string): Promise<void>;
|
|
88
|
+
exists(path: string): Promise<boolean>;
|
|
89
|
+
getAllMetadata(): FileMetaMap;
|
|
90
|
+
getMetadata(path: string): FileMeta;
|
|
91
|
+
setMetadata(path: string, patch: FileMeta): Promise<void>;
|
|
92
|
+
getEnvironment(): string | undefined;
|
|
93
|
+
setEnvironment(environment: string): Promise<void>;
|
|
94
|
+
getMode(): string | undefined;
|
|
95
|
+
setMode(mode: string): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Subscribe to any mutation. Each change carries its `path` and an `external`
|
|
98
|
+
* flag (`true` = written by the child iframe, `false` = a local edit). See
|
|
99
|
+
* {@link SandpackFSChange}.
|
|
100
|
+
*/
|
|
101
|
+
onChange(cb: SandpackFSListener): () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Record a write made by the child iframe (relayed from the `attachFS`
|
|
104
|
+
* boundary in the host). Surfaces as an `external` change so the editor can
|
|
105
|
+
* reflect it — distinct from local edits, which never reach here.
|
|
106
|
+
*/
|
|
107
|
+
handleRemoteChange(path: string): void;
|
|
108
|
+
private toAbs;
|
|
109
|
+
private notify;
|
|
110
|
+
private ensureMetaDir;
|
|
111
|
+
private ensureParent;
|
|
112
|
+
private walk;
|
|
113
|
+
private writeInitial;
|
|
114
|
+
private persistMeta;
|
|
115
|
+
private refreshMetaCache;
|
|
116
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The single chokepoint for creating **app iframes** (UI_AS_APPS_SPEC §2 / G1 /
|
|
5
|
+
* threat T1).
|
|
6
|
+
*
|
|
7
|
+
* App iframes MUST be opaque-origin: `sandbox="allow-scripts …"` WITHOUT
|
|
8
|
+
* `allow-same-origin`. With `allow-same-origin` alongside `allow-scripts` an app
|
|
9
|
+
* could remove its own sandboxing and reach the parent — the whole capability
|
|
10
|
+
* model collapses. This is the one invariant with no defense-in-depth, so
|
|
11
|
+
* creation is centralized here and the resolved attribute is asserted. Raw
|
|
12
|
+
* `document.createElement('iframe')` for app content elsewhere is forbidden (a
|
|
13
|
+
* greppable CI check), so the verifiable invariant is "every app iframe is born
|
|
14
|
+
* in this factory."
|
|
15
|
+
*
|
|
16
|
+
* Scope: the opaque-origin app iframes (the runtime + static preview clients).
|
|
17
|
+
* The node/nodebox emulator is a different execution model and is intentionally
|
|
18
|
+
* NOT routed through here.
|
|
19
|
+
*/
|
|
20
|
+
var APP_SANDBOX = "allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-downloads allow-pointer-lock";
|
|
21
|
+
var APP_ALLOW = "accelerometer; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; clipboard-read; clipboard-write; xr-spatial-tracking;";
|
|
22
|
+
/** Throw if the iframe would run scripts at a same-origin context (G1/T1). */
|
|
23
|
+
function assertOpaqueOrigin(iframe) {
|
|
24
|
+
var _a;
|
|
25
|
+
var sandbox = (_a = iframe.getAttribute("sandbox")) !== null && _a !== void 0 ? _a : "";
|
|
26
|
+
if (/(^|\s)allow-same-origin(\s|$)/.test(sandbox)) {
|
|
27
|
+
throw new Error("Refusing an app iframe with allow-same-origin alongside allow-scripts: " +
|
|
28
|
+
"the sandbox would be void (UI_AS_APPS_SPEC G1/T1).");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Create an opaque-origin sandboxed iframe for running untrusted app code. */
|
|
32
|
+
function createSandboxedIframe(doc) {
|
|
33
|
+
if (doc === void 0) { doc = document; }
|
|
34
|
+
var iframe = doc.createElement("iframe");
|
|
35
|
+
iframe.setAttribute("sandbox", APP_SANDBOX);
|
|
36
|
+
iframe.setAttribute("allow", APP_ALLOW);
|
|
37
|
+
assertOpaqueOrigin(iframe);
|
|
38
|
+
return iframe;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Ensure a (possibly externally-provided) app iframe is opaque-origin: set the
|
|
42
|
+
* sandbox/allow attributes if absent, then assert no `allow-same-origin`. Use
|
|
43
|
+
* this for the case where a host passes in its own iframe element.
|
|
44
|
+
*/
|
|
45
|
+
function ensureSandboxed(iframe) {
|
|
46
|
+
if (!iframe.getAttribute("sandbox")) {
|
|
47
|
+
iframe.setAttribute("sandbox", APP_SANDBOX);
|
|
48
|
+
iframe.setAttribute("allow", APP_ALLOW);
|
|
49
|
+
}
|
|
50
|
+
assertOpaqueOrigin(iframe);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.createSandboxedIframe = createSandboxedIframe;
|
|
54
|
+
exports.ensureSandboxed = ensureSandboxed;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single chokepoint for creating **app iframes** (UI_AS_APPS_SPEC §2 / G1 /
|
|
3
|
+
* threat T1).
|
|
4
|
+
*
|
|
5
|
+
* App iframes MUST be opaque-origin: `sandbox="allow-scripts …"` WITHOUT
|
|
6
|
+
* `allow-same-origin`. With `allow-same-origin` alongside `allow-scripts` an app
|
|
7
|
+
* could remove its own sandboxing and reach the parent — the whole capability
|
|
8
|
+
* model collapses. This is the one invariant with no defense-in-depth, so
|
|
9
|
+
* creation is centralized here and the resolved attribute is asserted. Raw
|
|
10
|
+
* `document.createElement('iframe')` for app content elsewhere is forbidden (a
|
|
11
|
+
* greppable CI check), so the verifiable invariant is "every app iframe is born
|
|
12
|
+
* in this factory."
|
|
13
|
+
*
|
|
14
|
+
* Scope: the opaque-origin app iframes (the runtime + static preview clients).
|
|
15
|
+
* The node/nodebox emulator is a different execution model and is intentionally
|
|
16
|
+
* NOT routed through here.
|
|
17
|
+
*/
|
|
18
|
+
var APP_SANDBOX = "allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-downloads allow-pointer-lock";
|
|
19
|
+
var APP_ALLOW = "accelerometer; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; clipboard-read; clipboard-write; xr-spatial-tracking;";
|
|
20
|
+
/** Throw if the iframe would run scripts at a same-origin context (G1/T1). */
|
|
21
|
+
function assertOpaqueOrigin(iframe) {
|
|
22
|
+
var _a;
|
|
23
|
+
var sandbox = (_a = iframe.getAttribute("sandbox")) !== null && _a !== void 0 ? _a : "";
|
|
24
|
+
if (/(^|\s)allow-same-origin(\s|$)/.test(sandbox)) {
|
|
25
|
+
throw new Error("Refusing an app iframe with allow-same-origin alongside allow-scripts: " +
|
|
26
|
+
"the sandbox would be void (UI_AS_APPS_SPEC G1/T1).");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Create an opaque-origin sandboxed iframe for running untrusted app code. */
|
|
30
|
+
function createSandboxedIframe(doc) {
|
|
31
|
+
if (doc === void 0) { doc = document; }
|
|
32
|
+
var iframe = doc.createElement("iframe");
|
|
33
|
+
iframe.setAttribute("sandbox", APP_SANDBOX);
|
|
34
|
+
iframe.setAttribute("allow", APP_ALLOW);
|
|
35
|
+
assertOpaqueOrigin(iframe);
|
|
36
|
+
return iframe;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Ensure a (possibly externally-provided) app iframe is opaque-origin: set the
|
|
40
|
+
* sandbox/allow attributes if absent, then assert no `allow-same-origin`. Use
|
|
41
|
+
* this for the case where a host passes in its own iframe element.
|
|
42
|
+
*/
|
|
43
|
+
function ensureSandboxed(iframe) {
|
|
44
|
+
if (!iframe.getAttribute("sandbox")) {
|
|
45
|
+
iframe.setAttribute("sandbox", APP_SANDBOX);
|
|
46
|
+
iframe.setAttribute("allow", APP_ALLOW);
|
|
47
|
+
}
|
|
48
|
+
assertOpaqueOrigin(iframe);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { createSandboxedIframe as c, ensureSandboxed as e };
|