@icgio/icg-utils 1.7.77 → 1.8.0
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.
- package/index.js +1 -1
- package/lib/influxdb.js +3 -1
- package/lib/logger.js +1 -0
- package/lib/observable.js +53 -0
- package/lib/rate-limiter.js +3 -1
- package/package.json +1 -1
- package/lib/wechat-bot-client.js +0 -49
package/index.js
CHANGED
|
@@ -7,10 +7,10 @@ module.exports = {
|
|
|
7
7
|
ip: require('./lib/ip.js'),
|
|
8
8
|
logger: require('./lib/logger.js'),
|
|
9
9
|
mail: require('./lib/mail.js'),
|
|
10
|
+
observable: require('./lib/observable.js'),
|
|
10
11
|
ohlcv: require('./lib/ohlcv'),
|
|
11
12
|
rate_limiter: require('./lib/rate-limiter.js'),
|
|
12
13
|
utils: require('./lib/utils.js'),
|
|
13
|
-
wechat: require('./lib/wechat-bot-client.js'),
|
|
14
14
|
constants: {
|
|
15
15
|
stablecoins: require('./constants/stablecoins.js'),
|
|
16
16
|
forex_rate: require('./constants/forex-rate.js'),
|
package/lib/influxdb.js
CHANGED
|
@@ -15,7 +15,7 @@ function fields_check(fields) {
|
|
|
15
15
|
return result
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
class Influx {
|
|
19
19
|
constructor(config) {
|
|
20
20
|
const { url, token, org, org_id, bucket, schema } = config
|
|
21
21
|
this.influx_db = new InfluxDB({ url, token })
|
|
@@ -95,3 +95,5 @@ module.exports = class Influx {
|
|
|
95
95
|
})
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
+
|
|
99
|
+
module.exports = Influx
|
package/lib/logger.js
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
class Observable {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.listeners = []
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Subscribe to changes
|
|
8
|
+
* @param {Function} callback - Function to call when an update occurs
|
|
9
|
+
*/
|
|
10
|
+
subscribe(callback) {
|
|
11
|
+
if (typeof callback !== 'function') {
|
|
12
|
+
throw new Error('Callback must be a function')
|
|
13
|
+
}
|
|
14
|
+
this.listeners.push(callback)
|
|
15
|
+
return () => this.unsubscribe(callback) // Return an unsubscribe function
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Unsubscribe from changes
|
|
20
|
+
* @param {Function} callback - The callback to remove
|
|
21
|
+
*/
|
|
22
|
+
unsubscribe(callback) {
|
|
23
|
+
this.listeners = this.listeners.filter((listener) => listener !== callback)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Notify all subscribers about an update
|
|
28
|
+
* @param {*} data - The data to pass to subscribers
|
|
29
|
+
*/
|
|
30
|
+
notify(data) {
|
|
31
|
+
this.listeners.forEach((callback) => callback(data))
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = Observable
|
|
36
|
+
|
|
37
|
+
// // Example Usage
|
|
38
|
+
// const observable = new Observable()
|
|
39
|
+
|
|
40
|
+
// // Subscribe to updates
|
|
41
|
+
// const unsubscribe = observable.subscribe((update) => {
|
|
42
|
+
// console.log('Received update:', update)
|
|
43
|
+
// })
|
|
44
|
+
|
|
45
|
+
// // Emit updates
|
|
46
|
+
// observable.notify({ message: 'Hello, world!' }) // Logs: Received update: { message: 'Hello, world!' }
|
|
47
|
+
// observable.notify({ message: 'Another update!' }) // Logs: Received update: { message: 'Another update!' }
|
|
48
|
+
|
|
49
|
+
// // Unsubscribe from updates
|
|
50
|
+
// unsubscribe()
|
|
51
|
+
|
|
52
|
+
// // No logs after unsubscribe
|
|
53
|
+
// observable.notify({ message: 'No one will see this' })
|
package/lib/rate-limiter.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
class RateLimiter {
|
|
2
2
|
constructor(settings) {
|
|
3
3
|
this.max_concurrent = settings.max_concurrent ? settings.max_concurrent : null
|
|
4
4
|
this.interval_concurrent = settings.interval_concurrent ? settings.interval_concurrent : null
|
|
@@ -25,3 +25,5 @@ module.exports = class RateLimiter {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
module.exports = RateLimiter
|
package/package.json
CHANGED
package/lib/wechat-bot-client.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
const websocketclient = require('websocket').client
|
|
2
|
-
|
|
3
|
-
module.exports = class Wechat {
|
|
4
|
-
constructor(server_addr = 'http://172.105.199.80:8080', origin = '') {
|
|
5
|
-
this.client = new websocketclient()
|
|
6
|
-
this.server_addr = server_addr
|
|
7
|
-
this.connection
|
|
8
|
-
this.connection_status
|
|
9
|
-
this.client.on('connectfailed', (err) => {
|
|
10
|
-
console.error('wechat connect error: ' + err)
|
|
11
|
-
})
|
|
12
|
-
this.client.on('connect', (connection) => {
|
|
13
|
-
console.log('wechat websocket client connected')
|
|
14
|
-
this.connection = connection
|
|
15
|
-
this.connection_status = 'opened'
|
|
16
|
-
connection.on('error', (err) => {
|
|
17
|
-
console.error('wechat connection error: ' + err)
|
|
18
|
-
})
|
|
19
|
-
connection.on('close', () => {
|
|
20
|
-
console.log('wechat connection closed')
|
|
21
|
-
this.connection_status = 'n-opened'
|
|
22
|
-
setTimeout(() => {
|
|
23
|
-
this.client.connect(this.server_addr, 'wechat-protocol', origin)
|
|
24
|
-
}, 1000)
|
|
25
|
-
})
|
|
26
|
-
connection.on('message', (message) => {
|
|
27
|
-
if (message.type === 'utf8') {
|
|
28
|
-
console.log('received: ' + message)
|
|
29
|
-
}
|
|
30
|
-
})
|
|
31
|
-
})
|
|
32
|
-
this.client.connect(this.server_addr, 'wechat-protocol', origin)
|
|
33
|
-
}
|
|
34
|
-
send(name, content, isgroup) {
|
|
35
|
-
if (this.connection_status === 'opened') {
|
|
36
|
-
let json = {
|
|
37
|
-
name,
|
|
38
|
-
content,
|
|
39
|
-
isgroup,
|
|
40
|
-
}
|
|
41
|
-
this.connection.sendUTF(JSON.stringify(json))
|
|
42
|
-
} else {
|
|
43
|
-
console.error('wechat websocket not connected')
|
|
44
|
-
setTimeout(() => {
|
|
45
|
-
this.client.connect(this.server_addr, 'wechat-protocol', origin)
|
|
46
|
-
}, 10000)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|