orator-static-server 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,5 @@
1
+ # Orator Static File Server
2
+
3
+ Very basic static file server for local development and lightweight tasks.
4
+
5
+ Leverages restify static server.
@@ -0,0 +1,72 @@
1
+ const libFable = require('fable');
2
+
3
+ const defaultFableSettings = (
4
+ {
5
+ Product:'Orator-Static',
6
+ ProductVersion: '1.0.0',
7
+ APIServerPort: 8766
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
+ // Add the static server service
30
+ const libOratorServeStatic = require(`../source/Orator-Static-Server.js`);
31
+ _Fable.serviceManager.addServiceType('OratorServeStatic', libOratorServeStatic);
32
+ _Fable.serviceManager.instantiateServiceProvider('OratorServeStatic');
33
+
34
+ // Create a simple custom endpoint on the server.
35
+ tmpAnticipate.anticipate(
36
+ (fNext)=>
37
+ {
38
+ // Create an endpoint. This can also be done after the service is started.
39
+ _Orator.serviceServer.get
40
+ (
41
+ '/test/:hash',
42
+ (pRequest, pResponse, fNext) =>
43
+ {
44
+ // Send back the request parameters
45
+ pResponse.send(pRequest.params);
46
+ _Orator.fable.log.info(`Endpoint sent parameters object:`, pRequest.params);
47
+ return fNext();
48
+ }
49
+ );
50
+ return fNext();
51
+ });
52
+
53
+ // Map the ./serve/ folder to the root of the server.
54
+ tmpAnticipate.anticipate(
55
+ (fNext)=>
56
+ {
57
+ _Fable.serviceManager.OratorServeStatic.addStaticRoute(_Orator, `${__dirname}/serve/`, 'index.html');
58
+ return fNext();
59
+ });
60
+
61
+ // Now start the service server.
62
+ tmpAnticipate.anticipate(_Orator.startService.bind(_Orator));
63
+
64
+ tmpAnticipate.wait(
65
+ (pError)=>
66
+ {
67
+ if (pError)
68
+ {
69
+ _Fable.log.error('Error initializing Orator Service Server: '+pError.message, pError);
70
+ }
71
+ _Fable.log.info('Orator Service Server Initialized.');
72
+ });
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head>
3
+ <title>Debug</title>
4
+ </head>
5
+ <body>
6
+ <h1>Debug</h1>
7
+ <p>Debugging is the process of finding and resolving defects or problems within a computer program that prevent correct operation of computer software or a system.</p>
8
+ </body>
9
+ </html>
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "orator-static-server",
3
+ "version": "1.0.0",
4
+ "description": "Orator static file server.",
5
+ "main": "source/Orator-Static-Server.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-static-server.git"
33
+ },
34
+ "author": "steven velozo <steven@velozo.com>",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/stevenvelozo/orator-static-server/issues"
38
+ },
39
+ "homepage": "https://github.com/stevenvelozo/orator-static-server#readme",
40
+ "dependencies": {
41
+ "finalhandler": "^1.3.1",
42
+ "orator-serviceserver-base": "^1.0.1",
43
+ "serve-static": "^1.16.2"
44
+ },
45
+ "devDependencies": {
46
+ "fable": "^3.0.145",
47
+ "orator": "^5.0.0",
48
+ "orator-serviceserver-restify": "^2.0.3",
49
+ "quackage": "^1.0.33"
50
+ }
51
+ }
@@ -0,0 +1,98 @@
1
+ const FableServiceProviderBase = require('fable-serviceproviderbase');
2
+
3
+ const libServeStatic = require('serve-static');
4
+ const libFinalHandler = require('finalhandler');
5
+
6
+ /**
7
+ * Fable service that provides a simple static file server.
8
+ */
9
+ class OratorStaticFileService 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 static file service 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
+
31
+
32
+ /**
33
+ * Brought over from old orator and ported to work in the same way.
34
+ *
35
+ * @param {object} pOrator The Orator instance.
36
+ * @param {string} pFilePath The path on disk that we are serving files from.
37
+ * @param {string?} pDefaultFile (optional) The default file served if no specific file is requested.
38
+ * @param {string?} pRoute (optional) The route matcher that will be used. Defaults to everything.
39
+ * @param {string?} pRouteStrip (optional) If provided, this prefix will be removed from URL paths before being served.
40
+ * @param {object?} pParams (optional) Additional parameters to pass to serve-static.
41
+ * @return {boolean} true if the handler was successfully installed, otherwise false.
42
+ */
43
+ addStaticRoute(pOrator, pFilePath, pDefaultFile, pRoute, pRouteStrip, pParams)
44
+ {
45
+ if (typeof(pFilePath) !== 'string')
46
+ {
47
+ pOrator.fable.log.error('A file path must be passed in as part of the server.');
48
+ return false;
49
+ }
50
+
51
+ // Default to just serving from root
52
+ const tmpRoute = (typeof(pRoute) === 'undefined') ? '/*' : pRoute;
53
+ const tmpRouteStrip = (typeof(pRouteStrip) === 'undefined') ? '/' : pRouteStrip;
54
+
55
+ // Default to serving index.html
56
+ const tmpDefaultFile = (typeof(pDefaultFile) === 'undefined') ? 'index.html' : pDefaultFile;
57
+
58
+ pOrator.fable.log.info('Orator mapping static route to files: '+tmpRoute+' ==> '+pFilePath+' '+tmpDefaultFile);
59
+
60
+ // Add the route
61
+ pOrator.serviceServer.server.get(tmpRoute, (pRequest, pResponse, fNext) =>
62
+ {
63
+ // The split removes query string parameters so they are ignored by our static web server.
64
+ // The substring cuts that out from the file path so relative files serve from the folders and server
65
+ //FIXME: .....
66
+ // See if there is a magic subdomain put at the beginning of a request.
67
+ // If there is, then we need to see if there is a subfolder and add that to the file path
68
+ let tmpHostSet = pRequest.headers.host.split('.');
69
+ let tmpPotentialSubfolderMagicHost = false;
70
+ let servePath = pFilePath;
71
+ // Check if there are more than one host in the host header (this will be 127 a lot)
72
+ if (tmpHostSet.length > 1)
73
+ {
74
+ tmpPotentialSubfolderMagicHost = tmpHostSet[0];
75
+ }
76
+ if (tmpPotentialSubfolderMagicHost)
77
+ {
78
+ // Check if the subfolder exists
79
+ let tmpPotentialSubfolder = servePath + tmpPotentialSubfolderMagicHost;
80
+ if (this.fable.FilePersistence.libFS.existsSync(tmpPotentialSubfolder))
81
+ {
82
+ // If it does, then we need to add it to the file path
83
+ servePath = `${tmpPotentialSubfolder}/`;
84
+ }
85
+ }
86
+ pRequest.url = pRequest.url.split('?')[0].substr(tmpRouteStrip.length) || '/';
87
+ pRequest.path = function()
88
+ {
89
+ return pRequest.url;
90
+ };
91
+ const tmpServe = libServeStatic(servePath, Object.assign({ index: tmpDefaultFile }, pParams));
92
+ tmpServe(pRequest, pResponse, libFinalHandler(pRequest, pResponse));
93
+ });
94
+ return true;
95
+ }
96
+ }
97
+
98
+ module.exports = OratorStaticFileService;
@@ -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 libOratorStaticServer = require('../source/Orator-Static-Server.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('OratorStaticServer', libOratorStaticServer);
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('OratorStaticServer', {});
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
+ );