-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (201 loc) · 7.07 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
const fs = require('fs')
const path = require('path')
let sync = true
let folder = path.join(process.cwd(), '.DeBSON')
if (!fs.existsSync(folder)) fs.mkdirSync(folder)
const handlers = []
class DeBSONObject {
constructor (pth, obj) {
this.pth = pth
this.obj = obj
}
_readCat () {
if (sync) {
return JSON.parse(`${fs.readFileSync(this.pth)}`)
} else {
let p = this
return new Promise((resolve) => {
fs.readFile(p.pth, (err, data) => {
if (err) throw err
return resolve(JSON.parse(`${data}`))
})
})
}
}
/**
* It returns a promise that resolves to the value of the object property `this.obj` of the object
* returned by the promise returned by the function `this._readCat()`.
*
* The function `this._readCat()` returns a promise that resolves to an object. The function
* `this.read()` returns a promise that resolves to the value of the object property `this.obj` of
* the object returned by the promise returned by the function `this._readCat()`.
*
* The function `this.read()` returns a promise that resolves to the value of the object property
* `this.obj` of the object returned by the promise returned by the function `this._readCat()`.
*
* The function `this.read()` returns a promise that resolves to the value of the object property
* `this.obj` of the object returned by the promise returned by the function `this._readCat()`.
* @returns The value of the property of the object.
*/
read () {
if (sync) {
return this._readCat()[this.obj]
} else {
p = this
return new Promise(resolve => {
// deepcode ignore PromiseNotCaughtNode: <please specify a reason of ignoring this>
p._readCat().then(r => {
return resolve(r[p.obj])
})
})
}
}
/**
* It reads a file, modifies the content, and writes it back to the file.
* @param content - The content to write to the file.
* @returns A promise.
*/
write (content) {
if (sync) {
let c = this._readCat()
if (c[this.obj] !== content) {
for (const [_pth, _obj, handler] of handlers) {
if (_pth == this.pth && _obj == this.obj) handler(content)
}
}
c[this.obj] = content
fs.writeFileSync(this.pth, JSON.stringify(c))
} else {
let p = this
return new Promise((resolve) => {
// deepcode ignore PromiseNotCaughtNode: <please specify a reason of ignoring this>
p._readCat().then(r => {
if (r[p.obj] !== content) {
for (const [_pth, _obj, handler] of handlers) {
if (_pth == p.pth && _obj == p.obj) handler(content)
}
}
r[p.obj] = content
fs.writeFile(this.pth, JSON.stringify(r), 'utf8', () => {
resolve()
})
})
})
}
}
watch (cb) {
handlers.push([this.pth, this.obj, cb])
}
/**
* It deletes the value of the variable.
*/
delete () {
this.write(undefined)
}
/**
* It deletes the value of the variable.
*/
del() {
this.write(undefined)
}
}
const DeBSON = {
env: {},
/**
* It takes a string as an argument and sets the value of the sync variable to true if the string
* is 'sync' and false if the string is 'async'.
* @param mode - This is the mode you want to set the function to. It can be either 'sync' or
* 'async'.
*/
mode (mode) {
if (!mode) throw new Error('No mode specified')
if (mode === 'sync') {
sync = true
} else if (mode === 'async') {
sync = false
} else {
throw new Error('Please specify a valid mode')
}
},
folder (folder) {
if (!folder) throw new Error('No folder specified')
if (!fs.existsSync(folder)) fs.mkdirSync(folder)
},
/* Creating or choosing a category. */
category (categoryName) {
if (typeof categoryName !== 'string' || categoryName === '') throw new Error('Please specify a valid category name')
const filepath = path.join(folder, categoryName + '.deb.json')
if (sync) {
if (!fs.existsSync(filepath)) fs.writeFileSync(filepath, '{}')
DeBSON.env = {pth: filepath, inCat: true}
return DeBSON
} else {
return new Promise((resolve) => {
fs.exists(filepath, (exists => {
if (!exists) {
fs.writeFile(filepath, '{}', (err) => {
if (err) throw err
DeBSON.env = {pth: filepath, inCat: true}
return resolve(DeBSON)
})
}
}))
})
}
},
/**
* It returns a new DeBSONObject, which is a class that has methods to get and set values in the
* database
* @param objName - The name of the object you want to create.
* @returns A new DeBSONObject.
*/
obj (objName) {
if (!DeBSON.env.inCat) throw Error('Please choose a category first')
objName = `${objName}`
if (objName === '') objName = 'EMPTY_OBJ_NAME!76537'
return new DeBSONObject(DeBSON.env.pth, objName)
},
/**
* Binds a socket (from socket.io) for remote connection
*/
bind (socket) {
let p = this
socket.on('@deb-test-connection', (cb) => {
cb()
})
socket.on('@deb-exec-cmd', (payload, cb) => {
let success = true
let data = null
let err = null
try {
const category = p.category(payload.category)
const object = category.obj(payload.object)
if (payload.cmd === 'write') {
object.write(payload.data)
} else if (payload.cmd === 'read') {
data = object.read()
} else if (payload.cmd === 'delete') {
object.delete()
} else {
throw new Error('Invalid Command')
}
} catch (e) {
success = false
console.error(e)
err = e
}
cb(success, data, err)
})
socket.on('@deb-exec-watch-cmd', (payload, cb) => {
const category = p.category(payload.category)
const object = category.obj(payload.object)
object.watch((val) => {
socket.emit('@deb-wtrigger-' + payload.wid, val)
})
cb(true, null, null)
})
}
}
DeBSON.cat = DeBSON.category
DeBSON.object = DeBSON.obj
module.exports = DeBSON