@caboodle-tech/node-simple-server 4.1.1 → 4.2.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 +25 -2
- package/bin/nss.js +42 -6
- package/changelogs/v4.md +4 -0
- package/handlers/live-reloading.html +3 -2
- package/handlers/websocket-only.html +10 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,7 +97,13 @@ function watcherCallback(event, path, extension) {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
/**
|
|
101
|
+
* A bare minimum callback to handle all websocket messages from the frontend. By
|
|
102
|
+
* default NSS registers and responds to WebSockets by the web pages pathname. In
|
|
103
|
+
* a web page:
|
|
104
|
+
*
|
|
105
|
+
* NSS_WS.send([string|int|bool|object]) // Pathname lookup will be used.
|
|
106
|
+
*/
|
|
101
107
|
function websocketCallback(messageObject, pageId) {
|
|
102
108
|
// Interpret and do what you need to with the message:
|
|
103
109
|
const datatype = messageObject.type
|
|
@@ -107,9 +113,26 @@ function websocketCallback(messageObject, pageId) {
|
|
|
107
113
|
// Respond to the page that sent the message if you like:
|
|
108
114
|
Server.message(pageId, 'Messaged received!');
|
|
109
115
|
}
|
|
110
|
-
|
|
111
116
|
Server.addWebsocketCallback('.*', websocketCallback);
|
|
112
117
|
|
|
118
|
+
/**
|
|
119
|
+
* A bare minimum callback to handle all websocket messages from the frontend.
|
|
120
|
+
* This is a special example that registers a specific route to listen for. In a
|
|
121
|
+
* web page:
|
|
122
|
+
*
|
|
123
|
+
* NSS_WS.send([string|int|bool|object], [route string]) // Route lookup will be used.
|
|
124
|
+
*/
|
|
125
|
+
function websocketCallback(messageObject, pageId) {
|
|
126
|
+
// Interpret and do what you need to with the message:
|
|
127
|
+
const datatype = messageObject.type
|
|
128
|
+
const data = messageObject.data;
|
|
129
|
+
console.log(`Route received ${datatype} data from page ${pageId}: ${data}`)
|
|
130
|
+
|
|
131
|
+
// Respond to the page that sent the message if you like:
|
|
132
|
+
Server.message(pageId, 'Route specific messaged received!');
|
|
133
|
+
}
|
|
134
|
+
Server.addWebsocketCallback('api/search', websocketCallback);
|
|
135
|
+
|
|
113
136
|
// A bare minimum watcher options object; use for development, omit for production.
|
|
114
137
|
const watcherOptions = {
|
|
115
138
|
events: {
|
package/bin/nss.js
CHANGED
|
@@ -44,7 +44,7 @@ class NodeSimpleServer {
|
|
|
44
44
|
map: {}
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
#VERSION = '4.
|
|
47
|
+
#VERSION = '4.2.1';
|
|
48
48
|
|
|
49
49
|
#watching = [];
|
|
50
50
|
|
|
@@ -731,7 +731,6 @@ class NodeSimpleServer {
|
|
|
731
731
|
* @param {object} request The incoming initial connection.
|
|
732
732
|
*/
|
|
733
733
|
#socketListener(socket, request) {
|
|
734
|
-
|
|
735
734
|
// Strip the page ID and /ws tag off the url to get the actual url.
|
|
736
735
|
let cleanURL = request.url.substr(1, request.url.indexOf('/ws?id=') - 1);
|
|
737
736
|
if (!cleanURL) {
|
|
@@ -750,7 +749,20 @@ class NodeSimpleServer {
|
|
|
750
749
|
}
|
|
751
750
|
|
|
752
751
|
// Record the unique page ID directly on the socket object.
|
|
753
|
-
|
|
752
|
+
let idStartIndex;
|
|
753
|
+
if (request.url.indexOf('?id=') !== -1) {
|
|
754
|
+
idStartIndex = request.url.indexOf('?id=');
|
|
755
|
+
} else {
|
|
756
|
+
idStartIndex = -1;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
let pageId;
|
|
760
|
+
if (idStartIndex !== -1) {
|
|
761
|
+
pageId = request.url.substr(idStartIndex).replace('?id=', '');
|
|
762
|
+
} else {
|
|
763
|
+
pageId = 'unknown';
|
|
764
|
+
}
|
|
765
|
+
|
|
754
766
|
// eslint-disable-next-line no-param-reassign
|
|
755
767
|
socket.nssUid = pageId;
|
|
756
768
|
|
|
@@ -780,7 +792,17 @@ class NodeSimpleServer {
|
|
|
780
792
|
// Handle future incoming WebSocket messages from this page.
|
|
781
793
|
socket.on('message', (message) => {
|
|
782
794
|
// NSS messages have a standard format.
|
|
783
|
-
|
|
795
|
+
let msgObj;
|
|
796
|
+
try {
|
|
797
|
+
msgObj = JSON.parse(message.toString());
|
|
798
|
+
if (!('message' in msgObj) || !('type' in msgObj)) {
|
|
799
|
+
throw new Error('Bad format!');
|
|
800
|
+
}
|
|
801
|
+
} catch (e) {
|
|
802
|
+
Print.warn('Invalid socket message received! Socket message must be in Node Simple Server\'s format.');
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
|
|
784
806
|
// If message is a ping send pong and stop.
|
|
785
807
|
if (msgObj.type === 'string') {
|
|
786
808
|
if (msgObj.message === 'ping') {
|
|
@@ -788,15 +810,30 @@ class NodeSimpleServer {
|
|
|
788
810
|
return;
|
|
789
811
|
}
|
|
790
812
|
}
|
|
813
|
+
|
|
814
|
+
// Pull out specific route if there is one.
|
|
815
|
+
let route = null;
|
|
816
|
+
if ('route' in msgObj) {
|
|
817
|
+
route = msgObj.route;
|
|
818
|
+
}
|
|
819
|
+
|
|
791
820
|
// See if the message belongs to a callback and send it there.
|
|
792
821
|
for (let i = 0; i < this.#OPS.callbacks.length; i++) {
|
|
793
822
|
const regex = this.#OPS.callbacks[i][0];
|
|
794
823
|
const callback = this.#OPS.callbacks[i][1];
|
|
795
|
-
|
|
824
|
+
// If the message has a route check that only.
|
|
825
|
+
if (route) {
|
|
826
|
+
if (regex.test(route)) {
|
|
827
|
+
callback(msgObj, pageId);
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
} else if (regex.test(cleanURL)) {
|
|
831
|
+
// Default to the URL (pathname).
|
|
796
832
|
callback(msgObj, pageId);
|
|
797
833
|
return;
|
|
798
834
|
}
|
|
799
835
|
}
|
|
836
|
+
|
|
800
837
|
// No one is listening for this message.
|
|
801
838
|
Print.warn(`Unanswered WebSocket message from ${cleanURL}: ${message.toString()}`);
|
|
802
839
|
});
|
|
@@ -813,7 +850,6 @@ class NodeSimpleServer {
|
|
|
813
850
|
}
|
|
814
851
|
}
|
|
815
852
|
});
|
|
816
|
-
|
|
817
853
|
}
|
|
818
854
|
|
|
819
855
|
/**
|
package/changelogs/v4.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
### NSS 4.2.0 (23 January 2024)
|
|
2
|
+
|
|
3
|
+
- feat: WebSocket connections can now specify a specific backend route instead of just sending messages to the the listener (if any) for the pages path.
|
|
4
|
+
|
|
1
5
|
### NSS 4.1.1 (22 January 2024)
|
|
2
6
|
|
|
3
7
|
- 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.
|
|
@@ -91,10 +91,11 @@
|
|
|
91
91
|
};
|
|
92
92
|
|
|
93
93
|
/** Send a WebSocket message to the WebSocket server. */
|
|
94
|
-
const send = (message) => {
|
|
94
|
+
const send = (message, route = null) => {
|
|
95
95
|
if (ready && socket.readyState === WebSocket.OPEN) {
|
|
96
96
|
socket.send(JSON.stringify({
|
|
97
97
|
message,
|
|
98
|
+
route,
|
|
98
99
|
type: whatIs(message)
|
|
99
100
|
}));
|
|
100
101
|
return;
|
|
@@ -147,7 +148,7 @@
|
|
|
147
148
|
|
|
148
149
|
// Socket specific variables.
|
|
149
150
|
const protocol = window.location.protocol === "http:" ? "ws://" : "wss://";
|
|
150
|
-
const address = protocol + window.location.host +
|
|
151
|
+
const address = protocol + window.location.host + pathname + "/ws?id=" + pageId;
|
|
151
152
|
const socket = new WebSocket(address);
|
|
152
153
|
|
|
153
154
|
// Respond to messages the socket receives.
|
|
@@ -42,15 +42,16 @@
|
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
/** Send a WebSocket message to the WebSocket server. */
|
|
45
|
-
const send = (message) => {
|
|
45
|
+
const send = (message, route = null) => {
|
|
46
46
|
if (ready && socket.readyState === WebSocket.OPEN) {
|
|
47
47
|
socket.send(JSON.stringify({
|
|
48
48
|
message,
|
|
49
|
+
route,
|
|
49
50
|
type: whatIs(message)
|
|
50
51
|
}));
|
|
51
52
|
return;
|
|
52
53
|
}
|
|
53
|
-
console.warn(
|
|
54
|
+
console.warn('Node Simple Server: The WebSocket is not ready or the connection was closed.');
|
|
54
55
|
};
|
|
55
56
|
|
|
56
57
|
/** Generate a random unique ID for this page; will be registered in the back-end. */
|
|
@@ -90,9 +91,15 @@
|
|
|
90
91
|
let restartAttempts = 0;
|
|
91
92
|
let restartInterval = null;
|
|
92
93
|
|
|
94
|
+
// Prep window path.
|
|
95
|
+
let pathname = window.location.pathname;
|
|
96
|
+
if (pathname === '/') {
|
|
97
|
+
pathname = '';
|
|
98
|
+
}
|
|
99
|
+
|
|
93
100
|
// Socket specific variables.
|
|
94
101
|
const protocol = window.location.protocol === "http:" ? "ws://" : "wss://";
|
|
95
|
-
const address = protocol + window.location.host +
|
|
102
|
+
const address = protocol + window.location.host + pathname + "/ws?id=" + pageId;
|
|
96
103
|
const socket = new WebSocket(address);
|
|
97
104
|
|
|
98
105
|
// Respond to messages the socket receives.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caboodle-tech/node-simple-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.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": {
|