-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonic-fs.js
418 lines (387 loc) · 12.5 KB
/
jsonic-fs.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
let stoTag = "ljsoned::"
const util = require('util');
function subToObj(text) {
let numOf = Number(text)
if(!isNaN(numOf))return [];
if(text === "..") throw new Error("(forcefull): cannot construct parent word");
if(text === "." || text === "") throw new Error("(forcefull): cannot construct \".\" ");
return {};
}
function subType(v) { //if is constructable childLike value, return constructor
if(v === null || v === undefined)return null;
switch (v.constructor) {
case Array:
case Object:
return v.constructor
break;
default:
return null;
}
}
let secureReturnToken = null
function getPrivateOf(public,callerName){
secureReturnToken = null
if(typeof public !== "object")throw new Error((callerName || "getPrivateOf")+": expected (public face of) node, got "+typeof public);
public.__getPrivate()
if(secureReturnToken == null || secureReturnToken.public !== public)
throw new Error((callerName || "getPrivateOf")+": expected (public face of) node");
return secureReturnToken
}
class FsNode {
//pls confgure me//
fileToArray(arr){
return arr
}
arrayToFile(p){
return p.join("/")
}
createArbitraryFn(args,body,base){ //makes arbitrary functions from string, basically a wrapper for Function()
return Function(args,body).bind(base)
}
//actual code//
constructor(root,parent,parentName) {
this.root = this.root || this
this.parent = parent
this.parentId = parentName
this.clear()
this.setupPublic()
}
[util.inspect.custom](dep,opts){
return `[FsNode private ${
this.storage ? "storage: "+util.inspect(this.storage ,opts)+" " : ""
}${this.children ? "children: "+util.inspect(this.children,opts)+" " : ""
}${this.value ? "value: "+util.inspect(this.value ,opts)+" " : ""
}]`
}
setupPublic(){
let privateFace = this
let publicFace;
publicFace = {
__getPrivate(){
secureReturnToken = privateFace
},
get parent(){
return privateFace.parent.public
},
set parent(name){
//Obj(Obj(parent = Obj(...)))
let privateDest = privateFace.linkish
if (name) {
privateDest.moveTo(getPrivateOf(name,"(node.parent = node)"))
} else {
privateDest.remove()
}
},
get value(){
//if(privateFace.children)return this.subGet("value");
let privateDest = privateFace.linkish
return privateDest.children ? privateDest.save() : privateDest.value;
},
get preview(){
let privateDest = privateFace.linkish
return privateDest.children ? privateDest.save(true) : privateDest.value;
},
set value(v){
//if(privateFace.children)return this.subSet("value",v);
let privateDest = privateFace.linkish
if(v === null) privateDest.clear();
else privateDest.load(v,true);
},
set valueWithTrust(v){//DEPRECATED replased with trustedValue
let privateDest = privateFace.linkish
if(v === null) privateDest.clear(); //technically redundant
else privateDest.load(v,false);
},
set trustedValue(v){//disables trust pretections for user input data
let privateDest = privateFace.linkish
if(v === null) privateDest.clear(); //technically redundant
else privateDest.load(v,false);
},
set link(v){
//TODO finish
},
sub(name){
let privateDest = privateFace.linkish
let l = privateDest.sub(name)
return l ? l.public : null
},
go(where,assert){
let privateDest = privateFace.linkish
try{
let l = privateDest.go(where,assert)
return l ? l.public : null
}
catch(e){
console.log("inside go attempt,",where);
throw e
}
},
setSub(name,value){
let privateDest = privateFace.linkish
let l = privateDest.sub(name)
if(l)l.load(value,true);
else privateDest.makeSub(name).load(value,true);
},
getSub(name){
let privateDest = privateFace.linkish
let l = privateDest.sub(name)
return l ? l.value : null
},
get path(){
//let privateDest = privateFace.linkish
return privateFace.path
},
get list(){
let privateDest = privateFace.linkish
return privateDest.list.map(f=>f.public)
},
get storage(){
let privateDest = privateFace.linkish
return privateDest.storage
},
get name(){
return privateFace.parentId
},
get exec(){
if(!privateFace.hasValue)privateFace.load({});
if(!privateFace.storage)throw new Error("only objects can contain code");
if(privateFace.functionCache)
return privateFace.functionCache;
let func = publicFace.storage["function"] || ""
let args = publicFace.storage["functionArgs"] || "...args"
func = privateFace.createArbitraryFn(args,func,publicFace)
privateFace.functionCache = func;
return func;
},
set exec(v){
var entire = v.toString(); // this part may fail!
var body = entire,args = "...args";
if(typeof v == "function")body = entire.substring(entire.indexOf("{") + 1, entire.lastIndexOf("}"));
if(typeof v == "function")args = entire.substring(entire.indexOf("(") + 1, entire.indexOf(")"));
if(!privateFace.hasValue)privateFace.load({});
if(!privateFace.storage)throw new Error("only objects can contain code");
privateFace.functionCache = privateFace.createArbitraryFn(args,body,publicFace)
publicFace.storage["function"] = body
publicFace.storage["functionArgs"] = args
}
}
publicFace[util.inspect.custom] = (dep,opts)=>{
return `[FsNode public "/${privateFace.arrayToFile(this.path)}" ${
privateFace.storage && privateFace.storage.link ? "linkedTo: "+privateFace.arrayToFile(privateFace.storage.link) : ""}]`
}
this.public = publicFace
}
get linkish(){ //is able to control the public face of the object disabled due to unsolvable bugs
//let storage = this.storage
//if(storage && storage.link) return this.go(storage.link,true,true);
return this
}
get childLike(){//copy and paste this logic and avoid calling me
return !!this.children
}
save(isPreview){
let children = this.children
let newO;
if(children){
if (this.arrayLike) {
newO = [];
children.forEach((item, i) => {
newO[i] = item.save(isPreview)
});
} else {
newO = {};
for (var key in children) {
if (children.hasOwnProperty(key)) {
newO[key] = children[key].save(isPreview);
}
}//end of for
if(!isPreview){
let storage = this.storage
for (var key in storage) {
if (storage.hasOwnProperty(key)) {
newO[stoTag+key] = storage[key]
}
}
}
}
} else {
newO = this.value
}
return newO;
}
go(where,forcefull,firstNonLinkish){
let newp = this.root.fileToArray(where)
let car = this
let i = 0
if(newp[0] == ""){
car = car.root //avoid linkish TEMP
i++
}
//console.log(firstNonLinkish,car.public,car.root.public,where);
for (; i < newp.length; i++) {
let node = newp[i];
if(node == "" || node == ".");
else if(node == "..") car = car.parent; //parent is non linkish
else {
let cat;
//if(firstNonLinkish) firstNonLinkish = false;
//else car = car.linkish; //folow link
//if(!car) throw new Error("invalid link "+this.arrayToFile(where)+" file: "+cat.path+" to: "+this.arrayToFile(cat.storage.link))
cat = car.sub(node); //this is linkish
if(!cat || !cat.hasValue){
if(forcefull){
let next = newp[i+1]
if(next === undefined) return car.makeSub(node);
cat = car.makeSub(node);
cat.load(subToObj(next));
//console.log(subToObj(node));
}
else return null;
}
car = cat
//
}
}
return car
}
get path(){
let parr = []
let car = this
while(car.parent){
parr.push(car.parentId)
car = car.parent
}
return parr.reverse()
}
load(value,untrusted){
if(this.hasValue)this.clear();
if(value === null)return;
let mker = subType(value)
this.arrayLike = value instanceof Array
if(mker){
this.children = new mker()
if (this.arrayLike) {
value.forEach((item, i) => {
let l = new this.constructor(this.root,this,i)
this.children[i] = l
l.load(item,untrusted)
});
} else {
let storage = {}
this.storage = storage
for (var key in value) {
if(key.substring(0,stoTag.length)==stoTag){
let stoK = key.substring(stoTag.length)
if(stoK == "trusted" && untrusted)
storage["trusted"] = false; //marks values as untrustworthy
else storage[stoK] = value[key];
} else if (value.hasOwnProperty(key)) {
let l = new this.constructor(this.root,this,key)
this.children[key] = l
l.load(value[key],untrusted)
}
}//end of for
}//end if this.arrayLike
} else {
this.value = value
} //end of if mker else
this.hasValue = true
//if(this.storage && this.storage.trusted && untrusted)this.storage.trusted = false; //marks values as untrustworthy
this.onLoad()
}//endof func
onLoad(){
//when an item loads data
let storage = this.storage
if(this === this.root && storage && storage.trusted && storage.onInit){
(this.createArbitraryFn("",storage.onInit,this.public))()
}
}
clear(){
if(this.children)this.clearChildren();
this.hasValue = false
this.value = null
this.children = null //childLike if trueish
this.arrayLike = false
this.storage = null //nulled when not used
}
clearChildren(){
if (this.arrayLike) {
this.children.forEach((item, i) => {
item.parent = null
this.children[i] = null
});
} else {
for (var key in this.children) {
this.children[key].parent = null
this.children[key] = null
}//end of for
}//end if this.arrayLike
}
keyCorrect(key,act){
if(!this.children)throw new Error("attempt to "+act+" on non childLike element")
if(this.arrayLike){
key = Number(key)
if(Number.isNaN(key))throw new Error("array key, expected number got text");
key = Math.floor(key)
if(key < 0) key = key % this.children.length; //if i < 0 wrap around to the end
};
return key;
}
makeSub(key){
key = this.keyCorrect(key,"make child");
let l = new this.constructor(this.root,this,key)
this.children[key] = l
return l
}
sub(key){
key = this.keyCorrect(key,"get child");
//if(key == "" || key == ".")return this; //TODO make this change stable
return this.children.hasOwnProperty(key) ? this.children[key] : null
}
subSet(key,value){
key = this.keyCorrect(key,"write child");
return this.children[key] = value
}
remove(){
if(this.parent){
this.parent.children[this.parentId] = null
this.parent = null
}
}
attach(node,name){
if(this.parent)this.remove();
name = (name !== undefined || name !== null) ? name : this.parentId
name = node.keyCorrect(name,"attach node")
if(node.children[name])throw new Error("that node already exists");
this.parent = node
node.children[name] = this
}
delete(){
this.remove()
this.clear()
}
moveTo(dest){
let origParent = this.parent
try {
this.attach(dest)
} catch (e) {
this.attach(origParent)
throw e
}
}
get list() {
let children = this.children
let newO = [];
if(children){
if (this.arrayLike) {
newO = Array.from(children)
} else {
newO = Object.values(children)
}
} else {
return []
}
return newO.filter(v=>v&&v.hasValue)
}
}
exports.FsNode = FsNode