@did-space/core 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.
Files changed (41) hide show
  1. package/dist/drivers/base.d.ts +5 -5
  2. package/dist/drivers/base.js +8 -8
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +1 -0
  5. package/dist/meta/object.d.ts +5 -0
  6. package/dist/model/index.d.ts +3 -0
  7. package/dist/model/index.js +19 -0
  8. package/dist/model/object-collection.d.ts +28 -0
  9. package/dist/model/object-collection.js +7 -0
  10. package/dist/model/object.d.ts +59 -0
  11. package/dist/model/object.js +7 -0
  12. package/dist/model/tree.d.ts +94 -0
  13. package/dist/model/tree.js +18 -0
  14. package/dist/protocols/space-operator.d.ts +2 -0
  15. package/dist/protocols/space-owner-operator.d.ts +30 -4
  16. package/dist/schemas/global-space.d.ts +2 -0
  17. package/dist/schemas/global-space.js +21 -0
  18. package/dist/schemas/index.d.ts +1 -0
  19. package/dist/schemas/index.js +1 -0
  20. package/dist/schemas/object-space.d.ts +6 -0
  21. package/dist/schemas/object-space.js +39 -0
  22. package/dist/space/global-space.d.ts +43 -0
  23. package/dist/space/global-space.js +153 -0
  24. package/dist/space/index.d.ts +3 -45
  25. package/dist/space/index.js +16 -187
  26. package/dist/space/object-space.d.ts +111 -0
  27. package/dist/space/object-space.js +652 -0
  28. package/dist/space/space.d.ts +45 -0
  29. package/dist/space/space.js +190 -0
  30. package/dist/utils/common.d.ts +3 -0
  31. package/dist/utils/common.js +17 -0
  32. package/dist/utils/hash.d.ts +20 -0
  33. package/dist/utils/hash.js +60 -0
  34. package/dist/utils/index.d.ts +3 -2
  35. package/dist/utils/index.js +3 -2
  36. package/dist/utils/{string-to-stream.d.ts → stream.d.ts} +2 -0
  37. package/dist/utils/stream.js +23 -0
  38. package/package.json +11 -2
  39. package/dist/utils/stream-to-string.d.ts +0 -1
  40. package/dist/utils/stream-to-string.js +0 -10
  41. package/dist/utils/string-to-stream.js +0 -11
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Space = void 0;
16
+ const xbytes_1 = __importDefault(require("xbytes"));
17
+ class Space {
18
+ constructor(driver) {
19
+ this.driver = driver;
20
+ }
21
+ createSpace(spaceConfiguration) {
22
+ return this.driver.createSpace(spaceConfiguration);
23
+ }
24
+ destroySpace() {
25
+ return this.driver.destroySpace();
26
+ }
27
+ isSpaceCreated() {
28
+ return this.driver.isSpaceCreated();
29
+ }
30
+ getSpaceSize() {
31
+ return this.driver.getSpaceSize();
32
+ }
33
+ createAppSpace(options) {
34
+ return this.driver.createAppSpace(options);
35
+ }
36
+ destroyAppSpace(options) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const permissionOptions = {
39
+ fromAppDid: options.appDid,
40
+ toAppDid: options.appDid,
41
+ };
42
+ yield this.driver.setListable(permissionOptions, false);
43
+ yield this.driver.setReadable(permissionOptions, false);
44
+ yield this.driver.setWritable(permissionOptions, false);
45
+ return this.driver.destroyAppSpace(options);
46
+ });
47
+ }
48
+ getAppSpacePath(options) {
49
+ return this.driver.getAppSpacePath(options);
50
+ }
51
+ createConfig(spaceConfig) {
52
+ return this.driver.createConfig(spaceConfig);
53
+ }
54
+ destroyConfig() {
55
+ return this.driver.destroyConfig();
56
+ }
57
+ set(key, value) {
58
+ return this.driver.set(key, value);
59
+ }
60
+ get(key) {
61
+ return this.driver.get(key);
62
+ }
63
+ setListable(options, value) {
64
+ return this.driver.setListable(options, value);
65
+ }
66
+ setReadable(options, value) {
67
+ return this.driver.setReadable(options, value);
68
+ }
69
+ setWritable(options, value) {
70
+ return this.driver.setWritable(options, value);
71
+ }
72
+ isListable(options) {
73
+ return this.driver.isListable(options);
74
+ }
75
+ isReadable(options) {
76
+ return this.driver.isReadable(options);
77
+ }
78
+ isWritable(options) {
79
+ return this.driver.isWritable(options);
80
+ }
81
+ getSpaceStatus() {
82
+ var _a, _b;
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ // FIXME: @yejianchao 临时性解决方案,需要新增拓容功能,需要去除白名单功能
85
+ const [did, totalCapacity, usedCapacity] = yield Promise.all([
86
+ this.driver.get('did'),
87
+ this.driver.get('size'),
88
+ this.driver.getSpaceSize(),
89
+ ]);
90
+ const whiteList = ((_b = (_a = process.env) === null || _a === void 0 ? void 0 : _a.SPACE_WHITE_LIST) !== null && _b !== void 0 ? _b : '')
91
+ .split(',')
92
+ .map((s) => s.trim())
93
+ .filter(Boolean);
94
+ // @see: https://github.com/ArcBlock/did-spaces/issues/269#issuecomment-1430852234
95
+ // 存储空间无限制
96
+ if (totalCapacity === 0) {
97
+ return {
98
+ isFull: false,
99
+ totalCapacity,
100
+ usedCapacity,
101
+ };
102
+ }
103
+ // 跳过白名单
104
+ if (whiteList.length && whiteList.includes(did)) {
105
+ return {
106
+ isFull: false,
107
+ totalCapacity,
108
+ usedCapacity,
109
+ };
110
+ }
111
+ // 存储空间不足
112
+ if (usedCapacity >= totalCapacity) {
113
+ return {
114
+ isFull: true,
115
+ totalCapacity,
116
+ usedCapacity,
117
+ };
118
+ }
119
+ return {
120
+ isFull: false,
121
+ totalCapacity,
122
+ usedCapacity,
123
+ };
124
+ });
125
+ }
126
+ write(options) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ // FIXME: @yejianchao 插入数据和编辑数据应该分离?
129
+ if (yield this.exists(options)) {
130
+ return this.driver.write(options);
131
+ }
132
+ const spaceStatus = yield this.getSpaceStatus();
133
+ if (spaceStatus.isFull) {
134
+ throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity)}, used capacity is ${(0, xbytes_1.default)(spaceStatus.usedCapacity)}`);
135
+ }
136
+ return this.driver.write(options);
137
+ });
138
+ }
139
+ delete(options) {
140
+ return this.driver.delete(options);
141
+ }
142
+ read(options) {
143
+ return this.driver.read(options);
144
+ }
145
+ getHash(options) {
146
+ return this.driver.getHash(options);
147
+ }
148
+ exists(options) {
149
+ return this.driver.exists(options);
150
+ }
151
+ lists(options) {
152
+ return this.driver.lists(options);
153
+ }
154
+ list(options) {
155
+ return this.driver.list(options);
156
+ }
157
+ writeAsOwner(key, data) {
158
+ return __awaiter(this, void 0, void 0, function* () {
159
+ // FIXME: @yejianchao 插入数据和编辑数据应该分离
160
+ if (yield this.existsAsOwner(key)) {
161
+ return this.driver.writeAsOwner(key, data);
162
+ }
163
+ const spaceStatus = yield this.getSpaceStatus();
164
+ if (spaceStatus.isFull) {
165
+ throw new Error(`Insufficient free space, total capacity is ${(0, xbytes_1.default)(spaceStatus.totalCapacity)}, used capacity is ${(0, xbytes_1.default)(spaceStatus.usedCapacity)}`);
166
+ }
167
+ return this.driver.writeAsOwner(key, data);
168
+ });
169
+ }
170
+ deleteAsOwner(key) {
171
+ if (Space.READONLY_OBJECT_KEYS.includes(key)) {
172
+ throw new Error(`Object ${key} cannot be deleted`);
173
+ }
174
+ return this.driver.deleteAsOwner(key);
175
+ }
176
+ readAsOwner(key, options) {
177
+ return this.driver.readAsOwner(key, options);
178
+ }
179
+ existsAsOwner(key, options) {
180
+ return this.driver.existsAsOwner(key, options);
181
+ }
182
+ getStatusAsOwner(key) {
183
+ return this.driver.getStatusAsOwner(key);
184
+ }
185
+ static editable(key) {
186
+ return !Space.READONLY_OBJECT_KEYS.includes(key);
187
+ }
188
+ }
189
+ exports.Space = Space;
190
+ Space.READONLY_OBJECT_KEYS = ['/config.yml'];
@@ -0,0 +1,3 @@
1
+ import { Data } from '../meta';
2
+ export declare function getSize(data: Data): number;
3
+ export declare function isDirectory(key: string): boolean;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDirectory = exports.getSize = void 0;
4
+ function getSize(data) {
5
+ if (typeof data === 'string') {
6
+ return Buffer.byteLength(data, 'utf-8');
7
+ }
8
+ if (Buffer.isBuffer(data)) {
9
+ return Buffer.byteLength(data);
10
+ }
11
+ throw new Error(`#getSize() Invalid data type: ${data}`);
12
+ }
13
+ exports.getSize = getSize;
14
+ function isDirectory(key) {
15
+ return key.endsWith('/');
16
+ }
17
+ exports.isDirectory = isDirectory;
@@ -0,0 +1,20 @@
1
+ import { Data } from '../meta';
2
+ /**
3
+ *
4
+ * @see cid各个版本 https://docs.ipfs.tech/concepts/content-addressing/#cid-versions
5
+ * @see 浏览 markle 树 https://explore.ipld.io/#/
6
+ * @see cidV0转cidV1 https://cid.ipfs.tech/#QmaNNTsLtScRVUfPn45s2aVbe8jLwfx6yU4Jiu45h6L9FN
7
+ * @description
8
+ * @export
9
+ * @param {Data} data
10
+ * @return {Promise<string>} {Promise<string>}
11
+ */
12
+ export declare function getHash(data: Data): Promise<string>;
13
+ /**
14
+ * @description
15
+ * @export
16
+ * @param {string} hash cid hash
17
+ * @return {string} {string}
18
+ */
19
+ export declare function getHashPath(hash: string): string;
20
+ export declare function isCidV1(cid: string): boolean;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.isCidV1 = exports.getHashPath = exports.getHash = void 0;
16
+ const path_1 = require("path");
17
+ const ipfs_only_hash_1 = __importDefault(require("ipfs-only-hash"));
18
+ const multiformats_1 = require("multiformats");
19
+ const stream_1 = require("./stream");
20
+ /**
21
+ *
22
+ * @see cid各个版本 https://docs.ipfs.tech/concepts/content-addressing/#cid-versions
23
+ * @see 浏览 markle 树 https://explore.ipld.io/#/
24
+ * @see cidV0转cidV1 https://cid.ipfs.tech/#QmaNNTsLtScRVUfPn45s2aVbe8jLwfx6yU4Jiu45h6L9FN
25
+ * @description
26
+ * @export
27
+ * @param {Data} data
28
+ * @return {Promise<string>} {Promise<string>}
29
+ */
30
+ function getHash(data) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ if (typeof data === 'string' || Buffer.isBuffer(data) || (0, stream_1.isStream)(data)) {
33
+ const cidV0 = yield ipfs_only_hash_1.default.of(data);
34
+ return multiformats_1.CID.parse(cidV0).toV1().toString();
35
+ }
36
+ throw new Error(`#getHash() Invalid data type: ${data}`);
37
+ });
38
+ }
39
+ exports.getHash = getHash;
40
+ /**
41
+ * @description
42
+ * @export
43
+ * @param {string} hash cid hash
44
+ * @return {string} {string}
45
+ */
46
+ function getHashPath(hash) {
47
+ const [prefix1, prefix2] = [hash.slice(-2), hash.slice(-6, -2)];
48
+ return (0, path_1.join)('.objects', prefix1, prefix2, hash);
49
+ }
50
+ exports.getHashPath = getHashPath;
51
+ function isCidV1(cid) {
52
+ try {
53
+ const c = multiformats_1.CID.parse(cid);
54
+ return c.version === 1;
55
+ }
56
+ catch (_a) {
57
+ return false;
58
+ }
59
+ }
60
+ exports.isCidV1 = isCidV1;
@@ -1,3 +1,4 @@
1
1
  export * from './logger';
