@koolbase/core 12.17.0 → 12.19.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.
- package/dist/cjs/files-controller.d.ts +62 -0
- package/dist/cjs/files-controller.js +160 -0
- package/dist/cjs/files-refresh.d.ts +9 -0
- package/dist/cjs/files-refresh.js +39 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/react/index.d.ts +1 -0
- package/dist/cjs/react/index.js +3 -1
- package/dist/cjs/react/use-files.d.ts +8 -0
- package/dist/cjs/react/use-files.js +37 -0
- package/dist/cjs/storage-errors.d.ts +14 -0
- package/dist/cjs/storage-errors.js +29 -1
- package/dist/cjs/storage-multipart.d.ts +29 -0
- package/dist/cjs/storage-multipart.js +168 -0
- package/dist/cjs/storage-transfer.d.ts +25 -6
- package/dist/cjs/storage-transfer.js +78 -37
- package/dist/cjs/storage.d.ts +40 -0
- package/dist/cjs/storage.js +205 -15
- package/dist/esm/files-controller.d.ts +62 -0
- package/dist/esm/files-controller.js +155 -0
- package/dist/esm/files-refresh.d.ts +9 -0
- package/dist/esm/files-refresh.js +34 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/react/index.d.ts +1 -0
- package/dist/esm/react/index.js +1 -0
- package/dist/esm/react/use-files.d.ts +8 -0
- package/dist/esm/react/use-files.js +34 -0
- package/dist/esm/storage-errors.d.ts +14 -0
- package/dist/esm/storage-errors.js +26 -0
- package/dist/esm/storage-multipart.d.ts +29 -0
- package/dist/esm/storage-multipart.js +164 -0
- package/dist/esm/storage-transfer.d.ts +25 -6
- package/dist/esm/storage-transfer.js +77 -37
- package/dist/esm/storage.d.ts +40 -0
- package/dist/esm/storage.js +205 -15
- package/package.json +1 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { KoolbaseFileInfo, KoolbaseFilePage, KoolbaseFileSort, ListFilesOptions } from './storage-files.js';
|
|
2
|
+
export type FilesStatus = 'loading' | 'loaded' | 'error';
|
|
3
|
+
/** Which files, in what order. The bucket's view rule decides the rest. */
|
|
4
|
+
export interface FilesQuery {
|
|
5
|
+
/** Only files whose path starts with this, taken literally. */
|
|
6
|
+
prefix?: string;
|
|
7
|
+
/** By path unless set. */
|
|
8
|
+
sort?: KoolbaseFileSort;
|
|
9
|
+
/** Z to A, newest first, largest first. */
|
|
10
|
+
descending?: boolean;
|
|
11
|
+
/** Files per page: 50 unless set, at most 100. */
|
|
12
|
+
limit?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface FilesState {
|
|
15
|
+
readonly status: FilesStatus;
|
|
16
|
+
readonly files: readonly KoolbaseFileInfo[];
|
|
17
|
+
/** Why the first load failed, while status is 'error'. */
|
|
18
|
+
readonly error: unknown;
|
|
19
|
+
/** True while refresh() runs. */
|
|
20
|
+
readonly refreshing: boolean;
|
|
21
|
+
/** True while loadMore() runs. */
|
|
22
|
+
readonly loadingMore: boolean;
|
|
23
|
+
/** Whether there is a page past the last one loaded. True before the first load. */
|
|
24
|
+
readonly hasMore: boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare const initialFilesState: FilesState;
|
|
27
|
+
/** One string per distinct listing. */
|
|
28
|
+
export declare function filesQueryKey(bucket: string, query?: FilesQuery): string;
|
|
29
|
+
type FilesReader = {
|
|
30
|
+
listFiles(options: ListFilesOptions): Promise<KoolbaseFilePage>;
|
|
31
|
+
};
|
|
32
|
+
export declare class KoolbaseFilesController {
|
|
33
|
+
readonly bucket: string;
|
|
34
|
+
private readonly storage;
|
|
35
|
+
private readonly spec;
|
|
36
|
+
private state;
|
|
37
|
+
private readonly listeners;
|
|
38
|
+
private generation;
|
|
39
|
+
private disposed;
|
|
40
|
+
private pageOne;
|
|
41
|
+
private laterPages;
|
|
42
|
+
private cursor;
|
|
43
|
+
private unregister;
|
|
44
|
+
constructor(storage: FilesReader, bucket: string, query?: FilesQuery);
|
|
45
|
+
getState(): FilesState;
|
|
46
|
+
/** Called on every change. Returns the unsubscribe. */
|
|
47
|
+
subscribe(listener: () => void): () => void;
|
|
48
|
+
/** The first load. Call once; refresh() for later loads. */
|
|
49
|
+
load(): Promise<void>;
|
|
50
|
+
/** Page one again. What is shown stays while it runs, and stays if it fails. */
|
|
51
|
+
refresh(): Promise<void>;
|
|
52
|
+
/** The next page, appended. Does nothing unless loaded with a page to come. */
|
|
53
|
+
loadMore(): Promise<void>;
|
|
54
|
+
/** Stops all updates. Results still in flight are dropped when they land. */
|
|
55
|
+
dispose(): void;
|
|
56
|
+
private firstPage;
|
|
57
|
+
private publish;
|
|
58
|
+
private options;
|
|
59
|
+
private isStale;
|
|
60
|
+
private set;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// A bucket's files read as a list, framework-free (storage S4).
|
|
3
|
+
//
|
|
4
|
+
// The data half of a file gallery: loading, loaded or error, refresh, and
|
|
5
|
+
// the next page by cursor. No React here, so it is tested in this package;
|
|
6
|
+
// useFiles in @koolbase/react-native and @koolbase/js/react is a thin
|
|
7
|
+
// wrapper. Mirrors the Flutter SDK's KoolbaseFilesController.
|
|
8
|
+
//
|
|
9
|
+
// - only a first load with nothing to show is the error state; a failed
|
|
10
|
+
// refresh or loadMore keeps what is shown (stale beats blank)
|
|
11
|
+
// - loadMore appends the next page while there is one; a file already
|
|
12
|
+
// shown is never shown twice (matched by path)
|
|
13
|
+
// - an upload, delete, move, copy, metadata change or restore made through
|
|
14
|
+
// this SDK in the bucket reloads page one silently (files-refresh.ts)
|
|
15
|
+
// - every fetch belongs to a generation: a result from a fetch a later
|
|
16
|
+
// refresh superseded, or one landing after dispose(), is dropped
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.KoolbaseFilesController = exports.initialFilesState = void 0;
|
|
19
|
+
exports.filesQueryKey = filesQueryKey;
|
|
20
|
+
const files_refresh_js_1 = require("./files-refresh.js");
|
|
21
|
+
exports.initialFilesState = Object.freeze({
|
|
22
|
+
status: 'loading',
|
|
23
|
+
files: Object.freeze([]),
|
|
24
|
+
error: null,
|
|
25
|
+
refreshing: false,
|
|
26
|
+
loadingMore: false,
|
|
27
|
+
hasMore: true,
|
|
28
|
+
});
|
|
29
|
+
/** One string per distinct listing. */
|
|
30
|
+
function filesQueryKey(bucket, query = {}) {
|
|
31
|
+
return JSON.stringify([bucket, query.prefix ?? '', query.sort ?? 'path', query.descending === true, query.limit ?? null]);
|
|
32
|
+
}
|
|
33
|
+
/** Page one, then the later pages without any file already shown. */
|
|
34
|
+
function mergeByPath(first, rest) {
|
|
35
|
+
const seen = new Set(first.map((f) => f.path));
|
|
36
|
+
const out = [...first];
|
|
37
|
+
for (const f of rest) {
|
|
38
|
+
if (seen.has(f.path))
|
|
39
|
+
continue;
|
|
40
|
+
seen.add(f.path);
|
|
41
|
+
out.push(f);
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
class KoolbaseFilesController {
|
|
46
|
+
constructor(storage, bucket, query = {}) {
|
|
47
|
+
this.state = exports.initialFilesState;
|
|
48
|
+
this.listeners = new Set();
|
|
49
|
+
this.generation = 0;
|
|
50
|
+
this.disposed = false;
|
|
51
|
+
this.pageOne = [];
|
|
52
|
+
this.laterPages = [];
|
|
53
|
+
this.cursor = null;
|
|
54
|
+
this.unregister = null;
|
|
55
|
+
this.storage = storage;
|
|
56
|
+
this.bucket = bucket;
|
|
57
|
+
this.spec = query;
|
|
58
|
+
}
|
|
59
|
+
getState() {
|
|
60
|
+
return this.state;
|
|
61
|
+
}
|
|
62
|
+
/** Called on every change. Returns the unsubscribe. */
|
|
63
|
+
subscribe(listener) {
|
|
64
|
+
if (this.disposed)
|
|
65
|
+
return () => { };
|
|
66
|
+
this.listeners.add(listener);
|
|
67
|
+
return () => {
|
|
68
|
+
this.listeners.delete(listener);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The first load. Call once; refresh() for later loads. */
|
|
72
|
+
async load() {
|
|
73
|
+
if (this.disposed)
|
|
74
|
+
return;
|
|
75
|
+
this.unregister ?? (this.unregister = (0, files_refresh_js_1.registerFilesRefresher)(this.bucket, () => this.firstPage()));
|
|
76
|
+
await this.firstPage();
|
|
77
|
+
}
|
|
78
|
+
/** Page one again. What is shown stays while it runs, and stays if it fails. */
|
|
79
|
+
async refresh() {
|
|
80
|
+
if (this.disposed)
|
|
81
|
+
return;
|
|
82
|
+
this.set({ refreshing: true });
|
|
83
|
+
await this.firstPage();
|
|
84
|
+
}
|
|
85
|
+
/** The next page, appended. Does nothing unless loaded with a page to come. */
|
|
86
|
+
async loadMore() {
|
|
87
|
+
const s = this.state;
|
|
88
|
+
if (this.disposed || s.loadingMore || !s.hasMore || s.status !== 'loaded' || this.cursor === null)
|
|
89
|
+
return;
|
|
90
|
+
const gen = this.generation;
|
|
91
|
+
this.set({ loadingMore: true });
|
|
92
|
+
try {
|
|
93
|
+
const page = await this.storage.listFiles(this.options(this.cursor));
|
|
94
|
+
if (this.isStale(gen))
|
|
95
|
+
return;
|
|
96
|
+
this.laterPages = [...this.laterPages, ...page.files];
|
|
97
|
+
this.cursor = page.nextCursor;
|
|
98
|
+
this.publish({});
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// Keep what is shown; the caller can try again.
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
if (!this.isStale(gen))
|
|
105
|
+
this.set({ loadingMore: false });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Stops all updates. Results still in flight are dropped when they land. */
|
|
109
|
+
dispose() {
|
|
110
|
+
this.disposed = true;
|
|
111
|
+
this.unregister?.();
|
|
112
|
+
this.unregister = null;
|
|
113
|
+
this.listeners.clear();
|
|
114
|
+
}
|
|
115
|
+
async firstPage() {
|
|
116
|
+
const gen = ++this.generation;
|
|
117
|
+
if (this.state.loadingMore)
|
|
118
|
+
this.set({ loadingMore: false });
|
|
119
|
+
try {
|
|
120
|
+
const page = await this.storage.listFiles(this.options(undefined));
|
|
121
|
+
if (!this.isStale(gen)) {
|
|
122
|
+
this.pageOne = page.files;
|
|
123
|
+
this.laterPages = [];
|
|
124
|
+
this.cursor = page.nextCursor;
|
|
125
|
+
this.publish({ status: 'loaded', error: null, refreshing: false });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
// Only a first load with nothing to show is the error state. Whichever
|
|
130
|
+
// fetch is newest ends a refresh, a silent one included.
|
|
131
|
+
if (!this.isStale(gen)) {
|
|
132
|
+
this.set(this.state.files.length === 0 ? { status: 'error', error, refreshing: false } : { refreshing: false });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
publish(patch) {
|
|
137
|
+
this.set({ ...patch, files: mergeByPath(this.pageOne, this.laterPages), hasMore: this.cursor !== null });
|
|
138
|
+
}
|
|
139
|
+
options(cursor) {
|
|
140
|
+
return {
|
|
141
|
+
bucket: this.bucket,
|
|
142
|
+
...(this.spec.prefix ? { prefix: this.spec.prefix } : {}),
|
|
143
|
+
...(this.spec.limit != null ? { limit: this.spec.limit } : {}),
|
|
144
|
+
...(this.spec.sort ? { sort: this.spec.sort } : {}),
|
|
145
|
+
...(this.spec.descending ? { descending: true } : {}),
|
|
146
|
+
...(cursor ? { cursor } : {}),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
isStale(gen) {
|
|
150
|
+
return this.disposed || gen !== this.generation;
|
|
151
|
+
}
|
|
152
|
+
set(patch) {
|
|
153
|
+
if (this.disposed)
|
|
154
|
+
return;
|
|
155
|
+
this.state = Object.freeze({ ...this.state, ...patch });
|
|
156
|
+
for (const listener of [...this.listeners])
|
|
157
|
+
listener();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.KoolbaseFilesController = KoolbaseFilesController;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Registers one file list's reload; returns the unregister. */
|
|
2
|
+
export declare function registerFilesRefresher(bucket: string, refresh: () => Promise<void>): () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Reloads every file list on [buckets]. One after another; a failure in one
|
|
5
|
+
* never stops the others, and never reaches the change that caused it.
|
|
6
|
+
*/
|
|
7
|
+
export declare function refreshBucketFiles(...buckets: string[]): Promise<void>;
|
|
8
|
+
/** Tests only. */
|
|
9
|
+
export declare function clearFilesRefreshers(): void;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// File lists reloaded after a change made through this SDK (storage S4): an
|
|
3
|
+
// upload, a delete, a move or copy, a metadata change, a restore. Keyed by
|
|
4
|
+
// bucket; each list reloads ITSELF, with its own prefix and order.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.registerFilesRefresher = registerFilesRefresher;
|
|
7
|
+
exports.refreshBucketFiles = refreshBucketFiles;
|
|
8
|
+
exports.clearFilesRefreshers = clearFilesRefreshers;
|
|
9
|
+
const refreshers = new Map();
|
|
10
|
+
let next = 0;
|
|
11
|
+
/** Registers one file list's reload; returns the unregister. */
|
|
12
|
+
function registerFilesRefresher(bucket, refresh) {
|
|
13
|
+
const key = `${bucket}\u0000${++next}`;
|
|
14
|
+
refreshers.set(key, refresh);
|
|
15
|
+
return () => {
|
|
16
|
+
refreshers.delete(key);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Reloads every file list on [buckets]. One after another; a failure in one
|
|
21
|
+
* never stops the others, and never reaches the change that caused it.
|
|
22
|
+
*/
|
|
23
|
+
async function refreshBucketFiles(...buckets) {
|
|
24
|
+
const prefixes = buckets.map((b) => `${b}\u0000`);
|
|
25
|
+
const due = [...refreshers.entries()].filter(([k]) => prefixes.some((p) => k.startsWith(p))).map(([, f]) => f);
|
|
26
|
+
for (const refresh of due) {
|
|
27
|
+
try {
|
|
28
|
+
await refresh();
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
// eslint-disable-next-line no-console
|
|
32
|
+
console.warn('[Koolbase] A file list failed to reload after a change:', e);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Tests only. */
|
|
37
|
+
function clearFilesRefreshers() {
|
|
38
|
+
refreshers.clear();
|
|
39
|
+
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export { KoolbaseStorage } from './storage.js';
|
|
|
23
23
|
export { KoolbaseUploadTask } from './storage-transfer.js';
|
|
24
24
|
export type { KoolbaseUploadProgress, KoolbaseUploadState } from './storage-transfer.js';
|
|
25
25
|
export type { KoolbaseFileInfo, KoolbaseFilePage, KoolbaseFileSort, ListFilesOptions } from './storage-files.js';
|
|
26
|
+
export { KoolbaseFilesController, initialFilesState, filesQueryKey } from './files-controller.js';
|
|
27
|
+
export type { FilesQuery, FilesState, FilesStatus } from './files-controller.js';
|
|
26
28
|
export { KoolbaseAnalytics, disabledAnalytics } from './analytics.js';
|
|
27
29
|
export { KoolbaseFiscal, KoolbaseFiscalError, decodeFiscalResponse, isFiscalized, isPending } from './fiscal.js';
|
|
28
30
|
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.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;
|
|
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.filesQueryKey = exports.initialFilesState = exports.KoolbaseFilesController = 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);
|
|
@@ -53,6 +53,11 @@ var storage_js_1 = require("./storage.js");
|
|
|
53
53
|
Object.defineProperty(exports, "KoolbaseStorage", { enumerable: true, get: function () { return storage_js_1.KoolbaseStorage; } });
|
|
54
54
|
var storage_transfer_js_1 = require("./storage-transfer.js");
|
|
55
55
|
Object.defineProperty(exports, "KoolbaseUploadTask", { enumerable: true, get: function () { return storage_transfer_js_1.KoolbaseUploadTask; } });
|
|
56
|
+
// A bucket's files as list state (storage S4).
|
|
57
|
+
var files_controller_js_1 = require("./files-controller.js");
|
|
58
|
+
Object.defineProperty(exports, "KoolbaseFilesController", { enumerable: true, get: function () { return files_controller_js_1.KoolbaseFilesController; } });
|
|
59
|
+
Object.defineProperty(exports, "initialFilesState", { enumerable: true, get: function () { return files_controller_js_1.initialFilesState; } });
|
|
60
|
+
Object.defineProperty(exports, "filesQueryKey", { enumerable: true, get: function () { return files_controller_js_1.filesQueryKey; } });
|
|
56
61
|
var analytics_js_1 = require("./analytics.js");
|
|
57
62
|
Object.defineProperty(exports, "KoolbaseAnalytics", { enumerable: true, get: function () { return analytics_js_1.KoolbaseAnalytics; } });
|
|
58
63
|
Object.defineProperty(exports, "disabledAnalytics", { enumerable: true, get: function () { return analytics_js_1.disabledAnalytics; } });
|
package/dist/cjs/react/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createUseRecord = exports.createUseCollection = void 0;
|
|
3
|
+
exports.createUseFiles = exports.createUseRecord = exports.createUseCollection = void 0;
|
|
4
4
|
// React hooks over core's controllers: ONE implementation, exported to apps by
|
|
5
5
|
// @koolbase/react-native (from its root, as always) and @koolbase/js/react.
|
|
6
6
|
// React only -- nothing native, nothing DOM. Deliberately not exported from
|
|
@@ -9,3 +9,5 @@ var use_collection_js_1 = require("./use-collection.js");
|
|
|
9
9
|
Object.defineProperty(exports, "createUseCollection", { enumerable: true, get: function () { return use_collection_js_1.createUseCollection; } });
|
|
10
10
|
var use_record_js_1 = require("./use-record.js");
|
|
11
11
|
Object.defineProperty(exports, "createUseRecord", { enumerable: true, get: function () { return use_record_js_1.createUseRecord; } });
|
|
12
|
+
var use_files_js_1 = require("./use-files.js");
|
|
13
|
+
Object.defineProperty(exports, "createUseFiles", { enumerable: true, get: function () { return use_files_js_1.createUseFiles; } });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FilesQuery, type FilesState, type KoolbaseStorage } from '../index.js';
|
|
2
|
+
export interface UseFilesResult extends FilesState {
|
|
3
|
+
/** Page one again. What is shown stays while it runs, and stays if it fails. */
|
|
4
|
+
refresh: () => Promise<void>;
|
|
5
|
+
/** The next page, appended, while hasMore. */
|
|
6
|
+
loadMore: () => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export declare function createUseFiles(getStorage: () => Pick<KoolbaseStorage, 'listFiles'>): (bucket: string, query?: FilesQuery) => UseFilesResult;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// useFiles: a bucket's files as React state (storage S4).
|
|
3
|
+
//
|
|
4
|
+
// A thin wrapper. Everything about getting the files right lives in
|
|
5
|
+
// @koolbase/core's KoolbaseFilesController, which is tested there; this
|
|
6
|
+
// file only ties one controller's lifetime to a component's.
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.createUseFiles = createUseFiles;
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const index_js_1 = require("../index.js");
|
|
11
|
+
const noop = () => { };
|
|
12
|
+
const done = () => Promise.resolve();
|
|
13
|
+
function createUseFiles(getStorage) {
|
|
14
|
+
return function useFiles(bucket, query = {}) {
|
|
15
|
+
// The query as a value: an inline object is a new object every render,
|
|
16
|
+
// and must not start a new fetch every render.
|
|
17
|
+
const key = (0, index_js_1.filesQueryKey)(bucket, query);
|
|
18
|
+
const [current, setCurrent] = (0, react_1.useState)(null);
|
|
19
|
+
(0, react_1.useEffect)(() => {
|
|
20
|
+
// Created here, not during render, so React strict mode's second run
|
|
21
|
+
// never keeps a disposed controller.
|
|
22
|
+
const controller = new index_js_1.KoolbaseFilesController(getStorage(), bucket, query);
|
|
23
|
+
setCurrent({ key, controller });
|
|
24
|
+
void controller.load();
|
|
25
|
+
return () => controller.dispose();
|
|
26
|
+
}, [key]);
|
|
27
|
+
// A controller from the previous key reads as a fresh load, never as
|
|
28
|
+
// the previous listing's files.
|
|
29
|
+
const controller = current?.key === key ? current.controller : null;
|
|
30
|
+
const subscribe = (0, react_1.useCallback)((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
|
|
31
|
+
const getSnapshot = (0, react_1.useCallback)(() => (controller ? controller.getState() : index_js_1.initialFilesState), [controller]);
|
|
32
|
+
const state = (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
33
|
+
const refresh = (0, react_1.useCallback)(() => (controller ? controller.refresh() : done()), [controller]);
|
|
34
|
+
const loadMore = (0, react_1.useCallback)(() => (controller ? controller.loadMore() : done()), [controller]);
|
|
35
|
+
return { ...state, refresh, loadMore };
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -226,6 +226,20 @@ export declare class KoolbaseStorageInvalidCursorError extends KoolbaseStorageEr
|
|
|
226
226
|
export declare class KoolbaseStorageSignInRequiredError extends KoolbaseStorageError {
|
|
227
227
|
constructor(message?: string);
|
|
228
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Completing an upload in parts with parts missing or the wrong size (S3):
|
|
231
|
+
* resume it, which sends `missing`. Code: `upload_incomplete`.
|
|
232
|
+
*/
|
|
233
|
+
export declare class KoolbaseUploadIncompleteError extends KoolbaseStorageError {
|
|
234
|
+
missing: number[];
|
|
235
|
+
constructor(message?: string, missing?: number[]);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* A download stopped by its `signal` (S3). Code: `download_cancelled`.
|
|
239
|
+
*/
|
|
240
|
+
export declare class KoolbaseDownloadCancelledError extends KoolbaseStorageError {
|
|
241
|
+
constructor(message?: string);
|
|
242
|
+
}
|
|
229
243
|
/**
|
|
230
244
|
* A move refused while records point at the file (S1b): `field` holds its
|
|
231
245
|
* path in `records` records. Change those first, or copy the file. Code:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KoolbaseStorageNameHasHistoryError = exports.KoolbaseStorageFileInUseError = 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;
|
|
3
|
+
exports.KoolbaseStorageNameHasHistoryError = exports.KoolbaseStorageFileInUseError = exports.KoolbaseDownloadCancelledError = exports.KoolbaseUploadIncompleteError = 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");
|
|
@@ -316,6 +316,30 @@ class KoolbaseStorageSignInRequiredError extends KoolbaseStorageError {
|
|
|
316
316
|
}
|
|
317
317
|
}
|
|
318
318
|
exports.KoolbaseStorageSignInRequiredError = KoolbaseStorageSignInRequiredError;
|
|
319
|
+
/**
|
|
320
|
+
* Completing an upload in parts with parts missing or the wrong size (S3):
|
|
321
|
+
* resume it, which sends `missing`. Code: `upload_incomplete`.
|
|
322
|
+
*/
|
|
323
|
+
class KoolbaseUploadIncompleteError extends KoolbaseStorageError {
|
|
324
|
+
constructor(message, missing = []) {
|
|
325
|
+
super(message ?? 'The upload is missing parts; resume it', 'upload_incomplete');
|
|
326
|
+
this.missing = missing;
|
|
327
|
+
this.name = 'KoolbaseUploadIncompleteError';
|
|
328
|
+
Object.setPrototypeOf(this, KoolbaseUploadIncompleteError.prototype);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
exports.KoolbaseUploadIncompleteError = KoolbaseUploadIncompleteError;
|
|
332
|
+
/**
|
|
333
|
+
* A download stopped by its `signal` (S3). Code: `download_cancelled`.
|
|
334
|
+
*/
|
|
335
|
+
class KoolbaseDownloadCancelledError extends KoolbaseStorageError {
|
|
336
|
+
constructor(message) {
|
|
337
|
+
super(message ?? 'The download was cancelled', 'download_cancelled');
|
|
338
|
+
this.name = 'KoolbaseDownloadCancelledError';
|
|
339
|
+
Object.setPrototypeOf(this, KoolbaseDownloadCancelledError.prototype);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
exports.KoolbaseDownloadCancelledError = KoolbaseDownloadCancelledError;
|
|
319
343
|
/**
|
|
320
344
|
* A move refused while records point at the file (S1b): `field` holds its
|
|
321
345
|
* path in `records` records. Change those first, or copy the file. Code:
|
|
@@ -397,6 +421,10 @@ function koolbaseStorageError(status, body, fallbackMessage = 'Storage request f
|
|
|
397
421
|
}
|
|
398
422
|
case 'name_has_history':
|
|
399
423
|
return new KoolbaseStorageNameHasHistoryError(message);
|
|
424
|
+
case 'upload_incomplete': {
|
|
425
|
+
const d = (body?.details ?? {});
|
|
426
|
+
return new KoolbaseUploadIncompleteError(message, Array.isArray(d.missing) ? d.missing : []);
|
|
427
|
+
}
|
|
400
428
|
}
|
|
401
429
|
// ─── status fallback (pre-code servers or uncoded paths) ───
|
|
402
430
|
switch (status) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { KoolbaseUploadTask } from './storage-transfer.js';
|
|
2
|
+
/** The server's plan for an upload in parts, and which parts arrived. */
|
|
3
|
+
export interface MultipartPlan {
|
|
4
|
+
upload_id: string;
|
|
5
|
+
path: string;
|
|
6
|
+
size: number;
|
|
7
|
+
part_size: number;
|
|
8
|
+
part_count: number;
|
|
9
|
+
content_type: string;
|
|
10
|
+
uploaded?: number[];
|
|
11
|
+
}
|
|
12
|
+
/** What sending the parts needs from the server. */
|
|
13
|
+
export interface MultipartApi {
|
|
14
|
+
links(parts: number[]): Promise<Record<string, string>>;
|
|
15
|
+
abort(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
/** Parts in flight at once. */
|
|
18
|
+
export declare const PARTS_AT_ONCE = 3;
|
|
19
|
+
/** Tries per part. */
|
|
20
|
+
export declare const PART_TRIES = 4;
|
|
21
|
+
/** How long to wait before try `n` (1-based) of a part; tests shorten it. */
|
|
22
|
+
export declare const partRetry: {
|
|
23
|
+
delayMs: (n: number) => number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Sends the parts of `file` not yet in `plan.uploaded`, reporting the bytes
|
|
27
|
+
* of the whole file; resolves once every part has arrived.
|
|
28
|
+
*/
|
|
29
|
+
export declare function sendParts(task: KoolbaseUploadTask, plan: MultipartPlan, file: Blob, api: MultipartApi): Promise<void>;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// A large file's upload, in parts (S3).
|
|
3
|
+
//
|
|
4
|
+
// Each part goes straight to storage on its own signed link, three at a time.
|
|
5
|
+
// A part that fails for a passing reason -- the network, storage busy -- is
|
|
6
|
+
// sent again, up to four times, waiting longer each time; one whose link
|
|
7
|
+
// has expired gets a new link. A failed upload is not abandoned: its id
|
|
8
|
+
// (`KoolbaseUploadTask.uploadId`) resumes it, sending only the parts that
|
|
9
|
+
// did not arrive. Cancelling abandons it.
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.partRetry = exports.PART_TRIES = exports.PARTS_AT_ONCE = void 0;
|
|
12
|
+
exports.sendParts = sendParts;
|
|
13
|
+
const storage_errors_js_1 = require("./storage-errors.js");
|
|
14
|
+
const storage_transfer_js_1 = require("./storage-transfer.js");
|
|
15
|
+
/** Parts in flight at once. */
|
|
16
|
+
exports.PARTS_AT_ONCE = 3;
|
|
17
|
+
/** Tries per part. */
|
|
18
|
+
exports.PART_TRIES = 4;
|
|
19
|
+
/** Links asked for at a time (the server signs at most 100 a call). */
|
|
20
|
+
const LINKS_AT_ONCE = 50;
|
|
21
|
+
/** How long to wait before try `n` (1-based) of a part; tests shorten it. */
|
|
22
|
+
exports.partRetry = { delayMs: (n) => 500 * 2 ** (n - 1) };
|
|
23
|
+
function sleep(ms) {
|
|
24
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
25
|
+
}
|
|
26
|
+
/** A status a part may be sent again after. */
|
|
27
|
+
const passing = (status) => status === 408 || status === 429 || status >= 500;
|
|
28
|
+
/**
|
|
29
|
+
* Sends the parts of `file` not yet in `plan.uploaded`, reporting the bytes
|
|
30
|
+
* of the whole file; resolves once every part has arrived.
|
|
31
|
+
*/
|
|
32
|
+
async function sendParts(task, plan, file, api) {
|
|
33
|
+
const count = plan.part_count;
|
|
34
|
+
const partSize = plan.part_size;
|
|
35
|
+
const bounds = (n) => [(n - 1) * partSize, Math.min(n * partSize, plan.size)];
|
|
36
|
+
const arrived = new Set(plan.uploaded ?? []);
|
|
37
|
+
const sent = new Map();
|
|
38
|
+
for (const n of arrived) {
|
|
39
|
+
const [a, b] = bounds(n);
|
|
40
|
+
sent.set(n, b - a);
|
|
41
|
+
}
|
|
42
|
+
const report = () => {
|
|
43
|
+
if (task.cancelled)
|
|
44
|
+
return;
|
|
45
|
+
let total = 0;
|
|
46
|
+
for (const v of sent.values())
|
|
47
|
+
total += v;
|
|
48
|
+
task.emit('uploading', Math.min(total, plan.size));
|
|
49
|
+
};
|
|
50
|
+
report();
|
|
51
|
+
const todo = [];
|
|
52
|
+
for (let n = 1; n <= count; n++)
|
|
53
|
+
if (!arrived.has(n))
|
|
54
|
+
todo.push(n);
|
|
55
|
+
// Links, asked for a few at a time -- one request for parts in flight at
|
|
56
|
+
// once, not one each -- and renewed when one has expired.
|
|
57
|
+
const links = new Map();
|
|
58
|
+
const asking = new Map();
|
|
59
|
+
const ask = (parts) => {
|
|
60
|
+
const p = api.links(parts).then((got) => {
|
|
61
|
+
for (const [k, v] of Object.entries(got))
|
|
62
|
+
links.set(Number(k), v);
|
|
63
|
+
});
|
|
64
|
+
for (const n of parts)
|
|
65
|
+
asking.set(n, p);
|
|
66
|
+
return p.finally(() => {
|
|
67
|
+
for (const n of parts)
|
|
68
|
+
if (asking.get(n) === p)
|
|
69
|
+
asking.delete(n);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const linkFor = async (n, fresh = false) => {
|
|
73
|
+
if (fresh) {
|
|
74
|
+
links.delete(n);
|
|
75
|
+
await ask([n]);
|
|
76
|
+
}
|
|
77
|
+
else if (!links.has(n)) {
|
|
78
|
+
if (asking.has(n))
|
|
79
|
+
await asking.get(n);
|
|
80
|
+
else {
|
|
81
|
+
const from = Math.max(todo.indexOf(n), 0);
|
|
82
|
+
await ask(todo.slice(from, from + LINKS_AT_ONCE).filter((p) => p === n || (!links.has(p) && !asking.has(p))));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const url = links.get(n);
|
|
86
|
+
if (!url)
|
|
87
|
+
throw new storage_errors_js_1.KoolbaseStorageError(`No upload link for part ${n}`);
|
|
88
|
+
return url;
|
|
89
|
+
};
|
|
90
|
+
// Every transfer in flight, so a cancel stops them all.
|
|
91
|
+
const flying = new Set();
|
|
92
|
+
task.setAbort(() => {
|
|
93
|
+
for (const t of flying)
|
|
94
|
+
t.abort();
|
|
95
|
+
});
|
|
96
|
+
const sendOne = async (n) => {
|
|
97
|
+
const [a, b] = bounds(n);
|
|
98
|
+
const body = file.slice(a, b);
|
|
99
|
+
let fresh = false;
|
|
100
|
+
for (let attempt = 1;; attempt++) {
|
|
101
|
+
task.checkCancelled();
|
|
102
|
+
const url = await linkFor(n, fresh);
|
|
103
|
+
fresh = false;
|
|
104
|
+
task.checkCancelled();
|
|
105
|
+
sent.set(n, 0);
|
|
106
|
+
report();
|
|
107
|
+
const t = (0, storage_transfer_js_1.transferBytes)(url, body, null, (s) => {
|
|
108
|
+
sent.set(n, s);
|
|
109
|
+
report();
|
|
110
|
+
});
|
|
111
|
+
flying.add(t);
|
|
112
|
+
let status = 0;
|
|
113
|
+
let failure = null;
|
|
114
|
+
try {
|
|
115
|
+
status = (await t.done).status;
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
failure = e;
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
flying.delete(t);
|
|
122
|
+
}
|
|
123
|
+
task.checkCancelled();
|
|
124
|
+
if (failure === null && status >= 200 && status < 300) {
|
|
125
|
+
sent.set(n, b - a);
|
|
126
|
+
report();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (failure instanceof storage_errors_js_1.KoolbaseUploadCancelledError)
|
|
130
|
+
throw failure;
|
|
131
|
+
sent.set(n, 0);
|
|
132
|
+
report();
|
|
133
|
+
const again = failure !== null || passing(status) || status === 403;
|
|
134
|
+
if (!again || attempt >= exports.PART_TRIES) {
|
|
135
|
+
throw failure ?? new storage_errors_js_1.KoolbaseStorageError(`Upload of part ${n} failed: ${status}`);
|
|
136
|
+
}
|
|
137
|
+
// An expired link (403) is renewed; anything else waits and tries again.
|
|
138
|
+
if (status === 403)
|
|
139
|
+
fresh = true;
|
|
140
|
+
else
|
|
141
|
+
await sleep(exports.partRetry.delayMs(attempt));
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
try {
|
|
145
|
+
let next = 0;
|
|
146
|
+
const worker = async () => {
|
|
147
|
+
while (next < todo.length) {
|
|
148
|
+
const n = todo[next++];
|
|
149
|
+
await sendOne(n);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
await Promise.all(Array.from({ length: Math.min(exports.PARTS_AT_ONCE, todo.length) }, worker));
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
// One part failing stops the rest; the parts that arrived stay, for a
|
|
156
|
+
// resume -- unless the upload was cancelled, which abandons it.
|
|
157
|
+
for (const t of flying)
|
|
158
|
+
t.abort();
|
|
159
|
+
if (task.cancelled) {
|
|
160
|
+
await api.abort().catch(() => undefined);
|
|
161
|
+
throw new storage_errors_js_1.KoolbaseUploadCancelledError();
|
|
162
|
+
}
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
task.setAbort(null);
|
|
167
|
+
}
|
|
168
|
+
}
|