@caboodle-tech/node-simple-server 1.3.1 → 2.0.1

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.
Files changed (40) hide show
  1. package/.eslintrc.json +35 -23
  2. package/COPYING.txt +7 -0
  3. package/LICENSE.txt +22 -0
  4. package/README.md +73 -40
  5. package/bin/nss.js +1039 -0
  6. package/changelogs/v1.md +7 -0
  7. package/changelogs/v2.md +28 -0
  8. package/examples/README.md +15 -0
  9. package/examples/controllers/website.js +60 -0
  10. package/examples/controllers/websocket.js +83 -0
  11. package/examples/run.js +17 -0
  12. package/examples/www-website/assets/fonts/roboto/LICENSE.txt +202 -0
  13. package/examples/www-website/assets/fonts/roboto/roboto-regular.ttf +0 -0
  14. package/examples/www-website/assets/fonts/roboto/roboto-regular.woff +0 -0
  15. package/examples/www-website/assets/fonts/roboto/roboto-regular.woff2 +0 -0
  16. package/examples/www-website/assets/imgs/logo.png +0 -0
  17. package/examples/www-website/css/main.css +96 -0
  18. package/examples/www-website/css/normalize.css +349 -0
  19. package/examples/www-website/index.html +47 -0
  20. package/examples/www-website/js/main.js +33 -0
  21. package/examples/www-websockets/assets/fonts/roboto/LICENSE.txt +202 -0
  22. package/examples/www-websockets/assets/fonts/roboto/roboto-regular.ttf +0 -0
  23. package/examples/www-websockets/assets/fonts/roboto/roboto-regular.woff +0 -0
  24. package/examples/www-websockets/assets/fonts/roboto/roboto-regular.woff2 +0 -0
  25. package/examples/www-websockets/assets/imgs/logo.png +0 -0
  26. package/examples/www-websockets/css/main.css +148 -0
  27. package/examples/www-websockets/css/normalize.css +349 -0
  28. package/examples/www-websockets/index.html +45 -0
  29. package/examples/www-websockets/js/main.js +63 -0
  30. package/{sources → handlers}/dir-listing.html +68 -5
  31. package/handlers/forbidden.html +43 -0
  32. package/{js → handlers/js}/content-types.js +3 -1
  33. package/{js → handlers/js}/http-status.js +2 -1
  34. package/handlers/live-reloading.html +209 -0
  35. package/handlers/not-found.html +43 -0
  36. package/package.json +13 -10
  37. package/LICENSE +0 -21
  38. package/server.js +0 -866
  39. package/sources/socket.html +0 -204
  40. /package/{sources → handlers}/favicon.ico +0 -0
package/.eslintrc.json CHANGED
@@ -1,38 +1,50 @@
1
1
  {
2
2
  "env": {
3
3
  "browser": true,
4
+ "commonjs": true,
4
5
  "es2021": true
5
6
  },
6
- "extends": ["airbnb-base"],
7
+ "extends": [
8
+ "airbnb-base"
9
+ ],
10
+ "overrides": [],
7
11
  "parserOptions": {
8
- "ecmaVersion": 12,
9
- "sourceType": "module"
12
+ "ecmaVersion": "latest"
10
13
  },
11
14
  "rules": {
12
- "comma-dangle": "off",
13
- "func-names": "off",
14
- "indent": ["error", 4, { "SwitchCase": 1 }],
15
- "no-param-reassign": "off",
16
- "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
17
- "no-template-curly-in-string": "off",
18
- "no-unused-vars": "off",
19
- "no-use-before-define": "off",
20
- "no-restricted-syntax": [
15
+ "class-methods-use-this": "off",
16
+ "comma-dangle": [
17
+ "error",
18
+ "never"
19
+ ],
20
+ "default-case": "off",
21
+ "import/extensions": ["error", "always", {"ignorePackages": true} ],
22
+ "import/no-extraneous-dependencies": [
21
23
  "error",
22
24
  {
23
- "selector": "ForInStatement",
24
- "message": "for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array."
25
- },
25
+ "devDependencies": true
26
+ }
27
+ ],
28
+ "indent": [
29
+ "error",
30
+ 4,
26
31
  {
27
- "selector": "LabeledStatement",
28
- "message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
29
- },
32
+ "SwitchCase": 1
33
+ }
34
+ ],
35
+ "max-len": ["warn", { "code": 120 }],
36
+ "no-plusplus": [
37
+ "error",
30
38
  {
31
- "selector": "WithStatement",
32
- "message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
39
+ "allowForLoopAfterthoughts": true
33
40
  }
34
41
  ],
35
- "padded-blocks": "off",
36
- "prefer-destructuring": "off"
42
+ "no-use-before-define": "off",
43
+ "padded-blocks": [
44
+ "error",
45
+ {
46
+ "classes": "always"
47
+ }
48
+ ]
37
49
  }
