-
Notifications
You must be signed in to change notification settings - Fork 1
/
Package.js
714 lines (527 loc) · 15.7 KB
/
Package.js
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
var EventEmitter = require('events');
var inherits = require('util').inherits;
var mime = require('./mime.js');
var PackageRequest = require('./PackageRequest.js');
/**
* Represents a downloaded package of HTML, CSS, JavaScript and any other content type
*
* @param {PackageRequest} pkgRequest The representation of the package request
* @constructor
*/
function Package(pkgRequest) {
// this is where a package request becomes a real package
if (!(pkgRequest instanceof PackageRequest)) {
throw new TypeError('Package constructor expects a PackageRequest');
}
EventEmitter.call(this);
this.name = pkgRequest.packageName;
this.language = pkgRequest.language;
this.screen = pkgRequest.screen;
this.density = pkgRequest.density;
// the original strings representing the content
// they are automatically cleaned up when possible to preserve memory
this.content = {};
// the <style> and <div> elements
this.containers = {};
// where to inject style and div tags
this.parentElements = {
'text/html': document.body,
'text/css': document.head
};
}
inherits(Package, EventEmitter);
module.exports = Package;
// ----------------------------
// Static API for instantiation
// ----------------------------
/**
* Parses a string into its various pieces of content and stores those in the package's "content"
* object.
*
* @param {string} data The data to parse and store inside the package
* @returns {Object} The meta data that was parsed (containing hash and delimiter)
*/
Package.prototype.parse = function (data) {
// parse out meta data, which should at least contain a part-delimiter string
var metaData = {};
var i, len;
var index = data.indexOf('\n\n');
if (index !== -1) {
var lines = data.substring(0, index).split('\n');
data = data.substring(index + 2);
for (i = 0, len = lines.length; i < len; i++) {
var line = lines[i].split(':');
var key = line.shift().trim().toLowerCase();
var value = line.join(':').trim();
metaData[key] = value;
}
}
if (typeof metaData.delimiter !== 'string' || !metaData.delimiter.length) {
throw new Error('No valid part-delimiter found in package data');
}
var parts = data.split(metaData.delimiter);
for (i = 0, len = parts.length; i < len; i++) {
var part = parts[i];
var offset = -1;
var eol;
do {
offset += 1;
eol = part.indexOf('\n', offset);
} while (offset === eol && eol !== -1);
if (eol === -1) {
throw new Error('Could not find content-type in package part.');
}
var contentType = mime.parse(part.substring(offset, eol));
var content = part.substring(eol + 1);
// store the content
if (contentType) {
this.addContent(contentType.type, content);
}
}
return metaData;
};
/**
* Creates a package from string data and stores it in the cache library if provided.
*
* @param {PackageRequest} pkgRequest The representation of the package request
* @param {string} data The data to parse
* @param {Object|null} cache A storage cache
* @param {Function} cb Callback, called on completion
*/
Package.fromData = function (pkgRequest, data, cache, cb) {
if (!data) {
return cb(new Error('Package data is empty'));
}
var metaData;
var pkg = new Package(pkgRequest);
try {
metaData = pkg.parse(data);
} catch (error) {
return cb(error);
}
if (cache) {
// asynchronous to the callback, there is no need to wait for I/O here
cache.set(pkgRequest, metaData, data, function (cacheError) {
if (cacheError) {
console.warn(cacheError);
}
});
}
return cb(null, pkg);
};
/**
* Creates a package by reading it from the provided cache library
*
* @param {PackageRequest} pkgRequest The representation of the package request
* @param {Object|null} cache A storage cache
* @param {Function} cb Callback, called on completion
*/
Package.fromCache = function (pkgRequest, cache, cb) {
cache.getData(pkgRequest, function (error, data) {
if (error) {
return cb(error);
}
return Package.fromData(pkgRequest, data, null, cb);
});
};
/**
* Creates a package from a downloaded response, which may either indicate a requirement to read
* from cache, or a requirement to parse and store to cache.
*
* @param {PackageRequest} pkgRequest The representation of the package request
* @param {string} data The data to parse or "usecache" if our cached version is up-to-date
* @param {Object|null} cache A storage cache
* @param {Function} cb Callback, called on completion
*/
Package.fromDownload = function (pkgRequest, data, cache, cb) {
if (typeof data !== 'string') {
return cb(new TypeError('Package data must be a string'));
}
if (data.substr(0, 8) === 'usecache') {
Package.fromCache(pkgRequest, cache, cb);
} else {
Package.fromData(pkgRequest, data, cache, cb);
}
};
// -------------------------
// Content-type agnostic API
// -------------------------
/**
* Adds content of a specific type to the content collection.
*
* @param {string} type The content-type
* @param {string} content
*/
Package.prototype.addContent = function (type, content) {
if (!type || typeof type !== 'string') {
throw new TypeError('You must provide a valid content-type (string)');
}
if (!content) {
return;
}
if (typeof content !== 'string') {
throw new TypeError('Content added to a package must be a string');
}
var cnt = this.containers[type];
if (cnt) {
if (type === 'text/html') {
// the container must be empty, we do not want to overwrite instantiated HTML elements
if (cnt.firstChild) {
throw new Error('Cannot add content of type "' + type + '" when already created and populated.');
}
cnt.innerHTML = content;
} else if (type === 'text/css') {
cnt.textContent += '\n' + content;
} else {
throw new TypeError('Element of type ' + type + ' has already been instantiated and cannot be augmented.');
}
} else {
if (this.content[type]) {
this.content[type].push(content);
} else {
this.content[type] = [content];
}
}
};
/**
* Extracts downloaded content of a given type, removes it from memory and returns it. Packages may
* contain multiple pieces of content of one type. Every time this function is called, it returns
* one piece. This function may thus be repeated until undefined is returned.
*
* @param {string} type The content-type
* @returns {string}
*/
Package.prototype.claimContent = function (type) {
if (!type || typeof type !== 'string') {
throw new TypeError('You must provide a valid content-type (string)');
}
var list = this.content[type];
if (!list || list.length === 0) {
return;
}
return list.shift();
};
/**
* Extracts all downloaded content of a given type, removing them from memory and returning them,
* joined together with the given glue.
*
* @param {string} type The content-type
* @param {string} [glue] The string to join the content with, "\n" by default
* @returns {string}
*/
Package.prototype.claimAllContent = function (type, glue) {
if (!type || typeof type !== 'string') {
throw new TypeError('You must provide a valid content-type (string)');
}
if (glue === undefined) {
glue = '\n';
}
var list = this.content[type];
if (!list || list.length === 0) {
return;
}
var content = list.join(glue);
delete this.content[type];
return content;
};
/**
* Instantly drops all content of a given type
*
* @param {string} type
*/
Package.prototype.rejectAllContent = function (type) {
delete this.content[type];
};
/**
* Injects content into the given parent element
*
* @param {Element} elm The element to inject
* @param {Element} parent The parent element to append to
* @returns {Element} The injected element
*/
Package.prototype.injectElement = function (elm, parent) {
if (!elm) {
throw new Error('No element to inject');
}
if (!parent) {
throw new Error('No parent element to append to');
}
if (parent !== elm.parentNode) {
parent.appendChild(elm);
}
return elm;
};
/**
* Ejects content from its parent element
*
* @param {string} type The content-type
* @returns {Element} The ejected element
*/
Package.prototype.ejectElement = function (type) {
var elm = this.containers[type];
if (!elm) {
throw new Error('There is no container for type "' + type + '" to eject');
}
if (elm.parentNode) {
elm.parentNode.removeChild(elm);
}
return elm;
};
/**
* Ejects the container of the given content type, and forgets about its existence.
*
* @param {string} type The content-type
* @returns {Element} The ejected (and forgotten) element
*/
Package.prototype.destroyElement = function (type) {
var elm = this.ejectElement(type);
if (!elm) {
return;
}
delete this.containers[type];
return elm;
};
/**
* Destroys any remaining content strings and elements.
*/
Package.prototype.destroy = function () {
var types = Object.keys(this.containers);
for (var i = 0; i < types.length; i += 1) {
var type = types[i];
this.destroyElement(type);
}
this.content = {};
};
// -----------------------
// JavaScript specific API
// -----------------------
/**
* Execute the JavaScript in this package
*/
Package.prototype.runJs = function () {
var content = this.claimAllContent('text/javascript');
if (content) {
/*jshint evil:true */
new Function(content)();
}
};
// -----------------
// HTML specific API
// -----------------
/**
* Static function to instantiate a package's HTML.
*
* @param {string} content The HTML content
* @param {string} name The package name
* @returns {HTMLDivElement} A <div> element that now contains the given content
*/
Package.createHtml = function (content, name) {
var cnt = document.createElement('div');
cnt.className = 'mage-package';
cnt.className += ' mage-page '; // deprecated
if (name) {
cnt.setAttribute('data-package', name);
cnt.setAttribute('data-page', name); // deprecated
}
// start hidden
cnt.style.display = 'none';
// inject HTML from the package
if (content) {
cnt.innerHTML = content;
}
return cnt;
};
/**
* Add HTML content to this package
*
* @param {string} content
*/
Package.prototype.addHtml = function (content) {
this.addContent('text/html', content);
};
/**
* Creates (if needed) and returns the HTML container for this package.
*
* @returns {HTMLDivElement} The <div> container for this package.
*/
Package.prototype.getHtml = function () {
var type = 'text/html';
var cnt = this.containers[type];
if (!cnt) {
cnt = Package.createHtml(this.claimAllContent(type), this.name);
this.containers[type] = cnt;
}
return cnt;
};
/**
* Creates (if needed) and injects the HTML container for this package into a given parent.
*
* @param {Element} The parent to append to.
* @returns {HTMLDivElement} The <div> container for this package.
*/
Package.prototype.injectHtml = function (parent) {
parent = parent || this.parentElements['text/html'];
return this.injectElement(this.getHtml(), parent);
};
/**
* Ejects HTML from its parent element
*
* @returns {HTMLDivElement} The ejected element
*/
Package.prototype.ejectHtml = function () {
return this.ejectElement('text/html');
};
/**
* Creates (if needed) and injects (if needed) the HTML container for this package into its logical
* parent, then removes the "display: none" style from the HTML container.
*
* @returns {HTMLDivElement} The visible element
*/
Package.prototype.showHtml = function () {
var cnt = this.injectHtml();
cnt.style.display = '';
this.emit('show', cnt);
return cnt;
};
/**
* If there is an HTML container, it sets the "display: none" style to hide it.
*
* @returns {HTMLDivElement} The hidden element
*/
Package.prototype.hideHtml = function () {
var cnt = this.containers['text/html'];
if (!cnt) {
return;
}
cnt.style.display = 'none';
this.emit('hide', cnt);
return cnt;
};
// ----------------
// CSS specific API
// ----------------
/**
* Static function to instantiate a package's CSS.
*
* @param {string} content The HTML content
* @param {string} name The package name
* @returns {Element} A <style> element that contains the given content
*/
Package.createCss = function (content, name) {
var cnt = document.createElement('style');
cnt.setAttribute('type', 'text/css');
if (name) {
cnt.setAttribute('data-package', name);
cnt.setAttribute('data-page', name); // deprecated
}
// inject CSS from the package
if (content) {
cnt.textContent = content;
}
return cnt;
};
/**
* Add CSS content to this package
*
* @param {string} content
*/
Package.prototype.addCss = function (content) {
this.addContent('text/css', content);
};
/**
* Creates (if needed) and returns the <style> element for this package.
*
* @returns {HTMLStyleElement} The <style> element for this package.
*/
Package.prototype.getCss = function () {
var type = 'text/css';
var cnt = this.containers[type];
if (!cnt) {
cnt = Package.createCss(this.claimAllContent('text/css'), this.name);
this.containers[type] = cnt;
}
return cnt;
};
/**
* Creates (if needed) and injects the <style> element for this package into a given parent.
*
* @param {Element} The parent to append to.
* @returns {HTMLStyleElement} The <style> element for this package.
*/
Package.prototype.injectCss = function (parent) {
parent = parent || this.parentElements['text/css'];
return this.injectElement(this.getCss(), parent);
};
/**
* Ejects the <style> element from its parent element
*
* @returns {HTMLStyleElement} The ejected element
*/
Package.prototype.ejectCss = function () {
return this.ejectElement('text/css');
};
/**
* Replaces the HTML of the existing package, but only if it hasn't been turned into DOM yet.
*
* @param {string} str The HTML content
*/
Package.prototype.assimilateHtml = function (str) {
// we only replace HTML if it hasn't been turned into DOM yet
if (!this.containers['text/html']) {
this.content['text/html'] = [str];
}
};
/**
* Replace the CSS of the existing package, even if already turned into DOM
*
* @param {string} str The CSS content
*/
Package.prototype.assimilateCss = function (str) {
var prevContainer = this.containers['text/css'];
if (prevContainer) {
delete this.containers['text/css'];
}
this.content['text/css'] = [str];
if (prevContainer && prevContainer.parentNode) {
this.injectCss();
prevContainer.parentNode.removeChild(prevContainer);
}
};
/**
* Unknown content types will be collected for later retrieval
*
* @param {string} contentType The content type (without augmented parameters such as charset etc)
* @param {Package} pkg The package containing all the content of the given type
*/
Package.prototype.assimilateOther = function (contentType, pkg) {
var strings = pkg.content[contentType];
if (strings && strings.length > 0) {
this.content[contentType] = this.content[contentType] || [];
for (var i = 0; i < strings.length; i += 1) {
this.content[contentType].push(strings[i]);
}
pkg.rejectAllContent(contentType);
}
};
/**
* Allows a newer version of a package to be assimilated into the current package.
*
* If pkg has a different clientConfig, it may provide us with:
* - HTML (action: replace only if not already turned into DOM)
* - CSS (action: replace)
* - JS: (action: ignore)
* - other content types (action: augment)
*
* @param {Package} pkg
*/
Package.prototype.assimilateContent = function (pkg) {
// known types first
pkg.rejectAllContent('text/javascript');
this.assimilateHtml(pkg.claimAllContent('text/html'));
this.assimilateCss(pkg.claimAllContent('text/css'));
// remaining (unblessed types)
var contentTypes = Object.keys(pkg.content);
for (var i = 0; i < contentTypes.length; i += 1) {
this.assimilateOther(contentTypes[i], pkg);
}
};