@shapediver/sdk.geometry-api-sdk-v2 1.12.2 → 2.0.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.
Files changed (137) hide show
  1. package/README.md +133 -58
  2. package/dist/UtilsApi.d.ts +20 -0
  3. package/dist/UtilsApi.d.ts.map +1 -0
  4. package/dist/UtilsApi.js +185 -0
  5. package/dist/UtilsApi.js.map +1 -0
  6. package/dist/client/api.d.ts +2117 -0
  7. package/dist/client/api.d.ts.map +1 -0
  8. package/dist/client/api.js +3473 -0
  9. package/dist/client/api.js.map +1 -0
  10. package/dist/client/base.d.ts +32 -0
  11. package/dist/client/base.d.ts.map +1 -0
  12. package/dist/client/base.js +37 -0
  13. package/dist/client/base.js.map +1 -0
  14. package/dist/client/common.d.ts +14 -0
  15. package/dist/client/common.d.ts.map +1 -0
  16. package/dist/client/common.js +108 -0
  17. package/dist/client/common.js.map +1 -0
  18. package/dist/client/configuration.d.ts +23 -0
  19. package/dist/client/configuration.d.ts.map +1 -0
  20. package/dist/client/configuration.js +21 -0
  21. package/dist/client/configuration.js.map +1 -0
  22. package/dist/client/index.d.ts +3 -0
  23. package/dist/client/index.d.ts.map +1 -0
  24. package/dist/client/index.js +19 -0
  25. package/dist/client/index.js.map +1 -0
  26. package/dist/configuration.d.ts +8 -0
  27. package/dist/configuration.d.ts.map +1 -0
  28. package/dist/configuration.js +22 -0
  29. package/dist/configuration.js.map +1 -0
  30. package/dist/error.d.ts +22 -0
  31. package/dist/error.d.ts.map +1 -0
  32. package/dist/error.js +48 -0
  33. package/dist/error.js.map +1 -0
  34. package/dist/index.d.ts +5 -6
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +5 -17
  37. package/dist/index.js.map +1 -1
  38. package/dist/utils.d.ts +12 -0
  39. package/dist/utils.d.ts.map +1 -0
  40. package/dist/utils.js +93 -0
  41. package/dist/utils.js.map +1 -0
  42. package/package.json +55 -57
  43. package/src/UtilsApi.ts +393 -0
  44. package/src/client/api.ts +14084 -0
  45. package/src/client/base.ts +86 -0
  46. package/src/client/common.ts +150 -0
  47. package/src/client/configuration.ts +110 -0
  48. package/src/client/index.ts +18 -0
  49. package/src/configuration.ts +27 -0
  50. package/src/error.ts +92 -0
  51. package/src/index.ts +5 -17
  52. package/src/utils.ts +126 -0
  53. package/dist/ShapeDiverErrors.d.ts +0 -10
  54. package/dist/ShapeDiverErrors.d.ts.map +0 -1
  55. package/dist/ShapeDiverErrors.js +0 -18
  56. package/dist/ShapeDiverErrors.js.map +0 -1
  57. package/dist/ShapeDiverSdk.d.ts +0 -48
  58. package/dist/ShapeDiverSdk.d.ts.map +0 -1
  59. package/dist/ShapeDiverSdk.js +0 -87
  60. package/dist/ShapeDiverSdk.js.map +0 -1
  61. package/dist/resources/ShapeDiverAnalyticsApi.d.ts +0 -12
  62. package/dist/resources/ShapeDiverAnalyticsApi.d.ts.map +0 -1
  63. package/dist/resources/ShapeDiverAnalyticsApi.js +0 -63
  64. package/dist/resources/ShapeDiverAnalyticsApi.js.map +0 -1
  65. package/dist/resources/ShapeDiverArSceneApi.d.ts +0 -8
  66. package/dist/resources/ShapeDiverArSceneApi.d.ts.map +0 -1
  67. package/dist/resources/ShapeDiverArSceneApi.js +0 -63
  68. package/dist/resources/ShapeDiverArSceneApi.js.map +0 -1
  69. package/dist/resources/ShapeDiverAssetApi.d.ts +0 -13
  70. package/dist/resources/ShapeDiverAssetApi.d.ts.map +0 -1
  71. package/dist/resources/ShapeDiverAssetApi.js +0 -131
  72. package/dist/resources/ShapeDiverAssetApi.js.map +0 -1
  73. package/dist/resources/ShapeDiverExportApi.d.ts +0 -10
  74. package/dist/resources/ShapeDiverExportApi.d.ts.map +0 -1
  75. package/dist/resources/ShapeDiverExportApi.js +0 -49
  76. package/dist/resources/ShapeDiverExportApi.js.map +0 -1
  77. package/dist/resources/ShapeDiverFileApi.d.ts +0 -15
  78. package/dist/resources/ShapeDiverFileApi.d.ts.map +0 -1
  79. package/dist/resources/ShapeDiverFileApi.js +0 -67
  80. package/dist/resources/ShapeDiverFileApi.js.map +0 -1
  81. package/dist/resources/ShapeDiverGltfApi.d.ts +0 -7
  82. package/dist/resources/ShapeDiverGltfApi.d.ts.map +0 -1
  83. package/dist/resources/ShapeDiverGltfApi.js +0 -32
  84. package/dist/resources/ShapeDiverGltfApi.js.map +0 -1
  85. package/dist/resources/ShapeDiverModelApi.d.ts +0 -22
  86. package/dist/resources/ShapeDiverModelApi.d.ts.map +0 -1
  87. package/dist/resources/ShapeDiverModelApi.js +0 -156
  88. package/dist/resources/ShapeDiverModelApi.js.map +0 -1
  89. package/dist/resources/ShapeDiverModelStateApi.d.ts +0 -14
  90. package/dist/resources/ShapeDiverModelStateApi.d.ts.map +0 -1
  91. package/dist/resources/ShapeDiverModelStateApi.js +0 -104
  92. package/dist/resources/ShapeDiverModelStateApi.js.map +0 -1
  93. package/dist/resources/ShapeDiverOutputApi.d.ts +0 -10
  94. package/dist/resources/ShapeDiverOutputApi.d.ts.map +0 -1
  95. package/dist/resources/ShapeDiverOutputApi.js +0 -52
  96. package/dist/resources/ShapeDiverOutputApi.js.map +0 -1
  97. package/dist/resources/ShapeDiverSdtfApi.d.ts +0 -10
  98. package/dist/resources/ShapeDiverSdtfApi.d.ts.map +0 -1
  99. package/dist/resources/ShapeDiverSdtfApi.js +0 -52
  100. package/dist/resources/ShapeDiverSdtfApi.js.map +0 -1
  101. package/dist/resources/ShapeDiverSessionApi.d.ts +0 -11
  102. package/dist/resources/ShapeDiverSessionApi.d.ts.map +0 -1
  103. package/dist/resources/ShapeDiverSessionApi.js +0 -66
  104. package/dist/resources/ShapeDiverSessionApi.js.map +0 -1
  105. package/dist/resources/ShapeDiverSystemApi.d.ts +0 -12
  106. package/dist/resources/ShapeDiverSystemApi.d.ts.map +0 -1
  107. package/dist/resources/ShapeDiverSystemApi.js +0 -63
  108. package/dist/resources/ShapeDiverSystemApi.js.map +0 -1
  109. package/dist/resources/ShapeDiverTextureApi.d.ts +0 -7
  110. package/dist/resources/ShapeDiverTextureApi.d.ts.map +0 -1
  111. package/dist/resources/ShapeDiverTextureApi.js +0 -31
  112. package/dist/resources/ShapeDiverTextureApi.js.map +0 -1
  113. package/dist/resources/ShapeDiverUtilsApi.d.ts +0 -18
  114. package/dist/resources/ShapeDiverUtilsApi.d.ts.map +0 -1
  115. package/dist/resources/ShapeDiverUtilsApi.js +0 -146
  116. package/dist/resources/ShapeDiverUtilsApi.js.map +0 -1
  117. package/dist/utils/utils.d.ts +0 -12
  118. package/dist/utils/utils.d.ts.map +0 -1
  119. package/dist/utils/utils.js +0 -109
  120. package/dist/utils/utils.js.map +0 -1
  121. package/src/ShapeDiverErrors.ts +0 -34
  122. package/src/ShapeDiverSdk.ts +0 -121
  123. package/src/resources/ShapeDiverAnalyticsApi.ts +0 -108
  124. package/src/resources/ShapeDiverArSceneApi.ts +0 -77
  125. package/src/resources/ShapeDiverAssetApi.ts +0 -241
  126. package/src/resources/ShapeDiverExportApi.ts +0 -103
  127. package/src/resources/ShapeDiverFileApi.ts +0 -150
  128. package/src/resources/ShapeDiverGltfApi.ts +0 -46
  129. package/src/resources/ShapeDiverModelApi.ts +0 -374
  130. package/src/resources/ShapeDiverModelStateApi.ts +0 -163
  131. package/src/resources/ShapeDiverOutputApi.ts +0 -109
  132. package/src/resources/ShapeDiverSdtfApi.ts +0 -103
  133. package/src/resources/ShapeDiverSessionApi.ts +0 -142
  134. package/src/resources/ShapeDiverSystemApi.ts +0 -118
  135. package/src/resources/ShapeDiverTextureApi.ts +0 -36
  136. package/src/resources/ShapeDiverUtilsApi.ts +0 -318
  137. package/src/utils/utils.ts +0 -135
