@motherbase/client 1.2.8 → 1.4.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/README.md +73 -9
- package/dist/auth.d.ts +172 -14
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +399 -14
- package/dist/auth.js.map +1 -1
- package/dist/channel.d.ts +15 -12
- package/dist/channel.d.ts.map +1 -1
- package/dist/channel.js.map +1 -1
- package/dist/compat.d.ts +40 -0
- package/dist/compat.d.ts.map +1 -0
- package/dist/compat.js +98 -0
- package/dist/compat.js.map +1 -0
- package/dist/cookie.d.ts +23 -2
- package/dist/cookie.d.ts.map +1 -1
- package/dist/cookie.js +71 -9
- package/dist/cookie.js.map +1 -1
- package/dist/database.d.ts +20 -1
- package/dist/database.d.ts.map +1 -1
- package/dist/functions.d.ts +36 -0
- package/dist/functions.d.ts.map +1 -1
- package/dist/functions.js +56 -5
- package/dist/functions.js.map +1 -1
- package/dist/http.d.ts +47 -7
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +73 -12
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +79 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +183 -11
- package/dist/index.js.map +1 -1
- package/dist/postgrest.d.ts +94 -1
- package/dist/postgrest.d.ts.map +1 -1
- package/dist/postgrest.js +185 -17
- package/dist/postgrest.js.map +1 -1
- package/dist/query.d.ts +97 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +94 -0
- package/dist/query.js.map +1 -0
- package/dist/session.d.ts +19 -1
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +57 -4
- package/dist/session.js.map +1 -1
- package/dist/storage.d.ts +172 -5
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +249 -6
- package/dist/storage.js.map +1 -1
- package/dist/testing.d.ts +38 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +50 -0
- package/dist/testing.js.map +1 -0
- package/dist/types.d.ts +264 -20
- package/dist/types.d.ts.map +1 -1
- package/dist/warn.d.ts +4 -0
- package/dist/warn.d.ts.map +1 -0
- package/dist/warn.js +27 -0
- package/dist/warn.js.map +1 -0
- package/package.json +60 -52
package/dist/session.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import { normalizeSession } from './compat.js';
|
|
2
|
+
/**
|
|
3
|
+
* The key every built-in storage adapter defaults to, for both the cookie and
|
|
4
|
+
* localStorage. Defined once: it is a contract between the browser half of an
|
|
5
|
+
* app and the server half, and a second copy of the literal is how the two
|
|
6
|
+
* drift apart. Re-exported from `cookie.ts` and from the package root.
|
|
7
|
+
*/
|
|
8
|
+
export const SESSION_COOKIE_KEY = 'motherbase.session';
|
|
1
9
|
/** In-memory adapter. Safe everywhere (SSR, tests); does not survive reloads. */
|
|
2
10
|
export function memoryStorage() {
|
|
3
11
|
let current = null;
|
|
@@ -12,7 +20,7 @@ export function memoryStorage() {
|
|
|
12
20
|
* Browser localStorage adapter. Falls back to memory when localStorage is
|
|
13
21
|
* unavailable (SSR, private mode), so it never throws on import or use.
|
|
14
22
|
*/
|
|
15
|
-
export function localStorageStorage(key =
|
|
23
|
+
export function localStorageStorage(key = SESSION_COOKIE_KEY) {
|
|
16
24
|
const ls = typeof globalThis !== 'undefined' ? globalThis.localStorage : undefined;
|
|
17
25
|
if (!ls)
|
|
18
26
|
return memoryStorage();
|
|
@@ -36,6 +44,39 @@ export function localStorageStorage(key = 'motherbase.session') {
|
|
|
36
44
|
},
|
|
37
45
|
};
|
|
38
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Wrap a supabase-js style key/value store (localStorage, React Native's
|
|
49
|
+
* AsyncStorage, a cookie shim) as a session adapter.
|
|
50
|
+
*
|
|
51
|
+
* This is what lets `createClient(url, key, { auth: { storage: AsyncStorage } })`
|
|
52
|
+
* work verbatim on a ported app. A value that will not parse is treated as no
|
|
53
|
+
* session rather than thrown: a corrupt entry must not brick every launch.
|
|
54
|
+
*/
|
|
55
|
+
export function keyValueStorage(store, key = SESSION_COOKIE_KEY) {
|
|
56
|
+
return {
|
|
57
|
+
get: async () => {
|
|
58
|
+
const raw = await store.getItem(key);
|
|
59
|
+
if (!raw)
|
|
60
|
+
return null;
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(raw);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
set: async (s) => {
|
|
69
|
+
if (s)
|
|
70
|
+
await store.setItem(key, JSON.stringify(s));
|
|
71
|
+
else
|
|
72
|
+
await store.removeItem(key);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Tell the two adapter shapes apart: supabase-js's is a key/value store. */
|
|
77
|
+
export function isKeyValueStorage(value) {
|
|
78
|
+
return typeof value.getItem === 'function';
|
|
79
|
+
}
|
|
39
80
|
/**
|
|
40
81
|
* Holds the live session in memory, mirrors it to a storage adapter, and
|
|
41
82
|
* notifies listeners of auth changes. The in-memory copy is the source of truth
|
|
@@ -52,7 +93,19 @@ export class SessionStore {
|
|
|
52
93
|
async hydrate() {
|
|
53
94
|
if (this.hydrated)
|
|
54
95
|
return this.current;
|
|
55
|
-
|
|
96
|
+
// A session read back from storage was written by an older version (or by
|
|
97
|
+
// the server half of an SSR app), so it is normalised on the way in too:
|
|
98
|
+
// the aliases must not depend on which code path produced the session.
|
|
99
|
+
const loaded = await this.storage.get();
|
|
100
|
+
// Re-check AFTER the await. `hydrate()` is started but not awaited by
|
|
101
|
+
// `onAuthStateChange`, so a `set()` can land while this one is still
|
|
102
|
+
// reading storage, and `loaded` is then a snapshot from before that write.
|
|
103
|
+
// Assigning it anyway is a lost update, and it presents as the opposite of
|
|
104
|
+
// a storage bug: `setSession()` resolves with "Invalid session" because the
|
|
105
|
+
// token it just stored was wiped out from under the request that used it.
|
|
106
|
+
if (this.hydrated)
|
|
107
|
+
return this.current;
|
|
108
|
+
this.current = normalizeSession(loaded);
|
|
56
109
|
this.hydrated = true;
|
|
57
110
|
return this.current;
|
|
58
111
|
}
|
|
@@ -60,9 +113,9 @@ export class SessionStore {
|
|
|
60
113
|
return this.current;
|
|
61
114
|
}
|
|
62
115
|
async set(session, event) {
|
|
63
|
-
this.current = session;
|
|
116
|
+
this.current = normalizeSession(session);
|
|
64
117
|
this.hydrated = true;
|
|
65
|
-
await this.storage.set(
|
|
118
|
+
await this.storage.set(this.current);
|
|
66
119
|
this.emit(event);
|
|
67
120
|
}
|
|
68
121
|
/** Replace only the tokens, keeping the existing user (used after refresh). */
|
package/dist/session.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAS/C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAC;AAEvD,iFAAiF;AACjF,MAAM,UAAU,aAAa;IAC3B,IAAI,OAAO,GAAmB,IAAI,CAAC;IACnC,OAAO;QACL,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO;QAClB,GAAG,EAAE,CAAC,CAAC,EAAE,EAAE;YACT,OAAO,GAAG,CAAC,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAG,GAAG,kBAAkB;IAC1D,MAAM,EAAE,GAAG,OAAO,UAAU,KAAK,WAAW,CAAC,CAAC,CAAE,UAAyC,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC;IACnH,IAAI,CAAC,EAAE;QAAE,OAAO,aAAa,EAAE,CAAC;IAChC,OAAO;QACL,GAAG,EAAE,GAAG,EAAE;YACR,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,C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EAAE,GAAG,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,EAAE,gBAAgB,CAAC,CAAC;IACnF,CAAC;IAED,SAAS,CAAC,QAAsB;QAC9B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC7B,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/C,CAAC;IAEO,IAAI,CAAC,KAAgB;QAC3B,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,SAAS;YAAE,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACzD,CAAC;CACF"}
|
package/dist/storage.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { FileObject, FileVisibility, MotherbaseResponse, StorageRemoveRespo
|
|
|
3
3
|
export interface UploadOptions {
|
|
4
4
|
/** Object visibility. Defaults to the backend's default (private). */
|
|
5
5
|
visibility?: FileVisibility;
|
|
6
|
-
/**
|
|
6
|
+
/** Content type stored with the object. Overrides what the file itself declares. */
|
|
7
7
|
contentType?: string;
|
|
8
8
|
upsert?: boolean;
|
|
9
9
|
}
|
|
@@ -15,6 +15,29 @@ export interface UploadResult {
|
|
|
15
15
|
/** `bucket/key`. */
|
|
16
16
|
fullPath: string;
|
|
17
17
|
}
|
|
18
|
+
/** A bucket, in the supabase-js shape. `id` is the slug. */
|
|
19
|
+
export interface BucketInfo {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
owner: string;
|
|
23
|
+
public: boolean;
|
|
24
|
+
created_at: string | null;
|
|
25
|
+
updated_at: string | null;
|
|
26
|
+
}
|
|
27
|
+
/** One object's metadata, in the supabase-js `info()` shape. */
|
|
28
|
+
export interface ObjectInfo {
|
|
29
|
+
name: string;
|
|
30
|
+
id: string;
|
|
31
|
+
version: string | null;
|
|
32
|
+
bucket_id: string;
|
|
33
|
+
created_at: string | null;
|
|
34
|
+
updated_at: string | null;
|
|
35
|
+
last_accessed_at: string | null;
|
|
36
|
+
size: number;
|
|
37
|
+
content_type: string | null;
|
|
38
|
+
cache_control: string | null;
|
|
39
|
+
metadata: Record<string, unknown>;
|
|
40
|
+
}
|
|
18
41
|
export interface ListOptions {
|
|
19
42
|
limit?: number;
|
|
20
43
|
offset?: number;
|
|
@@ -28,7 +51,11 @@ export declare class StorageBucket {
|
|
|
28
51
|
private readonly http;
|
|
29
52
|
private readonly baseUrl;
|
|
30
53
|
private readonly bucket;
|
|
31
|
-
|
|
54
|
+
/** Used only by `uploadToSignedUrl`, which PUTs straight to object storage. */
|
|
55
|
+
private readonly fetchImpl;
|
|
56
|
+
constructor(http: HttpClient, baseUrl: string, bucket: string,
|
|
57
|
+
/** Used only by `uploadToSignedUrl`, which PUTs straight to object storage. */
|
|
58
|
+
fetchImpl: typeof fetch);
|
|
32
59
|
/**
|
|
33
60
|
* Route for one object. A bare uuid is treated as a file id (the pre-path
|
|
34
61
|
* addressing form, which existing callers have stored); anything else is a
|
|
@@ -36,6 +63,41 @@ export declare class StorageBucket {
|
|
|
36
63
|
*/
|
|
37
64
|
private objectPath;
|
|
38
65
|
upload(path: string, file: UploadInput, options?: UploadOptions): Promise<MotherbaseResponse<UploadResult>>;
|
|
66
|
+
/**
|
|
67
|
+
* Upload several objects, a few at a time, reporting progress per file.
|
|
68
|
+
*
|
|
69
|
+
* Uploading a folder is the common case and `Promise.all` is the common
|
|
70
|
+
* mistake: a hundred parallel multipart posts saturate the connection, and
|
|
71
|
+
* the server rate limiter answers 429 to the tail of them. Four at a time
|
|
72
|
+
* finishes sooner in practice and fails less.
|
|
73
|
+
*
|
|
74
|
+
* One failing file does not cancel the rest. The result reports every entry
|
|
75
|
+
* in the order it was given, each with its own error or `null`, so the caller
|
|
76
|
+
* can retry exactly what failed.
|
|
77
|
+
*
|
|
78
|
+
* ponytail: progress is per file, not per byte. Byte-level progress needs
|
|
79
|
+
* XMLHttpRequest, which does not exist outside a browser; if a progress bar
|
|
80
|
+
* has to move within one large file, that is the upgrade path.
|
|
81
|
+
*/
|
|
82
|
+
uploadMany(files: Array<{
|
|
83
|
+
path: string;
|
|
84
|
+
file: UploadInput;
|
|
85
|
+
options?: UploadOptions;
|
|
86
|
+
}>, options?: {
|
|
87
|
+
/** Uploads in flight at once. Default 4. */
|
|
88
|
+
concurrency?: number;
|
|
89
|
+
/** Called after each file settles, with how many have settled so far. */
|
|
90
|
+
onProgress?: (progress: {
|
|
91
|
+
completed: number;
|
|
92
|
+
total: number;
|
|
93
|
+
path: string;
|
|
94
|
+
error: string | null;
|
|
95
|
+
}) => void;
|
|
96
|
+
}): Promise<MotherbaseResponse<Array<{
|
|
97
|
+
path: string;
|
|
98
|
+
data: UploadResult | null;
|
|
99
|
+
error: string | null;
|
|
100
|
+
}>>>;
|
|
39
101
|
download(path: string): Promise<MotherbaseResponse<Blob>>;
|
|
40
102
|
remove(paths: string[]): Promise<MotherbaseResponse<StorageRemoveResponse[]>>;
|
|
41
103
|
/** Objects under a prefix, in the Supabase response shape. */
|
|
@@ -48,25 +110,130 @@ export declare class StorageBucket {
|
|
|
48
110
|
};
|
|
49
111
|
/**
|
|
50
112
|
* A download URL for the object, valid for `expiresIn` seconds (default 3600,
|
|
51
|
-
* maximum 7 days
|
|
113
|
+
* maximum 7 days: the AWS SigV4 ceiling; anything outside that is a `400`).
|
|
52
114
|
*
|
|
53
115
|
* `expiresAt` reports when the URL actually stops working, and is `null` when
|
|
54
116
|
* nothing expires: a public object's URL is permanent, and a private object on
|
|
55
117
|
* the `local` storage driver resolves to the authenticated streaming endpoint,
|
|
56
118
|
* which is bound to the session rather than to a clock and so cannot be handed
|
|
57
|
-
* to a third party at all. Check it rather than assuming `expiresIn` applied
|
|
119
|
+
* to a third party at all. Check it rather than assuming `expiresIn` applied,
|
|
58
120
|
* that assumption is exactly what this used to get wrong, silently.
|
|
59
121
|
*/
|
|
60
122
|
createSignedUrl(path: string, expiresIn?: number): Promise<MotherbaseResponse<{
|
|
61
123
|
signedUrl: string;
|
|
62
124
|
expiresAt: string | null;
|
|
63
125
|
}>>;
|
|
126
|
+
/**
|
|
127
|
+
* Sign several objects in one call.
|
|
128
|
+
*
|
|
129
|
+
* There is no batch endpoint behind this: it signs each key and reports per
|
|
130
|
+
* entry, exactly as supabase-js does, so one missing object yields one entry
|
|
131
|
+
* with an `error` rather than failing the whole batch.
|
|
132
|
+
*/
|
|
133
|
+
createSignedUrls(paths: string[], expiresIn?: number): Promise<MotherbaseResponse<Array<{
|
|
134
|
+
path: string;
|
|
135
|
+
signedUrl: string | null;
|
|
136
|
+
error: string | null;
|
|
137
|
+
}>>>;
|
|
138
|
+
/**
|
|
139
|
+
* Replace the object at `path`. In supabase-js `update` is `upload` with
|
|
140
|
+
* upsert forced on, and the same is true here.
|
|
141
|
+
*/
|
|
142
|
+
update(path: string, file: UploadInput, options?: UploadOptions): Promise<MotherbaseResponse<UploadResult>>;
|
|
143
|
+
/** Copy an object to another key in this bucket. */
|
|
144
|
+
copy(from: string, to: string, options?: {
|
|
145
|
+
upsert?: boolean;
|
|
146
|
+
}): Promise<MotherbaseResponse<{
|
|
147
|
+
path: string;
|
|
148
|
+
}>>;
|
|
149
|
+
/** Move an object to another key in this bucket (copy, then delete the source). */
|
|
150
|
+
move(from: string, to: string, options?: {
|
|
151
|
+
upsert?: boolean;
|
|
152
|
+
}): Promise<MotherbaseResponse<{
|
|
153
|
+
path: string;
|
|
154
|
+
}>>;
|
|
155
|
+
private transfer;
|
|
156
|
+
/** One object's metadata, without downloading it. */
|
|
157
|
+
info(path: string): Promise<MotherbaseResponse<ObjectInfo>>;
|
|
158
|
+
/**
|
|
159
|
+
* Whether an object exists and this caller can read it.
|
|
160
|
+
*
|
|
161
|
+
* A 404 is the answer `false`, not a failure. Anything else (a 403, a network
|
|
162
|
+
* error) is reported as an error rather than as "no": treating an
|
|
163
|
+
* authorization failure as absence is how a UI ends up offering to create
|
|
164
|
+
* something that is already there.
|
|
165
|
+
*/
|
|
166
|
+
exists(path: string): Promise<MotherbaseResponse<boolean>>;
|
|
167
|
+
/**
|
|
168
|
+
* Mint a URL the browser can PUT the bytes to directly, skipping this server.
|
|
169
|
+
*
|
|
170
|
+
* Unlike supabase-js this needs the file's `size` and content type up front:
|
|
171
|
+
* the upload is reserved against the bucket quota and the signature is bound
|
|
172
|
+
* to that exact length, which is what stops a signed URL from becoming an
|
|
173
|
+
* unbounded write. Pair it with `uploadToSignedUrl`.
|
|
174
|
+
*
|
|
175
|
+
* Requires the S3 driver; the local driver has no presigned uploads.
|
|
176
|
+
*/
|
|
177
|
+
createSignedUploadUrl(path: string, options: {
|
|
178
|
+
size: number;
|
|
179
|
+
contentType?: string;
|
|
180
|
+
visibility?: FileVisibility;
|
|
181
|
+
}): Promise<MotherbaseResponse<{
|
|
182
|
+
signedUrl: string;
|
|
183
|
+
token: string;
|
|
184
|
+
path: string;
|
|
185
|
+
}>>;
|
|
186
|
+
/**
|
|
187
|
+
* Upload to a URL from `createSignedUploadUrl`: PUT the bytes to storage,
|
|
188
|
+
* then tell the server the object landed. The finalise step is what makes the
|
|
189
|
+
* row readable, so a PUT that succeeds and a finalise that fails leaves the
|
|
190
|
+
* reservation to expire rather than a half-visible object.
|
|
191
|
+
*/
|
|
192
|
+
uploadToSignedUrl(path: string, token: string, file: Blob, options?: {
|
|
193
|
+
contentType?: string;
|
|
194
|
+
}): Promise<MotherbaseResponse<UploadResult>>;
|
|
64
195
|
}
|
|
65
196
|
/** Supabase-compatible storage client: `client.storage.from(bucket)`. */
|
|
66
197
|
export declare class StorageClient {
|
|
67
198
|
private readonly http;
|
|
68
199
|
private readonly baseUrl;
|
|
69
|
-
|
|
200
|
+
private readonly fetchImpl;
|
|
201
|
+
constructor(http: HttpClient, baseUrl: string, fetchImpl: typeof fetch);
|
|
70
202
|
from(bucket: string): StorageBucket;
|
|
203
|
+
/**
|
|
204
|
+
* Bucket administration, supabase-js shaped.
|
|
205
|
+
*
|
|
206
|
+
* Every method here needs a SECRET project API key, which is the same rule
|
|
207
|
+
* Supabase applies (bucket administration is a service-role operation). With
|
|
208
|
+
* a publishable key they resolve with a 401 error, so keep them in server
|
|
209
|
+
* code and setup scripts, never in a browser bundle.
|
|
210
|
+
*/
|
|
211
|
+
listBuckets(): Promise<MotherbaseResponse<BucketInfo[]>>;
|
|
212
|
+
getBucket(id: string): Promise<MotherbaseResponse<BucketInfo>>;
|
|
213
|
+
createBucket(id: string, options?: {
|
|
214
|
+
public?: boolean;
|
|
215
|
+
name?: string;
|
|
216
|
+
}): Promise<MotherbaseResponse<BucketInfo>>;
|
|
217
|
+
updateBucket(id: string, options: {
|
|
218
|
+
public?: boolean;
|
|
219
|
+
name?: string;
|
|
220
|
+
}): Promise<MotherbaseResponse<BucketInfo>>;
|
|
221
|
+
/** Delete every object in the bucket, keeping the bucket itself. */
|
|
222
|
+
emptyBucket(id: string): Promise<MotherbaseResponse<{
|
|
223
|
+
emptied: boolean;
|
|
224
|
+
removed: number;
|
|
225
|
+
}>>;
|
|
226
|
+
/**
|
|
227
|
+
* Delete the bucket. A bucket that still holds objects is a `409` unless
|
|
228
|
+
* `force` is set, which queues a background deletion of its contents: an
|
|
229
|
+
* accidental `deleteBucket` must not be able to destroy a project's files in
|
|
230
|
+
* one unqualified call.
|
|
231
|
+
*/
|
|
232
|
+
deleteBucket(id: string, options?: {
|
|
233
|
+
force?: boolean;
|
|
234
|
+
}): Promise<MotherbaseResponse<{
|
|
235
|
+
deleted: boolean;
|
|
236
|
+
}>>;
|
|
237
|
+
private bucketCall;
|
|
71
238
|
}
|
|
72
239
|
//# sourceMappingURL=storage.d.ts.map
|
package/dist/storage.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,kBAAk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AAE,MAAM,EAAE,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,kBAAkB,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;YAatG,UAAU;CAQzB"}
|
package/dist/storage.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { toError } from './http.js';
|
|
2
2
|
import { HTTP_ROUTES, contractPath } from '@motherbase/contracts';
|
|
3
|
+
/** Bucket administration lives one level up from an object route. */
|
|
4
|
+
const BUCKET_ROUTE = `${HTTP_ROUTES.files}/bucket`;
|
|
3
5
|
/**
|
|
4
6
|
* Supabase-compatible storage bucket handle over the authenticated
|
|
5
7
|
* `/api/files` endpoints.
|
|
@@ -11,10 +13,13 @@ import { HTTP_ROUTES, contractPath } from '@motherbase/contracts';
|
|
|
11
13
|
*/
|
|
12
14
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
13
15
|
export class StorageBucket {
|
|
14
|
-
constructor(http, baseUrl, bucket
|
|
16
|
+
constructor(http, baseUrl, bucket,
|
|
17
|
+
/** Used only by `uploadToSignedUrl`, which PUTs straight to object storage. */
|
|
18
|
+
fetchImpl) {
|
|
15
19
|
this.http = http;
|
|
16
20
|
this.baseUrl = baseUrl;
|
|
17
21
|
this.bucket = bucket;
|
|
22
|
+
this.fetchImpl = fetchImpl;
|
|
18
23
|
}
|
|
19
24
|
/**
|
|
20
25
|
* Route for one object. A bare uuid is treated as a file id (the pre-path
|
|
@@ -23,6 +28,9 @@ export class StorageBucket {
|
|
|
23
28
|
*/
|
|
24
29
|
objectPath(pathOrId, kind) {
|
|
25
30
|
if (UUID.test(pathOrId)) {
|
|
31
|
+
if (kind === 'object-info') {
|
|
32
|
+
throw new Error('info() addresses objects by path. Pass the key you uploaded under, not a file id.');
|
|
33
|
+
}
|
|
26
34
|
return contractPath.file(pathOrId, kind === 'object-url' ? 'url' : 'raw');
|
|
27
35
|
}
|
|
28
36
|
const key = pathOrId.split('/').map(encodeURIComponent).join('/');
|
|
@@ -32,7 +40,13 @@ export class StorageBucket {
|
|
|
32
40
|
try {
|
|
33
41
|
const form = new FormData();
|
|
34
42
|
if (typeof Blob !== 'undefined' && file instanceof Blob) {
|
|
35
|
-
|
|
43
|
+
// `contentType` used to be documented as a hint and then dropped on the
|
|
44
|
+
// floor: the server reads the mime type off the multipart part, so the
|
|
45
|
+
// only way to honour it is to retype the blob. `slice()` with a type is
|
|
46
|
+
// the retyping that does not copy the bytes, which matters at upload
|
|
47
|
+
// sizes.
|
|
48
|
+
const body = options.contentType ? file.slice(0, file.size, options.contentType) : file;
|
|
49
|
+
form.append('file', body, path);
|
|
36
50
|
}
|
|
37
51
|
else {
|
|
38
52
|
form.append('file', file);
|
|
@@ -57,6 +71,45 @@ export class StorageBucket {
|
|
|
57
71
|
return { data: null, error: toError(err) };
|
|
58
72
|
}
|
|
59
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Upload several objects, a few at a time, reporting progress per file.
|
|
76
|
+
*
|
|
77
|
+
* Uploading a folder is the common case and `Promise.all` is the common
|
|
78
|
+
* mistake: a hundred parallel multipart posts saturate the connection, and
|
|
79
|
+
* the server rate limiter answers 429 to the tail of them. Four at a time
|
|
80
|
+
* finishes sooner in practice and fails less.
|
|
81
|
+
*
|
|
82
|
+
* One failing file does not cancel the rest. The result reports every entry
|
|
83
|
+
* in the order it was given, each with its own error or `null`, so the caller
|
|
84
|
+
* can retry exactly what failed.
|
|
85
|
+
*
|
|
86
|
+
* ponytail: progress is per file, not per byte. Byte-level progress needs
|
|
87
|
+
* XMLHttpRequest, which does not exist outside a browser; if a progress bar
|
|
88
|
+
* has to move within one large file, that is the upgrade path.
|
|
89
|
+
*/
|
|
90
|
+
async uploadMany(files, options = {}) {
|
|
91
|
+
const total = files.length;
|
|
92
|
+
const concurrency = Math.max(1, Math.min(options.concurrency ?? 4, total || 1));
|
|
93
|
+
const results = new Array(total);
|
|
94
|
+
let next = 0;
|
|
95
|
+
let completed = 0;
|
|
96
|
+
// A shared cursor rather than fixed slices: one slow file would otherwise
|
|
97
|
+
// hold up its whole slice while other workers sit idle.
|
|
98
|
+
const worker = async () => {
|
|
99
|
+
for (;;) {
|
|
100
|
+
const index = next++;
|
|
101
|
+
if (index >= total)
|
|
102
|
+
return;
|
|
103
|
+
const entry = files[index];
|
|
104
|
+
const { data, error } = await this.upload(entry.path, entry.file, entry.options);
|
|
105
|
+
results[index] = { path: entry.path, data: data ?? null, error: error?.message ?? null };
|
|
106
|
+
completed += 1;
|
|
107
|
+
options.onProgress?.({ completed, total, path: entry.path, error: error?.message ?? null });
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
await Promise.all(Array.from({ length: concurrency }, worker));
|
|
111
|
+
return { data: results, error: null };
|
|
112
|
+
}
|
|
60
113
|
async download(path) {
|
|
61
114
|
try {
|
|
62
115
|
const res = await this.http.requestRaw(this.objectPath(path, 'object'));
|
|
@@ -115,13 +168,13 @@ export class StorageBucket {
|
|
|
115
168
|
}
|
|
116
169
|
/**
|
|
117
170
|
* A download URL for the object, valid for `expiresIn` seconds (default 3600,
|
|
118
|
-
* maximum 7 days
|
|
171
|
+
* maximum 7 days: the AWS SigV4 ceiling; anything outside that is a `400`).
|
|
119
172
|
*
|
|
120
173
|
* `expiresAt` reports when the URL actually stops working, and is `null` when
|
|
121
174
|
* nothing expires: a public object's URL is permanent, and a private object on
|
|
122
175
|
* the `local` storage driver resolves to the authenticated streaming endpoint,
|
|
123
176
|
* which is bound to the session rather than to a clock and so cannot be handed
|
|
124
|
-
* to a third party at all. Check it rather than assuming `expiresIn` applied
|
|
177
|
+
* to a third party at all. Check it rather than assuming `expiresIn` applied,
|
|
125
178
|
* that assumption is exactly what this used to get wrong, silently.
|
|
126
179
|
*/
|
|
127
180
|
async createSignedUrl(path, expiresIn) {
|
|
@@ -134,15 +187,205 @@ export class StorageBucket {
|
|
|
134
187
|
return { data: null, error: toError(err) };
|
|
135
188
|
}
|
|
136
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Sign several objects in one call.
|
|
192
|
+
*
|
|
193
|
+
* There is no batch endpoint behind this: it signs each key and reports per
|
|
194
|
+
* entry, exactly as supabase-js does, so one missing object yields one entry
|
|
195
|
+
* with an `error` rather than failing the whole batch.
|
|
196
|
+
*/
|
|
197
|
+
async createSignedUrls(paths, expiresIn) {
|
|
198
|
+
const results = await Promise.all(paths.map(async (path) => {
|
|
199
|
+
const { data, error } = await this.createSignedUrl(path, expiresIn);
|
|
200
|
+
return { path, signedUrl: data?.signedUrl ?? null, error: error?.message ?? null };
|
|
201
|
+
}));
|
|
202
|
+
return { data: results, error: null };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Replace the object at `path`. In supabase-js `update` is `upload` with
|
|
206
|
+
* upsert forced on, and the same is true here.
|
|
207
|
+
*/
|
|
208
|
+
async update(path, file, options = {}) {
|
|
209
|
+
return this.upload(path, file, { ...options, upsert: true });
|
|
210
|
+
}
|
|
211
|
+
/** Copy an object to another key in this bucket. */
|
|
212
|
+
async copy(from, to, options = {}) {
|
|
213
|
+
return this.transfer('copy', from, to, options);
|
|
214
|
+
}
|
|
215
|
+
/** Move an object to another key in this bucket (copy, then delete the source). */
|
|
216
|
+
async move(from, to, options = {}) {
|
|
217
|
+
return this.transfer('move', from, to, options);
|
|
218
|
+
}
|
|
219
|
+
async transfer(action, from, to, options) {
|
|
220
|
+
try {
|
|
221
|
+
const res = await this.http.request(`${HTTP_ROUTES.files}/${action}`, { method: 'POST', body: { bucket: this.bucket, from, to, upsert: options.upsert === true } });
|
|
222
|
+
return { data: { path: res.fullPath ?? `${this.bucket}/${to}` }, error: null };
|
|
223
|
+
}
|
|
224
|
+
catch (err) {
|
|
225
|
+
return { data: null, error: toError(err) };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** One object's metadata, without downloading it. */
|
|
229
|
+
async info(path) {
|
|
230
|
+
try {
|
|
231
|
+
const res = await this.http.request(this.objectPath(path, 'object-info'));
|
|
232
|
+
return { data: res, error: null };
|
|
233
|
+
}
|
|
234
|
+
catch (err) {
|
|
235
|
+
return { data: null, error: toError(err) };
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Whether an object exists and this caller can read it.
|
|
240
|
+
*
|
|
241
|
+
* A 404 is the answer `false`, not a failure. Anything else (a 403, a network
|
|
242
|
+
* error) is reported as an error rather than as "no": treating an
|
|
243
|
+
* authorization failure as absence is how a UI ends up offering to create
|
|
244
|
+
* something that is already there.
|
|
245
|
+
*/
|
|
246
|
+
async exists(path) {
|
|
247
|
+
const { data, error } = await this.info(path);
|
|
248
|
+
if (data)
|
|
249
|
+
return { data: true, error: null };
|
|
250
|
+
if (error?.status === 404)
|
|
251
|
+
return { data: false, error: null };
|
|
252
|
+
return { data: null, error: error ?? { message: 'Unknown storage error' } };
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Mint a URL the browser can PUT the bytes to directly, skipping this server.
|
|
256
|
+
*
|
|
257
|
+
* Unlike supabase-js this needs the file's `size` and content type up front:
|
|
258
|
+
* the upload is reserved against the bucket quota and the signature is bound
|
|
259
|
+
* to that exact length, which is what stops a signed URL from becoming an
|
|
260
|
+
* unbounded write. Pair it with `uploadToSignedUrl`.
|
|
261
|
+
*
|
|
262
|
+
* Requires the S3 driver; the local driver has no presigned uploads.
|
|
263
|
+
*/
|
|
264
|
+
async createSignedUploadUrl(path, options) {
|
|
265
|
+
try {
|
|
266
|
+
const res = await this.http.request(`${HTTP_ROUTES.files}/presign`, {
|
|
267
|
+
method: 'POST',
|
|
268
|
+
body: {
|
|
269
|
+
bucket: this.bucket,
|
|
270
|
+
path,
|
|
271
|
+
size: options.size,
|
|
272
|
+
mime: options.contentType,
|
|
273
|
+
visibility: options.visibility,
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
// supabase-js's `token` is opaque to the caller and is handed straight
|
|
277
|
+
// back to uploadToSignedUrl, so it carries what finalisation needs.
|
|
278
|
+
const token = JSON.stringify({ url: res.uploadUrl, id: res.file.id });
|
|
279
|
+
return { data: { signedUrl: res.uploadUrl, token, path }, error: null };
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
return { data: null, error: toError(err) };
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Upload to a URL from `createSignedUploadUrl`: PUT the bytes to storage,
|
|
287
|
+
* then tell the server the object landed. The finalise step is what makes the
|
|
288
|
+
* row readable, so a PUT that succeeds and a finalise that fails leaves the
|
|
289
|
+
* reservation to expire rather than a half-visible object.
|
|
290
|
+
*/
|
|
291
|
+
async uploadToSignedUrl(path, token, file, options = {}) {
|
|
292
|
+
let parsed;
|
|
293
|
+
try {
|
|
294
|
+
parsed = JSON.parse(token);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return { data: null, error: { message: 'Invalid upload token. Use the token from createSignedUploadUrl().' } };
|
|
298
|
+
}
|
|
299
|
+
if (!parsed.url || !parsed.id) {
|
|
300
|
+
return { data: null, error: { message: 'Invalid upload token. Use the token from createSignedUploadUrl().' } };
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
const put = await this.fetchImpl(parsed.url, {
|
|
304
|
+
method: 'PUT',
|
|
305
|
+
body: file,
|
|
306
|
+
headers: { 'Content-Type': options.contentType ?? file.type ?? 'application/octet-stream' },
|
|
307
|
+
});
|
|
308
|
+
if (!put.ok) {
|
|
309
|
+
return { data: null, error: { message: `Upload failed with status ${put.status}`, status: put.status } };
|
|
310
|
+
}
|
|
311
|
+
await this.http.request(`${contractPath.file(parsed.id)}/finalize`, { method: 'POST' });
|
|
312
|
+
return { data: { path, id: parsed.id, fullPath: `${this.bucket}/${path}` }, error: null };
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
return { data: null, error: toError(err) };
|
|
316
|
+
}
|
|
317
|
+
}
|
|
137
318
|
}
|
|
138
319
|
/** Supabase-compatible storage client: `client.storage.from(bucket)`. */
|
|
139
320
|
export class StorageClient {
|
|
140
|
-
constructor(http, baseUrl) {
|
|
321
|
+
constructor(http, baseUrl, fetchImpl) {
|
|
141
322
|
this.http = http;
|
|
142
323
|
this.baseUrl = baseUrl;
|
|
324
|
+
this.fetchImpl = fetchImpl;
|
|
143
325
|
}
|
|
144
326
|
from(bucket) {
|
|
145
|
-
return new StorageBucket(this.http, this.baseUrl, bucket);
|
|
327
|
+
return new StorageBucket(this.http, this.baseUrl, bucket, this.fetchImpl);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Bucket administration, supabase-js shaped.
|
|
331
|
+
*
|
|
332
|
+
* Every method here needs a SECRET project API key, which is the same rule
|
|
333
|
+
* Supabase applies (bucket administration is a service-role operation). With
|
|
334
|
+
* a publishable key they resolve with a 401 error, so keep them in server
|
|
335
|
+
* code and setup scripts, never in a browser bundle.
|
|
336
|
+
*/
|
|
337
|
+
async listBuckets() {
|
|
338
|
+
try {
|
|
339
|
+
const res = await this.http.request(BUCKET_ROUTE);
|
|
340
|
+
return { data: res.buckets, error: null };
|
|
341
|
+
}
|
|
342
|
+
catch (err) {
|
|
343
|
+
return { data: null, error: toError(err) };
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async getBucket(id) {
|
|
347
|
+
return this.bucketCall(`${BUCKET_ROUTE}/${encodeURIComponent(id)}`);
|
|
348
|
+
}
|
|
349
|
+
async createBucket(id, options = {}) {
|
|
350
|
+
return this.bucketCall(BUCKET_ROUTE, 'POST', { id, name: options.name, public: options.public === true });
|
|
351
|
+
}
|
|
352
|
+
async updateBucket(id, options) {
|
|
353
|
+
return this.bucketCall(`${BUCKET_ROUTE}/${encodeURIComponent(id)}`, 'PUT', options);
|
|
354
|
+
}
|
|
355
|
+
/** Delete every object in the bucket, keeping the bucket itself. */
|
|
356
|
+
async emptyBucket(id) {
|
|
357
|
+
try {
|
|
358
|
+
const res = await this.http.request(`${BUCKET_ROUTE}/${encodeURIComponent(id)}/empty`, { method: 'POST' });
|
|
359
|
+
return { data: res, error: null };
|
|
360
|
+
}
|
|
361
|
+
catch (err) {
|
|
362
|
+
return { data: null, error: toError(err) };
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Delete the bucket. A bucket that still holds objects is a `409` unless
|
|
367
|
+
* `force` is set, which queues a background deletion of its contents: an
|
|
368
|
+
* accidental `deleteBucket` must not be able to destroy a project's files in
|
|
369
|
+
* one unqualified call.
|
|
370
|
+
*/
|
|
371
|
+
async deleteBucket(id, options = {}) {
|
|
372
|
+
try {
|
|
373
|
+
const query = options.force ? '?force=true' : '';
|
|
374
|
+
const res = await this.http.request(`${BUCKET_ROUTE}/${encodeURIComponent(id)}${query}`, { method: 'DELETE' });
|
|
375
|
+
return { data: res, error: null };
|
|
376
|
+
}
|
|
377
|
+
catch (err) {
|
|
378
|
+
return { data: null, error: toError(err) };
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
async bucketCall(path, method = 'GET', body) {
|
|
382
|
+
try {
|
|
383
|
+
const res = await this.http.request(path, { method, body });
|
|
384
|
+
return { data: res, error: null };
|
|
385
|
+
}
|
|
386
|
+
catch (err) {
|
|
387
|
+
return { data: null, error: toError(err) };
|
|
388
|
+
}
|
|
146
389
|
}
|
|
147
390
|
}
|
|
148
391
|
//# sourceMappingURL=storage.js.map
|