@vector-metis/browser-sdk 0.1.3 → 0.2.0
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 +13 -6
- package/dist/index.d.ts +10 -4
- package/dist/index.js +74 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,18 +8,25 @@
|
|
|
8
8
|
npm install @vector-metis/browser-sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```ts
|
|
12
|
+
import { init } from "@vector-metis/browser-sdk";
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const Metis = await init();
|
|
15
|
+
const dependencies = await Metis.listDependencies();
|
|
15
16
|
```
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
import { Metis } from "@vector-metis/browser-sdk";
|
|
18
|
+
`init()` 会自动加载当前平台的 `/api/runtime/v1/browser-sdk.js`,并在脚本完成且能力校验通过后返回 SDK。重复调用会复用已经加载的 `window.Metis`。本地开发或严格 CSP 场景可以覆盖脚本地址、nonce 和超时时间:
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
```ts
|
|
21
|
+
const Metis = await init({
|
|
22
|
+
scriptUrl: "/api/runtime/v1/browser-sdk.js",
|
|
23
|
+
nonce: document.querySelector("meta[name=csp-nonce]")?.content,
|
|
24
|
+
timeoutMs: 10000,
|
|
25
|
+
});
|
|
21
26
|
```
|
|
22
27
|
|
|
28
|
+
请在浏览器应用启动阶段调用 `init()`;服务端渲染、Node.js 和构建阶段不提供浏览器环境。
|
|
29
|
+
|
|
23
30
|
浏览器 SDK 不暴露应用令牌、模型 API key、对象存储凭据或 Service 端口。完整说明见[开发文档](https://github.com/vector-metis/metis-sdk-typescript#readme)。
|
|
24
31
|
|
|
25
32
|
Apache-2.0,见仓库中的 `LICENSE`。
|
package/dist/index.d.ts
CHANGED
|
@@ -34,10 +34,16 @@ declare global {
|
|
|
34
34
|
Metis?: InjectedBrowserSDK;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
export type BrowserSDKInitOptions = {
|
|
38
|
+
/** Same-origin by default; override for local development or a platform proxy. */
|
|
39
|
+
scriptUrl?: string;
|
|
40
|
+
/** Optional CSP nonce for the dynamically-created script element. */
|
|
41
|
+
nonce?: string;
|
|
42
|
+
/** Milliseconds to wait for the platform script before rejecting. */
|
|
43
|
+
timeoutMs?: number;
|
|
44
|
+
};
|
|
45
|
+
/** Load the platform-injected browser capabilities and return a ready SDK. */
|
|
46
|
+
export declare function init(options?: BrowserSDKInitOptions): Promise<InjectedBrowserSDK>;
|
|
41
47
|
export type MetisBrowserContext = BrowserContext;
|
|
42
48
|
export type MetisBrowserSDK = InjectedBrowserSDK;
|
|
43
49
|
export type MetisWebDependency = WebDependency;
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
let initialization;
|
|
2
|
+
function validateSDK(value) {
|
|
3
|
+
if (!value || typeof value !== "object") {
|
|
4
|
+
throw new Error("Metis browser SDK loaded without a valid window.Metis object");
|
|
5
|
+
}
|
|
6
|
+
const sdk = value;
|
|
7
|
+
const methods = [
|
|
8
|
+
"appURL",
|
|
9
|
+
"getContext",
|
|
10
|
+
"listDependencies",
|
|
11
|
+
"dependency",
|
|
12
|
+
"dependencyURL",
|
|
13
|
+
"openAppPage",
|
|
14
|
+
];
|
|
15
|
+
if (methods.some((method) => typeof sdk[method] !== "function")) {
|
|
16
|
+
throw new Error("Metis browser SDK loaded without the required platform methods");
|
|
6
17
|
}
|
|
7
18
|
return sdk;
|
|
8
19
|
}
|
|
9
|
-
/**
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
/** Load the platform-injected browser capabilities and return a ready SDK. */
|
|
21
|
+
export function init(options = {}) {
|
|
22
|
+
if (initialization) {
|
|
23
|
+
return initialization;
|
|
24
|
+
}
|
|
25
|
+
const browserWindow = globalThis.window;
|
|
26
|
+
const document = globalThis.document;
|
|
27
|
+
if (!browserWindow) {
|
|
28
|
+
return Promise.reject(new Error("Metis browser SDK requires a browser environment"));
|
|
29
|
+
}
|
|
30
|
+
if (browserWindow.Metis) {
|
|
31
|
+
return Promise.resolve(validateSDK(browserWindow.Metis));
|
|
32
|
+
}
|
|
33
|
+
if (!document) {
|
|
34
|
+
return Promise.reject(new Error("Metis browser SDK requires a browser document"));
|
|
35
|
+
}
|
|
36
|
+
const timeoutMs = options.timeoutMs ?? 10000;
|
|
37
|
+
const script = document.createElement("script");
|
|
38
|
+
let timer;
|
|
39
|
+
let resolveInitialization;
|
|
40
|
+
let rejectInitialization;
|
|
41
|
+
initialization = new Promise((resolve, reject) => {
|
|
42
|
+
resolveInitialization = resolve;
|
|
43
|
+
rejectInitialization = reject;
|
|
44
|
+
});
|
|
45
|
+
const pending = initialization;
|
|
46
|
+
const fail = (error) => {
|
|
47
|
+
if (timer)
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
initialization = undefined;
|
|
50
|
+
rejectInitialization(error);
|
|
51
|
+
};
|
|
52
|
+
script.src = options.scriptUrl ?? "/api/runtime/v1/browser-sdk.js";
|
|
53
|
+
script.async = true;
|
|
54
|
+
if (options.nonce)
|
|
55
|
+
script.nonce = options.nonce;
|
|
56
|
+
script.onload = () => {
|
|
57
|
+
try {
|
|
58
|
+
if (timer)
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
const sdk = validateSDK(browserWindow.Metis);
|
|
61
|
+
initialization = undefined;
|
|
62
|
+
resolveInitialization(sdk);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
fail(error instanceof Error ? error : new Error(String(error)));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
script.onerror = () => fail(new Error(`Failed to load Metis browser SDK: ${script.src}`));
|
|
69
|
+
timer = setTimeout(() => fail(new Error(`Metis browser SDK load timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
70
|
+
try {
|
|
71
|
+
document.head.appendChild(script);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
fail(error instanceof Error ? error : new Error(String(error)));
|
|
75
|
+
}
|
|
76
|
+
return pending;
|
|
77
|
+
}
|