@spacelr/sdk 0.19.7 → 0.19.9
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/index.d.mts +29 -1
- package/dist/index.d.ts +29 -1
- package/dist/index.js +105 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +105 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -91,6 +91,9 @@ function isStaleSessionBody(body) {
|
|
|
91
91
|
const flat = body;
|
|
92
92
|
return flat["code"] === "STALE_SESSION" || flat["message"] === "stale_session";
|
|
93
93
|
}
|
|
94
|
+
function abortError() {
|
|
95
|
+
return new DOMException("The upload was aborted", "AbortError");
|
|
96
|
+
}
|
|
94
97
|
var HttpClient = class {
|
|
95
98
|
constructor(config, tokenManager) {
|
|
96
99
|
this.config = config;
|
|
@@ -164,22 +167,28 @@ var HttpClient = class {
|
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
169
|
}
|
|
167
|
-
|
|
168
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Posts a form (file upload). `signal` lets the caller stop it: the request
|
|
172
|
+
* is aborted and the call rejects with the `AbortError` a browser throws,
|
|
173
|
+
* never with a timeout or network error.
|
|
174
|
+
*/
|
|
175
|
+
async uploadForm(path, formData, onProgress, signal) {
|
|
176
|
+
return this.uploadFormWithRetry(path, formData, onProgress, signal, false);
|
|
169
177
|
}
|
|
170
|
-
async uploadFormWithRetry(path, formData, onProgress, isRetry) {
|
|
178
|
+
async uploadFormWithRetry(path, formData, onProgress, signal, isRetry) {
|
|
179
|
+
if (signal?.aborted) throw abortError();
|
|
171
180
|
const url = this.buildUrl(path);
|
|
172
181
|
const headers = await this.buildFormHeaders();
|
|
173
182
|
const timeoutMs = this.config.timeout ?? 3e4;
|
|
174
183
|
try {
|
|
175
184
|
if (onProgress) {
|
|
176
|
-
return await this.uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs);
|
|
185
|
+
return await this.uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs, signal);
|
|
177
186
|
}
|
|
178
|
-
return await this.uploadFormOnce(url, headers, formData, timeoutMs);
|
|
187
|
+
return await this.uploadFormOnce(url, headers, formData, timeoutMs, signal);
|
|
179
188
|
} catch (error) {
|
|
180
189
|
if (error instanceof SpacelrAuthError && error.statusCode === 401) {
|
|
181
190
|
if (await this.recoverFromAuthFailure(isRetry)) {
|
|
182
|
-
return this.uploadFormWithRetry(path, formData, onProgress, true);
|
|
191
|
+
return this.uploadFormWithRetry(path, formData, onProgress, signal, true);
|
|
183
192
|
}
|
|
184
193
|
}
|
|
185
194
|
throw error;
|
|
@@ -213,9 +222,20 @@ var HttpClient = class {
|
|
|
213
222
|
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
214
223
|
return false;
|
|
215
224
|
}
|
|
216
|
-
async uploadFormOnce(url, headers, formData, timeoutMs) {
|
|
225
|
+
async uploadFormOnce(url, headers, formData, timeoutMs, signal) {
|
|
217
226
|
const controller = new AbortController();
|
|
218
|
-
|
|
227
|
+
let timeoutFired = false;
|
|
228
|
+
const timeoutId = setTimeout(() => {
|
|
229
|
+
timeoutFired = true;
|
|
230
|
+
controller.abort();
|
|
231
|
+
}, timeoutMs);
|
|
232
|
+
let callerAborted = false;
|
|
233
|
+
const onCallerAbort = () => {
|
|
234
|
+
callerAborted = true;
|
|
235
|
+
controller.abort();
|
|
236
|
+
};
|
|
237
|
+
if (signal?.aborted) onCallerAbort();
|
|
238
|
+
else signal?.addEventListener("abort", onCallerAbort);
|
|
219
239
|
try {
|
|
220
240
|
const response = await fetch(url, {
|
|
221
241
|
method: "POST",
|
|
@@ -231,6 +251,7 @@ var HttpClient = class {
|
|
|
231
251
|
} catch (error) {
|
|
232
252
|
if (error instanceof SpacelrError) throw error;
|
|
233
253
|
if (error instanceof DOMException && error.name === "AbortError") {
|
|
254
|
+
if (callerAborted && !timeoutFired) throw abortError();
|
|
234
255
|
throw new SpacelrTimeoutError(timeoutMs);
|
|
235
256
|
}
|
|
236
257
|
throw new SpacelrNetworkError(
|
|
@@ -238,11 +259,29 @@ var HttpClient = class {
|
|
|
238
259
|
);
|
|
239
260
|
} finally {
|
|
240
261
|
clearTimeout(timeoutId);
|
|
262
|
+
signal?.removeEventListener("abort", onCallerAbort);
|
|
241
263
|
}
|
|
242
264
|
}
|
|
243
|
-
uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs) {
|
|
244
|
-
return new Promise((
|
|
265
|
+
uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs, signal) {
|
|
266
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
245
267
|
const xhr = new XMLHttpRequest();
|
|
268
|
+
const onCallerAbort = () => {
|
|
269
|
+
xhr.abort();
|
|
270
|
+
reject(abortError());
|
|
271
|
+
};
|
|
272
|
+
const resolve = (value) => {
|
|
273
|
+
signal?.removeEventListener("abort", onCallerAbort);
|
|
274
|
+
resolvePromise(value);
|
|
275
|
+
};
|
|
276
|
+
const reject = (reason) => {
|
|
277
|
+
signal?.removeEventListener("abort", onCallerAbort);
|
|
278
|
+
rejectPromise(reason);
|
|
279
|
+
};
|
|
280
|
+
if (signal?.aborted) {
|
|
281
|
+
reject(abortError());
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
signal?.addEventListener("abort", onCallerAbort);
|
|
246
285
|
xhr.open("POST", url);
|
|
247
286
|
xhr.timeout = timeoutMs;
|
|
248
287
|
for (const [key, value] of Object.entries(headers)) {
|
|
@@ -2953,17 +2992,35 @@ var AuthModule = class _AuthModule {
|
|
|
2953
2992
|
// libs/sdk/src/modules/storage.module.ts
|
|
2954
2993
|
var THROTTLE_RETRY_MS = 6e4;
|
|
2955
2994
|
var THROTTLE_MAX_RETRIES = 2;
|
|
2956
|
-
async function retryThrottled(call) {
|
|
2995
|
+
async function retryThrottled(call, signal, detached = false) {
|
|
2957
2996
|
for (let attempt = 0; ; attempt++) {
|
|
2958
2997
|
try {
|
|
2959
2998
|
return await call();
|
|
2960
2999
|
} catch (error) {
|
|
2961
3000
|
const throttled = error instanceof SpacelrError && error.statusCode === 429;
|
|
2962
3001
|
if (!throttled || attempt >= THROTTLE_MAX_RETRIES) throw error;
|
|
2963
|
-
await
|
|
3002
|
+
await waitOrAbort(THROTTLE_RETRY_MS, signal, detached);
|
|
2964
3003
|
}
|
|
2965
3004
|
}
|
|
2966
3005
|
}
|
|
3006
|
+
function waitOrAbort(ms, signal, detached = false) {
|
|
3007
|
+
return new Promise((resolve, reject) => {
|
|
3008
|
+
if (signal?.aborted) {
|
|
3009
|
+
reject(abortError());
|
|
3010
|
+
return;
|
|
3011
|
+
}
|
|
3012
|
+
const onAbort = () => {
|
|
3013
|
+
clearTimeout(timer);
|
|
3014
|
+
reject(abortError());
|
|
3015
|
+
};
|
|
3016
|
+
const timer = setTimeout(() => {
|
|
3017
|
+
signal?.removeEventListener("abort", onAbort);
|
|
3018
|
+
resolve();
|
|
3019
|
+
}, ms);
|
|
3020
|
+
if (detached && typeof timer === "object") timer.unref();
|
|
3021
|
+
signal?.addEventListener("abort", onAbort);
|
|
3022
|
+
});
|
|
3023
|
+
}
|
|
2967
3024
|
var StorageModule = class {
|
|
2968
3025
|
constructor(http, tokenManager, config) {
|
|
2969
3026
|
this.http = http;
|
|
@@ -2973,6 +3030,11 @@ var StorageModule = class {
|
|
|
2973
3030
|
/**
|
|
2974
3031
|
* Upload a file through the gateway (no direct S3 access).
|
|
2975
3032
|
* Accepts a Blob/File (browser) or ArrayBuffer/Uint8Array (Node).
|
|
3033
|
+
*
|
|
3034
|
+
* `params.signal` stops the upload (#1138); the call rejects with an
|
|
3035
|
+
* AbortError. The server stores the file only once it has all of it, so a
|
|
3036
|
+
* stopped upload leaves nothing, unless the abort came after the last byte
|
|
3037
|
+
* was sent: then the file may exist and this call cannot name it.
|
|
2976
3038
|
*/
|
|
2977
3039
|
async uploadFile(file, params, onProgress) {
|
|
2978
3040
|
const formData = new FormData();
|
|
@@ -2987,15 +3049,23 @@ var StorageModule = class {
|
|
|
2987
3049
|
percentage: e.total > 0 ? Math.round(e.loaded / e.total * 100) : 0
|
|
2988
3050
|
});
|
|
2989
3051
|
} : void 0;
|
|
2990
|
-
return this.http.uploadForm("/storage/files", formData, progressHandler);
|
|
3052
|
+
return this.http.uploadForm("/storage/files", formData, progressHandler, params.signal);
|
|
2991
3053
|
}
|
|
2992
3054
|
/**
|
|
2993
3055
|
* Upload a large file using multipart upload through the gateway.
|
|
2994
3056
|
* Splits into parts, uploads concurrently, and completes.
|
|
3057
|
+
*
|
|
3058
|
+
* `params.signal` stops the upload (#1138): the parts on their way are
|
|
3059
|
+
* aborted, no further part starts, the upload is removed on the server and
|
|
3060
|
+
* the call rejects with an AbortError. The requests that open and that
|
|
3061
|
+
* complete the upload are not cut off, since a cut-off one would leave a
|
|
3062
|
+
* state this call cannot clean up; the signal is checked right after each.
|
|
2995
3063
|
*/
|
|
2996
3064
|
async uploadLargeFile(file, params, onProgress) {
|
|
2997
3065
|
const fileSize = file instanceof Blob ? file.size : file.byteLength;
|
|
2998
3066
|
const concurrency = params.concurrency ?? 3;
|
|
3067
|
+
const { signal } = params;
|
|
3068
|
+
if (signal?.aborted) throw abortError();
|
|
2999
3069
|
const init = await this.initMultipartUpload({
|
|
3000
3070
|
filename: params.filename,
|
|
3001
3071
|
mimeType: params.mimeType,
|
|
@@ -3007,6 +3077,7 @@ var StorageModule = class {
|
|
|
3007
3077
|
const completedParts = [];
|
|
3008
3078
|
let completedBytes = 0;
|
|
3009
3079
|
const partProgressMap = /* @__PURE__ */ new Map();
|
|
3080
|
+
let completed;
|
|
3010
3081
|
try {
|
|
3011
3082
|
const allPartNumbers = Array.from(
|
|
3012
3083
|
{ length: totalParts },
|
|
@@ -3037,8 +3108,10 @@ var StorageModule = class {
|
|
|
3037
3108
|
() => this.http.uploadForm(
|
|
3038
3109
|
`/storage/files/${fileId}/multipart/upload-part`,
|
|
3039
3110
|
formData,
|
|
3040
|
-
partProgress
|
|
3041
|
-
|
|
3111
|
+
partProgress,
|
|
3112
|
+
signal
|
|
3113
|
+
),
|
|
3114
|
+
signal
|
|
3042
3115
|
);
|
|
3043
3116
|
partProgressMap.delete(partNumber);
|
|
3044
3117
|
completedBytes += chunkSize;
|
|
@@ -3058,14 +3131,29 @@ var StorageModule = class {
|
|
|
3058
3131
|
partProgressMap.clear();
|
|
3059
3132
|
}
|
|
3060
3133
|
completedParts.sort((a, b) => a.partNumber - b.partNumber);
|
|
3061
|
-
|
|
3134
|
+
if (signal?.aborted) throw abortError();
|
|
3135
|
+
completed = await retryThrottled(() => this.completeMultipartUpload(fileId, completedParts), signal);
|
|
3062
3136
|
} catch (error) {
|
|
3137
|
+
const cleanUp = () => this.abortMultipartUpload(fileId);
|
|
3063
3138
|
try {
|
|
3064
|
-
|
|
3139
|
+
if (signal?.aborted) {
|
|
3140
|
+
await cleanUp().catch((cleanUpError) => {
|
|
3141
|
+
if (cleanUpError instanceof SpacelrError && cleanUpError.statusCode === 429) {
|
|
3142
|
+
void retryThrottled(cleanUp, void 0, true).catch(() => void 0);
|
|
3143
|
+
}
|
|
3144
|
+
});
|
|
3145
|
+
} else {
|
|
3146
|
+
await retryThrottled(cleanUp);
|
|
3147
|
+
}
|
|
3065
3148
|
} catch {
|
|
3066
3149
|
}
|
|
3067
3150
|
throw error;
|
|
3068
3151
|
}
|
|
3152
|
+
if (signal?.aborted) {
|
|
3153
|
+
await this.deleteFile(fileId).catch(() => void 0);
|
|
3154
|
+
throw abortError();
|
|
3155
|
+
}
|
|
3156
|
+
return completed;
|
|
3069
3157
|
}
|
|
3070
3158
|
async initMultipartUpload(params) {
|
|
3071
3159
|
return this.http.request({
|