├── .travis.yml ├── lib └── prelude.js ├── .gitignore ├── test └── index.js ├── package.json ├── LICENSE.md ├── index.js └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'stable' 4 | - '0.12' 5 | - '0.10' 6 | sudo: false 7 | cache: 8 | directories: 9 | - node_modules 10 | -------------------------------------------------------------------------------- /lib/prelude.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const head = xs => xs[0]; 4 | const last = xs => xs[xs.length - 1]; 5 | 6 | exports.fromPairs = function fromPairs(xs) { 7 | return xs.reduce(function(obj, x) { 8 | if (x.length) { 9 | obj[head(x)] = last(x); 10 | } 11 | return obj; 12 | }, {}); 13 | }; 14 | 15 | exports.defaultTo = (defaultValue, value) => value || defaultValue; 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const test = require('tape'); 3 | const nock = require('nock'); 4 | const hapiProxyServer = require('../index'); 5 | 6 | const server = hapiProxyServer({ 7 | port: 9001, 8 | host: 'localhost', 9 | proxy: [{ 10 | method: 'GET', 11 | path: '/dummy', 12 | mapRequest: function(request) { 13 | return 'http://localhost:9000/api/dummy'; 14 | } 15 | }] 16 | }); 17 | 18 | test('should have port of 9001', function(t){ 19 | t.plan(1); 20 | t.ok(server.info.port, 9001); 21 | }); 22 | 23 | test('should have host of localhost', function(t){ 24 | t.plan(1); 25 | t.ok(server.info.port, 'localhost'); 26 | }); 27 | 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapi-proxy-server", 3 | "description": "A simple h2o2 wrapper", 4 | "version": "1.1.0", 5 | "author": "David Chase ", 6 | "bugs": { 7 | "url": "https://github.com/davidchase/hapi-proxy-server/issues" 8 | }, 9 | "devDependencies": { 10 | "nock": "8.0.0", 11 | "tap-spec": "^4.0.2", 12 | "tape": "^4.0.0" 13 | }, 14 | "homepage": "https://github.com/davidchase/hapi-proxy-server", 15 | "keywords": [ 16 | "h2o2", 17 | "hapi", 18 | "proxy" 19 | ], 20 | "license": "MIT", 21 | "main": "index.js", 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/davidchase/hapi-proxy-server.git" 25 | }, 26 | "scripts": { 27 | "test": "tape test/*.js | tap-spec" 28 | }, 29 | "dependencies": { 30 | "h2o2": "5.1.0", 31 | "hapi": "13.3.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # [MIT License](https://spdx.org/licenses/MIT) 2 | 3 | Copyright (c) 2016 David Chase 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const Hapi = require('hapi'); 3 | const h2o2 = require('h2o2'); 4 | const fromPairs = require('./lib/prelude').fromPairs; 5 | const defaultTo = require('./lib/prelude').defaultTo; 6 | 7 | const defaults = { 8 | host: 'localhost', 9 | port: 0, 10 | proxy: [] 11 | }; 12 | 13 | const buildOptions = opts => opts.proxy.map(proxy => ({ 14 | method: proxy.method, 15 | path: proxy.path, 16 | handler: { 17 | proxy: { 18 | passThrough: defaultTo(true, proxy.passThrough), 19 | mapUri: function(request, callback) { 20 | const proxiedURI = proxy.mapRequest(request); 21 | callback(null, proxiedURI, fromPairs(defaultTo([], proxy.headers))); 22 | } 23 | } 24 | } 25 | })); 26 | 27 | const startServer = function startServer(opts, routeOpts) { 28 | const server = new Hapi.Server(); 29 | 30 | server.connection({ 31 | host: opts.host, 32 | port: opts.port 33 | }); 34 | 35 | server.register({ 36 | register: h2o2 37 | }, 38 | function startProxy(err) { 39 | if (err) { 40 | throw err; 41 | } 42 | server.start(() => console.log('server started at:', server.info.uri)); 43 | }); 44 | 45 | server.route(routeOpts); 46 | return server; 47 | }; 48 | 49 | module.exports = function hapiProxyServer(config) { 50 | const opts = Object.assign({}, defaults, config); 51 | const routeOpts = buildOptions(opts); 52 | 53 | return startServer(opts, routeOpts); 54 | }; 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hapi-proxy-server 2 | 3 | [![npm][npm-image]][npm-url] 4 | [![travis][travis-image]][travis-url] 5 | 6 | [npm-image]: https://img.shields.io/npm/v/hapi-proxy-server.svg?style=flat-square 7 | [npm-url]: https://www.npmjs.com/package/hapi-proxy-server 8 | [travis-image]: https://img.shields.io/travis/davidchase/hapi-proxy-server.svg?style=flat-square 9 | [travis-url]: https://travis-ci.org/davidchase/hapi-proxy-server 10 | 11 | A simple h2o2 wrapper 12 | 13 | ## Install 14 | 15 | ``` 16 | npm install hapi-proxy-server 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```js 22 | const hapiProxyServer = require('hapi-proxy-server'); 23 | 24 | const config = { 25 | port: 9001, 26 | host: 'localhost', 27 | proxy: [{ 28 | method: 'GET', 29 | path: '/path/{p*}', 30 | passThrough: true, 31 | headers: [ 32 | ['Content-Encoding', 'gzip'], 33 | ['Content-Type', 'text/html charset=utf-8'] // 2d Arrays allow for dynamic key values 34 | ], 35 | mapRequest: function(request) { 36 | return request; // return proxy path 37 | } 38 | },{ 39 | method: '*', 40 | path: '/path/{p*}', 41 | passThrough: true, 42 | headers: [ 43 | ['Content-Encoding', 'gzip'], 44 | ['Content-Type', 'text/html charset=utf-8'] 45 | ], 46 | mapRequest: function(request) { 47 | return request; 48 | } 49 | }] 50 | }; 51 | 52 | hapiProxyServer(config); // will create proxy hapi server at localhost:9001 53 | ``` 54 | 55 | ### Min config 56 | 57 | ```js 58 | const config = { 59 | proxy: [{ 60 | method: 'GET', 61 | path: '/path/{p*}', 62 | mapRequest: function(request) { 63 | return request; // return proxy path 64 | } 65 | }] 66 | } 67 | 68 | hapiProxyServer(config); // will create proxy hapi server at localhost and a random available port 69 | ``` 70 | 71 | ## API 72 | 73 | `passThrough` a boolean to allow headers from the client to pass onto the proxied endpoint (default true). 74 | 75 | `headers` as a list `[[key, values]]` pairs to pass on to the proxy. 76 | 77 | `mapRequest` a `function (request)` that takes a request and needs to return the absolute uri. 78 | 79 | `port` a port to listen on for the server (default 0, random). 80 | 81 | `host` proxy host name (default localhost). 82 | 83 | `path` path of the proxy route. 84 | 85 | `method` method name to use on the proxy route. 86 | 87 | ## Todo 88 | 89 | - [ ] Add more options 90 | - [ ] Publish to npm 91 | - [x] Add tests 92 | 93 | ## License 94 | 95 | [MIT](LICENSE.md) 96 | --------------------------------------------------------------------------------