@jsenv/core 40.0.1 → 40.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.
Files changed (39) hide show
  1. package/dist/{js → client/autoreload}/autoreload.js +45 -2
  2. package/dist/{html → client/directory_listing}/directory_listing.html +2 -2
  3. package/dist/client/directory_listing/js/directory_listing.js +247 -0
  4. package/dist/{js → client/new_stylesheet}/new_stylesheet.js +1 -1
  5. package/dist/{js → client/regenerator_runtime}/regenerator_runtime.js +1 -1
  6. package/dist/{js → client/server_events_client}/server_events_client.js +0 -1
  7. package/dist/js/build.js +2637 -0
  8. package/dist/js/start_build_server.js +210 -0
  9. package/dist/js/start_dev_server.js +840 -0
  10. package/dist/jsenv_core.js +14 -21359
  11. package/dist/jsenv_core_packages.js +10061 -0
  12. package/dist/main.js +117 -0
  13. package/dist/plugins.js +7944 -0
  14. package/package.json +18 -14
  15. package/src/build/build.js +61 -60
  16. package/src/build/build_params.js +20 -0
  17. package/src/build/build_specifier_manager.js +12 -1
  18. package/src/build/build_urls_generator.js +5 -1
  19. package/src/build/jsenv_plugin_subbuilds.js +71 -0
  20. package/src/dev/start_dev_server.js +21 -7
  21. package/src/kitchen/fetched_content_compliance.js +7 -3
  22. package/src/kitchen/kitchen.js +3 -0
  23. package/src/main.js +13 -3
  24. package/src/plugins/autoreload/jsenv_plugin_autoreload_client.js +1 -4
  25. package/src/plugins/html_syntax_error_fallback/jsenv_plugin_html_syntax_error_fallback.js +4 -3
  26. package/src/plugins/import_meta_hot/jsenv_plugin_import_meta_hot.js +2 -3
  27. package/src/plugins/protocol_file/jsenv_plugin_directory_listing.js +1 -2
  28. package/src/plugins/resolution_node_esm/node_esm_resolver.js +17 -1
  29. package/src/plugins/ribbon/jsenv_plugin_ribbon.js +2 -2
  30. package/dist/js/directory_listing.js +0 -247
  31. /package/dist/babel_helpers/{OverloadYield → overloadYield}/OverloadYield.js +0 -0
  32. /package/dist/{css → client/directory_listing/css}/directory_listing.css +0 -0
  33. /package/dist/{other → client/directory_listing/other}/dir.png +0 -0
  34. /package/dist/{other → client/directory_listing/other}/file.png +0 -0
  35. /package/dist/{other → client/directory_listing/other}/home.svg +0 -0
  36. /package/dist/{html → client/html_syntax_error}/html_syntax_error.html +0 -0
  37. /package/dist/{js → client/import_meta_hot}/import_meta_hot.js +0 -0
  38. /package/dist/{js → client/inline_content}/inline_content.js +0 -0
  39. /package/dist/{js → client/ribbon}/ribbon.js +0 -0