38
- }
50
+ }
package/COPYING.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2023 Caboodle Tech Inc.
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
package/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Commons Clause License Condition v1.0
2
+
3
+ The Software is provided to you by the Licensor under the License, as defined
4
+ below, subject to the following condition.
5
+
6
+ Without limiting other conditions in the License, the grant of rights under the
7
+ License will not include, and the License does not grant to you, the right to
8
+ Sell the Software.
9
+
10
+ For purposes of the foregoing, “Sell” means practicing any or all of the rights
11
+ granted to you under the License to provide to third parties, for a fee or other
12
+ consideration (including without limitation fees for hosting or consulting/
13
+ support services related to the Software), a product or service whose value
14
+ derives, entirely or substantially, from the functionality of the Software. Any
15
+ license notice or attribution required by the License must also include this
16
+ Commons Clause License Condition notice.
17
+
18
+ Software: Node Simple Server (NSS)
19
+
20
+ License: MIT
21
+
22
+ Licensor: Caboodle Tech Inc.
package/README.md CHANGED
@@ -1,23 +1,25 @@
1
1
  # NSS: a Node based Simple Server
2
2
 
3
- Node Simple Server (NSS) is a small but effective node based server for development sites and self controlled live reloading. You should consider using NSS if:
3
+ A small but effective node based server for development sites, customizable live reloading, and websocket support built-in. You should consider using NSS if:
4
4
 
5
5
  :heavy_check_mark:  You want to add live reloading to the development process of a static site.
6
6
 
7
- :heavy_check_mark:  You want easy two-way communication from the back-end and front-end of your development site; WebSockets managed for you.
7
+ :heavy_check_mark:  You want easy two-way communication from the back-end and front-end of your development site with built-in WebSockets ready for use.
8
8
 
9
9
  :heavy_check_mark:  You want more fine grained control over the whole live reloading process.
10
10
 
11
11
  :heavy_check_mark:  You want to easily test your development site on multiple devices; must be on the same LAN.
12
12
 
13
+ :heavy_check_mark:  You want to easily setup a LAN application for educational purposes or other development; must be on the same LAN, please consider security implications.
14
+
13
15
  ## Installation
14
16
 
15
17
  ### Manually:
16
18
 
