@caboodle-tech/node-simple-server 4.1.0 → 4.1.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.
package/README.md CHANGED
@@ -87,7 +87,7 @@ function watcherCallback(event, path, extension) {
87
87
  /**
88
88
  * NOTE: This is a heavy request to use if your site loads resources from
89
89
  * other sites such as images, databases, or API calls. Consider a better
90
- * approach in this cases such as throttling.
90
+ * approach in these cases such as throttling.
91
91
  */
92
92
  Server.reloadAllPages();
93
93
  return;
package/bin/nss.js CHANGED
@@ -8,6 +8,7 @@ import { fileURLToPath } from 'url';
8
8
 
9
9
  import ContentTypes from '../handlers/js/content-types.js';
10
10
  import HTTPStatus from '../handlers/js/http-status.js';
11
+ import Print from './print.js';
11
12
 
12
13
  // eslint-disable-next-line no-underscore-dangle
13
14
  const __filename = fileURLToPath(import.meta.url);
@@ -43,7 +44,7 @@ class NodeSimpleServer {
43
44
  map: {}
44
45
  };
45
46
 
46
- #VERSION = '4.1.0';
47
+ #VERSION = '4.1.1';
47
48
 
48
49
  #watching = [];
49
50
 
@@ -797,7 +798,7 @@ class NodeSimpleServer {
797
798
  }
798
799
  }
799
800
  // No one is listening for this message.
800
- console.log(`Unanswered WebSocket message from ${cleanURL}: ${message.toString()}`);
801
+ Print.warn(`Unanswered WebSocket message from ${cleanURL}: ${message.toString()}`);
801
802
  });
802
803
 
803
804
  // When a connection closes remove it from CONNECTIONS.
