@webqit/webflo 0.8.46 → 0.8.50

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/docker/Dockerfile +25 -0
  2. package/docker/README.md +71 -0
  3. package/package.json +9 -8
  4. package/src/cmd/client.js +68 -97
  5. package/src/cmd/origins.js +2 -2
  6. package/src/cmd/server.js +1 -1
  7. package/src/modules/Router.js +130 -0
  8. package/src/modules/_FormData.js +60 -0
  9. package/src/modules/_Headers.js +88 -0
  10. package/src/modules/_MessageStream.js +191 -0
  11. package/src/modules/_NavigationEvent.js +89 -0
  12. package/src/modules/_Request.js +61 -0
  13. package/src/modules/_RequestHeaders.js +72 -0
  14. package/src/modules/_Response.js +56 -0
  15. package/src/modules/_ResponseHeaders.js +81 -0
  16. package/src/modules/_URL.js +111 -0
  17. package/src/modules/client/Cache.js +38 -0
  18. package/src/modules/client/Client.js +51 -22
  19. package/src/modules/client/Http.js +26 -11
  20. package/src/modules/client/NavigationEvent.js +20 -0
  21. package/src/modules/client/Router.js +30 -104
  22. package/src/modules/client/StdRequest.js +34 -33
  23. package/src/modules/client/Storage.js +56 -0
  24. package/src/modules/client/Url.js +1 -1
  25. package/src/modules/client/Worker.js +74 -68
  26. package/src/modules/client/WorkerClient.js +102 -0
  27. package/src/modules/client/WorkerComm.js +183 -0
  28. package/src/modules/client/effects/sounds.js +64 -0
  29. package/src/modules/server/NavigationEvent.js +38 -0
  30. package/src/modules/server/Router.js +53 -124
  31. package/src/modules/server/Server.js +195 -87
  32. package/src/modules/util.js +7 -7
  33. package/src/modules/NavigationEvent.js +0 -32
  34. package/src/modules/Response.js +0 -98
  35. package/src/modules/XURL.js +0 -125
  36. package/src/modules/client/ClientNavigationEvent.js +0 -22
  37. package/src/modules/client/Push.js +0 -84
  38. package/src/modules/server/ServerNavigationEvent.js +0 -23
  39. package/src/modules/server/StdIncomingMessage.js +0 -70
@@ -5,117 +5,50 @@
5
5
  import Fs from 'fs';
6
6
  import Url from 'url';
7
7
  import Path from 'path';
8
- import _isString from '@webqit/util/js/isString.js';
9
- import _isArray from '@webqit/util/js/isArray.js';
10
- import _arrFrom from '@webqit/util/arr/from.js';
8
+ import Mime from 'mime-types';
9
+ import _Router from '../Router.js';
11
10
 
