From 61c97db6af0fc7c842c64aa72ead7b57234dd9fe Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Thu, 18 Sep 2014 20:52:55 +0100 Subject: [PATCH 1/3] Add node-ip type definitions Add node-ip type definitions file with a test script. --- node-ip/ip-tests.ts | 19 ++++++++++ node-ip/ip.d.ts | 89 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 node-ip/ip-tests.ts create mode 100644 node-ip/ip.d.ts diff --git a/node-ip/ip-tests.ts b/node-ip/ip-tests.ts new file mode 100644 index 000000000..0975361b5 --- /dev/null +++ b/node-ip/ip-tests.ts @@ -0,0 +1,19 @@ +/// + +import ip = require('ip'); + +var myIP = ip.address(); + +ip.cidr("192.168.0.1/24"); +ip.fromLong(ip.toLong(myIP)); +ip.fromPrefixLen(24); +ip.isEqual("192.168.0.1", myIP); +ip.isLoopback("127.0.0.1"); +ip.isPrivate(myIP); +ip.isPublic(myIP); +ip.loopback("ipv6"); +ip.mask("192.168.0.1", "255.255.255.0"); +ip.not("255.255.255.0"); +ip.or("192.168.0.1", "255.255.255.0"); +var buff = ip.toBuffer(myIP); +ip.toString(buff); \ No newline at end of file diff --git a/node-ip/ip.d.ts b/node-ip/ip.d.ts new file mode 100644 index 000000000..82c2755d7 --- /dev/null +++ b/node-ip/ip.d.ts @@ -0,0 +1,89 @@ +// Type definitions for node-ip +// Project: https://github.com/indutny/node-ip +// Definitions by: Peter Harris +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "ip" { + /** + * Check two IP address are the same. + **/ + export function isEqual(ip1: string, ip2: string): boolean; + + /** + * Convert an IP string into a buffer. + **/ + export function toBuffer(ip: string, buffer?: number, offset?: number): NodeBuffer; + + /** + * Convert an IP buffer into a string. + **/ + export function toString(ip: NodeBuffer, offset?: number, length?: number): string; + + /** + * Get the subnet mask from a CIDR prefix length. + * + * @param family The IP family is infered from the prefixLength, but can be explicity specified as either "ipv4" or "ipv6". + **/ + export function fromPrefixLen(prefixLength: number, family?:string): string; + + /** + * Get the network ID IP address from an IP address and its subnet mask. + **/ + export function mask(ip: string, mask: string): string; + + /** + * Get the network ID IP address from an IP address in CIDR notation. + **/ + export function cidr(cidr: string): string; + + /** + * Get the bitwise inverse (NOT every octet) of an IP address or subnet mask. + **/ + export function not(ip: string): string; + + /** + * Get the bitwise OR of two IP addresses (usually an IP address and a subnet mask). + **/ + export function or(ip: string, mask:string): string; + + /** + * Check whether an IP is within a private IP address range. + **/ + export function isPrivate(ip: string): boolean; + + /** + * Check whether an IP is within a public IP address range. + **/ + export function isPublic(ip: string): boolean; + + /** + * Check whether an IP is a loopback address. + **/ + export function isLoopback(ip: string): boolean; + + /** + * Get the loopback address for an IP family. + * + * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4". + **/ + export function loopback(family?: string): string; + + /** + * Get the address for the network interface on the current system with the specified 'name'. + * If no interface name is specified, the first IPv4 address or loopback address is returned. + * + * @param name The name can be any named interface, or 'public' or 'private'. + * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4". + **/ + export function address(name?: string, family?: string):string; + + /** + * Convert a string IPv4 IP address to the equivalent long numeric value. + **/ + export function toLong(ip: string): number; + + /** + * Convert an IPv4 IP address from its the long numeric value to a string. + **/ + export function fromLong(ip: number): string; +} \ No newline at end of file From 203d7a8d33c9ff9a42a61065888587394efee0a8 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Thu, 18 Sep 2014 21:10:55 +0100 Subject: [PATCH 2/3] Add empty NodeBuffer interface The empty NodeBuffer interface acts as a placeholder if node's type definitions file isn't referenced. This is to hopefully stop the Travis CI build error. --- node-ip/ip-tests.ts | 2 +- node-ip/ip.d.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/node-ip/ip-tests.ts b/node-ip/ip-tests.ts index 0975361b5..b852e08e7 100644 --- a/node-ip/ip-tests.ts +++ b/node-ip/ip-tests.ts @@ -15,5 +15,5 @@ ip.loopback("ipv6"); ip.mask("192.168.0.1", "255.255.255.0"); ip.not("255.255.255.0"); ip.or("192.168.0.1", "255.255.255.0"); -var buff = ip.toBuffer(myIP); +var buff:any = ip.toBuffer(myIP); ip.toString(buff); \ No newline at end of file diff --git a/node-ip/ip.d.ts b/node-ip/ip.d.ts index 82c2755d7..2fe3201c7 100644 --- a/node-ip/ip.d.ts +++ b/node-ip/ip.d.ts @@ -3,6 +3,8 @@ // Definitions by: Peter Harris // Definitions: https://github.com/borisyankov/DefinitelyTyped +interface NodeBuffer { } + declare module "ip" { /** * Check two IP address are the same. From 5c7e8235061fea99ebda10f6c572ac77b72c3221 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Wed, 24 Sep 2014 15:42:07 +0100 Subject: [PATCH 3/3] Rename node-ip directory to ip Rename node-ip to ip so the folder name matches the library name on npm. --- {node-ip => ip}/ip-tests.ts | 0 {node-ip => ip}/ip.d.ts | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {node-ip => ip}/ip-tests.ts (100%) rename {node-ip => ip}/ip.d.ts (100%) diff --git a/node-ip/ip-tests.ts b/ip/ip-tests.ts similarity index 100% rename from node-ip/ip-tests.ts rename to ip/ip-tests.ts diff --git a/node-ip/ip.d.ts b/ip/ip.d.ts similarity index 100% rename from node-ip/ip.d.ts rename to ip/ip.d.ts