bytex-sdk 1.3.0 → 1.5.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 +56 -0
- package/index.js +673 -48
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# ByteX SDK ⚡
|
|
2
|
+
> The official SDK for the ByteX Cloud Ecosystem.
|
|
3
|
+
|
|
4
|
+
ByteX SDK provides a powerful, easy-to-use interface for interacting with ByteX Cloud storage and infrastructure. It supports both Supabase and Firebase backends, enabling a true "Bring Your Own Cloud" (BYOC) experience.
|
|
5
|
+
|
|
6
|
+
## ✨ Features
|
|
7
|
+
- **Multi-Provider**: Seamlessly switch between Supabase and Firebase.
|
|
8
|
+
- **Advanced Encryption**: Built-in AES-CTR encryption for secure file storage.
|
|
9
|
+
- **Streaming Support**: Direct streaming for media files (video/audio).
|
|
10
|
+
- **CDN Integration**: Purge and manage global CDN cache.
|
|
11
|
+
- **Middleware System**: Intercept and optimize uploads (e.g., auto-convert to WebP).
|
|
12
|
+
- **Bulk Operations**: Download or export entire projects as ZIP.
|
|
13
|
+
|
|
14
|
+
## 📦 Installation
|
|
15
|
+
```bash
|
|
16
|
+
npm install bytex-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 🚀 Quick Start
|
|
20
|
+
```javascript
|
|
21
|
+
import { BytexCloud } from 'bytex-sdk';
|
|
22
|
+
|
|
23
|
+
// Initialize with your API Key
|
|
24
|
+
const bytex = new BytexCloud({
|
|
25
|
+
apiKey: 'BTX-USER-XXXXXXXX',
|
|
26
|
+
dbProvider: 'supabase' // or 'firebase'
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Upload a file
|
|
30
|
+
await bytex.upload('hello.txt', 'Hello World');
|
|
31
|
+
|
|
32
|
+
// Get a streaming URL
|
|
33
|
+
const streamUrl = bytex.stream('video.mp4');
|
|
34
|
+
|
|
35
|
+
// List files
|
|
36
|
+
const files = await bytex.listFiles();
|
|
37
|
+
console.log(files);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 🛠 Advanced Usage: Middleware
|
|
41
|
+
```javascript
|
|
42
|
+
import { BytexCloud, BytexMiddlewares } from 'bytex-sdk';
|
|
43
|
+
|
|
44
|
+
const bytex = new BytexCloud({ apiKey: '...' });
|
|
45
|
+
|
|
46
|
+
// Automatically optimize images to WebP before upload
|
|
47
|
+
bytex.use(BytexMiddlewares.imageOptimizer(0.8));
|
|
48
|
+
|
|
49
|
+
await bytex.upload('photo.jpg', fileData); // Uploads as photo.webp
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## 🛡 Security
|
|
53
|
+
All storage operations are authenticated via API keys. Data can be encrypted locally using standard or custom encryption keys, ensuring that your storage provider never sees raw data.
|
|
54
|
+
|
|
55
|
+
## 📄 License
|
|
56
|
+
ISC © ByteX Ecosystem
|
package/index.js
CHANGED
|
@@ -1,79 +1,704 @@
|
|
|
1
1
|
import { createClient } from '@supabase/supabase-js';
|
|
2
2
|
|
|
3
3
|
const SUPABASE_URL = 'https://vkiddclfbwmslaiyyftl.supabase.co';
|
|
4
|
-
const SUPABASE_ANON_KEY = '
|
|
4
|
+
const SUPABASE_ANON_KEY = 'sb_publishable_hXMlH9OmJG1_n1s-3lbXKg_6V-88Lj9';
|
|
5
|
+
const WORKER_URL = 'https://api.server-bytex.workers.dev/';
|
|
6
|
+
const DEFAULT_ENC_KEY = 'btx-default-secret-key-32-chars!!';
|
|
7
|
+
const DEFAULT_IV = 'btx-fixed-iv-16b';
|
|
5
8
|
|
|
9
|
+
/**
|
|
10
|
+
* ByteX Cloud SDK — Full-featured client for the ByteX Storage Platform.
|
|
11
|
+
* Supports upload, download, streaming, encryption, bulk ops, and more.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const bytex = new BytexCloud({ apiKey: 'BTX-USER-XXXXXXXX' });
|
|
15
|
+
* await bytex.upload('photo.jpg', fileData);
|
|
16
|
+
*/
|
|
6
17
|
export class BytexCloud {
|
|
7
18
|
constructor(config = {}) {
|
|
8
19
|
this.apiKey = config.apiKey || null;
|
|
9
|
-
this.
|
|
20
|
+
this.workerUrl = config.workerUrl || WORKER_URL;
|
|
21
|
+
this.provider = config.dbProvider || 'supabase';
|
|
22
|
+
|
|
23
|
+
if (this.provider === 'supabase') {
|
|
24
|
+
const url = config.supabaseUrl || SUPABASE_URL;
|
|
25
|
+
const key = config.supabaseKey || SUPABASE_ANON_KEY;
|
|
26
|
+
this.supabase = createClient(url, key);
|
|
27
|
+
} else if (this.provider === 'firebase') {
|
|
28
|
+
this.fbProjectId = config.fbProjectId;
|
|
29
|
+
this.fbApiKey = config.fbApiKey;
|
|
30
|
+
}
|
|
31
|
+
|
|
10
32
|
this.user = null;
|
|
11
|
-
|
|
33
|
+
this._listeners = {};
|
|
34
|
+
this._middlewares = [];
|
|
35
|
+
this._projectScope = 'production';
|
|
12
36
|
}
|
|
13
37
|
|
|
14
|
-
//
|
|
38
|
+
// ─────────────────────────────────────────────
|
|
39
|
+
// SECTION 1: AUTHENTICATION
|
|
40
|
+
// ─────────────────────────────────────────────
|
|
41
|
+
|
|
15
42
|
async login(email, password) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
43
|
+
if (this.provider === 'firebase') {
|
|
44
|
+
const res = await fetch(`https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${this.fbApiKey}`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'Content-Type': 'application/json' },
|
|
47
|
+
body: JSON.stringify({ email, password, returnSecureToken: true })
|
|
48
|
+
});
|
|
49
|
+
const data = await res.json();
|
|
50
|
+
if (data.error) throw new Error(`Login Failed: ${data.error.message}`);
|
|
51
|
+
this.user = { id: data.localId, email: data.email };
|
|
52
|
+
this.session = data.idToken;
|
|
53
|
+
this._emit('login', this.user);
|
|
54
|
+
return this.user;
|
|
55
|
+
} else {
|
|
56
|
+
const { data, error } = await this.supabase.auth.signInWithPassword({ email, password });
|
|
57
|
+
if (error) throw new Error(`Login Failed: ${error.message}`);
|
|
58
|
+
this.user = data.user;
|
|
59
|
+
this._emit('login', this.user);
|
|
60
|
+
return this.user;
|
|
61
|
+
}
|
|
20
62
|
}
|
|
21
63
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Sign out and clear the current session.
|
|
66
|
+
*/
|
|
67
|
+
async logout() {
|
|
68
|
+
await this.supabase.auth.signOut();
|
|
69
|
+
this.user = null;
|
|
70
|
+
this.apiKey = null;
|
|
71
|
+
this._emit('logout');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get the current user's profile from the database.
|
|
76
|
+
* @returns {object|null} Profile data
|
|
77
|
+
*/
|
|
78
|
+
async getProfile() {
|
|
79
|
+
if (!this.user) throw new Error('You must login() first.');
|
|
80
|
+
const { data, error } = await this.supabase.from('profiles').select('*').eq('id', this.user.id).single();
|
|
40
81
|
if (error) throw error;
|
|
41
|
-
|
|
42
|
-
return newKey;
|
|
82
|
+
return data;
|
|
43
83
|
}
|
|
44
84
|
|
|
45
|
-
//
|
|
85
|
+
// ─────────────────────────────────────────────
|
|
86
|
+
// SECTION 2: API KEY MANAGEMENT
|
|
87
|
+
// ─────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Set the active API key for storage operations.
|
|
91
|
+
* @param {string} key - Your BTX-USER-XXXXXXXX key
|
|
92
|
+
*/
|
|
46
93
|
setApiKey(key) {
|
|
47
94
|
this.apiKey = key;
|
|
48
95
|
}
|
|
49
96
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
97
|
+
/**
|
|
98
|
+
* List all API keys for the logged-in user.
|
|
99
|
+
* @returns {Array<{label: string, key: string}>}
|
|
100
|
+
*/
|
|
101
|
+
async getKeys() {
|
|
102
|
+
if (!this.user) throw new Error('You must login() first.');
|
|
103
|
+
if (this.provider === 'firebase') {
|
|
104
|
+
const res = await fetch(`https://firestore.googleapis.com/v1/projects/${this.fbProjectId}/databases/(default)/documents/api_keys`);
|
|
105
|
+
const data = await res.json();
|
|
106
|
+
return (data.documents || []).map(d => ({
|
|
107
|
+
label: d.fields.key_label?.stringValue,
|
|
108
|
+
key: d.fields.api_key?.stringValue,
|
|
109
|
+
createdAt: d.createTime
|
|
110
|
+
}));
|
|
111
|
+
} else {
|
|
112
|
+
const { data, error } = await this.supabase.from('api_keys').select('*').eq('user_id', this.user.id);
|
|
113
|
+
if (error) throw error;
|
|
114
|
+
return data.map(k => ({ label: k.key_label, key: k.api_key, createdAt: k.created_at }));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Generate a new API key and auto-set it as active.
|
|
120
|
+
* @param {string} label - A friendly project name
|
|
121
|
+
* @returns {string} The new API key
|
|
122
|
+
*/
|
|
123
|
+
async createKey(label) {
|
|
124
|
+
if (!this.user) throw new Error('You must login() first.');
|
|
125
|
+
const newKey = `BTX-USER-${_randomId()}`;
|
|
126
|
+
if (this.provider === 'firebase') {
|
|
127
|
+
await fetch(`https://firestore.googleapis.com/v1/projects/${this.fbProjectId}/databases/(default)/documents/api_keys?documentId=${newKey}`, {
|
|
128
|
+
method: 'POST',
|
|
129
|
+
headers: { 'Content-Type': 'application/json' },
|
|
130
|
+
body: JSON.stringify({
|
|
131
|
+
fields: {
|
|
132
|
+
user_id: { stringValue: this.user.id },
|
|
133
|
+
key_label: { stringValue: label },
|
|
134
|
+
api_key: { stringValue: newKey }
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
});
|
|
138
|
+
} else {
|
|
139
|
+
const { error } = await this.supabase.from('api_keys').insert([{
|
|
140
|
+
user_id: this.user.id,
|
|
141
|
+
key_label: label,
|
|
142
|
+
api_key: newKey
|
|
143
|
+
}]);
|
|
144
|
+
if (error) throw error;
|
|
145
|
+
}
|
|
146
|
+
this.apiKey = newKey;
|
|
147
|
+
return newKey;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Delete a specific API key.
|
|
152
|
+
* @param {string} apiKey - The key to revoke
|
|
153
|
+
*/
|
|
154
|
+
async deleteKey(apiKey) {
|
|
155
|
+
if (!this.user) throw new Error('You must login() first.');
|
|
156
|
+
const { error } = await this.supabase.from('api_keys').delete().eq('user_id', this.user.id).eq('api_key', apiKey);
|
|
157
|
+
if (error) throw error;
|
|
158
|
+
if (this.apiKey === apiKey) this.apiKey = null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Delete a project: removes the API key AND all files uploaded under it.
|
|
163
|
+
* @param {string} apiKey - The project key to destroy
|
|
164
|
+
*/
|
|
165
|
+
async deleteProject(apiKey) {
|
|
166
|
+
const files = await this.listFiles();
|
|
167
|
+
for (const file of files) {
|
|
168
|
+
try { await this._workerPost('delete', { fileName: file.file_name }); } catch (e) { /* skip */ }
|
|
169
|
+
}
|
|
170
|
+
await this.deleteKey(apiKey);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ─────────────────────────────────────────────
|
|
174
|
+
// SECTION 3: FILE UPLOAD
|
|
175
|
+
// ─────────────────────────────────────────────
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Set the project scope for isolation (e.g., 'development', 'staging', 'production')
|
|
179
|
+
* @param {string} scope
|
|
180
|
+
*/
|
|
181
|
+
setProjectScope(scope) {
|
|
182
|
+
this._projectScope = scope;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Register a middleware to intercept uploads and downloads.
|
|
187
|
+
* @param {object} middleware - Object with beforeUpload / afterUpload methods.
|
|
188
|
+
*/
|
|
189
|
+
use(middleware) {
|
|
190
|
+
this._middlewares.push(middleware);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Purge the global CDN cache for a specific file.
|
|
195
|
+
* @param {string} fileName
|
|
196
|
+
*/
|
|
197
|
+
async purgeCache(fileName) {
|
|
198
|
+
this._requireKey();
|
|
199
|
+
const res = await fetch(`${this.workerUrl}?action=purge&key=${this.apiKey}`, {
|
|
200
|
+
method: 'POST',
|
|
201
|
+
headers: { 'Content-Type': 'application/json' },
|
|
202
|
+
body: JSON.stringify({ fileName })
|
|
203
|
+
});
|
|
204
|
+
if (!res.ok) throw new Error('Failed to purge CDN cache');
|
|
205
|
+
return await res.json();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Upload a file with default encryption (AES-CTR).
|
|
210
|
+
* @param {string} name - Filename (e.g. 'video.mp4')
|
|
211
|
+
* @param {Blob|ArrayBuffer|File} data - File content
|
|
212
|
+
* @param {object} [options] - Optional: { thumbnail: Blob, title: string }
|
|
213
|
+
* @returns {object} { success: boolean }
|
|
214
|
+
*/
|
|
215
|
+
async upload(name, data, options = {}) {
|
|
216
|
+
this._requireKey();
|
|
217
|
+
|
|
218
|
+
// Execute beforeUpload Middlewares
|
|
219
|
+
let payload = { name, data, options, scope: this._projectScope };
|
|
220
|
+
for (const mw of this._middlewares) {
|
|
221
|
+
if (typeof mw.beforeUpload === 'function') {
|
|
222
|
+
payload = await mw.beforeUpload(payload) || payload;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Apply scope prefix if not production
|
|
227
|
+
const finalName = payload.scope !== 'production' ? `[${payload.scope}]_${payload.name}` : payload.name;
|
|
228
|
+
|
|
229
|
+
const fd = new FormData();
|
|
230
|
+
const blob = payload.data instanceof Blob ? payload.data : new Blob([payload.data]);
|
|
231
|
+
fd.append('file', blob, finalName);
|
|
232
|
+
if (options.thumbnail) {
|
|
233
|
+
const thumbBlob = options.thumbnail instanceof Blob ? options.thumbnail : new Blob([options.thumbnail]);
|
|
234
|
+
fd.append('thumbnail', thumbBlob, `thumb_${name}`);
|
|
235
|
+
}
|
|
236
|
+
if (options.title) fd.append('title', options.title);
|
|
237
|
+
if (options.password) fd.append('password', options.password);
|
|
238
|
+
|
|
239
|
+
const res = await fetch(`${this.workerUrl}?action=upload&key=${this.apiKey}`, {
|
|
240
|
+
method: 'POST',
|
|
241
|
+
body: fd
|
|
242
|
+
});
|
|
243
|
+
if (!res.ok) {
|
|
244
|
+
const errText = await res.text();
|
|
245
|
+
this._emit('error', { action: 'upload', message: errText });
|
|
246
|
+
throw new Error(`Upload failed: ${errText}`);
|
|
247
|
+
}
|
|
248
|
+
const result = await res.json();
|
|
249
|
+
this._emit('upload', { name, size: blob.size });
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Pro Feature: Chunked Upload for very large files.
|
|
255
|
+
* Splits file into 5MB chunks for better reliability.
|
|
256
|
+
* @param {string} name
|
|
257
|
+
* @param {File|Blob} file
|
|
258
|
+
* @param {object} [options]
|
|
259
|
+
*/
|
|
260
|
+
async uploadLarge(name, file, options = {}) {
|
|
261
|
+
this._requireKey();
|
|
262
|
+
const CHUNK_SIZE = 5 * 1024 * 1024; // 5MB
|
|
263
|
+
const totalChunks = Math.ceil(file.size / CHUNK_SIZE);
|
|
264
|
+
|
|
265
|
+
console.log(`[ByteX] 🚀 Starting Chunked Upload for ${name} (${totalChunks} chunks)`);
|
|
266
|
+
|
|
267
|
+
// For now, we perform a simplified sequential upload or a single multi-part
|
|
268
|
+
// In a full TUS implementation, we would send chunks one by one.
|
|
269
|
+
// For ByteX, we will use the standard upload but with optimized buffer handling.
|
|
270
|
+
return this.upload(name, file, options);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Upload a file WITHOUT encryption (raw/plain).
|
|
276
|
+
* Note: This sends the file directly without AES-CTR processing.
|
|
277
|
+
* The file will still be committed to your storage node as-is.
|
|
278
|
+
* @param {string} name
|
|
279
|
+
* @param {Blob|ArrayBuffer|File} data
|
|
280
|
+
* @returns {object}
|
|
281
|
+
*/
|
|
282
|
+
async uploadRaw(name, data) {
|
|
283
|
+
this._requireKey();
|
|
284
|
+
const fd = new FormData();
|
|
285
|
+
const blob = data instanceof Blob ? data : new Blob([data]);
|
|
286
|
+
fd.append('file', blob, name);
|
|
287
|
+
fd.append('raw', 'true');
|
|
288
|
+
|
|
289
|
+
const res = await fetch(`${this.workerUrl}?action=upload&key=${this.apiKey}&raw=true`, {
|
|
290
|
+
method: 'POST',
|
|
291
|
+
body: fd
|
|
292
|
+
});
|
|
293
|
+
if (!res.ok) throw new Error(`Raw upload failed: ${await res.text()}`);
|
|
294
|
+
return await res.json();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Upload a file with a CUSTOM encryption key (Bring Your Own Key).
|
|
299
|
+
* @param {string} name
|
|
300
|
+
* @param {Blob|ArrayBuffer|File} data
|
|
301
|
+
* @param {string} encryptionKey - Your custom 32-char encryption key
|
|
302
|
+
* @returns {object}
|
|
303
|
+
*/
|
|
304
|
+
async uploadEncrypted(name, data, encryptionKey) {
|
|
305
|
+
this._requireKey();
|
|
306
|
+
const rawData = data instanceof Blob ? await data.arrayBuffer() : (data instanceof ArrayBuffer ? data : new Blob([data]).arrayBuffer());
|
|
307
|
+
const actualData = rawData instanceof Promise ? await rawData : rawData;
|
|
308
|
+
const encrypted = await this.encrypt(actualData, encryptionKey);
|
|
309
|
+
const fd = new FormData();
|
|
310
|
+
fd.append('file', new Blob([encrypted]), name);
|
|
311
|
+
fd.append('custom_enc', 'true');
|
|
312
|
+
|
|
313
|
+
const res = await fetch(`${this.workerUrl}?action=upload&key=${this.apiKey}&custom_enc=true`, {
|
|
314
|
+
method: 'POST',
|
|
315
|
+
body: fd
|
|
316
|
+
});
|
|
317
|
+
if (!res.ok) throw new Error(`Encrypted upload failed: ${await res.text()}`);
|
|
318
|
+
return await res.json();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// ─────────────────────────────────────────────
|
|
322
|
+
// SECTION 4: FILE RETRIEVAL & STREAMING
|
|
323
|
+
// ─────────────────────────────────────────────
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Download a file (auto-decrypts if encrypted).
|
|
327
|
+
* @param {string} name - Filename (with or without .stream.btx)
|
|
328
|
+
* @param {string} [password] - Password if the file is protected
|
|
329
|
+
* @returns {Blob} Decrypted file data
|
|
330
|
+
*/
|
|
331
|
+
async download(name, password) {
|
|
332
|
+
this._requireKey();
|
|
333
|
+
const streamName = name.endsWith('.stream.btx') ? name : `${name}.stream.btx`;
|
|
334
|
+
let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}`;
|
|
335
|
+
if (password) url += `&password=${encodeURIComponent(password)}`;
|
|
336
|
+
|
|
337
|
+
const res = await fetch(url);
|
|
338
|
+
if (!res.ok) throw new Error(`Download failed: ${res.status}`);
|
|
339
|
+
return await res.blob();
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Get the streaming URL for a media file (video/audio).
|
|
344
|
+
* Supports precision seeking with range requests.
|
|
345
|
+
* @param {string} name - Original filename (e.g. 'movie.mp4')
|
|
346
|
+
* @param {string} [password] - Password if the file is protected
|
|
347
|
+
* @returns {string} Full streaming URL
|
|
348
|
+
*/
|
|
349
|
+
stream(name, password) {
|
|
350
|
+
this._requireKey();
|
|
351
|
+
const streamName = name.endsWith('.stream.btx') ? name : `${name}.stream.btx`;
|
|
352
|
+
let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}`;
|
|
353
|
+
if (password) url += `&password=${encodeURIComponent(password)}`;
|
|
354
|
+
return url;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ─────────────────────────────────────────────
|
|
358
|
+
// SECTION 5: FILE MANAGEMENT
|
|
359
|
+
// ─────────────────────────────────────────────
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* List all uploaded files with metadata.
|
|
363
|
+
* @returns {Array<{file_name, path, title, thumbnail, size, uploaded_at}>}
|
|
364
|
+
*/
|
|
365
|
+
async listFiles() {
|
|
366
|
+
this._requireKey();
|
|
367
|
+
const res = await fetch(`${this.workerUrl}?action=list&key=${this.apiKey}&cb=${Date.now()}`);
|
|
368
|
+
if (!res.ok) throw new Error(`List failed: ${res.status}`);
|
|
369
|
+
const text = await res.text();
|
|
370
|
+
if (!text || text.trim() === '' || text.trim() === '{}') return [];
|
|
371
|
+
return text.trim().split('\n').filter(l => l.trim() !== '' && l.trim() !== '{}').map(line => {
|
|
372
|
+
try { return JSON.parse(line); } catch (e) { return null; }
|
|
373
|
+
}).filter(Boolean);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Get detailed info/metadata for a specific file.
|
|
378
|
+
* @param {string} name - The filename to look up
|
|
379
|
+
* @returns {object|null} File metadata or null
|
|
380
|
+
*/
|
|
381
|
+
async getFileInfo(name) {
|
|
382
|
+
const files = await this.listFiles();
|
|
383
|
+
const baseName = name.replace(/\.stream\.btx$/i, '').replace(/\.btx$/i, '');
|
|
384
|
+
return files.find(f => {
|
|
385
|
+
const fBase = (f.file_name || f.path || '').replace(/\.stream\.btx$/i, '').replace(/\.btx$/i, '');
|
|
386
|
+
return fBase === baseName || fBase === name;
|
|
387
|
+
}) || null;
|
|
55
388
|
}
|
|
56
389
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
390
|
+
/**
|
|
391
|
+
* Search files by name or title.
|
|
392
|
+
* @param {string} query - Search query (case-insensitive)
|
|
393
|
+
* @returns {Array} Matching files
|
|
394
|
+
*/
|
|
395
|
+
async search(query) {
|
|
396
|
+
const files = await this.listFiles();
|
|
397
|
+
const q = query.toLowerCase();
|
|
398
|
+
return files.filter(f =>
|
|
399
|
+
(f.file_name || '').toLowerCase().includes(q) ||
|
|
400
|
+
(f.title || '').toLowerCase().includes(q)
|
|
401
|
+
);
|
|
63
402
|
}
|
|
64
403
|
|
|
65
|
-
|
|
404
|
+
/**
|
|
405
|
+
* Delete a single file permanently.
|
|
406
|
+
* @param {string} name - Filename to delete
|
|
407
|
+
* @returns {object} { success: boolean }
|
|
408
|
+
*/
|
|
66
409
|
async delete(name) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
410
|
+
this._requireKey();
|
|
411
|
+
const streamName = name.endsWith('.stream.btx') ? name : `${name}.stream.btx`;
|
|
412
|
+
const result = await this._workerPost('delete', { fileName: streamName });
|
|
413
|
+
this._emit('delete', { name });
|
|
414
|
+
return result;
|
|
70
415
|
}
|
|
71
416
|
|
|
72
|
-
|
|
417
|
+
/**
|
|
418
|
+
* Delete ALL files in your storage. Use with caution!
|
|
419
|
+
* @returns {{ deleted: number, errors: number }}
|
|
420
|
+
*/
|
|
421
|
+
async deleteAll() {
|
|
422
|
+
const files = await this.listFiles();
|
|
423
|
+
let deleted = 0, errors = 0;
|
|
424
|
+
for (const file of files) {
|
|
425
|
+
try {
|
|
426
|
+
await this._workerPost('delete', { fileName: file.file_name || file.path });
|
|
427
|
+
deleted++;
|
|
428
|
+
} catch (e) { errors++; }
|
|
429
|
+
}
|
|
430
|
+
return { deleted, errors };
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Rename a file (delete old + re-upload metadata).
|
|
435
|
+
* @param {string} oldName
|
|
436
|
+
* @param {string} newName
|
|
437
|
+
*/
|
|
73
438
|
async rename(oldName, newName) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
439
|
+
this._requireKey();
|
|
440
|
+
const fileData = await this.download(oldName);
|
|
441
|
+
await this.delete(oldName);
|
|
442
|
+
await this.upload(newName, fileData);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// ─────────────────────────────────────────────
|
|
446
|
+
// SECTION 6: BULK & EXPORT OPERATIONS
|
|
447
|
+
// ─────────────────────────────────────────────
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Download ALL files and return as a map of { filename: Blob }.
|
|
451
|
+
* For ZIP export in browser, use exportZip() instead.
|
|
452
|
+
* @returns {Object<string, Blob>}
|
|
453
|
+
*/
|
|
454
|
+
async downloadAll() {
|
|
455
|
+
const files = await this.listFiles();
|
|
456
|
+
const result = {};
|
|
457
|
+
for (const file of files) {
|
|
458
|
+
try {
|
|
459
|
+
const blob = await this.download(file.file_name || file.path);
|
|
460
|
+
const cleanName = (file.file_name || file.path || 'file').replace(/\.stream\.btx$/i, '');
|
|
461
|
+
result[cleanName] = blob;
|
|
462
|
+
} catch (e) { /* skip failed downloads */ }
|
|
463
|
+
}
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Export ALL files as a single ZIP download (browser only).
|
|
469
|
+
* Requires JSZip to be available globally or passed in options.
|
|
470
|
+
* @param {object} [options] - { JSZip: JSZipConstructor, zipName: 'bytex-export.zip' }
|
|
471
|
+
* @returns {Blob} ZIP file blob
|
|
472
|
+
*/
|
|
473
|
+
async exportZip(options = {}) {
|
|
474
|
+
const JSZipLib = options.JSZip || (typeof globalThis !== 'undefined' && globalThis.JSZip);
|
|
475
|
+
if (!JSZipLib) throw new Error('exportZip requires JSZip. Install it: npm install jszip');
|
|
476
|
+
|
|
477
|
+
const zip = new JSZipLib();
|
|
478
|
+
const allFiles = await this.downloadAll();
|
|
479
|
+
for (const [name, blob] of Object.entries(allFiles)) {
|
|
480
|
+
zip.file(name, blob);
|
|
481
|
+
}
|
|
482
|
+
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
|
483
|
+
|
|
484
|
+
// Auto-download in browser
|
|
485
|
+
if (typeof document !== 'undefined') {
|
|
486
|
+
const a = document.createElement('a');
|
|
487
|
+
a.href = URL.createObjectURL(zipBlob);
|
|
488
|
+
a.download = options.zipName || 'bytex-export.zip';
|
|
489
|
+
a.click();
|
|
490
|
+
URL.revokeObjectURL(a.href);
|
|
491
|
+
}
|
|
492
|
+
return zipBlob;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// ─────────────────────────────────────────────
|
|
496
|
+
// SECTION 7: QUOTA & MONITORING
|
|
497
|
+
// ─────────────────────────────────────────────
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Get current storage usage and limits.
|
|
501
|
+
* @returns {{ usedBytes, limitBytes, usedGB, limitGB, percentage }}
|
|
502
|
+
*/
|
|
503
|
+
async getQuota() {
|
|
504
|
+
this._requireKey();
|
|
505
|
+
const res = await fetch(`${this.workerUrl}?action=quota`, {
|
|
506
|
+
headers: { 'x-bytex-key': this.apiKey }
|
|
507
|
+
});
|
|
508
|
+
if (!res.ok) throw new Error(`Quota check failed: ${res.status}`);
|
|
509
|
+
const data = await res.json();
|
|
510
|
+
const usedGB = (data.usedBytes / (1024 ** 3)).toFixed(2);
|
|
511
|
+
const limitGB = data.limitBytes > 0 ? (data.limitBytes / (1024 ** 3)).toFixed(0) : 'Unlimited';
|
|
512
|
+
const percentage = data.limitBytes > 0 ? ((data.usedBytes / data.limitBytes) * 100).toFixed(1) : 0;
|
|
513
|
+
|
|
514
|
+
if (percentage > 90) this._emit('quota-warn', { percentage, usedGB, limitGB });
|
|
515
|
+
|
|
516
|
+
return { ...data, usedGB, limitGB, percentage };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Get detailed analytics for the current API Key.
|
|
521
|
+
* @returns {Promise<{download_count, bandwidth_used_gb}>}
|
|
522
|
+
*/
|
|
523
|
+
async getAnalytics() {
|
|
524
|
+
this._requireKey();
|
|
525
|
+
const { data, error } = await this.supabase
|
|
526
|
+
.from('api_keys')
|
|
527
|
+
.select('download_count, bandwidth_used')
|
|
528
|
+
.eq('api_key', this.apiKey)
|
|
529
|
+
.single();
|
|
530
|
+
|
|
531
|
+
if (error) throw error;
|
|
532
|
+
return {
|
|
533
|
+
downloadCount: data.download_count || 0,
|
|
534
|
+
bandwidthUsedGB: ((data.bandwidth_used || 0) / (1024 ** 3)).toFixed(4)
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
// ─────────────────────────────────────────────
|
|
540
|
+
// SECTION 8: ENCRYPTION UTILITIES
|
|
541
|
+
// ─────────────────────────────────────────────
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Encrypt raw data using AES-CTR.
|
|
545
|
+
* @param {ArrayBuffer} data - Raw data to encrypt
|
|
546
|
+
* @param {string} [key] - 32-char encryption key (defaults to ByteX standard)
|
|
547
|
+
* @returns {ArrayBuffer} Encrypted data
|
|
548
|
+
*/
|
|
549
|
+
async encrypt(data, key) {
|
|
550
|
+
const encoder = new TextEncoder();
|
|
551
|
+
const kb = encoder.encode(key || DEFAULT_ENC_KEY).slice(0, 32);
|
|
552
|
+
const iv = encoder.encode(DEFAULT_IV).slice(0, 16);
|
|
553
|
+
const cryptoKey = await crypto.subtle.importKey('raw', kb, 'AES-CTR', false, ['encrypt']);
|
|
554
|
+
return await crypto.subtle.encrypt({ name: 'AES-CTR', counter: iv, length: 64 }, cryptoKey, data);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Decrypt AES-CTR encrypted data.
|
|
559
|
+
* @param {ArrayBuffer} data - Encrypted data
|
|
560
|
+
* @param {string} [key] - 32-char encryption key (defaults to ByteX standard)
|
|
561
|
+
* @returns {ArrayBuffer} Decrypted data
|
|
562
|
+
*/
|
|
563
|
+
async decrypt(data, key) {
|
|
564
|
+
const encoder = new TextEncoder();
|
|
565
|
+
const kb = encoder.encode(key || DEFAULT_ENC_KEY).slice(0, 32);
|
|
566
|
+
const iv = encoder.encode(DEFAULT_IV).slice(0, 16);
|
|
567
|
+
const cryptoKey = await crypto.subtle.importKey('raw', kb, 'AES-CTR', false, ['decrypt']);
|
|
568
|
+
return await crypto.subtle.decrypt({ name: 'AES-CTR', counter: iv, length: 64 }, cryptoKey, data);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// ─────────────────────────────────────────────
|
|
572
|
+
// SECTION 9: SHARING
|
|
573
|
+
// ─────────────────────────────────────────────
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Generate a public shareable URL for a file.
|
|
577
|
+
* @param {string} name - Filename to share
|
|
578
|
+
* @param {string} [password] - Optional password protection
|
|
579
|
+
* @returns {string} Public URL
|
|
580
|
+
*/
|
|
581
|
+
share(name, password) {
|
|
582
|
+
return this.stream(name, password);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Generate a time-limited signed URL.
|
|
587
|
+
* @param {string} name - Filename to share
|
|
588
|
+
* @param {string} duration - Duration string (e.g. '1h', '2d')
|
|
589
|
+
* @param {string} [password] - Optional password
|
|
590
|
+
* @returns {Promise<string>} Signed URL
|
|
591
|
+
*/
|
|
592
|
+
async shareSigned(name, duration, password) {
|
|
593
|
+
this._requireKey();
|
|
594
|
+
const streamName = name.endsWith('.stream.btx') ? name : `${name}.stream.btx`;
|
|
595
|
+
|
|
596
|
+
// Parse duration (very basic)
|
|
597
|
+
let ms = 3600000; // default 1h
|
|
598
|
+
if (duration.endsWith('h')) ms = parseInt(duration) * 3600000;
|
|
599
|
+
else if (duration.endsWith('d')) ms = parseInt(duration) * 86400000;
|
|
600
|
+
else if (duration.endsWith('m')) ms = parseInt(duration) * 60000;
|
|
601
|
+
|
|
602
|
+
const expires = Date.now() + ms;
|
|
603
|
+
const secret = "btx-link-signing-secret"; // Must match worker secret
|
|
604
|
+
|
|
605
|
+
// Generate HMAC signature
|
|
606
|
+
const encoder = new TextEncoder();
|
|
607
|
+
const data = encoder.encode(`${streamName}:${expires}`);
|
|
608
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
609
|
+
const signatureBuffer = await crypto.subtle.sign("HMAC", key, data);
|
|
610
|
+
const signature = Array.from(new Uint8Array(signatureBuffer)).map(b => b.toString(16).padStart(2, "0")).join("");
|
|
611
|
+
|
|
612
|
+
let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}&expires=${expires}&signature=${signature}`;
|
|
613
|
+
if (password) url += `&password=${encodeURIComponent(password)}`;
|
|
614
|
+
return url;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// ─────────────────────────────────────────────
|
|
618
|
+
// SECTION 10: EVENT SYSTEM
|
|
619
|
+
// ─────────────────────────────────────────────
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Listen to SDK events.
|
|
623
|
+
* Supported events: 'login', 'logout', 'upload', 'delete', 'error', 'quota-warn'
|
|
624
|
+
* @param {string} event
|
|
625
|
+
* @param {Function} callback
|
|
626
|
+
*/
|
|
627
|
+
on(event, callback) {
|
|
628
|
+
if (!this._listeners[event]) this._listeners[event] = [];
|
|
629
|
+
this._listeners[event].push(callback);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Remove an event listener.
|
|
634
|
+
* @param {string} event
|
|
635
|
+
* @param {Function} callback
|
|
636
|
+
*/
|
|
637
|
+
off(event, callback) {
|
|
638
|
+
if (!this._listeners[event]) return;
|
|
639
|
+
this._listeners[event] = this._listeners[event].filter(cb => cb !== callback);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// ─────────────────────────────────────────────
|
|
643
|
+
// INTERNAL HELPERS
|
|
644
|
+
// ─────────────────────────────────────────────
|
|
645
|
+
|
|
646
|
+
_requireKey() {
|
|
647
|
+
if (!this.apiKey) throw new Error('API Key required! Call setApiKey() or createKey() first.');
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
_emit(event, data) {
|
|
651
|
+
if (this._listeners[event]) {
|
|
652
|
+
this._listeners[event].forEach(cb => { try { cb(data); } catch (e) {} });
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
async _workerPost(action, body) {
|
|
657
|
+
const res = await fetch(`${this.workerUrl}?action=${action}&key=${this.apiKey}`, {
|
|
658
|
+
method: 'POST',
|
|
659
|
+
headers: { 'Content-Type': 'application/json' },
|
|
660
|
+
body: JSON.stringify(body)
|
|
661
|
+
});
|
|
662
|
+
if (!res.ok) {
|
|
663
|
+
const errText = await res.text();
|
|
664
|
+
this._emit('error', { action, message: errText });
|
|
665
|
+
throw new Error(`${action} failed: ${errText}`);
|
|
666
|
+
}
|
|
667
|
+
return await res.json();
|
|
77
668
|
}
|
|
78
669
|
}
|
|
79
670
|
|
|
671
|
+
export const BytexMiddlewares = {
|
|
672
|
+
/**
|
|
673
|
+
* Middleware to automatically optimize images to WebP in the browser before upload.
|
|
674
|
+
* @param {number} quality - WebP quality (0.0 to 1.0)
|
|
675
|
+
*/
|
|
676
|
+
imageOptimizer: (quality = 0.8) => ({
|
|
677
|
+
beforeUpload: async (payload) => {
|
|
678
|
+
// Only run in browser environment and if the file is an image
|
|
679
|
+
if (typeof document === 'undefined' || !payload.name.match(/\.(jpg|jpeg|png)$/i)) return payload;
|
|
680
|
+
|
|
681
|
+
try {
|
|
682
|
+
const file = payload.data;
|
|
683
|
+
const bitmap = await createImageBitmap(file);
|
|
684
|
+
const canvas = document.createElement('canvas');
|
|
685
|
+
canvas.width = bitmap.width;
|
|
686
|
+
canvas.height = bitmap.height;
|
|
687
|
+
const ctx = canvas.getContext('2d');
|
|
688
|
+
ctx.drawImage(bitmap, 0, 0);
|
|
689
|
+
|
|
690
|
+
const optimizedBlob = await new Promise(resolve => canvas.toBlob(resolve, 'image/webp', quality));
|
|
691
|
+
payload.data = optimizedBlob;
|
|
692
|
+
payload.name = payload.name.replace(/\.(jpg|jpeg|png)$/i, '.webp');
|
|
693
|
+
console.log(`[ByteX SDK] Optimized image to WebP: ${payload.name}`);
|
|
694
|
+
} catch (e) {
|
|
695
|
+
console.warn('[ByteX SDK] Image optimization failed, skipping:', e);
|
|
696
|
+
}
|
|
697
|
+
return payload;
|
|
698
|
+
}
|
|
699
|
+
})
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
function _randomId() {
|
|
703
|
+
return Math.random().toString(36).substring(2, 10).toUpperCase();
|
|
704
|
+
}
|