@jsenv/server 16.0.5 → 16.0.7

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.
@@ -121,6 +121,9 @@
121
121
  };
122
122
 
123
123
  const initialWebSocket = await connect();
124
+ if (!initialWebSocket) {
125
+ return;
126
+ }
124
127
  initialWebSocket.onclose = async () => {
125
128
  console.info("connection to server lost, trying to reconnect");
126
129
  const retry = async () => {
@@ -165,12 +168,16 @@
165
168
  );
166
169
  }
167
170
 
171
+ const websocketEndpoint =
172
+ document.currentScript.getAttribute("data-ws-endpoint") ||
173
+ "/.internal/alive.websocket";
174
+
168
175
  const jsenvAutoreloadOnServerRestartElement =
169
176
  new JsenvAutoreloadOnServerRestart({
170
177
  url: (() => {
171
178
  const websocketScheme =
172
179
  self.location.protocol === "https:" ? "wss" : "ws";
173
- const websocketUrl = `${websocketScheme}://${self.location.host}/.internal/alive.websocket`;
180
+ const websocketUrl = `${websocketScheme}://${self.location.host}${websocketEndpoint}`;
174
181
  return websocketUrl;
175
182
  })(),
176
183
  });
@@ -278,6 +278,9 @@
278
278
  };
279
279
 
280
280
  const initialWebSocket = await connect();
281
+ if (!initialWebSocket) {
282
+ return;
283
+ }
281
284
  initialWebSocket.onclose = async () => {
282
285
  console.info("connection to server lost, trying to reconnect");
283
286
  const retry = async () => {
@@ -322,12 +325,16 @@
322
325
  );
323
326
  }
324
327
 
328
+ const websocketEndpoint =
329
+ document.currentScript.getAttribute("data-ws-endpoint") ||
330
+ "/.internal/alive.websocket";
331
+
325
332
  const jsenvAutoreloadOnServerRestartElement =
326
333
  new JsenvAutoreloadOnServerRestart({
327
334
  url: (() => {
328
335
  const websocketScheme =
329
336
  self.location.protocol === "https:" ? "wss" : "ws";
330
- const websocketUrl = `${websocketScheme}://${self.location.host}/.internal/alive.websocket`;
337
+ const websocketUrl = `${websocketScheme}://${self.location.host}${websocketEndpoint}`;
331
338
  return websocketUrl;
332
339
  })(),
333
340
  });
@@ -1,6 +1,3 @@
1
- import process$1 from "node:process";
2
- import os, { networkInterfaces } from "node:os";
3
- import tty from "node:tty";
4
1
  import cluster from "node:cluster";
5
2
  import net, { Socket, createServer, isIP } from "node:net";
6
3
  import { extname } from "node:path";
