require-from-url
Advanced tools
Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "require-from-url", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Require module from URL", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
# require-from-url | ||
Load module from URL in Node. | ||
## Install | ||
``` | ||
@@ -12,15 +10,10 @@ $ npm install require-from-url | ||
## Usage | ||
```js | ||
var requireFromUrl = require('require-from-url'); | ||
requireFromUrl(["https://nodeway.org/HelloWorld.js"], function(err, HelloWorld) { | ||
if(err) return; | ||
var api = new HelloWorld.default; | ||
api.hello('world', (err, greeting) => console.log(greeting)); | ||
}); | ||
var requireFromUrl = require('require-from-url/sync'); | ||
var HelloWorld = requireFromUrl("https://nodeway.org/HelloWorld.js"); | ||
var api = new HelloWorld; | ||
api.on('data', console.log); | ||
``` | ||
## License | ||
MIT © [May xiaoya zhang](gzhangzy@gmail.com) |
@@ -1,40 +0,11 @@ | ||
var path = require("path"); | ||
var proto = { | ||
http : require("http"), | ||
https : require("https") | ||
}; | ||
var crypto= require('crypto'); | ||
var fetch = require("./fetchFromUrl.js"); | ||
var requireFromString = require("./requireFromString.js"); | ||
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 md5(str) { | ||
var md5sum = crypto.createHash('md5'); | ||
md5sum.update(str); | ||
return md5sum.digest('hex'); | ||
} | ||
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+'/'+md5(url.href)+url.path)); | ||
break; | ||
default: | ||
callback(new Error("href="+url.href+"; statusCode="+res.statusCode)); | ||
} | ||
}); | ||
}).on('error', callback); | ||
fetch(url, function(err, str, filename) { | ||
err ? callback(err) | ||
: callback(requireFromString(str, filename)); | ||
}); | ||
} | ||
module.exports = requireFromUrl; |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
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
4180
9
102
19
5