@@ -1,318 +0,0 @@
1
- import {
2
- ShapeDiverRequestCache,
3
- ShapeDiverRequestCustomization,
4
- ShapeDiverRequestExport,
5
- ShapeDiverResponseDto,
6
- ShapeDiverResponseAssetUploadHeaders,
7
- ShapeDiverResponseExport,
8
- ShapeDiverResponseOutput,
9
- } from "@shapediver/api.geometry-api-dto-v2";
10
- import {
11
- BaseResourceApi,
12
- ShapeDiverError,
13
- ShapeDiverSdkApi,
14
- ShapeDiverSdkApiResponseType,
15
- } from "@shapediver/sdk.geometry-api-sdk-core";
16
- import { ShapeDiverSdk } from "../ShapeDiverSdk";
17
- import {
18
- contentDispositionFromFilename,
19
- sendRequest,
20
- sleep,
21
- } from "../utils/utils";
22
-
23
- // TypeScript wrapper for a response type that depend on an input type
24
- type ShapeDiverSdkUtilsDownloadType<T extends ShapeDiverSdkApiResponseType> =
25
- T extends ShapeDiverSdkApiResponseType.TEXT
26
- ? string
27
- : T extends ShapeDiverSdkApiResponseType.JSON
28
- ? Record<string, any>
29
- : T extends ShapeDiverSdkApiResponseType.DATA
30
- ? ArrayBuffer
31
- : never;
32
-
33
- export class ShapeDiverUtilsApi extends BaseResourceApi {
34
- constructor(api: ShapeDiverSdkApi) {
35
- super(api);
36
- }
37
-
38
- /**
39
- * Upload the given file to the specified URL.
40
- *
41
- * @param url - The target URL of the upload request.
42
- * @param data - The data that should be uploaded.
43
- * @param contentType - Indicate the original media type of the resource.
44
- * @param filename - The name of the file to be uploaded. When a filename has been specified in
45
- * the request-upload call, then the same filename has to be specified for the upload as well.
46
- */
47
- async upload(
48
- url: string,
49
- data: ArrayBuffer | Record<string, any> | string,
50
- contentType: string,
51
- filename?: string,
52
- ): Promise<any> {
53
- return await sendRequest(async () =>
54
- this.api.put<any>(url, undefined, data, {
55
- contentType: contentType,
56
- contentDisposition: filename
57
- ? contentDispositionFromFilename(filename)
58
- : undefined,
59
- responseType: ShapeDiverSdkApiResponseType.JSON,
60
- disableAuthorization: true,
61
- disableCustomHeaders: true,
62
- }),
63
- );
64
- }
65
-
66
- /**
67
- * Upload the given asset to the specified ShapeDiver URL.
68
- *
69
- * @param url - The ShapeDiver URL of the upload request.
70
- * @param data - The data that should be uploaded.
71
- * @param headers - The headers object that was returned from the request-upload call.
72
- */
73
- async uploadAsset(
74
- url: string,
75
- data: ArrayBuffer | Record<string, any> | string,
76
- headers: ShapeDiverResponseAssetUploadHeaders,
77
- ): Promise<any> {
78
- return await sendRequest(async () =>
79
- this.api.put<any>(url, undefined, data, {
80
- contentType: headers.contentType,
81
- contentDisposition: headers.contentDisposition,
82
- responseType: ShapeDiverSdkApiResponseType.JSON,
83
- disableAuthorization: true,
84
- disableCustomHeaders: true,
85
- }),
86
- );
87
- }
88
-
89
- /**
90
- * Download from the given URL.
91
- *
92
- * @param url - The target URL of the download request.
93
- * @param responseType - Indicates the type of data that the server should respond with if possible.
94
- * @returns Array of size 2: [0] = response headers, [1] = response data
95
- */
96
- async download<T extends ShapeDiverSdkApiResponseType>(
97
- url: string,
98
- responseType: T,
99
- ): Promise<[Record<string, any>, ShapeDiverSdkUtilsDownloadType<T>]> {
100
- return await sendRequest(async () =>
101
- this.api.get<any>(url, undefined, {
102
- contentType: "application/json",
103
- responseType: responseType,
104
- disableAuthorization: true,
105
- disableCustomHeaders: true,
106
- }),
107
- );
108
- }
109
-
110
- /**
111
- * Submit a customization request and wait for the result to be finished.
112
- *
113
- * @param sdk
114
- * @param sessionId
115
- * @param body
116
- * @param maxWaitMsec - Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
117
- * @throws {@link ShapeDiverError} in case a maximum duration has been specified and is exceeded.
118
- * @returns
119
- */
120
- async submitAndWaitForCustomization(
121
- sdk: ShapeDiverSdk,
122
- sessionId: string,
123
- body: ShapeDiverRequestCustomization,
124
- maxWaitMsec = -1,
125
- ): Promise<ShapeDiverResponseDto> {
126
- const startMsec = Date.now();
127
- const dto = await sendRequest(async () =>
128
- sdk.output.customize(sessionId, body),
129
- );
130
- const waitMsec = Date.now() - startMsec;
131
-
132
- // Reduce the total max waiting time by the amount the customization-request took
133
- maxWaitMsec =
134
- maxWaitMsec < 0 ? maxWaitMsec : Math.max(0, maxWaitMsec - waitMsec);
135
-
136
- return ShapeDiverUtilsApi.waitForCustomizationResult(
137
- sdk,
138
- sessionId,
139
- dto,
140
- maxWaitMsec,
141
- );
142
- }
143
-
144
- /**
145
- * Submit an export request and wait for the result to be finished.
146
- *
147
- * @param sdk
148
- * @param sessionId
149
- * @param body
150
- * @param maxWaitMsec - Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
151
- * @throws {@link ShapeDiverError} in case a maximum duration has been specified and is exceeded.
152
- * @returns
153
- */
154
- async submitAndWaitForExport(
155
- sdk: ShapeDiverSdk,
156
- sessionId: string,
157
- body: ShapeDiverRequestExport,
158
- maxWaitMsec = -1,
159
- ): Promise<ShapeDiverResponseDto> {
160
- const startMsec = Date.now();
161
- const dto = await sendRequest(async () =>
162
- sdk.export.compute(sessionId, body),
163
- );
164
- const waitMsec = Date.now() - startMsec;
165
-
166
- // Reduce the total max waiting time by the amount the compute-request took
167
- maxWaitMsec =
168
- maxWaitMsec < 0 ? maxWaitMsec : Math.max(0, maxWaitMsec - waitMsec);
169
-
170
- return ShapeDiverUtilsApi.waitForExportResult(
171
- sdk,
172
- sessionId,
173
- body,
174
- dto,
175
- maxWaitMsec,
176
- );
177
- }
178
-
179
- /**
180
- * Given a DTO resulting from a customization request, wait for the results to be finished.
181
- *
182
- * @param sdk
183
- * @param sessionId
184
- * @param dto
185
- * @param maxWaitMsec - Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
186
- * @returns
187
- */
188
- private static async waitForCustomizationResult(
189
- sdk: ShapeDiverSdk,
190
- sessionId: string,
191
- dto: ShapeDiverResponseDto,
192
- maxWaitMsec = -1,
193
- ): Promise<ShapeDiverResponseDto> {
194
- if (!dto.outputs) return dto;
195
-
196
- // Build new cache request
197
- const outputVersions: ShapeDiverRequestCache = {};
198
- Object.keys(dto.outputs).forEach(
199
- (id) =>
200
- (outputVersions[id] = (
201
- dto.outputs![id] as ShapeDiverResponseOutput
202
- ).version),
203
- );
204
-
205
- let delay = ShapeDiverUtilsApi.getMaxOutputDelay(dto);
206
- const startMsec = Date.now();
207
-
208
- while (delay > 0) {
209
- // Check whether maxWaitMsec has been reached
210
- if (maxWaitMsec >= 0) {
211
- const waitMsec = Date.now() - startMsec;
212
- if (waitMsec >= maxWaitMsec) {
213
- throw new ShapeDiverError(
214
- `Maximum wait time of ${maxWaitMsec} ms reached`,
215
- );
216
- }
217
- if (waitMsec + delay > maxWaitMsec) {
218
- delay = maxWaitMsec - waitMsec;
219
- }
220
- }
221
-
222
- await sleep(delay);
223
-
224
- // Send cache request
225
- dto = await sendRequest(async () =>
226
- sdk.output.getCache(sessionId, outputVersions),
227
- );
228
- delay = ShapeDiverUtilsApi.getMaxOutputDelay(dto);
229
- }
230
-
231
- return dto;
232
- }
233
-
234
- /**
235
- * Given a DTO resulting from an export request, wait for the result to be finished.
236
- *
237
- * @param sdk
238
- * @param sessionId
239
- * @param dto
240
- * @param exportIds
241
- * @param outputIds
242
- * @param maxWaitMsec - Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
243
- * @returns
244
- */
245
- private static async waitForExportResult(
246
- sdk: ShapeDiverSdk,
247
- sessionId: string,
248
- body: ShapeDiverRequestExport,
249
- dto: ShapeDiverResponseDto,
250
- maxWaitMsec = -1,
251
- ): Promise<ShapeDiverResponseDto> {
252
- let delay = ShapeDiverUtilsApi.getMaxExportDelay(dto, body);
253
- const startMsec = Date.now();
254
-
255
- while (delay > 0) {
256
- // Check whether maxWaitMsec has been reached
257
- if (maxWaitMsec >= 0) {
258
- const waitMsec = Date.now() - startMsec;
259
- if (waitMsec >= maxWaitMsec) {
260
- throw new ShapeDiverError(
261
- `Maximum wait time of ${maxWaitMsec} ms reached`,
262
- );
263
- }
264
- if (waitMsec + delay > maxWaitMsec) {
265
- delay = maxWaitMsec - waitMsec;
266
- }
267
- }
268
-
269
- await sleep(delay);
270
-
271
- // Send cache request
272
- dto = await sendRequest(async () => sdk.export.getCache(sessionId, body));
273
- delay = ShapeDiverUtilsApi.getMaxExportDelay(dto, body);
274
- }
275
-
276
- return dto;
277
- }
278
-
279
- /**
280
- * Get the maximum delay that was reported for output versions.
281
- *
282
- * @returns maximum delay, -1 in case no delay was reported
283
- */
284
- private static getMaxOutputDelay(dto: ShapeDiverResponseDto): number {
285
- return Math.max(
286
- ...Object.values(dto.outputs!)
287
- .map((output) => output as ShapeDiverResponseOutput)
288
- .map((output) => output.delay ?? -1),
289
- );
290
- }
291
-
292
- /**
293
- * Get the maximum delay that was reported for the exports. If outputs have been reported as
294
- * well, their delay time is included too.
295
- *
296
- * @returns delay, -1 in case no delay was reported
297
- */
298
- private static getMaxExportDelay(
299
- dto: ShapeDiverResponseDto,
300
- body: ShapeDiverRequestExport,
301
- ): number {
302
- const exports = Array.isArray(body.exports)
303
- ? body.exports
304
- : [body.exports.id];
305
- const outputs = body.outputs ?? [];
306
-
307
- return Math.max(
308
- ...Object.values(dto.exports ?? {})
309
- .filter((e) => exports.includes(e.id))
310
- .map((e) => e as ShapeDiverResponseExport)
311
- .map((e) => e.delay ?? -1),
312
- ...Object.values(dto.outputs ?? {})
313
- .filter((o) => outputs.includes(o.id))
314
- .map((o) => o as ShapeDiverResponseOutput)
315
- .map((o) => o.delay ?? -1),
316
- );
317
- }
318
- }
@@ -1,135 +0,0 @@
1
- import {
2
- ShapeDiverError,
3
- ShapeDiverErrorType,
4
- ShapeDiverRequestError as ShapeDiverRequestErrorCore,
5
- ShapeDiverResponseError as ShapeDiverResponseErrorCore,
6
- } from "@shapediver/sdk.geometry-api-sdk-core";
7
- import { ShapeDiverResponseError } from "../ShapeDiverErrors";
8
-
9
- /** Type guard for all error types of the Geometry Backend SDK package. */
10
- export function isGBError(
11
- e: any,
12
- ): e is ShapeDiverError & ShapeDiverRequestErrorCore & ShapeDiverResponseError {
13
- return (
14
- e instanceof Error &&
15
- "errorType" in e &&
16
- Object.values(ShapeDiverErrorType).includes(e.errorType as any)
17
- );
18
- }
19
-
20
- /** Type guard for a Geometry Backend SDK generic error. */
21
- export function isGBGenericError(e: any): e is ShapeDiverError {
22
- return (
23
- e instanceof Error &&
24
- "errorType" in e &&
25
- e.errorType === ShapeDiverErrorType.Generic
26
- );
27
- }
28
-
29
- /** Type guard for a Geometry Backend SDK request error. */
30
- export function isGBRequestError(e: any): e is ShapeDiverRequestErrorCore {
31
- return (
32
- e instanceof Error &&
33
- "errorType" in e &&
34
- e.errorType === ShapeDiverErrorType.Request
35
- );
36
- }
37
-
38
- /** Type guard for a Geometry Backend SDK response error. */
39
- export function isGBResponseError(e: any): e is ShapeDiverResponseError {
40
- return (
41
- e instanceof Error &&
42
- "errorType" in e &&
43
- e.errorType === ShapeDiverErrorType.Response
44
- );
45
- }
46
-
47
- /**
48
- * Sends the given request, handles retries for the HTTP status 429, and maps
49
- * the response error object to its typed representation.
50
- */
51
- export async function sendRequest<T>(call: () => Promise<T>): Promise<T> {
52
- const retryLimit = 5;
53
- let retryCounter = 0;
54
-
55
- while (retryCounter < retryLimit) {
56
- try {
57
- return await call();
58
- } catch (e) {
59
- retryCounter++;
60
-
61
- if (e instanceof ShapeDiverResponseErrorCore) {
62
- /* Check for special response statuses */
63
- if (e.status === 429) {
64
- // 429 Too Many Requests - Extract waiting time from Retry-After header if existing.
65
- const seconds = e.headers["retry-after"] ?? 60; // default is 1 minute
66
- await sleep(Number(seconds) * 1000);
67
- continue;
68
- } else if (e.status === 502) {
69
- await sleep(1000); // 1 second
70
- continue;
71
- }
72
-
73
- // Error mapping
74
- throw new ShapeDiverResponseError(e);
75
- } else {
76
- // Not a response error -> something went wrong
77
- throw e;
78
- }
79
- }
80
- }
81
-
82
- throw new ShapeDiverError("Could not send request: Retry-limit reached");
83
- }
84
-
85
- /** Delays the response for the given number of milliseconds */
86
- export function sleep(ms: number): Promise<void> {
87
- return new Promise((resolve) => setTimeout(resolve, ms));
88
- }
89
-
90
- /** Encode the given string to base64 */
91
- export function encodeBase64(str: string): string {
92
- try {
93
- return btoa(str);
94
- } catch (err) {
95
- return Buffer.from(str).toString("base64");
96
- }
97
- }
98
-
99
- /** Set content headers according to RFC 5987 */
100
- export function contentDispositionFromFilename(filename: string): string {
101
- const asciiName = filename.replace(/[^\x00-\x7F]/g, "");
102
- let header = `attachment; filename="${asciiName}"`;
103
-
104
- if (filename.length !== asciiName.length) {
105
- header += `; filename*=UTF-8''${encodeURIComponent(filename)}`;
106
- }
107
-
108
- return header;
109
- }
110
-
111
- /**
112
- * Extract and return the filename from a content-disposition HTTP header. Decodes the filename*
113
- * property if set.
114
- */
115
- export function filenameFromContentDisposition(
116
- contentDisposition: string,
117
- ): string | undefined {
118
- let filename, filenameStar;
119
-
120
- // Search for filename
121
- const match = contentDisposition.match(/filename="([^"]+)"/);
122
- if (match) filename = match[1];
123
-
124
- // Search for filename*
125
- const matchStar = contentDisposition.match(/filename\*=([^\'\']+\'\')?(.+)/);
126
- if (matchStar) {
127
- const encoding = matchStar[1] ? matchStar[1] : "utf-8";
128
- const encodedName = matchStar[2] ?? matchStar[1];
129
- if (encoding.toLowerCase().startsWith("utf-8")) {
130
- filenameStar = decodeURIComponent(encodedName);
131
- }
132
- }
133
-
134
- return filenameStar ?? filename;
135
- }