webfast 0.1.35 → 0.1.38

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.
@@ -1,6 +1,34 @@
1
1
  console.log(`WebFast!`);
2
- window.WebFast = {
3
- ts : Date.now()
2
+ let web= {};
3
+ web.fast = {
4
+ ts : Date.now(),
5
+ action : function(data,ell) {
6
+ console.log(`Action Function`,data,ell);
7
+ },
8
+ functions : {
9
+ form : function(data,ell) {
10
+ console.log(`Handle Form Function`);
11
+ }
12
+ },
13
+ que : {
14
+ list : [],
15
+ state : false,
16
+ run : function(count){
17
+ // Grab first before sending
18
+ item = web.fast.que.list[0];
19
+ if (item != undefined) {
20
+ delete web.fast.que.list[0];
21
+ web.fast.que.list.slice(1);
22
+ console.log(`Sending Item`,item);
23
+
24
+ web.fast.socket.send(JSON.stringify(item)); // Send data to the server
25
+ }
26
+ }
27
+ },
28
+ tmp : {
29
+ int : {}
30
+ },
31
+ socket : undefined
4
32
  }
5
33
  // Connect to the Socket.IO server
6
34
  const telegram = window.Telegram.WebApp;
@@ -18,25 +46,129 @@ if (telegram.initData == ``) {
18
46
 
19
47
  const socketURL = `wss://${webfastSocket.replace(`https://`,``)}socket.io/?qbt=${setData}`;
20
48
 
21
- try {
22
- new WebSocket(socketURL,`test`,{
23
- timeout : 0
24
- });
49
+ // Assuming you have a variable `socketURL` containing your WebSocket URL
50
+ web.fast.connectWebSocket = function(socketURL,maxRetries = 40, retries = 0) {
51
+ web.fast.createWebSocket = function(socketURL, maxRetries, retries) {
52
+ const ws = new WebSocket(socketURL);
53
+ web.fast.socket = ws;
54
+ ws.onopen = () => {
55
+ console.log('WebSocket connected');
56
+ // Start other things (e.g., send initial data)
57
+ web.fast.que.state = true;
58
+ };
59
+
60
+ ws.onmessage = (event) => {
61
+ console.log('Received:', event.data);
62
+ // Handle received data
63
+ web.fast.que.state = Date.now();
64
+ web.fast.receive(`socket`,event.data); // Placeholder for processing response
65
+ };
66
+
67
+ ws.onclose = (event) => {
68
+ web.fast.que.state = false;
69
+ if (retries < maxRetries) {
70
+ retries++;
71
+ console.log(`WebSocket closed. Retrying in 10 seconds...`);
72
+ setTimeout(web.fast.createWebSocket(socketURL,maxRetries,retries), 10000); // Retry after 10 seconds
73
+ } else {
74
+ console.log(`WebSocket connection failed after ${maxRetries} attempts.`);
75
+ // Handle failure (e.g., show an error message)
76
+ }
77
+ };
78
+
79
+ ws.onerror = (error) => {
80
+ console.error('WebSocket error:', error);
81
+ // Handle error (e.g., show an error message)
82
+ web.fast.que.state = false;
83
+ };
84
+
85
+ // Send message to path for example api.example.event
86
+ web.fast.sendMessage = function(path,message) {
87
+ // Add to que
88
+ web.fast.que.list.push({
89
+ path : path,
90
+ message : message});
91
+ }
92
+ };
93
+
94
+ web.fast.createWebSocket(socketURL, maxRetries, retries); // Initial connection
95
+ }
25
96
 
26
- // Start the other things
27
- //alert(telegram.initData);
28
- } catch (err) {
29
- alert(`Err`);
30
- console.error(err);
97
+ // Call the function to start the WebSocket connection
98
+ web.fast.connectWebSocket(socketURL);
99
+ web.fast.receive = function(data) {
100
+ // Placeholder for processing the received data
101
+ // Implement your logic here (e.g., update UI, handle specific messages)
102
+ console.log('Processing received data:', data);
31
103
  }
32
104
 
33
105
  // On Ready desable all forms with jquery
34
106
  jQuery(document).ready(function() {
35
- jQuery('form').submit(function(e) {
36
- e.preventDefault(); // Prevent the default form submission
107
+
108
+ // Make function
109
+ web.fast.tmp.int.que = setInterval(function(){
110
+ // This is the function
111
+ if (web.fast.que.state != false && web.fast.que.list.length > 0) {
112
+ // State check
113
+ web.fast.que.run();
114
+ }
115
+ },10);
116
+
117
+ // Scan for all webfast ellements
118
+ jQuery(`[webfast]`).each(function() {
119
+ let elementType = jQuery(this).prop('nodeName');
120
+ console.log('Element type:', elementType);
121
+
122
+ // Get now the data
123
+ let webAction = jQuery(this).attr(`webfast`);
124
+ let action = jQuery(this).attr(`webfast-${webAction}`);
125
+ console.log(`Data Actions webAction, action`,webAction,action);
126
+
127
+ // Create action runner
128
+ // Check if id
129
+ if (jQuery(this).attr(`id`) == undefined) {
130
+ const array = new Uint32Array(1);
131
+ window.crypto.getRandomValues(array);
132
+ const randomValue = array[0];
133
+
134
+ console.log(`Set UUID: ${randomValue}`);
135
+ jQuery(this).attr(`id`,randomValue);
136
+ }
37
137
 
38
- // Check what to do like socketpath
39
- alert(`Form Submit`);
40
- return false;
138
+ switch (elementType) {
139
+ case "FORM":
140
+ jQuery(this).submit(function(e) {
141
+ e.preventDefault(); // Prevent the default form submission
142
+
143
+ // Check what to do like socketpath
144
+ web.fast.action(action,this,e);
145
+ return false;
146
+ });
147
+ break;
148
+ case "DIV":
149
+ // We will do the div element part
150
+ // check whyt type etc
151
+ // Make request to server with websocket thingy
152
+ // Set in QUE
153
+ let other;
154
+ if (action == `list`) {
155
+ other = {
156
+ type : action,
157
+ html : jQuery(this).html()
158
+ }
159
+ }
160
+ web.fast.sendMessage(`socket.api.${webAction}`,{
161
+ ts : Date.now(),
162
+ ell : jQuery(this).attr(`id`),
163
+ other : other
164
+ })
165
+ break;
166
+ case "BUTTON":
167
+ // Check button interaction
168
+ console.error(`SETUP BUTTON INTERACTION`);
169
+ break;
170
+ default:
171
+ console.error(`No Action for : ${elementType}`);
172
+ }
41
173
  });
42
174
  });
@@ -0,0 +1,2 @@
1
+ console.log(`Test`);
2
+ console.log(web.fast);
package/example.js CHANGED
@@ -1,5 +1,10 @@
1
1
  const path = require(`path`);
2
+ const contentPath = path.join(__dirname,`example`,`content`)
2
3
  let program = require(path.join(__dirname,`index.js`))({
3
- wget : '/usr/local/bin/wget'
4
+ wget : '/usr/local/bin/wget',
5
+ process : {
6
+ ts : Date.now()
7
+ },
8
+ contentPath : contentPath
4
9
  });
5
10
  console.log(`Required`);
@@ -5,6 +5,10 @@ module.exports = async function(req,res,body,params,command,middleValue) {
5
5
  if (middleValue.location != undefined) {
6
6
  locSendMessage = `Thank you for sending your location\n ${middleValue.location.longitude}\n${middleValue.location.latitude}`;
7
7
  }
8
+
9
+ const scripting = await program.modules.telegram.script.function.check(program,command,middleValue.chat.id,middleValue,body);
10
+ console.log(scripting);
11
+
8
12
  return {
9
13
  message : locSendMessage,
10
14
  response : {
@@ -49,11 +49,15 @@ module.exports = async function(program,req,res,body,params,command,middleValue)
49
49
  console.log(scripting);
50
50
 
51
51
  // Send back
52
- return {
53
- message : locSendMessage,
54
- response : {
52
+ if (scripting == true) {
53
+ return true;
54
+ } else {
55
+ return {
55
56
  message : locSendMessage,
56
- uuid : uuid
57
+ response : {
58
+ message : locSendMessage,
59
+ uuid : uuid
60
+ }
57
61
  }
58
62
  }
59
63
  }
@@ -34,6 +34,7 @@ module.exports = {
34
34
  } catch(err) {
35
35
  console.error(err);
36
36
  console.error(`Error Scripting`);
37
+ return false;
37
38
  }
38
39
  },
39
40
  response : async function() {
@@ -2,6 +2,7 @@ module.exports = async function (program) {
2
2
  console.log(`Starting UP Express`);
3
3
  program.express = {
4
4
  ts: Date.now(),
5
+ process : program.set.process
5
6
  };
6
7
 
7
8
  const express = require('express');
@@ -138,12 +139,18 @@ module.exports = async function (program) {
138
139
 
139
140
  // Check if minify at the end
140
141
  const fileName = req.params.file;
141
- const isMinified = /-min\.js$/.test(fileName);
142
+ const isExtend = /-extend\.js$/.test(fileName);
142
143
 
143
- if (isMinified) {
144
- console.log(`${fileName} ends with -min.js`);
145
- const toRequestFile = req.params.file.replace(`-min.js`, `.js`);
144
+ // Check if extending
145
+ if (isExtend) {
146
+ console.log(`${fileName} ends with -extend.js`);
147
+ // IS extended file include loading in
148
+
149
+ const toRequestFile = req.params.file.replace(`-extend.js`, `.js`);
146
150
  contentFolder = program.path.join(__dirname, `..`, `..`, `app`, `content`, req.params.type, toRequestFile);
151
+ // check if file exists in process, if not make it before giving out
152
+ // Check i
153
+ console.log(`Content Folder`);
147
154
  } else {
148
155
  console.log(`${fileName} does not end with -min.js`);
149
156
  }
@@ -155,6 +162,33 @@ module.exports = async function (program) {
155
162
  }
156
163
  })
157
164
 
165
+ // Walkt through for paths to be open
166
+ if (program.set.contentPath != undefined) {
167
+ // Loop Through
168
+ const readDir = program.fs.readdirSync(program.set.contentPath);
169
+
170
+ // Create request app path for it
171
+ //loop through
172
+ for (let rdi in readDir) {
173
+ // Loop
174
+ const dirItem = readDir[rdi];
175
+ const dirPath = program.path.join(program.set.contentPath,dirItem);
176
+ // Now read the dir
177
+ // Create app.get
178
+ try {
179
+ const theRoute = `/inc/${dirItem}/*`;
180
+ app.get(theRoute, async (req, res) => {
181
+ const params = req.params;
182
+ console.log(`The Route is there`, theRoute);
183
+ });
184
+
185
+ } catch (err) {
186
+ console.error(`Errro for path read dir including something`, diritem);
187
+ }
188
+ }
189
+ console.log(`We have directory`);
190
+ }
191
+
158
192
  app.listen(port, () => {
159
193
  console.log(`Server Listening`, port, basePath);
160
194
  });
@@ -267,16 +301,46 @@ module.exports = async function (program) {
267
301
  });
268
302
 
269
303
  // WebSocket on message event
270
- ws.on('message', (message) => {
304
+ ws.on('message', async (message) => {
271
305
  console.log(`Received message from ${clientId}: ${message}`);
272
306
 
273
- // Add your custom on message logic here
274
- // For example, you can broadcast the message to all connected clients
275
- clients.forEach((client, id) => {
276
- if (client.readyState === WebSocket.OPEN && id !== clientId) {
277
- client.send(`Broadcast from ${clientId}: ${message}`);
307
+ try {
308
+ // Check for function
309
+ const json = JSON.parse(message.toString(`utf-8`));
310
+ const data = json.message;
311
+ const path = json.path;
312
+ const split = path.split(".")
313
+
314
+ // Check if function is running in program modules that you can add in the init scirpt when using remote
315
+ if (program.express.process != undefined) {
316
+ try {
317
+ let resp = await program.express.process[split[0]][split[1]][split[2]](program,ws,json,data,path);
318
+ if (resp != false) {
319
+ ws.send(JSON.stringify(resp));
320
+ }
321
+ } catch (err) {
322
+ console.error(`Error Running program.express.process for `,split[0],split[1],split[2]);
323
+ ws.send(JSON.stringify({
324
+ ts : Date.now(),
325
+ error : true,
326
+ message : `Error Event Program receive`
327
+ }));
328
+ }
278
329
  }
279
- });
330
+
331
+ // Add your custom on message logic here
332
+ // For example, you can broadcast the message to all connected clients
333
+ clients.forEach((client, id) => {
334
+ if (client.readyState === WebSocket.OPEN && id !== clientId) {
335
+ //ws.send(`Broadcast from ${clientId}: ${message}`);
336
+ }
337
+ });
338
+
339
+ // Check if
340
+ } catch(err) {
341
+ console.error(`Error Something`);
342
+
343
+ }
280
344
  });
281
345
  });
282
346
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webfast",
3
- "version": "0.1.35",
3
+ "version": "0.1.38",
4
4
  "description": "WebFast! Bot Application, including TON mini-apps for makign it easy and fast to build ini-apps",
5
5
  "main": "index.js",
6
6
  "repository": {