@jsenv/server 16.3.6 → 17.0.0

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 (32) hide show
  1. package/dist/client/route_inspector/route_inspector.html +4 -4
  2. package/dist/jsenv_server.js +3628 -3376
  3. package/dist/ws/ws.js +37 -31
  4. package/index.js +56 -0
  5. package/package.json +7 -6
  6. package/src/{services/autoreload_on_server_restart/jsenv_service_autoreload_on_server_restart.js → plugins/autoreload_on_server_restart/server_plugin_autoreload_on_server_restart.js} +1 -1
  7. package/src/{services/cors/jsenv_service_cors.js → plugins/cors/server_plugin_cors.js} +1 -1
  8. package/src/{services → plugins}/default_body_4xx_5xx/client/4xx.html +1 -1
  9. package/src/{services/default_body_4xx_5xx/jsenv_service_default_body_4xx_5xx.js → plugins/default_body_4xx_5xx/server_plugin_default_body_4xx_5xx.js} +2 -1
  10. package/src/{services/error_handler/jsenv_service_error_handler.js → plugins/error_handler/server_plugin_error_handler.js} +1 -1
  11. package/src/{filesystem → plugins/filesystem}/fetch_filesystem.js +6 -5
  12. package/src/{filesystem → plugins/filesystem}/serve_directory.js +2 -1
  13. package/src/plugins/filesystem/server_plugin_static_files.js +44 -0
  14. package/src/{services/internal_client_files/jsenv_service_internal_client_files.js → plugins/internal_client_files/server_plugin_internal_client_files.js} +1 -1
  15. package/src/{services/open_file/jsenv_service_open_file.js → plugins/open_file/server_plugin_open_file.js} +1 -1
  16. package/src/{services/request_aliases/jsenv_service_request_aliases.js → plugins/request_aliases/server_plugin_request_aliases.js} +1 -1
  17. package/src/{services/response_acceptance_check/jsenv_service_response_acceptance_check.js → plugins/response_acceptance_check/server_plugin_response_acceptance_check.js} +1 -1
  18. package/src/{services → plugins}/route_inspector/client/route_inspector.html +2 -2
  19. package/src/{services/route_inspector/jsenv_service_route_inspector.js → plugins/route_inspector/server_plugin_route_inspector.js} +1 -1
  20. package/src/plugins/server_plugins_controller.js +21 -0
  21. package/src/plugins_controller.js +341 -0
  22. package/src/router/router.js +8 -5
  23. package/src/start_server.js +34 -38
  24. package/src/main.js +0 -52
  25. package/src/service_controller.js +0 -193
  26. /package/src/{services → plugins}/autoreload_on_server_restart/client/autoreload_on_server_restart.js +0 -0
  27. /package/src/{services → plugins}/error_handler/client/500.html +0 -0
  28. /package/src/{internal → plugins/filesystem}/etag.js +0 -0
  29. /package/src/{internal/convertFileSystemErrorToResponseProperties.js → plugins/filesystem/filesystem_error_to_response.js} +0 -0
  30. /package/src/{internal/filesystem.js → plugins/filesystem/filesystem_path_and_url.js} +0 -0
  31. /package/src/{services → plugins}/route_inspector/client/code_block.js +0 -0
  32. /package/src/{stopReasons.js → stop_reasons.js} +0 -0
