@blocklet/meta 1.6.17 → 1.6.18

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/lib/info.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const get = require('lodash/get');
2
2
  const getBlockletWallet = require('./wallet');
3
+ const { getDisplayName } = require('./util');
3
4
 
4
5
  module.exports = (state, nodeSk, { returnWallet = true } = {}) => {
5
6
  if (!state || typeof state !== 'object' || !Array.isArray(state.environments) || !get(state, 'meta.did')) {
@@ -9,12 +10,11 @@ module.exports = (state, nodeSk, { returnWallet = true } = {}) => {
9
10
  const { environments, configs = [] } = state;
10
11
  const envs = [...configs, ...environments];
11
12
 
12
- const customName = envs.find((x) => x.key === 'BLOCKLET_APP_NAME');
13
13
  const customDescription = envs.find((x) => x.key === 'BLOCKLET_APP_DESCRIPTION');
14
14
  const customPassportColor = envs.find((x) => x.key === 'BLOCKLET_PASSPORT_COLOR');
15
15
 
16
16
  const { did } = state.meta;
17
- const name = get(customName, 'value', state.meta.title || state.meta.name);
17
+ const name = getDisplayName(state);
18
18
  const description = get(customDescription, 'value', state.meta.description);
19
19
  const passportColor = get(customPassportColor, 'value', 'auto');
20
20
 
package/lib/schema.js CHANGED
@@ -1,8 +1,9 @@
1
1
  /* eslint-disable newline-per-chained-call */
2
2
  const fs = require('fs');
3
3
  const JOI = require('joi');
4
- const { semver, semverRange } = require('joi-extension-semver');
4
+ const cjkLength = require('cjk-length').default;
5
5
  const isGlob = require('is-glob');
6
+ const { semver, semverRange } = require('joi-extension-semver');
6
7
 
7
8
  const { fileExtension, didExtension } = require('./extension');
8
9
  const { validateName } = require('./name');
@@ -23,6 +24,7 @@ const {
23
24
  } = require('./constants');
24
25
 
25
26
  const WELLKNOWN_PATH_PREFIX = '/.well-known';
27
+ const MAX_TITLE_LENGTH = 48;
26
28
 
27
29
  const Joi = JOI.extend(semver).extend(semverRange).extend(fileExtension).extend(didExtension);
28
30
 
@@ -184,14 +186,25 @@ const createBlockletSchema = (
184
186
  return value;
185
187
  })
186
188
  .required(),
187
- description: Joi.string().trim().required(),
189
+ description: Joi.string().trim().min(3).max(512).required(),
188
190
  group: Joi.string().valid(...BLOCKLET_GROUPS).required(), // prettier-ignore
189
191
  main: Joi.when('group', {
190
192
  is: Joi.valid(BlockletGroup.gateway),
191
193
  then: Joi.forbidden(),
192
194
  otherwise: ensureMain ? Joi.file().exists({ baseDir }).required() : Joi.string().trim().required(),
193
195
  }),
194
- title: Joi.string().trim().optional().allow(''),
196
+ title: Joi.string()
197
+ .trim()
198
+ .min(3)
199
+ .custom((value) => {
200
+ if (cjkLength(value) > MAX_TITLE_LENGTH) {
201
+ throw new Error(`Blocklet title is too long. Max length is ${MAX_TITLE_LENGTH}`);
202
+ }
203
+
204
+ return value;
205
+ })
206
+ .optional()
207
+ .allow(''),
195
208
  logo: ensureFiles ? Joi.file().trim().exists({ baseDir }).optional() : Joi.string().trim().optional(),
196
209
  specVersion: Joi.semver().valid().gte('1.0.0').optional(),
197
210
 
@@ -216,7 +229,7 @@ const createBlockletSchema = (
216
229
  .max(4)
217
230
  .items(
218
231
  Joi.object({
219
- value: Joi.number().required(),
232
+ value: Joi.number().greater(0).required(),
220
233
  address: Joi.DID().required(), // token address
221
234
  })
222
235
  ),
@@ -232,7 +245,7 @@ const createBlockletSchema = (
232
245
  Joi.object({
233
246
  name: Joi.string().required(),
234
247
  address: Joi.DID().required(),
235
- value: Joi.number().min(0).max(1).required(),
248
+ value: Joi.number().greater(0).max(1).required(),
236
249
  })
237
250
  )
238
251
  .custom((value) => {
@@ -242,10 +255,10 @@ const createBlockletSchema = (
242
255
  }
243
256
  const total = value.reduce((acc, cur) => acc + cur.value, 0);
244
257
  if (total !== 1) {
245
- throw new Error(`invalid share: ${JSON.stringify(value)}`);
258
+ throw new Error(`Invalid blocklet payment share config: total share must be 1, got ${total}`);
246
259
  }
247
260
  return value;
248
- }, 'share invalid'),
261
+ }, 'invalid blocklet share'),
249
262
  })
250
263
  .default({ price: [], share: [] })
251
264
  .optional(),
package/lib/util.js CHANGED
@@ -153,6 +153,25 @@ const hasRunnableComponent = (blocklet) => {
153
153
  return has;
154
154
  };
155
155
 
156
+ /**
157
+ * 获取 blocklet 的 name
158
+ * @param {Object} blocklet 应用数据
159
+ * @param {Boolean} onlyUseMeta 优先使用应用元数据的name
160
+ * @returns blocklet display name
161
+ */
162
+ const getDisplayName = (blocklet, onlyUseMeta = false) => {
163
+ const { meta } = blocklet;
164
+ let displayName;
165
+
166
+ if (!onlyUseMeta && blocklet.environments) {
167
+ const target = blocklet.environments.find((e) => e.key === 'BLOCKLET_APP_NAME');
168
+ if (target && target.value) {
169
+ displayName = target.value;
170
+ }
171
+ }
172
+ return displayName || meta.title || meta.name;
173
+ };
174
+
156
175
  module.exports = {
157
176
  isFreeBlocklet,
158
177
  forEachBlockletSync,
@@ -162,4 +181,5 @@ module.exports = {
162
181
  getRequiredMissingConfigs,
163
182
  wipeSensitiveData,
164
183
  hasRunnableComponent,
184
+ getDisplayName,
165
185
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.6.17",
6
+ "version": "1.6.18",
7
7
  "description": "Library to parse/validate/fix blocklet meta",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -27,6 +27,7 @@
27
27
  "@ocap/util": "^1.14.8",
28
28
  "@ocap/wallet": "^1.14.8",
29
29
  "ajv": "^7.0.3",
30
+ "cjk-length": "^1.0.0",
30
31
  "debug": "^4.3.3",
31
32
  "fs-extra": "^10.0.0",
32
33
  "hosted-git-info": "3.0.8",
@@ -42,5 +43,5 @@
42
43
  "devDependencies": {
43
44
  "jest": "^27.4.5"
44
45
  },
45
- "gitHead": "d567a622a779eba43c95eaeb4633cb2b276bb7b9"
46
+ "gitHead": "2f02f166869d8ebedc0466068f6ed90ab3e07b87"
46
47
  }