@blocklet/meta 1.15.17 → 1.16.0-beta-b16cb035
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/channel.d.ts +32 -0
- package/lib/channel.js +54 -0
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +5 -152
- package/lib/did.d.ts +3 -0
- package/lib/did.js +9 -9
- package/lib/engine.d.ts +7 -0
- package/lib/engine.js +21 -25
- package/lib/entry.d.ts +3 -0
- package/lib/entry.js +51 -64
- package/lib/extension.d.ts +14 -0
- package/lib/extension.js +82 -77
- package/lib/file.d.ts +23 -0
- package/lib/file.js +51 -36
- package/lib/fix.d.ts +36 -0
- package/lib/fix.js +231 -228
- package/lib/get-component-process-id.d.ts +5 -0
- package/lib/get-component-process-id.js +16 -0
- package/lib/has-reserved-key.d.ts +3 -0
- package/lib/has-reserved-key.js +15 -0
- package/lib/index.d.ts +86 -0
- package/lib/index.js +55 -34
- package/lib/info.d.ts +15 -0
- package/lib/info.js +70 -38
- package/lib/name.d.ts +15 -0
- package/lib/name.js +41 -8
- package/lib/nft-templates.d.ts +86 -0
- package/lib/nft-templates.js +52 -0
- package/lib/parse-navigation-from-blocklet.d.ts +92 -0
- package/lib/parse-navigation-from-blocklet.js +539 -0
- package/lib/parse-navigation.d.ts +3 -0
- package/lib/parse-navigation.js +197 -0
- package/lib/parse.d.ts +22 -0
- package/lib/parse.js +100 -89
- package/lib/payment/index.d.ts +254 -0
- package/lib/payment/index.js +14 -0
- package/lib/payment/v1.d.ts +185 -0
- package/lib/payment/v1.js +84 -0
- package/lib/payment/v2.d.ts +242 -0
- package/lib/payment/v2.js +576 -0
- package/lib/schema.d.ts +63 -0
- package/lib/schema.js +669 -283
- package/lib/service.d.ts +27 -0
- package/lib/service.js +71 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +18 -0
- package/lib/types/schema.d.ts +284 -0
- package/lib/types/schema.js +3 -0
- package/lib/url-friendly.d.ts +6 -0
- package/lib/url-friendly.js +20 -0
- package/lib/util-meta.d.ts +42 -0
- package/lib/util-meta.js +146 -0
- package/lib/util.d.ts +201 -0
- package/lib/util.js +501 -82
- package/lib/validate.d.ts +13 -0
- package/lib/validate.js +37 -61
- package/lib/verify-multi-sig.d.ts +3 -0
- package/lib/verify-multi-sig.js +86 -59
- package/lib/wallet.d.ts +9 -0
- package/lib/wallet.js +19 -30
- package/package.json +59 -20
- package/lib/payment.js +0 -114
package/lib/service.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare const setService: (meta: {
|
|
2
|
+
name: string;
|
|
3
|
+
schema: any;
|
|
4
|
+
default: any;
|
|
5
|
+
}) => void;
|
|
6
|
+
declare const getServiceConfig: (serviceName: string, customConfig: any, { validate }?: {
|
|
7
|
+
validate?: any;
|
|
8
|
+
}) => any;
|
|
9
|
+
declare const getDefaultServiceConfig: (serviceName: string) => any;
|
|
10
|
+
declare const findService: (services: any[], name: string) => any;
|
|
11
|
+
export { getServiceConfig };
|
|
12
|
+
export { getDefaultServiceConfig };
|
|
13
|
+
export { findService };
|
|
14
|
+
export { setService };
|
|
15
|
+
declare const _default: {
|
|
16
|
+
getServiceConfig: (serviceName: string, customConfig: any, { validate }?: {
|
|
17
|
+
validate?: any;
|
|
18
|
+
}) => any;
|
|
19
|
+
getDefaultServiceConfig: (serviceName: string) => any;
|
|
20
|
+
findService: (services: any[], name: string) => any;
|
|
21
|
+
setService: (meta: {
|
|
22
|
+
name: string;
|
|
23
|
+
schema: any;
|
|
24
|
+
default: any;
|
|
25
|
+
}) => void;
|
|
26
|
+
};
|
|
27
|
+
export default _default;
|
package/lib/service.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
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.setService = exports.findService = exports.getDefaultServiceConfig = exports.getServiceConfig = void 0;
|
|
7
|
+
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
8
|
+
const constant_1 = __importDefault(require("@abtnode/constant"));
|
|
9
|
+
const schema_1 = require("./schema");
|
|
10
|
+
const { NODE_SERVICES } = constant_1.default;
|
|
11
|
+
const SERVICES = {};
|
|
12
|
+
const setService = (meta) => {
|
|
13
|
+
SERVICES[meta.name] = {
|
|
14
|
+
validate: meta.schema.validate.bind(meta.schema),
|
|
15
|
+
defaultConfig: meta.default,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.setService = setService;
|
|
19
|
+
setService({
|
|
20
|
+
name: 'auth',
|
|
21
|
+
schema: schema_1.authConfigSchema,
|
|
22
|
+
default: {},
|
|
23
|
+
});
|
|
24
|
+
// backward compatible
|
|
25
|
+
SERVICES[NODE_SERVICES.AUTH_SERVICE] = {
|
|
26
|
+
...SERVICES[NODE_SERVICES.AUTH],
|
|
27
|
+
meta: {
|
|
28
|
+
...SERVICES[NODE_SERVICES.AUTH].meta,
|
|
29
|
+
name: NODE_SERVICES.AUTH_SERVICE,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const getService = (serviceName) => {
|
|
33
|
+
if (!serviceName) {
|
|
34
|
+
throw new Error('service name should not be empty');
|
|
35
|
+
}
|
|
36
|
+
const service = SERVICES[serviceName];
|
|
37
|
+
if (!service) {
|
|
38
|
+
throw new Error(`service ${serviceName} does not exist`);
|
|
39
|
+
}
|
|
40
|
+
return service;
|
|
41
|
+
};
|
|
42
|
+
const getServiceConfig = (serviceName, customConfig, { validate } = {}) => {
|
|
43
|
+
const service = getService(serviceName);
|
|
44
|
+
const data = (0, cloneDeep_1.default)(customConfig || {});
|
|
45
|
+
const { value, error } = service.validate(data);
|
|
46
|
+
if (validate && error) {
|
|
47
|
+
throw new Error(`Invalid blocklet service config: ${error.details.map((x) => x.message).join(', ')}`);
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
};
|
|
51
|
+
exports.getServiceConfig = getServiceConfig;
|
|
52
|
+
const getDefaultServiceConfig = (serviceName) => {
|
|
53
|
+
const { defaultConfig } = getService(serviceName);
|
|
54
|
+
return defaultConfig;
|
|
55
|
+
};
|
|
56
|
+
exports.getDefaultServiceConfig = getDefaultServiceConfig;
|
|
57
|
+
const findService = (services, name) => {
|
|
58
|
+
const names = [name];
|
|
59
|
+
// backward compatible
|
|
60
|
+
if (name === NODE_SERVICES.AUTH) {
|
|
61
|
+
names.push(NODE_SERVICES.AUTH_SERVICE);
|
|
62
|
+
}
|
|
63
|
+
return (services || []).find((x) => names.includes(x.name));
|
|
64
|
+
};
|
|
65
|
+
exports.findService = findService;
|
|
66
|
+
exports.default = {
|
|
67
|
+
getServiceConfig,
|
|
68
|
+
getDefaultServiceConfig,
|
|
69
|
+
findService,
|
|
70
|
+
setService,
|
|
71
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './schema';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/indent */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./schema"), exports);
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
export interface TAuthConfig {
|
|
2
|
+
allowSwitchProfile?: boolean;
|
|
3
|
+
blockUnauthenticated?: boolean;
|
|
4
|
+
blockUnauthorized?: boolean;
|
|
5
|
+
ignoreUrls?: string[];
|
|
6
|
+
profileFields?: ('fullName' | 'email' | 'avatar' | 'phone')[];
|
|
7
|
+
whoCanAccess?: 'owner' | 'invited' | 'all';
|
|
8
|
+
}
|
|
9
|
+
export interface TBlockletMeta {
|
|
10
|
+
author?: TPerson;
|
|
11
|
+
bundleDid?: string;
|
|
12
|
+
bundleName?: string;
|
|
13
|
+
capabilities?: {
|
|
14
|
+
clusterMode?: boolean;
|
|
15
|
+
component?: boolean;
|
|
16
|
+
didSpace?: 'required' | 'optional';
|
|
17
|
+
navigation?: boolean;
|
|
18
|
+
};
|
|
19
|
+
community?: string;
|
|
20
|
+
components?: {
|
|
21
|
+
description?: TDescription;
|
|
22
|
+
mountPoint?: TMountPoint;
|
|
23
|
+
name?: TBlockletName;
|
|
24
|
+
required?: boolean;
|
|
25
|
+
source: {
|
|
26
|
+
url: string | string[];
|
|
27
|
+
version?: 'latest' | string;
|
|
28
|
+
} | {
|
|
29
|
+
name: TBlockletName;
|
|
30
|
+
store?: string | string[];
|
|
31
|
+
version?: 'latest' | string;
|
|
32
|
+
};
|
|
33
|
+
title?: TTitle;
|
|
34
|
+
}[];
|
|
35
|
+
contributors?: TPerson[];
|
|
36
|
+
copyright?: {
|
|
37
|
+
owner?: string;
|
|
38
|
+
year?: string | number;
|
|
39
|
+
};
|
|
40
|
+
description: TDescription;
|
|
41
|
+
did: string;
|
|
42
|
+
dist?: TDist;
|
|
43
|
+
documentation?: string;
|
|
44
|
+
engine?: TEngine | TEngine[];
|
|
45
|
+
environments?: TEnvironment[];
|
|
46
|
+
files?: string[];
|
|
47
|
+
gitHash?: string;
|
|
48
|
+
group: 'dapp' | 'static' | 'gateway';
|
|
49
|
+
homepage?: string;
|
|
50
|
+
htmlAst?: any;
|
|
51
|
+
interfaces?: TInterface[];
|
|
52
|
+
keywords?: string | string[];
|
|
53
|
+
lastPublishedAt?: string;
|
|
54
|
+
license?: string;
|
|
55
|
+
logo?: string;
|
|
56
|
+
logoUrl?: string;
|
|
57
|
+
main: string;
|
|
58
|
+
maintainers?: TPerson[];
|
|
59
|
+
name?: string;
|
|
60
|
+
navigation?: TNavigation;
|
|
61
|
+
nftFactory?: string;
|
|
62
|
+
path?: string;
|
|
63
|
+
payment?: {
|
|
64
|
+
componentPrice?: {
|
|
65
|
+
parentPriceRange?: number[];
|
|
66
|
+
type: 'fixed' | 'percentage';
|
|
67
|
+
value: number;
|
|
68
|
+
}[];
|
|
69
|
+
price?: {
|
|
70
|
+
address: string;
|
|
71
|
+
value: number;
|
|
72
|
+
}[];
|
|
73
|
+
share?: {
|
|
74
|
+
address: string;
|
|
75
|
+
name: string;
|
|
76
|
+
value: number;
|
|
77
|
+
}[];
|
|
78
|
+
};
|
|
79
|
+
repository?: string | {
|
|
80
|
+
directory?: string;
|
|
81
|
+
type: 'git' | 'https' | 'svn';
|
|
82
|
+
url: string;
|
|
83
|
+
};
|
|
84
|
+
requirements?: {
|
|
85
|
+
abtnode?: string;
|
|
86
|
+
cpu?: '*' | 'arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 's390' | 's390x' | 'x32' | 'x64' | ('arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 's390' | 's390x' | 'x32' | 'x64')[];
|
|
87
|
+
fuels?: {
|
|
88
|
+
address?: string;
|
|
89
|
+
endpoint: string;
|
|
90
|
+
reason: string;
|
|
91
|
+
value: string;
|
|
92
|
+
}[];
|
|
93
|
+
nodejs?: string;
|
|
94
|
+
os?: '*' | 'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32' | ('aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32')[];
|
|
95
|
+
server?: string;
|
|
96
|
+
};
|
|
97
|
+
screenshots?: string[];
|
|
98
|
+
scripts?: TScripts;
|
|
99
|
+
signatures?: TSignature[];
|
|
100
|
+
specVersion?: string;
|
|
101
|
+
staticComponents?: {
|
|
102
|
+
description?: TDescription;
|
|
103
|
+
mountPoint: TMountPoint;
|
|
104
|
+
name: TBlockletName;
|
|
105
|
+
source: {
|
|
106
|
+
url: string | string[];
|
|
107
|
+
version?: 'latest' | string;
|
|
108
|
+
} | {
|
|
109
|
+
name: TBlockletName;
|
|
110
|
+
store?: string | string[];
|
|
111
|
+
version?: 'latest' | string;
|
|
112
|
+
};
|
|
113
|
+
title?: TTitle;
|
|
114
|
+
}[];
|
|
115
|
+
stats?: TStats;
|
|
116
|
+
storeId?: string;
|
|
117
|
+
support?: string | string;
|
|
118
|
+
tags?: string | string[];
|
|
119
|
+
theme?: TTheme;
|
|
120
|
+
timeout?: {
|
|
121
|
+
start?: number;
|
|
122
|
+
};
|
|
123
|
+
title?: TTitle | '';
|
|
124
|
+
version: string;
|
|
125
|
+
}
|
|
126
|
+
export type TBlockletName = string;
|
|
127
|
+
export interface TComponent {
|
|
128
|
+
description?: TDescription;
|
|
129
|
+
mountPoint?: TMountPoint;
|
|
130
|
+
name?: TBlockletName;
|
|
131
|
+
required?: boolean;
|
|
132
|
+
source: {
|
|
133
|
+
url: string | string[];
|
|
134
|
+
version?: 'latest' | string;
|
|
135
|
+
} | {
|
|
136
|
+
name: TBlockletName;
|
|
137
|
+
store?: string | string[];
|
|
138
|
+
version?: 'latest' | string;
|
|
139
|
+
};
|
|
140
|
+
title?: TTitle;
|
|
141
|
+
}
|
|
142
|
+
export type TDescription = string;
|
|
143
|
+
export interface TDist {
|
|
144
|
+
integrity: string;
|
|
145
|
+
size?: number;
|
|
146
|
+
tarball: string | string;
|
|
147
|
+
}
|
|
148
|
+
export interface TEndpoint {
|
|
149
|
+
meta?: {
|
|
150
|
+
params?: {
|
|
151
|
+
description: string;
|
|
152
|
+
name: string;
|
|
153
|
+
}[];
|
|
154
|
+
payable?: boolean;
|
|
155
|
+
vcType?: string;
|
|
156
|
+
};
|
|
157
|
+
path: string;
|
|
158
|
+
type: string;
|
|
159
|
+
}
|
|
160
|
+
export interface TEngine {
|
|
161
|
+
args?: string[];
|
|
162
|
+
interpreter?: 'binary' | 'node';
|
|
163
|
+
platform?: 'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32';
|
|
164
|
+
script: string;
|
|
165
|
+
}
|
|
166
|
+
export interface TEnvironment {
|
|
167
|
+
default?: string;
|
|
168
|
+
description: string;
|
|
169
|
+
name: TEnvironmentName;
|
|
170
|
+
required?: boolean;
|
|
171
|
+
secure?: boolean;
|
|
172
|
+
shared?: boolean;
|
|
173
|
+
validation?: string;
|
|
174
|
+
}
|
|
175
|
+
export type TEnvironmentName = string;
|
|
176
|
+
export interface TInterface {
|
|
177
|
+
cacheable?: TPathPrefix[];
|
|
178
|
+
endpoints?: TEndpoint[];
|
|
179
|
+
name: string;
|
|
180
|
+
path?: string;
|
|
181
|
+
port?: string | {
|
|
182
|
+
external: number;
|
|
183
|
+
internal: string;
|
|
184
|
+
};
|
|
185
|
+
prefix?: string;
|
|
186
|
+
protocol?: 'tcp' | 'udp' | 'http';
|
|
187
|
+
services?: TService[];
|
|
188
|
+
type: 'web' | 'service' | 'wellknown';
|
|
189
|
+
}
|
|
190
|
+
export type TLogo = string;
|
|
191
|
+
export type TMountPoint = string;
|
|
192
|
+
export type TNavigation = TNavigationItem[];
|
|
193
|
+
export interface TNavigationItem {
|
|
194
|
+
component?: string;
|
|
195
|
+
icon?: string;
|
|
196
|
+
id?: string;
|
|
197
|
+
items?: {
|
|
198
|
+
component?: string;
|
|
199
|
+
icon?: string;
|
|
200
|
+
id?: string;
|
|
201
|
+
link?: string | {
|
|
202
|
+
/**
|
|
203
|
+
* Unknown Property
|
|
204
|
+
*/
|
|
205
|
+
[x: string]: string;
|
|
206
|
+
};
|
|
207
|
+
role?: string[];
|
|
208
|
+
section?: string[];
|
|
209
|
+
title: string | {
|
|
210
|
+
/**
|
|
211
|
+
* Unknown Property
|
|
212
|
+
*/
|
|
213
|
+
[x: string]: string;
|
|
214
|
+
};
|
|
215
|
+
visible?: boolean;
|
|
216
|
+
}[];
|
|
217
|
+
link?: string | {
|
|
218
|
+
/**
|
|
219
|
+
* Unknown Property
|
|
220
|
+
*/
|
|
221
|
+
[x: string]: string;
|
|
222
|
+
};
|
|
223
|
+
role?: string[];
|
|
224
|
+
section?: string[];
|
|
225
|
+
title: string | {
|
|
226
|
+
/**
|
|
227
|
+
* Unknown Property
|
|
228
|
+
*/
|
|
229
|
+
[x: string]: string;
|
|
230
|
+
};
|
|
231
|
+
visible?: boolean;
|
|
232
|
+
}
|
|
233
|
+
export type TPathPrefix = string;
|
|
234
|
+
export interface TPerson {
|
|
235
|
+
email?: string;
|
|
236
|
+
name: string;
|
|
237
|
+
url?: string;
|
|
238
|
+
}
|
|
239
|
+
export interface TScripts {
|
|
240
|
+
dev?: string;
|
|
241
|
+
e2eDev?: string;
|
|
242
|
+
postInstall?: string;
|
|
243
|
+
postStart?: string;
|
|
244
|
+
preConfig?: string;
|
|
245
|
+
preInstall?: string;
|
|
246
|
+
preStart?: string;
|
|
247
|
+
preStop?: string;
|
|
248
|
+
preUninstall?: string;
|
|
249
|
+
}
|
|
250
|
+
export interface TService {
|
|
251
|
+
/**
|
|
252
|
+
* Any Property
|
|
253
|
+
*/
|
|
254
|
+
[x: string]: any;
|
|
255
|
+
config?: object;
|
|
256
|
+
name: string;
|
|
257
|
+
}
|
|
258
|
+
export interface TSignature {
|
|
259
|
+
appended?: string[];
|
|
260
|
+
created: string;
|
|
261
|
+
delegatee?: string;
|
|
262
|
+
delegateePk?: string;
|
|
263
|
+
delegation?: string;
|
|
264
|
+
excludes?: string[];
|
|
265
|
+
name: string;
|
|
266
|
+
pk: string;
|
|
267
|
+
sig: string;
|
|
268
|
+
signer: string;
|
|
269
|
+
type: string;
|
|
270
|
+
}
|
|
271
|
+
export interface TStats {
|
|
272
|
+
downloads?: number;
|
|
273
|
+
purchases?: number;
|
|
274
|
+
star?: number;
|
|
275
|
+
}
|
|
276
|
+
export interface TTheme {
|
|
277
|
+
background?: string | {
|
|
278
|
+
default?: string;
|
|
279
|
+
footer?: string;
|
|
280
|
+
header?: string;
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
export type TTitle = string;
|
|
284
|
+
export type TUpdateMountPoint = string;
|
|
@@ -0,0 +1,20 @@
|
|
|
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.isValidUrl = void 0;
|
|
7
|
+
const toLower_1 = __importDefault(require("lodash/toLower"));
|
|
8
|
+
const slugify_1 = __importDefault(require("slugify"));
|
|
9
|
+
const isValidUrl = (name) => {
|
|
10
|
+
const regex = /[\s$*_+~.()'"!:@\\]+/g;
|
|
11
|
+
return !regex.test(name);
|
|
12
|
+
};
|
|
13
|
+
exports.isValidUrl = isValidUrl;
|
|
14
|
+
const urlFriendly = (name, { keepSlash = true } = { keepSlash: true }) => {
|
|
15
|
+
if (!keepSlash) {
|
|
16
|
+
return (0, toLower_1.default)((0, slugify_1.default)(name.replace(/^[@./-]/, '').replace(/[@./_]/g, '-')));
|
|
17
|
+
}
|
|
18
|
+
return (0, toLower_1.default)((0, slugify_1.default)(name.replace(/^[@.-]/, '').replace(/[@._]/g, '-'), { remove: /[^\w\s$*_+~.()'"!:@^/]+/g }));
|
|
19
|
+
};
|
|
20
|
+
exports.default = urlFriendly;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { TBlockletMeta, TComponent } from './types';
|
|
2
|
+
declare const validateUrl: (url: string, expectedHttpResTypes?: string[]) => Promise<boolean>;
|
|
3
|
+
declare const getBlockletMetaByUrl: (url: string) => Promise<TBlockletMeta>;
|
|
4
|
+
declare const getBlockletMetaFromUrl: (url: string, { validateFn, returnUrl, ensureTarball, logger, }?: {
|
|
5
|
+
validateFn?: Function;
|
|
6
|
+
returnUrl?: boolean;
|
|
7
|
+
ensureTarball?: boolean;
|
|
8
|
+
logger?: any;
|
|
9
|
+
}) => Promise<any>;
|
|
10
|
+
declare const getBlockletMetaFromUrls: (urls: string[], { validateFn, returnUrl, ensureTarball, logger, }?: {
|
|
11
|
+
validateFn?: Function;
|
|
12
|
+
returnUrl?: boolean;
|
|
13
|
+
ensureTarball?: boolean;
|
|
14
|
+
logger?: any;
|
|
15
|
+
}) => Promise<any>;
|
|
16
|
+
/**
|
|
17
|
+
* @param {*} config defined in componentSchema in blocklet meta schema
|
|
18
|
+
*/
|
|
19
|
+
declare const getSourceUrlsFromConfig: (config: TComponent & any) => string[];
|
|
20
|
+
export { validateUrl };
|
|
21
|
+
export { getBlockletMetaByUrl };
|
|
22
|
+
export { getBlockletMetaFromUrl };
|
|
23
|
+
export { getBlockletMetaFromUrls };
|
|
24
|
+
export { getSourceUrlsFromConfig };
|
|
25
|
+
declare const _default: {
|
|
26
|
+
validateUrl: (url: string, expectedHttpResTypes?: string[]) => Promise<boolean>;
|
|
27
|
+
getBlockletMetaByUrl: (url: string) => Promise<TBlockletMeta>;
|
|
28
|
+
getBlockletMetaFromUrl: (url: string, { validateFn, returnUrl, ensureTarball, logger, }?: {
|
|
29
|
+
validateFn?: Function;
|
|
30
|
+
returnUrl?: boolean;
|
|
31
|
+
ensureTarball?: boolean;
|
|
32
|
+
logger?: any;
|
|
33
|
+
}) => Promise<any>;
|
|
34
|
+
getBlockletMetaFromUrls: (urls: string[], { validateFn, returnUrl, ensureTarball, logger, }?: {
|
|
35
|
+
validateFn?: Function;
|
|
36
|
+
returnUrl?: boolean;
|
|
37
|
+
ensureTarball?: boolean;
|
|
38
|
+
logger?: any;
|
|
39
|
+
}) => Promise<any>;
|
|
40
|
+
getSourceUrlsFromConfig: (config: any) => string[];
|
|
41
|
+
};
|
|
42
|
+
export default _default;
|
package/lib/util-meta.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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.getSourceUrlsFromConfig = exports.getBlockletMetaFromUrls = exports.getBlockletMetaFromUrl = exports.getBlockletMetaByUrl = exports.validateUrl = void 0;
|
|
7
|
+
const url_1 = require("url");
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const axios_1 = __importDefault(require("@abtnode/util/lib/axios"));
|
|
10
|
+
const promise_any_1 = __importDefault(require("promise.any"));
|
|
11
|
+
const url_join_1 = __importDefault(require("url-join"));
|
|
12
|
+
const constant_1 = __importDefault(require("@abtnode/constant"));
|
|
13
|
+
const did_1 = __importDefault(require("./did"));
|
|
14
|
+
const validate_1 = require("./validate");
|
|
15
|
+
const { BLOCKLET_STORE_API_BLOCKLET_PREFIX } = constant_1.default;
|
|
16
|
+
const validateUrl = async (url, expectedHttpResTypes = ['application/json', 'text/plain']) => {
|
|
17
|
+
const parsed = new URL(url);
|
|
18
|
+
const { protocol } = parsed;
|
|
19
|
+
// file
|
|
20
|
+
if (protocol.startsWith('file')) {
|
|
21
|
+
const decoded = decodeURIComponent((0, url_1.fileURLToPath)(url));
|
|
22
|
+
if (!fs_1.default.existsSync(decoded)) {
|
|
23
|
+
throw new Error(`File does not exist: ${decoded}`);
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
// http(s)
|
|
28
|
+
if (protocol.startsWith('http')) {
|
|
29
|
+
let res;
|
|
30
|
+
try {
|
|
31
|
+
res = await (0, axios_1.default)({ url, method: 'HEAD', timeout: 1000 * 10 });
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
throw new Error(`Cannot get content-type from ${url}: ${err.message}`);
|
|
35
|
+
}
|
|
36
|
+
if (res.headers['content-type'] &&
|
|
37
|
+
expectedHttpResTypes.some((x) => res.headers['content-type'].includes(x)) === false) {
|
|
38
|
+
throw new Error(`Unexpected content-type from ${url}: ${res.headers['content-type']}`);
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`Invalid url protocol: ${protocol.replace(/:$/, '')}`);
|
|
43
|
+
};
|
|
44
|
+
exports.validateUrl = validateUrl;
|
|
45
|
+
const validateBlockletMeta = (meta, opts = {}) => {
|
|
46
|
+
(0, validate_1.fixAndValidateService)(meta);
|
|
47
|
+
return (0, validate_1.validateMeta)(meta, opts);
|
|
48
|
+
};
|
|
49
|
+
const getBlockletMetaByUrl = async (url) => {
|
|
50
|
+
const { protocol } = new URL(url);
|
|
51
|
+
if (protocol.startsWith('file')) {
|
|
52
|
+
const decoded = decodeURIComponent((0, url_1.fileURLToPath)(url));
|
|
53
|
+
if (!fs_1.default.existsSync(decoded)) {
|
|
54
|
+
throw new Error(`File does not exist: ${decoded}`);
|
|
55
|
+
}
|
|
56
|
+
const d = await fs_1.default.promises.readFile(decoded);
|
|
57
|
+
// @ts-expect-error TS(2345) FIXME: Argument of type 'Buffer' is not assignable to par... Remove this comment to see the full error message
|
|
58
|
+
const meta = JSON.parse(d);
|
|
59
|
+
return meta;
|
|
60
|
+
}
|
|
61
|
+
if (protocol.startsWith('http')) {
|
|
62
|
+
const { data: meta } = await (0, axios_1.default)({ url, method: 'GET', timeout: 1000 * 20 });
|
|
63
|
+
if (Object.prototype.toString.call(meta) !== '[object Object]') {
|
|
64
|
+
throw new Error('Url is not valid');
|
|
65
|
+
}
|
|
66
|
+
return meta;
|
|
67
|
+
}
|
|
68
|
+
throw new Error(`Invalid url protocol: ${protocol.replace(/:$/, '')}`);
|
|
69
|
+
};
|
|
70
|
+
exports.getBlockletMetaByUrl = getBlockletMetaByUrl;
|
|
71
|
+
const getBlockletMetaFromUrl = async (url, { validateFn = validateBlockletMeta, returnUrl = false, ensureTarball = true, logger, } = {}) => {
|
|
72
|
+
const meta = await getBlockletMetaByUrl(url);
|
|
73
|
+
delete meta.htmlAst;
|
|
74
|
+
const newMeta = validateFn(meta, { ensureDist: true });
|
|
75
|
+
if (ensureTarball) {
|
|
76
|
+
try {
|
|
77
|
+
const { href } = new URL(newMeta.dist.tarball, url);
|
|
78
|
+
const tarball = decodeURIComponent(href);
|
|
79
|
+
try {
|
|
80
|
+
await validateUrl(tarball, ['application/octet-stream', 'application/x-gzip']);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (!error.message.startsWith('Cannot get content-type')) {
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
newMeta.dist.tarball = tarball;
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
const msg = `Invalid blocklet meta: dist.tarball is not a valid url ${err.message}`;
|
|
91
|
+
if (logger) {
|
|
92
|
+
logger.error(msg);
|
|
93
|
+
}
|
|
94
|
+
throw new Error(msg);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (returnUrl) {
|
|
98
|
+
return { meta: newMeta, url };
|
|
99
|
+
}
|
|
100
|
+
return newMeta;
|
|
101
|
+
};
|
|
102
|
+
exports.getBlockletMetaFromUrl = getBlockletMetaFromUrl;
|
|
103
|
+
const getBlockletMetaFromUrls = async (urls, { validateFn, returnUrl = false, ensureTarball = true, logger, } = {}) => {
|
|
104
|
+
try {
|
|
105
|
+
const res = await (0, promise_any_1.default)(urls.map((url) => getBlockletMetaFromUrl(url, { validateFn, returnUrl, ensureTarball, logger })));
|
|
106
|
+
return res;
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
let { message } = err;
|
|
110
|
+
if (Array.isArray(err.errors)) {
|
|
111
|
+
message = err.errors.map((x) => x.message).join(', ');
|
|
112
|
+
}
|
|
113
|
+
if (logger) {
|
|
114
|
+
logger.error('failed get blocklet meta', { urls, message });
|
|
115
|
+
}
|
|
116
|
+
throw new Error(message);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
exports.getBlockletMetaFromUrls = getBlockletMetaFromUrls;
|
|
120
|
+
/**
|
|
121
|
+
* @param {*} config defined in componentSchema in blocklet meta schema
|
|
122
|
+
*/
|
|
123
|
+
const getSourceUrlsFromConfig = (config) => {
|
|
124
|
+
if (config.source) {
|
|
125
|
+
if (config.source.url) {
|
|
126
|
+
return [config.source.url].flat();
|
|
127
|
+
}
|
|
128
|
+
const { store, version, name } = config.source;
|
|
129
|
+
// FIXME: @linchen the format or version is semverRange, blocklet store should support it
|
|
130
|
+
return [store]
|
|
131
|
+
.flat()
|
|
132
|
+
.map((x) => (0, url_join_1.default)(x, BLOCKLET_STORE_API_BLOCKLET_PREFIX, (0, did_1.default)(name), !version || version === 'latest' ? '' : version, 'blocklet.json'));
|
|
133
|
+
}
|
|
134
|
+
if (config.resolved) {
|
|
135
|
+
return [config.resolved];
|
|
136
|
+
}
|
|
137
|
+
throw new Error('Invalid child config');
|
|
138
|
+
};
|
|
139
|
+
exports.getSourceUrlsFromConfig = getSourceUrlsFromConfig;
|
|
140
|
+
exports.default = {
|
|
141
|
+
validateUrl,
|
|
142
|
+
getBlockletMetaByUrl,
|
|
143
|
+
getBlockletMetaFromUrl,
|
|
144
|
+
getBlockletMetaFromUrls,
|
|
145
|
+
getSourceUrlsFromConfig,
|
|
146
|
+
};
|