2
- export * from './stream-to-string';
3
- export * from './string-to-stream';
2
+ export * from './hash';
3
+ export * from './common';
4
+ export * from './stream';
@@ -15,5 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./logger"), exports);
18
- __exportStar(require("./stream-to-string"), exports);
19
- __exportStar(require("./string-to-stream"), exports);
18
+ __exportStar(require("./hash"), exports);
19
+ __exportStar(require("./common"), exports);
20
+ __exportStar(require("./stream"), exports);
@@ -1,3 +1,5 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable } from 'stream';
3
+ export declare function isStream(data: any): boolean;
3
4
  export declare function stringToStream(str: string): Readable;
5
+ export declare const streamToString: (stream: any) => Promise<string>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.streamToString = exports.stringToStream = exports.isStream = void 0;
4
+ const stream_1 = require("stream");
5
+ const lodash_1 = require("lodash");
6
+ function isStream(data) {
7
+ return data instanceof stream_1.Stream || data instanceof stream_1.Readable || (0, lodash_1.isFunction)(data === null || data === void 0 ? void 0 : data.pipe);
8
+ }
9
+ exports.isStream = isStream;
10
+ function stringToStream(str) {
11
+ const stream = new stream_1.Readable();
12
+ stream.push(str);
13
+ stream.push(null);
14
+ return stream;
15
+ }
16
+ exports.stringToStream = stringToStream;
17
+ const streamToString = (stream) => new Promise((resolve, reject) => {
18
+ const chunks = [];
19
+ stream.on('data', (chunk) => chunks.push(chunk));
20
+ stream.on('error', reject);
21
+ stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
22
+ });
23
+ exports.streamToString = streamToString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/core",
3
- "version": "0.2.172",
3
+ "version": "0.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -33,7 +33,16 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@arcblock/validator": "^1.18.89",
36
+ "debug": "^4.3.4",
37
+ "hasha": "^5.2.2",
38
+ "ipfs-only-hash": "^4.0.0",
36
39
  "joi": "^17.9.2",
