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