@lmjs/cli 1.0.13 → 1.0.14

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 (2) hide show
  1. package/lib/app.js +70 -90
  2. package/package.json +1 -1
package/lib/app.js CHANGED
@@ -1,26 +1,46 @@
1
1
  var fs = require('fs');
2
2
  const axios = require('axios').default;
3
3
  const path = require("path");
4
- var uws = require('uWebSockets.js');
4
+
5
+ const express = require('express');
6
+ var compression = require('compression');
7
+ const app = express();
8
+ const server = require('http').createServer(app);
9
+ var WebSocketServer = require("ws").Server;
10
+ var wss = new WebSocketServer({ server: server, path: "/" });
5
11
  const fcs = require("./fcs.js");
6
- const mime = require('mime-types');
7
12
  var cl = console.log;
8
- var zlib = require('zlib');
13
+
14
+ var cookieParser = require('cookie-parser');
15
+ var expressSession = require('express-session');
16
+ const bodyParser = require('body-parser');
17
+ var multer = require('multer');
18
+ var upload = multer();
19
+
20
+ let session = expressSession({
21
+ secret: 'keyboardcat',
22
+ resave: true,
23
+ saveUninitialized: true,
24
+ cookie: { maxAge: 9999000 }
25
+ });
26
+
9
27
  var config = fcs.getConfig();
10
28
  var _views = {};
11
29
  var _comps = {};
