@blocklet/sdk 1.8.32 → 1.8.34

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.
Files changed (53) hide show
  1. package/lib/component/index.d.ts +35 -0
  2. package/lib/component/index.js +83 -80
  3. package/lib/config.d.ts +33 -0
  4. package/lib/config.js +10 -7
  5. package/lib/connect/authenticator.d.ts +5 -0
  6. package/lib/connect/authenticator.js +14 -15
  7. package/lib/connect/handler.d.ts +2 -0
  8. package/lib/connect/handler.js +91 -65
  9. package/lib/connect/shared.d.ts +13 -0
  10. package/lib/connect/shared.js +26 -31
  11. package/lib/database/index.d.ts +13 -0
  12. package/lib/database/index.js +52 -39
  13. package/lib/env.d.ts +12 -0
  14. package/lib/env.js +12 -9
  15. package/lib/error-handler.d.ts +0 -0
  16. package/lib/error-handler.js +5 -5
  17. package/lib/index.d.ts +51 -0
  18. package/lib/index.js +51 -40
  19. package/lib/middlewares/auth.d.ts +3 -0
  20. package/lib/middlewares/auth.js +52 -49
  21. package/lib/middlewares/component.d.ts +7 -0
  22. package/lib/middlewares/component.js +28 -24
  23. package/lib/middlewares/index.d.ts +20 -0
  24. package/lib/middlewares/index.js +16 -8
  25. package/lib/middlewares/user.d.ts +10 -0
  26. package/lib/middlewares/user.js +20 -10
  27. package/lib/security/index.d.ts +9 -0
  28. package/lib/security/index.js +22 -21
  29. package/lib/service/auth.d.ts +9 -0
  30. package/lib/service/auth.js +131 -165
  31. package/lib/service/notification.d.ts +44 -0
  32. package/lib/service/notification.js +149 -129
  33. package/lib/types/notification.d.ts +71 -0
  34. package/lib/types/notification.js +3 -0
  35. package/lib/util/check-blocklet-env.d.ts +2 -0
  36. package/lib/util/check-blocklet-env.js +20 -19
  37. package/lib/util/constants.d.ts +5 -0
  38. package/lib/util/constants.js +6 -2
  39. package/lib/util/send-notification.d.ts +25 -0
  40. package/lib/util/send-notification.js +118 -115
  41. package/lib/validators/index.d.ts +12 -0
  42. package/lib/validators/index.js +13 -7
  43. package/lib/validators/notification.d.ts +59 -0
  44. package/lib/validators/notification.js +127 -97
  45. package/lib/version.d.ts +6 -0
  46. package/lib/version.js +6 -0
  47. package/lib/wallet-authenticator.d.ts +5 -0
  48. package/lib/wallet-authenticator.js +13 -16
  49. package/lib/wallet-handler.d.ts +17 -0
  50. package/lib/wallet-handler.js +121 -88
  51. package/lib/wallet.d.ts +3 -0
  52. package/lib/wallet.js +9 -10
  53. package/package.json +40 -18
@@ -1,20 +1,55 @@
1
- const Jwt = require('@arcblock/jwt');
2
- const EventEmitter = require('events');
3
-
4
- const { WsClient } = require('@arcblock/ws');
5
- const { getAppPublicChannel } = require('@blocklet/meta/lib/channel');
6
-
7
- const checkBlockletEnv = require('../util/check-blocklet-env');
8
- const { sendToUser, sendToAppChannel } = require('../util/send-notification');
9
- const { SERVICE_PREFIX } = require('../util/constants');
10
- const getWallet = require('../wallet');
11
- const { NOTIFICATION_TYPES } = require('../validators/notification');
12
-
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.broadcast = exports.sendToUser = exports._message = exports.off = exports.on = void 0;
39
+ /* eslint-disable @typescript-eslint/naming-convention */
40
+ const Jwt = __importStar(require("@arcblock/jwt"));
41
+ const events_1 = __importDefault(require("events"));
42
+ const ws_1 = require("@arcblock/ws");
43
+ const channel_1 = require("@blocklet/meta/lib/channel");
44
+ const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env"));
45
+ const send_notification_1 = require("../util/send-notification");
46
+ const constants_1 = require("../util/constants");
47
+ const wallet_1 = __importDefault(require("../wallet"));
48
+ const notification_1 = require("../validators/notification");
13
49
  const getSender = () => ({
14
- appDid: process.env.BLOCKLET_APP_ID,
15
- appSk: process.env.BLOCKLET_APP_SK,
50
+ appDid: process.env.BLOCKLET_APP_ID,
51
+ appSk: process.env.BLOCKLET_APP_SK,
16
52
  });
17
-
18
53
  /**
19
54
  *
20
55
  * @param {Notification} notification
@@ -23,12 +58,11 @@ const getSender = () => ({
23
58
  * }} options
24
59
  * @returns
25
60
  */
