@shapediver/viewer.shared.services 3.16.8 → 3.16.10
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/dist/converter/Converter.d.ts +77 -77
- package/dist/converter/Converter.js +424 -424
- package/dist/converter/Converter.js.map +1 -1
- package/dist/dom-event-engine/DomEventEngine.d.ts +56 -56
- package/dist/dom-event-engine/DomEventEngine.js +275 -275
- package/dist/dom-event-engine/DomEventEngine.js.map +1 -1
- package/dist/dom-event-engine/IDomEventListener.d.ts +10 -10
- package/dist/dom-event-engine/IDomEventListener.js +2 -2
- package/dist/event-engine/EventEngine.d.ts +35 -35
- package/dist/event-engine/EventEngine.js +107 -107
- package/dist/event-engine/EventEngine.js.map +1 -1
- package/dist/event-engine/interfaces/ICallback.d.ts +4 -4
- package/dist/event-engine/interfaces/ICallback.js +2 -2
- package/dist/event-engine/interfaces/IListener.d.ts +5 -5
- package/dist/event-engine/interfaces/IListener.js +2 -2
- package/dist/http-client/HttpClient.d.ts +106 -106
- package/dist/http-client/HttpClient.js +375 -375
- package/dist/http-client/HttpClient.js.map +1 -1
- package/dist/http-client/HttpResponse.d.ts +5 -5
- package/dist/http-client/HttpResponse.js +2 -2
- package/dist/index.d.ts +43 -43
- package/dist/index.js +102 -102
- package/dist/input-validator/InputValidator.d.ts +10 -10
- package/dist/input-validator/InputValidator.d.ts.map +1 -1
- package/dist/input-validator/InputValidator.js +169 -169
- package/dist/input-validator/InputValidator.js.map +1 -1
- package/dist/logger/ErrorTypeGuards.d.ts +41 -41
- package/dist/logger/ErrorTypeGuards.js +158 -159
- package/dist/logger/ErrorTypeGuards.js.map +1 -1
- package/dist/logger/Logger.d.ts +63 -63
- package/dist/logger/Logger.js +179 -179
- package/dist/logger/Logger.js.map +1 -1
- package/dist/logger/ShapeDiverBackendErrors.d.ts +14 -14
- package/dist/logger/ShapeDiverBackendErrors.js +28 -28
- package/dist/logger/ShapeDiverError.d.ts +33 -33
- package/dist/logger/ShapeDiverError.js +43 -43
- package/dist/logger/ShapeDiverError.js.map +1 -1
- package/dist/logger/ShapeDiverViewerErrors.d.ts +87 -87
- package/dist/logger/ShapeDiverViewerErrors.d.ts.map +1 -1
- package/dist/logger/ShapeDiverViewerErrors.js +119 -119
- package/dist/logger/ShapeDiverViewerErrors.js.map +1 -1
- package/dist/parameter-utils/ParameterUtils.d.ts +3 -3
- package/dist/parameter-utils/ParameterUtils.d.ts.map +1 -1
- package/dist/parameter-utils/ParameterUtils.js +161 -161
- package/dist/parameter-utils/ParameterUtils.js.map +1 -1
- package/dist/performance-evaluator/PerformanceEvaluator.d.ts +65 -65
- package/dist/performance-evaluator/PerformanceEvaluator.js +125 -125
- package/dist/settings-engine/SettingsEngine.d.ts +43 -43
- package/dist/settings-engine/SettingsEngine.d.ts.map +1 -1
- package/dist/settings-engine/SettingsEngine.js +162 -162
- package/dist/settings-engine/SettingsEngine.js.map +1 -1
- package/dist/settings-engine/defaults/Defaults.d.ts +2 -2
- package/dist/settings-engine/defaults/Defaults.js +102 -102
- package/dist/settings-engine/defaults/FurnitureDefaults.d.ts +2 -2
- package/dist/settings-engine/defaults/FurnitureDefaults.js +158 -158
- package/dist/settings-engine/defaults/HighPerformanceDefaults.d.ts +2 -2
- package/dist/settings-engine/defaults/HighPerformanceDefaults.js +114 -114
- package/dist/settings-engine/defaults/JewelryDefaults.d.ts +2 -2
- package/dist/settings-engine/defaults/JewelryDefaults.js +91 -91
- package/dist/settings-engine/defaults/MedicalDefaults.d.ts +2 -2
- package/dist/settings-engine/defaults/MedicalDefaults.js +102 -102
- package/dist/settings-engine/defaults/TwoDimensionalDefaults.d.ts +2 -2
- package/dist/settings-engine/defaults/TwoDimensionalDefaults.js +83 -83
- package/dist/state-engine/ISessionGlobalAccessObjectDefinition.d.ts +21 -21
- package/dist/state-engine/ISessionGlobalAccessObjectDefinition.js +2 -2
- package/dist/state-engine/IViewportGlobalAccessObjectDefinition.d.ts +33 -33
- package/dist/state-engine/IViewportGlobalAccessObjectDefinition.js +2 -2
- package/dist/state-engine/StateEngine.d.ts +18 -18
- package/dist/state-engine/StateEngine.js +32 -32
- package/dist/state-engine/StatePromise.d.ts +13 -13
- package/dist/state-engine/StatePromise.js +55 -55
- package/dist/system-info/SystemInfo.d.ts +57 -57
- package/dist/system-info/SystemInfo.js +127 -127
- package/dist/system-info/SystemInfo.js.map +1 -1
- package/dist/type-check/TypeChecker.d.ts +6 -6
- package/dist/type-check/TypeChecker.js +19 -19
- package/dist/utilities/ObservableArray.d.ts +25 -25
- package/dist/utilities/ObservableArray.d.ts.map +1 -1
- package/dist/utilities/ObservableArray.js +64 -64
- package/dist/utilities/ObservableArray.js.map +1 -1
- package/dist/utilities/base64.d.ts +2 -2
- package/dist/utilities/base64.d.ts.map +1 -1
- package/dist/utilities/base64.js +21 -21
- package/dist/utilities/base64.js.map +1 -1
- package/dist/utilities/hashCreator.d.ts +7 -7
- package/dist/utilities/hashCreator.d.ts.map +1 -1
- package/dist/utilities/hashCreator.js +29 -29
- package/dist/utilities/hashCreator.js.map +1 -1
- package/dist/utilities/numberCleaner.d.ts +1 -1
- package/dist/utilities/numberCleaner.d.ts.map +1 -1
- package/dist/utilities/numberCleaner.js +8 -8
- package/dist/uuid-generator/UuidGenerator.d.ts +28 -28
- package/dist/uuid-generator/UuidGenerator.js +46 -46
- package/package.json +7 -7
|
@@ -1,376 +1,376 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.HttpClient = void 0;
|
|
16
|
-
const sdk_geometry_api_sdk_v2_1 = require("@shapediver/sdk.geometry-api-sdk-v2");
|
|
17
|
-
const axios_1 = __importDefault(require("axios"));
|
|
18
|
-
const Converter_1 = require("../converter/Converter");
|
|
19
|
-
const Logger_1 = require("../logger/Logger");
|
|
20
|
-
const ShapeDiverBackendErrors_1 = require("../logger/ShapeDiverBackendErrors");
|
|
21
|
-
const base64_1 = require("../utilities/base64");
|
|
22
|
-
class HttpClient {
|
|
23
|
-
// #endregion Properties (7)
|
|
24
|
-
// #region Constructors (1)
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
26
|
-
constructor() {
|
|
27
|
-
// #region Properties (7)
|
|
28
|
-
this._logger = Logger_1.Logger.instance;
|
|
29
|
-
this._dataCache = new Map();
|
|
30
|
-
this._enableCaching = true;
|
|
31
|
-
this._excludedQueryParameters = [
|
|
32
|
-
"Expires",
|
|
33
|
-
"Signature",
|
|
34
|
-
"Key-Pair-Id",
|
|
35
|
-
];
|
|
36
|
-
this._maxCacheSize = 1024 * 1024 * 32;
|
|
37
|
-
this._sessionLoading = {};
|
|
38
|
-
}
|
|
39
|
-
// #endregion Constructors (1)
|
|
40
|
-
// #region Public Static Getters And Setters (1)
|
|
41
|
-
static get instance() {
|
|
42
|
-
return this._instance || (this._instance = new this());
|
|
43
|
-
}
|
|
44
|
-
// #endregion Public Static Getters And Setters (1)
|
|
45
|
-
// #region Public Getters And Setters (6)
|
|
46
|
-
get enableCaching() {
|
|
47
|
-
return this._enableCaching;
|
|
48
|
-
}
|
|
49
|
-
set enableCaching(value) {
|
|
50
|
-
this._enableCaching = value;
|
|
51
|
-
if (this._enableCaching === false)
|
|
52
|
-
this._dataCache.clear();
|
|
53
|
-
}
|
|
54
|
-
get excludedQueryParameters() {
|
|
55
|
-
return this._excludedQueryParameters;
|
|
56
|
-
}
|
|
57
|
-
set excludedQueryParameters(value) {
|
|
58
|
-
this._excludedQueryParameters = value;
|
|
59
|
-
}
|
|
60
|
-
get maxCacheSize() {
|
|
61
|
-
return this._maxCacheSize;
|
|
62
|
-
}
|
|
63
|
-
set maxCacheSize(value) {
|
|
64
|
-
this._maxCacheSize = value;
|
|
65
|
-
}
|
|
66
|
-
// #endregion Public Getters And Setters (6)
|
|
67
|
-
// #region Public Methods (5)
|
|
68
|
-
/**
|
|
69
|
-
* Add the data loading options from a session.
|
|
70
|
-
*
|
|
71
|
-
* @param sessionId
|
|
72
|
-
* @param callbacks
|
|
73
|
-
*/
|
|
74
|
-
addDataLoading(sessionId, callbacks) {
|
|
75
|
-
this._sessionLoading[sessionId] = callbacks;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Maps the geometry backend error to the corresponding viewer errors:
|
|
79
|
-
* - ResError is mapped to ShapeDiverGeometryBackendResponseError
|
|
80
|
-
* - ReqError is mapped to ShapeDiverGeometryBackendRequestError
|
|
81
|
-
*
|
|
82
|
-
* Other error types are thrown as is.
|
|
83
|
-
*
|
|
84
|
-
* @param e
|
|
85
|
-
*/
|
|
86
|
-
convertError(err) {
|
|
87
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
const e = err instanceof Error ? yield (0, sdk_geometry_api_sdk_v2_1.processError)(err) : err;
|
|
89
|
-
let convertedError = e;
|
|
90
|
-
if (e instanceof sdk_geometry_api_sdk_v2_1.ResponseError) {
|
|
91
|
-
convertedError = new ShapeDiverBackendErrors_1.ShapeDiverGeometryBackendResponseError(e.message, e.status, e.type, e.description);
|
|
92
|
-
}
|
|
93
|
-
else if (e instanceof sdk_geometry_api_sdk_v2_1.RequestError) {
|
|
94
|
-
convertedError = new ShapeDiverBackendErrors_1.ShapeDiverGeometryBackendRequestError(e.message);
|
|
95
|
-
}
|
|
96
|
-
else if (e instanceof sdk_geometry_api_sdk_v2_1.SdGeometryError) {
|
|
97
|
-
convertedError = new ShapeDiverBackendErrors_1.ShapeDiverGeometryBackendError(e.message);
|
|
98
|
-
}
|
|
99
|
-
return convertedError;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Get the requested resource either as a download or from the cache.
|
|
104
|
-
* If available, the registered session loading is used for download.
|
|
105
|
-
* Textures are downloaded via a specific endpoint and can be converted in this step as well.
|
|
106
|
-
* Depending on the provided caching options, the requested resource might already be cached.
|
|
107
|
-
*
|
|
108
|
-
* @param href
|
|
109
|
-
* @param config
|
|
110
|
-
* @param textureLoading
|
|
111
|
-
* @param textureConversion
|
|
112
|
-
* @returns
|
|
113
|
-
*/
|
|
114
|
-
get(
|
|
115
|
-
return __awaiter(this,
|
|
116
|
-
const dataKey = this.hrefToDataKey(href);
|
|
117
|
-
// return element if it exists in cache
|
|
118
|
-
if (this._dataCache.has(dataKey))
|
|
119
|
-
return this.getFromCache(dataKey);
|
|
120
|
-
// try to get sessionId from href
|
|
121
|
-
let sessionId = this.getSessionId(href);
|
|
122
|
-
// if href does not have sessionId, use the first session, if available
|
|
123
|
-
if (!sessionId && Object.keys(this._sessionLoading).length > 0)
|
|
124
|
-
sessionId = Object.keys(this._sessionLoading)[0];
|
|
125
|
-
// get the session loading functions, if available
|
|
126
|
-
let sessionLoading;
|
|
127
|
-
if (sessionId)
|
|
128
|
-
sessionLoading = this._sessionLoading[sessionId];
|
|
129
|
-
let loadingPromise;
|
|
130
|
-
// separation texture vs everything else
|
|
131
|
-
if (textureLoading) {
|
|
132
|
-
// if we have a sessionId and the sessionLoading functions and the image is not a blob or data, we load it via the sdk
|
|
133
|
-
if (sessionLoading !== undefined &&
|
|
134
|
-
sessionId !== undefined &&
|
|
135
|
-
!href.startsWith("blob:") &&
|
|
136
|
-
!href.startsWith("data:")) {
|
|
137
|
-
// take first session to load a texture that is not session related
|
|
138
|
-
loadingPromise = new Promise((resolve, reject) => {
|
|
139
|
-
sessionLoading
|
|
140
|
-
.downloadTexture(href)
|
|
141
|
-
.then((result) => __awaiter(this, void 0, void 0, function* () {
|
|
142
|
-
resolve({
|
|
143
|
-
data: result[0],
|
|
144
|
-
headers: {
|
|
145
|
-
"content-type": result[1],
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
}))
|
|
149
|
-
.catch((e) => reject(e));
|
|
150
|
-
}).catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
151
|
-
throw yield this.convertError(e);
|
|
152
|
-
}));
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
// we can load blobs and data urls directly
|
|
156
|
-
// or load it directly if we don't have a session
|
|
157
|
-
loadingPromise = (0, axios_1.default)(href, Object.assign({ method: "get" }, config))
|
|
158
|
-
.then((result) => __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
return result;
|
|
160
|
-
}))
|
|
161
|
-
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
162
|
-
throw yield this.convertError(e);
|
|
163
|
-
}));
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
if (!sessionLoading) {
|
|
168
|
-
// if there is no session to load from, we use the fallback option
|
|
169
|
-
loadingPromise = (0, axios_1.default)(href, Object.assign({ method: "get" }, config)).catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
throw yield this.convertError(e);
|
|
171
|
-
}));
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
// all data links where we could somehow find a session to load it with
|
|
175
|
-
loadingPromise = new Promise((resolve, reject) => {
|
|
176
|
-
sessionLoading
|
|
177
|
-
.getAsset(href)
|
|
178
|
-
.then((result) => {
|
|
179
|
-
resolve({
|
|
180
|
-
data: result[0],
|
|
181
|
-
headers: {
|
|
182
|
-
"content-type": result[1],
|
|
183
|
-
},
|
|
184
|
-
});
|
|
185
|
-
})
|
|
186
|
-
.catch(() => {
|
|
187
|
-
// if this fails, we just load it directly
|
|
188
|
-
const axiosPromise = (0, axios_1.default)(href, Object.assign({ method: "get" }, config));
|
|
189
|
-
axiosPromise.catch((e) => reject(e));
|
|
190
|
-
resolve(axiosPromise);
|
|
191
|
-
});
|
|
192
|
-
}).catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
193
|
-
throw yield this.convertError(e);
|
|
194
|
-
}));
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
if (this.enableCaching)
|
|
198
|
-
this.addToCache(dataKey, loadingPromise);
|
|
199
|
-
return loadingPromise;
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Get the requested texture either as a download or from the cache.
|
|
204
|
-
* If the texture is not available, undefined is returned.
|
|
205
|
-
*
|
|
206
|
-
* @param href The URL of the texture to load.
|
|
207
|
-
* @returns Either the texture as a buffer and blob or undefined if the texture could not be loaded.
|
|
208
|
-
*/
|
|
209
|
-
loadTexture(href) {
|
|
210
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
const dataKey = this.hrefToDataKey(href);
|
|
212
|
-
// return element if it exists in cache
|
|
213
|
-
if (this._dataCache.has(dataKey))
|
|
214
|
-
return this.getFromCache(dataKey);
|
|
215
|
-
let loadingPromise = this.get(href, undefined, true)
|
|
216
|
-
.then((response) => __awaiter(this, void 0, void 0, function* () {
|
|
217
|
-
const buffer = response.data;
|
|
218
|
-
const arrayBufferView = new Uint8Array(response.data);
|
|
219
|
-
const blob = new Blob([arrayBufferView], {
|
|
220
|
-
type: response.headers["content-type"],
|
|
221
|
-
});
|
|
222
|
-
if (typeof window !== "undefined") {
|
|
223
|
-
const image = yield Converter_1.Converter.instance.responseToImage({
|
|
224
|
-
data: {
|
|
225
|
-
buffer,
|
|
226
|
-
blob,
|
|
227
|
-
},
|
|
228
|
-
size: response.data.byteLength,
|
|
229
|
-
headers: response.headers,
|
|
230
|
-
});
|
|
231
|
-
return {
|
|
232
|
-
data: {
|
|
233
|
-
image,
|
|
234
|
-
buffer,
|
|
235
|
-
blob,
|
|
236
|
-
},
|
|
237
|
-
size: response.data.byteLength,
|
|
238
|
-
headers: response.headers,
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
return {
|
|
243
|
-
data: {
|
|
244
|
-
buffer,
|
|
245
|
-
blob,
|
|
246
|
-
},
|
|
247
|
-
size: response.data.byteLength,
|
|
248
|
-
headers: response.headers,
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
}))
|
|
252
|
-
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
253
|
-
throw yield this.convertError(e);
|
|
254
|
-
}));
|
|
255
|
-
// add the result to the cache
|
|
256
|
-
this.addToCache(dataKey, loadingPromise);
|
|
257
|
-
// return undefined if the texture could not be loaded
|
|
258
|
-
// that way the loading can be continued without the texture
|
|
259
|
-
return loadingPromise;
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Add the data loading options from a session.
|
|
264
|
-
*
|
|
265
|
-
* @param sessionId
|
|
266
|
-
*/
|
|
267
|
-
removeDataLoading(sessionId) {
|
|
268
|
-
delete this._sessionLoading[sessionId];
|
|
269
|
-
}
|
|
270
|
-
// #endregion Public Methods (5)
|
|
271
|
-
// #region Private Methods (5)
|
|
272
|
-
/**
|
|
273
|
-
* Add
|
|
274
|
-
*
|
|
275
|
-
* @param key
|
|
276
|
-
* @param value
|
|
277
|
-
*/
|
|
278
|
-
addToCache(key, value) {
|
|
279
|
-
// Remove items from the cache until the cache size is smaller than the maximum cache size.
|
|
280
|
-
// Only resolved promises are evaluated, as unresolved promises don't add any size.
|
|
281
|
-
let oldestKey = this._dataCache.keys().next().value;
|
|
282
|
-
while (this.calculateCacheSize() >= this._maxCacheSize && oldestKey) {
|
|
283
|
-
// Remove the oldest entry if the cache is full
|
|
284
|
-
this._dataCache.delete(oldestKey);
|
|
285
|
-
oldestKey = this._dataCache.keys().next().value;
|
|
286
|
-
}
|
|
287
|
-
const timestamp = Date.now();
|
|
288
|
-
this._dataCache.set(key, { value, timestamp, resolved: false });
|
|
289
|
-
// once the promise resolves, set resolved and size properties
|
|
290
|
-
value
|
|
291
|
-
.then((promiseResult) => {
|
|
292
|
-
const size = promiseResult.size
|
|
293
|
-
? promiseResult.size
|
|
294
|
-
: promiseResult.data.byteLength;
|
|
295
|
-
this._dataCache.set(key, {
|
|
296
|
-
value,
|
|
297
|
-
timestamp,
|
|
298
|
-
resolved: true,
|
|
299
|
-
size,
|
|
300
|
-
});
|
|
301
|
-
})
|
|
302
|
-
.catch(() => {
|
|
303
|
-
// Rejection is handled by the caller; suppress unhandled rejection on this branch.
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Calculate the current cache size from all resolved promises.
|
|
308
|
-
*
|
|
309
|
-
* @returns
|
|
310
|
-
*/
|
|
311
|
-
calculateCacheSize() {
|
|
312
|
-
let size = 0;
|
|
313
|
-
this._dataCache.forEach((value) => {
|
|
314
|
-
if (value.resolved === true)
|
|
315
|
-
size += value.size;
|
|
316
|
-
});
|
|
317
|
-
return size;
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Get the value of an object from the cache.
|
|
321
|
-
*
|
|
322
|
-
* @param key
|
|
323
|
-
* @returns
|
|
324
|
-
*/
|
|
325
|
-
getFromCache(key) {
|
|
326
|
-
const cachedObject = this._dataCache.get(key);
|
|
327
|
-
this._dataCache.set(key, {
|
|
328
|
-
value: cachedObject.value,
|
|
329
|
-
timestamp: Date.now(),
|
|
330
|
-
resolved: cachedObject.resolved,
|
|
331
|
-
size: cachedObject.size,
|
|
332
|
-
});
|
|
333
|
-
return cachedObject.value;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Get the session id of the provided href.
|
|
337
|
-
*
|
|
338
|
-
* @param href
|
|
339
|
-
* @returns
|
|
340
|
-
*/
|
|
341
|
-
getSessionId(href) {
|
|
342
|
-
// searching for "/session/SESSION_ID/{'output' | 'export' | 'texture'}/ASSET_DATA"
|
|
343
|
-
const parts = href.split("/");
|
|
344
|
-
const sessionPartIndex = parts.indexOf("session");
|
|
345
|
-
// There have to be at exactly 4 parts, including the session
|
|
346
|
-
if (sessionPartIndex !== -1 && parts.length === sessionPartIndex + 4) {
|
|
347
|
-
const sessionId = parts[sessionPartIndex + 1];
|
|
348
|
-
// no such session has been registered, should never happen
|
|
349
|
-
if (!this._sessionLoading[sessionId])
|
|
350
|
-
return;
|
|
351
|
-
return sessionId;
|
|
352
|
-
}
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Convert the provided href to a data cache key.
|
|
357
|
-
* In this conversion the excludedQueryParameters are removed from the href.
|
|
358
|
-
*
|
|
359
|
-
* @param href
|
|
360
|
-
* @returns
|
|
361
|
-
*/
|
|
362
|
-
hrefToDataKey(href) {
|
|
363
|
-
const url = new URL(href);
|
|
364
|
-
// Create a URLSearchParams object from the existing query parameters
|
|
365
|
-
const params = new URLSearchParams(url.search);
|
|
366
|
-
for (let i = 0; i < this._excludedQueryParameters.length; i++)
|
|
367
|
-
// Remove specific query parameters
|
|
368
|
-
params.delete(this._excludedQueryParameters[i]);
|
|
369
|
-
// Reconstruct the URL with the modified query parameters
|
|
370
|
-
url.search = params.toString();
|
|
371
|
-
const hrefAsKey = url.toString();
|
|
372
|
-
return (0, base64_1.btoaCustom)(hrefAsKey);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
exports.HttpClient = HttpClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.HttpClient = void 0;
|
|
16
|
+
const sdk_geometry_api_sdk_v2_1 = require("@shapediver/sdk.geometry-api-sdk-v2");
|
|
17
|
+
const axios_1 = __importDefault(require("axios"));
|
|
18
|
+
const Converter_1 = require("../converter/Converter");
|
|
19
|
+
const Logger_1 = require("../logger/Logger");
|
|
20
|
+
const ShapeDiverBackendErrors_1 = require("../logger/ShapeDiverBackendErrors");
|
|
21
|
+
const base64_1 = require("../utilities/base64");
|
|
22
|
+
class HttpClient {
|
|
23
|
+
// #endregion Properties (7)
|
|
24
|
+
// #region Constructors (1)
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
26
|
+
constructor() {
|
|
27
|
+
// #region Properties (7)
|
|
28
|
+
this._logger = Logger_1.Logger.instance;
|
|
29
|
+
this._dataCache = new Map();
|
|
30
|
+
this._enableCaching = true;
|
|
31
|
+
this._excludedQueryParameters = [
|
|
32
|
+
"Expires",
|
|
33
|
+
"Signature",
|
|
34
|
+
"Key-Pair-Id",
|
|
35
|
+
];
|
|
36
|
+
this._maxCacheSize = 1024 * 1024 * 32;
|
|
37
|
+
this._sessionLoading = {};
|
|
38
|
+
}
|
|
39
|
+
// #endregion Constructors (1)
|
|
40
|
+
// #region Public Static Getters And Setters (1)
|
|
41
|
+
static get instance() {
|
|
42
|
+
return this._instance || (this._instance = new this());
|
|
43
|
+
}
|
|
44
|
+
// #endregion Public Static Getters And Setters (1)
|
|
45
|
+
// #region Public Getters And Setters (6)
|
|
46
|
+
get enableCaching() {
|
|
47
|
+
return this._enableCaching;
|
|
48
|
+
}
|
|
49
|
+
set enableCaching(value) {
|
|
50
|
+
this._enableCaching = value;
|
|
51
|
+
if (this._enableCaching === false)
|
|
52
|
+
this._dataCache.clear();
|
|
53
|
+
}
|
|
54
|
+
get excludedQueryParameters() {
|
|
55
|
+
return this._excludedQueryParameters;
|
|
56
|
+
}
|
|
57
|
+
set excludedQueryParameters(value) {
|
|
58
|
+
this._excludedQueryParameters = value;
|
|
59
|
+
}
|
|
60
|
+
get maxCacheSize() {
|
|
61
|
+
return this._maxCacheSize;
|
|
62
|
+
}
|
|
63
|
+
set maxCacheSize(value) {
|
|
64
|
+
this._maxCacheSize = value;
|
|
65
|
+
}
|
|
66
|
+
// #endregion Public Getters And Setters (6)
|
|
67
|
+
// #region Public Methods (5)
|
|
68
|
+
/**
|
|
69
|
+
* Add the data loading options from a session.
|
|
70
|
+
*
|
|
71
|
+
* @param sessionId
|
|
72
|
+
* @param callbacks
|
|
73
|
+
*/
|
|
74
|
+
addDataLoading(sessionId, callbacks) {
|
|
75
|
+
this._sessionLoading[sessionId] = callbacks;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Maps the geometry backend error to the corresponding viewer errors:
|
|
79
|
+
* - ResError is mapped to ShapeDiverGeometryBackendResponseError
|
|
80
|
+
* - ReqError is mapped to ShapeDiverGeometryBackendRequestError
|
|
81
|
+
*
|
|
82
|
+
* Other error types are thrown as is.
|
|
83
|
+
*
|
|
84
|
+
* @param e
|
|
85
|
+
*/
|
|
86
|
+
convertError(err) {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
const e = err instanceof Error ? yield (0, sdk_geometry_api_sdk_v2_1.processError)(err) : err;
|
|
89
|
+
let convertedError = e;
|
|
90
|
+
if (e instanceof sdk_geometry_api_sdk_v2_1.ResponseError) {
|
|
91
|
+
convertedError = new ShapeDiverBackendErrors_1.ShapeDiverGeometryBackendResponseError(e.message, e.status, e.type, e.description);
|
|
92
|
+
}
|
|
93
|
+
else if (e instanceof sdk_geometry_api_sdk_v2_1.RequestError) {
|
|
94
|
+
convertedError = new ShapeDiverBackendErrors_1.ShapeDiverGeometryBackendRequestError(e.message);
|
|
95
|
+
}
|
|
96
|
+
else if (e instanceof sdk_geometry_api_sdk_v2_1.SdGeometryError) {
|
|
97
|
+
convertedError = new ShapeDiverBackendErrors_1.ShapeDiverGeometryBackendError(e.message);
|
|
98
|
+
}
|
|
99
|
+
return convertedError;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the requested resource either as a download or from the cache.
|
|
104
|
+
* If available, the registered session loading is used for download.
|
|
105
|
+
* Textures are downloaded via a specific endpoint and can be converted in this step as well.
|
|
106
|
+
* Depending on the provided caching options, the requested resource might already be cached.
|
|
107
|
+
*
|
|
108
|
+
* @param href
|
|
109
|
+
* @param config
|
|
110
|
+
* @param textureLoading
|
|
111
|
+
* @param textureConversion
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
get(href_1) {
|
|
115
|
+
return __awaiter(this, arguments, void 0, function* (href, config = { responseType: "arraybuffer" }, textureLoading = false) {
|
|
116
|
+
const dataKey = this.hrefToDataKey(href);
|
|
117
|
+
// return element if it exists in cache
|
|
118
|
+
if (this._dataCache.has(dataKey))
|
|
119
|
+
return this.getFromCache(dataKey);
|
|
120
|
+
// try to get sessionId from href
|
|
121
|
+
let sessionId = this.getSessionId(href);
|
|
122
|
+
// if href does not have sessionId, use the first session, if available
|
|
123
|
+
if (!sessionId && Object.keys(this._sessionLoading).length > 0)
|
|
124
|
+
sessionId = Object.keys(this._sessionLoading)[0];
|
|
125
|
+
// get the session loading functions, if available
|
|
126
|
+
let sessionLoading;
|
|
127
|
+
if (sessionId)
|
|
128
|
+
sessionLoading = this._sessionLoading[sessionId];
|
|
129
|
+
let loadingPromise;
|
|
130
|
+
// separation texture vs everything else
|
|
131
|
+
if (textureLoading) {
|
|
132
|
+
// if we have a sessionId and the sessionLoading functions and the image is not a blob or data, we load it via the sdk
|
|
133
|
+
if (sessionLoading !== undefined &&
|
|
134
|
+
sessionId !== undefined &&
|
|
135
|
+
!href.startsWith("blob:") &&
|
|
136
|
+
!href.startsWith("data:")) {
|
|
137
|
+
// take first session to load a texture that is not session related
|
|
138
|
+
loadingPromise = new Promise((resolve, reject) => {
|
|
139
|
+
sessionLoading
|
|
140
|
+
.downloadTexture(href)
|
|
141
|
+
.then((result) => __awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
resolve({
|
|
143
|
+
data: result[0],
|
|
144
|
+
headers: {
|
|
145
|
+
"content-type": result[1],
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
}))
|
|
149
|
+
.catch((e) => reject(e));
|
|
150
|
+
}).catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
throw yield this.convertError(e);
|
|
152
|
+
}));
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
// we can load blobs and data urls directly
|
|
156
|
+
// or load it directly if we don't have a session
|
|
157
|
+
loadingPromise = (0, axios_1.default)(href, Object.assign({ method: "get" }, config))
|
|
158
|
+
.then((result) => __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
return result;
|
|
160
|
+
}))
|
|
161
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
162
|
+
throw yield this.convertError(e);
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
if (!sessionLoading) {
|
|
168
|
+
// if there is no session to load from, we use the fallback option
|
|
169
|
+
loadingPromise = (0, axios_1.default)(href, Object.assign({ method: "get" }, config)).catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
throw yield this.convertError(e);
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
// all data links where we could somehow find a session to load it with
|
|
175
|
+
loadingPromise = new Promise((resolve, reject) => {
|
|
176
|
+
sessionLoading
|
|
177
|
+
.getAsset(href)
|
|
178
|
+
.then((result) => {
|
|
179
|
+
resolve({
|
|
180
|
+
data: result[0],
|
|
181
|
+
headers: {
|
|
182
|
+
"content-type": result[1],
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
})
|
|
186
|
+
.catch(() => {
|
|
187
|
+
// if this fails, we just load it directly
|
|
188
|
+
const axiosPromise = (0, axios_1.default)(href, Object.assign({ method: "get" }, config));
|
|
189
|
+
axiosPromise.catch((e) => reject(e));
|
|
190
|
+
resolve(axiosPromise);
|
|
191
|
+
});
|
|
192
|
+
}).catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
193
|
+
throw yield this.convertError(e);
|
|
194
|
+
}));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (this.enableCaching)
|
|
198
|
+
this.addToCache(dataKey, loadingPromise);
|
|
199
|
+
return loadingPromise;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Get the requested texture either as a download or from the cache.
|
|
204
|
+
* If the texture is not available, undefined is returned.
|
|
205
|
+
*
|
|
206
|
+
* @param href The URL of the texture to load.
|
|
207
|
+
* @returns Either the texture as a buffer and blob or undefined if the texture could not be loaded.
|
|
208
|
+
*/
|
|
209
|
+
loadTexture(href) {
|
|
210
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
+
const dataKey = this.hrefToDataKey(href);
|
|
212
|
+
// return element if it exists in cache
|
|
213
|
+
if (this._dataCache.has(dataKey))
|
|
214
|
+
return this.getFromCache(dataKey);
|
|
215
|
+
let loadingPromise = this.get(href, undefined, true)
|
|
216
|
+
.then((response) => __awaiter(this, void 0, void 0, function* () {
|
|
217
|
+
const buffer = response.data;
|
|
218
|
+
const arrayBufferView = new Uint8Array(response.data);
|
|
219
|
+
const blob = new Blob([arrayBufferView], {
|
|
220
|
+
type: response.headers["content-type"],
|
|
221
|
+
});
|
|
222
|
+
if (typeof window !== "undefined") {
|
|
223
|
+
const image = yield Converter_1.Converter.instance.responseToImage({
|
|
224
|
+
data: {
|
|
225
|
+
buffer,
|
|
226
|
+
blob,
|
|
227
|
+
},
|
|
228
|
+
size: response.data.byteLength,
|
|
229
|
+
headers: response.headers,
|
|
230
|
+
});
|
|
231
|
+
return {
|
|
232
|
+
data: {
|
|
233
|
+
image,
|
|
234
|
+
buffer,
|
|
235
|
+
blob,
|
|
236
|
+
},
|
|
237
|
+
size: response.data.byteLength,
|
|
238
|
+
headers: response.headers,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
return {
|
|
243
|
+
data: {
|
|
244
|
+
buffer,
|
|
245
|
+
blob,
|
|
246
|
+
},
|
|
247
|
+
size: response.data.byteLength,
|
|
248
|
+
headers: response.headers,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}))
|
|
252
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
253
|
+
throw yield this.convertError(e);
|
|
254
|
+
}));
|
|
255
|
+
// add the result to the cache
|
|
256
|
+
this.addToCache(dataKey, loadingPromise);
|
|
257
|
+
// return undefined if the texture could not be loaded
|
|
258
|
+
// that way the loading can be continued without the texture
|
|
259
|
+
return loadingPromise;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Add the data loading options from a session.
|
|
264
|
+
*
|
|
265
|
+
* @param sessionId
|
|
266
|
+
*/
|
|
267
|
+
removeDataLoading(sessionId) {
|
|
268
|
+
delete this._sessionLoading[sessionId];
|
|
269
|
+
}
|
|
270
|
+
// #endregion Public Methods (5)
|
|
271
|
+
// #region Private Methods (5)
|
|
272
|
+
/**
|
|
273
|
+
* Add
|
|
274
|
+
*
|
|
275
|
+
* @param key
|
|
276
|
+
* @param value
|
|
277
|
+
*/
|
|
278
|
+
addToCache(key, value) {
|
|
279
|
+
// Remove items from the cache until the cache size is smaller than the maximum cache size.
|
|
280
|
+
// Only resolved promises are evaluated, as unresolved promises don't add any size.
|
|
281
|
+
let oldestKey = this._dataCache.keys().next().value;
|
|
282
|
+
while (this.calculateCacheSize() >= this._maxCacheSize && oldestKey) {
|
|
283
|
+
// Remove the oldest entry if the cache is full
|
|
284
|
+
this._dataCache.delete(oldestKey);
|
|
285
|
+
oldestKey = this._dataCache.keys().next().value;
|
|
286
|
+
}
|
|
287
|
+
const timestamp = Date.now();
|
|
288
|
+
this._dataCache.set(key, { value, timestamp, resolved: false });
|
|
289
|
+
// once the promise resolves, set resolved and size properties
|
|
290
|
+
value
|
|
291
|
+
.then((promiseResult) => {
|
|
292
|
+
const size = promiseResult.size
|
|
293
|
+
? promiseResult.size
|
|
294
|
+
: promiseResult.data.byteLength;
|
|
295
|
+
this._dataCache.set(key, {
|
|
296
|
+
value,
|
|
297
|
+
timestamp,
|
|
298
|
+
resolved: true,
|
|
299
|
+
size,
|
|
300
|
+
});
|
|
301
|
+
})
|
|
302
|
+
.catch(() => {
|
|
303
|
+
// Rejection is handled by the caller; suppress unhandled rejection on this branch.
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Calculate the current cache size from all resolved promises.
|
|
308
|
+
*
|
|
309
|
+
* @returns
|
|
310
|
+
*/
|
|
311
|
+
calculateCacheSize() {
|
|
312
|
+
let size = 0;
|
|
313
|
+
this._dataCache.forEach((value) => {
|
|
314
|
+
if (value.resolved === true)
|
|
315
|
+
size += value.size;
|
|
316
|
+
});
|
|
317
|
+
return size;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Get the value of an object from the cache.
|
|
321
|
+
*
|
|
322
|
+
* @param key
|
|
323
|
+
* @returns
|
|
324
|
+
*/
|
|
325
|
+
getFromCache(key) {
|
|
326
|
+
const cachedObject = this._dataCache.get(key);
|
|
327
|
+
this._dataCache.set(key, {
|
|
328
|
+
value: cachedObject.value,
|
|
329
|
+
timestamp: Date.now(),
|
|
330
|
+
resolved: cachedObject.resolved,
|
|
331
|
+
size: cachedObject.size,
|
|
332
|
+
});
|
|
333
|
+
return cachedObject.value;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Get the session id of the provided href.
|
|
337
|
+
*
|
|
338
|
+
* @param href
|
|
339
|
+
* @returns
|
|
340
|
+
*/
|
|
341
|
+
getSessionId(href) {
|
|
342
|
+
// searching for "/session/SESSION_ID/{'output' | 'export' | 'texture'}/ASSET_DATA"
|
|
343
|
+
const parts = href.split("/");
|
|
344
|
+
const sessionPartIndex = parts.indexOf("session");
|
|
345
|
+
// There have to be at exactly 4 parts, including the session
|
|
346
|
+
if (sessionPartIndex !== -1 && parts.length === sessionPartIndex + 4) {
|
|
347
|
+
const sessionId = parts[sessionPartIndex + 1];
|
|
348
|
+
// no such session has been registered, should never happen
|
|
349
|
+
if (!this._sessionLoading[sessionId])
|
|
350
|
+
return;
|
|
351
|
+
return sessionId;
|
|
352
|
+
}
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Convert the provided href to a data cache key.
|
|
357
|
+
* In this conversion the excludedQueryParameters are removed from the href.
|
|
358
|
+
*
|
|
359
|
+
* @param href
|
|
360
|
+
* @returns
|
|
361
|
+
*/
|
|
362
|
+
hrefToDataKey(href) {
|
|
363
|
+
const url = new URL(href);
|
|
364
|
+
// Create a URLSearchParams object from the existing query parameters
|
|
365
|
+
const params = new URLSearchParams(url.search);
|
|
366
|
+
for (let i = 0; i < this._excludedQueryParameters.length; i++)
|
|
367
|
+
// Remove specific query parameters
|
|
368
|
+
params.delete(this._excludedQueryParameters[i]);
|
|
369
|
+
// Reconstruct the URL with the modified query parameters
|
|
370
|
+
url.search = params.toString();
|
|
371
|
+
const hrefAsKey = url.toString();
|
|
372
|
+
return (0, base64_1.btoaCustom)(hrefAsKey);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
exports.HttpClient = HttpClient;
|
|
376
376
|
//# sourceMappingURL=HttpClient.js.map
|