@did-space/core 0.5.31 → 0.5.32

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.
@@ -27,7 +27,6 @@ const model_1 = require("../model");
27
27
  const utils_1 = require("../utils");
28
28
  const schemas_1 = require("../schemas");
29
29
  const constants_1 = require("../constants");
30
- const stream_1 = require("../utils/stream");
31
30
  const global_space_1 = require("./global-space");
32
31
  const debug = (0, debug_1.default)('@did-space/core:ObjectSpace');
33
32
  class ObjectSpace extends events_1.default {
@@ -336,7 +335,7 @@ class ObjectSpace extends events_1.default {
336
335
  if (error) {
337
336
  throw error;
338
337
  }
339
- if (!(0, utils_1.isDirectory)(key) && !(0, stream_1.isStream)(data)) {
338
+ if (!(0, utils_1.isDirectory)(key) && !(0, utils_1.isStream)(data)) {
340
339
  options.hash = options.hash || (yield (0, utils_1.getHash)(data));
341
340
  options.size = options.size || (0, utils_1.getSize)(data);
342
341
  // @note: size 可能是空的
@@ -632,7 +631,7 @@ class ObjectSpace extends events_1.default {
632
631
  return __awaiter(this, void 0, void 0, function* () {
633
632
  debug('set.before', JSON.stringify({ key, value }));
634
633
  const configData = yield this.readAsOwner('/config.yml');
635
- const data = js_yaml_1.default.load(yield (0, stream_1.streamToString)(configData));
634
+ const data = js_yaml_1.default.load(yield (0, utils_1.streamToString)(configData));
636
635
  data[key] = value;
637
636
  yield this.createConfig(data);
638
637
  });
@@ -642,7 +641,7 @@ class ObjectSpace extends events_1.default {
642
641
  return __awaiter(this, void 0, void 0, function* () {
643
642
  debug('get.before', JSON.stringify({ key }));
644
643
  const configData = yield this.readAsOwner('/config.yml');
645
- const data = js_yaml_1.default.load(yield (0, stream_1.streamToString)(configData));
644
+ const data = js_yaml_1.default.load(yield (0, utils_1.streamToString)(configData));
646
645
  return (_a = data[key]) !== null && _a !== void 0 ? _a : defaultValue;
647
646
  });
648
647
  }
@@ -1,4 +1,5 @@
1
1
  import { Data } from '../meta';
2
2
  export declare function getSize(data?: Data): number;
3
3
  export declare function isDirectory(key: string): boolean;
4
+ export declare function isStream(data: any): boolean;
4
5
  export declare function isValidData(data: Data): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidData = exports.isDirectory = exports.getSize = void 0;
3
+ exports.isValidData = exports.isStream = exports.isDirectory = exports.getSize = void 0;
4
4
  const lodash_1 = require("lodash");
5
5
  const stream_1 = require("stream");
6
6
  function getSize(data = '') {
@@ -17,13 +17,11 @@ function isDirectory(key) {
17
17
  return key.endsWith('/');
18
18
  }
19
19
  exports.isDirectory = isDirectory;
20
+ function isStream(data) {
21
+ return data instanceof stream_1.Stream || data instanceof stream_1.Readable || (0, lodash_1.isFunction)(data === null || data === void 0 ? void 0 : data.pipe);
22
+ }
23
+ exports.isStream = isStream;
20
24
  function isValidData(data) {
21
- if ((0, lodash_1.isString)(data) || (0, lodash_1.isBuffer)(data)) {
22
- return true;
23
- }
24
- if (data instanceof stream_1.Stream || data instanceof stream_1.Readable || (0, lodash_1.isFunction)(data === null || data === void 0 ? void 0 : data.pipe)) {
25
- return true;
26
- }
27
- return false;
25
+ return (0, lodash_1.isString)(data) || (0, lodash_1.isBuffer)(data) || isStream(data);
28
26
  }
29
27
  exports.isValidData = isValidData;
@@ -1,4 +1,6 @@
1
+ import type { LiteralUnion } from 'type-fest';
1
2
  import { Data } from '../meta';
3
+ export type Algorithm = LiteralUnion<'ipfs' | 'sha256', string>;
2
4
  /**
3
5
  *
4
6
  * @see cid各个版本 https://docs.ipfs.tech/concepts/content-addressing/#cid-versions
@@ -9,7 +11,16 @@ import { Data } from '../meta';
9
11
  * @param {Data} data
10
12
  * @return {Promise<string>} {Promise<string>}
11
13
  */
12
- export declare function getHash(data?: Data): Promise<string>;
14
+ export declare function getHashByIpfs(data?: Data): Promise<string>;
15
+ export declare function getHashByAlgorithm(data?: Data, algorithm?: Algorithm): Promise<string>;
16
+ /**
17
+ *
18
+ *
19
+ * @export
20
+ * @param {Data} [data='']
21
+ * @param {AlgorithmName} [algorithm='ipfs']
22
+ */
23
+ export declare function getHash(data?: Data, algorithm?: Algorithm): Promise<string>;
13
24
  /**
14
25
  * @description
15
26
  * @export
@@ -8,12 +8,16 @@ 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
- exports.isCidV1 = exports.getHashPath = exports.getHash = void 0;
15
+ exports.isCidV1 = exports.getHashPath = exports.getHash = exports.getHashByAlgorithm = exports.getHashByIpfs = void 0;
13
16
  const path_1 = require("path");
14
17
  const ipfs_only_hash_1 = require("@arcblock/ipfs-only-hash");
15
18
  const multiformats_1 = require("multiformats");
16
- const stream_1 = require("./stream");
19
+ const hasha_1 = __importDefault(require("hasha"));
20
+ const common_1 = require("./common");
17
21
  /**
18
22
  *
19
23
  * @see cid各个版本 https://docs.ipfs.tech/concepts/content-addressing/#cid-versions
@@ -24,13 +28,40 @@ const stream_1 = require("./stream");
24
28
  * @param {Data} data
25
29
  * @return {Promise<string>} {Promise<string>}
26
30
  */
27
- function getHash(data = '') {
31
+ function getHashByIpfs(data = '') {
28
32
  return __awaiter(this, void 0, void 0, function* () {
29
- if (typeof data === 'string' || Buffer.isBuffer(data) || (0, stream_1.isStream)(data)) {
30
- const cidV0 = yield (0, ipfs_only_hash_1.onlyHash)(data);
31
- return multiformats_1.CID.parse(cidV0).toV1().toString();
33
+ const cidV0 = yield (0, ipfs_only_hash_1.onlyHash)(data);
34
+ return multiformats_1.CID.parse(cidV0).toV1().toString();
35
+ });
36
+ }
37
+ exports.getHashByIpfs = getHashByIpfs;
38
+ function getHashByAlgorithm(data = '', algorithm = 'sha256') {
39
+ if (typeof data === 'string' || Buffer.isBuffer(data)) {
40
+ return hasha_1.default.async(data, { algorithm });
41
+ }
42
+ return hasha_1.default.fromStream(data, { algorithm });
43
+ }
44
+ exports.getHashByAlgorithm = getHashByAlgorithm;
45
+ /**
46
+ *
47
+ *
48
+ * @export
49
+ * @param {Data} [data='']
50
+ * @param {AlgorithmName} [algorithm='ipfs']
51
+ */
52
+ // eslint-disable-next-line require-await
53
+ function getHash(data = '', algorithm = 'ipfs') {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ if (!['ipfs', 'sha256'].includes(algorithm)) {
56
+ throw new Error(`#getHash() Invalid algorithm type: ${algorithm}`);
57
+ }
58
+ if (!(0, common_1.isValidData)(data)) {
59
+ throw new Error(`#getHash() Invalid data type: ${data}`);
60
+ }
61
+ if (algorithm === 'ipfs') {
62
+ return getHashByIpfs(data);
32
63
  }
33
- throw new Error(`#getHash() Invalid data type: ${data}`);
64
+ return getHashByAlgorithm(data, algorithm);
34
65
  });
35
66
  }
36
67
  exports.getHash = getHash;
@@ -1,7 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { Readable } from 'stream';
4
- export declare function isStream(data: any): boolean;
5
4
  export declare function stringToStream(str: string): Readable;
6
5
  /**
7
6
  * @see https://stackoverflow.com/a/78087909
@@ -9,14 +9,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.streamToString = exports.stringToStream = exports.isStream = void 0;
12
+ exports.streamToString = exports.stringToStream = void 0;
13
13
  const stream_1 = require("stream");
14
- const lodash_1 = require("lodash");
15
14
  const consumers_1 = require("node:stream/consumers");
16
- function isStream(data) {
17
- return data instanceof stream_1.Stream || data instanceof stream_1.Readable || (0, lodash_1.isFunction)(data === null || data === void 0 ? void 0 : data.pipe);
18
- }
19
- exports.isStream = isStream;
20
15
  function stringToStream(str) {
21
16
  const stream = new stream_1.Readable();
22
17
  stream.push(str);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/core",
3
- "version": "0.5.31",
3
+ "version": "0.5.32",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -53,5 +53,5 @@
53
53
  "ts-jest": "^28.0.8",
54
54
  "typescript": "^4.9.5"
55
55
  },
56
- "gitHead": "34b6e844b3128e1958cffe80c9bc6d71269e362b"
56
+ "gitHead": "20c2365e56af81f1e51855d9dc96e939a82d6d1c"
57
57
  }