yodata 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +128 -0
- package/data/collection.js +156 -0
- package/data/plugin.js +250 -0
- package/package.json +19 -0
- package/ws/Message.js +18 -0
- package/ws/index.js +195 -0
package/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# Y0data services #
|
2
|
+
|
3
|
+
**Please read y0data inc before using it**
|
4
|
+
**To get database id go to https://data.y0host.net/**
|
5
|
+
**Your data will save in servers y0data, so no disk will take from your server**
|
6
|
+
|
7
|
+
# install y0data
|
8
|
+
|
9
|
+
``` js
|
10
|
+
npm install y0data
|
11
|
+
```
|
12
|
+
|
13
|
+
# Connect Database
|
14
|
+
|
15
|
+
``` js
|
16
|
+
let y0data = require('y0data')
|
17
|
+
let db = new y0data('DATA BASE ID')
|
18
|
+
db.connect();
|
19
|
+
```
|
20
|
+
|
21
|
+
# events
|
22
|
+
|
23
|
+
```js
|
24
|
+
db.once('ready', async ()=>{
|
25
|
+
console.log(`DataBase Ready`)
|
26
|
+
})
|
27
|
+
|
28
|
+
db.on('error', (err)=>{
|
29
|
+
console.log(`Error: ` + err)
|
30
|
+
})
|
31
|
+
```
|
32
|
+
|
33
|
+
# setup collection
|
34
|
+
|
35
|
+
```js
|
36
|
+
let customersDB = db.setUpCollection('customers')
|
37
|
+
```
|
38
|
+
|
39
|
+
# functions
|
40
|
+
|
41
|
+
## Create
|
42
|
+
```js
|
43
|
+
let data = await customersDB.add([{"money": 51}, {"money": 9}]) // example output → '[Accepted Data]'
|
44
|
+
let data = await customersDB.add({"_id": "123", "money": 5}) // example output → '{Accepted Data} / null'
|
45
|
+
/*
|
46
|
+
For free plan 50 array max in same time
|
47
|
+
For Shared Plan 100 Array max in same time
|
48
|
+
For Server Plan 500 Array max in same time
|
49
|
+
*/
|
50
|
+
```
|
51
|
+
|
52
|
+
## find data
|
53
|
+
```js
|
54
|
+
let options = {
|
55
|
+
$gte: {money: 10}, // Get Users have money low of 10
|
56
|
+
$gt: {money: 50} // get Users have money more of 50
|
57
|
+
$in: {_id: ['123', '1234']} // get Users have in same _id
|
58
|
+
}
|
59
|
+
|
60
|
+
let data = await customersDB.find(options).exec(); // example output → 'Array'
|
61
|
+
let data = await customersDB.find(options).limit(20).exec(); // (Get Only 20 data) example output → 'Array'
|
62
|
+
let data = await customersDB.find(options).skip(20).exec(); // (Skip first 20) example output → 'Array'
|
63
|
+
let data = await customersDB.find(options).select({money: 1}).exec(); // (Will only see _id and money) example output → 'Array'
|
64
|
+
let data = await customersDB.find(options).select({money: 0}).exec(); // (Will hide money) example output → 'Array'
|
65
|
+
|
66
|
+
let data = await customersDB.findOne(options).exec(); // example output → 'Object/Null'
|
67
|
+
let data = await customersDB.findOne(options).skip(20).exec(); // (Skip first 20) example output → 'Object/Null'
|
68
|
+
let data = await customersDB.findOne(options).select({money: 1}).exec(); // (Will only see _id and money) example output → 'Object/Null'
|
69
|
+
let data = await customersDB.findOne(options).select({money: 0}).exec(); // (Will hide money) example output → 'Object/Null'
|
70
|
+
```
|
71
|
+
|
72
|
+
## Update Data
|
73
|
+
```js
|
74
|
+
let options = {
|
75
|
+
$gte: {money: 10}, // Get Users have money low of 10
|
76
|
+
$gt: {money: 50} // get Users have money more of 50
|
77
|
+
$in: {_id: ['123', '1234']} // get Users have in same _id
|
78
|
+
}
|
79
|
+
|
80
|
+
let new_values = {
|
81
|
+
$inc: {money: 50}, // Add money you can also do -50 to remove
|
82
|
+
|
83
|
+
$push: {arr: ["1"]}, // add in array items
|
84
|
+
$push: {arr: "1"}, // add in array item
|
85
|
+
|
86
|
+
$pull: {arr: ["1"]}, // delete from array items
|
87
|
+
$pull: {arr: "1"} // delete from array item
|
88
|
+
}
|
89
|
+
|
90
|
+
let data = await customersDB.updateOne(options, new_values) // example output → 'true/false'
|
91
|
+
|
92
|
+
let data = await customersDB.updateMany(options, new_values); // example output → 'true/false' (if update one only will return true always)
|
93
|
+
```
|
94
|
+
|
95
|
+
## Delete Data
|
96
|
+
```js
|
97
|
+
let options = {
|
98
|
+
$gte: {money: 10}, // Get Users have money low of 10
|
99
|
+
$gt: {money: 50} // get Users have money more of 50
|
100
|
+
$in: {_id: ['123', '1234']} // get Users have in same _id
|
101
|
+
}
|
102
|
+
|
103
|
+
let data = await customersDB.deleteOne(options) // example output → 'true/false'
|
104
|
+
|
105
|
+
let data = await customersDB.deleteMany(options); // example output → 'true/false' (if update one only will return true always)
|
106
|
+
```
|
107
|
+
|
108
|
+
## Other functions will support you
|
109
|
+
```js
|
110
|
+
let options = {
|
111
|
+
$gte: {money: 10}, // Get Users have money low of 10
|
112
|
+
$gt: {money: 50} // get Users have money more of 50
|
113
|
+
$in: {_id: ['123', '1234']} // get Users have in same _id
|
114
|
+
}
|
115
|
+
|
116
|
+
let data = await customersDB.countDocuments(options) // example output → 'true/false'
|
117
|
+
|
118
|
+
let data = await customersDB.getSize(options); // example output → 'true/false' (if update one only will return true always)
|
119
|
+
```
|
120
|
+
|
121
|
+
## Watch
|
122
|
+
```js
|
123
|
+
await cusotmersDB.watch()
|
124
|
+
|
125
|
+
db.on('watch', (data)=>{
|
126
|
+
console.log('data watch =>', data) // will get name of collection and more data
|
127
|
+
})
|
128
|
+
```
|
@@ -0,0 +1,156 @@
|
|
1
|
+
let randomToken = require('random-token')
|
2
|
+
let ws = require('../ws/index.js')
|
3
|
+
let EventEmitter = require("events");
|
4
|
+
|
5
|
+
let create = async function (thi, collectionName){
|
6
|
+
|
7
|
+
let requestID = randomToken(8)
|
8
|
+
|
9
|
+
setTimeout(()=>{ thi.project.createMessage({event: "createCollection", data:{name: collectionName}, requestID}) }, 100)
|
10
|
+
|
11
|
+
let res = (await thi.waitResponse(requestID))
|
12
|
+
|
13
|
+
if(res.event === 'rejected') return null
|
14
|
+
return true
|
15
|
+
|
16
|
+
}
|
17
|
+
|
18
|
+
let push = async function (thi, collectionName, options){
|
19
|
+
|
20
|
+
let requestID = randomToken(8)
|
21
|
+
|
22
|
+
setTimeout(()=>{ thi.project.createMessage({event: "create", data:{name: collectionName, options}, requestID}) }, 100)
|
23
|
+
|
24
|
+
let res = (await thi.waitResponse(requestID))
|
25
|
+
|
26
|
+
if(res.event === 'rejected') return null
|
27
|
+
return res.data
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
let find = async function (thi, collectionName, options, select, limit, skip){
|
32
|
+
|
33
|
+
let requestID = randomToken(8)
|
34
|
+
|
35
|
+
setTimeout(()=>{ thi.project.createMessage({event: "find", data:{name: collectionName, options, select, limit, skip}, requestID}) }, 100)
|
36
|
+
|
37
|
+
let res = (await thi.waitChunk(requestID))
|
38
|
+
|
39
|
+
if(res.event === 'rejected') return null
|
40
|
+
|
41
|
+
return res.data
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
let findOne = async function (thi, collectionName, options, select, skip){
|
46
|
+
|
47
|
+
let requestID = randomToken(8)
|
48
|
+
|
49
|
+
setTimeout(()=>{ thi.project.createMessage({event: "findOne", data:{name: collectionName, options, select, skip}, requestID}) }, 100)
|
50
|
+
|
51
|
+
let res = (await thi.waitResponse(requestID))
|
52
|
+
|
53
|
+
if(res.event === 'rejected') return null
|
54
|
+
|
55
|
+
return res.data
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
let deleteOne = async function (thi, collectionName, options){
|
60
|
+
|
61
|
+
let requestID = randomToken(8)
|
62
|
+
|
63
|
+
setTimeout(()=>{ thi.project.createMessage({event: "deleteOne", data:{name: collectionName, options}, requestID}) }, 100)
|
64
|
+
|
65
|
+
let res = (await thi.waitResponse(requestID))
|
66
|
+
|
67
|
+
if(res.event === 'rejected') return null
|
68
|
+
|
69
|
+
return res.data
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
let deleteMany = async function (thi, collectionName, options){
|
74
|
+
|
75
|
+
let requestID = randomToken(8)
|
76
|
+
|
77
|
+
setTimeout(()=>{ thi.project.createMessage({event: "deleteMany", data:{name: collectionName, options}, requestID}) }, 100)
|
78
|
+
|
79
|
+
let res = (await thi.waitResponse(requestID))
|
80
|
+
|
81
|
+
if(res.event === 'rejected') return null
|
82
|
+
|
83
|
+
return res.data
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
let updateOne = async function (thi, collectionName, options, new_values){
|
88
|
+
|
89
|
+
let requestID = randomToken(8)
|
90
|
+
|
91
|
+
setTimeout(()=>{ thi.project.createMessage({event: "updateOne", data:{name: collectionName, options, new_values}, requestID}) }, 100)
|
92
|
+
|
93
|
+
let res = (await thi.waitResponse(requestID))
|
94
|
+
|
95
|
+
if(res.event === 'rejected') return null
|
96
|
+
return res.data
|
97
|
+
|
98
|
+
}
|
99
|
+
|
100
|
+
let updateMany = async function (thi, collectionName, options, new_values){
|
101
|
+
|
102
|
+
let requestID = randomToken(8)
|
103
|
+
|
104
|
+
setTimeout(()=>{ thi.project.createMessage({event: "updateMany", data:{name: collectionName, options, new_values}, requestID}) }, 100)
|
105
|
+
|
106
|
+
let res = (await thi.waitResponse(requestID))
|
107
|
+
|
108
|
+
if(res.event === 'rejected') return null
|
109
|
+
|
110
|
+
return res.data
|
111
|
+
|
112
|
+
}
|
113
|
+
|
114
|
+
let countDocuments = async function (thi, collectionName, options){
|
115
|
+
|
116
|
+
let requestID = randomToken(8)
|
117
|
+
|
118
|
+
setTimeout(()=>{ thi.project.createMessage({event: "countDocuments", data:{name: collectionName, options}, requestID}) }, 100)
|
119
|
+
|
120
|
+
let res = (await thi.waitResponse(requestID))
|
121
|
+
|
122
|
+
if(res.event === 'rejected') return null
|
123
|
+
|
124
|
+
return res.data || 0
|
125
|
+
|
126
|
+
}
|
127
|
+
|
128
|
+
let getSize = async function (thi, collectionName, options){
|
129
|
+
|
130
|
+
let requestID = randomToken(8)
|
131
|
+
|
132
|
+
setTimeout(()=>{ thi.project.createMessage({event: "getSize", data:{name: collectionName, options}, requestID}) }, 100)
|
133
|
+
|
134
|
+
let res = (await thi.waitResponse(requestID))
|
135
|
+
|
136
|
+
if(res.event === 'rejected') return null
|
137
|
+
|
138
|
+
return res.data || 0
|
139
|
+
|
140
|
+
}
|
141
|
+
|
142
|
+
let watch = async function (thi, collectionName){
|
143
|
+
|
144
|
+
let requestID = randomToken(8)
|
145
|
+
|
146
|
+
setTimeout(()=>{ thi.project.createMessage({event: "watch", data:{name: collectionName}, requestID}) }, 100)
|
147
|
+
|
148
|
+
let res = (await thi.waitResponse(requestID))
|
149
|
+
|
150
|
+
if(res.event === 'rejected') return null
|
151
|
+
|
152
|
+
return res.data || 0
|
153
|
+
|
154
|
+
}
|
155
|
+
|
156
|
+
module.exports = {create, push, find, findOne, deleteOne, deleteMany, updateOne, updateMany, countDocuments, getSize, watch};
|
package/data/plugin.js
ADDED
@@ -0,0 +1,250 @@
|
|
1
|
+
let randomToken = require('random-token')
|
2
|
+
let ws = require('../ws/index.js')
|
3
|
+
let EventEmitter = require("events");
|
4
|
+
|
5
|
+
//console.log(manage.select([{must: true, love: true}, {must: true, love: false}], {love: 1}))
|
6
|
+
let collection = require('../data/collection.js')
|
7
|
+
|
8
|
+
class Plugin extends EventEmitter {
|
9
|
+
constructor (dataBaseID) {
|
10
|
+
super (dataBaseID)
|
11
|
+
|
12
|
+
this.dataBaseID = dataBaseID
|
13
|
+
this.project = new ws(`wss://y0data.glitch.me/?database_id=${this.dataBaseID}`)
|
14
|
+
|
15
|
+
process.plugin = this
|
16
|
+
|
17
|
+
}
|
18
|
+
|
19
|
+
connect(){
|
20
|
+
|
21
|
+
this.project.on('ready', ()=>{
|
22
|
+
this.emit('connect')
|
23
|
+
})
|
24
|
+
|
25
|
+
this.project.on('disconnect', () =>{
|
26
|
+
this.emit('disconnect')
|
27
|
+
this.project.reconnectStatus = true
|
28
|
+
})
|
29
|
+
|
30
|
+
this.project.on('message', (msg) =>{
|
31
|
+
|
32
|
+
let data = msg.json()
|
33
|
+
|
34
|
+
// console.log(data)
|
35
|
+
|
36
|
+
if(data.event === 'ready'){
|
37
|
+
this.emit('ready')
|
38
|
+
|
39
|
+
}
|
40
|
+
if(data.event === 'watchData'){
|
41
|
+
|
42
|
+
if(data.requestID) return;
|
43
|
+
|
44
|
+
this.emit('watch', data.data)
|
45
|
+
|
46
|
+
}
|
47
|
+
if(data.event === 'error'){
|
48
|
+
|
49
|
+
let errors = [{
|
50
|
+
num: 401,
|
51
|
+
message: "Failed Authorized with your database, please verify from id",
|
52
|
+
reconnect: false
|
53
|
+
}]
|
54
|
+
|
55
|
+
let err = errors.find(x => x.num === data.data)
|
56
|
+
|
57
|
+
if(!err?.reconnect){
|
58
|
+
|
59
|
+
this.project.reconnectStatus = false
|
60
|
+
|
61
|
+
}else{
|
62
|
+
|
63
|
+
this.project.reconnectStatus = true
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
this.emit('error', (err?.message || data.data))
|
68
|
+
}
|
69
|
+
|
70
|
+
})
|
71
|
+
|
72
|
+
}
|
73
|
+
|
74
|
+
async waitResponse(requestID){
|
75
|
+
return await new Promise(async re => {
|
76
|
+
|
77
|
+
let claimedMessage = (data)=>{
|
78
|
+
let msg = data.json()
|
79
|
+
|
80
|
+
if(msg.requestID === requestID){
|
81
|
+
re(msg);
|
82
|
+
this.project.removeListener('message', claimedMessage)
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
this.project.on('message', (data)=>claimedMessage(data))
|
88
|
+
|
89
|
+
})
|
90
|
+
}
|
91
|
+
|
92
|
+
async waitChunk(requestID){
|
93
|
+
return await new Promise(async re => {
|
94
|
+
|
95
|
+
let result = '';
|
96
|
+
|
97
|
+
let claimedMessage = (data)=>{
|
98
|
+
let msg = data.json()
|
99
|
+
|
100
|
+
if(msg.requestID === requestID && msg.event === 'chunk'){
|
101
|
+
// re(msg);
|
102
|
+
// this.project.removeListener('message', claimedMessage)
|
103
|
+
|
104
|
+
result += msg.data
|
105
|
+
|
106
|
+
}
|
107
|
+
|
108
|
+
if(msg.requestID === requestID && msg.event === 'finish'){
|
109
|
+
|
110
|
+
result += msg.data
|
111
|
+
|
112
|
+
re({event: "accepted", data: JSON.parse(result)})
|
113
|
+
result = ''
|
114
|
+
this.project.removeListener('message', claimedMessage)
|
115
|
+
|
116
|
+
}
|
117
|
+
|
118
|
+
}
|
119
|
+
|
120
|
+
|
121
|
+
this.project.on('message', (data)=>claimedMessage(data))
|
122
|
+
|
123
|
+
})
|
124
|
+
}
|
125
|
+
|
126
|
+
setUpCollection(collectionName){
|
127
|
+
//, withData, select
|
128
|
+
let requestID = randomToken(8)
|
129
|
+
|
130
|
+
//setTimeout(()=>{ this.project.createMessage({event: "findCollection", data:{name: collectionName, withData: withData, select: select}, requestID}) }, 100)
|
131
|
+
|
132
|
+
//var response = await this.waitResponse(requestID)
|
133
|
+
|
134
|
+
//let res = Array.isArray(response?.data) ? response?.data : [] || []
|
135
|
+
|
136
|
+
let res = []
|
137
|
+
|
138
|
+
res.add = async (options)=>{
|
139
|
+
return await collection.push(this, collectionName, options)
|
140
|
+
}
|
141
|
+
|
142
|
+
res.find = (options)=>{
|
143
|
+
|
144
|
+
let query = {}
|
145
|
+
|
146
|
+
var useSelect = null
|
147
|
+
let useLimit = null
|
148
|
+
let useSkip = null
|
149
|
+
|
150
|
+
query.select = (select)=>{
|
151
|
+
useSelect = select
|
152
|
+
return query
|
153
|
+
}
|
154
|
+
|
155
|
+
query.limit = (num)=>{
|
156
|
+
useLimit = num
|
157
|
+
return query
|
158
|
+
}
|
159
|
+
|
160
|
+
query.skip = (num)=>{
|
161
|
+
useSkip = num
|
162
|
+
return query
|
163
|
+
}
|
164
|
+
|
165
|
+
query.exec = ()=>{
|
166
|
+
return collection.find(this, collectionName, options, useSelect, useLimit, useSkip)
|
167
|
+
}
|
168
|
+
|
169
|
+
return query
|
170
|
+
}
|
171
|
+
|
172
|
+
res.findOne = (options, select)=>{
|
173
|
+
|
174
|
+
let query = {}
|
175
|
+
|
176
|
+
var useSelect = null
|
177
|
+
let useLimit = null
|
178
|
+
let useSkip = null
|
179
|
+
|
180
|
+
query.select = (select)=>{
|
181
|
+
useSelect = select
|
182
|
+
return query
|
183
|
+
}
|
184
|
+
|
185
|
+
query.limit = (num)=>{
|
186
|
+
useLimit = num
|
187
|
+
return query
|
188
|
+
}
|
189
|
+
|
190
|
+
query.skip = (num)=>{
|
191
|
+
useSkip = num
|
192
|
+
return query
|
193
|
+
}
|
194
|
+
|
195
|
+
query.exec = ()=>{
|
196
|
+
return collection.findOne(this, collectionName, options, useSelect, useSkip)
|
197
|
+
}
|
198
|
+
|
199
|
+
return query
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
res.deleteOne = (options)=>{
|
204
|
+
|
205
|
+
return collection.deleteOne(this, collectionName, options)
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
res.deleteMany = (options)=>{
|
210
|
+
|
211
|
+
return collection.deleteMany(this, collectionName, options)
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
res.updateOne = (options, new_values)=>{
|
216
|
+
|
217
|
+
return collection.updateOne(this, collectionName, options, new_values)
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
res.updateMany = (options, new_values)=>{
|
222
|
+
|
223
|
+
return collection.updateMany(this, collectionName, options, new_values)
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
res.countDocuments = (options)=>{
|
228
|
+
|
229
|
+
return collection.countDocuments(this, collectionName, options)
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
res.size = (options)=>{
|
234
|
+
|
235
|
+
return collection.getSize(this, collectionName, options)
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
res.watch = ()=>{
|
240
|
+
|
241
|
+
return collection.watch(this, collectionName)
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
return res
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
module.exports = Plugin;
|
package/package.json
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"name": "yodata",
|
3
|
+
"description": "Y0data online services to save your data",
|
4
|
+
"version": "0.0.1",
|
5
|
+
"main": "data/plugin.js",
|
6
|
+
"dependencies": {
|
7
|
+
"events": "^3.3.0",
|
8
|
+
"random-token": "^0.0.8",
|
9
|
+
"ws": "^8.16.0"
|
10
|
+
},
|
11
|
+
"keywords": [
|
12
|
+
"database",
|
13
|
+
"data",
|
14
|
+
"y0data"
|
15
|
+
],
|
16
|
+
"author": "Yousuf",
|
17
|
+
"license": "BSD-2-Clause",
|
18
|
+
"readmeFilename": "README.md"
|
19
|
+
}
|
package/ws/Message.js
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Message extends Object {
|
2
|
+
constructor (message) {
|
3
|
+
super ()
|
4
|
+
this.body = message
|
5
|
+
}
|
6
|
+
|
7
|
+
json(){
|
8
|
+
try { return JSON.parse(this.body) } catch(e) { return e.message };
|
9
|
+
}
|
10
|
+
|
11
|
+
text(){
|
12
|
+
if(this.body.includes('Buffer')) return Buffer.from(this.body).toString("utf8")
|
13
|
+
return this.body
|
14
|
+
}
|
15
|
+
|
16
|
+
}
|
17
|
+
module.exports = Message
|
18
|
+
|
package/ws/index.js
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
let EventEmitter = require("events");
|
2
|
+
const ws = require('ws')
|
3
|
+
let Message = require('../ws/Message.js')
|
4
|
+
|
5
|
+
class index extends EventEmitter {
|
6
|
+
constructor (url, options) {
|
7
|
+
super ()
|
8
|
+
this.headers = {}
|
9
|
+
if(!options) options = {}
|
10
|
+
if(options.headers) this.headers = options.headers
|
11
|
+
|
12
|
+
this.eventSendMessage = options.sendMessage
|
13
|
+
|
14
|
+
if(options.sendMessage) {
|
15
|
+
if(options.sendMessage.on) this.eventSendMessage = options.sendMessage.on
|
16
|
+
}
|
17
|
+
|
18
|
+
this.options = options
|
19
|
+
this.url = url
|
20
|
+
this.pingStatus = false
|
21
|
+
this.project = null
|
22
|
+
this.autosendStatus = false
|
23
|
+
this.historyPing = []
|
24
|
+
this.historySend = []
|
25
|
+
this.autoSend = {time: 10, msg: {"event": "hello", data: {world: true}}}
|
26
|
+
this.pingOptions = {time: 35, msg: {"event": "ping", data: {}}}
|
27
|
+
this._messages = []
|
28
|
+
this.cache = true
|
29
|
+
|
30
|
+
this.ready = false
|
31
|
+
|
32
|
+
if(options.cache === false) this.cache = false
|
33
|
+
this.reconnectStatus = options.reconnect || false
|
34
|
+
this._catchSystem()
|
35
|
+
this._connect(url, this.headers, options)
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
_catchSystem(){
|
40
|
+
setInterval(async ()=>{
|
41
|
+
if(this.cache === true) return;
|
42
|
+
this._messages = []
|
43
|
+
for(const d of this.historyPing.filter(data => data.use === true)) this.historyPing.shift(d)
|
44
|
+
for(const d of this.historySend.filter(data => data.use === true)) this.historySend.shift(d)
|
45
|
+
|
46
|
+
}, 500)
|
47
|
+
}
|
48
|
+
|
49
|
+
_ready(){
|
50
|
+
this.emit("ready")
|
51
|
+
}
|
52
|
+
|
53
|
+
async _close(a1, a2, a3){
|
54
|
+
await this.disablePing()
|
55
|
+
this.emit('disconnect', a1, a2)
|
56
|
+
this.ready = false
|
57
|
+
if(this.reconnectStatus === true) this.reconnect()
|
58
|
+
}
|
59
|
+
|
60
|
+
_error(a1, a2, a3){
|
61
|
+
//this.emit('error', a1, a2)
|
62
|
+
}
|
63
|
+
|
64
|
+
_eventCreateMessage(a1){
|
65
|
+
this.emit('createMessage', a1)
|
66
|
+
}
|
67
|
+
|
68
|
+
_eventReConnect(){
|
69
|
+
this.emit('reConnect')
|
70
|
+
}
|
71
|
+
|
72
|
+
_ping(message){
|
73
|
+
this.emit('ping', message)
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
async createMessage(msg){
|
78
|
+
|
79
|
+
let data; try { data = JSON.parse(JSON.stringify(msg)) } catch(e) {this._messages.unshift({json: null, text: msg}); return new Message(msg) };
|
80
|
+
try {
|
81
|
+
await this.project.send(JSON.stringify(msg))
|
82
|
+
this._messages.unshift({json: JSON.parse(JSON.stringify(msg)), text: msg})
|
83
|
+
|
84
|
+
this.emit(this.eventSendMessage, new Message(JSON.stringify(msg)))
|
85
|
+
|
86
|
+
return new Message(JSON.stringify(msg))
|
87
|
+
} catch(e) {
|
88
|
+
return e.message
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
_message(msg){
|
93
|
+
try { this._messages.unshift({json: JSON.parse(msg), text: msg})} catch { this._messages.unshift({json: null, text: msg}) };
|
94
|
+
this.emit('message', new Message(msg))
|
95
|
+
}
|
96
|
+
|
97
|
+
async disconnect(){
|
98
|
+
await this.project.close()
|
99
|
+
return {url: this.url, headers: this.headers, options: this.options}
|
100
|
+
}
|
101
|
+
|
102
|
+
systemAutoSend(id){
|
103
|
+
this.historySend.unshift({id: id, use: false, send: null})
|
104
|
+
let d = this.historySend.find(a => a.id === id)
|
105
|
+
setInterval(async ()=>{
|
106
|
+
if(d.use === true) return;
|
107
|
+
this.systemAutoSend(this.historySend.length + 1);
|
108
|
+
d.use = true
|
109
|
+
if(this.autosendStatus === false) return d.send = false;
|
110
|
+
this.event('autoSend', this.autoSend.msg)
|
111
|
+
this.createMessage(this.autoSend.msg)
|
112
|
+
d.send = true
|
113
|
+
}, this.autoSend.time * 1000)
|
114
|
+
return {status: this.autosendStatus, options: this.autoSend, dataConnect: d}
|
115
|
+
}
|
116
|
+
|
117
|
+
systemPing(id){
|
118
|
+
this.historyPing.unshift({id: id, use: false, send: null})
|
119
|
+
let d = this.historyPing.find(a => a.id === id)
|
120
|
+
setInterval(async ()=>{
|
121
|
+
if(d.use === true) return;
|
122
|
+
this.systemPing(this.historyPing.length + 1);
|
123
|
+
d.use = true
|
124
|
+
if(this.pingStatus === false) return d.send = false;
|
125
|
+
this.event('ping', this.pingOptions.msg)
|
126
|
+
this.createMessage(this.pingOptions.msg)
|
127
|
+
d.send = true
|
128
|
+
}, this.pingOptions.time * 1000)
|
129
|
+
return {status: this.pingStatus, options: this.pingOptions, dataConnect: d}
|
130
|
+
}
|
131
|
+
|
132
|
+
|
133
|
+
enableAutoSend(options){
|
134
|
+
if(options) this.autoSend = options
|
135
|
+
this.autosendStatus = true
|
136
|
+
this.systemAutoSend(this.historySend.length + 1)
|
137
|
+
return {status: this.autosendStatus, options: this.autoSend}
|
138
|
+
}
|
139
|
+
|
140
|
+
disableAutoSend(){
|
141
|
+
this.autosendStatus = false
|
142
|
+
return {status: this.autosendStatus, options: this.autoSend}
|
143
|
+
}
|
144
|
+
enablePing(options){
|
145
|
+
if(options) this.pingOptions = options
|
146
|
+
this.pingStatus = true
|
147
|
+
this.systemPing(this.historyPing.length + 1)
|
148
|
+
return {status: this.pingStatus, options: this.pingOptions}
|
149
|
+
}
|
150
|
+
|
151
|
+
disablePing(){
|
152
|
+
this.pingStatus = false
|
153
|
+
return {status: this.pingStatus, options: this.pingOptions}
|
154
|
+
}
|
155
|
+
|
156
|
+
async reconnect(time){
|
157
|
+
return await new Promise(re => {
|
158
|
+
if(!time){
|
159
|
+
if(this.options && this.options.reconnectTime) {time = this.options.reconnectTime}else{time = 2}
|
160
|
+
}
|
161
|
+
|
162
|
+
setTimeout(()=>{
|
163
|
+
this.event('reconnect')
|
164
|
+
this._connect(this.url, this.headers, this.options)
|
165
|
+
re({url: this.url, headers: this.headers, options: this.options})
|
166
|
+
}, time * 1000)
|
167
|
+
})
|
168
|
+
}
|
169
|
+
|
170
|
+
event(d1, d2, d3){
|
171
|
+
if(d1 === "open") this._ready()
|
172
|
+
if(d1 === "close") this._close(d2, d3)
|
173
|
+
if(d1 === "error") this._error(d2, d3)
|
174
|
+
if(d1 === "message") this._message(d2)
|
175
|
+
if(d1 === "createmessage") this._eventCreateMessage(d2)
|
176
|
+
if(d1 === "reconnect") this._eventReConnect()
|
177
|
+
if(d1 === "ping") this._ping(d1)
|
178
|
+
|
179
|
+
return {dataOne: d1, dataTwo: d2, dataThree: d3}
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
_connect(url, headers, options){
|
184
|
+
this.project = new ws( url , options.protocol || options._protocol , { headers:this.headers })
|
185
|
+
this.ready=true
|
186
|
+
this.project.on('open', async (open) => {this.event('open')});
|
187
|
+
this.project.on('message', async (msg) => { this.event('message', msg) })
|
188
|
+
this.project.on('close', async (d1, d2) => {this.event('close', d1, d2)});
|
189
|
+
this.project.on('error', async (d1, d2) => {this.event('error', d1, d2);});
|
190
|
+
return {url: url, headers: headers, options: options}
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
}
|
195
|
+
module.exports = index
|