@did-space/fs-driver 0.2.172 → 0.3.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.
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ReadOptions, WriteOptions, SpaceOperatorProtocol, SpaceConfig, AppSpaceOptions, DeleteOptions, ListOptions, Object, Data, ListsOptions, GetHashOptions } from '@did-space/core';
2
+ import { ReadOptions, WriteOptions, SpaceOperatorProtocol, SpaceConfig, AppSpaceOptions, DeleteOptions, ListOptions, Object, Data, ListsOptions, GetHashOptions, WriteAsOwnerOptions, ReadAsOwnerOptions, DeleteAsOwnerOptions, ExistsAsOwnerOptions } from '@did-space/core';
3
3
  import { Stream } from 'stream';
4
4
  type PathStatus = {
5
5
  objects: number;
@@ -19,7 +19,8 @@ export declare class FsSpaceOperator implements SpaceOperatorProtocol {
19
19
  isSpaceCreated(): Promise<boolean>;
20
20
  getSpaceSize(): Promise<number>;
21
21
  /**
22
- * @description @jianchao 这个 getStatusAsOwner 应该 join blocklet 的 data dir,目前不修改,因为需要充分测试
22
+ * @FIXME @jianchao 这个 getStatusAsOwner 应该 join blocklet 的 data dir,目前不修改,因为需要充分测试
23
+ * @description
23
24
  * @param {string} path
24
25
  * @return {*} {Promise<PathStatus>}
25
26
  * @memberof FsSpaceOperator
@@ -43,9 +44,9 @@ export declare class FsSpaceOperator implements SpaceOperatorProtocol {
43
44
  */
44
45
  lists(options: ListsOptions): Promise<Object[]>;
45
46
  list(options: ListOptions): Promise<Object>;
46
- writeAsOwner(key: string, data: Data): Promise<void>;
47
- deleteAsOwner(key: string): Promise<void>;
48
- readAsOwner(key: string): Promise<Stream>;
49
- existsAsOwner(key: string): Promise<boolean>;
47
+ writeAsOwner(key: string, data: Data, options?: WriteAsOwnerOptions): Promise<void>;
48
+ deleteAsOwner(key: string, options?: DeleteAsOwnerOptions): Promise<void>;
49
+ readAsOwner(key: string, options?: ReadAsOwnerOptions): Promise<Stream>;
50
+ existsAsOwner(key: string, options?: ExistsAsOwnerOptions): Promise<boolean>;
50
51
  }
51
52
  export {};
@@ -1,4 +1,27 @@
1
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
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -16,16 +39,21 @@ exports.FsSpaceOperator = void 0;
16
39
  /* eslint-disable @typescript-eslint/no-unused-vars */
17
40
  const core_1 = require("@did-space/core");
18
41
  const filehound_1 = __importDefault(require("filehound"));
19
- const fs_extra_1 = __importDefault(require("fs-extra"));
42
+ const fs_extra_1 = __importStar(require("fs-extra"));
20
43
  const lodash_1 = require("lodash");
21
44
  const mime_types_1 = __importDefault(require("mime-types"));
22
45
  const path_1 = require("path");
23
46
  const stream_1 = require("stream");
24
- const hasha_1 = __importDefault(require("hasha"));
47
+ const debug_1 = __importDefault(require("debug"));
25
48
  const read_stream_to_file_1 = require("./read-stream-to-file");
49
+ const debug = (0, debug_1.default)('@did-space/fs-driver:FsSpaceOperator');
50
+ function getPrefix(root) {
51
+ return root.split('/').slice(0, -1).join('/');
52
+ }
26
53
  class FsSpaceOperator {
27
54
  constructor(options) {
28
55
  this.options = options;
56
+ debug('FsSpaceOperator.constructor', JSON.stringify({ options }));
29
57
  }
30
58
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
31
59
  createSpace(_spaceConfig) {
@@ -45,7 +73,8 @@ class FsSpaceOperator {
45
73
  });
46
74
  }
47
75
  /**
48
- * @description @jianchao 这个 getStatusAsOwner 应该 join blocklet 的 data dir,目前不修改,因为需要充分测试
76
+ * @FIXME @jianchao 这个 getStatusAsOwner 应该 join blocklet 的 data dir,目前不修改,因为需要充分测试
77
+ * @description
49
78
  * @param {string} path
50
79
  * @return {*} {Promise<PathStatus>}
51
80
  * @memberof FsSpaceOperator
@@ -121,9 +150,7 @@ class FsSpaceOperator {
121
150
  getHash(options) {
122
151
  return __awaiter(this, void 0, void 0, function* () {
123
152
  const streamData = yield this.read(options);
124
- const hash = yield hasha_1.default.fromStream(streamData, {
125
- algorithm: 'md5',
126
- });
153
+ const hash = yield (0, core_1.getHash)(streamData);
127
154
  return hash;
128
155
  });
129
156
  }
@@ -141,8 +168,14 @@ class FsSpaceOperator {
141
168
  */
142
169
  lists(options) {
143
170
  return __awaiter(this, void 0, void 0, function* () {
144
- const path = (0, path_1.join)(this.options.root, options.key);
145
- if (!(yield this.existsAsOwner(options.key))) {
171
+ debug('lists.before', JSON.stringify({ options }));
172
+ let path = (0, path_1.join)(this.options.root, options.key);
173
+ if (options === null || options === void 0 ? void 0 : options.useGlobal) {
174
+ const prefix = this.options.root.split('/').slice(0, -1).join('/');
175
+ path = (0, path_1.join)(prefix, options.key);
176
+ debug('lists.$prefix', prefix);
177
+ }
178
+ if (!(yield this.existsAsOwner(options.key, options))) {
146
179
  return [];
147
180
  }
148
181
  // 以 非递归的方式 && 显示文件夹 的方式列出当前文件
@@ -214,9 +247,18 @@ class FsSpaceOperator {
214
247
  mimeType: mime_types_1.default.lookup(objectName) || 'unknown',
215
248
  };
216
249
  }
217
- writeAsOwner(key, data) {
250
+ writeAsOwner(key, data, options) {
218
251
  return __awaiter(this, void 0, void 0, function* () {
219
- const objectPath = (0, path_1.join)(this.options.root, key);
252
+ debug('writeAsOwner.before', JSON.stringify({ key, options }));
253
+ let objectPath = (0, path_1.join)(this.options.root, key);
254
+ if (options === null || options === void 0 ? void 0 : options.useGlobal) {
255
+ const prefix = this.options.root.split('/').slice(0, -1).join('/');
256
+ objectPath = (0, path_1.join)(prefix, key);
257
+ debug('writeAsOwner.$prefix', prefix);
258
+ }
259
+ yield (0, fs_extra_1.ensureDir)((0, path_1.dirname)(objectPath));
260
+ debug('writeAsOwner.$dirname(objectPath)', (0, path_1.dirname)(objectPath));
261
+ debug('writeAsOwner.$objectPath', objectPath);
220
262
  if (objectPath.endsWith('/')) {
221
263
  // 这是一个文件夹
222
264
  yield fs_extra_1.default.ensureDir(objectPath);
@@ -233,16 +275,42 @@ class FsSpaceOperator {
233
275
  }
234
276
  });
235
277
  }
236
- deleteAsOwner(key) {
237
- return fs_extra_1.default.remove((0, path_1.join)(this.options.root, key));
278
+ deleteAsOwner(key, options) {
279
+ debug('deleteAsOwner.before', JSON.stringify({ key, options }));
280
+ debug('deleteAsOwner.$this.options.root', this.options.root);
281
+ let $key = (0, path_1.join)(this.options.root, key);
282
+ if (options === null || options === void 0 ? void 0 : options.useGlobal) {
283
+ const prefix = this.options.root.split('/').slice(0, -1).join('/');
284
+ $key = (0, path_1.join)(prefix, key);
285
+ debug('deleteAsOwner.$prefix', prefix);
286
+ }
287
+ debug('deleteAsOwner.$$key', $key);
288
+ return fs_extra_1.default.remove($key);
238
289
  }
239
- readAsOwner(key) {
240
- // FIXME: @yejianchao 应该可以refactor,但是我还需要测试一下
241
- return fs_extra_1.default.readFile((0, path_1.join)(this.options.root, key)).then((data) => stream_1.Readable.from(data));
290
+ readAsOwner(key, options) {
291
+ debug('readAsOwner.before', JSON.stringify({ key, options }));
292
+ // FIXME: @yejianchao 这里的 key 不能以 / 开头
293
+ let $key = (0, path_1.join)(this.options.root, key);
294
+ if (options === null || options === void 0 ? void 0 : options.useGlobal) {
295
+ const prefix = this.options.root.split('/').slice(0, -1).join('/');
296
+ $key = (0, path_1.join)(prefix, key);
297
+ debug('readAsOwner.$prefix', prefix);
298
+ }
299
+ debug('readAsOwner.$key', $key);
300
+ // @ts-expect-error
301
+ return fs_extra_1.default.createReadStream($key);
242
302
  }
243
- existsAsOwner(key) {
303
+ existsAsOwner(key, options) {
304
+ debug('existsAsOwner.before', JSON.stringify({ key, options }));
305
+ let path = (0, path_1.join)(this.options.root, key);
306
+ if (options === null || options === void 0 ? void 0 : options.useGlobal) {
307
+ const prefix = getPrefix(this.options.root);
308
+ path = (0, path_1.join)(prefix, key);
309
+ debug('existsAsOwner.$prefix', prefix);
310
+ }
311
+ debug('existsAsOwner.$path', path);
244
312
  // @ts-expect-error
245
- return fs_extra_1.default.existsSync((0, path_1.join)(this.options.root, key));
313
+ return fs_extra_1.default.existsSync(path);
246
314
  }
247
315
  }
248
316
  exports.FsSpaceOperator = FsSpaceOperator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/fs-driver",
3
- "version": "0.2.172",
3
+ "version": "0.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -32,7 +32,8 @@
32
32
  ]
33
33
  },
34
34
  "dependencies": {
35
- "@did-space/core": "0.2.172",
35
+ "@did-space/core": "0.3.0",
36
+ "debug": "^4.3.4",
36
37
  "filehound": "^1.17.6",
37
38
  "fs-extra": "^10.1.0",
38
39
  "hasha": "^5.2.2",
@@ -55,5 +56,5 @@
55
56
  "ts-jest": "^28.0.6",
56
57
  "typescript": "^4.9.5"
57
58
  },
58
- "gitHead": "0f221e8d8ec17645130499297d7a4a4ae0ef0efb"
59
+ "gitHead": "a883d2a4ab600138c8a23093ace6b94d9107e43e"
59
60
  }