@did-space/fs-driver 0.3.21 → 0.3.23

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.
@@ -43,7 +43,7 @@ export declare class FsSpaceOperator implements SpaceOperatorProtocol {
43
43
  * @memberof FsSpaceOperator
44
44
  */
45
45
  lists(options: ListsOptions): Promise<Object[]>;
46
- list(options: ListOptions): Promise<Object>;
46
+ list(options: ListOptions): Promise<Object | null>;
47
47
  writeAsOwner(key: string, data: Data, options?: WriteAsOwnerOptions): Promise<void>;
48
48
  deleteAsOwner(key: string, options?: DeleteAsOwnerOptions): Promise<void>;
49
49
  readAsOwner(key: string, options?: ReadAsOwnerOptions): Promise<Stream>;
@@ -45,7 +45,7 @@ const mime_types_1 = __importDefault(require("mime-types"));
45
45
  const path_1 = require("path");
46
46
  const stream_1 = require("stream");
47
47
  const debug_1 = __importDefault(require("debug"));
48
- const read_stream_to_file_1 = require("./read-stream-to-file");
48
+ const promises_1 = require("stream/promises");
49
49
  const debug = (0, debug_1.default)('@did-space/fs-driver:FsSpaceOperator');
50
50
  function getPrefix(root) {
51
51
  return root.split('/').slice(0, -1).join('/');
@@ -120,6 +120,7 @@ class FsSpaceOperator {
120
120
  return __awaiter(this, void 0, void 0, function* () {
121
121
  const objectPath = yield this.getObjectKey(options);
122
122
  const { data } = options;
123
+ yield fs_extra_1.default.ensureDir((0, path_1.dirname)(objectPath));
123
124
  if (objectPath.endsWith('/')) {
124
125
  // 这是一个文件夹
125
126
  yield fs_extra_1.default.ensureDir(objectPath);
@@ -128,8 +129,7 @@ class FsSpaceOperator {
128
129
  yield fs_extra_1.default.outputFile(objectPath, data);
129
130
  }
130
131
  else if (data instanceof stream_1.Stream || data instanceof stream_1.Readable || (0, lodash_1.isFunction)(data === null || data === void 0 ? void 0 : data.pipe)) {
131
- fs_extra_1.default.ensureDirSync((0, path_1.dirname)(objectPath));
132
- yield (0, read_stream_to_file_1.readStreamToFile)(data, fs_extra_1.default.createWriteStream(objectPath));
132
+ yield (0, promises_1.pipeline)(data, fs_extra_1.default.createWriteStream(objectPath));
133
133
  }
134
134
  else {
135
135
  throw new Error('Data is unsupported format, expected data to be one of: string | Buffer | ReadableStream.');
@@ -171,7 +171,7 @@ class FsSpaceOperator {
171
171
  debug('lists.before', JSON.stringify({ options }));
172
172
  let path = (0, path_1.join)(this.options.root, options.key);
173
173
  if (options === null || options === void 0 ? void 0 : options.useGlobal) {
174
- const prefix = this.options.root.split('/').slice(0, -1).join('/');
174
+ const prefix = getPrefix(this.options.root);
175
175
  path = (0, path_1.join)(prefix, options.key);
176
176
  debug('lists.$prefix', prefix);
177
177
  }
@@ -231,7 +231,14 @@ class FsSpaceOperator {
231
231
  }
232
232
  list(options) {
233
233
  return __awaiter(this, void 0, void 0, function* () {
234
- const objectKey = (0, path_1.join)(this.options.root, options.key);
234
+ debug('list.before', { options: (0, lodash_1.omit)(options, 'data') });
235
+ let objectKey = (0, path_1.join)(this.options.root, options.key);
236
+ if (options === null || options === void 0 ? void 0 : options.useGlobal) {
237
+ const prefix = getPrefix(this.options.root);
238
+ objectKey = (0, path_1.join)(prefix, options.key);
239
+ debug('lists.$prefix', prefix);
240
+ }
241
+ debug('list.$objectKey', objectKey);
235
242
  if (!fs_extra_1.default.existsSync(objectKey)) {
236
243
  return null;
237
244
  }
@@ -256,27 +263,34 @@ class FsSpaceOperator {
256
263
  debug('writeAsOwner.before', JSON.stringify({ key, options: (0, lodash_1.omit)(options, 'data') }));
257
264
  let objectPath = (0, path_1.join)(this.options.root, key);
258
265
  if (options === null || options === void 0 ? void 0 : options.useGlobal) {
259
- const prefix = this.options.root.split('/').slice(0, -1).join('/');
266
+ const prefix = getPrefix(this.options.root);
260
267
  objectPath = (0, path_1.join)(prefix, key);
261
268
  debug('writeAsOwner.$prefix', prefix);
262
269
  }
263
270
  yield (0, fs_extra_1.ensureDir)((0, path_1.dirname)(objectPath));
264
271
  debug('writeAsOwner.$dirname(objectPath)', (0, path_1.dirname)(objectPath));
265
272
  debug('writeAsOwner.$objectPath', objectPath);
266
- if (objectPath.endsWith('/')) {
273
+ const isDirectory = objectPath.endsWith('/');
274
+ if (isDirectory) {
267
275
  // 这是一个文件夹
268
276
  yield fs_extra_1.default.ensureDir(objectPath);
277
+ return;
269
278
  }
270
- else if ((0, lodash_1.isString)(data) || (0, lodash_1.isBuffer)(data)) {
271
- yield fs_extra_1.default.outputFile(objectPath, data);
279
+ if (!(0, core_1.isValidData)(data)) {
280
+ throw new Error('Data is unsupported format, expected data to be one of: string | Buffer | ReadableStream.');
272
281
  }
273
- else if (data instanceof stream_1.Stream || data instanceof stream_1.Readable || (0, lodash_1.isFunction)(data === null || data === void 0 ? void 0 : data.pipe)) {
274
- fs_extra_1.default.ensureDirSync((0, path_1.dirname)(objectPath));
275
- yield (0, read_stream_to_file_1.readStreamToFile)(data, fs_extra_1.default.createWriteStream(objectPath));
282
+ if ((0, lodash_1.isString)(data) || (0, lodash_1.isBuffer)(data)) {
283
+ yield fs_extra_1.default.outputFile(objectPath, data);
276
284
  }
277
285
  else {
278
- throw new Error('Data is unsupported format, expected data to be one of: string | Buffer | ReadableStream.');
286
+ yield (0, promises_1.pipeline)(data, fs_extra_1.default.createWriteStream(objectPath));
279
287
  }
288
+ const object = yield this.list(Object.assign(Object.assign({}, options), { key }));
289
+ if (!isDirectory && options && (!object || object.size !== (options === null || options === void 0 ? void 0 : options.size))) {
290
+ core_1.logger.error('writeAsOwner.throwError', { key, options });
291
+ throw new Error(`Uploaded size ${object === null || object === void 0 ? void 0 : object.size} does not match actual size ${options === null || options === void 0 ? void 0 : options.size}`);
292
+ }
293
+ debug('writeAsOwner.after', JSON.stringify({ key, options: (0, lodash_1.omit)(options, 'data') }));
280
294
  });
281
295
  }
282
296
  deleteAsOwner(key, options) {
@@ -284,7 +298,7 @@ class FsSpaceOperator {
284
298
  debug('deleteAsOwner.$this.options.root', this.options.root);
285
299
  let $key = (0, path_1.join)(this.options.root, key);
286
300
  if (options === null || options === void 0 ? void 0 : options.useGlobal) {
287
- const prefix = this.options.root.split('/').slice(0, -1).join('/');
301
+ const prefix = getPrefix(this.options.root);
288
302
  $key = (0, path_1.join)(prefix, key);
289
303
  debug('deleteAsOwner.$prefix', prefix);
290
304
  }
@@ -296,7 +310,7 @@ class FsSpaceOperator {
296
310
  // FIXME: @yejianchao 这里的 key 不能以 / 开头
297
311
  let $key = (0, path_1.join)(this.options.root, key);
298
312
  if (options === null || options === void 0 ? void 0 : options.useGlobal) {
299
- const prefix = this.options.root.split('/').slice(0, -1).join('/');
313
+ const prefix = getPrefix(this.options.root);
300
314
  $key = (0, path_1.join)(prefix, key);
301
315
  debug('readAsOwner.$prefix', prefix);
302
316
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/fs-driver",
3
- "version": "0.3.21",
3
+ "version": "0.3.23",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -36,7 +36,7 @@
36
36
  ]
37
37
  },
38
38
  "dependencies": {
39
- "@did-space/core": "0.3.21",
39
+ "@did-space/core": "0.3.23",
40
40
  "debug": "^4.3.4",
41
41
  "filehound": "^1.17.6",
42
42
  "fs-extra": "^10.1.0",
@@ -60,5 +60,5 @@
60
60
  "ts-jest": "^28.0.6",
61
61
  "typescript": "^4.9.5"
62
62
  },
63
- "gitHead": "558719cb308f43ce1cc62c60125522726d8e1014"
63
+ "gitHead": "5e918882de7560a6fcb4e4cfa64272d3ac6162e3"
64
64
  }
@@ -1,6 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { Stream } from 'stream';
4
- import { WriteStream } from 'fs';
5
- declare function readStreamToFile(readStream: Stream, writeStream: WriteStream): Promise<void>;
6
- export { readStreamToFile };
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readStreamToFile = void 0;
4
- const core_1 = require("@did-space/core");
5
- function readStreamToFile(readStream, writeStream) {
6
- return new Promise((resolve, reject) => {
7
- writeStream.on('data', () => {
8
- return resolve();
9
- });
10
- writeStream.on('finish', () => {
11
- return resolve();
12
- });
13
- writeStream.on('error', (error) => {
14
- core_1.logger.error('readStreamToFile', error.message);
15
- return reject(error);
16
- });
17
- readStream.pipe(writeStream);
18
- });
19
- }
20
- exports.readStreamToFile = readStreamToFile;