12
- export default class Router {
11
+ /**
12
+ * ---------------------------
13
+ * The Router class
14
+ * ---------------------------
15
+ */
16
+
17
+ export default class Router extends _Router {
13
18
 
14
- /**
15
- * Instantiates a new Router.
16
- *
17
- * @param string|array path
18
- * @param object layout
19
- * @param object context
20
- *
21
- * @return void
22
- */
23
- constructor(path, layout, context) {
24
- this.path = _isArray(path) ? path : (path + '').split('/').filter(a => a);
25
- this.layout = layout;
26
- this.context = context;
19
+ async readTick(thisTick) {
20
+ if (thisTick.trail) {
21
+ thisTick.currentSegment = thisTick.destination[thisTick.trail.length];
22
+ thisTick.currentSegmentOnFile = [ thisTick.currentSegment, '-' ].reduce((_segmentOnFile, _seg) => {
23
+ if (_segmentOnFile.index) return _segmentOnFile;
24
+ var _currentPath = thisTick.trailOnFile.concat(_seg).join('/'),
25
+ routeHandlerFile;
26
+ return Fs.existsSync(routeHandlerFile = Path.join(this.layout.ROOT, this.layout.SERVER_DIR, _currentPath, 'index.js')) ? { seg: _seg, index: routeHandlerFile } : (
27
+ Fs.existsSync(Path.join(this.layout.ROOT, this.layout.SERVER_DIR, _currentPath)) ? { seg: _seg, dirExists: true } : _segmentOnFile
28
+ );
29
+ }, { seg: null });
30
+ thisTick.trail.push(thisTick.currentSegment);
31
+ thisTick.trailOnFile.push(thisTick.currentSegmentOnFile.seg);
32
+ thisTick.exports = thisTick.currentSegmentOnFile.index ? await import(Url.pathToFileURL(thisTick.currentSegmentOnFile.index)) : undefined;
33
+ } else {
34
+ thisTick.trail = [];
35
+ thisTick.trailOnFile = [];
36
+ thisTick.currentSegmentOnFile = { index: Path.join(this.layout.ROOT, this.layout.SERVER_DIR, 'index.js') };
37
+ thisTick.exports = Fs.existsSync(thisTick.currentSegmentOnFile.index)
38
+ ? await import(Url.pathToFileURL(thisTick.currentSegmentOnFile.index))
39
+ : null;
40
+ }
41
+ return thisTick;
27
42
  }
28
43
 
29
- /**
30
- * Performs dynamic routing.
31
- *
32
- * @param array|string target
33
- * @param array argsA
34
- * @param any input
35
- * @param function _default
36
- * @param array argsB
37
- *
38
- * @return object
39
- */
40
- async route(target, argsA, input, _default, argsB = []) {
41
-
42
- target = _arrFrom(target);
43
- var layout = this.layout;
44
- var context = this.context;
44
+ finalizeHandlerContext(context, thisTick) {
45
+ if (thisTick.currentSegmentOnFile.index) {
46
+ context.dirname = Path.dirname(thisTick.currentSegmentOnFile.index);
47
+ }
48
+ }
45
49
 
46
- // ----------------
47
- // ROUTER
48
- // ----------------
49
- const next = async function(index, input, path) {
50
-
51
- var exports, routeHandlerFile, wildcardRouteHandlerFile;
52
- if (index === 0) {
53
- routeHandlerFile = 'index.js';
54
- } else if (path[index - 1]) {
55
- var routeSlice = path.slice(0, index).join('/');
56
- var wildcardRouteSlice = path.slice(0, index - 1).concat('-').join('/');
57
- routeHandlerFile = Path.join(routeSlice, './index.js');
58
- wildcardRouteHandlerFile = Path.join(wildcardRouteSlice, './index.js');
59
- }
60
-
61
- if ((routeHandlerFile && Fs.existsSync(routeHandlerFile = Path.join(layout.ROOT, layout.SERVER_DIR, routeHandlerFile)))
62
- || (wildcardRouteHandlerFile && Fs.existsSync(routeHandlerFile = Path.join(layout.ROOT, layout.SERVER_DIR, wildcardRouteHandlerFile)))) {
63
- exports = await import(Url.pathToFileURL(routeHandlerFile));
64
- // ---------------
65
- const func = target.reduce((func, name) => func || exports[name], null);
66
- if (func) {
67
- // -------------
68
- // Then we can call the handler
69
- // -------------
70
- const _next = (..._args) => {
71
- var _index;
72
- if (_args.length > 1) {
73
- if (!_isString(_args[1])) {
74
- throw new Error('Router redirect must be a string!');
75
- }
76
- var newPath = Path.join(path.slice(0, index).join('/'), _args[1]);
77
- if (newPath.startsWith('../')) {
78
- throw new Error('Router redirect cannot traverse beyond the routing directory! (' + _args[1] + ' >> ' + newPath + ')');
79
- }
80
- _args[1] = newPath.split('/').map(a => a.trim()).filter(a => a);
81
- _index = path.slice(0, index).reduce((build, seg, i) => build.length === i && seg === _args[1][i] ? build.concat(seg) : build, []).length;
82
- } else {
83
- _args[1] = path;
84
- _index = index;
85
- }
86
- return next(_index + 1, ..._args);
87
- };
88
- _next.pathname = path.slice(index).join('/');
89
- _next.stepname = _next.pathname.split('/').shift();
90
- // -------------
91
- const _this = {
92
- pathname: '/' + path.slice(0, index).join('/'),
93
- dirname: Path.dirname(routeHandlerFile),
94
- ...context
95
- };
96
- _this.stepname = _this.pathname.split('/').pop();
97
- // -------------
98
- return await func.bind(_this)(...argsA.concat([input, _next/*next*/].concat(argsB)));
99
- } else {
100
- return next(index + 1, input, path);
101
- }
102
- }
103
-
104
- if (_default) {
105
- // -------------
106
- // Local file
107
- // -------------
108
- const defaultThis = {pathname: '/' + path.join('/'), ...context};
109
- return await _default.call(defaultThis, input);
110
- }
111
-
112
- // -------------
113
- // Recieved response or undefined
114
- // -------------
115
- return;
116
- };
117
-
118
- return next(0, input, this.path);
50
+ pathJoin(...args) {
51
+ return Path.join(...args);
119
52
  }
120
53
 
121
54
  /**
@@ -152,14 +85,21 @@ export default class Router {
152
85
  error: 'Error reading static file: ' + filename + '.',
153
86
  });
154
87
  } else {
88
+
155
89
  // if the file is found, set Content-type and send data
156
- resolve(new event.Response({
157
- contentType: mimeTypes[ext] || 'text/plain',
158
- filename: _filename,
159
- body: ext === '.json' ? data + '' : data,
160
- static: true,
161
- autoIndex,
90
+ const type = Mime.lookup(ext);
91
+ resolve(new event.Response(data, {
92
+ headers: {
93
+ contentType: type === 'application/javascript' ? 'text/javascript' : type,
94
+ contentLength: Buffer.byteLength(data),
95
+ },
96
+ meta: {
97
+ filename: _filename,
98
+ static: true,
99
+ autoIndex,
100
+ }
162
101
  }));
102
+
163
103
  }
164
104
  });
165
105
  });
@@ -206,6 +146,7 @@ const mimeTypes = {
206
146
  '.json': 'application/json',
207
147
  '.css': 'text/css',
208
148
  '.png': 'image/png',
149
+ '.jpeg': 'image/jpeg',
209
150
  '.jpg': 'image/jpeg',
210
151
  '.wav': 'audio/wav',
211
152
  '.mp3': 'audio/mpeg',
@@ -215,15 +156,3 @@ const mimeTypes = {
215
156
  };
216
157
 
217
158
  export { mimeTypes };
218
-
219
- // Static response
220
- export class FixedResponse {
221
- // construct
222
- constructor(content, contentType, filename, autoIndex) {
223
- this.content = content;
224
- this.contentType = contentType;
225
- this.filename = filename;
226
- this.autoIndex = autoIndex;
227
- this.static = true;
228
- }
229
- };