@did-space/core 0.5.78 → 0.5.80

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 (201) hide show
  1. package/dist/{constants → cjs/constants}/index.d.ts +0 -6
  2. package/dist/{constants → cjs/constants}/index.js +1 -9
  3. package/dist/{meta → cjs/meta}/template/app.d.ts +0 -8
  4. package/dist/cjs/meta/template/index.d.ts +2 -0
  5. package/dist/cjs/meta/template/index.js +18 -0
  6. package/dist/{model → cjs/model}/app-space-apply.d.ts +1 -1
  7. package/dist/cjs/model/audit-log.d.ts +527 -0
  8. package/dist/cjs/model/audit-log.js +72 -0
  9. package/dist/{model → cjs/model}/did-object-mapping.d.ts +1 -1
  10. package/dist/{model → cjs/model}/index.d.ts +1 -0
  11. package/dist/{model → cjs/model}/index.js +1 -0
  12. package/dist/{model → cjs/model}/object-collection.d.ts +1 -1
  13. package/dist/{model → cjs/model}/object.d.ts +1 -1
  14. package/dist/{model → cjs/model}/space.d.ts +1 -1
  15. package/dist/{model → cjs/model}/tree.d.ts +2 -2
  16. package/dist/cjs/model/tree.js +9 -0
  17. package/dist/cjs/protocols/driver.js +2 -0
  18. package/dist/cjs/protocols/space-config.js +2 -0
  19. package/dist/cjs/protocols/space-operator.js +2 -0
  20. package/dist/cjs/protocols/space-owner-operator.js +2 -0
  21. package/dist/cjs/protocols/space.js +2 -0
  22. package/dist/es/configuration/index.d.ts +1 -0
  23. package/dist/es/configuration/index.js +1 -0
  24. package/dist/es/configuration/space-config.d.ts +23 -0
  25. package/dist/es/configuration/space-config.js +5 -0
  26. package/dist/es/constants/index.d.ts +30 -0
  27. package/dist/es/constants/index.js +35 -0
  28. package/dist/es/drivers/base.d.ts +40 -0
  29. package/dist/es/drivers/base.js +113 -0
  30. package/dist/es/drivers/index.d.ts +1 -0
  31. package/dist/es/drivers/index.js +1 -0
  32. package/dist/es/index.d.ts +9 -0
  33. package/dist/es/index.js +9 -0
  34. package/dist/es/meta/data.d.ts +4 -0
  35. package/dist/es/meta/data.js +1 -0
  36. package/dist/es/meta/index.d.ts +3 -0
  37. package/dist/es/meta/index.js +3 -0
  38. package/dist/es/meta/object.d.ts +70 -0
  39. package/dist/es/meta/object.js +1 -0
  40. package/dist/es/meta/template/app.d.ts +27 -0
  41. package/dist/es/meta/template/app.js +1 -0
  42. package/dist/es/meta/template/index.d.ts +2 -0
  43. package/dist/es/meta/template/index.js +2 -0
  44. package/dist/es/meta/template/preview/base.d.ts +13 -0
  45. package/dist/es/meta/template/preview/base.js +12 -0
  46. package/dist/es/meta/template/preview/default.d.ts +75 -0
  47. package/dist/es/meta/template/preview/default.js +9 -0
  48. package/dist/es/meta/template/preview/index.d.ts +5 -0
  49. package/dist/es/meta/template/preview/index.js +5 -0
  50. package/dist/es/meta/template/preview/nft.d.ts +23 -0
  51. package/dist/es/meta/template/preview/nft.js +8 -0
  52. package/dist/es/meta/template/preview/passport.d.ts +35 -0
  53. package/dist/es/meta/template/preview/passport.js +19 -0
  54. package/dist/es/meta/template/preview/profile.d.ts +23 -0
  55. package/dist/es/meta/template/preview/profile.js +8 -0
  56. package/dist/es/model/app-space-apply.d.ts +75 -0
  57. package/dist/es/model/app-space-apply.js +1 -0
  58. package/dist/es/model/audit-log.d.ts +527 -0
  59. package/dist/es/model/audit-log.js +69 -0
  60. package/dist/es/model/did-object-mapping.d.ts +8 -0
  61. package/dist/es/model/did-object-mapping.js +1 -0
  62. package/dist/es/model/index.d.ts +5 -0
  63. package/dist/es/model/index.js +5 -0
  64. package/dist/es/model/object-collection.d.ts +42 -0
  65. package/dist/es/model/object-collection.js +1 -0
  66. package/dist/es/model/object.d.ts +59 -0
  67. package/dist/es/model/object.js +1 -0
  68. package/dist/es/model/space.d.ts +21 -0
  69. package/dist/es/model/space.js +1 -0
  70. package/dist/es/model/tree.d.ts +96 -0
  71. package/dist/es/model/tree.js +6 -0
  72. package/dist/es/protocols/base-space.d.ts +4 -0
  73. package/dist/es/protocols/base-space.js +1 -0
  74. package/dist/es/protocols/driver.d.ts +7 -0
  75. package/dist/es/protocols/driver.js +1 -0
  76. package/dist/es/protocols/index.d.ts +5 -0
  77. package/dist/es/protocols/index.js +5 -0
  78. package/dist/es/protocols/space-config.d.ts +17 -0
  79. package/dist/es/protocols/space-config.js +1 -0
  80. package/dist/es/protocols/space-operator.d.ts +63 -0
  81. package/dist/es/protocols/space-operator.js +1 -0
  82. package/dist/es/protocols/space-owner-operator.d.ts +43 -0
  83. package/dist/es/protocols/space-owner-operator.js +1 -0
  84. package/dist/es/protocols/space.d.ts +7 -0
  85. package/dist/es/protocols/space.js +1 -0
  86. package/dist/es/schemas/global-space.d.ts +2 -0
  87. package/dist/es/schemas/global-space.js +19 -0
  88. package/dist/es/schemas/index.d.ts +3 -0
  89. package/dist/es/schemas/index.js +3 -0
  90. package/dist/es/schemas/object-space.d.ts +6 -0
  91. package/dist/es/schemas/object-space.js +44 -0
  92. package/dist/es/schemas/space-configuration.d.ts +4 -0
  93. package/dist/es/schemas/space-configuration.js +22 -0
  94. package/dist/es/schemas/space-options.d.ts +1 -0
  95. package/dist/es/schemas/space-options.js +4 -0
  96. package/dist/es/space/global-space.d.ts +43 -0
  97. package/dist/es/space/global-space.js +169 -0
  98. package/dist/es/space/index.d.ts +3 -0
  99. package/dist/es/space/index.js +3 -0
  100. package/dist/es/space/object-space.d.ts +123 -0
  101. package/dist/es/space/object-space.js +684 -0
  102. package/dist/es/space/space.d.ts +47 -0
  103. package/dist/es/space/space.js +172 -0
  104. package/dist/es/utils/common.d.ts +5 -0
  105. package/dist/es/utils/common.js +38 -0
  106. package/dist/es/utils/hash.d.ts +31 -0
  107. package/dist/es/utils/hash.js +64 -0
  108. package/dist/es/utils/index.d.ts +4 -0
  109. package/dist/es/utils/index.js +4 -0
  110. package/dist/es/utils/logger.d.ts +2 -0
  111. package/dist/es/utils/logger.js +2 -0
  112. package/dist/es/utils/stream.d.ts +11 -0
  113. package/dist/es/utils/stream.js +18 -0
  114. package/package.json +98 -9
  115. package/dist/meta/template/app-backup.d.ts +0 -55
  116. package/dist/meta/template/app-backup.js +0 -4
  117. package/dist/meta/template/app-connected.d.ts +0 -49
  118. package/dist/meta/template/app-connected.js +0 -4
  119. package/dist/meta/template/app-disconnected.d.ts +0 -20
  120. package/dist/meta/template/app-disconnected.js +0 -4
  121. package/dist/meta/template/app-restore.d.ts +0 -40
  122. package/dist/meta/template/app-restore.js +0 -4
  123. package/dist/meta/template/index.d.ts +0 -6
  124. package/dist/meta/template/index.js +0 -22
  125. package/dist/model/app-space-apply.js +0 -7
  126. package/dist/model/did-object-mapping.js +0 -8
  127. package/dist/model/object-collection.js +0 -7
  128. package/dist/model/object.js +0 -7
  129. package/dist/model/space.js +0 -7
  130. package/dist/model/tree.js +0 -18
  131. /package/dist/{configuration → cjs/configuration}/index.d.ts +0 -0
  132. /package/dist/{configuration → cjs/configuration}/index.js +0 -0
  133. /package/dist/{configuration → cjs/configuration}/space-config.d.ts +0 -0
  134. /package/dist/{configuration → cjs/configuration}/space-config.js +0 -0
  135. /package/dist/{drivers → cjs/drivers}/base.d.ts +0 -0
  136. /package/dist/{drivers → cjs/drivers}/base.js +0 -0
  137. /package/dist/{drivers → cjs/drivers}/index.d.ts +0 -0
  138. /package/dist/{drivers → cjs/drivers}/index.js +0 -0
  139. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  140. /package/dist/{index.js → cjs/index.js} +0 -0
  141. /package/dist/{meta → cjs/meta}/data.d.ts +0 -0
  142. /package/dist/{meta → cjs/meta}/data.js +0 -0
  143. /package/dist/{meta → cjs/meta}/index.d.ts +0 -0
  144. /package/dist/{meta → cjs/meta}/index.js +0 -0
  145. /package/dist/{meta → cjs/meta}/object.d.ts +0 -0
  146. /package/dist/{meta → cjs/meta}/object.js +0 -0
  147. /package/dist/{meta → cjs/meta}/template/app.js +0 -0
  148. /package/dist/{meta → cjs/meta}/template/preview/base.d.ts +0 -0
  149. /package/dist/{meta → cjs/meta}/template/preview/base.js +0 -0
  150. /package/dist/{meta → cjs/meta}/template/preview/default.d.ts +0 -0
  151. /package/dist/{meta → cjs/meta}/template/preview/default.js +0 -0
  152. /package/dist/{meta → cjs/meta}/template/preview/index.d.ts +0 -0
  153. /package/dist/{meta → cjs/meta}/template/preview/index.js +0 -0
  154. /package/dist/{meta → cjs/meta}/template/preview/nft.d.ts +0 -0
  155. /package/dist/{meta → cjs/meta}/template/preview/nft.js +0 -0
  156. /package/dist/{meta → cjs/meta}/template/preview/passport.d.ts +0 -0
  157. /package/dist/{meta → cjs/meta}/template/preview/passport.js +0 -0
  158. /package/dist/{meta → cjs/meta}/template/preview/profile.d.ts +0 -0
  159. /package/dist/{meta → cjs/meta}/template/preview/profile.js +0 -0
  160. /package/dist/{protocols/base-space.js → cjs/model/app-space-apply.js} +0 -0
  161. /package/dist/{protocols/driver.js → cjs/model/did-object-mapping.js} +0 -0
  162. /package/dist/{protocols/space-config.js → cjs/model/object-collection.js} +0 -0
  163. /package/dist/{protocols/space-operator.js → cjs/model/object.js} +0 -0
  164. /package/dist/{protocols → cjs/model}/space.js +0 -0
  165. /package/dist/{protocols → cjs/protocols}/base-space.d.ts +0 -0
  166. /package/dist/{protocols/space-owner-operator.js → cjs/protocols/base-space.js} +0 -0
  167. /package/dist/{protocols → cjs/protocols}/driver.d.ts +0 -0
  168. /package/dist/{protocols → cjs/protocols}/index.d.ts +0 -0
  169. /package/dist/{protocols → cjs/protocols}/index.js +0 -0
  170. /package/dist/{protocols → cjs/protocols}/space-config.d.ts +0 -0
  171. /package/dist/{protocols → cjs/protocols}/space-operator.d.ts +0 -0
  172. /package/dist/{protocols → cjs/protocols}/space-owner-operator.d.ts +0 -0
  173. /package/dist/{protocols → cjs/protocols}/space.d.ts +0 -0
  174. /package/dist/{schemas → cjs/schemas}/global-space.d.ts +0 -0
  175. /package/dist/{schemas → cjs/schemas}/global-space.js +0 -0
  176. /package/dist/{schemas → cjs/schemas}/index.d.ts +0 -0
  177. /package/dist/{schemas → cjs/schemas}/index.js +0 -0
  178. /package/dist/{schemas → cjs/schemas}/object-space.d.ts +0 -0
  179. /package/dist/{schemas → cjs/schemas}/object-space.js +0 -0
  180. /package/dist/{schemas → cjs/schemas}/space-configuration.d.ts +0 -0
  181. /package/dist/{schemas → cjs/schemas}/space-configuration.js +0 -0
  182. /package/dist/{schemas → cjs/schemas}/space-options.d.ts +0 -0
  183. /package/dist/{schemas → cjs/schemas}/space-options.js +0 -0
  184. /package/dist/{space → cjs/space}/global-space.d.ts +0 -0
  185. /package/dist/{space → cjs/space}/global-space.js +0 -0
  186. /package/dist/{space → cjs/space}/index.d.ts +0 -0
  187. /package/dist/{space → cjs/space}/index.js +0 -0
  188. /package/dist/{space → cjs/space}/object-space.d.ts +0 -0
  189. /package/dist/{space → cjs/space}/object-space.js +0 -0
  190. /package/dist/{space → cjs/space}/space.d.ts +0 -0
  191. /package/dist/{space → cjs/space}/space.js +0 -0
  192. /package/dist/{utils → cjs/utils}/common.d.ts +0 -0
  193. /package/dist/{utils → cjs/utils}/common.js +0 -0
  194. /package/dist/{utils → cjs/utils}/hash.d.ts +0 -0
  195. /package/dist/{utils → cjs/utils}/hash.js +0 -0
  196. /package/dist/{utils → cjs/utils}/index.d.ts +0 -0
  197. /package/dist/{utils → cjs/utils}/index.js +0 -0
  198. /package/dist/{utils → cjs/utils}/logger.d.ts +0 -0
  199. /package/dist/{utils → cjs/utils}/logger.js +0 -0
  200. /package/dist/{utils → cjs/utils}/stream.d.ts +0 -0
  201. /package/dist/{utils → cjs/utils}/stream.js +0 -0
