5.0.29 • Published 2 months ago

@podium/proxy v5.0.29

Weekly downloads
763
License
MIT
Repository
github
Last release
2 months ago

@podium/proxy v5

Transparent HTTP proxy. Dynamically mounts proxy targets on an existing HTTP server instance.

Dependencies GitHub Actions status Known Vulnerabilities

This module is intended for internal use in Podium and is not a module an end user would use directly. End users will typically interact with this module through higher level modules such as the @podium/layout and @podium/podlet modules.

Installation

$ npm install @podium/proxy

Simple usage

Attach a proxy target to an HTTP server.

import { HttpIncoming } from '@podium/utils';
import Proxy from '@podium/proxy';
import http from 'http';

// Set up proxy
const proxy = new Proxy();

// Register remote target(s) on separate namespace
proxy.register({
    name: 'bar',
    proxy: {
        api: 'http://www.external.com/some/path',
    },
    version: '1.0.0',
    content: '/bar',
});

// Attach proxy to http server
const app = http.createServer(async (req, res) => {
    const incoming = new HttpIncoming(req, res);
    const result = await proxy.process(incoming);

    // The proxy did return "undefined" so nothing matched our proxy
    if (!result) {
        res.statusCode = 404;
        res.end('404 - Not found');
    }
});

// Start appserver where proxy is attached
app.listen(9999);

Proxy is now mounted on: http://localhost:9999/podium-resource/bar/api

Constructor

Create a new Proxy instance.

import Proxy from '@podium/proxy';
const proxy = new Proxy(options);

The constructor takes the following arguments:

options (optional)

An options object containing configuration. The following values can be provided:

  • 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
  • logger - {Object} - A logger which conforms to the log4j interface. See the docs for abstract logger for more information.

API

The Proxy instance havs the following API:

.register(name, manifest)

Registers proxy target(s) by providing a Podium manifest.

Example:

import Proxy from '@podium/proxy';

// Set up proxy
const proxy = new Proxy();

// Register remote target(s) on separate namespace
proxy.register('podlet-name-in-layout', {
    name: 'bar',
    proxy: {
        api: 'http://www.external.com/some/path',
    },
    version: '1.0.0',
    content: '/bar',
});

name (required)

Name for the registered podlet. This will be part of the URL to distinguish the podlets proxy endpoints in the URL space.

manifest (required)

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.

.process(HttpIncoming)

Metod for processing a incoming HTTP request. Matches the request against the registered routing targets and proxies the request if a match is found.

Returns a promise. When resolving the passed in HttpIncoming object will be returned.

If the inbound request matches a proxy endpoint and a proxy request was successfully performed, the .proxy property on the returned HttpIncoming object will be true. If the inbound request did not yeld a proxy request, the .proxy property on the returned HttpIncoming object will be false.

The method takes the following arguments:

HttpIncoming (required)

An instance of an HttpIncoming class.

import { HttpIncoming } from '@podium/utils';
import Proxy from '@podium/proxy';
import http from 'http';

const proxy = new Proxy();

proxy.register({ ...[snip]... });

const app = http.createServer(async (req, res) => {
    const incoming = new HttpIncoming(req, res);
    const result = await proxy.process(incoming);

    if (result.proxy) return;

    res.statusCode = 404;
    res.end('404 - Not found');
});

.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 module for full documentation.

.dump()

Returns an Array of all loaded manifests ready to be used by .load().

.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 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.

Returns an Array with the keys which were inserted into the cache.

Where are proxy targets mounted?

To be able to have multiple proxy targets in an HTTP server we need to make sure that they do not collide with each other. To prevent this, each proxy target defined is mounted on its own separate namespace in an HTTP server.

The convention for these namespaces is as follow:

{pathname}/{prefix}/{podletName}/{proxyName}/

  • 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 has the following manifest defined in an express server:

import { HttpIncoming } from '@podium/utils';
import Proxy from '@podium/proxy';
import http from 'http';

const proxy = new Proxy();

proxy.register({
    name: 'bar',
    proxy: {
        api: 'http://www.external.com/some/path',
    },
    version: '1.0.0',
    content: '/index.html',
});

const app = http.createServer(async (req, res) => {
    ...[snip]...
});

app.listen(8000);

The following proxy targets will be mounted:

Example II

If one has the following manifest and overrides the prefix on the constructor:

import { HttpIncoming } from '@podium/utils';
import Proxy from '@podium/proxy';
import http from 'http';

const proxy = new Proxy({
    prefix: '/my-proxy',
});

proxy.register({
    name: 'bar',
    proxy: {
        api: 'http://www.external.com/some/path',
    },
    version: '1.0.0',
    content: '/index.html',
});

const app = http.createServer(async (req, res) => {
    ...[snip]...
});

app.listen(8000);

The following proxy targets will be mounted:

Example III

If one has the following manifest defined in an express server:

import { HttpIncoming } from '@podium/utils';
import Proxy from '@podium/proxy';
import http from 'http';

const proxy = new Proxy();

proxy.register({
    name: 'bar',
    proxy: {
        api: 'http://www.external.com/some/path',
        feed: '/feed',
    },
    version: '1.0.0',
    content: '/index.html',
});

