@podium/proxy
Advanced tools
Comparing version 3.0.0-beta.1 to 3.0.0-beta.2
@@ -5,2 +5,3 @@ 'use strict'; | ||
const schemas = require('@podium/schemas'); | ||
const Metrics = require('@metrics/client'); | ||
const { URL } = require('url'); | ||
@@ -63,2 +64,7 @@ const abslog = require('abslog'); | ||
Object.defineProperty(this, 'metrics', { | ||
enumerable: true, | ||
value: new Metrics(), | ||
}); | ||
Object.defineProperty(this, 'pathnameEntries', { | ||
@@ -83,8 +89,20 @@ value: [], | ||
this.proxy.on('proxyReq', () => { | ||
// this.log.debug('proxy request started', req.path, req.method, req); | ||
}); | ||
this.proxy.on('proxyReq', (proxyReq, req, res) => { | ||
const end = this.metrics.timer({ | ||
name: 'podium_proxy_request', | ||
description: | ||
'Proxy requests through a layout to target determined by podlet manifest', | ||
/* | ||
TODO: we can not count like this due to not having res.locals | ||
when stuff is framework free | ||
meta: { | ||
podlet: res.locals.podlet, | ||
proxy: res.locals.proxy, | ||
}, | ||
*/ | ||
}); | ||
this.proxy.on('proxyRes', () => { | ||
// this.log.debug('proxy request got response', res); | ||
res.on('finish', () => { | ||
end(); | ||
}); | ||
}); | ||
@@ -133,3 +151,16 @@ | ||
process(state) { | ||
process(req, res) { | ||
// TODO: replace with a instance check when there is one PodiumState | ||
// class used by multiple modules. | ||
let state; | ||
if (Object.prototype.toString.call(req) === '[object PodiumState]') { | ||
state = req; | ||
} else { | ||
state = new State(req, res); | ||
} | ||
// TODO: Only needed to support 2.x version of Layout. | ||
// Remove when layout is http framework free. | ||
state.context = utils.getFromLocalsPodium(res, 'context'); | ||
return new Promise((resolve, reject) => { | ||
@@ -168,5 +199,8 @@ const match = this.pathnameParser.exec(state.url.pathname); | ||
if (utils.uriIsRelative(target)) { | ||
target = `${state.url.origin}${utils.pathnameBuilder( | ||
target = utils.uriBuilder( | ||
target, | ||
)}`; | ||
`${state.url.protocol}//${state.url.hostname}${ | ||
state.url.port ? ':' : '' | ||
}${state.url.port}`, | ||
); | ||
} | ||
@@ -177,3 +211,3 @@ | ||
if (params.podiumProxyExtras) { | ||
target += utils.pathnameBuilder(params.podiumProxyExtras); | ||
target = utils.uriBuilder(params.podiumProxyExtras, target); | ||
} | ||
@@ -188,5 +222,4 @@ | ||
// Append context | ||
const request = state.getRequest(); | ||
utils.serializeContext( | ||
request.headers, | ||
state.request.headers, | ||
state.context, | ||
@@ -209,3 +242,2 @@ params.podiumPodletName, | ||
const response = state.getResponse(); | ||
state.response.on('finish', () => { | ||
@@ -215,3 +247,3 @@ resolve(); | ||
this.proxy.web(request, response, config, error => { | ||
this.proxy.web(state.request, state.response, config, error => { | ||
reject(error); | ||
@@ -228,12 +260,10 @@ }); | ||
return (req, res, next) => { | ||
const state = new State(req, res); | ||
state.context = utils.getFromLocalsPodium(res, 'context'); | ||
this.process(state) | ||
.then(stat => { | ||
if (stat) { | ||
this.process(req, res) | ||
.then(state => { | ||
if (state) { | ||
next(); | ||
} | ||
}) | ||
.catch(err => { | ||
next(err); | ||
.catch(error => { | ||
next(error); | ||
}); | ||
@@ -240,0 +270,0 @@ }; |
@@ -6,10 +6,22 @@ 'use strict'; | ||
const noop = str => str; | ||
const PodiumState = class PodiumState { | ||
constructor(request = {}, response = {}, view = '') { | ||
constructor(request = {}, response = {}) { | ||
Object.defineProperty(this, 'request', { | ||
value: request, | ||
set() { | ||
throw new Error('Cannot set read-only property.'); | ||
}, | ||
get() { | ||
return request; | ||
}, | ||
}); | ||
Object.defineProperty(this, 'response', { | ||
value: response, | ||
set() { | ||
throw new Error('Cannot set read-only property.'); | ||
}, | ||
get() { | ||
return response; | ||
}, | ||
}); | ||
@@ -22,4 +34,10 @@ | ||
let view = noop; | ||
Object.defineProperty(this, 'view', { | ||
value: view, | ||
set(value) { | ||
view = value; | ||
}, | ||
get() { | ||
return view; | ||
}, | ||
}); | ||
@@ -62,10 +80,2 @@ | ||
getRequest() { | ||
return this.request; | ||
} | ||
getResponse() { | ||
return this.response; | ||
} | ||
render(fragment) { | ||
@@ -72,0 +82,0 @@ if (this.development) { |
114
package.json
{ | ||
"name": "@podium/proxy", | ||
"version": "3.0.0-beta.1", | ||
"license": "MIT", | ||
"description": "Transparent http proxy for Podium. Does dynamically mount proxy targets on a existing http server instance.", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.schibsted.io:Podium/proxy.git" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"main": "./lib/proxy.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test": "jest", | ||
"test:verbose": "jest --verbose", | ||
"test:coverage": "jest --coverage" | ||
}, | ||
"dependencies": { | ||
"@podium/schemas": "3.0.0-beta.1", | ||
"@podium/utils": "3.0.0-beta.1", | ||
"abslog": "2.2.2", | ||
"http-proxy": "^1.17.0", | ||
"joi": "^14.0.1", | ||
"original-url": "^1.2.2", | ||
"path-to-regexp": "^2.4.0", | ||
"ttl-mem-cache": "4.0.2" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.6.1", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^3.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"jest": "^23.6.0", | ||
"prettier": "^1.14.2", | ||
"express": "^4.16.4" | ||
}, | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"test/" | ||
"name": "@podium/proxy", | ||
"version": "3.0.0-beta.2", | ||
"description": "Transparent http proxy. Dynamically mounts proxy targets on an existing HTTP server instance.", | ||
"license": "MIT", | ||
"keywords": [ | ||
"micro services", | ||
"micro frontend", | ||
"components", | ||
"podium", | ||
"proxy" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 60, | ||
"functions": 60, | ||
"lines": 60, | ||
"statements": 60 | ||
} | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:podium-lib/proxy.git" | ||
}, | ||
"testEnvironment": "node" | ||
} | ||
"bugs": { | ||
"url": "https://github.com/podium-lib/issues" | ||
}, | ||
"homepage": "https://podium-lib.io/", | ||
"files": [ | ||
"lib" | ||
], | ||
"main": "./lib/proxy.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test": "jest", | ||
"test:verbose": "jest --verbose", | ||
"test:coverage": "jest --coverage" | ||
}, | ||
"dependencies": { | ||
"@metrics/client": "2.3.0", | ||
"@podium/schemas": "3.0.0", | ||
"@podium/utils": "3.0.0", | ||
"abslog": "2.2.3", | ||
"http-proxy": "1.17.0", | ||
"joi": "14.3.1", | ||
"original-url": "1.2.2", | ||
"path-to-regexp": "3.0.0", | ||
"ttl-mem-cache": "4.0.2" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.6.1", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^4.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"jest": "^24.0.0", | ||
"prettier": "^1.14.2", | ||
"readable-stream": "^3.1.1" | ||
}, | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"test/" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 60, | ||
"functions": 60, | ||
"lines": 60, | ||
"statements": 60 | ||
} | ||
}, | ||
"testEnvironment": "node" | ||
} | ||
} |
# @podium/proxy | ||
[![Build Status](https://travis.schibsted.io/Podium/proxy.svg?token=qt273uGfEz64UyWuNHJ1&branch=master)](https://travis.schibsted.io/Podium/proxy) | ||
Transparent http proxy. Dynamically mounts proxy targets on an existing HTTP server instance. | ||
Transparent http proxy. Does dynamically mount proxy targets on a | ||
existing http server instance. | ||
[![Build Status](https://travis-ci.org/podium-lib/proxy.svg?branch=master)](https://travis-ci.org/podium-lib/proxy) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/podium-lib/proxy.svg)](https://greenkeeper.io/) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/podium-lib/proxy/badge.svg)](https://snyk.io/test/github/podium-lib/proxy) | ||
## Installation | ||
```bash | ||
$ npm i @podium/proxy | ||
$ npm install @podium/proxy | ||
``` | ||
## Simple usage | ||
@@ -50,3 +49,2 @@ | ||
## Constructor | ||
@@ -61,3 +59,3 @@ | ||
The constructor take the following arguments: | ||
The constructor takes the following arguments: | ||
@@ -68,13 +66,12 @@ ### options (optional) | ||
* `pathname` - {String} - Pathname to the root of where the proxy is mounted. Default: `/`. | ||
* `prefix` - {String} - Prefix used to namespace the proxy so its isolated from other routes in a http server. Appended after pathname. Default: `podium-resource`. | ||
* `timeout` - {Number} - Default value, in milliseconds, for how long a request should wait before connection is terminated. Default: 6000 | ||
* `maxAge` - {Number} - Default value, in milliseconds, for how long manifests should be cached. Default: Infinity | ||
* `agent` - {HTTPAgent} - Default HTTP Agent used for all requests. | ||
* `logger` - {Object} - A logger which conform to a log4j interface. See the doc for the internal [abstract logger](https://www.npmjs.com/package/abslog) for more information. | ||
- `pathname` - {String} - Pathname to the root of where the proxy is to be mounted. Default: `/`. | ||
- `prefix` - {String} - Prefix used to namespace the proxy so its isolated from other routes in a HTTP server. Appended after pathname. Default: `podium-resource`. | ||
- `timeout` - {Number} - Default value, in milliseconds, for how long a request should wait before the connection is terminated. Default: 6000 | ||
- `maxAge` - {Number} - Default value, in milliseconds, for how long manifests should be cached. Default: Infinity | ||
- `agent` - {HTTPAgent} - Default HTTP Agent used for all requests. | ||
- `logger` - {Object} - A logger which conforms to the log4j interface. See the docs for [abstract logger](https://www.npmjs.com/package/abslog) for more information. | ||
## API | ||
The Proxy instance have the following API: | ||
The Proxy instance havs the following API: | ||
@@ -106,12 +103,17 @@ ### .register(manifest) | ||
A Podium manifest where the `proxy` property is given. The `proxy` | ||
property is an object where the `key` identifies the target and the `property` is a URI to the target. | ||
A Podium manifest where the `proxy` property is given. The `proxy` property is an object where the `key` identifies the target and the `property` is a URI to the target. | ||
### .metrics | ||
Property that exposes a metric stream. | ||
Exposes a single metric called `podium_proxy_request` which includes `podlet` and `proxy` meta fields. | ||
Please see the [@metrics/client](https://www.npmjs.com/package/@metrics/client) module for full documentation. | ||
### .middleware() | ||
Middleware that mounts the proxy on an Connect middleware compatible | ||
http server. | ||
Middleware that mounts the proxy on a Connect middleware compatible | ||
HTTP server. | ||
### .dump() | ||
@@ -121,33 +123,26 @@ | ||
### .load() | ||
Loads an Array of manifests, provided by `.dump()`, into the proxy. If any of | ||
the items in the loaded Array contains a key which already are in the cache | ||
the entry in the cache will be overwritten. | ||
Loads an Array of manifests (provided by `.dump()`) into the proxy. If any of the items in the loaded Array contains a key which is already in the cache, the entry in the cache will be overwritten. | ||
If any of the entries in the loaded Array are not compatible with the format | ||
which `.dump()` exports, they will not be inserted into the cache. | ||
If any of the entries in the loaded Array are not compatible with the format which `.dump()` exports, they will not be inserted into the cache. | ||
Returns and Array with the keys which was inserted into the cache. | ||
Returns an Array with the keys which were inserted into the cache. | ||
## Where are proxy targets mounted? | ||
To be able to have multible proxy targets in a http server we need to make | ||
sure that they do not collide with each other. To prevent so, each proxy | ||
target defined is mounted on their own separate namespace in a http server. | ||
To be able to have multible proxy targets in an HTTP server we need to make sure that they do not collide with each other. To prevent so, each proxy target defined is mounted on their own separate namespace in an HTTP server. | ||
The convention for these namespaces are as follow: | ||
The convention for these namespaces is as follow: | ||
`{pathname}/{prefix}/{podletName}/{proxyName}/` | ||
* pathname - Defined by the value given to the `pathname` argument on the constructor. Defaults to `/`. | ||
* prefix - Defined by the value given to the `prefix` argument on the constructor. Defaults to `podium-resource`. | ||
* podletName - Defined by the value given to `name` in the manifest. Note: When the proxy module subscribe on manifests from the Podium Client, this name will be the name a Podlet is registered with in the Podium Client. | ||
* proxyName - Defined by the property on the object on the `proxy` property for the target in the manifest. | ||
- pathname - Defined by the `pathname` argument in the constructor. Defaults to `/`. | ||
- prefix - Defined by `prefix` argument in the constructor. Defaults to `podium-resource`. | ||
- podletName - Defined by the `name` value in the manifest. Note: When the proxy module subscribes to receive manifest updates from the Podium Client, this name will be the name a Podlet is registered with in the Podium Client. | ||
- proxyName - Defined by the `proxy.name` property defined in the manifest. | ||
### Example I | ||
If one have the following manifest appended to an express server: | ||
If one has the following manifest defined in an express server: | ||
@@ -175,7 +170,7 @@ ```js | ||
* http://localhost:8000/podium-resource/bar/api/ | ||
- http://localhost:8000/podium-resource/bar/api/ | ||
### Example II | ||
If one have the following manifest and override the `prefix` on the constructor: | ||
If one has the following manifest and overrides the `prefix` on the constructor: | ||
@@ -186,3 +181,3 @@ ```js | ||
const proxy = new Proxy({ | ||
prefix: '/my-proxy' | ||
prefix: '/my-proxy', | ||
}); | ||
@@ -206,7 +201,7 @@ | ||
* http://localhost:8000/my-proxy/bar/api/ | ||
- http://localhost:8000/my-proxy/bar/api/ | ||
### Example III | ||
If one have the following manifest appended to an express server: | ||
If one has the following manifest defined in an express server: | ||
@@ -235,8 +230,8 @@ ```js | ||
* http://localhost:8000/podium-resource/bar/api/ | ||
* http://localhost:8000/podium-resource/bar/feed/ | ||
- http://localhost:8000/podium-resource/bar/api/ | ||
- http://localhost:8000/podium-resource/bar/feed/ | ||
### Example IV | ||
If one have the following manifests appended to an express server: | ||
If one has the following manifests defined in an express server: | ||
@@ -274,9 +269,4 @@ ```js | ||
* http://localhost:8000/podium-resource/bar/api/ | ||
* http://localhost:8000/podium-resource/bar/feed/ | ||
* http://localhost:8000/podium-resource/foo/users/ | ||
## A word on appending Podium context | ||
TODO | ||
- http://localhost:8000/podium-resource/bar/api/ | ||
- http://localhost:8000/podium-resource/bar/feed/ | ||
- http://localhost:8000/podium-resource/foo/users/ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
20202
305
1
1
9
263
+ Added@metrics/client@2.3.0
+ Added@metrics/client@2.3.0(transitive)
+ Added@metrics/metric@2.3.3(transitive)
+ Added@podium/schemas@3.0.0(transitive)
+ Added@podium/utils@3.0.0(transitive)
+ Addedabslog@2.2.3(transitive)
+ Addedconvert-hrtime@2.0.0(transitive)
+ Addedeventemitter3@3.1.2(transitive)
+ Addedhttp-proxy@1.17.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedoriginal-url@1.2.2(transitive)
+ Addedpath-to-regexp@3.0.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtime-span@2.0.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removed@podium/schemas@3.0.0-beta.1(transitive)
- Removed@podium/utils@3.0.0-beta.1(transitive)
- Removedabslog@2.2.2(transitive)
- Removedeventemitter3@4.0.7(transitive)
- Removedhoek@5.0.4(transitive)
- Removedhttp-proxy@1.18.1(transitive)
- Removedjoi@14.0.1(transitive)
- Removedoriginal-url@1.2.3(transitive)
- Removedpath-to-regexp@2.4.0(transitive)
Updated@podium/schemas@3.0.0
Updated@podium/utils@3.0.0
Updatedabslog@2.2.3
Updatedhttp-proxy@1.17.0
Updatedjoi@14.3.1
Updatedoriginal-url@1.2.2
Updatedpath-to-regexp@3.0.0