@@ -842,7 +843,7 @@ class NodeSimpleServer {
842
843
 
843
844
  // Don't start an already running server.
844
845
  if (this.#OPS.running) {
845
- console.log('Server is already running.');
846
+ Print.warn('Server is already running.');
846
847
  // Notify the callback.
847
848
  if (callback) {
848
849
  callback(true);
@@ -878,7 +879,8 @@ class NodeSimpleServer {
878
879
  if (port) {
879
880
  // Stop trying new ports after 100 attempts.
880
881
  if (this.#OPS.port - port > 100) {
881
- console.log(`FATAL ERROR: Could not find an available port number in the range of ${this.#OPS.port}–${this.#OPS.port + 100}.`);
882
+ // eslint-disable-next-line max-len
883
+ Print.error(`FATAL ERROR: Could not find an available port number in the range of ${this.#OPS.port}–${this.#OPS.port + 100}.`);
882
884
  // Notify the callback.
883
885
  if (callback) {
884
886
  callback(false);
@@ -891,7 +893,7 @@ class NodeSimpleServer {
891
893
  }
892
894
  return;
893
895
  }
894
- console.log('Server error', error);
896
+ Print.error(`Server Error:\n${error}`);
895
897
  });
896
898
 
897
899
  // Attempt to start the server now.
@@ -907,19 +909,19 @@ class NodeSimpleServer {
907
909
 
908
910
  // Warn the user if we had to change port numbers.
909
911
  if (port && (port !== this.#OPS.port)) {
910
- console.log(`Port ${this.#OPS.port} was in use, switched to using ${port}.\n`);
912
+ Print.warn(`Port ${this.#OPS.port} was in use, switched to using ${port}.\n`);
911
913
  }
912
914
 
913
915
  // Record port in use.
914
916
  this.#OPS.portInUse = port;
915
917
 
916
918
  // Log the ip addresses being watched.
917
- console.log('Node Simple Server live @:');
919
+ Print.notice('Node Simple Server live @:');
918
920
  const addresses = this.getAddresses(port);
919
921
  addresses.forEach((address) => {
920
- console.log(` ${address}`);
922
+ Print.notice(` ${address}`);
921
923
  });
922
- console.log('');
924
+ Print.log('');
923
925
 
924
926
  // Notify the callback.
925
927
  if (callback) {
@@ -953,7 +955,7 @@ class NodeSimpleServer {
953
955
  this.#server = null;
954
956
  this.#socket = null;
955
957
  this.#OPS.running = false;
956
- console.log('Server has been stopped.');
958
+ Print.notice('Server has been stopped.');
957
959
  }
958
960
  // Notify the callback.
959
961
  if (callback) {
@@ -1031,7 +1033,7 @@ class NodeSimpleServer {
1031
1033
  * all backslashes (\) into forward slashes (/).
1032
1034
  */
1033
1035
  if (alterCatachAlls.includes(key)) {
1034
- watcher.on(key, (evt, path, statsOrDetails) => {
1036
+ watcher.on(key, (evt, path, statsOrDetails = {}) => {
1035
1037
  // Capture the call and alter the path before passing it on.
1036
1038
  const altPath = path.replace(/\\/g, '/');
1037
1039
  // Since we're messing with the path already grab the extension for the user.
@@ -1040,7 +1042,7 @@ class NodeSimpleServer {
1040
1042
  options.events[key](evt, altPath, statsOrDetails);
1041
1043
  });
1042
1044
  } else if (alterAddUpdates.includes(key)) {
1043
- watcher.on(key, (path, statsOrDetails) => {
1045
+ watcher.on(key, (path, statsOrDetails = {}) => {
1044
1046
  // Capture the call and alter the path before passing it on.
1045
1047
  const altPath = path.replace(/\\/g, '/');
1046
1048
  // Since we're messing with the path already grab the extension for the user.
@@ -1061,7 +1063,7 @@ class NodeSimpleServer {
1061
1063
  }
1062
1064
  });
1063
1065
  } catch (error) {
1064
- console.log(error);
1066
+ Print.error(error);
1065
1067
  return false;
1066
1068
  }
1067
1069
  return true;
package/bin/print.js ADDED
@@ -0,0 +1,64 @@
1
+ /* eslint-disable no-console */
2
+
3
+ class Print {
4
+
5
+ #enabled = true;
6
+
7
+ disable() {
8
+ this.#enabled = false;
9
+ }
10
+
11
+ enable() {
12
+ this.#enabled = true;
13
+ }
14
+
15
+ error(message, override = false) {
16
+ if (!this.#enabled && !override) {
17
+ return;
18
+ }
19
+ // Red color
20
+ console.error(this.#formatMessage(message, '\x1b[31m'));
21
+ }
22
+
23
+ #formatMessage(message, colorCode) {
24
+ const resetCode = '\x1b[0m';
25
+ return `${colorCode}${message}${resetCode}`;
26
+ }
27
+
28
+ log(message, override = false) {
29
+ if (!this.#enabled && !override) {
30
+ return;
31
+ }
32
+ // White color
33
+ console.log(this.#formatMessage(message, '\x1b[37m'));
34
+ }
35
+
36
+ notice(message, override = false) {
37
+ if (!this.#enabled && !override) {
38
+ return;
39
+ }
40
+ // Blue color
41
+ console.log(this.#formatMessage(message, '\x1b[94m'));
42
+ }
43
+
44
+ success(message, override = false) {
45
+ if (!this.#enabled && !override) {
46
+ return;
47
+ }
48
+ // Green color
49
+ console.log(this.#formatMessage(message, '\x1b[32m'));
50
+ }
51
+
52
+ warn(message, override = false) {
53
+ if (!this.#enabled && !override) {
54
+ return;
55
+ }
56
+ // Yellow color
57
+ console.warn(this.#formatMessage(message, '\x1b[33m'));
58
+ }
59
+
60
+ }
61
+
62
+ const printer = new Print();
63
+
64
+ export default printer;
package/changelogs/v4.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### NSS 4.1.1 (22 January 2024)
2
+
3
+ - fix: Correct a bug in the watcher where we attach a files extension to the stats object. This object is missing when moving directories causing a crash since there is no object to attach the extension, albeit non-existent, to.
4
+ - improve: Quality of life improvement by adding the Caboodle Tech Print class to replace console output; Now in color!
5
+
1
6
  ### NSS 4.0.0 (7 January 2024)
2
7
 
3
8
  - !feat: Correct bug caused by NSS non-standard implementation of Chokidar; see below.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caboodle-tech/node-simple-server",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "Node Simple Server (NSS): A small but effective node based server for development sites, customizable live reloading, and websocket support built-in.",
5
5
  "main": "bin/nss.js",
6
6
  "scripts": {