@blocklet/sdk 1.16.33-beta-20241024-064549-2c1ad302 → 1.16.33-beta-20241028-005826-60afb7c4
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/component/index.d.ts +1 -1
- package/lib/component/index.js +3 -8
- package/lib/middlewares/blocklet.d.ts +7 -0
- package/lib/middlewares/blocklet.js +60 -0
- package/lib/middlewares/component.d.ts +1 -1
- package/lib/middlewares/component.js +34 -1
- package/lib/util/component-api.d.ts +2 -0
- package/lib/util/component-api.js +36 -0
- package/lib/util/service-api.js +19 -6
- package/package.json +8 -8
- package/lib/embed/adapters/express.d.ts +0 -3
- package/lib/embed/adapters/express.js +0 -13
- package/lib/embed/generate.d.ts +0 -6
- package/lib/embed/generate.js +0 -30
- package/lib/embed/get-embed-url.d.ts +0 -1
- package/lib/embed/get-embed-url.js +0 -55
- package/lib/embed/get-embed.d.ts +0 -1
- package/lib/embed/get-embed.js +0 -32
- package/lib/embed/index.d.ts +0 -10
- package/lib/embed/index.js +0 -13
- package/lib/embed/message.d.ts +0 -26
- package/lib/embed/message.js +0 -134
package/lib/component/index.d.ts
CHANGED
package/lib/component/index.js
CHANGED
|
@@ -28,25 +28,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.getPackResources = exports.getResources = exports.getReleaseExportDir = exports.getResourceExportDir = exports.waitForComponentRunning = exports.getComponentWebEndpoint = exports.getComponentMountPoint = exports.getRelativeUrl = exports.getUrl = exports.call = void 0;
|
|
30
30
|
const path_1 = require("path");
|
|
31
|
-
const axios_1 = __importDefault(require("axios"));
|
|
32
31
|
const get_1 = __importDefault(require("lodash/get"));
|
|
33
32
|
const ufo_1 = require("ufo");
|
|
34
33
|
const wait_port_1 = __importDefault(require("wait-port"));
|
|
35
34
|
const constant_1 = require("@blocklet/constant");
|
|
36
35
|
const config_1 = require("../config");
|
|
37
|
-
const verify_sign_1 = require("../util/verify-sign");
|
|
38
36
|
const Util = __importStar(require("./util"));
|
|
37
|
+
const component_api_1 = __importDefault(require("../util/component-api"));
|
|
39
38
|
const doCall = async ({ url, headers = {}, ...options }) => {
|
|
40
39
|
try {
|
|
41
|
-
const resp = await (0,
|
|
40
|
+
const resp = await (0, component_api_1.default)({
|
|
42
41
|
url,
|
|
43
42
|
timeout: 60 * 1000,
|
|
44
43
|
...options,
|
|
45
|
-
headers
|
|
46
|
-
...(headers || {}),
|
|
47
|
-
'x-component-sig': (0, verify_sign_1.sign)(options.data),
|
|
48
|
-
'x-component-did': process.env.BLOCKLET_COMPONENT_DID,
|
|
49
|
-
},
|
|
44
|
+
headers,
|
|
50
45
|
});
|
|
51
46
|
config_1.logger.info(`call ${url} api success`);
|
|
52
47
|
return resp;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
2
|
+
declare const verifyBlockletSig: (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
|
|
3
|
+
export { verifyBlockletSig };
|
|
4
|
+
declare const _default: {
|
|
5
|
+
verifyBlockletSig: (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
|
|
6
|
+
};
|
|
7
|
+
export default _default;
|
|
@@ -0,0 +1,60 @@
|
|
|
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.verifyBlockletSig = void 0;
|
|
7
|
+
const semver_1 = __importDefault(require("semver"));
|
|
8
|
+
const ufo_1 = require("ufo");
|
|
9
|
+
const constant_1 = require("@blocklet/constant");
|
|
10
|
+
const config_1 = require("../config");
|
|
11
|
+
const verify_sign_1 = require("../util/verify-sign");
|
|
12
|
+
const legacyFn = (req) => {
|
|
13
|
+
const data = req?.body ?? {};
|
|
14
|
+
const params = req?.query ?? {};
|
|
15
|
+
return { data, params };
|
|
16
|
+
};
|
|
17
|
+
const defaultFn = (req) => {
|
|
18
|
+
const now = Math.floor(Date.now() / 1000);
|
|
19
|
+
const iat = Number(req.get('x-blocklet-sig-iat'));
|
|
20
|
+
const exp = Number(req.get('x-blocklet-sig-exp'));
|
|
21
|
+
if (Number.isNaN(iat) || Number.isNaN(exp)) {
|
|
22
|
+
throw new Error('invalid sig');
|
|
23
|
+
}
|
|
24
|
+
if (exp < now) {
|
|
25
|
+
throw new Error('expired sig');
|
|
26
|
+
}
|
|
27
|
+
const data = {
|
|
28
|
+
iat,
|
|
29
|
+
exp,
|
|
30
|
+
body: req.body ?? {},
|
|
31
|
+
query: req.query ?? {},
|
|
32
|
+
method: req.method.toLowerCase(),
|
|
33
|
+
url: (0, ufo_1.parseURL)(req.originalUrl).pathname,
|
|
34
|
+
};
|
|
35
|
+
return data;
|
|
36
|
+
};
|
|
37
|
+
const verifyBlockletSig = (req, res, next) => {
|
|
38
|
+
try {
|
|
39
|
+
const sig = req.get('x-blocklet-sig');
|
|
40
|
+
const sigVersion = req.get('x-blocklet-sig-version');
|
|
41
|
+
if (!sig) {
|
|
42
|
+
return res.status(400).json({ error: 'Bad Request' });
|
|
43
|
+
}
|
|
44
|
+
const getData = semver_1.default.gt(semver_1.default.coerce(sigVersion), semver_1.default.coerce(constant_1.SIG_VERSION.V0)) ? defaultFn : legacyFn;
|
|
45
|
+
const data = getData(req);
|
|
46
|
+
const verified = (0, verify_sign_1.verify)(data, sig);
|
|
47
|
+
if (!verified) {
|
|
48
|
+
return res.status(401).json({ error: 'verify sig failed' });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
config_1.logger.error(error);
|
|
53
|
+
return res.status(401).json({ error: 'verify sig failed' });
|
|
54
|
+
}
|
|
55
|
+
return next();
|
|
56
|
+
};
|
|
57
|
+
exports.verifyBlockletSig = verifyBlockletSig;
|
|
58
|
+
exports.default = {
|
|
59
|
+
verifyBlockletSig,
|
|
60
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from 'express';
|
|
1
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
2
2
|
declare const verifySig: (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
|
|
3
3
|
export { verifySig };
|
|
4
4
|
declare const _default: {
|
|
@@ -1,15 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.verifySig = void 0;
|
|
7
|
+
const semver_1 = __importDefault(require("semver"));
|
|
8
|
+
const ufo_1 = require("ufo");
|
|
9
|
+
const constant_1 = require("@blocklet/constant");
|
|
4
10
|
const config_1 = require("../config");
|
|
5
11
|
const verify_sign_1 = require("../util/verify-sign");
|
|
12
|
+
const legacyFn = (req) => {
|
|
13
|
+
const data = req?.body ?? {};
|
|
14
|
+
const params = req?.query ?? {};
|
|
15
|
+
return { data, params };
|
|
16
|
+
};
|
|
17
|
+
const latestFn = (req) => {
|
|
18
|
+
const now = Math.floor(Date.now() / 1000);
|
|
19
|
+
const iat = Number(req.get('x-component-sig-iat'));
|
|
20
|
+
const exp = Number(req.get('x-component-sig-exp'));
|
|
21
|
+
if (Number.isNaN(iat) || Number.isNaN(exp)) {
|
|
22
|
+
throw new Error('invalid sig');
|
|
23
|
+
}
|
|
24
|
+
if (exp < now) {
|
|
25
|
+
throw new Error('expired sig');
|
|
26
|
+
}
|
|
27
|
+
const data = {
|
|
28
|
+
iat,
|
|
29
|
+
exp,
|
|
30
|
+
body: req.body ?? {},
|
|
31
|
+
query: req.query ?? {},
|
|
32
|
+
method: req.method.toLowerCase(),
|
|
33
|
+
url: (0, ufo_1.parseURL)(req.originalUrl).pathname,
|
|
34
|
+
};
|
|
35
|
+
return data;
|
|
36
|
+
};
|
|
6
37
|
const verifySig = (req, res, next) => {
|
|
7
38
|
try {
|
|
8
39
|
const sig = req.get('x-component-sig');
|
|
40
|
+
const sigVersion = req.get('x-component-sig-version');
|
|
9
41
|
if (!sig) {
|
|
10
42
|
return res.status(400).json({ error: 'Bad Request' });
|
|
11
43
|
}
|
|
12
|
-
const
|
|
44
|
+
const getData = semver_1.default.gt(semver_1.default.coerce(sigVersion), semver_1.default.coerce(constant_1.SIG_VERSION.V0)) ? latestFn : legacyFn;
|
|
45
|
+
const data = getData(req);
|
|
13
46
|
const verified = (0, verify_sign_1.verify)(data, sig);
|
|
14
47
|
if (!verified) {
|
|
15
48
|
return res.status(401).json({ error: 'verify sig failed' });
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
const axios_1 = __importDefault(require("axios"));
|
|
7
|
+
const env_1 = require("@blocklet/env");
|
|
8
|
+
const constant_1 = require("@blocklet/constant");
|
|
9
|
+
const ufo_1 = require("ufo");
|
|
10
|
+
const verify_sign_1 = require("./verify-sign");
|
|
11
|
+
const componentApi = axios_1.default.create({
|
|
12
|
+
timeout: 60 * 1000,
|
|
13
|
+
headers: {
|
|
14
|
+
'User-Agent': `BlockletSDK/${env_1.serverVersion}`,
|
|
15
|
+
'x-blocklet-server-version': env_1.serverVersion,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
componentApi.interceptors.request.use((config) => {
|
|
19
|
+
const iat = Math.floor(Date.now() / 1000);
|
|
20
|
+
const exp = iat + 60 * 5;
|
|
21
|
+
const data = {
|
|
22
|
+
iat,
|
|
23
|
+
exp,
|
|
24
|
+
};
|
|
25
|
+
data.body = config.data ?? {};
|
|
26
|
+
data.query = config.params ?? {};
|
|
27
|
+
data.method = config.method.toLowerCase();
|
|
28
|
+
data.url = (0, ufo_1.parseURL)(config.url).pathname;
|
|
29
|
+
config.headers['x-component-did'] = process.env.BLOCKLET_COMPONENT_DID;
|
|
30
|
+
config.headers['x-component-sig'] = (0, verify_sign_1.sign)(data);
|
|
31
|
+
config.headers['x-component-sig-iat'] = iat;
|
|
32
|
+
config.headers['x-component-sig-exp'] = exp;
|
|
33
|
+
config.headers['x-component-sig-version'] = constant_1.SIG_VERSION.DEFAULT;
|
|
34
|
+
return config;
|
|
35
|
+
});
|
|
36
|
+
exports.default = componentApi;
|
package/lib/util/service-api.js
CHANGED
|
@@ -8,7 +8,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
8
8
|
};
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
const axios_1 = __importDefault(require("axios"));
|
|
11
|
-
const
|
|
11
|
+
const env_1 = require("@blocklet/env");
|
|
12
|
+
const ufo_1 = require("ufo");
|
|
13
|
+
const constant_1 = require("@blocklet/constant");
|
|
12
14
|
const constants_1 = require("./constants");
|
|
13
15
|
const verify_sign_1 = require("./verify-sign");
|
|
14
16
|
const axios = axios_1.default.create({
|
|
@@ -18,19 +20,30 @@ const axios = axios_1.default.create({
|
|
|
18
20
|
// 内部调用,超时时间不用过长
|
|
19
21
|
timeout: 6 * 1000,
|
|
20
22
|
headers: {
|
|
21
|
-
'User-Agent': `BlockletSDK/${
|
|
22
|
-
|
|
23
|
-
'x-blocklet-api-version': version_1.version,
|
|
23
|
+
'User-Agent': `BlockletSDK/${env_1.serverVersion}`,
|
|
24
|
+
'x-blocklet-server-version': env_1.serverVersion,
|
|
24
25
|
// NOTICE: 需要注入以下两个 header,才能使 blocklet-service 中的代码识别到当前的 blocklet 环境
|
|
25
26
|
'x-blocklet-did': process.env.BLOCKLET_DID,
|
|
26
27
|
'x-blocklet-component-id': process.env.BLOCKLET_REAL_DID,
|
|
27
28
|
},
|
|
28
29
|
});
|
|
29
30
|
axios.interceptors.request.use((config) => {
|
|
31
|
+
const iat = Math.floor(Date.now() / 1000);
|
|
32
|
+
const exp = iat + 60 * 5;
|
|
30
33
|
// 同时对 post 和 get 参数做签名,确保同时支持 post get 请求的校验
|
|
31
|
-
const
|
|
34
|
+
const data = {
|
|
35
|
+
iat,
|
|
36
|
+
exp,
|
|
37
|
+
};
|
|
38
|
+
data.body = config.data ?? {};
|
|
39
|
+
data.query = config.params ?? {};
|
|
40
|
+
data.method = config.method.toLowerCase();
|
|
41
|
+
data.url = (0, ufo_1.parseURL)(config.url).pathname;
|
|
32
42
|
// 签名使用的是当前 blocklet 的 appSk,固命名为 x-blocklet-sig,以后可做统一使用
|
|
33
|
-
config.headers['x-blocklet-sig'] = (0, verify_sign_1.sign)(
|
|
43
|
+
config.headers['x-blocklet-sig'] = (0, verify_sign_1.sign)(data);
|
|
44
|
+
config.headers['x-blocklet-sig-iat'] = iat;
|
|
45
|
+
config.headers['x-blocklet-sig-exp'] = exp;
|
|
46
|
+
config.headers['x-blocklet-sig-version'] = constant_1.SIG_VERSION.DEFAULT;
|
|
34
47
|
return config;
|
|
35
48
|
});
|
|
36
49
|
exports.default = axios;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.33-beta-
|
|
6
|
+
"version": "1.16.33-beta-20241028-005826-60afb7c4",
|
|
7
7
|
"description": "graphql client to read/write data on abt node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"typings": "lib/index.d.ts",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@abtnode/client": "1.16.33-beta-
|
|
31
|
-
"@abtnode/constant": "1.16.33-beta-
|
|
30
|
+
"@abtnode/client": "1.16.33-beta-20241028-005826-60afb7c4",
|
|
31
|
+
"@abtnode/constant": "1.16.33-beta-20241028-005826-60afb7c4",
|
|
32
32
|
"@arcblock/did": "1.18.136",
|
|
33
33
|
"@arcblock/did-auth": "1.18.136",
|
|
34
34
|
"@arcblock/jwt": "1.18.136",
|
|
35
35
|
"@arcblock/ws": "1.18.136",
|
|
36
|
-
"@blocklet/constant": "1.16.33-beta-
|
|
37
|
-
"@blocklet/env": "1.16.33-beta-
|
|
38
|
-
"@blocklet/meta": "1.16.33-beta-
|
|
36
|
+
"@blocklet/constant": "1.16.33-beta-20241028-005826-60afb7c4",
|
|
37
|
+
"@blocklet/env": "1.16.33-beta-20241028-005826-60afb7c4",
|
|
38
|
+
"@blocklet/meta": "1.16.33-beta-20241028-005826-60afb7c4",
|
|
39
39
|
"@did-connect/authenticator": "^2.2.4",
|
|
40
40
|
"@did-connect/handler": "^2.2.4",
|
|
41
41
|
"@nedb/core": "^2.1.5",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@nedb/core": "^2.1.2"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@arcblock/eslint-config-ts": "^0.3.
|
|
62
|
+
"@arcblock/eslint-config-ts": "^0.3.3",
|
|
63
63
|
"@types/express": "^5.0.0",
|
|
64
64
|
"@types/jest": "^29.5.13",
|
|
65
65
|
"@types/json-stable-stringify": "^1.0.36",
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"ts-node": "^10.9.1",
|
|
78
78
|
"typescript": "^5.6.3"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "12b595141a023fe3c3fdb2d7083b3f11624601e8"
|
|
81
81
|
}
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
const get_embed_1 = __importDefault(require("../get-embed"));
|
|
7
|
-
async function expressEmbed(req, res) {
|
|
8
|
-
const { query = {} } = req;
|
|
9
|
-
const { url = '' } = query;
|
|
10
|
-
const data = await (0, get_embed_1.default)(url);
|
|
11
|
-
res.json(data);
|
|
12
|
-
}
|
|
13
|
-
exports.default = expressEmbed;
|
package/lib/embed/generate.d.ts
DELETED
package/lib/embed/generate.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = generateBlockletEmbed;
|
|
4
|
-
function safeJsonParse(input, defaultValue) {
|
|
5
|
-
try {
|
|
6
|
-
return JSON.parse(input);
|
|
7
|
-
}
|
|
8
|
-
catch {
|
|
9
|
-
return defaultValue;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function generateBlockletEmbed() {
|
|
13
|
-
const { BLOCKLET_APP_NAME, BLOCKLET_APP_URL, BLOCKLET_REAL_NAME } = process.env;
|
|
14
|
-
const BLOCKLET_MOUNT_POINTS = safeJsonParse(process.env.BLOCKLET_MOUNT_POINTS, []);
|
|
15
|
-
let url = '/';
|
|
16
|
-
let name = BLOCKLET_APP_NAME;
|
|
17
|
-
const blockletNames = BLOCKLET_REAL_NAME.split('/');
|
|
18
|
-
const componentBlocklet = BLOCKLET_MOUNT_POINTS.find((blockletItem) => blockletItem.name === blockletNames[blockletNames.length - 1]);
|
|
19
|
-
if (componentBlocklet) {
|
|
20
|
-
// 先假设只有一层 component blocklet
|
|
21
|
-
url = componentBlocklet.mountPoint;
|
|
22
|
-
name = componentBlocklet.title;
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
name,
|
|
26
|
-
url,
|
|
27
|
-
origin: BLOCKLET_APP_URL,
|
|
28
|
-
embed: [],
|
|
29
|
-
};
|
|
30
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function getEmbedUrlFromUrl(url: string): Promise<string>;
|
|
@@ -1,55 +0,0 @@
|
|
|
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.default = getEmbedUrlFromUrl;
|
|
7
|
-
const cheerio_1 = require("cheerio");
|
|
8
|
-
const ufo_1 = require("ufo");
|
|
9
|
-
const axios_1 = __importDefault(require("axios"));
|
|
10
|
-
/**
|
|
11
|
-
* 判断 htmlContent 是否为开发环境
|
|
12
|
-
* @param {string} htmlContent
|
|
13
|
-
* @returns boolean
|
|
14
|
-
*/
|
|
15
|
-
function checkViteDev(htmlContent) {
|
|
16
|
-
const $ = (0, cheerio_1.load)(htmlContent);
|
|
17
|
-
const scriptList = $('script[type="module"]');
|
|
18
|
-
return !![...scriptList].find((scriptItem) => {
|
|
19
|
-
if (scriptItem.firstChild?.type === 'text') {
|
|
20
|
-
return scriptItem.firstChild?.data?.includes('window.__vite_plugin_react_preamble_installed__ = true');
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
async function getEmbedUrlFromUrl(url) {
|
|
26
|
-
try {
|
|
27
|
-
const { data: htmlContent } = await axios_1.default.get(url, {
|
|
28
|
-
headers: {
|
|
29
|
-
accept: 'text/html,application/xhtml+xml,application/xml',
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
let prefix = '/';
|
|
33
|
-
let metaUrl = url;
|
|
34
|
-
const $ = (0, cheerio_1.load)(htmlContent);
|
|
35
|
-
const link = $('link[rel="blocklet-open-embed"]');
|
|
36
|
-
const blockletScript = $('script[src="__blocklet__.js"]');
|
|
37
|
-
if (link && link.length > 0) {
|
|
38
|
-
metaUrl = link.attr('href') || url;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
return url;
|
|
42
|
-
}
|
|
43
|
-
const isViteDev = checkViteDev(htmlContent);
|
|
44
|
-
if (blockletScript && blockletScript.length > 0) {
|
|
45
|
-
const scriptUrl = new URL('__blocklet__.js?type=json', (0, ufo_1.withTrailingSlash)(url));
|
|
46
|
-
const { data: blockletMeta } = await axios_1.default.get(scriptUrl.href);
|
|
47
|
-
prefix = blockletMeta.prefix;
|
|
48
|
-
}
|
|
49
|
-
// 在 vite 开发模式下,embed-url 指向的地址会自动被增加一个前缀
|
|
50
|
-
return isViteDev ? (0, ufo_1.joinURL)(prefix, metaUrl.replace(prefix, '')) : (0, ufo_1.joinURL)(prefix, metaUrl);
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return url;
|
|
54
|
-
}
|
|
55
|
-
}
|
package/lib/embed/get-embed.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function getBlockletEmbedFromUrl(url: string): Promise<object | null>;
|
package/lib/embed/get-embed.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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.default = getBlockletEmbedFromUrl;
|
|
7
|
-
const path_1 = require("path");
|
|
8
|
-
const lodash_1 = require("lodash");
|
|
9
|
-
const axios_1 = __importDefault(require("axios"));
|
|
10
|
-
const get_embed_url_1 = __importDefault(require("./get-embed-url"));
|
|
11
|
-
async function getBlockletEmbedFromUrl(url) {
|
|
12
|
-
const metaUrl = await (0, get_embed_url_1.default)(url);
|
|
13
|
-
try {
|
|
14
|
-
if (metaUrl) {
|
|
15
|
-
let finalUrl = '';
|
|
16
|
-
if (metaUrl.startsWith('http://') || metaUrl.startsWith('https://')) {
|
|
17
|
-
finalUrl = metaUrl;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
finalUrl = `${new URL(url).origin}${(0, path_1.join)('/', metaUrl)}`;
|
|
21
|
-
}
|
|
22
|
-
const { data: metaData } = await axios_1.default.get(finalUrl);
|
|
23
|
-
if ((0, lodash_1.isObject)(metaData)) {
|
|
24
|
-
return metaData;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
/* empty */
|
|
30
|
-
}
|
|
31
|
-
return null;
|
|
32
|
-
}
|
package/lib/embed/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import generateBlockletEmbed from './generate';
|
|
2
|
-
import expressEmbed from './adapters/express';
|
|
3
|
-
import getBlockletEmbedFromUrl from './get-embed';
|
|
4
|
-
declare const _default: {
|
|
5
|
-
expressEmbed: typeof expressEmbed;
|
|
6
|
-
generateBlockletEmbed: typeof generateBlockletEmbed;
|
|
7
|
-
getBlockletEmbedFromUrl: typeof getBlockletEmbedFromUrl;
|
|
8
|
-
};
|
|
9
|
-
export default _default;
|
|
10
|
-
export { expressEmbed, generateBlockletEmbed, getBlockletEmbedFromUrl };
|
package/lib/embed/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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.getBlockletEmbedFromUrl = exports.generateBlockletEmbed = exports.expressEmbed = void 0;
|
|
7
|
-
const generate_1 = __importDefault(require("./generate"));
|
|
8
|
-
exports.generateBlockletEmbed = generate_1.default;
|
|
9
|
-
const express_1 = __importDefault(require("./adapters/express"));
|
|
10
|
-
exports.expressEmbed = express_1.default;
|
|
11
|
-
const get_embed_1 = __importDefault(require("./get-embed"));
|
|
12
|
-
exports.getBlockletEmbedFromUrl = get_embed_1.default;
|
|
13
|
-
exports.default = { expressEmbed: express_1.default, generateBlockletEmbed: generate_1.default, getBlockletEmbedFromUrl: get_embed_1.default };
|
package/lib/embed/message.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
type InstanceType = 'client' | 'server';
|
|
2
|
-
type MessageType = string;
|
|
3
|
-
interface Callback {
|
|
4
|
-
(...args: any[]): void;
|
|
5
|
-
}
|
|
6
|
-
declare class Message {
|
|
7
|
-
id: string;
|
|
8
|
-
type: InstanceType;
|
|
9
|
-
private bc;
|
|
10
|
-
private eventMap;
|
|
11
|
-
init: (cb: Callback) => void;
|
|
12
|
-
onInit: (getData: () => any) => void;
|
|
13
|
-
close: () => void;
|
|
14
|
-
constructor(id: string, type?: InstanceType);
|
|
15
|
-
on(type: MessageType, cb: Callback): void;
|
|
16
|
-
off(type: MessageType, cb: Callback): void;
|
|
17
|
-
once(type: MessageType, cb: Callback): void;
|
|
18
|
-
send(type: MessageType, payload: any): void;
|
|
19
|
-
}
|
|
20
|
-
declare function useMessage(id?: string, type?: InstanceType): any;
|
|
21
|
-
declare const _default: {
|
|
22
|
-
useMessage: typeof useMessage;
|
|
23
|
-
Message: typeof Message;
|
|
24
|
-
};
|
|
25
|
-
export default _default;
|
|
26
|
-
export { useMessage, Message };
|
package/lib/embed/message.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Message = void 0;
|
|
4
|
-
exports.useMessage = useMessage;
|
|
5
|
-
const INIT = '__init';
|
|
6
|
-
const ON_INIT = '__on-init';
|
|
7
|
-
function checkKey(typeKey) {
|
|
8
|
-
return !typeKey.startsWith(INIT);
|
|
9
|
-
}
|
|
10
|
-
function ensureType(type) {
|
|
11
|
-
if (!checkKey(type)) {
|
|
12
|
-
throw new Error(`Type can't starts with ${INIT}`);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function notExist() {
|
|
16
|
-
console.error('function not exist');
|
|
17
|
-
}
|
|
18
|
-
class Message {
|
|
19
|
-
constructor(id, type = 'client') {
|
|
20
|
-
this.id = id;
|
|
21
|
-
this.type = type;
|
|
22
|
-
const bc = new BroadcastChannel(this.id);
|
|
23
|
-
this.bc = bc;
|
|
24
|
-
this.eventMap = new Map();
|
|
25
|
-
const onMessage = (event) => {
|
|
26
|
-
const { data } = event;
|
|
27
|
-
const { type: messageType, payload, id: dataId } = data;
|
|
28
|
-
if (dataId === this.id) {
|
|
29
|
-
const cbList = this.eventMap.get(messageType) || [];
|
|
30
|
-
cbList.forEach((cbItem) => {
|
|
31
|
-
cbItem(payload);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
window.addEventListener('message', onMessage, false);
|
|
36
|
-
this.close = () => {
|
|
37
|
-
window.removeEventListener('message', onMessage, false);
|
|
38
|
-
// v1 message
|
|
39
|
-
this.bc.close();
|
|
40
|
-
};
|
|
41
|
-
// v1 message
|
|
42
|
-
bc.onmessage = ({ data }) => {
|
|
43
|
-
const { type: messageType, payload } = data;
|
|
44
|
-
const cbList = this.eventMap.get(messageType) || [];
|
|
45
|
-
cbList.forEach((cbItem) => {
|
|
46
|
-
cbItem(payload);
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
this.init = notExist;
|
|
50
|
-
this.onInit = notExist;
|
|
51
|
-
if (type === 'server') {
|
|
52
|
-
this.onInit = (getData = () => null) => {
|
|
53
|
-
this.eventMap.set(INIT, [
|
|
54
|
-
(listenKey) => {
|
|
55
|
-
const initData = getData();
|
|
56
|
-
this.send(listenKey, initData);
|
|
57
|
-
},
|
|
58
|
-
]);
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
if (type === 'client') {
|
|
62
|
-
this.init = (cb) => {
|
|
63
|
-
const uniqueId = String(+new Date());
|
|
64
|
-
const listenKey = `${ON_INIT}_${uniqueId}`;
|
|
65
|
-
this.once(listenKey, cb);
|
|
66
|
-
this.send(INIT, listenKey);
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
on(type, cb) {
|
|
71
|
-
ensureType(type);
|
|
72
|
-
const cbList = this.eventMap.get(type) || [];
|
|
73
|
-
cbList.push(cb);
|
|
74
|
-
this.eventMap.set(type, cbList);
|
|
75
|
-
}
|
|
76
|
-
off(type, cb) {
|
|
77
|
-
ensureType(type);
|
|
78
|
-
const cbList = this.eventMap.get(type) || [];
|
|
79
|
-
if (cb) {
|
|
80
|
-
const index = cbList.indexOf(cb);
|
|
81
|
-
if (index !== -1) {
|
|
82
|
-
cbList.splice(index, 1);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
cbList.length = 0;
|
|
87
|
-
}
|
|
88
|
-
this.eventMap.set(type, cbList);
|
|
89
|
-
}
|
|
90
|
-
once(type, cb) {
|
|
91
|
-
ensureType(type);
|
|
92
|
-
const callback = (...args) => {
|
|
93
|
-
cb(...args);
|
|
94
|
-
this.off(type, callback);
|
|
95
|
-
};
|
|
96
|
-
this.on(type, callback);
|
|
97
|
-
}
|
|
98
|
-
send(type, payload) {
|
|
99
|
-
const data = {
|
|
100
|
-
id: this.id,
|
|
101
|
-
type,
|
|
102
|
-
payload,
|
|
103
|
-
};
|
|
104
|
-
if (this.type === 'server') {
|
|
105
|
-
const iframeList = [...document.querySelectorAll(`iframe[data-id="${this.id}"]`)];
|
|
106
|
-
iframeList.forEach((iframeItem) => {
|
|
107
|
-
iframeItem.contentWindow.postMessage(data, '*');
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
window.parent.postMessage(data, '*');
|
|
112
|
-
}
|
|
113
|
-
// v1 message
|
|
114
|
-
this.bc.postMessage(data);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
exports.Message = Message;
|
|
118
|
-
const messageCache = new Map();
|
|
119
|
-
function useMessage(id, type = 'client') {
|
|
120
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
121
|
-
const _id = id || window.frameElement?.dataset.id || window.name;
|
|
122
|
-
if (messageCache.has(_id)) {
|
|
123
|
-
return messageCache.get(_id);
|
|
124
|
-
}
|
|
125
|
-
const isInIframe = window !== window.parent;
|
|
126
|
-
const message = (type === 'server' || isInIframe) && _id ? new Message(_id, type) : null;
|
|
127
|
-
const data = { message };
|
|
128
|
-
messageCache.set(_id, data);
|
|
129
|
-
return data;
|
|
130
|
-
}
|
|
131
|
-
exports.default = {
|
|
132
|
-
useMessage,
|
|
133
|
-
Message,
|
|
134
|
-
};
|