@jsenv/server 16.0.4 → 16.0.6

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.
@@ -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";
@@ -1718,7 +1345,7 @@ const fromNodeRequest = (
1718
1345
  `formData() called on a request with content-type: "${contentType}". multipart/form-data was expected.`,
1719
1346
  );
1720
1347
  }
1721
- const { formidable } = await import("./js/formidable_index.js");
1348
+ const { formidable } = await import("./formidable_index/formidable_index.js");
1722
1349
  const form = formidable({});
1723
1350
  nodeRequest.resume(); // was paused in line #53
1724
1351
  const [fields, files] = await form.parse(nodeRequest);
@@ -3895,9 +3522,19 @@ const applyPatternMatching = ({ url, pattern }) => {
3895
3522
  return applyPattern({ url, pattern });
3896
3523
  };
3897
3524
 
3898
- const resolveAssociations = (associations, baseUrl) => {
3899
- if (baseUrl && typeof baseUrl.href === "string") baseUrl = baseUrl.href;
3900
- assertUrlLike(baseUrl, "baseUrl");
3525
+ const resolveAssociations = (associations, resolver) => {
3526
+ let resolve = () => {};
3527
+ if (typeof resolver === "function") {
3528
+ resolve = resolver;
3529
+ } else if (typeof resolver === "string") {
3530
+ const baseUrl = resolver;
3531
+ assertUrlLike(baseUrl, "baseUrl");
3532
+ resolve = (pattern) => new URL(pattern, baseUrl).href;
3533
+ } else if (resolver && typeof resolver.href === "string") {
3534
+ const baseUrl = resolver.href;
3535
+ assertUrlLike(baseUrl, "baseUrl");
3536
+ resolve = (pattern) => new URL(pattern, baseUrl).href;
3537
+ }
3901
3538
 
3902
3539
  const associationsResolved = {};
3903
3540
  for (const key of Object.keys(associations)) {
@@ -3908,12 +3545,11 @@ const resolveAssociations = (associations, baseUrl) => {
3908
3545
  const valueAssociated = value[pattern];
3909
3546
  let patternResolved;
3910
3547
  try {
3911
- patternResolved = String(new URL(pattern, baseUrl));
3548
+ patternResolved = resolve(pattern);
3912
3549
  } catch {
3913
3550
  // it's not really an url, no need to perform url resolution nor encoding
3914
3551
  patternResolved = pattern;
3915
3552
  }
3916
-
3917
3553
  valueMapResolved[patternResolved] = valueAssociated;
3918
3554
  }
3919
3555
  associationsResolved[key] = valueMapResolved;
@@ -4036,34 +3672,29 @@ const urlChildMayMatch = ({ url, associations, predicate }) => {
4036
3672
  };
4037
3673
 
4038
3674
  const applyAliases = ({ url, aliases }) => {
4039
- let aliasFullMatchResult;
4040
- const aliasMatchingKey = Object.keys(aliases).find((key) => {
4041
- const aliasMatchResult = applyPatternMatching({
3675
+ for (const key of Object.keys(aliases)) {
3676
+ const matchResult = applyPatternMatching({
4042
3677
  pattern: key,
4043
3678
  url,
4044
3679
  });
4045
- if (aliasMatchResult.matched) {
4046
- aliasFullMatchResult = aliasMatchResult;
4047
- return true;
3680
+ if (!matchResult.matched) {
3681
+ continue;
4048
3682
  }
4049
- return false;
4050
- });
4051
- if (!aliasMatchingKey) {
4052
- return url;
4053
- }
4054
- const { matchGroups } = aliasFullMatchResult;
4055
- const alias = aliases[aliasMatchingKey];
4056
- const parts = alias.split("*");
4057
- let newUrl = "";
4058
- let index = 0;
4059
- for (const part of parts) {
4060
- newUrl += `${part}`;
4061
- if (index < parts.length - 1) {
4062
- newUrl += matchGroups[index];
3683
+ const { matchGroups } = matchResult;
3684
+ const alias = aliases[key];
3685
+ const parts = alias.split("*");
3686
+ let newUrl = "";
3687
+ let index = 0;
3688
+ for (const part of parts) {
3689
+ newUrl += `${part}`;
3690
+ if (index < parts.length - 1) {
3691
+ newUrl += matchGroups[index];
3692
+ }
3693
+ index++;
4063
3694
  }
4064
- index++;
3695
+ return newUrl;
4065
3696
  }
4066
- return newUrl;
3697
+ return url;
4067
3698
  };
4068
3699
 
4069
3700
  const matches = (url, patterns) => {
@@ -6489,7 +6120,7 @@ const replacePlaceholdersInHtml = (html, replacers) => {
6489
6120
  });
6490
6121
  };
6491
6122
 
6492
- const clientErrorHtmlTemplateFileUrl = import.meta.resolve("./html/4xx.html");
6123
+ const clientErrorHtmlTemplateFileUrl = import.meta.resolve("./client/default_body_4xx_5xx/4xx.html");
6493
6124
 
6494
6125
  const jsenvServiceDefaultBody4xx5xx = () => {
6495
6126
  return {
@@ -6568,7 +6199,7 @@ const generateBadStatusResponse = (
6568
6199
  );
6569
6200
  };
6570
6201
 
6571
- const jsenvServerRootDirectoryUrl = import.meta.resolve("../");
6202
+ const jsenvServerRootDirectoryUrl = import.meta.resolve("./");
6572
6203
 
6573
6204
  const jsenvServiceInternalClientFiles = () => {
6574
6205
  return {
@@ -6636,7 +6267,7 @@ const jsenvServiceOpenFile = () => {
6636
6267
  };
6637
6268
 
6638
6269
  const routeInspectorHtmlFileUrl = import.meta.resolve(
6639
- "./html/route_inspector.html",
6270
+ "./client/route_inspector/route_inspector.html",
6640
6271
  );
6641
6272
 
6642
6273
  const jsenvServiceRouteInspector = () => {
@@ -7627,7 +7258,7 @@ const startServer = async ({
7627
7258
  const webSocketSet = new Set();
7628
7259
  let upgradeRequestToWebSocket;
7629
7260
  const loadUpgradeRequestToWebSocket = async () => {
7630
- const { WebSocketServer } = await import("./js/ws.js");
7261
+ const { WebSocketServer } = await import("./ws/ws.js");
7631
7262
  let webSocketServer = new WebSocketServer({ noServer: true });
7632
7263
  stopCallbackSet.add(() => {
7633
7264
  webSocketServer.close();
@@ -7812,7 +7443,7 @@ const PROCESS_TEARDOWN_EVENTS_MAP = {
7812
7443
  exit: STOP_REASON_PROCESS_EXIT,
7813
7444
  };
7814
7445
 
7815
- const internalErrorHtmlFileUrl = import.meta.resolve("./html/500.html");
7446
+ const internalErrorHtmlFileUrl = import.meta.resolve("./client/error_handler/500.html");
7816
7447
 
7817
7448
  const jsenvServiceErrorHandler = ({ sendErrorDetails = false } = {}) => {
7818
7449
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/server",
3
- "version": "16.0.4",
3
+ "version": "16.0.6",
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",
45
- "@jsenv/url-meta": "8.5.5",
46
- "@jsenv/utils": "2.2.1",
44
+ "@jsenv/humanize": "1.4.0",
45
+ "@jsenv/url-meta": "8.5.6",
46
+ "@jsenv/utils": "2.3.0",
47
47
  "ws": "8.18.1",
48
48
  "formidable": "3.5.2"
49
49
  },
File without changes
File without changes