@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.
- package/dist/client/route_inspector/route_inspector.html +4 -4
- package/dist/jsenv_server.js +3628 -3376
- package/dist/ws/ws.js +37 -31
- package/index.js +56 -0
- package/package.json +7 -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
- package/src/{services/cors/jsenv_service_cors.js → plugins/cors/server_plugin_cors.js} +1 -1
- package/src/{services → plugins}/default_body_4xx_5xx/client/4xx.html +1 -1
- 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
- package/src/{services/error_handler/jsenv_service_error_handler.js → plugins/error_handler/server_plugin_error_handler.js} +1 -1
- package/src/{filesystem → plugins/filesystem}/fetch_filesystem.js +6 -5
- package/src/{filesystem → plugins/filesystem}/serve_directory.js +2 -1
- package/src/plugins/filesystem/server_plugin_static_files.js +44 -0
- package/src/{services/internal_client_files/jsenv_service_internal_client_files.js → plugins/internal_client_files/server_plugin_internal_client_files.js} +1 -1
- package/src/{services/open_file/jsenv_service_open_file.js → plugins/open_file/server_plugin_open_file.js} +1 -1
- package/src/{services/request_aliases/jsenv_service_request_aliases.js → plugins/request_aliases/server_plugin_request_aliases.js} +1 -1
- package/src/{services/response_acceptance_check/jsenv_service_response_acceptance_check.js → plugins/response_acceptance_check/server_plugin_response_acceptance_check.js} +1 -1
- package/src/{services → plugins}/route_inspector/client/route_inspector.html +2 -2
- package/src/{services/route_inspector/jsenv_service_route_inspector.js → plugins/route_inspector/server_plugin_route_inspector.js} +1 -1
- package/src/plugins/server_plugins_controller.js +21 -0
- package/src/plugins_controller.js +341 -0
- package/src/router/router.js +8 -5
- package/src/start_server.js +34 -38
- package/src/main.js +0 -52
- package/src/service_controller.js +0 -193
- /package/src/{services → plugins}/autoreload_on_server_restart/client/autoreload_on_server_restart.js +0 -0
- /package/src/{services → plugins}/error_handler/client/500.html +0 -0
- /package/src/{internal → plugins/filesystem}/etag.js +0 -0
- /package/src/{internal/convertFileSystemErrorToResponseProperties.js → plugins/filesystem/filesystem_error_to_response.js} +0 -0
- /package/src/{internal/filesystem.js → plugins/filesystem/filesystem_path_and_url.js} +0 -0
- /package/src/{services → plugins}/route_inspector/client/code_block.js +0 -0
- /package/src/{stopReasons.js → stop_reasons.js} +0 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
|
|
3
|
+
// createPluginsController manages an ordered list of plugins.
|
|
4
|
+
// "plugins" is a generic term: it can refer to jsenv core plugins,
|
|
5
|
+
// server plugins, or any extensibility mechanism using the same pattern.
|
|
6
|
+
export const createPluginsController = async ({
|
|
7
|
+
plugins,
|
|
8
|
+
pluginDescription = {},
|
|
9
|
+
filterPlugin,
|
|
10
|
+
getInitPluginArgs,
|
|
11
|
+
getHookFunction = defaultGetHookFunction,
|
|
12
|
+
getTimingKey = defaultGetTimingKey,
|
|
13
|
+
getEffectArgs = defaultGetEffectArgs,
|
|
14
|
+
meta = {},
|
|
15
|
+
}) => {
|
|
16
|
+
const { name: pluginName = "plugin", properties = {} } = pluginDescription;
|
|
17
|
+
|
|
18
|
+
const hookSetMap = new Map();
|
|
19
|
+
|
|
20
|
+
const addHook = (hook) => {
|
|
21
|
+
let hookSet = hookSetMap.get(hook.name);
|
|
22
|
+
if (!hookSet) {
|
|
23
|
+
hookSet = new Set();
|
|
24
|
+
hookSetMap.set(hook.name, hookSet);
|
|
25
|
+
}
|
|
26
|
+
hookSet.add(hook);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const addPluginToHookSetMap = (plugin) => {
|
|
30
|
+
for (const key of Object.keys(plugin)) {
|
|
31
|
+
if (
|
|
32
|
+
key === "name" ||
|
|
33
|
+
key === "init" ||
|
|
34
|
+
key === "destroy" ||
|
|
35
|
+
key === "meta" ||
|
|
36
|
+
key === "effect"
|
|
37
|
+
) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const propDef = properties[key];
|
|
41
|
+
if (propDef === undefined) {
|
|
42
|
+
console.warn(
|
|
43
|
+
`Unexpected "${key}" property on "${plugin.name}" ${pluginName}`,
|
|
44
|
+
);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (propDef.type !== "hook") {
|
|
48
|
+
// non-hook key, silently skip
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const hookValue = plugin[key];
|
|
52
|
+
if (!hookValue) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
addHook({
|
|
56
|
+
plugin,
|
|
57
|
+
name: key,
|
|
58
|
+
value: hookValue,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const activatePlugin = (plugin) => {
|
|
64
|
+
if (plugin.meta) {
|
|
65
|
+
const pluginMeta = plugin.meta;
|
|
66
|
+
if (typeof pluginMeta !== "object" || pluginMeta === null) {
|
|
67
|
+
console.warn(`${pluginName}.meta must be an object, got ${pluginMeta}`);
|
|
68
|
+
} else {
|
|
69
|
+
Object.assign(meta, pluginMeta);
|
|
70
|
+
Object.freeze(pluginMeta);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
addPluginToHookSetMap(plugin);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const pluginCandidates = plugins;
|
|
77
|
+
const activePlugins = [];
|
|
78
|
+
const pluginsWithEffect = [];
|
|
79
|
+
|
|
80
|
+
for (const pluginCandidate of pluginCandidates) {
|
|
81
|
+
if (filterPlugin && !filterPlugin(pluginCandidate)) {
|
|
82
|
+
pluginCandidate.destroy?.();
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const active = await callInitOnPlugin(pluginCandidate, getInitPluginArgs);
|
|
86
|
+
if (!active) {
|
|
87
|
+
pluginCandidate.destroy?.();
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (pluginCandidate.effect) {
|
|
91
|
+
pluginsWithEffect.push(pluginCandidate);
|
|
92
|
+
} else {
|
|
93
|
+
activePlugins.push(pluginCandidate);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const pluginWithEffect of pluginsWithEffect) {
|
|
98
|
+
const effectArgs = getEffectArgs({ otherPlugins: activePlugins });
|
|
99
|
+
const returnValue = pluginWithEffect.effect(...effectArgs);
|
|
100
|
+
if (!returnValue) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
activePlugins.push(pluginWithEffect);
|
|
104
|
+
if (typeof returnValue === "function" && !pluginWithEffect.destroy) {
|
|
105
|
+
pluginWithEffect.destroy = returnValue;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Preserve original declaration order
|
|
110
|
+
activePlugins.sort(
|
|
111
|
+
(a, b) => pluginCandidates.indexOf(a) - pluginCandidates.indexOf(b),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
for (const activePlugin of activePlugins) {
|
|
115
|
+
activatePlugin(activePlugin);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const assertAndNormalizeReturnValue = (valueReturned, info, { hook }) => {
|
|
119
|
+
if (valueReturned === null || valueReturned === undefined) {
|
|
120
|
+
// all hooks are allowed to return null/undefined as a signal of "I don't do anything"
|
|
121
|
+
return valueReturned;
|
|
122
|
+
}
|
|
123
|
+
const propDef = properties[hook.name];
|
|
124
|
+
if (!propDef || !propDef.assertAndNormalize) {
|
|
125
|
+
return valueReturned;
|
|
126
|
+
}
|
|
127
|
+
const assertAndNormalizeResult = propDef.assertAndNormalize(
|
|
128
|
+
valueReturned,
|
|
129
|
+
info,
|
|
130
|
+
{ hook },
|
|
131
|
+
);
|
|
132
|
+
return assertAndNormalizeResult;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
let lastPluginUsed = null;
|
|
136
|
+
let currentPlugin = null;
|
|
137
|
+
let currentHookName = null;
|
|
138
|
+
|
|
139
|
+
const callHook = (hook, info, secondArg) => {
|
|
140
|
+
const hookFn = getHookFunction(hook, info);
|
|
141
|
+
if (!hookFn) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
lastPluginUsed = hook.plugin;
|
|
145
|
+
currentPlugin = hook.plugin;
|
|
146
|
+
currentHookName = hook.name;
|
|
147
|
+
let startTimestamp;
|
|
148
|
+
if (info && info.timing) {
|
|
149
|
+
startTimestamp = performance.now();
|
|
150
|
+
}
|
|
151
|
+
let valueReturned = hookFn(info, secondArg);
|
|
152
|
+
if (startTimestamp !== undefined) {
|
|
153
|
+
info.timing[getTimingKey(hook)] = performance.now() - startTimestamp;
|
|
154
|
+
}
|
|
155
|
+
valueReturned = assertAndNormalizeReturnValue(valueReturned, info, {
|
|
156
|
+
hook,
|
|
157
|
+
});
|
|
158
|
+
currentPlugin = null;
|
|
159
|
+
currentHookName = null;
|
|
160
|
+
return valueReturned;
|
|
161
|
+
};
|
|
162
|
+
const callAsyncHook = async (hook, info, secondArg) => {
|
|
163
|
+
const hookFn = getHookFunction(hook, info);
|
|
164
|
+
if (!hookFn) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
lastPluginUsed = hook.plugin;
|
|
168
|
+
currentPlugin = hook.plugin;
|
|
169
|
+
currentHookName = hook.name;
|
|
170
|
+
let startTimestamp;
|
|
171
|
+
if (info && info.timing) {
|
|
172
|
+
startTimestamp = performance.now();
|
|
173
|
+
}
|
|
174
|
+
let valueReturned = await hookFn(info, secondArg);
|
|
175
|
+
if (startTimestamp !== undefined) {
|
|
176
|
+
info.timing[getTimingKey(hook)] = performance.now() - startTimestamp;
|
|
177
|
+
}
|
|
178
|
+
valueReturned = assertAndNormalizeReturnValue(valueReturned, info, {
|
|
179
|
+
hook,
|
|
180
|
+
});
|
|
181
|
+
currentPlugin = null;
|
|
182
|
+
currentHookName = null;
|
|
183
|
+
return valueReturned;
|
|
184
|
+
};
|
|
185
|
+
// callHooks(hookName, info, callback)
|
|
186
|
+
// callHooks(hookName, info, secondArg, callback)
|
|
187
|
+
const callHooks = (hookName, info, secondArgOrCallback, callback) => {
|
|
188
|
+
const hookSet = hookSetMap.get(hookName);
|
|
189
|
+
if (!hookSet) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
let secondArg;
|
|
193
|
+
if (typeof secondArgOrCallback === "function") {
|
|
194
|
+
callback = secondArgOrCallback;
|
|
195
|
+
secondArg = undefined;
|
|
196
|
+
} else {
|
|
197
|
+
secondArg = secondArgOrCallback;
|
|
198
|
+
}
|
|
199
|
+
const setHookParams = (firstArg = info) => {
|
|
200
|
+
info = firstArg;
|
|
201
|
+
};
|
|
202
|
+
for (const hook of hookSet) {
|
|
203
|
+
const returnValue = callHook(hook, info, secondArg);
|
|
204
|
+
if (returnValue && callback) {
|
|
205
|
+
callback(returnValue, hook.plugin, setHookParams);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
// callAsyncHooks(hookName, info, callback)
|
|
210
|
+
// callAsyncHooks(hookName, info, secondArg, callback)
|
|
211
|
+
const callAsyncHooks = async (
|
|
212
|
+
hookName,
|
|
213
|
+
info,
|
|
214
|
+
secondArgOrCallback,
|
|
215
|
+
callback,
|
|
216
|
+
) => {
|
|
217
|
+
const hookSet = hookSetMap.get(hookName);
|
|
218
|
+
if (!hookSet) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
let secondArg;
|
|
222
|
+
if (typeof secondArgOrCallback === "function") {
|
|
223
|
+
callback = secondArgOrCallback;
|
|
224
|
+
secondArg = undefined;
|
|
225
|
+
} else {
|
|
226
|
+
secondArg = secondArgOrCallback;
|
|
227
|
+
}
|
|
228
|
+
for (const hook of hookSet) {
|
|
229
|
+
const returnValue = await callAsyncHook(hook, info, secondArg);
|
|
230
|
+
if (returnValue && callback) {
|
|
231
|
+
await callback(returnValue, hook.plugin);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
// callHooksUntil(hookName, info)
|
|
236
|
+
// callHooksUntil(hookName, info, secondArg)
|
|
237
|
+
// callHooksUntil(hookName, info, secondArg, until)
|
|
238
|
+
const callHooksUntil = (hookName, info, secondArgOrUntil, until) => {
|
|
239
|
+
const hookSet = hookSetMap.get(hookName);
|
|
240
|
+
if (!hookSet) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
let secondArg;
|
|
244
|
+
if (typeof secondArgOrUntil === "function") {
|
|
245
|
+
until = secondArgOrUntil;
|
|
246
|
+
secondArg = undefined;
|
|
247
|
+
} else {
|
|
248
|
+
secondArg = secondArgOrUntil;
|
|
249
|
+
}
|
|
250
|
+
for (const hook of hookSet) {
|
|
251
|
+
const returnValue = callHook(hook, info, secondArg);
|
|
252
|
+
if (until) {
|
|
253
|
+
const untilValue = until(returnValue);
|
|
254
|
+
if (untilValue) {
|
|
255
|
+
return untilValue;
|
|
256
|
+
}
|
|
257
|
+
} else if (returnValue) {
|
|
258
|
+
return returnValue;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
};
|
|
263
|
+
// callAsyncHooksUntil(hookName, info)
|
|
264
|
+
// callAsyncHooksUntil(hookName, info, secondArg)
|
|
265
|
+
const callAsyncHooksUntil = async (hookName, info, secondArg) => {
|
|
266
|
+
const hookSet = hookSetMap.get(hookName);
|
|
267
|
+
if (!hookSet) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
if (hookSet.size === 0) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
const iterator = hookSet.values()[Symbol.iterator]();
|
|
274
|
+
let result;
|
|
275
|
+
const visit = async () => {
|
|
276
|
+
const { done, value: hook } = iterator.next();
|
|
277
|
+
if (done) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const returnValue = await callAsyncHook(hook, info, secondArg);
|
|
281
|
+
if (returnValue) {
|
|
282
|
+
result = returnValue;
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
await visit();
|
|
286
|
+
};
|
|
287
|
+
await visit();
|
|
288
|
+
return result;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
activePlugins,
|
|
293
|
+
|
|
294
|
+
callHook,
|
|
295
|
+
callAsyncHook,
|
|
296
|
+
callHooks,
|
|
297
|
+
callHooksUntil,
|
|
298
|
+
callAsyncHooks,
|
|
299
|
+
callAsyncHooksUntil,
|
|
300
|
+
|
|
301
|
+
getPluginMeta: (pluginName) => meta[pluginName],
|
|
302
|
+
getMeta: () => meta,
|
|
303
|
+
getLastPluginUsed: () => lastPluginUsed,
|
|
304
|
+
getCurrentPlugin: () => currentPlugin,
|
|
305
|
+
getCurrentHookName: () => currentHookName,
|
|
306
|
+
};
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const callInitOnPlugin = async (plugin, getInitPluginArgs) => {
|
|
310
|
+
const { init } = plugin;
|
|
311
|
+
if (!init) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
const initArgs = getInitPluginArgs ? getInitPluginArgs(plugin) : [];
|
|
315
|
+
const initReturnValue = await init(...initArgs);
|
|
316
|
+
if (initReturnValue === false) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
if (typeof initReturnValue === "function" && !plugin.destroy) {
|
|
320
|
+
plugin.destroy = initReturnValue;
|
|
321
|
+
}
|
|
322
|
+
return true;
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const defaultGetTimingKey = (hook) =>
|
|
326
|
+
`${hook.name}-${hook.plugin.name.replace("jsenv:", "")}`;
|
|
327
|
+
const defaultGetEffectArgs = ({ otherPlugins }) => [{ otherPlugins }];
|
|
328
|
+
|
|
329
|
+
// By default, hook values can be a function or an object keyed by info.type.
|
|
330
|
+
// When an object, the hook for the matching type (or "*") is used.
|
|
331
|
+
const defaultGetHookFunction = (hook, info = {}) => {
|
|
332
|
+
const hookValue = hook.value;
|
|
333
|
+
if (typeof hookValue === "object") {
|
|
334
|
+
const hookForType = hookValue[info.type] || hookValue["*"];
|
|
335
|
+
if (!hookForType) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
return hookForType;
|
|
339
|
+
}
|
|
340
|
+
return hookValue;
|
|
341
|
+
};
|
package/src/router/router.js
CHANGED
|
@@ -188,19 +188,20 @@ export const createRouter = (
|
|
|
188
188
|
upgrade: false,
|
|
189
189
|
};
|
|
190
190
|
|
|
191
|
-
let
|
|
191
|
+
let currentServerPlugin;
|
|
192
192
|
let currentRoutingTiming;
|
|
193
193
|
const onRouteMatchStart = (route) => {
|
|
194
|
-
|
|
194
|
+
const { serverPlugin } = route;
|
|
195
|
+
if (serverPlugin === currentServerPlugin) {
|
|
195
196
|
return;
|
|
196
197
|
}
|
|
197
198
|
onRouteGroupEnd(route);
|
|
198
199
|
currentRoutingTiming = fetchSecondArg.timing(
|
|
199
|
-
|
|
200
|
-
? `${
|
|
200
|
+
serverPlugin && serverPlugin.name
|
|
201
|
+
? `${serverPlugin.name.replace("jsenv:", "")}.routing`
|
|
201
202
|
: "routing",
|
|
202
203
|
);
|
|
203
|
-
|
|
204
|
+
currentServerPlugin = serverPlugin;
|
|
204
205
|
};
|
|
205
206
|
const onRouteGroupEnd = () => {
|
|
206
207
|
if (currentRoutingTiming) {
|
|
@@ -540,6 +541,7 @@ const createRoute = ({
|
|
|
540
541
|
description,
|
|
541
542
|
headers,
|
|
542
543
|
service,
|
|
544
|
+
serverPlugin,
|
|
543
545
|
availableMediaTypes = [],
|
|
544
546
|
availableLanguages = [],
|
|
545
547
|
availableVersions = [],
|
|
@@ -589,6 +591,7 @@ const createRoute = ({
|
|
|
589
591
|
resource,
|
|
590
592
|
description,
|
|
591
593
|
service,
|
|
594
|
+
serverPlugin,
|
|
592
595
|
availableMediaTypes,
|
|
593
596
|
availableLanguages,
|
|
594
597
|
availableVersions,
|
package/src/start_server.js
CHANGED
|
@@ -23,17 +23,14 @@ import { composeTwoResponses } from "./internal/response_composition.js";
|
|
|
23
23
|
import { createPolyglotServer } from "./internal/server-polyglot.js";
|
|
24
24
|
import { trackServerPendingConnections } from "./internal/trackServerPendingConnections.js";
|
|
25
25
|
import { trackServerPendingRequests } from "./internal/trackServerPendingRequests.js";
|
|
26
|
+
import { serverPluginAutoreloadOnRestart } from "./plugins/autoreload_on_server_restart/server_plugin_autoreload_on_server_restart.js";
|
|
27
|
+
import { serverPluginDefaultBody4xx5xx } from "./plugins/default_body_4xx_5xx/server_plugin_default_body_4xx_5xx.js";
|
|
28
|
+
import { serverPluginInternalClientFiles } from "./plugins/internal_client_files/server_plugin_internal_client_files.js";
|
|
29
|
+
import { serverPluginOpenFile } from "./plugins/open_file/server_plugin_open_file.js";
|
|
30
|
+
import { serverPluginRouteInspector } from "./plugins/route_inspector/server_plugin_route_inspector.js";
|
|
31
|
+
import { createServerPluginsController } from "./plugins/server_plugins_controller.js";
|
|
26
32
|
import { createRouter } from "./router/router.js";
|
|
27
33
|
import { timingToServerTimingResponseHeaders } from "./server_timing/timing_header.js";
|
|
28
|
-
import {
|
|
29
|
-
createServiceController,
|
|
30
|
-
flattenAndFilterServices,
|
|
31
|
-
} from "./service_controller.js";
|
|
32
|
-
import { jsenvServiceAutoreloadOnRestart } from "./services/autoreload_on_server_restart/jsenv_service_autoreload_on_server_restart.js";
|
|
33
|
-
import { jsenvServiceDefaultBody4xx5xx } from "./services/default_body_4xx_5xx/jsenv_service_default_body_4xx_5xx.js";
|
|
34
|
-
import { jsenvServiceInternalClientFiles } from "./services/internal_client_files/jsenv_service_internal_client_files.js";
|
|
35
|
-
import { jsenvServiceOpenFile } from "./services/open_file/jsenv_service_open_file.js";
|
|
36
|
-
import { jsenvServiceRouteInspector } from "./services/route_inspector/jsenv_service_route_inspector.js";
|
|
37
34
|
import {
|
|
38
35
|
STOP_REASON_INTERNAL_ERROR,
|
|
39
36
|
STOP_REASON_NOT_SPECIFIED,
|
|
@@ -42,7 +39,7 @@ import {
|
|
|
42
39
|
STOP_REASON_PROCESS_SIGHUP,
|
|
43
40
|
STOP_REASON_PROCESS_SIGINT,
|
|
44
41
|
STOP_REASON_PROCESS_SIGTERM,
|
|
45
|
-
} from "./
|
|
42
|
+
} from "./stop_reasons.js";
|
|
46
43
|
import { getWebSocketHandler } from "./web_socket_response.js";
|
|
47
44
|
|
|
48
45
|
import { applyDnsResolution } from "./internal/dns_resolution.js";
|
|
@@ -81,7 +78,7 @@ export const startServer = async ({
|
|
|
81
78
|
stopOnInternalError = false,
|
|
82
79
|
keepProcessAlive = true,
|
|
83
80
|
routes = [],
|
|
84
|
-
|
|
81
|
+
plugins = [],
|
|
85
82
|
nagle = true,
|
|
86
83
|
serverTiming = false,
|
|
87
84
|
requestWaitingMs = 0,
|
|
@@ -155,25 +152,26 @@ export const startServer = async ({
|
|
|
155
152
|
}
|
|
156
153
|
}
|
|
157
154
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
...
|
|
155
|
+
plugins = [
|
|
156
|
+
serverPluginOpenFile(),
|
|
157
|
+
serverPluginDefaultBody4xx5xx(),
|
|
158
|
+
serverPluginRouteInspector(),
|
|
159
|
+
serverPluginInternalClientFiles(),
|
|
160
|
+
serverPluginAutoreloadOnRestart(),
|
|
161
|
+
...plugins,
|
|
165
162
|
];
|
|
166
163
|
|
|
164
|
+
const serverPluginsController = await createServerPluginsController(plugins);
|
|
167
165
|
const allRouteArray = [];
|
|
168
166
|
for (const route of routes) {
|
|
169
167
|
allRouteArray.push(route);
|
|
170
168
|
}
|
|
171
|
-
for (const
|
|
172
|
-
const
|
|
173
|
-
if (
|
|
174
|
-
for (const
|
|
175
|
-
|
|
176
|
-
allRouteArray.push(
|
|
169
|
+
for (const serverPlugin of serverPluginsController.activePlugins) {
|
|
170
|
+
const routes = serverPlugin.routes;
|
|
171
|
+
if (routes) {
|
|
172
|
+
for (const route of routes) {
|
|
173
|
+
route.serverPlugin = serverPlugin;
|
|
174
|
+
allRouteArray.push(route);
|
|
177
175
|
}
|
|
178
176
|
}
|
|
179
177
|
}
|
|
@@ -184,8 +182,6 @@ export const startServer = async ({
|
|
|
184
182
|
});
|
|
185
183
|
|
|
186
184
|
const server = {};
|
|
187
|
-
|
|
188
|
-
const serviceController = createServiceController(services);
|
|
189
185
|
const processTeardownEvents = {
|
|
190
186
|
SIGHUP: stopOnExit,
|
|
191
187
|
SIGTERM: stopOnExit,
|
|
@@ -296,7 +292,7 @@ export const startServer = async ({
|
|
|
296
292
|
serverOrigins[key] = new URL(`${serverOrigins[key]}:${port}`).origin;
|
|
297
293
|
});
|
|
298
294
|
|
|
299
|
-
|
|
295
|
+
serverPluginsController.callHooks("serverListening", { port });
|
|
300
296
|
startServerOperation.addAbortCallback(async () => {
|
|
301
297
|
await stopListening(nodeServer);
|
|
302
298
|
});
|
|
@@ -341,7 +337,7 @@ export const startServer = async ({
|
|
|
341
337
|
}
|
|
342
338
|
stopCallbackSet.clear();
|
|
343
339
|
await Promise.all(promises);
|
|
344
|
-
|
|
340
|
+
serverPluginsController.callHooks("serverStopped", { reason });
|
|
345
341
|
status = "stopped";
|
|
346
342
|
stoppedResolve(reason);
|
|
347
343
|
});
|
|
@@ -396,7 +392,7 @@ export const startServer = async ({
|
|
|
396
392
|
});
|
|
397
393
|
|
|
398
394
|
const applyRequestInternalRedirection = (request) => {
|
|
399
|
-
|
|
395
|
+
serverPluginsController.callHooks(
|
|
400
396
|
"redirectRequest",
|
|
401
397
|
request,
|
|
402
398
|
{},
|
|
@@ -501,7 +497,7 @@ export const startServer = async ({
|
|
|
501
497
|
headersToInject,
|
|
502
498
|
);
|
|
503
499
|
}
|
|
504
|
-
|
|
500
|
+
serverPluginsController.callHooks(
|
|
505
501
|
"injectResponseProperties",
|
|
506
502
|
request,
|
|
507
503
|
responseProperties,
|
|
@@ -559,7 +555,7 @@ export const startServer = async ({
|
|
|
559
555
|
);
|
|
560
556
|
},
|
|
561
557
|
};
|
|
562
|
-
await
|
|
558
|
+
await serverPluginsController.callAsyncHooks(
|
|
563
559
|
"augmentRouteFetchSecondArg",
|
|
564
560
|
request,
|
|
565
561
|
fetchSecondArg,
|
|
@@ -599,7 +595,7 @@ export const startServer = async ({
|
|
|
599
595
|
stop(STOP_REASON_INTERNAL_ERROR);
|
|
600
596
|
}
|
|
601
597
|
const handleErrorReturnValue =
|
|
602
|
-
await
|
|
598
|
+
await serverPluginsController.callAsyncHooksUntil("handleError", e, {
|
|
603
599
|
request,
|
|
604
600
|
});
|
|
605
601
|
if (!handleErrorReturnValue) {
|
|
@@ -718,19 +714,19 @@ export const startServer = async ({
|
|
|
718
714
|
return;
|
|
719
715
|
}
|
|
720
716
|
|
|
721
|
-
let
|
|
717
|
+
let preventedByPlugin = null;
|
|
722
718
|
const prevent = () => {
|
|
723
|
-
|
|
719
|
+
preventedByPlugin = serverPluginsController.getCurrentPlugin();
|
|
724
720
|
};
|
|
725
|
-
|
|
721
|
+
serverPluginsController.callHooksUntil(
|
|
726
722
|
"onResponsePush",
|
|
727
723
|
{ path, method },
|
|
728
724
|
{ request, prevent },
|
|
729
|
-
() =>
|
|
725
|
+
() => preventedByPlugin,
|
|
730
726
|
);
|
|
731
|
-
if (
|
|
727
|
+
if (preventedByPlugin) {
|
|
732
728
|
pushRequestLogger.debug(
|
|
733
|
-
`response push prevented by "${
|
|
729
|
+
`response push prevented by "${preventedByPlugin.name}" plugin`,
|
|
734
730
|
);
|
|
735
731
|
return;
|
|
736
732
|
}
|
package/src/main.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This file is the first file executed by code using the package
|
|
3
|
-
* Its responsability is to export what is documented
|
|
4
|
-
* Ideally this file should be kept simple to help discovering codebase progressively.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// tslint:disable:ordered-imports
|
|
8
|
-
|
|
9
|
-
export { startServer } from "./start_server.js";
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
STOP_REASON_INTERNAL_ERROR,
|
|
13
|
-
STOP_REASON_PROCESS_SIGHUP,
|
|
14
|
-
STOP_REASON_PROCESS_SIGTERM,
|
|
15
|
-
STOP_REASON_PROCESS_SIGINT,
|
|
16
|
-
STOP_REASON_PROCESS_BEFORE_EXIT,
|
|
17
|
-
STOP_REASON_PROCESS_EXIT,
|
|
18
|
-
STOP_REASON_NOT_SPECIFIED,
|
|
19
|
-
} from "./stopReasons.js";
|
|
20
|
-
export { jsenvServiceErrorHandler } from "./services/error_handler/jsenv_service_error_handler.js";
|
|
21
|
-
|
|
22
|
-
// filesystem
|
|
23
|
-
export {
|
|
24
|
-
createFileSystemFetch,
|
|
25
|
-
fetchFileSystem,
|
|
26
|
-
} from "./filesystem/fetch_filesystem.js";
|
|
27
|
-
|
|
28
|
-
// webSocket
|
|
29
|
-
export { WebSocketResponse } from "./web_socket_response.js";
|
|
30
|
-
// long polling
|
|
31
|
-
export { ProgressiveResponse } from "./progressive_response.js";
|
|
32
|
-
// Server event (can be used to push events to clients that can be connected with WebSocket or EventSource)
|
|
33
|
-
export { ServerEvents, LazyServerEvents } from "./server_events.js";
|
|
34
|
-
|
|
35
|
-
// CORS
|
|
36
|
-
export {
|
|
37
|
-
jsenvServiceCORS,
|
|
38
|
-
jsenvAccessControlAllowedHeaders,
|
|
39
|
-
jsenvAccessControlAllowedMethods,
|
|
40
|
-
} from "./services/cors/jsenv_service_cors.js";
|
|
41
|
-
|
|
42
|
-
// content-negotiation
|
|
43
|
-
export { pickContentType } from "./content_negotiation/pick_content_type.js";
|
|
44
|
-
export { pickContentEncoding } from "./content_negotiation/pick_content_encoding.js";
|
|
45
|
-
export { pickContentLanguage } from "./content_negotiation/pick_content_language.js";
|
|
46
|
-
export { jsenvServiceResponseAcceptanceCheck } from "./services/response_acceptance_check/jsenv_service_response_acceptance_check.js";
|
|
47
|
-
|
|
48
|
-
// others
|
|
49
|
-
export { serveDirectory } from "./filesystem/serve_directory.js";
|
|
50
|
-
export { composeTwoResponses } from "./internal/response_composition.js";
|
|
51
|
-
export { jsenvServiceRequestAliases } from "./services/request_aliases/jsenv_service_request_aliases.js";
|
|
52
|
-
export { findFreePort } from "./internal/listen.js";
|