12
- function toArrayBuffer(buffer) {
13
- return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
14
- }
15
- uws.App().ws('/*', {
16
- idleTimeout: 8,
17
- maxBackpressure: 1024 * 1024,
18
- maxPayloadLength: 16 * 1024 * 1024,
19
- compression: uws.SHARED_COMPRESSOR,
20
- open: (ws) => {
21
- ws.subscribe('/home/rat');
22
- },
23
- message: async (ws, message, isBinary) => {
30
+
31
+ app.use(compression());
32
+ app.use(cookieParser());
33
+ app.use(session);
34
+ app.use('/', express.static(`${process.cwd()}` + path.sep + `src`));
35
+ app.set('etag', false);
36
+ app.use(bodyParser.urlencoded({ extended: true }));
37
+ app.use(bodyParser.json());
38
+ app.use(bodyParser.raw());
39
+ app.use(upload.array());
40
+
41
+ wss.on('connection', (ws) => {
42
+ ws.on('message', async (message) => {
43
+
24
44
  try {
25
45
  const buffer = Buffer.from(message);
26
46
  const data = JSON.parse(buffer.toString());
@@ -53,95 +73,55 @@ uws.App().ws('/*', {
53
73
  headers: _body['headers'] ?? config.apis[_body['api'] ?? 0].headers
54
74
  });
55
75
  _data.payload = res.data;
56
- if (ws.isSubscribed('/home/rat')) ws.send(JSON.stringify(_data), false, true);
76
+ ws.send(JSON.stringify(_data));
57
77
 
58
78
  } catch (e) {
59
79
  _data.payload = { "code": 108, "error": e.message, "type": "RequestError" };
60
- if (ws.isSubscribed('/home/rat')) ws.send(JSON.stringify(_data), false, true);
80
+ ws.send(JSON.stringify(_data));
61
81
  }
62
82
  return;
63
83
  }
64
84
 
65
- if (ws.isSubscribed('/home/rat')) ws.send(JSON.stringify(_data), false, true);
85
+ ws.send(JSON.stringify(_data));
66
86
 
67
87
  }
68
88
  } catch (e) {
69
89
 
70
90
  }
71
- },
72
- drain: (ws) => {
73
- },
74
- close: (ws, code, message) => {
75
- }
76
-
77
- }).get('/*', async (res, req) => {
78
- let nodes = req.getUrl().split("/");
79
- while (nodes[0] == "") nodes.shift();
80
- let _nodes = JSON.parse(JSON.stringify(nodes));
81
- let pubDir = `${process.cwd()}` + path.sep + `src`;
82
- let filePath = "";
83
- let errorPath = path.sep + '404.html';
84
- if (nodes[0]) {
85
- let last = nodes[nodes.length - 1];
86
- while (_nodes.length) filePath += path.sep + _nodes.shift();
87
- if (last.indexOf('.') > -1) {
88
- filePath = fs.existsSync(pubDir + path.sep + filePath) ? filePath : errorPath;
89
- } else {
90
- if (!fs.existsSync(pubDir + path.sep + `views` + path.sep + nodes[0] + '.view')) {
91
- filePath = errorPath;
92
- } else {
93
- filePath = path.sep + 'index.html';
94
- }
95
- }
96
- } else filePath = path.sep + 'index.html';
97
-
98
- let readStream;
99
- let paths = req.getUrl();
100
- const acceptEncoding = req.getHeader('accept-encoding');
101
-
102
- // at this point you need a way to get the requested file and catch errors if the file does not exist:
103
- const fileName = pubDir + filePath; // i have some DANGEROUS_PATH_PATTERN test on the path var you must make sure it's safe
104
- try {
105
- readStream = toArrayBuffer(fs.readFileSync(fileName)); // toArrayBuffer is a function i got from examples
106
- } catch (err) {
107
- readStream = 'Error reading file.. or something';
108
- }
109
-
110
- readStream = toArrayBuffer(fs.readFileSync(fileName));
111
- // setting headers and responding: res.aborted must be present
112
- res.onAborted(() => {
113
- res.aborted = true;
114
- });
115
- // write the headers and respond
116
-
117
- res.cork(() => {
118
- const mimeT = mime.lookup(pubDir + filePath) || 'application/octet-stream';
119
- res.writeHeader('Content-Type', mimeT);
120
- // for (let i = 0; i < securityHeaders.length; i++) res.writeHeader(securityHeaders[i].name, securityHeaders[i].value);
121
- if (!acceptEncoding) acceptEncoding = '';
122
- if (/\bgzip\b/.test(acceptEncoding)) {
123
- res.writeHeader('Content-Encoding', 'gzip');
124
- readStream = zlib.gzipSync(readStream);
125
- } else if (/\bdeflate\b/.test(acceptEncoding)) {
126
- res.writeHeader('Content-Encoding', 'deflate');
127
- readStream = zlib.deflateSync(readStream);
128
- }
129
- if (!res.aborted) res.end(readStream);
130
91
  });
131
92
 
93
+ ws.send('Hi there, I am a WebSocket server');
94
+ });
95
+
96
+ global.app = app;
97
+ global.axios = axios;
98
+ global.wss = wss;
99
+ global.fs = fs;
100
+ global.path = path;
101
+
102
+ var API = fcs.API;
103
+ global.API = API;
104
+
105
+ if (fs.existsSync(`${process.cwd()}` + path.sep + `src` + path.sep + `app.js`)) {
106
+ require(`${process.cwd()}` + path.sep + `src` + path.sep + `app.js`);
107
+ }
108
+
109
+ app.get('*', function (req, res) {
110
+ let nodes = req.params[0].split("/");
111
+ if (nodes.length) nodes.shift();
112
+ res.sendFile(`${process.cwd()}` + path.sep + `src` + path.sep + ((fs.existsSync(`${process.cwd()}` + path.sep + `src` + path.sep + `views` + path.sep + nodes[0] + `.view`)) ? 'index' : '404') + `.html`);
113
+ return;
114
+ });
115
+
116
+ app.post('*', function (req, res) {
117
+ let nodes = req.params[0].split("/");
118
+ if (nodes.length) nodes.shift();
119
+ res.sendFile(`${process.cwd()}` + path.sep + `src` + path.sep + ((fs.existsSync(`${process.cwd()}` + path.sep + `src` + path.sep + `views` + path.sep + nodes[0] + `.view`)) ? 'index' : '404') + `.html`);
132
120
  return;
133
- }).any('/*', (res, req) => {
134
- res.end('Nothing to see here!');
135
- }).listen(config.server.port * 1, (listenSocket) => {
136
- if (listenSocket) {
137
- // for automatic port selection use 0 as port above and we'll get one below this line.
138
- // port = uws.us_socket_local_port(listenSocket);
139
- // console.log('Listening to port ' + port);
140
- console.log(`LumenJS server running at http://${config.server.host}:${config.server.port}/`);
141
- _views = fcs.getAllFiles(`${process.cwd()}` + path.sep + `src` + path.sep + `views` + path.sep);
142
- _comps = fcs.getAllFiles(`${process.cwd()}` + path.sep + `src` + path.sep + `comps` + path.sep);
143
- } else {
144
- console.log('Failed to run LumenJS server');
145
- }
121
+ });
146
122
 
123
+ server.listen(config.server.port, function () {
124
+ console.log(`LumenJS server running at http://${config.server.host}:${config.server.port}/`);
125
+ _views = fcs.getAllFiles(`${process.cwd()}` + path.sep + `src` + path.sep + `views` + path.sep);
126
+ _comps = fcs.getAllFiles(`${process.cwd()}` + path.sep + `src` + path.sep + `comps` + path.sep);
147
127
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lmjs/cli",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "LumenJS is a tiny robust reactive javascript framework for building scalable and high-performant web applications and web user interfaces, be it simple or complex, with standard HTML, CSS and JavaScript",
5
5
  "main": "./index.js",
6
6
  "bin": {