Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

require-from-url

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-from-url - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

61

index.js
'use strict';
var path = require("path");
var proto= {

@@ -7,6 +8,14 @@ http : require("http"),

};
var path = require("path");
var parse= require("url").parse;
function getStringFromUrl(url, callback) {
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) {

@@ -17,4 +26,9 @@ var str = "";

}).on("end", function() {
if(res.statusCode===200) callback(str);
else callback(new Error("statusCode="+res.statusCode));
switch(res.statusCode) {
case 200:
callback(requireFromString(str, __dirname+url.path));
break;
default:
callback(new Error("href="+url.href+"; statusCode="+res.statusCode));
}
});

@@ -24,19 +38,32 @@ }).on('error', callback);

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;
}
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 urlObj = parse(url);
var _url = [].concat(url),
_len = _url.length,
_mod = Array(_len);
error = undefined;
getStringFromUrl(urlObj, function(str) {
if(str instanceof Error) error ? error(str) : callback(str);
else callback(requireFromString(str, __dirname+urlObj.path));
_url.forEach(function(url, index) {
_requireFromUrl(parse(url), function(mod) {
_mod[index] = mod;
if(!--_len) _callback(_mod, callback);
});
});

@@ -43,0 +70,0 @@ return result;

{
"name": "require-from-url",
"version": "0.0.1",
"version": "0.0.2",
"description": "Require module from URL",

@@ -5,0 +5,0 @@ "main": "./index.js",

@@ -17,3 +17,3 @@ # require-from-url

requireFromUrl("http://yishizhencang.com:8080/HelloWorld.js", function(HelloWorld) {
requireFromUrl(["http://yishizhencang.com:8080/HelloWorld.js"], function(HelloWorld) {
var api = new HelloWorld.default;

@@ -24,27 +24,9 @@ api.welcome(function(greeting) {

}).error(function(e) {
console.log('<1>', e);
console.log(e);
});
```
### or
```js
var requireFromUrl = require('require-from-url');
requireFromUrl("http://yishizhencang.com:8080/HelloWorld.js", function(HelloWorld) {
if(HelloWorld instanceof Error) {
console.log('<2>', HelloWorld);
}
else {
var api = new HelloWorld.default;
api.welcome(function(greeting) {
console.log(greeting);
});
}
});
```
## License
MIT © [May xiaoya zhang](gzhangzy@gmail.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc