@did-space/core 0.6.0 → 1.0.0
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/dist/cjs/schemas/global-space.js +0 -2
- package/dist/cjs/space/global-space.d.ts +0 -10
- package/dist/cjs/space/global-space.js +0 -35
- package/dist/es/schemas/global-space.js +0 -2
- package/dist/es/space/global-space.d.ts +0 -10
- package/dist/es/space/global-space.js +1 -35
- package/package.json +4 -4
|
@@ -8,8 +8,6 @@ exports.GlobalSpaceOptionsSchema = validator_1.Joi.object({
|
|
|
8
8
|
treeRepository: validator_1.Joi.any().required(),
|
|
9
9
|
objectRepository: validator_1.Joi.any().required(),
|
|
10
10
|
objectCollectionRepository: validator_1.Joi.any().required(),
|
|
11
|
-
gcMaxCount: validator_1.Joi.number().integer().optional().default(100),
|
|
12
|
-
gcMaxConcurrency: validator_1.Joi.number().integer().optional().default(8),
|
|
13
11
|
});
|
|
14
12
|
exports.HashSchema = validator_1.Joi.string()
|
|
15
13
|
.custom((value, helper) => {
|
|
@@ -6,16 +6,12 @@ export type GlobalSpaceOptions = {
|
|
|
6
6
|
treeRepository: TreeRepository;
|
|
7
7
|
objectRepository: ObjectRepository;
|
|
8
8
|
objectCollectionRepository: ObjectCollectionRepository;
|
|
9
|
-
gcMaxCount?: 100 | number;
|
|
10
|
-
gcMaxConcurrency?: 8 | number;
|
|
11
9
|
};
|
|
12
10
|
export declare class GlobalSpace {
|
|
13
11
|
readonly driver: DriverProtocol;
|
|
14
12
|
readonly treeRepository: TreeRepository;
|
|
15
13
|
readonly objectRepository: ObjectRepository;
|
|
16
14
|
readonly objectCollectionRepository: ObjectCollectionRepository;
|
|
17
|
-
readonly gcMaxCount: number;
|
|
18
|
-
readonly gcMaxConcurrency: number;
|
|
19
15
|
constructor(options: GlobalSpaceOptions);
|
|
20
16
|
/**
|
|
21
17
|
* @description 存储新对象到全局存储区
|
|
@@ -27,12 +23,6 @@ export declare class GlobalSpace {
|
|
|
27
23
|
*/
|
|
28
24
|
add(key: string, data: Data, options: WriteAsOwnerOptions): Promise<void>;
|
|
29
25
|
delete(hash: string): Promise<void>;
|
|
30
|
-
/**
|
|
31
|
-
* @description 清理未被使用的对象
|
|
32
|
-
* @return {Promise<void>} {Promise<void>}
|
|
33
|
-
* @memberof GlobalSpace
|
|
34
|
-
*/
|
|
35
|
-
gc(): Promise<void>;
|
|
36
26
|
/**
|
|
37
27
|
* @description 标记一个对象,以备之后回收
|
|
38
28
|
* @param {...string[]} hashList
|
|
@@ -14,19 +14,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.GlobalSpace = void 0;
|
|
16
16
|
const lodash_1 = require("lodash");
|
|
17
|
-
const sequelize_1 = require("sequelize");
|
|
18
17
|
const mime_types_1 = __importDefault(require("mime-types"));
|
|
19
18
|
const debug_1 = __importDefault(require("debug"));
|
|
20
|
-
const p_all_1 = __importDefault(require("p-all"));
|
|
21
|
-
const dayjs_1 = __importDefault(require("dayjs"));
|
|
22
19
|
const utils_1 = require("../utils");
|
|
23
20
|
const global_space_1 = require("../schemas/global-space");
|
|
24
21
|
const schemas_1 = require("../schemas");
|
|
25
22
|
const debug = (0, debug_1.default)('@did-space/core:GlobalSpace');
|
|
26
23
|
class GlobalSpace {
|
|
27
24
|
constructor(options) {
|
|
28
|
-
var _a, _b;
|
|
29
|
-
debug('GlobalSpace.constructor', JSON.stringify({ options: (0, lodash_1.pick)(options, 'gcMaxCount', 'gcMaxConcurrency') }));
|
|
30
25
|
const { error, value } = global_space_1.GlobalSpaceOptionsSchema.validate(options);
|
|
31
26
|
if (error) {
|
|
32
27
|
throw error;
|
|
@@ -35,9 +30,6 @@ class GlobalSpace {
|
|
|
35
30
|
this.treeRepository = value.treeRepository;
|
|
36
31
|
this.objectRepository = value.objectRepository;
|
|
37
32
|
this.objectCollectionRepository = value.objectCollectionRepository;
|
|
38
|
-
this.gcMaxCount = (_a = value.gcMaxCount) !== null && _a !== void 0 ? _a : 100;
|
|
39
|
-
this.gcMaxConcurrency = (_b = value.gcMaxConcurrency) !== null && _b !== void 0 ? _b : 8;
|
|
40
|
-
debug('GlobalSpace.constructor', JSON.stringify({ gcMaxCount: value.gcMaxCount, gcMaxConcurrency: value.gcMaxConcurrency }));
|
|
41
33
|
}
|
|
42
34
|
/**
|
|
43
35
|
* @description 存储新对象到全局存储区
|
|
@@ -133,33 +125,6 @@ class GlobalSpace {
|
|
|
133
125
|
debug('delete.after', { hash });
|
|
134
126
|
});
|
|
135
127
|
}
|
|
136
|
-
/**
|
|
137
|
-
* @description 清理未被使用的对象
|
|
138
|
-
* @return {Promise<void>} {Promise<void>}
|
|
139
|
-
* @memberof GlobalSpace
|
|
140
|
-
*/
|
|
141
|
-
gc() {
|
|
142
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
debug('gc.before', 'start gc...');
|
|
144
|
-
const objects = yield this.objectCollectionRepository.findAll({
|
|
145
|
-
where: {
|
|
146
|
-
createdAt: {
|
|
147
|
-
[sequelize_1.Op.lt]: (0, dayjs_1.default)().subtract(10, 'm').toDate(),
|
|
148
|
-
},
|
|
149
|
-
},
|
|
150
|
-
attributes: ['id'],
|
|
151
|
-
order: [['createdAt', 'ASC']],
|
|
152
|
-
limit: this.gcMaxCount,
|
|
153
|
-
});
|
|
154
|
-
debug('gc.$objects.length', objects.length);
|
|
155
|
-
const actions = objects.map((x) => {
|
|
156
|
-
return () => this.delete(x.id);
|
|
157
|
-
});
|
|
158
|
-
// 限制并发数量
|
|
159
|
-
yield (0, p_all_1.default)(actions, { concurrency: this.gcMaxConcurrency });
|
|
160
|
-
debug('gc.after', true);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
128
|
/**
|
|
164
129
|
* @description 标记一个对象,以备之后回收
|
|
165
130
|
* @param {...string[]} hashList
|
|
@@ -5,8 +5,6 @@ export const GlobalSpaceOptionsSchema = Joi.object({
|
|
|
5
5
|
treeRepository: Joi.any().required(),
|
|
6
6
|
objectRepository: Joi.any().required(),
|
|
7
7
|
objectCollectionRepository: Joi.any().required(),
|
|
8
|
-
gcMaxCount: Joi.number().integer().optional().default(100),
|
|
9
|
-
gcMaxConcurrency: Joi.number().integer().optional().default(8),
|
|
10
8
|
});
|
|
11
9
|
export const HashSchema = Joi.string()
|
|
12
10
|
.custom((value, helper) => {
|
|
@@ -6,16 +6,12 @@ export type GlobalSpaceOptions = {
|
|
|
6
6
|
treeRepository: TreeRepository;
|
|
7
7
|
objectRepository: ObjectRepository;
|
|
8
8
|
objectCollectionRepository: ObjectCollectionRepository;
|
|
9
|
-
gcMaxCount?: 100 | number;
|
|
10
|
-
gcMaxConcurrency?: 8 | number;
|
|
11
9
|
};
|
|
12
10
|
export declare class GlobalSpace {
|
|
13
11
|
readonly driver: DriverProtocol;
|
|
14
12
|
readonly treeRepository: TreeRepository;
|
|
15
13
|
readonly objectRepository: ObjectRepository;
|
|
16
14
|
readonly objectCollectionRepository: ObjectCollectionRepository;
|
|
17
|
-
readonly gcMaxCount: number;
|
|
18
|
-
readonly gcMaxConcurrency: number;
|
|
19
15
|
constructor(options: GlobalSpaceOptions);
|
|
20
16
|
/**
|
|
21
17
|
* @description 存储新对象到全局存储区
|
|
@@ -27,12 +23,6 @@ export declare class GlobalSpace {
|
|
|
27
23
|
*/
|
|
28
24
|
add(key: string, data: Data, options: WriteAsOwnerOptions): Promise<void>;
|
|
29
25
|
delete(hash: string): Promise<void>;
|
|
30
|
-
/**
|
|
31
|
-
* @description 清理未被使用的对象
|
|
32
|
-
* @return {Promise<void>} {Promise<void>}
|
|
33
|
-
* @memberof GlobalSpace
|
|
34
|
-
*/
|
|
35
|
-
gc(): Promise<void>;
|
|
36
26
|
/**
|
|
37
27
|
* @description 标记一个对象,以备之后回收
|
|
38
28
|
* @param {...string[]} hashList
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { merge, omit
|
|
2
|
-
import { Op } from 'sequelize';
|
|
1
|
+
import { merge, omit } from 'lodash';
|
|
3
2
|
import mimeTypes from 'mime-types';
|
|
4
3
|
import Debug from 'debug';
|
|
5
|
-
import pAll from 'p-all';
|
|
6
|
-
import dayjs from 'dayjs';
|
|
7
4
|
import { getHashPath } from '../utils';
|
|
8
5
|
import { GlobalSpaceOptionsSchema, HashSchema } from '../schemas/global-space';
|
|
9
6
|
import { WriteAsOwnerOptionsSchema } from '../schemas';
|
|
@@ -13,10 +10,7 @@ export class GlobalSpace {
|
|
|
13
10
|
treeRepository;
|
|
14
11
|
objectRepository;
|
|
15
12
|
objectCollectionRepository;
|
|
16
|
-
gcMaxCount;
|
|
17
|
-
gcMaxConcurrency;
|
|
18
13
|
constructor(options) {
|
|
19
|
-
debug('GlobalSpace.constructor', JSON.stringify({ options: pick(options, 'gcMaxCount', 'gcMaxConcurrency') }));
|
|
20
14
|
const { error, value } = GlobalSpaceOptionsSchema.validate(options);
|
|
21
15
|
if (error) {
|
|
22
16
|
throw error;
|
|
@@ -25,9 +19,6 @@ export class GlobalSpace {
|
|
|
25
19
|
this.treeRepository = value.treeRepository;
|
|
26
20
|
this.objectRepository = value.objectRepository;
|
|
27
21
|
this.objectCollectionRepository = value.objectCollectionRepository;
|
|
28
|
-
this.gcMaxCount = value.gcMaxCount ?? 100;
|
|
29
|
-
this.gcMaxConcurrency = value.gcMaxConcurrency ?? 8;
|
|
30
|
-
debug('GlobalSpace.constructor', JSON.stringify({ gcMaxCount: value.gcMaxCount, gcMaxConcurrency: value.gcMaxConcurrency }));
|
|
31
22
|
}
|
|
32
23
|
/**
|
|
33
24
|
* @description 存储新对象到全局存储区
|
|
@@ -122,31 +113,6 @@ export class GlobalSpace {
|
|
|
122
113
|
await this.objectCollectionRepository.destroy({ where: { id: hash } });
|
|
123
114
|
debug('delete.after', { hash });
|
|
124
115
|
}
|
|
125
|
-
/**
|
|
126
|
-
* @description 清理未被使用的对象
|
|
127
|
-
* @return {Promise<void>} {Promise<void>}
|
|
128
|
-
* @memberof GlobalSpace
|
|
129
|
-
*/
|
|
130
|
-
async gc() {
|
|
131
|
-
debug('gc.before', 'start gc...');
|
|
132
|
-
const objects = await this.objectCollectionRepository.findAll({
|
|
133
|
-
where: {
|
|
134
|
-
createdAt: {
|
|
135
|
-
[Op.lt]: dayjs().subtract(10, 'm').toDate(),
|
|
136
|
-
},
|
|
137
|
-
},
|
|
138
|
-
attributes: ['id'],
|
|
139
|
-
order: [['createdAt', 'ASC']],
|
|
140
|
-
limit: this.gcMaxCount,
|
|
141
|
-
});
|
|
142
|
-
debug('gc.$objects.length', objects.length);
|
|
143
|
-
const actions = objects.map((x) => {
|
|
144
|
-
return () => this.delete(x.id);
|
|
145
|
-
});
|
|
146
|
-
// 限制并发数量
|
|
147
|
-
await pAll(actions, { concurrency: this.gcMaxConcurrency });
|
|
148
|
-
debug('gc.after', true);
|
|
149
|
-
}
|
|
150
116
|
/**
|
|
151
117
|
* @description 标记一个对象,以备之后回收
|
|
152
118
|
* @param {...string[]} hashList
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-space/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -115,9 +115,9 @@
|
|
|
115
115
|
"watch": "npm run build -- -w"
|
|
116
116
|
},
|
|
117
117
|
"dependencies": {
|
|
118
|
-
"@arcblock/did": "^1.18.
|
|
118
|
+
"@arcblock/did": "^1.18.165",
|
|
119
119
|
"@arcblock/ipfs-only-hash": "^0.0.2",
|
|
120
|
-
"@arcblock/validator": "^1.18.
|
|
120
|
+
"@arcblock/validator": "^1.18.165",
|
|
121
121
|
"dayjs": "^1.11.13",
|
|
122
122
|
"debug": "^4.4.0",
|
|
123
123
|
"hasha": "^5.2.2",
|
|
@@ -145,5 +145,5 @@
|
|
|
145
145
|
"vite": "^5.4.11",
|
|
146
146
|
"vitest": "^2.1.8"
|
|
147
147
|
},
|
|
148
|
-
"gitHead": "
|
|
148
|
+
"gitHead": "6a7b8a17b261f77eec2b4cf1a7955bb076b6ceeb"
|
|
149
149
|
}
|