-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
636 lines (636 loc) · 16.7 KB
/
Copy pathbundle.js
File metadata and controls
636 lines (636 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
(function(){
var win, doc, enableRspvarsetcb, _fetch, proxin, ref$, rsp;
enableRspvarsetcb = true;
_fetch = function(u, c){
if (rsp.__node && (typeof fs != 'undefined' && fs !== null) && !/^https?:/.exec(u)) {
return new Promise(function(res, rej){
return fs.readFile(u, function(e, b){
if (e) {
return rej(e);
} else {
return res(b.toString());
}
});
});
}
return fetch(u, c).then(function(ret){
var ref$;
if (ret && ret.ok) {
return ret.text();
}
if (!ret) {
return Promise.reject((ref$ = new Error("404"), ref$.name = 'lderror', ref$.id = 404, ref$));
}
return ret.clone().text().then(function(t){
var i, e, ref$, j, err;
i = ret.status || 404;
e = (ref$ = new Error(i + " " + t), ref$.name = 'lderror', ref$.id = i, ref$.message = t, ref$);
try {
if ((j = JSON.parse(t)) && j.name === 'lderror') {
import$(e, j).json = j;
}
} catch (e$) {
err = e$;
}
return Promise.reject(e);
});
});
};
proxin = function(o){
var ifr, ref$, attr, func, unwrapped, wrapped, wm, varSetter, this$ = this;
o == null && (o = {});
this.lc = o.context || {};
this.id = Math.random().toString(36).substring(2);
if (o.iframe) {
this.iframe = o.iframe;
} else {
this.iframe = ifr = doc.createElement('iframe');
ref$ = ifr.style;
ref$.position = 'absolute';
ref$.top = 0;
ref$.left = 0;
ref$.width = 0;
ref$.height = 0;
ref$.pointerEvents = 'none';
ref$.opacity = 0;
ifr.setAttribute('title', "rescope script loader");
ifr.setAttribute('name', "pdb-proxin-" + this.id);
doc.body.appendChild(ifr);
}
attr = Object.fromEntries(Reflect.ownKeys(this.iframe.contentWindow).map(function(it){
return [it, true];
}));
func = {};
unwrapped = {};
wrapped = {};
wm = new WeakMap();
this._proxy = new Proxy(o.target || win, {
get: function(t, k, o){
var f, e, ret;
if (this$.lc[k] != null) {
return this$.lc[k];
}
if (func[k] != null) {
return func[k];
}
if (unwrapped[k] != null) {
return unwrapped[k];
}
if (wrapped[k] != null) {
return wrapped[k];
}
if (k === 'addEventListener') {
return wrapped[k] = function(n, ocb){
var ncb;
if (n !== 'message') {
return (o.target || win).addEventListener(n, ocb);
}
(o.target || win).addEventListener(n, ncb = function(evt){
Object.defineProperty(evt, 'source', {
value: this$._proxy,
writable: false,
configurable: true
});
return ocb.apply(this$._proxy, arguments);
});
return wm.set(ocb, ncb);
};
}
if (k === 'removeEventListener') {
return wrapped[k] = function(n, ocb){
if (n !== 'message') {
return (o.target || win).removeEventListener(n, ocb);
}
return (o.target || win).removeEventListener(n, wm.get(ocb)) || ocb;
};
}
if (typeof t[k] === 'function') {
try {
f = Reflect.get(t, k, o);
} catch (e$) {
e = e$;
return f = t[k];
}
ret = func[k] = new Proxy(f.bind(t), {
get: function(d, g, o){
return Reflect.get(in$(g, d) ? d : f, g, o);
}
});
return ret;
}
if (attr[k] == null) {
return undefined;
}
return t[k];
},
set: function(t, k, v){
var f;
if (enableRspvarsetcb) {
if (k === '_rspvarsetcb_') {
varSetter.on(v.k, v.f);
return true;
}
varSetter.fire(k, v);
}
if (k === 'onmessage') {
f = function(v){
return function(evt){
Object.defineProperty(evt, 'source', {
value: this$._proxy,
writable: false,
configurable: true
});
if (v) {
return v.call(this$._proxy, evt);
}
};
};
unwrapped[k] = v;
queueMicrotask(function(){
return t[k] = f(v);
});
return true;
}
if (attr[k]) {
t[k] = v;
return true;
}
this$.lc[k] = v;
return true;
},
defineProperty: function(t, k, d){
Object.defineProperty(this$.lc, k, d);
return this$._proxy;
}
});
varSetter = {
evthdr: {},
on: function(n, cb){
var this$ = this;
return (Array.isArray(n)
? n
: [n]).map(function(n){
var ref$;
return ((ref$ = this$.evthdr)[n] || (ref$[n] = [])).push(cb);
});
},
fire: function(n){
var v, res$, i$, to$, ref$, len$, cb, results$ = [];
res$ = [];
for (i$ = 1, to$ = arguments.length; i$ < to$; ++i$) {
res$.push(arguments[i$]);
}
v = res$;
for (i$ = 0, len$ = (ref$ = this.evthdr[n] || []).length; i$ < len$; ++i$) {
cb = ref$[i$];
results$.push(cb.apply(this, v));
}
return results$;
}
};
return this;
};
proxin.prototype = (ref$ = Object.create(Object.prototype), ref$.proxy = function(){
return this._proxy;
}, ref$.ctx = function(){
return this.lc;
}, ref$);
rsp = function(o){
var ifr, ref$;
o == null && (o = {});
this.id = Math.random().toString(36).substring(2);
this.iframe = ifr = doc.createElement('iframe');
this._cache = {};
this.proxy = new proxin();
this.registry(o.registry || "/assets/lib/");
ref$ = ifr.style;
ref$.position = 'absolute';
ref$.top = 0;
ref$.left = 0;
ref$.width = 0;
ref$.height = 0;
ref$.pointerEvents = 'none';
ref$.opacity = 0;
ifr.setAttribute('title', "rescope script loader");
ifr.setAttribute('name', "pdb-rescope-" + this.id);
doc.body.appendChild(ifr);
ifr.contentWindow.document.body.innerHTML = (o.preloads || []).map(function(it){
return "<script type=\"text/javascript\" src=\"" + it + "\"></script>";
}).join('');
return this;
};
rsp.env = function(it){
var ref$;
return ref$ = [it, it.document], win = ref$[0], doc = ref$[1], ref$;
};
rsp.prop = {
legacy: {
webkitStorageInfo: true
}
};
rsp.id = function(o){
var path;
path = o.path || (o.type === 'js'
? 'index.min.js'
: o.type === 'css' ? 'index.min.css' : 'index.html');
return o.id || o.url || (o.ns ? o.ns + ":" : '') + "" + o.name + "@" + (o.version || 'main') + ":" + path;
};
rsp._cache = {};
rsp._ver = {
map: {},
list: {}
};
rsp.cache = function(o){
var that, k, nv, p, s, ret, n, v, ref$, i$, to$, i, ver;
if (typeof o === 'string') {
o = {
url: o
};
}
if (!o.id) {
o.id = rsp.id(o);
}
if (that = this._cache[o.id]) {
return that;
}
if (o.id && !o.name) {
k = o.id.split(':');
if (k.length <= 2) {
nv = k[0], p = k[1], s = k[2];
} else {
s = k[0], nv = k[1], p = k[2];
}
if (!(ret = /^(@?[^@]+)(?:@([^:]+))?$/.exec(nv))) {
ret = ['', o.id, ''];
}
n = ret[1];
v = ret[2];
} else {
ref$ = [o.ns, o.name, o.version || '', o.path || ''], s = ref$[0], n = ref$[1], v = ref$[2], p = ref$[3];
}
if (/^[0-9.]+$/.exec(v)) {
if (that = ((ref$ = this._ver.map)[n] || (ref$[n] = {}))[v]) {
v = that;
}
if (that = this._cache[rsp.id({
ns: s,
name: n,
version: v,
path: p
})]) {
return that;
}
for (i$ = 0, to$ = ((ref$ = this._ver.list)[n] || (ref$[n] = [])).length; i$ < to$; ++i$) {
i = i$;
ver = this._ver.list[n][i];
if (!semver.fit(ver, v)) {
continue;
}
this._ver.map[n][v] = ver;
o.id = rsp.id({
ns: s,
name: n,
version: ver,
path: p
});
if (that = this._cache[o.id]) {
return that;
}
}
}
if (!in$(v, (ref$ = this._ver.list)[n] || (ref$[n] = []))) {
this._ver.list[n].push(v);
}
return this._cache[o.id] = o;
};
rsp.prototype = (ref$ = Object.create(Object.prototype), ref$.peekScope = function(){
return false;
}, ref$.init = function(){
return Promise.resolve();
}, ref$._ref = function(o){
var r, ref$;
if (typeof o === 'string') {
o = {
url: o
};
}
if (typeof (r = this._reg.url || this._reg) === 'function') {
o = (ref$ = import$({}, o), ref$.url = r(o), ref$);
}
return this._reg.fetch
? this._reg.fetch(o)
: o.url;
}, ref$.registry = function(v){
if (typeof v === 'string') {
if (v[v.length - 1] === '/') {
v = v.substring(0, v.length - 1);
}
return this._reg = function(v){
return function(o){
return v + "/" + o.name + "/" + (o.version || 'main') + "/" + (o.path || 'index.min.js');
};
}(v);
} else {
return this._reg = v;
}
}, ref$.cache = function(o){
var that;
if (typeof o === 'string') {
o = {
url: o
};
}
if (!o.id) {
o.id = rsp.id(o);
}
if (that = this._cache[o.id]) {
return that;
}
return this._cache[o.id] = rsp.cache(o);
}, ref$.exports = function(o){
var ctx, libs, ref$, hash, iw, k, results$ = [];
o == null && (o = {});
ctx = o.ctx || {};
libs = typeof o.libs === 'string'
? [o.libs]
: o.libs || [];
ref$ = [{}, this.iframe.contentWindow], hash = ref$[0], iw = ref$[1];
for (k in ctx) {
hash[k] = iw[k];
iw[k] = ctx[k];
}
this._exports(libs, 0, ctx);
for (k in hash) {
results$.push(iw[k] = hash[k]);
}
return results$;
}, ref$._exports = function(libs, idx, ctx){
var lib, ref$, hash, fprop, iw, k, att1, e, att2, results$ = [];
idx == null && (idx = 0);
ctx == null && (ctx = {});
if (!(lib = libs[idx])) {
return;
}
lib = this.cache(lib);
ref$ = [{}, lib.fprop, this.iframe.contentWindow], hash = ref$[0], fprop = ref$[1], iw = ref$[2];
if (!fprop) {
lib.fprop = fprop = {};
lib.prop = {};
lib.propIniting = true;
if (lib.gen) {
import$(fprop, lib.gen.apply(iw, [iw, iw, iw]));
lib.prop = Object.fromEntries((function(){
var results$ = [];
for (k in fprop) {
results$.push([k, null]);
}
return results$;
}()));
} else {
att1 = Object.fromEntries(Reflect.ownKeys(iw).filter(function(it){
return !rsp.prop.legacy[it];
}).map(function(it){
return [it, true];
}));
for (k in att1) {
hash[k] = iw[k];
}
try {
iw.eval((lib.code || '').replace('"use strict";', ''));
} catch (e$) {
e = e$;
console.error("[@plotdb/rescope] Parse failed", {
url: lib.url,
ns: lib.ns,
name: lib.name,
version: lib.version,
path: lib.path
});
console.error("[@plotdb/rescope] with this error:", e);
throw e;
}
att2 = Object.fromEntries(Reflect.ownKeys(iw).filter(function(it){
return !rsp.prop.legacy[it];
}).map(function(it){
return [it, true];
}));
for (k in att2) {
if (iw[k] === hash[k] || k === 'NaN') {
continue;
}
fprop[k] = iw[k];
lib.prop[k] = null;
}
}
} else {
for (k in fprop) {
hash[k] = iw[k];
iw[k] = fprop[k];
}
}
for (k in fprop) {
ctx[k] = fprop[k];
}
this._exports(libs, idx + 1);
for (k in fprop) {
results$.push(iw[k] = hash[k]);
}
return results$;
}, ref$._wrap = function(o, ctx, opt){
var varre, prop, code, _, k;
o == null && (o = {});
ctx == null && (ctx = {});
opt == null && (opt = {});
varre = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
prop = o.prop || {};
code = "var window, global, globalThis, self, __ret = {}; __win = {};\nwindow = global = globalThis = self = window = scope;";
_ = !enableRspvarsetcb
? function(){}
: function(k){
return "window['_rspvarsetcb_'] = {k:'" + k + "',f:function(v){" + k + "=v}};";
};
for (k in prop) {
if (varre.exec(k)) {
code += "var " + k + ";" + _(k);
}
code += "__win['" + k + "'] = win['" + k + "']; win['" + k + "'] = undefined;";
}
for (k in ctx) {
code += "window['" + k + "'] = ctx['" + k + "'];";
if (varre.exec(k)) {
code += "var " + k + " = window['" + k + "'];" + _(k);
}
}
code += o.code + ";";
for (k in prop) {
if (varre.exec(k)) {
code += "if(!(" + k + ")) { " + k + " = scope['" + k + "']; }\n__ret['" + k + "'] = " + k + " || window['" + k + "'] || win['" + k + "'] || this['" + k + "'];\nwin['" + k + "'] = __win['" + k + "'];";
} else {
code += "__ret['" + k + "'] = window['" + k + "'] || win['" + k + "'] || this['" + k + "'];\nwin['" + k + "'] = __win['" + k + "'];";
}
}
code += "return __ret;";
if (opt.codeOnly) {
return "function(scope, ctx, win){" + code + "}";
}
return new Function("scope", "ctx", "win", code);
}, ref$.load = function(libs, dctx, forceFetch, onlyFetch){
var px, ctx, proxy, segs, _, this$ = this;
dctx == null && (dctx = {});
forceFetch == null && (forceFetch = false);
onlyFetch == null && (onlyFetch = false);
libs = (Array.isArray(libs)
? libs
: [libs]).map(function(o){
return this$.cache(o);
});
px = libs.px
? libs.px
: libs.px = dctx && dctx.p
? dctx.p
: new proxin();
ctx = px.ctx();
proxy = px.proxy();
/*
# this tries to segment libs based on async flag.
# however, current implementation batches fetches and then loads by order
# in this case segment seems to be unnecessary.
# we will keep the code here for reference.
[segs, seg] = [[], []]
for lib in libs =>
seg.push lib
if !(lib.async? and !lib.async) => continue
segs.push seg
seg = []
if seg.length => segs.push seg
*/
segs = [libs];
_ = function(idx){
var libs, ps;
idx == null && (idx = 0);
if (!(libs = segs[idx])) {
return Promise.resolve(ctx);
}
ps = libs.map(function(lib){
var ref;
if ((lib.code || lib.gen) && !forceFetch) {
return Promise.resolve();
}
ref = this$._ref(lib);
if (ref.then) {
return ref.then(function(it){
lib.code = it.content;
return this$.cache((lib.id = undefined, lib.version = it.version, lib.code = it.content, lib));
});
} else {
return _fetch(ref, {
method: 'GET'
}).then(function(it){
return lib.code = it;
});
}
});
return Promise.all(ps).then(function(){
if (onlyFetch) {
return;
}
this$.exports({
libs: libs,
ctx: dctx.f
});
return libs.map(function(lib){
if (lib.propIniting) {
if (!lib.gen) {
lib.gen = this$._wrap(lib, ctx);
}
lib.prop = lib.gen.apply(proxy, [proxy, ctx, win]);
lib.propIniting = false;
}
return import$(ctx, lib.prop);
});
}).then(function(){
return ctx;
}).then(function(){
return _(idx + 1);
});
};
return _(0);
}, ref$.context = function(libs, func, px){
var ref$;
if (typeof func !== 'function') {
ref$ = [px, func], func = ref$[0], px = ref$[1];
}
return this.load(libs, px).then(function(ctx){
if (func) {
return func(ctx);
} else {
return ctx;
}
});
}, ref$);
rsp.env(typeof self != 'undefined' && self !== null ? self : globalThis);
rsp.proxin = proxin;
rsp.dualContext = function(){
return {
p: new proxin(),
f: {},
ctx: function(){
return this.p.ctx();
}
};
};
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}
function in$(x, xs){
var i = -1, l = xs.length >>> 0;
while (++i < l) if (x === xs[i]) return true;
return false;
}rsp.prototype.bundle = function(libs){
var hash, res$, k, v, this$ = this;
libs == null && (libs = {});
libs = (Array.isArray(libs)
? libs
: [libs]).map(function(o){
return this$.cache(o);
});
hash = {};
libs.filter(function(it){
return it && it.id;
}).map(function(it){
return hash[it.id] = it;
});
res$ = [];
for (k in hash) {
v = hash[k];
res$.push(v);
}
libs = res$;
return this.load(libs, null, true, true).then(function(){
var codes;
codes = libs.filter(function(it){
return it.code;
}).map(function(o){
/*
code = @_wrap o, {}, code-only: true
"""{#{if o.url => "url: '#{o.url}'," else ''}id: '#{o.id}',gen: #code}"""
*/
return JSON.stringify({
url: o.url,
id: o.id,
ns: o.ns,
name: o.name,
version: o.version,
path: o.path,
code: o.code
});
});
return Promise.resolve("[" + codes.join(',') + "].forEach(function(o){rescope.cache(o);})");
});
};if (typeof module != 'undefined' && module !== null) {
module.exports = rsp;
} else if (typeof window != 'undefined' && window !== null) {
window.rescope = rsp;
}}())