@koolbase/core 12.15.0 → 12.16.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.
@@ -20,6 +20,9 @@ export { KoolbaseRealtime } from './realtime.js';
20
20
  export { KoolbaseConnectivity } from './connectivity.js';
21
21
  export type { ConnectivityState } from './connectivity.js';
22
22
  export { KoolbaseStorage } from './storage.js';
23
+ export { KoolbaseUploadTask } from './storage-transfer.js';
24
+ export type { KoolbaseUploadProgress, KoolbaseUploadState } from './storage-transfer.js';
25
+ export type { KoolbaseFileInfo, KoolbaseFilePage, KoolbaseFileSort, ListFilesOptions } from './storage-files.js';
23
26
  export { KoolbaseAnalytics, disabledAnalytics } from './analytics.js';
24
27
  export { KoolbaseFiscal, KoolbaseFiscalError, decodeFiscalResponse, isFiscalized, isPending } from './fiscal.js';
25
28
  export type { FiscalIntentResult, FiscalStatus } from './fiscal.js';
package/dist/cjs/index.js CHANGED
@@ -19,7 +19,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
19
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.RestoreResult = exports.koolbaseSdkVersion = exports.getOrCreateDeviceId = exports.KoolbaseMessaging = exports.isPending = exports.isFiscalized = exports.decodeFiscalResponse = exports.KoolbaseFiscalError = exports.KoolbaseFiscal = exports.disabledAnalytics = exports.KoolbaseAnalytics = exports.KoolbaseStorage = exports.KoolbaseConnectivity = exports.KoolbaseRealtime = exports.KoolbaseFunctions = exports.KoolbaseFlags = exports.initialRecordState = exports.KoolbaseRecordController = exports.initialCollectionState = exports.collectionQueryKey = exports.KoolbaseCollectionController = exports.KoolbaseDatabase = exports.KoolbaseAuth = void 0;
22
+ exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.RestoreResult = exports.koolbaseSdkVersion = exports.getOrCreateDeviceId = exports.KoolbaseMessaging = exports.isPending = exports.isFiscalized = exports.decodeFiscalResponse = exports.KoolbaseFiscalError = exports.KoolbaseFiscal = exports.disabledAnalytics = exports.KoolbaseAnalytics = exports.KoolbaseUploadTask = exports.KoolbaseStorage = exports.KoolbaseConnectivity = exports.KoolbaseRealtime = exports.KoolbaseFunctions = exports.KoolbaseFlags = exports.initialRecordState = exports.KoolbaseRecordController = exports.initialCollectionState = exports.collectionQueryKey = exports.KoolbaseCollectionController = exports.KoolbaseDatabase = exports.KoolbaseAuth = void 0;
23
23
  __exportStar(require("./types.js"), exports);
24
24
  __exportStar(require("./errors.js"), exports);
25
25
  __exportStar(require("./network.js"), exports);
@@ -51,6 +51,8 @@ var connectivity_js_1 = require("./connectivity.js");
51
51
  Object.defineProperty(exports, "KoolbaseConnectivity", { enumerable: true, get: function () { return connectivity_js_1.KoolbaseConnectivity; } });
52
52
  var storage_js_1 = require("./storage.js");
53
53
  Object.defineProperty(exports, "KoolbaseStorage", { enumerable: true, get: function () { return storage_js_1.KoolbaseStorage; } });
54
+ var storage_transfer_js_1 = require("./storage-transfer.js");
55
+ Object.defineProperty(exports, "KoolbaseUploadTask", { enumerable: true, get: function () { return storage_transfer_js_1.KoolbaseUploadTask; } });
54
56
  var analytics_js_1 = require("./analytics.js");
55
57
  Object.defineProperty(exports, "KoolbaseAnalytics", { enumerable: true, get: function () { return analytics_js_1.KoolbaseAnalytics; } });
56
58
  Object.defineProperty(exports, "disabledAnalytics", { enumerable: true, get: function () { return analytics_js_1.disabledAnalytics; } });
@@ -187,6 +187,45 @@ export declare class KoolbaseStorageMetadataInvalidError extends KoolbaseStorage
187
187
  detail?: string;
188
188
  constructor(message?: string, detail?: string);
