webfast 0.1.38 → 0.1.42

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,8 +6,71 @@ web.fast = {
6
6
  console.log(`Action Function`,data,ell);
7
7
  },
8
8
  functions : {
9
+ isURL : function(str) {
10
+ // Regular expression to check if a string is a URL
11
+ var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
12
+ '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
13
+ '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
14
+ '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
15
+ '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
16
+ '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
17
+ return pattern.test(str);
18
+ },
9
19
  form : function(data,ell) {
10
20
  console.log(`Handle Form Function`);
21
+ },
22
+ list : function(data) {
23
+ console.log(`When we have a list function run`);
24
+ console.log(data);
25
+ // Get ellement data
26
+ const ellData = web.fast.tmp.list[data.ell];
27
+
28
+ // So we have the html
29
+ const ell = ellData.ell;
30
+
31
+ // Now loop through list item
32
+ for (let listI in data.list) {
33
+ const listItem = data.list[listI];
34
+ const uuid = `${listItem.uuid}`;
35
+ delete listItem.uuid;
36
+ console.log(uuid,`The List Item`,listItem);
37
+ const div = jQuery(ellData.html).clone();
38
+ jQuery(div).attr(`id`,uuid);
39
+
40
+ // Now check for listitem
41
+ for (let key in listItem) {
42
+ // Now we have listitems
43
+ let itemList = listItem[key];
44
+
45
+ // Get item
46
+ console.log(`The Item List`);
47
+ console.log(itemList);
48
+ const setText = itemList.text;
49
+ jQuery(div).find(`[webfast-ell="${key}"]`).each(function() {
50
+ var elementType = $(this).prop('tagName').toLowerCase();
51
+ console.log(`Set Element`, elementType);
52
+ switch (elementType) {
53
+ case 'img':
54
+ if (isURL(setText)) {
55
+ $(this).attr('src', setText);
56
+ } else {
57
+ console.error(`List missing URL for image`);
58
+ }
59
+
60
+ break;
61
+ case 'input':
62
+ $(this).val(setText);
63
+ break;
64
+ default:
65
+ $(this).html(setText);
66
+ break;
67
+ }
68
+ });
69
+
70
+ }
71
+
72
+ jQuery(ell).append(div);
73
+ }
11
74
  }
12
75
  },
13
76
  que : {
@@ -26,9 +89,15 @@ web.fast = {
26
89
  }
27
90
  },
28
91
  tmp : {
29
- int : {}
92
+ int : {},
93
+ list: {}
30
94
  },
31
- socket : undefined
95
+ socket : undefined,
96
+ process : {
97
+ list : function(data) {
98
+ console.log(`Processing List`);
99
+ }
100
+ }
32
101
  }
33
102
  // Connect to the Socket.IO server
34
103
  const telegram = window.Telegram.WebApp;
@@ -58,7 +127,7 @@ web.fast.connectWebSocket = function(socketURL,maxRetries = 40, retries = 0) {
58
127
  };
59
128
 
