@did-space/core 0.4.18 → 0.4.20
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.
|
@@ -18,8 +18,8 @@ exports.SpaceConfigSchema = validator_1.Joi.object({
|
|
|
18
18
|
name: validator_1.Joi.string().required(),
|
|
19
19
|
// @see: https://github.com/sideway/joi/blob/master/API.md#string
|
|
20
20
|
description: validator_1.Joi.string().required().allow('').default(''),
|
|
21
|
-
// 字节数,默认给个
|
|
22
|
-
size: validator_1.Joi.number().required().default(xbytes_1.default.parseSize('
|
|
21
|
+
// 字节数,默认给个1GiB, size 为 0 表示空间无限制
|
|
22
|
+
size: validator_1.Joi.number().required().default(xbytes_1.default.parseSize('1GiB')),
|
|
23
23
|
assetDid: validator_1.Joi.DID(),
|
|
24
24
|
createAt: validator_1.Joi.string().required(),
|
|
25
25
|
updateAt: validator_1.Joi.string().required(),
|
|
@@ -11,14 +11,16 @@ export type ObjectSpaceOptions = {
|
|
|
11
11
|
treeRepository: TreeRepository;
|
|
12
12
|
objectRepository: ObjectRepository;
|
|
13
13
|
objectCollectionRepository: ObjectCollectionRepository;
|
|
14
|
+
/**
|
|
15
|
+
* @description 忽略存储容量限制吗?
|
|
16
|
+
* @type {(false | true)}
|
|
17
|
+
*/
|
|
18
|
+
ignoreStorageLimit?: false | true;
|
|
14
19
|
};
|
|
15
20
|
export declare class ObjectSpace implements SpaceProtocol {
|
|
16
|
-
readonly
|
|
17
|
-
readonly driver: DriverProtocol;
|
|
18
|
-
readonly objectRepository: ObjectRepository;
|
|
19
|
-
readonly treeRepository: TreeRepository;
|
|
20
|
-
readonly objectCollectionRepository: ObjectCollectionRepository;
|
|
21
|
+
readonly options: ObjectSpaceOptions;
|
|
21
22
|
readonly globalSpace: GlobalSpace;
|
|
23
|
+
readonly driver: DriverProtocol;
|
|
22
24
|
static readonly READONLY_OBJECT_KEYS: string[];
|
|
23
25
|
constructor(options: ObjectSpaceOptions);
|
|
24
26
|
createSpace(spaceConfig: SpaceConfig): Promise<void>;
|
|
@@ -39,10 +39,7 @@ class ObjectSpace {
|
|
|
39
39
|
throw error;
|
|
40
40
|
}
|
|
41
41
|
this.driver = options.driver;
|
|
42
|
-
this.
|
|
43
|
-
this.treeRepository = options.treeRepository;
|
|
44
|
-
this.objectRepository = options.objectRepository;
|
|
45
|
-
this.objectCollectionRepository = options.objectCollectionRepository;
|
|
42
|
+
this.options = options;
|
|
46
43
|
this.globalSpace = new global_space_1.GlobalSpace({
|
|
47
44
|
driver: options.driver,
|
|
48
45
|
treeRepository: options.treeRepository,
|
|
@@ -61,11 +58,11 @@ class ObjectSpace {
|
|
|
61
58
|
isSpaceCreated() {
|
|
62
59
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
60
|
const where = {
|
|
64
|
-
spaceDid: this.spaceDid,
|
|
61
|
+
spaceDid: this.options.spaceDid,
|
|
65
62
|
key: '/config.yml',
|
|
66
63
|
};
|
|
67
64
|
debug('isSpaceCreated.before', JSON.stringify(where));
|
|
68
|
-
const created = Boolean(yield this.treeRepository.count({
|
|
65
|
+
const created = Boolean(yield this.options.treeRepository.count({
|
|
69
66
|
where,
|
|
70
67
|
}));
|
|
71
68
|
debug('isSpaceCreated.after', JSON.stringify({ created }));
|
|
@@ -128,8 +125,12 @@ class ObjectSpace {
|
|
|
128
125
|
if (!(yield this.isSpaceCreated())) {
|
|
129
126
|
return;
|
|
130
127
|
}
|
|
128
|
+
// 如果忽略了存储限制,直接不用校验了
|
|
129
|
+
if (this.options.ignoreStorageLimit) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
131
132
|
const [did, capacity, usedCapacity] = yield Promise.all([
|
|
132
|
-
this.spaceDid,
|
|
133
|
+
this.options.spaceDid,
|
|
133
134
|
// @FIXME: 此处应该用 cache 层的数据,可以加速查询
|
|
134
135
|
this.get('size'),
|
|
135
136
|
this.getSpaceSize(),
|
|
@@ -140,7 +141,7 @@ class ObjectSpace {
|
|
|
140
141
|
.filter(Boolean);
|
|
141
142
|
debug('verifySpace.before', { did, capacity, usedCapacity });
|
|
142
143
|
if (capacity && usedCapacity + size >= capacity && !whiteList.includes(did)) {
|
|
143
|
-
throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(capacity)}, used capacity is ${(0, xbytes_1.default)(usedCapacity)}`);
|
|
144
|
+
throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(capacity, { iec: true })}, used capacity is ${(0, xbytes_1.default)(usedCapacity, { iec: true })}`);
|
|
144
145
|
}
|
|
145
146
|
});
|
|
146
147
|
}
|
|
@@ -185,7 +186,7 @@ class ObjectSpace {
|
|
|
185
186
|
debug('getHash.before', JSON.stringify({ options }));
|
|
186
187
|
const key = (0, path_1.join)(yield this.getAppSpacePath(options), options.key);
|
|
187
188
|
const where = {
|
|
188
|
-
spaceDid: this.spaceDid,
|
|
189
|
+
spaceDid: this.options.spaceDid,
|
|
189
190
|
key,
|
|
190
191
|
};
|
|
191
192
|
debug('getHash.$key', key);
|
|
@@ -193,7 +194,7 @@ class ObjectSpace {
|
|
|
193
194
|
if (!(yield this.exists(options))) {
|
|
194
195
|
throw new Error(`Object(${key}) not exists`);
|
|
195
196
|
}
|
|
196
|
-
const tree = yield this.treeRepository.findOne({ where });
|
|
197
|
+
const tree = yield this.options.treeRepository.findOne({ where });
|
|
197
198
|
return tree.objectId;
|
|
198
199
|
});
|
|
199
200
|
}
|
|
@@ -207,21 +208,21 @@ class ObjectSpace {
|
|
|
207
208
|
listsOneLevelAsOwner(options) {
|
|
208
209
|
return __awaiter(this, void 0, void 0, function* () {
|
|
209
210
|
const where = {
|
|
210
|
-
spaceDid: this.spaceDid,
|
|
211
|
+
spaceDid: this.options.spaceDid,
|
|
211
212
|
key: options.key,
|
|
212
213
|
};
|
|
213
214
|
debug('listsOneLevel.before', JSON.stringify(options));
|
|
214
|
-
const parentTree = yield this.treeRepository.findOne({
|
|
215
|
+
const parentTree = yield this.options.treeRepository.findOne({
|
|
215
216
|
where,
|
|
216
217
|
raw: true,
|
|
217
218
|
});
|
|
218
219
|
debug('listsOneLevel.$parentTree', JSON.stringify(parentTree, null, 2));
|
|
219
|
-
const trees = yield this.treeRepository.findAll({
|
|
220
|
+
const trees = yield this.options.treeRepository.findAll({
|
|
220
221
|
where: {
|
|
221
|
-
spaceDid: this.spaceDid,
|
|
222
|
+
spaceDid: this.options.spaceDid,
|
|
222
223
|
parentId: parentTree.id,
|
|
223
224
|
},
|
|
224
|
-
include: this.objectRepository,
|
|
225
|
+
include: this.options.objectRepository,
|
|
225
226
|
});
|
|
226
227
|
return Promise.all(trees.map((x) => __awaiter(this, void 0, void 0, function* () {
|
|
227
228
|
const absolutePath = (0, path_1.join)(x.spaceDid, x.key);
|
|
@@ -259,15 +260,15 @@ class ObjectSpace {
|
|
|
259
260
|
return __awaiter(this, void 0, void 0, function* () {
|
|
260
261
|
debug('listsRecursiveAsOwner.before', JSON.stringify(options));
|
|
261
262
|
const where = {
|
|
262
|
-
spaceDid: this.spaceDid,
|
|
263
|
+
spaceDid: this.options.spaceDid,
|
|
263
264
|
key: {
|
|
264
265
|
[skypesky_sequelize_1.Op.like]: `${options.key}%`,
|
|
265
266
|
},
|
|
266
267
|
type: model_1.TreeModelType.FILE,
|
|
267
268
|
};
|
|
268
|
-
const trees = yield this.treeRepository.findAll({
|
|
269
|
+
const trees = yield this.options.treeRepository.findAll({
|
|
269
270
|
where,
|
|
270
|
-
include: this.objectRepository,
|
|
271
|
+
include: this.options.objectRepository,
|
|
271
272
|
});
|
|
272
273
|
return trees.map((x) => {
|
|
273
274
|
return {
|
|
@@ -322,12 +323,12 @@ class ObjectSpace {
|
|
|
322
323
|
return __awaiter(this, void 0, void 0, function* () {
|
|
323
324
|
debug('list.before', JSON.stringify(options));
|
|
324
325
|
const where = {
|
|
325
|
-
spaceDid: this.spaceDid,
|
|
326
|
+
spaceDid: this.options.spaceDid,
|
|
326
327
|
key: options.key,
|
|
327
328
|
};
|
|
328
|
-
const x = yield this.treeRepository.findOne({
|
|
329
|
+
const x = yield this.options.treeRepository.findOne({
|
|
329
330
|
where,
|
|
330
|
-
include: this.objectRepository,
|
|
331
|
+
include: this.options.objectRepository,
|
|
331
332
|
});
|
|
332
333
|
return {
|
|
333
334
|
key: x.key,
|
|
@@ -395,13 +396,13 @@ class ObjectSpace {
|
|
|
395
396
|
return;
|
|
396
397
|
}
|
|
397
398
|
const where = {
|
|
398
|
-
spaceDid: this.spaceDid,
|
|
399
|
+
spaceDid: this.options.spaceDid,
|
|
399
400
|
key,
|
|
400
401
|
};
|
|
401
402
|
debug('updateAsOwner.before', JSON.stringify({ key, options: (0, omit_1.default)(options, 'data'), where }));
|
|
402
|
-
const oldTree = yield this.treeRepository.findOne({
|
|
403
|
+
const oldTree = yield this.options.treeRepository.findOne({
|
|
403
404
|
where,
|
|
404
|
-
include: this.objectRepository,
|
|
405
|
+
include: this.options.objectRepository,
|
|
405
406
|
});
|
|
406
407
|
debug('updateAsOwner.$oldTree', JSON.stringify(oldTree));
|
|
407
408
|
const listObject = yield this.driver.list({
|
|
@@ -413,7 +414,7 @@ class ObjectSpace {
|
|
|
413
414
|
oldTree.Object.size === options.size &&
|
|
414
415
|
(listObject === null || listObject === void 0 ? void 0 : listObject.size) === options.size) {
|
|
415
416
|
// 内容不变,不需要再次存储对象,只需要更新对象的事件即可
|
|
416
|
-
yield this.treeRepository.update({
|
|
417
|
+
yield this.options.treeRepository.update({
|
|
417
418
|
updatedAt: new Date().toISOString(),
|
|
418
419
|
}, {
|
|
419
420
|
where: {
|
|
@@ -424,7 +425,7 @@ class ObjectSpace {
|
|
|
424
425
|
}
|
|
425
426
|
yield this.globalSpace.add(key, data, options);
|
|
426
427
|
// 更新 tree 记录
|
|
427
|
-
yield this.treeRepository.update({
|
|
428
|
+
yield this.options.treeRepository.update({
|
|
428
429
|
updatedAt: new Date().toISOString(),
|
|
429
430
|
objectId: options.hash,
|
|
430
431
|
}, {
|
|
@@ -442,8 +443,8 @@ class ObjectSpace {
|
|
|
442
443
|
const isDir = key.endsWith('/');
|
|
443
444
|
if (isDir) {
|
|
444
445
|
// 添加 tree 记录
|
|
445
|
-
yield this.treeRepository.deepCreate({
|
|
446
|
-
spaceDid: this.spaceDid,
|
|
446
|
+
yield this.options.treeRepository.deepCreate({
|
|
447
|
+
spaceDid: this.options.spaceDid,
|
|
447
448
|
objectId: null,
|
|
448
449
|
parentId: null,
|
|
449
450
|
key,
|
|
@@ -455,8 +456,8 @@ class ObjectSpace {
|
|
|
455
456
|
// 存储新对象到全局存储区
|
|
456
457
|
yield this.globalSpace.add(key, data, options);
|
|
457
458
|
// 添加 tree 记录
|
|
458
|
-
yield this.treeRepository.deepCreate({
|
|
459
|
-
spaceDid: this.spaceDid,
|
|
459
|
+
yield this.options.treeRepository.deepCreate({
|
|
460
|
+
spaceDid: this.options.spaceDid,
|
|
460
461
|
objectId: options.hash,
|
|
461
462
|
parentId: null,
|
|
462
463
|
key,
|
|
@@ -469,12 +470,12 @@ class ObjectSpace {
|
|
|
469
470
|
return __awaiter(this, void 0, void 0, function* () {
|
|
470
471
|
debug('updateMetadata.before', { key, options: (0, omit_1.default)(options, 'data') });
|
|
471
472
|
const where = {
|
|
472
|
-
spaceDid: this.spaceDid,
|
|
473
|
+
spaceDid: this.options.spaceDid,
|
|
473
474
|
key,
|
|
474
475
|
type: key.endsWith('/') ? model_1.TreeModelType.FOLDER : model_1.TreeModelType.FILE,
|
|
475
476
|
};
|
|
476
477
|
debug('updateMetadata.before', JSON.stringify({ key, options: (0, omit_1.default)(options, 'data'), where }));
|
|
477
|
-
const oldTree = yield this.treeRepository.findOne({
|
|
478
|
+
const oldTree = yield this.options.treeRepository.findOne({
|
|
478
479
|
where,
|
|
479
480
|
});
|
|
480
481
|
debug('updateMetadata.$oldTree', JSON.stringify(oldTree));
|
|
@@ -501,34 +502,34 @@ class ObjectSpace {
|
|
|
501
502
|
if (isDir) {
|
|
502
503
|
// 删除文件夹
|
|
503
504
|
const whereForDeleteFolder = {
|
|
504
|
-
spaceDid: this.spaceDid,
|
|
505
|
+
spaceDid: this.options.spaceDid,
|
|
505
506
|
// 删除以 key 开头的记录
|
|
506
507
|
key: {
|
|
507
508
|
[skypesky_sequelize_1.Op.like]: `${key}%`,
|
|
508
509
|
},
|
|
509
510
|
};
|
|
510
511
|
debug('deleteAsOwner.$whereForDeleteFolder', JSON.stringify(whereForDeleteFolder));
|
|
511
|
-
const trees = yield this.treeRepository.findAll({
|
|
512
|
+
const trees = yield this.options.treeRepository.findAll({
|
|
512
513
|
where: whereForDeleteFolder,
|
|
513
514
|
attributes: ['objectId'],
|
|
514
515
|
raw: true,
|
|
515
516
|
});
|
|
516
|
-
yield this.treeRepository.destroy({
|
|
517
|
+
yield this.options.treeRepository.destroy({
|
|
517
518
|
where: whereForDeleteFolder,
|
|
518
519
|
});
|
|
519
520
|
yield this.globalSpace.mark(...trees.map((x) => x.objectId));
|
|
520
521
|
return;
|
|
521
522
|
}
|
|
522
523
|
const where = {
|
|
523
|
-
spaceDid: this.spaceDid,
|
|
524
|
+
spaceDid: this.options.spaceDid,
|
|
524
525
|
key,
|
|
525
526
|
};
|
|
526
|
-
const tree = yield this.treeRepository.findOne({
|
|
527
|
+
const tree = yield this.options.treeRepository.findOne({
|
|
527
528
|
where,
|
|
528
529
|
attributes: ['objectId'],
|
|
529
530
|
raw: true,
|
|
530
531
|
});
|
|
531
|
-
yield this.treeRepository.destroy({
|
|
532
|
+
yield this.options.treeRepository.destroy({
|
|
532
533
|
where,
|
|
533
534
|
});
|
|
534
535
|
yield this.globalSpace.mark(tree.objectId);
|
|
@@ -538,11 +539,11 @@ class ObjectSpace {
|
|
|
538
539
|
return __awaiter(this, void 0, void 0, function* () {
|
|
539
540
|
debug('readAsOwner.before', JSON.stringify({ key, options }));
|
|
540
541
|
const where = {
|
|
541
|
-
spaceDid: this.spaceDid,
|
|
542
|
+
spaceDid: this.options.spaceDid,
|
|
542
543
|
key,
|
|
543
544
|
};
|
|
544
545
|
debug('readAsOwner.$where', JSON.stringify(where));
|
|
545
|
-
const tree = yield this.treeRepository.findOne({
|
|
546
|
+
const tree = yield this.options.treeRepository.findOne({
|
|
546
547
|
where,
|
|
547
548
|
});
|
|
548
549
|
if (!tree) {
|
|
@@ -554,11 +555,11 @@ class ObjectSpace {
|
|
|
554
555
|
existsAsOwner(key) {
|
|
555
556
|
return __awaiter(this, void 0, void 0, function* () {
|
|
556
557
|
const where = {
|
|
557
|
-
spaceDid: this.spaceDid,
|
|
558
|
+
spaceDid: this.options.spaceDid,
|
|
558
559
|
key,
|
|
559
560
|
};
|
|
560
561
|
debug('existsAsOwner.before', JSON.stringify({ where }));
|
|
561
|
-
const exists = Boolean(yield this.treeRepository.count({
|
|
562
|
+
const exists = Boolean(yield this.options.treeRepository.count({
|
|
562
563
|
where,
|
|
563
564
|
}));
|
|
564
565
|
debug('existsAsOwner.after', JSON.stringify({ exists }));
|
|
@@ -568,7 +569,7 @@ class ObjectSpace {
|
|
|
568
569
|
getStatusAsOwner(key) {
|
|
569
570
|
return __awaiter(this, void 0, void 0, function* () {
|
|
570
571
|
debug('getStatusAsOwner.before', JSON.stringify({
|
|
571
|
-
spaceDid: this.spaceDid,
|
|
572
|
+
spaceDid: this.options.spaceDid,
|
|
572
573
|
key,
|
|
573
574
|
}));
|
|
574
575
|
const { error } = schemas_1.OwnerOperatorKeySchema.validate(key);
|
|
@@ -576,22 +577,22 @@ class ObjectSpace {
|
|
|
576
577
|
throw error;
|
|
577
578
|
}
|
|
578
579
|
const where = {
|
|
579
|
-
spaceDid: this.spaceDid,
|
|
580
|
+
spaceDid: this.options.spaceDid,
|
|
580
581
|
key: {
|
|
581
582
|
[skypesky_sequelize_1.Op.like]: `${key}%`,
|
|
582
583
|
},
|
|
583
584
|
type: model_1.TreeModelType.FILE,
|
|
584
585
|
};
|
|
585
|
-
const objects = yield this.treeRepository.count({ where });
|
|
586
|
-
const lastModified = yield this.treeRepository.max('updatedAt', {
|
|
586
|
+
const objects = yield this.options.treeRepository.count({ where });
|
|
587
|
+
const lastModified = yield this.options.treeRepository.max('updatedAt', {
|
|
587
588
|
where,
|
|
588
589
|
});
|
|
589
590
|
// @FIXME: 没有正确关联
|
|
590
|
-
const objectRecords = yield this.objectRepository.sequelize.query(`SELECT SUM(Object.size) as totalSize FROM Object JOIN Tree ON Object.id = Tree.objectId WHERE Tree.spaceDid = '${this.spaceDid}' AND Tree.key LIKE '${key}%'`, { type: skypesky_sequelize_1.QueryTypes.SELECT });
|
|
591
|
+
const objectRecords = yield this.options.objectRepository.sequelize.query(`SELECT SUM(Object.size) as totalSize FROM Object JOIN Tree ON Object.id = Tree.objectId WHERE Tree.spaceDid = '${this.options.spaceDid}' AND Tree.type = ${model_1.TreeModelType.FILE} AND Tree.key LIKE '${key}%'`, { type: skypesky_sequelize_1.QueryTypes.SELECT });
|
|
591
592
|
// @ts-expect-error
|
|
592
593
|
const size = objectRecords[0].totalSize;
|
|
593
594
|
debug('getStatusAsOwner.after', JSON.stringify({
|
|
594
|
-
spaceDid: this.spaceDid,
|
|
595
|
+
spaceDid: this.options.spaceDid,
|
|
595
596
|
key,
|
|
596
597
|
objects,
|
|
597
598
|
size,
|
package/dist/space/space.js
CHANGED
|
@@ -131,7 +131,9 @@ class Space {
|
|
|
131
131
|
}
|
|
132
132
|
const spaceStatus = yield this.getSpaceStatus();
|
|
133
133
|
if (spaceStatus.isFull) {
|
|
134
|
-
throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity
|
|
134
|
+
throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity, {
|
|
135
|
+
iec: true,
|
|
136
|
+
})}, used capacity is ${(0, xbytes_1.default)(spaceStatus.usedCapacity, { iec: true })}`);
|
|
135
137
|
}
|
|
136
138
|
return this.driver.write(options);
|
|
137
139
|
});
|
|
@@ -162,7 +164,9 @@ class Space {
|
|
|
162
164
|
}
|
|
163
165
|
const spaceStatus = yield this.getSpaceStatus();
|
|
164
166
|
if (spaceStatus.isFull) {
|
|
165
|
-
throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity
|
|
167
|
+
throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity, {
|
|
168
|
+
iec: true,
|
|
169
|
+
})}, used capacity is ${(0, xbytes_1.default)(spaceStatus.usedCapacity, { iec: true })}`);
|
|
166
170
|
}
|
|
167
171
|
return this.driver.writeAsOwner(key, data);
|
|
168
172
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-space/core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.20",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
"ts-jest": "^28.0.8",
|
|
62
62
|
"typescript": "^4.9.5"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "3791c92ff078f306d84a2c0fda775edf13ce6826"
|
|
65
65
|
}
|