safely-serve 0.0.2 → 0.0.8

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/libsafely-serve CHANGED
@@ -22,33 +22,33 @@
22
22
  // along with this program. If not, see <https://www.gnu.org/licenses/>.
23
23
 
24
24
  const
25
- _serve_module =
25
+ { spawn } =
26
26
  require(
27
- 'serve');
27
+ "node:child_process");
28
+ _spawn =
29
+ spawn;
28
30
  const
29
31
  _cert_tools_module =
30
32
  require(
31
33
  'cert-tools');
34
+ _cert_gen =
35
+ _cert_tools_module._cert_gen;
36
+ const
37
+ _sha_module =
38
+ require(
39
+ 'js-sha256');
40
+ _sha256 =
41
+ _sha_module.sha256;
32
42
  const
33
43
  _libcrash =
34
44
  require(
35
45
  'crash-js');
46
+ _dirname =
47
+ _libcrash._dirname;
36
48
  _error_display =
37
49
  _libcrash._error_display;
38
- _ext_rm =
39
- _libcrash._ext_rm;
40
50
  _file_exists =
41
51
  _libcrash._file_exists;
42
- _file_read =
43
- _libcrash._file_read;
44
- _file_write =
45
- _libcrash._file_write;
46
- _fs_worker_start =
47
- _libcrash._fs_worker_start;
48
- _json_read =
49
- _libcrash._json_read;
50
- _ls =
51
- _libcrash._ls;
52
52
  _mkdir =
53
53
  _libcrash._mkdir;
54
54
  _msg_info =
@@ -56,9 +56,222 @@ _msg_info =
56
56
  _msg_error =
57
57
  _libcrash._msg_error;
58
58
 