189
189
  }
190
+ /**
191
+ * An upload that was cancelled -- by `KoolbaseUploadTask.cancel()` or the
192
+ * upload's `signal` -- before the server began checking it. Nothing was
193
+ * stored. Code: `upload_cancelled`.
194
+ */
195
+ export declare class KoolbaseUploadCancelledError extends KoolbaseStorageError {
196
+ constructor(message?: string);
197
+ }
198
+ /**
199
+ * The confirm named an upload the server does not have: it expired, was
200
+ * confirmed already, or belongs to someone else. Upload the file again.
201
+ * Code: `upload_not_found`.
202
+ */
203
+ export declare class KoolbaseUploadNotFoundError extends KoolbaseStorageError {
204
+ constructor(message?: string);
205
+ }
206
+ /**
207
+ * The bucket is busy with other changes (a large move, many uploads at
208
+ * once). Nothing was changed; retry shortly. Code: `bucket_busy`.
209
+ */
210
+ export declare class KoolbaseStorageBusyError extends KoolbaseStorageError {
211
+ constructor(message?: string);
212
+ }
213
+ /**
214
+ * A listing's cursor was not one this listing issued -- made up, or from a
215
+ * listing with another sort or order. Start again from the first page.
216
+ * Code: `invalid_cursor`.
217
+ */
218
+ export declare class KoolbaseStorageInvalidCursorError extends KoolbaseStorageError {
219
+ constructor(message?: string);
220
+ }
221
+ /**
222
+ * The bucket's rules allow this only to someone signed in, and nobody is.
223
+ * Not a session that ended: the SDK's session is left alone. Code:
224
+ * `sign_in_required`.
225
+ */
226
+ export declare class KoolbaseStorageSignInRequiredError extends KoolbaseStorageError {
227
+ constructor(message?: string);
228
+ }
190
229
  /**
191
230
  * Maps a non-2xx storage-layer response to a typed
192
231
  * {@link KoolbaseStorageError}, preferring the server's stable `code` and
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.KoolbaseStorageMetadataInvalidError = exports.KoolbaseStorageMimeTypeError = exports.KoolbaseStorageFileTooLargeError = exports.KoolbaseStorageQuotaError = exports.KoolbaseCapBelowUsageError = exports.KoolbaseUploadExpiredError = exports.KoolbaseUploadURLFailedError = exports.KoolbaseStoragePermissionError = exports.KoolbaseStorageValidationError = exports.KoolbaseStorageNotFoundError = exports.KoolbaseStorageConflictError = exports.KoolbaseStorageProjectIdentityError = exports.KoolbaseStorageError = void 0;
3
+ exports.KoolbaseStorageSignInRequiredError = exports.KoolbaseStorageInvalidCursorError = exports.KoolbaseStorageBusyError = exports.KoolbaseUploadNotFoundError = exports.KoolbaseUploadCancelledError = exports.KoolbaseStorageMetadataInvalidError = exports.KoolbaseStorageMimeTypeError = exports.KoolbaseStorageFileTooLargeError = exports.KoolbaseStorageQuotaError = exports.KoolbaseCapBelowUsageError = exports.KoolbaseUploadExpiredError = exports.KoolbaseUploadURLFailedError = exports.KoolbaseStoragePermissionError = exports.KoolbaseStorageValidationError = exports.KoolbaseStorageNotFoundError = exports.KoolbaseStorageConflictError = exports.KoolbaseStorageProjectIdentityError = exports.KoolbaseStorageError = void 0;
4
4
  exports.koolbaseStorageError = koolbaseStorageError;
5
5
  exports.koolbaseStorageErrorFromResponse = koolbaseStorageErrorFromResponse;
6
6
  const errors_js_1 = require("./errors.js");
@@ -252,6 +252,70 @@ class KoolbaseStorageMetadataInvalidError extends KoolbaseStorageError {
252
252
  }
253
253
  }
254
254
  exports.KoolbaseStorageMetadataInvalidError = KoolbaseStorageMetadataInvalidError;
255
+ /**
256
+ * An upload that was cancelled -- by `KoolbaseUploadTask.cancel()` or the
257
+ * upload's `signal` -- before the server began checking it. Nothing was
258
+ * stored. Code: `upload_cancelled`.
259
+ */
260
+ class KoolbaseUploadCancelledError extends KoolbaseStorageError {
261
+ constructor(message) {
262
+ super(message ?? 'The upload was cancelled', 'upload_cancelled');
263
+ this.name = 'KoolbaseUploadCancelledError';
264
+ Object.setPrototypeOf(this, KoolbaseUploadCancelledError.prototype);
265
+ }
266
+ }
267
+ exports.KoolbaseUploadCancelledError = KoolbaseUploadCancelledError;
268
+ /**
269
+ * The confirm named an upload the server does not have: it expired, was
270
+ * confirmed already, or belongs to someone else. Upload the file again.
271
+ * Code: `upload_not_found`.
272
+ */
273
+ class KoolbaseUploadNotFoundError extends KoolbaseStorageError {
274
+ constructor(message) {
275
+ super(message ?? 'This upload was not found -- please upload the file again', 'upload_not_found');
276
+ this.name = 'KoolbaseUploadNotFoundError';
277
+ Object.setPrototypeOf(this, KoolbaseUploadNotFoundError.prototype);
278
+ }
279
+ }
280
+ exports.KoolbaseUploadNotFoundError = KoolbaseUploadNotFoundError;
281
+ /**
282
+ * The bucket is busy with other changes (a large move, many uploads at
283
+ * once). Nothing was changed; retry shortly. Code: `bucket_busy`.
284
+ */
285
+ class KoolbaseStorageBusyError extends KoolbaseStorageError {
286
+ constructor(message) {
287
+ super(message ?? 'The bucket is busy -- try again shortly', 'bucket_busy');
288
+ this.name = 'KoolbaseStorageBusyError';
289
+ Object.setPrototypeOf(this, KoolbaseStorageBusyError.prototype);
290
+ }
291
+ }
292
+ exports.KoolbaseStorageBusyError = KoolbaseStorageBusyError;
293
+ /**
294
+ * A listing's cursor was not one this listing issued -- made up, or from a
295
+ * listing with another sort or order. Start again from the first page.
296
+ * Code: `invalid_cursor`.
297
+ */
298
+ class KoolbaseStorageInvalidCursorError extends KoolbaseStorageError {
299
+ constructor(message) {
300
+ super(message ?? 'The cursor is not valid for this listing', 'invalid_cursor');
301
+ this.name = 'KoolbaseStorageInvalidCursorError';
302
+ Object.setPrototypeOf(this, KoolbaseStorageInvalidCursorError.prototype);
303
+ }
304
+ }
305
+ exports.KoolbaseStorageInvalidCursorError = KoolbaseStorageInvalidCursorError;
306
+ /**
307
+ * The bucket's rules allow this only to someone signed in, and nobody is.
308
+ * Not a session that ended: the SDK's session is left alone. Code:
309
+ * `sign_in_required`.
310
+ */
311
+ class KoolbaseStorageSignInRequiredError extends KoolbaseStorageError {
312
+ constructor(message) {
313
+ super(message ?? 'Sign in to do this', 'sign_in_required');
314
+ this.name = 'KoolbaseStorageSignInRequiredError';
315
+ Object.setPrototypeOf(this, KoolbaseStorageSignInRequiredError.prototype);
316
+ }
317
+ }
318
+ exports.KoolbaseStorageSignInRequiredError = KoolbaseStorageSignInRequiredError;
255
319
  /**
256
320
  * Maps a non-2xx storage-layer response to a typed
257
321
  * {@link KoolbaseStorageError}, preferring the server's stable `code` and
@@ -289,6 +353,16 @@ function koolbaseStorageError(status, body, fallbackMessage = 'Storage request f
289
353
  return new KoolbaseStorageMetadataInvalidError(message, body?.detail);
290
354
  case 'upload_url_failed':
291
355
  return new KoolbaseUploadURLFailedError(message);
356
+ case 'upload_not_found':
357
+ return new KoolbaseUploadNotFoundError(message);
358
+ case 'bucket_busy':
359
+ return new KoolbaseStorageBusyError(message);
360
+ case 'invalid_cursor':
361
+ return new KoolbaseStorageInvalidCursorError(message);
362
+ case 'sign_in_required':
363
+ return new KoolbaseStorageSignInRequiredError(message);
364
+ case 'not_allowed':
365
+ return new KoolbaseStoragePermissionError(message);
292
366
  }
293
367
  // ─── status fallback (pre-code servers or uncoded paths) ───
294
368
  switch (status) {
@@ -0,0 +1,53 @@
1
+ /** What a listing is ordered by. */
2
+ export type KoolbaseFileSort = 'path' | 'createdAt' | 'updatedAt' | 'size';
3
+ export interface ListFilesOptions {
4
+ bucket: string;
5
+ /** Only files whose path starts with this, taken literally. */
6
+ prefix?: string;
7
+ /** Files per page: 50 unless set, at most 100. */
8
+ limit?: number;
9
+ /** A page's `nextCursor`, for the page after it. */
10
+ cursor?: string;
11
+ /** By path unless set. */
12
+ sort?: KoolbaseFileSort;
13
+ /** Z to A, newest first, largest first. */
14
+ descending?: boolean;
15
+ }
16
+ /** One file. */
17
+ export interface KoolbaseFileInfo {
18
+ /** Its path in the bucket: what a record stores to point at it. */
19
+ path: string;
20
+ /** The last part of its path. */
21
+ name: string;
22
+ size: number;
23
+ contentType: string | null;
24
+ etag: string | null;
25
+ metadata: Record<string, string>;
26
+ versionId: string | null;
27
+ /** Who uploaded it: a project user's id; null when the project did. */
28
+ uploadedBy: string | null;
29
+ /** ISO 8601. */
30
+ createdAt: string;
31
+ /** ISO 8601. */
32
+ updatedAt: string;
33
+ /**
34
+ * Its permanent link, while its bucket is public; null otherwise -- ask
35
+ * for a download link instead.
36
+ */
37
+ publicUrl: string | null;
38
+ }
39
+ /** One page of a listing. */
40
+ export interface KoolbaseFilePage {
41
+ files: KoolbaseFileInfo[];
42
+ /**
43
+ * Pass to the next call for the page after this one; null at the end. A
44
+ * page can hold fewer files than asked for -- the server passes over
45
+ * files you may not view -- and still have a next one.
46
+ */
47
+ nextCursor: string | null;
48
+ hasMore: boolean;
49
+ }
50
+ /** The query string for a listing. */
51
+ export declare function listFilesQuery(o: ListFilesOptions): string;
52
+ export declare function fileInfoFromJson(j: Record<string, unknown>): KoolbaseFileInfo;
53
+ export declare function filePageFromJson(j: Record<string, unknown>): KoolbaseFilePage;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ // A bucket's files, as an app sees them (S1).
3
+ //
4
+ // The server applies the bucket's view rule to every file: a listing holds
5
+ // only files the signed-in person may view, and says nothing of the rest.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.listFilesQuery = listFilesQuery;
8
+ exports.fileInfoFromJson = fileInfoFromJson;
9
+ exports.filePageFromJson = filePageFromJson;
10
+ const sortWire = {
11
+ path: 'path',
12
+ createdAt: 'created_at',
13
+ updatedAt: 'updated_at',
14
+ size: 'size',
15
+ };
16
+ /** The query string for a listing. */
17
+ function listFilesQuery(o) {
18
+ const q = new URLSearchParams();
19
+ q.set('bucket', o.bucket);
20
+ if (o.prefix)
21
+ q.set('prefix', o.prefix);
22
+ if (o.limit != null)
23
+ q.set('limit', String(o.limit));
24
+ if (o.cursor)
25
+ q.set('cursor', o.cursor);
26
+ q.set('sort', sortWire[o.sort ?? 'path'] ?? 'path');
27
+ q.set('order', o.descending ? 'desc' : 'asc');
28
+ return q.toString();
29
+ }
30
+ const str = (v) => (typeof v === 'string' && v !== '' ? v : null);
31
+ function fileInfoFromJson(j) {
32
+ const path = String(j.path ?? '');
33
+ const metadata = {};
34
+ if (j.metadata && typeof j.metadata === 'object') {
35
+ for (const [k, v] of Object.entries(j.metadata)) {
36
+ metadata[k] = typeof v === 'string' ? v : String(v);
37
+ }
38
+ }
39
+ return {
40
+ path,
41
+ name: path.split('/').pop() ?? path,
42
+ size: Number(j.size ?? 0),
43
+ contentType: str(j.content_type),
44
+ etag: str(j.etag),
45
+ metadata,
46
+ versionId: str(j.version_id),
47
+ uploadedBy: str(j.uploaded_by),
48
+ createdAt: String(j.created_at ?? ''),
49
+ updatedAt: String(j.updated_at ?? ''),
50
+ publicUrl: str(j.public_url),
51
+ };
52
+ }
53
+ function filePageFromJson(j) {
54
+ const files = Array.isArray(j.files) ? j.files : [];
55
+ const nextCursor = str(j.next_cursor);
56
+ return {
57
+ files: files.map((f) => fileInfoFromJson(f)),
58
+ nextCursor,
59
+ hasMore: nextCursor !== null,
60
+ };
61
+ }
@@ -0,0 +1,88 @@
1
+ import type { UploadResult } from './types.js';
2
+ /** Where an upload is. */
3
+ export type KoolbaseUploadState =
4
+ /** Not started: asking the server for an upload link. */
5
+ 'pending'
6
+ /** Sending the file's bytes; see `bytesSent`. */
7
+ | 'uploading'
8
+ /** The bytes have arrived; the server is checking them and putting the file in place. It can no longer be cancelled. */
9
+ | 'verifying'
10
+ /** Done: the file is in the bucket. */
11
+ | 'completed'
12
+ /** It failed; `error` says why. */
13
+ | 'failed'
14
+ /** Cancelled before the server began checking it. Nothing was stored. */
15
+ | 'cancelled';
16
+ /** One moment of an upload. */
17
+ export interface KoolbaseUploadProgress {
18
+ state: KoolbaseUploadState;
19
+ /** Bytes sent so far. */
20
+ bytesSent: number;
21
+ bytesTotal: number;
22
+ /** 0 to 1: the share of bytes sent; 1 once verifying. */
23
+ fraction: number;
24
+ /** Whether the upload has ended, one way or another. */
25
+ isDone: boolean;
26
+ /** Why it failed, when `state` is `'failed'`. */
27
+ error?: Error;
28
+ }
29
+ /** A running upload: watch it with `onProgress`, `cancel` it, await `result`. */
30
+ export declare class KoolbaseUploadTask {
31
+ private _current;
32
+ private listeners;
33
+ private cancelRequested;
34
+ private abortTransfer;
35
+ private total;
36
+ /**
37
+ * The uploaded file. Rejects with `KoolbaseUploadCancelledError` if it was
38
+ * cancelled, or the error that made it fail.
39
+ */
40
+ readonly result: Promise<UploadResult>;
41
+ private resolve;
42
+ private reject;
43
+ /** @internal */
44
+ constructor();
45
+ /** Where the upload is now. */
46
+ get current(): KoolbaseUploadProgress;
47
+ /**
48
+ * Calls `listener` on every change of state and every chunk sent, starting
49
+ * with where the upload is now. Returns a function that stops it.
50
+ */
51
+ onProgress(listener: (p: KoolbaseUploadProgress) => void): () => void;
52
+ /**
53
+ * Stops the upload, if the server has not begun checking it yet. Returns
54
+ * whether it was stopped: false once verifying (the file will land, or
55
+ * fail on its own) or done.
56
+ */
57
+ cancel(): boolean;
58
+ /** @internal */
59
+ get cancelled(): boolean;
60
+ /** @internal Throws if the upload was cancelled, between steps. */
61
+ checkCancelled(): void;
62
+ /** @internal */
63
+ setTotal(total: number): void;
64
+ /** @internal */
65
+ setAbort(abort: (() => void) | null): void;
66
+ /** @internal */
67
+ emit(state: KoolbaseUploadState, sent?: number, error?: Error): void;
68
+ /** @internal Marks the bytes sent: from here the upload cannot be cancelled. */
69
+ verifying(): void;
70
+ /** @internal */
71
+ finish(result: UploadResult): void;
72
+ /** @internal */
73
+ fail(error: unknown): void;
74
+ }
75
+ /** Runs `body` as the task's upload, starting on the next turn so a caller can listen to every event. */
76
+ export declare function runUploadTask(body: (task: KoolbaseUploadTask) => Promise<UploadResult>): KoolbaseUploadTask;
77
+ interface PutResult {
78
+ status: number;
79
+ etag: string;
80
+ }
81
+ /**
82
+ * Sends `body` to a presigned `url` with PUT, reporting bytes as they are
83
+ * sent. XMLHttpRequest where the host has it (browsers and React Native,
84
+ * both report upload progress); fetch elsewhere, where progress jumps from
85
+ * 0 to the total. A cancel aborts it.
86
+ */
87
+ export declare function putWithProgress(task: KoolbaseUploadTask, url: string, body: Blob, contentType: string): Promise<PutResult>;
88
+ export {};
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ // An upload's lifecycle, as an app watches it (S1).
3
+ //
4
+ // pending -> uploading (bytes sent of total) -> verifying -> completed, or
5
+ // failed / cancelled. Verifying is its own phase: the bytes have arrived and
6
+ // the server is checking them (size, type, the bucket's rules and cap) and
7
+ // putting them in place. Only completed means the file can be referenced
8
+ // from a record.
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.KoolbaseUploadTask = void 0;
11
+ exports.runUploadTask = runUploadTask;
12
+ exports.putWithProgress = putWithProgress;
13
+ const network_js_1 = require("./network.js");
14
+ const storage_errors_js_1 = require("./storage-errors.js");
15
+ function progressOf(state, bytesSent, bytesTotal, error) {
16
+ let fraction;
17
+ if (state === 'verifying' || state === 'completed')
18
+ fraction = 1;
19
+ else if (bytesTotal <= 0)
20
+ fraction = 0;
21
+ else
22
+ fraction = Math.min(1, Math.max(0, bytesSent / bytesTotal));
23
+ return {
24
+ state,
25
+ bytesSent,
26
+ bytesTotal,
27
+ fraction,
28
+ isDone: state === 'completed' || state === 'failed' || state === 'cancelled',
29
+ ...(error ? { error } : {}),
30
+ };
31
+ }
32
+ /** A running upload: watch it with `onProgress`, `cancel` it, await `result`. */
33
+ class KoolbaseUploadTask {
34
+ /** @internal */
35
+ constructor() {
36
+ this.listeners = new Set();
37
+ this.cancelRequested = false;
38
+ this.abortTransfer = null;
39
+ this.total = 0;
40
+ this._current = progressOf('pending', 0, 0);
41
+ this.result = new Promise((res, rej) => {
42
+ this.resolve = res;
43
+ this.reject = rej;
44
+ });
45
+ // An app that only listens to progress is not left with an unhandled
46
+ // rejection; awaiting `result` still sees the error.
47
+ this.result.catch(() => undefined);
48
+ }
49
+ /** Where the upload is now. */
50
+ get current() {
51
+ return this._current;
52
+ }
53
+ /**
54
+ * Calls `listener` on every change of state and every chunk sent, starting
55
+ * with where the upload is now. Returns a function that stops it.
56
+ */
57
+ onProgress(listener) {
58
+ this.listeners.add(listener);
59
+ listener(this._current);
60
+ return () => {
61
+ this.listeners.delete(listener);
62
+ };
63
+ }
64
+ /**
65
+ * Stops the upload, if the server has not begun checking it yet. Returns
66
+ * whether it was stopped: false once verifying (the file will land, or
67
+ * fail on its own) or done.
68
+ */
69
+ cancel() {
70
+ if (this._current.isDone || this._current.state === 'verifying')
71
+ return false;
72
+ this.cancelRequested = true;
73
+ this.abortTransfer?.();
74
+ return true;
75
+ }
76
+ /** @internal */
77
+ get cancelled() {
78
+ return this.cancelRequested;
79
+ }
80
+ /** @internal Throws if the upload was cancelled, between steps. */
81
+ checkCancelled() {
82
+ if (this.cancelRequested)
83
+ throw new storage_errors_js_1.KoolbaseUploadCancelledError();
84
+ }
85
+ /** @internal */
86
+ setTotal(total) {
87
+ this.total = total;
88
+ }
89
+ /** @internal */
90
+ setAbort(abort) {
91
+ this.abortTransfer = abort;
92
+ }
93
+ /** @internal */
94
+ emit(state, sent, error) {
95
+ if (this._current.isDone)
96
+ return;
97
+ this._current = progressOf(state, sent ?? this._current.bytesSent, this.total, error);
98
+ for (const l of [...this.listeners]) {
99
+ try {
100
+ l(this._current);
101
+ }
102
+ catch {
103
+ // A listener's own failure is not the upload's.
104
+ }
105
+ }
106
+ }
107
+ /** @internal Marks the bytes sent: from here the upload cannot be cancelled. */
108
+ verifying() {
109
+ this.checkCancelled();
110
+ this.emit('verifying', this.total);
111
+ }
112
+ /** @internal */
113
+ finish(result) {
114
+ this.emit('completed', this.total);
115
+ this.listeners.clear();
116
+ this.resolve(result);
117
+ }
118
+ /** @internal */
119
+ fail(error) {
120
+ if (this.cancelRequested) {
121
+ const e = new storage_errors_js_1.KoolbaseUploadCancelledError();
122
+ this.emit('cancelled');
123
+ this.reject(e);
124
+ }
125
+ else {
126
+ const e = error instanceof Error ? error : new storage_errors_js_1.KoolbaseStorageError(String(error));
127
+ this.emit('failed', undefined, e);
128
+ this.reject(e);
129
+ }
130
+ this.listeners.clear();
131
+ }
132
+ }
133
+ exports.KoolbaseUploadTask = KoolbaseUploadTask;
134
+ /** Runs `body` as the task's upload, starting on the next turn so a caller can listen to every event. */
135
+ function runUploadTask(body) {
136
+ const task = new KoolbaseUploadTask();
137
+ Promise.resolve().then(async () => {
138
+ try {
139
+ task.finish(await body(task));
140
+ }
141
+ catch (e) {
142
+ task.fail(e);
143
+ }
144
+ });
145
+ return task;
146
+ }
147
+ /**
148
+ * Sends `body` to a presigned `url` with PUT, reporting bytes as they are
149
+ * sent. XMLHttpRequest where the host has it (browsers and React Native,
150
+ * both report upload progress); fetch elsewhere, where progress jumps from
151
+ * 0 to the total. A cancel aborts it.
152
+ */
153
+ function putWithProgress(task, url, body, contentType) {
154
+ task.checkCancelled();
155
+ const total = body.size;
156
+ const report = (sent) => {
157
+ if (!task.cancelled)
158
+ task.emit('uploading', Math.min(sent, total));
159
+ };
160
+ report(0);
161
+ const XHR = globalThis.XMLHttpRequest;
162
+ if (XHR) {
163
+ return new Promise((resolve, reject) => {
164
+ const xhr = new XHR();
165
+ let settled = false;
166
+ const done = (fn) => {
167
+ if (settled)
168
+ return;
169
+ settled = true;
170
+ task.setAbort(null);
171
+ fn();
172
+ };
173
+ xhr.open('PUT', url);
174
+ xhr.setRequestHeader('Content-Type', contentType);
175
+ if (xhr.upload) {
176
+ xhr.upload.onprogress = (ev) => report(ev.loaded);
177
+ }
178
+ xhr.onload = () => done(() => {
179
+ if (task.cancelled)
180
+ return reject(new storage_errors_js_1.KoolbaseUploadCancelledError());
181
+ resolve({ status: xhr.status, etag: xhr.getResponseHeader('etag') ?? '' });
182
+ });
183
+ xhr.onerror = () => done(() => reject(task.cancelled ? new storage_errors_js_1.KoolbaseUploadCancelledError() : new network_js_1.KoolbaseNetworkError(url, new Error('the upload did not complete'))));
184
+ xhr.onabort = () => done(() => reject(new storage_errors_js_1.KoolbaseUploadCancelledError()));
185
+ task.setAbort(() => {
186
+ xhr.abort();
187
+ // Some hosts do not fire onabort for an aborted send.
188
+ done(() => reject(new storage_errors_js_1.KoolbaseUploadCancelledError()));
189
+ });
190
+ xhr.send(body);
191
+ });
192
+ }
193
+ const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
194
+ task.setAbort(() => controller?.abort());
195
+ return fetch(url, {
196
+ method: 'PUT',
197
+ headers: { 'Content-Type': contentType },
198
+ body,
199
+ signal: controller?.signal,
200
+ })
201
+ .then((res) => {
202
+ task.checkCancelled();
203
+ return { status: res.status, etag: res.headers.get('etag') ?? '' };
204
+ })
205
+ .catch((e) => {
206
+ if (task.cancelled || e?.name === 'AbortError') {
207
+ throw new storage_errors_js_1.KoolbaseUploadCancelledError();
208
+ }
209
+ if (e instanceof storage_errors_js_1.KoolbaseStorageError)
210
+ throw e;
211
+ throw new network_js_1.KoolbaseNetworkError(url, e);
212
+ })
213
+ .finally(() => task.setAbort(null));
214
+ }
@@ -1,4 +1,6 @@
1
1
  import { KoolbaseConfig, UploadOptions, UploadResult, KoolbaseObject, KoolbaseObjectVersion, KoolbaseImageTransform } from './types.js';
