This repository was archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
386 lines (349 loc) · 13.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
const app = require('express')();
const express = require("express");
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const fs = require("fs");
const { isObject, assign } = require('lodash');
const ovh = true
// Local vars
const foldir = process.cwd() + "/public";
let helperAtStartup = true
//initialse local folder
if (!fs.existsSync("./trees")){
fs.mkdirSync("./trees");
}
// Master keywords
let masterKeywords = { //includes
createObjectK: 'n', // new
addToObjectK: 'a', // add
setActiveObjectK: 's', //set
listObjectsK: 'l', //list
navigateObjectK: 'ss', //set subkey as active
getTreeStructureK: 'what', //
delTreeK: 'd',
showTreeK: 'where',
}
let i = 0;
let masterKeywordsHelper = {
createObjectK: ['create an object: \''+ masterKeywords.createObjectK + ' objname\''], // new
addToObjectK: ['add property to existing object: \''+ masterKeywords.addToObjectK+ '\''],
setActiveObjectK: ['set active object: \''+ masterKeywords.setActiveObjectK + ' objname\''], //set
listObjectsK: ['list object in tree folder: \''+ masterKeywords.listObjectsK + '\''], //list
navigateObjectK: ['set sub key as active: \''+ masterKeywords.navigateObjectK + '\''], //list
getTreeStructureK: ['print tree \''+ masterKeywords.getTreeStructureK + '\''], // what
delTreeK: ['delete active Tree/Branch: \''+ masterKeywords.delTreeK + '\''], //del
showTreeK: ['show active path: \''+ masterKeywords.showTreeK + '\''], //del
}
// Data structure
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// New toString proto method for arrays (: instead of ,)
Array.prototype.str = function() {
b = ""
for (a of this) {
if(this.indexOf(a) != this.length -1) {
b += a + ": "}
else {
b += a }
}
return b
}
// Method to print all object and subobjects to the webbrowser
function show(obj) {
for (o in obj) {
io.emit('channel2', [o, obj[o]].str() )
if (isObject( obj[o])) {
show(obj[o])
}
}
}
// Methods to print arrays to the webbrowser
function showarr(obj, type = 1) {
for (o in obj) {
if ( typeof(obj[o]) != 'function' ) {
if(type == 1) {
io.emit('channel1', obj[o].substr(0, obj[o].length -5) )
}
if(type == 2) {
io.emit('channel1', obj[o] )
}
}
}
}
// Method to read Tree architecture of Object
getTreeStructure = (o,s) => {
console.log(s)
!o | [o]==o || Object.keys(o).map(
k => getTreeStructure(o[k],
k=s
?s+['[\''+k+ '\']' ]
:'[\'' + k + '\']',
io.emit("channel2", k) ))
i = 0 }
// Serving app
app.set("view engine", "pug");
app.use(express.static(foldir));
app.get("/", function (req, res, next) {
res.render("acte/index", {
title: "Acte"
});
next();
});
// Socket.io instance listening to user input: opening dialog
io.on('connection', (socket) => {
// helper notice
if (helperAtStartup == true) {
io.emit("channel1", "help: existing methods: ")
showarr(masterKeywordsHelper, 2)
}
// Store json
function store(arg) {
fs.writeFileSync(process.cwd() + "/trees/" + activeTree._.name + ".json", JSON.stringify(activeTree, null, 2), 'utf8')
io.emit("channel1", "object modified and saved, added key " + arg)
}
clickValues = []
socket.on('channel3', (clickVal) => {
clickValues.unshift(clickVal)
clickValues.splice(2, 1)
console.log(clickValues)
let computedCurClicked = eval("activeTree" + clickValues[0])
io.emit('channel3', computedCurClicked)
// console.log(eval("activeTree" + clickValues[0]))
})
socket.on('channel4', (submittedVal) => {
if (i >= 1) {
// do nothing (prevent socket mltiple rquest)
}
else {
console.log(i)
let submittedValStr = JSON.stringify(submittedVal)
eval("activeTree" + clickValues[0] + " = " + submittedValStr)
// console.log(submittedVal)
store(submittedValStr)
socket.emit('channel4', 'disconnect');
i+=1
}
})
socket.on('channel1', (msg) => {
// printing self to the browser
io.emit('channel1', msg);
// secret methods
if (msg === "chebka") {
io.emit('channel1', "tan tan tan!")
}
// Core methods
try {
////// Retrieve arguments given at each request and store it in arg variable
let arg = []
// Split arguments into words
for (let i = 0; i < msg.split(" ").length; i++) {
arg[i] = msg.split(" ")[i]
if (arg[i] == '') {arg.splice(i)}
// Remove all non alphanumerical characters from user input and throw error if any
if (/\W/g.test(arg[i]) == true ) {
io.emit("channel1", "Error Type Nikoumouk")
throw("Nikoumouk")
}
}
////// Data Structure
const dataStruct = {
'_': {
'name': arg[1],
'value': "",
'id': uuidv4(),
'type': "object",
'rel' : [],
createdTime: new Date()
}
}
const subdataStruct = {
'_': {
'name': arg[1],
'value': "",
'type': "object",
'rel': [],
modifiedDate: new Date()
}
}
////// Create new obj using keyword routine
if(arg[0] == masterKeywords.createObjectK && arg.length === 2 ) {
// Prevent create object method overwriting existing objects
fs.access(process.cwd() + "\\trees\\" + arg[1] + ".json", fs.constants.F_OK, (e) => {
try{
if (e == null) {
console.log("you ain\'t god")
io.emit('channel1', "new is new, use \'" + masterKeywords.addToObjectK + "\' special keyword to modify existing object")
throw("Can't create an existing object.")
}
else {
////// Actual create object method
var tree = dataStruct
// Print obj to webbrowser
getTreeStructure(tree)
// Write local json file
fs.writeFileSync(process.cwd() + "/trees/" + tree._.name + ".json", JSON.stringify(tree, null, 2), 'utf8')
}
} catch (err) {console.error(err)}
})
}
////// Prevent general misuse of masterkeywords
if(arg[0] != masterKeywords.listObjectsK && arg.length === 1
&& arg[0] != masterKeywords.getTreeStructureK
&& arg[0] != masterKeywords.showTreeK
&& arg[0] != masterKeywords.delTreeK) {
if (Object.values(masterKeywords).includes(arg[0]) == true && arg.length == 1
|| Object.values(masterKeywords).includes(arg[0]) == true && arg.length >= 3
|| Object.values(masterKeywords).includes(arg[0]) == true && arg.length >= 3) {
io.emit('channel1', "invalid syntax: " + arg[0] + " method needs 1 arg")
console.error("invalid syntax: " + arg[0] + " method needs arg")
}
}
////// master method: add property to existing object // subbranch level
if(arg[0] == masterKeywords.addToObjectK && arg.length === 2 ) {
try {
if (typeof activeTree != undefined) {
if (treePath.length == 0) {
activeTree[arg[1]] = subdataStruct
store(arg[1])
} else {
dataStructStr = JSON.stringify(dataStruct, null, 2)
console.log(eval("activeTree" + branchage + "['" + arg[1] + "'] = " + dataStructStr))
eval("activeTree" + branchage + "['" + arg[1] + "'] = " + dataStructStr)
store(arg[1])
}
}
} catch(e) {
if (e.name == "ReferenceError") {
if (e.message == "activeTree is not defined")
io.emit("channel1", "can't modify as no active obj is set")
}
else {
console.log(e)}
}
if (typeof activeBranch === 'undefined' && typeof activeTree === 'undefined') {
console.log("no branch no chocolate")
}
}
////// Master method: list existing objects
if(arg[0] == masterKeywords.listObjectsK && arg.length === 1 ) {
var files = fs.readdirSync(process.cwd() + "/trees")
showarr(files)
if (files.length == 0) {
io.emit("channel1", "no object yet, try creating one using \'" + masterKeywords.createObjectK + "\' master keyword")
}
console.log(files)
}
////// Master method: print tree architecture
if(arg[0] == masterKeywords.getTreeStructureK && arg.length === 1 && typeof activeTree != 'undefined') {
getTreeStructure(activeTree)
}
////// Master method: print current folder
if(arg[0] == masterKeywords.showTreeK && arg.length === 1 && typeof activeTree != 'undefined') {
io.emit("channel1", "current is:" + activeTree._.name + branchage )
}
if(arg[0] == masterKeywords.showTreeK && arg.length === 1 && typeof activeTree == 'undefined') {
io.emit("channel1", "nowhere" )
}
////// Delete method
if(arg[0] == masterKeywords.delTreeK && arg.length === 1 ) {
if(typeof activeTree == 'undefined') {
console.error("set active object first")
io.emit("channel1", "set active object obj before delete attempt")
}
else {
if (treePath.length == 0) {
try {
fs.unlinkSync(process.cwd() + "/trees/" + activeTree._.name + ".json");
io.emit("channel1", "current Tree " + activeTree._.name + " deleted")
} catch(e) {console.error(e)}
}
else if (treePath.length != 0) {
if (typeof eval("activeTree" + branchage) != 'undefined') {
// delete activeTree[eval(branchage)]
console.log(treePath)
if (treePath.length == 1) {
delete activeTree[treePath[0]]
}
if (treePath.length == 2) {
delete activeTree[treePath[0]][treePath[1]]
}
if (treePath.length == 3) {
delete activeTree[treePath[0]][treePath[1]][treePath[2]]
}
if (treePath.length == 4) {
delete activeTree[treePath[0]][treePath[1]][treePath[2]][treePath[3]]
}
if (treePath.length == 5) {
delete activeTree[treePath[0]][treePath[1]][treePath[2]][treePath[3]][treePath[4]]
}
if (treePath.length == 6) {
delete activeTree[treePath[0]][treePath[1]][treePath[2]][treePath[3]][treePath[4]][treePath[5]]
}
if (treePath.length == 7) {
delete activeTree[treePath[0]][treePath[1]][treePath[2]][treePath[3]][treePath[4]][treePath[5]][treePath[6]]
}
io.emit("channel1", "active branch " + branchage + " removed from Tree " + activeTree._.name )
store()
}
}
}
}
////// Master method: set active object // once method
if(arg[0] == masterKeywords.setActiveObjectK && arg.length === 2 ) {
fs.access(process.cwd() + "/trees/" + arg[1] + ".json", fs.constants.F_OK, (e) => {
try {
if(e == null) {
activeTree = JSON.parse(fs.readFileSync(process.cwd() + "\\trees\\" + arg[1] + ".json"))
treePath = []
branchage = ""
io.emit("channel1", "object " + arg[1] + " is now active")
getTreeStructure(activeTree)
console.log(arg[1], "object loaded")
}
// Prevent set method to activate non-existing object
else {
io.emit("channel1", "Object 404")
throw("Invalid Tree name")
}
} catch(err) {console.error(err)}
})
}
////// Set subobject as active branch // ss method
if(arg[0] == masterKeywords.navigateObjectK && arg.length === 2 ) {
if (typeof activeTree === 'undefined') {
io.emit("channel1", "Can\'t navigate when object is not set")
console.error("Can\'t navigate when object is not set")
}
if (true) { //obj.nom //obj.nom.nom
branchageOriginal = branchage
branchage += "['" +arg[1]+ "']"
if (typeof eval("activeTree" + branchage) == 'undefined') {
let arglen = arg[1].length + 4
branchage = branchage.substr(0, branchage.length - arglen)
io.emit("channel1", "key doesnt exists")
// throw("Cant open non existing subkey")
}
if (typeof eval("activeTree" + branchage) != 'undefined') {
if(branchageOriginal != branchage) {
io.emit("channel1", "Active branch is: "+ activeTree._.name + branchage)
treePath.push(arg[1])
getTreeStructure(activeTree)
//console.log(treePath)
}
}
}
}
/// Catch method for initial try:
} catch(e) {console.error(e)}
});
});
if (ovh) {
server.listen(9171);
}
else {
server.listen(80);
}