40
+ "js-yaml": "^4.1.0",
41
+ "lodash": "^4.17.21",
42
+ "mime-types": "^2.1.35",
43
+ "multiformats": "9",
44
+ "p-all": "3.0.0",
45
+ "skypesky-sequelize": "^6.32.2",
37
46
  "xbytes": "^1.8.0"
38
47
  },
39
48
  "devDependencies": {
@@ -47,5 +56,5 @@
47
56
  "ts-jest": "^28.0.6",
48
57
  "typescript": "^4.9.5"
49
58
  },
50
- "gitHead": "0f221e8d8ec17645130499297d7a4a4ae0ef0efb"
59
+ "gitHead": "a883d2a4ab600138c8a23093ace6b94d9107e43e"
51
60
  }
@@ -1 +0,0 @@
1
- export declare const streamToString: (stream: any) => Promise<string>;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.streamToString = void 0;
4
- const streamToString = (stream) => new Promise((resolve, reject) => {
5
- const chunks = [];
6
- stream.on('data', (chunk) => chunks.push(chunk));
7
- stream.on('error', reject);
8
- stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
9
- });
10
- exports.streamToString = streamToString;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringToStream = void 0;
4
- const stream_1 = require("stream");
5
- function stringToStream(str) {
6
- const stream = new stream_1.Readable();
7
- stream.push(str);
8
- stream.push(null);
9
- return stream;
10
- }
11
- exports.stringToStream = stringToStream;