2
+ import { KoolbaseUploadTask } from './storage-transfer.js';
3
+ import { KoolbaseFileInfo, KoolbaseFilePage, ListFilesOptions } from './storage-files.js';
2
4
  /**
3
5
  * Koolbase storage client — uploads, downloads, and deletes via presigned
4
6
  * Cloudflare R2 URLs.
@@ -53,6 +55,45 @@ export declare class KoolbaseStorage {
53
55
  * overwriting silently, pass `overwrite: true` explicitly.
54
56
  */
55
57
  upload(options: UploadOptions): Promise<UploadResult>;
58
+ /**
59
+ * Starts an upload you can follow and cancel: the same upload as
60
+ * {@link upload}, returned at once as a {@link KoolbaseUploadTask}.
61
+ *
62
+ * The task goes pending -> uploading (bytes sent of total) -> verifying
63
+ * -> completed, or failed / cancelled. `cancel()` stops it until the
64
+ * server begins checking the bytes (verifying); nothing is stored. Only
65
+ * completed means the file can be referenced from a record.
66
+ *
67
+ * @example
68
+ * const task = Koolbase.storage.startUpload({ bucket: 'posters', path: 'gig.png', file });
69
+ * const stop = task.onProgress((p) => setPercent(Math.round(p.fraction * 100)));
70
+ * cancelButton.onclick = () => task.cancel();
71
+ * const { object } = await task.result;
72
+ */
73
+ startUpload(options: UploadOptions): KoolbaseUploadTask;
74
+ private runUpload;
75
+ /**
76
+ * One page of a bucket's files. The bucket's view rule is applied to every
77
+ * file: the page holds only files the signed-in person may view. Pass the
78
+ * page's `nextCursor` back as `cursor` for the next one; a page can hold
79
+ * fewer files than `limit` and still have a next.
80
+ *
81
+ * @example
82
+ * let cursor: string | undefined;
83
+ * do {
84
+ * const page = await Koolbase.storage.listFiles({ bucket: 'posters', prefix: 'events/', cursor });
85
+ * show(page.files);
86
+ * cursor = page.nextCursor ?? undefined;
87
+ * } while (cursor);
88
+ */
89
+ listFiles(options: ListFilesOptions): Promise<KoolbaseFilePage>;
90
+ /**
91
+ * One file's details -- size, type, metadata, who uploaded it, and its
92
+ * permanent link while its bucket is public. Throws
93
+ * `KoolbaseStorageNotFoundError` for a file that is not there or that
94
+ * the bucket's view rule hides from the caller.
95
+ */
96
+ getFileInfo(bucket: string, path: string): Promise<KoolbaseFileInfo>;
56
97
  /**
57
98
  * Apply a partial metadata update to an existing object. Returns the
58
99
  * post-update {@link KoolbaseObject} with the merged metadata.