@caboodle-tech/node-simple-server 1.3.1 → 2.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.
- package/.eslintrc.json +35 -23
- package/COPYING.txt +7 -0
- package/LICENSE.txt +22 -0
- package/README.md +73 -40
- package/bin/nss.js +1039 -0
- package/changelogs/v1.md +7 -0
- package/changelogs/v2.md +28 -0
- package/examples/README.md +15 -0
- package/examples/controllers/website.js +60 -0
- package/examples/controllers/websocket.js +83 -0
- package/examples/run.js +17 -0
- package/examples/www-website/assets/fonts/roboto/LICENSE.txt +202 -0
- package/examples/www-website/assets/fonts/roboto/roboto-regular.ttf +0 -0
- package/examples/www-website/assets/fonts/roboto/roboto-regular.woff +0 -0
- package/examples/www-website/assets/fonts/roboto/roboto-regular.woff2 +0 -0
- package/examples/www-website/assets/imgs/logo.png +0 -0
- package/examples/www-website/css/main.css +96 -0
- package/examples/www-website/css/normalize.css +349 -0
- package/examples/www-website/index.html +47 -0
- package/examples/www-website/js/main.js +33 -0
- package/examples/www-websockets/assets/fonts/roboto/LICENSE.txt +202 -0
- package/examples/www-websockets/assets/fonts/roboto/roboto-regular.ttf +0 -0
- package/examples/www-websockets/assets/fonts/roboto/roboto-regular.woff +0 -0
- package/examples/www-websockets/assets/fonts/roboto/roboto-regular.woff2 +0 -0
- package/examples/www-websockets/assets/imgs/logo.png +0 -0
- package/examples/www-websockets/css/main.css +148 -0
- package/examples/www-websockets/css/normalize.css +349 -0
- package/examples/www-websockets/index.html +45 -0
- package/examples/www-websockets/js/main.js +63 -0
- package/{sources → handlers}/dir-listing.html +68 -5
- package/handlers/forbidden.html +43 -0
- package/{js → handlers/js}/content-types.js +3 -1
- package/{js → handlers/js}/http-status.js +2 -1
- package/handlers/live-reloading.html +209 -0
- package/handlers/not-found.html +43 -0
- package/package.json +13 -10
- package/LICENSE +0 -21
- package/server.js +0 -866
- package/sources/socket.html +0 -204
- /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": [
|
|
7
|
+
"extends": [
|
|
8
|
+
"airbnb-base"
|
|
9
|
+
],
|
|
10
|
+
"overrides": [],
|
|
7
11
|
"parserOptions": {
|
|
8
|
-
"ecmaVersion":
|
|
9
|
-
"sourceType": "module"
|
|
12
|
+
"ecmaVersion": "latest"
|
|
10
13
|
},
|
|
11
14
|
"rules": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"no-
|
|
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
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
"devDependencies": true
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"indent": [
|
|
29
|
+
"error",
|
|
30
|
+
4,
|
|
26
31
|
{
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
"SwitchCase": 1
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"max-len": ["warn", { "code": 120 }],
|
|
36
|
+
"no-plusplus": [
|
|
37
|
+
"error",
|
|
30
38
|
{
|
|
31
|
-
"
|
|
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
|
-
"
|
|
36
|
-
"
|
|
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
|
-
|
|
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
|
|
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 `
|
|
19
|
+
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 `import` the server module into your code, similar to:
|
|
18
20
|
|
|
19
21
|
```javascript
|
|
20
|
-
|
|
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
|
-
|
|
30
|
+
# As a normal dependency:
|
|
29
31
|
npm install @caboodle-tech/node-simple-server
|
|
30
32
|
|
|
31
|
-
|
|
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
|
|
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
|
-
|
|
51
|
-
|
|
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
|
|
61
|
-
if (
|
|
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
|
|
98
|
+
const watcherOptions = {
|
|
68
99
|
events: {
|
|
69
|
-
all:
|
|
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(
|
|
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
|
|
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
|
|
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
|
-
#### **
|
|
244
|
+
#### **removeWebsocketCallback(pattern^, callback)**
|
|
212
245
|
|
|
213
|
-
-
|
|
246
|
+
- Unregister (stop messaging) a `callback` function that was initially registered with the `pattern`.
|
|
214
247
|
|
|
215
|
-
#### **
|
|
248
|
+
#### **start(\[callback\]) | start(\[port\], \[callback\])**
|
|
216
249
|
|
|
217
|
-
-
|
|
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
|
-
#### **
|
|
252
|
+
#### **stop(\[callback\])**
|
|
220
253
|
|
|
221
|
-
-
|
|
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
|
|
258
|
+
- Stop watching directories or files (`paths`) for changes previously registered with `watch`.
|
|
226
259
|
|
|
227
|
-
#### **watch(paths^^,
|
|
260
|
+
#### **watch(paths^^, watchOptionsObject)**
|
|
228
261
|
|
|
229
|
-
- Start watching a file, files, directory, or directories for changes and then callback to functions that
|
|
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/
|
|
276
|
+
The [current changelog is here](./changelogs/v2.md). All [other changelogs are here](./changelogs).
|
|
244
277
|
|
|
245
278
|
## Contributions
|
|
246
279
|
|