@webex/webex-core 3.12.0-next.62 → 3.12.0-next.64

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.
@@ -0,0 +1,124 @@
1
+ /*!
2
+ * Copyright (c) 2015-2026 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ // @ts-ignore
6
+ import StatelessWebexPlugin from './stateless-webex-plugin';
7
+
8
+ /**
9
+ * Detects an HTML document prefix in an otherwise successful binary asset response.
10
+ * @param {ArrayBuffer} buffer
11
+ * @returns {boolean}
12
+ */
13
+ function looksLikeHtmlDocument(buffer: ArrayBuffer): boolean {
14
+ const prefix = new TextDecoder().decode(buffer.slice(0, 256)).trimStart();
15
+
16
+ return prefix.startsWith('<');
17
+ }
18
+
19
+ /**
20
+ * Resolves and downloads static assets for SDK plugins. Requests are shared between callers and
21
+ * cached after a successful response.
22
+ */
23
+ export default class StaticAssetLoader extends StatelessWebexPlugin {
24
+ private readonly baseUrl: string;
25
+
26
+ private readonly assets = new Map<string, Promise<ArrayBuffer>>();
27
+
28
+ /**
29
+ * @param {Object} attrs
30
+ * @param {string} attrs.baseUrl - Base URL for the plugin's static assets.
31
+ * @param {Object} options
32
+ * @param {Object} [options.parent] - The parent Webex SDK instance.
33
+ */
34
+ constructor(attrs: {baseUrl: string}, options: {parent?: any} = {}) {
35
+ super(attrs, options);
36
+ this.baseUrl = attrs.baseUrl.replace(/\/+$/, ''); // remove trailing slashes
37
+ }
38
+
39
+ /**
40
+ * Returns the URL for an asset in the configured asset directory.
41
+ * @param {string} assetName
42
+ * @returns {string}
43
+ */
44
+ getUrl(assetName: string): string {
45
+ return `${this.baseUrl}/${assetName.replace(/^\/+/, '')}`; // regex removes leading slashes from the asset name
46
+ }
47
+
48
+ /**
49
+ * Downloads an asset as bytes. Failed requests are evicted so a later call can retry.
50
+ * @param {string} assetName
51
+ * @returns {Promise<Uint8Array>}
52
+ */
53
+ async getBytes(assetName: string): Promise<Uint8Array> {
54
+ const url = this.getUrl(assetName);
55
+ let asset = this.assets.get(url);
56
+
57
+ if (!asset) {
58
+ // @ts-ignore - StatelessWebexPlugin exposes request from the parent Webex instance.
59
+ asset = this.request({
60
+ method: 'GET',
61
+ uri: url,
62
+ responseType: 'buffer',
63
+ // Assets may be hosted on a third-party CDN: never send the user's token, and suppress
64
+ // the SDK's custom headers so the request doesn't trigger a CORS preflight.
65
+ addAuthHeader: false,
66
+ headers: {
67
+ trackingid: null,
68
+ 'spark-user-agent': null,
69
+ 'cisco-no-http-redirect': null,
70
+ },
71
+ })
72
+ .then((response: any) => {
73
+ if (response.statusCode < 200 || response.statusCode >= 300) {
74
+ throw new Error(`Failed to fetch static asset ${url}: HTTP ${response.statusCode}`);
75
+ }
76
+
77
+ const contentType = response.headers?.['content-type']
78
+ ?.split(';')[0]
79
+ .trim()
80
+ .toLowerCase();
81
+
82
+ if (contentType === 'text/html' || contentType === 'application/xhtml+xml') {
83
+ throw new Error(
84
+ `Failed to fetch static asset ${url}: unexpected HTML content type "${contentType}"`
85
+ );
86
+ }
87
+
88
+ const responseBody = response.body;
89
+ let buffer: ArrayBuffer;
90
+
91
+ if (responseBody instanceof ArrayBuffer) {
92
+ buffer = responseBody;
93
+ } else if (ArrayBuffer.isView(responseBody)) {
94
+ buffer = responseBody.buffer.slice(
95
+ responseBody.byteOffset,
96
+ responseBody.byteOffset + responseBody.byteLength
97
+ ) as ArrayBuffer;
98
+ } else {
99
+ throw new Error(
100
+ `Failed to fetch static asset ${url}: response body is not binary data`
101
+ );
102
+ }
103
+
104
+ // Some static servers return an HTML fallback page with a successful status and a
105
+ // generic binary content type. Reject it before callers pass those bytes to modules
106
+ // that expect binary data, such as WASM.
107
+ if (looksLikeHtmlDocument(buffer)) {
108
+ throw new Error(
109
+ `Failed to fetch static asset ${url}: response body looks like an HTML document`
110
+ );
111
+ }
112
+
113
+ return buffer;
114
+ })
115
+ .catch((error: Error) => {
116
+ this.assets.delete(url);
117
+ throw error;
118
+ });
119
+ this.assets.set(url, asset);
120
+ }
121
+
122
+ return new Uint8Array((await asset).slice(0));
123
+ }
124
+ }