textbrowser 0.42.6 → 0.45.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/CHANGES.md CHANGED
@@ -1,4 +1,20 @@
1
- # textbrowser CHANGES
1
+ # CHANGES to `textbrowser`
2
+
3
+ ## 0.45.0
4
+
5
+ - fix: proper use of `await`
6
+ - fix: avoid extra problematic static server call
7
+ - fix: allow for empty router
8
+ - fix: allow fall-through to express if http server fails to match
9
+ - fix: pass on `next` to express
10
+
11
+ ## 0.44.0
12
+
13
+ - fix: allow passing on if middleware does not match
14
+
15
+ ## 0.43.0
16
+
17
+ - feat: if Express route not detected and `httpServer` present, defer to that
2
18
 
3
19
  ## 0.42.6
4
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "textbrowser",
3
- "version": "0.42.6",
3
+ "version": "0.45.0",
4
4
  "description": "Multilinear text browser",
5
5
  "type": "module",
6
6
  "main": "dist/index-es.min.js",
package/server/main.js CHANGED
@@ -53,6 +53,7 @@ const optionDefinitions = [
53
53
  {name: 'files', type: String},
54
54
  {name: 'namespace', type: String},
55
55
 
56
+ {name: 'httpServer', type: String},
56
57
  {name: 'expressServer', type: String}
57
58
  ];
58
59
  const userParams = commandLineArgs(optionDefinitions);
@@ -121,14 +122,49 @@ const srv = http.createServer(async (req, res) => {
121
122
  }
122
123
  fileServer.serve(req, res);
123
124
  };
125
+
126
+ const next = function () {
127
+ req.addListener('end', staticServer).resume();
128
+ };
129
+
130
+ /**
131
+ * @returns {Promise<void>}
132
+ */
133
+ const runHttpServer = async function () {
134
+ // eslint-disable-next-line no-unsanitized/method -- Site-specified plugin
135
+ const server = (await import(userParams.httpServer)).default();
136
+ return await server(
137
+ req,
138
+ res,
139
+ // For express, we'll first give a chance to other static servers
140
+ // they might supply
141
+ userParams.expressServer ? () => {
142
+ // Empty
143
+ } : next
144
+ );
145
+ };
124
146
  if (userParams.expressServer) {
125
147
  // eslint-disable-next-line no-unsanitized/method -- Site-specified plugin
126
- const server = (await import(userParams.expressServer)).default();
127
- server.get('*', staticServer);
128
- server(req, res);
148
+ const app = (await import(userParams.expressServer)).default();
149
+
150
+ if (userParams.httpServer && (!app._router || !app._router.stack.some(({regexp}) => {
151
+ // Hack to ignore middleware like jsonParser (and hopefully
152
+ // not get any other)
153
+ return regexp.source !== '^\\/?(?=\\/|$)' && regexp.test(req.url);
154
+ }))) {
155
+ await runHttpServer();
156
+ }
157
+
158
+ // app.get('*', staticServer);
159
+ app(req, res, next);
160
+
161
+ return;
162
+ } else if (userParams.httpServer) {
163
+ await runHttpServer();
129
164
  return;
130
165
  }
131
- req.addListener('end', staticServer).resume();
166
+
167
+ next();
132
168
  /*
133
169
  res.writeHead(404, {'Content-Type': 'text/html'});
134
170
  res.end('<h1>File not found</h1>');