Version 3.18.1
Show:

File: get/js/get-nodejs.js

  1. /**
  2. * NodeJS specific Get module used to load remote resources.
  3. * It contains the same signature as the default Get module so there is no code change needed.
  4. * @module get-nodejs
  5. * @class GetNodeJS
  6. */
  7. var Module = require('module'),
  8. path = require('path'),
  9. fs = require('fs'),
  10. request = require('request'),
  11. end = function(cb, msg, result) {
  12. //Y.log('Get end: ' + cb.onEnd);
  13. if (Y.Lang.isFunction(cb.onEnd)) {
  14. cb.onEnd.call(Y, msg, result);
  15. }
  16. }, pass = function(cb) {
  17. //Y.log('Get pass: ' + cb.onSuccess);
  18. if (Y.Lang.isFunction(cb.onSuccess)) {
  19. cb.onSuccess.call(Y, cb);
  20. }
  21. end(cb, 'success', 'success');
  22. }, fail = function(cb, er) {
  23. //Y.log('Get fail: ' + er);
  24. er.errors = [er];
  25. if (Y.Lang.isFunction(cb.onFailure)) {
  26. cb.onFailure.call(Y, er, cb);
  27. }
  28. end(cb, er, 'fail');
  29. };
  30. Y.Get = function() {
  31. };
  32. //Setup the default config base path
  33. Y.config.base = path.join(__dirname, '../');
  34. YUI.require = require;
  35. YUI.process = process;
  36. /**
  37. * Takes the raw JS files and wraps them to be executed in the YUI context so they can be loaded
  38. * into the YUI object
  39. * @method _exec
  40. * @private
  41. * @param {String} data The JS to execute
  42. * @param {String} url The path to the file that was parsed
  43. * @param {Function} cb The callback to execute when this is completed
  44. * @param {Error} cb.err=null Error object
  45. * @param {String} cb.url The URL that was just parsed
  46. */
  47. Y.Get._exec = function(data, url, cb) {
  48. if (data.charCodeAt(0) === 0xFEFF) {
  49. data = data.slice(1);
  50. }
  51. var mod = new Module(url, module);
  52. mod.filename = url;
  53. mod.paths = Module._nodeModulePaths(path.dirname(url));
  54. if (typeof YUI._getLoadHook === 'function') {
  55. data = YUI._getLoadHook(data, url);
  56. }
  57. mod._compile('module.exports = function (YUI) {' +
  58. 'return (function () {'+ data + '\n;return YUI;}).apply(global);' +
  59. '};', url);
  60. /*global YUI:true */
  61. YUI = mod.exports(YUI);
  62. mod.loaded = true;
  63. cb(null, url);
  64. };
  65. /**
  66. * Fetches the content from a remote URL or a file from disc and passes the content
  67. * off to `_exec` for parsing
  68. * @method _include
  69. * @private
  70. * @param {String} url The URL/File path to fetch the content from
  71. * @param {Function} cb The callback to fire once the content has been executed via `_exec`
  72. */
  73. Y.Get._include = function (url, cb) {
  74. var cfg,
  75. mod,
  76. self = this;
  77. if (url.match(/^https?:\/\//)) {
  78. cfg = {
  79. url: url,
  80. timeout: self.timeout
  81. };
  82. request(cfg, function (err, response, body) {
  83. if (err) {
  84. Y.log(err, 'error', 'get');
  85. cb(err, url);
  86. } else {
  87. Y.Get._exec(body, url, cb);
  88. }
  89. });
  90. } else {
  91. try {
  92. // Try to resolve paths relative to the module that required yui.
  93. url = Module._findPath(url, Module._resolveLookupPaths(url, module.parent.parent)[1]);
  94. if (Y.config.useSync) {
  95. //Needs to be in useSync
  96. mod = fs.readFileSync(url,'utf8');
  97. } else {
  98. fs.readFile(url, 'utf8', function (err, mod) {
  99. if (err) {
  100. cb(err, url);
  101. } else {
  102. Y.Get._exec(mod, url, cb);
  103. }
  104. });
  105. return;
  106. }
  107. } catch (err) {
  108. cb(err, url);
  109. return;
  110. }
  111. Y.Get._exec(mod, url, cb);
  112. }
  113. };
  114. /**
  115. * Override for Get.script for loading local or remote YUI modules.
  116. * @method js
  117. * @param {Array|String} s The URL's to load into this context
  118. * @param {Object} options Transaction options
  119. */
  120. Y.Get.js = function(s, options) {
  121. var urls = Y.Array(s), url, i, l = urls.length, c= 0,
  122. check = function() {
  123. if (c === l) {
  124. pass(options);
  125. }
  126. };
  127. /*jshint loopfunc: true */
  128. for (i=0; i<l; i++) {
  129. url = urls[i];
  130. if (Y.Lang.isObject(url)) {
  131. url = url.url;
  132. }
  133. url = url.replace(/'/g, '%27');
  134. Y.log('URL: ' + url, 'info', 'get');
  135. Y.Get._include(url, function(err, url) {
  136. if (!Y.config) {
  137. Y.config = {
  138. debug: true
  139. };
  140. }
  141. if (options.onProgress) {
  142. options.onProgress.call(options.context || Y, url);
  143. }
  144. Y.log('After Load: ' + url, 'info', 'get');
  145. if (err) {
  146. fail(options, err);
  147. Y.log('----------------------------------------------------------', 'error', 'get');
  148. Y.log(err, 'error', 'get');
  149. Y.log('----------------------------------------------------------', 'error', 'get');
  150. } else {
  151. c++;
  152. check();
  153. }
  154. });
  155. }
  156. //Keeping Signature in the browser.
  157. return {
  158. execute: function() {}
  159. };
  160. };
  161. /**
  162. * Alias for `Y.Get.js`
  163. * @method script
  164. */
  165. Y.Get.script = Y.Get.js;
  166. //Place holder for SS Dom access
  167. Y.Get.css = function(s, cb) {
  168. Y.log('Y.Get.css is not supported, just reporting that it has loaded but not fetching.', 'warn', 'get');
  169. pass(cb);
  170. };