1 /* 2 * AsyncAsyncNodeLoader.js - Loader implementation for nodejs with asynchronous calls. 3 * Mostly this is used for testing the async calls 4 * 5 * Copyright © 2018, JEDLSoft 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 /** 22 * @class 23 * Implementation of async Loader for nodejs. 24 * 25 * @constructor 26 * @private 27 */ 28 module.exports = function (ilib) { 29 var path = require("./Path.js"), 30 fs = require("fs"), 31 util = require("util"), 32 Loader = require("./Loader.js"); 33 34 var AsyncNodeLoader = function (ilib) { 35 // util.print("new common AsyncNodeLoader instance\n"); 36 37 this.parent.call(this, ilib); 38 39 // root of the app that created this loader 40 // this.root = root || process.cwd(); 41 this.root = process.cwd(); 42 43 this.base = (typeof(module) !== 'undefined' && module.filename) ? 44 path.join(path.dirname(module.filename), "..") : 45 this.root; 46 47 //console.log("module.filename is " + module.filename + "\n"); 48 //console.log("base is defined as " + this.base + "\n"); 49 50 this.includePath.push(path.join(this.root, "resources")); // always check the application's resources dir first 51 52 // then a standard locale dir of a built version of ilib from npm 53 this._exists(path.join(this.base, "locale"), "localeinfo.json"); 54 55 // try the standard install directories 56 this._exists("/usr/share/javascript/ilib/locale", "localeinfo.json"); 57 58 // ... else fall back to see if we're in a check-out dir of ilib 59 // this._exists(path.join(this.base, "data", "locale"), "localeinfo.json"); 60 61 // console.log("AsyncNodeLoader: include path is now " + JSON.stringify(this.includePath)); 62 }; 63 64 // make this a subclass of loader 65 AsyncNodeLoader.prototype = new Loader(); 66 AsyncNodeLoader.prototype.parent = Loader; 67 AsyncNodeLoader.prototype.constructor = AsyncNodeLoader; 68 69 AsyncNodeLoader.prototype.name = "AsyncNodeLoader"; 70 AsyncNodeLoader.prototype._loadFile = function (pathname, sync, cb) { 71 var text; 72 //console.log("AsyncNodeLoader._loadFile: loading " + pathname + (sync ? " sync" : " async")); 73 try { 74 // on node, just secret load everything synchronously, even when asynchronous 75 // load is requested, or else you will get crazy results where files are not read 76 // until a long time later when the run queue is free 77 fs.readFile(pathname, "utf-8", function(err, text) { 78 if (typeof(cb) === 'function') { 79 cb(!err || err.errno === -2 ? text : undefined); 80 } 81 }); 82 } catch (e) { 83 //console.log("AsyncNodeLoader._loadFile: caught exception"); 84 if (typeof(cb) === 'function') { 85 cb(); 86 } 87 } 88 return text; 89 }; 90 91 AsyncNodeLoader.prototype._exists = function(dir, file) { 92 var fullpath = path.normalize(path.join(dir, file)); 93 //console.log("AsyncNodeLoader._exists: checking for the existence of " + dir); 94 if (fs.existsSync(fullpath)) { 95 //console.log("AsyncNodeLoader._exists: found"); 96 this.includePath.push(dir); 97 } 98 }; 99 100 return new AsyncNodeLoader(ilib); 101 }; 102