@@ -12,6 +9,7 @@ import { createReadStream, existsSync, readFileSync, readdirSync, lstatSync, sta
12
9
  import { createRequire } from "node:module";
13
10
  import { fileURLToPath, pathToFileURL } from "node:url";
14
11
  import { lookup } from "node:dns";
12
+ import { networkInterfaces } from "node:os";
15
13
  import { createHash } from "node:crypto";
16
14
 
17
15
  const createCallbackListNotifiedOnce = () => {
@@ -523,407 +521,36 @@ const SIGINT_CALLBACK = {
523
521
  },
524
522
  };
525
523
 
526
- // From: https://github.com/sindresorhus/has-flag/blob/main/index.js
527
- /// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
528
- function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process$1.argv) {
529
- const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
530
- const position = argv.indexOf(prefix + flag);
531
- const terminatorPosition = argv.indexOf('--');
532
- return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
533
- }
534
-
535
- const {env} = process$1;
536
-
537
- let flagForceColor;
538
- if (
539
- hasFlag('no-color')
540
- || hasFlag('no-colors')
541
- || hasFlag('color=false')
542
- || hasFlag('color=never')
543
- ) {
544
- flagForceColor = 0;
545
- } else if (
546
- hasFlag('color')
547
- || hasFlag('colors')
548
- || hasFlag('color=true')
549
- || hasFlag('color=always')
550
- ) {
551
- flagForceColor = 1;
552
- }
553
-
554
- function envForceColor() {
555
- if (!('FORCE_COLOR' in env)) {
556
- return;
557
- }
558
-
559
- if (env.FORCE_COLOR === 'true') {
560
- return 1;
561
- }
562
-
563
- if (env.FORCE_COLOR === 'false') {
564
- return 0;
565
- }
566
-
567
- if (env.FORCE_COLOR.length === 0) {
568
- return 1;
569
- }
570
-
571
- const level = Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
572
-
573
- if (![0, 1, 2, 3].includes(level)) {
574
- return;
575
- }
576
-
577
- return level;
578
- }
579
-
580
- function translateLevel(level) {
581
- if (level === 0) {
582
- return false;
583
- }
584
-
585
- return {
586
- level,
587
- hasBasic: true,
588
- has256: level >= 2,
589
- has16m: level >= 3,
590
- };
591
- }
592
-
593
- function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
594
- const noFlagForceColor = envForceColor();
595
- if (noFlagForceColor !== undefined) {
596
- flagForceColor = noFlagForceColor;
597
- }
598
-
599
- const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
600
-
601
- if (forceColor === 0) {
602
- return 0;
603
- }
604
-
605
- if (sniffFlags) {
606
- if (hasFlag('color=16m')
607
- || hasFlag('color=full')
608
- || hasFlag('color=truecolor')) {
609
- return 3;
610
- }
611
-
612
- if (hasFlag('color=256')) {
613
- return 2;
614
- }
615
- }
616
-
617
- // Check for Azure DevOps pipelines.
618
- // Has to be above the `!streamIsTTY` check.
619
- if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
620
- return 1;
621
- }
622
-
623
- if (haveStream && !streamIsTTY && forceColor === undefined) {
624
- return 0;
625
- }
626
-
627
- const min = forceColor || 0;
628
-
629
- if (env.TERM === 'dumb') {
630
- return min;
631
- }
632
-
633
- if (process$1.platform === 'win32') {
634
- // Windows 10 build 10586 is the first Windows release that supports 256 colors.
635
- // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
636
- const osRelease = os.release().split('.');
637
- if (
638
- Number(osRelease[0]) >= 10
639
- && Number(osRelease[2]) >= 10_586
640
- ) {
641
- return Number(osRelease[2]) >= 14_931 ? 3 : 2;
642
- }
643
-
644
- return 1;
645
- }
646
-
647
- if ('CI' in env) {
648
- if (['GITHUB_ACTIONS', 'GITEA_ACTIONS', 'CIRCLECI'].some(key => key in env)) {
649
- return 3;
650
- }
651
-
652
- if (['TRAVIS', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
653
- return 1;
654
- }
655
-
656
- return min;
657
- }
658
-
659
- if ('TEAMCITY_VERSION' in env) {
660
- return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
661
- }
662
-
663
- if (env.COLORTERM === 'truecolor') {
664
- return 3;
665
- }
666
-
667
- if (env.TERM === 'xterm-kitty') {
668
- return 3;
669
- }
670
-
671
- if ('TERM_PROGRAM' in env) {
672
- const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
673
-
674
- switch (env.TERM_PROGRAM) {
675
- case 'iTerm.app': {
676
- return version >= 3 ? 3 : 2;
677
- }
678
-
679
- case 'Apple_Terminal': {
680
- return 2;
681
- }
682
- // No default
683
- }
684
- }
685
-
686
- if (/-256(color)?$/i.test(env.TERM)) {
687
- return 2;
688
- }
689
-
690
- if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
691
- return 1;
692
- }
693
-
694
- if ('COLORTERM' in env) {
695
- return 1;
696
- }
697
-
698
- return min;
699
- }
700
-
701
- function createSupportsColor(stream, options = {}) {
702
- const level = _supportsColor(stream, {
703
- streamIsTTY: stream && stream.isTTY,
704
- ...options,
705
- });
706
-
707
- return translateLevel(level);
708
- }
709
-
710
- ({
711
- stdout: createSupportsColor({isTTY: tty.isatty(1)}),
712
- stderr: createSupportsColor({isTTY: tty.isatty(2)}),
713
- });
714
-
715
- function isUnicodeSupported() {
716
- const {env} = process$1;
717
- const {TERM, TERM_PROGRAM} = env;
718
-
719
- if (process$1.platform !== 'win32') {
720
- return TERM !== 'linux'; // Linux console (kernel)
721
- }
722
-
723
- return Boolean(env.WT_SESSION) // Windows Terminal
724
- || Boolean(env.TERMINUS_SUBLIME) // Terminus (<0.2.27)
725
- || env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder
726
- || TERM_PROGRAM === 'Terminus-Sublime'
727
- || TERM_PROGRAM === 'vscode'
728
- || TERM === 'xterm-256color'
729
- || TERM === 'alacritty'
730
- || TERM === 'rxvt-unicode'
731
- || TERM === 'rxvt-unicode-256color'
732
- || env.TERMINAL_EMULATOR === 'JetBrains-JediTerm';
733
- }
734
-
735
- /* globals WorkerGlobalScope, DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, ServiceWorkerGlobalScope */
736
-
737
- const isBrowser = globalThis.window?.document !== undefined;
738
-
739
- globalThis.process?.versions?.node !== undefined;
740
-
741
- globalThis.process?.versions?.bun !== undefined;
742
-
743
- globalThis.Deno?.version?.deno !== undefined;
744
-
745
- globalThis.process?.versions?.electron !== undefined;
746
-
747
- globalThis.navigator?.userAgent?.includes('jsdom') === true;
748
-
749
- typeof WorkerGlobalScope !== 'undefined' && globalThis instanceof WorkerGlobalScope;
750
-
751
- typeof DedicatedWorkerGlobalScope !== 'undefined' && globalThis instanceof DedicatedWorkerGlobalScope;
752
-
753
- typeof SharedWorkerGlobalScope !== 'undefined' && globalThis instanceof SharedWorkerGlobalScope;
754
-
755
- typeof ServiceWorkerGlobalScope !== 'undefined' && globalThis instanceof ServiceWorkerGlobalScope;
756
-
757
- // Note: I'm intentionally not DRYing up the other variables to keep them "lazy".
758
- const platform = globalThis.navigator?.userAgentData?.platform;
759
-
760
- platform === 'macOS'
761
- || globalThis.navigator?.platform === 'MacIntel' // Even on Apple silicon Macs.
762
- || globalThis.navigator?.userAgent?.includes(' Mac ') === true
763
- || globalThis.process?.platform === 'darwin';
764
-
765
- platform === 'Windows'
766
- || globalThis.navigator?.platform === 'Win32'
767
- || globalThis.process?.platform === 'win32';
768
-
769
- platform === 'Linux'
770
- || globalThis.navigator?.platform?.startsWith('Linux') === true
771
- || globalThis.navigator?.userAgent?.includes(' Linux ') === true
772
- || globalThis.process?.platform === 'linux';
773
-
774
- platform === 'Android'
775
- || globalThis.navigator?.platform === 'Android'
776
- || globalThis.navigator?.userAgent?.includes(' Android ') === true
777
- || globalThis.process?.platform === 'android';
778
-
779
- !isBrowser && process$1.env.TERM_PROGRAM === 'Apple_Terminal';
780
- !isBrowser && process$1.platform === 'win32';
781
-
782
- isBrowser ? () => {
783
- throw new Error('`process.cwd()` only works in Node.js, not the browser.');
784
- } : process$1.cwd;
785
-
786
524
  const createDetailedMessage = (message, details = {}) => {
787
- let string = `${message}`;
788
-
789
- Object.keys(details).forEach((key) => {
790
- const value = details[key];
791
- string += `
792
- --- ${key} ---
525
+ let text = `${message}`;
526
+ const namedSectionsText = renderNamedSections(details);
527
+ if (namedSectionsText) {
528
+ text += `
529
+ ${namedSectionsText}`;
530
+ }
531
+ return text;
532
+ };
533
+
534
+ const renderNamedSections = (namedSections) => {
535
+ let text = "";
536
+ let keys = Object.keys(namedSections);
537
+ for (const key of keys) {
538
+ const isLastKey = key === keys[keys.length - 1];
539
+ const value = namedSections[key];
540
+ text += `--- ${key} ---
793
541
  ${
794
542
  Array.isArray(value)
795
543
  ? value.join(`
796
544
  `)
797
545
  : value
798
546
  }`;
799
- });
800
-
801
- return string;
802
- };
803
-
804
- // https://github.com/Marak/colors.js/blob/master/lib/styles.js
805
- // https://stackoverflow.com/a/75985833/2634179
806
- const RESET = "\x1b[0m";
807
-
808
- const createAnsi = ({ supported }) => {
809
- const ANSI = {
810
- supported,
811
-
812
- RED: "\x1b[31m",
813
- GREEN: "\x1b[32m",
814
- YELLOW: "\x1b[33m",
815
- BLUE: "\x1b[34m",
816
- MAGENTA: "\x1b[35m",
817
- CYAN: "\x1b[36m",
818
- GREY: "\x1b[90m",
819
- color: (text, color) => {
820
- if (!ANSI.supported) {
821
- return text;
822
- }
823
- if (!color) {
824
- return text;
825
- }
826
- if (typeof text === "string" && text.trim() === "") {
827
- // cannot set color of blank chars
828
- return text;
829
- }
830
- return `${color}${text}${RESET}`;
831
- },
832
-
833
- BOLD: "\x1b[1m",
834
- UNDERLINE: "\x1b[4m",
835
- STRIKE: "\x1b[9m",
836
- effect: (text, effect) => {
837
- if (!ANSI.supported) {
838
- return text;
839
- }
840
- if (!effect) {
841
- return text;
842
- }
843
- // cannot add effect to empty string
844
- if (text === "") {
845
- return text;
846
- }
847
- return `${effect}${text}${RESET}`;
848
- },
849
- };
850
-
851
- return ANSI;
852
- };
853
-
854
- const processSupportsBasicColor = createSupportsColor(process.stdout).hasBasic;
855
-
856
- const ANSI = createAnsi({
857
- supported:
858
- process.env.FORCE_COLOR === "1" ||
859
- processSupportsBasicColor ||
860
- // GitHub workflow does support ANSI but "supports-color" returns false
861
- // because stream.isTTY returns false, see https://github.com/actions/runner/issues/241
862
- process.env.GITHUB_WORKFLOW,
863
- });
864
-
865
- // see also https://github.com/sindresorhus/figures
866
-
867
- const createUnicode = ({ supported, ANSI }) => {
868
- const UNICODE = {
869
- supported,
870
- get COMMAND_RAW() {
871
- return UNICODE.supported ? `❯` : `>`;
872
- },
873
- get OK_RAW() {
874
- return UNICODE.supported ? `✔` : `√`;
875
- },
876
- get FAILURE_RAW() {
877
- return UNICODE.supported ? `✖` : `×`;
878
- },
879
- get DEBUG_RAW() {
880
- return UNICODE.supported ? `◆` : `♦`;
881
- },
882
- get INFO_RAW() {
883
- return UNICODE.supported ? `ℹ` : `i`;
884
- },
885
- get WARNING_RAW() {
886
- return UNICODE.supported ? `⚠` : `‼`;
887
- },
888
- get CIRCLE_CROSS_RAW() {
889
- return UNICODE.supported ? `ⓧ` : `(×)`;
890
- },
891
- get CIRCLE_DOTTED_RAW() {
892
- return UNICODE.supported ? `◌` : `*`;
893
- },
894
- get COMMAND() {
895
- return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
896
- },
897
- get OK() {
898
- return ANSI.color(UNICODE.OK_RAW, ANSI.GREEN);
899
- },
900
- get FAILURE() {
901
- return ANSI.color(UNICODE.FAILURE_RAW, ANSI.RED);
902
- },
903
- get DEBUG() {
904
- return ANSI.color(UNICODE.DEBUG_RAW, ANSI.GREY);
905
- },
906
- get INFO() {
907
- return ANSI.color(UNICODE.INFO_RAW, ANSI.BLUE);
908
- },
909
- get WARNING() {
910
- return ANSI.color(UNICODE.WARNING_RAW, ANSI.YELLOW);
911
- },
912
- get CIRCLE_CROSS() {
913
- return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
914
- },
915
- get ELLIPSIS() {
916
- return UNICODE.supported ? `…` : `...`;
917
- },
918
- };
919
- return UNICODE;
547
+ if (!isLastKey) {
548
+ text += "\n";
549
+ }
550
+ }
551
+ return text;
920
552
  };
921
553
 
922
- createUnicode({
923
- supported: process.env.FORCE_UNICODE === "1" || isUnicodeSupported(),
924
- ANSI,
925
- });
926
-
927
554
  const LOG_LEVEL_OFF = "off";
928
555
 
929
556
  const LOG_LEVEL_DEBUG = "debug";
@@ -6493,7 +6120,7 @@ const replacePlaceholdersInHtml = (html, replacers) => {
6493
6120
  });
6494
6121
  };
6495
6122
 
6496
- const clientErrorHtmlTemplateFileUrl = import.meta.resolve("./client/4xx/4xx.html");
6123
+ const clientErrorHtmlTemplateFileUrl = import.meta.resolve("./client/default_body_4xx_5xx/4xx.html");
6497
6124
 
6498
6125
  const jsenvServiceDefaultBody4xx5xx = () => {
6499
6126
  return {
@@ -7816,7 +7443,7 @@ const PROCESS_TEARDOWN_EVENTS_MAP = {
7816
7443
  exit: STOP_REASON_PROCESS_EXIT,
7817
7444
  };
7818
7445
 
7819
- const internalErrorHtmlFileUrl = import.meta.resolve("./client/500/500.html");
7446
+ const internalErrorHtmlFileUrl = import.meta.resolve("./client/error_handler/500.html");
7820
7447
 
7821
7448
  const jsenvServiceErrorHandler = ({ sendErrorDetails = false } = {}) => {
7822
7449
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/server",
3
- "version": "16.0.5",
3
+ "version": "16.0.7",
4
4
  "description": "A modern Node.js HTTP server with declarative routing, content negotiation, and WebSocket support",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -41,9 +41,9 @@
41
41
  "dependencies": {
42
42
  "launch-editor": "2.10.0",
43
43
  "@jsenv/abort": "4.3.1",
44
- "@jsenv/humanize": "1.3.1",
44
+ "@jsenv/humanize": "1.4.0",
45
45
  "@jsenv/url-meta": "8.5.6",
46
- "@jsenv/utils": "2.2.1",
46
+ "@jsenv/utils": "2.3.0",
47
47
  "ws": "8.18.1",
48
48
  "formidable": "3.5.2"
49
49
  },
@@ -53,6 +53,9 @@ class JsenvAutoreloadOnServerRestart extends HTMLElement {
53
53
  };
54
54
 
55
55
  const initialWebSocket = await connect();
56
+ if (!initialWebSocket) {
57
+ return;
58
+ }
56
59
  initialWebSocket.onclose = async () => {
57
60
  console.info("connection to server lost, trying to reconnect");
58
61
  const retry = async () => {
@@ -97,12 +100,16 @@ if (!customElements.get("jsenv-autoreload-on-server-restart")) {
97
100
  );
98
101
  }
99
102
 
103
+ const websocketEndpoint =
104
+ document.currentScript.getAttribute("data-ws-endpoint") ||
105
+ "/.internal/alive.websocket";
106
+
100
107
  const jsenvAutoreloadOnServerRestartElement =
101
108
  new JsenvAutoreloadOnServerRestart({
102
109
  url: (() => {
103
110
  const websocketScheme =
104
111
  self.location.protocol === "https:" ? "wss" : "ws";
105
- const websocketUrl = `${websocketScheme}://${self.location.host}/.internal/alive.websocket`;
112
+ const websocketUrl = `${websocketScheme}://${self.location.host}${websocketEndpoint}`;
106
113
  return websocketUrl;
107
114
  })(),
108
115
  });
File without changes