@@ -0,0 +1,47 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Stream } from 'stream';
4
+ import EventEmitter from 'events';
5
+ import { SpaceConfig } from '../configuration';
6
+ import { Data, Object } from '../meta';
7
+ import { WriteOptions, ReadOptions, SpaceProtocol, DriverProtocol, AppSpaceOptions, PermissionOptions, DeleteOptions, ListOptions, ListsOptions, GetHashOptions, KeyStatus, ReadAsOwnerOptions, ExistsAsOwnerOptions } from '../protocols';
8
+ export declare class Space extends EventEmitter implements SpaceProtocol {
9
+ readonly driver: DriverProtocol;
10
+ static readonly READONLY_OBJECT_KEYS: string[];
11
+ constructor(driver: DriverProtocol);
12
+ createSpace(spaceConfiguration: SpaceConfig): Promise<void>;
13
+ destroySpace(): Promise<void>;
14
+ isSpaceCreated(): Promise<boolean>;
15
+ getSpaceSize(): Promise<number>;
16
+ createAppSpace(options: AppSpaceOptions): Promise<void>;
17
+ destroyAppSpace(options: AppSpaceOptions): Promise<void>;
18
+ getAppSpacePath(options: AppSpaceOptions): Promise<string>;
19
+ createConfig(spaceConfig: SpaceConfig): Promise<void>;
20
+ destroyConfig(): Promise<void>;
21
+ set<T = any>(key: string, value: T): Promise<void>;
22
+ get<T = any>(key: string): Promise<T>;
23
+ setListable(options: PermissionOptions, value: boolean): Promise<void>;
24
+ setReadable(options: PermissionOptions, value: boolean): Promise<void>;
25
+ setWritable(options: PermissionOptions, value: boolean): Promise<void>;
26
+ isListable(options: PermissionOptions): Promise<boolean>;
27
+ isReadable(options: PermissionOptions): Promise<boolean>;
28
+ isWritable(options: PermissionOptions): Promise<boolean>;
29
+ getSpaceStatus(): Promise<{
30
+ totalCapacity: number;
31
+ usedCapacity: number;
32
+ isFull: boolean;
33
+ }>;
34
+ write(options: WriteOptions): Promise<void>;
35
+ delete(options: DeleteOptions): Promise<void>;
36
+ read(options: ReadOptions): Promise<Stream>;
37
+ getHash(options: GetHashOptions): Promise<string>;
38
+ exists(options: ReadOptions): Promise<boolean>;
39
+ lists(options: ListsOptions): Promise<Object[]>;
40
+ list(options: ListOptions): Promise<Object | null>;
41
+ writeAsOwner(key: string, data: Data): Promise<void>;
42
+ deleteAsOwner(key: string): Promise<void>;
43
+ readAsOwner(key: string, options?: ReadAsOwnerOptions): Promise<Stream>;
44
+ existsAsOwner(key: string, options?: ExistsAsOwnerOptions): Promise<boolean>;
45
+ getStatusAsOwner(key: string): Promise<KeyStatus>;
46
+ static editable(key: string): boolean;
47
+ }
@@ -0,0 +1,172 @@
1
+ import xbytes from 'xbytes';
2
+ import EventEmitter from 'events';
3
+ export class Space extends EventEmitter {
4
+ driver;
5
+ static READONLY_OBJECT_KEYS = ['/config.yml'];
6
+ constructor(driver) {
7
+ super();
8
+ this.driver = driver;
9
+ }
10
+ createSpace(spaceConfiguration) {
11
+ return this.driver.createSpace(spaceConfiguration);
12
+ }
13
+ destroySpace() {
14
+ return this.driver.destroySpace();
15
+ }
16
+ isSpaceCreated() {
17
+ return this.driver.isSpaceCreated();
18
+ }
19
+ getSpaceSize() {
20
+ return this.driver.getSpaceSize();
21
+ }
22
+ createAppSpace(options) {
23
+ return this.driver.createAppSpace(options);
24
+ }
25
+ async destroyAppSpace(options) {
26
+ const permissionOptions = {
27
+ fromAppDid: options.appDid,
28
+ toAppDid: options.appDid,
29
+ };
30
+ await this.driver.setListable(permissionOptions, false);
31
+ await this.driver.setReadable(permissionOptions, false);
32
+ await this.driver.setWritable(permissionOptions, false);
33
+ return this.driver.destroyAppSpace(options);
34
+ }
35
+ getAppSpacePath(options) {
36
+ return this.driver.getAppSpacePath(options);
37
+ }
38
+ createConfig(spaceConfig) {
39
+ return this.driver.createConfig(spaceConfig);
40
+ }
41
+ destroyConfig() {
42
+ return this.driver.destroyConfig();
43
+ }
44
+ set(key, value) {
45
+ return this.driver.set(key, value);
46
+ }
47
+ get(key) {
48
+ return this.driver.get(key);
49
+ }
50
+ setListable(options, value) {
51
+ return this.driver.setListable(options, value);
52
+ }
53
+ setReadable(options, value) {
54
+ return this.driver.setReadable(options, value);
55
+ }
56
+ setWritable(options, value) {
57
+ return this.driver.setWritable(options, value);
58
+ }
59
+ isListable(options) {
60
+ return this.driver.isListable(options);
61
+ }
62
+ isReadable(options) {
63
+ return this.driver.isReadable(options);
64
+ }
65
+ isWritable(options) {
66
+ return this.driver.isWritable(options);
67
+ }
68
+ async getSpaceStatus() {
69
+ // FIXME: @yejianchao 临时性解决方案,需要新增拓容功能,需要去除白名单功能
70
+ const [did, totalCapacity, usedCapacity] = await Promise.all([
71
+ this.driver.get('did'),
72
+ this.driver.get('size'),
73
+ this.driver.getSpaceSize(),
74
+ ]);
75
+ const whiteList = (process.env?.SPACE_WHITE_LIST ?? '')
76
+ .split(',')
77
+ .map((s) => s.trim())
78
+ .filter(Boolean);
79
+ // @see: https://github.com/ArcBlock/did-spaces/issues/269#issuecomment-1430852234
80
+ // 存储空间无限制
81
+ if (totalCapacity === 0) {
82
+ return {
83
+ isFull: false,
84
+ totalCapacity,
85
+ usedCapacity,
86
+ };
87
+ }
88
+ // 跳过白名单
89
+ if (whiteList.length && whiteList.includes(did)) {
90
+ return {
91
+ isFull: false,
92
+ totalCapacity,
93
+ usedCapacity,
94
+ };
95
+ }
96
+ // 存储空间不足
97
+ if (usedCapacity >= totalCapacity) {
98
+ return {
99
+ isFull: true,
100
+ totalCapacity,
101
+ usedCapacity,
102
+ };
103
+ }
104
+ return {
105
+ isFull: false,
106
+ totalCapacity,
107
+ usedCapacity,
108
+ };
109
+ }
110
+ async write(options) {
111
+ // FIXME: @yejianchao 插入数据和编辑数据应该分离?
112
+ if (await this.exists(options)) {
113
+ return this.driver.write(options);
114
+ }
115
+ const spaceStatus = await this.getSpaceStatus();
116
+ if (spaceStatus.isFull) {
117
+ throw new Error(`Insufficient free space, total capacity is ${xbytes(spaceStatus.totalCapacity, {
118
+ iec: true,
119
+ })}, used capacity is ${xbytes(spaceStatus.usedCapacity, { iec: true })}`);
120
+ }
121
+ return this.driver.write(options);
122
+ }
123
+ delete(options) {
124
+ return this.driver.delete(options);
125
+ }
126
+ read(options) {
127
+ return this.driver.read(options);
128
+ }
129
+ getHash(options) {
130
+ return this.driver.getHash(options);
131
+ }
132
+ exists(options) {
133
+ return this.driver.exists(options);
134
+ }
135
+ lists(options) {
136
+ return this.driver.lists(options);
137
+ }
138
+ list(options) {
139
+ return this.driver.list(options);
140
+ }
141
+ async writeAsOwner(key, data) {
142
+ // FIXME: @yejianchao 插入数据和编辑数据应该分离
143
+ if (await this.existsAsOwner(key)) {
144
+ return this.driver.writeAsOwner(key, data);
145
+ }
146
+ const spaceStatus = await this.getSpaceStatus();
147
+ if (spaceStatus.isFull) {
148
+ throw new Error(`Insufficient free space, total capacity is ${xbytes(spaceStatus.totalCapacity, {
149
+ iec: true,
150
+ })}, used capacity is ${xbytes(spaceStatus.usedCapacity, { iec: true })}`);
151
+ }
152
+ return this.driver.writeAsOwner(key, data);
153
+ }
154
+ deleteAsOwner(key) {
155
+ if (Space.READONLY_OBJECT_KEYS.includes(key)) {
156
+ throw new Error(`Object ${key} cannot be deleted`);
157
+ }
158
+ return this.driver.deleteAsOwner(key);
159
+ }
160
+ readAsOwner(key, options) {
161
+ return this.driver.readAsOwner(key, options);
162
+ }
163
+ existsAsOwner(key, options) {
164
+ return this.driver.existsAsOwner(key, options);
165
+ }
166
+ getStatusAsOwner(key) {
167
+ return this.driver.getStatusAsOwner(key);
168
+ }
169
+ static editable(key) {
170
+ return !Space.READONLY_OBJECT_KEYS.includes(key);
171
+ }
172
+ }
@@ -0,0 +1,5 @@
1
+ import { Data } from '../meta';
2
+ export declare function isStream(data: any): boolean;
3
+ export declare function getSize(data?: Data): Promise<number>;
4
+ export declare function isDirectory(key: string): boolean;
5
+ export declare function isValidData(data: Data): boolean;
@@ -0,0 +1,38 @@
1
+ import { isBuffer, isFunction, isString } from 'lodash';
2
+ import { Readable, Stream } from 'stream';
3
+ export function isStream(data) {
4
+ return data instanceof Stream || data instanceof Readable || isFunction(data?.pipe);
5
+ }
6
+ // eslint-disable-next-line require-await
7
+ export async function getSize(data = '') {
8
+ if (typeof data === 'string') {
9
+ return Buffer.byteLength(data, 'utf-8');
10
+ }
11
+ if (Buffer.isBuffer(data)) {
12
+ return Buffer.byteLength(data);
13
+ }
14
+ if (isStream(data)) {
15
+ return new Promise((resolve, reject) => {
16
+ let size = 0;
17
+ // @ts-expect-error
18
+ data.on('data', (chunk) => {
19
+ size += chunk.length;
20
+ });
21
+ // @ts-expect-error
22
+ data.on('end', () => {
23
+ resolve(size);
24
+ });
25
+ // @ts-expect-error
26
+ data.on('error', (err) => {
27
+ reject(err);
28
+ });
29
+ });
30
+ }
31
+ throw new Error(`#getSize() Invalid data type: ${data}`);
32
+ }
33
+ export function isDirectory(key) {
34
+ return key.endsWith('/');
35
+ }
36
+ export function isValidData(data) {
37
+ return isString(data) || isBuffer(data) || isStream(data);
38
+ }
@@ -0,0 +1,31 @@
1
+ import type { LiteralUnion } from 'type-fest';
2
+ import { Data } from '../meta';
3
+ export type Algorithm = LiteralUnion<'ipfs' | 'sha256', string>;
4
+ /**
5
+ *
6
+ * @see cid各个版本 https://docs.ipfs.tech/concepts/content-addressing/#cid-versions
7
+ * @see 浏览 markle 树 https://explore.ipld.io/#/
8
+ * @see cidV0转cidV1 https://cid.ipfs.tech/#QmaNNTsLtScRVUfPn45s2aVbe8jLwfx6yU4Jiu45h6L9FN
9
+ * @description
10
+ * @export
11
+ * @param {Data} data
12
+ * @return {Promise<string>} {Promise<string>}
13
+ */
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>;
24
+ /**
25
+ * @description
26
+ * @export
27
+ * @param {string} hash cid hash
28
+ * @return {string} {string}
29
+ */
30
+ export declare function getHashPath(hash: string): string;
31
+ export declare function isCidV1(cid: string): boolean;
@@ -0,0 +1,64 @@
1
+ import { join } from 'path';
2
+ import { onlyHash } from '@arcblock/ipfs-only-hash';
3
+ import { CID } from 'multiformats';
4
+ import hasha from 'hasha';
5
+ import { isValidData } from './common';
6
+ /**
7
+ *
8
+ * @see cid各个版本 https://docs.ipfs.tech/concepts/content-addressing/#cid-versions
9
+ * @see 浏览 markle 树 https://explore.ipld.io/#/
10
+ * @see cidV0转cidV1 https://cid.ipfs.tech/#QmaNNTsLtScRVUfPn45s2aVbe8jLwfx6yU4Jiu45h6L9FN
11
+ * @description
12
+ * @export
13
+ * @param {Data} data
14
+ * @return {Promise<string>} {Promise<string>}
15
+ */
16
+ export async function getHashByIpfs(data = '') {
17
+ const cidV0 = await onlyHash(data);
18
+ return CID.parse(cidV0).toV1().toString();
19
+ }
20
+ export function getHashByAlgorithm(data = '', algorithm = 'sha256') {
21
+ if (typeof data === 'string' || Buffer.isBuffer(data)) {
22
+ return hasha.async(data, { algorithm });
23
+ }
24
+ return hasha.fromStream(data, { algorithm });
25
+ }
26
+ /**
27
+ *
28
+ *
29
+ * @export
30
+ * @param {Data} [data='']
31
+ * @param {AlgorithmName} [algorithm='ipfs']
32
+ */
33
+ // eslint-disable-next-line require-await
34
+ export async function getHash(data = '', algorithm = 'ipfs') {
35
+ if (!['ipfs', 'sha256'].includes(algorithm)) {
36
+ throw new Error(`#getHash() Invalid algorithm type: ${algorithm}`);
37
+ }
38
+ if (!isValidData(data)) {
39
+ throw new Error(`#getHash() Invalid data type: ${data}`);
40
+ }
41
+ if (algorithm === 'ipfs') {
42
+ return getHashByIpfs(data);
43
+ }
44
+ return getHashByAlgorithm(data, algorithm);
45
+ }
46
+ /**
47
+ * @description
48
+ * @export
49
+ * @param {string} hash cid hash
50
+ * @return {string} {string}
51
+ */
52
+ export function getHashPath(hash) {
53
+ const [prefix1, prefix2] = [hash.slice(-2), hash.slice(-6, -2)];
54
+ return join('.objects', prefix1, prefix2, hash);
55
+ }
56
+ export function isCidV1(cid) {
57
+ try {
58
+ const c = CID.parse(cid);
59
+ return c.version === 1;
60
+ }
61
+ catch {
62
+ return false;
63
+ }
64
+ }
@@ -0,0 +1,4 @@
1
+ export * from './logger';
2
+ export * from './hash';
3
+ export * from './common';
4
+ export * from './stream';
@@ -0,0 +1,4 @@
1
+ export * from './logger';
2
+ export * from './hash';
3
+ export * from './common';
4
+ export * from './stream';
@@ -0,0 +1,2 @@
1
+ declare const logger: Console;
2
+ export { logger };
@@ -0,0 +1,2 @@
1
+ const logger = console;
2
+ export { logger };
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Readable } from 'stream';
4
+ export declare function stringToStream(str: string): Readable;
5
+ /**
6
+ * @see https://stackoverflow.com/a/78087909
7
+ * @param stream
8
+ * @param encoding
9
+ * @returns
10
+ */
11
+ export declare const streamToString: (stream: any, encoding?: BufferEncoding) => Promise<string>;
@@ -0,0 +1,18 @@
1
+ import { Readable } from 'stream';
2
+ import { buffer } from 'node:stream/consumers';
3
+ export function stringToStream(str) {
4
+ const stream = new Readable();
5
+ stream.push(str);
6
+ stream.push(null);
7
+ return stream;
8
+ }
9
+ /**
10
+ * @see https://stackoverflow.com/a/78087909
11
+ * @param stream
12
+ * @param encoding
13
+ * @returns
14
+ */
15
+ export const streamToString = async (stream, encoding) => {
16
+ const data = await buffer(stream);
17
+ return data.toString(encoding);
18
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/core",
3
- "version": "0.5.78",
3
+ "version": "0.5.80",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -12,8 +12,96 @@
12
12
  },
