@did-space/template 0.1.87-beta-1

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/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @did-space/template
2
+
3
+ # Install
4
+
5
+ ```
6
+ npm install @did-space/template
7
+ ```
@@ -0,0 +1,2 @@
1
+ export * from './schema';
2
+ export * from './template';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./schema"), exports);
18
+ __exportStar(require("./template"), exports);
@@ -0,0 +1 @@
1
+ export declare const api: import("axios").AxiosInstance;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.api = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ exports.api = axios_1.default.create();
@@ -0,0 +1 @@
1
+ export * from './api';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./api"), exports);
@@ -0,0 +1 @@
1
+ export declare const AppBackupSchema: import("joi").ObjectSchema<any>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppBackupSchema = void 0;
4
+ const validator_1 = require("@arcblock/validator");
5
+ const core_1 = require("@did-space/core");
6
+ const app_connected_1 = require("./app-connected");
7
+ const nanoid_1 = require("./nanoid");
8
+ exports.AppBackupSchema = validator_1.Joi.object({
9
+ id: nanoid_1.NanoidSchema,
10
+ type: validator_1.Joi.string().valid(core_1.APP_BACKUP_TYPE).default(core_1.APP_BACKUP_TYPE),
11
+ userDid: validator_1.Joi.DID().required(),
12
+ referrer: app_connected_1.AppUrlSchema,
13
+ createAt: validator_1.Joi.string()
14
+ .optional()
15
+ .default(() => new Date().toISOString()),
16
+ updateAt: validator_1.Joi.string().optional().allow('').default(''),
17
+ });
@@ -0,0 +1,5 @@
1
+ export declare const AppUrlSchema: import("joi").StringSchema<string>;
2
+ /**
3
+ * @link My Data 准备工作 之 交互设计 && 实现 https://team.arcblock.io/comment/discussions/594accd7-9379-4700-87a4-9fd5ef5b0bee
4
+ */
5
+ export declare const AppConnectedSchema: import("joi").ObjectSchema<any>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AppConnectedSchema = exports.AppUrlSchema = void 0;
7
+ const validator_1 = require("@arcblock/validator");
8
+ const is_url_1 = __importDefault(require("is-url"));
9
+ const core_1 = require("@did-space/core");
10
+ const nanoid_1 = require("./nanoid");
11
+ exports.AppUrlSchema = validator_1.Joi.string()
12
+ .required()
13
+ .custom((value, helper) => {
14
+ if (!(0, is_url_1.default)(value)) {
15
+ return helper.message(`appUrl(${value}) must be a http/https url`);
16
+ }
17
+ return value;
18
+ });
19
+ /**
20
+ * @link My Data 准备工作 之 交互设计 && 实现 https://team.arcblock.io/comment/discussions/594accd7-9379-4700-87a4-9fd5ef5b0bee
21
+ */
22
+ exports.AppConnectedSchema = validator_1.Joi.object({
23
+ id: nanoid_1.NanoidSchema,
24
+ type: validator_1.Joi.string().valid(core_1.APP_CONNECT_TYPE).default(core_1.APP_CONNECT_TYPE),
25
+ userDid: validator_1.Joi.DID().required(),
26
+ spaceDid: validator_1.Joi.DID().required(),
27
+ appDid: validator_1.Joi.DID().required(),
28
+ appName: validator_1.Joi.string().required(),
29
+ appDescription: validator_1.Joi.string().optional().allow('').default(''),
30
+ scopes: validator_1.Joi.string().required(),
31
+ appUrl: exports.AppUrlSchema,
32
+ referrer: exports.AppUrlSchema,
33
+ createAt: validator_1.Joi.string()
34
+ .optional()
35
+ .default(() => new Date().toISOString()),
36
+ updateAt: validator_1.Joi.string()
37
+ .optional()
38
+ .default(() => new Date().toISOString()),
39
+ });
@@ -0,0 +1 @@
1
+ export declare const AppRestoreSchema: import("joi").ObjectSchema<any>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppRestoreSchema = void 0;
4
+ const validator_1 = require("@arcblock/validator");
5
+ const core_1 = require("@did-space/core");
6
+ const app_connected_1 = require("./app-connected");
7
+ const nanoid_1 = require("./nanoid");
8
+ exports.AppRestoreSchema = validator_1.Joi.object({
9
+ id: nanoid_1.NanoidSchema,
10
+ type: validator_1.Joi.string().valid(core_1.APP_RESTORE_TYPE).default(core_1.APP_RESTORE_TYPE),
11
+ userDid: validator_1.Joi.DID().required(),
12
+ referrer: app_connected_1.AppUrlSchema,
13
+ createAt: validator_1.Joi.string()
14
+ .optional()
15
+ .default(() => new Date().toISOString()),
16
+ updateAt: validator_1.Joi.string().optional().allow('').default(''),
17
+ });
@@ -0,0 +1 @@
1
+ export declare const AppSchema: import("joi").ObjectSchema<any>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppSchema = void 0;
4
+ const validator_1 = require("@arcblock/validator");
5
+ const core_1 = require("@did-space/core");
6
+ const app_backup_1 = require("./app-backup");
7
+ const app_connected_1 = require("./app-connected");
8
+ const app_restore_1 = require("./app-restore");
9
+ const nanoid_1 = require("./nanoid");
10
+ exports.AppSchema = validator_1.Joi.object({
11
+ id: nanoid_1.NanoidSchema,
12
+ type: validator_1.Joi.string().valid(core_1.APP_TYPE).default(core_1.APP_TYPE),
13
+ userDid: validator_1.Joi.DID().required(),
14
+ connected: validator_1.Joi.array().items(app_connected_1.AppConnectedSchema).optional().default([]),
15
+ backup: validator_1.Joi.array().items(app_backup_1.AppBackupSchema).optional().default([]),
16
+ restore: validator_1.Joi.array().items(app_restore_1.AppRestoreSchema).optional().default([]),
17
+ createAt: validator_1.Joi.string()
18
+ .optional()
19
+ .default(() => new Date().toISOString()),
20
+ updateAt: validator_1.Joi.string()
21
+ .optional()
22
+ .default(() => new Date().toISOString()),
23
+ });
@@ -0,0 +1,2 @@
1
+ export * from './app-connected';
2
+ export * from './app';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./app-connected"), exports);
18
+ __exportStar(require("./app"), exports);
@@ -0,0 +1 @@
1
+ export declare const NanoidSchema: import("joi").StringSchema<string>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NanoidSchema = void 0;
4
+ const validator_1 = require("@arcblock/validator");
5
+ const nanoid_1 = require("nanoid");
6
+ exports.NanoidSchema = validator_1.Joi.string()
7
+ .optional()
8
+ .default(() => (0, nanoid_1.nanoid)());
@@ -0,0 +1,29 @@
1
+ import { Space, App, AppBackup, AppRestore } from '@did-space/core';
2
+ export interface AppTemplateOptions {
3
+ space: Space;
4
+ appDid: string;
5
+ }
6
+ /**
7
+ *
8
+ * FIXME: @yejianchao 实验性的功能实现,需要后续改进支持
9
+ * @export
10
+ * @class AppTemplate
11
+ */
12
+ export declare class AppTemplate {
13
+ private readonly options;
14
+ private cacheApp;
15
+ constructor(options: AppTemplateOptions);
16
+ private get key();
17
+ private verify;
18
+ create(options: {
19
+ userDid: string;
20
+ }): Promise<void>;
21
+ isCreated(): Promise<boolean>;
22
+ get(): Promise<App | null>;
23
+ set(app: App): Promise<App>;
24
+ isConnected(url: string): Promise<boolean>;
25
+ addAppBackup(appBackup: AppBackup): Promise<AppBackup>;
26
+ updateAppBackupById(id: string, appBackup: Omit<AppBackup, 'id'>): Promise<AppBackup>;
27
+ addAppRestore(appRestore: AppRestore): Promise<AppRestore>;
28
+ updateAppRestoreById(id: string, appRestore: Omit<AppRestore, 'id'>): Promise<AppRestore>;
29
+ }
@@ -0,0 +1,144 @@
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.AppTemplate = void 0;
16
+ const core_1 = require("@did-space/core");
17
+ const did_1 = require("@arcblock/did");
18
+ const js_yaml_1 = __importDefault(require("js-yaml"));
19
+ const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
20
+ const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
21
+ const last_1 = __importDefault(require("lodash/last"));
22
+ const findLastIndex_1 = __importDefault(require("lodash/findLastIndex"));
23
+ const path_1 = require("path");
24
+ const schema_1 = require("../schema");
25
+ const libs_1 = require("../libs");
26
+ /**
27
+ *
28
+ * FIXME: @yejianchao 实验性的功能实现,需要后续改进支持
29
+ * @export
30
+ * @class AppTemplate
31
+ */
32
+ class AppTemplate {
33
+ constructor(options) {
34
+ this.verify(options);
35
+ this.options = options;
36
+ }
37
+ get key() {
38
+ return (0, path_1.join)('/apps', this.options.appDid, '.meta/config.yml');
39
+ }
40
+ verify(options) {
41
+ if (!options) {
42
+ throw new Error('options is required');
43
+ }
44
+ if (!options.space instanceof core_1.Space) {
45
+ throw new Error('space must be a Space instance');
46
+ }
47
+ if (!(0, did_1.isValid)(options.appDid)) {
48
+ throw new Error(`appDid(${options.appDid}) must be a valid did`);
49
+ }
50
+ }
51
+ create(options) {
52
+ var _a;
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const app = (_a = (yield this.get())) !== null && _a !== void 0 ? _a : {};
55
+ if ((0, isEmpty_1.default)(app)) {
56
+ app.userDid = options.userDid;
57
+ yield this.set(app);
58
+ }
59
+ });
60
+ }
61
+ isCreated() {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ return this.options.space.existsAsOwner(this.key);
64
+ });
65
+ }
66
+ get() {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ if (!(yield this.isCreated())) {
69
+ return null;
70
+ }
71
+ if (this.cacheApp) {
72
+ return (0, cloneDeep_1.default)(this.cacheApp);
73
+ }
74
+ // 我觉得这个 readAsOwner 以及 其他 xxxAsOwner 函数已经不好用了,建议重新思考一下 @yejianchao
75
+ const data = yield this.options.space.readAsOwner(this.key);
76
+ const ymlStr = yield (0, core_1.streamToString)(data);
77
+ this.cacheApp = js_yaml_1.default.load(ymlStr);
78
+ return this.cacheApp;
79
+ });
80
+ }
81
+ set(app) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const { error, value } = schema_1.AppSchema.validate(app);
84
+ if (error) {
85
+ throw new Error(error.message);
86
+ }
87
+ yield this.options.space.writeAsOwner(this.key, js_yaml_1.default.dump(value));
88
+ this.cacheApp = value;
89
+ return this.cacheApp;
90
+ });
91
+ }
92
+ isConnected(url) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ try {
95
+ const res = yield libs_1.api.get(url);
96
+ return res.status === 200;
97
+ }
98
+ catch (error) {
99
+ return false;
100
+ }
101
+ });
102
+ }
103
+ addAppBackup(appBackup) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const app = yield this.get();
106
+ app.backup.push(Object.assign({}, appBackup));
107
+ const latestApp = yield this.set(app);
108
+ return (0, last_1.default)(latestApp.backup);
109
+ });
110
+ }
111
+ updateAppBackupById(id, appBackup) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const app = yield this.get();
114
+ const index = (0, findLastIndex_1.default)(app.backup, (b) => b.id === id);
115
+ if (index === -1) {
116
+ throw new Error(`AppBackup with id(${id}) not found`);
117
+ }
118
+ app.backup[index] = Object.assign(Object.assign({}, app.backup[index]), appBackup);
119
+ yield this.set(app);
120
+ return app.backup[index];
121
+ });
122
+ }
123
+ addAppRestore(appRestore) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ const app = yield this.get();
126
+ app.restore.push(Object.assign({}, appRestore));
127
+ const latestApp = yield this.set(app);
128
+ return (0, last_1.default)(latestApp.restore);
129
+ });
130
+ }
131
+ updateAppRestoreById(id, appRestore) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ const app = yield this.get();
134
+ const index = (0, findLastIndex_1.default)(app.restore, (b) => b.id === id);
135
+ if (index === -1) {
136
+ throw new Error(`AppRestore with id(${id}) not found`);
137
+ }
138
+ app.restore[index] = Object.assign(Object.assign({}, app.restore[index]), appRestore);
139
+ yield this.set(app);
140
+ return app.restore[index];
141
+ });
142
+ }
143
+ }
144
+ exports.AppTemplate = AppTemplate;
@@ -0,0 +1 @@
1
+ export * from './app';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./app"), exports);
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@did-space/template",
3
+ "version": "0.1.87-beta-1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Decentralized file space managed by DID",
8
+ "author": "linchen1987 <linchen.1987@foxmail.com>",
9
+ "homepage": "",
10
+ "license": "ISC",
11
+ "main": "dist/index.js",
12
+ "typings": "dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "scripts": {
17
+ "lint": "eslint src tests",
18
+ "lint:fix": "npm run lint -- --fix",
19
+ "test": "jest --forceExit --detectOpenHandles",
20
+ "coverage": "yarn test -- --coverage",
21
+ "prebuild": "rm -fr dist",
22
+ "build": "tsc",
23
+ "build:watch": "npm run build -- --watch",
24
+ "pre-commit": "lint-staged",
25
+ "verify": "npm run lint:fix && npm run test && npm run build"
26
+ },
27
+ "lint-staged": {
28
+ "**/*.{js,ts}": [
29
+ "npm run lint",
30
+ "npm run test",
31
+ "git add ."
32
+ ]
33
+ },
34
+ "dependencies": {
35
+ "@arcblock/did": "^1.18.57",
36
+ "@arcblock/jwt": "^1.18.57",
37
+ "@arcblock/validator": "^1.18.57",
38
+ "@blocklet/sdk": "1.8.67",
39
+ "@did-space/core": "^0.1.86",
40
+ "@ocap/mcrypto": "^1.18.57",
41
+ "@ocap/util": "^1.18.57",
42
+ "@ocap/wallet": "^1.18.57",
43
+ "agentkeepalive": "^4.2.1",
44
+ "axios": "^0.27.2",
45
+ "debug": "^4.3.4",
46
+ "filehound": "^1.17.6",
47
+ "form-data": "^4.0.0",
48
+ "fs-extra": "^11.1.0",
49
+ "got": "11.8.6",
50
+ "hasha": "^5.2.2",
51
+ "is-url": "^1.2.4",
52
+ "js-yaml": "^4.1.0",
53
+ "json-stable-stringify": "^1.0.1",
54
+ "lodash": "^4.17.21",
55
+ "nanoid": "^3.3.4",
56
+ "p-all": "3.0.0",
57
+ "p-queue": "6.6.2",
58
+ "readable-stream-clone": "^0.0.7",
59
+ "url-join": "^4.0.1",
60
+ "valid-url": "^1.0.9"
61
+ },
62
+ "devDependencies": {
63
+ "@arcblock/eslint-config-ts": "^0.2.3",
64
+ "@types/fs-extra": "^9.0.13",
65
+ "@types/is-url": "^1.2.30",
66
+ "@types/jest": "^28.1.5",
67
+ "@types/js-yaml": "^4.0.5",
68
+ "@types/json-stable-stringify": "^1.0.34",
69
+ "@types/lodash": "^4.14.191",
70
+ "@types/node": "15.12.2",
71
+ "@types/url-join": "^4.0.1",
72
+ "@typescript-eslint/eslint-plugin": "^5.30.6",
73
+ "eslint": "^8.19.0",
74
+ "jest": "^28.1.3",
75
+ "lint-staged": "^13.0.3",
76
+ "ts-jest": "^28.0.6",
77
+ "typescript": "^4.9.5"
78
+ }
79
+ }