This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
yui.server.js
687 lines (620 loc) · 26 KB
/
yui.server.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
/*
* Copyright (c) 2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true, nomen:true, stupid:true*/
/*global YUI*/
/**
* @module ResourceStoreAddon
*/
/**
* @class RSAddonYUI
* @extension ResourceStore.server
*/
YUI.add('addon-rs-yui', function(Y, NAME) {
var libfs = require('fs'),
libpath = require('path'),
libvm = require('vm'),
WARN_SERVER_MODULES = /\b(dom-[\w\-]+|node-[\w\-]+|io-upload-iframe)/ig,
MODULE_SUBDIRS = {
autoload: true,
tests: true,
yui_modules: true
},
intlPath;
function RSAddonYUI() {
RSAddonYUI.superclass.constructor.apply(this, arguments);
}
RSAddonYUI.NS = 'yui';
Y.extend(RSAddonYUI, Y.Plugin.Base, {
/**
* This methods is part of Y.Plugin.Base. See documentation for that for details.
* @method initializer
* @param {object} config Configuration object as per Y.Plugin.Base
* @return {nothing}
*/
initializer: function(config) {
this.appRoot = config.appRoot;
this.mojitoRoot = config.mojitoRoot;
this.afterHostMethod('findResourceVersionByConvention', this.findResourceVersionByConvention, this);
this.beforeHostMethod('parseResourceVersion', this.parseResourceVersion, this);
this.beforeHostMethod('addResourceVersion', this.addResourceVersion, this);
this.onHostEvent('getMojitTypeDetails', this.onGetMojitTypeDetails, this);
this.onHostEvent('mojitResourcesResolved', this.onMojitResourcesResolved, this);
this.yuiConfig = config.host.getStaticAppConfig().yui;
this.modules = {}; // env: poslKey: module: details
this.sortedModules = {}; // env: poslKey: lang: module: details
this.usePrecomputed = -1 !== this.yuiConfig.dependencyCalculations.indexOf('precomputed');
this.useOnDemand = -1 !== this.yuiConfig.dependencyCalculations.indexOf('ondemand');
if (!this.usePrecomputed) {
this.useOnDemand = true;
}
},
/**
* Returns a datastructure which tells a YUI instance where to find
* the YUI modules that are shared among all mojits.
* @method getConfigShared
* @param {string} env runtime environment (either `client`, or `server`)
* @param {object} ctx runtime context
* @param {boolean} justApp Indicates whether to include the YUI
* modules just found in the application (true), or also include
* those found in mojito (false).
* @return {object} datastructure for configuring YUI
*/
getConfigShared: function(env, ctx, justApp) {
var r,
res,
ress,
modules = {};
ress = this.get('host').getResources(env, ctx, { mojit: 'shared' });
for (r = 0; r < ress.length; r += 1) {
res = ress[r];
if (!res.yui || !res.yui.name) {
continue;
}
if (justApp && ('mojito' === res.source.pkg.name)) {
continue;
}
modules[res.yui.name] = this._makeYUIModuleConfig(env, res);
}
return { modules: modules };
},
/**
* Returns a datastructure which tells a YUI instance where to find
* the YUI modules that are in all the mojits.
* @method getConfigAllMojits
* @param {string} env runtime environment (either `client`, or `server`)
* @param {object} ctx runtime context
* @return {object} datastructure for configuring YUI
*/
getConfigAllMojits: function(env, ctx) {
var store = this.get('host'),
m,
mojit,
mojits,
r,
res,
ress,
modules = {};
mojits = store.listAllMojits();
for (m = 0; m < mojits.length; m += 1) {
mojit = mojits[m];
ress = store.getResources(env, ctx, { mojit: mojit });
for (r = 0; r < ress.length; r += 1) {
res = ress[r];
if (!res.yui || !res.yui.name) {
continue;
}
if (res.mojit !== mojit) {
// generally only happens if res.mojit is 'shared'
continue;
}
modules[res.yui.name] = this._makeYUIModuleConfig(env, res);
}
}
return { modules: modules };
},
/**
* Using AOP, this is called after the ResourceStore's version.
* @method findResourceVersionByConvention
* @param {object} source metadata about where the resource is located
* @param {string} mojitType name of mojit to which the resource likely belongs
* @return {object||null} for yui modules or lang bundles, returns metadata signifying that
*/
findResourceVersionByConvention: function(source, mojitType) {
var fs = source.fs;
if (!fs.isFile) {
return;
}
if ('.js' !== fs.ext) {
return;
}
if (fs.subDirArray.length >= 1 && MODULE_SUBDIRS[fs.subDirArray[0]]) {
return new Y.Do.AlterReturn(null, {
type: 'yui-module',
skipSubdirParts: 1
});
}
if (fs.subDirArray.length >= 1 && 'lang' === fs.subDirArray[0]) {
return new Y.Do.AlterReturn(null, {
type: 'yui-lang',
skipSubdirParts: 1
});
}
},
/**
* Using AOP, this is called before the ResourceStore's version.
* @method parseResourceVersion
* @param {object} source metadata about where the resource is located
* @param {string} type type of the resource
* @param {string} subtype subtype of the resource
* @param {string} mojitType name of mojit to which the resource likely belongs
* @return {object||null} for yui modules or lang bundles, returns the resource metadata
*/
parseResourceVersion: function(source, type, subtype, mojitType) {
var fs = source.fs,
baseParts,
res,
sandbox;
if ('yui-lang' === type) {
res = {
source: source,
mojit: mojitType,
type: 'yui-lang',
affinity: 'common',
selector: '*'
};
if (!res.yui) {
res.yui = {};
}
sandbox = {
Intl: {
add: function(langFor, lang) {
res.yui.langFor = langFor;
res.yui.lang = lang;
}
}
};
this._captureYUIModuleDetails(res, sandbox);
res.name = res.yui.name;
res.id = [res.type, res.subtype, res.name].join('-');
return new Y.Do.Halt(null, res);
}
if ('yui-module' === type) {
baseParts = fs.basename.split('.');
res = {
source: source,
mojit: mojitType,
type: 'yui-module',
affinity: 'server',
selector: '*'
};
if (baseParts.length >= 3) {
res.selector = baseParts.pop();
}
if (baseParts.length >= 2) {
res.affinity = baseParts.pop();
}
if (baseParts.length !== 1) {
Y.log('invalid yui-module filename. skipping ' + fs.fullPath, 'warn', NAME);
return;
}
this._captureYUIModuleDetails(res);
res.name = res.yui.name;
res.id = [res.type, res.subtype, res.name].join('-');
return new Y.Do.Halt(null, res);
}
},
/**
* Using AOP, this is called before the ResourceStore's version.
* If the resource is a YUI module, augments the metadata with details
* about the YUI module.
* @method addResourceVersion
* @param {object} res resource version metadata
* @return {nothing}
*/
addResourceVersion: function(res) {
if ('.js' !== res.source.fs.ext) {
return;
}
if (res.yui && res.yui.name) {
// work done already
return;
}
// ASSUMPTION: no app-level resources are YUI modules
if (!res.mojit) {
return;
}
if ('asset' === res.type) {
return;
}
this._captureYUIModuleDetails(res);
},
/**
* This is called when the ResourceStore fires this event.
* It augments the mojit type details with the precomputed YUI module
* dependencies.
* @method onGetMojitTypeDetails
* @param {object} evt The fired event.
* @return {nothing}
*/
onGetMojitTypeDetails: function(evt) {
var store = this.get('host'),
dest = evt.mojit,
env = evt.args.env,
ctx = evt.args.ctx,
posl = evt.args.posl,
poslKey = JSON.stringify(posl),
mojitType = evt.args.mojitType,
ress,
r,
res,
sorted;
//console.log('--------------------------------- onGetMojitTypeDetails -- ' + [env, ctx.lang, poslKey, mojitType].join(','));
if (!dest.yui) {
dest.yui = { config: {} };
}
if (!dest.yui.config) {
dest.yui.config = { module: {} };
}
if (this.modules[env] && this.modules[env][poslKey]) {
dest.yui.config.modules = this.modules[env][poslKey][mojitType];
}
ress = store.getResources(env, ctx, {mojit: mojitType});
for (r = 0; r < ress.length; r += 1) {
res = ress[r];
if (res.type === 'binder') {
if (!dest.views[res.name]) {
dest.views[res.name] = {};
}
dest.views[res.name]['binder-module'] = res.yui.name;
sorted = this._getYUIDependencies('client', poslKey, ctx.lang, res.yui.name);
if (sorted && sorted.paths) {
dest.views[res.name]['binder-yui-sorted'] = Y.mojito.util.copy(sorted.paths);
}
}
if (res.type === 'controller') {
dest['controller-module'] = res.yui.name;
sorted = this._getYUIDependencies(env, poslKey, ctx.lang, res.yui.name);
if (sorted && sorted.sorted) {
dest.yui.sorted = sorted.sorted.slice();
}
if (this.usePrecomputed && sorted && sorted.paths) {
dest.yui.sortedPaths = Y.mojito.util.copy(sorted.paths);
}
}
}
},
/**
* This is called when the ResourceStore fires this event.
* It precomputes the YUI module dependencies, to be used later during
* onGetMojitTypeDetails.
* @method onMojitResourcesResolved
* @param {object} evt The fired event
* @return {nothing}
*/
onMojitResourcesResolved: function(evt) {
var env = evt.env,
posl = evt.posl,
poslKey = JSON.stringify(posl),
mojit = evt.mojit,
ress = evt.ress,
r,
res,
langRess = {}, // YUI module name: language: resource
l,
langName,
langNames = {},
langSorted,
viewEngineRequired = {},
modules = {},
binders = {},
controller,
required,
sorted,
binderName,
binder;
//console.log('--------------------------------- onMojitResourcesResolved -- ' + [env, poslKey, mojit].join(','));
if ('shared' === mojit) {
return;
}
if (!intlPath) {
modules = {};
required = { intl: true };
sorted = this._precomputeYUIDependencies('', 'client', 'notreallyamojit', modules, required);
intlPath = sorted.paths.intl;
// cleanup
modules = {};
}
for (r = 0; r < ress.length; r += 1) {
res = ress[r];
if ('addon' === res.type && 'view-engines' === res.subtype) {
viewEngineRequired[res.yui.name] = true;
}
if ('yui-lang' === res.type) {
langNames[res.yui.lang] = true;
if (!langRess[res.yui.langFor]) {
langRess[res.yui.langFor] = {};
}
langRess[res.yui.langFor][res.yui.lang] = res;
}
if (res.yui && res.yui.name) {
modules[res.yui.name] = this._makeYUIModuleConfig(env, res);
if ('binder' === res.type) {
binders[res.name] = res;
}
if ('controller' === res.type) {
controller = res;
}
}
}
if (controller && modules['inlinecss/' + mojit]) {
controller.yui.meta.requires.push('inlinecss/' + mojit);
}
if (!this.modules[env]) {
this.modules[env] = {};
}
if (!this.modules[env][poslKey]) {
this.modules[env][poslKey] = {};
}
this.modules[env][poslKey][mojit] = Y.mojito.util.copy(modules);
// we always want to do calculations for no-lang
langNames[''] = true;
langNames = Object.keys(langNames);
if (controller) {
required = { 'mojito-dispatcher': true };
required[controller.yui.name] = true;
// we don't know which views will be used, so we need all view engines
required = Y.merge(required, viewEngineRequired);
sorted = this._precomputeYUIDependencies(langName, env, mojit, modules, required);
for (l = 0; l < langNames.length; l += 1) {
langName = langNames[l];
langSorted = Y.mojito.util.copy(sorted);
this._addLangsToSorted(env, langSorted, langName, langRess);
this._setYUIDependencies(env, poslKey, langName, controller.yui.name, langSorted);
}
}
if ('client' === env) {
for (binderName in binders) {
if (binders.hasOwnProperty(binderName)) {
binder = binders[binderName];
required = { 'mojito-client': true };
required[binder.yui.name] = true;
// view engines are needed to support mojitProxy.render()
required = Y.merge(required, viewEngineRequired);
sorted = this._precomputeYUIDependencies(langName, env, mojit, modules, required);
}
}
}
},
/**
* Precomputes a set of dependencies.
* @private
* @method _precomputeYUIDependencies
* @param {string} lang YUI language code
* @param {string} env runtime environment (either `client`, or `server`)
* @param {string} mojit name of the mojit
* @param {object} modules YUI module metadata
* @param {object} required lookup hash of YUI module names that are required
* @param {boolean} forceYLoader whether to force the use of Y.Loader
* @return {object} precomputed (and sorted) module dependencies
*/
_precomputeYUIDependencies: function(lang, env, mojit, modules, required, forceYLoader) {
var loader,
m,
module,
originalYUAnodejs,
info,
warn,
sortedPaths = {};
// We don't actually need the full list, just the required modules.
// YUI.Loader() will do the rest at runtime.
if ((!this.usePrecomputed) && (!forceYLoader)) {
for (module in required) {
if (required.hasOwnProperty(module)) {
sortedPaths[module] = modules[module].fullpath;
}
}
return {
sorted: Object.keys(sortedPaths),
paths: sortedPaths
};
}
// HACK
// We need to clear YUI's cached dependencies, since there's no
// guarantee that the previously calculated dependencies have been done
// using the same context as this calculation.
delete YUI.Env._renderedMods;
// Trick the loader into thinking it's -not- running on nodejs.
// This is the official way to do it.
originalYUAnodejs = Y.UA.nodejs;
Y.UA.nodejs = ('server' === env);
// Use ignoreRegistered here instead of the old `delete YUI.Env._renderedMods` hack
loader = new Y.Loader({ ignoreRegistered: true });
// Only override the default if it's required
if (this.yuiConfig && this.yuiConfig.base) {
loader.base = this.yuiConfig.base;
}
loader.addGroup({modules: modules}, mojit);
loader.calculate({required: required});
Y.UA.nodejs = originalYUAnodejs;
for (m = 0; m < loader.sorted.length; m += 1) {
module = loader.sorted[m];
info = loader.moduleInfo[module];
if (info) {
// modules with "nodejs" in their name are tweaks on other modules
if ('client' === env && module.indexOf('nodejs') !== -1) {
continue;
}
sortedPaths[module] = info.fullpath || loader._url(info.path);
}
}
// log warning if server mojit has dom dependency
if ('server' === env) {
warn = Y.Object.keys(sortedPaths).join(' ').match(WARN_SERVER_MODULES);
if (warn) {
Y.log('your mojit "' + mojit + '" has a server affinity and these client-related deps: ' + warn.join(', '), 'WARN', NAME);
Y.log('Mojito may be unable to start, unless you have provided server-side DOM/host-object suppport', 'WARN', NAME);
}
}
return {
sorted: loader.sorted,
paths: sortedPaths
};
},
/**
* Saves the precomputed YUI module dependencies for later.
* @private
* @method _setYUIDependencies
* @param {string} env runtime environment (either `client`, or `server`)
* @param {string} poslKey key (representing the POSL) under which to save the moduldes
* @param {string} lang YUI language code
* @param {string} module YUI module name for which the precomputed dependencies are for
* @param {object} sorted the precomputed dependencies
* @return {nothing}
*/
_setYUIDependencies: function(env, poslKey, lang, module, sorted) {
if (!this.sortedModules[env]) {
this.sortedModules[env] = {};
}
if (!this.sortedModules[env][poslKey]) {
this.sortedModules[env][poslKey] = {};
}
if (!this.sortedModules[env][poslKey][lang]) {
this.sortedModules[env][poslKey][lang] = {};
}
this.sortedModules[env][poslKey][lang][module] = sorted;
},
/**
* Returns precomputed dependencies saved by _setYuiSorted.
* @private
* @method _getYUIDependencies
* @param {string} env runtime environment (either `client`, or `server`)
* @param {string} poslKey key (representing the POSL) under which to save the moduldes
* @param {string} lang YUI language code
* @param {string} module YUI module name for which the precomputed dependencies are for
* @return {object} the precomputed dependencies
*/
_getYUIDependencies: function(env, poslKey, lang, module) {
lang = lang || '';
var parts = lang.split('-'),
p,
test;
if (!this.sortedModules[env]) {
return;
}
if (!this.sortedModules[env][poslKey]) {
return;
}
// example: first try "zh-Hans-CN", then "zh-Hans", then "zh"
for (p = parts.length; p > 0; p -= 1) {
test = parts.slice(0, p).join('-');
if (this.sortedModules[env][poslKey][test] &&
this.sortedModules[env][poslKey][test][module]) {
return this.sortedModules[env][poslKey][test][module];
}
}
// fall back to "default language"
return this.sortedModules[env][poslKey][''][module];
},
/**
* Generates the YUI configuration for the resource.
* @private
* @method _makeYUIModuleConfig
* @param {string} env runtime environment (either `client`, or `server`)
* @param {object} res the resource metadata
* @return {object} the YUI configuration for the module
*/
_makeYUIModuleConfig: function(env, res) {
var config = {
fullpath: ('client' === env) ? res.url : res.source.fs.fullPath,
requires: (res.yui.meta && res.yui.meta.requires) || []
};
return config;
},
/**
* If the resource is a YUI module, augments its metadata with metadata
* about the YUI module.
* @private
* @method _captureYUIModuleDetails
* @param {object} res resource metadata
* @param {object} runSandbox if passed, the function in the module
* will be called using this parameter as the YUI sandbox
* @return {nothing}
*/
_captureYUIModuleDetails: function(res, runSandbox) {
var file,
ctx,
yui = {};
file = libfs.readFileSync(res.source.fs.fullPath, 'utf8');
ctx = {
console: {
log: function() {}
},
window: {},
document: {},
YUI: {
ENV: {},
config: {},
use: function() {},
add: function(name, fn, version, meta) {
yui.name = name;
yui.version = version;
yui.meta = meta || {};
if (!yui.meta.requires) {
yui.meta.requires = [];
}
if (runSandbox) {
try {
fn(runSandbox, yui.name);
} catch (e) {
Y.log('failed to run javascript file ' + res.source.fs.fullPath + '\n' + e.message, 'error', NAME);
}
}
}
}
};
try {
libvm.runInNewContext(file, ctx, res.source.fs.fullPath);
} catch (e) {
yui = null;
Y.log('failed to parse javascript file ' + res.source.fs.fullPath, 'error', NAME);
}
if (yui) {
res.yui = Y.merge(res.yui || {}, yui);
}
},
/**
* Augments the results of _precomputeYUIDependencies() with the details
* about the language.
*
* @private
* @method _addLangsToSorted
* @param {string} env runtime environment (either `client`, or `server`)
* @param {object} sorted results of _precomputeYUIDependencies()
* @param {string} langName which language to add
* @param {object} langRess resources representing the language bundle
* @return {nothing} results are added to the `sorted` parameter
*/
_addLangsToSorted: function(env, sorted, langName, langRess) {
var modName,
langRes;
for (modName in langRess) {
if (langRess.hasOwnProperty(modName)) {
langRes = langRess[modName][langName] || langRess[modName][''];
if (langRes) {
sorted.sorted.push(langRes.yui.name);
sorted.paths[langRes.yui.name] = ('client' === env) ? langRes.url : langRes.source.fs.fullPath;
}
}
}
if (!sorted.paths.intl) {
sorted.sorted.unshift('intl');
sorted.paths.intl = intlPath;
}
}
});
Y.namespace('mojito.addons.rs');
Y.mojito.addons.rs.yui = RSAddonYUI;
}, '0.0.1', { requires: ['plugin', 'oop', 'loader-base', 'mojito-util']});