webfast 0.1.38 → 0.1.41

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.
@@ -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
@@ -96,10 +165,36 @@ web.fast.connectWebSocket = function(socketURL,maxRetries = 40, retries = 0) {
96
165
 
97
166
  // Call the function to start the WebSocket connection
98
167
  web.fast.connectWebSocket(socketURL);
99
- web.fast.receive = function(data) {
168
+ web.fast.receive = function(data,message) {
100
169
  // Placeholder for processing the received data
101
170
  // Implement your logic here (e.g., update UI, handle specific messages)
102
- console.log('Processing received data:', data);
171
+ //console.log('Processing received data:', data,message);
172
+ const json = JSON.parse(message);
173
+ switch (data) {
174
+ case `socket`:
175
+ // Socket Data response
176
+ // Check if js
177
+ if (json.js) {
178
+ try {
179
+ eval(json.js);
180
+ } catch (err){
181
+ console.error(`Error Running Message js`);
182
+ }
183
+ }
184
+
185
+ // Check if there is any func or something
186
+ if (json.func != undefined) {
187
+ // Run this func with the data
188
+ console.log(`Run Function`,`web.fast.${json.func}`);
189
+ try {
190
+ eval(`web.fast.${json.func}`)(json.data)
191
+ } catch (err) {
192
+ console.error(`Error with running dynamic function`);
193
+ console.error(err);
194
+ }
195
+ }
196
+ break;
197
+ }
103
198
  }
104
199
 
105
200
  // On Ready desable all forms with jquery
@@ -134,6 +229,8 @@ jQuery(document).ready(function() {
134
229
  console.log(`Set UUID: ${randomValue}`);
135
230
  jQuery(this).attr(`id`,randomValue);
136
231
  }
232
+
233
+ const id = jQuery(this).attr(`id`);
137
234
 
138
235
  switch (elementType) {
139
236
  case "FORM":
@@ -151,16 +248,51 @@ jQuery(document).ready(function() {
151
248
  // Make request to server with websocket thingy
152
249
  // Set in QUE
153
250
  let other;
154
- if (action == `list`) {
251
+ console.log(`The Action`,action);
252
+ if (action.split(`.`)[1] == `list`) {
155
253
  other = {
156
- type : action,
157
- html : jQuery(this).html()
254
+ type : action
158
255
  }
256
+
257
+ // Create empty list
258
+ const html = jQuery(this).html();
259
+ jQuery(this).html(``);
260
+ // Set List Event data
261
+ web.fast.tmp.list[id] = {
262
+ html : html,
263
+ ell : this,
264
+ items : []
265
+ };
266
+
267
+ // Now scan all
268
+ jQuery(html).find(`[webfast-ell]`).each(function(){
269
+ if (jQuery(this).attr(`id`) == undefined) {
270
+ const newArray = new Uint32Array(1);
271
+ window.crypto.getRandomValues(newArray);
272
+ const newRandom = newArray[0];
273
+
274
+ const name = jQuery(this).attr(`webfast-ell`);
275
+ console.log(`Set UUID: ${newRandom}`);
276
+ jQuery(this).attr(`id`,newRandom);
277
+ web.fast.tmp.list[id].items.push({
278
+ id : newRandom,
279
+ name : name
280
+ });
281
+ }
282
+ });
283
+
284
+ other.items = web.fast.tmp.list[id].items;
285
+ console.log(`We found some`);
159
286
  }
287
+
288
+ const webFastFunc = jQuery(this).attr(`webfast-func`);
160
289
  web.fast.sendMessage(`socket.api.${webAction}`,{
161
290
  ts : Date.now(),
162
291
  ell : jQuery(this).attr(`id`),
163
- other : other
292
+ other : other,
293
+ action : action,
294
+ webAction : webAction,
295
+ function : webFastFunc
164
296
  })
165
297
  break;
166
298
  case "BUTTON":
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.41",
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": {