17
- Noe Simple Server (NSS) can be manually incorporated into your development process/ application. Extract the `nss` folder from the [latest release](https://github.com/caboodle-tech/node-simple-server/releases/) and then `require` the server module into your code, similar to:
19
+ Node Simple Server (NSS) can be manually incorporated into your development process/ application. Extract the `nss` folder from the [latest release](https://github.com/caboodle-tech/node-simple-server/releases/) and then `import` the server module into your code, similar to:
18
20
 
19
21
  ```javascript
20
- const NodeSimpleServer = require("./node-simple-server/server");
22
+ import NodeSimpleServer from './nss.js';
21
23
  ```
22
24
 
23
25
  ### Locally:
@@ -25,14 +27,14 @@ const NodeSimpleServer = require("./node-simple-server/server");
25
27
  You can install and use NSS locally in a project with:
26
28
 
27
29
  ```bash
28
- // As a normal dependency:
30
+ # As a normal dependency:
29
31
  npm install @caboodle-tech/node-simple-server
30
32
 
31
- // or as a development dependency:
33
+ # or as a development dependency:
32
34
  npm install @caboodle-tech/node-simple-server --save-dev
33
35
  ```
34
36
 
35
- Depending on how you use and incorporate NSS into your project will determine what it's dependency type should be.
37
+ Depending on how you use and incorporate NSS into your project will determine the best dependency strategy to use.
36
38
 
37
39
  ### Globally:
38
40
 
@@ -47,36 +49,65 @@ npm install --global @caboodle-tech/node-simple-server
47
49
  NSS is designed to be controlled and/or wrapped by another application. The bare minimum code needed to use NSS in your application is:
48
50
 
49
51
  ```javascript
50
- // Require NSS. Here it is required from a manual install.
51
- const NodeSimpleServer = require("./server");
52
+ /**
53
+ * Import NSS. Here it is being imported from a manual install.
54
+ * NOTE: Manual installs must include the handlers directory one directory higher than NSS.
55
+ */
56
+ import NodeSimpleServer from './nss.js';
57
+ import path from 'path'
58
+
59
+ // This is needed for ES modules.
60
+ const __filename = fileURLToPath(import.meta.url);
61
+ const __dirname = path.dirname(__filename);
62
+
63
+ // Determine what directory to watch for changes.
64
+ const websiteRoot = __dirname;
65
+
66
+ // Build a bare minimum server options object.
67
+ const serverOptions = {
68
+ root: websiteRoot
69
+ };
52
70
 
53
71
  // Get a new instance of NSS.
54
- const Server = new NodeSimpleServer();
72
+ const Server = new NodeSimpleServer(serverOptions);
55
73
 
56
74
  // Start the server.
57
75
  Server.start();
58
76
 
59
- // A bare minimum callback to handle changes.
60
- function changes(event, path) {
61
- if (event === "change") {
77
+ // A bare minimum callback to handle most development changes.
78
+ function watcherCallback(event, path, extension) {
79
+ if (extension === 'css') {
80
+ Server.reloadAllStyles();
81
+ return;
82
+ }
83
+ if (extension === 'js') {
84
+ /**
85
+ * NOTE: This is a heavy request to use if your site loads resources from
86
+ * other sites such as images, databases, or API calls. Consider a better
87
+ * approach in this cases such as throttling.
88
+ */
89
+ Server.reloadAllPages();
90
+ return;
91
+ }
92
+ if (event === 'change') {
62
93
  Server.reloadSinglePage(path);
63
94
  }
64
95
  }
65
96
 
66
97
  // Build a bare minimum watcher options object.
67
- const options = {
98
+ const watcherOptions = {
68
99
  events: {
69
- all: changes,
100
+ all: watcherCallback, // Just send everything to a single function.
70
101
  },
71
102
  };
72
103
 
73
- // Watch current directory for changes.
74
- Server.watch(".", options);
104
+ // Watch the current directory for changes.
105
+ Server.watch(websiteRoot, watcherOptions);
75
106
  ```
76
107
 
77
108
  The `options` object **required** by the `watch` method must include an `events` property with at least one watched event. The demo code above used `all` to capture any event. This object takes a lot of settings and is explained below in the **Watch Options** table.
78
109
 
79
- NSS uses the current working directory as the live servers root and is pre-configured with several additional default settings. You can change these by providing your own `options` object when instantiating the server. How this looks in code is shown below, the following table **Server Options** explains all available options.
110
+ NSS uses `process.cwd()` as the live servers root if omitted and is pre-configured with several additional default settings. You can change these by providing your own `options` object when instantiating the server. How this looks in code is shown below, the following table **Server Options** explains all available options.
80
111
 
81
112
  ```javascript
82
113
  // Make your options object.
@@ -86,6 +117,8 @@ const options = {...};
86
117
  const Server = new NodeSimpleServer(options);
87
118
  ```
88
119
 
120
+ **Note:** If you set `options.root` to a different location than the current directory, you should usually provide the same path or a child path of this path, when you instantiate `Server.watch`.
121
+
89
122
  ### :bookmark: Server Options (Object)
90
123
 
91
124
  #### **contentType**    default: text/html
@@ -106,7 +139,7 @@ const Server = new NodeSimpleServer(options);
106
139
 
107
140
  #### **root**    default: process.cwd()
108
141
 
109
- - The port number the HTTP and WebSocket server should listen on for requests.
142
+ - The absolute path to the directory that should be considered the servers root directory.
110
143
 
111
144
  ### :bookmark: Watch Options (Object)
112
145
 
@@ -132,7 +165,7 @@ const Server = new NodeSimpleServer(options);
132
165
 
133
166
  #### **cwd**
134
167
 
135
- - The base directory from which watch `paths` are to be derived. Paths emitted with events will be relative to this.
168
+ - The base directory from which watch `paths` are to be derived. Paths emitted with events will be relative to this path and will use only forward slashes (/) on all operating systems.
136
169
 
137
170
  #### **disableGlobbing**    default: false
138
171
 
@@ -154,9 +187,9 @@ const Server = new NodeSimpleServer(options);
154
187
 
155
188
  - If relying upon the `fs.Stats` object that may get passed with `add`, `addDir`, and `change` events, set this to `true` to ensure it is provided even in cases where it wasn't already available from the underlying watch events.
156
189
 
157
- #### **depth**
190
+ #### **depth**    default: undefined
158
191
 
159
- - If set, limits how many levels of subdirectories will be traversed.
192
+ - If set, limits how many levels of subdirectories will be traversed when watching for changes.
160
193
 
161
194
  #### **awaitWriteFinish**    default: false
162
195
 
@@ -170,12 +203,16 @@ const Server = new NodeSimpleServer(options);
170
203
 
171
204
  - Automatically filters out artifacts that occur when using editors that use "atomic writes" instead of writing directly to the source file.
172
205
 
173
- Most of the **Watch Object Options** are directly from [chokidar](https://github.com/paulmillr/chokidar) which is being used to handle the file monitoring. You may want to visit the [chokidar repo](https://github.com/paulmillr/chokidar) for more information.
206
+ Most of the **Watch Object Options** are directly from [chokidar](https://github.com/paulmillr/chokidar) which is being used to handle the file monitoring. You may want to visit the [chokidar repo](https://github.com/paulmillr/chokidar) for more information. Please note that event paths are altered by NSS to only use forward slashes (/) on all operating systems.
174
207
 
175
208
  ### :bookmark: Server Methods
176
209
 
177
210
  With your new instance of NSS you can call any of the following public methods:
178
211
 
212
+ #### **addWebsocketCallback(pattern^, callback)**
213
+
214
+ - Register a function (`callback`) to receive messages from the front-end if the pages URL matches the `pattern`.
215
+
179
216
  ### **getAddresses**
180
217
 
181
218
  - Returns an array of all the IP addresses you can reach this server at either from the machine itself or on the local area network (LAN).
@@ -184,13 +221,9 @@ With your new instance of NSS you can call any of the following public methods:
184
221
 
185
222
  - Returns an array of watcher objects showing you which directories and files are actively being watched for changes.
186
223
 
187
- #### **message(pattern^, msg)**
224
+ #### **message(pageId | pattern^, msg)**
188
225
 
189
- - Send a message (`msg`) via WebSocket to the page or pages that match the `pattern`.
190
-
191
- #### **registerCallback(pattern^, callback)**
192
-
193
- - Register a function (`callback`) to receive messages from the front-end if the pages URL matches the `pattern`.
226
+ - Send a message (`msg`) via WebSocket to the page that matches the `pageId`, or send to a page or pages that match the `pattern`.
194
227
 
195
228
  #### **reloadPages()**
196
229
 
@@ -208,29 +241,29 @@ With your new instance of NSS you can call any of the following public methods:
208
241
 
209
242
  - Sends the refreshCSS signal to all active pages.
210
243
 
211
- #### **start(\[port\], \[callback\])**
244
+ #### **removeWebsocketCallback(pattern^, callback)**
212
245
 
213
- - Starts the HTTP and WebSocket servers. NOTE: This is a blocking process and will keep any application that ran it alive until stopped gracefully or forcefully terminated. If you do not want this behavior for any reason you will need to call this in its own process.
246
+ - Unregister (stop messaging) a `callback` function that was initially registered with the `pattern`.
214
247
 
215
- #### **stop(\[callback\])**
248
+ #### **start(\[callback\]) | start(\[port\], \[callback\])**
216
249
 
217
- - Gracefully closes all HTTP and WebSocket connections and turns off the servers.
250
+ - Starts the HTTP and WebSocket servers and notifies `callback` if present. `Port` is meant to be an internal option for NSS only but you may specify a port number for NSS to use if you have strict requirements in your environment. NOTE: This is a blocking process and will keep any application that ran it alive until stopped gracefully or forcefully terminated. If you do not want this behavior for any reason you will need to call this in its own process.
218
251
 
219
- #### **unregisterCallback(pattern^, callback)**
252
+ #### **stop(\[callback\])**
220
253
 
221
- - Unregister (stop messaging) a function (`callback`) that was initially registered with the `pattern`.
254
+ - Gracefully closes all HTTP and WebSocket connections and turns off the servers, notifying `callback` if present.
222
255
 
223
256
  #### **unwatch(paths^^)**
224
257
 
225
- - Stop watching directories or files for changes; previously registered with `watch`.
258
+ - Stop watching directories or files (`paths`) for changes previously registered with `watch`.
226
259
 
227
- #### **watch(paths^^, options)**
260
+ #### **watch(paths^^, watchOptionsObject)**
228
261
 
229
- - Start watching a file, files, directory, or directories for changes and then callback to functions that can/ will respond to these changes.
262
+ - Start watching a file, files, directory, or directories (`paths`) for changes and then callback to functions set in `watchOptionsObject` that will respond to these changes.
230
263
 
231
264
  #### **watchEnd()**
232
265
 
233
- - Stop watching registered file, files, directory, or directories for changes.
266
+ - Stop watching all registered file, files, directory, or directories for changes.
234
267
 
235
268
  ### :bookmark: Symbol Key
236
269
 
@@ -240,7 +273,7 @@ With your new instance of NSS you can call any of the following public methods:
240
273
 
241
274
  ## Changelog
242
275
 
243
- The [current changelog is here](./changelogs/v1.md). All [other changelogs are here](./changelogs).
276
+ The [current changelog is here](./changelogs/v2.md). All [other changelogs are here](./changelogs).
244
277
 
245
278
  ## Contributions
246
279