orator-http-proxy 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Launch Debug Harness",
9
+ "type": "pwa-node",
10
+ "request": "launch",
11
+ "outputCapture": "std",
12
+ "skipFiles": [
13
+ "<node_internals>/**"
14
+ ],
15
+ "program": "${workspaceFolder}/debug/Harness.js",
16
+ "presentation": {
17
+ "hidden": false,
18
+ "group": "",
19
+ "order": 1
20
+ }
21
+ },
22
+ {
23
+ "name": "Mocha Tests",
24
+ "args": [
25
+ "-u",
26
+ "tdd",
27
+ "--timeout",
28
+ "999999",
29
+ "--colors",
30
+ "${workspaceFolder}/test"
31
+ ],
32
+ "internalConsoleOptions": "openOnSessionStart",
33
+ "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
34
+ "request": "launch",
35
+ "skipFiles": [
36
+ "<node_internals>/**"
37
+ ],
38
+ "type": "pwa-node",
39
+ "presentation": {
40
+ "hidden": false,
41
+ "group": "",
42
+ "order": 2
43
+ }
44
+ }
45
+ ]
46
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Steven Velozo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Orator Service Server Proxy
2
+
3
+ Proxy API requests from a stable prefix. This lets you map /1.0/ to one set
4
+ of servers, /1.1/ to another and /1.0/CustomApi* to a third.
5
+
6
+ Meant to be used for local development and pass-through API for front-side
7
+ web servers, not an industrial-grade API routing service.
@@ -0,0 +1,62 @@
1
+ const libFable = require('fable');
2
+
3
+ const defaultFableSettings = (
4
+ {
5
+ Product:'Orator-Proxy',
6
+ ProductVersion: '1.0.0',
7
+ APIServerPort: 8765
8
+ });
9
+
10
+ // Initialize Fable
11
+ let _Fable = new libFable(defaultFableSettings);
12
+
13
+ // Now initialize the Restify ServiceServer Fable Service
14
+ _Fable.serviceManager.addServiceType('OratorServiceServer', require('orator-serviceserver-restify'));
15
+ _Fable.serviceManager.instantiateServiceProvider('OratorServiceServer',
16
+ {
17
+ RestifyConfiguration: { strictNext: true }
18
+ });
19
+
20
+ // Now add the orator service to Fable
21
+ _Fable.serviceManager.addServiceType('Orator', require('orator'));
22
+ let _Orator = _Fable.serviceManager.instantiateServiceProvider('Orator', {});
23
+
24
+ let tmpAnticipate = _Fable.newAnticipate();
25
+
26
+ // Initialize the Orator server
27
+ tmpAnticipate.anticipate(_Orator.initialize.bind(_Orator));
28
+
29
+ // Create a simple custom endpoint on the server.
30
+ tmpAnticipate.anticipate(
31
+ (fStageComplete)=>
32
+ {
33
+ // Create an endpoint. This can also be done after the service is started.
34
+ _Orator.serviceServer.get
35
+ (
36
+ '/test/:hash',
37
+ (pRequest, pResponse, fNext) =>
38
+ {
39
+ // Send back the request parameters
40
+ pResponse.send(pRequest.params);
41
+ _Orator.fable.log.info(`Endpoint sent parameters object:`, pRequest.params);
42
+ return fNext();
43
+ }
44
+ );
45
+ return fStageComplete();
46
+ });
47
+
48
+ // Proxy all /1.0/ requests to the locally-running bookstore service (you need to run this from https://github.com/stevenvelozo/retold-harness ... it's a one-liner to start the service)
49
+
50
+
51
+ // Now start the service server.
52
+ tmpAnticipate.anticipate(_Orator.startService.bind(_Orator));
53
+
54
+ tmpAnticipate.wait(
55
+ (pError)=>
56
+ {
57
+ if (pError)
58
+ {
59
+ _Fable.log.error('Error initializing Orator Service Server: '+pError.message, pError);
60
+ }
61
+ _Fable.log.info('Orator Service Server Initialized.');
62
+ });
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "orator-http-proxy",
3
+ "version": "1.0.0",
4
+ "description": "Orator http proxy pass-through.",
5
+ "main": "source/Orator-HTTP-Proxy.js",
6
+ "scripts": {
7
+ "test": "./node_modules/mocha/bin/_mocha --exit -u tdd -R spec",
8
+ "coverage": "./node_modules/.bin/nyc --reporter=lcov --reporter=text-lcov ./node_modules/mocha/bin/_mocha -- -u tdd -R spec",
9
+ "start": "node debug/Harness.js",
10
+ "tests": "npx mocha -u tdd --exit -R spec --grep"
11
+ },
12
+ "mocha": {
13
+ "diff": true,
14
+ "extension": [
15
+ "js"
16
+ ],
17
+ "package": "./package.json",
18
+ "reporter": "spec",
19
+ "slow": "75",
20
+ "timeout": "5000",
21
+ "ui": "tdd",
22
+ "watch-files": [
23
+ "source/**/*.js",
24
+ "test/**/*.js"
25
+ ],
26
+ "watch-ignore": [
27
+ "lib/vendor"
28
+ ]
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/stevenvelozo/orator-http-proxy.git"
33
+ },
34
+ "author": "steven velozo <steven@velozo.com>",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/stevenvelozo/orator-http-proxy/issues"
38
+ },
39
+ "homepage": "https://github.com/stevenvelozo/orator-http-proxy#readme",
40
+ "dependencies": {
41
+ "http-proxy": "^1.18.1",
42
+ "orator-serviceserver-base": "^1.0.1"
43
+ },
44
+ "devDependencies": {
45
+ "fable": "^3.0.145",
46
+ "orator": "^5.0.0",
47
+ "orator-serviceserver-restify": "^2.0.3",
48
+ "quackage": "^1.0.33"
49
+ }
50
+ }
@@ -0,0 +1,89 @@
1
+ const FableServiceProviderBase = require('fable-serviceproviderbase');
2
+
3
+ const HttpProxy = require('http-proxy');
4
+
5
+ /**
6
+ * Fable service that provies a simple proxy for an orator web server instance that redirects /1.0/* to
7
+ * a given URL (ex. a hosted Headlight API).
8
+ */
9
+ class HeadlightAPIProxyService extends FableServiceProviderBase
10
+ {
11
+ /**
12
+ * Construct a service instance.
13
+ *
14
+ * @param {object} pFable The fable instance for the application. Used for logging and settings.
15
+ * @param {object} pOptions Custom settings for this service instance.
16
+ * @param {string} pServiceHash The hash for this service instance.
17
+ *
18
+ * @return a HeadlightAPIProxyService instance.
19
+ */
20
+ constructor(pFable, pOptions, pServiceHash)
21
+ {
22
+ super(pFable, pOptions, pServiceHash);
23
+
24
+ if (typeof(this.options.proxyUrl) != 'string' || !this.options.proxyUrl.startsWith('http'))
25
+ {
26
+ this.log.trace('API proxy url falling back to settings...', { badUrl: this.options.proxyUrl });
27
+ this.options.proxyUrl = this.fable.settings.APIProxyUrl;
28
+ }
29
+
30
+ if (typeof(this.options.proxyUrl) != 'string' || !this.options.proxyUrl.startsWith('http'))
31
+ {
32
+ this.log.trace('API proxy url falling back to default...', { badUrl: this.options.proxyUrl });
33
+ this.options.proxyUrl = 'https://127.0.0.1/';
34
+ }
35
+
36
+ if (typeof(this.options.requestPrefix) != 'string' || !this.options.requestPrefix.startsWith('/'))
37
+ {
38
+ this.options.requestPrefix = '/1.0/*';
39
+ }
40
+
41
+ if (!Array.isArray(this.options.requestPrefixes) || this.options.requestPrefixes.length < 1)
42
+ {
43
+ this.options.requestPrefixes = [this.options.requestPrefix];
44
+ }
45
+
46
+ this.httpProxyServer = HttpProxy.createProxyServer({});
47
+ }
48
+
49
+ /**
50
+ * Create handlers for each HTTP verb on /1.0/* that proxy requests to the configured proxy URL.
51
+ */
52
+ connectProxyRoutes(pOrator)
53
+ {
54
+ const proxyRequest = (pRequest, pResponse, fNext) =>
55
+ {
56
+ this.log.info(`Proxying request from URI [${pRequest.url}] to [${this.options.proxyUrl}]`);
57
+ const options =
58
+ {
59
+ target: this.options.proxyUrl,
60
+ secure: false, // needed when proxying from HTTP to HTTPS
61
+ };
62
+ if (this.options.httpProxyOptions)
63
+ {
64
+ Object.assign(options, this.options.httpProxyOptions);
65
+ }
66
+ try
67
+ {
68
+ this.httpProxyServer.web(pRequest, pResponse, options);
69
+ }
70
+ catch (pError)
71
+ {
72
+ this.log.error(`Error proxying ${pRequest.url}: ${pError.message}`, { stack: pError.stack });
73
+ pResponse.end(` - ERROR: ${pError.message}`);
74
+ }
75
+ };
76
+
77
+ this.log.info('Adding API proxy to orator...', { proxyUrl: this.options.proxyUrl, requestPrefixes: this.options.requestPrefixes });
78
+
79
+ for (const requestPrefix of this.options.requestPrefixes)
80
+ {
81
+ pOrator.webServer.get(requestPrefix, proxyRequest);
82
+ pOrator.webServer.put(requestPrefix, proxyRequest);
83
+ pOrator.webServer.del(requestPrefix, proxyRequest);
84
+ pOrator.webServer.post(requestPrefix, proxyRequest);
85
+ }
86
+ }
87
+ }
88
+
89
+ module.exports = HeadlightAPIProxyService;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Unit tests for Orator
3
+ * @license MIT
4
+ * @author Steven Velozo <steven@velozo.com>
5
+ */
6
+
7
+ const Chai = require("chai");
8
+ const Expect = Chai.expect;
9
+ const Assert = Chai.assert;
10
+
11
+ const libFable = require('fable');
12
+ const libOrator = require('orator');
13
+ const libOratorHTTPProxy = require('../source/Orator-HTTP-Proxy.js');
14
+
15
+ //const libSuperTest = require('supertest');
16
+
17
+ suite
18
+ (
19
+ 'Orator Restify Abstraction',
20
+ () =>
21
+ {
22
+ suite
23
+ (
24
+ 'Object Sanity',
25
+ () =>
26
+ {
27
+ test
28
+ (
29
+ 'The class should initialize itself into a happy little object.',
30
+ function (fDone)
31
+ {
32
+ // Initialize fable
33
+ let tmpFable = new libFable();
34
+
35
+ // Add Restify as the default service server type
36
+ tmpFable.addServiceType('OratorHTTPProxy', libOratorHTTPProxy);
37
+
38
+ // We can safely create the service now if we want, or after Orator is created. As long as it's before we initialize orator.
39
+ let tmpOratorServiceServerRestify = tmpFable.instantiateServiceProvider('OratorHTTPProxy', {});
40
+
41
+ // Add Orator as a service
42
+ tmpFable.addServiceType('Orator', libOrator);
43
+
44
+ // Initialize the Orator service
45
+ let tmpOrator = tmpFable.instantiateServiceProvider('Orator', {});
46
+ // Sanity check Orator
47
+ Expect(tmpOrator).to.be.an('object', 'Orator should initialize as an object directly from the require statement.');
48
+
49
+ tmpOrator.initialize(
50
+ (pError)=>
51
+ {
52
+ Expect(tmpOrator.startService).to.be.an('function');
53
+
54
+ Expect(tmpOrator.serviceServer.ServiceServerType).to.equal('IPC', 'The service server provider should be Restify.');
55
+ fDone();
56
+ });
57
+ }
58
+ );
59
+ }
60
+ );
61
+ }
62
+ );