@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/sources/socket.html
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
<!-- Code injected by Node Simple Server for live reloading. -->
|
|
2
|
-
<style>
|
|
3
|
-
#NSSWS {
|
|
4
|
-
display: flex;
|
|
5
|
-
justify-content: center;
|
|
6
|
-
align-items: center;
|
|
7
|
-
position: fixed;
|
|
8
|
-
right: 10px;
|
|
9
|
-
bottom: 10px;
|
|
10
|
-
z-index: 9000;
|
|
11
|
-
width: 80px;
|
|
12
|
-
height: 45px;
|
|
13
|
-
border-radius: 25px;
|
|
14
|
-
background: #000000;
|
|
15
|
-
}
|
|
16
|
-
#NSSWS svg {
|
|
17
|
-
margin-left: 5px;
|
|
18
|
-
fill: #ffffff;
|
|
19
|
-
}
|
|
20
|
-
#NSSWS span {
|
|
21
|
-
display: inline-block;
|
|
22
|
-
margin-right: 5px;
|
|
23
|
-
font-family: monospace;
|
|
24
|
-
font-size: 22px;
|
|
25
|
-
color: #ffffff;
|
|
26
|
-
}
|
|
27
|
-
</style>
|
|
28
|
-
<script type="text/javascript">
|
|
29
|
-
// <![CDATA[
|
|
30
|
-
if ("WebSocket" in window) {
|
|
31
|
-
var NSS_WS = (function () {
|
|
32
|
-
/* Handles showing the user the server disconnected and attempts to reconnect */
|
|
33
|
-
var disconnected = function () {
|
|
34
|
-
var icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd"><path d="M6.817 5.084l-2.057-2.937 1.639-1.147 14.601 20.853-1.638 1.147-1.401-2h-12.461c-3.037 0-5.5-2.463-5.5-5.5 0-2.702 1.951-4.945 4.521-5.408.105-1.965.965-3.73 2.296-5.008zm3.032-1.771c.681-.204 1.403-.313 2.151-.313 4.006 0 7.267 3.141 7.479 7.092 2.57.463 4.521 2.706 4.521 5.408 0 1.898-.962 3.571-2.424 4.56l-1.149-1.64c.947-.627 1.573-1.702 1.573-2.92 0-2.969-2.688-3.766-4.433-3.72.324-3.983-2.114-6.78-5.567-6.78-.317 0-.624.023-.922.068l-1.229-1.755zm-4.349 15.687h11.061l-8.595-12.274c-1.038 1.169-1.618 2.895-1.533 5.054-1.954-.113-4.433.923-4.433 3.72 0 1.93 1.57 3.5 3.5 3.5z"></path></svg>';
|
|
35
|
-
|
|
36
|
-
var div = document.createElement("DIV");
|
|
37
|
-
div.id = "NSSWS";
|
|
38
|
-
div.innerHTML = '<span id="NSSWS_RETRY">02</span>' + icon;
|
|
39
|
-
|
|
40
|
-
if (document.body.firstChild) {
|
|
41
|
-
document.body.insertBefore(div, document.body.firstChild);
|
|
42
|
-
} else {
|
|
43
|
-
document.body.appendChild(div);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
counterInterval = setInterval(function () {
|
|
47
|
-
var counter = document.getElementById("NSSWS_RETRY");
|
|
48
|
-
var seconds = parseInt(counter.innerHTML) - 1;
|
|
49
|
-
if (seconds <= 0) {
|
|
50
|
-
seconds = 3000 * restartAttempts * 0.001;
|
|
51
|
-
}
|
|
52
|
-
if (seconds < 10) {
|
|
53
|
-
seconds = "0" + seconds;
|
|
54
|
-
}
|
|
55
|
-
counter.innerHTML = seconds;
|
|
56
|
-
}, 1000);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/* Expose the random page ID that was assigned to this page. */
|
|
60
|
-
var getID = function () {
|
|
61
|
-
return pageID;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/* Reload the pages stylesheets. */
|
|
65
|
-
var refreshCSS = function () {
|
|
66
|
-
var sheets = [].slice.call(document.getElementsByTagName("link"));
|
|
67
|
-
var head = document.getElementsByTagName("head")[0];
|
|
68
|
-
for (var i = 0; i < sheets.length; ++i) {
|
|
69
|
-
var elem = sheets[i];
|
|
70
|
-
var parent = elem.parentElement || head;
|
|
71
|
-
parent.removeChild(elem);
|
|
72
|
-
var rel = elem.rel;
|
|
73
|
-
if ((elem.href && typeof rel != "string") || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
|
|
74
|
-
var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, "");
|
|
75
|
-
var newUrl = url + (url.indexOf("?") >= 0 ? "&" : "?");
|
|
76
|
-
newUrl += "_cacheOverride=" + new Date().valueOf();
|
|
77
|
-
elem.href = newUrl;
|
|
78
|
-
}
|
|
79
|
-
parent.appendChild(elem);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/* Register a function or functions to call when this page received a WebSocket message. */
|
|
84
|
-
var registerCallback = function (func) {
|
|
85
|
-
if (typeof func === "function") {
|
|
86
|
-
callbacks.push(func);
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/* Attempt to reestablish a connection to the WebSocket server; used with disconnected(). */
|
|
91
|
-
var restart = function () {
|
|
92
|
-
var http = new XMLHttpRequest();
|
|
93
|
-
http.onerror = function (e) {
|
|
94
|
-
// Ignore the error in browsers that respect that.
|
|
95
|
-
};
|
|
96
|
-
http.onreadystatechange = function () {
|
|
97
|
-
if (http.readyState === 4) {
|
|
98
|
-
if (http.status >= 200 && http.status < 400) {
|
|
99
|
-
window.location.reload();
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
http.open("GET", window.location.href);
|
|
104
|
-
http.send();
|
|
105
|
-
|
|
106
|
-
if (autoRestart) {
|
|
107
|
-
if (restartAttempts < 10) {
|
|
108
|
-
restartAttempts += 1;
|
|
109
|
-
}
|
|
110
|
-
clearInterval(restartInterval);
|
|
111
|
-
restartInterval = setInterval(restart, 3000 * restartAttempts);
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/* Send a WebSocket message to the WebSocket server. */
|
|
116
|
-
var send = function (message) {
|
|
117
|
-
if (ready && socket.readyState === WebSocket.OPEN) {
|
|
118
|
-
socket.send(message);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
console.warn("Node Simple Server: The WebSocket is not ready or the connection was closed.");
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
/* Generate a random unique ID for this page; will be registered in the back-end. */
|
|
125
|
-
var uid = function () {
|
|
126
|
-
return Math.random().toString(16).slice(2);
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/* Remove a callback function previously registered with registerCallback(). */
|
|
130
|
-
var unregisterCallback = function (func) {
|
|
131
|
-
for (var i = 0; i < callbacks.length; i++) {
|
|
132
|
-
if (callbacks[i] == func) {
|
|
133
|
-
callbacks.splice(i, 1);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// NSS_WS internal global variables.
|
|
139
|
-
var autoRestart = true;
|
|
140
|
-
var callbacks = [];
|
|
141
|
-
var counterInterval = null;
|
|
142
|
-
var pageID = uid();
|
|
143
|
-
var ready = false;
|
|
144
|
-
var restartAttempts = 0;
|
|
145
|
-
var restartInterval = null;
|
|
146
|
-
// Socket specific variables.
|
|
147
|
-
var protocol = window.location.protocol === "http:" ? "ws://" : "wss://";
|
|
148
|
-
var address = protocol + window.location.host + window.location.pathname + "/ws?id=" + pageID;
|
|
149
|
-
var socket = new WebSocket(address);
|
|
150
|
-
|
|
151
|
-
socket.onmessage = function (event) {
|
|
152
|
-
switch (event.data) {
|
|
153
|
-
case "close":
|
|
154
|
-
ready = false;
|
|
155
|
-
break;
|
|
156
|
-
case "disableAutoRestart":
|
|
157
|
-
autoRestart = false;
|
|
158
|
-
clearInterval(restartInterval);
|
|
159
|
-
clearInterval(counterInterval);
|
|
160
|
-
console.log("Node Simple Server: Auto restart disabled, manually refresh page if server disconnects.");
|
|
161
|
-
break;
|
|
162
|
-
case "refreshCSS":
|
|
163
|
-
refreshCSS();
|
|
164
|
-
break;
|
|
165
|
-
case "reload":
|
|
166
|
-
window.location.reload();
|
|
167
|
-
break;
|
|
168
|
-
default:
|
|
169
|
-
if (callbacks.length > 0) {
|
|
170
|
-
for (var i = 0; i < callbacks.length; i++) {
|
|
171
|
-
callbacks[i](event.data);
|
|
172
|
-
}
|
|
173
|
-
} else {
|
|
174
|
-
console.log(`Received from WebSocket: ${event.data}`);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
socket.addEventListener("close", function () {
|
|
180
|
-
ready = false;
|
|
181
|
-
console.warn("Node Simple Server: Connection closed, live reload disabled.");
|
|
182
|
-
if (autoRestart) {
|
|
183
|
-
disconnected();
|
|
184
|
-
restartInterval = setInterval(restart, 3000);
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
socket.addEventListener("open", function () {
|
|
189
|
-
console.log("Node Simple Server: Live reload enabled.");
|
|
190
|
-
ready = true;
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
return {
|
|
194
|
-
getID: getID,
|
|
195
|
-
registerCallback: registerCallback,
|
|
196
|
-
send: send,
|
|
197
|
-
unregisterCallback: unregisterCallback,
|
|
198
|
-
};
|
|
199
|
-
})();
|
|
200
|
-
} else {
|
|
201
|
-
console.error("Node Simple Server: This Browser does NOT support WebSocket for live reloading.");
|
|
202
|
-
}
|
|
203
|
-
// ]]>
|
|
204
|
-
</script>
|
|
File without changes
|