@did-space/core 0.1.68 → 0.1.70

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.
@@ -24,6 +24,11 @@ export declare class Space implements SpaceProtocol {
24
24
  isListable(options: PermissionOptions): Promise<boolean>;
25
25
  isReadable(options: PermissionOptions): Promise<boolean>;
26
26
  isWritable(options: PermissionOptions): Promise<boolean>;
27
+ getSpaceStatus(): Promise<{
28
+ totalCapacity: number;
29
+ usedCapacity: number;
30
+ isFull: boolean;
31
+ }>;
27
32
  write(options: WriteOptions): Promise<void>;
28
33
  delete(options: DeleteOptions): Promise<void>;
29
34
  read(options: ReadOptions): Promise<Stream>;
@@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.Space = void 0;
16
+ const xbytes_1 = __importDefault(require("xbytes"));
13
17
  class Space {
14
18
  constructor(driver) {
15
19
  this.driver = driver;
@@ -99,8 +103,48 @@ class Space {
99
103
  return this.driver.isWritable(options);
100
104
  });
101
105
  }
106
+ getSpaceStatus() {
107
+ var _a, _b;
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ // FIXME: 临时性解决方案,需要新增拓容功能,需要去除白名单功能
110
+ const [totalCapacity, usedCapacity] = yield Promise.all([this.driver.get('size'), this.driver.getSpaceSize()]);
111
+ const whiteList = ((_b = (_a = process.env) === null || _a === void 0 ? void 0 : _a.SPACE_WHITE_LIST) !== null && _b !== void 0 ? _b : '')
112
+ .split(',')
113
+ .map((s) => s.trim())
114
+ .filter(Boolean);
115
+ // 跳过白名单
116
+ if (whiteList.length && whiteList.includes(this.driver.options.root)) {
117
+ return {
118
+ isFull: false,
119
+ totalCapacity,
120
+ usedCapacity,
121
+ };
122
+ }
123
+ // 存储空间不足
124
+ if (usedCapacity >= totalCapacity) {
125
+ return {
126
+ isFull: true,
127
+ totalCapacity,
128
+ usedCapacity,
129
+ };
130
+ }
131
+ return {
132
+ isFull: false,
133
+ totalCapacity,
134
+ usedCapacity,
135
+ };
136
+ });
137
+ }
102
138
  write(options) {
103
139
  return __awaiter(this, void 0, void 0, function* () {
140
+ // FIXME: 插入数据和编辑数据应该分离?
141
+ if (yield this.exists(options)) {
142
+ return this.driver.write(options);
143
+ }
144
+ const spaceStatus = yield this.getSpaceStatus();
145
+ if (spaceStatus.isFull) {
146
+ throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity)}, used capacity is ${(0, xbytes_1.default)(spaceStatus.usedCapacity)}`);
147
+ }
104
148
  return this.driver.write(options);
105
149
  });
106
150
  }
@@ -136,6 +180,14 @@ class Space {
136
180
  }
137
181
  writeAsOwner(key, data) {
138
182
  return __awaiter(this, void 0, void 0, function* () {
183
+ // FIXME: 插入数据和编辑数据应该分离
184
+ if (yield this.existsAsOwner(key)) {
185
+ return this.driver.writeAsOwner(key, data);
186
+ }
187
+ const spaceStatus = yield this.getSpaceStatus();
188
+ if (spaceStatus.isFull) {
189
+ throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity)}, used capacity is ${(0, xbytes_1.default)(spaceStatus.usedCapacity)}`);
190
+ }
139
191
  return this.driver.writeAsOwner(key, data);
140
192
  });
141
193
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/core",
3
- "version": "0.1.68",
3
+ "version": "0.1.70",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -61,5 +61,5 @@
61
61
  "ts-jest": "^28.0.6",
62
62
  "typescript": "4.4.4"
63
63
  },
64
- "gitHead": "df55bed8c071fbc8f72a6566914c2e698bc4f5e5"
64
+ "gitHead": "1c67a8376a2ab06e06c771d34149be4fefe1e7c7"
65
65
  }