@jsenv/core 41.0.2 → 41.0.3

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,5 @@
1
1
  import { createSupportsColor, isUnicodeSupported, emojiRegex, eastAsianWidth, clearTerminal, eraseLines } from "./jsenv_core_node_modules.js";
2
2
  import { stripVTControlCharacters } from "node:util";
3
- import { pathToFileURL } from "node:url";
4
3
 
5
4
  const createCallbackListNotifiedOnce = () => {
6
5
  let callbacks = [];
@@ -1394,116 +1393,4 @@ const createTaskLog = (
1394
1393
  };
1395
1394
  };
1396
1395
 
1397
- const transformUrlPathname = (url, transformer) => {
1398
- if (typeof url === "string") {
1399
- const urlObject = new URL(url);
1400
- const { pathname } = urlObject;
1401
- const pathnameTransformed = transformer(pathname);
1402
- if (pathnameTransformed === pathname) {
1403
- return url;
1404
- }
1405
- let { origin } = urlObject;
1406
- // origin is "null" for "file://" urls with Node.js
1407
- if (origin === "null" && urlObject.href.startsWith("file:")) {
1408
- origin = "file://";
1409
- }
1410
- const { search, hash } = urlObject;
1411
- const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
1412
- return urlWithPathnameTransformed;
1413
- }
1414
- const pathnameTransformed = transformer(url.pathname);
1415
- url.pathname = pathnameTransformed;
1416
- return url;
1417
- };
1418
- const ensurePathnameTrailingSlash = (url) => {
1419
- return transformUrlPathname(url, (pathname) => {
1420
- return pathname.endsWith("/") ? pathname : `${pathname}/`;
1421
- });
1422
- };
1423
-
1424
- const isFileSystemPath = (value) => {
1425
- if (typeof value !== "string") {
1426
- throw new TypeError(
1427
- `isFileSystemPath first arg must be a string, got ${value}`,
1428
- );
1429
- }
1430
- if (value[0] === "/") {
1431
- return true;
1432
- }
1433
- return startsWithWindowsDriveLetter(value);
1434
- };
1435
-
1436
- const startsWithWindowsDriveLetter = (string) => {
1437
- const firstChar = string[0];
1438
- if (!/[a-zA-Z]/.test(firstChar)) return false;
1439
-
1440
- const secondChar = string[1];
1441
- if (secondChar !== ":") return false;
1442
-
1443
- return true;
1444
- };
1445
-
1446
- const fileSystemPathToUrl = (value) => {
1447
- if (!isFileSystemPath(value)) {
1448
- throw new Error(`value must be a filesystem path, got ${value}`);
1449
- }
1450
- return String(pathToFileURL(value));
1451
- };
1452
-
1453
- const validateDirectoryUrl = (value) => {
1454
- let urlString;
1455
-
1456
- if (value instanceof URL) {
1457
- urlString = value.href;
1458
- } else if (typeof value === "string") {
1459
- if (isFileSystemPath(value)) {
1460
- urlString = fileSystemPathToUrl(value);
1461
- } else {
1462
- try {
1463
- urlString = String(new URL(value));
1464
- } catch {
1465
- return {
1466
- valid: false,
1467
- value,
1468
- message: `must be a valid url`,
1469
- };
1470
- }
1471
- }
1472
- } else if (
1473
- value &&
1474
- typeof value === "object" &&
1475
- typeof value.href === "string"
1476
- ) {
1477
- value = value.href;
1478
- } else {
1479
- return {
1480
- valid: false,
1481
- value,
1482
- message: `must be a string or an url`,
1483
- };
1484
- }
1485
- if (!urlString.startsWith("file://")) {
1486
- return {
1487
- valid: false,
1488
- value,
1489
- message: 'must start with "file://"',
1490
- };
1491
- }
1492
- return {
1493
- valid: true,
1494
- value: ensurePathnameTrailingSlash(urlString),
1495
- };
1496
- };
1497
-
1498
- const assertAndNormalizeDirectoryUrl = (
1499
- directoryUrl,
1500
- name = "directoryUrl",
1501
- ) => {
1502
- const { valid, message, value } = validateDirectoryUrl(directoryUrl);
1503
- if (!valid) {
1504
- throw new TypeError(`${name} ${message}, got ${value}`);
1505
- }
1506
- return value;
1507
- };
1508
-
1509
- export { Abort, assertAndNormalizeDirectoryUrl, createLogger, createTaskLog, raceProcessTeardownEvents };
1396
+ export { Abort, createLogger, createTaskLog, raceProcessTeardownEvents };
@@ -1,12 +1,10 @@
1
- import { startServer, serverPluginCORS, jsenvAccessControlAllowedHeaders, serverPluginStaticFiles, serverPluginErrorHandler } from "@jsenv/server";
2
- import { existsSync } from "node:fs";
3
- import { assertAndNormalizeDirectoryUrl, createLogger, Abort, raceProcessTeardownEvents, createTaskLog } from "./jsenv_core_packages.js";
1
+ import { startServer, createFileSystemFetch, serverPluginCORS, jsenvAccessControlAllowedHeaders, serverPluginErrorHandler } from "@jsenv/server";
2
+ import { createLogger, Abort, raceProcessTeardownEvents, createTaskLog } from "./jsenv_core_packages.js";
4
3
  import "./jsenv_core_node_modules.js";
