@abtnode/core 1.16.8-beta-186fd5aa → 1.16.8-next-c66e39c7
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/api/team.js +42 -62
- package/lib/blocklet/manager/disk.js +2 -8
- package/lib/blocklet/manager/helper/migrate-application-to-struct-v2.js +5 -5
- package/lib/blocklet/storage/backup/blocklet-extras.js +2 -2
- package/lib/blocklet/storage/backup/blocklet.js +2 -2
- package/lib/index.js +17 -16
- package/lib/migrations/1.16.8-component-title.js +1 -1
- package/lib/migrations/1.6.9-update-node-info-and-certificate.js +1 -1
- package/lib/migrations/index.js +190 -40
- package/lib/monitor/node-runtime-monitor.js +2 -29
- package/lib/router/helper.js +6 -6
- package/lib/router/manager.js +35 -36
- package/lib/states/access-key.js +3 -20
- package/lib/states/audit-log.js +7 -8
- package/lib/states/backup.js +11 -59
- package/lib/states/base.js +13 -5
- package/lib/states/blocklet-extras.js +11 -8
- package/lib/states/blocklet.js +148 -222
- package/lib/states/cache.js +3 -21
- package/lib/states/connect-account.js +8 -0
- package/lib/states/index.js +28 -18
- package/lib/states/job.js +8 -0
- package/lib/states/migration.js +3 -4
- package/lib/states/node.js +104 -145
- package/lib/states/notification.js +18 -40
- package/lib/states/passport.js +8 -0
- package/lib/states/session.js +28 -44
- package/lib/states/site.js +32 -39
- package/lib/states/user.js +169 -378
- package/lib/states/webhook.js +5 -7
- package/lib/team/manager.js +108 -116
- package/lib/util/blocklet.js +0 -1
- package/lib/util/index.js +3 -0
- package/lib/util/queue.js +14 -20
- package/lib/util/ready.js +1 -1
- package/lib/webhook/index.js +6 -4
- package/package.json +19 -18
- package/lib/states/challenge.js +0 -58
package/lib/states/node.js
CHANGED
|
@@ -4,22 +4,25 @@ const omit = require('lodash/omit');
|
|
|
4
4
|
const isEmpty = require('lodash/isEmpty');
|
|
5
5
|
const security = require('@abtnode/util/lib/security');
|
|
6
6
|
const { isFromPublicKey } = require('@arcblock/did');
|
|
7
|
-
const logger = require('@abtnode/logger')('@abtnode/core:node');
|
|
8
7
|
const { NODE_MODES, DISK_ALERT_THRESHOLD_PERCENT, EVENTS, SERVER_STATUS } = require('@abtnode/constant');
|
|
8
|
+
// const logger = require('@abtnode/logger')('@abtnode/core:node');
|
|
9
9
|
|
|
10
10
|
const BaseState = require('./base');
|
|
11
11
|
const { validateOwner } = require('../util');
|
|
12
12
|
const { get: getDefaultConfigs } = require('../util/default-node-config');
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @extends BaseState<import('@abtnode/models').ServerState>
|
|
16
|
+
*/
|
|
14
17
|
class NodeState extends BaseState {
|
|
15
18
|
/**
|
|
16
19
|
* Creates an instance of NodeState.
|
|
17
|
-
* @param {
|
|
20
|
+
* @param {object} model
|
|
18
21
|
* @param {object} config
|
|
19
22
|
* @memberof NodeState
|
|
20
23
|
*/
|
|
21
|
-
constructor(
|
|
22
|
-
super(
|
|
24
|
+
constructor(model, config = {}, dataDirs = {}, notification) {
|
|
25
|
+
super(model, config);
|
|
23
26
|
|
|
24
27
|
// Initialize the store
|
|
25
28
|
if (!config.nodeSk) {
|
|
@@ -37,14 +40,6 @@ class NodeState extends BaseState {
|
|
|
37
40
|
|
|
38
41
|
this.dataDirs = dataDirs;
|
|
39
42
|
this.notification = notification;
|
|
40
|
-
|
|
41
|
-
this.onReady(() => {
|
|
42
|
-
this.ensureIndex({ fieldName: 'did', unique: true }, (err) => {
|
|
43
|
-
if (err) {
|
|
44
|
-
console.error('Failed to ensure unique index', err);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
43
|
}
|
|
49
44
|
|
|
50
45
|
isInitialized(doc) {
|
|
@@ -60,113 +55,89 @@ class NodeState extends BaseState {
|
|
|
60
55
|
* @returns {object} Node document json
|
|
61
56
|
* @memberof NodeState
|
|
62
57
|
*/
|
|
63
|
-
read() {
|
|
64
|
-
|
|
65
|
-
const { nodeDid, dek } = this.config;
|
|
66
|
-
this.findOne({ did: nodeDid }, (err, record) => {
|
|
67
|
-
if (err) {
|
|
68
|
-
// eslint-disable-next-line no-console
|
|
69
|
-
console.error(err);
|
|
70
|
-
return reject(err);
|
|
71
|
-
}
|
|
58
|
+
async read() {
|
|
59
|
+
const { nodeDid, dek } = this.config;
|
|
72
60
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
61
|
+
let doc = await this.findOne({ did: nodeDid });
|
|
62
|
+
if (doc) {
|
|
63
|
+
if (dek) {
|
|
64
|
+
doc.sk = security.decrypt(doc.sk, doc.did, dek);
|
|
65
|
+
}
|
|
77
66
|
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
return doc;
|
|
68
|
+
}
|
|
80
69
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
70
|
+
const {
|
|
71
|
+
name,
|
|
72
|
+
description,
|
|
73
|
+
nodeSk,
|
|
74
|
+
nodePk,
|
|
75
|
+
nodeOwner,
|
|
76
|
+
port,
|
|
77
|
+
version,
|
|
78
|
+
routing = { provider: 'default' },
|
|
79
|
+
docker,
|
|
80
|
+
mode,
|
|
81
|
+
runtimeConfig,
|
|
82
|
+
ownerNft,
|
|
83
|
+
launcher,
|
|
84
|
+
didRegistry,
|
|
85
|
+
didDomain,
|
|
86
|
+
enablePassportIssuance = true,
|
|
87
|
+
trustedPassports = [],
|
|
88
|
+
webWalletUrl,
|
|
89
|
+
} = this.config;
|
|
90
|
+
|
|
91
|
+
if (nodeOwner && !validateOwner(nodeOwner)) {
|
|
92
|
+
throw new Error('Node owner is invalid');
|
|
93
|
+
}
|
|
105
94
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
customBlockletNumber: 0,
|
|
138
|
-
webWalletUrl,
|
|
139
|
-
},
|
|
140
|
-
async (e, data) => {
|
|
141
|
-
if (e) {
|
|
142
|
-
logger.error('create error', { error: e });
|
|
143
|
-
if (e.message.indexOf('violates the unique constraint') > 0) {
|
|
144
|
-
return resolve(await this.read());
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return reject(e);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
logger.info('create', { data });
|
|
151
|
-
if (dek) {
|
|
152
|
-
data.sk = security.decrypt(data.sk, data.did, dek);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return resolve(data);
|
|
156
|
-
}
|
|
157
|
-
)
|
|
158
|
-
) // eslint-disable-line
|
|
159
|
-
.catch(reject);
|
|
160
|
-
});
|
|
95
|
+
const initialized = this.isInitialized({ nodeOwner, enablePassportIssuance, trustedPassports });
|
|
96
|
+
const defaultConfigs = await getDefaultConfigs();
|
|
97
|
+
|
|
98
|
+
doc = await this.insert({
|
|
99
|
+
...(defaultConfigs || {}),
|
|
100
|
+
name,
|
|
101
|
+
description,
|
|
102
|
+
pk: nodePk,
|
|
103
|
+
sk: dek ? security.encrypt(nodeSk, nodeDid, dek) : nodeSk,
|
|
104
|
+
did: nodeDid,
|
|
105
|
+
initialized,
|
|
106
|
+
version,
|
|
107
|
+
nodeOwner: nodeOwner || null,
|
|
108
|
+
port,
|
|
109
|
+
initializedAt: initialized ? new Date() : null,
|
|
110
|
+
startedAt: null,
|
|
111
|
+
routing,
|
|
112
|
+
docker,
|
|
113
|
+
mode,
|
|
114
|
+
enableWelcomePage: mode !== NODE_MODES.SERVERLESS,
|
|
115
|
+
enableBetaRelease: false,
|
|
116
|
+
runtimeConfig,
|
|
117
|
+
ownerNft,
|
|
118
|
+
diskAlertThreshold: DISK_ALERT_THRESHOLD_PERCENT,
|
|
119
|
+
launcher: launcher || null,
|
|
120
|
+
didRegistry,
|
|
121
|
+
didDomain,
|
|
122
|
+
enablePassportIssuance,
|
|
123
|
+
trustedPassports,
|
|
124
|
+
customBlockletNumber: 0,
|
|
125
|
+
webWalletUrl,
|
|
161
126
|
});
|
|
127
|
+
|
|
128
|
+
if (dek) {
|
|
129
|
+
doc.sk = security.decrypt(doc.sk, doc.did, dek);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return doc;
|
|
162
133
|
}
|
|
163
134
|
|
|
164
135
|
// FIXME: 这个接口比较危险,可能会修改一些本不应该修改的字段,后续需要考虑改进
|
|
165
136
|
async updateNodeInfo(entity = {}) {
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
this.emit(EVENTS.NODE_UPDATED,
|
|
169
|
-
return
|
|
137
|
+
const old = await this.read();
|
|
138
|
+
const doc = await this.update({ $set: omit(entity, ['ownerNft', 'sk']) });
|
|
139
|
+
this.emit(EVENTS.NODE_UPDATED, doc, old);
|
|
140
|
+
return doc;
|
|
170
141
|
}
|
|
171
142
|
|
|
172
143
|
async updateNodeRouting(entity = {}) {
|
|
@@ -191,27 +162,26 @@ class NodeState extends BaseState {
|
|
|
191
162
|
updates.previousMode = '';
|
|
192
163
|
}
|
|
193
164
|
|
|
194
|
-
return this.update(
|
|
165
|
+
return this.update({ $set: updates });
|
|
195
166
|
}
|
|
196
167
|
|
|
197
168
|
return doc;
|
|
198
169
|
});
|
|
199
170
|
}
|
|
200
171
|
|
|
201
|
-
async addOwner(owner
|
|
172
|
+
async addOwner(owner) {
|
|
202
173
|
const initialized = this.isInitialized({ nodeOwner: owner });
|
|
203
|
-
|
|
204
174
|
if (this.notification && typeof this.notification.setDefaultReceiver === 'function') {
|
|
205
175
|
this.notification.setDefaultReceiver(owner.did);
|
|
206
176
|
}
|
|
207
177
|
|
|
208
|
-
const
|
|
178
|
+
const updated = await this.update({
|
|
209
179
|
$set: { nodeOwner: owner, initialized, initializedAt: initialized ? new Date() : null },
|
|
210
180
|
});
|
|
211
181
|
|
|
212
|
-
this.emit(EVENTS.NODE_ADDED_OWNER,
|
|
182
|
+
this.emit(EVENTS.NODE_ADDED_OWNER, updated);
|
|
213
183
|
|
|
214
|
-
return
|
|
184
|
+
return updated;
|
|
215
185
|
}
|
|
216
186
|
|
|
217
187
|
async updateNodeOwner({ nodeOwner, ownerNft }) {
|
|
@@ -219,10 +189,7 @@ class NodeState extends BaseState {
|
|
|
219
189
|
throw new Error('Node owner is invalid');
|
|
220
190
|
}
|
|
221
191
|
|
|
222
|
-
const doc = await this.read();
|
|
223
|
-
|
|
224
192
|
const initialized = this.isInitialized({ nodeOwner });
|
|
225
|
-
|
|
226
193
|
if (this.notification && typeof this.notification.setDefaultReceiver === 'function') {
|
|
227
194
|
this.notification.setDefaultReceiver(nodeOwner.did);
|
|
228
195
|
}
|
|
@@ -232,13 +199,9 @@ class NodeState extends BaseState {
|
|
|
232
199
|
entities.ownerNft = ownerNft;
|
|
233
200
|
}
|
|
234
201
|
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
this.emit(EVENTS.NODE_ADDED_OWNER, updateResult);
|
|
240
|
-
|
|
241
|
-
return updateResult;
|
|
202
|
+
const updated = await this.update({ $set: entities });
|
|
203
|
+
this.emit(EVENTS.NODE_ADDED_OWNER, updated);
|
|
204
|
+
return updated;
|
|
242
205
|
}
|
|
243
206
|
|
|
244
207
|
async updateNftHolder(holder) {
|
|
@@ -246,11 +209,7 @@ class NodeState extends BaseState {
|
|
|
246
209
|
throw new Error('NFT holder can not be empty');
|
|
247
210
|
}
|
|
248
211
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
return this.update(doc._id, {
|
|
252
|
-
$set: { 'ownerNft.holder': holder },
|
|
253
|
-
});
|
|
212
|
+
return this.update({ $set: { 'ownerNft.holder': holder } });
|
|
254
213
|
}
|
|
255
214
|
|
|
256
215
|
async enterMode(mode) {
|
|
@@ -305,21 +264,19 @@ class NodeState extends BaseState {
|
|
|
305
264
|
|
|
306
265
|
// deprecated
|
|
307
266
|
async increaseCustomBlockletNumber() {
|
|
308
|
-
const {
|
|
267
|
+
const { customBlockletNumber = 0 } = await this.read();
|
|
309
268
|
const num = customBlockletNumber + 1;
|
|
310
|
-
await this.update(
|
|
269
|
+
await this.update({ $set: { customBlockletNumber: num } });
|
|
311
270
|
return num;
|
|
312
271
|
}
|
|
313
272
|
|
|
314
273
|
async updateGateway(gateway) {
|
|
315
|
-
const
|
|
316
|
-
{},
|
|
317
|
-
|
|
318
|
-
);
|
|
319
|
-
|
|
320
|
-
this.emit(EVENTS.RELOAD_GATEWAY, nodeInfo);
|
|
274
|
+
const doc = await this.update({
|
|
275
|
+
$set: { 'routing.requestLimit': gateway.requestLimit, 'routing.cacheEnabled': gateway.cacheEnabled },
|
|
276
|
+
});
|
|
321
277
|
|
|
322
|
-
|
|
278
|
+
this.emit(EVENTS.RELOAD_GATEWAY, doc);
|
|
279
|
+
return doc.routing;
|
|
323
280
|
}
|
|
324
281
|
|
|
325
282
|
async updateStatus(status) {
|
|
@@ -327,16 +284,18 @@ class NodeState extends BaseState {
|
|
|
327
284
|
throw new Error('status is invalid');
|
|
328
285
|
}
|
|
329
286
|
|
|
330
|
-
|
|
331
|
-
await this.update(record._id, { $set: { status } });
|
|
332
|
-
|
|
333
|
-
record.status = status;
|
|
334
|
-
return record;
|
|
287
|
+
return this.update({ $set: { status } });
|
|
335
288
|
}
|
|
336
289
|
|
|
337
290
|
async resetStatus() {
|
|
338
291
|
return this.updateStatus(SERVER_STATUS.RUNNING);
|
|
339
292
|
}
|
|
293
|
+
|
|
294
|
+
async update(updates) {
|
|
295
|
+
await this.read();
|
|
296
|
+
const [, [updated]] = await super.update({ did: this.config.nodeDid }, updates);
|
|
297
|
+
return updated;
|
|
298
|
+
}
|
|
340
299
|
}
|
|
341
300
|
|
|
342
301
|
module.exports = NodeState;
|
|
@@ -3,6 +3,9 @@ const { EVENTS } = require('@abtnode/constant');
|
|
|
3
3
|
|
|
4
4
|
const BaseState = require('./base');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @extends BaseState<import('@abtnode/models').NotificationState>
|
|
8
|
+
*/
|
|
6
9
|
class NotificationState extends BaseState {
|
|
7
10
|
constructor(baseDir, config = {}) {
|
|
8
11
|
super(baseDir, { filename: 'notification.db', ...config });
|
|
@@ -19,43 +22,30 @@ class NotificationState extends BaseState {
|
|
|
19
22
|
this.defaultReceiver = receiver;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
create(payload) {
|
|
25
|
+
async create(payload) {
|
|
23
26
|
['title', 'description'].forEach((x) => {
|
|
24
27
|
if (!payload[x]) {
|
|
25
28
|
throw new Error(`Invalid notification payload: ${x} is required`);
|
|
26
29
|
}
|
|
27
30
|
});
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
severity: payload.severity || 'info',
|
|
40
|
-
read: false,
|
|
41
|
-
},
|
|
42
|
-
(e, data) => {
|
|
43
|
-
if (e) {
|
|
44
|
-
logger.error('create error', { error: e });
|
|
45
|
-
return reject(e);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
logger.info('create', { data });
|
|
49
|
-
this.emit(EVENTS.NOTIFICATION_CREATE, data);
|
|
50
|
-
return resolve(data);
|
|
51
|
-
}
|
|
52
|
-
);
|
|
32
|
+
const doc = await this.insert({
|
|
33
|
+
sender: payload.sender || this.defaultSender,
|
|
34
|
+
receiver: payload.receiver || this.defaultReceiver,
|
|
35
|
+
title: payload.title,
|
|
36
|
+
description: payload.description,
|
|
37
|
+
action: payload.action || '',
|
|
38
|
+
entityType: payload.entityType || '',
|
|
39
|
+
entityId: payload.entityId || '',
|
|
40
|
+
severity: payload.severity || 'info',
|
|
41
|
+
read: false,
|
|
53
42
|
});
|
|
43
|
+
this.emit(EVENTS.NOTIFICATION_CREATE, doc);
|
|
44
|
+
return doc;
|
|
54
45
|
}
|
|
55
46
|
|
|
56
47
|
async findPaginated({ read, paging } = {}) {
|
|
57
48
|
const conditions = {};
|
|
58
|
-
|
|
59
49
|
if (typeof read === 'boolean') {
|
|
60
50
|
conditions.read = read;
|
|
61
51
|
}
|
|
@@ -67,26 +57,14 @@ class NotificationState extends BaseState {
|
|
|
67
57
|
async read({ id }, context) {
|
|
68
58
|
const idList = id.split(',').map((x) => x.trim());
|
|
69
59
|
logger.info('mark notification as read', { idList });
|
|
70
|
-
|
|
71
|
-
const [numAffected] = await this.update(
|
|
72
|
-
{ _id: { $in: idList } },
|
|
73
|
-
{ $set: { read: true } },
|
|
74
|
-
{ multi: true, upsert: false, returnUpdatedDocs: false }
|
|
75
|
-
);
|
|
76
|
-
|
|
60
|
+
const [numAffected] = await this.update({ id: { $in: idList } }, { $set: { read: true } });
|
|
77
61
|
return numAffected;
|
|
78
62
|
}
|
|
79
63
|
|
|
80
64
|
// eslint-disable-next-line no-unused-vars
|
|
81
65
|
async unread({ id }, context) {
|
|
82
66
|
const idList = Array.isArray(id) ? id : [id];
|
|
83
|
-
|
|
84
|
-
const [numAffected] = await this.update(
|
|
85
|
-
{ _id: { $in: idList } },
|
|
86
|
-
{ $set: { read: false } },
|
|
87
|
-
{ multi: true, upsert: false, returnUpdatedDocs: false }
|
|
88
|
-
);
|
|
89
|
-
|
|
67
|
+
const [numAffected] = await this.update({ id: { $in: idList } }, { $set: { read: false } });
|
|
90
68
|
return numAffected;
|
|
91
69
|
}
|
|
92
70
|
}
|
package/lib/states/session.js
CHANGED
|
@@ -1,68 +1,52 @@
|
|
|
1
|
-
const
|
|
1
|
+
const omit = require('lodash/omit');
|
|
2
2
|
const BaseState = require('./base');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* This db is used to save session data generated in a http session. the session is NOT user auth session.
|
|
6
|
+
* @extends BaseState<import('@abtnode/models').SessionState>
|
|
6
7
|
*/
|
|
7
8
|
class SessionState extends BaseState {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
this.ensureIndex({ fieldName: 'expireDate', expireAfterSeconds: 0 }, (error) => {
|
|
19
|
-
if (error) {
|
|
20
|
-
logger.error('ensure expireDate index failed', { error });
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async start(initialData) {
|
|
27
|
-
const { _id, ...data } = await this.insert(initialData);
|
|
28
|
-
return { id: _id, ...data };
|
|
9
|
+
/**
|
|
10
|
+
* type is used to group sessions
|
|
11
|
+
* key is used to identify sessions
|
|
12
|
+
*/
|
|
13
|
+
async start({ type, key, ...data }) {
|
|
14
|
+
const doc = await this.insert({ type, key, __data: data });
|
|
15
|
+
return this._format(doc);
|
|
29
16
|
}
|
|
30
17
|
|
|
31
|
-
async update(id,
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
if (!exist) {
|
|
18
|
+
async update(id, updates) {
|
|
19
|
+
const doc = await this.findOne({ id });
|
|
20
|
+
if (!doc) {
|
|
35
21
|
throw new Error(`Session does not exist: ${id}`);
|
|
36
22
|
}
|
|
37
23
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
await super.update({ _id: id }, { $set: { ...exist, ...data } }, { multi: false, upsert: false });
|
|
41
|
-
|
|
42
|
-
return { id, ...exist, ...data };
|
|
24
|
+
await super.update({ id }, { __data: { ...doc.__data, ...updates } });
|
|
25
|
+
return this._format(doc, updates);
|
|
43
26
|
}
|
|
44
27
|
|
|
45
28
|
async read(id) {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
49
|
-
item.id = item._id;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return item;
|
|
29
|
+
const doc = await this.findOne({ id });
|
|
30
|
+
return this._format(doc);
|
|
53
31
|
}
|
|
54
32
|
|
|
55
33
|
async end(id) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
if (!data) {
|
|
34
|
+
const doc = await this.findOne({ id });
|
|
35
|
+
if (!doc) {
|
|
59
36
|
throw new Error(`Session does not exist: ${id}`);
|
|
60
37
|
}
|
|
61
38
|
|
|
62
|
-
await this.remove({
|
|
39
|
+
await this.remove({ id });
|
|
40
|
+
return this._format(doc);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async find(...args) {
|
|
44
|
+
const docs = await super.find(...args);
|
|
45
|
+
return docs.map(this._format);
|
|
46
|
+
}
|
|
63
47
|
|
|
64
|
-
|
|
65
|
-
return
|
|
48
|
+
_format(doc, extra = {}) {
|
|
49
|
+
return doc ? omit({ ...doc, ...doc.__data, ...extra }, ['__data']) : doc;
|
|
66
50
|
}
|
|
67
51
|
}
|
|
68
52
|
|
package/lib/states/site.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const logger = require('@abtnode/logger')('@abtnode/core:states:site');
|
|
2
|
-
const { toSlotDomain } = require('@abtnode/router-provider/lib/util');
|
|
3
2
|
|
|
4
3
|
const BaseState = require('./base');
|
|
5
4
|
const { getBlockletDomainGroupName } = require('../util/router');
|
|
@@ -14,24 +13,26 @@ const { validateUpdateDomainAliases } = require('../validators/router');
|
|
|
14
13
|
// corsAllowedOrigins: Array<string>;
|
|
15
14
|
// };
|
|
16
15
|
|
|
16
|
+
/**
|
|
17
|
+
* FIXME: @wangshijun some functions may not be performant
|
|
18
|
+
* @extends BaseState<import('@abtnode/models').SiteState>
|
|
19
|
+
*/
|
|
17
20
|
class SiteState extends BaseState {
|
|
18
|
-
constructor(baseDir, config = {}) {
|
|
19
|
-
super(baseDir, { filename: 'routing_rule.db', ...config });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
21
|
async add(site) {
|
|
23
22
|
const result = await this.insert(site);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
logger.info('site created', { site: tmpSite });
|
|
27
|
-
return tmpSite;
|
|
23
|
+
logger.info('site created', { site: result });
|
|
24
|
+
return result;
|
|
28
25
|
}
|
|
29
26
|
|
|
30
27
|
async addRuleToSite(id, rule) {
|
|
31
|
-
const
|
|
28
|
+
const { rules } = await this.findOne({ id });
|
|
29
|
+
if (rules.indexOf(rule) === -1) {
|
|
30
|
+
const [addedCount] = await this.update({ id }, { $set: { rules: [...rules, rule] } });
|
|
31
|
+
logger.info('added rule to site', { count: addedCount });
|
|
32
|
+
return addedCount;
|
|
33
|
+
}
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
return addedCount;
|
|
35
|
+
return 0;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
async getSites(...args) {
|
|
@@ -46,48 +47,41 @@ class SiteState extends BaseState {
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
async getSitesByBlocklet(did) {
|
|
49
|
-
const
|
|
50
|
-
return
|
|
50
|
+
const sites = await this.getSites();
|
|
51
|
+
return sites.filter((x) => x.rules.some((r) => r.to?.did === did));
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
async
|
|
54
|
-
const site = await this.findOne({
|
|
55
|
-
if (
|
|
56
|
-
return
|
|
54
|
+
async getSiteByRuleId(id, ruleId) {
|
|
55
|
+
const site = await this.findOne({ id });
|
|
56
|
+
if (site && site.rules.some((r) => r.id === ruleId)) {
|
|
57
|
+
return site;
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
return
|
|
60
|
+
return null;
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
async
|
|
63
|
-
const
|
|
63
|
+
async getRuleById(id) {
|
|
64
|
+
const sites = await this.getSites();
|
|
65
|
+
const site = sites.find((x) => x.rules.some((r) => r.id === id));
|
|
64
66
|
if (!site) {
|
|
65
|
-
return
|
|
67
|
+
return null;
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
return
|
|
70
|
+
return site.rules.find((x) => x.id === id);
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
async domainExists(domain) {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
return count > 0;
|
|
74
|
+
const sites = await this.getSites();
|
|
75
|
+
return sites.some((x) => x.domain === domain || x.domainAliases.some((y) => y.value === domain));
|
|
77
76
|
}
|
|
78
77
|
|
|
79
|
-
async
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return this.findOne({
|
|
83
|
-
$or: [{ domain }, { domainAliases: domain }, { 'domainAliases.value': domain }],
|
|
84
|
-
});
|
|
78
|
+
async findByDomainAlias(domain) {
|
|
79
|
+
const sites = await this.getSites();
|
|
80
|
+
return sites.find((x) => x.domainAliases.some((y) => y.value === domain));
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
async findOneByBlocklet(did) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return BaseState.renameIdFiledName(result);
|
|
84
|
+
return this.findOne({ domain: getBlockletDomainGroupName(did) });
|
|
91
85
|
}
|
|
92
86
|
|
|
93
87
|
async getBlockletDomains(did) {
|
|
@@ -97,8 +91,7 @@ class SiteState extends BaseState {
|
|
|
97
91
|
|
|
98
92
|
async updateDomainAliasList(id, domainAliases) {
|
|
99
93
|
await validateUpdateDomainAliases(domainAliases);
|
|
100
|
-
|
|
101
|
-
return super.update({ _id: id }, { $set: { domainAliases } });
|
|
94
|
+
return super.update({ id }, { $set: { domainAliases } });
|
|
102
95
|
}
|
|
103
96
|
}
|
|
104
97
|
|