@@ -0,0 +1,210 @@
1
+ import { assertAndNormalizeDirectoryUrl, createLogger, Abort, raceProcessTeardownEvents, createTaskLog, urlToExtension, urlToPathname } from "../jsenv_core_packages.js";
2
+ import { startServer, jsenvServiceCORS, jsenvServiceErrorHandler, jsenvAccessControlAllowedHeaders, createFileSystemFetch } from "@jsenv/server";
3
+ import { existsSync } from "node:fs";
4
+ import "node:path";
5
+ import "node:crypto";
6
+ import "@jsenv/ast";
7
+ import "@jsenv/sourcemap";
8
+ import "node:url";
9
+ import "node:module";
10
+ import "@jsenv/js-module-fallback";
11
+ import "node:process";
12
+ import "node:os";
13
+ import "node:tty";
14
+ import "string-width";
15
+
16
+ /*
17
+ * startBuildServer is mean to interact with the build files;
18
+ * files that will be deployed to production server(s).
19
+ * We want to be as close as possible from the production in order to:
20
+ * - run lighthouse
21
+ * - run an automated test tool such as cypress, playwright
22
+ * - see exactly how build file behaves (debug, measure perf, etc)
23
+ * For these reasons "startBuildServer" must be as close as possible from a static file server.
24
+ * It is not meant to provide a nice developper experience: this is the role "startDevServer".
25
+ *
26
+ * Conclusion:
27
+ * "startBuildServer" must be as close as possible from a static file server because
28
+ * we want to be in the user shoes and we should not alter build files.
29
+ */
30
+
31
+
32
+ /**
33
+ * Start a server for build files.
34
+ * @param {Object} buildServerParameters
35
+ * @param {string|url} buildServerParameters.buildDirectoryUrl Directory where build files are written
36
+ * @return {Object} A build server object
37
+ */
38
+ const startBuildServer = async ({
39
+ buildDirectoryUrl,
40
+ buildMainFilePath = "index.html",
41
+ port = 9779,
42
+ routes,
43
+ services = [],
44
+ acceptAnyIp,
45
+ hostname,
46
+ https,
47
+ http2,
48
+ logLevel,
49
+ serverLogLevel = "warn",
50
+
51
+ signal = new AbortController().signal,
52
+ handleSIGINT = true,
53
+ keepProcessAlive = true,
54
+
55
+ ...rest
56
+ }) => {
57
+ // params validation
58
+ {
59
+ const unexpectedParamNames = Object.keys(rest);
60
+ if (unexpectedParamNames.length > 0) {
61
+ throw new TypeError(
62
+ `${unexpectedParamNames.join(",")}: there is no such param`,
63
+ );
64
+ }
65
+ buildDirectoryUrl = assertAndNormalizeDirectoryUrl(
66
+ buildDirectoryUrl,
67
+ "buildDirectoryUrl",
68
+ );
69
+
70
+ if (buildMainFilePath) {
71
+ if (typeof buildMainFilePath !== "string") {
72
+ throw new TypeError(
73
+ `buildMainFilePath must be a string, got ${buildMainFilePath}`,
74
+ );
75
+ }
76
+ if (buildMainFilePath[0] === "/") {
77
+ buildMainFilePath = buildMainFilePath.slice(1);
78
+ } else {
79
+ const buildMainFileUrl = new URL(buildMainFilePath, buildDirectoryUrl)
80
+ .href;
81
+ if (!buildMainFileUrl.startsWith(buildDirectoryUrl)) {
82
+ throw new Error(
83
+ `buildMainFilePath must be relative, got ${buildMainFilePath}`,
84
+ );
85
+ }
86
+ buildMainFilePath = buildMainFileUrl.slice(buildDirectoryUrl.length);
87
+ }
88
+ if (!existsSync(new URL(buildMainFilePath, buildDirectoryUrl))) {
89
+ buildMainFilePath = null;
90
+ }
91
+ }
92
+ }
93
+
94
+ const logger = createLogger({ logLevel });
95
+ const operation = Abort.startOperation();
96
+ operation.addAbortSignal(signal);
97
+ if (handleSIGINT) {
98
+ operation.addAbortSource((abort) => {
99
+ return raceProcessTeardownEvents(
100
+ {
101
+ SIGINT: true,
102
+ },
103
+ abort,
104
+ );
105
+ });
106
+ }
107
+
108
+ const startBuildServerTask = createTaskLog("start build server", {
109
+ disabled: !logger.levels.info,
110
+ });
111
+ const server = await startServer({
112
+ signal,
113
+ stopOnExit: false,
114
+ stopOnSIGINT: false,
115
+ stopOnInternalError: false,
116
+ keepProcessAlive,
117
+ logLevel: serverLogLevel,
118
+ startLog: false,
119
+
120
+ https,
121
+ http2,
122
+ acceptAnyIp,
123
+ hostname,
124
+ port,
125
+ serverTiming: true,
126
+ requestWaitingMs: 60_000,
127
+ routes,
128
+ services: [
129
+ jsenvServiceCORS({
130
+ accessControlAllowRequestOrigin: true,
131
+ accessControlAllowRequestMethod: true,
132
+ accessControlAllowRequestHeaders: true,
133
+ accessControlAllowedRequestHeaders: jsenvAccessControlAllowedHeaders,
134
+ accessControlAllowCredentials: true,
135
+ timingAllowOrigin: true,
136
+ }),
137
+ ...services,
138
+ jsenvBuildFileService({
139
+ buildDirectoryUrl,
140
+ buildMainFilePath,
141
+ }),
142
+ jsenvServiceErrorHandler({
143
+ sendErrorDetails: true,
144
+ }),
145
+ ],
146
+ });
147
+ startBuildServerTask.done();
148
+ if (hostname) {
149
+ delete server.origins.localip;
150
+ delete server.origins.externalip;
151
+ }
152
+ logger.info(``);
153
+ Object.keys(server.origins).forEach((key) => {
154
+ logger.info(`- ${server.origins[key]}`);
155
+ });
156
+ logger.info(``);
157
+ return {
158
+ origin: server.origin,
159
+ stop: () => {
160
+ server.stop();
161
+ },
162
+ };
163
+ };
164
+
165
+ const jsenvBuildFileService = ({ buildDirectoryUrl, buildMainFilePath }) => {
166
+ return {
167
+ name: "jsenv:build_files",
168
+ routes: [
169
+ {
170
+ endpoint: "GET *",
171
+ description: "Serve static files.",
172
+ fetch: (request, helpers) => {
173
+ const urlIsVersioned = new URL(request.url).searchParams.has("v");
174
+ if (buildMainFilePath && request.resource === "/") {
175
+ request = {
176
+ ...request,
177
+ resource: `/${buildMainFilePath}`,
178
+ };
179
+ }
180
+ const urlObject = new URL(
181
+ request.resource.slice(1),
182
+ buildDirectoryUrl,
183
+ );
184
+ return createFileSystemFetch(buildDirectoryUrl, {
185
+ cacheControl: urlIsVersioned
186
+ ? `private,max-age=${SECONDS_IN_30_DAYS},immutable`
187
+ : "private,max-age=0,must-revalidate",
188
+ etagEnabled: true,
189
+ compressionEnabled: true,
190
+ rootDirectoryUrl: buildDirectoryUrl,
191
+ canReadDirectory: true,
192
+ ENOENTFallback: () => {
193
+ if (
194
+ !urlToExtension(urlObject) &&
195
+ !urlToPathname(urlObject).endsWith("/")
196
+ ) {
197
+ return new URL(buildMainFilePath, buildDirectoryUrl);
198
+ }
199
+ return null;
200
+ },
201
+ })(request, helpers);
202
+ },
203
+ },
204
+ ],
205
+ };
206
+ };
207
+
208
+ const SECONDS_IN_30_DAYS = 60 * 60 * 24 * 30;
209
+
210
+ export { startBuildServer };