5
4
  import "node:process";
6
5
  import "node:os";
7
6
  import "node:tty";
8
7
  import "node:util";
9
- import "node:url";
10
8
 
11
9
  /*
12
10
  * startBuildServer is mean to interact with the build files;
@@ -34,7 +32,7 @@ const startBuildServer = async ({
34
32
  buildDirectoryUrl,
35
33
  buildDirectoryMainFileRelativeUrl = "index.html",
36
34
  port = 9779,
37
- routes,
35
+ routes = [],
38
36
  serverPlugins = [],
39
37
  acceptAnyIp,
40
38
  hostname,
@@ -46,55 +44,7 @@ const startBuildServer = async ({
46
44
  signal = new AbortController().signal,
47
45
  handleSIGINT = true,
48
46
  keepProcessAlive = true,
49
-
50
- ...rest
51
47
  }) => {
52
- // params validation
53
- {
54
- const unexpectedParamNames = Object.keys(rest);
55
- if (unexpectedParamNames.length > 0) {
56
- throw new TypeError(
57
- `${unexpectedParamNames.join(",")}: there is no such param`,
58
- );
59
- }
60
- buildDirectoryUrl = assertAndNormalizeDirectoryUrl(
61
- buildDirectoryUrl,
62
- "buildDirectoryUrl",
63
- );
64
-
65
- if (buildDirectoryMainFileRelativeUrl) {
66
- if (typeof buildDirectoryMainFileRelativeUrl !== "string") {
67
- throw new TypeError(
68
- `buildDirectoryMainFileRelativeUrl must be a string, got ${buildDirectoryMainFileRelativeUrl}`,
69
- );
70
- }
71
- if (buildDirectoryMainFileRelativeUrl[0] === "/") {
72
- buildDirectoryMainFileRelativeUrl =
73
- buildDirectoryMainFileRelativeUrl.slice(1);
74
- } else {
75
- const buildMainFileUrl = new URL(
76
- buildDirectoryMainFileRelativeUrl,
77
- buildDirectoryUrl,
78
- ).href;
79
- if (!buildMainFileUrl.startsWith(buildDirectoryUrl)) {
80
- throw new Error(
81
- `buildDirectoryMainFileRelativeUrl must be relative, got ${buildDirectoryMainFileRelativeUrl}`,
82
- );
83
- }
84
- buildDirectoryMainFileRelativeUrl = buildMainFileUrl.slice(
85
- buildDirectoryUrl.length,
86
- );
87
- }
88
- if (
89
- !existsSync(
90
- new URL(buildDirectoryMainFileRelativeUrl, buildDirectoryUrl),
91
- )
92
- ) {
93
- buildDirectoryMainFileRelativeUrl = null;
94
- }
95
- }
96
- }
97
-
98
48
  const logger = createLogger({ logLevel });
99
49
  const operation = Abort.startOperation();
100
50
  operation.addAbortSignal(signal);
@@ -127,7 +77,6 @@ const startBuildServer = async ({
127
77
  port,
128
78
  serverTiming: true,
129
79
  requestWaitingMs: 60_000,
130
- routes,
131
80
  plugins: [
132
81
  serverPluginCORS({
133
82
  accessControlAllowRequestOrigin: true,
@@ -138,15 +87,20 @@ const startBuildServer = async ({
138
87
  timingAllowOrigin: true,
139
88
  }),
140
89
  ...serverPlugins,
141
- serverPluginStaticFiles({
142
- serverRelativeUrl: "/",
143
- directoryUrl: buildDirectoryUrl,
144
- directoryMainFileRelativeUrl: buildDirectoryMainFileRelativeUrl,
145
- }),
146
90
  serverPluginErrorHandler({
147
91
  sendErrorDetails: false,
148
92
  }),
149
93
  ],
94
+ routes: [
95
+ ...routes,
96
+ {
97
+ endpoint: "GET /",
98
+ description: "Serve build files",
99
+ fetch: createFileSystemFetch(buildDirectoryUrl, {
100
+ mainFileRelativeUrl: buildDirectoryMainFileRelativeUrl,
101
+ }),
102
+ },
103
+ ],
150
104
  });
151
105
  startBuildServerTask.done();
152
106
  if (hostname) {
@@ -1,4 +1,4 @@
1
- import { WebSocketResponse, pickContentType, ServerEvents, serverPluginCORS, jsenvAccessControlAllowedHeaders, composeTwoResponses, serveDirectory, serverPluginErrorHandler, startServer } from "@jsenv/server";
1
+ import { WebSocketResponse, pickContentType, ServerEvents, serverPluginCORS, jsenvAccessControlAllowedHeaders, composeTwoResponses, fetchDirectory, serverPluginErrorHandler, startServer } from "@jsenv/server";
2
2
  import { convertFileSystemErrorToResponseProperties } from "@jsenv/server/src/plugins/filesystem/filesystem_error_to_response.js";
3
3
  import { lookupPackageDirectory, registerDirectoryLifecycle, urlToRelativeUrl, moveUrl, urlIsOrIsInsideOf, ensureWindowsDriveLetter, createDetailedMessage, stringifyUrlSite, generateContentFrame, validateResponseIntegrity, setUrlFilename, getCallerPosition, urlToBasename, urlToExtension, asSpecifierWithoutSearch, asUrlWithoutSearch, injectQueryParamsIntoSpecifier, bufferToEtag, isFileSystemPath, urlToPathname, setUrlBasename, urlToFileSystemPath, writeFileSync, createLogger, URL_META, applyNodeEsmResolution, normalizeUrl, ANSI, RUNTIME_COMPAT, CONTENT_TYPE, readPackageAtOrNull, errorToHTML, DATA_URL, normalizeImportMap, composeTwoImportMaps, resolveImport, JS_QUOTES, readCustomConditionsFromProcessArgs, readEntryStatSync, ensurePathnameTrailingSlash, compareFileUrls, urlToFilename, applyFileSystemMagicResolution, getExtensionsToTry, setUrlExtension, isSpecifierForNodeBuiltin, injectQueryParams, memoizeByFirstArgument, assertAndNormalizeDirectoryUrl, createTaskLog, formatError } from "./jsenv_core_packages.js";
4
4
  import { readFileSync, existsSync, readdirSync, lstatSync, realpathSync } from "node:fs";
@@ -10041,7 +10041,7 @@ const startDevServer = async ({
10041
10041
  };
10042
10042
  }
10043
10043
  if (code === "DIRECTORY_REFERENCE_NOT_ALLOWED") {
10044
- return serveDirectory(reference.url, {
10044
+ return fetchDirectory(reference.url, {
10045
10045
  headers: {
10046
10046
  accept: "text/html",
10047
10047
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/core",
3
- "version": "41.0.2",
3
+ "version": "41.0.3",
4
4
  "description": "Tool to develop, test and build js projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -81,7 +81,7 @@
81
81
  "@jsenv/plugin-minification": "1.7.3",
82
82
  "@jsenv/plugin-supervisor": "1.7.15",
83
83
  "@jsenv/plugin-transpilation": "1.5.70",
84
- "@jsenv/server": "17.0.1",
84
+ "@jsenv/server": "17.0.2",
85
85
  "@jsenv/sourcemap": "1.3.17",
86
86
  "react-table": "7.8.0"
87
87
  },
@@ -14,16 +14,14 @@
14
14
  */
