@blocklet/sdk 1.6.18 → 1.6.22-beta

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.
@@ -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
- class NotificationService {
5
- static async sendToUser(receiver, notification) {
6
- checkBlockletEnv();
25
+ let client = null;
7
26
 
8
- const sender = {
9
- appId: process.env.BLOCKLET_APP_ID,
10
- appSk: process.env.BLOCKLET_APP_SK,
11
- did: process.env.BLOCKLET_DID,
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
- return sendNotification(receiver, notification, sender, process.env.ABT_NODE_SERVICE_PORT);
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
- module.exports = NotificationService;
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
  };
@@ -5,6 +5,8 @@ const getWallet = require('./wallet');
5
5
  const checkBlockletEnv = require('./util/check-blocklet-env');
6
6
  const { AUTH_SERVICE_PREFIX } = require('./util/constants');
7
7
 
8
+ const env = require('./env');
9
+
8
10
  // wraps value in closure or returns closure
9
11
  const closure = (value) => (typeof value === 'function' ? value : () => value);
10
12
 
@@ -26,17 +28,26 @@ class WalletAuthenticator extends Authenticator {
26
28
  const info = await closure(options.appInfo)(...args);
27
29
 
28
30
  const { request, baseUrl } = args[0];
29
- const groupPathPrefix = request.headers['x-group-path-prefix'] || '/';
31
+ const rootPath = '/';
32
+ const pathPrefix = request.headers['x-path-prefix'] || rootPath;
33
+ const groupPathPrefix = request.headers['x-group-path-prefix'] || rootPath;
30
34
  const blockletDid = request.headers['x-blocklet-did'];
31
35
 
36
+ let appUrl = baseUrl;
37
+ let subscriptionPath = joinUrl(groupPathPrefix, AUTH_SERVICE_PREFIX, 'websocket');
38
+ if (env.isComponent && pathPrefix !== rootPath) {
39
+ appUrl = joinUrl(appUrl, rootPath).replace(pathPrefix, rootPath);
40
+ subscriptionPath = joinUrl(pathPrefix, subscriptionPath);
41
+ }
42
+
32
43
  return {
33
44
  name: process.env.BLOCKLET_APP_NAME,
34
45
  description: process.env.BLOCKLET_APP_DESCRIPTION,
35
46
  ...(info || {}),
36
- link: baseUrl,
47
+ link: appUrl,
37
48
  icon: joinUrl(baseUrl, AUTH_SERVICE_PREFIX, `/blocklet/logo/${blockletDid}`),
38
49
  updateSubEndpoint: true,
39
- subscriptionEndpoint: joinUrl(groupPathPrefix, AUTH_SERVICE_PREFIX, 'websocket'),
50
+ subscriptionEndpoint: subscriptionPath,
40
51
  nodeDid: process.env.ABT_NODE_DID,
41
52
  };
42
53
  },
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.6.18",
6
+ "version": "1.6.22-beta",
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.18",
23
- "@abtnode/constant": "1.6.18",
24
- "@arcblock/did-auth": "^1.14.8",
25
- "@arcblock/jwt": "^1.14.8",
26
- "@blocklet/meta": "1.6.18",
22
+ "@abtnode/client": "1.6.22",
23
+ "@abtnode/constant": "1.6.22",
24
+ "@arcblock/did-auth": "^1.14.17",
25
+ "@arcblock/jwt": "^1.14.17",
26
+ "@arcblock/ws": "^1.14.17",
27
+ "@blocklet/meta": "1.6.22",
27
28
  "@nedb/core": "^1.2.2",
28
- "@ocap/mcrypto": "^1.14.8",
29
- "@ocap/wallet": "^1.14.8",
30
- "axios": "^0.21.4",
29
+ "@ocap/mcrypto": "^1.14.17",
30
+ "@ocap/wallet": "^1.14.17",
31
+ "axios": "^0.25.0",
31
32
  "fs-extra": "^10.0.0",
32
- "joi": "^17.5.0",
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"
@@ -37,6 +38,5 @@
37
38
  "devDependencies": {
38
39
  "detect-port": "^1.3.0",
39
40
  "jest": "^27.4.5"
40
- },
41
- "gitHead": "2f02f166869d8ebedc0466068f6ed90ab3e07b87"
41
+ }
42
42
  }