bytex-sdk 1.7.6 → 1.8.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/index.js +32 -7
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -361,13 +361,15 @@ export class BytexCloud {
|
|
|
361
361
|
this._requireKey();
|
|
362
362
|
// ByteX Worker always expects .stream.btx for action=view
|
|
363
363
|
const streamName = name.endsWith('.stream.btx') ? name : `${name}.stream.btx`;
|
|
364
|
-
let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}`;
|
|
364
|
+
let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}&_cb=${Date.now()}`;
|
|
365
365
|
if (password) url += `&password=${encodeURIComponent(password)}`;
|
|
366
366
|
|
|
367
367
|
const { data: { session } } = await this.supabase.auth.getSession();
|
|
368
368
|
const res = await fetch(url, {
|
|
369
369
|
headers: {
|
|
370
|
-
'Authorization': `Bearer ${session?.access_token || ''}
|
|
370
|
+
'Authorization': `Bearer ${session?.access_token || ''}`,
|
|
371
|
+
'Cache-Control': 'no-cache',
|
|
372
|
+
'Pragma': 'no-cache'
|
|
371
373
|
}
|
|
372
374
|
});
|
|
373
375
|
if (!res.ok) throw new Error(`Download failed: ${res.status}`);
|
|
@@ -375,14 +377,33 @@ export class BytexCloud {
|
|
|
375
377
|
}
|
|
376
378
|
|
|
377
379
|
/**
|
|
378
|
-
* Helper to download JSON data directly from cloud.
|
|
380
|
+
* Helper to download JSON or NDJSON data directly from cloud.
|
|
381
|
+
* Compatible with React Native (uses FileReader).
|
|
379
382
|
* @param {string} name
|
|
380
|
-
* @returns {object}
|
|
383
|
+
* @returns {object|Array}
|
|
381
384
|
*/
|
|
382
385
|
async downloadData(name) {
|
|
383
386
|
const blob = await this.download(name);
|
|
384
|
-
|
|
385
|
-
return
|
|
387
|
+
|
|
388
|
+
return new Promise((resolve, reject) => {
|
|
389
|
+
const reader = new FileReader();
|
|
390
|
+
reader.onload = () => {
|
|
391
|
+
const text = reader.result;
|
|
392
|
+
try {
|
|
393
|
+
// Detect NDJSON (multiple lines of JSON)
|
|
394
|
+
if (text.includes('\n') && text.trim().startsWith('{')) {
|
|
395
|
+
const lines = text.trim().split('\n');
|
|
396
|
+
resolve(lines.map(l => JSON.parse(l)));
|
|
397
|
+
} else {
|
|
398
|
+
resolve(JSON.parse(text));
|
|
399
|
+
}
|
|
400
|
+
} catch (e) {
|
|
401
|
+
reject(new Error("Failed to parse Cloud Data (Invalid JSON/NDJSON)"));
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
reader.onerror = () => reject(new Error("Failed to read Cloud Blob"));
|
|
405
|
+
reader.readAsText(blob);
|
|
406
|
+
});
|
|
386
407
|
}
|
|
387
408
|
|
|
388
409
|
/**
|
|
@@ -715,9 +736,13 @@ export class BytexCloud {
|
|
|
715
736
|
}
|
|
716
737
|
|
|
717
738
|
async _workerPost(action, body) {
|
|
739
|
+
const { data: { session } } = await this.supabase.auth.getSession();
|
|
718
740
|
const res = await fetch(`${this.workerUrl}?action=${action}&key=${this.apiKey}`, {
|
|
719
741
|
method: 'POST',
|
|
720
|
-
headers: {
|
|
742
|
+
headers: {
|
|
743
|
+
'Content-Type': 'application/json',
|
|
744
|
+
'Authorization': `Bearer ${session?.access_token || ''}`
|
|
745
|
+
},
|
|
721
746
|
body: JSON.stringify(body)
|
|
722
747
|
});
|
|
723
748
|
if (!res.ok) {
|