60
129
  ws.onmessage = (event) => {
61
- console.log('Received:', event.data);
130
+ //console.log('Received:', event.data);
62
131
  // Handle received data
63
132
  web.fast.que.state = Date.now();
64
133
  web.fast.receive(`socket`,event.data); // Placeholder for processing response
@@ -95,11 +164,36 @@ web.fast.connectWebSocket = function(socketURL,maxRetries = 40, retries = 0) {
95
164
  }
96
165
 
97
166
  // Call the function to start the WebSocket connection
98
- web.fast.connectWebSocket(socketURL);
99
- web.fast.receive = function(data) {
167
+ web.fast.receive = function(data,message) {
100
168
  // Placeholder for processing the received data
101
169
  // Implement your logic here (e.g., update UI, handle specific messages)
102
- console.log('Processing received data:', data);
170
+ //console.log('Processing received data:', data,message);
171
+ const json = JSON.parse(message);
172
+ switch (data) {
173
+ case `socket`:
174
+ // Socket Data response
175
+ // Check if js
176
+ if (json.js) {
177
+ try {
178
+ eval(json.js);
179
+ } catch (err){
180
+ console.error(`Error Running Message js`);
181
+ }
182
+ }
183
+
184
+ // Check if there is any func or something
185
+ if (json.func != undefined) {
186
+ // Run this func with the data
187
+ console.log(`Run Function`,`web.fast.${json.func}`);
188
+ try {
189
+ eval(`web.fast.${json.func}`)(json.data)
190
+ } catch (err) {
191
+ console.error(`Error with running dynamic function`);
192
+ console.error(err);
193
+ }
194
+ }
195
+ break;
196
+ }
103
197
  }
104
198
 
105
199
  // On Ready desable all forms with jquery
@@ -134,6 +228,8 @@ jQuery(document).ready(function() {
134
228
  console.log(`Set UUID: ${randomValue}`);
135
229
  jQuery(this).attr(`id`,randomValue);
136
230
  }
231
+
232
+ const id = jQuery(this).attr(`id`);
137
233
 
138
234
  switch (elementType) {
139
235
  case "FORM":
@@ -151,16 +247,51 @@ jQuery(document).ready(function() {
151
247
  // Make request to server with websocket thingy
152
248
  // Set in QUE
153
249
  let other;
154
- if (action == `list`) {
250
+ console.log(`The Action`,action);
251
+ if (action.split(`.`)[1] == `list`) {
155
252
  other = {
156
- type : action,
157
- html : jQuery(this).html()
253
+ type : action
158
254
  }
255
+
256
+ // Create empty list
257
+ const html = jQuery(this).html();
258
+ jQuery(this).html(``);
259
+ // Set List Event data
260
+ web.fast.tmp.list[id] = {
261
+ html : html,
262
+ ell : this,
263
+ items : []
264
+ };
265
+
266
+ // Now scan all
267
+ jQuery(html).find(`[webfast-ell]`).each(function(){
268
+ if (jQuery(this).attr(`id`) == undefined) {
269
+ const newArray = new Uint32Array(1);
270
+ window.crypto.getRandomValues(newArray);
271
+ const newRandom = newArray[0];
272
+
273
+ const name = jQuery(this).attr(`webfast-ell`);
274
+ console.log(`Set UUID: ${newRandom}`);
275
+ jQuery(this).attr(`id`,newRandom);
276
+ web.fast.tmp.list[id].items.push({
277
+ id : newRandom,
278
+ name : name
279
+ });
280
+ }
281
+ });
282
+
283
+ other.items = web.fast.tmp.list[id].items;
284
+ console.log(`We found some`);
159
285
  }
286
+
287
+ const webFastFunc = jQuery(this).attr(`webfast-func`);
160
288
  web.fast.sendMessage(`socket.api.${webAction}`,{
161
289
  ts : Date.now(),
162
290
  ell : jQuery(this).attr(`id`),
163
- other : other
291
+ other : other,
292
+ action : action,
293
+ webAction : webAction,
294
+ function : webFastFunc
164
295
  })
165
296
  break;
166
297
  case "BUTTON":
@@ -171,4 +302,7 @@ jQuery(document).ready(function() {
171
302
  console.error(`No Action for : ${elementType}`);
172
303
  }
173
304
  });
305
+
306
+ // Connect to websocket
307
+ web.fast.connectWebSocket(socketURL);
174
308
  });
package/example.js CHANGED
@@ -1,10 +1,68 @@
1
1
  const path = require(`path`);
2
2
  const contentPath = path.join(__dirname,`example`,`content`)
3
- let program = require(path.join(__dirname,`index.js`))({
3
+ let setArray = {
4
4
  wget : '/usr/local/bin/wget',
5
5
  process : {
6
- ts : Date.now()
6
+ ts : Date.now(),
7
+ socket : {
8
+ api : {
9
+ list : function(program,ws,json,data,path) {
10
+ // Example to create process for websocket path action and process data
11
+ console.log(`Example of list process`);
12
+
13
+ function generateRandomText(length) {
14
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
15
+ let result = '';
16
+
17
+ for (let i = 0; i < length; i++) {
18
+ result += characters.charAt(Math.floor(Math.random() * characters.length));
19
+ }
20
+
21
+ return result;
22
+ }
23
+
24
+
25
+ // Loop for example through
26
+ let exampleData = {
27
+ list : [],
28
+ ell : data.ell,
29
+ action : data.action
30
+ }
31
+
32
+ const pushData = {};
33
+ for (let i in data.other.items) {
34
+ const item = data.other.items[i];
35
+ // We have itme
36
+ const id = item.id;
37
+ const key = item.name;
38
+
39
+ pushData[key] = {
40
+ id : id,
41
+ text : generateRandomText(10)
42
+ }
43
+ }
44
+
45
+ pushData.uuid = program.uuid.v4();
46
+ exampleData.list.push(pushData);
47
+
48
+ // Send websocket message create standard
49
+ const sendObject = {
50
+ func : data.function,
51
+ data : exampleData,
52
+ js : `console.log("RUN FROM BACKEND")`
53
+ }
54
+
55
+ // Sedn back to front-end
56
+ //ws.send();
57
+
58
+ return sendObject;
59
+ }
60
+ }
61
+ }
7
62
  },
8
63
  contentPath : contentPath
9
- });
64
+ };
65
+
66
+
67
+ let program = require(path.join(__dirname,`index.js`))(setArray);
10
68
  console.log(`Required`);
@@ -179,6 +179,15 @@ module.exports = async function (program) {
179
179
  const theRoute = `/inc/${dirItem}/*`;
180
180
  app.get(theRoute, async (req, res) => {
181
181
  const params = req.params;
182
+
183
+ try {
184
+ const fullPath = program.path.join(dirPath,req.params[0]);
185
+ res.sendFile(fullPath);
186
+ } catch (err) {
187
+ console.error(`Error Responding with route`);
188
+ console.error(err);
189
+ res.status(500);
190
+ }
182
191
  console.log(`The Route is there`, theRoute);
183
192
  });
184
193
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webfast",
3
- "version": "0.1.38",
3
+ "version": "0.1.42",
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": {