@blocklet/sdk 1.15.17 → 1.16.0-beta-b16cb035
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/README.md +1 -198
- package/lib/component/index.d.ts +35 -0
- package/lib/component/index.js +95 -0
- package/lib/config.d.ts +33 -0
- package/lib/config.js +17 -0
- package/lib/connect/authenticator.d.ts +5 -0
- package/lib/connect/authenticator.js +18 -0
- package/lib/connect/handler.d.ts +2 -0
- package/lib/connect/handler.js +90 -0
- package/lib/connect/shared.d.ts +19 -0
- package/lib/connect/shared.js +81 -0
- package/lib/database/index.d.ts +13 -0
- package/lib/database/index.js +46 -0
- package/lib/embed/adapters/express.d.ts +3 -0
- package/lib/embed/adapters/express.js +13 -0
- package/lib/embed/generate.d.ts +6 -0
- package/lib/embed/generate.js +30 -0
- package/lib/embed/get-embed-url.d.ts +1 -0
- package/lib/embed/get-embed-url.js +55 -0
- package/lib/embed/get-embed.d.ts +1 -0
- package/lib/embed/get-embed.js +32 -0
- package/lib/embed/index.d.ts +10 -0
- package/lib/embed/index.js +13 -0
- package/lib/embed/message.d.ts +26 -0
- package/lib/embed/message.js +134 -0
- package/lib/env.d.ts +17 -0
- package/lib/env.js +3 -0
- package/lib/error-handler.d.ts +0 -0
- package/lib/error-handler.js +5 -5
- package/lib/index.d.ts +53 -0
- package/lib/index.js +49 -12
- package/lib/middlewares/auth.d.ts +8 -0
- package/lib/middlewares/auth.js +54 -0
- package/lib/middlewares/component.d.ts +7 -0
- package/lib/middlewares/component.js +32 -0
- package/lib/middlewares/index.d.ts +24 -0
- package/lib/middlewares/index.js +17 -0
- package/lib/middlewares/user.d.ts +10 -0
- package/lib/middlewares/user.js +12 -0
- package/lib/security/index.d.ts +9 -0
- package/lib/security/index.js +27 -0
- package/lib/service/auth.d.ts +31 -0
- package/lib/service/auth.js +129 -136
- package/lib/service/notification.d.ts +47 -0
- package/lib/service/notification.js +182 -18
- package/lib/types/notification.d.ts +113 -0
- package/lib/types/notification.js +3 -0
- package/lib/util/check-blocklet-env.d.ts +2 -0
- package/lib/util/check-blocklet-env.js +20 -17
- package/lib/util/constants.d.ts +5 -0
- package/lib/util/constants.js +6 -2
- package/lib/util/env.d.ts +19 -0
- package/lib/util/env.js +33 -0
- package/lib/util/send-notification.d.ts +26 -0
- package/lib/util/send-notification.js +149 -38
- package/lib/validators/index.d.ts +12 -0
- package/lib/validators/index.js +13 -5
- package/lib/validators/notification.d.ts +74 -0
- package/lib/validators/notification.js +227 -69
- package/lib/version.d.ts +6 -0
- package/lib/version.js +6 -0
- package/lib/wallet-authenticator.d.ts +5 -0
- package/lib/wallet-authenticator.js +16 -51
- package/lib/wallet-handler.d.ts +19 -0
- package/lib/wallet-handler.js +109 -87
- package/lib/wallet.d.ts +3 -0
- package/lib/wallet.js +10 -0
- package/package.json +52 -19
- package/lib/database.js +0 -98
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.decrypt = exports.encrypt = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const aes_legacy_1 = __importDefault(require("@ocap/mcrypto/lib/crypter/aes-legacy"));
|
|
9
|
+
const AES = { default: aes_legacy_1.default }.default;
|
|
10
|
+
const encrypt = (message) => {
|
|
11
|
+
if (!process.env.BLOCKLET_DID || !process.env.BLOCKLET_APP_EK) {
|
|
12
|
+
return message;
|
|
13
|
+
}
|
|
14
|
+
return (AES.encrypt(message, crypto_1.default.pbkdf2Sync(process.env.BLOCKLET_APP_EK, process.env.BLOCKLET_DID, 256, 32, 'sha512').toString('hex')));
|
|
15
|
+
};
|
|
16
|
+
exports.encrypt = encrypt;
|
|
17
|
+
const decrypt = (message) => {
|
|
18
|
+
if (!process.env.BLOCKLET_DID || !process.env.BLOCKLET_APP_EK) {
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
21
|
+
return (AES.decrypt(message, crypto_1.default.pbkdf2Sync(process.env.BLOCKLET_APP_EK, process.env.BLOCKLET_DID, 256, 32, 'sha512').toString('hex')));
|
|
22
|
+
};
|
|
23
|
+
exports.decrypt = decrypt;
|
|
24
|
+
exports.default = {
|
|
25
|
+
encrypt,
|
|
26
|
+
decrypt,
|
|
27
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import Client from '@abtnode/client';
|
|
2
|
+
type PartialDeep<T> = {
|
|
3
|
+
[K in keyof T]?: T[K] extends object ? PartialDeep<T[K]> : T[K];
|
|
4
|
+
};
|
|
5
|
+
type OmitTeamDid<T> = PartialDeep<Omit<T, 'teamDid'>>;
|
|
6
|
+
declare class AuthService {
|
|
7
|
+
constructor(httpEndpoint?: string);
|
|
8
|
+
}
|
|
9
|
+
interface AuthService {
|
|
10
|
+
getUser(did: string): Promise<Client.ResponseUser>;
|
|
11
|
+
getUsers(args?: OmitTeamDid<Client.RequestUsersInput>): Promise<Client.ResponseUsers>;
|
|
12
|
+
getOwner(): Promise<Client.ResponseUser>;
|
|
13
|
+
updateUserApproval(did: string, approved: boolean): Promise<Client.ResponseUser>;
|
|
14
|
+
issuePassportToUser(args: OmitTeamDid<Client.RequestIssuePassportToUserInput>): Promise<Client.ResponseUser>;
|
|
15
|
+
enableUserPassport(args: OmitTeamDid<Client.RequestRevokeUserPassportInput>): Promise<Client.ResponseUser>;
|
|
16
|
+
revokeUserPassport(args: OmitTeamDid<Client.RequestRevokeUserPassportInput>): Promise<Client.ResponseUser>;
|
|
17
|
+
getPermissionsByRole(role: string): Promise<Client.ResponsePermissions>;
|
|
18
|
+
getRoles(): Promise<Client.ResponseRoles>;
|
|
19
|
+
createRole(args: OmitTeamDid<Client.RequestCreateRoleInput>): Promise<Client.ResponseRole>;
|
|
20
|
+
updateRole(name: string, updates: Pick<Client.RoleInput, 'title' | 'description'>): Promise<Client.ResponseRole>;
|
|
21
|
+
deleteRole(name: string): Promise<Client.GeneralResponse>;
|
|
22
|
+
grantPermissionForRole(role: string, permission: string): Promise<Client.GeneralResponse>;
|
|
23
|
+
revokePermissionFromRole(role: string, permission: string): Promise<Client.GeneralResponse>;
|
|
24
|
+
updatePermissionsForRole(role: string, permissions: string): Promise<Client.ResponseRole>;
|
|
25
|
+
hasPermission(role: string, permission: string): Promise<Client.BooleanResponse>;
|
|
26
|
+
getPermissions(): Promise<Client.ResponsePermissions>;
|
|
27
|
+
createPermission(args: OmitTeamDid<Client.RequestCreatePermissionInput>): Promise<Client.ResponsePermission>;
|
|
28
|
+
updatePermission(args: OmitTeamDid<Client.PermissionInput>): Promise<Client.ResponsePermission>;
|
|
29
|
+
deletePermission(name: string): Promise<Client.GeneralResponse>;
|
|
30
|
+
}
|
|
31
|
+
export = AuthService;
|
package/lib/service/auth.js
CHANGED
|
@@ -1,142 +1,135 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
/* eslint-disable max-classes-per-file */
|
|
6
|
+
/* eslint-disable @typescript-eslint/indent */
|
|
7
|
+
const pickBy_1 = __importDefault(require("lodash/pickBy"));
|
|
8
|
+
const url_join_1 = __importDefault(require("url-join"));
|
|
9
|
+
const client_1 = __importDefault(require("@abtnode/client"));
|
|
10
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
|
11
|
+
const wallet_1 = require("@ocap/wallet");
|
|
12
|
+
const constant_1 = __importDefault(require("@abtnode/constant"));
|
|
13
|
+
const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env"));
|
|
14
|
+
const version_1 = require("../version");
|
|
15
|
+
const { NODE_SERVICES, WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_URL_PREFIX, USER_AVATAR_PATH_PREFIX } = constant_1.default;
|
|
16
|
+
const VERSION = version_1.version; // version of notification sdk
|
|
17
|
+
const isNotNullOrUndefined = (x) => ![null, undefined].includes(x);
|
|
18
|
+
const type = process.env.BLOCKLET_WALLET_TYPE !== 'eth'
|
|
19
|
+
? (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 })
|
|
20
|
+
: 'eth';
|
|
21
|
+
const fixAvatar = (user) => {
|
|
22
|
+
const avatar = user?.avatar;
|
|
23
|
+
if (avatar && avatar.startsWith(USER_AVATAR_URL_PREFIX)) {
|
|
24
|
+
user.avatar = (0, url_join_1.default)(WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_PATH_PREFIX, avatar.replace(USER_AVATAR_URL_PREFIX, ''));
|
|
25
|
+
}
|
|
26
|
+
return user;
|
|
27
|
+
};
|
|
28
|
+
class AuthClient extends client_1.default {
|
|
29
|
+
constructor(httpEndpoint) {
|
|
30
|
+
(0, check_blocklet_env_1.default)();
|
|
31
|
+
super(httpEndpoint || `http://127.0.0.1:${process.env.ABT_NODE_PORT}/api/gql`.trim(), `BlockletSDK/${VERSION}`);
|
|
32
|
+
this.setAuthAccessKey({
|
|
33
|
+
accessKeyId: process.env.BLOCKLET_APP_ID,
|
|
34
|
+
accessKeySecret: process.env.BLOCKLET_APP_SK,
|
|
35
|
+
type,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
_getAuthHeaders() {
|
|
39
|
+
const headers = super._getAuthHeaders();
|
|
40
|
+
// BLOCKLET_DID is always same as BLOCKLET_APP_PID in structV2 application
|
|
41
|
+
headers['x-access-blocklet'] = process.env.BLOCKLET_DID;
|
|
42
|
+
// x-access-service is not used since 1.8.35
|
|
43
|
+
headers['x-access-service'] = NODE_SERVICES.AUTH_SERVICE;
|
|
44
|
+
return headers;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
9
47
|
// Auth 相关的功能 API 都在这里
|
|
10
48
|
// core/state/lib/api/team.js L42
|
|
11
|
-
|
|
12
49
|
// 所有可配置调用的函数在这里,如果需要额外增加,则需要在这里新增对应的函数
|
|
13
50
|
// core/gql/lib/config.js L25
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
(fn) =>
|
|
98
|
-
(name, { title, description }) =>
|
|
99
|
-
fn({ input: { role: pickBy({ name, title, description }, isNotNullOrUndefined), teamDid } }), // todo
|
|
100
|
-
deleteRole: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
101
|
-
grantPermissionForRole: (fn) => (roleName, permissionName) =>
|
|
102
|
-
fn({ input: { teamDid, roleName, grantName: permissionName } }),
|
|
103
|
-
revokePermissionFromRole: (fn) => (roleName, permissionName) =>
|
|
104
|
-
fn({ input: { teamDid, roleName, grantName: permissionName } }),
|
|
105
|
-
updatePermissionsForRole: (fn) => (roleName, permissionNames) =>
|
|
106
|
-
fn({ input: { teamDid, roleName, grantNames: permissionNames } }),
|
|
107
|
-
createPermission:
|
|
108
|
-
(fn) =>
|
|
109
|
-
({ name, description }) =>
|
|
110
|
-
fn({ input: { ...pickBy({ name, description }, isNotNullOrUndefined), teamDid } }),
|
|
111
|
-
updatePermission:
|
|
112
|
-
(fn) =>
|
|
113
|
-
(name, { description }) =>
|
|
114
|
-
fn({ input: { permission: pickBy({ name, description }, isNotNullOrUndefined), teamDid } }), // todo
|
|
115
|
-
deletePermission: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
116
|
-
hasPermission: (fn) => (role, permission) => fn({ input: { teamDid, role, permission } }),
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
apiList.forEach((api) => {
|
|
120
|
-
const fn = this[api];
|
|
121
|
-
this[api] = apiFnMap[api] ? apiFnMap[api](fn) : apiFallback(fn);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
return new Proxy(this, {
|
|
125
|
-
get(target, propKey) {
|
|
126
|
-
if (!apiList.includes(propKey)) {
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
return target[propKey];
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
_getAuthHeaders() {
|
|
135
|
-
const headers = super._getAuthHeaders();
|
|
136
|
-
headers['x-access-blocklet'] = this._blockletDid;
|
|
137
|
-
headers['x-access-service'] = NODE_SERVICES.AUTH_SERVICE;
|
|
138
|
-
return headers;
|
|
139
|
-
}
|
|
51
|
+
class AuthService {
|
|
52
|
+
constructor(httpEndpoint) {
|
|
53
|
+
const client = new AuthClient(httpEndpoint);
|
|
54
|
+
const apiList = [
|
|
55
|
+
// user
|
|
56
|
+
'getUsers',
|
|
57
|
+
'getUser',
|
|
58
|
+
'getOwner',
|
|
59
|
+
// 'removeUser',
|
|
60
|
+
// 'updateUserRole',
|
|
61
|
+
'updateUserApproval',
|
|
62
|
+
// invitation
|
|
63
|
+
// 'getInvitations',
|
|
64
|
+
// 'createMemberInvitation',
|
|
65
|
+
// 'deleteInvitation',
|
|
66
|
+
// rbac
|
|
67
|
+
'getRoles',
|
|
68
|
+
'createRole',
|
|
69
|
+
'updateRole',
|
|
70
|
+
'deleteRole',
|
|
71
|
+
'grantPermissionForRole',
|
|
72
|
+
'revokePermissionFromRole',
|
|
73
|
+
'updatePermissionsForRole',
|
|
74
|
+
'hasPermission',
|
|
75
|
+
// user passport
|
|
76
|
+
'issuePassportToUser',
|
|
77
|
+
'revokeUserPassport',
|
|
78
|
+
'enableUserPassport',
|
|
79
|
+
// 'getPassportIssuances',
|
|
80
|
+
// 'createPassportIssuance',
|
|
81
|
+
// 'deletePassportIssuance',
|
|
82
|
+
// permission
|
|
83
|
+
'getPermissions',
|
|
84
|
+
'createPermission',
|
|
85
|
+
'updatePermission',
|
|
86
|
+
'deletePermission',
|
|
87
|
+
'getPermissionsByRole',
|
|
88
|
+
// disabled in current
|
|
89
|
+
// 'configTrustedPassports',
|
|
90
|
+
];
|
|
91
|
+
const teamDid = process.env.BLOCKLET_DID;
|
|
92
|
+
const apiFallback = (fn) => (params = {}, ...args) => fn({ input: { ...params, teamDid }, ...args });
|
|
93
|
+
// const apiConvertDid = (fn: Function) => (did: string) => fn({ input: { user: { did }, teamDid } });
|
|
94
|
+
const apiFnMap = {
|
|
95
|
+
getUser: (fn) => async (did) => {
|
|
96
|
+
const res = await fn({ input: { user: { did }, teamDid } });
|
|
97
|
+
fixAvatar(res.user);
|
|
98
|
+
return res;
|
|
99
|
+
},
|
|
100
|
+
getUsers: (fn) => async (args) => {
|
|
101
|
+
const res = await fn({ input: { teamDid, ...args } });
|
|
102
|
+
(res.users || []).forEach(fixAvatar);
|
|
103
|
+
return res;
|
|
104
|
+
},
|
|
105
|
+
// removeUser: apiConvertDid,
|
|
106
|
+
// updateUserRole: (fn: Function) => (did: string, role: string) => fn({ input: { user: { did, role }, teamDid } }),
|
|
107
|
+
updateUserApproval: (fn) => (did, approved) => fn({ input: { user: { did, approved }, teamDid } }),
|
|
108
|
+
getPermissionsByRole: (fn) => (name) => fn({ input: { role: { name }, teamDid } }),
|
|
109
|
+
createRole: (fn) => ({ name, title, description }) => fn({ input: { ...(0, pickBy_1.default)({ name, title, description }, isNotNullOrUndefined), teamDid } }),
|
|
110
|
+
updateRole: (fn) => (name, { title, description }) => fn({ input: { role: (0, pickBy_1.default)({ name, title, description }, isNotNullOrUndefined), teamDid } }),
|
|
111
|
+
deleteRole: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
112
|
+
grantPermissionForRole: (fn) => (roleName, permissionName) => fn({ input: { teamDid, roleName, grantName: permissionName } }),
|
|
113
|
+
revokePermissionFromRole: (fn) => (roleName, permissionName) => fn({ input: { teamDid, roleName, grantName: permissionName } }),
|
|
114
|
+
updatePermissionsForRole: (fn) => (roleName, permissionNames) => fn({ input: { teamDid, roleName, grantNames: permissionNames } }),
|
|
115
|
+
createPermission: (fn) => ({ name, description }) => fn({ input: { ...(0, pickBy_1.default)({ name, description }, isNotNullOrUndefined), teamDid } }),
|
|
116
|
+
updatePermission: (fn) => (name, { description }) => fn({ input: { permission: (0, pickBy_1.default)({ name, description }, isNotNullOrUndefined), teamDid } }),
|
|
117
|
+
deletePermission: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
118
|
+
hasPermission: (fn) => (role, permission) => fn({ input: { teamDid, role, permission } }),
|
|
119
|
+
};
|
|
120
|
+
apiList.forEach((api) => {
|
|
121
|
+
const fn = client[api];
|
|
122
|
+
this[api] = apiFnMap[api] ? apiFnMap[api](fn) : apiFallback(fn);
|
|
123
|
+
});
|
|
124
|
+
// eslint-disable-next-line no-constructor-return
|
|
125
|
+
return new Proxy(this, {
|
|
126
|
+
get(target, propKey) {
|
|
127
|
+
if (!apiList.includes(propKey)) {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
return target[propKey];
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
140
134
|
}
|
|
141
|
-
|
|
142
135
|
module.exports = AuthService;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
import { TNotificationInput, TSendOptions } from '../types/notification';
|
|
4
|
+
type $TSFixMe = any;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {Notification} notification
|
|
8
|
+
* @param {{
|
|
9
|
+
* keepForOfflineUser: Boolean
|
|
10
|
+
* }} options
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
declare const doSendToUser: (receiver: string | string[], notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
14
|
+
declare const doSendToRelay: (topic: string, event: string, data: any) => Promise<any>;
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param {Notification} notification
|
|
18
|
+
* @param {{
|
|
19
|
+
* channel: String
|
|
20
|
+
* event: String
|
|
21
|
+
* socketId: String
|
|
22
|
+
* socketDid: String
|
|
23
|
+
* }} options
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
declare const broadcast: (notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
27
|
+
export declare const on: (event: string, cb?: $TSFixMe) => EventEmitter;
|
|
28
|
+
export declare const off: any;
|
|
29
|
+
export declare const _message: {
|
|
30
|
+
on: (event: string, cb: $TSFixMe) => EventEmitter;
|
|
31
|
+
off: any;
|
|
32
|
+
};
|
|
33
|
+
export { doSendToUser as sendToUser };
|
|
34
|
+
export { doSendToRelay as sendToRelay };
|
|
35
|
+
export { broadcast };
|
|
36
|
+
declare const _default: {
|
|
37
|
+
sendToUser: (receiver: string | string[], notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
38
|
+
sendToRelay: (topic: string, event: string, data: any) => Promise<any>;
|
|
39
|
+
broadcast: (notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
40
|
+
on: (event: string, cb?: any) => EventEmitter;
|
|
41
|
+
off: any;
|
|
42
|
+
_message: {
|
|
43
|
+
on: (event: string, cb: any) => EventEmitter;
|
|
44
|
+
off: any;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export default _default;
|
|
@@ -1,18 +1,182 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.broadcast = exports.sendToRelay = exports.sendToUser = exports._message = exports.off = exports.on = void 0;
|
|
30
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
31
|
+
const Jwt = __importStar(require("@arcblock/jwt"));
|
|
32
|
+
const events_1 = __importDefault(require("events"));
|
|
33
|
+
const ws_1 = require("@arcblock/ws");
|
|
34
|
+
const channel_1 = require("@blocklet/meta/lib/channel");
|
|
35
|
+
const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env"));
|
|
36
|
+
const send_notification_1 = require("../util/send-notification");
|
|
37
|
+
const constants_1 = require("../util/constants");
|
|
38
|
+
const wallet_1 = __importDefault(require("../wallet"));
|
|
39
|
+
const notification_1 = require("../validators/notification");
|
|
40
|
+
const getSender = () => ({
|
|
41
|
+
appDid: process.env.BLOCKLET_APP_ID,
|
|
42
|
+
appSk: process.env.BLOCKLET_APP_SK,
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
* @param {Notification} notification
|
|
47
|
+
* @param {{
|
|
48
|
+
* keepForOfflineUser: Boolean
|
|
49
|
+
* }} options
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
const doSendToUser = async (receiver, notification, options) => {
|
|
53
|
+
(0, check_blocklet_env_1.default)();
|
|
54
|
+
return (0, send_notification_1.sendToUser)(receiver, notification, getSender(), process.env.ABT_NODE_SERVICE_PORT, options);
|
|
55
|
+
};
|
|
56
|
+
exports.sendToUser = doSendToUser;
|
|
57
|
+
const doSendToRelay = async (topic, event, data) => {
|
|
58
|
+
(0, check_blocklet_env_1.default)();
|
|
59
|
+
return (0, send_notification_1.sendToRelay)(topic, event, data, getSender(), process.env.ABT_NODE_SERVICE_PORT);
|
|
60
|
+
};
|
|
61
|
+
exports.sendToRelay = doSendToRelay;
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* @param {Notification} notification
|
|
65
|
+
* @param {{
|
|
66
|
+
* channel: String
|
|
67
|
+
* event: String
|
|
68
|
+
* socketId: String
|
|
69
|
+
* socketDid: String
|
|
70
|
+
* }} options
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
const broadcast = async (notification, options = {}) => {
|
|
74
|
+
(0, check_blocklet_env_1.default)();
|
|
75
|
+
const sender = getSender();
|
|
76
|
+
const { channel = (0, channel_1.getAppPublicChannel)(sender.appDid) } = options;
|
|
77
|
+
const { event = 'message' } = options;
|
|
78
|
+
return (0, send_notification_1.sendToAppChannel)(channel, event, notification, sender, process.env.ABT_NODE_SERVICE_PORT, options);
|
|
79
|
+
};
|
|
80
|
+
exports.broadcast = broadcast;
|
|
81
|
+
const emitter = new events_1.default();
|
|
82
|
+
const messageEmitter = new events_1.default();
|
|
83
|
+
const emitError = (error) => {
|
|
84
|
+
messageEmitter.emit('error', error);
|
|
85
|
+
emitter.emit('error', error);
|
|
86
|
+
};
|
|
87
|
+
let client = null;
|
|
88
|
+
const initClient = () => {
|
|
89
|
+
if (!client) {
|
|
90
|
+
const wallet = (0, wallet_1.default)();
|
|
91
|
+
const { address: did, publicKey: pk, secretKey: sk } = wallet;
|
|
92
|
+
const url = `ws://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}${constants_1.SERVICE_PREFIX}`;
|
|
93
|
+
const token = () => Jwt.sign(did, sk, {});
|
|
94
|
+
client = new ws_1.WsClient(url, {
|
|
95
|
+
heartbeatIntervalMs: 10 * 1000,
|
|
96
|
+
params: () => ({
|
|
97
|
+
token: token(),
|
|
98
|
+
pk,
|
|
99
|
+
}),
|
|
100
|
+
});
|
|
101
|
+
client.connect();
|
|
102
|
+
const messageChannel = client.channel(did, () => ({ token: token(), pk }));
|
|
103
|
+
const appPublicChannel = client.channel((0, channel_1.getAppPublicChannel)(did), () => ({ token: token(), pk }));
|
|
104
|
+
messageChannel
|
|
105
|
+
.join()
|
|
106
|
+
.receive('error', (err) => {
|
|
107
|
+
const msg = `join channel error: ${err.message}`;
|
|
108
|
+
console.error(msg);
|
|
109
|
+
emitError({ message: msg });
|
|
110
|
+
})
|
|
111
|
+
.receive('timeout', () => {
|
|
112
|
+
const msg = 'join channel timeout';
|
|
113
|
+
console.error(msg);
|
|
114
|
+
emitError({ message: msg });
|
|
115
|
+
});
|
|
116
|
+
messageChannel.on('message', ({ status, response } = {}) => {
|
|
117
|
+
if (status === 'ok') {
|
|
118
|
+
messageEmitter.emit(response.type, response);
|
|
119
|
+
if (response.type === notification_1.NOTIFICATION_TYPES.HI) {
|
|
120
|
+
emitter.emit(response.type, response);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
emitError(response);
|
|
125
|
+
console.error({
|
|
126
|
+
status,
|
|
127
|
+
response,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
appPublicChannel
|
|
132
|
+
.join()
|
|
133
|
+
.receive('error', (err) => {
|
|
134
|
+
const msg = `join channel error: ${err.message}`;
|
|
135
|
+
console.error(msg);
|
|
136
|
+
emitter.emit('error', { message: msg });
|
|
137
|
+
})
|
|
138
|
+
.receive('timeout', () => {
|
|
139
|
+
const msg = 'join channel timeout';
|
|
140
|
+
console.error(msg);
|
|
141
|
+
emitter.emit('error', { message: msg });
|
|
142
|
+
});
|
|
143
|
+
appPublicChannel.on(notification_1.NOTIFICATION_TYPES.HI, ({ status, response } = {}) => {
|
|
144
|
+
if (status === 'ok') {
|
|
145
|
+
emitter.emit(notification_1.NOTIFICATION_TYPES.HI, response);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
emitter.emit('error', response);
|
|
149
|
+
console.error({
|
|
150
|
+
status,
|
|
151
|
+
response,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const ensureClient = () => {
|
|
158
|
+
if (!client) {
|
|
159
|
+
initClient();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const on = (event, cb) => {
|
|
163
|
+
ensureClient();
|
|
164
|
+
return emitter.on(event, cb);
|
|
165
|
+
};
|
|
166
|
+
exports.on = on;
|
|
167
|
+
exports.off = emitter.off.bind(emitter);
|
|
168
|
+
exports._message = {
|
|
169
|
+
on: (event, cb) => {
|
|
170
|
+
ensureClient();
|
|
171
|
+
return messageEmitter.on(event, cb);
|
|
172
|
+
},
|
|
173
|
+
off: messageEmitter.off.bind(messageEmitter),
|
|
174
|
+
};
|
|
175
|
+
exports.default = {
|
|
176
|
+
sendToUser: doSendToUser,
|
|
177
|
+
sendToRelay: doSendToRelay,
|
|
178
|
+
broadcast,
|
|
179
|
+
on: exports.on,
|
|
180
|
+
off: exports.off,
|
|
181
|
+
_message: exports._message,
|
|
182
|
+
};
|