require-from-url
Advanced tools
Comparing version 0.0.2 to 1.0.0
99
index.js
@@ -1,74 +0,45 @@ | ||
'use strict'; | ||
var parse = require("url").parse; | ||
var listen = require("./addLoadListener.js"); | ||
var path = require("path"); | ||
var proto= { | ||
http : require("http"), | ||
https: require("https") | ||
}; | ||
var parse= require("url").parse; | ||
function requireFromString(code, filename) { | ||
var Module = module.constructor; | ||
var m = new Module(filename, module.parent); | ||
m.filename = filename; | ||
m.paths = Module._nodeModulePaths(path.dirname(filename)); | ||
m._compile(code, filename); | ||
return m.exports; | ||
} | ||
function requireFromUrl(url, callback) { | ||
proto[url.protocol.slice(0, -1)].get(url, function(res) { | ||
var str = ""; | ||
res.on("data", function(data) { | ||
str+= data; | ||
}).on("end", function() { | ||
switch(res.statusCode) { | ||
case 200: | ||
callback(requireFromString(str, __dirname+url.path)); | ||
break; | ||
default: | ||
callback(new Error("href="+url.href+"; statusCode="+res.statusCode)); | ||
} | ||
}); | ||
}).on('error', callback); | ||
} | ||
module.exports = function() { | ||
var resolv = {}; | ||
var error; | ||
function _requireFromUrl(url, callback) { | ||
resolv[url.href] ? callback(resolv[url.href]) : requireFromUrl(url, function(mod) { | ||
if(!(mod instanceof Error)) resolv[url.href] = mod; | ||
callback(mod); | ||
}); | ||
} | ||
function _callback(_mod, callback) { | ||
if(error) { | ||
for(var i=0; i<_mod.length; i++) { | ||
if(_mod[i] instanceof Error) { | ||
error(_mod[i]); | ||
return; | ||
} | ||
} | ||
} | ||
callback.apply(undefined, _mod); | ||
} | ||
var result = function(url, callback) { | ||
var _status, _resolve, _reject, _args; | ||
var _result = function(url) { | ||
_status = 'Pending'; | ||
_resolve = []; | ||
_reject = []; | ||
_args = undefined; | ||
var _url = [].concat(url), | ||
_len = _url.length, | ||
_mod = Array(_len); | ||
error = undefined; | ||
values = Array(_len); | ||
_url.forEach(function(url, index) { | ||
_requireFromUrl(parse(url), function(mod) { | ||
_mod[index] = mod; | ||
if(!--_len) _callback(_mod, callback); | ||
listen(parse(url), function(value) { | ||
values[index] = value; | ||
if(!--_len) { | ||
for(var i=0; i<values.length; i++) { | ||
if(values[i] instanceof Error) { | ||
_status = 'Rejected'; | ||
_args = values[i]; | ||
_reject.forEach(reject => reject(_args)); | ||
return; | ||
} | ||
} | ||
_status = 'Resolved'; | ||
_args = values; | ||
_resolve.forEach(resolve => resolve.apply(undefined, _args)); | ||
} | ||
}); | ||
}); | ||
return result; | ||
return _result; | ||
}; | ||
result.error = function(callback) { | ||
error = callback; | ||
return result; | ||
_result.then = function(resolve) { | ||
if(_status==='Resolved') resolve.apply(undefined, _args); | ||
else _resolve.push(resolve); | ||
return _result; | ||
}; | ||
return result; | ||
_result.catch = function(reject) { | ||
if(_status==='Rejected') reject(_args); | ||
else _reject.push(reject); | ||
return _result; | ||
}; | ||
return _result; | ||
}(); |
{ | ||
"name": "require-from-url", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "Require module from URL", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -17,3 +17,4 @@ # require-from-url | ||
requireFromUrl(["http://yishizhencang.com:8080/HelloWorld.js"], function(HelloWorld) { | ||
requireFromUrl(["http://yishizhencang.com:8080/HelloWorld.js"]) | ||
.then(function(HelloWorld) { | ||
var api = new HelloWorld.default; | ||
@@ -23,3 +24,4 @@ api.welcome(function(greeting) { | ||
}); | ||
}).error(function(e) { | ||
}) | ||
.catch(function(e) { | ||
console.log(e); | ||
@@ -26,0 +28,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3937
5
92
2
33