13
13
  "homepage": "",
14
14
  "license": "ISC",
15
- "main": "dist/index.js",
16
- "typings": "dist/index.d.ts",
15
+ "main": "dist/cjs/index.js",
16
+ "module": "dist/es/index.js",
17
+ "types": "./dist/cjs/index.d.ts",
18
+ "typesVersions": {
19
+ "*": {
20
+ "configuration": [
21
+ "./dist/cjs/configuration/index.d.ts"
22
+ ],
23
+ "constants": [
24
+ "./dist/cjs/constants/index.d.ts"
25
+ ],
26
+ "drivers": [
27
+ "./dist/cjs/drivers/index.d.ts"
28
+ ],
29
+ "meta": [
30
+ "./dist/cjs/meta/index.d.ts"
31
+ ],
32
+ "meta/template": [
33
+ "./dist/cjs/meta/template/index.d.ts"
34
+ ],
35
+ "meta/template/preview": [
36
+ "./dist/cjs/meta/template/preview/index.d.ts"
37
+ ],
38
+ "model": [
39
+ "./dist/cjs/model/index.d.ts"
40
+ ],
41
+ "protocols": [
42
+ "./dist/cjs/protocols/index.d.ts"
43
+ ],
44
+ "schemas": [
45
+ "./dist/cjs/schemas/index.d.ts"
46
+ ],
47
+ "space": [
48
+ "./dist/cjs/space/index.d.ts"
49
+ ],
50
+ "utils": [
51
+ "./dist/cjs/utils/index.d.ts"
52
+ ]
53
+ }
54
+ },
55
+ "exports": {
56
+ ".": {
57
+ "import": "./dist/es/index.js",
58
+ "require": "./dist/cjs/index.js"
59
+ },
60
+ "./configuration": {
61
+ "import": "./dist/es/configuration/index.js",
62
+ "require": "./dist/cjs/configuration/index.js"
63
+ },
64
+ "./constants": {
65
+ "import": "./dist/es/constants/index.js",
66
+ "require": "./dist/cjs/constants/index.js"
67
+ },
68
+ "./drivers": {
69
+ "import": "./dist/es/drivers/index.js",
70
+ "require": "./dist/cjs/drivers/index.js"
71
+ },
72
+ "./meta": {
73
+ "import": "./dist/es/meta/index.js",
74
+ "require": "./dist/cjs/meta/index.js"
75
+ },
76
+ "./meta/template": {
77
+ "import": "./dist/es/meta/template/index.js",
78
+ "require": "./dist/cjs/meta/template/index.js"
79
+ },
80
+ "./meta/template/preview": {
81
+ "import": "./dist/es/meta/template/preview/index.js",
82
+ "require": "./dist/cjs/meta/template/preview/index.js"
83
+ },
84
+ "./model": {
85
+ "import": "./dist/es/model/index.js",
86
+ "require": "./dist/cjs/model/index.js"
87
+ },
88
+ "./protocols": {
89
+ "import": "./dist/es/protocols/index.js",
90
+ "require": "./dist/cjs/protocols/index.js"
91
+ },
92
+ "./schemas": {
93
+ "import": "./dist/es/schemas/index.js",
94
+ "require": "./dist/cjs/schemas/index.js"
95
+ },
96
+ "./space": {
97
+ "import": "./dist/es/space/index.js",
98
+ "require": "./dist/cjs/space/index.js"
99
+ },
100
+ "./utils": {
101
+ "import": "./dist/es/utils/index.js",
102
+ "require": "./dist/cjs/utils/index.js"
103
+ }
104
+ },
17
105
  "files": [
18
106
  "dist"
19
107
  ],