const app = http.createServer(async (req, res) => {
    ...[snip]...
});

app.listen(8000);

The following proxy targets will be mounted:

Example IV

If one has the following manifests defined in an express server:

import { HttpIncoming } from '@podium/utils';
import Proxy from '@podium/proxy';
import http from 'http';

const proxy = new Proxy();

proxy.register({
    name: 'bar',
    proxy: {
        api: 'http://www.external.com/some/path',
        feed: '/feed',
    },
    version: '1.0.0',
    content: '/index.html',
});

proxy.register({
    name: 'foo',
    proxy: {
        users: 'http://www.anywhere.com/api',
    },
    version: '2.0.0',
    content: '/index.html',
});

const app = http.createServer(async (req, res) => {
    ...[snip]...
});

app.listen(8000);

The following proxy targets will be mounted:

License

Copyright (c) 2019 FINN.no

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

5.0.29

2 months ago

5.0.28

3 months ago

5.0.27

3 months ago

5.0.26

3 months ago

5.0.25

4 months ago

5.0.20

7 months ago

5.0.21

6 months ago

5.0.22

5 months ago

5.0.23

5 months ago

5.0.24

4 months ago

5.0.18

7 months ago

5.0.19

7 months ago

5.0.17

7 months ago

5.0.15

7 months ago

5.0.16

7 months ago

5.0.14

7 months ago

5.0.13

8 months ago

5.0.12

8 months ago

5.0.10

8 months ago

5.0.11

8 months ago

5.0.9

8 months ago

5.0.8

9 months ago

5.0.7

9 months ago

5.0.6

9 months ago

5.0.5

9 months ago

5.0.4

10 months ago

5.0.3

11 months ago

5.0.2

1 year ago

5.0.1

1 year ago

4.2.81

1 year ago

4.2.86

1 year ago

4.2.82

1 year ago

4.2.83

1 year ago

4.2.84

1 year ago

4.2.85

1 year ago

5.0.0

1 year ago

5.0.0-next.8

1 year ago

4.2.80

2 years ago

4.2.78

2 years ago

4.2.79

2 years ago

4.2.76

2 years ago

4.2.77

2 years ago

5.0.0-next.7

2 years ago

4.2.75

3 years ago

5.0.0-next.6

3 years ago

4.2.73

3 years ago

4.2.74

3 years ago

4.2.70

3 years ago

4.2.71

3 years ago

4.2.72

3 years ago

4.2.68

3 years ago

4.2.69

3 years ago

4.2.64

3 years ago

4.2.65

3 years ago

4.2.66

3 years ago

4.2.67

3 years ago

4.2.60

3 years ago

4.2.61

3 years ago

4.2.62

3 years ago

4.2.63

3 years ago

4.2.59

3 years ago

4.2.58

3 years ago

4.2.56

3 years ago

4.2.57

3 years ago

4.2.54

3 years ago

4.2.55

3 years ago

4.2.53

3 years ago

4.2.52

3 years ago

4.2.51

3 years ago

4.2.50

3 years ago

4.2.48

4 years ago

4.2.49

4 years ago

4.2.42

4 years ago

4.2.43

4 years ago

4.2.44

4 years ago

4.2.45

4 years ago

4.2.40

4 years ago

4.2.41

4 years ago

4.2.46

4 years ago

4.2.47

4 years ago

4.2.39

4 years ago

4.2.38

4 years ago

5.0.0-next.5

4 years ago

4.2.36

4 years ago

4.2.37

4 years ago

4.2.33

4 years ago

4.2.34

4 years ago

4.2.35

4 years ago

4.2.32

4 years ago

4.2.31

4 years ago

4.2.30

4 years ago

4.2.29

4 years ago

4.2.28

4 years ago

4.2.27

4 years ago

4.2.25

4 years ago

4.2.26

4 years ago

4.2.23

4 years ago

4.2.24

4 years ago

4.2.21

4 years ago

4.2.22

4 years ago

4.2.20

4 years ago

4.2.19

4 years ago

4.2.18

4 years ago

4.2.17

4 years ago

4.2.16

4 years ago

4.2.15

4 years ago

4.2.14

4 years ago

4.2.13

4 years ago

4.2.12

4 years ago

4.2.11

4 years ago

4.2.9

4 years ago

4.2.10

4 years ago

4.2.8

4 years ago

4.2.7

4 years ago

4.2.6

4 years ago

4.2.5

4 years ago

4.2.4

4 years ago

4.2.3

4 years ago

4.2.2

4 years ago

5.0.0-next.4

4 years ago

5.0.0-next.3

4 years ago

5.0.0-next.2

4 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.5

5 years ago

4.1.4

5 years ago

4.1.3

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.6

5 years ago

4.0.5

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

6 years ago

4.0.0-next.3

6 years ago

4.0.0-next.2

6 years ago

4.0.0-next.1

6 years ago

4.0.0-next

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

3.0.0-beta.5

6 years ago

3.0.0-beta.4

6 years ago

3.0.0-beta.3

6 years ago

3.0.0-beta.2

6 years ago

2.5.0

6 years ago

2.2.0

6 years ago

2.4.4

6 years ago

2.4.3

6 years ago

3.0.0-beta.1

6 years ago