26
- const doSendToUser = async (receiver, notification, options) => {
27
- checkBlockletEnv();
28
-
29
- return sendToUser(receiver, notification, getSender(), process.env.ABT_NODE_SERVICE_PORT, options);
30
- };
31
-
61
+ const doSendToUser = (receiver, notification, options) => __awaiter(void 0, void 0, void 0, function* () {
62
+ (0, check_blocklet_env_1.default)();
63
+ return (0, send_notification_1.sendToUser)(receiver, notification, getSender(), process.env.ABT_NODE_SERVICE_PORT, options);
64
+ });
65
+ exports.sendToUser = doSendToUser;
32
66
  /**
33
67
  *
34
68
  * @param {Notification} notification
@@ -40,126 +74,112 @@ const doSendToUser = async (receiver, notification, options) => {
40
74
  * }} options
41
75
  * @returns
42
76
  */
43
- const broadcast = async (notification, options = {}) => {
44
- checkBlockletEnv();
45
-
46
- const sender = getSender();
47
-
48
- const { channel = getAppPublicChannel(sender.appDid) } = options;
49
- const { event = 'message' } = options;
50
-
51
- return sendToAppChannel(channel, event, notification, sender, process.env.ABT_NODE_SERVICE_PORT, options);
52
- };
53
-
54
- const emitter = new EventEmitter();
55
- const messageEmitter = new EventEmitter();
56
-
77
+ const broadcast = (notification, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
78
+ (0, check_blocklet_env_1.default)();
79
+ const sender = getSender();
80
+ const { channel = (0, channel_1.getAppPublicChannel)(sender.appDid) } = options;
81
+ const { event = 'message' } = options;
82
+ return (0, send_notification_1.sendToAppChannel)(channel, event, notification, sender, process.env.ABT_NODE_SERVICE_PORT, options);
83
+ });
84
+ exports.broadcast = broadcast;
85
+ const emitter = new events_1.default();
86
+ const messageEmitter = new events_1.default();
57
87
  const emitError = (error) => {
58
- messageEmitter.emit('error', error);
59
- emitter.emit('error', error);
88
+ messageEmitter.emit('error', error);
89
+ emitter.emit('error', error);
60
90
  };
61
-
62
91
  let client = null;
