@did-space/core 0.5.31 → 0.5.33
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/schemas/global-space.js +2 -1
- package/dist/space/object-space.js +3 -4
- package/dist/utils/common.d.ts +1 -0
- package/dist/utils/common.js +6 -8
- package/dist/utils/hash.d.ts +12 -1
- package/dist/utils/hash.js +38 -7
- package/dist/utils/stream.d.ts +0 -1
- package/dist/utils/stream.js +1 -6
- package/package.json +2 -2
|
@@ -13,7 +13,8 @@ exports.GlobalSpaceOptionsSchema = validator_1.Joi.object({
|
|
|
13
13
|
});
|
|
14
14
|
exports.HashSchema = validator_1.Joi.string()
|
|
15
15
|
.custom((value, helper) => {
|
|
16
|
-
|
|
16
|
+
// @NOTE: 不是一个 ipfs cid,且不是一个 sha256 hash,才会报错
|
|
17
|
+
if (!(0, utils_1.isCidV1)(value) && value.length !== 64) {
|
|
17
18
|
return helper.error(`Invalid hash(${value})`);
|
|
18
19
|
}
|
|
19
20
|
return value;
|
|
@@ -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,
|
|
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,
|
|
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,
|
|
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
|
}
|
package/dist/utils/common.d.ts
CHANGED
package/dist/utils/common.js
CHANGED
|
@@ -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
|
-
|
|
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;
|
package/dist/utils/hash.d.ts
CHANGED
|
@@ -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
|
|
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
|
package/dist/utils/hash.js
CHANGED
|
@@ -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
|
|
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
|
|
31
|
+
function getHashByIpfs(data = '') {
|
|
28
32
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
64
|
+
return getHashByAlgorithm(data, algorithm);
|
|
34
65
|
});
|
|
35
66
|
}
|
|
36
67
|
exports.getHash = getHash;
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -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
|
package/dist/utils/stream.js
CHANGED
|
@@ -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 =
|
|
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.
|
|
3
|
+
"version": "0.5.33",
|
|
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": "
|
|
56
|
+
"gitHead": "f5350d2c846415b1a0dcbff82f9f9176ea49bf6f"
|
|
57
57
|
}
|