15
15
 
16
16
  import { Abort, raceProcessTeardownEvents } from "@jsenv/abort";
17
- import { assertAndNormalizeDirectoryUrl } from "@jsenv/filesystem";
18
17
  import { createLogger, createTaskLog } from "@jsenv/humanize";
19
18
  import {
19
+ createFileSystemFetch,
20
20
  jsenvAccessControlAllowedHeaders,
21
21
  serverPluginCORS,
22
22
  serverPluginErrorHandler,
23
- serverPluginStaticFiles,
24
23
  startServer,
25
24
  } from "@jsenv/server";
26
- import { existsSync } from "node:fs";
27
25
 
28
26
  /**
29
27
  * Start a server for build files.
@@ -35,7 +33,7 @@ export const startBuildServer = async ({
35
33
  buildDirectoryUrl,
36
34
  buildDirectoryMainFileRelativeUrl = "index.html",
37
35
  port = 9779,
38
- routes,
36
+ routes = [],
39
37
  serverPlugins = [],
40
38
  acceptAnyIp,
41
39
  hostname,
@@ -47,55 +45,7 @@ export const startBuildServer = async ({
47
45
  signal = new AbortController().signal,
48
46
  handleSIGINT = true,
49
47
  keepProcessAlive = true,
50
-
51
- ...rest
52
48
  }) => {
53
- // params validation
54
- {
55
- const unexpectedParamNames = Object.keys(rest);
56
- if (unexpectedParamNames.length > 0) {
57
- throw new TypeError(
58
- `${unexpectedParamNames.join(",")}: there is no such param`,
59
- );
60
- }
61
- buildDirectoryUrl = assertAndNormalizeDirectoryUrl(
62
- buildDirectoryUrl,
63
- "buildDirectoryUrl",
64
- );
65
-
66
- if (buildDirectoryMainFileRelativeUrl) {
67
- if (typeof buildDirectoryMainFileRelativeUrl !== "string") {
68
- throw new TypeError(
69
- `buildDirectoryMainFileRelativeUrl must be a string, got ${buildDirectoryMainFileRelativeUrl}`,
70
- );
71
- }
72
- if (buildDirectoryMainFileRelativeUrl[0] === "/") {
73
- buildDirectoryMainFileRelativeUrl =
74
- buildDirectoryMainFileRelativeUrl.slice(1);
75
- } else {
76
- const buildMainFileUrl = new URL(
77
- buildDirectoryMainFileRelativeUrl,
78
- buildDirectoryUrl,
79
- ).href;
80
- if (!buildMainFileUrl.startsWith(buildDirectoryUrl)) {
81
- throw new Error(
82
- `buildDirectoryMainFileRelativeUrl must be relative, got ${buildDirectoryMainFileRelativeUrl}`,
83
- );
84
- }
85
- buildDirectoryMainFileRelativeUrl = buildMainFileUrl.slice(
86
- buildDirectoryUrl.length,
87
- );
88
- }
89
- if (
90
- !existsSync(
91
- new URL(buildDirectoryMainFileRelativeUrl, buildDirectoryUrl),
92
- )
93
- ) {
94
- buildDirectoryMainFileRelativeUrl = null;
95
- }
96
- }
97
- }
98
-
99
49
  const logger = createLogger({ logLevel });
100
50
  const operation = Abort.startOperation();
101
51
  operation.addAbortSignal(signal);
@@ -129,7 +79,6 @@ export const startBuildServer = async ({
129
79
  port,
130
80
  serverTiming: true,
131
81
  requestWaitingMs: 60_000,
132
- routes,
133
82
  plugins: [
134
83
  serverPluginCORS({
135
84
  accessControlAllowRequestOrigin: true,
@@ -140,15 +89,20 @@ export const startBuildServer = async ({
140
89
  timingAllowOrigin: true,
141
90
  }),
142
91
  ...serverPlugins,
143
- serverPluginStaticFiles({
144
- serverRelativeUrl: "/",
145
- directoryUrl: buildDirectoryUrl,
146
- directoryMainFileRelativeUrl: buildDirectoryMainFileRelativeUrl,
147
- }),
148
92
  serverPluginErrorHandler({
149
93
  sendErrorDetails: false,
150
94
  }),
151
95
  ],
96
+ routes: [
97
+ ...routes,
98
+ {
99
+ endpoint: "GET /",
100
+ description: "Serve build files",
101
+ fetch: createFileSystemFetch(buildDirectoryUrl, {
102
+ mainFileRelativeUrl: buildDirectoryMainFileRelativeUrl,
103
+ }),
104
+ },
105
+ ],
152
106
  });
153
107
  startBuildServerTask.done();
154
108
  if (hostname) {
@@ -6,8 +6,8 @@ import {
6
6
  import { createLogger, createTaskLog, formatError } from "@jsenv/humanize";
7
7
  import {
8
8
  composeTwoResponses,
9
+ fetchDirectory,
9
10
  jsenvAccessControlAllowedHeaders,
10
- serveDirectory,
11
11
  serverPluginCORS,
12
12
  serverPluginErrorHandler,
13
13
  startServer,
@@ -595,7 +595,7 @@ export const startDevServer = async ({
595
595
  };
596
596
  }
597
597
  if (code === "DIRECTORY_REFERENCE_NOT_ALLOWED") {
598
- return serveDirectory(reference.url, {
598
+ return fetchDirectory(reference.url, {
599
599
  headers: {
600
600
  accept: "text/html",
601
601
  },