@@ -23,15 +111,15 @@
23
111
  "test": "vitest run",
24
112
  "coverage": "vitest run --coverage",
25
113
  "verify": "npm run lint && npm run test && npm run build",
26
- "build": "tsc",
114
+ "build": "node tools/auto-exports.js &&tsc --project tsconfig.json && tsc --project tsconfig.es.json",
27
115
  "watch": "npm run build -- -w"
28
116
  },
29
117
  "dependencies": {
30
- "@arcblock/did": "^1.18.152",
118
+ "@arcblock/did": "^1.18.161",
31
119
  "@arcblock/ipfs-only-hash": "^0.0.2",
32
- "@arcblock/validator": "^1.18.152",
120
+ "@arcblock/validator": "^1.18.161",
33
121
  "dayjs": "^1.11.13",
34
- "debug": "^4.3.7",
122
+ "debug": "^4.4.0",
35
123
  "hasha": "^5.2.2",
36
124
  "joi": "^17.13.3",
37
125
  "js-yaml": "^4.1.0",
@@ -41,7 +129,7 @@
41
129
  "p-all": "3.0.0",
42
130
  "sequelize": "^6.37.5",
43
131
  "xbytes": "^1.9.1",
44
- "zod": "^3.23.8"
132
+ "zod": "^3.24.1"
45
133
  },