59
+ function
60
+ _path_encode(
61
+ _path) {
62
+ let
63
+ _string;
64
+ _string =
65
+ _sha256(
66
+ _path + "\n");
67
+ return _string;
68
+ }
69
+
70
+ function
71
+ _serve_close_callback_get(
72
+ _output) {
73
+ let
74
+ _close_callback;
75
+ _close_callback =
76
+ function(
77
+ _code) {
78
+ console.log(
79
+ `Exit code: ${_code}`);
80
+ console.log(
81
+ `Output: ${_output}`);
82
+ };
83
+ return _close_callback;
84
+ }
85
+
86
+ function
87
+ _serve_data_callback_get(
88
+ _output) {
89
+ let
90
+ _data_callback;
91
+ _data_callback =
92
+ function(
93
+ _data) {
94
+ _data =
95
+ _data.toString();
96
+ console.log(
97
+ _data);
98
+ _output +=
99
+ _data;
100
+ };
101
+ return _data_callback;
102
+ }
103
+
104
+ function
105
+ _npx_cmdline_get(
106
+ _target_directory,
107
+ _listen_uris,
108
+ _target_port,
109
+ _index_redirect,
110
+ _debug_enabled,
111
+ _config_file,
112
+ _logging_disabled,
113
+ _cors_enabled,
114
+ _clipboard_disabled,
115
+ _compression_disabled,
116
+ _etag_disabled,
117
+ _symlinks_enabled,
118
+ _ssl_cert,
119
+ _ssl_key,
120
+ _ssl_pass,
121
+ _port_switching_disabled) {
122
+ let
123
+ _cmd_args;
124
+ _cmd_args = [
125
+ "serve"
126
+ ];
127
+ for ( _uri of _listen_uris ) {
128
+ _cmd_args.push(
129
+ "-l",
130
+ _uri);
131
+ }
132
+ _cmd_args.push(
133
+ "-p",
134
+ _target_port);
135
+ if ( _index_redirect == "y" ) {
136
+ _cmd_args.push(
137
+ "--single");
138
+ }
139
+ if ( _debug_enabled != "n" ) {
140
+ _cmd_args.push(
141
+ "--debug");
142
+ }
143
+ if ( _config_file != "" ) {
144
+ _cmd_args.push(
145
+ "--config",
146
+ _config_file);
147
+ }
148
+ if ( _logging_disabled == "y" ) {
149
+ _cmd_args.push(
150
+ "--no-request-logging");
151
+ }
152
+ if ( _cors_enabled == "y" ) {
153
+ _cmd_args.push(
154
+ "--cors");
155
+ }
156
+ if ( _clipboard_disabled == "y" ) {
157
+ _cmd_args.push(
158
+ "--no-clipboard");
159
+ }
160
+ if ( _compression_disabled == "y" ) {
161
+ _cmd_args.push(
162
+ "--no-compression");
163
+ }
164
+ if ( _etag_disabled == "y" ) {
165
+ _cmd_args.push(
166
+ "--no-etag");
167
+ }
168
+ if ( _symlinks_enabled == "y" ) {
169
+ _cmd_args.push(
170
+ "--symlinks");
171
+ }
172
+ if ( _port_switching_disabled == "y" ) {
173
+ _cmd_args.push(
174
+ "--no-port-switching");
175
+ }
176
+ if ( _ssl_cert != "" ) {
177
+ _cmd_args.push(
178
+ "--ssl-cert",
179
+ ssl_cert);
180
+ }
181
+ if ( _ssl_key != "" ) {
182
+ _cmd_args.push(
183
+ "--ssl-key",
184
+ ssl_key);
185
+ }
186
+ if ( _ssl_pass != "" ) {
187
+ _cmd_args.push(
188
+ "--ssl-pass",
189
+ ssl_pass);
190
+ }
191
+
192
+ }
193
+
194
+ function
195
+ _serve(
196
+ _target_directory,
197
+ _listen_uris,
198
+ _target_port,
199
+ _index_redirect,
200
+ _debug_enabled,
201
+ _config_file,
202
+ _logging_disabled,
203
+ _cors_enabled,
204
+ _clipboard_disabled,
205
+ _compression_disabled,
206
+ _etag_disabled,
207
+ _symlinks_enabled,
208
+ _ssl_cert,
209
+ _ssl_key,
210
+ _ssl_pass,
211
+ _port_switching_disabled) {
212
+ let
213
+ _cmd,
214
+ _cmd_args,
215
+ _output,
216
+ _serve_process,
217
+ _uri;
218
+ _cmd =
219
+ "npx";
220
+ _cmd_args =
221
+ _npx_cmdline_get(
222
+ _target_directory,
223
+ _listen_uris,
224
+ _target_port,
225
+ _index_redirect,
226
+ _debug_enabled,
227
+ _config_file,
228
+ _logging_disabled,
229
+ _cors_enabled,
230
+ _clipboard_disabled,
231
+ _compression_disabled,
232
+ _etag_disabled,
233
+ _symlinks_enabled,
234
+ _ssl_cert,
235
+ _ssl_key,
236
+ _ssl_pass,
237
+ _port_switching_disabled);
238
+ _serve_process =
239
+ _spawn(
240
+ _cmd,
241
+ _cmd_args);
242
+ _serve_data_callback =
243
+ _serve_data_callback_get(
244
+ _output);
245
+ _serve_process.stdout.on(
246
+ 'data',
247
+ _serve_data_callback);
248
+ _serve_close_callback =
249
+ _serve_close_callback_get(
250
+ _output);
251
+ _serve_process.stdout.on(
252
+ 'close',
253
+ _serve_close_callback);
254
+ }
255
+
59
256
  async function
