@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 CHANGED
@@ -37,6 +37,12 @@ interface RegisterParams {
37
37
  firstName?: string;
38
38
  lastName?: string;
39
39
  displayName?: string;
40
+ /**
41
+ * Registration invite, for a project that only accepts registrations by
42
+ * invitation. The app's registration page usually receives it in the URL
43
+ * fragment of the invitation link (`#registrationToken=…`).
44
+ */
45
+ registrationToken?: string;
40
46
  }
41
47
  interface LoginResponse {
42
48
  user: {
@@ -457,6 +463,12 @@ interface UploadFileParams {
457
463
  mimeType: string;
458
464
  visibility?: FileVisibility;
459
465
  description?: string;
466
+ /**
467
+ * Stops the upload. The call then rejects with an error whose `name` is
468
+ * 'AbortError'. See the SDK guide ("Stopping an upload") for what is left
469
+ * on the server.
470
+ */
471
+ signal?: AbortSignal;
460
472
  }
461
473
  interface UploadLargeFileParams extends UploadFileParams {
462
474
  /** Concurrent part uploads (default: 3) */
@@ -616,10 +628,15 @@ declare class HttpClient {
616
628
  constructor(config: SpacelrClientConfig, tokenManager: TokenManager);
617
629
  request<T>(options: HttpRequestOptions): Promise<T>;
618
630
  private requestWithRetry;
631
+ /**
632
+ * Posts a form (file upload). `signal` lets the caller stop it: the request
633
+ * is aborted and the call rejects with the `AbortError` a browser throws,
634
+ * never with a timeout or network error.
635
+ */
619
636
  uploadForm<T>(path: string, formData: FormData, onProgress?: (event: {
620
637
  loaded: number;
621
638
  total: number;
622
- }) => void): Promise<T>;
639
+ }) => void, signal?: AbortSignal): Promise<T>;
623
640
  private uploadFormWithRetry;
624
641
  /**
625
642
  * Shared handler for a 401 on an authenticated request. Returns true if the
@@ -1518,11 +1535,22 @@ declare class StorageModule {
1518
1535
  /**
1519
1536
  * Upload a file through the gateway (no direct S3 access).
1520
1537
  * Accepts a Blob/File (browser) or ArrayBuffer/Uint8Array (Node).
1538
+ *
1539
+ * `params.signal` stops the upload (#1138); the call rejects with an
1540
+ * AbortError. The server stores the file only once it has all of it, so a
1541
+ * stopped upload leaves nothing, unless the abort came after the last byte
1542
+ * was sent: then the file may exist and this call cannot name it.
1521
1543
  */
1522
1544
  uploadFile(file: Blob | ArrayBuffer | Uint8Array, params: UploadFileParams, onProgress?: (progress: UploadProgress) => void): Promise<FileInfo>;
1523
1545
  /**
1524
1546
  * Upload a large file using multipart upload through the gateway.
1525
1547
  * Splits into parts, uploads concurrently, and completes.
1548
+ *
1549
+ * `params.signal` stops the upload (#1138): the parts on their way are
1550
+ * aborted, no further part starts, the upload is removed on the server and
1551
+ * the call rejects with an AbortError. The requests that open and that
1552
+ * complete the upload are not cut off, since a cut-off one would leave a
1553
+ * state this call cannot clean up; the signal is checked right after each.
1526
1554
  */
1527
1555
  uploadLargeFile(file: Blob | ArrayBuffer | Uint8Array, params: UploadLargeFileParams, onProgress?: (progress: UploadProgress) => void): Promise<FileInfo>;
1528
1556
  initMultipartUpload(params: InitMultipartUploadParams): Promise<InitMultipartUploadResponse>;
package/dist/index.d.ts CHANGED
@@ -37,6 +37,12 @@ interface RegisterParams {
37
37
  firstName?: string;
38
38
  lastName?: string;
39
39
  displayName?: string;
40
+ /**
41
+ * Registration invite, for a project that only accepts registrations by
42
+ * invitation. The app's registration page usually receives it in the URL
43
+ * fragment of the invitation link (`#registrationToken=…`).
44
+ */
45
+ registrationToken?: string;
40
46
  }
41
47
  interface LoginResponse {
42
48
  user: {
@@ -457,6 +463,12 @@ interface UploadFileParams {
457
463
  mimeType: string;
458
464
  visibility?: FileVisibility;
459
465
  description?: string;
466
+ /**
467
+ * Stops the upload. The call then rejects with an error whose `name` is
468
+ * 'AbortError'. See the SDK guide ("Stopping an upload") for what is left
469
+ * on the server.
470
+ */
471
+ signal?: AbortSignal;
460
472
  }
461
473
  interface UploadLargeFileParams extends UploadFileParams {
462
474
  /** Concurrent part uploads (default: 3) */
@@ -616,10 +628,15 @@ declare class HttpClient {
616
628
  constructor(config: SpacelrClientConfig, tokenManager: TokenManager);
617
629
  request<T>(options: HttpRequestOptions): Promise<T>;
618
630
  private requestWithRetry;
631
+ /**
632
+ * Posts a form (file upload). `signal` lets the caller stop it: the request
633
+ * is aborted and the call rejects with the `AbortError` a browser throws,
634
+ * never with a timeout or network error.
635
+ */
619
636
  uploadForm<T>(path: string, formData: FormData, onProgress?: (event: {
620
637
  loaded: number;
621
638
  total: number;
622
- }) => void): Promise<T>;
639
+ }) => void, signal?: AbortSignal): Promise<T>;
623
640
  private uploadFormWithRetry;
624
641
  /**
625
642
  * Shared handler for a 401 on an authenticated request. Returns true if the
@@ -1518,11 +1535,22 @@ declare class StorageModule {
1518
1535
  /**
1519
1536
  * Upload a file through the gateway (no direct S3 access).
1520
1537
  * Accepts a Blob/File (browser) or ArrayBuffer/Uint8Array (Node).
1538
+ *
1539
+ * `params.signal` stops the upload (#1138); the call rejects with an
1540
+ * AbortError. The server stores the file only once it has all of it, so a
1541
+ * stopped upload leaves nothing, unless the abort came after the last byte
1542
+ * was sent: then the file may exist and this call cannot name it.
1521
1543
  */
1522
1544
  uploadFile(file: Blob | ArrayBuffer | Uint8Array, params: UploadFileParams, onProgress?: (progress: UploadProgress) => void): Promise<FileInfo>;
1523
1545
  /**
1524
1546
  * Upload a large file using multipart upload through the gateway.
1525
1547
  * Splits into parts, uploads concurrently, and completes.
1548
+ *
1549
+ * `params.signal` stops the upload (#1138): the parts on their way are
1550
+ * aborted, no further part starts, the upload is removed on the server and
1551
+ * the call rejects with an AbortError. The requests that open and that
1552
+ * complete the upload are not cut off, since a cut-off one would leave a
1553
+ * state this call cannot clean up; the signal is checked right after each.
1526
1554
  */
1527
1555
  uploadLargeFile(file: Blob | ArrayBuffer | Uint8Array, params: UploadLargeFileParams, onProgress?: (progress: UploadProgress) => void): Promise<FileInfo>;
1528
1556
  initMultipartUpload(params: InitMultipartUploadParams): Promise<InitMultipartUploadResponse>;
package/dist/index.js CHANGED
@@ -138,6 +138,9 @@ function isStaleSessionBody(body) {
138
138
  const flat = body;
139
139
  return flat["code"] === "STALE_SESSION" || flat["message"] === "stale_session";
140
140
  }
141
+ function abortError() {
142
+ return new DOMException("The upload was aborted", "AbortError");
143
+ }
141
144
  var HttpClient = class {
142
145
  constructor(config, tokenManager) {
143
146
  this.config = config;
@@ -211,22 +214,28 @@ var HttpClient = class {
211
214
  }
212
215
  }
213
216
  }
214
- async uploadForm(path, formData, onProgress) {
215
- return this.uploadFormWithRetry(path, formData, onProgress, false);
217
+ /**
218
+ * Posts a form (file upload). `signal` lets the caller stop it: the request
219
+ * is aborted and the call rejects with the `AbortError` a browser throws,
220
+ * never with a timeout or network error.
221
+ */
222
+ async uploadForm(path, formData, onProgress, signal) {
223
+ return this.uploadFormWithRetry(path, formData, onProgress, signal, false);
216
224
  }
217
- async uploadFormWithRetry(path, formData, onProgress, isRetry) {
225
+ async uploadFormWithRetry(path, formData, onProgress, signal, isRetry) {
226
+ if (signal?.aborted) throw abortError();
218
227
  const url = this.buildUrl(path);
219
228
  const headers = await this.buildFormHeaders();
220
229
  const timeoutMs = this.config.timeout ?? 3e4;
221
230
  try {
222
231
  if (onProgress) {
223
- return await this.uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs);
232
+ return await this.uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs, signal);
224
233
  }
225
- return await this.uploadFormOnce(url, headers, formData, timeoutMs);
234
+ return await this.uploadFormOnce(url, headers, formData, timeoutMs, signal);
226
235
  } catch (error) {
227
236
  if (error instanceof SpacelrAuthError && error.statusCode === 401) {
228
237
  if (await this.recoverFromAuthFailure(isRetry)) {
229
- return this.uploadFormWithRetry(path, formData, onProgress, true);
238
+ return this.uploadFormWithRetry(path, formData, onProgress, signal, true);
230
239
  }
231
240
  }
232
241
  throw error;
@@ -260,9 +269,20 @@ var HttpClient = class {
260
269
  await this.tokenManager.notifyAuthLost("unauthorized");
261
270
  return false;
262
271
  }
263
- async uploadFormOnce(url, headers, formData, timeoutMs) {
272
+ async uploadFormOnce(url, headers, formData, timeoutMs, signal) {
264
273
  const controller = new AbortController();
265
- const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
274
+ let timeoutFired = false;
275
+ const timeoutId = setTimeout(() => {
276
+ timeoutFired = true;
277
+ controller.abort();
278
+ }, timeoutMs);
279
+ let callerAborted = false;
280
+ const onCallerAbort = () => {
281
+ callerAborted = true;
282
+ controller.abort();
283
+ };
284
+ if (signal?.aborted) onCallerAbort();
285
+ else signal?.addEventListener("abort", onCallerAbort);
266
286
  try {
267
287
  const response = await fetch(url, {
268
288
  method: "POST",
@@ -278,6 +298,7 @@ var HttpClient = class {
278
298
  } catch (error) {
279
299
  if (error instanceof SpacelrError) throw error;
280
300
  if (error instanceof DOMException && error.name === "AbortError") {
301
+ if (callerAborted && !timeoutFired) throw abortError();
281
302
  throw new SpacelrTimeoutError(timeoutMs);
282
303
  }
283
304
  throw new SpacelrNetworkError(
@@ -285,11 +306,29 @@ var HttpClient = class {
285
306
  );
286
307
  } finally {
287
308
  clearTimeout(timeoutId);
309
+ signal?.removeEventListener("abort", onCallerAbort);
288
310
  }
289
311
  }
290
- uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs) {
291
- return new Promise((resolve, reject) => {
312
+ uploadFormWithProgress(url, headers, formData, onProgress, timeoutMs, signal) {
313
+ return new Promise((resolvePromise, rejectPromise) => {
292
314
  const xhr = new XMLHttpRequest();
315
+ const onCallerAbort = () => {
316
+ xhr.abort();
317
+ reject(abortError());
318
+ };
319
+ const resolve = (value) => {
320
+ signal?.removeEventListener("abort", onCallerAbort);
321
+ resolvePromise(value);
322
+ };
323
+ const reject = (reason) => {
324
+ signal?.removeEventListener("abort", onCallerAbort);
325
+ rejectPromise(reason);
326
+ };
327
+ if (signal?.aborted) {
328
+ reject(abortError());
329
+ return;
330
+ }
331
+ signal?.addEventListener("abort", onCallerAbort);
293
332
  xhr.open("POST", url);
294
333
  xhr.timeout = timeoutMs;
295
334
  for (const [key, value] of Object.entries(headers)) {
@@ -3000,17 +3039,35 @@ var AuthModule = class _AuthModule {
3000
3039
  // libs/sdk/src/modules/storage.module.ts
3001
3040
  var THROTTLE_RETRY_MS = 6e4;
3002
3041
  var THROTTLE_MAX_RETRIES = 2;
3003
- async function retryThrottled(call) {
3042
+ async function retryThrottled(call, signal, detached = false) {
3004
3043
  for (let attempt = 0; ; attempt++) {
3005
3044
  try {
3006
3045
  return await call();
3007
3046
  } catch (error) {
3008
3047
  const throttled = error instanceof SpacelrError && error.statusCode === 429;
3009
3048
  if (!throttled || attempt >= THROTTLE_MAX_RETRIES) throw error;
3010
- await new Promise((resolve) => setTimeout(resolve, THROTTLE_RETRY_MS));
3049
+ await waitOrAbort(THROTTLE_RETRY_MS, signal, detached);
3011
3050
  }
3012
3051
  }
3013
3052
  }
3053
+ function waitOrAbort(ms, signal, detached = false) {
3054
+ return new Promise((resolve, reject) => {
3055
+ if (signal?.aborted) {
3056
+ reject(abortError());
3057
+ return;
3058
+ }
3059
+ const onAbort = () => {
3060
+ clearTimeout(timer);
3061
+ reject(abortError());
3062
+ };
3063
+ const timer = setTimeout(() => {
3064
+ signal?.removeEventListener("abort", onAbort);
3065
+ resolve();
3066
+ }, ms);
3067
+ if (detached && typeof timer === "object") timer.unref();
3068
+ signal?.addEventListener("abort", onAbort);
3069
+ });
3070
+ }
3014
3071
  var StorageModule = class {
3015
3072
  constructor(http, tokenManager, config) {
3016
3073
  this.http = http;
@@ -3020,6 +3077,11 @@ var StorageModule = class {
3020
3077
  /**
3021
3078
  * Upload a file through the gateway (no direct S3 access).
3022
3079
  * Accepts a Blob/File (browser) or ArrayBuffer/Uint8Array (Node).
3080
+ *
3081
+ * `params.signal` stops the upload (#1138); the call rejects with an
3082
+ * AbortError. The server stores the file only once it has all of it, so a
3083
+ * stopped upload leaves nothing, unless the abort came after the last byte
3084
+ * was sent: then the file may exist and this call cannot name it.
3023
3085
  */
3024
3086
  async uploadFile(file, params, onProgress) {
3025
3087
  const formData = new FormData();
@@ -3034,15 +3096,23 @@ var StorageModule = class {
3034
3096
  percentage: e.total > 0 ? Math.round(e.loaded / e.total * 100) : 0
3035
3097
  });
3036
3098
  } : void 0;
3037
- return this.http.uploadForm("/storage/files", formData, progressHandler);
3099
+ return this.http.uploadForm("/storage/files", formData, progressHandler, params.signal);
3038
3100
  }
3039
3101
  /**
3040
3102
  * Upload a large file using multipart upload through the gateway.
3041
3103
  * Splits into parts, uploads concurrently, and completes.
3104
+ *
3105
+ * `params.signal` stops the upload (#1138): the parts on their way are
3106
+ * aborted, no further part starts, the upload is removed on the server and
3107
+ * the call rejects with an AbortError. The requests that open and that
3108
+ * complete the upload are not cut off, since a cut-off one would leave a
3109
+ * state this call cannot clean up; the signal is checked right after each.
3042
3110
  */
3043
3111
  async uploadLargeFile(file, params, onProgress) {
3044
3112
  const fileSize = file instanceof Blob ? file.size : file.byteLength;
3045
3113
  const concurrency = params.concurrency ?? 3;
3114
+ const { signal } = params;
3115
+ if (signal?.aborted) throw abortError();
3046
3116
  const init = await this.initMultipartUpload({
3047
3117
  filename: params.filename,
3048
3118
  mimeType: params.mimeType,
@@ -3054,6 +3124,7 @@ var StorageModule = class {
3054
3124
  const completedParts = [];
3055
3125
  let completedBytes = 0;
3056
3126
  const partProgressMap = /* @__PURE__ */ new Map();
3127
+ let completed;
3057
3128
  try {
3058
3129
  const allPartNumbers = Array.from(
3059
3130
  { length: totalParts },
@@ -3084,8 +3155,10 @@ var StorageModule = class {
3084
3155
  () => this.http.uploadForm(
3085
3156
  `/storage/files/${fileId}/multipart/upload-part`,
3086
3157
  formData,
3087
- partProgress
3088
- )
3158
+ partProgress,
3159
+ signal
3160
+ ),
3161
+ signal
3089
3162
  );
3090
3163
  partProgressMap.delete(partNumber);
3091
3164
  completedBytes += chunkSize;
@@ -3105,14 +3178,29 @@ var StorageModule = class {
3105
3178
  partProgressMap.clear();
3106
3179
  }
3107
3180
  completedParts.sort((a, b) => a.partNumber - b.partNumber);
3108
- return await retryThrottled(() => this.completeMultipartUpload(fileId, completedParts));
3181
+ if (signal?.aborted) throw abortError();
3182
+ completed = await retryThrottled(() => this.completeMultipartUpload(fileId, completedParts), signal);
3109
3183
  } catch (error) {
3184
+ const cleanUp = () => this.abortMultipartUpload(fileId);
3110
3185
  try {
3111
- await retryThrottled(() => this.abortMultipartUpload(fileId));
3186
+ if (signal?.aborted) {
3187
+ await cleanUp().catch((cleanUpError) => {
3188
+ if (cleanUpError instanceof SpacelrError && cleanUpError.statusCode === 429) {
3189
+ void retryThrottled(cleanUp, void 0, true).catch(() => void 0);
3190
+ }
3191
+ });
3192
+ } else {
3193
+ await retryThrottled(cleanUp);
3194
+ }
3112
3195
  } catch {
3113
3196
  }
3114
3197
  throw error;
3115
3198
  }
3199
+ if (signal?.aborted) {
3200
+ await this.deleteFile(fileId).catch(() => void 0);
3201
+ throw abortError();
3202
+ }
3203
+ return completed;
3116
3204
  }
3117
3205
  async initMultipartUpload(params) {
3118
3206
  return this.http.request({