@blocklet/sdk 1.6.17 → 1.6.21
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/lib/service/notification.js +76 -11
- package/lib/validators/notification.js +14 -0
- package/package.json +12 -11
|
@@ -1,18 +1,83 @@
|
|
|
1
|
+
const Jwt = require('@arcblock/jwt');
|
|
2
|
+
const EventEmitter = require('events');
|
|
3
|
+
|
|
4
|
+
const { WsClient } = require('@arcblock/ws');
|
|
5
|
+
|
|
1
6
|
const checkBlockletEnv = require('../util/check-blocklet-env');
|
|
2
7
|
const sendNotification = require('../util/send-notification');
|
|
8
|
+
const { AUTH_SERVICE_PREFIX } = require('../util/constants');
|
|
9
|
+
const getWallet = require('../wallet');
|
|
10
|
+
|
|
11
|
+
const sendToUser = async (receiver, notification) => {
|
|
12
|
+
checkBlockletEnv();
|
|
13
|
+
|
|
14
|
+
const sender = {
|
|
15
|
+
appId: process.env.BLOCKLET_APP_ID,
|
|
16
|
+
appSk: process.env.BLOCKLET_APP_SK,
|
|
17
|
+
did: process.env.BLOCKLET_DID,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return sendNotification(receiver, notification, sender, process.env.ABT_NODE_SERVICE_PORT);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const emitter = new EventEmitter();
|
|
3
24
|
|
|
4
|
-
|
|
5
|
-
static async sendToUser(receiver, notification) {
|
|
6
|
-
checkBlockletEnv();
|
|
25
|
+
let client = null;
|
|
7
26
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
27
|
+
const initClient = () => {
|
|
28
|
+
if (!client) {
|
|
29
|
+
const wallet = getWallet();
|
|
30
|
+
const { address: did, publicKey: pk, secretKey: sk } = wallet;
|
|
31
|
+
const url = `ws://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}${AUTH_SERVICE_PREFIX}`;
|
|
32
|
+
const token = Jwt.sign(did, sk, {});
|
|
13
33
|
|
|
14
|
-
|
|
34
|
+
client = new WsClient(url, {
|
|
35
|
+
heartbeatIntervalMs: 10 * 1000,
|
|
36
|
+
params: () => ({
|
|
37
|
+
token,
|
|
38
|
+
pk,
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
client.connect();
|
|
43
|
+
|
|
44
|
+
const channel = client.channel(wallet.address, { token, pk });
|
|
45
|
+
|
|
46
|
+
channel
|
|
47
|
+
.join()
|
|
48
|
+
.receive('error', (err) => {
|
|
49
|
+
const msg = `join channel error: ${err.message}`;
|
|
50
|
+
console.error(msg);
|
|
51
|
+
emitter.emit('error', { message: msg });
|
|
52
|
+
})
|
|
53
|
+
.receive('timeout', () => {
|
|
54
|
+
const msg = 'join channel timeout';
|
|
55
|
+
console.error(msg);
|
|
56
|
+
emitter.emit('error', { message: msg });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
channel.on('message', ({ status, response } = {}) => {
|
|
60
|
+
if (status === 'ok') {
|
|
61
|
+
emitter.emit(response.type, response);
|
|
62
|
+
} else {
|
|
63
|
+
emitter.emit('error', response);
|
|
64
|
+
console.error({
|
|
65
|
+
status,
|
|
66
|
+
response,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
});
|
|
15
70
|
}
|
|
16
|
-
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
module.exports = {
|
|
74
|
+
sendToUser,
|
|
75
|
+
on: (event, cb) => {
|
|
76
|
+
if (!client) {
|
|
77
|
+
initClient();
|
|
78
|
+
}
|
|
17
79
|
|
|
18
|
-
|
|
80
|
+
return emitter.on(event, cb);
|
|
81
|
+
},
|
|
82
|
+
off: emitter.off.bind(emitter),
|
|
83
|
+
};
|
|
@@ -8,6 +8,7 @@ const TYPES = {
|
|
|
8
8
|
NOTIFICATION: 'notification',
|
|
9
9
|
CONNECT: 'connect',
|
|
10
10
|
FEED: 'feed',
|
|
11
|
+
HI: 'hi',
|
|
11
12
|
};
|
|
12
13
|
|
|
13
14
|
const assetSchema = Joi.object({
|
|
@@ -67,6 +68,17 @@ const feedTypeSchema = Joi.object({
|
|
|
67
68
|
|
|
68
69
|
const notificationSchema = Joi.alternatives().try(notificationTypeSchema, connectTypeSchema, feedTypeSchema).required();
|
|
69
70
|
|
|
71
|
+
const messageSchema = Joi.object({
|
|
72
|
+
id: Joi.string().required(),
|
|
73
|
+
createdAt: Joi.date().iso().required(),
|
|
74
|
+
type: Joi.string().required(),
|
|
75
|
+
receiver: Joi.object({
|
|
76
|
+
did: Joi.DID().trim().required(),
|
|
77
|
+
}).required(),
|
|
78
|
+
})
|
|
79
|
+
.unknown()
|
|
80
|
+
.required();
|
|
81
|
+
|
|
70
82
|
const receiverSchema = Joi.DID().trim().required();
|
|
71
83
|
|
|
72
84
|
const inputNotificationSchema = Joi.alternatives()
|
|
@@ -78,10 +90,12 @@ const inputReceiverSchema = Joi.alternatives().try(Joi.array().items(receiverSch
|
|
|
78
90
|
module.exports = {
|
|
79
91
|
validateReceiver: inputReceiverSchema.validateAsync.bind(inputReceiverSchema),
|
|
80
92
|
validateNotification: inputNotificationSchema.validateAsync.bind(inputNotificationSchema),
|
|
93
|
+
validateMessage: messageSchema.validateAsync.bind(messageSchema),
|
|
81
94
|
tokenSchema,
|
|
82
95
|
actionSchema,
|
|
83
96
|
assetSchema,
|
|
84
97
|
vcSchema,
|
|
85
98
|
attachmentSchema,
|
|
86
99
|
notificationSchema,
|
|
100
|
+
messageSchema,
|
|
87
101
|
};
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.6.
|
|
6
|
+
"version": "1.6.21",
|
|
7
7
|
"description": "graphql client to read/write data on abt node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -19,17 +19,18 @@
|
|
|
19
19
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@abtnode/client": "1.6.
|
|
23
|
-
"@abtnode/constant": "1.6.
|
|
24
|
-
"@arcblock/did-auth": "^1.14.
|
|
25
|
-
"@arcblock/jwt": "^1.14.
|
|
26
|
-
"@
|
|
22
|
+
"@abtnode/client": "1.6.21",
|
|
23
|
+
"@abtnode/constant": "1.6.21",
|
|
24
|
+
"@arcblock/did-auth": "^1.14.13",
|
|
25
|
+
"@arcblock/jwt": "^1.14.13",
|
|
26
|
+
"@arcblock/ws": "^1.14.13",
|
|
27
|
+
"@blocklet/meta": "1.6.21",
|
|
27
28
|
"@nedb/core": "^1.2.2",
|
|
28
|
-
"@ocap/mcrypto": "^1.14.
|
|
29
|
-
"@ocap/wallet": "^1.14.
|
|
30
|
-
"axios": "^0.
|
|
29
|
+
"@ocap/mcrypto": "^1.14.13",
|
|
30
|
+
"@ocap/wallet": "^1.14.13",
|
|
31
|
+
"axios": "^0.25.0",
|
|
31
32
|
"fs-extra": "^10.0.0",
|
|
32
|
-
"joi": "^17.
|
|
33
|
+
"joi": "^17.6.0",
|
|
33
34
|
"lodash": "^4.17.21",
|
|
34
35
|
"lru-cache": "^6.0.0",
|
|
35
36
|
"url-join": "^4.0.1"
|
|
@@ -38,5 +39,5 @@
|
|
|
38
39
|
"detect-port": "^1.3.0",
|
|
39
40
|
"jest": "^27.4.5"
|
|
40
41
|
},
|
|
41
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "864ae21711c119948475057a1f634fd7d16ae91a"
|
|
42
43
|
}
|