safely-serve 0.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.
package/safely-serve ADDED
@@ -0,0 +1,503 @@
1
+ #!/usr/bin/env node
2
+
3
+ // SPDX-License-Identifier: AGPL-3.0-or-later
4
+
5
+ // ----------------------------------------------------------------------
6
+ // Copyright © 2024, 2025 Pellegrino Prevete
7
+ //
8
+ // All rights reserved
9
+ // ----------------------------------------------------------------------
10
+ //
11
+ // This program is free software: you can redistribute it and/or modify
12
+ // it under the terms of the GNU Affero General Public License as published by
13
+ // the Free Software Foundation, either version 3 of the License, or
14
+ // (at your option) any later version.
15
+ //
16
+ // This program is distributed in the hope that it will be useful,
17
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ // GNU Affero General Public License for more details.
20
+ //
21
+ // You should have received a copy of the GNU Affero General Public License
22
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
23
+
24
+ const
25
+ _os =
26
+ require(
27
+ "os");
28
+ _homedir =
29
+ _os.homedir;
30
+ const
31
+ _libcrash =
32
+ require(
33
+ 'crash-js');
34
+ _cmdline_check =
35
+ _libcrash._cmdline_check;
36
+ _msg_info =
37
+ _libcrash._msg_info;
38
+ _msg_error =
39
+ _libcrash._msg_error;
40
+ const
41
+ _libsafely_serve_module =
42
+ require(
43
+ "./safely-serve");
44
+ _safely_serve =
45
+ _libsafely_serve_module._safely_serve;
46
+
47
+ function
48
+ _global_variables() {
49
+ app_name =
50
+ "safely-serve";
51
+ target_certificate =
52
+ "";
53
+ target_directory =
54
+ "";
55
+ key_private =
56
+ "";
57
+ keys_length =
58
+ "";
59
+ serial_number =
60
+ "";
61
+ validity_min =
62
+ "";
63
+ validity_max =
64
+ "";
65
+ domain_name =
66
+ "";
67
+ country_name =
68
+ "";
69
+ state_name =
70
+ "";
71
+ locality_name =
72
+ "";
73
+ organization_name =
74
+ "";
75
+ organization_unit =
76
+ "";
77
+ output_file =
78
+ "";
79
+ quiet =
80
+ "";
81
+ }
82
+
83
+ function
84
+ _target_tertificate_auto_detect() {
85
+ let
86
+ _encoded_target,
87
+ _target_certificate;
88
+ _target_certificate =
89
+ target_certificate;
90
+ if ( _target_certificate == "" ) {
91
+ _encoded_target =
92
+ _sha256sum(
93
+ target_directory);
94
+ _target_certificate =
95
+ _homedir() +
96
+ `/.cache/safely-serve/${app_name}/${_encoded_target}`;
97
+ }
98
+ return _target_certificate;
99
+ }
100
+
101
+ function
102
+ _overrides_set() {
103
+ target_certificate =
104
+ _target_certificate_auto_detect();
105
+ if ( validity_min == "" ) {
106
+ validity_min =
107
+ new Date();
108
+ }
109
+ if ( validity_max == "" ) {
110
+ validity_max =
111
+ new Date();
112
+ validity_max.setFullYear(
113
+ validity_min.getFullYear() +
114
+ 1);
115
+ }
116
+ if ( serial_number == "" ) {
117
+ serial_number =
118
+ "01";
119
+ }
120
+ if ( domain_name == "" ) {
121
+ domain_name =
122
+ "localhost";
123
+ }
124
+ if ( country_name == "" ) {
125
+ country_name =
126
+ "LP";
127
+ }
128
+ if ( state_name == "" ) {
129
+ state_name =
130
+ "Nananapoli";
131
+ }
132
+ if ( locality_name == "" ) {
133
+ locality_name =
134
+ "Pollena Trocchia";
135
+ }
136
+ if ( organization_name == "" ) {
137
+ organization_name =
138
+ "The Martian Company";
139
+ }
140
+ if ( organization_unit == "" ) {
141
+ organization_unit =
142
+ "Index Development Team";
143
+ }
144
+ if ( key_private == "" ) {
145
+ key_private =
146
+ process.cwd() +
147
+ "/key.pem";
148
+ }
149
+ if ( output_file == "" ) {
150
+ output_file =
151
+ process.cwd() +
152
+ "/cert.pem";
153
+ }
154
+ }
155
+
156
+ function
157
+ _config_show() {
158
+ let
159
+ _line,
160
+ _text;
161
+ _text = [
162
+ `${app_name} configuration:`,
163
+ ` Runtime environment: ${runtime_environment}`,
164
+ ` Target directory: ${target_directory}`,
165
+ ` Key length: ${keys_length}`,
166
+ ` Serial number: ${serial_number}`,
167
+ ` Valid after: ${validity_min}`,
168
+ ` Expires on: ${validity_max}`,
169
+ ` Domain name: ${domain_name}`,
170
+ ` Country: ${country_name}`,
171
+ ` State: ${state_name}`,
172
+ ` Locality: ${locality_name}`,
173
+ ` Organization: ${organization_name}`,
174
+ ` Organization unit: ${organization_unit}`,
175
+ ` Output file: ${output_file}`,
176
+ ];
177
+ for ( _line of _text ) {
178
+ _msg_info(
179
+ _line);
180
+ }
181
+ }
182
+
183
+ function
184
+ _usage(
185
+ _exit_code) {
186
+ let
187
+ _line,
188
+ _text;
189
+ _text = [
190
+ "",
191
+ "Serve extension which self-generate a SSL certificate at runtime.",
192
+ "",
193
+ "Usage:",
194
+ "",
195
+ " safely-serve",
196
+ " [options]",
197
+ " [serve-options]",
198
+ "",
199
+ " options:",
200
+ " -i, --key-private=FILE Use existing private key.",
201
+ ` Default: '${key_private}'`,
202
+ " -l, --keys-length=N Keys pair length.",
203
+ ` Default: '${keys_length}'`,
204
+ " -o, --output-file=FILE Set output file.",
205
+ ` Default: '${output_file}'`,
206
+ "",
207
+ " validity options:",
208
+ " -B, --validity-start=DATE Set activation date.",
209
+ ` Default: '${validity_min}'`,
210
+ " -E, --validity-end=DATE Set expiration date.",
211
+ ` Default: '${validity_max}'`,
212
+ "",
213
+ " identity options:",
214
+ " -D, --domain-name=DOMAIN Set domain name.",
215
+ ` Default: '${domain_name}'`,
216
+ " -C, --country-name=COUNTRY Set country name.",
217
+ ` Default: '${country_name}'`,
218
+ " -S, --state-name=STATE Set state name.",
219
+ ` Default: '${state_name}'`,
220
+
221
+ " -L, --locality-name=LOCALITY Set locality name.",
222
+ ` Default: '${locality_name}'`,
223
+ " -O, --organization-name=ORG Set organization name.",
224
+ ` Default: '${organization_name}'`,
225
+ " -U, --organization-unit=UNIT Set organization unit.",
226
+ ` Default: '${organization_unit}'`,
227
+ " -s, --serial-number=SERIAL Set serial number.",
228
+ ` Default: '${serial_number}'`,
229
+ "",
230
+ " -h --help This message.",
231
+ " -v --verbose Enable verbose output.",
232
+ "",
233
+ ];
234
+ for ( _line of _text ) {
235
+ console.log(
236
+ _line);
237
+ }
238
+ process.exit(
239
+ _exit_code);
240
+ }
241
+
242
+ function
243
+ _cmdline_parse_help(
244
+ _argv) {
245
+ let
246
+ _help_display;
247
+ _help_display =
248
+ _argv[
249
+ "help"] ||
250
+ _argv[
251
+ "h"];
252
+ if ( _help_display ) {
253
+ quiet =
254
+ "n";
255
+ _overrides_set();
256
+ _usage(
257
+ 0);
258
+ }
259
+ }
260
+
261
+ function
262
+ _input_args_check(
263
+ _argv) {
264
+ if ( _argv[
265
+ "_"].length < 1 ) {
266
+ _msg =
267
+ "Target directory required.";
268
+ _msg_error(
269
+ _msg,
270
+ 0);
271
+ _usage(
272
+ 1);
273
+ }
274
+ }
275
+
276
+ function
277
+ _cmdline_parse() {
278
+ let
279
+ _argv,
280
+ _msg;
281
+ _argv =
282
+ _yargv_get();
283
+ quiet =
284
+ "y";
285
+ runtime_environment =
286
+ "node";
287
+ _cmdline_parse_help(
288
+ _argv);
289
+ // _input_args_check(
290
+ // _argv);
291
+ // input_file =
292
+ // _argv[
293
+ // "_"][
294
+ // 0];
295
+ if ( "i" in _argv ) {
296
+ key_private =
297
+ _argv[
298
+ "i"];
299
+ }
300
+ if ( "key-private" in _argv ) {
301
+ key_private =
302
+ _argv[
303
+ "key-private"];
304
+ }
305
+ if ( "l" in _argv ) {
306
+ keys_length =
307
+ _argv[
308
+ "l"];
309
+ }
310
+ if ( "keys-length" in _argv ) {
311
+ keys_length =
312
+ _argv[
313
+ "keys-length"];
314
+ }
315
+ if ( "B" in _argv ) {
316
+ validity_min =
317
+ _argv[
318
+ "B"];
319
+ }
320
+ if ( "validity-max" in _argv ) {
321
+ validity_max =
322
+ _argv[
323
+ "validity-max"];
324
+ }
325
+ if ( "D" in _argv ) {
326
+ domain_name =
327
+ _argv[
328
+ "D"];
329
+ }
330
+ if ( "domain-name" in _argv ) {
331
+ domain_name =
332
+ _argv[
333
+ "domain-name"];
334
+ }
335
+ if ( "C" in _argv ) {
336
+ country_name =
337
+ _argv[
338
+ "C"];
339
+ }
340
+ if ( "country-name" in _argv ) {
341
+ country_name =
342
+ _argv[
343
+ "country-name"];
344
+ }
345
+ if ( "S" in _argv ) {
346
+ state_name =
347
+ _argv[
348
+ "S"];
349
+ }
350
+ if ( "state-name" in _argv ) {
351
+ country_name =
352
+ _argv[
353
+ "state-name"];
354
+ }
355
+ if ( "L" in _argv ) {
356
+ locality_name =
357
+ _argv[
358
+ "L"];
359
+ }
360
+ if ( "locality-name" in _argv ) {
361
+ locality_name =
362
+ _argv[
363
+ "locality-name"];
364
+ }
365
+ if ( "O" in _argv ) {
366
+ organization_name =
367
+ _argv[
368
+ "O"];
369
+ }
370
+ if ( "organization-name" in _argv ) {
371
+ organization_name =
372
+ _argv[
373
+ "organization-name"];
374
+ }
375
+ if ( "U" in _argv ) {
376
+ organization_unit =
377
+ _argv[
378
+ "U"];
379
+ }
380
+ if ( "organization-unit" in _argv ) {
381
+ organization_unit =
382
+ _argv[
383
+ "organization-unit"];
384
+ }
385
+ if ( "o" in _argv ) {
386
+ output_file =
387
+ _argv[
388
+ "o"];
389
+ }
390
+ if ( "output-file" in _argv ) {
391
+ output_file =
392
+ _argv[
393
+ "output-file"];
394
+ }
395
+ if ( "s" in _argv ) {
396
+ serial_number =
397
+ _argv[
398
+ "s"];
399
+ }
400
+ if ( "serial-number" in _argv ) {
401
+ serial_number =
402
+ _argv[
403
+ "serial-number"];
404
+ }
405
+ verbose =
406
+ _argv[
407
+ "verbose"] ||
408
+ _argv[
409
+ "v"];
410
+ if ( verbose ) {
411
+ quiet =
412
+ "n";
413
+ }
414
+ }
415
+
416
+ function
417
+ _url_parse() {
418
+ let
419
+ _argv;
420
+ runtime_environment =
421
+ "browser";
422
+ _argv =
423
+ _argv_url_get();
424
+ key_private =
425
+ _argv_url_get(
426
+ "key_private")[
427
+ 0];
428
+ keys_length =
429
+ _argv_url_get(
430
+ "keys_length")[
431
+ 0];
432
+ output_file =
433
+ _argv_url_get(
434
+ "output_file")[
435
+ 0];
436
+ validity_min =
437
+ _argv_url_get(
438
+ "validity_min")[
439
+ 0];
440
+ validity_max =
441
+ _argv_url_get(
442
+ "validity_max")[
443
+ 0];
444
+ country_name =
445
+ _argv_url_get(
446
+ "country_name")[
447
+ 0];
448
+ state_name =
449
+ _argv_url_get(
450
+ "state_name")[
451
+ 0];
452
+ locality_name =
453
+ _argv_url_get(
454
+ "locality_name")[
455
+ 0];
456
+ organization_name =
457
+ _argv_url_get(
458
+ "organization_name")[
459
+ 0];
460
+ organization_unit =
461
+ _argv_url_get(
462
+ "organization_unit")[
463
+ 0];
464
+ serial_number =
465
+ _argv_url_get(
466
+ "serial_number")[
467
+ 0];
468
+ quiet =
469
+ _argv_url_get(
470
+ "quiet");
471
+ if ( quiet.length === 0 ) {
472
+ quiet =
473
+ "n";
474
+ }
475
+ }
476
+
477
+ _global_variables();
478
+ if ( _cmdline_check(
479
+ "cert-gen") == true ) {
480
+ _cmdline_parse();
481
+ }
482
+ else {
483
+ _url_parse();
484
+ }
485
+ _overrides_set();
486
+ _config_show();
487
+ app_opts = [
488
+ key_private,
489
+ keys_length,
490
+ serial_number,
491
+ validity_min,
492
+ validity_max,
493
+ domain_name,
494
+ country_name,
495
+ state_name,
496
+ locality_name,
497
+ organization_name,
498
+ organization_unit,
499
+ output_file,
500
+ ];
501
+ _safely_serve.apply(
502
+ null,
503
+ app_opts);
@@ -0,0 +1,117 @@
1
+ const
2
+ _path =
3
+ require(
4
+ 'path');
5
+ const
6
+ _output_dir =
7
+ _path.resolve(
8
+ __dirname);
9
+ const
10
+ _input_file_name =
11
+ `safely-serve`;
12
+ const
13
+ _input_file_path =
14
+ `./${_input_file_name}`;
15
+ const
16
+ _output_file_name =
17
+ `${_input_file_name}.js`;
18
+ const
19
+ _output = {
20
+ path:
21
+ _output_dir,
22
+ filename:
23
+ _output_file_name
24
+ };
25
+ const
26
+ _utils_ignore =
27
+ { resourceRegExp:
28
+ /^utils$/ };
29
+ const
30
+ _web_worker_ignore =
31
+ { resourceRegExp:
32
+ /^web-worker$/ };
33
+ const
34
+ _yargs_ignore =
35
+ { resourceRegExp:
36
+ /^yargs$/ };
37
+ const
38
+ _yargs_helpers_ignore =
39
+ { resourceRegExp:
40
+ /^yargs\/helpers$/ };
41
+ const
42
+ _webpack =
43
+ require(
44
+ "webpack");
45
+ const
46
+ _ignore_plugin =
47
+ _webpack.IgnorePlugin;
48
+ const
49
+ _utils_ignore_plugin =
50
+ new _ignore_plugin(
51
+ _utils_ignore);
52
+ const
53
+ _web_worker_ignore_plugin =
54
+ new _ignore_plugin(
55
+ _web_worker_ignore);
56
+ const
57
+ _yargs_ignore_plugin =
58
+ new _ignore_plugin(
59
+ _yargs_ignore);
60
+ const
61
+ _yargs_helpers_ignore_plugin =
62
+ new _ignore_plugin(
63
+ _yargs_helpers_ignore);
64
+ module.exports = {
65
+ entry:
66
+ _input_file_path,
67
+ output:
68
+ _output,
69
+ optimization: {
70
+ moduleIds: 'deterministic',
71
+ },
72
+ resolve: {
73
+ alias: {
74
+ "fs":
75
+ _path.resolve(
76
+ __dirname,
77
+ 'node_modules/fs/fs'),
78
+ "path":
79
+ _path.resolve(
80
+ __dirname,
81
+ 'node_modules/path/mod.js'),
82
+ "web-worker":
83
+ _path.resolve(
84
+ __dirname,
85
+ 'node_modules/web-worker/mod.js'),
86
+ "yargs":
87
+ _path.resolve(
88
+ __dirname,
89
+ 'node_modules/yargs/browser.mjs'),
90
+ "yargs/helpers":
91
+ _path.resolve(
92
+ __dirname,
93
+ 'node_modules/yargs/helpers/helpers.mjs'),
94
+ "yargs-parser":
95
+ _path.resolve(
96
+ __dirname,
97
+ 'node_modules/yargs-parser/browser.mjs'),
98
+ },
99
+ fallback: {
100
+ "utils":
101
+ false,
102
+ "web-worker":
103
+ false,
104
+ "yargs":
105
+ false,
106
+ "yargs/helpers":
107
+ false,
108
+ }
109
+ },
110
+ externals:
111
+ { yargs:
112
+ 'yargs' },
113
+ plugins: [
114
+ _yargs_ignore_plugin,
115
+ _yargs_helpers_ignore_plugin
116
+ ]
117
+ };