@blocklet/resolver 1.16.12

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 (4) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +3 -0
  3. package/index.js +186 -0
  4. package/package.json +41 -0
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2018-2020 ArcBlock
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Blocklet Constants
2
+
3
+ Constants used in blocklet
package/index.js ADDED
@@ -0,0 +1,186 @@
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.parseBlocklet = exports.parseComponents = exports.getComponentConfig = exports.validateBlockletMeta = exports.ensureMeta = exports.filterDuplicateComponents = exports.isDidMatchName = void 0;
7
+ /* eslint-disable prettier/prettier */
8
+ /* eslint-disable no-await-in-loop */
9
+ const semver_1 = __importDefault(require("semver"));
10
+ const did_1 = require("@arcblock/did");
11
+ const constant_1 = require("@blocklet/constant");
12
+ const util_1 = require("@blocklet/meta/lib/util");
13
+ const schema_1 = require("@blocklet/meta/lib/schema");
14
+ const validate_1 = require("@blocklet/meta/lib/validate");
15
+ const util_meta_1 = require("@blocklet/meta/lib/util-meta");
16
+ const did_2 = __importDefault(require("@blocklet/meta/lib/did"));
17
+ const isDidMatchName = (did, name) => {
18
+ if ((0, did_1.isValid)(did) && name === did) {
19
+ return true;
20
+ }
21
+ return (0, did_2.default)(name) === did;
22
+ };
23
+ exports.isDidMatchName = isDidMatchName;
24
+ const filterDuplicateComponents = (components = [], currents = []) => {
25
+ const arr = [];
26
+ components.forEach((component) => {
27
+ if (currents.some((x) => x.meta.did === component.meta.did)) {
28
+ return;
29
+ }
30
+ const index = arr.findIndex((x) => x.meta.did === component.meta.did);
31
+ if (index > -1) {
32
+ const exist = arr[index];
33
+ // 选择最小版本:
34
+ // 如果 com1 和 com2 都依赖某个 component, com1 声明依赖版本 1.0.0, 实际获取版本 1.0.0; com2 声明依赖版本 latest(不限), 实际获取版本 2.0.0 -> 则应该取 1.0.0
35
+ if (semver_1.default.lt(component.meta.version, exist.meta.version)) {
36
+ arr.splice(index, 1, component);
37
+ }
38
+ }
39
+ else {
40
+ arr.push(component);
41
+ }
42
+ });
43
+ return arr;
44
+ };
45
+ exports.filterDuplicateComponents = filterDuplicateComponents;
46
+ /**
47
+ * set bundleDid and bundleMeta in meta
48
+ * update name and did to server index
49
+ * in app structure 2.0, application's bundleDid, bundleName, meta, did will be same
50
+ */
51
+ const ensureMeta = (meta, { name, did } = {}) => {
52
+ if (name && did && !(0, exports.isDidMatchName)(did, name)) {
53
+ throw new Error(`name does not match with did: ${name}, ${did}`);
54
+ }
55
+ const newMeta = {
56
+ ...meta,
57
+ };
58
+ if (!newMeta.did || !newMeta.name || !(0, exports.isDidMatchName)(newMeta.did, newMeta.name)) {
59
+ throw new Error(`name does not match with did in meta: ${newMeta.name}, ${newMeta.did}`);
60
+ }
61
+ if (!meta.bundleDid) {
62
+ newMeta.bundleDid = meta.did;
63
+ newMeta.bundleName = meta.name;
64
+ }
65
+ if (name) {
66
+ newMeta.name = name;
67
+ newMeta.did = did || (0, did_2.default)(name);
68
+ }
69
+ return newMeta;
70
+ };
71
+ exports.ensureMeta = ensureMeta;
72
+ const validateBlockletMeta = (meta, opts = {}) => {
73
+ (0, validate_1.fixAndValidateService)(meta);
74
+ return (0, validate_1.validateMeta)(meta, { ensureName: true, skipValidateDidName: true, schemaOptions: { ...opts } });
75
+ };
76
+ exports.validateBlockletMeta = validateBlockletMeta;
77
+ const getComponentConfig = (meta) => {
78
+ const components = meta.components || meta.children || [];
79
+ const staticComponents = (meta.staticComponents || []).map((x) => ({ ...x, static: true }));
80
+ return [...components, ...staticComponents];
81
+ };
82
+ exports.getComponentConfig = getComponentConfig;
83
+ const parseComponents = async (component, context = {}, logger = console) => {
84
+ const { ancestors = [], dynamicComponents = [] } = context;
85
+ if (ancestors.length > 40) {
86
+ throw new Error('The depth of component should not exceed 40');
87
+ }
88
+ // @ts-ignore
89
+ const configs = (0, exports.getComponentConfig)(component.meta) || [];
90
+ if (!configs || !configs.length) {
91
+ return {
92
+ staticComponents: [],
93
+ dynamicComponents,
94
+ };
95
+ }
96
+ const staticComponents = [];
97
+ for (const config of configs) {
98
+ const isStatic = !!config.static;
99
+ if (isStatic) {
100
+ if (!config.name) {
101
+ throw new Error(`Name does not found in child ${config.name}`);
102
+ }
103
+ if (!config.mountPoint) {
104
+ throw new Error(`MountPoint does not found in child ${config.name}`);
105
+ }
106
+ }
107
+ }
108
+ await Promise.all(configs.map(async (config) => {
109
+ const isStatic = !!config.static;
110
+ const urls = (0, util_meta_1.getSourceUrlsFromConfig)(config);
111
+ let rawMeta;
112
+ try {
113
+ rawMeta = await (0, util_meta_1.getBlockletMetaFromUrls)(urls, { logger });
114
+ }
115
+ catch (error) {
116
+ throw new Error(`Failed get component meta. Component: ${urls.join(', ')}, reason: ${error.message}`);
117
+ }
118
+ if (rawMeta.group === constant_1.BlockletGroup.gateway) {
119
+ throw new Error(`Cannot add gateway component ${rawMeta.title || rawMeta.did}`);
120
+ }
121
+ (0, exports.validateBlockletMeta)(rawMeta, { ensureDist: true });
122
+ if (!(0, util_1.isComponentBlocklet)(rawMeta)) {
123
+ throw new Error(`The blocklet cannot be a component: ${rawMeta.title}. The reason may be that the developer set capabilities.component to false in blocklet.yml`);
124
+ }
125
+ const webInterface = (0, util_1.findWebInterface)(rawMeta);
126
+ if (!webInterface) {
127
+ throw new Error(`Web interface does not found in component ${rawMeta.title || rawMeta.name}`);
128
+ }
129
+ const meta = (0, exports.ensureMeta)(rawMeta, { name: isStatic ? config.name : null });
130
+ // check circular dependencies
131
+ if (ancestors.map((x) => x.meta?.bundleDid).indexOf(meta.bundleDid) > -1) {
132
+ throw new Error('Blocklet components have circular dependencies');
133
+ }
134
+ if (config.title) {
135
+ meta.title = config.title;
136
+ meta.title = await schema_1.titleSchema.validateAsync(config.title);
137
+ }
138
+ if (config.description) {
139
+ meta.description = await schema_1.descriptionSchema.validateAsync(config.description);
140
+ }
141
+ const mountPoint = isStatic ? config.mountPoint : config.mountPoint || `/${meta.did}`;
142
+ const child = {
143
+ mountPoint,
144
+ meta,
145
+ bundleSource: config.source,
146
+ dependencies: [],
147
+ children: [],
148
+ };
149
+ if (isStatic) {
150
+ staticComponents.push(child);
151
+ }
152
+ else {
153
+ // @ts-ignore
154
+ dynamicComponents.push(child);
155
+ component.dependencies = component.dependencies || [];
156
+ component.dependencies.push({
157
+ did: child.meta.bundleDid,
158
+ required: !!config.required,
159
+ version: config.source.version || 'latest',
160
+ });
161
+ }
162
+ // @ts-ignore
163
+ await (0, exports.parseComponents)(child, { ancestors: [...ancestors, { meta }], dynamicComponents });
164
+ }));
165
+ component.children = staticComponents;
166
+ return {
167
+ staticComponents,
168
+ dynamicComponents,
169
+ };
170
+ };
171
+ exports.parseComponents = parseComponents;
172
+ const parseBlocklet = async (url) => {
173
+ const meta = await (0, util_meta_1.getBlockletMetaFromUrl)(url);
174
+ const newChildMeta = (0, exports.ensureMeta)(meta);
175
+ // children
176
+ const newChild = {
177
+ // @ts-ignore
178
+ meta: newChildMeta,
179
+ mountPoint: '/',
180
+ bundleSource: { url },
181
+ };
182
+ const { dynamicComponents } = await (0, exports.parseComponents)(newChild);
183
+ dynamicComponents.unshift(newChild);
184
+ return dynamicComponents;
185
+ };
186
+ exports.parseBlocklet = parseBlocklet;
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@blocklet/resolver",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "1.16.12",
7
+ "description": "Blocklet meta resolver",
8
+ "main": "index.js",
9
+ "files": [
10
+ "index.js"
11
+ ],
12
+ "scripts": {
13
+ "lint": "eslint tests index.ts",
14
+ "lint:fix": "npm run lint -- --fix",
15
+ "test": "NODE_ENV=test node tools/jest.js",
16
+ "coverage": "npm run test -- --coverage",
17
+ "clean": "rm -f index.d.ts index.js",
18
+ "build": "npm run clean && tsc"
19
+ },
20
+ "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
21
+ "license": "Apache-2.0",
22
+ "dependencies": {
23
+ "@arcblock/did": "^1.18.84",
24
+ "@blocklet/constant": "1.16.12",
25
+ "@blocklet/meta": "1.16.12",
26
+ "semver": "^7.3.8"
27
+ },
28
+ "devDependencies": {
29
+ "@arcblock/eslint-config-ts": "^0.2.4",
30
+ "@abtnode/types": "1.16.12",
31
+ "@types/jest": "^29.2.0",
32
+ "@types/node": "^18.11.0",
33
+ "@typescript-eslint/eslint-plugin": "^5.40.1",
34
+ "@typescript-eslint/parser": "^5.40.1",
35
+ "eslint": "^8.25.0",
36
+ "jest": "^27.5.1",
37
+ "prettier": "^2.7.1",
38
+ "ts-jest": "^27.1.5",
39
+ "typescript": "^5.0.4"
40
+ }
41
+ }