46
134
  "devDependencies": {
47
135
  "@arcblock/eslint-config-ts": "^0.3.3",
@@ -49,6 +137,7 @@
49
137
  "@types/node": "18.19.31",
50
138
  "@typescript-eslint/eslint-plugin": "^7.18.0",
51
139
  "eslint": "^8.57.1",
140
+ "glob": "^10.4.5",
52
141
  "jest": "^29.7.0",
53
142
  "lint-staged": "^13.3.0",
54
143
  "ts-jest": "^28.0.8",
@@ -56,5 +145,5 @@
56
145
  "vite": "^5.4.11",
57
146
  "vitest": "^2.1.8"
58
147
  },
59
- "gitHead": "a78e40b323dff7d1dd378b6b2ef8fd805df4df4d"
148
+ "gitHead": "16c29f7cfe40d7d54a438fd4047b94519d51062e"
60
149
  }
@@ -1,55 +0,0 @@
1
- import { APP_BACKUP_STATUS } from '../../constants';
2
- export declare const APP_BACKUP_TYPE = "app-backup";
3
- export interface AppBackup {
4
- /**
5
- *
6
- * @description uid
7
- * @link https://www.npmjs.com/package/nanoid?activeTab=readme
8
- * @type {string}
9
- * @memberof AppBackup
10
- */
11
- id: string;
12
- type: 'app-backup';
13
- userDid: string;
14
- /**
15
- *
16
- * @description 授权的时候用户当时在哪个网址?比如在 server 的 blocklet 的备份页面进行授权的,在blocklet 的授权页面进行授权的
17
- * @type {string}
18
- * @memberof AppBackup
19
- */
20
- referrer: string;
21
- /**
22
- *
23
- * @description 指向 server 的 did 地址
24
- * @default ""
25
- * @type {string}
26
- * @memberof AppBackup
27
- */
28
- serverDid: string;
29
- /**
30
- *
31
- * @description 本质上是始终指向最新的 appDid,目前主要是 rotate 的 blocklet 在还原的时候会用到这个参数
32
- * @default ""
33
- * @type {string}
34
- * @memberof AppBackup
35
- */
36
- signerDid: string;
37
- appName: string;
38
- appDescription: string;
39
- appLogoUrl?: string;
40
- /**
41
- * @description
42
- * @type {string}
43
- * @memberof AppBackup
44
- */
45
- status: APP_BACKUP_STATUS;
46
- /**
47
- * @description 目前保存着错误消息
48
- * @default ""
49
- * @type {string}
50
- * @memberof AppBackup
51
- */
52
- message: string;
53
- createAt: string;
54
- updateAt?: string;
55
- }
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.APP_BACKUP_TYPE = void 0;
4
- exports.APP_BACKUP_TYPE = 'app-backup';
@@ -1,49 +0,0 @@
1
- export declare const APP_CONNECT_TYPE = "app-connected";
2
- /**
3
- *
4
- * @description
5
- * @export
6
- * @interface AppConnected
7
- */
8
- export interface AppConnected {
9
- /**
10
- *
11
- * @description uid
12
- * @link https://www.npmjs.com/package/nanoid?activeTab=readme
13
- * @type {string}
14
- * @memberof AppConnected
15
- */
16
- id: string;
17
- type: 'app-connected';
18
- userDid: string;
19
- spaceDid: string;
20
- appDid: string;
21
- appName: string;
22
- appDescription: string;
23
- scopes: string;
24
- /**
25
- *
26
- * @description 注意,有了 appUrl,只要使用它的 __blocklet__.js?type=json api就能获取 blocklet的 logo,描述,等信息
27
- * @type {string}
28
- * @memberof AppConnected
29
- */
30
- appUrl: string;
31
- appLogoUrl: string;
32
- /**
33
- *
34
- * @description 授权的时候用户当时在哪个网址?比如在 server 的 blocklet 的备份页面进行授权的,在blocklet 的授权页面进行授权的
35
- * @type {string}
36
- * @memberof AppConnected
37
- */
38
- referrer: string;
39
- /**
40
- *
41
- * @description 指向 server 的 did 地址
42
- * @default ""
43
- * @type {string}
44
- * @memberof AppBackup
45
- */
46
- serverDid: string;
47
- createAt: string;
48
- updateAt?: string;
49
- }
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.APP_CONNECT_TYPE = void 0;
4
- exports.APP_CONNECT_TYPE = 'app-connected';