@@ -1,193 +0,0 @@
1
- const HOOK_NAMES = [
2
- "serverListening",
3
- "redirectRequest",
4
- "augmentRouteFetchSecondArg",
5
- "routes",
6
- "handleError",
7
- "onResponsePush",
8
- "injectResponseProperties",
9
- "serverStopped",
10
- ];
11
-
12
- export const createServiceController = (services) => {
13
- const hookSetMap = new Map();
14
-
15
- const addHook = (hook) => {
16
- let hookSet = hookSetMap.get(hook.name);
17
- if (!hookSet) {
18
- hookSet = new Set();
19
- hookSetMap.set(hook.name, hookSet);
20
- }
21
- hookSet.add(hook);
22
- };
23
-
24
- const addService = (service) => {
25
- for (const key of Object.keys(service)) {
26
- if (key === "name") continue;
27
- const isHook = HOOK_NAMES.includes(key);
28
- if (!isHook) {
29
- console.warn(
30
- `Unexpected "${key}" property on "${service.name}" service`,
31
- );
32
- }
33
- const hookName = key;
34
- const hookValue = service[hookName];
35
- if (!hookValue) {
36
- continue;
37
- }
38
- if (hookName === "routes") {
39
- } else {
40
- addHook({
41
- service,
42
- name: hookName,
43
- value: hookValue,
44
- });
45
- }
46
- }
47
- };
48
-
49
- for (const service of services) {
50
- addService(service);
51
- }
52
-
53
- let currentService = null;
54
- let currentHookName = null;
55
- const callHook = (hook, info, context) => {
56
- const hookFn = getHookFunction(hook, info);
57
- if (!hookFn) {
58
- return null;
59
- }
60
- currentService = hook.service;
61
- currentHookName = hook.name;
62
- let valueReturned = hookFn(info, context);
63
- currentService = null;
64
- currentHookName = null;
65
- return valueReturned;
66
- };
67
- const callAsyncHook = async (hook, info, context) => {
68
- const hookFn = getHookFunction(hook, info);
69
- if (!hookFn) {
70
- return null;
71
- }
72
- currentService = hook.service;
73
- currentHookName = hook.name;
74
- let valueReturned = await hookFn(info, context);
75
- currentService = null;
76
- currentHookName = null;
77
- return valueReturned;
78
- };
79
-
80
- const callHooks = (hookName, info, context, callback = () => {}) => {
81
- const hookSet = hookSetMap.get(hookName);
82
- if (!hookSet) {
83
- return;
84
- }
85
- for (const hook of hookSet) {
86
- const returnValue = callHook(hook, info, context);
87
- if (returnValue) {
88
- callback(returnValue);
89
- }
90
- }
91
- };
92
- const callAsyncHooks = async (hookName, info, context, callback) => {
93
- const hookSet = hookSetMap.get(hookName);
94
- if (!hookSet) {
95
- return;
96
- }
97
- for (const hook of hookSet) {
98
- const returnValue = await callAsyncHook(hook, info, context);
99
- if (returnValue && callback) {
100
- await callback(returnValue, hook.plugin);
101
- }
102
- }
103
- };
104
- const callHooksUntil = (
105
- hookName,
106
- info,
107
- context,
108
- until = (returnValue) => returnValue,
109
- ) => {
110
- const hookSet = hookSetMap.get(hookName);
111
- if (!hookSet) {
112
- return null;
113
- }
114
- for (const hook of hookSet) {
115
- const returnValue = callHook(hook, info, context);
116
- const untilReturnValue = until(returnValue);
117
- if (untilReturnValue) {
118
- return untilReturnValue;
119
- }
120
- }
121
- return null;
122
- };
123
- const callAsyncHooksUntil = async (hookName, info, context) => {
124
- const hookSet = hookSetMap.get(hookName);
125
- if (!hookSet) {
126
- return null;
127
- }
128
- if (hookSet.size === 0) {
129
- return null;
130
- }
131
- const iterator = hookSet.values()[Symbol.iterator]();
132
- let result;
133
- const visit = async () => {
134
- const { done, value: hook } = iterator.next();
135
- if (done) {
136
- return;
137
- }
138
- const returnValue = await callAsyncHook(hook, info, context);
139
- if (returnValue) {
140
- result = returnValue;
141
- return;
142
- }
143
- await visit();
144
- };
145
- await visit();
146
- return result;
147
- };
148
-
149
- return {
150
- services,
151
-
152
- callHooks,
153
- callHooksUntil,
154
- callAsyncHooks,
155
- callAsyncHooksUntil,
156
-
157
- getCurrentService: () => currentService,
158
- getCurrentHookName: () => currentHookName,
159
- };
160
- };
161
-
162
- const getHookFunction = (hook, info) => {
163
- const hookValue = hook.value;
164
- if (hook.name === "handleRequest" && typeof hookValue === "object") {
165
- const request = info;
166
- const hookForMethod = hookValue[request.method] || hookValue["*"];
167
- if (!hookForMethod) {
168
- return null;
169
- }
170
- return hookForMethod;
171
- }
172
- return hookValue;
173
- };
174
-
175
- export const flattenAndFilterServices = (services) => {
176
- const flatServices = [];
177
- const visitServiceEntry = (serviceEntry) => {
178
- if (Array.isArray(serviceEntry)) {
179
- serviceEntry.forEach((value) => visitServiceEntry(value));
180
- return;
181
- }
182
- if (typeof serviceEntry === "object" && serviceEntry !== null) {
183
- if (!serviceEntry.name) {
184
- serviceEntry.name = "anonymous";
185
- }
186
- flatServices.push(serviceEntry);
187
- return;
188
- }
189
- throw new Error(`services must be objects, got ${serviceEntry}`);
190
- };
191
- services.forEach((serviceEntry) => visitServiceEntry(serviceEntry));
192
- return flatServices;
193
- };
File without changes
File without changes