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

insubnet

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

insubnet - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

50

insubnet.js

@@ -7,3 +7,3 @@ /*

var proto = {
version: '0.0.3',
version: '0.0.4',
Auto: function (ip, subnet, prefix) {

@@ -15,7 +15,3 @@ if (!ip || !subnet) {

if (sm[0] && sm[1] && !isNaN(sm[1])) {
if (this.isIPv4(ip)) {
return this.__IPv4(ip,sm[0],sm[1]);
} else {
return this.__IPv6(ip,sm[0],sm[1]);
};
return this[this.isIPv4(ip)?'__IPv4':'__IPv6'](ip,sm[0],sm[1]);
};

@@ -62,16 +58,36 @@ return false;

},
Validate: function (ip) {
if (this.subnets && this.isIP(ip) && (!!(this.subnets[this.isIPv4(ip)?"ipv4":"ipv6"].length))) {
var subs = this.subnets[(this.isIPv4(ip)?"ipv4":"ipv6")];
for (var num in subs) {
if (this.Auto(ip,subs[num])) {
return true;
};
};
};
return false;
Filter: function (ip, subnets) {
this.setSubnets(subnets);
if (Array.isArray(ip)){
return ip.filter(function(ip) {
return this.__Validate(ip);
},this);
} else {
return (this.__Validate(ip)?ip:false);
};
},
Validate: function (ip, subnets) {
this.setSubnets(subnets);
if (Array.isArray(ip)){
return ip.map(function(ip) {
return this.__Validate(ip);
},this);
} else {
return this.__Validate(ip);
};
},
__Validate: function (ip) {
if (this.subnets && this.isIP(ip) && (!!(this.subnets[this.isIPv4(ip)?"ipv4":"ipv6"].length))) {
var subs = this.subnets[(this.isIPv4(ip)?"ipv4":"ipv6")];
for (var num in subs) {
if (this.Auto(ip,subs[num])) {
return true;
};
};
};
return false;
},
setSubnets: function (subnets) {
this.subnets = {ipv4:[],ipv6:[]};
if (Array.isArray(subnets)) {
this.subnets = {ipv4:[],ipv6:[]};
for (var num in subnets) {

@@ -78,0 +94,0 @@ var subnet = subnets[num];

{
"name": "insubnet",
"version": "0.0.3",
"version": "0.0.4",
"homepage": "http://louist.github.io/inSubnet/",

@@ -5,0 +5,0 @@ "author": {

@@ -1,2 +0,2 @@

inSubnet (v0.0.3)
inSubnet (v0.0.4)
======

@@ -16,29 +16,47 @@

inSubnet.Auto(ip, subnet[, mask]) - Check to find out if <ip> is in <subnet>. Works with IPv4 and IPv6. Returns true or false.
Examples: inSubnet.Auto('1.2.3.4','1.2.0.0/16'); OR inSubnet.Auto('1.2.3.4','1.2.0.0','16');
inSubnet.Auto(ip, subnet[, mask]) - Check to find out if <ip> is in <subnet>. Works with IPv4 and IPv6. Returns boolean.
Examples: inSubnet.Auto('1.2.3.4','1.2.0.0/16'); // true
inSubnet.Auto('1.2.3.4','1.2.0.0','16'); // true
inSubnet.Auto('1.4.3.4','1.2.0.0','16'); // false
inSubnet.IPv4(ip, subnet[, mask]) - Same as "Auto()" but for IPv4 only. - Returns true or false.
Examples: inSubnet.IPv4('1.2.3.4','1.2.0.0/16'); OR inSubnet.IPv4('1.2.3.4','1.2.0.0','16');
inSubnet.IPv4(ip, subnet[, mask]) - Same as "Auto()" but for IPv4 only. - Returns boolean.
Examples: See "Auto()" examples.
inSubnet.IPv6(ip, subnet[, mask]) - Same as "Auto()" but for IPv6 only. - Returns true or false.
Examples: inSubnet.IPv6('2400:cb00::123','2400:cb00::/32'); OR inSubnet.IPv6('2400:cb00::123','2400:cb00::','32');
inSubnet.IPv6(ip, subnet[, mask]) - Same as "Auto()" but for IPv6 only. - Returns boolean.
Examples: inSubnet.IPv6('2400:cb00::123','2400:cb00::/32'); // true
inSubnet.IPv6('2400:cb00::123','2400:cb00::','32'); // true
inSubnet.IPv6('2500:cb00::123','2400:cb00::','32'); // false
inSubnet.isIP(string) - Check if <string> is an IP address. Works for IPv6 and IPv4. - Returns true or false.
Examples: inSubnet.isIP("127.0.0.1"); OR inSubnet.isIP("afd::1");
inSubnet.isIP(string) - Check if <string> is an IP address. Works for IPv6 and IPv4. - Returns boolean.
Examples: inSubnet.isIP("127.0.0.1"); // true
inSubnet.isIP("afd::1"); // true
inSubnet.isIP("asd::1"); // false
inSubnet.isIPv4(string) - Same as "isIP()" but for IPv4 only. - Returns true or false.
Example: inSubnet.isIPv4("127.0.0.1");
inSubnet.isIPv4(string) - Same as "isIP()" but for IPv4 only. - Returns boolean.
Examples: inSubnet.isIPv4("127.0.0.1"); // true
inSubnet.isIPv4("127.0.0.256"); // false
inSubnet.isIPv6(string) - Same as "isIP()" but for IPv6 only. - Returns true or false.
Example: inSubnet.isIPv6("adf::1");
inSubnet.isIPv6(string) - Same as "isIP()" but for IPv6 only. - Returns boolean.
Examples: inSubnet.isIPv6("adf::1"); // true
inSubnet.isIPv6("asf::1"); // false
inSubnet.Expand(ipv6) - Expands an IPv6. - Returns IPv6 or false.
Example: inSubnet.expand("afd::1");
Examples: inSubnet.Expand("afd::1"); // 0afd:0000:0000:0000:0000:0000:0000:0001
inSubnet.Expand("2001:4860:4860::8888"); // 2001:4860:4860:0000:0000:0000:0000:8888
inSubnet.Validate(ip) - Check against an array of subnets set by "setSubnet()". - Returns true or false.
Example: inSubnet.Validate("192.168.1.3");
inSubnet.Validate(ip[, subnets]) - Check <ip> or an Array of IPs against an array of subnets set by "setSubnets()".
If <subnets> is passed, uses "setSubnets()". - Returns boolean or an Array of boolean.
Examples: inSubnet.Validate('127.0.0.1',['127.0.0.1/32','adf::1/32']); // true
inSubnet.Validate(['127.0.0.1','127.0.0.2'],['127.0.0.1/32']); // [true,false]
inSubnet.setSubnets(subnets) - Set a list of subnets for "Validate()". - Returns true or false.
Example: inSubnet.setSubnets(["192.168.1.0/30","::1/32"]);
inSubnet.Filter(array[, subnets]) - Filter an Array of IP addresses against subnets set with "setSubnets()" - Returns an Array of valid IPs.
If <subnets> is passed, uses "setSubnets()". - Returns IP or false, Array of valid IPs.
Examples: inSubnet.Filter(['127.0.0.1','adf::1','127.0.0.2'],['127.0.0.1/32','adf::1/32']); // ['127.0.0.1','adf::1']
inSubnet.Filter('127.0.0.1',['127.0.0.1/32','adf::1/32']); // 127.0.0.1
inSubnet.Filter('127.0.0.2',['127.0.0.1/32','adf::1/32']); // false
inSubnet.setSubnets(subnets) - Set a list of subnets for "Validate()".
WARNING: Overrides all previous "setSubnets()" calls. - Returns boolean.
Examples: inSubnet.setSubnets(["192.168.1.0/30","::1/32"]); // true
inSubnet.setSubnets(["not","subnets","subnet/32"]); // false

@@ -48,4 +66,4 @@ TODO:

- [x] Figure out what should be in the TODO and then write it! (Done!?)
- [ ] Write a better README!
- [ ] Write a better README! (Is this happening?)
- [x] Write a simple HTTP example for CloudFlare. (Look in ./examples/cloudflare.js)
- [ ] Make "Exporter" better.

@@ -53,3 +53,3 @@ var assert = require('assert'),

} catch (e) { errors.push(e.message); };
console.log("[Subnet Validation] Number of errors found: "+errors.length+" - Tests passed: "+pass+" ("+((pass/(errors.length+pass)*100))+"%)");
console.log("[Subnet String Tests] Number of errors found: "+errors.length+" - Tests passed: "+pass+" ("+((pass/(errors.length+pass)*100))+"%)");
errors.forEach(function(err) { console.log('\033[1;31mERROR:\033[0m '+err); });

@@ -95,3 +95,3 @@ };

} catch (e) { errors.push(e.message); };
console.log("[IP Validation] Number of errors found: "+errors.length+" - Tests passed: "+pass+" ("+((pass/(errors.length+pass)*100))+"%)");
console.log("[IP Validation Tests] Number of errors found: "+errors.length+" - Tests passed: "+pass+" ("+((pass/(errors.length+pass)*100))+"%)");
errors.forEach(function(err) { console.log('\033[1;31mERROR:\033[0m '+err); });

@@ -130,3 +130,20 @@ };

} catch (e) { errors.push(e.message); };
console.log("[Subnet Array Validation] Number of errors found: "+errors.length+" - Tests passed: "+pass+" ("+((pass/(errors.length+pass)*100))+"%)");
try {
assert(inSubnet.Validate(['2405:b500::123'])[0],"Index 0 should be true.");
pass++;
} catch (e) { errors.push(e.message); };
try {
assert(!inSubnet.Validate(['2406:b500::123'])[0],"Index 0 should NOT be true.");
pass++;
} catch (e) { errors.push(e.message); };
try {
var ret = inSubnet.Filter(['127.0.0.1','adf::1','127.0.0.2'],['127.0.0.1/32','adf::1/32']).toString();
assert(ret === '127.0.0.1,adf::1',"Returned array should be ['127.0.0.1','adf::1'].");
pass++;
} catch (e) { errors.push(e.message); };
try {
assert(!(inSubnet.Filter(['127.0.0.2'],['127.0.0.1/32','adf::1/32']).length),"Returned array should be empty.");
pass++;
} catch (e) { errors.push(e.message); };
console.log("[Subnet Array Tests] Number of errors found: "+errors.length+" - Tests passed: "+pass+" ("+((pass/(errors.length+pass)*100))+"%)");
errors.forEach(function(err) { console.log('\033[1;31mERROR:\033[0m '+err); });

@@ -133,0 +150,0 @@ };

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