60
257
  _safely_serve(
61
- _key_private_path,
258
+ _target_directory,
259
+ _listen_uris,
260
+ _target_port,
261
+ _index_redirect,
262
+ _debug_enabled,
263
+ _config_file,
264
+ _logging_disabled,
265
+ _cors_enabled,
266
+ _clipboard_disabled,
267
+ _compression_disabled,
268
+ _etag_disabled,
269
+ _symlinks_enabled,
270
+ _cert_new,
271
+ _ssl_cert,
272
+ _ssl_key,
273
+ _ssl_pass,
274
+ _port_switching_disabled,
62
275
  _keys_length,
63
276
  _serial_number,
64
277
  _validity_min,
@@ -68,65 +281,74 @@ async function
68
281
  _state_name,
69
282
  _locality_name,
70
283
  _organization_name,
71
- _organization_unit,
72
- _output_file) {
284
+ _organization_unit) {
73
285
  let
74
- _alt_names,
75
- _attrs,
76
- _basic_constraints,
77
- _cert,
78
- _ext_key_usage,
79
- _key_usage,
80
- _key_pem,
81
- _key_private,
82
- _keys,
83
- _keys_pem,
84
- _ns_cert_type,
85
- _pem,
86
- _subject_alt_name,
87
- _subject_key_identifier;
88
- _keys_length =
89
- Number(
90
- _keys_length);
91
- if ( _key_private_path == "" ||
92
- ! _file_exists(
93
- _key_private_path) ) {
94
- _keys =
95
- _rsa.generateKeyPair(
96
- _keys_length);
97
- _key_pem =
98
- _pki.privateKeyToPem(
99
- _keys.privateKey);
100
- _file_write(
101
- _key_private_path,
102
- _key_pem);
103
- _key_private =
104
- _keys.privateKey;
105
- }
106
- else {
107
- _key_pem =
108
- _file_read(
109
- _key_private_path);
110
- _key_private =
111
- _pki.privateKeyFromPem(
112
- _key_pem);
113
- _keys =
114
- { privateKey:
115
- _key_private };
116
- }
117
- if ( _log_file != "" ) {
118
- if ( typeof window !== 'undefined') {
119
- await _fs_worker_start(
120
- "fs-worker.js");
121
- }
122
- _file_write(
123
- _log_file,
124
- _whatever_log);
286
+ _cert_gen_opts,
287
+ _mkdir_args,
288
+ _mkdir_opts,
289
+ _serve_opts;
290
+ if ( ! _file_exists(
291
+ _ssl_cert) &&
292
+ _cert_new == "y" ) {
293
+ _mkdir_opts =
294
+ { "recursive":
295
+ true,
296
+ "mode":
297
+ 0700 };
298
+ _mkdir_args = [
299
+ _dirname(
300
+ _ssl_cert),
301
+ _mkdir_opts
302
+ ];
303
+ _mkdir.apply(
304
+ null,
305
+ _mkdir_args);
306
+ _cert_gen_opts = [
307
+ _ssl_key,
308
+ _keys_length,
309
+ _serial_number,
310
+ _validity_min,
311
+ _validity_max,
312
+ _domain_name,
313
+ _country_name,
314
+ _state_name,
315
+ _locality_name,
316
+ _organization_name,
317
+ _organization_unit,
318
+ _ssl_cert
319
+ ];
320
+ await _cert_gen.apply(
321
+ null,
322
+ _cert_gen_opts);
125
323
  }
126
- return true;
324
+ _serve_opts = [
325
+ _target_directory,
326
+ _listen_uris,
327
+ _target_port,
328
+ _index_redirect,
329
+ _debug_enabled,
330
+ _config_file,
331
+ _logging_disabled,
332
+ _cors_enabled,
333
+ _clipboard_disabled,
334
+ _compression_disabled,
335
+ _etag_disabled,
336
+ _symlinks_enabled,
337
+ _ssl_cert,
338
+ _ssl_key,
339
+ _ssl_pass,
340
+ _port_switching_disabled
341
+ ];
342
+ _serve.apply(
343
+ null,
344
+ _serve_opts);
127
345
  }
128
346
 
129
347
  module.exports = {
348
+ _path_encode:
349
+ _path_encode,
130
350
  _safely_serve:
131
- _safely_serve
351
+ _safely_serve,
352
+ _serve:
353
+ _serve
132
354
  };
@@ -15,6 +15,10 @@ const
15
15
  const
16
16
  _output_file_name =
17
17
  `${_input_file_name}.js`;
18
+ const
19
+ _child_process_ignore =
20
+ { resourceRegExp:
21
+ /^node:child_process$/ };
18
22
  const
19
23
  _serve_ignore =
20
24
  { resourceRegExp:
@@ -35,6 +39,10 @@ const
35
39
  const
36
40
  _ignore_plugin =
37
41
  _webpack.IgnorePlugin;
42
+ const
43
+ _child_process_ignore_plugin =
44
+ new _ignore_plugin(
45
+ _child_process_ignore);
38
46
  const
39
47
  _serve_ignore_plugin =
40
48
  new _ignore_plugin(
@@ -72,8 +80,14 @@ module.exports = {
72
80
  _path.resolve(
73
81
  __dirname,
74
82
  'node_modules/path/mod.js'),
83
+ "serve":
84
+ _path.resolve(
85
+ __dirname,
86
+ 'node_modules/serve/build/main.js'),
75
87
  },
76
88
  fallback: {
89
+ "node:child_process":
90
+ false,
77
91
  "serve":
78
92
  false,
79
93
  "utils":
@@ -90,6 +104,7 @@ module.exports = {
90
104
  { yargs:
91
105
  'yargs' },
92
106
  plugins: [
107
+ _child_process_ignore_plugin,
93
108
  _serve_ignore_plugin,
94
109
  _yargs_ignore_plugin,
95
110
  _yargs_helpers_ignore_plugin
@@ -28,12 +28,12 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
28
28
  .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
29
29
  .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
30
30
  ..
31
- .TH "cert-gen" "1" "" "cert-gen insert.version.here"
31
+ .TH "safely-serve" "1" "" "safely-serve insert.version.here"
32
32
  .SH Name
33
- cert-gen \- Certificate generator
33
+ safely-serve \- Safe static file serving and directory listing
34
34
  .SH Synopsis
35
35
  .sp
36
- cert\-gen \fIoption\fP
36
+ safely\-serve \fIoption\fP
37
37
  .SH Description
38
38
  .sp
39
39
  Generates an X509 version 3
@@ -44,21 +44,93 @@ It depends on the Forge TLS
44
44
  Javascript implementation
45
45
  and on the Crash Javascript Library.
46
46
  .sp
47
- It runs both in Node as in
48
- the browser.
47
+ By default, serve will listen
48
+ on 0.0.0.0:3000 and serve the
49
+ current working directory on
50
+ that address.
51
+ .sp
52
+ Specifying a single \-\-listen argument
53
+ will overwrite the default,
54
+ not supplement it.
55
+ .sp
56
+ If no SSL certificate and private key
57
+ are passed, new ones are generated
58
+ unique to that specific directory.
49
59
  .SH Options
50
60
  .INDENT 0.0
51
61
  .TP
52
- .BI \-i\fP,\fB \-\-key\-private\fB= FILE
53
- Use existing private key.
62
+ .BI \-l\fP,\fB \-\-listen\fB= URI
63
+ Specify a URI endpoint on
64
+ which to listen (see below);
65
+ more than one may be specified to
66
+ listen in multiple places.
67
+ .TP
68
+ .BI \-p\fP,\fB \-\-port\fB= PORT
69
+ Specify custom port
70
+ .TP
71
+ .B \-s\fP,\fB \-\-single
72
+ Rewrite all not\-found requests
73
+ to \(aqindex.html\(aq.
74
+ .TP
75
+ .B \-d\fP,\fB \-\-debug
76
+ Show debugging information.
77
+ .TP
78
+ .BI \-c\fP,\fB \-\-config\fB= PATH
79
+ Specify custom path to \(aqserve.json\(aq.
80
+ .TP
81
+ .B \-L\fP,\fB \-\-no\-request\-logging
82
+ Do not log any request information
83
+ to the console.
84
+ .TP
85
+ .B \-C\fP,\fB \-\-cors
86
+ Enable CORS, sets
87
+ \(aqAccess\-Control\-Allow\-Origin\(aq to \(aq*\(aq.
88
+ .TP
89
+ .B \-n\fP,\fB \-\-no\-clipboard
90
+ Do not copy the local address
91
+ to the clipboard.
92
+ .TP
93
+ .B \-u\fP,\fB \-\-no\-compression
94
+ Do not compress files.
95
+ .TP
96
+ .B \-e\fP,\fB \-\-no\-etag
97
+ Send \(aqLast\-Modified\(aq header
98
+ instead of \fIETag\fP\&.
99
+ .TP
100
+ .B \-S\fP,\fB \-\-symlinks
101
+ Resolve symlinks instead of
102
+ showing 404 errors.
103
+ .TP
104
+ .B \-\-no\-port\-switching
105
+ Do not open a port other than
106
+ the one specified when it\(aqs taken.
107
+ .UNINDENT
108
+ .SH SSL options
109
+ .INDENT 0.0
110
+ .TP
111
+ .B \-g\fP,\fB \-\-cert\-gen
112
+ If enabled, generate self\-signed certificate
113
+ at the specified path.
114
+ .TP
115
+ .BI \-I\fP,\fB \-\-ssl\-cert\fB= PATH
116
+ Optional path to an SSL/TLS
117
+ certificate to serve with HTTPS.
118
+ Supported formats: PEM (default)
119
+ and PKCS12 (PFX)
120
+ .TP
121
+ .BI \-i\fP,\fB \-\-ssl\-key\fB= PATH
122
+ Optional path to the SSL/TLS
123
+ certificate\(aqs private key.
124
+ Applicable only for PEM certificates.
125
+ .TP
126
+ .BI \-k\fP,\fB \-\-ssl\-pass\fB= PATH
127
+ Optional path to the SSL/TLS
128
+ certificate\(aqs passphrase.
54
129
  .TP
55
130
  .BI \-l\fP,\fB \-\-keys\-length\fB= N
56
131
  Keys pair length.
57
- .TP
58
- .BI \-o\fP,\fB \-\-output\-file\fB= FILE
59
- Set output file.
60
132
  .UNINDENT
61
- .SH Validity options
133
+ .SH Certificate options
62
134
  .INDENT 0.0
63
135
  .TP
64
136
  .BI \-B\fP,\fB \-\-validity\-start\fB= DATE
@@ -66,9 +138,6 @@ Set activation date.
66
138
  .TP
67
139
  .BI \-E\fP,\fB \-\-validity\-end\fB= DATE
68
140
  Set expiration date.
69
- .UNINDENT
70
- .SH Identity Options
71
- .INDENT 0.0
72
141
  .TP
73
142
  .BI \-D\fP,\fB \-\-domain\-name\fB= DOMAIN
74
143
  Set domain name.
@@ -91,18 +160,84 @@ Set organization unit.
91
160
  .BI \-s\fP,\fB \-\-serial\-number\fB= SERIAL
92
161
  Set serial number.
93
162
  .UNINDENT
163
+ .SH Application options
164
+ .INDENT 0.0
165
+ .TP
166
+ .B \-v\fP,\fB \-\-version
167
+ Displays the current version of
168
+ Safely Serve.
169
+ .TP
170
+ .B \-\-help
171
+ Shows this help message.
172
+ .UNINDENT
173
+ .SH Endpoints
174
+ .sp
175
+ Listen endpoints (specified by the
176
+ \fI\-\-listen\fP or \fI\-l\fP options above)
177
+ instruct serve to listen on one or
178
+ more interfaces/ports, UNIX domain sockets,
179
+ or Windows named pipes.
180
+ .sp
181
+ For TCP ports on hostname \(dqlocalhost\(dq:
182
+ .INDENT 0.0
183
+ .INDENT 3.5
184
+ .sp
185
+ .EX
186
+ # serve \e
187
+ \-l \e
188
+ \(dq1234\(dq
189
+ .EE
190
+ .UNINDENT
191
+ .UNINDENT
192
+ .sp
193
+ For TCP (traditional host/port) endpoints:
194
+ .INDENT 0.0
195
+ .INDENT 3.5
196
+ .sp
197
+ .EX
198
+ $ serve \e
199
+ \-l \e
200
+ \(dqtcp://hostname:1234\(dq
201
+ .EE
202
+ .UNINDENT
203
+ .UNINDENT
204
+ .sp
205
+ For UNIX domain socket endpoints:
206
+ .INDENT 0.0
207
+ .INDENT 3.5
208
+ .sp
209
+ .EX
210
+ $ serve \e
211
+ \-l \e
212
+ \(dqunix:/path/to/socket.sock\(dq
213
+ .EE
214
+ .UNINDENT
215
+ .UNINDENT
94
216
  .sp
95
- \-v \-\-verbose Enable verbose output.
96
- \-h Display help.
217
+ For Windows named pipe endpoints:
218
+ .INDENT 0.0
219
+ .INDENT 3.5
220
+ .sp
221
+ .EX
222
+ $ serve \e
223
+ \-l \e
224
+ \(dqpipe:\e\e.\epipe\ePipeName\(dq
225
+ .EE
226
+ .UNINDENT
227
+ .UNINDENT
97
228
  .SH Bugs
98
229
  .sp
99
- \%<https://\:github\:.com/\:themartiancompany/\:cert-tools/\:-/\:issues>
230
+ \%<https://\:github\:.com/\:themartiancompany/\:safely-serve/\:-/\:issues>
100
231
  .SH Copyright
101
232
  .sp
102
233
  Copyright Pellegrino Prevete. AGPL\-3.0.
103
234
  .SH See also
104
235
  .INDENT 0.0
105
236
  .IP \(bu 2
237
+ serve
238
+ .IP \(bu 2
239
+ cert\-gen
240
+ .IP \(bu 2
106
241
  node\-run
107
242
  .IP \(bu 2
108
243
  libcrash\-js
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name":
3
3
  "safely-serve",
4
4
  "version":
5
- "0.0.2",
5
+ "0.0.8",
6
6
  "description":
7
7
  "Serve extension which auto-generates self-signed OpenSSL certificates' at runtime",
8
8
  "funding": {
@@ -63,8 +63,6 @@
63
63
  "^0.0.3",
64
64
  "eslint":
65
65
  "^9.32.0",
66
- "parcel":
67
- "^2.16.0",
68
66
  "webpack":
69
67
  "^5.102.1",
70
68
  "webpack-cli":
@@ -103,7 +101,7 @@
103
101
  "publish-pnm":
104
102
  "npm run build && npm publish --access public",
105
103
  "start":
106
- "npx parcel serve index.html --port 3000 --https --no-cache",
104
+ "npx safely-serve -d .",
107
105
  "test":
108
106
  "echo \"Error: no test specified\" && exit 1"
109
107
  }