@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
@@ -0,0 +1,148 @@
1
+ @font-face {
2
+ font-family: 'Roboto';
3
+ src: url('../assets/fonts/roboto/roboto-regular.woff2') format('woff2'),
4
+ url('../assets/fonts/roboto/roboto-regular.woff') format('woff'),
5
+ url('../assets/fonts/roboto/roboto-regular.ttf') format('truetype')
6
+ }
7
+
8
+ :root {
9
+ --padding: 15px
10
+ }
11
+
12
+ html,
13
+ body {
14
+ margin: 0;
15
+ padding: 0;
16
+ font-size: 62.5%;
17
+ font-family: 'Roboto', sans-serif;
18
+ height: 100%;
19
+ overflow: hidden;
20
+ }
21
+
22
+ body {
23
+ display: flex;
24
+ flex-direction: column;
25
+ }
26
+
27
+ a {
28
+ text-decoration: none;
29
+ color: #1170bd;
30
+ cursor: pointer;
31
+ }
32
+
33
+ .loader {
34
+ width: 100px;
35
+ height: 100px;
36
+ margin: 20px;
37
+ display:inline-block;
38
+ }
39
+
40
+ #page-container {
41
+ flex: 1;
42
+ display: flex;
43
+ flex-direction: column;
44
+ font-size: 1.5rem;
45
+ overflow-y: auto;
46
+ }
47
+
48
+ #page-container .content {
49
+ display: block;
50
+ max-width: 1200px;
51
+ margin: 0 auto;
52
+ }
53
+
54
+ #page-container .content p {
55
+ font-size: 1.85rem;
56
+ line-height: 2.30rem;
57
+ }
58
+
59
+ header {
60
+ text-align: center;
61
+ padding: var(--padding);
62
+ }
63
+
64
+ header .logo {
65
+ max-height: 80px;
66
+ width: auto;
67
+ }
68
+
69
+ main {
70
+ flex: 1;
71
+ padding: var(--padding);
72
+ }
73
+
74
+ main #chat {
75
+ max-width: 500px;
76
+ margin: 0 auto;
77
+ padding: 0.5rem 0.5rem 1.5rem 0.5rem;
78
+ border-radius: 5px;
79
+ border: 1px solid #b5ddff;
80
+ background-color: #f0f8ff;
81
+ box-shadow: 0 3px 5px rgb(0 0 0 / 0.1);
82
+ }
83
+
84
+ main #messages {
85
+ display: flex;
86
+ flex-flow: column wrap;
87
+ }
88
+
89
+ main #messages .msg {
90
+ border-radius: 1rem;
91
+ padding: 1rem;
92
+ margin: 0.65rem 1rem;
93
+ max-width: 50%;
94
+ font-size: 1.75rem;
95
+ line-height: 2.25rem;
96
+ box-shadow: 0 3px 5px rgb(0 0 0 / 0.1);
97
+ }
98
+
99
+ main #messages .msg.backend {
100
+ align-self: flex-start;
101
+ background-color: cornflowerblue;
102
+ color: #fff;
103
+ }
104
+
105
+ main #messages .msg.frontend {
106
+ align-self: flex-end;
107
+ background-color: #efefef;
108
+ }
109
+
110
+ main #chat #controls {
111
+ position: relative;
112
+ display: grid;
113
+ grid-template-columns: 1fr auto;
114
+ background-color: #efefef;
115
+ border-radius: 4px;
116
+ padding: 10px;
117
+ margin: 1rem 1rem 0 auto;
118
+ max-width: 200px;
119
+ box-shadow: 0 3px 5px rgb(0 0 0 / 0.1);
120
+ }
121
+
122
+ main #chat #controls textarea {
123
+ background-color: transparent;
124
+ border: none;
125
+ resize: none;
126
+ }
127
+
128
+ main #chat #controls textarea:focus {
129
+ outline: none;
130
+ }
131
+
132
+ main #chat #controls button {
133
+ display: flex;
134
+ align-items: end;
135
+ padding-bottom: 5px;
136
+ border: 0;
137
+ }
138
+
139
+ footer {
140
+ text-align: center;
141
+ font-size: 2rem;
142
+ padding: var(--padding);
143
+ }
144
+
145
+ footer .heart-icon {
146
+ fill: #f06464;
147
+ margin: 0 2px -5px 2px;
148
+ }
@@ -0,0 +1,349 @@
1
+ /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
2
+
3
+ /* Document
4
+ ========================================================================== */
5
+
6
+ /**
7
+ * 1. Correct the line height in all browsers.
8
+ * 2. Prevent adjustments of font size after orientation changes in iOS.
9
+ */
10
+
11
+ html {
12
+ line-height: 1.15; /* 1 */
13
+ -webkit-text-size-adjust: 100%; /* 2 */
14
+ }
15
+
16
+ /* Sections
17
+ ========================================================================== */
18
+
19
+ /**
20
+ * Remove the margin in all browsers.
21
+ */
22
+
23
+ body {
24
+ margin: 0;
25
+ }
26
+
27
+ /**
28
+ * Render the `main` element consistently in IE.
29
+ */
30
+
31
+ main {
32
+ display: block;
33
+ }
34
+
35
+ /**
36
+ * Correct the font size and margin on `h1` elements within `section` and
37
+ * `article` contexts in Chrome, Firefox, and Safari.
38
+ */
39
+
40
+ h1 {
41
+ font-size: 2em;
42
+ margin: 0.67em 0;
43
+ }
44
+
45
+ /* Grouping content
46
+ ========================================================================== */
47
+
48
+ /**
49
+ * 1. Add the correct box sizing in Firefox.
50
+ * 2. Show the overflow in Edge and IE.
51
+ */
52
+
53
+ hr {
54
+ box-sizing: content-box; /* 1 */
55
+ height: 0; /* 1 */
56
+ overflow: visible; /* 2 */
57
+ }
58
+
59
+ /**
60
+ * 1. Correct the inheritance and scaling of font size in all browsers.
61
+ * 2. Correct the odd `em` font sizing in all browsers.
62
+ */
63
+
64
+ pre {
65
+ font-family: monospace, monospace; /* 1 */
66
+ font-size: 1em; /* 2 */
67
+ }
68
+
69
+ /* Text-level semantics
70
+ ========================================================================== */
71
+
72
+ /**
73
+ * Remove the gray background on active links in IE 10.
74
+ */
75
+
76
+ a {
77
+ background-color: transparent;
78
+ }
79
+
80
+ /**
81
+ * 1. Remove the bottom border in Chrome 57-
82
+ * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
83
+ */
84
+
85
+ abbr[title] {
86
+ border-bottom: none; /* 1 */
87
+ text-decoration: underline; /* 2 */
88
+ text-decoration: underline dotted; /* 2 */
89
+ }
90
+
91
+ /**
92
+ * Add the correct font weight in Chrome, Edge, and Safari.
93
+ */
94
+
95
+ b,
96
+ strong {
97
+ font-weight: bolder;
98
+ }
99
+
100
+ /**
101
+ * 1. Correct the inheritance and scaling of font size in all browsers.
102
+ * 2. Correct the odd `em` font sizing in all browsers.
103
+ */
104
+
105
+ code,
106
+ kbd,
107
+ samp {
108
+ font-family: monospace, monospace; /* 1 */
109
+ font-size: 1em; /* 2 */
110
+ }
111
+
112
+ /**
113
+ * Add the correct font size in all browsers.
114
+ */
115
+
116
+ small {
117
+ font-size: 80%;
118
+ }
119
+
120
+ /**
121
+ * Prevent `sub` and `sup` elements from affecting the line height in
122
+ * all browsers.
123
+ */
124
+
125
+ sub,
126
+ sup {
127
+ font-size: 75%;
128
+ line-height: 0;
129
+ position: relative;
130
+ vertical-align: baseline;
131
+ }
132
+
133
+ sub {
134
+ bottom: -0.25em;
135
+ }
136
+
137
+ sup {
138
+ top: -0.5em;
139
+ }
140
+
141
+ /* Embedded content
142
+ ========================================================================== */
143
+
144
+ /**
145
+ * Remove the border on images inside links in IE 10.
146
+ */
147
+
148
+ img {
149
+ border-style: none;
150
+ }
151
+
152
+ /* Forms
153
+ ========================================================================== */
154
+
155
+ /**
156
+ * 1. Change the font styles in all browsers.
157
+ * 2. Remove the margin in Firefox and Safari.
158
+ */
159
+
160
+ button,
161
+ input,
162
+ optgroup,
163
+ select,
164
+ textarea {
165
+ font-family: inherit; /* 1 */
166
+ font-size: 100%; /* 1 */
167
+ line-height: 1.15; /* 1 */
168
+ margin: 0; /* 2 */
169
+ }
170
+
171
+ /**
172
+ * Show the overflow in IE.
173
+ * 1. Show the overflow in Edge.
174
+ */
175
+
176
+ button,
177
+ input { /* 1 */
178
+ overflow: visible;
179
+ }
180
+
181
+ /**
182
+ * Remove the inheritance of text transform in Edge, Firefox, and IE.
183
+ * 1. Remove the inheritance of text transform in Firefox.
184
+ */
185
+
186
+ button,
187
+ select { /* 1 */
188
+ text-transform: none;
189
+ }
190
+
191
+ /**
192
+ * Correct the inability to style clickable types in iOS and Safari.
193
+ */
194
+
195
+ button,
196
+ [type="button"],
197
+ [type="reset"],
198
+ [type="submit"] {
199
+ -webkit-appearance: button;
200
+ }
201
+
202
+ /**
203
+ * Remove the inner border and padding in Firefox.
204
+ */
205
+
206
+ button::-moz-focus-inner,
207
+ [type="button"]::-moz-focus-inner,
208
+ [type="reset"]::-moz-focus-inner,
209
+ [type="submit"]::-moz-focus-inner {
210
+ border-style: none;
211
+ padding: 0;
212
+ }
213
+
214
+ /**
215
+ * Restore the focus styles unset by the previous rule.
216
+ */
217
+
218
+ button:-moz-focusring,
219
+ [type="button"]:-moz-focusring,
220
+ [type="reset"]:-moz-focusring,
221
+ [type="submit"]:-moz-focusring {
222
+ outline: 1px dotted ButtonText;
223
+ }
224
+
225
+ /**
226
+ * Correct the padding in Firefox.
227
+ */
228
+
229
+ fieldset {
230
+ padding: 0.35em 0.75em 0.625em;
231
+ }
232
+
233
+ /**
234
+ * 1. Correct the text wrapping in Edge and IE.
235
+ * 2. Correct the color inheritance from `fieldset` elements in IE.
236
+ * 3. Remove the padding so developers are not caught out when they zero out
237
+ * `fieldset` elements in all browsers.
238
+ */
239
+
240
+ legend {
241
+ box-sizing: border-box; /* 1 */
242
+ color: inherit; /* 2 */
243
+ display: table; /* 1 */
244
+ max-width: 100%; /* 1 */
245
+ padding: 0; /* 3 */
246
+ white-space: normal; /* 1 */
247
+ }
248
+
249
+ /**
250
+ * Add the correct vertical alignment in Chrome, Firefox, and Opera.
251
+ */
252
+
253
+ progress {
254
+ vertical-align: baseline;
255
+ }
256
+
257
+ /**
258
+ * Remove the default vertical scrollbar in IE 10+.
259
+ */
260
+
261
+ textarea {
262
+ overflow: auto;
263
+ }
264
+
265
+ /**
266
+ * 1. Add the correct box sizing in IE 10.
267
+ * 2. Remove the padding in IE 10.
268
+ */
269
+
270
+ [type="checkbox"],
271
+ [type="radio"] {
272
+ box-sizing: border-box; /* 1 */
273
+ padding: 0; /* 2 */
274
+ }
275
+
276
+ /**
277
+ * Correct the cursor style of increment and decrement buttons in Chrome.
278
+ */
279
+
280
+ [type="number"]::-webkit-inner-spin-button,
281
+ [type="number"]::-webkit-outer-spin-button {
282
+ height: auto;
283
+ }
284
+
285
+ /**
286
+ * 1. Correct the odd appearance in Chrome and Safari.
287
+ * 2. Correct the outline style in Safari.
288
+ */
289
+
290
+ [type="search"] {
291
+ -webkit-appearance: textfield; /* 1 */
292
+ outline-offset: -2px; /* 2 */
293
+ }
294
+
295
+ /**
296
+ * Remove the inner padding in Chrome and Safari on macOS.
297
+ */
298
+
299
+ [type="search"]::-webkit-search-decoration {
300
+ -webkit-appearance: none;
301
+ }
302
+
303
+ /**
304
+ * 1. Correct the inability to style clickable types in iOS and Safari.
305
+ * 2. Change font properties to `inherit` in Safari.
306
+ */
307
+
308
+ ::-webkit-file-upload-button {
309
+ -webkit-appearance: button; /* 1 */
310
+ font: inherit; /* 2 */
311
+ }
312
+
313
+ /* Interactive
314
+ ========================================================================== */
315
+
316
+ /*
317
+ * Add the correct display in Edge, IE 10+, and Firefox.
318
+ */
319
+
320
+ details {
321
+ display: block;
322
+ }
323
+
324
+ /*
325
+ * Add the correct display in all browsers.
326
+ */
327
+
328
+ summary {
329
+ display: list-item;
330
+ }
331
+
332
+ /* Misc
333
+ ========================================================================== */
334
+
335
+ /**
336
+ * Add the correct display in IE 10+.
337
+ */
338
+
339
+ template {
340
+ display: none;
341
+ }
342
+
343
+ /**
344
+ * Add the correct display in IE 10.
345
+ */
346
+
347
+ [hidden] {
348
+ display: none;
349
+ }
@@ -0,0 +1,45 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Websocket Demo</title>
7
+ <link rel="stylesheet" href="./css/normalize.css">
8
+ <link rel="stylesheet" href="./css/main.css">
9
+ <script src="./js/main.js" defer></script>
10
+ </head>
11
+ <body>
12
+ <div id="page-container">
13
+ <header>
14
+ <div class="content">
15
+ <img class="logo" src="./assets/imgs/logo.png" title="Caboodle Tech Inc." alt="Caboodle Tech logo">
16
+ </div>
17
+ </header>
18
+ <main>
19
+ <div class="content">
20
+ <h1>Websocket Demo</h1>
21
+ <p>
22
+ This is a simple demo of using websockets with Node Simple Server (NSS). The demo below allows you to send a short text message from your website (front-end) to the back-end (server). The server will then respond after a short delay with a generic message letting you know the message was received. This demo mimics a messaging app for fun, you do not need to use websockets for an actual messaging app, and you should consider the security implications if you choose to.
23
+ </p>
24
+ <div id="chat">
25
+ <div id="messages"></div>
26
+ <div id="controls">
27
+ <textarea maxlength="160"></textarea>
28
+ <button type="button">
29
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M24 0l-6 22-8.129-7.239 7.802-8.234-10.458 7.227-7.215-1.754 24-12zm-15 16.668v7.332l3.258-4.431-3.258-2.901z"/></svg>
30
+ </button>
31
+ </div>
32
+ </div>
33
+ <p>
34
+ <strong>NOTE:</strong> Every time a page connects via websocket it is assigned a random page id so you can reply to that exact page if you ever need to. This id is not permanent! Every page refresh or navigation triggers the creation of a new page id and the deletion of the old from the servers websocket records.
35
+ </p>
36
+ </div>
37
+ </main>
38
+ <footer>
39
+ <div class="content">
40
+ Made with <svg class="heart-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M18 1l-6 4-6-4-6 5v7l12 10 12-10v-7z"/></svg> by Caboodle Tech
41
+ </div>
42
+ </footer>
43
+ </div>
44
+ </body>
45
+ </html>
@@ -0,0 +1,63 @@
1
+ function setupChatApp() {
2
+ // Make sure the basic elements are present on the page.
3
+ const chatContainer = document.getElementById('chat');
4
+ if (!chatContainer) { return; }
5
+
6
+ const msgContainer = document.getElementById('messages');
7
+ if (!msgContainer) { return; }
8
+
9
+ const textarea = chatContainer.querySelector('textarea');
10
+ if (!textarea) { return; }
11
+
12
+ const button = chatContainer.querySelector('button');
13
+ if (!button) { return; }
14
+
15
+ // Allow submitting the message on enter key.
16
+ textarea.addEventListener('keypress', (evt) => {
17
+ if (evt.key === 'Enter' && !evt.shiftKey) {
18
+ evt.preventDefault();
19
+ chatContainer.querySelector('button').click();
20
+ }
21
+ textarea.style.height = '';
22
+ textarea.style.height = `${textarea.scrollHeight}px`;
23
+ });
24
+
25
+ // Auto resize the textarea based on its input.
26
+ textarea.addEventListener('input', () => {
27
+ textarea.style.height = '';
28
+ textarea.style.height = `${textarea.scrollHeight}px`;
29
+ });
30
+
31
+ // Handle websocket messages from the server.
32
+ NSS_WS.registerCallback((msgObj) => {
33
+ // NSS demos only use strings so check for that.
34
+ if (msgObj.type === 'string') {
35
+ // Add the servers response to the chat app.
36
+ const msgDiv = document.createElement('div');
37
+ msgDiv.classList.add('msg');
38
+ msgDiv.classList.add('backend');
39
+ msgDiv.innerHTML = msgObj.message;
40
+ msgContainer.appendChild(msgDiv);
41
+ }
42
+ });
43
+
44
+ // When the submit button (send icon) is pressed send a websocket message.
45
+ button.addEventListener('click', () => {
46
+ // Send the message.
47
+ NSS_WS.send(textarea.value.replace(/\n/g, ' '));
48
+ // Add the message to the chat app.
49
+ const msgDiv = document.createElement('div');
50
+ msgDiv.classList.add('msg');
51
+ msgDiv.classList.add('frontend');
52
+ msgDiv.innerHTML = textarea.value.replace(/\n/g, '<br>');
53
+ msgContainer.appendChild(msgDiv);
54
+ // Reset the chat message box.
55
+ textarea.value = '';
56
+ textarea.style.height = 'initial';
57
+ });
58
+ }
59
+
60
+ // Wait until the page is ready and then setup the demo.
61
+ document.addEventListener('DOMContentLoaded', () => {
62
+ setupChatApp();
63
+ });