63
-
64
92
  const initClient = () => {
65
- if (!client) {
66
- const wallet = getWallet();
67
- const { address: did, publicKey: pk, secretKey: sk } = wallet;
68
- const url = `ws://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}${SERVICE_PREFIX}`;
69
- const token = () => Jwt.sign(did, sk, {});
70
-
71
- client = new WsClient(url, {
72
- heartbeatIntervalMs: 10 * 1000,
73
- params: () => ({
74
- token: token(),
75
- pk,
76
- }),
77
- });
78
-
79
- client.connect();
80
-
81
- const messageChannel = client.channel(did, () => ({ token: token(), pk }));
82
- const appPublicChannel = client.channel(getAppPublicChannel(did), () => ({ token: token(), pk }));
83
-
84
- messageChannel
85
- .join()
86
- .receive('error', (err) => {
87
- const msg = `join channel error: ${err.message}`;
88
- console.error(msg);
89
- emitError({ message: msg });
90
- })
91
- .receive('timeout', () => {
92
- const msg = 'join channel timeout';
93
- console.error(msg);
94
- emitError({ message: msg });
95
- });
96
-
97
- messageChannel.on('message', ({ status, response } = {}) => {
98
- if (status === 'ok') {
99
- messageEmitter.emit(response.type, response);
100
- if (response.type === NOTIFICATION_TYPES.HI) {
101
- emitter.emit(response.type, response);
102
- }
103
- } else {
104
- emitError(response);
105
- console.error({
106
- status,
107
- response,
93
+ if (!client) {
94
+ const wallet = (0, wallet_1.default)();
95
+ const { address: did, publicKey: pk, secretKey: sk } = wallet;
96
+ const url = `ws://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}${constants_1.SERVICE_PREFIX}`;
97
+ const token = () => Jwt.sign(did, sk, {});
98
+ client = new ws_1.WsClient(url, {
99
+ heartbeatIntervalMs: 10 * 1000,
100
+ params: () => ({
101
+ token: token(),
102
+ pk,
103
+ }),
108
104
  });
109
- }
110
- });
111
-
112
- appPublicChannel
113
- .join()
114
- .receive('error', (err) => {
115
- const msg = `join channel error: ${err.message}`;
116
- console.error(msg);
117
- emitter.emit('error', { message: msg });
118
- })
119
- .receive('timeout', () => {
120
- const msg = 'join channel timeout';
121
- console.error(msg);
122
- emitter.emit('error', { message: msg });
123
- });
124
-
125
- appPublicChannel.on(NOTIFICATION_TYPES.HI, ({ status, response } = {}) => {
126
- if (status === 'ok') {
127
- emitter.emit(NOTIFICATION_TYPES.HI, response);
128
- } else {
129
- emitter.emit('error', response);
130
- console.error({
131
- status,
132
- response,
105
+ client.connect();
106
+ const messageChannel = client.channel(did, () => ({ token: token(), pk }));
107
+ const appPublicChannel = client.channel((0, channel_1.getAppPublicChannel)(did), () => ({ token: token(), pk }));
108
+ messageChannel
109
+ .join()
110
+ .receive('error', (err) => {
111
+ const msg = `join channel error: ${err.message}`;
112
+ console.error(msg);
113
+ emitError({ message: msg });
114
+ })
115
+ .receive('timeout', () => {
116
+ const msg = 'join channel timeout';
117
+ console.error(msg);
118
+ emitError({ message: msg });
133
119
  });
134
- }
135
- });
136
- }
120
+ messageChannel.on('message', ({ status, response } = {}) => {
121
+ if (status === 'ok') {
122
+ messageEmitter.emit(response.type, response);
123
+ if (response.type === notification_1.NOTIFICATION_TYPES.HI) {
124
+ emitter.emit(response.type, response);
125
+ }
126
+ }
127
+ else {
128
+ emitError(response);
129
+ console.error({
130
+ status,
131
+ response,
132
+ });
133
+ }
134
+ });
135
+ appPublicChannel
136
+ .join()
137
+ .receive('error', (err) => {
138
+ const msg = `join channel error: ${err.message}`;
139
+ console.error(msg);
140
+ emitter.emit('error', { message: msg });
141
+ })
142
+ .receive('timeout', () => {
143
+ const msg = 'join channel timeout';
144
+ console.error(msg);
145
+ emitter.emit('error', { message: msg });
146
+ });
147
+ appPublicChannel.on(notification_1.NOTIFICATION_TYPES.HI, ({ status, response } = {}) => {
148
+ if (status === 'ok') {
149
+ emitter.emit(notification_1.NOTIFICATION_TYPES.HI, response);
150
+ }
151
+ else {
152
+ emitter.emit('error', response);
153
+ console.error({
154
+ status,
155
+ response,
156
+ });
157
+ }
158
+ });
159
+ }
137
160
  };
138
-
139
161
  const ensureClient = () => {
140
- if (!client) {
141
- initClient();
142
- }
162
+ if (!client) {
163
+ initClient();
164
+ }
143
165
  };
144
-
145
- module.exports = {
146
- sendToUser: doSendToUser,
147
- broadcast,
148
-
149
- // System notification
150
- on: (event, cb) => {
166
+ const on = (event, cb) => {
151
167
  ensureClient();
152
168
  return emitter.on(event, cb);
153
- },
154
- off: emitter.off.bind(emitter),
155
-
156
- // Just for DID Wallet developing
157
- // Any message received from any account
158
- _message: {
169
+ };
170
+ exports.on = on;
171
+ exports.off = emitter.off.bind(emitter);
172
+ exports._message = {
159
173
  on: (event, cb) => {
160
- ensureClient();
161
- return messageEmitter.on(event, cb);
174
+ ensureClient();
175
+ return messageEmitter.on(event, cb);
162
176
  },
163
177
  off: messageEmitter.off.bind(messageEmitter),
164
- },
178
+ };
179
+ exports.default = {
180
+ sendToUser: doSendToUser,
181
+ broadcast,
182
+ on: exports.on,
183
+ off: exports.off,
184
+ _message: exports._message,
165
185
  };
@@ -0,0 +1,71 @@
1
+ export declare type TChannelEvent = string;
2
+ export interface TDataAsset {
3
+ chainHost: string;
4
+ did: unknown;
5
+ }
6
+ export interface TDataToken {
7
+ address?: unknown;
8
+ amount: string;
9
+ chainHost: string;
10
+ decimal: number;
11
+ senderDid: unknown;
12
+ symbol: string;
13
+ }
14
+ export interface TDataVC {
15
+ credential: {
16
+ /**
17
+ * Unknown Property
18
+ */
19
+ [x: string]: unknown;
20
+ };
21
+ tag?: string;
22
+ }
23
+ export interface TMessage {
24
+ /**
25
+ * Any Property
26
+ */
27
+ [x: string]: any;
28
+ createdAt: Date;
29
+ id: string;
30
+ receiver: {
31
+ did: unknown;
32
+ };
33
+ type: string;
34
+ }
35
+ export declare type TNotification = TNotificationItem | TNotificationConnect | TNotificationFeed;
36
+ export interface TNotificationAction {
37
+ bgColor?: string;
38
+ color?: string;
39
+ link?: string;
40
+ name: string;
41
+ title?: string;
42
+ }
43
+ export interface TNotificationAttachment {
44
+ data?: object;
45
+ type: 'asset' | 'vc' | 'token';
46
+ }
47
+ export interface TNotificationConnect {
48
+ checkUrl?: string;
49
+ type?: 'connect';
50
+ url: string;
51
+ }
52
+ export interface TNotificationFeed {
53
+ data: object;
54
+ feedType: string;
55
+ type?: 'feed';
56
+ }
57
+ export declare type TNotificationInput = TNotification[] | TNotification;
58
+ export interface TNotificationItem {
59
+ actions?: TNotificationAction[];
60
+ attachments?: TNotificationAttachment[];
61
+ body?: string;
62
+ title?: string;
63
+ type?: 'notification';
64
+ }
65
+ export interface TSendOptions {
66
+ /**
67
+ * Any Property
68
+ */
69
+ [x: string]: any;
70
+ keepForOfflineUser?: boolean;
71
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/indent */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ declare const checkBlockletEnvironment: () => void;
2
+ export = checkBlockletEnvironment;
@@ -1,20 +1,21 @@
1
- module.exports = () => {
2
- const props = [
3
- 'BLOCKLET_APP_ID',
4
- 'BLOCKLET_APP_NAME',
5
- 'BLOCKLET_APP_DESCRIPTION',
6
- 'BLOCKLET_APP_SK',
7
- 'BLOCKLET_DID',
8
- 'ABT_NODE', // abtnode version
9
- 'ABT_NODE_DID',
10
- 'ABT_NODE_PK',
11
- 'ABT_NODE_PORT', // abtnode daemon port
12
- 'ABT_NODE_SERVICE_PORT',
13
- ];
14
-
15
- props.forEach((prop) => {
16
- if (!process.env[prop]) {
17
- throw new Error(`${prop} does not exist in environments`);
18
- }
19
- });
1
+ "use strict";
2
+ const checkBlockletEnvironment = () => {
3
+ const envNames = [
4
+ 'BLOCKLET_APP_ID',
5
+ 'BLOCKLET_APP_NAME',
6
+ 'BLOCKLET_APP_DESCRIPTION',
7
+ 'BLOCKLET_APP_SK',
8
+ 'BLOCKLET_DID',
9
+ 'ABT_NODE',
10
+ 'ABT_NODE_DID',
11
+ 'ABT_NODE_PK',
12
+ 'ABT_NODE_PORT',
13
+ 'ABT_NODE_SERVICE_PORT',
14
+ ];
15
+ envNames.forEach((envName) => {
16
+ if (!process.env[envName]) {
17
+ throw new Error(`${envName} does not exist in environments`);
18
+ }
19
+ });
20
20
  };
21
+ module.exports = checkBlockletEnvironment;
@@ -0,0 +1,5 @@
1
+ export declare const SERVICE_PREFIX = "/.well-known/service";
2
+ declare const _default: {
3
+ SERVICE_PREFIX: string;
4
+ };
5
+ export default _default;
@@ -1,3 +1,7 @@
1
- module.exports = {
2
- SERVICE_PREFIX: '/.well-known/service',
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SERVICE_PREFIX = void 0;
4
+ exports.SERVICE_PREFIX = '/.well-known/service';
5
+ exports.default = {
6
+ SERVICE_PREFIX: exports.SERVICE_PREFIX,
3
7
  };
@@ -0,0 +1,25 @@
1
+ import { TNotificationInput, TSendOptions } from '../types/notification';
2
+ export declare type TNotificationSender = {
3
+ appDid: string;
4
+ appSk: string;
5
+ };
6
+ /**
7
+ * @param {String|Array} receiver
8
+ * @param {Object} notification
9
+ * @param {{
10
+ * appDid: String
11
+ * appSk: String
12
+ * }} sender
13
+ * @param {String|Number} port port of abtnode service endpoint
14
+ * @param {Object} options
15
+ * @returns
16
+ */
17
+ declare const sendToUser: (receiver: string | string[], notification: TNotificationInput, { appDid, appSk }: TNotificationSender, port?: string, options?: {}) => Promise<any>;
18
+ declare const sendToAppChannel: (channel: string, event: string, notification: TNotificationInput, { appDid, appSk }: TNotificationSender, port?: string, options?: TSendOptions) => Promise<any>;
19
+ export { sendToUser };
20
+ export { sendToAppChannel };
21
+ declare const _default: {
22
+ sendToUser: (receiver: string | string[], notification: TNotificationInput, { appDid, appSk }: TNotificationSender, port?: string, options?: {}) => Promise<any>;
23
+ sendToAppChannel: (channel: string, event: string, notification: TNotificationInput, { appDid, appSk }: TNotificationSender, port?: string, options?: TSendOptions) => Promise<any>;
24
+ };
25
+ export default _default;