diff --git a/types/tunnel/index.d.ts b/types/tunnel/index.d.ts new file mode 100644 index 0000000000..eecefdc8b8 --- /dev/null +++ b/types/tunnel/index.d.ts @@ -0,0 +1,46 @@ +// Type definitions for tunnel 0.0 +// Project: https://github.com/koichik/node-tunnel/ +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +import { Agent } from 'http'; + +export function httpOverHttp(options?: HttpOptions): Agent; +export function httpsOverHttp(options?: HttpsOverHttpOptions): Agent; +export function httpOverHttps(options?: HttpOverHttpsOptions): Agent; +export function httpsOverHttps(options?: HttpsOverHttpsOptions): Agent; + +export interface HttpOptions { + maxSockets?: number; + proxy?: ProxyOptions; +} + +export interface HttpsOverHttpOptions extends HttpOptions { + ca?: Buffer[]; + key?: Buffer; + cert?: Buffer; +} + +export interface HttpOverHttpsOptions extends HttpOptions { + proxy?: HttpsProxyOptions; +} + +export interface HttpsOverHttpsOptions extends HttpsOverHttpOptions { + proxy?: HttpsProxyOptions; +} + +export interface ProxyOptions { + host?: string; + port?: number; + localAddress?: string; + proxyAuth?: string; + headers: { [key: string]: any }; +} + +export interface HttpsProxyOptions extends ProxyOptions { + ca?: Buffer[]; + servername?: string; + key?: Buffer; + cert?: Buffer; +} diff --git a/types/tunnel/tsconfig.json b/types/tunnel/tsconfig.json new file mode 100644 index 0000000000..6b7ab12383 --- /dev/null +++ b/types/tunnel/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "tunnel-tests.ts" + ] +} diff --git a/types/tunnel/tslint.json b/types/tunnel/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/tunnel/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/tunnel/tunnel-tests.ts b/types/tunnel/tunnel-tests.ts new file mode 100644 index 0000000000..13ca0d15d7 --- /dev/null +++ b/types/tunnel/tunnel-tests.ts @@ -0,0 +1,147 @@ +import * as fs from 'fs'; +import * as tunnel from 'tunnel'; +import * as http from 'http'; +import * as https from 'https'; + +const poolSize = 10; +const proxyHost = 'host'; +const proxyPort = 80; +const localAddress = '1.2.3.4'; + +(() => { + const tunnelingAgent = tunnel.httpOverHttp({ + maxSockets: poolSize, // Defaults to http.Agent.defaultMaxSockets + + proxy: { // Proxy settings + host: proxyHost, // Defaults to 'localhost' + port: proxyPort, // Defaults to 80 + localAddress, // Local interface if necessary + + // Basic authorization for proxy server if necessary + proxyAuth: 'user:password', + + // Header fields for proxy server if necessary + headers: { + 'User-Agent': 'Node' + } + } + }); + + const req = http.request({ + host: 'example.com', + port: 80, + agent: tunnelingAgent + }); +})(); + +(() => { + const tunnelingAgent = tunnel.httpsOverHttp({ + maxSockets: poolSize, // Defaults to http.Agent.defaultMaxSockets + + // CA for origin server if necessary + ca: [ fs.readFileSync('origin-server-ca.pem')], + + // Client certification for origin server if necessary + key: fs.readFileSync('origin-server-key.pem'), + cert: fs.readFileSync('origin-server-cert.pem'), + + proxy: { // Proxy settings + host: proxyHost, // Defaults to 'localhost' + port: proxyPort, // Defaults to 80 + localAddress, // Local interface if necessary + + // Basic authorization for proxy server if necessary + proxyAuth: 'user:password', + + // Header fields for proxy server if necessary + headers: { + 'User-Agent': 'Node' + }, + } + }); + + const req = https.request({ + host: 'example.com', + port: 443, + agent: tunnelingAgent + }); +})(); + +(() => { + const tunnelingAgent = tunnel.httpOverHttps({ + maxSockets: poolSize, // Defaults to http.Agent.defaultMaxSockets + + proxy: { // Proxy settings + host: proxyHost, // Defaults to 'localhost' + port: proxyPort, // Defaults to 443 + localAddress, // Local interface if necessary + + // Basic authorization for proxy server if necessary + proxyAuth: 'user:password', + + // Header fields for proxy server if necessary + headers: { + 'User-Agent': 'Node' + }, + + // CA for proxy server if necessary + ca: [ fs.readFileSync('origin-server-ca.pem')], + + // Server name for verification if necessary + servername: 'example.com', + + // Client certification for proxy server if necessary + key: fs.readFileSync('origin-server-key.pem'), + cert: fs.readFileSync('origin-server-cert.pem'), + } + }); + + const req = http.request({ + host: 'example.com', + port: 80, + agent: tunnelingAgent + }); +})(); + +(() => { + const tunnelingAgent = tunnel.httpsOverHttps({ + maxSockets: poolSize, // Defaults to http.Agent.defaultMaxSockets + + // CA for origin server if necessary + ca: [ fs.readFileSync('origin-server-ca.pem')], + + // Client certification for origin server if necessary + key: fs.readFileSync('origin-server-key.pem'), + cert: fs.readFileSync('origin-server-cert.pem'), + + proxy: { // Proxy settings + host: proxyHost, // Defaults to 'localhost' + port: proxyPort, // Defaults to 443 + localAddress, // Local interface if necessary + + // Basic authorization for proxy server if necessary + proxyAuth: 'user:password', + + // Header fields for proxy server if necessary + headers: { + 'User-Agent': 'Node' + }, + + // CA for proxy server if necessary + ca: [ fs.readFileSync('origin-server-ca.pem')], + + // Server name for verification if necessary + servername: 'example.com', + + // Client certification for proxy server if necessary + key: fs.readFileSync('origin-server-key.pem'), + cert: fs.readFileSync('origin-server-cert.pem'), + } + }); + + const req = https.request({ + host: 'example.com', + port: 443, + agent: tunnelingAgent + }); +})();