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
|
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview The module loader for loading modules across the network.
*
* Browsers do not guarantee that scripts appended to the document
* are executed in the order they are added. For production mode, we use
* XHRs to load scripts, because they do not have this problem and they
* have superior mechanisms for handling failure. However, XHR-evaled
* scripts are harder to debug.
*
* In debugging mode, we use normal script tags. In order to make this work,
* we load the scripts in serial: we do not execute script B to the document
* until we are certain that script A is finished loading.
*
*/
goog.provide('goog.module.ModuleLoader');
goog.require('goog.Timer');
goog.require('goog.array');
goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.events.EventHandler');
goog.require('goog.events.EventId');
goog.require('goog.events.EventTarget');
goog.require('goog.html.TrustedResourceUrl');
goog.require('goog.labs.userAgent.browser');
goog.require('goog.log');
goog.require('goog.module.AbstractModuleLoader');
goog.require('goog.net.BulkLoader');
goog.require('goog.net.EventType');
goog.require('goog.net.jsloader');
goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
/**
* A class that loads Javascript modules.
* @constructor
* @extends {goog.events.EventTarget}
* @implements {goog.module.AbstractModuleLoader}
*/
goog.module.ModuleLoader = function() {
goog.module.ModuleLoader.base(this, 'constructor');
/**
* Event handler for managing handling events.
* @type {goog.events.EventHandler<!goog.module.ModuleLoader>}
* @private
*/
this.eventHandler_ = new goog.events.EventHandler(this);
/**
* A map from module IDs to goog.module.ModuleLoader.LoadStatus.
* @type {!Object<Array<string>, goog.module.ModuleLoader.LoadStatus>}
* @private
*/
this.loadingModulesStatus_ = {};
};
goog.inherits(goog.module.ModuleLoader, goog.events.EventTarget);
/**
* A logger.
* @type {goog.log.Logger}
* @protected
*/
goog.module.ModuleLoader.prototype.logger =
goog.log.getLogger('goog.module.ModuleLoader');
/**
* Whether debug mode is enabled.
* @type {boolean}
* @private
*/
goog.module.ModuleLoader.prototype.debugMode_ = false;
/**
* Whether source url injection is enabled.
* @type {boolean}
* @private
*/
goog.module.ModuleLoader.prototype.sourceUrlInjection_ = false;
/**
* @return {boolean} Whether sourceURL affects stack traces.
*/
goog.module.ModuleLoader.supportsSourceUrlStackTraces = function() {
return goog.userAgent.product.CHROME ||
(goog.labs.userAgent.browser.isFirefox() &&
goog.labs.userAgent.browser.isVersionOrHigher('36'));
};
/**
* @return {boolean} Whether sourceURL affects the debugger.
*/
goog.module.ModuleLoader.supportsSourceUrlDebugger = function() {
return goog.userAgent.product.CHROME || goog.userAgent.GECKO;
};
/**
* Gets the debug mode for the loader.
* @return {boolean} Whether the debug mode is enabled.
*/
goog.module.ModuleLoader.prototype.getDebugMode = function() {
return this.debugMode_;
};
/**
* Sets the debug mode for the loader.
* @param {boolean} debugMode Whether the debug mode is enabled.
*/
goog.module.ModuleLoader.prototype.setDebugMode = function(debugMode) {
this.debugMode_ = debugMode;
};
/**
* When enabled, we will add a sourceURL comment to the end of all scripts
* to mark their origin.
*
* On WebKit, stack traces will reflect the sourceURL comment, so this is
* useful for debugging webkit stack traces in production.
*
* Notice that in debug mode, we will use source url injection + eval rather
* then appending script nodes to the DOM, because the scripts will load far
* faster. (Appending script nodes is very slow, because we can't parallelize
* the downloading and evaling of the script).
*
* The cost of appending sourceURL information is negligible when compared to
* the cost of evaling the script. Almost all clients will want this on.
*
* TODO(nicksantos): Turn this on by default. We may want to turn this off
* for clients that inject their own sourceURL.
*
* @param {boolean} enabled Whether source url injection is enabled.
*/
goog.module.ModuleLoader.prototype.setSourceUrlInjection = function(enabled) {
this.sourceUrlInjection_ = enabled;
};
/**
* @return {boolean} Whether we're using source url injection.
* @private
*/
goog.module.ModuleLoader.prototype.usingSourceUrlInjection_ = function() {
return this.sourceUrlInjection_ ||
(this.getDebugMode() &&
goog.module.ModuleLoader.supportsSourceUrlStackTraces());
};
/** @override */
goog.module.ModuleLoader.prototype.loadModules = function(
ids, moduleInfoMap, opt_successFn, opt_errorFn, opt_timeoutFn,
opt_forceReload) {
var loadStatus = this.loadingModulesStatus_[ids] ||
new goog.module.ModuleLoader.LoadStatus();
loadStatus.loadRequested = true;
loadStatus.successFn = opt_successFn || null;
loadStatus.errorFn = opt_errorFn || null;
if (!this.loadingModulesStatus_[ids]) {
// Modules were not prefetched.
this.loadingModulesStatus_[ids] = loadStatus;
this.downloadModules_(ids, moduleInfoMap);
// TODO(user): Need to handle timeouts in the module loading code.
} else if (goog.isDefAndNotNull(loadStatus.responseTexts)) {
// Modules prefetch is complete.
this.evaluateCode_(ids);
}
// Otherwise modules prefetch is in progress, and these modules will be
// executed after the prefetch is complete.
};
/**
* Evaluate the JS code.
* @param {Array<string>} moduleIds The module ids.
* @private
*/
goog.module.ModuleLoader.prototype.evaluateCode_ = function(moduleIds) {
this.dispatchEvent(
new goog.module.ModuleLoader.RequestSuccessEvent(moduleIds));
goog.log.info(this.logger, 'evaluateCode ids:' + moduleIds);
var loadStatus = this.loadingModulesStatus_[moduleIds];
var uris = loadStatus.requestUris;
var texts = loadStatus.responseTexts;
var error = null;
try {
if (this.usingSourceUrlInjection_()) {
for (var i = 0; i < uris.length; i++) {
var uri = uris[i];
goog.globalEval(texts[i] + ' //# sourceURL=' + uri);
}
} else {
goog.globalEval(texts.join('\n'));
}
} catch (e) {
error = e;
// TODO(user): Consider throwing an exception here.
goog.log.warning(
this.logger, 'Loaded incomplete code for module(s): ' + moduleIds, e);
}
this.dispatchEvent(new goog.module.ModuleLoader.EvaluateCodeEvent(moduleIds));
if (error) {
this.handleErrorHelper_(
moduleIds, loadStatus.errorFn, null /* status */, error);
} else if (loadStatus.successFn) {
loadStatus.successFn();
}
delete this.loadingModulesStatus_[moduleIds];
};
/**
* Handles a successful response to a request for prefetch or load one or more
* modules.
*
* @param {goog.net.BulkLoader} bulkLoader The bulk loader.
* @param {Array<string>} moduleIds The ids of the modules requested.
* @private
*/
goog.module.ModuleLoader.prototype.handleSuccess_ = function(
bulkLoader, moduleIds) {
goog.log.info(this.logger, 'Code loaded for module(s): ' + moduleIds);
var loadStatus = this.loadingModulesStatus_[moduleIds];
loadStatus.responseTexts = bulkLoader.getResponseTexts();
if (loadStatus.loadRequested) {
this.evaluateCode_(moduleIds);
}
// NOTE: A bulk loader instance is used for loading a set of module ids.
// Once these modules have been loaded successfully or in error the bulk
// loader should be disposed as it is not needed anymore. A new bulk loader
// is instantiated for any new modules to be loaded. The dispose is called
// on a timer so that the bulkloader has a chance to release its
// objects.
goog.Timer.callOnce(bulkLoader.dispose, 5, bulkLoader);
};
/** @override */
goog.module.ModuleLoader.prototype.prefetchModule = function(id, moduleInfo) {
// Do not prefetch in debug mode.
if (this.getDebugMode()) {
return;
}
var loadStatus = this.loadingModulesStatus_[[id]];
if (loadStatus) {
return;
}
var moduleInfoMap = {};
moduleInfoMap[id] = moduleInfo;
this.loadingModulesStatus_[[id]] = new goog.module.ModuleLoader.LoadStatus();
this.downloadModules_([id], moduleInfoMap);
};
/**
* Downloads a list of JavaScript modules.
*
* @param {Array<string>} ids The module ids in dependency order.
* @param {!Object<string, !goog.module.ModuleInfo>} moduleInfoMap A mapping
* from module id to ModuleInfo object.
* @private
*/
goog.module.ModuleLoader.prototype.downloadModules_ = function(
ids, moduleInfoMap) {
var trustedUris = [];
for (var i = 0; i < ids.length; i++) {
goog.array.extend(trustedUris, moduleInfoMap[ids[i]].getUris());
}
var uris = goog.array.map(trustedUris, goog.html.TrustedResourceUrl.unwrap);
goog.log.info(this.logger, 'downloadModules ids:' + ids + ' uris:' + uris);
if (this.getDebugMode() && !this.usingSourceUrlInjection_()) {
// In debug mode use <script> tags rather than XHRs to load the files.
// This makes it possible to debug and inspect stack traces more easily.
// It's also possible to use it to load JavaScript files that are hosted on
// another domain.
// The scripts need to load serially, so this is much slower than parallel
// script loads with source url injection.
goog.net.jsloader.safeLoadMany(trustedUris);
} else {
var loadStatus = this.loadingModulesStatus_[ids];
loadStatus.requestUris = uris;
var bulkLoader = new goog.net.BulkLoader(uris);
var eventHandler = this.eventHandler_;
eventHandler.listen(
bulkLoader, goog.net.EventType.SUCCESS,
goog.bind(this.handleSuccess_, this, bulkLoader, ids));
eventHandler.listen(
bulkLoader, goog.net.EventType.ERROR,
goog.bind(this.handleError_, this, bulkLoader, ids));
bulkLoader.load();
}
};
/**
* Handles an error during a request for one or more modules.
* @param {goog.net.BulkLoader} bulkLoader The bulk loader.
* @param {Array<string>} moduleIds The ids of the modules requested.
* @param {number} status The response status.
* @private
*/
goog.module.ModuleLoader.prototype.handleError_ = function(
bulkLoader, moduleIds, status) {
var loadStatus = this.loadingModulesStatus_[moduleIds];
// The bulk loader doesn't cancel other requests when a request fails. We will
// delete the loadStatus in the first failure, so it will be undefined in
// subsequent errors.
if (loadStatus) {
delete this.loadingModulesStatus_[moduleIds];
this.handleErrorHelper_(moduleIds, loadStatus.errorFn, status);
}
// NOTE: A bulk loader instance is used for loading a set of module ids. Once
// these modules have been loaded successfully or in error the bulk loader
// should be disposed as it is not needed anymore. A new bulk loader is
// instantiated for any new modules to be loaded. The dispose is called
// on another thread so that the bulkloader has a chance to release its
// objects.
goog.Timer.callOnce(bulkLoader.dispose, 5, bulkLoader);
};
/**
* Handles an error during a request for one or more modules.
* @param {Array<string>} moduleIds The ids of the modules requested.
* @param {?function(?number)} errorFn The function to call on failure.
* @param {?number} status The response status.
* @param {!Error=} opt_error The error encountered, if available.
* @private
*/
goog.module.ModuleLoader.prototype.handleErrorHelper_ = function(
moduleIds, errorFn, status, opt_error) {
this.dispatchEvent(
new goog.module.ModuleLoader.RequestErrorEvent(moduleIds, opt_error));
goog.log.warning(this.logger, 'Request failed for module(s): ' + moduleIds);
if (errorFn) {
errorFn(status);
}
};
/** @override */
goog.module.ModuleLoader.prototype.disposeInternal = function() {
goog.module.ModuleLoader.superClass_.disposeInternal.call(this);
this.eventHandler_.dispose();
this.eventHandler_ = null;
};
/**
* Events dispatched by the ModuleLoader.
* @const
*/
goog.module.ModuleLoader.EventType = {
/**
* @const {!goog.events.EventId<
* !goog.module.ModuleLoader.EvaluateCodeEvent>} Called after the code for
* a module is evaluated.
*/
EVALUATE_CODE:
new goog.events.EventId(goog.events.getUniqueId('evaluateCode')),
/**
* @const {!goog.events.EventId<
* !goog.module.ModuleLoader.RequestSuccessEvent>} Called when the
* BulkLoader finishes successfully.
*/
REQUEST_SUCCESS:
new goog.events.EventId(goog.events.getUniqueId('requestSuccess')),
/**
* @const {!goog.events.EventId<
* !goog.module.ModuleLoader.RequestErrorEvent>} Called when the
* BulkLoader fails, or code loading fails.
*/
REQUEST_ERROR:
new goog.events.EventId(goog.events.getUniqueId('requestError'))
};
/**
* @param {Array<string>} moduleIds The ids of the modules being evaluated.
* @constructor
* @extends {goog.events.Event}
* @final
* @protected
*/
goog.module.ModuleLoader.EvaluateCodeEvent = function(moduleIds) {
goog.module.ModuleLoader.EvaluateCodeEvent.base(
this, 'constructor', goog.module.ModuleLoader.EventType.EVALUATE_CODE);
/**
* @type {Array<string>}
*/
this.moduleIds = moduleIds;
};
goog.inherits(goog.module.ModuleLoader.EvaluateCodeEvent, goog.events.Event);
/**
* @param {Array<string>} moduleIds The ids of the modules being evaluated.
* @constructor
* @extends {goog.events.Event}
* @final
* @protected
*/
goog.module.ModuleLoader.RequestSuccessEvent = function(moduleIds) {
goog.module.ModuleLoader.RequestSuccessEvent.base(
this, 'constructor', goog.module.ModuleLoader.EventType.REQUEST_SUCCESS);
/**
* @type {Array<string>}
*/
this.moduleIds = moduleIds;
};
goog.inherits(goog.module.ModuleLoader.RequestSuccessEvent, goog.events.Event);
/**
* @param {Array<string>} moduleIds The ids of the modules being evaluated.
* @param {!Error=} opt_error The error encountered, if available.
* @constructor
* @extends {goog.events.Event}
* @final
* @protected
*/
goog.module.ModuleLoader.RequestErrorEvent = function(moduleIds, opt_error) {
goog.module.ModuleLoader.RequestErrorEvent.base(
this, 'constructor', goog.module.ModuleLoader.EventType.REQUEST_ERROR);
/**
* @type {Array<string>}
*/
this.moduleIds = moduleIds;
/** @type {?Error} */
this.error = opt_error || null;
};
goog.inherits(goog.module.ModuleLoader.RequestErrorEvent, goog.events.Event);
/**
* A class that keeps the state of the module during the loading process. It is
* used to save loading information between modules download and evaluation.
* @constructor
* @final
*/
goog.module.ModuleLoader.LoadStatus = function() {
/**
* The request uris.
* @type {Array<string>}
*/
this.requestUris = null;
/**
* The response texts.
* @type {Array<string>}
*/
this.responseTexts = null;
/**
* Whether loadModules was called for the set of modules referred by this
* status.
* @type {boolean}
*/
this.loadRequested = false;
/**
* Success callback.
* @type {?function()}
*/
this.successFn = null;
/**
* Error callback.
* @type {?function(?number)}
*/
this.errorFn = null;
};
|